blob: 206645a4f181ec48a207ca83d04ed2c8d4321899 [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{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000437 if (s_textArea == NULL)
438 return;
439
440 RECT rect;
441 GetClientRect(s_textArea, &rect);
442 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100443}
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
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000666 if (blink_timer == 0)
667 return;
668
669 KillTimer(NULL, blink_timer);
670 // Eat spurious WM_TIMER messages
671 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
672 ;
673 blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100674}
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);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000891
Christopher Plewright7b0afc12022-12-30 16:54:58 +0000892 // 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 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001037
1038 if (button < 0)
1039 return;
1040
1041 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1042
1043 /*
1044 * Holding down the left and right buttons simulates pushing the middle
1045 * button.
1046 */
1047 if (repeated_click
1048 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1049 || (button == MOUSE_RIGHT
1050 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001051 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001052 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001053 * Hmm, gui.c will ignore more than one button down at a time, so
1054 * pretend we let go of it first.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001055 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001056 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1057 button = MOUSE_MIDDLE;
1058 repeated_click = FALSE;
1059 s_button_pending = -1;
1060 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001061 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001062 else if ((repeated_click)
1063 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1064 {
1065 if (s_button_pending > -1)
1066 {
1067 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1068 s_button_pending = -1;
1069 }
1070 // TRACE("Button down at x %d, y %d\n", x, y);
1071 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1072 }
1073 else
1074 {
1075 /*
1076 * If this is the first press (i.e. not a multiple click) don't
1077 * action immediately, but store and wait for:
1078 * i) button-up
1079 * ii) mouse move
1080 * iii) another button press
1081 * before using it.
1082 * This enables us to make left+right simulate middle button,
1083 * without left or right being actioned first. The side-effect is
1084 * that if you click and hold the mouse without dragging, the
1085 * cursor doesn't move until you release the button. In practice
1086 * this is hardly a problem.
1087 */
1088 s_button_pending = button;
1089 s_x_pending = x;
1090 s_y_pending = y;
1091 s_kFlags_pending = keyFlags;
1092 }
1093
1094 s_prevTime = currentTime;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001095}
1096
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001097 static void
1098_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001099 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001100 int x,
1101 int y,
1102 UINT keyFlags)
1103{
1104 int button;
1105
1106 s_getting_focus = FALSE;
1107 if (s_button_pending > -1)
1108 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001109 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001110 _OnMouseEvent(s_button_pending, s_x_pending,
1111 s_y_pending, FALSE, s_kFlags_pending);
1112 s_button_pending = -1;
1113 }
1114 if (s_uMsg == WM_MOUSEMOVE)
1115 {
1116 /*
1117 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1118 * down.
1119 */
1120 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1121 | MK_XBUTTON1 | MK_XBUTTON2)))
1122 {
1123 gui_mouse_moved(x, y);
1124 return;
1125 }
1126
1127 /*
1128 * While button is down, keep grabbing mouse move events when
1129 * the mouse goes outside the window
1130 */
1131 SetCapture(s_textArea);
1132 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001133 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001134 }
1135 else
1136 {
1137 ReleaseCapture();
1138 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001139 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001140 }
1141
1142 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1143}
1144
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001145 static void
1146_OnSizeTextArea(
1147 HWND hwnd UNUSED,
1148 UINT state UNUSED,
1149 int cx UNUSED,
1150 int cy UNUSED)
1151{
1152#if defined(FEAT_DIRECTX)
1153 if (IS_ENABLE_DIRECTX())
1154 directx_binddc();
1155#endif
1156}
1157
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001158#ifdef FEAT_MENU
1159/*
1160 * Find the vimmenu_T with the given id
1161 */
1162 static vimmenu_T *
1163gui_mswin_find_menu(
1164 vimmenu_T *pMenu,
1165 int id)
1166{
1167 vimmenu_T *pChildMenu;
1168
1169 while (pMenu)
1170 {
1171 if (pMenu->id == (UINT)id)
1172 break;
1173 if (pMenu->children != NULL)
1174 {
1175 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1176 if (pChildMenu)
1177 {
1178 pMenu = pChildMenu;
1179 break;
1180 }
1181 }
1182 pMenu = pMenu->next;
1183 }
1184 return pMenu;
1185}
1186
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001187 static void
1188_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001189 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001191 HWND hwndCtl UNUSED,
1192 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001193{
1194 vimmenu_T *pMenu;
1195
1196 pMenu = gui_mswin_find_menu(root_menu, id);
1197 if (pMenu)
1198 gui_menu_cb(pMenu);
1199}
1200#endif
1201
1202#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001203/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001204 * Handle a Find/Replace window message.
1205 */
1206 static void
1207_OnFindRepl(void)
1208{
1209 int flags = 0;
1210 int down;
1211
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001212 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001213 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001214 (void)SetFocus(s_hwnd);
1215
1216 if (s_findrep_struct.Flags & FR_FINDNEXT)
1217 {
1218 flags = FRD_FINDNEXT;
1219
Bram Moolenaar734a8672019-12-02 22:49:38 +01001220 // Give main window the focus back: this is so the cursor isn't
1221 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001222 (void)SetFocus(s_hwnd);
1223 }
1224 else if (s_findrep_struct.Flags & FR_REPLACE)
1225 {
1226 flags = FRD_REPLACE;
1227
Bram Moolenaar734a8672019-12-02 22:49:38 +01001228 // Give main window the focus back: this is so the cursor isn't
1229 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001230 (void)SetFocus(s_hwnd);
1231 }
1232 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1233 {
1234 flags = FRD_REPLACEALL;
1235 }
1236
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001237 if (flags == 0)
1238 return;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001239
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001240 char_u *p, *q;
1241
1242 // Call the generic GUI function to do the actual work.
1243 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1244 flags |= FRD_WHOLE_WORD;
1245 if (s_findrep_struct.Flags & FR_MATCHCASE)
1246 flags |= FRD_MATCH_CASE;
1247 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1248 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1249 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1250 if (p != NULL && q != NULL)
1251 gui_do_findrepl(flags, p, q, down);
1252 vim_free(p);
1253 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001254}
1255#endif
1256
1257 static void
1258HandleMouseHide(UINT uMsg, LPARAM lParam)
1259{
1260 static LPARAM last_lParam = 0L;
1261
Bram Moolenaar734a8672019-12-02 22:49:38 +01001262 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1264 {
1265 if (lParam == last_lParam)
1266 return;
1267 last_lParam = lParam;
1268 }
1269
Bram Moolenaar734a8672019-12-02 22:49:38 +01001270 // Handle specially, to centralise coding. We need to be sure we catch all
1271 // possible events which should cause us to restore the cursor (as it is a
1272 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 switch (uMsg)
1274 {
1275 case WM_KEYUP:
1276 case WM_CHAR:
1277 /*
1278 * blank out the pointer if necessary
1279 */
1280 if (p_mh)
1281 gui_mch_mousehide(TRUE);
1282 break;
1283
Bram Moolenaar734a8672019-12-02 22:49:38 +01001284 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001285 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001286 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001287 case WM_LBUTTONDOWN:
1288 case WM_LBUTTONUP:
1289 case WM_MBUTTONDOWN:
1290 case WM_MBUTTONUP:
1291 case WM_RBUTTONDOWN:
1292 case WM_RBUTTONUP:
1293 case WM_XBUTTONDOWN:
1294 case WM_XBUTTONUP:
1295 case WM_NCMOUSEMOVE:
1296 case WM_NCLBUTTONDOWN:
1297 case WM_NCLBUTTONUP:
1298 case WM_NCMBUTTONDOWN:
1299 case WM_NCMBUTTONUP:
1300 case WM_NCRBUTTONDOWN:
1301 case WM_NCRBUTTONUP:
1302 case WM_KILLFOCUS:
1303 /*
1304 * if the pointer is currently hidden, then we should show it.
1305 */
1306 gui_mch_mousehide(FALSE);
1307 break;
1308 }
1309}
1310
1311 static LRESULT CALLBACK
1312_TextAreaWndProc(
1313 HWND hwnd,
1314 UINT uMsg,
1315 WPARAM wParam,
1316 LPARAM lParam)
1317{
1318 /*
1319 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1320 hwnd, uMsg, wParam, lParam);
1321 */
1322
1323 HandleMouseHide(uMsg, lParam);
1324
1325 s_uMsg = uMsg;
1326 s_wParam = wParam;
1327 s_lParam = lParam;
1328
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001329#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001330 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331#endif
1332
1333 switch (uMsg)
1334 {
1335 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1336 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1337 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1338 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1339 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1340 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1341 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1342 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1343 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1344 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1345 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1346 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1347 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1348 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001349 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001350
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001351#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001352 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1353 return TRUE;
1354#endif
1355 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001356 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001357 }
1358}
1359
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001360/*
1361 * Called when the foreground or background color has been changed.
1362 */
1363 void
1364gui_mch_new_colors(void)
1365{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001366 HBRUSH prevBrush;
1367
1368 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001369 prevBrush = (HBRUSH)SetClassLongPtr(
1370 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001371 InvalidateRect(s_hwnd, NULL, TRUE);
1372 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001373}
1374
1375/*
1376 * Set the colors to their default values.
1377 */
1378 void
1379gui_mch_def_colors(void)
1380{
1381 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1382 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1383 gui.def_norm_pixel = gui.norm_pixel;
1384 gui.def_back_pixel = gui.back_pixel;
1385}
1386
1387/*
1388 * Open the GUI window which was created by a call to gui_mch_init().
1389 */
1390 int
1391gui_mch_open(void)
1392{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001393 // Actually open the window, if not already visible
1394 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001395 if (!IsWindowVisible(s_hwnd))
1396 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1397
1398#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001399 // Init replace string here, so that we keep it when re-opening the
1400 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001401 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1402#endif
1403
1404 return OK;
1405}
1406
1407/*
1408 * Get the position of the top left corner of the window.
1409 */
1410 int
1411gui_mch_get_winpos(int *x, int *y)
1412{
1413 RECT rect;
1414
1415 GetWindowRect(s_hwnd, &rect);
1416 *x = rect.left;
1417 *y = rect.top;
1418 return OK;
1419}
1420
1421/*
1422 * Set the position of the top left corner of the window to the given
1423 * coordinates.
1424 */
1425 void
1426gui_mch_set_winpos(int x, int y)
1427{
1428 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1429 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1430}
1431 void
1432gui_mch_set_text_area_pos(int x, int y, int w, int h)
1433{
1434 static int oldx = 0;
1435 static int oldy = 0;
1436
1437 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1438
1439#ifdef FEAT_TOOLBAR
1440 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1441 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001442 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001443#endif
1444#if defined(FEAT_GUI_TABLINE)
1445 if (showing_tabline)
1446 {
1447 int top = 0;
1448 RECT rect;
1449
1450# ifdef FEAT_TOOLBAR
1451 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001452 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001453# endif
1454 GetClientRect(s_hwnd, &rect);
1455 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1456 }
1457#endif
1458
Bram Moolenaar734a8672019-12-02 22:49:38 +01001459 // When side scroll bar is unshown, the size of window will change.
1460 // then, the text area move left or right. thus client rect should be
1461 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001462 if (oldx != x || oldy != y)
1463 {
1464 InvalidateRect(s_hwnd, NULL, FALSE);
1465 oldx = x;
1466 oldy = y;
1467 }
1468}
1469
1470
1471/*
1472 * Scrollbar stuff:
1473 */
1474
1475 void
1476gui_mch_enable_scrollbar(
1477 scrollbar_T *sb,
1478 int flag)
1479{
1480 ShowScrollBar(sb->id, SB_CTL, flag);
1481
Bram Moolenaar734a8672019-12-02 22:49:38 +01001482 // TODO: When the window is maximized, the size of the window stays the
1483 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1484 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001485}
1486
1487 void
1488gui_mch_set_scrollbar_pos(
1489 scrollbar_T *sb,
1490 int x,
1491 int y,
1492 int w,
1493 int h)
1494{
1495 SetWindowPos(sb->id, NULL, x, y, w, h,
1496 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1497}
1498
Bram Moolenaar203ec772020-07-17 20:43:43 +02001499 int
1500gui_mch_get_scrollbar_xpadding(void)
1501{
1502 RECT rcTxt, rcWnd;
1503 int xpad;
1504
1505 GetWindowRect(s_textArea, &rcTxt);
1506 GetWindowRect(s_hwnd, &rcWnd);
1507 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001508 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1509 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001510 return (xpad < 0) ? 0 : xpad;
1511}
1512
1513 int
1514gui_mch_get_scrollbar_ypadding(void)
1515{
1516 RECT rcTxt, rcWnd;
1517 int ypad;
1518
1519 GetWindowRect(s_textArea, &rcTxt);
1520 GetWindowRect(s_hwnd, &rcWnd);
1521 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001522 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1523 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001524 return (ypad < 0) ? 0 : ypad;
1525}
1526
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001527 void
1528gui_mch_create_scrollbar(
1529 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001530 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001531{
1532 sb->id = CreateWindow(
1533 "SCROLLBAR", "Scrollbar",
1534 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001535 10, // Any value will do for now
1536 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001537 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001538 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001539}
1540
1541/*
1542 * Find the scrollbar with the given hwnd.
1543 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001544 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001545gui_mswin_find_scrollbar(HWND hwnd)
1546{
1547 win_T *wp;
1548
1549 if (gui.bottom_sbar.id == hwnd)
1550 return &gui.bottom_sbar;
1551 FOR_ALL_WINDOWS(wp)
1552 {
1553 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1554 return &wp->w_scrollbars[SBAR_LEFT];
1555 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1556 return &wp->w_scrollbars[SBAR_RIGHT];
1557 }
1558 return NULL;
1559}
1560
K.Takatac81e9bf2022-01-16 14:15:49 +00001561 static void
1562update_scrollbar_size(void)
1563{
1564 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1565 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1566}
1567
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568/*
K.Takataabe628e2022-01-23 16:25:17 +00001569 * Get the average character size of a font.
1570 */
1571 static void
1572GetAverageFontSize(HDC hdc, SIZE *size)
1573{
1574 // GetTextMetrics() may not return the right value in tmAveCharWidth
1575 // for some fonts. Do our own average computation.
1576 GetTextExtentPoint(hdc,
1577 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1578 52, size);
1579 size->cx = (size->cx / 26 + 1) / 2;
1580}
1581
1582/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001583 * Get the character size of a font.
1584 */
1585 static void
1586GetFontSize(GuiFont font)
1587{
1588 HWND hwnd = GetDesktopWindow();
1589 HDC hdc = GetWindowDC(hwnd);
1590 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001591 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592 TEXTMETRIC tm;
1593
1594 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001595 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596
K.Takataabe628e2022-01-23 16:25:17 +00001597 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001598 gui.char_height = tm.tmHeight + p_linespace;
1599
1600 SelectFont(hdc, hfntOld);
1601
1602 ReleaseDC(hwnd, hdc);
1603}
1604
1605/*
1606 * Adjust gui.char_height (after 'linespace' was changed).
1607 */
1608 int
1609gui_mch_adjust_charheight(void)
1610{
1611 GetFontSize(gui.norm_font);
1612 return OK;
1613}
1614
1615 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001616get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001617{
1618 HFONT font = NULL;
1619
Bram Moolenaar734a8672019-12-02 22:49:38 +01001620 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001621 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622
1623 if (font == NULL)
1624 return NOFONT;
1625
1626 return (GuiFont)font;
1627}
1628
1629 static int
1630pixels_to_points(int pixels, int vertical)
1631{
1632 int points;
1633 HWND hwnd;
1634 HDC hdc;
1635
1636 hwnd = GetDesktopWindow();
1637 hdc = GetWindowDC(hwnd);
1638
1639 points = MulDiv(pixels, 72,
1640 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1641
1642 ReleaseDC(hwnd, hdc);
1643
1644 return points;
1645}
1646
1647 GuiFont
1648gui_mch_get_font(
1649 char_u *name,
1650 int giveErrorIfMissing)
1651{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001652 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 GuiFont font = NOFONT;
1654
1655 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001656 {
1657 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001658 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001659 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001660 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001661 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001662 return font;
1663}
1664
1665#if defined(FEAT_EVAL) || defined(PROTO)
1666/*
1667 * Return the name of font "font" in allocated memory.
1668 * Don't know how to get the actual name, thus use the provided name.
1669 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001670 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001671gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001672{
1673 if (name == NULL)
1674 return NULL;
1675 return vim_strsave(name);
1676}
1677#endif
1678
1679 void
1680gui_mch_free_font(GuiFont font)
1681{
1682 if (font)
1683 DeleteObject((HFONT)font);
1684}
1685
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001686/*
1687 * Return the Pixel value (color) for the given color name.
1688 * Return INVALCOLOR for error.
1689 */
1690 guicolor_T
1691gui_mch_get_color(char_u *name)
1692{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001693 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001694
1695 typedef struct SysColorTable
1696 {
1697 char *name;
1698 int color;
1699 } SysColorTable;
1700
1701 static SysColorTable sys_table[] =
1702 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001703 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1704 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001705#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001706 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1707#endif
1708 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1709 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1710 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1711 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1712 {"SYS_DESKTOP", COLOR_DESKTOP},
1713 {"SYS_INFOBK", COLOR_INFOBK},
1714 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1715 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716 {"SYS_BTNFACE", COLOR_BTNFACE},
1717 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1718 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1719 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1720 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1721 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1722 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1723 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1724 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1725 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1726 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1727 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1728 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1729 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1730 {"SYS_MENU", COLOR_MENU},
1731 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1732 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1733 {"SYS_WINDOW", COLOR_WINDOW},
1734 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1735 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1736 };
1737
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001738 /*
1739 * Try to look up a system colour.
1740 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001741 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001742 if (STRICMP(name, sys_table[i].name) == 0)
1743 return GetSysColor(sys_table[i].color);
1744
Bram Moolenaarab302212016-04-26 20:59:29 +02001745 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001746}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001747
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001748 guicolor_T
1749gui_mch_get_rgb_color(int r, int g, int b)
1750{
1751 return gui_get_rgb_color_cmn(r, g, b);
1752}
1753
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001754/*
1755 * Return OK if the key with the termcap name "name" is supported.
1756 */
1757 int
1758gui_mch_haskey(char_u *name)
1759{
1760 int i;
1761
1762 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001763 if (name[0] == special_keys[i].vim_code0
1764 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001765 return OK;
1766 return FAIL;
1767}
1768
1769 void
1770gui_mch_beep(void)
1771{
LemonBoy77771d32022-04-13 11:47:25 +01001772 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001773}
1774/*
1775 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1776 */
1777 void
1778gui_mch_invert_rectangle(
1779 int r,
1780 int c,
1781 int nr,
1782 int nc)
1783{
1784 RECT rc;
1785
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001786#if defined(FEAT_DIRECTX)
1787 if (IS_ENABLE_DIRECTX())
1788 DWriteContext_Flush(s_dwc);
1789#endif
1790
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001791 /*
1792 * Note: InvertRect() excludes right and bottom of rectangle.
1793 */
1794 rc.left = FILL_X(c);
1795 rc.top = FILL_Y(r);
1796 rc.right = rc.left + nc * gui.char_width;
1797 rc.bottom = rc.top + nr * gui.char_height;
1798 InvertRect(s_hdc, &rc);
1799}
1800
1801/*
1802 * Iconify the GUI window.
1803 */
1804 void
1805gui_mch_iconify(void)
1806{
1807 ShowWindow(s_hwnd, SW_MINIMIZE);
1808}
1809
1810/*
1811 * Draw a cursor without focus.
1812 */
1813 void
1814gui_mch_draw_hollow_cursor(guicolor_T color)
1815{
1816 HBRUSH hbr;
1817 RECT rc;
1818
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001819#if defined(FEAT_DIRECTX)
1820 if (IS_ENABLE_DIRECTX())
1821 DWriteContext_Flush(s_dwc);
1822#endif
1823
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001824 /*
1825 * Note: FrameRect() excludes right and bottom of rectangle.
1826 */
1827 rc.left = FILL_X(gui.col);
1828 rc.top = FILL_Y(gui.row);
1829 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001830 if (mb_lefthalve(gui.row, gui.col))
1831 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001832 rc.bottom = rc.top + gui.char_height;
1833 hbr = CreateSolidBrush(color);
1834 FrameRect(s_hdc, &rc, hbr);
1835 DeleteBrush(hbr);
1836}
1837/*
1838 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1839 * color "color".
1840 */
1841 void
1842gui_mch_draw_part_cursor(
1843 int w,
1844 int h,
1845 guicolor_T color)
1846{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001847 RECT rc;
1848
1849 /*
1850 * Note: FillRect() excludes right and bottom of rectangle.
1851 */
1852 rc.left =
1853#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001854 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001855 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1856#endif
1857 FILL_X(gui.col);
1858 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1859 rc.right = rc.left + w;
1860 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001861
Bram Moolenaar92467d32017-12-05 13:22:16 +01001862 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001863}
1864
1865
1866/*
1867 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1868 * dead key's nominal character and re-post the original message.
1869 */
1870 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001871outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872{
1873 static MSG deadCharExpel;
1874
Anton Sharonov3f026672022-07-26 21:26:18 +01001875 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001876 return;
1877
Anton Sharonov3f026672022-07-26 21:26:18 +01001878 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001879
Bram Moolenaar734a8672019-12-02 22:49:38 +01001880 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001881 deadCharExpel.message = originalMsg.message;
1882 deadCharExpel.hwnd = originalMsg.hwnd;
1883 deadCharExpel.wParam = VK_SPACE;
1884
K.Takata4ac893f2022-01-20 12:44:28 +00001885 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001886
Bram Moolenaar734a8672019-12-02 22:49:38 +01001887 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1889 originalMsg.lParam);
1890}
1891
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001892/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001893 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1894 */
1895 static void
1896outputDeadKey_rePost(MSG originalMsg)
1897{
1898 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1899}
1900
1901/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001902 * Process a single Windows message.
1903 * If one is not available we hang until one is.
1904 */
1905 static void
1906process_message(void)
1907{
1908 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001909 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001910 char_u string[40];
1911 int i;
1912 int modifiers = 0;
1913 int key;
1914#ifdef FEAT_MENU
1915 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1916#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001917 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001918
K.Takatab7057bd2022-01-21 11:37:07 +00001919 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001920
1921#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001922 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001923 if (msg.message == WM_OLE)
1924 {
1925 char_u *str = (char_u *)msg.lParam;
1926 if (str == NULL || *str == NUL)
1927 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001928 // Message can't be ours, forward it. Fixes problem with Ultramon
1929 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001930 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001931 }
1932 else
1933 {
1934 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001935 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001936 }
1937 return;
1938 }
1939#endif
1940
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001941#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001942 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001943 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001944 {
1945 HandleMouseHide(msg.message, msg.lParam);
1946 return;
1947 }
1948#endif
1949
1950 /*
1951 * Check if it's a special key that we recognise. If not, call
1952 * TranslateMessage().
1953 */
1954 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1955 {
1956 vk = (int) msg.wParam;
1957
1958 /*
1959 * Handle dead keys in special conditions in other cases we let Windows
1960 * handle them and do not interfere.
1961 *
1962 * The dead_key flag must be reset on several occasions:
1963 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1964 * consumed at that point (This is when we let Windows combine the
1965 * dead character on its own)
1966 *
1967 * - Before doing something special such as regenerating keypresses to
1968 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001969 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001970 * immediately to _OnChar() (or _OnSysChar()).
1971 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001972
1973 /*
1974 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1975 * was handled by _WndProc, this keypress we want to process normally
1976 */
1977 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1978 dead_key = DEAD_KEY_OFF;
1979
1980 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001981 {
1982 /*
dundargocc57b5bc2022-11-02 13:30:51 +00001983 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01001984 *
1985 * After dead key was pressed with Ctrl in some cases, ESC was
1986 * artificially injected and handled by _OnChar(), now we are
1987 * dealing with completely new key press from the user. If we don't
1988 * do anything, ToUnicode() call will interpret this vk+scan_code
1989 * under influence of "dead-modifier". To prevent this we translate
1990 * this message replacing current char from user with VK_SPACE,
1991 * which will cause WM_CHAR with dead_key's character itself. Using
1992 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1993 * ignore this one WM_CHAR event completely. Afterwards (due to
1994 * usage of PostMessage), this procedure is scheduled to be called
1995 * again with user char and on next entry we will clean
1996 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
1997 * outputDeadKey_rePost() since we do not wish to reset dead_key
1998 * value.
1999 */
2000 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
2001 {
2002 outputDeadKey_rePost_Ex(msg,
2003 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2004 return;
2005 }
2006
2007 if (dead_key != DEAD_KEY_SET_DEFAULT)
2008 {
2009 // should never happen - is there a way to make ASSERT here?
2010 return;
2011 }
2012
2013 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002014 * If a dead key was pressed and the user presses VK_SPACE,
2015 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2016 * with the dead char now, so do nothing special and let Windows
2017 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002018 *
2019 * Note that VK_SPACE combines with the dead_key's character and
2020 * only one WM_CHAR will be generated by TranslateMessage(), in
2021 * the two other cases two WM_CHAR will be generated: the dead
2022 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2023 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002024 */
2025 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2026 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002027 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002028 TranslateMessage(&msg);
2029 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002031 // In modes where we are not typing, dead keys should behave
2032 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002033 else if ((get_real_state()
2034 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002035 {
2036 outputDeadKey_rePost(msg);
2037 return;
2038 }
2039 }
2040
Bram Moolenaar734a8672019-12-02 22:49:38 +01002041 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002042 if (vk == VK_CANCEL)
2043 {
2044 trash_input_buf();
2045 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002046 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002047 string[0] = Ctrl_C;
2048 add_to_input_buf(string, 1);
2049 }
2050
LemonBoy77fc0b02022-04-22 22:45:52 +01002051 // This is an IME event or a synthetic keystroke, let Windows handle it.
2052 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2053 {
2054 TranslateMessage(&msg);
2055 return;
2056 }
2057
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002058 for (i = 0; special_keys[i].key_sym != 0; i++)
2059 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002060 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002061 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002062 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002063 {
2064 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002065 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002066 * is a key that would normally trigger the dead key nominal
2067 * character output (such as a NUMPAD printable character or
2068 * the TAB key, etc...).
2069 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002070 if (dead_key == DEAD_KEY_SET_DEFAULT
2071 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002072 || vk == VK_TAB || vk == CAR))
2073 {
2074 outputDeadKey_rePost(msg);
2075 return;
2076 }
2077
2078#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002079 // Check for <F10>: Windows selects the menu. When <F10> is
2080 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081 if (vk == VK_F10
2082 && gui.menu_is_active
2083 && check_map(k10, State, FALSE, TRUE, FALSE,
2084 NULL, NULL) == NULL)
2085 break;
2086#endif
LemonBoy45684c62022-04-24 15:46:42 +01002087 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088
2089 if (special_keys[i].vim_code1 == NUL)
2090 key = special_keys[i].vim_code0;
2091 else
2092 key = TO_SPECIAL(special_keys[i].vim_code0,
2093 special_keys[i].vim_code1);
2094 key = simplify_key(key, &modifiers);
2095 if (key == CSI)
2096 key = K_CSI;
2097
2098 if (modifiers)
2099 {
2100 string[0] = CSI;
2101 string[1] = KS_MODIFIER;
2102 string[2] = modifiers;
2103 add_to_input_buf(string, 3);
2104 }
2105
2106 if (IS_SPECIAL(key))
2107 {
2108 string[0] = CSI;
2109 string[1] = K_SECOND(key);
2110 string[2] = K_THIRD(key);
2111 add_to_input_buf(string, 3);
2112 }
2113 else
2114 {
2115 int len;
2116
Bram Moolenaar734a8672019-12-02 22:49:38 +01002117 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002118 len = char_to_string(key, string, 40, FALSE);
2119 add_to_input_buf(string, len);
2120 }
2121 break;
2122 }
2123 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002124
2125 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002126 if (special_keys[i].key_sym == 0)
2127 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002128 WCHAR ch[8];
2129 int len;
2130 int i;
2131 UINT scan_code;
2132
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002133 // Construct the state table with only a few modifiers, we don't
2134 // really care about the presence of Ctrl/Alt as those modifiers are
2135 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002136 memset(keyboard_state, 0, 256);
2137 if (GetKeyState(VK_SHIFT) & 0x8000)
2138 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002139 if (GetKeyState(VK_CAPITAL) & 0x0001)
2140 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002141 // Alt-Gr is synthesized as Alt + Ctrl.
2142 if ((GetKeyState(VK_RMENU) & 0x8000)
2143 && (GetKeyState(VK_CONTROL) & 0x8000))
2144 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002145 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002146 keyboard_state[VK_CONTROL] = 0x80;
2147 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002148
2149 // Translate the virtual key according to the current keyboard
2150 // layout.
2151 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2152 // Convert the scan-code into a sequence of zero or more unicode
2153 // codepoints.
2154 // If this is a dead key ToUnicode returns a negative value.
2155 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2156 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002157 if (len < 0)
2158 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002159
2160 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002161 {
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002162 int wm_char = NUL;
2163
2164 if (dead_key == DEAD_KEY_SET_DEFAULT
2165 && (GetKeyState(VK_CONTROL) & 0x8000))
2166 {
2167 if ( // AZERTY CTRL+dead_circumflex
2168 (vk == 221 && scan_code == 26)
2169 // QWERTZ CTRL+dead_circumflex
2170 || (vk == 220 && scan_code == 41))
2171 wm_char = '[';
2172 if ( // QWERTZ CTRL+dead_two-overdots
2173 (vk == 192 && scan_code == 27))
2174 wm_char = ']';
2175 }
2176 if (wm_char != NUL)
Anton Sharonov3f026672022-07-26 21:26:18 +01002177 {
2178 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002179 // still hold as ESC
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002180 PostMessageW(msg.hwnd, WM_CHAR, wm_char, msg.lParam);
Anton Sharonov3f026672022-07-26 21:26:18 +01002181 // ask _OnChar() to not touch this state, wait for next key
2182 // press and maintain knowledge that we are "poisoned" with
2183 // "dead state"
2184 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2185 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002186 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002187 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002188
2189 // Post the message as TranslateMessage would do.
2190 if (msg.message == WM_KEYDOWN)
2191 {
2192 for (i = 0; i < len; i++)
2193 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002194 }
2195 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002196 {
2197 for (i = 0; i < len; i++)
2198 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2199 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002200 }
2201 }
2202#ifdef FEAT_MBYTE_IME
2203 else if (msg.message == WM_IME_NOTIFY)
2204 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2205 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002206 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002207 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002208#endif
2209
2210#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002211 // Check for <F10>: Default effect is to select the menu. When <F10> is
2212 // mapped we need to stop it here to avoid strange effects (e.g., for the
2213 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002214 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2215 NULL, NULL) == NULL)
2216#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002217 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002218}
2219
2220/*
2221 * Catch up with any queued events. This may put keyboard input into the
2222 * input buffer, call resize call-backs, trigger timers etc. If there is
2223 * nothing in the event queue (& no timers pending), then we return
2224 * immediately.
2225 */
2226 void
2227gui_mch_update(void)
2228{
2229 MSG msg;
2230
2231 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002232 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233 && !vim_is_input_buf_full())
2234 process_message();
2235}
2236
Bram Moolenaar4231da42016-06-02 14:30:04 +02002237 static void
2238remove_any_timer(void)
2239{
2240 MSG msg;
2241
2242 if (s_wait_timer != 0 && !s_timed_out)
2243 {
2244 KillTimer(NULL, s_wait_timer);
2245
Bram Moolenaar734a8672019-12-02 22:49:38 +01002246 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002247 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002248 ;
2249 s_wait_timer = 0;
2250 }
2251}
2252
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002253/*
2254 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2255 * from the keyboard.
2256 * wtime == -1 Wait forever.
2257 * wtime == 0 This should never happen.
2258 * wtime > 0 Wait wtime milliseconds for a character.
2259 * Returns OK if a character was found to be available within the given time,
2260 * or FAIL otherwise.
2261 */
2262 int
2263gui_mch_wait_for_chars(int wtime)
2264{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002265 int focus;
2266
2267 s_timed_out = FALSE;
2268
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002269 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002270 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002271 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002272 if (s_busy_processing)
2273 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002274
2275 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002276 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2277 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002278 }
2279
2280 allow_scrollbar = TRUE;
2281
2282 focus = gui.in_focus;
2283 while (!s_timed_out)
2284 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002285 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002286 if (gui.in_focus != focus)
2287 {
2288 if (gui.in_focus)
2289 gui_mch_start_blink();
2290 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002291 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292 focus = gui.in_focus;
2293 }
2294
2295 if (s_need_activate)
2296 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002297 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002298 s_need_activate = FALSE;
2299 }
2300
Bram Moolenaar4231da42016-06-02 14:30:04 +02002301#ifdef FEAT_TIMERS
2302 did_add_timer = FALSE;
2303#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002304#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002305 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002306 for (;;)
2307 {
2308 MSG msg;
2309
2310 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002311# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002312 if (did_add_timer)
2313 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002314# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002315 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002316 {
2317 process_message();
2318 break;
2319 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002320 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002321 // TODO: The 10 msec is a compromise between laggy response
2322 // and consuming more CPU time. Better would be to handle
2323 // channel messages when they arrive.
2324 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002325 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002326 break;
2327 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002328#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002329 // Don't use gui_mch_update() because then we will spin-lock until a
2330 // char arrives, instead we use GetMessage() to hang until an
2331 // event arrives. No need to check for input_buf_full because we are
2332 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002333 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002334#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335
2336 if (input_available())
2337 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002338 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 allow_scrollbar = FALSE;
2340
Bram Moolenaar89c00032019-09-04 13:53:21 +02002341 // Clear pending mouse button, the release event may have been
2342 // taken by the dialog window. But don't do this when getting
2343 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002344 if (!s_getting_focus)
2345 s_button_pending = -1;
2346
2347 return OK;
2348 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002349
2350#ifdef FEAT_TIMERS
2351 if (did_add_timer)
2352 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002353 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002354 remove_any_timer();
2355 break;
2356 }
2357#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 }
2359 allow_scrollbar = FALSE;
2360 return FAIL;
2361}
2362
2363/*
2364 * Clear a rectangular region of the screen from text pos (row1, col1) to
2365 * (row2, col2) inclusive.
2366 */
2367 void
2368gui_mch_clear_block(
2369 int row1,
2370 int col1,
2371 int row2,
2372 int col2)
2373{
2374 RECT rc;
2375
2376 /*
2377 * Clear one extra pixel at the far right, for when bold characters have
2378 * spilled over to the window border.
2379 * Note: FillRect() excludes right and bottom of rectangle.
2380 */
2381 rc.left = FILL_X(col1);
2382 rc.top = FILL_Y(row1);
2383 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2384 rc.bottom = FILL_Y(row2 + 1);
2385 clear_rect(&rc);
2386}
2387
2388/*
2389 * Clear the whole text window.
2390 */
2391 void
2392gui_mch_clear_all(void)
2393{
2394 RECT rc;
2395
2396 rc.left = 0;
2397 rc.top = 0;
2398 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2399 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2400 clear_rect(&rc);
2401}
2402/*
2403 * Menu stuff.
2404 */
2405
2406 void
2407gui_mch_enable_menu(int flag)
2408{
2409#ifdef FEAT_MENU
2410 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2411#endif
2412}
2413
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414 void
2415gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002416 int x UNUSED,
2417 int y UNUSED,
2418 int w UNUSED,
2419 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002420{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002421 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002422}
2423
2424#if defined(FEAT_MENU) || defined(PROTO)
2425/*
2426 * Make menu item hidden or not hidden
2427 */
2428 void
2429gui_mch_menu_hidden(
2430 vimmenu_T *menu,
2431 int hidden)
2432{
2433 /*
2434 * This doesn't do what we want. Hmm, just grey the menu items for now.
2435 */
2436 /*
2437 if (hidden)
2438 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2439 else
2440 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2441 */
2442 gui_mch_menu_grey(menu, hidden);
2443}
2444
2445/*
2446 * This is called after setting all the menus to grey/hidden or not.
2447 */
2448 void
2449gui_mch_draw_menubar(void)
2450{
2451 DrawMenuBar(s_hwnd);
2452}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002453#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002454
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455/*
2456 * Return the RGB value of a pixel as a long.
2457 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002458 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002459gui_mch_get_rgb(guicolor_T pixel)
2460{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002461 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2462 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002463}
2464
2465#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002466/*
2467 * Convert pixels in X to dialog units
2468 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469 static WORD
2470PixelToDialogX(int numPixels)
2471{
2472 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2473}
2474
Bram Moolenaar734a8672019-12-02 22:49:38 +01002475/*
2476 * Convert pixels in Y to dialog units
2477 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002478 static WORD
2479PixelToDialogY(int numPixels)
2480{
2481 return (WORD)((numPixels * 8) / s_dlgfntheight);
2482}
2483
Bram Moolenaar734a8672019-12-02 22:49:38 +01002484/*
2485 * Return the width in pixels of the given text in the given DC.
2486 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002487 static int
2488GetTextWidth(HDC hdc, char_u *str, int len)
2489{
2490 SIZE size;
2491
2492 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2493 return size.cx;
2494}
2495
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496/*
2497 * Return the width in pixels of the given text in the given DC, taking care
2498 * of 'encoding' to active codepage conversion.
2499 */
2500 static int
2501GetTextWidthEnc(HDC hdc, char_u *str, int len)
2502{
2503 SIZE size;
2504 WCHAR *wstr;
2505 int n;
2506 int wlen = len;
2507
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002508 wstr = enc_to_utf16(str, &wlen);
2509 if (wstr == NULL)
2510 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002511
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002512 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2513 vim_free(wstr);
2514 if (n)
2515 return size.cx;
2516 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002517}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002519static void get_work_area(RECT *spi_rect);
2520
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002521/*
2522 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002523 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2524 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002525 */
2526 static BOOL
2527CenterWindow(
2528 HWND hwndChild,
2529 HWND hwndParent)
2530{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002531 HMONITOR mon;
2532 MONITORINFO moninfo;
2533 RECT rChild, rParent, rScreen;
2534 int wChild, hChild, wParent, hParent;
2535 int xNew, yNew;
2536 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002537
2538 GetWindowRect(hwndChild, &rChild);
2539 wChild = rChild.right - rChild.left;
2540 hChild = rChild.bottom - rChild.top;
2541
Bram Moolenaar734a8672019-12-02 22:49:38 +01002542 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002543 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002544 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002545 else
2546 GetWindowRect(hwndParent, &rParent);
2547 wParent = rParent.right - rParent.left;
2548 hParent = rParent.bottom - rParent.top;
2549
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002550 moninfo.cbSize = sizeof(MONITORINFO);
2551 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2552 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002554 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002555 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002556 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002557 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002558 hdc = GetDC(hwndChild);
2559 rScreen.left = 0;
2560 rScreen.top = 0;
2561 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2562 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2563 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002564 }
2565
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002566 xNew = rParent.left + ((wParent - wChild) / 2);
2567 if (xNew < rScreen.left)
2568 xNew = rScreen.left;
2569 else if ((xNew + wChild) > rScreen.right)
2570 xNew = rScreen.right - wChild;
2571
2572 yNew = rParent.top + ((hParent - hChild) / 2);
2573 if (yNew < rScreen.top)
2574 yNew = rScreen.top;
2575 else if ((yNew + hChild) > rScreen.bottom)
2576 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002577
2578 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2579 SWP_NOSIZE | SWP_NOZORDER);
2580}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002581#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583#if defined(FEAT_TOOLBAR) || defined(PROTO)
2584 void
2585gui_mch_show_toolbar(int showit)
2586{
2587 if (s_toolbarhwnd == NULL)
2588 return;
2589
2590 if (showit)
2591 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 // Enable unicode support
2593 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2594 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002595 ShowWindow(s_toolbarhwnd, SW_SHOW);
2596 }
2597 else
2598 ShowWindow(s_toolbarhwnd, SW_HIDE);
2599}
2600
Bram Moolenaar734a8672019-12-02 22:49:38 +01002601// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002602# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002603
2604#endif
2605
2606#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2607 static void
2608add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2609{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002610 WCHAR *wn;
2611 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002612
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002613 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002615 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002616
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002617 infow.cbSize = sizeof(infow);
2618 infow.fMask = MIIM_TYPE | MIIM_ID;
2619 infow.wID = item_id;
2620 infow.fType = MFT_STRING;
2621 infow.dwTypeData = wn;
2622 infow.cch = (UINT)wcslen(wn);
2623 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2624 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625}
2626
2627 static void
2628show_tabline_popup_menu(void)
2629{
2630 HMENU tab_pmenu;
2631 long rval;
2632 POINT pt;
2633
Bram Moolenaar734a8672019-12-02 22:49:38 +01002634 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002635 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002636 return;
2637
2638 tab_pmenu = CreatePopupMenu();
2639 if (tab_pmenu == NULL)
2640 return;
2641
2642 if (first_tabpage->tp_next != NULL)
2643 add_tabline_popup_menu_entry(tab_pmenu,
2644 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2645 add_tabline_popup_menu_entry(tab_pmenu,
2646 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2647 add_tabline_popup_menu_entry(tab_pmenu,
2648 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2649
2650 GetCursorPos(&pt);
2651 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2652 NULL);
2653
2654 DestroyMenu(tab_pmenu);
2655
Bram Moolenaar734a8672019-12-02 22:49:38 +01002656 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002657 if (rval > 0)
2658 {
2659 TCHITTESTINFO htinfo;
2660 int idx;
2661
2662 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2663 return;
2664
2665 htinfo.pt.x = pt.x;
2666 htinfo.pt.y = pt.y;
2667 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2668 if (idx == -1)
2669 idx = 0;
2670 else
2671 idx += 1;
2672
2673 send_tabline_menu_event(idx, (int)rval);
2674 }
2675}
2676
2677/*
2678 * Show or hide the tabline.
2679 */
2680 void
2681gui_mch_show_tabline(int showit)
2682{
2683 if (s_tabhwnd == NULL)
2684 return;
2685
2686 if (!showit != !showing_tabline)
2687 {
2688 if (showit)
2689 ShowWindow(s_tabhwnd, SW_SHOW);
2690 else
2691 ShowWindow(s_tabhwnd, SW_HIDE);
2692 showing_tabline = showit;
2693 }
2694}
2695
2696/*
2697 * Return TRUE when tabline is displayed.
2698 */
2699 int
2700gui_mch_showing_tabline(void)
2701{
2702 return s_tabhwnd != NULL && showing_tabline;
2703}
2704
2705/*
2706 * Update the labels of the tabline.
2707 */
2708 void
2709gui_mch_update_tabline(void)
2710{
2711 tabpage_T *tp;
2712 TCITEM tie;
2713 int nr = 0;
2714 int curtabidx = 0;
2715 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002717
2718 if (s_tabhwnd == NULL)
2719 return;
2720
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002721 // Enable unicode support
2722 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723
2724 tie.mask = TCIF_TEXT;
2725 tie.iImage = -1;
2726
Bram Moolenaar734a8672019-12-02 22:49:38 +01002727 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002728 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2729
Bram Moolenaar734a8672019-12-02 22:49:38 +01002730 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002731 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2732 {
2733 if (tp == curtab)
2734 curtabidx = nr;
2735
2736 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2737 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002738 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739 tie.pszText = "-Empty-";
2740 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2741 tabadded = 1;
2742 }
2743
2744 get_tabline_label(tp, FALSE);
2745 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002746
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002747 wstr = enc_to_utf16(NameBuff, NULL);
2748 if (wstr != NULL)
2749 {
2750 TCITEMW tiw;
2751
2752 tiw.mask = TCIF_TEXT;
2753 tiw.iImage = -1;
2754 tiw.pszText = wstr;
2755 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2756 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002757 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758 }
2759
Bram Moolenaar734a8672019-12-02 22:49:38 +01002760 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2762 TabCtrl_DeleteItem(s_tabhwnd, nr);
2763
2764 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2765 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2766
Bram Moolenaar734a8672019-12-02 22:49:38 +01002767 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002768 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2769 RedrawWindow(s_tabhwnd, NULL, NULL,
2770 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2771
2772 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2773 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2774}
2775
2776/*
2777 * Set the current tab to "nr". First tab is 1.
2778 */
2779 void
2780gui_mch_set_curtab(int nr)
2781{
2782 if (s_tabhwnd == NULL)
2783 return;
2784
2785 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2786 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2787}
2788
2789#endif
2790
2791/*
2792 * ":simalt" command.
2793 */
2794 void
2795ex_simalt(exarg_T *eap)
2796{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002797 char_u *keys = eap->arg;
2798 int fill_typebuf = FALSE;
2799 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002800
2801 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2802 while (*keys)
2803 {
2804 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002805 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002806 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2807 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002808 fill_typebuf = TRUE;
2809 }
2810 if (fill_typebuf)
2811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002812 // Put a NOP in the typeahead buffer so that the message will get
2813 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002814 key_name[0] = K_SPECIAL;
2815 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002816 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002817 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002818#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002819 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002820#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002821 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002822 }
2823}
2824
2825/*
2826 * Create the find & replace dialogs.
2827 * You can't have both at once: ":find" when replace is showing, destroys
2828 * the replace dialog first, and the other way around.
2829 */
2830#ifdef MSWIN_FIND_REPLACE
2831 static void
2832initialise_findrep(char_u *initial_string)
2833{
2834 int wword = FALSE;
2835 int mcase = !p_ic;
2836 char_u *entry_text;
2837
Bram Moolenaar734a8672019-12-02 22:49:38 +01002838 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002839 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2840
2841 s_findrep_struct.hwndOwner = s_hwnd;
2842 s_findrep_struct.Flags = FR_DOWN;
2843 if (mcase)
2844 s_findrep_struct.Flags |= FR_MATCHCASE;
2845 if (wword)
2846 s_findrep_struct.Flags |= FR_WHOLEWORD;
2847 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002848 {
2849 WCHAR *p = enc_to_utf16(entry_text, NULL);
2850 if (p != NULL)
2851 {
2852 int len = s_findrep_struct.wFindWhatLen - 1;
2853
2854 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2855 s_findrep_struct.lpstrFindWhat[len] = NUL;
2856 vim_free(p);
2857 }
2858 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859 vim_free(entry_text);
2860}
2861#endif
2862
2863 static void
2864set_window_title(HWND hwnd, char *title)
2865{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002866 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867 {
2868 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002869
Bram Moolenaar734a8672019-12-02 22:49:38 +01002870 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2872 if (wbuf != NULL)
2873 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002874 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002876 }
2877 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002878 else
2879 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880}
2881
2882 void
2883gui_mch_find_dialog(exarg_T *eap)
2884{
2885#ifdef MSWIN_FIND_REPLACE
2886 if (s_findrep_msg != 0)
2887 {
2888 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2889 DestroyWindow(s_findrep_hwnd);
2890
2891 if (!IsWindow(s_findrep_hwnd))
2892 {
2893 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002894 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002895 }
2896
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002897 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002898 (void)SetFocus(s_findrep_hwnd);
2899
2900 s_findrep_is_find = TRUE;
2901 }
2902#endif
2903}
2904
2905
2906 void
2907gui_mch_replace_dialog(exarg_T *eap)
2908{
2909#ifdef MSWIN_FIND_REPLACE
2910 if (s_findrep_msg != 0)
2911 {
2912 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2913 DestroyWindow(s_findrep_hwnd);
2914
2915 if (!IsWindow(s_findrep_hwnd))
2916 {
2917 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002918 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919 }
2920
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002921 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002922 (void)SetFocus(s_findrep_hwnd);
2923
2924 s_findrep_is_find = FALSE;
2925 }
2926#endif
2927}
2928
2929
2930/*
2931 * Set visibility of the pointer.
2932 */
2933 void
2934gui_mch_mousehide(int hide)
2935{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002936 if (hide == gui.pointer_hidden)
2937 return;
2938
2939 ShowCursor(!hide);
2940 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941}
2942
2943#ifdef FEAT_MENU
2944 static void
2945gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2946{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002947 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002948 gui_mch_mousehide(FALSE);
2949
2950 (void)TrackPopupMenu(
2951 (HMENU)menu->submenu_id,
2952 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2953 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002954 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002955 s_hwnd,
2956 NULL);
2957 /*
2958 * NOTE: The pop-up menu can eat the mouse up event.
2959 * We deal with this in normal.c.
2960 */
2961}
2962#endif
2963
2964/*
2965 * Got a message when the system will go down.
2966 */
2967 static void
2968_OnEndSession(void)
2969{
2970 getout_preserve_modified(1);
2971}
2972
2973/*
2974 * Get this message when the user clicks on the cross in the top right corner
2975 * of a Windows95 window.
2976 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002977 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002978_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002979{
2980 gui_shell_closed();
2981}
2982
2983/*
2984 * Get a message when the window is being destroyed.
2985 */
2986 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002987_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002988{
2989 if (!destroying)
2990 _OnClose(hwnd);
2991}
2992
2993 static void
2994_OnPaint(
2995 HWND hwnd)
2996{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002997 if (IsMinimized(hwnd))
2998 return;
2999
3000 PAINTSTRUCT ps;
3001
3002 out_flush(); // make sure all output has been processed
3003 (void)BeginPaint(hwnd, &ps);
3004
3005 // prevent multi-byte characters from misprinting on an invalid
3006 // rectangle
3007 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003008 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003009 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003010
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003011 GetClientRect(hwnd, &rect);
3012 ps.rcPaint.left = rect.left;
3013 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003014 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003015
3016 if (!IsRectEmpty(&ps.rcPaint))
3017 {
3018 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3019 ps.rcPaint.right - ps.rcPaint.left + 1,
3020 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3021 }
3022
3023 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003024}
3025
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003026 static void
3027_OnSize(
3028 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003029 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003030 int cx,
3031 int cy)
3032{
K.Takatac81e9bf2022-01-16 14:15:49 +00003033 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003034 {
3035 gui_resize_shell(cx, cy);
3036
Bram Moolenaar734a8672019-12-02 22:49:38 +01003037 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003038 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003039 }
3040}
3041
3042 static void
3043_OnSetFocus(
3044 HWND hwnd,
3045 HWND hwndOldFocus)
3046{
3047 gui_focus_change(TRUE);
3048 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003049 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003050}
3051
3052 static void
3053_OnKillFocus(
3054 HWND hwnd,
3055 HWND hwndNewFocus)
3056{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003057 if (destroying)
3058 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059 gui_focus_change(FALSE);
3060 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003061 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003062}
3063
3064/*
3065 * Get a message when the user switches back to vim
3066 */
3067 static LRESULT
3068_OnActivateApp(
3069 HWND hwnd,
3070 BOOL fActivate,
3071 DWORD dwThreadId)
3072{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003073 // we call gui_focus_change() in _OnSetFocus()
3074 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003075 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076}
3077
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003078 void
3079gui_mch_destroy_scrollbar(scrollbar_T *sb)
3080{
3081 DestroyWindow(sb->id);
3082}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003083
3084/*
3085 * Get current mouse coordinates in text window.
3086 */
3087 void
3088gui_mch_getmouse(int *x, int *y)
3089{
3090 RECT rct;
3091 POINT mp;
3092
3093 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003094 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003095 *x = (int)(mp.x - rct.left);
3096 *y = (int)(mp.y - rct.top);
3097}
3098
3099/*
3100 * Move mouse pointer to character at (x, y).
3101 */
3102 void
3103gui_mch_setmouse(int x, int y)
3104{
3105 RECT rct;
3106
3107 (void)GetWindowRect(s_textArea, &rct);
3108 (void)SetCursorPos(x + gui.border_offset + rct.left,
3109 y + gui.border_offset + rct.top);
3110}
3111
3112 static void
3113gui_mswin_get_valid_dimensions(
3114 int w,
3115 int h,
3116 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003117 int *valid_h,
3118 int *cols,
3119 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003120{
3121 int base_width, base_height;
3122
3123 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003124 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3125 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003127 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3128 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3129 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3130 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003131 *cols = (w - base_width) / gui.char_width;
3132 *rows = (h - base_height) / gui.char_height;
3133 *valid_w = base_width + *cols * gui.char_width;
3134 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003135}
3136
3137 void
3138gui_mch_flash(int msec)
3139{
3140 RECT rc;
3141
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003142#if defined(FEAT_DIRECTX)
3143 if (IS_ENABLE_DIRECTX())
3144 DWriteContext_Flush(s_dwc);
3145#endif
3146
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003147 /*
3148 * Note: InvertRect() excludes right and bottom of rectangle.
3149 */
3150 rc.left = 0;
3151 rc.top = 0;
3152 rc.right = gui.num_cols * gui.char_width;
3153 rc.bottom = gui.num_rows * gui.char_height;
3154 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003155 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156
Bram Moolenaar734a8672019-12-02 22:49:38 +01003157 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003158
3159 InvertRect(s_hdc, &rc);
3160}
3161
3162/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003163 * Check if the specified point is on-screen. (multi-monitor aware)
3164 */
3165 static BOOL
3166is_point_onscreen(int x, int y)
3167{
3168 POINT pt = {x, y};
3169
3170 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3171}
3172
3173/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003174 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003175 *
3176 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3177 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3178 * only works when the whole window is on-screen.
3179 */
3180 static BOOL
3181is_window_onscreen(HWND hwnd)
3182{
3183 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003184 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003185
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003186 GetClientRect(hwnd, &rc);
3187 p1.x = rc.left;
3188 p1.y = rc.top;
3189 p2.x = rc.right - 1;
3190 p2.y = rc.bottom - 1;
3191 ClientToScreen(hwnd, &p1);
3192 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003193
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003194 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003195 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003196 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003197 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003198 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003199 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003200 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003201 return FALSE;
3202 return TRUE;
3203}
3204
3205/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003206 * Return flags used for scrolling.
3207 * The SW_INVALIDATE is required when part of the window is covered or
3208 * off-screen. Refer to MS KB Q75236.
3209 */
3210 static int
3211get_scroll_flags(void)
3212{
3213 HWND hwnd;
3214 RECT rcVim, rcOther, rcDest;
3215
Bram Moolenaar185577e2020-10-29 20:08:21 +01003216 // Check if the window is (partly) off-screen.
3217 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218 return SW_INVALIDATE;
3219
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003220 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003221 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003222 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3223 if (IsWindowVisible(hwnd))
3224 {
3225 GetWindowRect(hwnd, &rcOther);
3226 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3227 return SW_INVALIDATE;
3228 }
3229 return 0;
3230}
3231
3232/*
3233 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3234 * may not be scrolled out properly.
3235 * For gVim, when _OnScroll() is repeated, the character at the
3236 * previous cursor position may be left drawn after scroll.
3237 * The problem can be avoided by calling GetPixel() to get a pixel in
3238 * the region before ScrollWindowEx().
3239 */
3240 static void
3241intel_gpu_workaround(void)
3242{
3243 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3244}
3245
3246/*
3247 * Delete the given number of lines from the given row, scrolling up any
3248 * text further down within the scroll region.
3249 */
3250 void
3251gui_mch_delete_lines(
3252 int row,
3253 int num_lines)
3254{
3255 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003256
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257 rc.left = FILL_X(gui.scroll_region_left);
3258 rc.right = FILL_X(gui.scroll_region_right + 1);
3259 rc.top = FILL_Y(row);
3260 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3261
Bram Moolenaar92467d32017-12-05 13:22:16 +01003262#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003263 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003264 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003265 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003266 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003267 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003268#endif
3269 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003270#if defined(FEAT_DIRECTX)
3271 if (IS_ENABLE_DIRECTX())
3272 DWriteContext_Flush(s_dwc);
3273#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003274 intel_gpu_workaround();
3275 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003276 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003277 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003278 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003279
Bram Moolenaar734a8672019-12-02 22:49:38 +01003280 // This seems to be required to avoid the cursor disappearing when
3281 // scrolling such that the cursor ends up in the top-left character on
3282 // the screen... But why? (Webb)
3283 // It's probably fixed by disabling drawing the cursor while scrolling.
3284 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003285
3286 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3287 gui.scroll_region_left,
3288 gui.scroll_region_bot, gui.scroll_region_right);
3289}
3290
3291/*
3292 * Insert the given number of lines before the given row, scrolling down any
3293 * following text within the scroll region.
3294 */
3295 void
3296gui_mch_insert_lines(
3297 int row,
3298 int num_lines)
3299{
3300 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003301
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003302 rc.left = FILL_X(gui.scroll_region_left);
3303 rc.right = FILL_X(gui.scroll_region_right + 1);
3304 rc.top = FILL_Y(row);
3305 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003306
3307#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003308 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003309 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003310 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003311 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003312 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003313#endif
3314 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003315#if defined(FEAT_DIRECTX)
3316 if (IS_ENABLE_DIRECTX())
3317 DWriteContext_Flush(s_dwc);
3318#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003319 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003320 // The SW_INVALIDATE is required when part of the window is covered or
3321 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003322 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003323 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003324 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003325 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327 gui_clear_block(row, gui.scroll_region_left,
3328 row + num_lines - 1, gui.scroll_region_right);
3329}
3330
3331
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003333gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334{
3335#if defined(FEAT_DIRECTX)
3336 DWriteContext_Close(s_dwc);
3337 DWrite_Final();
3338 s_dwc = NULL;
3339#endif
3340
3341 ReleaseDC(s_textArea, s_hdc);
3342 DeleteObject(s_brush);
3343
3344#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003345 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3347#endif
3348
Bram Moolenaar734a8672019-12-02 22:49:38 +01003349 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350 if (s_hwnd != NULL)
3351 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003352 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 DestroyWindow(s_hwnd);
3354 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355}
3356
3357 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003358logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359{
3360 char *p;
3361 char *res;
3362 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003363 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003364 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003365 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003366
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003367 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3368 if (font_name == NULL)
3369 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003370 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003371 quality_name = quality_id2name((int)lf.lfQuality);
3372
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003373 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003374 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003375 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376 if (res != NULL)
3377 {
3378 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003379 // make a normal font string out of the lf thing:
3380 points = pixels_to_points(
3381 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3382 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3383 sprintf((char *)p, "%s:h%d", font_name, points);
3384 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003385 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003386 while (*p)
3387 {
3388 if (*p == ' ')
3389 *p = '_';
3390 ++p;
3391 }
3392 if (lf.lfItalic)
3393 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003394 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 STRCAT(p, ":b");
3396 if (lf.lfUnderline)
3397 STRCAT(p, ":u");
3398 if (lf.lfStrikeOut)
3399 STRCAT(p, ":s");
3400 if (charset_name != NULL)
3401 {
3402 STRCAT(p, ":c");
3403 STRCAT(p, charset_name);
3404 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003405 if (quality_name != NULL)
3406 {
3407 STRCAT(p, ":q");
3408 STRCAT(p, quality_name);
3409 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003410 }
3411
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003412 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413 return (char_u *)res;
3414}
3415
3416
3417#ifdef FEAT_MBYTE_IME
3418/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003419 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003420 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003421 */
3422 static void
3423update_im_font(void)
3424{
K.Takatac81e9bf2022-01-16 14:15:49 +00003425 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426
3427 if (p_guifontwide != NULL && *p_guifontwide != NUL
3428 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003429 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430 norm_logfont = lf_wide;
3431 else
3432 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003433
3434 lf = norm_logfont;
3435 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3436 // Work around when PerMonitorV2 is not enabled in the process level.
3437 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3438 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003439}
3440#endif
3441
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003442/*
3443 * Handler of gui.wide_font (p_guifontwide) changed notification.
3444 */
3445 void
3446gui_mch_wide_font_changed(void)
3447{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003448 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003449
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003450#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003452#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003453
3454 gui_mch_free_font(gui.wide_ital_font);
3455 gui.wide_ital_font = NOFONT;
3456 gui_mch_free_font(gui.wide_bold_font);
3457 gui.wide_bold_font = NOFONT;
3458 gui_mch_free_font(gui.wide_boldital_font);
3459 gui.wide_boldital_font = NOFONT;
3460
3461 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003462 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463 {
3464 if (!lf.lfItalic)
3465 {
3466 lf.lfItalic = TRUE;
3467 gui.wide_ital_font = get_font_handle(&lf);
3468 lf.lfItalic = FALSE;
3469 }
3470 if (lf.lfWeight < FW_BOLD)
3471 {
3472 lf.lfWeight = FW_BOLD;
3473 gui.wide_bold_font = get_font_handle(&lf);
3474 if (!lf.lfItalic)
3475 {
3476 lf.lfItalic = TRUE;
3477 gui.wide_boldital_font = get_font_handle(&lf);
3478 }
3479 }
3480 }
3481}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482
3483/*
3484 * Initialise vim to use the font with the given name.
3485 * Return FAIL if the font could not be loaded, OK otherwise.
3486 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003487 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003488gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003489{
K.Takatac81e9bf2022-01-16 14:15:49 +00003490 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003491 GuiFont font = NOFONT;
3492 char_u *p;
3493
Bram Moolenaar734a8672019-12-02 22:49:38 +01003494 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003495 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003496 {
3497 lfOrig = lf;
3498 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003499 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003500 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 if (font == NOFONT)
3502 return FAIL;
3503
3504 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003505 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003506#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 norm_logfont = lf;
3508 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003509 if (!s_in_dpichanged)
3510 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511#endif
3512 gui_mch_free_font(gui.norm_font);
3513 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003514 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515 GetFontSize(font);
3516
K.Takatac81e9bf2022-01-16 14:15:49 +00003517 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 if (p != NULL)
3519 {
3520 hl_set_font_name(p);
3521
Bram Moolenaar734a8672019-12-02 22:49:38 +01003522 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3524 {
3525 vim_free(p_guifont);
3526 p_guifont = p;
3527 }
3528 else
3529 vim_free(p);
3530 }
3531
3532 gui_mch_free_font(gui.ital_font);
3533 gui.ital_font = NOFONT;
3534 gui_mch_free_font(gui.bold_font);
3535 gui.bold_font = NOFONT;
3536 gui_mch_free_font(gui.boldital_font);
3537 gui.boldital_font = NOFONT;
3538
3539 if (!lf.lfItalic)
3540 {
3541 lf.lfItalic = TRUE;
3542 gui.ital_font = get_font_handle(&lf);
3543 lf.lfItalic = FALSE;
3544 }
3545 if (lf.lfWeight < FW_BOLD)
3546 {
3547 lf.lfWeight = FW_BOLD;
3548 gui.bold_font = get_font_handle(&lf);
3549 if (!lf.lfItalic)
3550 {
3551 lf.lfItalic = TRUE;
3552 gui.boldital_font = get_font_handle(&lf);
3553 }
3554 }
3555
3556 return OK;
3557}
3558
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003559/*
3560 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003561 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003562 */
3563 int
3564gui_mch_maximized(void)
3565{
3566 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003567 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003568
3569 wp.length = sizeof(WINDOWPLACEMENT);
3570 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003571 {
3572 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003574 && wp.flags == WPF_RESTORETOMAXIMIZED))
3575 return TRUE;
3576 if (wp.showCmd == SW_SHOWMINIMIZED)
3577 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003579 // Assume the window is snapped when the sizes from two APIs differ.
3580 GetWindowRect(s_hwnd, &rc);
3581 if ((rc.right - rc.left !=
3582 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3583 || (rc.bottom - rc.top !=
3584 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3585 return TRUE;
3586 }
3587 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588}
3589
3590/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003591 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3592 * is set. Compute the new Rows and Columns. This is like resizing the
3593 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594 */
3595 void
3596gui_mch_newfont(void)
3597{
3598 RECT rect;
3599
3600 GetWindowRect(s_hwnd, &rect);
3601 if (win_socket_id == 0)
3602 {
3603 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003604 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3605 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003606 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003607 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3608 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3609 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3610 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611 }
3612 else
3613 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003614 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003616 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617 }
3618}
3619
3620/*
3621 * Set the window title
3622 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623 void
3624gui_mch_settitle(
3625 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003626 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627{
3628 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3629}
3630
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003631#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003632// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3633// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003634static LPCSTR mshape_idcs[] =
3635{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003636 IDC_ARROW, // arrow
3637 MAKEINTRESOURCE(0), // blank
3638 IDC_IBEAM, // beam
3639 IDC_SIZENS, // updown
3640 IDC_SIZENS, // udsizing
3641 IDC_SIZEWE, // leftright
3642 IDC_SIZEWE, // lrsizing
3643 IDC_WAIT, // busy
3644 IDC_NO, // no
3645 IDC_ARROW, // crosshair
3646 IDC_ARROW, // hand1
3647 IDC_ARROW, // hand2
3648 IDC_ARROW, // pencil
3649 IDC_ARROW, // question
3650 IDC_ARROW, // right-arrow
3651 IDC_UPARROW, // up-arrow
3652 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653};
3654
3655 void
3656mch_set_mouse_shape(int shape)
3657{
3658 LPCSTR idc;
3659
3660 if (shape == MSHAPE_HIDE)
3661 ShowCursor(FALSE);
3662 else
3663 {
3664 if (shape >= MSHAPE_NUMBERED)
3665 idc = IDC_ARROW;
3666 else
3667 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003668 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003669 if (!p_mh)
3670 {
3671 POINT mp;
3672
Bram Moolenaar734a8672019-12-02 22:49:38 +01003673 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003674 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675 (void)SetCursorPos(mp.x, mp.y);
3676 ShowCursor(TRUE);
3677 }
3678 }
3679}
3680#endif
3681
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003682#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003684 * Wide version of convert_filter().
3685 */
3686 static WCHAR *
3687convert_filterW(char_u *s)
3688{
3689 char_u *tmp;
3690 int len;
3691 WCHAR *res;
3692
3693 tmp = convert_filter(s);
3694 if (tmp == NULL)
3695 return NULL;
3696 len = (int)STRLEN(s) + 3;
3697 res = enc_to_utf16(tmp, &len);
3698 vim_free(tmp);
3699 return res;
3700}
3701
3702/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003703 * Pop open a file browser and return the file selected, in allocated memory,
3704 * or NULL if Cancel is hit.
3705 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3706 * title - Title message for the file browser dialog.
3707 * dflt - Default name of file.
3708 * ext - Default extension to be added to files without extensions.
3709 * initdir - directory in which to open the browser (NULL = current dir)
3710 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003711 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003712 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003713gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714 int saving,
3715 char_u *title,
3716 char_u *dflt,
3717 char_u *ext,
3718 char_u *initdir,
3719 char_u *filter)
3720{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003721 // We always use the wide function. This means enc_to_utf16() must work,
3722 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723 OPENFILENAMEW fileStruct;
3724 WCHAR fileBuf[MAXPATHL];
3725 WCHAR *wp;
3726 int i;
3727 WCHAR *titlep = NULL;
3728 WCHAR *extp = NULL;
3729 WCHAR *initdirp = NULL;
3730 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003731 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003732 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733
3734 if (dflt == NULL)
3735 fileBuf[0] = NUL;
3736 else
3737 {
3738 wp = enc_to_utf16(dflt, NULL);
3739 if (wp == NULL)
3740 fileBuf[0] = NUL;
3741 else
3742 {
3743 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3744 fileBuf[i] = wp[i];
3745 fileBuf[i] = NUL;
3746 vim_free(wp);
3747 }
3748 }
3749
Bram Moolenaar734a8672019-12-02 22:49:38 +01003750 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003751 filterp = convert_filterW(filter);
3752
Bram Moolenaara80faa82020-04-12 19:37:17 +02003753 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003754# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003755 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003756 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003757# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003759# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760
3761 if (title != NULL)
3762 titlep = enc_to_utf16(title, NULL);
3763 fileStruct.lpstrTitle = titlep;
3764
3765 if (ext != NULL)
3766 extp = enc_to_utf16(ext, NULL);
3767 fileStruct.lpstrDefExt = extp;
3768
3769 fileStruct.lpstrFile = fileBuf;
3770 fileStruct.nMaxFile = MAXPATHL;
3771 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003772 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3773 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003774 if (initdir != NULL && *initdir != NUL)
3775 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003776 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777 initdirp = enc_to_utf16(initdir, NULL);
3778 if (initdirp != NULL)
3779 {
3780 for (wp = initdirp; *wp != NUL; ++wp)
3781 if (*wp == '/')
3782 *wp = '\\';
3783 }
3784 fileStruct.lpstrInitialDir = initdirp;
3785 }
3786
3787 /*
3788 * TODO: Allow selection of multiple files. Needs another arg to this
3789 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3790 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3791 * files that don't exist yet, so I haven't put it in. What about
3792 * OFN_PATHMUSTEXIST?
3793 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3794 */
3795 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003796# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797 if (curbuf->b_p_bin)
3798 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003799# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003800 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003801 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003803 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003804
3805 vim_free(filterp);
3806 vim_free(initdirp);
3807 vim_free(titlep);
3808 vim_free(extp);
3809
K.Takata14b8d6a2022-01-20 15:05:22 +00003810 if (!ret)
3811 return NULL;
3812
3813 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003815 if (p == NULL)
3816 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817
Bram Moolenaar734a8672019-12-02 22:49:38 +01003818 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 SetFocus(s_hwnd);
3820
Bram Moolenaar734a8672019-12-02 22:49:38 +01003821 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003822 q = vim_strsave(shorten_fname1(p));
3823 vim_free(p);
3824 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003826
3827
3828/*
3829 * Convert the string s to the proper format for a filter string by replacing
3830 * the \t and \n delimiters with \0.
3831 * Returns the converted string in allocated memory.
3832 *
3833 * Keep in sync with convert_filterW() above!
3834 */
3835 static char_u *
3836convert_filter(char_u *s)
3837{
3838 char_u *res;
3839 unsigned s_len = (unsigned)STRLEN(s);
3840 unsigned i;
3841
3842 res = alloc(s_len + 3);
3843 if (res != NULL)
3844 {
3845 for (i = 0; i < s_len; ++i)
3846 if (s[i] == '\t' || s[i] == '\n')
3847 res[i] = '\0';
3848 else
3849 res[i] = s[i];
3850 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003851 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852 res[s_len + 1] = NUL;
3853 res[s_len + 2] = NUL;
3854 }
3855 return res;
3856}
3857
3858/*
3859 * Select a directory.
3860 */
3861 char_u *
3862gui_mch_browsedir(char_u *title, char_u *initdir)
3863{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003864 // We fake this: Use a filter that doesn't select anything and a default
3865 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003866 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3867 initdir, (char_u *)_("Directory\t*.nothing\n"));
3868}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003869#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003871 static void
3872_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003873 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003874 HDROP hDrop)
3875{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003876#define BUFPATHLEN _MAX_PATH
3877#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003879 char szFile[BUFPATHLEN];
3880 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3881 UINT i;
3882 char_u **fnames;
3883 POINT pt;
3884 int_u modifiers = 0;
3885
Bram Moolenaar734a8672019-12-02 22:49:38 +01003886 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003887 DragQueryPoint(hDrop, &pt);
3888 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3889
3890 reset_VIsual();
3891
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003892 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893
3894 if (fnames != NULL)
3895 for (i = 0; i < cFiles; ++i)
3896 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003897 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3898 fnames[i] = utf16_to_enc(wszFile, NULL);
3899 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003900 {
3901 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3902 fnames[i] = vim_strsave((char_u *)szFile);
3903 }
3904 }
3905
3906 DragFinish(hDrop);
3907
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003908 if (fnames == NULL)
3909 return;
LemonBoy45684c62022-04-24 15:46:42 +01003910
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003911 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003912
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003913 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
3914 modifiers |= MOUSE_SHIFT;
3915 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
3916 modifiers |= MOUSE_CTRL;
3917 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
3918 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003920 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3921
3922 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003923}
3924
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003925 static int
3926_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003927 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 HWND hwndCtl,
3929 UINT code,
3930 int pos)
3931{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003932 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 scrollbar_T *sb, *sb_info;
3934 long val;
3935 int dragging = FALSE;
3936 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003937 SCROLLINFO si;
3938
3939 si.cbSize = sizeof(si);
3940 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003941
3942 sb = gui_mswin_find_scrollbar(hwndCtl);
3943 if (sb == NULL)
3944 return 0;
3945
Bram Moolenaar734a8672019-12-02 22:49:38 +01003946 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003947 {
3948 /*
3949 * Careful: need to get scrollbar info out of first (left) scrollbar
3950 * for window, but keep real scrollbar too because we must pass it to
3951 * gui_drag_scrollbar().
3952 */
3953 sb_info = &sb->wp->w_scrollbars[0];
3954 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003955 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003956 sb_info = sb;
3957 val = sb_info->value;
3958
3959 switch (code)
3960 {
3961 case SB_THUMBTRACK:
3962 val = pos;
3963 dragging = TRUE;
3964 if (sb->scroll_shift > 0)
3965 val <<= sb->scroll_shift;
3966 break;
3967 case SB_LINEDOWN:
3968 val++;
3969 break;
3970 case SB_LINEUP:
3971 val--;
3972 break;
3973 case SB_PAGEDOWN:
3974 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3975 break;
3976 case SB_PAGEUP:
3977 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3978 break;
3979 case SB_TOP:
3980 val = 0;
3981 break;
3982 case SB_BOTTOM:
3983 val = sb_info->max;
3984 break;
3985 case SB_ENDSCROLL:
3986 if (prev_code == SB_THUMBTRACK)
3987 {
3988 /*
3989 * "pos" only gives us 16-bit data. In case of large file,
3990 * use GetScrollPos() which returns 32-bit. Unfortunately it
3991 * is not valid while the scrollbar is being dragged.
3992 */
3993 val = GetScrollPos(hwndCtl, SB_CTL);
3994 if (sb->scroll_shift > 0)
3995 val <<= sb->scroll_shift;
3996 }
3997 break;
3998
3999 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004000 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004001 return 0;
4002 }
4003 prev_code = code;
4004
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004005 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4006 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007
4008 /*
4009 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4010 */
4011 if (sb->wp != NULL)
4012 {
4013 scrollbar_T *sba = sb->wp->w_scrollbars;
4014 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4015
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004016 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004017 }
4018
Bram Moolenaar734a8672019-12-02 22:49:38 +01004019 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004020 s_busy_processing = TRUE;
4021
Bram Moolenaar734a8672019-12-02 22:49:38 +01004022 // When "allow_scrollbar" is FALSE still need to remember the new
4023 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004024 dont_scroll = !allow_scrollbar;
4025
Bram Moolenaara338adc2018-01-31 20:51:47 +01004026 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004027 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004028 mch_enable_flush();
4029 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004030
4031 s_busy_processing = FALSE;
4032 dont_scroll = dont_scroll_save;
4033
4034 return 0;
4035}
4036
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038#ifdef FEAT_XPM_W32
4039# include "xpm_w32.h"
4040#endif
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar734a8672019-12-02 22:49:38 +01004043// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#define TEAROFF_PADDING_X 2
4045#define TEAROFF_BUTTON_PAD_X 8
4046#define TEAROFF_MIN_WIDTH 200
4047#define TEAROFF_SUBMENU_LABEL ">>"
4048#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4049
4050
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004051#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052# define ID_BEVAL_TOOLTIP 200
4053# define BEVAL_TEXT_LEN MAXPATHL
4054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004056static UINT_PTR beval_timer_id = 0;
4057static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004058#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004059
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004060
Bram Moolenaar734a8672019-12-02 22:49:38 +01004061// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063#ifdef FEAT_MENU
4064static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067/*
4068 * Use the system font for dialogs and tear-off menus. Remove this line to
4069 * use DLG_FONT_NAME.
4070 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004071#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074#define VIM_CLASSW L"Vim"
4075
Bram Moolenaar734a8672019-12-02 22:49:38 +01004076// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4077// thus there should be room for every dialog. For tearoffs it's made bigger
4078// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079#define DLG_ALLOC_SIZE 16 * 1024
4080
4081/*
4082 * stuff for dialogs, menus, tearoffs etc.
4083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084static PWORD
4085add_dialog_element(
4086 PWORD p,
4087 DWORD lStyle,
4088 WORD x,
4089 WORD y,
4090 WORD w,
4091 WORD h,
4092 WORD Id,
4093 WORD clss,
4094 const char *caption);
4095static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004096static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004097#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100static void get_dialog_font_metrics(void);
4101
4102static int dialog_default_button = -1;
4103
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#ifdef FEAT_TOOLBAR
4105static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004106static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004107static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004109#else
4110# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#endif
4112
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004113#ifdef FEAT_GUI_TABLINE
4114static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004115static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004116#endif
4117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118#ifdef FEAT_MBYTE_IME
4119static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4120static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4121#endif
4122#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4123# ifdef NOIME
4124typedef struct tagCOMPOSITIONFORM {
4125 DWORD dwStyle;
4126 POINT ptCurrentPos;
4127 RECT rcArea;
4128} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4129typedef HANDLE HIMC;
4130# endif
4131
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004132static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004133static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4134static HIMC (WINAPI *pImmGetContext)(HWND);
4135static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4136static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4137static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4138static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004139static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4140static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004141static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4142static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004143static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144static void dyn_imm_load(void);
4145#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146# define pImmGetCompositionStringW ImmGetCompositionStringW
4147# define pImmGetContext ImmGetContext
4148# define pImmAssociateContext ImmAssociateContext
4149# define pImmReleaseContext ImmReleaseContext
4150# define pImmGetOpenStatus ImmGetOpenStatus
4151# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004152# define pImmGetCompositionFontW ImmGetCompositionFontW
4153# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154# define pImmSetCompositionWindow ImmSetCompositionWindow
4155# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004156# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157#endif
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#ifdef FEAT_MENU
4160/*
4161 * Figure out how high the menu bar is at the moment.
4162 */
4163 static int
4164gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004165 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 static int old_menu_height = -1;
4168
4169 RECT rc1, rc2;
4170 int num;
4171 int menu_height;
4172
4173 if (gui.menu_is_active)
4174 num = GetMenuItemCount(s_menuBar);
4175 else
4176 num = 0;
4177
4178 if (num == 0)
4179 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004180 else if (IsMinimized(s_hwnd))
4181 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004182 // The height of the menu cannot be determined while the window is
4183 // minimized. Take the previous height if the menu is changed in that
4184 // state, to avoid that Vim's vertical window size accidentally
4185 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004186 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
4189 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004190 /*
4191 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4192 * seem to have been set yet, so menu wraps in default window
4193 * width which is very narrow. Instead just return height of a
4194 * single menu item. Will still be wrong when the menu really
4195 * should wrap over more than one line.
4196 */
4197 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4198 if (gui.starting)
4199 menu_height = rc1.bottom - rc1.top + 1;
4200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004202 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4203 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 }
4206
4207 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004208 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004209 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 return menu_height;
4212}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004213#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215
4216/*
4217 * Setup for the Intellimouse
4218 */
LemonBoyc27747e2022-05-07 12:25:40 +01004219 static long
4220mouse_vertical_scroll_step(void)
4221{
4222 UINT val;
4223 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4224 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4225 return 3; // Safe default;
4226}
4227
4228 static long
4229mouse_horizontal_scroll_step(void)
4230{
4231 UINT val;
4232 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4233 return (long)val;
4234 return 3; // Safe default;
4235}
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 static void
4238init_mouse_wheel(void)
4239{
LemonBoyc27747e2022-05-07 12:25:40 +01004240 // Get the default values for the horizontal and vertical scroll steps from
4241 // the system.
4242 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4243 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244}
4245
Bram Moolenaarae20f342019-11-05 21:09:23 +01004246/*
LemonBoya773d842022-05-10 20:54:46 +01004247 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004250_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
LemonBoy365d8f72022-05-05 19:23:07 +01004252 int button;
4253 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004254 int modifiers = 0;
4255 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004256 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4257 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
Bram Moolenaarae20f342019-11-05 21:09:23 +01004259 wp = gui_mouse_window(FIND_POPUP);
4260
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004261#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004262 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004263 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004264 cmdarg_T cap;
4265 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004266
Bram Moolenaarae20f342019-11-05 21:09:23 +01004267 // Mouse hovers over popup window, scroll it if possible.
4268 mouse_row = wp->w_winrow;
4269 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004270 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004271 if (horizontal)
4272 {
4273 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4274 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4275 }
4276 else
4277 {
4278 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4279 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4280 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004281 clear_oparg(&oa);
4282 cap.oap = &oa;
4283 nv_mousescroll(&cap);
4284 update_screen(0);
4285 setcursor();
4286 out_flush();
4287 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004288 }
4289#endif
4290
Bram Moolenaarae20f342019-11-05 21:09:23 +01004291 if (wp == NULL || !p_scf)
4292 wp = curwin;
4293
LemonBoy365d8f72022-05-05 19:23:07 +01004294 // Translate the scroll event into an event that Vim can process so that
4295 // the user has a chance to map the scrollwheel buttons.
4296 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004297 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 else
LemonBoy365d8f72022-05-05 19:23:07 +01004299 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004300
4301 kbd_modifiers = get_active_modifiers();
4302
4303 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4304 modifiers |= MOUSE_SHIFT;
4305 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4306 modifiers |= MOUSE_CTRL;
4307 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4308 modifiers |= MOUSE_ALT;
4309
LemonBoya773d842022-05-10 20:54:46 +01004310 // The cursor position is relative to the upper-left corner of the screen.
4311 pt.x = GET_X_LPARAM(lParam);
4312 pt.y = GET_Y_LPARAM(lParam);
4313 ScreenToClient(s_textArea, &pt);
4314
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004315 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316}
4317
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004318#ifdef USE_SYSMENU_FONT
4319/*
4320 * Get Menu Font.
4321 * Return OK or FAIL.
4322 */
4323 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004324gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004325{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004326 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004327
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004328 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4329 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004330 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004331 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004332 &nm,
4333 0))
4334 return FAIL;
4335 *lf = nm.lfMenuFont;
4336 return OK;
4337}
4338#endif
4339
4340
4341#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4342/*
4343 * Set the GUI tabline font to the system menu font
4344 */
4345 static void
4346set_tabline_font(void)
4347{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004348 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004349 HFONT font;
4350 HWND hwnd;
4351 HDC hdc;
4352 HFONT hfntOld;
4353 TEXTMETRIC tm;
4354
4355 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4356 return;
4357
K.Takatac81e9bf2022-01-16 14:15:49 +00004358 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004359 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004360
4361 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4362
4363 /*
4364 * Compute the height of the font used for the tab text
4365 */
4366 hwnd = GetDesktopWindow();
4367 hdc = GetWindowDC(hwnd);
4368 hfntOld = SelectFont(hdc, font);
4369
4370 GetTextMetrics(hdc, &tm);
4371
4372 SelectFont(hdc, hfntOld);
4373 ReleaseDC(hwnd, hdc);
4374
4375 /*
4376 * The space used by the tab border and the space between the tab label
4377 * and the tab border is included as 7.
4378 */
4379 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4380}
K.Takatac81e9bf2022-01-16 14:15:49 +00004381#else
4382# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004383#endif
4384
Bram Moolenaar520470a2005-06-16 21:59:56 +00004385/*
4386 * Invoked when a setting was changed.
4387 */
4388 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004389_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004390{
LemonBoy365d8f72022-05-05 19:23:07 +01004391 switch (param)
4392 {
4393 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004394 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004395 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004396 case SPI_SETWHEELSCROLLCHARS:
4397 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004398 break;
4399 case SPI_SETNONCLIENTMETRICS:
4400 set_tabline_font();
4401 break;
4402 default:
4403 break;
4404 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004405 return 0;
4406}
4407
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408#ifdef FEAT_NETBEANS_INTG
4409 static void
4410_OnWindowPosChanged(
4411 HWND hwnd,
4412 const LPWINDOWPOS lpwpos)
4413{
4414 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004415 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4418 || lpwpos->cx != cx || lpwpos->cy != cy))
4419 {
4420 x = lpwpos->x;
4421 y = lpwpos->y;
4422 cx = lpwpos->cx;
4423 cy = lpwpos->cy;
4424 netbeans_frame_moved(x, y);
4425 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004426 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004427 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428}
4429#endif
4430
K.Takata4f2417f2021-06-05 16:25:32 +02004431
4432static HWND hwndTip = NULL;
4433
4434 static void
4435show_sizing_tip(int cols, int rows)
4436{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004437 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004438 char buf[32];
4439
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004440 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004441 ti.hwnd = s_hwnd;
4442 ti.uId = (UINT_PTR)s_hwnd;
4443 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4444 ti.lpszText = buf;
4445 sprintf(buf, "%dx%d", cols, rows);
4446 if (hwndTip == NULL)
4447 {
4448 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4449 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4450 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4451 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4452 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4453 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4454 }
4455 else
4456 {
4457 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4458 }
4459 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4460}
4461
4462 static void
4463destroy_sizing_tip(void)
4464{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004465 if (hwndTip == NULL)
4466 return;
4467
4468 DestroyWindow(hwndTip);
4469 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004470}
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 static int
4473_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 UINT fwSide,
4475 LPRECT lprc)
4476{
4477 int w, h;
4478 int valid_w, valid_h;
4479 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004480 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 w = lprc->right - lprc->left;
4483 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004484 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 w_offset = w - valid_w;
4486 h_offset = h - valid_h;
4487
4488 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4489 || fwSide == WMSZ_BOTTOMLEFT)
4490 lprc->left += w_offset;
4491 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4492 || fwSide == WMSZ_BOTTOMRIGHT)
4493 lprc->right -= w_offset;
4494
4495 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4496 || fwSide == WMSZ_TOPRIGHT)
4497 lprc->top += h_offset;
4498 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4499 || fwSide == WMSZ_BOTTOMRIGHT)
4500 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004501
4502 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 return TRUE;
4504}
4505
K.Takata92000e22022-01-20 15:10:57 +00004506#ifdef FEAT_GUI_TABLINE
4507 static void
4508_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4509{
4510 if (gui_mch_showing_tabline())
4511 {
4512 POINT pt;
4513 RECT rect;
4514
4515 /*
4516 * If the cursor is on the tabline, display the tab menu
4517 */
4518 GetCursorPos(&pt);
4519 GetWindowRect(s_textArea, &rect);
4520 if (pt.y < rect.top)
4521 {
4522 show_tabline_popup_menu();
4523 return;
4524 }
4525 }
4526 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4527}
4528
4529 static void
4530_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4531{
4532 /*
4533 * If the user double clicked the tabline, create a new tab
4534 */
4535 if (gui_mch_showing_tabline())
4536 {
4537 POINT pt;
4538 RECT rect;
4539
4540 GetCursorPos(&pt);
4541 GetWindowRect(s_textArea, &rect);
4542 if (pt.y < rect.top)
4543 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4544 }
4545 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4546}
4547#endif
4548
4549 static UINT
4550_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4551{
4552 UINT result;
4553 int x, y;
4554
4555 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4556 if (result != HTCLIENT)
4557 return result;
4558
4559#ifdef FEAT_GUI_TABLINE
4560 if (gui_mch_showing_tabline())
4561 {
4562 RECT rct;
4563
4564 // If the cursor is on the GUI tabline, don't process this event
4565 GetWindowRect(s_textArea, &rct);
4566 if (yPos < rct.top)
4567 return result;
4568 }
4569#endif
4570 (void)gui_mch_get_winpos(&x, &y);
4571 xPos -= x;
4572
4573 if (xPos < 48) // <VN> TODO should use system metric?
4574 return HTBOTTOMLEFT;
4575 else
4576 return HTBOTTOMRIGHT;
4577}
4578
4579#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4580 static LRESULT
4581_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4582{
4583 switch (hdr->code)
4584 {
4585 case TTN_GETDISPINFOW:
4586 case TTN_GETDISPINFO:
4587 {
4588 char_u *str = NULL;
4589 static void *tt_text = NULL;
4590
4591 VIM_CLEAR(tt_text);
4592
4593# ifdef FEAT_GUI_TABLINE
4594 if (gui_mch_showing_tabline()
4595 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4596 {
4597 POINT pt;
4598 /*
4599 * Mouse is over the GUI tabline. Display the
4600 * tooltip for the tab under the cursor
4601 *
4602 * Get the cursor position within the tab control
4603 */
4604 GetCursorPos(&pt);
4605 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4606 {
4607 TCHITTESTINFO htinfo;
4608 int idx;
4609
4610 /*
4611 * Get the tab under the cursor
4612 */
4613 htinfo.pt.x = pt.x;
4614 htinfo.pt.y = pt.y;
4615 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4616 if (idx != -1)
4617 {
4618 tabpage_T *tp;
4619
4620 tp = find_tabpage(idx + 1);
4621 if (tp != NULL)
4622 {
4623 get_tabline_label(tp, TRUE);
4624 str = NameBuff;
4625 }
4626 }
4627 }
4628 }
4629# endif
4630# ifdef FEAT_TOOLBAR
4631# ifdef FEAT_GUI_TABLINE
4632 else
4633# endif
4634 {
4635 UINT idButton;
4636 vimmenu_T *pMenu;
4637
4638 idButton = (UINT) hdr->idFrom;
4639 pMenu = gui_mswin_find_menu(root_menu, idButton);
4640 if (pMenu)
4641 str = pMenu->strings[MENU_INDEX_TIP];
4642 }
4643# endif
4644 if (str == NULL)
4645 break;
4646
4647 // Set the maximum width, this also enables using \n for
4648 // line break.
4649 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4650
4651 if (hdr->code == TTN_GETDISPINFOW)
4652 {
4653 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4654
4655 tt_text = enc_to_utf16(str, NULL);
4656 lpdi->lpszText = tt_text;
4657 // can't show tooltip if failed
4658 }
4659 else
4660 {
4661 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4662
4663 if (STRLEN(str) < sizeof(lpdi->szText)
4664 || ((tt_text = vim_strsave(str)) == NULL))
4665 vim_strncpy((char_u *)lpdi->szText, str,
4666 sizeof(lpdi->szText) - 1);
4667 else
4668 lpdi->lpszText = tt_text;
4669 }
4670 }
4671 break;
4672
4673# ifdef FEAT_GUI_TABLINE
4674 case TCN_SELCHANGE:
4675 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4676 {
4677 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4678 return 0L;
4679 }
4680 break;
4681
4682 case NM_RCLICK:
4683 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4684 {
4685 show_tabline_popup_menu();
4686 return 0L;
4687 }
4688 break;
4689# endif
4690
4691 default:
4692 break;
4693 }
4694 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4695}
4696#endif
4697
4698#if defined(MENUHINTS) && defined(FEAT_MENU)
4699 static LRESULT
4700_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4701{
4702 if (((UINT) HIWORD(wParam)
4703 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4704 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004705 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004706 {
4707 UINT idButton;
4708 vimmenu_T *pMenu;
4709 static int did_menu_tip = FALSE;
4710
4711 if (did_menu_tip)
4712 {
4713 msg_clr_cmdline();
4714 setcursor();
4715 out_flush();
4716 did_menu_tip = FALSE;
4717 }
4718
4719 idButton = (UINT)LOWORD(wParam);
4720 pMenu = gui_mswin_find_menu(root_menu, idButton);
4721 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4722 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4723 {
4724 ++msg_hist_off;
4725 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4726 --msg_hist_off;
4727 setcursor();
4728 out_flush();
4729 did_menu_tip = TRUE;
4730 }
4731 return 0L;
4732 }
4733 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4734}
4735#endif
4736
K.Takatac81e9bf2022-01-16 14:15:49 +00004737 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004738_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004739{
4740 s_dpi = ydpi;
4741 s_in_dpichanged = TRUE;
4742 //TRACE("DPI: %d", ydpi);
4743
4744 update_scrollbar_size();
4745 update_toolbar_size();
4746 set_tabline_font();
4747
4748 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4749 gui_get_wide_font();
4750 gui_mswin_get_menu_height(FALSE);
4751#ifdef FEAT_MBYTE_IME
4752 im_set_position(gui.row, gui.col);
4753#endif
4754 InvalidateRect(hwnd, NULL, TRUE);
4755
4756 s_in_dpichanged = FALSE;
4757 return 0L;
4758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
4760
4761 static LRESULT CALLBACK
4762_WndProc(
4763 HWND hwnd,
4764 UINT uMsg,
4765 WPARAM wParam,
4766 LPARAM lParam)
4767{
LemonBoya773d842022-05-10 20:54:46 +01004768 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4769 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771 HandleMouseHide(uMsg, lParam);
4772
4773 s_uMsg = uMsg;
4774 s_wParam = wParam;
4775 s_lParam = lParam;
4776
4777 switch (uMsg)
4778 {
4779 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4780 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004781 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004783 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4785 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4786 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4787 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4788#ifdef FEAT_MENU
4789 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4790#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004791 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4792 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4794 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004795 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4796 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4798 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4799 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4800#ifdef FEAT_NETBEANS_INTG
4801 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4802#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004803#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004804 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4805 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004806#endif
K.Takata92000e22022-01-20 15:10:57 +00004807 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004808
Bram Moolenaar734a8672019-12-02 22:49:38 +01004809 case WM_QUERYENDSESSION: // System wants to go down.
4810 gui_shell_closed(); // Will exit when no changed buffers.
4811 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004814 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004817 return 0L;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 break;
4820
4821 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004822 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4823 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004824 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 return 0L;
4826
4827 case WM_SYSCHAR:
4828 /*
4829 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4830 * shortcut key, handle like a typed ALT key, otherwise call Windows
4831 * ALT key handling.
4832 */
4833#ifdef FEAT_MENU
4834 if ( !gui.menu_is_active
4835 || p_wak[0] == 'n'
4836 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4837 )
4838#endif
4839 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004840 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return 0L;
4842 }
4843#ifdef FEAT_MENU
4844 else
K.Takata4ac893f2022-01-20 12:44:28 +00004845 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#endif
4847
4848 case WM_SYSKEYUP:
4849#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004850 // This used to be done only when menu is active: ALT key is used for
4851 // that. But that caused problems when menu is disabled and using
4852 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4853 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004854 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004856 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#endif
4858
K.Takata4f2417f2021-06-05 16:25:32 +02004859 case WM_EXITSIZEMOVE:
4860 destroy_sizing_tip();
4861 break;
4862
Bram Moolenaar734a8672019-12-02 22:49:38 +01004863 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004867 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004868 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004869 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaar734a8672019-12-02 22:49:38 +01004871 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004872 case WM_SETTINGCHANGE:
4873 return _OnSettingChange((UINT)wParam);
4874
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004875#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004877 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#endif
K.Takata92000e22022-01-20 15:10:57 +00004879
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880#if defined(MENUHINTS) && defined(FEAT_MENU)
4881 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004882 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885#ifdef FEAT_MBYTE_IME
4886 case WM_IME_NOTIFY:
4887 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004888 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004889 return 1L;
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 case WM_IME_COMPOSITION:
4892 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004893 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004894 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004896 case WM_DPICHANGED:
4897 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4898 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004902 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#endif
K.Takata92000e22022-01-20 15:10:57 +00004905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907
K.Takata4ac893f2022-01-20 12:44:28 +00004908 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909}
4910
4911/*
4912 * End of call-back routines
4913 */
4914
Bram Moolenaar734a8672019-12-02 22:49:38 +01004915// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916HWND vim_parent_hwnd = NULL;
4917
4918 static BOOL CALLBACK
4919FindWindowTitle(HWND hwnd, LPARAM lParam)
4920{
4921 char buf[2048];
4922 char *title = (char *)lParam;
4923
4924 if (GetWindowText(hwnd, buf, sizeof(buf)))
4925 {
4926 if (strstr(buf, title) != NULL)
4927 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004928 // Found it. Store the window ref. and quit searching if MDI
4929 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004931 if (vim_parent_hwnd != NULL)
4932 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004935 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
4937
4938/*
4939 * Invoked for '-P "title"' argument: search for parent application to open
4940 * our window in.
4941 */
4942 void
4943gui_mch_set_parent(char *title)
4944{
4945 EnumWindows(FindWindowTitle, (LPARAM)title);
4946 if (vim_parent_hwnd == NULL)
4947 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004948 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 mch_exit(2);
4950 }
4951}
4952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004953#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 static void
4955ole_error(char *arg)
4956{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004957 char buf[IOSIZE];
4958
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004959# ifdef VIMDLL
4960 gui.in_use = mch_is_gui_executable();
4961# endif
4962
Bram Moolenaar734a8672019-12-02 22:49:38 +01004963 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004964 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004965 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004966 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004970#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4971 static char *
4972gvim_error(void)
4973{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004974 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004975
4976 if (starting)
4977 {
4978 mch_errmsg(msg);
4979 mch_errmsg("\n");
4980 mch_exit(2);
4981 }
4982 return msg;
4983}
4984
4985 char *
4986gui_mch_do_spawn(char_u *arg)
4987{
4988 int len;
4989# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4990 char_u *session = NULL;
4991 LPWSTR tofree1 = NULL;
4992# endif
4993 WCHAR name[MAX_PATH];
4994 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4995 STARTUPINFOW si = {sizeof(si)};
4996 PROCESS_INFORMATION pi;
4997
4998 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4999 goto error;
5000 p = wcsrchr(name, L'\\');
5001 if (p == NULL)
5002 goto error;
5003 // Replace the executable name from vim(d).exe to gvim(d).exe.
5004# ifdef DEBUG
5005 wcscpy(p + 1, L"gvimd.exe");
5006# else
5007 wcscpy(p + 1, L"gvim.exe");
5008# endif
5009
5010# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5011 if (starting)
5012# endif
5013 {
5014 // Pass the command line to the new process.
5015 p = GetCommandLineW();
5016 // Skip 1st argument.
5017 while (*p && *p != L' ' && *p != L'\t')
5018 {
5019 if (*p == L'"')
5020 {
5021 while (*p && *p != L'"')
5022 ++p;
5023 if (*p)
5024 ++p;
5025 }
5026 else
5027 ++p;
5028 }
5029 cmd = p;
5030 }
5031# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5032 else
5033 {
5034 // Create a session file and pass it to the new process.
5035 LPWSTR wsession;
5036 char_u *savebg;
5037 int ret;
5038
5039 session = vim_tempname('s', FALSE);
5040 if (session == NULL)
5041 goto error;
5042 savebg = p_bg;
5043 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5044 ret = write_session_file(session);
5045 vim_free(p_bg);
5046 p_bg = savebg;
5047 if (!ret)
5048 goto error;
5049 wsession = enc_to_utf16(session, NULL);
5050 if (wsession == NULL)
5051 goto error;
5052 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005053 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005054 if (cmd == NULL)
5055 {
5056 vim_free(wsession);
5057 goto error;
5058 }
5059 tofree1 = cmd;
5060 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5061 wsession, wsession);
5062 vim_free(wsession);
5063 }
5064# endif
5065
5066 // Check additional arguments to the `:gui` command.
5067 if (arg != NULL)
5068 {
5069 warg = enc_to_utf16(arg, NULL);
5070 if (warg == NULL)
5071 goto error;
5072 tofree2 = warg;
5073 }
5074 else
5075 warg = L"";
5076
5077 // Set up the new command line.
5078 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005079 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005080 if (newcmd == NULL)
5081 goto error;
5082 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5083
5084 // Spawn a new GUI process.
5085 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5086 NULL, NULL, &si, &pi))
5087 goto error;
5088 CloseHandle(pi.hProcess);
5089 CloseHandle(pi.hThread);
5090 mch_exit(0);
5091
5092error:
5093# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5094 if (session)
5095 mch_remove(session);
5096 vim_free(session);
5097 vim_free(tofree1);
5098# endif
5099 vim_free(newcmd);
5100 vim_free(tofree2);
5101 return gvim_error();
5102}
5103#endif
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105/*
5106 * Parse the GUI related command-line arguments. Any arguments used are
5107 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005108 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 */
5110 void
5111gui_mch_prepare(int *argc, char **argv)
5112{
5113 int silent = FALSE;
5114 int idx;
5115
Bram Moolenaar734a8672019-12-02 22:49:38 +01005116 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5118 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005119 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5121 && (argv[2][0] == '-' || argv[2][0] == '/'))
5122 {
5123 silent = TRUE;
5124 idx = 2;
5125 }
5126 else
5127 idx = 1;
5128
Bram Moolenaar734a8672019-12-02 22:49:38 +01005129 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (STRICMP(argv[idx] + 1, "register") == 0)
5131 {
5132#ifdef FEAT_OLE
5133 RegisterMe(silent);
5134 mch_exit(0);
5135#else
5136 if (!silent)
5137 ole_error("register");
5138 mch_exit(2);
5139#endif
5140 }
5141
Bram Moolenaar734a8672019-12-02 22:49:38 +01005142 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5144 {
5145#ifdef FEAT_OLE
5146 UnregisterMe(!silent);
5147 mch_exit(0);
5148#else
5149 if (!silent)
5150 ole_error("unregister");
5151 mch_exit(2);
5152#endif
5153 }
5154
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 // Ignore an -embedding argument. It is only relevant if the
5156 // application wants to treat the case when it is started manually
5157 // differently from the case where it is started via automation (and
5158 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5160 {
5161#ifdef FEAT_OLE
5162 *argc = 1;
5163#else
5164 ole_error("embedding");
5165 mch_exit(2);
5166#endif
5167 }
5168 }
5169
5170#ifdef FEAT_OLE
5171 {
5172 int bDoRestart = FALSE;
5173
5174 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005175 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (bDoRestart)
5177 mch_exit(0);
5178 }
5179#endif
5180
5181#ifdef FEAT_NETBEANS_INTG
5182 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005183 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 int arg;
5185
5186 for (arg = 1; arg < *argc; arg++)
5187 if (strncmp("-nb", argv[arg], 3) == 0)
5188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 netbeansArg = argv[arg];
5190 mch_memmove(&argv[arg], &argv[arg + 1],
5191 (--*argc - arg) * sizeof(char *));
5192 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005193 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197}
5198
K.Takatac81e9bf2022-01-16 14:15:49 +00005199 static void
5200load_dpi_func(void)
5201{
5202 HMODULE hUser32;
5203
5204 hUser32 = GetModuleHandle("user32.dll");
5205 if (hUser32 == NULL)
5206 goto fail;
5207
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005208 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5209 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5210 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005211 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005212 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5213 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005214
5215 if (pSetThreadDpiAwarenessContext != NULL)
5216 {
5217 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5218 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5219 if (oldctx != NULL)
5220 {
5221 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5222 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5223#ifdef DEBUG
5224 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5225 {
5226 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5227 }
5228#endif
5229 return;
5230 }
5231 }
5232
5233fail:
5234 // Disable PerMonitorV2 APIs.
5235 pGetDpiForSystem = stubGetDpiForSystem;
5236 pGetDpiForWindow = NULL;
5237 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5238 pSetThreadDpiAwarenessContext = NULL;
5239 pGetAwarenessFromDpiAwarenessContext = NULL;
5240}
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242/*
5243 * Initialise the GUI. Create all the windows, set up all the call-backs
5244 * etc.
5245 */
5246 int
5247gui_mch_init(void)
5248{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005250 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 // Return here if the window was already opened (happens when
5254 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005256 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258 /*
5259 * Load the tearoff bitmap
5260 */
5261#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005262 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#endif
5264
K.Takatac81e9bf2022-01-16 14:15:49 +00005265 load_dpi_func();
5266
5267 s_dpi = pGetDpiForSystem();
5268 update_scrollbar_size();
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005271 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#endif
5273 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005274#ifdef FEAT_TOOLBAR
5275 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5279
Bram Moolenaar734a8672019-12-02 22:49:38 +01005280 // First try using the wide version, so that we can use any title.
5281 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005282 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005284 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 wndclassw.lpfnWndProc = _WndProc;
5286 wndclassw.cbClsExtra = 0;
5287 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005288 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5290 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5291 wndclassw.hbrBackground = s_brush;
5292 wndclassw.lpszMenuName = NULL;
5293 wndclassw.lpszClassName = szVimWndClassW;
5294
K.Takata4ac893f2022-01-20 12:44:28 +00005295 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005296 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (vim_parent_hwnd != NULL)
5300 {
5301#ifdef HAVE_TRY_EXCEPT
5302 __try
5303 {
5304#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005305 // Open inside the specified parent window.
5306 // TODO: last argument should point to a CLIENTCREATESTRUCT
5307 // structure.
5308 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005310 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005311 WS_OVERLAPPEDWINDOW | WS_CHILD
5312 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5314 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005315 100, // Any value will do
5316 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005318 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319#ifdef HAVE_TRY_EXCEPT
5320 }
5321 __except(EXCEPTION_EXECUTE_HANDLER)
5322 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005323 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 }
5325#endif
5326 if (s_hwnd == NULL)
5327 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005328 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 mch_exit(2);
5330 }
5331 }
5332 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005333 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005334 // If the provided windowid is not valid reset it to zero, so that it
5335 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005336 if (IsWindow((HWND)win_socket_id) <= 0)
5337 win_socket_id = 0;
5338
Bram Moolenaar734a8672019-12-02 22:49:38 +01005339 // Create a window. If win_socket_id is not zero without border and
5340 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005341 s_hwnd = CreateWindowW(
5342 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005343 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5344 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005345 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5346 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005347 100, // Any value will do
5348 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005349 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005350 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005351 if (s_hwnd != NULL && win_socket_id != 0)
5352 {
5353 SetParent(s_hwnd, (HWND)win_socket_id);
5354 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5355 }
5356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 if (s_hwnd == NULL)
5359 return FAIL;
5360
K.Takatac81e9bf2022-01-16 14:15:49 +00005361 if (pGetDpiForWindow != NULL)
5362 {
5363 s_dpi = pGetDpiForWindow(s_hwnd);
5364 update_scrollbar_size();
5365 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5366 }
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5369 dyn_imm_load();
5370#endif
5371
Bram Moolenaar734a8672019-12-02 22:49:38 +01005372 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005373 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005374 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005375 wndclassw.style = CS_OWNDC;
5376 wndclassw.lpfnWndProc = _TextAreaWndProc;
5377 wndclassw.cbClsExtra = 0;
5378 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005379 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005380 wndclassw.hIcon = NULL;
5381 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5382 wndclassw.hbrBackground = NULL;
5383 wndclassw.lpszMenuName = NULL;
5384 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005385
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005386 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return FAIL;
5388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005390 s_textArea = CreateWindowExW(
5391 0,
5392 szTextAreaClassW, L"Vim text area",
5393 WS_CHILD | WS_VISIBLE, 0, 0,
5394 100, // Any value will do for now
5395 100, // Any value will do for now
5396 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005397 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if (s_textArea == NULL)
5400 return FAIL;
5401
Bram Moolenaar20321902016-02-17 12:30:17 +01005402#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005403 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005404 {
5405 HANDLE hIcon = NULL;
5406
5407 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005408 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005409 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005410#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#ifdef FEAT_MENU
5413 s_menuBar = CreateMenu();
5414#endif
5415 s_hdc = GetDC(s_textArea);
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar734a8672019-12-02 22:49:38 +01005419 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005420 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
Bram Moolenaar734a8672019-12-02 22:49:38 +01005422 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 gui_mch_def_colors();
5424
Bram Moolenaar734a8672019-12-02 22:49:38 +01005425 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5426 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 set_normal_colors();
5428
5429 /*
5430 * Check that none of the colors are the same as the background color.
5431 * Then store the current values as the defaults.
5432 */
5433 gui_check_colors();
5434 gui.def_norm_pixel = gui.norm_pixel;
5435 gui.def_back_pixel = gui.back_pixel;
5436
Bram Moolenaar734a8672019-12-02 22:49:38 +01005437 // Get the colors for the highlight groups (gui_check_colors() might have
5438 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 highlight_gui_started();
5440
5441 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005442 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005444 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446 /*
5447 * Set up for Intellimouse processing
5448 */
5449 init_mouse_wheel();
5450
5451 /*
5452 * compute a couple of metrics used for the dialogs
5453 */
5454 get_dialog_font_metrics();
5455#ifdef FEAT_TOOLBAR
5456 /*
5457 * Create the toolbar
5458 */
5459 initialise_toolbar();
5460#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005461#ifdef FEAT_GUI_TABLINE
5462 /*
5463 * Create the tabline
5464 */
5465 initialise_tabline();
5466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467#ifdef MSWIN_FIND_REPLACE
5468 /*
5469 * Initialise the dialog box stuff
5470 */
5471 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5472
Bram Moolenaar734a8672019-12-02 22:49:38 +01005473 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005475 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005477 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5479 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5480 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005483#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005484 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005485 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005486#endif
5487
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005488#ifdef FEAT_RENDER_OPTIONS
5489 if (p_rop)
5490 (void)gui_mch_set_rendering_options(p_rop);
5491#endif
5492
Bram Moolenaar748bf032005-02-02 23:04:36 +00005493theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005494 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005495 display_errors();
5496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 return OK;
5498}
5499
5500/*
5501 * Get the size of the screen, taking position on multiple monitors into
5502 * account (if supported).
5503 */
5504 static void
5505get_work_area(RECT *spi_rect)
5506{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005507 HMONITOR mon;
5508 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
Bram Moolenaar734a8672019-12-02 22:49:38 +01005510 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005511 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005512 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005514 moninfo.cbSize = sizeof(MONITORINFO);
5515 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005517 *spi_rect = moninfo.rcWork;
5518 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
5520 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005521 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5523}
5524
5525/*
5526 * Set the size of the window to the given width and height in pixels.
5527 */
5528 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005529gui_mch_set_shellsize(
5530 int width,
5531 int height,
5532 int min_width UNUSED,
5533 int min_height UNUSED,
5534 int base_width UNUSED,
5535 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005536 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005539 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar734a8672019-12-02 22:49:38 +01005542 // Try to keep window completely on screen.
5543 // Get position of the screen work area. This is the part that is not
5544 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 get_work_area(&workarea_rect);
5546
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 // Resizing a maximized window looks very strange, unzoom it first.
5548 // But don't do it when still starting up, it may have been requested in
5549 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005550 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005552
5553 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Bram Moolenaar734a8672019-12-02 22:49:38 +01005555 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005556 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5557 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5558 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5559 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5560 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5561 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar734a8672019-12-02 22:49:38 +01005563 // The following should take care of keeping Vim on the same monitor, no
5564 // matter if the secondary monitor is left or right of the primary
5565 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005566 window_rect.right = window_rect.left + win_width;
5567 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005568
Bram Moolenaar734a8672019-12-02 22:49:38 +01005569 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005570 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5571 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005573 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5574 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005576 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5577 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005579 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5580 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005582 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5583 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
5585 SetActiveWindow(s_hwnd);
5586 SetFocus(s_hwnd);
5587
Bram Moolenaar734a8672019-12-02 22:49:38 +01005588 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590}
5591
5592
5593 void
5594gui_mch_set_scrollbar_thumb(
5595 scrollbar_T *sb,
5596 long val,
5597 long size,
5598 long max)
5599{
5600 SCROLLINFO info;
5601
5602 sb->scroll_shift = 0;
5603 while (max > 32767)
5604 {
5605 max = (max + 1) >> 1;
5606 val >>= 1;
5607 size >>= 1;
5608 ++sb->scroll_shift;
5609 }
5610
5611 if (sb->scroll_shift > 0)
5612 ++size;
5613
5614 info.cbSize = sizeof(info);
5615 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5616 info.nPos = val;
5617 info.nMin = 0;
5618 info.nMax = max;
5619 info.nPage = size;
5620 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5621}
5622
5623
5624/*
5625 * Set the current text font.
5626 */
5627 void
5628gui_mch_set_font(GuiFont font)
5629{
5630 gui.currFont = font;
5631}
5632
5633
5634/*
5635 * Set the current text foreground color.
5636 */
5637 void
5638gui_mch_set_fg_color(guicolor_T color)
5639{
5640 gui.currFgColor = color;
5641}
5642
5643/*
5644 * Set the current text background color.
5645 */
5646 void
5647gui_mch_set_bg_color(guicolor_T color)
5648{
5649 gui.currBgColor = color;
5650}
5651
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005652/*
5653 * Set the current text special color.
5654 */
5655 void
5656gui_mch_set_sp_color(guicolor_T color)
5657{
5658 gui.currSpColor = color;
5659}
5660
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005661#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662/*
5663 * Multi-byte handling, originally by Sung-Hoon Baek.
5664 * First static functions (no prototypes generated).
5665 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005666# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005667# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005668# endif
5669# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 * handle WM_IME_NOTIFY message
5673 */
5674 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005675_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 LRESULT lResult = 0;
5678 HIMC hImc;
5679
5680 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5681 return lResult;
5682 switch (dwCommand)
5683 {
5684 case IMN_SETOPENSTATUS:
5685 if (pImmGetOpenStatus(hImc))
5686 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005687 LOGFONTW lf = norm_logfont;
5688 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5689 // Work around when PerMonitorV2 is not enabled in the process level.
5690 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5691 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 im_set_position(gui.row, gui.col);
5693
Bram Moolenaar734a8672019-12-02 22:49:38 +01005694 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005695 State &= ~MODE_LANGMAP;
5696 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005698# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005699 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5701 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005702 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 int old_row = gui.row;
5704 int old_col = gui.col;
5705
5706 // This must be called here before
5707 // status_redraw_curbuf(), otherwise the mode
5708 // message may appear in the wrong position.
5709 showmode();
5710 status_redraw_curbuf();
5711 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005712 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 gui.row = old_row;
5714 gui.col = old_col;
5715 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 }
5719 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005720 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 lResult = 0;
5722 break;
5723 }
5724 pImmReleaseContext(hWnd, hImc);
5725 return lResult;
5726}
5727
5728 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005729_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 char_u *ret;
5732 int len;
5733
Bram Moolenaar734a8672019-12-02 22:49:38 +01005734 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 return 0;
5736
5737 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5738 if (ret != NULL)
5739 {
5740 add_to_input_buf_csi(ret, len);
5741 vim_free(ret);
5742 return 1;
5743 }
5744 return 0;
5745}
5746
5747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 * void GetResultStr()
5749 *
5750 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5751 * get complete composition string
5752 */
5753 static char_u *
5754GetResultStr(HWND hwnd, int GCS, int *lenp)
5755{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005756 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005757 LONG ret;
5758 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 char_u *convbuf = NULL;
5760
5761 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5762 return NULL;
5763
K.Takatab0b2b732022-01-19 12:59:21 +00005764 // Get the length of the composition string.
5765 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5766 if (ret <= 0)
5767 return NULL;
5768
5769 // Allocate the requested buffer plus space for the NUL character.
5770 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (buf == NULL)
5772 return NULL;
5773
K.Takatab0b2b732022-01-19 12:59:21 +00005774 // Reads in the composition string.
5775 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5776 *lenp = ret / sizeof(WCHAR);
5777
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005778 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 pImmReleaseContext(hwnd, hIMC);
5780 vim_free(buf);
5781 return convbuf;
5782}
5783#endif
5784
Bram Moolenaar734a8672019-12-02 22:49:38 +01005785// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005786#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788/*
5789 * set font to IM.
5790 */
5791 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005792im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 HIMC hImc;
5795
5796 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5797 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005798 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 pImmReleaseContext(s_hwnd, hImc);
5800 }
5801}
5802
5803/*
5804 * Notify cursor position to IM.
5805 */
5806 void
5807im_set_position(int row, int col)
5808{
5809 HIMC hImc;
5810
5811 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5812 {
5813 COMPOSITIONFORM cfs;
5814
5815 cfs.dwStyle = CFS_POINT;
5816 cfs.ptCurrentPos.x = FILL_X(col);
5817 cfs.ptCurrentPos.y = FILL_Y(row);
5818 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005819 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5820 {
5821 // Work around when PerMonitorV2 is not enabled in the process level.
5822 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5823 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 pImmSetCompositionWindow(hImc, &cfs);
5826
5827 pImmReleaseContext(s_hwnd, hImc);
5828 }
5829}
5830
5831/*
5832 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5833 */
5834 void
5835im_set_active(int active)
5836{
5837 HIMC hImc;
5838 static HIMC hImcOld = (HIMC)0;
5839
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005840# ifdef VIMDLL
5841 if (!gui.in_use && !gui.starting)
5842 {
5843 mbyte_im_set_active(active);
5844 return;
5845 }
5846# endif
5847
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005848 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
5849 return;
5850
5851 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005853 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005855 hImcOld = pImmGetContext(s_hwnd);
5856 if (hImcOld)
5857 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005859 active = FALSE;
5860 }
5861 else if (hImcOld != (HIMC)0)
5862 {
5863 pImmAssociateContext(s_hwnd, hImcOld);
5864 hImcOld = (HIMC)0;
5865 }
5866
5867 hImc = pImmGetContext(s_hwnd);
5868 if (!hImc)
5869 return;
5870
5871 /*
5872 * for Korean ime
5873 */
5874 HKL hKL = GetKeyboardLayout(0);
5875
5876 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5877 {
5878 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5879 static BOOL bSaved = FALSE;
5880
5881 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005883 // if we have a saved conversion status, restore it
5884 if (bSaved)
5885 pImmSetConversionStatus(hImc, dwConversionSaved,
5886 dwSentenceSaved);
5887 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005891 // save conversion status and disable korean
5892 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5893 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00005894 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005895 bSaved = TRUE;
5896 pImmSetConversionStatus(hImc,
5897 dwConversionSaved & ~(IME_CMODE_NATIVE
5898 | IME_CMODE_FULLSHAPE),
5899 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00005900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005903
5904 pImmSetOpenStatus(hImc, active);
5905 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906}
5907
5908/*
5909 * Get IM status. When IM is on, return not 0. Else return 0.
5910 */
5911 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005912im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 int status = 0;
5915 HIMC hImc;
5916
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005917# ifdef VIMDLL
5918 if (!gui.in_use && !gui.starting)
5919 return mbyte_im_get_status();
5920# endif
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5923 {
5924 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5925 pImmReleaseContext(s_hwnd, hImc);
5926 }
5927 return status;
5928}
5929
Bram Moolenaar734a8672019-12-02 22:49:38 +01005930#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005933/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005934 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005935 */
5936 static void
5937latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5938{
5939 int c;
5940
Bram Moolenaarca003e12006-03-17 23:19:38 +00005941 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005942 {
5943 c = *text++;
5944 switch (c)
5945 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005946 case 0xa4: c = 0x20ac; break; // euro
5947 case 0xa6: c = 0x0160; break; // S hat
5948 case 0xa8: c = 0x0161; break; // S -hat
5949 case 0xb4: c = 0x017d; break; // Z hat
5950 case 0xb8: c = 0x017e; break; // Z -hat
5951 case 0xbc: c = 0x0152; break; // OE
5952 case 0xbd: c = 0x0153; break; // oe
5953 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005954 }
5955 *unicodebuf++ = c;
5956 }
5957}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
5959#ifdef FEAT_RIGHTLEFT
5960/*
5961 * What is this for? In the case where you are using Win98 or Win2K or later,
5962 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5963 * reverses the string sent to the TextOut... family. This sucks, because we
5964 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5965 * way to tell Windblows not to do this!
5966 *
5967 * The short of it is that this 'RevOut' only gets called if you are running
5968 * one of the new, "improved" MS OSes, and only if you are running in
5969 * 'rightleft' mode. It makes display take *slightly* longer, but not
5970 * noticeably so.
5971 */
5972 static void
K.Takata135e1522022-01-29 15:27:58 +00005973RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 int col,
5975 int row,
5976 UINT foptions,
5977 CONST RECT *pcliprect,
5978 LPCTSTR text,
5979 UINT len,
5980 CONST INT *padding)
5981{
5982 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005984 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005985 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005986 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987}
5988#endif
5989
Bram Moolenaar92467d32017-12-05 13:22:16 +01005990 static void
5991draw_line(
5992 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005993 int y1,
5994 int x2,
5995 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005996 COLORREF color)
5997{
5998#if defined(FEAT_DIRECTX)
5999 if (IS_ENABLE_DIRECTX())
6000 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6001 else
6002#endif
6003 {
6004 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6005 HPEN old_pen = SelectObject(s_hdc, hpen);
6006 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006007 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006008 LineTo(s_hdc, x2, y2);
6009 DeleteObject(SelectObject(s_hdc, old_pen));
6010 }
6011}
6012
6013 static void
6014set_pixel(
6015 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006016 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006017 COLORREF color)
6018{
6019#if defined(FEAT_DIRECTX)
6020 if (IS_ENABLE_DIRECTX())
6021 DWriteContext_SetPixel(s_dwc, x, y, color);
6022 else
6023#endif
6024 SetPixel(s_hdc, x, y, color);
6025}
6026
6027 static void
6028fill_rect(
6029 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006030 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006031 COLORREF color)
6032{
6033#if defined(FEAT_DIRECTX)
6034 if (IS_ENABLE_DIRECTX())
6035 DWriteContext_FillRect(s_dwc, rcp, color);
6036 else
6037#endif
6038 {
6039 HBRUSH hbr2;
6040
6041 if (hbr == NULL)
6042 hbr2 = CreateSolidBrush(color);
6043 else
6044 hbr2 = hbr;
6045 FillRect(s_hdc, rcp, hbr2);
6046 if (hbr == NULL)
6047 DeleteBrush(hbr2);
6048 }
6049}
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 void
6052gui_mch_draw_string(
6053 int row,
6054 int col,
6055 char_u *text,
6056 int len,
6057 int flags)
6058{
6059 static int *padding = NULL;
6060 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 const RECT *pcliprect = NULL;
6062 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 static WCHAR *unicodebuf = NULL;
6064 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006065 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 int y;
6068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 /*
6070 * Italic and bold text seems to have an extra row of pixels at the bottom
6071 * (below where the bottom of the character should be). If we draw the
6072 * characters with a solid background, the top row of pixels in the
6073 * character below will be overwritten. We can fix this by filling in the
6074 * background ourselves, to the correct character proportions, and then
6075 * writing the character in transparent mode. Still have a problem when
6076 * the character is "_", which gets written on to the character below.
6077 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6078 * pixel in their slots, which fixes the problem with the bottom row of
6079 * pixels. We still need this code because otherwise the top row of pixels
6080 * becomes a problem. - webb.
6081 */
6082 static HBRUSH hbr_cache[2] = {NULL, NULL};
6083 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6084 static int brush_lru = 0;
6085 HBRUSH hbr;
6086 RECT rc;
6087
6088 if (!(flags & DRAW_TRANSP))
6089 {
6090 /*
6091 * Clear background first.
6092 * Note: FillRect() excludes right and bottom of rectangle.
6093 */
6094 rc.left = FILL_X(col);
6095 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (has_mbyte)
6097 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006098 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006099 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
6101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 rc.right = FILL_X(col + len);
6103 rc.bottom = FILL_Y(row + 1);
6104
Bram Moolenaar734a8672019-12-02 22:49:38 +01006105 // Cache the created brush, that saves a lot of time. We need two:
6106 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (gui.currBgColor == brush_color[0])
6108 {
6109 hbr = hbr_cache[0];
6110 brush_lru = 1;
6111 }
6112 else if (gui.currBgColor == brush_color[1])
6113 {
6114 hbr = hbr_cache[1];
6115 brush_lru = 0;
6116 }
6117 else
6118 {
6119 if (hbr_cache[brush_lru] != NULL)
6120 DeleteBrush(hbr_cache[brush_lru]);
6121 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6122 brush_color[brush_lru] = gui.currBgColor;
6123 hbr = hbr_cache[brush_lru];
6124 brush_lru = !brush_lru;
6125 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006126
Bram Moolenaar92467d32017-12-05 13:22:16 +01006127 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129 SetBkMode(s_hdc, TRANSPARENT);
6130
6131 /*
6132 * When drawing block cursor, prevent inverted character spilling
6133 * over character cell (can happen with bold/italic)
6134 */
6135 if (flags & DRAW_CURSOR)
6136 {
6137 pcliprect = &rc;
6138 foptions = ETO_CLIPPED;
6139 }
6140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 SetTextColor(s_hdc, gui.currFgColor);
6142 SelectFont(s_hdc, gui.currFont);
6143
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006144#ifdef FEAT_DIRECTX
6145 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006146 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006147#endif
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6150 {
K.Takata135e1522022-01-29 15:27:58 +00006151 int i;
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 vim_free(padding);
6154 pad_size = Columns;
6155
Bram Moolenaar734a8672019-12-02 22:49:38 +01006156 // Don't give an out-of-memory message here, it would call us
6157 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006158 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (padding != NULL)
6160 for (i = 0; i < pad_size; i++)
6161 padding[i] = gui.char_width;
6162 }
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 /*
6165 * We have to provide the padding argument because italic and bold versions
6166 * of fixed-width fonts are often one pixel or so wider than their normal
6167 * versions.
6168 * No check for DRAW_BOLD, Windows will have done it already.
6169 */
6170
Bram Moolenaar734a8672019-12-02 22:49:38 +01006171 // Check if there are any UTF-8 characters. If not, use normal text
6172 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (enc_utf8)
6174 for (n = 0; n < len; ++n)
6175 if (text[n] >= 0x80)
6176 break;
6177
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006178#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6180 // required that unicode drawing routine, currently. So this forces it
6181 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006182 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006184#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006185
Bram Moolenaar734a8672019-12-02 22:49:38 +01006186 // Check if the Unicode buffer exists and is big enough. Create it
6187 // with the same length as the multi-byte string, the number of wide
6188 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006189 if ((enc_utf8
6190 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6191 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 && (unicodebuf == NULL || len > unibuflen))
6193 {
6194 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006195 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006198 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
6200 unibuflen = len;
6201 }
6202
6203 if (enc_utf8 && n < len && unicodebuf != NULL)
6204 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006205 // Output UTF-8 characters. Composing characters should be
6206 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006207 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006208 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006209 int cells; // cell width of string up to composing char
6210 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006211 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006213 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006215 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006217 c = utf_ptr2char(text + i);
6218 if (c >= 0x10000)
6219 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006220 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006221 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6222 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006223 }
6224 else
6225 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006226 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006227 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006228
6229 if (utf_iscomposing(c))
6230 cw = 0;
6231 else
6232 {
6233 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006234 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006235 cw = 1;
6236 }
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (unicodepdy != NULL)
6239 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006240 // Use unicodepdy to make characters fit as we expect, even
6241 // when the font uses different widths (e.g., bold character
6242 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006243 if (c >= 0x10000)
6244 {
6245 unicodepdy[wlen - 2] = cw * gui.char_width;
6246 unicodepdy[wlen - 1] = 0;
6247 }
6248 else
6249 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006252 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006254#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006255 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006256 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006257 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006258 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006259 TEXT_X(col), TEXT_Y(row),
6260 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006261 gui.char_width, gui.currFgColor,
6262 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006263 }
6264 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006265#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006266 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6267 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006268 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006270 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006272 // If we want to display codepage data, and the current CP is not the
6273 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (unicodebuf != NULL)
6275 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006276 if (enc_latin9)
6277 latin9_to_ucs(text, len, unicodebuf);
6278 else
6279 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 MB_PRECOMPOSED,
6281 (char *)text, len,
6282 (LPWSTR)unicodebuf, unibuflen);
6283 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006284 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006285 // Use unicodepdy to make characters fit as we expect, even
6286 // when the font uses different widths (e.g., bold character
6287 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006288 if (unicodepdy != NULL)
6289 {
6290 int i;
6291 int cw;
6292
6293 for (i = 0; i < len; ++i)
6294 {
6295 cw = utf_char2cells(unicodebuf[i]);
6296 if (cw > 2)
6297 cw = 1;
6298 unicodepdy[i] = cw * gui.char_width;
6299 }
6300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006302 foptions, pcliprect, unicodebuf, len, unicodepdy);
6303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305 }
6306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
6308#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006309 // Windows will mess up RL text, so we have to draw it character by
6310 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006311 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6313 foptions, pcliprect, (char *)text, len, padding);
6314 else
6315#endif
6316 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6317 foptions, pcliprect, (char *)text, len, padding);
6318 }
6319
Bram Moolenaar734a8672019-12-02 22:49:38 +01006320 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (flags & DRAW_UNDERL)
6322 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006323 // When p_linespace is 0, overwrite the bottom row of pixels.
6324 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (p_linespace > 1)
6327 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006328 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006330
Bram Moolenaar734a8672019-12-02 22:49:38 +01006331 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006332 if (flags & DRAW_STRIKE)
6333 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006334 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006335 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006336 }
6337
Bram Moolenaar734a8672019-12-02 22:49:38 +01006338 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006339 if (flags & DRAW_UNDERC)
6340 {
6341 int x;
6342 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006343 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006344
6345 y = FILL_Y(row + 1) - 1;
6346 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6347 {
6348 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006349 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006350 }
6351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352}
6353
6354
6355/*
6356 * Output routines.
6357 */
6358
Bram Moolenaar734a8672019-12-02 22:49:38 +01006359/*
6360 * Flush any output to the screen
6361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 void
6363gui_mch_flush(void)
6364{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006365#if defined(FEAT_DIRECTX)
6366 if (IS_ENABLE_DIRECTX())
6367 DWriteContext_Flush(s_dwc);
6368#endif
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 GdiFlush();
6371}
6372
6373 static void
6374clear_rect(RECT *rcp)
6375{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006376 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377}
6378
6379
Bram Moolenaarc716c302006-01-21 22:12:51 +00006380 void
6381gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6382{
6383 RECT workarea_rect;
6384
6385 get_work_area(&workarea_rect);
6386
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006387 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006388 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6389 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006390
Bram Moolenaar734a8672019-12-02 22:49:38 +01006391 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6392 // the menubar for MSwin, we subtract it from the screen height, so that
6393 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006394 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006395 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6396 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6397 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6398 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006399}
6400
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402#if defined(FEAT_MENU) || defined(PROTO)
6403/*
6404 * Add a sub menu to the menu bar.
6405 */
6406 void
6407gui_mch_add_menu(
6408 vimmenu_T *menu,
6409 int pos)
6410{
6411 vimmenu_T *parent = menu->parent;
6412
6413 menu->submenu_id = CreatePopupMenu();
6414 menu->id = s_menu_id++;
6415
6416 if (menu_is_menubar(menu->name))
6417 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006418 WCHAR *wn;
6419 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006421 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006422 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006423 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006425 infow.cbSize = sizeof(infow);
6426 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6427 | MIIM_SUBMENU;
6428 infow.dwItemData = (long_u)menu;
6429 infow.wID = menu->id;
6430 infow.fType = MFT_STRING;
6431 infow.dwTypeData = wn;
6432 infow.cch = (UINT)wcslen(wn);
6433 infow.hSubMenu = menu->submenu_id;
6434 InsertMenuItemW((parent == NULL)
6435 ? s_menuBar : parent->submenu_id,
6436 (UINT)pos, TRUE, &infow);
6437 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439
Bram Moolenaar734a8672019-12-02 22:49:38 +01006440 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (parent == NULL)
6442 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006443# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 else if (IsWindow(parent->tearoff_handle))
6445 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447}
6448
6449 void
6450gui_mch_show_popupmenu(vimmenu_T *menu)
6451{
6452 POINT mp;
6453
K.Takata45f9cfb2022-01-21 11:11:00 +00006454 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6456}
6457
6458 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006459gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 vimmenu_T *menu = gui_find_menu(path_name);
6462
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006463 if (menu == NULL)
6464 return;
6465
6466 POINT p;
6467
6468 // Find the position of the current cursor
6469 GetDCOrgEx(s_hdc, &p);
6470 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006472 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006474 gui_mch_getmouse(&mx, &my);
6475 p.x += mx;
6476 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006478 else if (curwin != NULL)
6479 {
6480 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6481 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6482 }
6483 msg_scroll = FALSE;
6484 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006487# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6490 * create it as a pseudo-"tearoff menu".
6491 */
6492 void
6493gui_make_tearoff(char_u *path_name)
6494{
6495 vimmenu_T *menu = gui_find_menu(path_name);
6496
Bram Moolenaar734a8672019-12-02 22:49:38 +01006497 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (menu != NULL)
6499 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6500}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503/*
6504 * Add a menu item to a menu
6505 */
6506 void
6507gui_mch_add_menu_item(
6508 vimmenu_T *menu,
6509 int idx)
6510{
6511 vimmenu_T *parent = menu->parent;
6512
6513 menu->id = s_menu_id++;
6514 menu->submenu_id = NULL;
6515
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006516# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6518 {
6519 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6520 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6521 }
6522 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006523# endif
6524# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (menu_is_toolbar(parent->name))
6526 {
6527 TBBUTTON newtb;
6528
Bram Moolenaara80faa82020-04-12 19:37:17 +02006529 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (menu_is_separator(menu->name))
6531 {
6532 newtb.iBitmap = 0;
6533 newtb.fsStyle = TBSTYLE_SEP;
6534 }
6535 else
6536 {
6537 newtb.iBitmap = get_toolbar_bitmap(menu);
6538 newtb.fsStyle = TBSTYLE_BUTTON;
6539 }
6540 newtb.idCommand = menu->id;
6541 newtb.fsState = TBSTATE_ENABLED;
6542 newtb.iString = 0;
6543 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6544 (LPARAM)&newtb);
6545 menu->submenu_id = (HMENU)-1;
6546 }
6547 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006550 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006552 wn = enc_to_utf16(menu->name, NULL);
6553 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006555 InsertMenuW(parent->submenu_id, (UINT)idx,
6556 (menu_is_separator(menu->name)
6557 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6558 (UINT)menu->id, wn);
6559 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006561# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (IsWindow(parent->tearoff_handle))
6563 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566}
6567
6568/*
6569 * Destroy the machine specific menu widget.
6570 */
6571 void
6572gui_mch_destroy_menu(vimmenu_T *menu)
6573{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006574# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 /*
6576 * is this a toolbar button?
6577 */
6578 if (menu->submenu_id == (HMENU)-1)
6579 {
6580 int iButton;
6581
6582 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6583 (WPARAM)menu->id, 0);
6584 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6585 }
6586 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 {
6589 if (menu->parent != NULL
6590 && menu_is_popup(menu->parent->dname)
6591 && menu->parent->submenu_id != NULL)
6592 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6593 else
6594 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6595 if (menu->submenu_id != NULL)
6596 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006597# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 if (IsWindow(menu->tearoff_handle))
6599 DestroyWindow(menu->tearoff_handle);
6600 if (menu->parent != NULL
6601 && menu->parent->children != NULL
6602 && IsWindow(menu->parent->tearoff_handle))
6603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006604 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 menu->modes = 0;
6606 rebuild_tearoff(menu->parent);
6607 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
6610}
6611
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006612# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 static void
6614rebuild_tearoff(vimmenu_T *menu)
6615{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006616 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 char_u tbuf[128];
6618 RECT trect;
6619 RECT rct;
6620 RECT roct;
6621 int x, y;
6622
6623 HWND thwnd = menu->tearoff_handle;
6624
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006625 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (GetWindowRect(thwnd, &trect)
6627 && GetWindowRect(s_hwnd, &rct)
6628 && GetClientRect(s_hwnd, &roct))
6629 {
6630 x = trect.left - rct.left;
6631 y = (trect.top - rct.bottom + roct.bottom);
6632 }
6633 else
6634 {
6635 x = y = 0xffffL;
6636 }
6637 DestroyWindow(thwnd);
6638 if (menu->children != NULL)
6639 {
6640 gui_mch_tearoff(tbuf, menu, x, y);
6641 if (IsWindow(menu->tearoff_handle))
6642 (void) SetWindowPos(menu->tearoff_handle,
6643 NULL,
6644 (int)trect.left,
6645 (int)trect.top,
6646 0, 0,
6647 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6648 }
6649}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006650# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652/*
6653 * Make a menu either grey or not grey.
6654 */
6655 void
6656gui_mch_menu_grey(
6657 vimmenu_T *menu,
6658 int grey)
6659{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006660# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 /*
6662 * is this a toolbar button?
6663 */
6664 if (menu->submenu_id == (HMENU)-1)
6665 {
6666 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006667 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 }
6669 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006670# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006671 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6672 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006674# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6676 {
6677 WORD menuID;
6678 HWND menuHandle;
6679
6680 /*
6681 * A tearoff button has changed state.
6682 */
6683 if (menu->children == NULL)
6684 menuID = (WORD)(menu->id);
6685 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006686 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6688 if (menuHandle)
6689 EnableWindow(menuHandle, !grey);
6690
6691 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693}
6694
Bram Moolenaar734a8672019-12-02 22:49:38 +01006695#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697
Bram Moolenaar734a8672019-12-02 22:49:38 +01006698// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006701#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6704/*
6705 * stuff for dialogs
6706 */
6707
6708/*
6709 * The callback routine used by all the dialogs. Very simple. First,
6710 * acknowledges the INITDIALOG message so that Windows knows to do standard
6711 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6712 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6713 * number.
6714 */
6715 static LRESULT CALLBACK
6716dialog_callback(
6717 HWND hwnd,
6718 UINT message,
6719 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006720 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 if (message == WM_INITDIALOG)
6723 {
6724 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006725 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 (void)SetFocus(hwnd);
6727 if (dialog_default_button > IDCANCEL)
6728 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006729 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006730 // We don't have a default, set focus on another element of the
6731 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006732 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 return FALSE;
6734 }
6735
6736 if (message == WM_COMMAND)
6737 {
6738 int button = LOWORD(wParam);
6739
Bram Moolenaar734a8672019-12-02 22:49:38 +01006740 // Don't end the dialog if something was selected that was
6741 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (button >= DLG_NONBUTTON_CONTROL)
6743 return TRUE;
6744
Bram Moolenaar734a8672019-12-02 22:49:38 +01006745 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006747 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006748 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006749 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006750
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006751 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6752 p = utf16_to_enc(wp, NULL);
6753 vim_strncpy(s_textfield, p, IOSIZE);
6754 vim_free(p);
6755 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758 /*
6759 * Need to check for IDOK because if the user just hits Return to
6760 * accept the default value, some reason this is what we get.
6761 */
6762 if (button == IDOK)
6763 {
6764 if (dialog_default_button > IDCANCEL)
6765 EndDialog(hwnd, dialog_default_button);
6766 }
6767 else
6768 EndDialog(hwnd, button - IDCANCEL);
6769 return TRUE;
6770 }
6771
6772 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6773 {
6774 EndDialog(hwnd, 0);
6775 return TRUE;
6776 }
6777 return FALSE;
6778}
6779
6780/*
6781 * Create a dialog dynamically from the parameter strings.
6782 * type = type of dialog (question, alert, etc.)
6783 * title = dialog title. may be NULL for default title.
6784 * message = text to display. Dialog sizes to accommodate it.
6785 * buttons = '\n' separated list of button captions, default first.
6786 * dfltbutton = number of default button.
6787 *
6788 * This routine returns 1 if the first button is pressed,
6789 * 2 for the second, etc.
6790 *
6791 * 0 indicates Esc was pressed.
6792 * -1 for unexpected error
6793 *
6794 * If stubbing out this fn, return 1.
6795 */
6796
Bram Moolenaar734a8672019-12-02 22:49:38 +01006797static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799 "IDR_VIM",
6800 "IDR_VIM_ERROR",
6801 "IDR_VIM_ALERT",
6802 "IDR_VIM_INFO",
6803 "IDR_VIM_QUESTION"
6804};
6805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 int
6807gui_mch_dialog(
6808 int type,
6809 char_u *title,
6810 char_u *message,
6811 char_u *buttons,
6812 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006813 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006814 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006817 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 int numButtons;
6819 int *buttonWidths, *buttonPositions;
6820 int buttonYpos;
6821 int nchar, i;
6822 DWORD lStyle;
6823 int dlgwidth = 0;
6824 int dlgheight;
6825 int editboxheight;
6826 int horizWidth = 0;
6827 int msgheight;
6828 char_u *pstart;
6829 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006830 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 char_u *tbuffer;
6832 RECT rect;
6833 HWND hwnd;
6834 HDC hdc;
6835 HFONT font, oldFont;
6836 TEXTMETRIC fontInfo;
6837 int fontHeight;
6838 int textWidth, minButtonWidth, messageWidth;
6839 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006840 int maxDialogHeight;
6841 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 int vertical;
6843 int dlgPaddingX;
6844 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006845# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006846 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006848# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006849 garray_T ga;
6850 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006851 int dlg_icon_width;
6852 int dlg_icon_height;
6853 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006855# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006856 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006857# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006858 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006859# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006860 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006861 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
Bram Moolenaar748bf032005-02-02 23:04:36 +00006864 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006865 {
6866 load_dpi_func();
6867 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006868 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006869 }
6870 else
6871 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873 if ((type < 0) || (type > VIM_LAST_TYPE))
6874 type = 0;
6875
Bram Moolenaar734a8672019-12-02 22:49:38 +01006876 // allocate some memory for dialog template
6877 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006878 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006879 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
6881 if (p == NULL)
6882 return -1;
6883
6884 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006885 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6887 * const.
6888 */
6889 tbuffer = vim_strsave(buttons);
6890 if (tbuffer == NULL)
6891 return -1;
6892
Bram Moolenaar734a8672019-12-02 22:49:38 +01006893 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar734a8672019-12-02 22:49:38 +01006895 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 numButtons = 1;
6897 for (i = 0; tbuffer[i] != '\0'; i++)
6898 {
6899 if (tbuffer[i] == DLG_BUTTON_SEP)
6900 numButtons++;
6901 }
6902 if (dfltbutton >= numButtons)
6903 dfltbutton = -1;
6904
Bram Moolenaar734a8672019-12-02 22:49:38 +01006905 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006906 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (buttonWidths == NULL)
6908 return -1;
6909
Bram Moolenaar734a8672019-12-02 22:49:38 +01006910 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006911 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (buttonPositions == NULL)
6913 return -1;
6914
6915 /*
6916 * Calculate how big the dialog must be.
6917 */
6918 hwnd = GetDesktopWindow();
6919 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006920# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6922 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006923 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 use_lfSysmenu = TRUE;
6925 }
6926 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006927# endif
K.Takatad1c58992022-01-23 12:31:57 +00006928 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6929 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6930
6931 oldFont = SelectFont(hdc, font);
6932 dlgPaddingX = DLG_PADDING_X;
6933 dlgPaddingY = DLG_PADDING_Y;
6934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 GetTextMetrics(hdc, &fontInfo);
6936 fontHeight = fontInfo.tmHeight;
6937
Bram Moolenaar734a8672019-12-02 22:49:38 +01006938 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006939 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar734a8672019-12-02 22:49:38 +01006941 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006942 if (s_hwnd == NULL)
6943 {
6944 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
Bram Moolenaar734a8672019-12-02 22:49:38 +01006946 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006947 get_work_area(&workarea_rect);
6948 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006949 if (maxDialogWidth > adjust_by_system_dpi(600))
6950 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006951 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006952 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006953 }
6954 else
6955 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006956 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006957 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006958 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006959 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6960 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6961 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6962 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006963
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006964 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006965 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6966 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6967 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6968 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6969 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006970 }
6971
Bram Moolenaar734a8672019-12-02 22:49:38 +01006972 // Set dlgwidth to width of message.
6973 // Copy the message into "ga", changing NL to CR-NL and inserting line
6974 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 pstart = message;
6976 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006977 msgheight = 0;
6978 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 do
6980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006981 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006982
Bram Moolenaar734a8672019-12-02 22:49:38 +01006983 // Need to figure out where to break the string. The system does it
6984 // at a word boundary, which would mean we can't compute the number of
6985 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006986 textWidth = 0;
6987 last_white = NULL;
6988 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006989 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006990 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006991 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006992 && textWidth > maxDialogWidth * 3 / 4)
6993 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006994 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006995 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006996 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006999 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007000 textWidth = 0;
7001
7002 if (last_white != NULL)
7003 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007004 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007005 if (pend > last_white)
7006 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007007 pend = last_white + 1;
7008 last_white = NULL;
7009 }
7010 ga_append(&ga, '\r');
7011 ga_append(&ga, '\n');
7012 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007013 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007014
7015 while (--l >= 0)
7016 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007017 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007018 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007020
7021 ga_append(&ga, '\r');
7022 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 pstart = pend + 1;
7024 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007025
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007026 if (ga.ga_data != NULL)
7027 message = ga.ga_data;
7028
Bram Moolenaar734a8672019-12-02 22:49:38 +01007029 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007030
K.Takatac81e9bf2022-01-16 14:15:49 +00007031 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7032 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
K.Takatac81e9bf2022-01-16 14:15:49 +00007034 // Add width of icon to dlgwidth, and some space
7035 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7036 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7037
7038 if (msgheight < dlg_icon_height)
7039 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
7041 /*
7042 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007043 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007045 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (!vertical)
7047 {
7048 // Place buttons horizontally if they fit.
7049 horizWidth = dlgPaddingX;
7050 pstart = tbuffer;
7051 i = 0;
7052 do
7053 {
7054 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7055 if (pend == NULL)
7056 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007057 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 if (textWidth < minButtonWidth)
7059 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007060 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 buttonWidths[i] = textWidth;
7062 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007063 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 pstart = pend + 1;
7065 } while (*pend != NUL);
7066
7067 if (horizWidth > maxDialogWidth)
7068 vertical = TRUE; // Too wide to fit on the screen.
7069 else if (horizWidth > dlgwidth)
7070 dlgwidth = horizWidth;
7071 }
7072
7073 if (vertical)
7074 {
7075 // Stack buttons vertically.
7076 pstart = tbuffer;
7077 do
7078 {
7079 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7080 if (pend == NULL)
7081 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007082 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007083 textWidth += dlgPaddingX; // Padding within button
7084 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (textWidth > dlgwidth)
7086 dlgwidth = textWidth;
7087 pstart = pend + 1;
7088 } while (*pend != NUL);
7089 }
7090
7091 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007092 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar734a8672019-12-02 22:49:38 +01007094 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007095 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 add_long(lStyle);
7098 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007099 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 add_word(0); // NumberOfItems(will change later)
7101 add_word(10); // x
7102 add_word(10); // y
7103 add_word(PixelToDialogX(dlgwidth)); // cx
7104
7105 // Dialog height.
7106 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007107 dlgheight = msgheight + 2 * dlgPaddingY
7108 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 else
7110 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7111
7112 // Dialog needs to be taller if contains an edit box.
7113 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7114 if (textfield != NULL)
7115 dlgheight += editboxheight;
7116
Bram Moolenaar734a8672019-12-02 22:49:38 +01007117 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007118 if (dlgheight > maxDialogHeight)
7119 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007120 msgheight = msgheight - (dlgheight - maxDialogHeight);
7121 dlgheight = maxDialogHeight;
7122 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007123 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007124 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007125 }
7126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 add_word(PixelToDialogY(dlgheight));
7128
7129 add_word(0); // Menu
7130 add_word(0); // Class
7131
Bram Moolenaar734a8672019-12-02 22:49:38 +01007132 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007133 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7134 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 p += nchar;
7136
K.Takatad1c58992022-01-23 12:31:57 +00007137 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007138# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007139 if (use_lfSysmenu)
7140 {
7141 // point size
7142 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7143 GetDeviceCaps(hdc, LOGPIXELSY));
7144 wcscpy(p, lfSysmenu.lfFaceName);
7145 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 }
K.Takatad1c58992022-01-23 12:31:57 +00007147 else
7148# endif
7149 {
7150 *p++ = DLG_FONT_POINT_SIZE; // point size
7151 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7152 }
7153 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 buttonYpos = msgheight + 2 * dlgPaddingY;
7156
7157 if (textfield != NULL)
7158 buttonYpos += editboxheight;
7159
7160 pstart = tbuffer;
7161 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007162 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 for (i = 0; i < numButtons; i++)
7164 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007165 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 for ( pend = pstart;
7167 *pend && (*pend != DLG_BUTTON_SEP);
7168 pend++)
7169 ;
7170
7171 if (*pend)
7172 *pend = '\0';
7173
7174 /*
7175 * old NOTE:
7176 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7177 * the focus to the first tab-able button and in so doing makes that
7178 * the default!! Grrr. Workaround: Make the default button the only
7179 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7180 * he/she can use arrow keys.
7181 *
7182 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007183 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 * dialog. Also needed for when the textfield is the default control.
7185 * It appears to work now (perhaps not on Win95?).
7186 */
7187 if (vertical)
7188 {
7189 p = add_dialog_element(p,
7190 (i == dfltbutton
7191 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7192 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007193 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 + 2 * fontHeight * i),
7195 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7196 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007197 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199 else
7200 {
7201 p = add_dialog_element(p,
7202 (i == dfltbutton
7203 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7204 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007205 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 PixelToDialogX(buttonWidths[i]),
7207 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007208 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007210 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212 *pnumitems += numButtons;
7213
Bram Moolenaar734a8672019-12-02 22:49:38 +01007214 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 p = add_dialog_element(p, SS_ICON,
7216 PixelToDialogX(dlgPaddingX),
7217 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007218 PixelToDialogX(dlg_icon_width),
7219 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7221 dlg_icons[type]);
7222
Bram Moolenaar734a8672019-12-02 22:49:38 +01007223 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007224 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007225 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007226 PixelToDialogY(dlgPaddingY),
7227 (WORD)(PixelToDialogX(messageWidth) + 1),
7228 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007229 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
Bram Moolenaar734a8672019-12-02 22:49:38 +01007231 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 if (textfield != NULL)
7233 {
7234 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7235 PixelToDialogX(2 * dlgPaddingX),
7236 PixelToDialogY(2 * dlgPaddingY + msgheight),
7237 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7238 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007239 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 *pnumitems += 1;
7241 }
7242
7243 *pnumitems += 2;
7244
7245 SelectFont(hdc, oldFont);
7246 DeleteObject(font);
7247 ReleaseDC(hwnd, hdc);
7248
Bram Moolenaar734a8672019-12-02 22:49:38 +01007249 // Let the dialog_callback() function know which button to make default
7250 // If we have an edit box, make that the default. We also need to tell
7251 // dialog_callback() if this dialog contains an edit box or not. We do
7252 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (textfield != NULL)
7254 {
7255 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7256 s_textfield = textfield;
7257 }
7258 else
7259 {
7260 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7261 s_textfield = NULL;
7262 }
7263
Bram Moolenaar734a8672019-12-02 22:49:38 +01007264 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007266 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 (LPDLGTEMPLATE)pdlgtemplate,
7268 s_hwnd,
7269 (DLGPROC)dialog_callback);
7270
7271 LocalFree(LocalHandle(pdlgtemplate));
7272 vim_free(tbuffer);
7273 vim_free(buttonWidths);
7274 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007275 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
Bram Moolenaar734a8672019-12-02 22:49:38 +01007277 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 (void)SetFocus(s_hwnd);
7279
7280 return nchar;
7281}
7282
Bram Moolenaar734a8672019-12-02 22:49:38 +01007283#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285/*
7286 * Put a simple element (basic class) onto a dialog template in memory.
7287 * return a pointer to where the next item should be added.
7288 *
7289 * parameters:
7290 * lStyle = additional style flags
7291 * (be careful, NT3.51 & Win32s will ignore the new ones)
7292 * x,y = x & y positions IN DIALOG UNITS
7293 * w,h = width and height IN DIALOG UNITS
7294 * Id = ID used in messages
7295 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7296 * caption = usually text or resource name
7297 *
7298 * TODO: use the length information noted here to enable the dialog creation
7299 * routines to work out more exactly how much memory they need to alloc.
7300 */
7301 static PWORD
7302add_dialog_element(
7303 PWORD p,
7304 DWORD lStyle,
7305 WORD x,
7306 WORD y,
7307 WORD w,
7308 WORD h,
7309 WORD Id,
7310 WORD clss,
7311 const char *caption)
7312{
7313 int nchar;
7314
Bram Moolenaar734a8672019-12-02 22:49:38 +01007315 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7317 *p++ = LOWORD(lStyle);
7318 *p++ = HIWORD(lStyle);
7319 *p++ = 0; // LOWORD (lExtendedStyle)
7320 *p++ = 0; // HIWORD (lExtendedStyle)
7321 *p++ = x;
7322 *p++ = y;
7323 *p++ = w;
7324 *p++ = h;
7325 *p++ = Id; //9 or 10 words in all
7326
7327 *p++ = (WORD)0xffff;
7328 *p++ = clss; //2 more here
7329
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007330 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 p += nchar;
7332
7333 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7334
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007335 return p; // total = 15 + strlen(caption) words
7336 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
7339
7340/*
7341 * Helper routine. Take an input pointer, return closest pointer that is
7342 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7343 */
7344 static LPWORD
7345lpwAlign(
7346 LPWORD lpIn)
7347{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007348 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007350 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 ul += 3;
7352 ul >>= 2;
7353 ul <<= 2;
7354 return (LPWORD)ul;
7355}
7356
7357/*
7358 * Helper routine. Takes second parameter as Ansi string, copies it to first
7359 * parameter as wide character (16-bits / char) string, and returns integer
7360 * number of wide characters (words) in string (including the trailing wide
7361 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007362 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7363 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 static int
7365nCopyAnsiToWideChar(
7366 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007367 LPSTR lpAnsiIn,
7368 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
7370 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007371 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 int i;
7373 WCHAR *wn;
7374
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007375 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007377 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007378 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (wn != NULL)
7380 {
7381 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007382 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 vim_free(wn);
7384 }
7385 }
7386 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007387 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 nChar = MultiByteToWideChar(
7389 enc_codepage > 0 ? enc_codepage : CP_ACP,
7390 MB_PRECOMPOSED,
7391 lpAnsiIn, len,
7392 lpWCStr, len);
7393 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007394 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
7397 return nChar;
7398}
7399
7400
7401#ifdef FEAT_TEAROFF
7402/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007403 * Lookup menu handle from "menu_id".
7404 */
7405 static HMENU
7406tearoff_lookup_menuhandle(
7407 vimmenu_T *menu,
7408 WORD menu_id)
7409{
7410 for ( ; menu != NULL; menu = menu->next)
7411 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007412 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007413 continue;
7414 if (menu_is_separator(menu->dname))
7415 continue;
7416 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7417 return menu->submenu_id;
7418 }
7419 return NULL;
7420}
7421
7422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 * The callback function for all the modeless dialogs that make up the
7424 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7425 * thinking its menus have been clicked), and go away when closed.
7426 */
7427 static LRESULT CALLBACK
7428tearoff_callback(
7429 HWND hwnd,
7430 UINT message,
7431 WPARAM wParam,
7432 LPARAM lParam)
7433{
7434 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007435 {
7436 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
Bram Moolenaar734a8672019-12-02 22:49:38 +01007440 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 HandleMouseHide(message, lParam);
7442
7443 if (message == WM_COMMAND)
7444 {
7445 if ((WORD)(LOWORD(wParam)) & 0x8000)
7446 {
7447 POINT mp;
7448 RECT rect;
7449
7450 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7451 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007452 vimmenu_T *menu;
7453
7454 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007456 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7458 (int)rect.right - 8,
7459 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007460 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 s_hwnd,
7462 NULL);
7463 /*
7464 * NOTE: The pop-up menu can eat the mouse up event.
7465 * We deal with this in normal.c.
7466 */
7467 }
7468 }
7469 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7472 /*
7473 * Give main window the focus back: this is so after
7474 * choosing a tearoff button you can start typing again
7475 * straight away.
7476 */
7477 (void)SetFocus(s_hwnd);
7478 return TRUE;
7479 }
7480 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7481 {
7482 DestroyWindow(hwnd);
7483 return TRUE;
7484 }
7485
Bram Moolenaar734a8672019-12-02 22:49:38 +01007486 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 if (message == WM_EXITSIZEMOVE)
7488 (void)SetActiveWindow(s_hwnd);
7489
7490 return FALSE;
7491}
7492#endif
7493
7494
7495/*
K.Takatad1c58992022-01-23 12:31:57 +00007496 * Computes the dialog base units based on the current dialog font.
7497 * We don't use the GetDialogBaseUnits() API, because we don't use the
7498 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 */
7500 static void
7501get_dialog_font_metrics(void)
7502{
7503 HDC hdc;
7504 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 SIZE size;
7506#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007507 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#endif
7509
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007511 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007512 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514#endif
7515 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007516 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517
K.Takatad1c58992022-01-23 12:31:57 +00007518 hdc = GetDC(s_hwnd);
7519 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007520 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007521 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
K.Takataabe628e2022-01-23 16:25:17 +00007523 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007524 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525}
7526
7527#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7528/*
7529 * Create a pseudo-"tearoff menu" based on the child
7530 * items of a given menu pointer.
7531 */
7532 static void
7533gui_mch_tearoff(
7534 char_u *title,
7535 vimmenu_T *menu,
7536 int initX,
7537 int initY)
7538{
7539 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7540 int template_len;
7541 int nchar, textWidth, submenuWidth;
7542 DWORD lStyle;
7543 DWORD lExtendedStyle;
7544 WORD dlgwidth;
7545 WORD menuID;
7546 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007547 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 vimmenu_T *the_menu = menu;
7549 HWND hwnd;
7550 HDC hdc;
7551 HFONT font, oldFont;
7552 int col, spaceWidth, len;
7553 int columnWidths[2];
7554 char_u *label, *text;
7555 int acLen = 0;
7556 int nameLen;
7557 int padding0, padding1, padding2 = 0;
7558 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007559 int x;
7560 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007561# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007562 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565
7566 /*
7567 * If this menu is already torn off, move it to the mouse position.
7568 */
7569 if (IsWindow(menu->tearoff_handle))
7570 {
7571 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007572 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
7574 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7575 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7576 }
7577 return;
7578 }
7579
7580 /*
7581 * Create a new tearoff.
7582 */
7583 if (*title == MNU_HIDDEN_CHAR)
7584 title++;
7585
Bram Moolenaar734a8672019-12-02 22:49:38 +01007586 // Allocate memory to store the dialog template. It's made bigger when
7587 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 template_len = DLG_ALLOC_SIZE;
7589 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7590 if (p == NULL)
7591 return;
7592
7593 hwnd = GetDesktopWindow();
7594 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007595# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7597 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007598 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 use_lfSysmenu = TRUE;
7600 }
7601 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007602# endif
K.Takatad1c58992022-01-23 12:31:57 +00007603 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7604 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7605
7606 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
Bram Moolenaar734a8672019-12-02 22:49:38 +01007608 // Calculate width of a single space. Used for padding columns to the
7609 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007610 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
Bram Moolenaar734a8672019-12-02 22:49:38 +01007612 // Figure out max width of the text column, the accelerator column and the
7613 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 submenuWidth = 0;
7615 for (col = 0; col < 2; col++)
7616 {
7617 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007618 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007620 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 text = (col == 0) ? pmenu->dname : pmenu->actext;
7622 if (text != NULL && *text != NUL)
7623 {
7624 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7625 if (textWidth > columnWidths[col])
7626 columnWidths[col] = textWidth;
7627 }
7628 if (pmenu->children != NULL)
7629 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7630 }
7631 }
7632 if (columnWidths[1] == 0)
7633 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007634 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (submenuWidth != 0)
7636 columnWidths[0] += submenuWidth;
7637 else
7638 columnWidths[0] += spaceWidth;
7639 }
7640 else
7641 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007642 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7644 columnWidths[1] += submenuWidth;
7645 }
7646
7647 /*
7648 * Now find the total width of our 'menu'.
7649 */
7650 textWidth = columnWidths[0] + columnWidths[1];
7651 if (submenuWidth != 0)
7652 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007653 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7655 textWidth += submenuWidth;
7656 }
7657 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7658 if (textWidth > dlgwidth)
7659 dlgwidth = textWidth;
7660 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7661
Bram Moolenaar734a8672019-12-02 22:49:38 +01007662 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007663 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
7665 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7666 *p++ = LOWORD(lStyle);
7667 *p++ = HIWORD(lStyle);
7668 *p++ = LOWORD(lExtendedStyle);
7669 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007670 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007672 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007674 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 else
7676 *p++ = PixelToDialogX(initX); // x
7677 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007678 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 else
7680 *p++ = PixelToDialogY(initY); // y
7681 *p++ = PixelToDialogX(dlgwidth); // cx
7682 ptrueheight = p;
7683 *p++ = 0; // dialog height: changed later anyway
7684 *p++ = 0; // Menu
7685 *p++ = 0; // Class
7686
Bram Moolenaar734a8672019-12-02 22:49:38 +01007687 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007689 ? (LPSTR)title
7690 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 p += nchar;
7692
K.Takatad1c58992022-01-23 12:31:57 +00007693 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007694# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007695 if (use_lfSysmenu)
7696 {
7697 // point size
7698 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7699 GetDeviceCaps(hdc, LOGPIXELSY));
7700 wcscpy(p, lfSysmenu.lfFaceName);
7701 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
K.Takatad1c58992022-01-23 12:31:57 +00007703 else
7704# endif
7705 {
7706 *p++ = DLG_FONT_POINT_SIZE; // point size
7707 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7708 }
7709 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710
7711 /*
7712 * Loop over all the items in the menu.
7713 * But skip over the tearbar.
7714 */
7715 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7716 menu = menu->children->next;
7717 else
7718 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007719 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 for ( ; menu != NULL; menu = menu->next)
7721 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007722 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 continue;
7724 if (menu_is_separator(menu->dname))
7725 {
7726 sepPadding += 3;
7727 continue;
7728 }
7729
Bram Moolenaar734a8672019-12-02 22:49:38 +01007730 // Check if there still is plenty of room in the template. Make it
7731 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7733 {
7734 WORD *newp;
7735
7736 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7737 if (newp != NULL)
7738 {
7739 template_len += 4096;
7740 mch_memmove(newp, pdlgtemplate,
7741 (char *)p - (char *)pdlgtemplate);
7742 p = newp + (p - pdlgtemplate);
7743 pnumitems = newp + (pnumitems - pdlgtemplate);
7744 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7745 LocalFree(LocalHandle(pdlgtemplate));
7746 pdlgtemplate = newp;
7747 }
7748 }
7749
Bram Moolenaar734a8672019-12-02 22:49:38 +01007750 // Figure out minimal length of this menu label. Use "name" for the
7751 // actual text, "dname" for estimating the displayed size. "name"
7752 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 len = nameLen = (int)STRLEN(menu->name);
7754 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7755 (int)STRLEN(menu->dname))) / spaceWidth;
7756 len += padding0;
7757
7758 if (menu->actext != NULL)
7759 {
7760 acLen = (int)STRLEN(menu->actext);
7761 len += acLen;
7762 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7763 }
7764 else
7765 textWidth = 0;
7766 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7767 len += padding1;
7768
7769 if (menu->children == NULL)
7770 {
7771 padding2 = submenuWidth / spaceWidth;
7772 len += padding2;
7773 menuID = (WORD)(menu->id);
7774 }
7775 else
7776 {
7777 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007778 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780
Bram Moolenaar734a8672019-12-02 22:49:38 +01007781 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007782 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 if (label == NULL)
7784 break;
7785
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007786 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007787 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007789 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 while (padding0-- > 0)
7791 *text++ = ' ';
7792 if (menu->actext != NULL)
7793 {
7794 STRNCPY(text, menu->actext, acLen);
7795 text += acLen;
7796 }
7797 while (padding1-- > 0)
7798 *text++ = ' ';
7799 if (menu->children != NULL)
7800 {
7801 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7802 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7803 }
7804 else
7805 {
7806 while (padding2-- > 0)
7807 *text++ = ' ';
7808 }
7809 *text = NUL;
7810
7811 /*
7812 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7813 * W95/NT4 it makes the tear-off look more like a menu.
7814 */
7815 p = add_dialog_element(p,
7816 BS_PUSHBUTTON|BS_LEFT,
7817 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7818 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7819 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7820 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007821 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 vim_free(label);
7823 (*pnumitems)++;
7824 }
7825
7826 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7827
7828
Bram Moolenaar734a8672019-12-02 22:49:38 +01007829 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007830 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007831 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 (LPDLGTEMPLATE)pdlgtemplate,
7833 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007834 (DLGPROC)tearoff_callback,
7835 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 LocalFree(LocalHandle(pdlgtemplate));
7838 SelectFont(hdc, oldFont);
7839 DeleteObject(font);
7840 ReleaseDC(hwnd, hdc);
7841
7842 /*
7843 * Reassert ourselves as the active window. This is so that after creating
7844 * a tearoff, the user doesn't have to click with the mouse just to start
7845 * typing again!
7846 */
7847 (void)SetActiveWindow(s_hwnd);
7848
Bram Moolenaar734a8672019-12-02 22:49:38 +01007849 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 force_menu_update = TRUE;
7851}
7852#endif
7853
7854#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007855# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857/*
7858 * Create the toolbar, initially unpopulated.
7859 * (just like the menu, there are no defaults, it's all
7860 * set up through menu.vim)
7861 */
7862 static void
7863initialise_toolbar(void)
7864{
7865 InitCommonControls();
7866 s_toolbarhwnd = CreateToolbarEx(
7867 s_hwnd,
7868 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7869 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007870 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007871 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 IDR_TOOLBAR1, // id of initial bitmap
7873 NULL,
7874 0, // initial number of buttons
7875 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7876 TOOLBAR_BUTTON_HEIGHT,
7877 TOOLBAR_BUTTON_WIDTH,
7878 TOOLBAR_BUTTON_HEIGHT,
7879 sizeof(TBBUTTON)
7880 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007881
7882 // Remove transparency from the toolbar to prevent the main window
7883 // background colour showing through
7884 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7885 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7886
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007887 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888
7889 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007890
7891 update_toolbar_size();
7892}
7893
7894 static void
7895update_toolbar_size(void)
7896{
7897 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007898 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007899
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007900 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007901 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7902 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7903 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7904 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7905
7906 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7907 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7908 //TRACE("button size: %d, %d", w, h);
7909 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7910 gui.toolbar_height = h + 6;
7911
7912 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7913 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7914
7915 // TODO:
7916 // Currently, this function only updates the size of toolbar buttons.
7917 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918}
7919
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007920 static LRESULT CALLBACK
7921toolbar_wndproc(
7922 HWND hwnd,
7923 UINT uMsg,
7924 WPARAM wParam,
7925 LPARAM lParam)
7926{
7927 HandleMouseHide(uMsg, lParam);
7928 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7929}
7930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 static int
7932get_toolbar_bitmap(vimmenu_T *menu)
7933{
7934 int i = -1;
7935
7936 /*
7937 * Check user bitmaps first, unless builtin is specified.
7938 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007939 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
7941 char_u fname[MAXPATHL];
7942 HANDLE hbitmap = NULL;
7943
7944 if (menu->iconfile != NULL)
7945 {
7946 gui_find_iconfile(menu->iconfile, fname, "bmp");
7947 hbitmap = LoadImage(
7948 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007949 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 IMAGE_BITMAP,
7951 TOOLBAR_BUTTON_WIDTH,
7952 TOOLBAR_BUTTON_HEIGHT,
7953 LR_LOADFROMFILE |
7954 LR_LOADMAP3DCOLORS
7955 );
7956 }
7957
7958 /*
7959 * If the LoadImage call failed, or the "icon=" file
7960 * didn't exist or wasn't specified, try the menu name
7961 */
7962 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007963 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007964# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007965 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007966# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007967 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 hbitmap = LoadImage(
7969 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007970 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 IMAGE_BITMAP,
7972 TOOLBAR_BUTTON_WIDTH,
7973 TOOLBAR_BUTTON_HEIGHT,
7974 LR_LOADFROMFILE |
7975 LR_LOADMAP3DCOLORS
7976 );
7977
7978 if (hbitmap != NULL)
7979 {
7980 TBADDBITMAP tbAddBitmap;
7981
7982 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007983 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
7985 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7986 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007987 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989 }
7990 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7991 i = menu->iconidx;
7992
7993 return i;
7994}
7995#endif
7996
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007997#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7998 static void
7999initialise_tabline(void)
8000{
8001 InitCommonControls();
8002
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008003 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008004 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008005 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008006 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008007 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008008
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008009 gui.tabline_height = TABLINE_HEIGHT;
8010
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008011 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008012}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008013
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008014/*
8015 * Get tabpage_T from POINT.
8016 */
8017 static tabpage_T *
8018GetTabFromPoint(
8019 HWND hWnd,
8020 POINT pt)
8021{
8022 tabpage_T *ptp = NULL;
8023
8024 if (gui_mch_showing_tabline())
8025 {
8026 TCHITTESTINFO htinfo;
8027 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008028 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008029 if (s_tabhwnd == hWnd)
8030 {
8031 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8032 if (idx != -1)
8033 ptp = find_tabpage(idx + 1);
8034 }
8035 }
8036 return ptp;
8037}
8038
8039static POINT s_pt = {0, 0};
8040static HCURSOR s_hCursor = NULL;
8041
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008042 static LRESULT CALLBACK
8043tabline_wndproc(
8044 HWND hwnd,
8045 UINT uMsg,
8046 WPARAM wParam,
8047 LPARAM lParam)
8048{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008049 POINT pt;
8050 tabpage_T *tp;
8051 RECT rect;
8052 int nCenter;
8053 int idx0;
8054 int idx1;
8055
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008056 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008057
8058 switch (uMsg)
8059 {
8060 case WM_LBUTTONDOWN:
8061 {
8062 s_pt.x = GET_X_LPARAM(lParam);
8063 s_pt.y = GET_Y_LPARAM(lParam);
8064 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008065 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008066 break;
8067 }
8068 case WM_MOUSEMOVE:
8069 if (GetCapture() == hwnd
8070 && ((wParam & MK_LBUTTON)) != 0)
8071 {
8072 pt.x = GET_X_LPARAM(lParam);
8073 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008074 if (abs(pt.x - s_pt.x) >
8075 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008076 {
8077 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8078
8079 tp = GetTabFromPoint(hwnd, pt);
8080 if (tp != NULL)
8081 {
8082 idx0 = tabpage_index(curtab) - 1;
8083 idx1 = tabpage_index(tp) - 1;
8084
8085 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8086 nCenter = rect.left + (rect.right - rect.left) / 2;
8087
Bram Moolenaar734a8672019-12-02 22:49:38 +01008088 // Check if the mouse cursor goes over the center of
8089 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008090 if ((idx0 < idx1) && (nCenter < pt.x))
8091 {
8092 tabpage_move(idx1 + 1);
8093 update_screen(0);
8094 }
8095 else if ((idx1 < idx0) && (pt.x < nCenter))
8096 {
8097 tabpage_move(idx1);
8098 update_screen(0);
8099 }
8100 }
8101 }
8102 }
8103 break;
8104 case WM_LBUTTONUP:
8105 {
8106 if (GetCapture() == hwnd)
8107 {
8108 SetCursor(s_hCursor);
8109 ReleaseCapture();
8110 }
8111 break;
8112 }
regomne3bdef102022-09-26 20:48:32 +01008113 case WM_MBUTTONUP:
8114 {
8115 TCHITTESTINFO htinfo;
8116
8117 htinfo.pt.x = GET_X_LPARAM(lParam);
8118 htinfo.pt.y = GET_Y_LPARAM(lParam);
8119 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8120 if (idx0 != -1)
8121 {
8122 idx0 += 1;
8123 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8124 }
8125 break;
8126 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008127 default:
8128 break;
8129 }
8130
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008131 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8132}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008133#endif
8134
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8136/*
8137 * Make the GUI window come to the foreground.
8138 */
8139 void
8140gui_mch_set_foreground(void)
8141{
8142 if (IsIconic(s_hwnd))
8143 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8144 SetForegroundWindow(s_hwnd);
8145}
8146#endif
8147
8148#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8149 static void
8150dyn_imm_load(void)
8151{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008152 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (hLibImm == NULL)
8154 return;
8155
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008157 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008159 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008161 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008163 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008165 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008167 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008168 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008169 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008170 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008171 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008173 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008175 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008176 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008177 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
K.Takatab0b2b732022-01-19 12:59:21 +00008179 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 || pImmGetContext == NULL
8181 || pImmAssociateContext == NULL
8182 || pImmReleaseContext == NULL
8183 || pImmGetOpenStatus == NULL
8184 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008185 || pImmGetCompositionFontW == NULL
8186 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008188 || pImmGetConversionStatus == NULL
8189 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
8191 FreeLibrary(hLibImm);
8192 hLibImm = NULL;
8193 pImmGetContext = NULL;
8194 return;
8195 }
8196
8197 return;
8198}
8199
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200#endif
8201
8202#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8203
8204# ifdef FEAT_XPM_W32
8205# define IMAGE_XPM 100
8206# endif
8207
8208typedef struct _signicon_t
8209{
8210 HANDLE hImage;
8211 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008212# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008213 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215} signicon_t;
8216
8217 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008218gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 signicon_t *sign;
8221 int x, y, w, h;
8222
8223 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8224 return;
8225
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008226# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008227 if (IS_ENABLE_DIRECTX())
8228 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008229# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 x = TEXT_X(col);
8232 y = TEXT_Y(row);
8233 w = gui.char_width * 2;
8234 h = gui.char_height;
8235 switch (sign->uType)
8236 {
8237 case IMAGE_BITMAP:
8238 {
8239 HDC hdcMem;
8240 HBITMAP hbmpOld;
8241
8242 hdcMem = CreateCompatibleDC(s_hdc);
8243 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8244 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8245 SelectObject(hdcMem, hbmpOld);
8246 DeleteDC(hdcMem);
8247 }
8248 break;
8249 case IMAGE_ICON:
8250 case IMAGE_CURSOR:
8251 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8252 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008253# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 case IMAGE_XPM:
8255 {
8256 HDC hdcMem;
8257 HBITMAP hbmpOld;
8258
8259 hdcMem = CreateCompatibleDC(s_hdc);
8260 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008261 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8263
8264 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008265 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8267 SelectObject(hdcMem, hbmpOld);
8268 DeleteDC(hdcMem);
8269 }
8270 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 }
8273}
8274
8275 static void
8276close_signicon_image(signicon_t *sign)
8277{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008278 if (sign == NULL)
8279 return;
8280
8281 switch (sign->uType)
8282 {
8283 case IMAGE_BITMAP:
8284 DeleteObject((HGDIOBJ)sign->hImage);
8285 break;
8286 case IMAGE_CURSOR:
8287 DestroyCursor((HCURSOR)sign->hImage);
8288 break;
8289 case IMAGE_ICON:
8290 DestroyIcon((HICON)sign->hImage);
8291 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008292# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008293 case IMAGE_XPM:
8294 DeleteObject((HBITMAP)sign->hImage);
8295 DeleteObject((HBITMAP)sign->hShape);
8296 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008297# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299}
8300
8301 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008302gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 signicon_t sign, *psign;
8305 char_u *ext;
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008308 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (ext > signfile)
8310 {
8311 int do_load = 1;
8312
8313 if (!STRICMP(ext, ".bmp"))
8314 sign.uType = IMAGE_BITMAP;
8315 else if (!STRICMP(ext, ".ico"))
8316 sign.uType = IMAGE_ICON;
8317 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8318 sign.uType = IMAGE_CURSOR;
8319 else
8320 do_load = 0;
8321
8322 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008323 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 gui.char_width * 2, gui.char_height,
8325 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008326# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 if (!STRICMP(ext, ".xpm"))
8328 {
8329 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008330 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8331 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
8335
8336 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008337 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 *psign = sign;
8339
8340 if (!psign)
8341 {
8342 if (sign.hImage)
8343 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008344 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 }
8346 return (void *)psign;
8347
8348}
8349
8350 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008351gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008353 if (sign == NULL)
8354 return;
8355
8356 close_signicon_image((signicon_t *)sign);
8357 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008361#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
Bram Moolenaar734a8672019-12-02 22:49:38 +01008363/*
8364 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008365 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008367 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8369 * to get current mouse position).
8370 *
8371 * Trying to use as more Windows services as possible, and as less
8372 * IE version as possible :)).
8373 *
8374 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8375 * BalloonEval struct.
8376 * 2) Enable/Disable simply create/kill BalloonEval Timer
8377 * 3) When there was enough inactivity, timer procedure posts
8378 * async request to debugger
8379 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8380 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008381 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 */
8383
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008384 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008385make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008386{
K.Takata76687d22022-01-25 10:31:37 +00008387 TOOLINFOW *pti;
8388 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008389
K.Takata76687d22022-01-25 10:31:37 +00008390 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008391 if (pti == NULL)
8392 return;
8393
8394 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8395 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8396 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008397 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008398
8399 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8400 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8401
K.Takata76687d22022-01-25 10:31:37 +00008402 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008403 pti->uFlags = TTF_SUBCLASS;
8404 pti->hwnd = beval->target;
8405 pti->hinst = 0; // Don't use string resources
8406 pti->uId = ID_BEVAL_TOOLTIP;
8407
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008408 pti->lpszText = LPSTR_TEXTCALLBACKW;
8409 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8410 pti->lParam = (LPARAM)beval->tofree;
8411 // switch multiline tooltips on
8412 if (GetClientRect(s_textArea, &rect))
8413 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8414 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008415
8416 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8417 pti->rect.left = pt.x - 3;
8418 pti->rect.top = pt.y - 3;
8419 pti->rect.right = pt.x + 3;
8420 pti->rect.bottom = pt.y + 3;
8421
8422 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8423 // Make tooltip appear sooner.
8424 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8425 // I've performed some tests and it seems the longest possible life time
8426 // of tooltip is 30 seconds.
8427 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8428 /*
8429 * HACK: force tooltip to appear, because it'll not appear until
8430 * first mouse move. D*mn M$
8431 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8432 */
8433 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8434 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8435 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008436}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008437
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008439delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008441 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442}
8443
8444 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008445beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008446 HWND hwnd UNUSED,
8447 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008448 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008449 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 POINT pt;
8452 RECT rect;
8453
8454 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8455 return;
8456
8457 GetCursorPos(&pt);
8458 if (WindowFromPoint(pt) != s_textArea)
8459 return;
8460
8461 ScreenToClient(s_textArea, &pt);
8462 GetClientRect(s_textArea, &rect);
8463 if (!PtInRect(&rect, pt))
8464 return;
8465
K.Takataa8ec4912022-02-03 14:32:33 +00008466 if (last_user_activity > 0
8467 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 && (cur_beval->showState != ShS_PENDING
8469 || abs(cur_beval->x - pt.x) > 3
8470 || abs(cur_beval->y - pt.y) > 3))
8471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008472 // Pointer resting in one place long enough, it's time to show
8473 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 cur_beval->showState = ShS_PENDING;
8475 cur_beval->x = pt.x;
8476 cur_beval->y = pt.y;
8477
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 if (cur_beval->msgCB != NULL)
8479 (*cur_beval->msgCB)(cur_beval, 0);
8480 }
8481}
8482
8483 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008484gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
K.Takataa8ec4912022-02-03 14:32:33 +00008486 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487}
8488
8489 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008490gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 if (beval == NULL)
8493 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008494 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8495 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
8497
8498 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008499gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008502
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008503 vim_free(beval->msg);
8504 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8505 if (beval->msg == NULL)
8506 {
8507 delete_tooltip(beval);
8508 beval->showState = ShS_NEUTRAL;
8509 return;
8510 }
8511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (beval->showState == ShS_SHOWING)
8513 return;
8514 GetCursorPos(&pt);
8515 ScreenToClient(s_textArea, &pt);
8516
8517 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008519 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 gui_mch_disable_beval_area(cur_beval);
8521 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008522 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524}
8525
8526 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008527gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008528 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008529 char_u *mesg,
8530 void (*mesgCB)(BalloonEval *, int),
8531 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008533 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 BalloonEval *beval;
8535
8536 if (mesg != NULL && mesgCB != NULL)
8537 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008538 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 return NULL;
8540 }
8541
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008542 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (beval != NULL)
8544 {
8545 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546
8547 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 beval->msg = mesg;
8549 beval->msgCB = mesgCB;
8550 beval->clientData = clientData;
8551
8552 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 cur_beval = beval;
8554
8555 if (p_beval)
8556 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 }
8558 return beval;
8559}
8560
8561 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008562Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008564 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 return;
8566
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008567 if (cur_beval == NULL)
8568 return;
8569
8570 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008572 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008573 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008574 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 delete_tooltip(cur_beval);
8576 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577
8578 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008579 break;
8580 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008581 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008582 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008583 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008584 info->lpszText = (LPSTR)info->lParam;
8585 info->uFlags |= TTF_DI_SETITEM;
8586 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008587 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008588 case TTN_GETDISPINFOW:
8589 {
8590 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008591 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008592 info->lpszText = (LPWSTR)info->lParam;
8593 info->uFlags |= TTF_DI_SETITEM;
8594 }
8595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597}
8598
8599 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008600track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8603 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008604 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605}
8606
8607 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008608gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008610# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008611 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008612# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008613 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 vim_free(beval);
8615}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008616#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
8618#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8619/*
8620 * We have multiple signs to draw at the same location. Draw the
8621 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8622 */
8623 void
8624netbeans_draw_multisign_indicator(int row)
8625{
8626 int i;
8627 int y;
8628 int x;
8629
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008630 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008631 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 x = 0;
8634 y = TEXT_Y(row);
8635
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008636# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008637 if (IS_ENABLE_DIRECTX())
8638 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008639# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 for (i = 0; i < gui.char_height - 3; i++)
8642 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8643
8644 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8645 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8646 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8647 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8648 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8649 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8650 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8651}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008652#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008653
8654#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008655
Christopher Plewright20b795e2022-12-20 20:01:58 +00008656// TODO: at the moment, this is just a copy of test_gui_mouse_event.
8657// But, we could instead generate actual Win32 mouse event messages,
8658// ie. to make it consistent wih test_gui_w32_sendevent_keyboard.
8659 static int
8660test_gui_w32_sendevent_mouse(dict_T *args)
8661{
8662 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008663 return FALSE;
8664
Christopher Plewright20b795e2022-12-20 20:01:58 +00008665 // Note: "move" is optional, requires fewer arguments
8666 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008667
Christopher Plewright20b795e2022-12-20 20:01:58 +00008668 if (!move && (!dict_has_key(args, "button")
8669 || !dict_has_key(args, "multiclick")
8670 || !dict_has_key(args, "modifiers")))
8671 return FALSE;
8672
8673 int row = (int)dict_get_number(args, "row");
8674 int col = (int)dict_get_number(args, "col");
8675
8676 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008677 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00008678 // the "move" argument expects row and col coordnates to be in pixels,
8679 // unless "cell" is specified and is TRUE.
8680 if (dict_get_bool(args, "cell", FALSE))
8681 {
8682 // calculate the middle of the character cell
8683 // Note: Cell coordinates are 1-based from vimscript
8684 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
8685 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
8686 gui_mouse_moved(pX, pY);
8687 }
8688 else
8689 gui_mouse_moved(col, row);
8690 }
8691 else
8692 {
8693 int button = (int)dict_get_number(args, "button");
8694 int repeated_click = (int)dict_get_number(args, "multiclick");
8695 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008696
Christopher Plewright20b795e2022-12-20 20:01:58 +00008697 // Reset the scroll values to known values.
8698 // XXX: Remove this when/if the scroll step is made configurable.
8699 mouse_set_hor_scroll_step(6);
8700 mouse_set_vert_scroll_step(3);
8701
8702 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
8703 repeated_click, mods);
8704 }
8705 return TRUE;
8706}
8707
8708 static int
8709test_gui_w32_sendevent_keyboard(dict_T *args)
8710{
8711 INPUT inputs[1];
8712 INPUT modkeys[3];
8713 SecureZeroMemory(inputs, sizeof(INPUT));
8714 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
8715
8716 char_u *event = dict_get_string(args, "event", TRUE);
8717
8718 if (event && (STRICMP(event, "keydown") == 0
8719 || STRICMP(event, "keyup") == 0))
8720 {
8721 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008722 if (vkCode <= 0 || vkCode >= 0xFF)
8723 {
8724 semsg(_(e_invalid_argument_nr), (long)vkCode);
8725 return FALSE;
8726 }
8727
Christopher Plewright20b795e2022-12-20 20:01:58 +00008728 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
8729 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
8730 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
8731 || vkCode == VK_LMENU || vkCode == VK_RMENU );
8732
8733 BOOL unwrapMods = FALSE;
8734 int mods = (int)dict_get_number(args, "modifiers");
8735
8736 // If there are modifiers in the args, and it is not a keyup event and
8737 // vkCode is not a modifier key, then we generate virtual modifier key
8738 // messages before sending the actual key message.
8739 if(mods && STRICMP(event, "keydown") == 0 && !isModKey)
8740 {
8741 int n = 0;
8742 if (mods & MOD_MASK_SHIFT)
8743 {
8744 modkeys[n].type = INPUT_KEYBOARD;
8745 modkeys[n].ki.wVk = VK_LSHIFT;
8746 n++;
8747 }
8748 if (mods & MOD_MASK_CTRL)
8749 {
8750 modkeys[n].type = INPUT_KEYBOARD;
8751 modkeys[n].ki.wVk = VK_LCONTROL;
8752 n++;
8753 }
8754 if (mods & MOD_MASK_ALT)
8755 {
8756 modkeys[n].type = INPUT_KEYBOARD;
8757 modkeys[n].ki.wVk = VK_LMENU;
8758 n++;
8759 }
8760 if (n)
8761 {
8762 (void)SetForegroundWindow(s_hwnd);
8763 SendInput(n, modkeys, sizeof(INPUT));
8764 }
8765 }
8766
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008767 inputs[0].type = INPUT_KEYBOARD;
8768 inputs[0].ki.wVk = vkCode;
8769 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008770 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008771 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Christopher Plewright20b795e2022-12-20 20:01:58 +00008772 if(!isModKey)
8773 unwrapMods = TRUE;
8774 }
8775
K.Takatafef38d82022-09-07 19:03:42 +01008776 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008777 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00008778 vim_free(event);
8779
8780 if (unwrapMods)
8781 {
8782 modkeys[0].type = INPUT_KEYBOARD;
8783 modkeys[0].ki.wVk = VK_LSHIFT;
8784 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
8785
8786 modkeys[1].type = INPUT_KEYBOARD;
8787 modkeys[1].ki.wVk = VK_LCONTROL;
8788 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
8789
8790 modkeys[2].type = INPUT_KEYBOARD;
8791 modkeys[2].ki.wVk = VK_LMENU;
8792 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
8793
8794 (void)SetForegroundWindow(s_hwnd);
8795 SendInput(3, modkeys, sizeof(INPUT));
8796 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008797 }
8798 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00008799 {
8800 if (event == NULL)
8801 {
8802 semsg(_(e_missing_argument_str), "event");
8803 }
8804 else
8805 {
8806 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8807 vim_free(event);
8808 }
8809 return FALSE;
8810 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008811 return TRUE;
8812}
Christopher Plewright20b795e2022-12-20 20:01:58 +00008813
8814 int
8815test_gui_w32_sendevent(char_u *event, dict_T *args)
8816{
8817 if (STRICMP(event, "key") == 0)
8818 return test_gui_w32_sendevent_keyboard(args);
8819 else if (STRICMP(event, "mouse") == 0)
8820 return test_gui_w32_sendevent_mouse(args);
8821 else
8822 {
8823 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8824 return FALSE;
8825 }
8826}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008827#endif