blob: dfbb661eb68afd4690445a1d5fd6e4df8a8ccc9f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Anton Sharonov3f026672022-07-26 21:26:18 +010032// values for "dead_key"
33#define DEAD_KEY_OFF 0 // no dead key
34#define DEAD_KEY_SET_DEFAULT 1 // dead key pressed
35#define DEAD_KEY_TRANSIENT_IN_ON_CHAR 2 // wait for next key press
36#define DEAD_KEY_SKIP_ON_CHAR 3 // skip next _OnChar()
37
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010038#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020039static DWriteContext *s_dwc = NULL;
40static int s_directx_enabled = 0;
41static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010042# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010043static int directx_enabled(void);
44static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010045#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020046
Bram Moolenaar065bbac2016-02-20 13:08:46 +010047#ifdef FEAT_MENU
48static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000049#else
50# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010051#endif
52
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
54 int
55gui_mch_set_rendering_options(char_u *s)
56{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010057# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020058 char_u *p, *q;
59
60 int dx_enable = 0;
61 int dx_flags = 0;
62 float dx_gamma = 0.0f;
63 float dx_contrast = 0.0f;
64 float dx_level = 0.0f;
65 int dx_geom = 0;
66 int dx_renmode = 0;
67 int dx_taamode = 0;
68
Bram Moolenaar734a8672019-12-02 22:49:38 +010069 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020070 for (p = s; p != NULL && *p != NUL; )
71 {
72 char_u item[256];
73 char_u name[128];
74 char_u value[128];
75
Bram Moolenaarcde88542015-08-11 19:14:00 +020076 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020077 if (p == NULL)
78 break;
79 q = &item[0];
80 copy_option_part(&q, name, sizeof(name), ":");
81 if (q == NULL)
82 return FAIL;
83 copy_option_part(&q, value, sizeof(value), ":");
84
85 if (STRCMP(name, "type") == 0)
86 {
87 if (STRCMP(value, "directx") == 0)
88 dx_enable = 1;
89 else
90 return FAIL;
91 }
92 else if (STRCMP(name, "gamma") == 0)
93 {
94 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "contrast") == 0)
98 {
99 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "level") == 0)
103 {
104 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 }
107 else if (STRCMP(name, "geom") == 0)
108 {
109 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100110 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200111 if (dx_geom < 0 || dx_geom > 2)
112 return FAIL;
113 }
114 else if (STRCMP(name, "renmode") == 0)
115 {
116 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100117 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200118 if (dx_renmode < 0 || dx_renmode > 6)
119 return FAIL;
120 }
121 else if (STRCMP(name, "taamode") == 0)
122 {
123 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100124 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200125 if (dx_taamode < 0 || dx_taamode > 3)
126 return FAIL;
127 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100128 else if (STRCMP(name, "scrlines") == 0)
129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100130 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100131 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 else
133 return FAIL;
134 }
135
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100136 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100137 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100138
Bram Moolenaar734a8672019-12-02 22:49:38 +0100139 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200140 if (dx_enable)
141 {
142 if (!directx_enabled())
143 return FAIL;
144 DWriteContext_SetRenderingParams(s_dwc, NULL);
145 if (dx_flags)
146 {
147 DWriteRenderingParams param;
148 DWriteContext_GetRenderingParams(s_dwc, &param);
149 if (dx_flags & (1 << 0))
150 param.gamma = dx_gamma;
151 if (dx_flags & (1 << 1))
152 param.enhancedContrast = dx_contrast;
153 if (dx_flags & (1 << 2))
154 param.clearTypeLevel = dx_level;
155 if (dx_flags & (1 << 3))
156 param.pixelGeometry = dx_geom;
157 if (dx_flags & (1 << 4))
158 param.renderingMode = dx_renmode;
159 if (dx_flags & (1 << 5))
160 param.textAntialiasMode = dx_taamode;
161 DWriteContext_SetRenderingParams(s_dwc, &param);
162 }
163 }
164 s_directx_enabled = dx_enable;
165
166 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100167# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200168 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100169# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200170}
171#endif
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
174 * These are new in Windows ME/XP, only defined in recent compilers.
175 */
176#ifndef HANDLE_WM_XBUTTONUP
177# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180#ifndef HANDLE_WM_XBUTTONDOWN
181# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
182 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
183#endif
184#ifndef HANDLE_WM_XBUTTONDBLCLK
185# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
186 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
187#endif
188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100189
Bram Moolenaar734a8672019-12-02 22:49:38 +0100190#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100191#ifdef DEBUG
192# include <tchar.h>
193#endif
194
Bram Moolenaar734a8672019-12-02 22:49:38 +0100195// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100196#ifndef PROTO
197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifndef __MINGW32__
199# include <shellapi.h>
200# endif
K.Takata2da11a42022-09-10 13:03:12 +0100201# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100202# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000218#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
K.Takatac81e9bf2022-01-16 14:15:49 +0000225#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100226# define WM_DPICHANGED 0x02E0
227#endif
228
229#ifndef WM_MOUSEHWHEEL
230# define WM_MOUSEHWHEEL 0x020E
231#endif
232
233#ifndef SPI_GETWHEELSCROLLCHARS
234# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000235#endif
236
LemonBoyc27747e2022-05-07 12:25:40 +0100237#ifndef SPI_SETWHEELSCROLLCHARS
238# define SPI_SETWHEELSCROLLCHARS 0x006D
239#endif
240
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100243 * Define a few things for generating prototypes. This is just to avoid
244 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100246# define APIENTRY
247# define CALLBACK
248# define CONST
249# define FAR
250# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200251# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200252# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100253# define _cdecl
254typedef int BOOL;
255typedef int BYTE;
256typedef int DWORD;
257typedef int WCHAR;
258typedef int ENUMLOGFONT;
259typedef int FINDREPLACE;
260typedef int HANDLE;
261typedef int HBITMAP;
262typedef int HBRUSH;
263typedef int HDROP;
264typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100265typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100266typedef int LPARAM;
267typedef int LPCREATESTRUCT;
268typedef int LPCSTR;
269typedef int LPCTSTR;
270typedef int LPRECT;
271typedef int LPSTR;
272typedef int LPWINDOWPOS;
273typedef int LPWORD;
274typedef int LRESULT;
275typedef int HRESULT;
276# undef MSG
277typedef int MSG;
278typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100279typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100280typedef int OSVERSIONINFO;
281typedef int PWORD;
282typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100283typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284typedef int UINT;
285typedef int WORD;
286typedef int WPARAM;
287typedef int POINT;
288typedef void *HINSTANCE;
289typedef void *HMENU;
290typedef void *HWND;
291typedef void *HDC;
292typedef void VOID;
293typedef int LPNMHDR;
294typedef int LONG;
295typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200296typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200297typedef int COLORREF;
298typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299#endif
300
K.Takata45f9cfb2022-01-21 11:11:00 +0000301static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100302static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303static void clear_rect(RECT *rcp);
304
Bram Moolenaar734a8672019-12-02 22:49:38 +0100305static WORD s_dlgfntheight; // height of the dialog font
306static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307
308#ifdef FEAT_MENU
309static HMENU s_menuBar = NULL;
310#endif
311#ifdef FEAT_TEAROFF
312static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100313static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314#endif
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316// Flag that is set while processing a message that must not be interrupted by
317// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static int s_busy_processing = FALSE;
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321
322#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000323static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200324static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200326static int s_findrep_is_find; // TRUE for find dialog, FALSE
327 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100328#endif
329
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330HWND s_hwnd = NULL;
331static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100332static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100333
334#ifdef FEAT_TOOLBAR
335static HWND s_toolbarhwnd = NULL;
336static WNDPROC s_toolbar_wndproc = NULL;
337#endif
338
339#ifdef FEAT_GUI_TABLINE
340static HWND s_tabhwnd = NULL;
341static WNDPROC s_tabline_wndproc = NULL;
342static int showing_tabline = 0;
343#endif
344
345static WPARAM s_wParam = 0;
346static LPARAM s_lParam = 0;
347
348static HWND s_textArea = NULL;
349static UINT s_uMsg = 0;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100352
353static int s_need_activate = FALSE;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355// This variable is set when waiting for an event, which is the only moment
356// scrollbar dragging can be done directly. It's not allowed while commands
357// are executed, because it may move the cursor and that may cause unexpected
358// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100359static int allow_scrollbar = FALSE;
360
K.Takatac81e9bf2022-01-16 14:15:49 +0000361#ifndef _DPI_AWARENESS_CONTEXTS_
362typedef HANDLE DPI_AWARENESS_CONTEXT;
363
364typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000365 DPI_AWARENESS_INVALID = -1,
366 DPI_AWARENESS_UNAWARE = 0,
367 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000368 DPI_AWARENESS_PER_MONITOR_AWARE = 2
369} DPI_AWARENESS;
370
K.Takatab0b2b732022-01-19 12:59:21 +0000371# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
372# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000373# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
374# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
375# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
376#endif
377
378#define DEFAULT_DPI 96
379static int s_dpi = DEFAULT_DPI;
380static BOOL s_in_dpichanged = FALSE;
381static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
382
383static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
384static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
385static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
386//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
387static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
388static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
389
390 static UINT WINAPI
391stubGetDpiForSystem(void)
392{
393 HWND hwnd = GetDesktopWindow();
394 HDC hdc = GetWindowDC(hwnd);
395 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
396 ReleaseDC(hwnd, hdc);
397 return dpi;
398}
399
400 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100401stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000402{
403 return GetSystemMetrics(nIndex);
404}
405
406 static int
407adjust_fontsize_by_dpi(int size)
408{
409 return size * s_dpi / (int)pGetDpiForSystem();
410}
411
412 static int
413adjust_by_system_dpi(int size)
414{
415 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
416}
417
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100418#if defined(FEAT_DIRECTX)
419 static int
420directx_enabled(void)
421{
422 if (s_dwc != NULL)
423 return 1;
424 else if (s_directx_load_attempted)
425 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100426 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100427 DWrite_Init();
428 s_directx_load_attempted = 1;
429 s_dwc = DWriteContext_Open();
430 directx_binddc();
431 return s_dwc != NULL ? 1 : 0;
432}
433
434 static void
435directx_binddc(void)
436{
437 if (s_textArea != NULL)
438 {
439 RECT rect;
440 GetClientRect(s_textArea, &rect);
441 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
442 }
443}
444#endif
445
Bram Moolenaar734a8672019-12-02 22:49:38 +0100446extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100447
448static struct
449{
450 UINT key_sym;
451 char_u vim_code0;
452 char_u vim_code1;
453} special_keys[] =
454{
455 {VK_UP, 'k', 'u'},
456 {VK_DOWN, 'k', 'd'},
457 {VK_LEFT, 'k', 'l'},
458 {VK_RIGHT, 'k', 'r'},
459
460 {VK_F1, 'k', '1'},
461 {VK_F2, 'k', '2'},
462 {VK_F3, 'k', '3'},
463 {VK_F4, 'k', '4'},
464 {VK_F5, 'k', '5'},
465 {VK_F6, 'k', '6'},
466 {VK_F7, 'k', '7'},
467 {VK_F8, 'k', '8'},
468 {VK_F9, 'k', '9'},
469 {VK_F10, 'k', ';'},
470
471 {VK_F11, 'F', '1'},
472 {VK_F12, 'F', '2'},
473 {VK_F13, 'F', '3'},
474 {VK_F14, 'F', '4'},
475 {VK_F15, 'F', '5'},
476 {VK_F16, 'F', '6'},
477 {VK_F17, 'F', '7'},
478 {VK_F18, 'F', '8'},
479 {VK_F19, 'F', '9'},
480 {VK_F20, 'F', 'A'},
481
482 {VK_F21, 'F', 'B'},
483#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485#endif
486 {VK_F22, 'F', 'C'},
487 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100488 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
490 {VK_HELP, '%', '1'},
491 {VK_BACK, 'k', 'b'},
492 {VK_INSERT, 'k', 'I'},
493 {VK_DELETE, 'k', 'D'},
494 {VK_HOME, 'k', 'h'},
495 {VK_END, '@', '7'},
496 {VK_PRIOR, 'k', 'P'},
497 {VK_NEXT, 'k', 'N'},
498 {VK_PRINT, '%', '9'},
499 {VK_ADD, 'K', '6'},
500 {VK_SUBTRACT, 'K', '7'},
501 {VK_DIVIDE, 'K', '8'},
502 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100503 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100504 {VK_DECIMAL, 'K', 'B'},
505
506 {VK_NUMPAD0, 'K', 'C'},
507 {VK_NUMPAD1, 'K', 'D'},
508 {VK_NUMPAD2, 'K', 'E'},
509 {VK_NUMPAD3, 'K', 'F'},
510 {VK_NUMPAD4, 'K', 'G'},
511 {VK_NUMPAD5, 'K', 'H'},
512 {VK_NUMPAD6, 'K', 'I'},
513 {VK_NUMPAD7, 'K', 'J'},
514 {VK_NUMPAD8, 'K', 'K'},
515 {VK_NUMPAD9, 'K', 'L'},
516
Bram Moolenaar734a8672019-12-02 22:49:38 +0100517 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100518 {VK_SPACE, ' ', NUL},
519 {VK_TAB, TAB, NUL},
520 {VK_ESCAPE, ESC, NUL},
521 {NL, NL, NUL},
522 {CAR, CAR, NUL},
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525 {0, 0, 0}
526};
527
Bram Moolenaar734a8672019-12-02 22:49:38 +0100528// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_button_pending = -1;
530
Bram Moolenaar734a8672019-12-02 22:49:38 +0100531// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
532// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533static int s_getting_focus = FALSE;
534
535static int s_x_pending;
536static int s_y_pending;
537static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000538static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100540static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200541static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
542 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100543
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100544#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100545// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100546static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000547static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548#endif
549
550/*
551 * For control IME.
552 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100554 */
K.Takata4ac893f2022-01-20 12:44:28 +0000555#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100556// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100557static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100558// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100559static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100560#endif
561
562#ifdef FEAT_MBYTE_IME
563static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
564#endif
565
566#if defined(FEAT_BROWSE)
567static char_u *convert_filter(char_u *s);
568#endif
569
570#ifdef DEBUG_PRINT_ERROR
571/*
572 * Print out the last Windows error message
573 */
574 static void
575print_windows_error(void)
576{
577 LPVOID lpMsgBuf;
578
579 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
580 NULL, GetLastError(),
581 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
582 (LPTSTR) &lpMsgBuf, 0, NULL);
583 TRACE1("Error: %s\n", lpMsgBuf);
584 LocalFree(lpMsgBuf);
585}
586#endif
587
588/*
589 * Cursor blink functions.
590 *
591 * This is a simple state machine:
592 * BLINK_NONE not blinking at all
593 * BLINK_OFF blinking, cursor is not shown
594 * BLINK_ON blinking, cursor is shown
595 */
596
597#define BLINK_NONE 0
598#define BLINK_OFF 1
599#define BLINK_ON 2
600
601static int blink_state = BLINK_NONE;
602static long_u blink_waittime = 700;
603static long_u blink_ontime = 400;
604static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000605static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100606
Bram Moolenaar703a8042016-06-04 16:24:32 +0200607 int
608gui_mch_is_blinking(void)
609{
610 return blink_state != BLINK_NONE;
611}
612
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200613 int
614gui_mch_is_blink_off(void)
615{
616 return blink_state == BLINK_OFF;
617}
618
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100619 void
620gui_mch_set_blinking(long wait, long on, long off)
621{
622 blink_waittime = wait;
623 blink_ontime = on;
624 blink_offtime = off;
625}
626
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627 static VOID CALLBACK
628_OnBlinkTimer(
629 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100630 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000631 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100632 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633{
634 MSG msg;
635
636 /*
637 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
638 */
639
640 KillTimer(NULL, idEvent);
641
Bram Moolenaar734a8672019-12-02 22:49:38 +0100642 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000643 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100644 ;
645
646 if (blink_state == BLINK_ON)
647 {
648 gui_undraw_cursor();
649 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000650 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 }
652 else
653 {
654 gui_update_cursor(TRUE, FALSE);
655 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000656 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100657 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100658 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100659}
660
661 static void
662gui_mswin_rm_blink_timer(void)
663{
664 MSG msg;
665
666 if (blink_timer != 0)
667 {
668 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100669 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000670 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671 ;
672 blink_timer = 0;
673 }
674}
675
676/*
677 * Stop the cursor blinking. Show the cursor if it wasn't shown.
678 */
679 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100680gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681{
682 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100683 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100684 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100685 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100686 gui_mch_flush();
687 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100688 blink_state = BLINK_NONE;
689}
690
691/*
692 * Start the cursor blinking. If it was already blinking, this restarts the
693 * waiting time and shows the cursor.
694 */
695 void
696gui_mch_start_blink(void)
697{
698 gui_mswin_rm_blink_timer();
699
Bram Moolenaar734a8672019-12-02 22:49:38 +0100700 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
702 {
K.Takataa8ec4912022-02-03 14:32:33 +0000703 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100704 blink_state = BLINK_ON;
705 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100706 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100707 }
708}
709
710/*
711 * Call-back routines.
712 */
713
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714 static VOID CALLBACK
715_OnTimer(
716 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100717 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000718 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100719 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100720{
721 MSG msg;
722
723 /*
724 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
725 */
726 KillTimer(NULL, idEvent);
727 s_timed_out = TRUE;
728
Bram Moolenaar734a8672019-12-02 22:49:38 +0100729 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000730 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100731 ;
732 if (idEvent == s_wait_timer)
733 s_wait_timer = 0;
734}
735
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736 static void
737_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100738 HWND hwnd UNUSED,
739 UINT ch UNUSED,
740 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741{
742 dead_key = 1;
743}
744
745/*
746 * Convert Unicode character "ch" to bytes in "string[slen]".
747 * When "had_alt" is TRUE the ALT key was included in "ch".
748 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200749 * Because the Windows API uses UTF-16, we have to deal with surrogate
750 * pairs; this is where we choose to deal with them: if "ch" is a high
751 * surrogate, it will be stored, and the length returned will be zero; the next
752 * char_to_string call will then include the high surrogate, decoding the pair
753 * of UTF-16 code units to a single Unicode code point, presuming it is the
754 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 */
756 static int
757char_to_string(int ch, char_u *string, int slen, int had_alt)
758{
759 int len;
760 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100761 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200762 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100763
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200764 if (surrogate_pending_ch != 0)
765 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100766 // We don't guarantee ch is a low surrogate to match the high surrogate
767 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200768 wstring[0] = surrogate_pending_ch;
769 wstring[1] = ch;
770 surrogate_pending_ch = 0;
771 len = 2;
772 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100773 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200774 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // We don't have the entire code point yet, only the first UTF-16 code
776 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200777 surrogate_pending_ch = ch;
778 return 0;
779 }
780 else
781 {
782 wstring[0] = ch;
783 len = 1;
784 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785
Bram Moolenaar734a8672019-12-02 22:49:38 +0100786 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
787 // "enc_codepage" is non-zero use the standard Win32 function,
788 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200789 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100790 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
792 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100793 // If we had included the ALT key into the character but now the
794 // upper bit is no longer set, that probably means the conversion
795 // failed. Convert the original character and set the upper bit
796 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200797 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799 wstring[0] = ch & 0x7f;
800 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
801 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100802 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200803 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100804 }
805 }
806 else
807 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 ws = utf16_to_enc(wstring, &len);
809 if (ws == NULL)
810 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 else
812 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100813 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200814 len = slen;
815 mch_memmove(string, ws, len);
816 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 }
818 }
819
820 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100821 {
822 string[0] = ch;
823 len = 1;
824 }
825
826 for (i = 0; i < len; ++i)
827 if (string[i] == CSI && len <= slen - 2)
828 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100829 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100830 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
831 string[++i] = KS_EXTRA;
832 string[++i] = (int)KE_CSI;
833 len += 2;
834 }
835
836 return len;
837}
838
LemonBoy45684c62022-04-24 15:46:42 +0100839 static int
840get_active_modifiers(void)
841{
842 int modifiers = 0;
843
844 if (GetKeyState(VK_CONTROL) & 0x8000)
845 modifiers |= MOD_MASK_CTRL;
846 if (GetKeyState(VK_SHIFT) & 0x8000)
847 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100848 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
849 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100850 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100851 if (GetKeyState(VK_LMENU) & 0x8000)
852 modifiers |= MOD_MASK_ALT;
853 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
854 modifiers &= ~MOD_MASK_CTRL;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100855 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
856 // is pressed, Windows claims that Ctrl is hold as well. That way we can
857 // recognize Right-ALT alone and be sure that not AltGr is hold.
858 if (!(GetKeyState(VK_CONTROL) & 0x8000)
859 && (GetKeyState(VK_RMENU) & 0x8000)
860 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
861 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100862
863 return modifiers;
864}
865
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100866/*
867 * Key hit, add it to the input buffer.
868 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100869 static void
870_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100871 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100872 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100873 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100874{
875 char_u string[40];
876 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100877 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100878 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879
Anton Sharonov3f026672022-07-26 21:26:18 +0100880 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
881 return;
882
883 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
884 // process_message()
885 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
886 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100887
LemonBoy45684c62022-04-24 15:46:42 +0100888 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100889
890 ch = simplify_key(ch, &modifiers);
Christopher Plewright7b0afc12022-12-30 16:54:58 +0000891
892 // Some keys need adjustment when the Ctrl modifier is used.
893 ++no_reduce_keys;
894 ch = may_adjust_key_for_ctrl(modifiers, ch);
895 --no_reduce_keys;
896
LemonBoy77fc0b02022-04-22 22:45:52 +0100897 // remove the SHIFT modifier for keys where it's already included, e.g.,
898 // '(' and '*'
899 modifiers = may_remove_shift_modifier(modifiers, ch);
900
901 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
902 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
903 if (ch == CSI)
904 ch = K_CSI;
905
906 if (modifiers)
907 {
908 string[0] = CSI;
909 string[1] = KS_MODIFIER;
910 string[2] = modifiers;
911 add_to_input_buf(string, 3);
912 }
913
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100914 len = char_to_string(ch, string, 40, FALSE);
915 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
916 {
917 trash_input_buf();
918 got_int = TRUE;
919 }
920
921 add_to_input_buf(string, len);
922}
923
924/*
925 * Alt-Key hit, add it to the input buffer.
926 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100927 static void
928_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100929 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100930 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100931 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100933 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100934 int len;
935 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100936 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100937
Anton Sharonov3f026672022-07-26 21:26:18 +0100938 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100939
Bram Moolenaar734a8672019-12-02 22:49:38 +0100940 // OK, we have a character key (given by ch) which was entered with the
941 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
942 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
943 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100944 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100945 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100946 // remove the SHIFT modifier for keys where it's already included, e.g.,
947 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200948 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100949
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200950 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
951 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100952 if (ch == CSI)
953 ch = K_CSI;
954
955 len = 0;
956 if (modifiers)
957 {
958 string[len++] = CSI;
959 string[len++] = KS_MODIFIER;
960 string[len++] = modifiers;
961 }
962
963 if (IS_SPECIAL((int)ch))
964 {
965 string[len++] = CSI;
966 string[len++] = K_SECOND((int)ch);
967 string[len++] = K_THIRD((int)ch);
968 }
969 else
970 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100971 // Although the documentation isn't clear about it, we assume "ch" is
972 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100973 len += char_to_string(ch, string + len, 40 - len, TRUE);
974 }
975
976 add_to_input_buf(string, len);
977}
978
979 static void
980_OnMouseEvent(
981 int button,
982 int x,
983 int y,
984 int repeated_click,
985 UINT keyFlags)
986{
987 int vim_modifiers = 0x0;
988
989 s_getting_focus = FALSE;
990
991 if (keyFlags & MK_SHIFT)
992 vim_modifiers |= MOUSE_SHIFT;
993 if (keyFlags & MK_CONTROL)
994 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100995 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100996 vim_modifiers |= MOUSE_ALT;
997
998 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
999}
1000
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 static void
1002_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001003 HWND hwnd UNUSED,
1004 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001005 int x,
1006 int y,
1007 UINT keyFlags)
1008{
1009 static LONG s_prevTime = 0;
1010
1011 LONG currentTime = GetMessageTime();
1012 int button = -1;
1013 int repeated_click;
1014
Bram Moolenaar734a8672019-12-02 22:49:38 +01001015 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001016 (void)SetFocus(s_hwnd);
1017
1018 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1019 button = MOUSE_LEFT;
1020 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1021 button = MOUSE_MIDDLE;
1022 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1023 button = MOUSE_RIGHT;
1024 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1025 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001026 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1027 }
1028 else if (s_uMsg == WM_CAPTURECHANGED)
1029 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001030 // on W95/NT4, somehow you get in here with an odd Msg
1031 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001032 if (s_button_pending == MOUSE_LEFT)
1033 button = MOUSE_RIGHT;
1034 else
1035 button = MOUSE_LEFT;
1036 }
1037 if (button >= 0)
1038 {
1039 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1040
1041 /*
1042 * Holding down the left and right buttons simulates pushing the middle
1043 * button.
1044 */
1045 if (repeated_click
1046 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1047 || (button == MOUSE_RIGHT
1048 && s_button_pending == MOUSE_LEFT)))
1049 {
1050 /*
1051 * Hmm, gui.c will ignore more than one button down at a time, so
1052 * pretend we let go of it first.
1053 */
1054 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1055 button = MOUSE_MIDDLE;
1056 repeated_click = FALSE;
1057 s_button_pending = -1;
1058 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1059 }
1060 else if ((repeated_click)
1061 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1062 {
1063 if (s_button_pending > -1)
1064 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001065 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1066 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001067 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001068 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001069 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1070 }
1071 else
1072 {
1073 /*
1074 * If this is the first press (i.e. not a multiple click) don't
1075 * action immediately, but store and wait for:
1076 * i) button-up
1077 * ii) mouse move
1078 * iii) another button press
1079 * before using it.
1080 * This enables us to make left+right simulate middle button,
1081 * without left or right being actioned first. The side-effect is
1082 * that if you click and hold the mouse without dragging, the
1083 * cursor doesn't move until you release the button. In practice
1084 * this is hardly a problem.
1085 */
1086 s_button_pending = button;
1087 s_x_pending = x;
1088 s_y_pending = y;
1089 s_kFlags_pending = keyFlags;
1090 }
1091
1092 s_prevTime = currentTime;
1093 }
1094}
1095
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 static void
1097_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001098 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001099 int x,
1100 int y,
1101 UINT keyFlags)
1102{
1103 int button;
1104
1105 s_getting_focus = FALSE;
1106 if (s_button_pending > -1)
1107 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001108 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001109 _OnMouseEvent(s_button_pending, s_x_pending,
1110 s_y_pending, FALSE, s_kFlags_pending);
1111 s_button_pending = -1;
1112 }
1113 if (s_uMsg == WM_MOUSEMOVE)
1114 {
1115 /*
1116 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1117 * down.
1118 */
1119 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1120 | MK_XBUTTON1 | MK_XBUTTON2)))
1121 {
1122 gui_mouse_moved(x, y);
1123 return;
1124 }
1125
1126 /*
1127 * While button is down, keep grabbing mouse move events when
1128 * the mouse goes outside the window
1129 */
1130 SetCapture(s_textArea);
1131 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001132 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001133 }
1134 else
1135 {
1136 ReleaseCapture();
1137 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001138 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001139 }
1140
1141 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1142}
1143
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001144 static void
1145_OnSizeTextArea(
1146 HWND hwnd UNUSED,
1147 UINT state UNUSED,
1148 int cx UNUSED,
1149 int cy UNUSED)
1150{
1151#if defined(FEAT_DIRECTX)
1152 if (IS_ENABLE_DIRECTX())
1153 directx_binddc();
1154#endif
1155}
1156
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157#ifdef FEAT_MENU
1158/*
1159 * Find the vimmenu_T with the given id
1160 */
1161 static vimmenu_T *
1162gui_mswin_find_menu(
1163 vimmenu_T *pMenu,
1164 int id)
1165{
1166 vimmenu_T *pChildMenu;
1167
1168 while (pMenu)
1169 {
1170 if (pMenu->id == (UINT)id)
1171 break;
1172 if (pMenu->children != NULL)
1173 {
1174 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1175 if (pChildMenu)
1176 {
1177 pMenu = pChildMenu;
1178 break;
1179 }
1180 }
1181 pMenu = pMenu->next;
1182 }
1183 return pMenu;
1184}
1185
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 static void
1187_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001188 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001190 HWND hwndCtl UNUSED,
1191 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192{
1193 vimmenu_T *pMenu;
1194
1195 pMenu = gui_mswin_find_menu(root_menu, id);
1196 if (pMenu)
1197 gui_menu_cb(pMenu);
1198}
1199#endif
1200
1201#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001202/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001203 * Handle a Find/Replace window message.
1204 */
1205 static void
1206_OnFindRepl(void)
1207{
1208 int flags = 0;
1209 int down;
1210
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001211 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001212 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001213 (void)SetFocus(s_hwnd);
1214
1215 if (s_findrep_struct.Flags & FR_FINDNEXT)
1216 {
1217 flags = FRD_FINDNEXT;
1218
Bram Moolenaar734a8672019-12-02 22:49:38 +01001219 // Give main window the focus back: this is so the cursor isn't
1220 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001221 (void)SetFocus(s_hwnd);
1222 }
1223 else if (s_findrep_struct.Flags & FR_REPLACE)
1224 {
1225 flags = FRD_REPLACE;
1226
Bram Moolenaar734a8672019-12-02 22:49:38 +01001227 // Give main window the focus back: this is so the cursor isn't
1228 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001229 (void)SetFocus(s_hwnd);
1230 }
1231 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1232 {
1233 flags = FRD_REPLACEALL;
1234 }
1235
1236 if (flags != 0)
1237 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001238 char_u *p, *q;
1239
Bram Moolenaar734a8672019-12-02 22:49:38 +01001240 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001241 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1242 flags |= FRD_WHOLE_WORD;
1243 if (s_findrep_struct.Flags & FR_MATCHCASE)
1244 flags |= FRD_MATCH_CASE;
1245 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001246 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1247 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1248 if (p != NULL && q != NULL)
1249 gui_do_findrepl(flags, p, q, down);
1250 vim_free(p);
1251 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001252 }
1253}
1254#endif
1255
1256 static void
1257HandleMouseHide(UINT uMsg, LPARAM lParam)
1258{
1259 static LPARAM last_lParam = 0L;
1260
Bram Moolenaar734a8672019-12-02 22:49:38 +01001261 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001262 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1263 {
1264 if (lParam == last_lParam)
1265 return;
1266 last_lParam = lParam;
1267 }
1268
Bram Moolenaar734a8672019-12-02 22:49:38 +01001269 // Handle specially, to centralise coding. We need to be sure we catch all
1270 // possible events which should cause us to restore the cursor (as it is a
1271 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001272 switch (uMsg)
1273 {
1274 case WM_KEYUP:
1275 case WM_CHAR:
1276 /*
1277 * blank out the pointer if necessary
1278 */
1279 if (p_mh)
1280 gui_mch_mousehide(TRUE);
1281 break;
1282
Bram Moolenaar734a8672019-12-02 22:49:38 +01001283 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001284 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001285 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001286 case WM_LBUTTONDOWN:
1287 case WM_LBUTTONUP:
1288 case WM_MBUTTONDOWN:
1289 case WM_MBUTTONUP:
1290 case WM_RBUTTONDOWN:
1291 case WM_RBUTTONUP:
1292 case WM_XBUTTONDOWN:
1293 case WM_XBUTTONUP:
1294 case WM_NCMOUSEMOVE:
1295 case WM_NCLBUTTONDOWN:
1296 case WM_NCLBUTTONUP:
1297 case WM_NCMBUTTONDOWN:
1298 case WM_NCMBUTTONUP:
1299 case WM_NCRBUTTONDOWN:
1300 case WM_NCRBUTTONUP:
1301 case WM_KILLFOCUS:
1302 /*
1303 * if the pointer is currently hidden, then we should show it.
1304 */
1305 gui_mch_mousehide(FALSE);
1306 break;
1307 }
1308}
1309
1310 static LRESULT CALLBACK
1311_TextAreaWndProc(
1312 HWND hwnd,
1313 UINT uMsg,
1314 WPARAM wParam,
1315 LPARAM lParam)
1316{
1317 /*
1318 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1319 hwnd, uMsg, wParam, lParam);
1320 */
1321
1322 HandleMouseHide(uMsg, lParam);
1323
1324 s_uMsg = uMsg;
1325 s_wParam = wParam;
1326 s_lParam = lParam;
1327
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001328#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001329 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001330#endif
1331
1332 switch (uMsg)
1333 {
1334 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1335 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1336 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1337 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1338 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1339 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1340 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1341 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1342 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1343 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1344 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1345 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1346 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1347 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001348 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001349
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001350#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001351 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1352 return TRUE;
1353#endif
1354 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001355 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001356 }
1357}
1358
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001359/*
1360 * Called when the foreground or background color has been changed.
1361 */
1362 void
1363gui_mch_new_colors(void)
1364{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001365 HBRUSH prevBrush;
1366
1367 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001368 prevBrush = (HBRUSH)SetClassLongPtr(
1369 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001370 InvalidateRect(s_hwnd, NULL, TRUE);
1371 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001372}
1373
1374/*
1375 * Set the colors to their default values.
1376 */
1377 void
1378gui_mch_def_colors(void)
1379{
1380 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1381 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1382 gui.def_norm_pixel = gui.norm_pixel;
1383 gui.def_back_pixel = gui.back_pixel;
1384}
1385
1386/*
1387 * Open the GUI window which was created by a call to gui_mch_init().
1388 */
1389 int
1390gui_mch_open(void)
1391{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001392 // Actually open the window, if not already visible
1393 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001394 if (!IsWindowVisible(s_hwnd))
1395 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1396
1397#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001398 // Init replace string here, so that we keep it when re-opening the
1399 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001400 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1401#endif
1402
1403 return OK;
1404}
1405
1406/*
1407 * Get the position of the top left corner of the window.
1408 */
1409 int
1410gui_mch_get_winpos(int *x, int *y)
1411{
1412 RECT rect;
1413
1414 GetWindowRect(s_hwnd, &rect);
1415 *x = rect.left;
1416 *y = rect.top;
1417 return OK;
1418}
1419
1420/*
1421 * Set the position of the top left corner of the window to the given
1422 * coordinates.
1423 */
1424 void
1425gui_mch_set_winpos(int x, int y)
1426{
1427 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1428 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1429}
1430 void
1431gui_mch_set_text_area_pos(int x, int y, int w, int h)
1432{
1433 static int oldx = 0;
1434 static int oldy = 0;
1435
1436 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1437
1438#ifdef FEAT_TOOLBAR
1439 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1440 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001441 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001442#endif
1443#if defined(FEAT_GUI_TABLINE)
1444 if (showing_tabline)
1445 {
1446 int top = 0;
1447 RECT rect;
1448
1449# ifdef FEAT_TOOLBAR
1450 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001451 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001452# endif
1453 GetClientRect(s_hwnd, &rect);
1454 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1455 }
1456#endif
1457
Bram Moolenaar734a8672019-12-02 22:49:38 +01001458 // When side scroll bar is unshown, the size of window will change.
1459 // then, the text area move left or right. thus client rect should be
1460 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001461 if (oldx != x || oldy != y)
1462 {
1463 InvalidateRect(s_hwnd, NULL, FALSE);
1464 oldx = x;
1465 oldy = y;
1466 }
1467}
1468
1469
1470/*
1471 * Scrollbar stuff:
1472 */
1473
1474 void
1475gui_mch_enable_scrollbar(
1476 scrollbar_T *sb,
1477 int flag)
1478{
1479 ShowScrollBar(sb->id, SB_CTL, flag);
1480
Bram Moolenaar734a8672019-12-02 22:49:38 +01001481 // TODO: When the window is maximized, the size of the window stays the
1482 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1483 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001484}
1485
1486 void
1487gui_mch_set_scrollbar_pos(
1488 scrollbar_T *sb,
1489 int x,
1490 int y,
1491 int w,
1492 int h)
1493{
1494 SetWindowPos(sb->id, NULL, x, y, w, h,
1495 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1496}
1497
Bram Moolenaar203ec772020-07-17 20:43:43 +02001498 int
1499gui_mch_get_scrollbar_xpadding(void)
1500{
1501 RECT rcTxt, rcWnd;
1502 int xpad;
1503
1504 GetWindowRect(s_textArea, &rcTxt);
1505 GetWindowRect(s_hwnd, &rcWnd);
1506 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001507 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1508 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001509 return (xpad < 0) ? 0 : xpad;
1510}
1511
1512 int
1513gui_mch_get_scrollbar_ypadding(void)
1514{
1515 RECT rcTxt, rcWnd;
1516 int ypad;
1517
1518 GetWindowRect(s_textArea, &rcTxt);
1519 GetWindowRect(s_hwnd, &rcWnd);
1520 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001521 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1522 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001523 return (ypad < 0) ? 0 : ypad;
1524}
1525
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001526 void
1527gui_mch_create_scrollbar(
1528 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001529 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001530{
1531 sb->id = CreateWindow(
1532 "SCROLLBAR", "Scrollbar",
1533 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001534 10, // Any value will do for now
1535 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001536 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001537 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001538}
1539
1540/*
1541 * Find the scrollbar with the given hwnd.
1542 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001543 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001544gui_mswin_find_scrollbar(HWND hwnd)
1545{
1546 win_T *wp;
1547
1548 if (gui.bottom_sbar.id == hwnd)
1549 return &gui.bottom_sbar;
1550 FOR_ALL_WINDOWS(wp)
1551 {
1552 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1553 return &wp->w_scrollbars[SBAR_LEFT];
1554 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1555 return &wp->w_scrollbars[SBAR_RIGHT];
1556 }
1557 return NULL;
1558}
1559
K.Takatac81e9bf2022-01-16 14:15:49 +00001560 static void
1561update_scrollbar_size(void)
1562{
1563 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1564 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1565}
1566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001567/*
K.Takataabe628e2022-01-23 16:25:17 +00001568 * Get the average character size of a font.
1569 */
1570 static void
1571GetAverageFontSize(HDC hdc, SIZE *size)
1572{
1573 // GetTextMetrics() may not return the right value in tmAveCharWidth
1574 // for some fonts. Do our own average computation.
1575 GetTextExtentPoint(hdc,
1576 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1577 52, size);
1578 size->cx = (size->cx / 26 + 1) / 2;
1579}
1580
1581/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582 * Get the character size of a font.
1583 */
1584 static void
1585GetFontSize(GuiFont font)
1586{
1587 HWND hwnd = GetDesktopWindow();
1588 HDC hdc = GetWindowDC(hwnd);
1589 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001590 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001591 TEXTMETRIC tm;
1592
1593 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001594 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595
K.Takataabe628e2022-01-23 16:25:17 +00001596 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 gui.char_height = tm.tmHeight + p_linespace;
1598
1599 SelectFont(hdc, hfntOld);
1600
1601 ReleaseDC(hwnd, hdc);
1602}
1603
1604/*
1605 * Adjust gui.char_height (after 'linespace' was changed).
1606 */
1607 int
1608gui_mch_adjust_charheight(void)
1609{
1610 GetFontSize(gui.norm_font);
1611 return OK;
1612}
1613
1614 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001615get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001616{
1617 HFONT font = NULL;
1618
Bram Moolenaar734a8672019-12-02 22:49:38 +01001619 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001620 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001621
1622 if (font == NULL)
1623 return NOFONT;
1624
1625 return (GuiFont)font;
1626}
1627
1628 static int
1629pixels_to_points(int pixels, int vertical)
1630{
1631 int points;
1632 HWND hwnd;
1633 HDC hdc;
1634
1635 hwnd = GetDesktopWindow();
1636 hdc = GetWindowDC(hwnd);
1637
1638 points = MulDiv(pixels, 72,
1639 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1640
1641 ReleaseDC(hwnd, hdc);
1642
1643 return points;
1644}
1645
1646 GuiFont
1647gui_mch_get_font(
1648 char_u *name,
1649 int giveErrorIfMissing)
1650{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001651 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001652 GuiFont font = NOFONT;
1653
1654 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001655 {
1656 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001657 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001658 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001659 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001660 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001661 return font;
1662}
1663
1664#if defined(FEAT_EVAL) || defined(PROTO)
1665/*
1666 * Return the name of font "font" in allocated memory.
1667 * Don't know how to get the actual name, thus use the provided name.
1668 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001669 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001670gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001671{
1672 if (name == NULL)
1673 return NULL;
1674 return vim_strsave(name);
1675}
1676#endif
1677
1678 void
1679gui_mch_free_font(GuiFont font)
1680{
1681 if (font)
1682 DeleteObject((HFONT)font);
1683}
1684
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001685/*
1686 * Return the Pixel value (color) for the given color name.
1687 * Return INVALCOLOR for error.
1688 */
1689 guicolor_T
1690gui_mch_get_color(char_u *name)
1691{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001692 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001693
1694 typedef struct SysColorTable
1695 {
1696 char *name;
1697 int color;
1698 } SysColorTable;
1699
1700 static SysColorTable sys_table[] =
1701 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001702 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1703 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001704#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001705 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1706#endif
1707 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1708 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1709 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1710 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1711 {"SYS_DESKTOP", COLOR_DESKTOP},
1712 {"SYS_INFOBK", COLOR_INFOBK},
1713 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1714 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001715 {"SYS_BTNFACE", COLOR_BTNFACE},
1716 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1717 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1718 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1719 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1720 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1721 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1722 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1723 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1724 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1725 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1726 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1727 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1728 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1729 {"SYS_MENU", COLOR_MENU},
1730 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1731 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1732 {"SYS_WINDOW", COLOR_WINDOW},
1733 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1734 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1735 };
1736
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001737 /*
1738 * Try to look up a system colour.
1739 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001740 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001741 if (STRICMP(name, sys_table[i].name) == 0)
1742 return GetSysColor(sys_table[i].color);
1743
Bram Moolenaarab302212016-04-26 20:59:29 +02001744 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001745}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001746
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001747 guicolor_T
1748gui_mch_get_rgb_color(int r, int g, int b)
1749{
1750 return gui_get_rgb_color_cmn(r, g, b);
1751}
1752
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753/*
1754 * Return OK if the key with the termcap name "name" is supported.
1755 */
1756 int
1757gui_mch_haskey(char_u *name)
1758{
1759 int i;
1760
1761 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001762 if (name[0] == special_keys[i].vim_code0
1763 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001764 return OK;
1765 return FAIL;
1766}
1767
1768 void
1769gui_mch_beep(void)
1770{
LemonBoy77771d32022-04-13 11:47:25 +01001771 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001772}
1773/*
1774 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1775 */
1776 void
1777gui_mch_invert_rectangle(
1778 int r,
1779 int c,
1780 int nr,
1781 int nc)
1782{
1783 RECT rc;
1784
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001785#if defined(FEAT_DIRECTX)
1786 if (IS_ENABLE_DIRECTX())
1787 DWriteContext_Flush(s_dwc);
1788#endif
1789
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001790 /*
1791 * Note: InvertRect() excludes right and bottom of rectangle.
1792 */
1793 rc.left = FILL_X(c);
1794 rc.top = FILL_Y(r);
1795 rc.right = rc.left + nc * gui.char_width;
1796 rc.bottom = rc.top + nr * gui.char_height;
1797 InvertRect(s_hdc, &rc);
1798}
1799
1800/*
1801 * Iconify the GUI window.
1802 */
1803 void
1804gui_mch_iconify(void)
1805{
1806 ShowWindow(s_hwnd, SW_MINIMIZE);
1807}
1808
1809/*
1810 * Draw a cursor without focus.
1811 */
1812 void
1813gui_mch_draw_hollow_cursor(guicolor_T color)
1814{
1815 HBRUSH hbr;
1816 RECT rc;
1817
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001818#if defined(FEAT_DIRECTX)
1819 if (IS_ENABLE_DIRECTX())
1820 DWriteContext_Flush(s_dwc);
1821#endif
1822
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 /*
1824 * Note: FrameRect() excludes right and bottom of rectangle.
1825 */
1826 rc.left = FILL_X(gui.col);
1827 rc.top = FILL_Y(gui.row);
1828 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001829 if (mb_lefthalve(gui.row, gui.col))
1830 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001831 rc.bottom = rc.top + gui.char_height;
1832 hbr = CreateSolidBrush(color);
1833 FrameRect(s_hdc, &rc, hbr);
1834 DeleteBrush(hbr);
1835}
1836/*
1837 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1838 * color "color".
1839 */
1840 void
1841gui_mch_draw_part_cursor(
1842 int w,
1843 int h,
1844 guicolor_T color)
1845{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001846 RECT rc;
1847
1848 /*
1849 * Note: FillRect() excludes right and bottom of rectangle.
1850 */
1851 rc.left =
1852#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001853 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001854 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1855#endif
1856 FILL_X(gui.col);
1857 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1858 rc.right = rc.left + w;
1859 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001860
Bram Moolenaar92467d32017-12-05 13:22:16 +01001861 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862}
1863
1864
1865/*
1866 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1867 * dead key's nominal character and re-post the original message.
1868 */
1869 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001870outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871{
1872 static MSG deadCharExpel;
1873
Anton Sharonov3f026672022-07-26 21:26:18 +01001874 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001875 return;
1876
Anton Sharonov3f026672022-07-26 21:26:18 +01001877 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001878
Bram Moolenaar734a8672019-12-02 22:49:38 +01001879 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001880 deadCharExpel.message = originalMsg.message;
1881 deadCharExpel.hwnd = originalMsg.hwnd;
1882 deadCharExpel.wParam = VK_SPACE;
1883
K.Takata4ac893f2022-01-20 12:44:28 +00001884 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885
Bram Moolenaar734a8672019-12-02 22:49:38 +01001886 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001887 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1888 originalMsg.lParam);
1889}
1890
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001891/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001892 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1893 */
1894 static void
1895outputDeadKey_rePost(MSG originalMsg)
1896{
1897 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1898}
1899
1900/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001901 * Process a single Windows message.
1902 * If one is not available we hang until one is.
1903 */
1904 static void
1905process_message(void)
1906{
1907 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001908 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001909 char_u string[40];
1910 int i;
1911 int modifiers = 0;
1912 int key;
1913#ifdef FEAT_MENU
1914 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1915#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001916 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001917
K.Takatab7057bd2022-01-21 11:37:07 +00001918 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001919
1920#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001921 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001922 if (msg.message == WM_OLE)
1923 {
1924 char_u *str = (char_u *)msg.lParam;
1925 if (str == NULL || *str == NUL)
1926 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001927 // Message can't be ours, forward it. Fixes problem with Ultramon
1928 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001929 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 }
1931 else
1932 {
1933 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001934 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 }
1936 return;
1937 }
1938#endif
1939
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001940#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001941 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001942 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001943 {
1944 HandleMouseHide(msg.message, msg.lParam);
1945 return;
1946 }
1947#endif
1948
1949 /*
1950 * Check if it's a special key that we recognise. If not, call
1951 * TranslateMessage().
1952 */
1953 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1954 {
1955 vk = (int) msg.wParam;
1956
1957 /*
1958 * Handle dead keys in special conditions in other cases we let Windows
1959 * handle them and do not interfere.
1960 *
1961 * The dead_key flag must be reset on several occasions:
1962 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1963 * consumed at that point (This is when we let Windows combine the
1964 * dead character on its own)
1965 *
1966 * - Before doing something special such as regenerating keypresses to
1967 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001968 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001969 * immediately to _OnChar() (or _OnSysChar()).
1970 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001971
1972 /*
1973 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1974 * was handled by _WndProc, this keypress we want to process normally
1975 */
1976 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1977 dead_key = DEAD_KEY_OFF;
1978
1979 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001980 {
1981 /*
dundargocc57b5bc2022-11-02 13:30:51 +00001982 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01001983 *
1984 * After dead key was pressed with Ctrl in some cases, ESC was
1985 * artificially injected and handled by _OnChar(), now we are
1986 * dealing with completely new key press from the user. If we don't
1987 * do anything, ToUnicode() call will interpret this vk+scan_code
1988 * under influence of "dead-modifier". To prevent this we translate
1989 * this message replacing current char from user with VK_SPACE,
1990 * which will cause WM_CHAR with dead_key's character itself. Using
1991 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1992 * ignore this one WM_CHAR event completely. Afterwards (due to
1993 * usage of PostMessage), this procedure is scheduled to be called
1994 * again with user char and on next entry we will clean
1995 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
1996 * outputDeadKey_rePost() since we do not wish to reset dead_key
1997 * value.
1998 */
1999 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
2000 {
2001 outputDeadKey_rePost_Ex(msg,
2002 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2003 return;
2004 }
2005
2006 if (dead_key != DEAD_KEY_SET_DEFAULT)
2007 {
2008 // should never happen - is there a way to make ASSERT here?
2009 return;
2010 }
2011
2012 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002013 * If a dead key was pressed and the user presses VK_SPACE,
2014 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2015 * with the dead char now, so do nothing special and let Windows
2016 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002017 *
2018 * Note that VK_SPACE combines with the dead_key's character and
2019 * only one WM_CHAR will be generated by TranslateMessage(), in
2020 * the two other cases two WM_CHAR will be generated: the dead
2021 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2022 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002023 */
2024 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2025 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002026 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002027 TranslateMessage(&msg);
2028 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002029 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002030 // In modes where we are not typing, dead keys should behave
2031 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002032 else if ((get_real_state()
2033 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002034 {
2035 outputDeadKey_rePost(msg);
2036 return;
2037 }
2038 }
2039
Bram Moolenaar734a8672019-12-02 22:49:38 +01002040 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 if (vk == VK_CANCEL)
2042 {
2043 trash_input_buf();
2044 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002045 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002046 string[0] = Ctrl_C;
2047 add_to_input_buf(string, 1);
2048 }
2049
LemonBoy77fc0b02022-04-22 22:45:52 +01002050 // This is an IME event or a synthetic keystroke, let Windows handle it.
2051 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2052 {
2053 TranslateMessage(&msg);
2054 return;
2055 }
2056
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002057 for (i = 0; special_keys[i].key_sym != 0; i++)
2058 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002059 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002060 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002061 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002062 {
2063 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002064 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002065 * is a key that would normally trigger the dead key nominal
2066 * character output (such as a NUMPAD printable character or
2067 * the TAB key, etc...).
2068 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002069 if (dead_key == DEAD_KEY_SET_DEFAULT
2070 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002071 || vk == VK_TAB || vk == CAR))
2072 {
2073 outputDeadKey_rePost(msg);
2074 return;
2075 }
2076
2077#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002078 // Check for <F10>: Windows selects the menu. When <F10> is
2079 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002080 if (vk == VK_F10
2081 && gui.menu_is_active
2082 && check_map(k10, State, FALSE, TRUE, FALSE,
2083 NULL, NULL) == NULL)
2084 break;
2085#endif
LemonBoy45684c62022-04-24 15:46:42 +01002086 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087
2088 if (special_keys[i].vim_code1 == NUL)
2089 key = special_keys[i].vim_code0;
2090 else
2091 key = TO_SPECIAL(special_keys[i].vim_code0,
2092 special_keys[i].vim_code1);
2093 key = simplify_key(key, &modifiers);
2094 if (key == CSI)
2095 key = K_CSI;
2096
2097 if (modifiers)
2098 {
2099 string[0] = CSI;
2100 string[1] = KS_MODIFIER;
2101 string[2] = modifiers;
2102 add_to_input_buf(string, 3);
2103 }
2104
2105 if (IS_SPECIAL(key))
2106 {
2107 string[0] = CSI;
2108 string[1] = K_SECOND(key);
2109 string[2] = K_THIRD(key);
2110 add_to_input_buf(string, 3);
2111 }
2112 else
2113 {
2114 int len;
2115
Bram Moolenaar734a8672019-12-02 22:49:38 +01002116 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002117 len = char_to_string(key, string, 40, FALSE);
2118 add_to_input_buf(string, len);
2119 }
2120 break;
2121 }
2122 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002123
2124 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002125 if (special_keys[i].key_sym == 0)
2126 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002127 WCHAR ch[8];
2128 int len;
2129 int i;
2130 UINT scan_code;
2131
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002132 // Construct the state table with only a few modifiers, we don't
2133 // really care about the presence of Ctrl/Alt as those modifiers are
2134 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002135 memset(keyboard_state, 0, 256);
2136 if (GetKeyState(VK_SHIFT) & 0x8000)
2137 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002138 if (GetKeyState(VK_CAPITAL) & 0x0001)
2139 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002140 // Alt-Gr is synthesized as Alt + Ctrl.
2141 if ((GetKeyState(VK_RMENU) & 0x8000)
2142 && (GetKeyState(VK_CONTROL) & 0x8000))
2143 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002144 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002145 keyboard_state[VK_CONTROL] = 0x80;
2146 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002147
2148 // Translate the virtual key according to the current keyboard
2149 // layout.
2150 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2151 // Convert the scan-code into a sequence of zero or more unicode
2152 // codepoints.
2153 // If this is a dead key ToUnicode returns a negative value.
2154 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2155 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002156 if (len < 0)
2157 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002158
2159 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002160 {
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002161 int wm_char = NUL;
2162
2163 if (dead_key == DEAD_KEY_SET_DEFAULT
2164 && (GetKeyState(VK_CONTROL) & 0x8000))
2165 {
2166 if ( // AZERTY CTRL+dead_circumflex
2167 (vk == 221 && scan_code == 26)
2168 // QWERTZ CTRL+dead_circumflex
2169 || (vk == 220 && scan_code == 41))
2170 wm_char = '[';
2171 if ( // QWERTZ CTRL+dead_two-overdots
2172 (vk == 192 && scan_code == 27))
2173 wm_char = ']';
2174 }
2175 if (wm_char != NUL)
Anton Sharonov3f026672022-07-26 21:26:18 +01002176 {
2177 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002178 // still hold as ESC
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002179 PostMessageW(msg.hwnd, WM_CHAR, wm_char, msg.lParam);
Anton Sharonov3f026672022-07-26 21:26:18 +01002180 // ask _OnChar() to not touch this state, wait for next key
2181 // press and maintain knowledge that we are "poisoned" with
2182 // "dead state"
2183 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2184 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002185 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002186 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002187
2188 // Post the message as TranslateMessage would do.
2189 if (msg.message == WM_KEYDOWN)
2190 {
2191 for (i = 0; i < len; i++)
2192 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002193 }
2194 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002195 {
2196 for (i = 0; i < len; i++)
2197 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2198 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002199 }
2200 }
2201#ifdef FEAT_MBYTE_IME
2202 else if (msg.message == WM_IME_NOTIFY)
2203 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2204 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002205 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002206 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002207#endif
2208
2209#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002210 // Check for <F10>: Default effect is to select the menu. When <F10> is
2211 // mapped we need to stop it here to avoid strange effects (e.g., for the
2212 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002213 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2214 NULL, NULL) == NULL)
2215#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002216 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002217}
2218
2219/*
2220 * Catch up with any queued events. This may put keyboard input into the
2221 * input buffer, call resize call-backs, trigger timers etc. If there is
2222 * nothing in the event queue (& no timers pending), then we return
2223 * immediately.
2224 */
2225 void
2226gui_mch_update(void)
2227{
2228 MSG msg;
2229
2230 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002231 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002232 && !vim_is_input_buf_full())
2233 process_message();
2234}
2235
Bram Moolenaar4231da42016-06-02 14:30:04 +02002236 static void
2237remove_any_timer(void)
2238{
2239 MSG msg;
2240
2241 if (s_wait_timer != 0 && !s_timed_out)
2242 {
2243 KillTimer(NULL, s_wait_timer);
2244
Bram Moolenaar734a8672019-12-02 22:49:38 +01002245 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002246 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002247 ;
2248 s_wait_timer = 0;
2249 }
2250}
2251
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002252/*
2253 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2254 * from the keyboard.
2255 * wtime == -1 Wait forever.
2256 * wtime == 0 This should never happen.
2257 * wtime > 0 Wait wtime milliseconds for a character.
2258 * Returns OK if a character was found to be available within the given time,
2259 * or FAIL otherwise.
2260 */
2261 int
2262gui_mch_wait_for_chars(int wtime)
2263{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002264 int focus;
2265
2266 s_timed_out = FALSE;
2267
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002268 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002270 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271 if (s_busy_processing)
2272 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002273
2274 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002275 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2276 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002277 }
2278
2279 allow_scrollbar = TRUE;
2280
2281 focus = gui.in_focus;
2282 while (!s_timed_out)
2283 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002284 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002285 if (gui.in_focus != focus)
2286 {
2287 if (gui.in_focus)
2288 gui_mch_start_blink();
2289 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002290 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002291 focus = gui.in_focus;
2292 }
2293
2294 if (s_need_activate)
2295 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002297 s_need_activate = FALSE;
2298 }
2299
Bram Moolenaar4231da42016-06-02 14:30:04 +02002300#ifdef FEAT_TIMERS
2301 did_add_timer = FALSE;
2302#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002304 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002305 for (;;)
2306 {
2307 MSG msg;
2308
2309 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002310# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002311 if (did_add_timer)
2312 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002313# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002314 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002315 {
2316 process_message();
2317 break;
2318 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002319 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002320 // TODO: The 10 msec is a compromise between laggy response
2321 // and consuming more CPU time. Better would be to handle
2322 // channel messages when they arrive.
2323 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002324 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002325 break;
2326 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002327#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002328 // Don't use gui_mch_update() because then we will spin-lock until a
2329 // char arrives, instead we use GetMessage() to hang until an
2330 // event arrives. No need to check for input_buf_full because we are
2331 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002332 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002333#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002334
2335 if (input_available())
2336 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002337 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 allow_scrollbar = FALSE;
2339
Bram Moolenaar89c00032019-09-04 13:53:21 +02002340 // Clear pending mouse button, the release event may have been
2341 // taken by the dialog window. But don't do this when getting
2342 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002343 if (!s_getting_focus)
2344 s_button_pending = -1;
2345
2346 return OK;
2347 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002348
2349#ifdef FEAT_TIMERS
2350 if (did_add_timer)
2351 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002352 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002353 remove_any_timer();
2354 break;
2355 }
2356#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357 }
2358 allow_scrollbar = FALSE;
2359 return FAIL;
2360}
2361
2362/*
2363 * Clear a rectangular region of the screen from text pos (row1, col1) to
2364 * (row2, col2) inclusive.
2365 */
2366 void
2367gui_mch_clear_block(
2368 int row1,
2369 int col1,
2370 int row2,
2371 int col2)
2372{
2373 RECT rc;
2374
2375 /*
2376 * Clear one extra pixel at the far right, for when bold characters have
2377 * spilled over to the window border.
2378 * Note: FillRect() excludes right and bottom of rectangle.
2379 */
2380 rc.left = FILL_X(col1);
2381 rc.top = FILL_Y(row1);
2382 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2383 rc.bottom = FILL_Y(row2 + 1);
2384 clear_rect(&rc);
2385}
2386
2387/*
2388 * Clear the whole text window.
2389 */
2390 void
2391gui_mch_clear_all(void)
2392{
2393 RECT rc;
2394
2395 rc.left = 0;
2396 rc.top = 0;
2397 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2398 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2399 clear_rect(&rc);
2400}
2401/*
2402 * Menu stuff.
2403 */
2404
2405 void
2406gui_mch_enable_menu(int flag)
2407{
2408#ifdef FEAT_MENU
2409 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2410#endif
2411}
2412
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413 void
2414gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002415 int x UNUSED,
2416 int y UNUSED,
2417 int w UNUSED,
2418 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002420 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002421}
2422
2423#if defined(FEAT_MENU) || defined(PROTO)
2424/*
2425 * Make menu item hidden or not hidden
2426 */
2427 void
2428gui_mch_menu_hidden(
2429 vimmenu_T *menu,
2430 int hidden)
2431{
2432 /*
2433 * This doesn't do what we want. Hmm, just grey the menu items for now.
2434 */
2435 /*
2436 if (hidden)
2437 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2438 else
2439 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2440 */
2441 gui_mch_menu_grey(menu, hidden);
2442}
2443
2444/*
2445 * This is called after setting all the menus to grey/hidden or not.
2446 */
2447 void
2448gui_mch_draw_menubar(void)
2449{
2450 DrawMenuBar(s_hwnd);
2451}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002452#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002454/*
2455 * Return the RGB value of a pixel as a long.
2456 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002457 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458gui_mch_get_rgb(guicolor_T pixel)
2459{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002460 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2461 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002462}
2463
2464#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002465/*
2466 * Convert pixels in X to dialog units
2467 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002468 static WORD
2469PixelToDialogX(int numPixels)
2470{
2471 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2472}
2473
Bram Moolenaar734a8672019-12-02 22:49:38 +01002474/*
2475 * Convert pixels in Y to dialog units
2476 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002477 static WORD
2478PixelToDialogY(int numPixels)
2479{
2480 return (WORD)((numPixels * 8) / s_dlgfntheight);
2481}
2482
Bram Moolenaar734a8672019-12-02 22:49:38 +01002483/*
2484 * Return the width in pixels of the given text in the given DC.
2485 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002486 static int
2487GetTextWidth(HDC hdc, char_u *str, int len)
2488{
2489 SIZE size;
2490
2491 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2492 return size.cx;
2493}
2494
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002495/*
2496 * Return the width in pixels of the given text in the given DC, taking care
2497 * of 'encoding' to active codepage conversion.
2498 */
2499 static int
2500GetTextWidthEnc(HDC hdc, char_u *str, int len)
2501{
2502 SIZE size;
2503 WCHAR *wstr;
2504 int n;
2505 int wlen = len;
2506
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002507 wstr = enc_to_utf16(str, &wlen);
2508 if (wstr == NULL)
2509 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002510
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002511 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2512 vim_free(wstr);
2513 if (n)
2514 return size.cx;
2515 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002517
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002518static void get_work_area(RECT *spi_rect);
2519
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002520/*
2521 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002522 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2523 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524 */
2525 static BOOL
2526CenterWindow(
2527 HWND hwndChild,
2528 HWND hwndParent)
2529{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002530 HMONITOR mon;
2531 MONITORINFO moninfo;
2532 RECT rChild, rParent, rScreen;
2533 int wChild, hChild, wParent, hParent;
2534 int xNew, yNew;
2535 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002536
2537 GetWindowRect(hwndChild, &rChild);
2538 wChild = rChild.right - rChild.left;
2539 hChild = rChild.bottom - rChild.top;
2540
Bram Moolenaar734a8672019-12-02 22:49:38 +01002541 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002542 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002543 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002544 else
2545 GetWindowRect(hwndParent, &rParent);
2546 wParent = rParent.right - rParent.left;
2547 hParent = rParent.bottom - rParent.top;
2548
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002549 moninfo.cbSize = sizeof(MONITORINFO);
2550 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2551 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002552 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002553 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002555 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002556 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002557 hdc = GetDC(hwndChild);
2558 rScreen.left = 0;
2559 rScreen.top = 0;
2560 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2561 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2562 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002563 }
2564
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002565 xNew = rParent.left + ((wParent - wChild) / 2);
2566 if (xNew < rScreen.left)
2567 xNew = rScreen.left;
2568 else if ((xNew + wChild) > rScreen.right)
2569 xNew = rScreen.right - wChild;
2570
2571 yNew = rParent.top + ((hParent - hChild) / 2);
2572 if (yNew < rScreen.top)
2573 yNew = rScreen.top;
2574 else if ((yNew + hChild) > rScreen.bottom)
2575 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576
2577 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2578 SWP_NOSIZE | SWP_NOZORDER);
2579}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002580#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002581
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582#if defined(FEAT_TOOLBAR) || defined(PROTO)
2583 void
2584gui_mch_show_toolbar(int showit)
2585{
2586 if (s_toolbarhwnd == NULL)
2587 return;
2588
2589 if (showit)
2590 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002591 // Enable unicode support
2592 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2593 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 ShowWindow(s_toolbarhwnd, SW_SHOW);
2595 }
2596 else
2597 ShowWindow(s_toolbarhwnd, SW_HIDE);
2598}
2599
Bram Moolenaar734a8672019-12-02 22:49:38 +01002600// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002601# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602
2603#endif
2604
2605#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2606 static void
2607add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2608{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002609 WCHAR *wn;
2610 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002611
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002612 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002614 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002615
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002616 infow.cbSize = sizeof(infow);
2617 infow.fMask = MIIM_TYPE | MIIM_ID;
2618 infow.wID = item_id;
2619 infow.fType = MFT_STRING;
2620 infow.dwTypeData = wn;
2621 infow.cch = (UINT)wcslen(wn);
2622 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2623 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624}
2625
2626 static void
2627show_tabline_popup_menu(void)
2628{
2629 HMENU tab_pmenu;
2630 long rval;
2631 POINT pt;
2632
Bram Moolenaar734a8672019-12-02 22:49:38 +01002633 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002634 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002635 return;
2636
2637 tab_pmenu = CreatePopupMenu();
2638 if (tab_pmenu == NULL)
2639 return;
2640
2641 if (first_tabpage->tp_next != NULL)
2642 add_tabline_popup_menu_entry(tab_pmenu,
2643 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2644 add_tabline_popup_menu_entry(tab_pmenu,
2645 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2646 add_tabline_popup_menu_entry(tab_pmenu,
2647 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2648
2649 GetCursorPos(&pt);
2650 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2651 NULL);
2652
2653 DestroyMenu(tab_pmenu);
2654
Bram Moolenaar734a8672019-12-02 22:49:38 +01002655 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002656 if (rval > 0)
2657 {
2658 TCHITTESTINFO htinfo;
2659 int idx;
2660
2661 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2662 return;
2663
2664 htinfo.pt.x = pt.x;
2665 htinfo.pt.y = pt.y;
2666 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2667 if (idx == -1)
2668 idx = 0;
2669 else
2670 idx += 1;
2671
2672 send_tabline_menu_event(idx, (int)rval);
2673 }
2674}
2675
2676/*
2677 * Show or hide the tabline.
2678 */
2679 void
2680gui_mch_show_tabline(int showit)
2681{
2682 if (s_tabhwnd == NULL)
2683 return;
2684
2685 if (!showit != !showing_tabline)
2686 {
2687 if (showit)
2688 ShowWindow(s_tabhwnd, SW_SHOW);
2689 else
2690 ShowWindow(s_tabhwnd, SW_HIDE);
2691 showing_tabline = showit;
2692 }
2693}
2694
2695/*
2696 * Return TRUE when tabline is displayed.
2697 */
2698 int
2699gui_mch_showing_tabline(void)
2700{
2701 return s_tabhwnd != NULL && showing_tabline;
2702}
2703
2704/*
2705 * Update the labels of the tabline.
2706 */
2707 void
2708gui_mch_update_tabline(void)
2709{
2710 tabpage_T *tp;
2711 TCITEM tie;
2712 int nr = 0;
2713 int curtabidx = 0;
2714 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002715 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716
2717 if (s_tabhwnd == NULL)
2718 return;
2719
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002720 // Enable unicode support
2721 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722
2723 tie.mask = TCIF_TEXT;
2724 tie.iImage = -1;
2725
Bram Moolenaar734a8672019-12-02 22:49:38 +01002726 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002727 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2728
Bram Moolenaar734a8672019-12-02 22:49:38 +01002729 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2731 {
2732 if (tp == curtab)
2733 curtabidx = nr;
2734
2735 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2736 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002737 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 tie.pszText = "-Empty-";
2739 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2740 tabadded = 1;
2741 }
2742
2743 get_tabline_label(tp, FALSE);
2744 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002746 wstr = enc_to_utf16(NameBuff, NULL);
2747 if (wstr != NULL)
2748 {
2749 TCITEMW tiw;
2750
2751 tiw.mask = TCIF_TEXT;
2752 tiw.iImage = -1;
2753 tiw.pszText = wstr;
2754 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2755 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002756 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002757 }
2758
Bram Moolenaar734a8672019-12-02 22:49:38 +01002759 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2761 TabCtrl_DeleteItem(s_tabhwnd, nr);
2762
2763 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2764 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2765
Bram Moolenaar734a8672019-12-02 22:49:38 +01002766 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2768 RedrawWindow(s_tabhwnd, NULL, NULL,
2769 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2770
2771 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2772 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2773}
2774
2775/*
2776 * Set the current tab to "nr". First tab is 1.
2777 */
2778 void
2779gui_mch_set_curtab(int nr)
2780{
2781 if (s_tabhwnd == NULL)
2782 return;
2783
2784 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2785 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2786}
2787
2788#endif
2789
2790/*
2791 * ":simalt" command.
2792 */
2793 void
2794ex_simalt(exarg_T *eap)
2795{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002796 char_u *keys = eap->arg;
2797 int fill_typebuf = FALSE;
2798 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799
2800 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2801 while (*keys)
2802 {
2803 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002804 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2806 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002807 fill_typebuf = TRUE;
2808 }
2809 if (fill_typebuf)
2810 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002811 // Put a NOP in the typeahead buffer so that the message will get
2812 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002813 key_name[0] = K_SPECIAL;
2814 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002815 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002816 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002817#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002818 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002819#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002820 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002821 }
2822}
2823
2824/*
2825 * Create the find & replace dialogs.
2826 * You can't have both at once: ":find" when replace is showing, destroys
2827 * the replace dialog first, and the other way around.
2828 */
2829#ifdef MSWIN_FIND_REPLACE
2830 static void
2831initialise_findrep(char_u *initial_string)
2832{
2833 int wword = FALSE;
2834 int mcase = !p_ic;
2835 char_u *entry_text;
2836
Bram Moolenaar734a8672019-12-02 22:49:38 +01002837 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002838 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2839
2840 s_findrep_struct.hwndOwner = s_hwnd;
2841 s_findrep_struct.Flags = FR_DOWN;
2842 if (mcase)
2843 s_findrep_struct.Flags |= FR_MATCHCASE;
2844 if (wword)
2845 s_findrep_struct.Flags |= FR_WHOLEWORD;
2846 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002847 {
2848 WCHAR *p = enc_to_utf16(entry_text, NULL);
2849 if (p != NULL)
2850 {
2851 int len = s_findrep_struct.wFindWhatLen - 1;
2852
2853 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2854 s_findrep_struct.lpstrFindWhat[len] = NUL;
2855 vim_free(p);
2856 }
2857 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 vim_free(entry_text);
2859}
2860#endif
2861
2862 static void
2863set_window_title(HWND hwnd, char *title)
2864{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002865 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002866 {
2867 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002868
Bram Moolenaar734a8672019-12-02 22:49:38 +01002869 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002870 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2871 if (wbuf != NULL)
2872 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002873 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002874 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 }
2876 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002877 else
2878 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879}
2880
2881 void
2882gui_mch_find_dialog(exarg_T *eap)
2883{
2884#ifdef MSWIN_FIND_REPLACE
2885 if (s_findrep_msg != 0)
2886 {
2887 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2888 DestroyWindow(s_findrep_hwnd);
2889
2890 if (!IsWindow(s_findrep_hwnd))
2891 {
2892 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002893 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002894 }
2895
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002896 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897 (void)SetFocus(s_findrep_hwnd);
2898
2899 s_findrep_is_find = TRUE;
2900 }
2901#endif
2902}
2903
2904
2905 void
2906gui_mch_replace_dialog(exarg_T *eap)
2907{
2908#ifdef MSWIN_FIND_REPLACE
2909 if (s_findrep_msg != 0)
2910 {
2911 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2912 DestroyWindow(s_findrep_hwnd);
2913
2914 if (!IsWindow(s_findrep_hwnd))
2915 {
2916 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002917 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918 }
2919
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002920 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921 (void)SetFocus(s_findrep_hwnd);
2922
2923 s_findrep_is_find = FALSE;
2924 }
2925#endif
2926}
2927
2928
2929/*
2930 * Set visibility of the pointer.
2931 */
2932 void
2933gui_mch_mousehide(int hide)
2934{
2935 if (hide != gui.pointer_hidden)
2936 {
2937 ShowCursor(!hide);
2938 gui.pointer_hidden = hide;
2939 }
2940}
2941
2942#ifdef FEAT_MENU
2943 static void
2944gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2945{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002946 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002947 gui_mch_mousehide(FALSE);
2948
2949 (void)TrackPopupMenu(
2950 (HMENU)menu->submenu_id,
2951 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2952 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002953 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954 s_hwnd,
2955 NULL);
2956 /*
2957 * NOTE: The pop-up menu can eat the mouse up event.
2958 * We deal with this in normal.c.
2959 */
2960}
2961#endif
2962
2963/*
2964 * Got a message when the system will go down.
2965 */
2966 static void
2967_OnEndSession(void)
2968{
2969 getout_preserve_modified(1);
2970}
2971
2972/*
2973 * Get this message when the user clicks on the cross in the top right corner
2974 * of a Windows95 window.
2975 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002976 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002977_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002978{
2979 gui_shell_closed();
2980}
2981
2982/*
2983 * Get a message when the window is being destroyed.
2984 */
2985 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002986_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987{
2988 if (!destroying)
2989 _OnClose(hwnd);
2990}
2991
2992 static void
2993_OnPaint(
2994 HWND hwnd)
2995{
2996 if (!IsMinimized(hwnd))
2997 {
2998 PAINTSTRUCT ps;
2999
Bram Moolenaar734a8672019-12-02 22:49:38 +01003000 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003001 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003002
Bram Moolenaar734a8672019-12-02 22:49:38 +01003003 // prevent multi-byte characters from misprinting on an invalid
3004 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003005 if (has_mbyte)
3006 {
3007 RECT rect;
3008
3009 GetClientRect(hwnd, &rect);
3010 ps.rcPaint.left = rect.left;
3011 ps.rcPaint.right = rect.right;
3012 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003013
3014 if (!IsRectEmpty(&ps.rcPaint))
3015 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3017 ps.rcPaint.right - ps.rcPaint.left + 1,
3018 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3019 }
3020
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021 EndPaint(hwnd, &ps);
3022 }
3023}
3024
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003025 static void
3026_OnSize(
3027 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003028 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003029 int cx,
3030 int cy)
3031{
K.Takatac81e9bf2022-01-16 14:15:49 +00003032 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003033 {
3034 gui_resize_shell(cx, cy);
3035
Bram Moolenaar734a8672019-12-02 22:49:38 +01003036 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003037 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003038 }
3039}
3040
3041 static void
3042_OnSetFocus(
3043 HWND hwnd,
3044 HWND hwndOldFocus)
3045{
3046 gui_focus_change(TRUE);
3047 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003048 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003049}
3050
3051 static void
3052_OnKillFocus(
3053 HWND hwnd,
3054 HWND hwndNewFocus)
3055{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003056 if (destroying)
3057 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003058 gui_focus_change(FALSE);
3059 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003060 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003061}
3062
3063/*
3064 * Get a message when the user switches back to vim
3065 */
3066 static LRESULT
3067_OnActivateApp(
3068 HWND hwnd,
3069 BOOL fActivate,
3070 DWORD dwThreadId)
3071{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003072 // we call gui_focus_change() in _OnSetFocus()
3073 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003074 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003075}
3076
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003077 void
3078gui_mch_destroy_scrollbar(scrollbar_T *sb)
3079{
3080 DestroyWindow(sb->id);
3081}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003082
3083/*
3084 * Get current mouse coordinates in text window.
3085 */
3086 void
3087gui_mch_getmouse(int *x, int *y)
3088{
3089 RECT rct;
3090 POINT mp;
3091
3092 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003093 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003094 *x = (int)(mp.x - rct.left);
3095 *y = (int)(mp.y - rct.top);
3096}
3097
3098/*
3099 * Move mouse pointer to character at (x, y).
3100 */
3101 void
3102gui_mch_setmouse(int x, int y)
3103{
3104 RECT rct;
3105
3106 (void)GetWindowRect(s_textArea, &rct);
3107 (void)SetCursorPos(x + gui.border_offset + rct.left,
3108 y + gui.border_offset + rct.top);
3109}
3110
3111 static void
3112gui_mswin_get_valid_dimensions(
3113 int w,
3114 int h,
3115 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003116 int *valid_h,
3117 int *cols,
3118 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119{
3120 int base_width, base_height;
3121
3122 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003123 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3124 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003125 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003126 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3127 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3128 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3129 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003130 *cols = (w - base_width) / gui.char_width;
3131 *rows = (h - base_height) / gui.char_height;
3132 *valid_w = base_width + *cols * gui.char_width;
3133 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003134}
3135
3136 void
3137gui_mch_flash(int msec)
3138{
3139 RECT rc;
3140
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003141#if defined(FEAT_DIRECTX)
3142 if (IS_ENABLE_DIRECTX())
3143 DWriteContext_Flush(s_dwc);
3144#endif
3145
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003146 /*
3147 * Note: InvertRect() excludes right and bottom of rectangle.
3148 */
3149 rc.left = 0;
3150 rc.top = 0;
3151 rc.right = gui.num_cols * gui.char_width;
3152 rc.bottom = gui.num_rows * gui.char_height;
3153 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003154 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003155
Bram Moolenaar734a8672019-12-02 22:49:38 +01003156 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003157
3158 InvertRect(s_hdc, &rc);
3159}
3160
3161/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162 * Check if the specified point is on-screen. (multi-monitor aware)
3163 */
3164 static BOOL
3165is_point_onscreen(int x, int y)
3166{
3167 POINT pt = {x, y};
3168
3169 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3170}
3171
3172/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003173 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003174 *
3175 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3176 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3177 * only works when the whole window is on-screen.
3178 */
3179 static BOOL
3180is_window_onscreen(HWND hwnd)
3181{
3182 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003183 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003184
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003185 GetClientRect(hwnd, &rc);
3186 p1.x = rc.left;
3187 p1.y = rc.top;
3188 p2.x = rc.right - 1;
3189 p2.y = rc.bottom - 1;
3190 ClientToScreen(hwnd, &p1);
3191 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003192
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003193 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003194 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003195 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003196 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003197 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003198 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003199 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003200 return FALSE;
3201 return TRUE;
3202}
3203
3204/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205 * Return flags used for scrolling.
3206 * The SW_INVALIDATE is required when part of the window is covered or
3207 * off-screen. Refer to MS KB Q75236.
3208 */
3209 static int
3210get_scroll_flags(void)
3211{
3212 HWND hwnd;
3213 RECT rcVim, rcOther, rcDest;
3214
Bram Moolenaar185577e2020-10-29 20:08:21 +01003215 // Check if the window is (partly) off-screen.
3216 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003217 return SW_INVALIDATE;
3218
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003219 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003220 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3222 if (IsWindowVisible(hwnd))
3223 {
3224 GetWindowRect(hwnd, &rcOther);
3225 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3226 return SW_INVALIDATE;
3227 }
3228 return 0;
3229}
3230
3231/*
3232 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3233 * may not be scrolled out properly.
3234 * For gVim, when _OnScroll() is repeated, the character at the
3235 * previous cursor position may be left drawn after scroll.
3236 * The problem can be avoided by calling GetPixel() to get a pixel in
3237 * the region before ScrollWindowEx().
3238 */
3239 static void
3240intel_gpu_workaround(void)
3241{
3242 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3243}
3244
3245/*
3246 * Delete the given number of lines from the given row, scrolling up any
3247 * text further down within the scroll region.
3248 */
3249 void
3250gui_mch_delete_lines(
3251 int row,
3252 int num_lines)
3253{
3254 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003255
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256 rc.left = FILL_X(gui.scroll_region_left);
3257 rc.right = FILL_X(gui.scroll_region_right + 1);
3258 rc.top = FILL_Y(row);
3259 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3260
Bram Moolenaar92467d32017-12-05 13:22:16 +01003261#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003262 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003263 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003264 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003265 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003266 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003267#endif
3268 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003269#if defined(FEAT_DIRECTX)
3270 if (IS_ENABLE_DIRECTX())
3271 DWriteContext_Flush(s_dwc);
3272#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003273 intel_gpu_workaround();
3274 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003276 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003277 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278
Bram Moolenaar734a8672019-12-02 22:49:38 +01003279 // This seems to be required to avoid the cursor disappearing when
3280 // scrolling such that the cursor ends up in the top-left character on
3281 // the screen... But why? (Webb)
3282 // It's probably fixed by disabling drawing the cursor while scrolling.
3283 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284
3285 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3286 gui.scroll_region_left,
3287 gui.scroll_region_bot, gui.scroll_region_right);
3288}
3289
3290/*
3291 * Insert the given number of lines before the given row, scrolling down any
3292 * following text within the scroll region.
3293 */
3294 void
3295gui_mch_insert_lines(
3296 int row,
3297 int num_lines)
3298{
3299 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003300
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003301 rc.left = FILL_X(gui.scroll_region_left);
3302 rc.right = FILL_X(gui.scroll_region_right + 1);
3303 rc.top = FILL_Y(row);
3304 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003305
3306#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003307 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003308 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003309 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003310 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003311 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003312#endif
3313 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003314#if defined(FEAT_DIRECTX)
3315 if (IS_ENABLE_DIRECTX())
3316 DWriteContext_Flush(s_dwc);
3317#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003318 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003319 // The SW_INVALIDATE is required when part of the window is covered or
3320 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003321 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003322 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003323 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003324 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326 gui_clear_block(row, gui.scroll_region_left,
3327 row + num_lines - 1, gui.scroll_region_right);
3328}
3329
3330
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003332gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003333{
3334#if defined(FEAT_DIRECTX)
3335 DWriteContext_Close(s_dwc);
3336 DWrite_Final();
3337 s_dwc = NULL;
3338#endif
3339
3340 ReleaseDC(s_textArea, s_hdc);
3341 DeleteObject(s_brush);
3342
3343#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003344 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3346#endif
3347
Bram Moolenaar734a8672019-12-02 22:49:38 +01003348 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 if (s_hwnd != NULL)
3350 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003351 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 DestroyWindow(s_hwnd);
3353 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354}
3355
3356 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003357logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003358{
3359 char *p;
3360 char *res;
3361 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003362 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003363 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003364 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003366 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3367 if (font_name == NULL)
3368 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003370 quality_name = quality_id2name((int)lf.lfQuality);
3371
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003372 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003373 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003374 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375 if (res != NULL)
3376 {
3377 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003378 // make a normal font string out of the lf thing:
3379 points = pixels_to_points(
3380 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3381 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3382 sprintf((char *)p, "%s:h%d", font_name, points);
3383 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003384 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385 while (*p)
3386 {
3387 if (*p == ' ')
3388 *p = '_';
3389 ++p;
3390 }
3391 if (lf.lfItalic)
3392 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003393 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394 STRCAT(p, ":b");
3395 if (lf.lfUnderline)
3396 STRCAT(p, ":u");
3397 if (lf.lfStrikeOut)
3398 STRCAT(p, ":s");
3399 if (charset_name != NULL)
3400 {
3401 STRCAT(p, ":c");
3402 STRCAT(p, charset_name);
3403 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003404 if (quality_name != NULL)
3405 {
3406 STRCAT(p, ":q");
3407 STRCAT(p, quality_name);
3408 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003409 }
3410
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003411 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412 return (char_u *)res;
3413}
3414
3415
3416#ifdef FEAT_MBYTE_IME
3417/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003418 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003419 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 */
3421 static void
3422update_im_font(void)
3423{
K.Takatac81e9bf2022-01-16 14:15:49 +00003424 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003425
3426 if (p_guifontwide != NULL && *p_guifontwide != NUL
3427 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003428 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003429 norm_logfont = lf_wide;
3430 else
3431 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003432
3433 lf = norm_logfont;
3434 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3435 // Work around when PerMonitorV2 is not enabled in the process level.
3436 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3437 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003438}
3439#endif
3440
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003441/*
3442 * Handler of gui.wide_font (p_guifontwide) changed notification.
3443 */
3444 void
3445gui_mch_wide_font_changed(void)
3446{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003447 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003448
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003449#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003450 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003451#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452
3453 gui_mch_free_font(gui.wide_ital_font);
3454 gui.wide_ital_font = NOFONT;
3455 gui_mch_free_font(gui.wide_bold_font);
3456 gui.wide_bold_font = NOFONT;
3457 gui_mch_free_font(gui.wide_boldital_font);
3458 gui.wide_boldital_font = NOFONT;
3459
3460 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003461 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462 {
3463 if (!lf.lfItalic)
3464 {
3465 lf.lfItalic = TRUE;
3466 gui.wide_ital_font = get_font_handle(&lf);
3467 lf.lfItalic = FALSE;
3468 }
3469 if (lf.lfWeight < FW_BOLD)
3470 {
3471 lf.lfWeight = FW_BOLD;
3472 gui.wide_bold_font = get_font_handle(&lf);
3473 if (!lf.lfItalic)
3474 {
3475 lf.lfItalic = TRUE;
3476 gui.wide_boldital_font = get_font_handle(&lf);
3477 }
3478 }
3479 }
3480}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481
3482/*
3483 * Initialise vim to use the font with the given name.
3484 * Return FAIL if the font could not be loaded, OK otherwise.
3485 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003486 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003487gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488{
K.Takatac81e9bf2022-01-16 14:15:49 +00003489 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003490 GuiFont font = NOFONT;
3491 char_u *p;
3492
Bram Moolenaar734a8672019-12-02 22:49:38 +01003493 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003494 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003495 {
3496 lfOrig = lf;
3497 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003499 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 if (font == NOFONT)
3501 return FAIL;
3502
3503 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003504 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003505#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 norm_logfont = lf;
3507 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003508 if (!s_in_dpichanged)
3509 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510#endif
3511 gui_mch_free_font(gui.norm_font);
3512 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003513 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 GetFontSize(font);
3515
K.Takatac81e9bf2022-01-16 14:15:49 +00003516 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003517 if (p != NULL)
3518 {
3519 hl_set_font_name(p);
3520
Bram Moolenaar734a8672019-12-02 22:49:38 +01003521 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3523 {
3524 vim_free(p_guifont);
3525 p_guifont = p;
3526 }
3527 else
3528 vim_free(p);
3529 }
3530
3531 gui_mch_free_font(gui.ital_font);
3532 gui.ital_font = NOFONT;
3533 gui_mch_free_font(gui.bold_font);
3534 gui.bold_font = NOFONT;
3535 gui_mch_free_font(gui.boldital_font);
3536 gui.boldital_font = NOFONT;
3537
3538 if (!lf.lfItalic)
3539 {
3540 lf.lfItalic = TRUE;
3541 gui.ital_font = get_font_handle(&lf);
3542 lf.lfItalic = FALSE;
3543 }
3544 if (lf.lfWeight < FW_BOLD)
3545 {
3546 lf.lfWeight = FW_BOLD;
3547 gui.bold_font = get_font_handle(&lf);
3548 if (!lf.lfItalic)
3549 {
3550 lf.lfItalic = TRUE;
3551 gui.boldital_font = get_font_handle(&lf);
3552 }
3553 }
3554
3555 return OK;
3556}
3557
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003558/*
3559 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003560 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561 */
3562 int
3563gui_mch_maximized(void)
3564{
3565 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003566 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003567
3568 wp.length = sizeof(WINDOWPLACEMENT);
3569 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003570 {
3571 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003573 && wp.flags == WPF_RESTORETOMAXIMIZED))
3574 return TRUE;
3575 if (wp.showCmd == SW_SHOWMINIMIZED)
3576 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003578 // Assume the window is snapped when the sizes from two APIs differ.
3579 GetWindowRect(s_hwnd, &rc);
3580 if ((rc.right - rc.left !=
3581 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3582 || (rc.bottom - rc.top !=
3583 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3584 return TRUE;
3585 }
3586 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587}
3588
3589/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003590 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3591 * is set. Compute the new Rows and Columns. This is like resizing the
3592 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003593 */
3594 void
3595gui_mch_newfont(void)
3596{
3597 RECT rect;
3598
3599 GetWindowRect(s_hwnd, &rect);
3600 if (win_socket_id == 0)
3601 {
3602 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003603 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3604 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003606 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3607 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3608 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3609 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610 }
3611 else
3612 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003613 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003614 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003615 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 }
3617}
3618
3619/*
3620 * Set the window title
3621 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622 void
3623gui_mch_settitle(
3624 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003625 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003626{
3627 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3628}
3629
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003630#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003631// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3632// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633static LPCSTR mshape_idcs[] =
3634{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003635 IDC_ARROW, // arrow
3636 MAKEINTRESOURCE(0), // blank
3637 IDC_IBEAM, // beam
3638 IDC_SIZENS, // updown
3639 IDC_SIZENS, // udsizing
3640 IDC_SIZEWE, // leftright
3641 IDC_SIZEWE, // lrsizing
3642 IDC_WAIT, // busy
3643 IDC_NO, // no
3644 IDC_ARROW, // crosshair
3645 IDC_ARROW, // hand1
3646 IDC_ARROW, // hand2
3647 IDC_ARROW, // pencil
3648 IDC_ARROW, // question
3649 IDC_ARROW, // right-arrow
3650 IDC_UPARROW, // up-arrow
3651 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652};
3653
3654 void
3655mch_set_mouse_shape(int shape)
3656{
3657 LPCSTR idc;
3658
3659 if (shape == MSHAPE_HIDE)
3660 ShowCursor(FALSE);
3661 else
3662 {
3663 if (shape >= MSHAPE_NUMBERED)
3664 idc = IDC_ARROW;
3665 else
3666 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003667 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668 if (!p_mh)
3669 {
3670 POINT mp;
3671
Bram Moolenaar734a8672019-12-02 22:49:38 +01003672 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003673 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674 (void)SetCursorPos(mp.x, mp.y);
3675 ShowCursor(TRUE);
3676 }
3677 }
3678}
3679#endif
3680
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003681#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003682/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683 * Wide version of convert_filter().
3684 */
3685 static WCHAR *
3686convert_filterW(char_u *s)
3687{
3688 char_u *tmp;
3689 int len;
3690 WCHAR *res;
3691
3692 tmp = convert_filter(s);
3693 if (tmp == NULL)
3694 return NULL;
3695 len = (int)STRLEN(s) + 3;
3696 res = enc_to_utf16(tmp, &len);
3697 vim_free(tmp);
3698 return res;
3699}
3700
3701/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003702 * Pop open a file browser and return the file selected, in allocated memory,
3703 * or NULL if Cancel is hit.
3704 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3705 * title - Title message for the file browser dialog.
3706 * dflt - Default name of file.
3707 * ext - Default extension to be added to files without extensions.
3708 * initdir - directory in which to open the browser (NULL = current dir)
3709 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003711 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003712gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003713 int saving,
3714 char_u *title,
3715 char_u *dflt,
3716 char_u *ext,
3717 char_u *initdir,
3718 char_u *filter)
3719{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003720 // We always use the wide function. This means enc_to_utf16() must work,
3721 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003722 OPENFILENAMEW fileStruct;
3723 WCHAR fileBuf[MAXPATHL];
3724 WCHAR *wp;
3725 int i;
3726 WCHAR *titlep = NULL;
3727 WCHAR *extp = NULL;
3728 WCHAR *initdirp = NULL;
3729 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003730 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003731 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732
3733 if (dflt == NULL)
3734 fileBuf[0] = NUL;
3735 else
3736 {
3737 wp = enc_to_utf16(dflt, NULL);
3738 if (wp == NULL)
3739 fileBuf[0] = NUL;
3740 else
3741 {
3742 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3743 fileBuf[i] = wp[i];
3744 fileBuf[i] = NUL;
3745 vim_free(wp);
3746 }
3747 }
3748
Bram Moolenaar734a8672019-12-02 22:49:38 +01003749 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003750 filterp = convert_filterW(filter);
3751
Bram Moolenaara80faa82020-04-12 19:37:17 +02003752 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003753# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003754 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003755 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003756# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003758# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759
3760 if (title != NULL)
3761 titlep = enc_to_utf16(title, NULL);
3762 fileStruct.lpstrTitle = titlep;
3763
3764 if (ext != NULL)
3765 extp = enc_to_utf16(ext, NULL);
3766 fileStruct.lpstrDefExt = extp;
3767
3768 fileStruct.lpstrFile = fileBuf;
3769 fileStruct.nMaxFile = MAXPATHL;
3770 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003771 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3772 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 if (initdir != NULL && *initdir != NUL)
3774 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003775 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 initdirp = enc_to_utf16(initdir, NULL);
3777 if (initdirp != NULL)
3778 {
3779 for (wp = initdirp; *wp != NUL; ++wp)
3780 if (*wp == '/')
3781 *wp = '\\';
3782 }
3783 fileStruct.lpstrInitialDir = initdirp;
3784 }
3785
3786 /*
3787 * TODO: Allow selection of multiple files. Needs another arg to this
3788 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3789 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3790 * files that don't exist yet, so I haven't put it in. What about
3791 * OFN_PATHMUSTEXIST?
3792 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3793 */
3794 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003795# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 if (curbuf->b_p_bin)
3797 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003798# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003800 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003802 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803
3804 vim_free(filterp);
3805 vim_free(initdirp);
3806 vim_free(titlep);
3807 vim_free(extp);
3808
K.Takata14b8d6a2022-01-20 15:05:22 +00003809 if (!ret)
3810 return NULL;
3811
3812 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003813 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003814 if (p == NULL)
3815 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003816
Bram Moolenaar734a8672019-12-02 22:49:38 +01003817 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003818 SetFocus(s_hwnd);
3819
Bram Moolenaar734a8672019-12-02 22:49:38 +01003820 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003821 q = vim_strsave(shorten_fname1(p));
3822 vim_free(p);
3823 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003824}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825
3826
3827/*
3828 * Convert the string s to the proper format for a filter string by replacing
3829 * the \t and \n delimiters with \0.
3830 * Returns the converted string in allocated memory.
3831 *
3832 * Keep in sync with convert_filterW() above!
3833 */
3834 static char_u *
3835convert_filter(char_u *s)
3836{
3837 char_u *res;
3838 unsigned s_len = (unsigned)STRLEN(s);
3839 unsigned i;
3840
3841 res = alloc(s_len + 3);
3842 if (res != NULL)
3843 {
3844 for (i = 0; i < s_len; ++i)
3845 if (s[i] == '\t' || s[i] == '\n')
3846 res[i] = '\0';
3847 else
3848 res[i] = s[i];
3849 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003850 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 res[s_len + 1] = NUL;
3852 res[s_len + 2] = NUL;
3853 }
3854 return res;
3855}
3856
3857/*
3858 * Select a directory.
3859 */
3860 char_u *
3861gui_mch_browsedir(char_u *title, char_u *initdir)
3862{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003863 // We fake this: Use a filter that doesn't select anything and a default
3864 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003865 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3866 initdir, (char_u *)_("Directory\t*.nothing\n"));
3867}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003868#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003869
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870 static void
3871_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003872 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003873 HDROP hDrop)
3874{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003875#define BUFPATHLEN _MAX_PATH
3876#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003877 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 char szFile[BUFPATHLEN];
3879 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3880 UINT i;
3881 char_u **fnames;
3882 POINT pt;
3883 int_u modifiers = 0;
3884
Bram Moolenaar734a8672019-12-02 22:49:38 +01003885 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003886 DragQueryPoint(hDrop, &pt);
3887 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3888
3889 reset_VIsual();
3890
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003891 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003892
3893 if (fnames != NULL)
3894 for (i = 0; i < cFiles; ++i)
3895 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003896 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3897 fnames[i] = utf16_to_enc(wszFile, NULL);
3898 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003899 {
3900 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3901 fnames[i] = vim_strsave((char_u *)szFile);
3902 }
3903 }
3904
3905 DragFinish(hDrop);
3906
3907 if (fnames != NULL)
3908 {
LemonBoy45684c62022-04-24 15:46:42 +01003909 int kbd_modifiers = get_active_modifiers();
3910
3911 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003912 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003913 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003915 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003916 modifiers |= MOUSE_ALT;
3917
3918 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3919
3920 s_need_activate = TRUE;
3921 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922}
3923
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924 static int
3925_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003926 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003927 HWND hwndCtl,
3928 UINT code,
3929 int pos)
3930{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003931 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003932 scrollbar_T *sb, *sb_info;
3933 long val;
3934 int dragging = FALSE;
3935 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003936 SCROLLINFO si;
3937
3938 si.cbSize = sizeof(si);
3939 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003940
3941 sb = gui_mswin_find_scrollbar(hwndCtl);
3942 if (sb == NULL)
3943 return 0;
3944
Bram Moolenaar734a8672019-12-02 22:49:38 +01003945 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003946 {
3947 /*
3948 * Careful: need to get scrollbar info out of first (left) scrollbar
3949 * for window, but keep real scrollbar too because we must pass it to
3950 * gui_drag_scrollbar().
3951 */
3952 sb_info = &sb->wp->w_scrollbars[0];
3953 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003954 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003955 sb_info = sb;
3956 val = sb_info->value;
3957
3958 switch (code)
3959 {
3960 case SB_THUMBTRACK:
3961 val = pos;
3962 dragging = TRUE;
3963 if (sb->scroll_shift > 0)
3964 val <<= sb->scroll_shift;
3965 break;
3966 case SB_LINEDOWN:
3967 val++;
3968 break;
3969 case SB_LINEUP:
3970 val--;
3971 break;
3972 case SB_PAGEDOWN:
3973 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3974 break;
3975 case SB_PAGEUP:
3976 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3977 break;
3978 case SB_TOP:
3979 val = 0;
3980 break;
3981 case SB_BOTTOM:
3982 val = sb_info->max;
3983 break;
3984 case SB_ENDSCROLL:
3985 if (prev_code == SB_THUMBTRACK)
3986 {
3987 /*
3988 * "pos" only gives us 16-bit data. In case of large file,
3989 * use GetScrollPos() which returns 32-bit. Unfortunately it
3990 * is not valid while the scrollbar is being dragged.
3991 */
3992 val = GetScrollPos(hwndCtl, SB_CTL);
3993 if (sb->scroll_shift > 0)
3994 val <<= sb->scroll_shift;
3995 }
3996 break;
3997
3998 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003999 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004000 return 0;
4001 }
4002 prev_code = code;
4003
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004004 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4005 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004006
4007 /*
4008 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4009 */
4010 if (sb->wp != NULL)
4011 {
4012 scrollbar_T *sba = sb->wp->w_scrollbars;
4013 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4014
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004015 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004016 }
4017
Bram Moolenaar734a8672019-12-02 22:49:38 +01004018 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004019 s_busy_processing = TRUE;
4020
Bram Moolenaar734a8672019-12-02 22:49:38 +01004021 // When "allow_scrollbar" is FALSE still need to remember the new
4022 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023 dont_scroll = !allow_scrollbar;
4024
Bram Moolenaara338adc2018-01-31 20:51:47 +01004025 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004026 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004027 mch_enable_flush();
4028 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004029
4030 s_busy_processing = FALSE;
4031 dont_scroll = dont_scroll_save;
4032
4033 return 0;
4034}
4035
4036
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#ifdef FEAT_XPM_W32
4038# include "xpm_w32.h"
4039#endif
4040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaar734a8672019-12-02 22:49:38 +01004042// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#define TEAROFF_PADDING_X 2
4044#define TEAROFF_BUTTON_PAD_X 8
4045#define TEAROFF_MIN_WIDTH 200
4046#define TEAROFF_SUBMENU_LABEL ">>"
4047#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4048
4049
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004050#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051# define ID_BEVAL_TOOLTIP 200
4052# define BEVAL_TEXT_LEN MAXPATHL
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004055static UINT_PTR beval_timer_id = 0;
4056static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004057#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004058
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004059
Bram Moolenaar734a8672019-12-02 22:49:38 +01004060// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062#ifdef FEAT_MENU
4063static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066/*
4067 * Use the system font for dialogs and tear-off menus. Remove this line to
4068 * use DLG_FONT_NAME.
4069 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004070#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073#define VIM_CLASSW L"Vim"
4074
Bram Moolenaar734a8672019-12-02 22:49:38 +01004075// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4076// thus there should be room for every dialog. For tearoffs it's made bigger
4077// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#define DLG_ALLOC_SIZE 16 * 1024
4079
4080/*
4081 * stuff for dialogs, menus, tearoffs etc.
4082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083static PWORD
4084add_dialog_element(
4085 PWORD p,
4086 DWORD lStyle,
4087 WORD x,
4088 WORD y,
4089 WORD w,
4090 WORD h,
4091 WORD Id,
4092 WORD clss,
4093 const char *caption);
4094static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004095static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004096#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099static void get_dialog_font_metrics(void);
4100
4101static int dialog_default_button = -1;
4102
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#ifdef FEAT_TOOLBAR
4104static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004105static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004106static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004108#else
4109# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#endif
4111
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004112#ifdef FEAT_GUI_TABLINE
4113static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004114static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004115#endif
4116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#ifdef FEAT_MBYTE_IME
4118static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4119static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4120#endif
4121#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4122# ifdef NOIME
4123typedef struct tagCOMPOSITIONFORM {
4124 DWORD dwStyle;
4125 POINT ptCurrentPos;
4126 RECT rcArea;
4127} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4128typedef HANDLE HIMC;
4129# endif
4130
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004131static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004132static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4133static HIMC (WINAPI *pImmGetContext)(HWND);
4134static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4135static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4136static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4137static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004138static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4139static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004140static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4141static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004142static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143static void dyn_imm_load(void);
4144#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145# define pImmGetCompositionStringW ImmGetCompositionStringW
4146# define pImmGetContext ImmGetContext
4147# define pImmAssociateContext ImmAssociateContext
4148# define pImmReleaseContext ImmReleaseContext
4149# define pImmGetOpenStatus ImmGetOpenStatus
4150# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004151# define pImmGetCompositionFontW ImmGetCompositionFontW
4152# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153# define pImmSetCompositionWindow ImmSetCompositionWindow
4154# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004155# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#endif
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158#ifdef FEAT_MENU
4159/*
4160 * Figure out how high the menu bar is at the moment.
4161 */
4162 static int
4163gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004164 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
4166 static int old_menu_height = -1;
4167
4168 RECT rc1, rc2;
4169 int num;
4170 int menu_height;
4171
4172 if (gui.menu_is_active)
4173 num = GetMenuItemCount(s_menuBar);
4174 else
4175 num = 0;
4176
4177 if (num == 0)
4178 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004179 else if (IsMinimized(s_hwnd))
4180 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004181 // The height of the menu cannot be determined while the window is
4182 // minimized. Take the previous height if the menu is changed in that
4183 // state, to avoid that Vim's vertical window size accidentally
4184 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004185 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
4188 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004189 /*
4190 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4191 * seem to have been set yet, so menu wraps in default window
4192 * width which is very narrow. Instead just return height of a
4193 * single menu item. Will still be wrong when the menu really
4194 * should wrap over more than one line.
4195 */
4196 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4197 if (gui.starting)
4198 menu_height = rc1.bottom - rc1.top + 1;
4199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004201 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4202 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204 }
4205
4206 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004207 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004208 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 return menu_height;
4211}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004212#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214
4215/*
4216 * Setup for the Intellimouse
4217 */
LemonBoyc27747e2022-05-07 12:25:40 +01004218 static long
4219mouse_vertical_scroll_step(void)
4220{
4221 UINT val;
4222 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4223 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4224 return 3; // Safe default;
4225}
4226
4227 static long
4228mouse_horizontal_scroll_step(void)
4229{
4230 UINT val;
4231 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4232 return (long)val;
4233 return 3; // Safe default;
4234}
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 static void
4237init_mouse_wheel(void)
4238{
LemonBoyc27747e2022-05-07 12:25:40 +01004239 // Get the default values for the horizontal and vertical scroll steps from
4240 // the system.
4241 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4242 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243}
4244
Bram Moolenaarae20f342019-11-05 21:09:23 +01004245/*
LemonBoya773d842022-05-10 20:54:46 +01004246 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004249_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
LemonBoy365d8f72022-05-05 19:23:07 +01004251 int button;
4252 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004253 int modifiers = 0;
4254 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004255 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4256 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaarae20f342019-11-05 21:09:23 +01004258 wp = gui_mouse_window(FIND_POPUP);
4259
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004260#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004261 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004262 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004263 cmdarg_T cap;
4264 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004265
Bram Moolenaarae20f342019-11-05 21:09:23 +01004266 // Mouse hovers over popup window, scroll it if possible.
4267 mouse_row = wp->w_winrow;
4268 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004269 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004270 if (horizontal)
4271 {
4272 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4273 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4274 }
4275 else
4276 {
4277 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4278 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4279 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004280 clear_oparg(&oa);
4281 cap.oap = &oa;
4282 nv_mousescroll(&cap);
4283 update_screen(0);
4284 setcursor();
4285 out_flush();
4286 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004287 }
4288#endif
4289
Bram Moolenaarae20f342019-11-05 21:09:23 +01004290 if (wp == NULL || !p_scf)
4291 wp = curwin;
4292
LemonBoy365d8f72022-05-05 19:23:07 +01004293 // Translate the scroll event into an event that Vim can process so that
4294 // the user has a chance to map the scrollwheel buttons.
4295 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004296 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 else
LemonBoy365d8f72022-05-05 19:23:07 +01004298 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004299
4300 kbd_modifiers = get_active_modifiers();
4301
4302 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4303 modifiers |= MOUSE_SHIFT;
4304 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4305 modifiers |= MOUSE_CTRL;
4306 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4307 modifiers |= MOUSE_ALT;
4308
LemonBoya773d842022-05-10 20:54:46 +01004309 // The cursor position is relative to the upper-left corner of the screen.
4310 pt.x = GET_X_LPARAM(lParam);
4311 pt.y = GET_Y_LPARAM(lParam);
4312 ScreenToClient(s_textArea, &pt);
4313
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004314 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315}
4316
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004317#ifdef USE_SYSMENU_FONT
4318/*
4319 * Get Menu Font.
4320 * Return OK or FAIL.
4321 */
4322 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004323gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004324{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004325 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004326
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004327 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4328 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004329 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004330 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004331 &nm,
4332 0))
4333 return FAIL;
4334 *lf = nm.lfMenuFont;
4335 return OK;
4336}
4337#endif
4338
4339
4340#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4341/*
4342 * Set the GUI tabline font to the system menu font
4343 */
4344 static void
4345set_tabline_font(void)
4346{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004347 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004348 HFONT font;
4349 HWND hwnd;
4350 HDC hdc;
4351 HFONT hfntOld;
4352 TEXTMETRIC tm;
4353
4354 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4355 return;
4356
K.Takatac81e9bf2022-01-16 14:15:49 +00004357 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004358 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004359
4360 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4361
4362 /*
4363 * Compute the height of the font used for the tab text
4364 */
4365 hwnd = GetDesktopWindow();
4366 hdc = GetWindowDC(hwnd);
4367 hfntOld = SelectFont(hdc, font);
4368
4369 GetTextMetrics(hdc, &tm);
4370
4371 SelectFont(hdc, hfntOld);
4372 ReleaseDC(hwnd, hdc);
4373
4374 /*
4375 * The space used by the tab border and the space between the tab label
4376 * and the tab border is included as 7.
4377 */
4378 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4379}
K.Takatac81e9bf2022-01-16 14:15:49 +00004380#else
4381# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004382#endif
4383
Bram Moolenaar520470a2005-06-16 21:59:56 +00004384/*
4385 * Invoked when a setting was changed.
4386 */
4387 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004388_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004389{
LemonBoy365d8f72022-05-05 19:23:07 +01004390 switch (param)
4391 {
4392 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004393 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004394 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004395 case SPI_SETWHEELSCROLLCHARS:
4396 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004397 break;
4398 case SPI_SETNONCLIENTMETRICS:
4399 set_tabline_font();
4400 break;
4401 default:
4402 break;
4403 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004404 return 0;
4405}
4406
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407#ifdef FEAT_NETBEANS_INTG
4408 static void
4409_OnWindowPosChanged(
4410 HWND hwnd,
4411 const LPWINDOWPOS lpwpos)
4412{
4413 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004414 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4417 || lpwpos->cx != cx || lpwpos->cy != cy))
4418 {
4419 x = lpwpos->x;
4420 y = lpwpos->y;
4421 cx = lpwpos->cx;
4422 cy = lpwpos->cy;
4423 netbeans_frame_moved(x, y);
4424 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004425 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004426 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427}
4428#endif
4429
K.Takata4f2417f2021-06-05 16:25:32 +02004430
4431static HWND hwndTip = NULL;
4432
4433 static void
4434show_sizing_tip(int cols, int rows)
4435{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004436 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004437 char buf[32];
4438
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004439 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004440 ti.hwnd = s_hwnd;
4441 ti.uId = (UINT_PTR)s_hwnd;
4442 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4443 ti.lpszText = buf;
4444 sprintf(buf, "%dx%d", cols, rows);
4445 if (hwndTip == NULL)
4446 {
4447 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4448 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4449 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4450 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4451 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4452 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4453 }
4454 else
4455 {
4456 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4457 }
4458 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4459}
4460
4461 static void
4462destroy_sizing_tip(void)
4463{
4464 if (hwndTip != NULL)
4465 {
4466 DestroyWindow(hwndTip);
4467 hwndTip = NULL;
4468 }
4469}
4470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 static int
4472_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 UINT fwSide,
4474 LPRECT lprc)
4475{
4476 int w, h;
4477 int valid_w, valid_h;
4478 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004479 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 w = lprc->right - lprc->left;
4482 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004483 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 w_offset = w - valid_w;
4485 h_offset = h - valid_h;
4486
4487 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4488 || fwSide == WMSZ_BOTTOMLEFT)
4489 lprc->left += w_offset;
4490 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4491 || fwSide == WMSZ_BOTTOMRIGHT)
4492 lprc->right -= w_offset;
4493
4494 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4495 || fwSide == WMSZ_TOPRIGHT)
4496 lprc->top += h_offset;
4497 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4498 || fwSide == WMSZ_BOTTOMRIGHT)
4499 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004500
4501 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 return TRUE;
4503}
4504
K.Takata92000e22022-01-20 15:10:57 +00004505#ifdef FEAT_GUI_TABLINE
4506 static void
4507_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4508{
4509 if (gui_mch_showing_tabline())
4510 {
4511 POINT pt;
4512 RECT rect;
4513
4514 /*
4515 * If the cursor is on the tabline, display the tab menu
4516 */
4517 GetCursorPos(&pt);
4518 GetWindowRect(s_textArea, &rect);
4519 if (pt.y < rect.top)
4520 {
4521 show_tabline_popup_menu();
4522 return;
4523 }
4524 }
4525 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4526}
4527
4528 static void
4529_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4530{
4531 /*
4532 * If the user double clicked the tabline, create a new tab
4533 */
4534 if (gui_mch_showing_tabline())
4535 {
4536 POINT pt;
4537 RECT rect;
4538
4539 GetCursorPos(&pt);
4540 GetWindowRect(s_textArea, &rect);
4541 if (pt.y < rect.top)
4542 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4543 }
4544 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4545}
4546#endif
4547
4548 static UINT
4549_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4550{
4551 UINT result;
4552 int x, y;
4553
4554 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4555 if (result != HTCLIENT)
4556 return result;
4557
4558#ifdef FEAT_GUI_TABLINE
4559 if (gui_mch_showing_tabline())
4560 {
4561 RECT rct;
4562
4563 // If the cursor is on the GUI tabline, don't process this event
4564 GetWindowRect(s_textArea, &rct);
4565 if (yPos < rct.top)
4566 return result;
4567 }
4568#endif
4569 (void)gui_mch_get_winpos(&x, &y);
4570 xPos -= x;
4571
4572 if (xPos < 48) // <VN> TODO should use system metric?
4573 return HTBOTTOMLEFT;
4574 else
4575 return HTBOTTOMRIGHT;
4576}
4577
4578#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4579 static LRESULT
4580_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4581{
4582 switch (hdr->code)
4583 {
4584 case TTN_GETDISPINFOW:
4585 case TTN_GETDISPINFO:
4586 {
4587 char_u *str = NULL;
4588 static void *tt_text = NULL;
4589
4590 VIM_CLEAR(tt_text);
4591
4592# ifdef FEAT_GUI_TABLINE
4593 if (gui_mch_showing_tabline()
4594 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4595 {
4596 POINT pt;
4597 /*
4598 * Mouse is over the GUI tabline. Display the
4599 * tooltip for the tab under the cursor
4600 *
4601 * Get the cursor position within the tab control
4602 */
4603 GetCursorPos(&pt);
4604 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4605 {
4606 TCHITTESTINFO htinfo;
4607 int idx;
4608
4609 /*
4610 * Get the tab under the cursor
4611 */
4612 htinfo.pt.x = pt.x;
4613 htinfo.pt.y = pt.y;
4614 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4615 if (idx != -1)
4616 {
4617 tabpage_T *tp;
4618
4619 tp = find_tabpage(idx + 1);
4620 if (tp != NULL)
4621 {
4622 get_tabline_label(tp, TRUE);
4623 str = NameBuff;
4624 }
4625 }
4626 }
4627 }
4628# endif
4629# ifdef FEAT_TOOLBAR
4630# ifdef FEAT_GUI_TABLINE
4631 else
4632# endif
4633 {
4634 UINT idButton;
4635 vimmenu_T *pMenu;
4636
4637 idButton = (UINT) hdr->idFrom;
4638 pMenu = gui_mswin_find_menu(root_menu, idButton);
4639 if (pMenu)
4640 str = pMenu->strings[MENU_INDEX_TIP];
4641 }
4642# endif
4643 if (str == NULL)
4644 break;
4645
4646 // Set the maximum width, this also enables using \n for
4647 // line break.
4648 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4649
4650 if (hdr->code == TTN_GETDISPINFOW)
4651 {
4652 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4653
4654 tt_text = enc_to_utf16(str, NULL);
4655 lpdi->lpszText = tt_text;
4656 // can't show tooltip if failed
4657 }
4658 else
4659 {
4660 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4661
4662 if (STRLEN(str) < sizeof(lpdi->szText)
4663 || ((tt_text = vim_strsave(str)) == NULL))
4664 vim_strncpy((char_u *)lpdi->szText, str,
4665 sizeof(lpdi->szText) - 1);
4666 else
4667 lpdi->lpszText = tt_text;
4668 }
4669 }
4670 break;
4671
4672# ifdef FEAT_GUI_TABLINE
4673 case TCN_SELCHANGE:
4674 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4675 {
4676 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4677 return 0L;
4678 }
4679 break;
4680
4681 case NM_RCLICK:
4682 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4683 {
4684 show_tabline_popup_menu();
4685 return 0L;
4686 }
4687 break;
4688# endif
4689
4690 default:
4691 break;
4692 }
4693 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4694}
4695#endif
4696
4697#if defined(MENUHINTS) && defined(FEAT_MENU)
4698 static LRESULT
4699_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4700{
4701 if (((UINT) HIWORD(wParam)
4702 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4703 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004704 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004705 {
4706 UINT idButton;
4707 vimmenu_T *pMenu;
4708 static int did_menu_tip = FALSE;
4709
4710 if (did_menu_tip)
4711 {
4712 msg_clr_cmdline();
4713 setcursor();
4714 out_flush();
4715 did_menu_tip = FALSE;
4716 }
4717
4718 idButton = (UINT)LOWORD(wParam);
4719 pMenu = gui_mswin_find_menu(root_menu, idButton);
4720 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4721 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4722 {
4723 ++msg_hist_off;
4724 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4725 --msg_hist_off;
4726 setcursor();
4727 out_flush();
4728 did_menu_tip = TRUE;
4729 }
4730 return 0L;
4731 }
4732 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4733}
4734#endif
4735
K.Takatac81e9bf2022-01-16 14:15:49 +00004736 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004737_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004738{
4739 s_dpi = ydpi;
4740 s_in_dpichanged = TRUE;
4741 //TRACE("DPI: %d", ydpi);
4742
4743 update_scrollbar_size();
4744 update_toolbar_size();
4745 set_tabline_font();
4746
4747 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4748 gui_get_wide_font();
4749 gui_mswin_get_menu_height(FALSE);
4750#ifdef FEAT_MBYTE_IME
4751 im_set_position(gui.row, gui.col);
4752#endif
4753 InvalidateRect(hwnd, NULL, TRUE);
4754
4755 s_in_dpichanged = FALSE;
4756 return 0L;
4757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759
4760 static LRESULT CALLBACK
4761_WndProc(
4762 HWND hwnd,
4763 UINT uMsg,
4764 WPARAM wParam,
4765 LPARAM lParam)
4766{
LemonBoya773d842022-05-10 20:54:46 +01004767 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4768 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 HandleMouseHide(uMsg, lParam);
4771
4772 s_uMsg = uMsg;
4773 s_wParam = wParam;
4774 s_lParam = lParam;
4775
4776 switch (uMsg)
4777 {
4778 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4779 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004780 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004782 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4784 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4785 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4786 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4787#ifdef FEAT_MENU
4788 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4789#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004790 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4791 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4793 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004794 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4795 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4797 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4798 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4799#ifdef FEAT_NETBEANS_INTG
4800 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4801#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004802#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004803 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4804 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004805#endif
K.Takata92000e22022-01-20 15:10:57 +00004806 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004807
Bram Moolenaar734a8672019-12-02 22:49:38 +01004808 case WM_QUERYENDSESSION: // System wants to go down.
4809 gui_shell_closed(); // Will exit when no changed buffers.
4810 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004813 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004816 return 0L;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 break;
4819
4820 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004821 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4822 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004823 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return 0L;
4825
4826 case WM_SYSCHAR:
4827 /*
4828 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4829 * shortcut key, handle like a typed ALT key, otherwise call Windows
4830 * ALT key handling.
4831 */
4832#ifdef FEAT_MENU
4833 if ( !gui.menu_is_active
4834 || p_wak[0] == 'n'
4835 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4836 )
4837#endif
4838 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004839 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return 0L;
4841 }
4842#ifdef FEAT_MENU
4843 else
K.Takata4ac893f2022-01-20 12:44:28 +00004844 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
4846
4847 case WM_SYSKEYUP:
4848#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004849 // This used to be done only when menu is active: ALT key is used for
4850 // that. But that caused problems when menu is disabled and using
4851 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4852 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004853 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004855 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856#endif
4857
K.Takata4f2417f2021-06-05 16:25:32 +02004858 case WM_EXITSIZEMOVE:
4859 destroy_sizing_tip();
4860 break;
4861
Bram Moolenaar734a8672019-12-02 22:49:38 +01004862 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004863 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004866 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004867 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004868 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
Bram Moolenaar734a8672019-12-02 22:49:38 +01004870 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004871 case WM_SETTINGCHANGE:
4872 return _OnSettingChange((UINT)wParam);
4873
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004874#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004876 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#endif
K.Takata92000e22022-01-20 15:10:57 +00004878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879#if defined(MENUHINTS) && defined(FEAT_MENU)
4880 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004881 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884#ifdef FEAT_MBYTE_IME
4885 case WM_IME_NOTIFY:
4886 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004887 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004888 return 1L;
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 case WM_IME_COMPOSITION:
4891 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004892 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004893 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004895 case WM_DPICHANGED:
4896 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4897 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
4899 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004901 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#endif
K.Takata92000e22022-01-20 15:10:57 +00004904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906
K.Takata4ac893f2022-01-20 12:44:28 +00004907 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908}
4909
4910/*
4911 * End of call-back routines
4912 */
4913
Bram Moolenaar734a8672019-12-02 22:49:38 +01004914// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915HWND vim_parent_hwnd = NULL;
4916
4917 static BOOL CALLBACK
4918FindWindowTitle(HWND hwnd, LPARAM lParam)
4919{
4920 char buf[2048];
4921 char *title = (char *)lParam;
4922
4923 if (GetWindowText(hwnd, buf, sizeof(buf)))
4924 {
4925 if (strstr(buf, title) != NULL)
4926 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004927 // Found it. Store the window ref. and quit searching if MDI
4928 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004930 if (vim_parent_hwnd != NULL)
4931 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004934 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935}
4936
4937/*
4938 * Invoked for '-P "title"' argument: search for parent application to open
4939 * our window in.
4940 */
4941 void
4942gui_mch_set_parent(char *title)
4943{
4944 EnumWindows(FindWindowTitle, (LPARAM)title);
4945 if (vim_parent_hwnd == NULL)
4946 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004947 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 mch_exit(2);
4949 }
4950}
4951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004952#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 static void
4954ole_error(char *arg)
4955{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004956 char buf[IOSIZE];
4957
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004958# ifdef VIMDLL
4959 gui.in_use = mch_is_gui_executable();
4960# endif
4961
Bram Moolenaar734a8672019-12-02 22:49:38 +01004962 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004963 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004964 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004965 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004969#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4970 static char *
4971gvim_error(void)
4972{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004973 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004974
4975 if (starting)
4976 {
4977 mch_errmsg(msg);
4978 mch_errmsg("\n");
4979 mch_exit(2);
4980 }
4981 return msg;
4982}
4983
4984 char *
4985gui_mch_do_spawn(char_u *arg)
4986{
4987 int len;
4988# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4989 char_u *session = NULL;
4990 LPWSTR tofree1 = NULL;
4991# endif
4992 WCHAR name[MAX_PATH];
4993 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4994 STARTUPINFOW si = {sizeof(si)};
4995 PROCESS_INFORMATION pi;
4996
4997 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4998 goto error;
4999 p = wcsrchr(name, L'\\');
5000 if (p == NULL)
5001 goto error;
5002 // Replace the executable name from vim(d).exe to gvim(d).exe.
5003# ifdef DEBUG
5004 wcscpy(p + 1, L"gvimd.exe");
5005# else
5006 wcscpy(p + 1, L"gvim.exe");
5007# endif
5008
5009# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5010 if (starting)
5011# endif
5012 {
5013 // Pass the command line to the new process.
5014 p = GetCommandLineW();
5015 // Skip 1st argument.
5016 while (*p && *p != L' ' && *p != L'\t')
5017 {
5018 if (*p == L'"')
5019 {
5020 while (*p && *p != L'"')
5021 ++p;
5022 if (*p)
5023 ++p;
5024 }
5025 else
5026 ++p;
5027 }
5028 cmd = p;
5029 }
5030# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5031 else
5032 {
5033 // Create a session file and pass it to the new process.
5034 LPWSTR wsession;
5035 char_u *savebg;
5036 int ret;
5037
5038 session = vim_tempname('s', FALSE);
5039 if (session == NULL)
5040 goto error;
5041 savebg = p_bg;
5042 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5043 ret = write_session_file(session);
5044 vim_free(p_bg);
5045 p_bg = savebg;
5046 if (!ret)
5047 goto error;
5048 wsession = enc_to_utf16(session, NULL);
5049 if (wsession == NULL)
5050 goto error;
5051 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005052 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005053 if (cmd == NULL)
5054 {
5055 vim_free(wsession);
5056 goto error;
5057 }
5058 tofree1 = cmd;
5059 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5060 wsession, wsession);
5061 vim_free(wsession);
5062 }
5063# endif
5064
5065 // Check additional arguments to the `:gui` command.
5066 if (arg != NULL)
5067 {
5068 warg = enc_to_utf16(arg, NULL);
5069 if (warg == NULL)
5070 goto error;
5071 tofree2 = warg;
5072 }
5073 else
5074 warg = L"";
5075
5076 // Set up the new command line.
5077 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005078 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005079 if (newcmd == NULL)
5080 goto error;
5081 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5082
5083 // Spawn a new GUI process.
5084 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5085 NULL, NULL, &si, &pi))
5086 goto error;
5087 CloseHandle(pi.hProcess);
5088 CloseHandle(pi.hThread);
5089 mch_exit(0);
5090
5091error:
5092# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5093 if (session)
5094 mch_remove(session);
5095 vim_free(session);
5096 vim_free(tofree1);
5097# endif
5098 vim_free(newcmd);
5099 vim_free(tofree2);
5100 return gvim_error();
5101}
5102#endif
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104/*
5105 * Parse the GUI related command-line arguments. Any arguments used are
5106 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005107 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
5109 void
5110gui_mch_prepare(int *argc, char **argv)
5111{
5112 int silent = FALSE;
5113 int idx;
5114
Bram Moolenaar734a8672019-12-02 22:49:38 +01005115 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5117 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005118 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5120 && (argv[2][0] == '-' || argv[2][0] == '/'))
5121 {
5122 silent = TRUE;
5123 idx = 2;
5124 }
5125 else
5126 idx = 1;
5127
Bram Moolenaar734a8672019-12-02 22:49:38 +01005128 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (STRICMP(argv[idx] + 1, "register") == 0)
5130 {
5131#ifdef FEAT_OLE
5132 RegisterMe(silent);
5133 mch_exit(0);
5134#else
5135 if (!silent)
5136 ole_error("register");
5137 mch_exit(2);
5138#endif
5139 }
5140
Bram Moolenaar734a8672019-12-02 22:49:38 +01005141 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5143 {
5144#ifdef FEAT_OLE
5145 UnregisterMe(!silent);
5146 mch_exit(0);
5147#else
5148 if (!silent)
5149 ole_error("unregister");
5150 mch_exit(2);
5151#endif
5152 }
5153
Bram Moolenaar734a8672019-12-02 22:49:38 +01005154 // Ignore an -embedding argument. It is only relevant if the
5155 // application wants to treat the case when it is started manually
5156 // differently from the case where it is started via automation (and
5157 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5159 {
5160#ifdef FEAT_OLE
5161 *argc = 1;
5162#else
5163 ole_error("embedding");
5164 mch_exit(2);
5165#endif
5166 }
5167 }
5168
5169#ifdef FEAT_OLE
5170 {
5171 int bDoRestart = FALSE;
5172
5173 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005174 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (bDoRestart)
5176 mch_exit(0);
5177 }
5178#endif
5179
5180#ifdef FEAT_NETBEANS_INTG
5181 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005182 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 int arg;
5184
5185 for (arg = 1; arg < *argc; arg++)
5186 if (strncmp("-nb", argv[arg], 3) == 0)
5187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 netbeansArg = argv[arg];
5189 mch_memmove(&argv[arg], &argv[arg + 1],
5190 (--*argc - arg) * sizeof(char *));
5191 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005192 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196}
5197
K.Takatac81e9bf2022-01-16 14:15:49 +00005198 static void
5199load_dpi_func(void)
5200{
5201 HMODULE hUser32;
5202
5203 hUser32 = GetModuleHandle("user32.dll");
5204 if (hUser32 == NULL)
5205 goto fail;
5206
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005207 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5208 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5209 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005210 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005211 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5212 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005213
5214 if (pSetThreadDpiAwarenessContext != NULL)
5215 {
5216 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5217 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5218 if (oldctx != NULL)
5219 {
5220 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5221 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5222#ifdef DEBUG
5223 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5224 {
5225 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5226 }
5227#endif
5228 return;
5229 }
5230 }
5231
5232fail:
5233 // Disable PerMonitorV2 APIs.
5234 pGetDpiForSystem = stubGetDpiForSystem;
5235 pGetDpiForWindow = NULL;
5236 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5237 pSetThreadDpiAwarenessContext = NULL;
5238 pGetAwarenessFromDpiAwarenessContext = NULL;
5239}
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241/*
5242 * Initialise the GUI. Create all the windows, set up all the call-backs
5243 * etc.
5244 */
5245 int
5246gui_mch_init(void)
5247{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005249 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
Bram Moolenaar734a8672019-12-02 22:49:38 +01005252 // Return here if the window was already opened (happens when
5253 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005255 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257 /*
5258 * Load the tearoff bitmap
5259 */
5260#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005261 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#endif
5263
K.Takatac81e9bf2022-01-16 14:15:49 +00005264 load_dpi_func();
5265
5266 s_dpi = pGetDpiForSystem();
5267 update_scrollbar_size();
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005270 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#endif
5272 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005273#ifdef FEAT_TOOLBAR
5274 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5278
Bram Moolenaar734a8672019-12-02 22:49:38 +01005279 // First try using the wide version, so that we can use any title.
5280 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005281 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005283 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 wndclassw.lpfnWndProc = _WndProc;
5285 wndclassw.cbClsExtra = 0;
5286 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005287 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5289 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5290 wndclassw.hbrBackground = s_brush;
5291 wndclassw.lpszMenuName = NULL;
5292 wndclassw.lpszClassName = szVimWndClassW;
5293
K.Takata4ac893f2022-01-20 12:44:28 +00005294 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (vim_parent_hwnd != NULL)
5299 {
5300#ifdef HAVE_TRY_EXCEPT
5301 __try
5302 {
5303#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005304 // Open inside the specified parent window.
5305 // TODO: last argument should point to a CLIENTCREATESTRUCT
5306 // structure.
5307 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005309 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005310 WS_OVERLAPPEDWINDOW | WS_CHILD
5311 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5313 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005314 100, // Any value will do
5315 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005317 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#ifdef HAVE_TRY_EXCEPT
5319 }
5320 __except(EXCEPTION_EXECUTE_HANDLER)
5321 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005322 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324#endif
5325 if (s_hwnd == NULL)
5326 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005327 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 mch_exit(2);
5329 }
5330 }
5331 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005332 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005333 // If the provided windowid is not valid reset it to zero, so that it
5334 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005335 if (IsWindow((HWND)win_socket_id) <= 0)
5336 win_socket_id = 0;
5337
Bram Moolenaar734a8672019-12-02 22:49:38 +01005338 // Create a window. If win_socket_id is not zero without border and
5339 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005340 s_hwnd = CreateWindowW(
5341 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005342 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5343 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005344 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5345 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005346 100, // Any value will do
5347 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005348 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005349 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005350 if (s_hwnd != NULL && win_socket_id != 0)
5351 {
5352 SetParent(s_hwnd, (HWND)win_socket_id);
5353 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5354 }
5355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 if (s_hwnd == NULL)
5358 return FAIL;
5359
K.Takatac81e9bf2022-01-16 14:15:49 +00005360 if (pGetDpiForWindow != NULL)
5361 {
5362 s_dpi = pGetDpiForWindow(s_hwnd);
5363 update_scrollbar_size();
5364 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5365 }
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5368 dyn_imm_load();
5369#endif
5370
Bram Moolenaar734a8672019-12-02 22:49:38 +01005371 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005372 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005373 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005374 wndclassw.style = CS_OWNDC;
5375 wndclassw.lpfnWndProc = _TextAreaWndProc;
5376 wndclassw.cbClsExtra = 0;
5377 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005378 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005379 wndclassw.hIcon = NULL;
5380 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5381 wndclassw.hbrBackground = NULL;
5382 wndclassw.lpszMenuName = NULL;
5383 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005384
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005385 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 return FAIL;
5387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005389 s_textArea = CreateWindowExW(
5390 0,
5391 szTextAreaClassW, L"Vim text area",
5392 WS_CHILD | WS_VISIBLE, 0, 0,
5393 100, // Any value will do for now
5394 100, // Any value will do for now
5395 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005396 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (s_textArea == NULL)
5399 return FAIL;
5400
Bram Moolenaar20321902016-02-17 12:30:17 +01005401#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005402 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005403 {
5404 HANDLE hIcon = NULL;
5405
5406 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005407 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005408 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005409#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_MENU
5412 s_menuBar = CreateMenu();
5413#endif
5414 s_hdc = GetDC(s_textArea);
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
Bram Moolenaar734a8672019-12-02 22:49:38 +01005418 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005419 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar734a8672019-12-02 22:49:38 +01005421 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 gui_mch_def_colors();
5423
Bram Moolenaar734a8672019-12-02 22:49:38 +01005424 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5425 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 set_normal_colors();
5427
5428 /*
5429 * Check that none of the colors are the same as the background color.
5430 * Then store the current values as the defaults.
5431 */
5432 gui_check_colors();
5433 gui.def_norm_pixel = gui.norm_pixel;
5434 gui.def_back_pixel = gui.back_pixel;
5435
Bram Moolenaar734a8672019-12-02 22:49:38 +01005436 // Get the colors for the highlight groups (gui_check_colors() might have
5437 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 highlight_gui_started();
5439
5440 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005441 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005443 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 /*
5446 * Set up for Intellimouse processing
5447 */
5448 init_mouse_wheel();
5449
5450 /*
5451 * compute a couple of metrics used for the dialogs
5452 */
5453 get_dialog_font_metrics();
5454#ifdef FEAT_TOOLBAR
5455 /*
5456 * Create the toolbar
5457 */
5458 initialise_toolbar();
5459#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005460#ifdef FEAT_GUI_TABLINE
5461 /*
5462 * Create the tabline
5463 */
5464 initialise_tabline();
5465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#ifdef MSWIN_FIND_REPLACE
5467 /*
5468 * Initialise the dialog box stuff
5469 */
5470 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5471
Bram Moolenaar734a8672019-12-02 22:49:38 +01005472 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005474 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005476 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5478 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5479 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005482#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005483 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005484 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005485#endif
5486
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005487#ifdef FEAT_RENDER_OPTIONS
5488 if (p_rop)
5489 (void)gui_mch_set_rendering_options(p_rop);
5490#endif
5491
Bram Moolenaar748bf032005-02-02 23:04:36 +00005492theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005493 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005494 display_errors();
5495
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return OK;
5497}
5498
5499/*
5500 * Get the size of the screen, taking position on multiple monitors into
5501 * account (if supported).
5502 */
5503 static void
5504get_work_area(RECT *spi_rect)
5505{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005506 HMONITOR mon;
5507 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaar734a8672019-12-02 22:49:38 +01005509 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005510 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005511 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005513 moninfo.cbSize = sizeof(MONITORINFO);
5514 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005516 *spi_rect = moninfo.rcWork;
5517 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 }
5519 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005520 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5522}
5523
5524/*
5525 * Set the size of the window to the given width and height in pixels.
5526 */
5527 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005528gui_mch_set_shellsize(
5529 int width,
5530 int height,
5531 int min_width UNUSED,
5532 int min_height UNUSED,
5533 int base_width UNUSED,
5534 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005535 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005538 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
Bram Moolenaar734a8672019-12-02 22:49:38 +01005541 // Try to keep window completely on screen.
5542 // Get position of the screen work area. This is the part that is not
5543 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 get_work_area(&workarea_rect);
5545
Bram Moolenaar734a8672019-12-02 22:49:38 +01005546 // Resizing a maximized window looks very strange, unzoom it first.
5547 // But don't do it when still starting up, it may have been requested in
5548 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005549 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005551
5552 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
Bram Moolenaar734a8672019-12-02 22:49:38 +01005554 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005555 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5556 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5557 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5558 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5559 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5560 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaar734a8672019-12-02 22:49:38 +01005562 // The following should take care of keeping Vim on the same monitor, no
5563 // matter if the secondary monitor is left or right of the primary
5564 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005565 window_rect.right = window_rect.left + win_width;
5566 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005567
Bram Moolenaar734a8672019-12-02 22:49:38 +01005568 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005569 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5570 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005572 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5573 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005575 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5576 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005578 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5579 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005581 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5582 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 SetActiveWindow(s_hwnd);
5585 SetFocus(s_hwnd);
5586
Bram Moolenaar734a8672019-12-02 22:49:38 +01005587 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589}
5590
5591
5592 void
5593gui_mch_set_scrollbar_thumb(
5594 scrollbar_T *sb,
5595 long val,
5596 long size,
5597 long max)
5598{
5599 SCROLLINFO info;
5600
5601 sb->scroll_shift = 0;
5602 while (max > 32767)
5603 {
5604 max = (max + 1) >> 1;
5605 val >>= 1;
5606 size >>= 1;
5607 ++sb->scroll_shift;
5608 }
5609
5610 if (sb->scroll_shift > 0)
5611 ++size;
5612
5613 info.cbSize = sizeof(info);
5614 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5615 info.nPos = val;
5616 info.nMin = 0;
5617 info.nMax = max;
5618 info.nPage = size;
5619 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5620}
5621
5622
5623/*
5624 * Set the current text font.
5625 */
5626 void
5627gui_mch_set_font(GuiFont font)
5628{
5629 gui.currFont = font;
5630}
5631
5632
5633/*
5634 * Set the current text foreground color.
5635 */
5636 void
5637gui_mch_set_fg_color(guicolor_T color)
5638{
5639 gui.currFgColor = color;
5640}
5641
5642/*
5643 * Set the current text background color.
5644 */
5645 void
5646gui_mch_set_bg_color(guicolor_T color)
5647{
5648 gui.currBgColor = color;
5649}
5650
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005651/*
5652 * Set the current text special color.
5653 */
5654 void
5655gui_mch_set_sp_color(guicolor_T color)
5656{
5657 gui.currSpColor = color;
5658}
5659
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005660#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661/*
5662 * Multi-byte handling, originally by Sung-Hoon Baek.
5663 * First static functions (no prototypes generated).
5664 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005665# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005666# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005667# endif
5668# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 * handle WM_IME_NOTIFY message
5672 */
5673 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005674_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 LRESULT lResult = 0;
5677 HIMC hImc;
5678
5679 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5680 return lResult;
5681 switch (dwCommand)
5682 {
5683 case IMN_SETOPENSTATUS:
5684 if (pImmGetOpenStatus(hImc))
5685 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005686 LOGFONTW lf = norm_logfont;
5687 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5688 // Work around when PerMonitorV2 is not enabled in the process level.
5689 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5690 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 im_set_position(gui.row, gui.col);
5692
Bram Moolenaar734a8672019-12-02 22:49:38 +01005693 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005694 State &= ~MODE_LANGMAP;
5695 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005697# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005698 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5700 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005701 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 int old_row = gui.row;
5703 int old_col = gui.col;
5704
5705 // This must be called here before
5706 // status_redraw_curbuf(), otherwise the mode
5707 // message may appear in the wrong position.
5708 showmode();
5709 status_redraw_curbuf();
5710 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005711 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 gui.row = old_row;
5713 gui.col = old_col;
5714 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 }
5718 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005719 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 lResult = 0;
5721 break;
5722 }
5723 pImmReleaseContext(hWnd, hImc);
5724 return lResult;
5725}
5726
5727 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005728_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 char_u *ret;
5731 int len;
5732
Bram Moolenaar734a8672019-12-02 22:49:38 +01005733 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return 0;
5735
5736 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5737 if (ret != NULL)
5738 {
5739 add_to_input_buf_csi(ret, len);
5740 vim_free(ret);
5741 return 1;
5742 }
5743 return 0;
5744}
5745
5746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 * void GetResultStr()
5748 *
5749 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5750 * get complete composition string
5751 */
5752 static char_u *
5753GetResultStr(HWND hwnd, int GCS, int *lenp)
5754{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005755 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005756 LONG ret;
5757 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 char_u *convbuf = NULL;
5759
5760 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5761 return NULL;
5762
K.Takatab0b2b732022-01-19 12:59:21 +00005763 // Get the length of the composition string.
5764 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5765 if (ret <= 0)
5766 return NULL;
5767
5768 // Allocate the requested buffer plus space for the NUL character.
5769 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (buf == NULL)
5771 return NULL;
5772
K.Takatab0b2b732022-01-19 12:59:21 +00005773 // Reads in the composition string.
5774 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5775 *lenp = ret / sizeof(WCHAR);
5776
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005777 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 pImmReleaseContext(hwnd, hIMC);
5779 vim_free(buf);
5780 return convbuf;
5781}
5782#endif
5783
Bram Moolenaar734a8672019-12-02 22:49:38 +01005784// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005785#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787/*
5788 * set font to IM.
5789 */
5790 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005791im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 HIMC hImc;
5794
5795 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5796 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005797 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 pImmReleaseContext(s_hwnd, hImc);
5799 }
5800}
5801
5802/*
5803 * Notify cursor position to IM.
5804 */
5805 void
5806im_set_position(int row, int col)
5807{
5808 HIMC hImc;
5809
5810 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5811 {
5812 COMPOSITIONFORM cfs;
5813
5814 cfs.dwStyle = CFS_POINT;
5815 cfs.ptCurrentPos.x = FILL_X(col);
5816 cfs.ptCurrentPos.y = FILL_Y(row);
5817 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005818 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5819 {
5820 // Work around when PerMonitorV2 is not enabled in the process level.
5821 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5822 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 pImmSetCompositionWindow(hImc, &cfs);
5825
5826 pImmReleaseContext(s_hwnd, hImc);
5827 }
5828}
5829
5830/*
5831 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5832 */
5833 void
5834im_set_active(int active)
5835{
5836 HIMC hImc;
5837 static HIMC hImcOld = (HIMC)0;
5838
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005839# ifdef VIMDLL
5840 if (!gui.in_use && !gui.starting)
5841 {
5842 mbyte_im_set_active(active);
5843 return;
5844 }
5845# endif
5846
Bram Moolenaar734a8672019-12-02 22:49:38 +01005847 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
5849 if (p_imdisable)
5850 {
5851 if (hImcOld == (HIMC)0)
5852 {
5853 hImcOld = pImmGetContext(s_hwnd);
5854 if (hImcOld)
5855 pImmAssociateContext(s_hwnd, (HIMC)0);
5856 }
5857 active = FALSE;
5858 }
5859 else if (hImcOld != (HIMC)0)
5860 {
5861 pImmAssociateContext(s_hwnd, hImcOld);
5862 hImcOld = (HIMC)0;
5863 }
5864
5865 hImc = pImmGetContext(s_hwnd);
5866 if (hImc)
5867 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005868 /*
5869 * for Korean ime
5870 */
5871 HKL hKL = GetKeyboardLayout(0);
5872
5873 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5874 {
5875 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5876 static BOOL bSaved = FALSE;
5877
5878 if (active)
5879 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005880 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005881 if (bSaved)
5882 pImmSetConversionStatus(hImc, dwConversionSaved,
5883 dwSentenceSaved);
5884 bSaved = FALSE;
5885 }
5886 else
5887 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005888 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005889 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5890 &dwSentenceSaved))
5891 {
5892 bSaved = TRUE;
5893 pImmSetConversionStatus(hImc,
5894 dwConversionSaved & ~(IME_CMODE_NATIVE
5895 | IME_CMODE_FULLSHAPE),
5896 dwSentenceSaved);
5897 }
5898 }
5899 }
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 pImmSetOpenStatus(hImc, active);
5902 pImmReleaseContext(s_hwnd, hImc);
5903 }
5904 }
5905}
5906
5907/*
5908 * Get IM status. When IM is on, return not 0. Else return 0.
5909 */
5910 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005911im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 int status = 0;
5914 HIMC hImc;
5915
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005916# ifdef VIMDLL
5917 if (!gui.in_use && !gui.starting)
5918 return mbyte_im_get_status();
5919# endif
5920
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5922 {
5923 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5924 pImmReleaseContext(s_hwnd, hImc);
5925 }
5926 return status;
5927}
5928
Bram Moolenaar734a8672019-12-02 22:49:38 +01005929#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005932/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005933 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005934 */
5935 static void
5936latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5937{
5938 int c;
5939
Bram Moolenaarca003e12006-03-17 23:19:38 +00005940 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005941 {
5942 c = *text++;
5943 switch (c)
5944 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005945 case 0xa4: c = 0x20ac; break; // euro
5946 case 0xa6: c = 0x0160; break; // S hat
5947 case 0xa8: c = 0x0161; break; // S -hat
5948 case 0xb4: c = 0x017d; break; // Z hat
5949 case 0xb8: c = 0x017e; break; // Z -hat
5950 case 0xbc: c = 0x0152; break; // OE
5951 case 0xbd: c = 0x0153; break; // oe
5952 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005953 }
5954 *unicodebuf++ = c;
5955 }
5956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
5958#ifdef FEAT_RIGHTLEFT
5959/*
5960 * What is this for? In the case where you are using Win98 or Win2K or later,
5961 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5962 * reverses the string sent to the TextOut... family. This sucks, because we
5963 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5964 * way to tell Windblows not to do this!
5965 *
5966 * The short of it is that this 'RevOut' only gets called if you are running
5967 * one of the new, "improved" MS OSes, and only if you are running in
5968 * 'rightleft' mode. It makes display take *slightly* longer, but not
5969 * noticeably so.
5970 */
5971 static void
K.Takata135e1522022-01-29 15:27:58 +00005972RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 int col,
5974 int row,
5975 UINT foptions,
5976 CONST RECT *pcliprect,
5977 LPCTSTR text,
5978 UINT len,
5979 CONST INT *padding)
5980{
5981 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005983 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005984 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005985 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986}
5987#endif
5988
Bram Moolenaar92467d32017-12-05 13:22:16 +01005989 static void
5990draw_line(
5991 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005992 int y1,
5993 int x2,
5994 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005995 COLORREF color)
5996{
5997#if defined(FEAT_DIRECTX)
5998 if (IS_ENABLE_DIRECTX())
5999 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6000 else
6001#endif
6002 {
6003 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6004 HPEN old_pen = SelectObject(s_hdc, hpen);
6005 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006006 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006007 LineTo(s_hdc, x2, y2);
6008 DeleteObject(SelectObject(s_hdc, old_pen));
6009 }
6010}
6011
6012 static void
6013set_pixel(
6014 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006015 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006016 COLORREF color)
6017{
6018#if defined(FEAT_DIRECTX)
6019 if (IS_ENABLE_DIRECTX())
6020 DWriteContext_SetPixel(s_dwc, x, y, color);
6021 else
6022#endif
6023 SetPixel(s_hdc, x, y, color);
6024}
6025
6026 static void
6027fill_rect(
6028 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006029 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006030 COLORREF color)
6031{
6032#if defined(FEAT_DIRECTX)
6033 if (IS_ENABLE_DIRECTX())
6034 DWriteContext_FillRect(s_dwc, rcp, color);
6035 else
6036#endif
6037 {
6038 HBRUSH hbr2;
6039
6040 if (hbr == NULL)
6041 hbr2 = CreateSolidBrush(color);
6042 else
6043 hbr2 = hbr;
6044 FillRect(s_hdc, rcp, hbr2);
6045 if (hbr == NULL)
6046 DeleteBrush(hbr2);
6047 }
6048}
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 void
6051gui_mch_draw_string(
6052 int row,
6053 int col,
6054 char_u *text,
6055 int len,
6056 int flags)
6057{
6058 static int *padding = NULL;
6059 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 const RECT *pcliprect = NULL;
6061 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 static WCHAR *unicodebuf = NULL;
6063 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006064 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 int y;
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 /*
6069 * Italic and bold text seems to have an extra row of pixels at the bottom
6070 * (below where the bottom of the character should be). If we draw the
6071 * characters with a solid background, the top row of pixels in the
6072 * character below will be overwritten. We can fix this by filling in the
6073 * background ourselves, to the correct character proportions, and then
6074 * writing the character in transparent mode. Still have a problem when
6075 * the character is "_", which gets written on to the character below.
6076 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6077 * pixel in their slots, which fixes the problem with the bottom row of
6078 * pixels. We still need this code because otherwise the top row of pixels
6079 * becomes a problem. - webb.
6080 */
6081 static HBRUSH hbr_cache[2] = {NULL, NULL};
6082 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6083 static int brush_lru = 0;
6084 HBRUSH hbr;
6085 RECT rc;
6086
6087 if (!(flags & DRAW_TRANSP))
6088 {
6089 /*
6090 * Clear background first.
6091 * Note: FillRect() excludes right and bottom of rectangle.
6092 */
6093 rc.left = FILL_X(col);
6094 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (has_mbyte)
6096 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006097 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006098 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 }
6100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 rc.right = FILL_X(col + len);
6102 rc.bottom = FILL_Y(row + 1);
6103
Bram Moolenaar734a8672019-12-02 22:49:38 +01006104 // Cache the created brush, that saves a lot of time. We need two:
6105 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 if (gui.currBgColor == brush_color[0])
6107 {
6108 hbr = hbr_cache[0];
6109 brush_lru = 1;
6110 }
6111 else if (gui.currBgColor == brush_color[1])
6112 {
6113 hbr = hbr_cache[1];
6114 brush_lru = 0;
6115 }
6116 else
6117 {
6118 if (hbr_cache[brush_lru] != NULL)
6119 DeleteBrush(hbr_cache[brush_lru]);
6120 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6121 brush_color[brush_lru] = gui.currBgColor;
6122 hbr = hbr_cache[brush_lru];
6123 brush_lru = !brush_lru;
6124 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006125
Bram Moolenaar92467d32017-12-05 13:22:16 +01006126 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 SetBkMode(s_hdc, TRANSPARENT);
6129
6130 /*
6131 * When drawing block cursor, prevent inverted character spilling
6132 * over character cell (can happen with bold/italic)
6133 */
6134 if (flags & DRAW_CURSOR)
6135 {
6136 pcliprect = &rc;
6137 foptions = ETO_CLIPPED;
6138 }
6139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 SetTextColor(s_hdc, gui.currFgColor);
6141 SelectFont(s_hdc, gui.currFont);
6142
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006143#ifdef FEAT_DIRECTX
6144 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006145 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006146#endif
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6149 {
K.Takata135e1522022-01-29 15:27:58 +00006150 int i;
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 vim_free(padding);
6153 pad_size = Columns;
6154
Bram Moolenaar734a8672019-12-02 22:49:38 +01006155 // Don't give an out-of-memory message here, it would call us
6156 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006157 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (padding != NULL)
6159 for (i = 0; i < pad_size; i++)
6160 padding[i] = gui.char_width;
6161 }
6162
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 /*
6164 * We have to provide the padding argument because italic and bold versions
6165 * of fixed-width fonts are often one pixel or so wider than their normal
6166 * versions.
6167 * No check for DRAW_BOLD, Windows will have done it already.
6168 */
6169
Bram Moolenaar734a8672019-12-02 22:49:38 +01006170 // Check if there are any UTF-8 characters. If not, use normal text
6171 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (enc_utf8)
6173 for (n = 0; n < len; ++n)
6174 if (text[n] >= 0x80)
6175 break;
6176
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006177#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006178 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6179 // required that unicode drawing routine, currently. So this forces it
6180 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006181 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006182 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006183#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006184
Bram Moolenaar734a8672019-12-02 22:49:38 +01006185 // Check if the Unicode buffer exists and is big enough. Create it
6186 // with the same length as the multi-byte string, the number of wide
6187 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006188 if ((enc_utf8
6189 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6190 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 && (unicodebuf == NULL || len > unibuflen))
6192 {
6193 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006194 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
6196 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006197 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 unibuflen = len;
6200 }
6201
6202 if (enc_utf8 && n < len && unicodebuf != NULL)
6203 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006204 // Output UTF-8 characters. Composing characters should be
6205 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006206 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006207 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006208 int cells; // cell width of string up to composing char
6209 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006210 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006212 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006214 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006216 c = utf_ptr2char(text + i);
6217 if (c >= 0x10000)
6218 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006219 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006220 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6221 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006222 }
6223 else
6224 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006225 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006226 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006227
6228 if (utf_iscomposing(c))
6229 cw = 0;
6230 else
6231 {
6232 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006233 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006234 cw = 1;
6235 }
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (unicodepdy != NULL)
6238 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006239 // Use unicodepdy to make characters fit as we expect, even
6240 // when the font uses different widths (e.g., bold character
6241 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006242 if (c >= 0x10000)
6243 {
6244 unicodepdy[wlen - 2] = cw * gui.char_width;
6245 unicodepdy[wlen - 1] = 0;
6246 }
6247 else
6248 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006251 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006253#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006254 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006256 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006257 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006258 TEXT_X(col), TEXT_Y(row),
6259 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006260 gui.char_width, gui.currFgColor,
6261 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006262 }
6263 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006264#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006265 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6266 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006267 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006269 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006271 // If we want to display codepage data, and the current CP is not the
6272 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (unicodebuf != NULL)
6274 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006275 if (enc_latin9)
6276 latin9_to_ucs(text, len, unicodebuf);
6277 else
6278 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 MB_PRECOMPOSED,
6280 (char *)text, len,
6281 (LPWSTR)unicodebuf, unibuflen);
6282 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006283 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006284 // Use unicodepdy to make characters fit as we expect, even
6285 // when the font uses different widths (e.g., bold character
6286 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006287 if (unicodepdy != NULL)
6288 {
6289 int i;
6290 int cw;
6291
6292 for (i = 0; i < len; ++i)
6293 {
6294 cw = utf_char2cells(unicodebuf[i]);
6295 if (cw > 2)
6296 cw = 1;
6297 unicodepdy[i] = cw * gui.char_width;
6298 }
6299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006301 foptions, pcliprect, unicodebuf, len, unicodepdy);
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304 }
6305 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006308 // Windows will mess up RL text, so we have to draw it character by
6309 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006310 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6312 foptions, pcliprect, (char *)text, len, padding);
6313 else
6314#endif
6315 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6316 foptions, pcliprect, (char *)text, len, padding);
6317 }
6318
Bram Moolenaar734a8672019-12-02 22:49:38 +01006319 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (flags & DRAW_UNDERL)
6321 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322 // When p_linespace is 0, overwrite the bottom row of pixels.
6323 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 if (p_linespace > 1)
6326 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006327 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006329
Bram Moolenaar734a8672019-12-02 22:49:38 +01006330 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006331 if (flags & DRAW_STRIKE)
6332 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006333 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006334 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006335 }
6336
Bram Moolenaar734a8672019-12-02 22:49:38 +01006337 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006338 if (flags & DRAW_UNDERC)
6339 {
6340 int x;
6341 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006342 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006343
6344 y = FILL_Y(row + 1) - 1;
6345 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6346 {
6347 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006348 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006349 }
6350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351}
6352
6353
6354/*
6355 * Output routines.
6356 */
6357
Bram Moolenaar734a8672019-12-02 22:49:38 +01006358/*
6359 * Flush any output to the screen
6360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 void
6362gui_mch_flush(void)
6363{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006364#if defined(FEAT_DIRECTX)
6365 if (IS_ENABLE_DIRECTX())
6366 DWriteContext_Flush(s_dwc);
6367#endif
6368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 GdiFlush();
6370}
6371
6372 static void
6373clear_rect(RECT *rcp)
6374{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006375 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376}
6377
6378
Bram Moolenaarc716c302006-01-21 22:12:51 +00006379 void
6380gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6381{
6382 RECT workarea_rect;
6383
6384 get_work_area(&workarea_rect);
6385
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006386 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006387 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6388 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006389
Bram Moolenaar734a8672019-12-02 22:49:38 +01006390 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6391 // the menubar for MSwin, we subtract it from the screen height, so that
6392 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006394 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6395 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6396 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6397 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006398}
6399
6400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401#if defined(FEAT_MENU) || defined(PROTO)
6402/*
6403 * Add a sub menu to the menu bar.
6404 */
6405 void
6406gui_mch_add_menu(
6407 vimmenu_T *menu,
6408 int pos)
6409{
6410 vimmenu_T *parent = menu->parent;
6411
6412 menu->submenu_id = CreatePopupMenu();
6413 menu->id = s_menu_id++;
6414
6415 if (menu_is_menubar(menu->name))
6416 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006417 WCHAR *wn;
6418 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006420 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006421 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006424 infow.cbSize = sizeof(infow);
6425 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6426 | MIIM_SUBMENU;
6427 infow.dwItemData = (long_u)menu;
6428 infow.wID = menu->id;
6429 infow.fType = MFT_STRING;
6430 infow.dwTypeData = wn;
6431 infow.cch = (UINT)wcslen(wn);
6432 infow.hSubMenu = menu->submenu_id;
6433 InsertMenuItemW((parent == NULL)
6434 ? s_menuBar : parent->submenu_id,
6435 (UINT)pos, TRUE, &infow);
6436 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 }
6438
Bram Moolenaar734a8672019-12-02 22:49:38 +01006439 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (parent == NULL)
6441 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006442# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 else if (IsWindow(parent->tearoff_handle))
6444 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446}
6447
6448 void
6449gui_mch_show_popupmenu(vimmenu_T *menu)
6450{
6451 POINT mp;
6452
K.Takata45f9cfb2022-01-21 11:11:00 +00006453 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6455}
6456
6457 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006458gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 vimmenu_T *menu = gui_find_menu(path_name);
6461
6462 if (menu != NULL)
6463 {
6464 POINT p;
6465
Bram Moolenaar734a8672019-12-02 22:49:38 +01006466 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006468 if (mouse_pos)
6469 {
6470 int mx, my;
6471
6472 gui_mch_getmouse(&mx, &my);
6473 p.x += mx;
6474 p.y += my;
6475 }
6476 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006478 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6480 }
6481 msg_scroll = FALSE;
6482 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6483 }
6484}
6485
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006486# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487/*
6488 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6489 * create it as a pseudo-"tearoff menu".
6490 */
6491 void
6492gui_make_tearoff(char_u *path_name)
6493{
6494 vimmenu_T *menu = gui_find_menu(path_name);
6495
Bram Moolenaar734a8672019-12-02 22:49:38 +01006496 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (menu != NULL)
6498 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6499}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
6502/*
6503 * Add a menu item to a menu
6504 */
6505 void
6506gui_mch_add_menu_item(
6507 vimmenu_T *menu,
6508 int idx)
6509{
6510 vimmenu_T *parent = menu->parent;
6511
6512 menu->id = s_menu_id++;
6513 menu->submenu_id = NULL;
6514
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006515# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6517 {
6518 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6519 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6520 }
6521 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006522# endif
6523# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (menu_is_toolbar(parent->name))
6525 {
6526 TBBUTTON newtb;
6527
Bram Moolenaara80faa82020-04-12 19:37:17 +02006528 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (menu_is_separator(menu->name))
6530 {
6531 newtb.iBitmap = 0;
6532 newtb.fsStyle = TBSTYLE_SEP;
6533 }
6534 else
6535 {
6536 newtb.iBitmap = get_toolbar_bitmap(menu);
6537 newtb.fsStyle = TBSTYLE_BUTTON;
6538 }
6539 newtb.idCommand = menu->id;
6540 newtb.fsState = TBSTATE_ENABLED;
6541 newtb.iString = 0;
6542 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6543 (LPARAM)&newtb);
6544 menu->submenu_id = (HMENU)-1;
6545 }
6546 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006549 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006551 wn = enc_to_utf16(menu->name, NULL);
6552 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006554 InsertMenuW(parent->submenu_id, (UINT)idx,
6555 (menu_is_separator(menu->name)
6556 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6557 (UINT)menu->id, wn);
6558 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006560# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (IsWindow(parent->tearoff_handle))
6562 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565}
6566
6567/*
6568 * Destroy the machine specific menu widget.
6569 */
6570 void
6571gui_mch_destroy_menu(vimmenu_T *menu)
6572{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006573# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 /*
6575 * is this a toolbar button?
6576 */
6577 if (menu->submenu_id == (HMENU)-1)
6578 {
6579 int iButton;
6580
6581 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6582 (WPARAM)menu->id, 0);
6583 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6584 }
6585 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
6588 if (menu->parent != NULL
6589 && menu_is_popup(menu->parent->dname)
6590 && menu->parent->submenu_id != NULL)
6591 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6592 else
6593 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6594 if (menu->submenu_id != NULL)
6595 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006596# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (IsWindow(menu->tearoff_handle))
6598 DestroyWindow(menu->tearoff_handle);
6599 if (menu->parent != NULL
6600 && menu->parent->children != NULL
6601 && IsWindow(menu->parent->tearoff_handle))
6602 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006603 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 menu->modes = 0;
6605 rebuild_tearoff(menu->parent);
6606 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609}
6610
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006611# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 static void
6613rebuild_tearoff(vimmenu_T *menu)
6614{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006615 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 char_u tbuf[128];
6617 RECT trect;
6618 RECT rct;
6619 RECT roct;
6620 int x, y;
6621
6622 HWND thwnd = menu->tearoff_handle;
6623
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006624 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 if (GetWindowRect(thwnd, &trect)
6626 && GetWindowRect(s_hwnd, &rct)
6627 && GetClientRect(s_hwnd, &roct))
6628 {
6629 x = trect.left - rct.left;
6630 y = (trect.top - rct.bottom + roct.bottom);
6631 }
6632 else
6633 {
6634 x = y = 0xffffL;
6635 }
6636 DestroyWindow(thwnd);
6637 if (menu->children != NULL)
6638 {
6639 gui_mch_tearoff(tbuf, menu, x, y);
6640 if (IsWindow(menu->tearoff_handle))
6641 (void) SetWindowPos(menu->tearoff_handle,
6642 NULL,
6643 (int)trect.left,
6644 (int)trect.top,
6645 0, 0,
6646 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6647 }
6648}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006649# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651/*
6652 * Make a menu either grey or not grey.
6653 */
6654 void
6655gui_mch_menu_grey(
6656 vimmenu_T *menu,
6657 int grey)
6658{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006659# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 /*
6661 * is this a toolbar button?
6662 */
6663 if (menu->submenu_id == (HMENU)-1)
6664 {
6665 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006666 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
6668 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006669# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006670 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6671 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006673# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6675 {
6676 WORD menuID;
6677 HWND menuHandle;
6678
6679 /*
6680 * A tearoff button has changed state.
6681 */
6682 if (menu->children == NULL)
6683 menuID = (WORD)(menu->id);
6684 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006685 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6687 if (menuHandle)
6688 EnableWindow(menuHandle, !grey);
6689
6690 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006691# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692}
6693
Bram Moolenaar734a8672019-12-02 22:49:38 +01006694#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696
Bram Moolenaar734a8672019-12-02 22:49:38 +01006697// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006700#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6703/*
6704 * stuff for dialogs
6705 */
6706
6707/*
6708 * The callback routine used by all the dialogs. Very simple. First,
6709 * acknowledges the INITDIALOG message so that Windows knows to do standard
6710 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6711 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6712 * number.
6713 */
6714 static LRESULT CALLBACK
6715dialog_callback(
6716 HWND hwnd,
6717 UINT message,
6718 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006719 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 if (message == WM_INITDIALOG)
6722 {
6723 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006724 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 (void)SetFocus(hwnd);
6726 if (dialog_default_button > IDCANCEL)
6727 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006728 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006729 // We don't have a default, set focus on another element of the
6730 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006731 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 return FALSE;
6733 }
6734
6735 if (message == WM_COMMAND)
6736 {
6737 int button = LOWORD(wParam);
6738
Bram Moolenaar734a8672019-12-02 22:49:38 +01006739 // Don't end the dialog if something was selected that was
6740 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (button >= DLG_NONBUTTON_CONTROL)
6742 return TRUE;
6743
Bram Moolenaar734a8672019-12-02 22:49:38 +01006744 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006746 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006747 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006748 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006749
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006750 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6751 p = utf16_to_enc(wp, NULL);
6752 vim_strncpy(s_textfield, p, IOSIZE);
6753 vim_free(p);
6754 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757 /*
6758 * Need to check for IDOK because if the user just hits Return to
6759 * accept the default value, some reason this is what we get.
6760 */
6761 if (button == IDOK)
6762 {
6763 if (dialog_default_button > IDCANCEL)
6764 EndDialog(hwnd, dialog_default_button);
6765 }
6766 else
6767 EndDialog(hwnd, button - IDCANCEL);
6768 return TRUE;
6769 }
6770
6771 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6772 {
6773 EndDialog(hwnd, 0);
6774 return TRUE;
6775 }
6776 return FALSE;
6777}
6778
6779/*
6780 * Create a dialog dynamically from the parameter strings.
6781 * type = type of dialog (question, alert, etc.)
6782 * title = dialog title. may be NULL for default title.
6783 * message = text to display. Dialog sizes to accommodate it.
6784 * buttons = '\n' separated list of button captions, default first.
6785 * dfltbutton = number of default button.
6786 *
6787 * This routine returns 1 if the first button is pressed,
6788 * 2 for the second, etc.
6789 *
6790 * 0 indicates Esc was pressed.
6791 * -1 for unexpected error
6792 *
6793 * If stubbing out this fn, return 1.
6794 */
6795
Bram Moolenaar734a8672019-12-02 22:49:38 +01006796static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
6798 "IDR_VIM",
6799 "IDR_VIM_ERROR",
6800 "IDR_VIM_ALERT",
6801 "IDR_VIM_INFO",
6802 "IDR_VIM_QUESTION"
6803};
6804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 int
6806gui_mch_dialog(
6807 int type,
6808 char_u *title,
6809 char_u *message,
6810 char_u *buttons,
6811 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006812 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006813 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006816 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 int numButtons;
6818 int *buttonWidths, *buttonPositions;
6819 int buttonYpos;
6820 int nchar, i;
6821 DWORD lStyle;
6822 int dlgwidth = 0;
6823 int dlgheight;
6824 int editboxheight;
6825 int horizWidth = 0;
6826 int msgheight;
6827 char_u *pstart;
6828 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006829 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 char_u *tbuffer;
6831 RECT rect;
6832 HWND hwnd;
6833 HDC hdc;
6834 HFONT font, oldFont;
6835 TEXTMETRIC fontInfo;
6836 int fontHeight;
6837 int textWidth, minButtonWidth, messageWidth;
6838 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006839 int maxDialogHeight;
6840 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 int vertical;
6842 int dlgPaddingX;
6843 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006844# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006845 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006847# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006848 garray_T ga;
6849 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006850 int dlg_icon_width;
6851 int dlg_icon_height;
6852 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006854# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006855 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006856# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006857 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006858# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006859 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
Bram Moolenaar748bf032005-02-02 23:04:36 +00006863 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006864 {
6865 load_dpi_func();
6866 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006867 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006868 }
6869 else
6870 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871
6872 if ((type < 0) || (type > VIM_LAST_TYPE))
6873 type = 0;
6874
Bram Moolenaar734a8672019-12-02 22:49:38 +01006875 // allocate some memory for dialog template
6876 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006877 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006878 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880 if (p == NULL)
6881 return -1;
6882
6883 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006884 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6886 * const.
6887 */
6888 tbuffer = vim_strsave(buttons);
6889 if (tbuffer == NULL)
6890 return -1;
6891
Bram Moolenaar734a8672019-12-02 22:49:38 +01006892 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
Bram Moolenaar734a8672019-12-02 22:49:38 +01006894 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 numButtons = 1;
6896 for (i = 0; tbuffer[i] != '\0'; i++)
6897 {
6898 if (tbuffer[i] == DLG_BUTTON_SEP)
6899 numButtons++;
6900 }
6901 if (dfltbutton >= numButtons)
6902 dfltbutton = -1;
6903
Bram Moolenaar734a8672019-12-02 22:49:38 +01006904 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006905 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (buttonWidths == NULL)
6907 return -1;
6908
Bram Moolenaar734a8672019-12-02 22:49:38 +01006909 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006910 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 if (buttonPositions == NULL)
6912 return -1;
6913
6914 /*
6915 * Calculate how big the dialog must be.
6916 */
6917 hwnd = GetDesktopWindow();
6918 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006919# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6921 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006922 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 use_lfSysmenu = TRUE;
6924 }
6925 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006926# endif
K.Takatad1c58992022-01-23 12:31:57 +00006927 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6928 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6929
6930 oldFont = SelectFont(hdc, font);
6931 dlgPaddingX = DLG_PADDING_X;
6932 dlgPaddingY = DLG_PADDING_Y;
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 GetTextMetrics(hdc, &fontInfo);
6935 fontHeight = fontInfo.tmHeight;
6936
Bram Moolenaar734a8672019-12-02 22:49:38 +01006937 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006938 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
Bram Moolenaar734a8672019-12-02 22:49:38 +01006940 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006941 if (s_hwnd == NULL)
6942 {
6943 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaar734a8672019-12-02 22:49:38 +01006945 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006946 get_work_area(&workarea_rect);
6947 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006948 if (maxDialogWidth > adjust_by_system_dpi(600))
6949 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006950 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006951 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006952 }
6953 else
6954 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006955 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006956 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006958 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6959 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6960 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6961 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006962
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006963 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006964 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6965 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6966 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6967 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6968 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006969 }
6970
Bram Moolenaar734a8672019-12-02 22:49:38 +01006971 // Set dlgwidth to width of message.
6972 // Copy the message into "ga", changing NL to CR-NL and inserting line
6973 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 pstart = message;
6975 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006976 msgheight = 0;
6977 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 do
6979 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006980 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006981
Bram Moolenaar734a8672019-12-02 22:49:38 +01006982 // Need to figure out where to break the string. The system does it
6983 // at a word boundary, which would mean we can't compute the number of
6984 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006985 textWidth = 0;
6986 last_white = NULL;
6987 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006988 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006989 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006990 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006991 && textWidth > maxDialogWidth * 3 / 4)
6992 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006993 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006994 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006995 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006996 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006997 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006998 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006999 textWidth = 0;
7000
7001 if (last_white != NULL)
7002 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007003 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007004 if (pend > last_white)
7005 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007006 pend = last_white + 1;
7007 last_white = NULL;
7008 }
7009 ga_append(&ga, '\r');
7010 ga_append(&ga, '\n');
7011 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007012 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007013
7014 while (--l >= 0)
7015 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007016 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007017 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007019
7020 ga_append(&ga, '\r');
7021 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 pstart = pend + 1;
7023 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007024
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007025 if (ga.ga_data != NULL)
7026 message = ga.ga_data;
7027
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007029
K.Takatac81e9bf2022-01-16 14:15:49 +00007030 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7031 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
K.Takatac81e9bf2022-01-16 14:15:49 +00007033 // Add width of icon to dlgwidth, and some space
7034 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7035 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7036
7037 if (msgheight < dlg_icon_height)
7038 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040 /*
7041 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007042 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007044 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (!vertical)
7046 {
7047 // Place buttons horizontally if they fit.
7048 horizWidth = dlgPaddingX;
7049 pstart = tbuffer;
7050 i = 0;
7051 do
7052 {
7053 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7054 if (pend == NULL)
7055 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007056 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (textWidth < minButtonWidth)
7058 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007059 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 buttonWidths[i] = textWidth;
7061 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007062 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 pstart = pend + 1;
7064 } while (*pend != NUL);
7065
7066 if (horizWidth > maxDialogWidth)
7067 vertical = TRUE; // Too wide to fit on the screen.
7068 else if (horizWidth > dlgwidth)
7069 dlgwidth = horizWidth;
7070 }
7071
7072 if (vertical)
7073 {
7074 // Stack buttons vertically.
7075 pstart = tbuffer;
7076 do
7077 {
7078 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7079 if (pend == NULL)
7080 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007081 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007082 textWidth += dlgPaddingX; // Padding within button
7083 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 if (textWidth > dlgwidth)
7085 dlgwidth = textWidth;
7086 pstart = pend + 1;
7087 } while (*pend != NUL);
7088 }
7089
7090 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007091 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar734a8672019-12-02 22:49:38 +01007093 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007094 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
7096 add_long(lStyle);
7097 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007098 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 add_word(0); // NumberOfItems(will change later)
7100 add_word(10); // x
7101 add_word(10); // y
7102 add_word(PixelToDialogX(dlgwidth)); // cx
7103
7104 // Dialog height.
7105 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007106 dlgheight = msgheight + 2 * dlgPaddingY
7107 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 else
7109 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7110
7111 // Dialog needs to be taller if contains an edit box.
7112 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7113 if (textfield != NULL)
7114 dlgheight += editboxheight;
7115
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007117 if (dlgheight > maxDialogHeight)
7118 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007119 msgheight = msgheight - (dlgheight - maxDialogHeight);
7120 dlgheight = maxDialogHeight;
7121 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007122 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007123 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007124 }
7125
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 add_word(PixelToDialogY(dlgheight));
7127
7128 add_word(0); // Menu
7129 add_word(0); // Class
7130
Bram Moolenaar734a8672019-12-02 22:49:38 +01007131 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007132 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7133 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 p += nchar;
7135
K.Takatad1c58992022-01-23 12:31:57 +00007136 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007137# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007138 if (use_lfSysmenu)
7139 {
7140 // point size
7141 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7142 GetDeviceCaps(hdc, LOGPIXELSY));
7143 wcscpy(p, lfSysmenu.lfFaceName);
7144 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 }
K.Takatad1c58992022-01-23 12:31:57 +00007146 else
7147# endif
7148 {
7149 *p++ = DLG_FONT_POINT_SIZE; // point size
7150 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7151 }
7152 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 buttonYpos = msgheight + 2 * dlgPaddingY;
7155
7156 if (textfield != NULL)
7157 buttonYpos += editboxheight;
7158
7159 pstart = tbuffer;
7160 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007161 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 for (i = 0; i < numButtons; i++)
7163 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007164 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 for ( pend = pstart;
7166 *pend && (*pend != DLG_BUTTON_SEP);
7167 pend++)
7168 ;
7169
7170 if (*pend)
7171 *pend = '\0';
7172
7173 /*
7174 * old NOTE:
7175 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7176 * the focus to the first tab-able button and in so doing makes that
7177 * the default!! Grrr. Workaround: Make the default button the only
7178 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7179 * he/she can use arrow keys.
7180 *
7181 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007182 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 * dialog. Also needed for when the textfield is the default control.
7184 * It appears to work now (perhaps not on Win95?).
7185 */
7186 if (vertical)
7187 {
7188 p = add_dialog_element(p,
7189 (i == dfltbutton
7190 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7191 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007192 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 + 2 * fontHeight * i),
7194 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7195 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007196 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
7198 else
7199 {
7200 p = add_dialog_element(p,
7201 (i == dfltbutton
7202 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7203 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007204 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 PixelToDialogX(buttonWidths[i]),
7206 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007207 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007209 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
7211 *pnumitems += numButtons;
7212
Bram Moolenaar734a8672019-12-02 22:49:38 +01007213 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 p = add_dialog_element(p, SS_ICON,
7215 PixelToDialogX(dlgPaddingX),
7216 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007217 PixelToDialogX(dlg_icon_width),
7218 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7220 dlg_icons[type]);
7221
Bram Moolenaar734a8672019-12-02 22:49:38 +01007222 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007223 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007224 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007225 PixelToDialogY(dlgPaddingY),
7226 (WORD)(PixelToDialogX(messageWidth) + 1),
7227 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007228 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
Bram Moolenaar734a8672019-12-02 22:49:38 +01007230 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (textfield != NULL)
7232 {
7233 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7234 PixelToDialogX(2 * dlgPaddingX),
7235 PixelToDialogY(2 * dlgPaddingY + msgheight),
7236 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7237 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007238 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 *pnumitems += 1;
7240 }
7241
7242 *pnumitems += 2;
7243
7244 SelectFont(hdc, oldFont);
7245 DeleteObject(font);
7246 ReleaseDC(hwnd, hdc);
7247
Bram Moolenaar734a8672019-12-02 22:49:38 +01007248 // Let the dialog_callback() function know which button to make default
7249 // If we have an edit box, make that the default. We also need to tell
7250 // dialog_callback() if this dialog contains an edit box or not. We do
7251 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (textfield != NULL)
7253 {
7254 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7255 s_textfield = textfield;
7256 }
7257 else
7258 {
7259 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7260 s_textfield = NULL;
7261 }
7262
Bram Moolenaar734a8672019-12-02 22:49:38 +01007263 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007265 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 (LPDLGTEMPLATE)pdlgtemplate,
7267 s_hwnd,
7268 (DLGPROC)dialog_callback);
7269
7270 LocalFree(LocalHandle(pdlgtemplate));
7271 vim_free(tbuffer);
7272 vim_free(buttonWidths);
7273 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007274 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
Bram Moolenaar734a8672019-12-02 22:49:38 +01007276 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 (void)SetFocus(s_hwnd);
7278
7279 return nchar;
7280}
7281
Bram Moolenaar734a8672019-12-02 22:49:38 +01007282#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284/*
7285 * Put a simple element (basic class) onto a dialog template in memory.
7286 * return a pointer to where the next item should be added.
7287 *
7288 * parameters:
7289 * lStyle = additional style flags
7290 * (be careful, NT3.51 & Win32s will ignore the new ones)
7291 * x,y = x & y positions IN DIALOG UNITS
7292 * w,h = width and height IN DIALOG UNITS
7293 * Id = ID used in messages
7294 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7295 * caption = usually text or resource name
7296 *
7297 * TODO: use the length information noted here to enable the dialog creation
7298 * routines to work out more exactly how much memory they need to alloc.
7299 */
7300 static PWORD
7301add_dialog_element(
7302 PWORD p,
7303 DWORD lStyle,
7304 WORD x,
7305 WORD y,
7306 WORD w,
7307 WORD h,
7308 WORD Id,
7309 WORD clss,
7310 const char *caption)
7311{
7312 int nchar;
7313
Bram Moolenaar734a8672019-12-02 22:49:38 +01007314 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7316 *p++ = LOWORD(lStyle);
7317 *p++ = HIWORD(lStyle);
7318 *p++ = 0; // LOWORD (lExtendedStyle)
7319 *p++ = 0; // HIWORD (lExtendedStyle)
7320 *p++ = x;
7321 *p++ = y;
7322 *p++ = w;
7323 *p++ = h;
7324 *p++ = Id; //9 or 10 words in all
7325
7326 *p++ = (WORD)0xffff;
7327 *p++ = clss; //2 more here
7328
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007329 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 p += nchar;
7331
7332 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7333
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007334 return p; // total = 15 + strlen(caption) words
7335 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336}
7337
7338
7339/*
7340 * Helper routine. Take an input pointer, return closest pointer that is
7341 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7342 */
7343 static LPWORD
7344lpwAlign(
7345 LPWORD lpIn)
7346{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007347 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007349 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 ul += 3;
7351 ul >>= 2;
7352 ul <<= 2;
7353 return (LPWORD)ul;
7354}
7355
7356/*
7357 * Helper routine. Takes second parameter as Ansi string, copies it to first
7358 * parameter as wide character (16-bits / char) string, and returns integer
7359 * number of wide characters (words) in string (including the trailing wide
7360 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007361 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7362 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 static int
7364nCopyAnsiToWideChar(
7365 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007366 LPSTR lpAnsiIn,
7367 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007370 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 int i;
7372 WCHAR *wn;
7373
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007374 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007376 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007377 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (wn != NULL)
7379 {
7380 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007381 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 vim_free(wn);
7383 }
7384 }
7385 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007386 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 nChar = MultiByteToWideChar(
7388 enc_codepage > 0 ? enc_codepage : CP_ACP,
7389 MB_PRECOMPOSED,
7390 lpAnsiIn, len,
7391 lpWCStr, len);
7392 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007393 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 return nChar;
7397}
7398
7399
7400#ifdef FEAT_TEAROFF
7401/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007402 * Lookup menu handle from "menu_id".
7403 */
7404 static HMENU
7405tearoff_lookup_menuhandle(
7406 vimmenu_T *menu,
7407 WORD menu_id)
7408{
7409 for ( ; menu != NULL; menu = menu->next)
7410 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007411 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007412 continue;
7413 if (menu_is_separator(menu->dname))
7414 continue;
7415 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7416 return menu->submenu_id;
7417 }
7418 return NULL;
7419}
7420
7421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 * The callback function for all the modeless dialogs that make up the
7423 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7424 * thinking its menus have been clicked), and go away when closed.
7425 */
7426 static LRESULT CALLBACK
7427tearoff_callback(
7428 HWND hwnd,
7429 UINT message,
7430 WPARAM wParam,
7431 LPARAM lParam)
7432{
7433 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007434 {
7435 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaar734a8672019-12-02 22:49:38 +01007439 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 HandleMouseHide(message, lParam);
7441
7442 if (message == WM_COMMAND)
7443 {
7444 if ((WORD)(LOWORD(wParam)) & 0x8000)
7445 {
7446 POINT mp;
7447 RECT rect;
7448
7449 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7450 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007451 vimmenu_T *menu;
7452
7453 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007455 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7457 (int)rect.right - 8,
7458 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007459 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 s_hwnd,
7461 NULL);
7462 /*
7463 * NOTE: The pop-up menu can eat the mouse up event.
7464 * We deal with this in normal.c.
7465 */
7466 }
7467 }
7468 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007469 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7471 /*
7472 * Give main window the focus back: this is so after
7473 * choosing a tearoff button you can start typing again
7474 * straight away.
7475 */
7476 (void)SetFocus(s_hwnd);
7477 return TRUE;
7478 }
7479 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7480 {
7481 DestroyWindow(hwnd);
7482 return TRUE;
7483 }
7484
Bram Moolenaar734a8672019-12-02 22:49:38 +01007485 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (message == WM_EXITSIZEMOVE)
7487 (void)SetActiveWindow(s_hwnd);
7488
7489 return FALSE;
7490}
7491#endif
7492
7493
7494/*
K.Takatad1c58992022-01-23 12:31:57 +00007495 * Computes the dialog base units based on the current dialog font.
7496 * We don't use the GetDialogBaseUnits() API, because we don't use the
7497 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 */
7499 static void
7500get_dialog_font_metrics(void)
7501{
7502 HDC hdc;
7503 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 SIZE size;
7505#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007506 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#endif
7508
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007510 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007511 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513#endif
7514 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007515 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
K.Takatad1c58992022-01-23 12:31:57 +00007517 hdc = GetDC(s_hwnd);
7518 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007519 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007520 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
K.Takataabe628e2022-01-23 16:25:17 +00007522 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007523 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524}
7525
7526#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7527/*
7528 * Create a pseudo-"tearoff menu" based on the child
7529 * items of a given menu pointer.
7530 */
7531 static void
7532gui_mch_tearoff(
7533 char_u *title,
7534 vimmenu_T *menu,
7535 int initX,
7536 int initY)
7537{
7538 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7539 int template_len;
7540 int nchar, textWidth, submenuWidth;
7541 DWORD lStyle;
7542 DWORD lExtendedStyle;
7543 WORD dlgwidth;
7544 WORD menuID;
7545 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007546 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 vimmenu_T *the_menu = menu;
7548 HWND hwnd;
7549 HDC hdc;
7550 HFONT font, oldFont;
7551 int col, spaceWidth, len;
7552 int columnWidths[2];
7553 char_u *label, *text;
7554 int acLen = 0;
7555 int nameLen;
7556 int padding0, padding1, padding2 = 0;
7557 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007558 int x;
7559 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007560# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007561 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
7565 /*
7566 * If this menu is already torn off, move it to the mouse position.
7567 */
7568 if (IsWindow(menu->tearoff_handle))
7569 {
7570 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007571 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {
7573 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7574 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7575 }
7576 return;
7577 }
7578
7579 /*
7580 * Create a new tearoff.
7581 */
7582 if (*title == MNU_HIDDEN_CHAR)
7583 title++;
7584
Bram Moolenaar734a8672019-12-02 22:49:38 +01007585 // Allocate memory to store the dialog template. It's made bigger when
7586 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 template_len = DLG_ALLOC_SIZE;
7588 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7589 if (p == NULL)
7590 return;
7591
7592 hwnd = GetDesktopWindow();
7593 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007594# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7596 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007597 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 use_lfSysmenu = TRUE;
7599 }
7600 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007601# endif
K.Takatad1c58992022-01-23 12:31:57 +00007602 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7603 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7604
7605 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606
Bram Moolenaar734a8672019-12-02 22:49:38 +01007607 // Calculate width of a single space. Used for padding columns to the
7608 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007609 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610
Bram Moolenaar734a8672019-12-02 22:49:38 +01007611 // Figure out max width of the text column, the accelerator column and the
7612 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 submenuWidth = 0;
7614 for (col = 0; col < 2; col++)
7615 {
7616 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007617 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007619 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 text = (col == 0) ? pmenu->dname : pmenu->actext;
7621 if (text != NULL && *text != NUL)
7622 {
7623 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7624 if (textWidth > columnWidths[col])
7625 columnWidths[col] = textWidth;
7626 }
7627 if (pmenu->children != NULL)
7628 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7629 }
7630 }
7631 if (columnWidths[1] == 0)
7632 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007633 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 if (submenuWidth != 0)
7635 columnWidths[0] += submenuWidth;
7636 else
7637 columnWidths[0] += spaceWidth;
7638 }
7639 else
7640 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007641 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7643 columnWidths[1] += submenuWidth;
7644 }
7645
7646 /*
7647 * Now find the total width of our 'menu'.
7648 */
7649 textWidth = columnWidths[0] + columnWidths[1];
7650 if (submenuWidth != 0)
7651 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007652 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7654 textWidth += submenuWidth;
7655 }
7656 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7657 if (textWidth > dlgwidth)
7658 dlgwidth = textWidth;
7659 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7660
Bram Moolenaar734a8672019-12-02 22:49:38 +01007661 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007662 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
7664 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7665 *p++ = LOWORD(lStyle);
7666 *p++ = HIWORD(lStyle);
7667 *p++ = LOWORD(lExtendedStyle);
7668 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007669 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007671 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007673 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 else
7675 *p++ = PixelToDialogX(initX); // x
7676 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007677 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 else
7679 *p++ = PixelToDialogY(initY); // y
7680 *p++ = PixelToDialogX(dlgwidth); // cx
7681 ptrueheight = p;
7682 *p++ = 0; // dialog height: changed later anyway
7683 *p++ = 0; // Menu
7684 *p++ = 0; // Class
7685
Bram Moolenaar734a8672019-12-02 22:49:38 +01007686 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007688 ? (LPSTR)title
7689 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 p += nchar;
7691
K.Takatad1c58992022-01-23 12:31:57 +00007692 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007693# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007694 if (use_lfSysmenu)
7695 {
7696 // point size
7697 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7698 GetDeviceCaps(hdc, LOGPIXELSY));
7699 wcscpy(p, lfSysmenu.lfFaceName);
7700 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
K.Takatad1c58992022-01-23 12:31:57 +00007702 else
7703# endif
7704 {
7705 *p++ = DLG_FONT_POINT_SIZE; // point size
7706 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7707 }
7708 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
7710 /*
7711 * Loop over all the items in the menu.
7712 * But skip over the tearbar.
7713 */
7714 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7715 menu = menu->children->next;
7716 else
7717 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007718 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 for ( ; menu != NULL; menu = menu->next)
7720 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007721 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 continue;
7723 if (menu_is_separator(menu->dname))
7724 {
7725 sepPadding += 3;
7726 continue;
7727 }
7728
Bram Moolenaar734a8672019-12-02 22:49:38 +01007729 // Check if there still is plenty of room in the template. Make it
7730 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7732 {
7733 WORD *newp;
7734
7735 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7736 if (newp != NULL)
7737 {
7738 template_len += 4096;
7739 mch_memmove(newp, pdlgtemplate,
7740 (char *)p - (char *)pdlgtemplate);
7741 p = newp + (p - pdlgtemplate);
7742 pnumitems = newp + (pnumitems - pdlgtemplate);
7743 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7744 LocalFree(LocalHandle(pdlgtemplate));
7745 pdlgtemplate = newp;
7746 }
7747 }
7748
Bram Moolenaar734a8672019-12-02 22:49:38 +01007749 // Figure out minimal length of this menu label. Use "name" for the
7750 // actual text, "dname" for estimating the displayed size. "name"
7751 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 len = nameLen = (int)STRLEN(menu->name);
7753 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7754 (int)STRLEN(menu->dname))) / spaceWidth;
7755 len += padding0;
7756
7757 if (menu->actext != NULL)
7758 {
7759 acLen = (int)STRLEN(menu->actext);
7760 len += acLen;
7761 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7762 }
7763 else
7764 textWidth = 0;
7765 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7766 len += padding1;
7767
7768 if (menu->children == NULL)
7769 {
7770 padding2 = submenuWidth / spaceWidth;
7771 len += padding2;
7772 menuID = (WORD)(menu->id);
7773 }
7774 else
7775 {
7776 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007777 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779
Bram Moolenaar734a8672019-12-02 22:49:38 +01007780 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007781 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 if (label == NULL)
7783 break;
7784
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007785 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007786 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007788 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 while (padding0-- > 0)
7790 *text++ = ' ';
7791 if (menu->actext != NULL)
7792 {
7793 STRNCPY(text, menu->actext, acLen);
7794 text += acLen;
7795 }
7796 while (padding1-- > 0)
7797 *text++ = ' ';
7798 if (menu->children != NULL)
7799 {
7800 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7801 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7802 }
7803 else
7804 {
7805 while (padding2-- > 0)
7806 *text++ = ' ';
7807 }
7808 *text = NUL;
7809
7810 /*
7811 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7812 * W95/NT4 it makes the tear-off look more like a menu.
7813 */
7814 p = add_dialog_element(p,
7815 BS_PUSHBUTTON|BS_LEFT,
7816 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7817 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7818 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7819 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007820 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 vim_free(label);
7822 (*pnumitems)++;
7823 }
7824
7825 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7826
7827
Bram Moolenaar734a8672019-12-02 22:49:38 +01007828 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007829 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007830 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 (LPDLGTEMPLATE)pdlgtemplate,
7832 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007833 (DLGPROC)tearoff_callback,
7834 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836 LocalFree(LocalHandle(pdlgtemplate));
7837 SelectFont(hdc, oldFont);
7838 DeleteObject(font);
7839 ReleaseDC(hwnd, hdc);
7840
7841 /*
7842 * Reassert ourselves as the active window. This is so that after creating
7843 * a tearoff, the user doesn't have to click with the mouse just to start
7844 * typing again!
7845 */
7846 (void)SetActiveWindow(s_hwnd);
7847
Bram Moolenaar734a8672019-12-02 22:49:38 +01007848 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 force_menu_update = TRUE;
7850}
7851#endif
7852
7853#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007854# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856/*
7857 * Create the toolbar, initially unpopulated.
7858 * (just like the menu, there are no defaults, it's all
7859 * set up through menu.vim)
7860 */
7861 static void
7862initialise_toolbar(void)
7863{
7864 InitCommonControls();
7865 s_toolbarhwnd = CreateToolbarEx(
7866 s_hwnd,
7867 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7868 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007869 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007870 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 IDR_TOOLBAR1, // id of initial bitmap
7872 NULL,
7873 0, // initial number of buttons
7874 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7875 TOOLBAR_BUTTON_HEIGHT,
7876 TOOLBAR_BUTTON_WIDTH,
7877 TOOLBAR_BUTTON_HEIGHT,
7878 sizeof(TBBUTTON)
7879 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007880
7881 // Remove transparency from the toolbar to prevent the main window
7882 // background colour showing through
7883 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7884 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7885
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007886 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
7888 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007889
7890 update_toolbar_size();
7891}
7892
7893 static void
7894update_toolbar_size(void)
7895{
7896 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007897 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007898
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007899 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007900 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7901 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7902 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7903 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7904
7905 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7906 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7907 //TRACE("button size: %d, %d", w, h);
7908 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7909 gui.toolbar_height = h + 6;
7910
7911 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7912 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7913
7914 // TODO:
7915 // Currently, this function only updates the size of toolbar buttons.
7916 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917}
7918
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007919 static LRESULT CALLBACK
7920toolbar_wndproc(
7921 HWND hwnd,
7922 UINT uMsg,
7923 WPARAM wParam,
7924 LPARAM lParam)
7925{
7926 HandleMouseHide(uMsg, lParam);
7927 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7928}
7929
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 static int
7931get_toolbar_bitmap(vimmenu_T *menu)
7932{
7933 int i = -1;
7934
7935 /*
7936 * Check user bitmaps first, unless builtin is specified.
7937 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007938 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
7940 char_u fname[MAXPATHL];
7941 HANDLE hbitmap = NULL;
7942
7943 if (menu->iconfile != NULL)
7944 {
7945 gui_find_iconfile(menu->iconfile, fname, "bmp");
7946 hbitmap = LoadImage(
7947 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007948 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 IMAGE_BITMAP,
7950 TOOLBAR_BUTTON_WIDTH,
7951 TOOLBAR_BUTTON_HEIGHT,
7952 LR_LOADFROMFILE |
7953 LR_LOADMAP3DCOLORS
7954 );
7955 }
7956
7957 /*
7958 * If the LoadImage call failed, or the "icon=" file
7959 * didn't exist or wasn't specified, try the menu name
7960 */
7961 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007962 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007963# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007964 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007965# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007966 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 hbitmap = LoadImage(
7968 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007969 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 IMAGE_BITMAP,
7971 TOOLBAR_BUTTON_WIDTH,
7972 TOOLBAR_BUTTON_HEIGHT,
7973 LR_LOADFROMFILE |
7974 LR_LOADMAP3DCOLORS
7975 );
7976
7977 if (hbitmap != NULL)
7978 {
7979 TBADDBITMAP tbAddBitmap;
7980
7981 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007982 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983
7984 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7985 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007986 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
7988 }
7989 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7990 i = menu->iconidx;
7991
7992 return i;
7993}
7994#endif
7995
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007996#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7997 static void
7998initialise_tabline(void)
7999{
8000 InitCommonControls();
8001
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008002 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008003 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008004 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008005 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008006 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008007
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008008 gui.tabline_height = TABLINE_HEIGHT;
8009
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008010 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008011}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008012
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008013/*
8014 * Get tabpage_T from POINT.
8015 */
8016 static tabpage_T *
8017GetTabFromPoint(
8018 HWND hWnd,
8019 POINT pt)
8020{
8021 tabpage_T *ptp = NULL;
8022
8023 if (gui_mch_showing_tabline())
8024 {
8025 TCHITTESTINFO htinfo;
8026 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008027 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008028 if (s_tabhwnd == hWnd)
8029 {
8030 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8031 if (idx != -1)
8032 ptp = find_tabpage(idx + 1);
8033 }
8034 }
8035 return ptp;
8036}
8037
8038static POINT s_pt = {0, 0};
8039static HCURSOR s_hCursor = NULL;
8040
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008041 static LRESULT CALLBACK
8042tabline_wndproc(
8043 HWND hwnd,
8044 UINT uMsg,
8045 WPARAM wParam,
8046 LPARAM lParam)
8047{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008048 POINT pt;
8049 tabpage_T *tp;
8050 RECT rect;
8051 int nCenter;
8052 int idx0;
8053 int idx1;
8054
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008055 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008056
8057 switch (uMsg)
8058 {
8059 case WM_LBUTTONDOWN:
8060 {
8061 s_pt.x = GET_X_LPARAM(lParam);
8062 s_pt.y = GET_Y_LPARAM(lParam);
8063 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008064 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008065 break;
8066 }
8067 case WM_MOUSEMOVE:
8068 if (GetCapture() == hwnd
8069 && ((wParam & MK_LBUTTON)) != 0)
8070 {
8071 pt.x = GET_X_LPARAM(lParam);
8072 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008073 if (abs(pt.x - s_pt.x) >
8074 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008075 {
8076 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8077
8078 tp = GetTabFromPoint(hwnd, pt);
8079 if (tp != NULL)
8080 {
8081 idx0 = tabpage_index(curtab) - 1;
8082 idx1 = tabpage_index(tp) - 1;
8083
8084 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8085 nCenter = rect.left + (rect.right - rect.left) / 2;
8086
Bram Moolenaar734a8672019-12-02 22:49:38 +01008087 // Check if the mouse cursor goes over the center of
8088 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008089 if ((idx0 < idx1) && (nCenter < pt.x))
8090 {
8091 tabpage_move(idx1 + 1);
8092 update_screen(0);
8093 }
8094 else if ((idx1 < idx0) && (pt.x < nCenter))
8095 {
8096 tabpage_move(idx1);
8097 update_screen(0);
8098 }
8099 }
8100 }
8101 }
8102 break;
8103 case WM_LBUTTONUP:
8104 {
8105 if (GetCapture() == hwnd)
8106 {
8107 SetCursor(s_hCursor);
8108 ReleaseCapture();
8109 }
8110 break;
8111 }
regomne3bdef102022-09-26 20:48:32 +01008112 case WM_MBUTTONUP:
8113 {
8114 TCHITTESTINFO htinfo;
8115
8116 htinfo.pt.x = GET_X_LPARAM(lParam);
8117 htinfo.pt.y = GET_Y_LPARAM(lParam);
8118 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8119 if (idx0 != -1)
8120 {
8121 idx0 += 1;
8122 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8123 }
8124 break;
8125 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008126 default:
8127 break;
8128 }
8129
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008130 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8131}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008132#endif
8133
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8135/*
8136 * Make the GUI window come to the foreground.
8137 */
8138 void
8139gui_mch_set_foreground(void)
8140{
8141 if (IsIconic(s_hwnd))
8142 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8143 SetForegroundWindow(s_hwnd);
8144}
8145#endif
8146
8147#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8148 static void
8149dyn_imm_load(void)
8150{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008151 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 if (hLibImm == NULL)
8153 return;
8154
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008156 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008158 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008160 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008162 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008164 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008166 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008167 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008168 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008169 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008170 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008172 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008174 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008175 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008176 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177
K.Takatab0b2b732022-01-19 12:59:21 +00008178 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 || pImmGetContext == NULL
8180 || pImmAssociateContext == NULL
8181 || pImmReleaseContext == NULL
8182 || pImmGetOpenStatus == NULL
8183 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008184 || pImmGetCompositionFontW == NULL
8185 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008187 || pImmGetConversionStatus == NULL
8188 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
8190 FreeLibrary(hLibImm);
8191 hLibImm = NULL;
8192 pImmGetContext = NULL;
8193 return;
8194 }
8195
8196 return;
8197}
8198
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199#endif
8200
8201#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8202
8203# ifdef FEAT_XPM_W32
8204# define IMAGE_XPM 100
8205# endif
8206
8207typedef struct _signicon_t
8208{
8209 HANDLE hImage;
8210 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008211# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008212 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214} signicon_t;
8215
8216 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008217gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
8219 signicon_t *sign;
8220 int x, y, w, h;
8221
8222 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8223 return;
8224
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008225# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008226 if (IS_ENABLE_DIRECTX())
8227 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008228# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 x = TEXT_X(col);
8231 y = TEXT_Y(row);
8232 w = gui.char_width * 2;
8233 h = gui.char_height;
8234 switch (sign->uType)
8235 {
8236 case IMAGE_BITMAP:
8237 {
8238 HDC hdcMem;
8239 HBITMAP hbmpOld;
8240
8241 hdcMem = CreateCompatibleDC(s_hdc);
8242 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8243 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8244 SelectObject(hdcMem, hbmpOld);
8245 DeleteDC(hdcMem);
8246 }
8247 break;
8248 case IMAGE_ICON:
8249 case IMAGE_CURSOR:
8250 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8251 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008252# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 case IMAGE_XPM:
8254 {
8255 HDC hdcMem;
8256 HBITMAP hbmpOld;
8257
8258 hdcMem = CreateCompatibleDC(s_hdc);
8259 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008260 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8262
8263 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008264 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8266 SelectObject(hdcMem, hbmpOld);
8267 DeleteDC(hdcMem);
8268 }
8269 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 }
8272}
8273
8274 static void
8275close_signicon_image(signicon_t *sign)
8276{
8277 if (sign)
8278 switch (sign->uType)
8279 {
8280 case IMAGE_BITMAP:
8281 DeleteObject((HGDIOBJ)sign->hImage);
8282 break;
8283 case IMAGE_CURSOR:
8284 DestroyCursor((HCURSOR)sign->hImage);
8285 break;
8286 case IMAGE_ICON:
8287 DestroyIcon((HICON)sign->hImage);
8288 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008289# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 case IMAGE_XPM:
8291 DeleteObject((HBITMAP)sign->hImage);
8292 DeleteObject((HBITMAP)sign->hShape);
8293 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296}
8297
8298 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008299gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 signicon_t sign, *psign;
8302 char_u *ext;
8303
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008305 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (ext > signfile)
8307 {
8308 int do_load = 1;
8309
8310 if (!STRICMP(ext, ".bmp"))
8311 sign.uType = IMAGE_BITMAP;
8312 else if (!STRICMP(ext, ".ico"))
8313 sign.uType = IMAGE_ICON;
8314 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8315 sign.uType = IMAGE_CURSOR;
8316 else
8317 do_load = 0;
8318
8319 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008320 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 gui.char_width * 2, gui.char_height,
8322 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008323# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 if (!STRICMP(ext, ".xpm"))
8325 {
8326 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008327 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8328 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 }
8332
8333 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008334 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 *psign = sign;
8336
8337 if (!psign)
8338 {
8339 if (sign.hImage)
8340 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008341 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343 return (void *)psign;
8344
8345}
8346
8347 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008348gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 if (sign)
8351 {
8352 close_signicon_image((signicon_t *)sign);
8353 vim_free(sign);
8354 }
8355}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008358#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
Bram Moolenaar734a8672019-12-02 22:49:38 +01008360/*
8361 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008362 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008364 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8366 * to get current mouse position).
8367 *
8368 * Trying to use as more Windows services as possible, and as less
8369 * IE version as possible :)).
8370 *
8371 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8372 * BalloonEval struct.
8373 * 2) Enable/Disable simply create/kill BalloonEval Timer
8374 * 3) When there was enough inactivity, timer procedure posts
8375 * async request to debugger
8376 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8377 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008378 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 */
8380
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008381 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008382make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008383{
K.Takata76687d22022-01-25 10:31:37 +00008384 TOOLINFOW *pti;
8385 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008386
K.Takata76687d22022-01-25 10:31:37 +00008387 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008388 if (pti == NULL)
8389 return;
8390
8391 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8392 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8393 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008394 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008395
8396 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8397 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8398
K.Takata76687d22022-01-25 10:31:37 +00008399 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008400 pti->uFlags = TTF_SUBCLASS;
8401 pti->hwnd = beval->target;
8402 pti->hinst = 0; // Don't use string resources
8403 pti->uId = ID_BEVAL_TOOLTIP;
8404
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008405 pti->lpszText = LPSTR_TEXTCALLBACKW;
8406 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8407 pti->lParam = (LPARAM)beval->tofree;
8408 // switch multiline tooltips on
8409 if (GetClientRect(s_textArea, &rect))
8410 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8411 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008412
8413 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8414 pti->rect.left = pt.x - 3;
8415 pti->rect.top = pt.y - 3;
8416 pti->rect.right = pt.x + 3;
8417 pti->rect.bottom = pt.y + 3;
8418
8419 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8420 // Make tooltip appear sooner.
8421 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8422 // I've performed some tests and it seems the longest possible life time
8423 // of tooltip is 30 seconds.
8424 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8425 /*
8426 * HACK: force tooltip to appear, because it'll not appear until
8427 * first mouse move. D*mn M$
8428 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8429 */
8430 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8431 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8432 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008433}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008436delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008438 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439}
8440
8441 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008442beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008443 HWND hwnd UNUSED,
8444 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008445 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008446 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 POINT pt;
8449 RECT rect;
8450
8451 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8452 return;
8453
8454 GetCursorPos(&pt);
8455 if (WindowFromPoint(pt) != s_textArea)
8456 return;
8457
8458 ScreenToClient(s_textArea, &pt);
8459 GetClientRect(s_textArea, &rect);
8460 if (!PtInRect(&rect, pt))
8461 return;
8462
K.Takataa8ec4912022-02-03 14:32:33 +00008463 if (last_user_activity > 0
8464 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 && (cur_beval->showState != ShS_PENDING
8466 || abs(cur_beval->x - pt.x) > 3
8467 || abs(cur_beval->y - pt.y) > 3))
8468 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008469 // Pointer resting in one place long enough, it's time to show
8470 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 cur_beval->showState = ShS_PENDING;
8472 cur_beval->x = pt.x;
8473 cur_beval->y = pt.y;
8474
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 if (cur_beval->msgCB != NULL)
8476 (*cur_beval->msgCB)(cur_beval, 0);
8477 }
8478}
8479
8480 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008481gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
K.Takataa8ec4912022-02-03 14:32:33 +00008483 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484}
8485
8486 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008487gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (beval == NULL)
8490 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008491 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8492 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008496gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008499
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008500 vim_free(beval->msg);
8501 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8502 if (beval->msg == NULL)
8503 {
8504 delete_tooltip(beval);
8505 beval->showState = ShS_NEUTRAL;
8506 return;
8507 }
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 if (beval->showState == ShS_SHOWING)
8510 return;
8511 GetCursorPos(&pt);
8512 ScreenToClient(s_textArea, &pt);
8513
8514 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008516 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 gui_mch_disable_beval_area(cur_beval);
8518 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008519 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521}
8522
8523 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008524gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008525 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008526 char_u *mesg,
8527 void (*mesgCB)(BalloonEval *, int),
8528 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008530 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 BalloonEval *beval;
8532
8533 if (mesg != NULL && mesgCB != NULL)
8534 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008535 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 return NULL;
8537 }
8538
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008539 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (beval != NULL)
8541 {
8542 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543
8544 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 beval->msg = mesg;
8546 beval->msgCB = mesgCB;
8547 beval->clientData = clientData;
8548
8549 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 cur_beval = beval;
8551
8552 if (p_beval)
8553 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 }
8555 return beval;
8556}
8557
8558 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008559Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008561 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 return;
8563
8564 if (cur_beval != NULL)
8565 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008566 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008568 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008569 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008570 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 delete_tooltip(cur_beval);
8572 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
8574 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008575 break;
8576 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008577 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008578 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008579 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008580 info->lpszText = (LPSTR)info->lParam;
8581 info->uFlags |= TTF_DI_SETITEM;
8582 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008583 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008584 case TTN_GETDISPINFOW:
8585 {
8586 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008587 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008588 info->lpszText = (LPWSTR)info->lParam;
8589 info->uFlags |= TTF_DI_SETITEM;
8590 }
8591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593 }
8594}
8595
8596 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008597track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8600 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008601 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602}
8603
8604 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008605gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008607# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008608 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008609# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008610 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 vim_free(beval);
8612}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008613#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
8615#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8616/*
8617 * We have multiple signs to draw at the same location. Draw the
8618 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8619 */
8620 void
8621netbeans_draw_multisign_indicator(int row)
8622{
8623 int i;
8624 int y;
8625 int x;
8626
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008627 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008628 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008629
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 x = 0;
8631 y = TEXT_Y(row);
8632
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008633# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008634 if (IS_ENABLE_DIRECTX())
8635 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008636# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008637
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 for (i = 0; i < gui.char_height - 3; i++)
8639 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8640
8641 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8642 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8643 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8644 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8645 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8646 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8647 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8648}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008649#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008650
8651#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008652
Christopher Plewright20b795e2022-12-20 20:01:58 +00008653// TODO: at the moment, this is just a copy of test_gui_mouse_event.
8654// But, we could instead generate actual Win32 mouse event messages,
8655// ie. to make it consistent wih test_gui_w32_sendevent_keyboard.
8656 static int
8657test_gui_w32_sendevent_mouse(dict_T *args)
8658{
8659 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008660 return FALSE;
8661
Christopher Plewright20b795e2022-12-20 20:01:58 +00008662 // Note: "move" is optional, requires fewer arguments
8663 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008664
Christopher Plewright20b795e2022-12-20 20:01:58 +00008665 if (!move && (!dict_has_key(args, "button")
8666 || !dict_has_key(args, "multiclick")
8667 || !dict_has_key(args, "modifiers")))
8668 return FALSE;
8669
8670 int row = (int)dict_get_number(args, "row");
8671 int col = (int)dict_get_number(args, "col");
8672
8673 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008674 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00008675 // the "move" argument expects row and col coordnates to be in pixels,
8676 // unless "cell" is specified and is TRUE.
8677 if (dict_get_bool(args, "cell", FALSE))
8678 {
8679 // calculate the middle of the character cell
8680 // Note: Cell coordinates are 1-based from vimscript
8681 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
8682 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
8683 gui_mouse_moved(pX, pY);
8684 }
8685 else
8686 gui_mouse_moved(col, row);
8687 }
8688 else
8689 {
8690 int button = (int)dict_get_number(args, "button");
8691 int repeated_click = (int)dict_get_number(args, "multiclick");
8692 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008693
Christopher Plewright20b795e2022-12-20 20:01:58 +00008694 // Reset the scroll values to known values.
8695 // XXX: Remove this when/if the scroll step is made configurable.
8696 mouse_set_hor_scroll_step(6);
8697 mouse_set_vert_scroll_step(3);
8698
8699 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
8700 repeated_click, mods);
8701 }
8702 return TRUE;
8703}
8704
8705 static int
8706test_gui_w32_sendevent_keyboard(dict_T *args)
8707{
8708 INPUT inputs[1];
8709 INPUT modkeys[3];
8710 SecureZeroMemory(inputs, sizeof(INPUT));
8711 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
8712
8713 char_u *event = dict_get_string(args, "event", TRUE);
8714
8715 if (event && (STRICMP(event, "keydown") == 0
8716 || STRICMP(event, "keyup") == 0))
8717 {
8718 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008719 if (vkCode <= 0 || vkCode >= 0xFF)
8720 {
8721 semsg(_(e_invalid_argument_nr), (long)vkCode);
8722 return FALSE;
8723 }
8724
Christopher Plewright20b795e2022-12-20 20:01:58 +00008725 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
8726 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
8727 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
8728 || vkCode == VK_LMENU || vkCode == VK_RMENU );
8729
8730 BOOL unwrapMods = FALSE;
8731 int mods = (int)dict_get_number(args, "modifiers");
8732
8733 // If there are modifiers in the args, and it is not a keyup event and
8734 // vkCode is not a modifier key, then we generate virtual modifier key
8735 // messages before sending the actual key message.
8736 if(mods && STRICMP(event, "keydown") == 0 && !isModKey)
8737 {
8738 int n = 0;
8739 if (mods & MOD_MASK_SHIFT)
8740 {
8741 modkeys[n].type = INPUT_KEYBOARD;
8742 modkeys[n].ki.wVk = VK_LSHIFT;
8743 n++;
8744 }
8745 if (mods & MOD_MASK_CTRL)
8746 {
8747 modkeys[n].type = INPUT_KEYBOARD;
8748 modkeys[n].ki.wVk = VK_LCONTROL;
8749 n++;
8750 }
8751 if (mods & MOD_MASK_ALT)
8752 {
8753 modkeys[n].type = INPUT_KEYBOARD;
8754 modkeys[n].ki.wVk = VK_LMENU;
8755 n++;
8756 }
8757 if (n)
8758 {
8759 (void)SetForegroundWindow(s_hwnd);
8760 SendInput(n, modkeys, sizeof(INPUT));
8761 }
8762 }
8763
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008764 inputs[0].type = INPUT_KEYBOARD;
8765 inputs[0].ki.wVk = vkCode;
8766 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008767 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008768 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Christopher Plewright20b795e2022-12-20 20:01:58 +00008769 if(!isModKey)
8770 unwrapMods = TRUE;
8771 }
8772
K.Takatafef38d82022-09-07 19:03:42 +01008773 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008774 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00008775 vim_free(event);
8776
8777 if (unwrapMods)
8778 {
8779 modkeys[0].type = INPUT_KEYBOARD;
8780 modkeys[0].ki.wVk = VK_LSHIFT;
8781 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
8782
8783 modkeys[1].type = INPUT_KEYBOARD;
8784 modkeys[1].ki.wVk = VK_LCONTROL;
8785 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
8786
8787 modkeys[2].type = INPUT_KEYBOARD;
8788 modkeys[2].ki.wVk = VK_LMENU;
8789 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
8790
8791 (void)SetForegroundWindow(s_hwnd);
8792 SendInput(3, modkeys, sizeof(INPUT));
8793 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008794 }
8795 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00008796 {
8797 if (event == NULL)
8798 {
8799 semsg(_(e_missing_argument_str), "event");
8800 }
8801 else
8802 {
8803 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8804 vim_free(event);
8805 }
8806 return FALSE;
8807 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008808 return TRUE;
8809}
Christopher Plewright20b795e2022-12-20 20:01:58 +00008810
8811 int
8812test_gui_w32_sendevent(char_u *event, dict_T *args)
8813{
8814 if (STRICMP(event, "key") == 0)
8815 return test_gui_w32_sendevent_keyboard(args);
8816 else if (STRICMP(event, "mouse") == 0)
8817 return test_gui_w32_sendevent_mouse(args);
8818 else
8819 {
8820 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8821 return FALSE;
8822 }
8823}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008824#endif