blob: eb4c841388e9a3a8c7b1464fea35328e5ed77940 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Anton Sharonov3f026672022-07-26 21:26:18 +010032// values for "dead_key"
33#define DEAD_KEY_OFF 0 // no dead key
34#define DEAD_KEY_SET_DEFAULT 1 // dead key pressed
35#define DEAD_KEY_TRANSIENT_IN_ON_CHAR 2 // wait for next key press
36#define DEAD_KEY_SKIP_ON_CHAR 3 // skip next _OnChar()
37
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010038#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020039static DWriteContext *s_dwc = NULL;
40static int s_directx_enabled = 0;
41static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010042# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010043static int directx_enabled(void);
44static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010045#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020046
Bram Moolenaar065bbac2016-02-20 13:08:46 +010047#ifdef FEAT_MENU
48static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000049#else
50# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010051#endif
52
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
54 int
55gui_mch_set_rendering_options(char_u *s)
56{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010057# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020058 char_u *p, *q;
59
60 int dx_enable = 0;
61 int dx_flags = 0;
62 float dx_gamma = 0.0f;
63 float dx_contrast = 0.0f;
64 float dx_level = 0.0f;
65 int dx_geom = 0;
66 int dx_renmode = 0;
67 int dx_taamode = 0;
68
Bram Moolenaar734a8672019-12-02 22:49:38 +010069 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020070 for (p = s; p != NULL && *p != NUL; )
71 {
72 char_u item[256];
73 char_u name[128];
74 char_u value[128];
75
Bram Moolenaarcde88542015-08-11 19:14:00 +020076 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020077 if (p == NULL)
78 break;
79 q = &item[0];
80 copy_option_part(&q, name, sizeof(name), ":");
81 if (q == NULL)
82 return FAIL;
83 copy_option_part(&q, value, sizeof(value), ":");
84
85 if (STRCMP(name, "type") == 0)
86 {
87 if (STRCMP(value, "directx") == 0)
88 dx_enable = 1;
89 else
90 return FAIL;
91 }
92 else if (STRCMP(name, "gamma") == 0)
93 {
94 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "contrast") == 0)
98 {
99 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "level") == 0)
103 {
104 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 }
107 else if (STRCMP(name, "geom") == 0)
108 {
109 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100110 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200111 if (dx_geom < 0 || dx_geom > 2)
112 return FAIL;
113 }
114 else if (STRCMP(name, "renmode") == 0)
115 {
116 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100117 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200118 if (dx_renmode < 0 || dx_renmode > 6)
119 return FAIL;
120 }
121 else if (STRCMP(name, "taamode") == 0)
122 {
123 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100124 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200125 if (dx_taamode < 0 || dx_taamode > 3)
126 return FAIL;
127 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100128 else if (STRCMP(name, "scrlines") == 0)
129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100130 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100131 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 else
133 return FAIL;
134 }
135
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100136 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100137 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100138
Bram Moolenaar734a8672019-12-02 22:49:38 +0100139 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200140 if (dx_enable)
141 {
142 if (!directx_enabled())
143 return FAIL;
144 DWriteContext_SetRenderingParams(s_dwc, NULL);
145 if (dx_flags)
146 {
147 DWriteRenderingParams param;
148 DWriteContext_GetRenderingParams(s_dwc, &param);
149 if (dx_flags & (1 << 0))
150 param.gamma = dx_gamma;
151 if (dx_flags & (1 << 1))
152 param.enhancedContrast = dx_contrast;
153 if (dx_flags & (1 << 2))
154 param.clearTypeLevel = dx_level;
155 if (dx_flags & (1 << 3))
156 param.pixelGeometry = dx_geom;
157 if (dx_flags & (1 << 4))
158 param.renderingMode = dx_renmode;
159 if (dx_flags & (1 << 5))
160 param.textAntialiasMode = dx_taamode;
161 DWriteContext_SetRenderingParams(s_dwc, &param);
162 }
163 }
164 s_directx_enabled = dx_enable;
165
166 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100167# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200168 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100169# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200170}
171#endif
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
174 * These are new in Windows ME/XP, only defined in recent compilers.
175 */
176#ifndef HANDLE_WM_XBUTTONUP
177# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180#ifndef HANDLE_WM_XBUTTONDOWN
181# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
182 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
183#endif
184#ifndef HANDLE_WM_XBUTTONDBLCLK
185# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
186 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
187#endif
188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100189
Bram Moolenaar734a8672019-12-02 22:49:38 +0100190#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100191#ifdef DEBUG
192# include <tchar.h>
193#endif
194
Bram Moolenaar734a8672019-12-02 22:49:38 +0100195// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100196#ifndef PROTO
197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifndef __MINGW32__
199# include <shellapi.h>
200# endif
K.Takata2da11a42022-09-10 13:03:12 +0100201# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100202# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000218#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
K.Takatac81e9bf2022-01-16 14:15:49 +0000225#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100226# define WM_DPICHANGED 0x02E0
227#endif
228
Ken Takata7086b3e2023-10-02 21:26:03 +0200229#ifndef WM_GETDPISCALEDSIZE
230# define WM_GETDPISCALEDSIZE 0x02E4
231#endif
232
LemonBoy365d8f72022-05-05 19:23:07 +0100233#ifndef WM_MOUSEHWHEEL
234# define WM_MOUSEHWHEEL 0x020E
235#endif
236
237#ifndef SPI_GETWHEELSCROLLCHARS
238# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000239#endif
240
LemonBoyc27747e2022-05-07 12:25:40 +0100241#ifndef SPI_SETWHEELSCROLLCHARS
242# define SPI_SETWHEELSCROLLCHARS 0x006D
243#endif
244
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247 * Define a few things for generating prototypes. This is just to avoid
248 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100250# define APIENTRY
251# define CALLBACK
252# define CONST
253# define FAR
254# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200255# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200256# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100257# define _cdecl
258typedef int BOOL;
259typedef int BYTE;
260typedef int DWORD;
261typedef int WCHAR;
262typedef int ENUMLOGFONT;
263typedef int FINDREPLACE;
264typedef int HANDLE;
265typedef int HBITMAP;
266typedef int HBRUSH;
267typedef int HDROP;
268typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100269typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100270typedef int LPARAM;
271typedef int LPCREATESTRUCT;
272typedef int LPCSTR;
273typedef int LPCTSTR;
274typedef int LPRECT;
275typedef int LPSTR;
276typedef int LPWINDOWPOS;
277typedef int LPWORD;
278typedef int LRESULT;
279typedef int HRESULT;
280# undef MSG
281typedef int MSG;
282typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100283typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284typedef int OSVERSIONINFO;
285typedef int PWORD;
286typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100287typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288typedef int UINT;
289typedef int WORD;
290typedef int WPARAM;
291typedef int POINT;
292typedef void *HINSTANCE;
293typedef void *HMENU;
294typedef void *HWND;
295typedef void *HDC;
296typedef void VOID;
297typedef int LPNMHDR;
298typedef int LONG;
299typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200300typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200301typedef int COLORREF;
302typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303#endif
304
K.Takata45f9cfb2022-01-21 11:11:00 +0000305static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100306static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307static void clear_rect(RECT *rcp);
308
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static WORD s_dlgfntheight; // height of the dialog font
310static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100311
312#ifdef FEAT_MENU
313static HMENU s_menuBar = NULL;
314#endif
315#ifdef FEAT_TEAROFF
316static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100317static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318#endif
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320// Flag that is set while processing a message that must not be interrupted by
321// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322static int s_busy_processing = FALSE;
323
Bram Moolenaar734a8672019-12-02 22:49:38 +0100324static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325
326#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000327static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200328static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200330static int s_findrep_is_find; // TRUE for find dialog, FALSE
331 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100332#endif
333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100334HWND s_hwnd = NULL;
335static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100336static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100337
338#ifdef FEAT_TOOLBAR
339static HWND s_toolbarhwnd = NULL;
340static WNDPROC s_toolbar_wndproc = NULL;
341#endif
342
343#ifdef FEAT_GUI_TABLINE
344static HWND s_tabhwnd = NULL;
345static WNDPROC s_tabline_wndproc = NULL;
346static int showing_tabline = 0;
347#endif
348
349static WPARAM s_wParam = 0;
350static LPARAM s_lParam = 0;
351
352static HWND s_textArea = NULL;
353static UINT s_uMsg = 0;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100356
357static int s_need_activate = FALSE;
358
Bram Moolenaar734a8672019-12-02 22:49:38 +0100359// This variable is set when waiting for an event, which is the only moment
360// scrollbar dragging can be done directly. It's not allowed while commands
361// are executed, because it may move the cursor and that may cause unexpected
362// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100363static int allow_scrollbar = FALSE;
364
K.Takatac81e9bf2022-01-16 14:15:49 +0000365#ifndef _DPI_AWARENESS_CONTEXTS_
366typedef HANDLE DPI_AWARENESS_CONTEXT;
367
368typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000369 DPI_AWARENESS_INVALID = -1,
370 DPI_AWARENESS_UNAWARE = 0,
371 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000372 DPI_AWARENESS_PER_MONITOR_AWARE = 2
373} DPI_AWARENESS;
374
K.Takatab0b2b732022-01-19 12:59:21 +0000375# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
376# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000377# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
378# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
379# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
380#endif
381
382#define DEFAULT_DPI 96
383static int s_dpi = DEFAULT_DPI;
384static BOOL s_in_dpichanged = FALSE;
385static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
Ken Takata7086b3e2023-10-02 21:26:03 +0200386static RECT s_suggested_rect;
K.Takatac81e9bf2022-01-16 14:15:49 +0000387
388static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
389static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
390static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
391//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
392static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
393static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
394
395 static UINT WINAPI
396stubGetDpiForSystem(void)
397{
398 HWND hwnd = GetDesktopWindow();
399 HDC hdc = GetWindowDC(hwnd);
400 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
401 ReleaseDC(hwnd, hdc);
402 return dpi;
403}
404
405 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100406stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000407{
408 return GetSystemMetrics(nIndex);
409}
410
411 static int
412adjust_fontsize_by_dpi(int size)
413{
414 return size * s_dpi / (int)pGetDpiForSystem();
415}
416
417 static int
418adjust_by_system_dpi(int size)
419{
420 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
421}
422
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100423#if defined(FEAT_DIRECTX)
424 static int
425directx_enabled(void)
426{
427 if (s_dwc != NULL)
428 return 1;
429 else if (s_directx_load_attempted)
430 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100431 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100432 DWrite_Init();
433 s_directx_load_attempted = 1;
434 s_dwc = DWriteContext_Open();
435 directx_binddc();
436 return s_dwc != NULL ? 1 : 0;
437}
438
439 static void
440directx_binddc(void)
441{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000442 if (s_textArea == NULL)
443 return;
444
445 RECT rect;
446 GetClientRect(s_textArea, &rect);
447 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100448}
449#endif
450
Bram Moolenaar734a8672019-12-02 22:49:38 +0100451extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100452
453static struct
454{
455 UINT key_sym;
456 char_u vim_code0;
457 char_u vim_code1;
458} special_keys[] =
459{
460 {VK_UP, 'k', 'u'},
461 {VK_DOWN, 'k', 'd'},
462 {VK_LEFT, 'k', 'l'},
463 {VK_RIGHT, 'k', 'r'},
464
465 {VK_F1, 'k', '1'},
466 {VK_F2, 'k', '2'},
467 {VK_F3, 'k', '3'},
468 {VK_F4, 'k', '4'},
469 {VK_F5, 'k', '5'},
470 {VK_F6, 'k', '6'},
471 {VK_F7, 'k', '7'},
472 {VK_F8, 'k', '8'},
473 {VK_F9, 'k', '9'},
474 {VK_F10, 'k', ';'},
475
476 {VK_F11, 'F', '1'},
477 {VK_F12, 'F', '2'},
478 {VK_F13, 'F', '3'},
479 {VK_F14, 'F', '4'},
480 {VK_F15, 'F', '5'},
481 {VK_F16, 'F', '6'},
482 {VK_F17, 'F', '7'},
483 {VK_F18, 'F', '8'},
484 {VK_F19, 'F', '9'},
485 {VK_F20, 'F', 'A'},
486
487 {VK_F21, 'F', 'B'},
488#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100489 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100490#endif
491 {VK_F22, 'F', 'C'},
492 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100493 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494
495 {VK_HELP, '%', '1'},
496 {VK_BACK, 'k', 'b'},
497 {VK_INSERT, 'k', 'I'},
498 {VK_DELETE, 'k', 'D'},
499 {VK_HOME, 'k', 'h'},
500 {VK_END, '@', '7'},
501 {VK_PRIOR, 'k', 'P'},
502 {VK_NEXT, 'k', 'N'},
503 {VK_PRINT, '%', '9'},
504 {VK_ADD, 'K', '6'},
505 {VK_SUBTRACT, 'K', '7'},
506 {VK_DIVIDE, 'K', '8'},
507 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100508 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100509 {VK_DECIMAL, 'K', 'B'},
510
511 {VK_NUMPAD0, 'K', 'C'},
512 {VK_NUMPAD1, 'K', 'D'},
513 {VK_NUMPAD2, 'K', 'E'},
514 {VK_NUMPAD3, 'K', 'F'},
515 {VK_NUMPAD4, 'K', 'G'},
516 {VK_NUMPAD5, 'K', 'H'},
517 {VK_NUMPAD6, 'K', 'I'},
518 {VK_NUMPAD7, 'K', 'J'},
519 {VK_NUMPAD8, 'K', 'K'},
520 {VK_NUMPAD9, 'K', 'L'},
521
Bram Moolenaar734a8672019-12-02 22:49:38 +0100522 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523 {VK_SPACE, ' ', NUL},
524 {VK_TAB, TAB, NUL},
525 {VK_ESCAPE, ESC, NUL},
526 {NL, NL, NUL},
527 {CAR, CAR, NUL},
528
Bram Moolenaar734a8672019-12-02 22:49:38 +0100529 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100530 {0, 0, 0}
531};
532
Bram Moolenaar734a8672019-12-02 22:49:38 +0100533// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100534static int s_button_pending = -1;
535
Bram Moolenaar734a8672019-12-02 22:49:38 +0100536// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
537// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100538static int s_getting_focus = FALSE;
539
540static int s_x_pending;
541static int s_y_pending;
542static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000543static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100544static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100545static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200546static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
547 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100549#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100550// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100551static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000552static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100553#endif
554
555/*
556 * For control IME.
557 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100558 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100559 */
K.Takata4ac893f2022-01-20 12:44:28 +0000560#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100561// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100562static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100563// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100564static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100565#endif
566
567#ifdef FEAT_MBYTE_IME
568static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
569#endif
570
571#if defined(FEAT_BROWSE)
572static char_u *convert_filter(char_u *s);
573#endif
574
575#ifdef DEBUG_PRINT_ERROR
576/*
577 * Print out the last Windows error message
578 */
579 static void
580print_windows_error(void)
581{
582 LPVOID lpMsgBuf;
583
584 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
585 NULL, GetLastError(),
586 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
587 (LPTSTR) &lpMsgBuf, 0, NULL);
588 TRACE1("Error: %s\n", lpMsgBuf);
589 LocalFree(lpMsgBuf);
590}
591#endif
592
593/*
594 * Cursor blink functions.
595 *
596 * This is a simple state machine:
597 * BLINK_NONE not blinking at all
598 * BLINK_OFF blinking, cursor is not shown
599 * BLINK_ON blinking, cursor is shown
600 */
601
602#define BLINK_NONE 0
603#define BLINK_OFF 1
604#define BLINK_ON 2
605
606static int blink_state = BLINK_NONE;
607static long_u blink_waittime = 700;
608static long_u blink_ontime = 400;
609static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000610static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100611
Bram Moolenaar703a8042016-06-04 16:24:32 +0200612 int
613gui_mch_is_blinking(void)
614{
615 return blink_state != BLINK_NONE;
616}
617
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200618 int
619gui_mch_is_blink_off(void)
620{
621 return blink_state == BLINK_OFF;
622}
623
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100624 void
625gui_mch_set_blinking(long wait, long on, long off)
626{
627 blink_waittime = wait;
628 blink_ontime = on;
629 blink_offtime = off;
630}
631
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100632 static VOID CALLBACK
633_OnBlinkTimer(
634 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100635 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000636 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100637 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638{
639 MSG msg;
640
641 /*
642 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
643 */
644
645 KillTimer(NULL, idEvent);
646
Bram Moolenaar734a8672019-12-02 22:49:38 +0100647 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000648 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100649 ;
650
651 if (blink_state == BLINK_ON)
652 {
653 gui_undraw_cursor();
654 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000655 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100656 }
657 else
658 {
659 gui_update_cursor(TRUE, FALSE);
660 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000661 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100662 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100663 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100664}
665
666 static void
667gui_mswin_rm_blink_timer(void)
668{
669 MSG msg;
670
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000671 if (blink_timer == 0)
672 return;
673
674 KillTimer(NULL, blink_timer);
675 // Eat spurious WM_TIMER messages
676 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
677 ;
678 blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679}
680
681/*
682 * Stop the cursor blinking. Show the cursor if it wasn't shown.
683 */
684 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100685gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686{
687 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100688 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100689 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100691 gui_mch_flush();
692 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100693 blink_state = BLINK_NONE;
694}
695
696/*
697 * Start the cursor blinking. If it was already blinking, this restarts the
698 * waiting time and shows the cursor.
699 */
700 void
701gui_mch_start_blink(void)
702{
703 gui_mswin_rm_blink_timer();
704
Bram Moolenaar734a8672019-12-02 22:49:38 +0100705 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
707 {
K.Takataa8ec4912022-02-03 14:32:33 +0000708 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 blink_state = BLINK_ON;
710 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100711 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 }
713}
714
715/*
716 * Call-back routines.
717 */
718
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719 static VOID CALLBACK
720_OnTimer(
721 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100722 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000723 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100724 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100725{
726 MSG msg;
727
728 /*
729 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
730 */
731 KillTimer(NULL, idEvent);
732 s_timed_out = TRUE;
733
Bram Moolenaar734a8672019-12-02 22:49:38 +0100734 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000735 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736 ;
737 if (idEvent == s_wait_timer)
738 s_wait_timer = 0;
739}
740
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741 static void
742_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100743 HWND hwnd UNUSED,
744 UINT ch UNUSED,
745 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100746{
747 dead_key = 1;
748}
749
750/*
751 * Convert Unicode character "ch" to bytes in "string[slen]".
752 * When "had_alt" is TRUE the ALT key was included in "ch".
753 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200754 * Because the Windows API uses UTF-16, we have to deal with surrogate
755 * pairs; this is where we choose to deal with them: if "ch" is a high
756 * surrogate, it will be stored, and the length returned will be zero; the next
757 * char_to_string call will then include the high surrogate, decoding the pair
758 * of UTF-16 code units to a single Unicode code point, presuming it is the
759 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 */
761 static int
762char_to_string(int ch, char_u *string, int slen, int had_alt)
763{
764 int len;
765 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100766 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200767 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100768
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200769 if (surrogate_pending_ch != 0)
770 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100771 // We don't guarantee ch is a low surrogate to match the high surrogate
772 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200773 wstring[0] = surrogate_pending_ch;
774 wstring[1] = ch;
775 surrogate_pending_ch = 0;
776 len = 2;
777 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100778 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // We don't have the entire code point yet, only the first UTF-16 code
781 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200782 surrogate_pending_ch = ch;
783 return 0;
784 }
785 else
786 {
787 wstring[0] = ch;
788 len = 1;
789 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790
Bram Moolenaar734a8672019-12-02 22:49:38 +0100791 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
792 // "enc_codepage" is non-zero use the standard Win32 function,
793 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200794 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100795 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
797 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100798 // If we had included the ALT key into the character but now the
799 // upper bit is no longer set, that probably means the conversion
800 // failed. Convert the original character and set the upper bit
801 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200804 wstring[0] = ch & 0x7f;
805 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
806 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100807 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100809 }
810 }
811 else
812 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 ws = utf16_to_enc(wstring, &len);
814 if (ws == NULL)
815 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816 else
817 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100818 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200819 len = slen;
820 mch_memmove(string, ws, len);
821 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822 }
823 }
824
825 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100826 {
827 string[0] = ch;
828 len = 1;
829 }
830
831 for (i = 0; i < len; ++i)
832 if (string[i] == CSI && len <= slen - 2)
833 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100834 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100835 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
836 string[++i] = KS_EXTRA;
837 string[++i] = (int)KE_CSI;
838 len += 2;
839 }
840
841 return len;
842}
843
LemonBoy45684c62022-04-24 15:46:42 +0100844 static int
845get_active_modifiers(void)
846{
847 int modifiers = 0;
848
849 if (GetKeyState(VK_CONTROL) & 0x8000)
850 modifiers |= MOD_MASK_CTRL;
851 if (GetKeyState(VK_SHIFT) & 0x8000)
852 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100853 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
854 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100855 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100856 if (GetKeyState(VK_LMENU) & 0x8000)
857 modifiers |= MOD_MASK_ALT;
858 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
859 modifiers &= ~MOD_MASK_CTRL;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100860 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
861 // is pressed, Windows claims that Ctrl is hold as well. That way we can
862 // recognize Right-ALT alone and be sure that not AltGr is hold.
863 if (!(GetKeyState(VK_CONTROL) & 0x8000)
864 && (GetKeyState(VK_RMENU) & 0x8000)
865 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
866 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100867
868 return modifiers;
869}
870
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100871/*
872 * Key hit, add it to the input buffer.
873 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100874 static void
875_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100876 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100877 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100878 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879{
880 char_u string[40];
881 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100882 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100883 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100884
Anton Sharonov3f026672022-07-26 21:26:18 +0100885 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
886 return;
887
888 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
889 // process_message()
890 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
891 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100892
LemonBoy45684c62022-04-24 15:46:42 +0100893 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100894
895 ch = simplify_key(ch, &modifiers);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000896
Christopher Plewright7b0afc12022-12-30 16:54:58 +0000897 // Some keys need adjustment when the Ctrl modifier is used.
898 ++no_reduce_keys;
899 ch = may_adjust_key_for_ctrl(modifiers, ch);
900 --no_reduce_keys;
901
LemonBoy77fc0b02022-04-22 22:45:52 +0100902 // remove the SHIFT modifier for keys where it's already included, e.g.,
903 // '(' and '*'
904 modifiers = may_remove_shift_modifier(modifiers, ch);
905
906 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
907 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
908 if (ch == CSI)
909 ch = K_CSI;
910
911 if (modifiers)
912 {
913 string[0] = CSI;
914 string[1] = KS_MODIFIER;
915 string[2] = modifiers;
916 add_to_input_buf(string, 3);
917 }
918
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100919 len = char_to_string(ch, string, 40, FALSE);
920 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
921 {
922 trash_input_buf();
923 got_int = TRUE;
924 }
925
926 add_to_input_buf(string, len);
927}
928
929/*
930 * Alt-Key hit, add it to the input buffer.
931 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 static void
933_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100934 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100935 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100936 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100937{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100938 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100939 int len;
940 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100941 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100942
Anton Sharonov3f026672022-07-26 21:26:18 +0100943 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944
Bram Moolenaar734a8672019-12-02 22:49:38 +0100945 // OK, we have a character key (given by ch) which was entered with the
946 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
947 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
948 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100949 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100951 // remove the SHIFT modifier for keys where it's already included, e.g.,
952 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200953 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100954
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200955 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
956 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100957 if (ch == CSI)
958 ch = K_CSI;
959
960 len = 0;
961 if (modifiers)
962 {
963 string[len++] = CSI;
964 string[len++] = KS_MODIFIER;
965 string[len++] = modifiers;
966 }
967
968 if (IS_SPECIAL((int)ch))
969 {
970 string[len++] = CSI;
971 string[len++] = K_SECOND((int)ch);
972 string[len++] = K_THIRD((int)ch);
973 }
974 else
975 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100976 // Although the documentation isn't clear about it, we assume "ch" is
977 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100978 len += char_to_string(ch, string + len, 40 - len, TRUE);
979 }
980
981 add_to_input_buf(string, len);
982}
983
984 static void
985_OnMouseEvent(
986 int button,
987 int x,
988 int y,
989 int repeated_click,
990 UINT keyFlags)
991{
992 int vim_modifiers = 0x0;
993
994 s_getting_focus = FALSE;
995
996 if (keyFlags & MK_SHIFT)
997 vim_modifiers |= MOUSE_SHIFT;
998 if (keyFlags & MK_CONTROL)
999 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +01001000 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 vim_modifiers |= MOUSE_ALT;
1002
1003 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1004}
1005
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001006 static void
1007_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001008 HWND hwnd UNUSED,
1009 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001010 int x,
1011 int y,
1012 UINT keyFlags)
1013{
1014 static LONG s_prevTime = 0;
1015
1016 LONG currentTime = GetMessageTime();
1017 int button = -1;
1018 int repeated_click;
1019
Bram Moolenaar734a8672019-12-02 22:49:38 +01001020 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001021 (void)SetFocus(s_hwnd);
1022
1023 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1024 button = MOUSE_LEFT;
1025 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1026 button = MOUSE_MIDDLE;
1027 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1028 button = MOUSE_RIGHT;
1029 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1030 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001031 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1032 }
1033 else if (s_uMsg == WM_CAPTURECHANGED)
1034 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001035 // on W95/NT4, somehow you get in here with an odd Msg
1036 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001037 if (s_button_pending == MOUSE_LEFT)
1038 button = MOUSE_RIGHT;
1039 else
1040 button = MOUSE_LEFT;
1041 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001042
1043 if (button < 0)
1044 return;
1045
1046 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1047
1048 /*
1049 * Holding down the left and right buttons simulates pushing the middle
1050 * button.
1051 */
1052 if (repeated_click
1053 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1054 || (button == MOUSE_RIGHT
1055 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001056 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001058 * Hmm, gui.c will ignore more than one button down at a time, so
1059 * pretend we let go of it first.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001060 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001061 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1062 button = MOUSE_MIDDLE;
1063 repeated_click = FALSE;
1064 s_button_pending = -1;
1065 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001066 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001067 else if ((repeated_click)
1068 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1069 {
1070 if (s_button_pending > -1)
1071 {
1072 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1073 s_button_pending = -1;
1074 }
1075 // TRACE("Button down at x %d, y %d\n", x, y);
1076 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1077 }
1078 else
1079 {
1080 /*
1081 * If this is the first press (i.e. not a multiple click) don't
1082 * action immediately, but store and wait for:
1083 * i) button-up
1084 * ii) mouse move
1085 * iii) another button press
1086 * before using it.
1087 * This enables us to make left+right simulate middle button,
1088 * without left or right being actioned first. The side-effect is
1089 * that if you click and hold the mouse without dragging, the
1090 * cursor doesn't move until you release the button. In practice
1091 * this is hardly a problem.
1092 */
1093 s_button_pending = button;
1094 s_x_pending = x;
1095 s_y_pending = y;
1096 s_kFlags_pending = keyFlags;
1097 }
1098
1099 s_prevTime = currentTime;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001100}
1101
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102 static void
1103_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001104 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105 int x,
1106 int y,
1107 UINT keyFlags)
1108{
1109 int button;
1110
1111 s_getting_focus = FALSE;
1112 if (s_button_pending > -1)
1113 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001114 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001115 _OnMouseEvent(s_button_pending, s_x_pending,
1116 s_y_pending, FALSE, s_kFlags_pending);
1117 s_button_pending = -1;
1118 }
1119 if (s_uMsg == WM_MOUSEMOVE)
1120 {
1121 /*
1122 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1123 * down.
1124 */
1125 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1126 | MK_XBUTTON1 | MK_XBUTTON2)))
1127 {
1128 gui_mouse_moved(x, y);
1129 return;
1130 }
1131
1132 /*
1133 * While button is down, keep grabbing mouse move events when
1134 * the mouse goes outside the window
1135 */
1136 SetCapture(s_textArea);
1137 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001138 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001139 }
1140 else
1141 {
1142 ReleaseCapture();
1143 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001144 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 }
1146
1147 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1148}
1149
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001150 static void
1151_OnSizeTextArea(
1152 HWND hwnd UNUSED,
1153 UINT state UNUSED,
1154 int cx UNUSED,
1155 int cy UNUSED)
1156{
1157#if defined(FEAT_DIRECTX)
1158 if (IS_ENABLE_DIRECTX())
1159 directx_binddc();
1160#endif
1161}
1162
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001163#ifdef FEAT_MENU
1164/*
1165 * Find the vimmenu_T with the given id
1166 */
1167 static vimmenu_T *
1168gui_mswin_find_menu(
1169 vimmenu_T *pMenu,
1170 int id)
1171{
1172 vimmenu_T *pChildMenu;
1173
1174 while (pMenu)
1175 {
1176 if (pMenu->id == (UINT)id)
1177 break;
1178 if (pMenu->children != NULL)
1179 {
1180 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1181 if (pChildMenu)
1182 {
1183 pMenu = pChildMenu;
1184 break;
1185 }
1186 }
1187 pMenu = pMenu->next;
1188 }
1189 return pMenu;
1190}
1191
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 static void
1193_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001194 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001195 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001196 HWND hwndCtl UNUSED,
1197 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198{
1199 vimmenu_T *pMenu;
1200
1201 pMenu = gui_mswin_find_menu(root_menu, id);
1202 if (pMenu)
1203 gui_menu_cb(pMenu);
1204}
1205#endif
1206
1207#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001208/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001209 * Handle a Find/Replace window message.
1210 */
1211 static void
1212_OnFindRepl(void)
1213{
1214 int flags = 0;
1215 int down;
1216
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001217 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001218 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001219 (void)SetFocus(s_hwnd);
1220
1221 if (s_findrep_struct.Flags & FR_FINDNEXT)
1222 {
1223 flags = FRD_FINDNEXT;
1224
Bram Moolenaar734a8672019-12-02 22:49:38 +01001225 // Give main window the focus back: this is so the cursor isn't
1226 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001227 (void)SetFocus(s_hwnd);
1228 }
1229 else if (s_findrep_struct.Flags & FR_REPLACE)
1230 {
1231 flags = FRD_REPLACE;
1232
Bram Moolenaar734a8672019-12-02 22:49:38 +01001233 // Give main window the focus back: this is so the cursor isn't
1234 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 (void)SetFocus(s_hwnd);
1236 }
1237 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1238 {
1239 flags = FRD_REPLACEALL;
1240 }
1241
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001242 if (flags == 0)
1243 return;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001244
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001245 char_u *p, *q;
1246
1247 // Call the generic GUI function to do the actual work.
1248 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1249 flags |= FRD_WHOLE_WORD;
1250 if (s_findrep_struct.Flags & FR_MATCHCASE)
1251 flags |= FRD_MATCH_CASE;
1252 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1253 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1254 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1255 if (p != NULL && q != NULL)
1256 gui_do_findrepl(flags, p, q, down);
1257 vim_free(p);
1258 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001259}
1260#endif
1261
1262 static void
1263HandleMouseHide(UINT uMsg, LPARAM lParam)
1264{
1265 static LPARAM last_lParam = 0L;
1266
Bram Moolenaar734a8672019-12-02 22:49:38 +01001267 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1269 {
1270 if (lParam == last_lParam)
1271 return;
1272 last_lParam = lParam;
1273 }
1274
Bram Moolenaar734a8672019-12-02 22:49:38 +01001275 // Handle specially, to centralise coding. We need to be sure we catch all
1276 // possible events which should cause us to restore the cursor (as it is a
1277 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278 switch (uMsg)
1279 {
1280 case WM_KEYUP:
1281 case WM_CHAR:
1282 /*
1283 * blank out the pointer if necessary
1284 */
1285 if (p_mh)
1286 gui_mch_mousehide(TRUE);
1287 break;
1288
Bram Moolenaar734a8672019-12-02 22:49:38 +01001289 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001290 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001291 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001292 case WM_LBUTTONDOWN:
1293 case WM_LBUTTONUP:
1294 case WM_MBUTTONDOWN:
1295 case WM_MBUTTONUP:
1296 case WM_RBUTTONDOWN:
1297 case WM_RBUTTONUP:
1298 case WM_XBUTTONDOWN:
1299 case WM_XBUTTONUP:
1300 case WM_NCMOUSEMOVE:
1301 case WM_NCLBUTTONDOWN:
1302 case WM_NCLBUTTONUP:
1303 case WM_NCMBUTTONDOWN:
1304 case WM_NCMBUTTONUP:
1305 case WM_NCRBUTTONDOWN:
1306 case WM_NCRBUTTONUP:
1307 case WM_KILLFOCUS:
1308 /*
1309 * if the pointer is currently hidden, then we should show it.
1310 */
1311 gui_mch_mousehide(FALSE);
1312 break;
1313 }
1314}
1315
1316 static LRESULT CALLBACK
1317_TextAreaWndProc(
1318 HWND hwnd,
1319 UINT uMsg,
1320 WPARAM wParam,
1321 LPARAM lParam)
1322{
1323 /*
1324 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1325 hwnd, uMsg, wParam, lParam);
1326 */
1327
1328 HandleMouseHide(uMsg, lParam);
1329
1330 s_uMsg = uMsg;
1331 s_wParam = wParam;
1332 s_lParam = lParam;
1333
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001334#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001335 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001336#endif
1337
1338 switch (uMsg)
1339 {
1340 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1341 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1342 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1343 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1344 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1345 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1346 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1347 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1348 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1349 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1350 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1351 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1352 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1353 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001354 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001355
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001356#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001357 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1358 return TRUE;
1359#endif
1360 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001361 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001362 }
1363}
1364
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001365/*
1366 * Called when the foreground or background color has been changed.
1367 */
1368 void
1369gui_mch_new_colors(void)
1370{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001371 HBRUSH prevBrush;
1372
1373 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001374 prevBrush = (HBRUSH)SetClassLongPtr(
1375 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001376 InvalidateRect(s_hwnd, NULL, TRUE);
1377 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001378}
1379
1380/*
1381 * Set the colors to their default values.
1382 */
1383 void
1384gui_mch_def_colors(void)
1385{
1386 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1387 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1388 gui.def_norm_pixel = gui.norm_pixel;
1389 gui.def_back_pixel = gui.back_pixel;
1390}
1391
1392/*
1393 * Open the GUI window which was created by a call to gui_mch_init().
1394 */
1395 int
1396gui_mch_open(void)
1397{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001398 // Actually open the window, if not already visible
1399 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001400 if (!IsWindowVisible(s_hwnd))
1401 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1402
1403#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001404 // Init replace string here, so that we keep it when re-opening the
1405 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001406 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1407#endif
1408
1409 return OK;
1410}
1411
1412/*
1413 * Get the position of the top left corner of the window.
1414 */
1415 int
1416gui_mch_get_winpos(int *x, int *y)
1417{
1418 RECT rect;
1419
1420 GetWindowRect(s_hwnd, &rect);
1421 *x = rect.left;
1422 *y = rect.top;
1423 return OK;
1424}
1425
1426/*
1427 * Set the position of the top left corner of the window to the given
1428 * coordinates.
1429 */
1430 void
1431gui_mch_set_winpos(int x, int y)
1432{
1433 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1434 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1435}
1436 void
1437gui_mch_set_text_area_pos(int x, int y, int w, int h)
1438{
1439 static int oldx = 0;
1440 static int oldy = 0;
1441
1442 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1443
1444#ifdef FEAT_TOOLBAR
1445 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1446 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001447 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448#endif
1449#if defined(FEAT_GUI_TABLINE)
1450 if (showing_tabline)
1451 {
1452 int top = 0;
1453 RECT rect;
1454
1455# ifdef FEAT_TOOLBAR
1456 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001457 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001458# endif
1459 GetClientRect(s_hwnd, &rect);
1460 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1461 }
1462#endif
1463
Bram Moolenaar734a8672019-12-02 22:49:38 +01001464 // When side scroll bar is unshown, the size of window will change.
1465 // then, the text area move left or right. thus client rect should be
1466 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001467 if (oldx != x || oldy != y)
1468 {
1469 InvalidateRect(s_hwnd, NULL, FALSE);
1470 oldx = x;
1471 oldy = y;
1472 }
1473}
1474
1475
1476/*
1477 * Scrollbar stuff:
1478 */
1479
1480 void
1481gui_mch_enable_scrollbar(
1482 scrollbar_T *sb,
1483 int flag)
1484{
1485 ShowScrollBar(sb->id, SB_CTL, flag);
1486
Bram Moolenaar734a8672019-12-02 22:49:38 +01001487 // TODO: When the window is maximized, the size of the window stays the
1488 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1489 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001490}
1491
1492 void
1493gui_mch_set_scrollbar_pos(
1494 scrollbar_T *sb,
1495 int x,
1496 int y,
1497 int w,
1498 int h)
1499{
1500 SetWindowPos(sb->id, NULL, x, y, w, h,
1501 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1502}
1503
Bram Moolenaar203ec772020-07-17 20:43:43 +02001504 int
1505gui_mch_get_scrollbar_xpadding(void)
1506{
1507 RECT rcTxt, rcWnd;
1508 int xpad;
1509
1510 GetWindowRect(s_textArea, &rcTxt);
1511 GetWindowRect(s_hwnd, &rcWnd);
1512 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001513 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1514 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001515 return (xpad < 0) ? 0 : xpad;
1516}
1517
1518 int
1519gui_mch_get_scrollbar_ypadding(void)
1520{
1521 RECT rcTxt, rcWnd;
1522 int ypad;
1523
1524 GetWindowRect(s_textArea, &rcTxt);
1525 GetWindowRect(s_hwnd, &rcWnd);
1526 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001527 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1528 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001529 return (ypad < 0) ? 0 : ypad;
1530}
1531
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001532 void
1533gui_mch_create_scrollbar(
1534 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001535 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001536{
1537 sb->id = CreateWindow(
1538 "SCROLLBAR", "Scrollbar",
1539 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001540 10, // Any value will do for now
1541 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001542 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001543 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001544}
1545
1546/*
1547 * Find the scrollbar with the given hwnd.
1548 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001549 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001550gui_mswin_find_scrollbar(HWND hwnd)
1551{
1552 win_T *wp;
1553
1554 if (gui.bottom_sbar.id == hwnd)
1555 return &gui.bottom_sbar;
1556 FOR_ALL_WINDOWS(wp)
1557 {
1558 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1559 return &wp->w_scrollbars[SBAR_LEFT];
1560 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1561 return &wp->w_scrollbars[SBAR_RIGHT];
1562 }
1563 return NULL;
1564}
1565
K.Takatac81e9bf2022-01-16 14:15:49 +00001566 static void
1567update_scrollbar_size(void)
1568{
1569 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1570 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1571}
1572
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001573/*
K.Takataabe628e2022-01-23 16:25:17 +00001574 * Get the average character size of a font.
1575 */
1576 static void
1577GetAverageFontSize(HDC hdc, SIZE *size)
1578{
1579 // GetTextMetrics() may not return the right value in tmAveCharWidth
1580 // for some fonts. Do our own average computation.
1581 GetTextExtentPoint(hdc,
1582 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1583 52, size);
1584 size->cx = (size->cx / 26 + 1) / 2;
1585}
1586
1587/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001588 * Get the character size of a font.
1589 */
1590 static void
Ken Takatada5da652023-10-05 20:20:58 +02001591GetFontSize(GuiFont font, int *char_width, int *char_height)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592{
1593 HWND hwnd = GetDesktopWindow();
1594 HDC hdc = GetWindowDC(hwnd);
1595 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001596 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 TEXTMETRIC tm;
1598
1599 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001600 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001601
Ken Takatada5da652023-10-05 20:20:58 +02001602 if (char_width)
1603 *char_width = size.cx + tm.tmOverhang;
1604 if (char_height)
1605 *char_height = tm.tmHeight + p_linespace;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001606
1607 SelectFont(hdc, hfntOld);
1608
1609 ReleaseDC(hwnd, hdc);
1610}
1611
1612/*
Ken Takatada5da652023-10-05 20:20:58 +02001613 * Update the character size in "gui" structure with the specified font.
1614 */
1615 static void
1616UpdateFontSize(GuiFont font)
1617{
1618 GetFontSize(font, &gui.char_width, &gui.char_height);
1619}
1620
1621/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622 * Adjust gui.char_height (after 'linespace' was changed).
1623 */
1624 int
1625gui_mch_adjust_charheight(void)
1626{
Ken Takatada5da652023-10-05 20:20:58 +02001627 UpdateFontSize(gui.norm_font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001628 return OK;
1629}
1630
1631 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001632get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001633{
1634 HFONT font = NULL;
1635
Bram Moolenaar734a8672019-12-02 22:49:38 +01001636 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001637 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001638
1639 if (font == NULL)
1640 return NOFONT;
1641
1642 return (GuiFont)font;
1643}
1644
1645 static int
1646pixels_to_points(int pixels, int vertical)
1647{
1648 int points;
1649 HWND hwnd;
1650 HDC hdc;
1651
1652 hwnd = GetDesktopWindow();
1653 hdc = GetWindowDC(hwnd);
1654
1655 points = MulDiv(pixels, 72,
1656 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1657
1658 ReleaseDC(hwnd, hdc);
1659
1660 return points;
1661}
1662
1663 GuiFont
1664gui_mch_get_font(
1665 char_u *name,
1666 int giveErrorIfMissing)
1667{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001668 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001669 GuiFont font = NOFONT;
1670
1671 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001672 {
1673 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001674 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001675 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001676 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001677 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678 return font;
1679}
1680
1681#if defined(FEAT_EVAL) || defined(PROTO)
1682/*
1683 * Return the name of font "font" in allocated memory.
1684 * Don't know how to get the actual name, thus use the provided name.
1685 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001686 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001687gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001688{
1689 if (name == NULL)
1690 return NULL;
1691 return vim_strsave(name);
1692}
1693#endif
1694
1695 void
1696gui_mch_free_font(GuiFont font)
1697{
1698 if (font)
1699 DeleteObject((HFONT)font);
1700}
1701
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001702/*
1703 * Return the Pixel value (color) for the given color name.
1704 * Return INVALCOLOR for error.
1705 */
1706 guicolor_T
1707gui_mch_get_color(char_u *name)
1708{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001709 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001710
1711 typedef struct SysColorTable
1712 {
1713 char *name;
1714 int color;
1715 } SysColorTable;
1716
1717 static SysColorTable sys_table[] =
1718 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001719 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1720 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001721#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001722 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1723#endif
1724 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1725 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1726 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1727 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1728 {"SYS_DESKTOP", COLOR_DESKTOP},
1729 {"SYS_INFOBK", COLOR_INFOBK},
1730 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1731 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001732 {"SYS_BTNFACE", COLOR_BTNFACE},
1733 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1734 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1735 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1736 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1737 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1738 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1739 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1740 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1741 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1742 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1743 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1744 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1745 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1746 {"SYS_MENU", COLOR_MENU},
1747 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1748 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1749 {"SYS_WINDOW", COLOR_WINDOW},
1750 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1751 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1752 };
1753
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001754 /*
1755 * Try to look up a system colour.
1756 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001757 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001758 if (STRICMP(name, sys_table[i].name) == 0)
1759 return GetSysColor(sys_table[i].color);
1760
Bram Moolenaarab302212016-04-26 20:59:29 +02001761 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001762}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001763
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001764 guicolor_T
1765gui_mch_get_rgb_color(int r, int g, int b)
1766{
1767 return gui_get_rgb_color_cmn(r, g, b);
1768}
1769
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001770/*
1771 * Return OK if the key with the termcap name "name" is supported.
1772 */
1773 int
1774gui_mch_haskey(char_u *name)
1775{
1776 int i;
1777
1778 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001779 if (name[0] == special_keys[i].vim_code0
1780 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001781 return OK;
1782 return FAIL;
1783}
1784
1785 void
1786gui_mch_beep(void)
1787{
LemonBoy77771d32022-04-13 11:47:25 +01001788 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001789}
1790/*
1791 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1792 */
1793 void
1794gui_mch_invert_rectangle(
1795 int r,
1796 int c,
1797 int nr,
1798 int nc)
1799{
1800 RECT rc;
1801
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001802#if defined(FEAT_DIRECTX)
1803 if (IS_ENABLE_DIRECTX())
1804 DWriteContext_Flush(s_dwc);
1805#endif
1806
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001807 /*
1808 * Note: InvertRect() excludes right and bottom of rectangle.
1809 */
1810 rc.left = FILL_X(c);
1811 rc.top = FILL_Y(r);
1812 rc.right = rc.left + nc * gui.char_width;
1813 rc.bottom = rc.top + nr * gui.char_height;
1814 InvertRect(s_hdc, &rc);
1815}
1816
1817/*
1818 * Iconify the GUI window.
1819 */
1820 void
1821gui_mch_iconify(void)
1822{
1823 ShowWindow(s_hwnd, SW_MINIMIZE);
1824}
1825
1826/*
1827 * Draw a cursor without focus.
1828 */
1829 void
1830gui_mch_draw_hollow_cursor(guicolor_T color)
1831{
1832 HBRUSH hbr;
1833 RECT rc;
1834
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001835#if defined(FEAT_DIRECTX)
1836 if (IS_ENABLE_DIRECTX())
1837 DWriteContext_Flush(s_dwc);
1838#endif
1839
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001840 /*
1841 * Note: FrameRect() excludes right and bottom of rectangle.
1842 */
1843 rc.left = FILL_X(gui.col);
1844 rc.top = FILL_Y(gui.row);
1845 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001846 if (mb_lefthalve(gui.row, gui.col))
1847 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001848 rc.bottom = rc.top + gui.char_height;
1849 hbr = CreateSolidBrush(color);
1850 FrameRect(s_hdc, &rc, hbr);
1851 DeleteBrush(hbr);
1852}
1853/*
1854 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1855 * color "color".
1856 */
1857 void
1858gui_mch_draw_part_cursor(
1859 int w,
1860 int h,
1861 guicolor_T color)
1862{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001863 RECT rc;
1864
1865 /*
1866 * Note: FillRect() excludes right and bottom of rectangle.
1867 */
1868 rc.left =
1869#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001870 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1872#endif
1873 FILL_X(gui.col);
1874 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1875 rc.right = rc.left + w;
1876 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001877
Bram Moolenaar92467d32017-12-05 13:22:16 +01001878 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001879}
1880
1881
1882/*
1883 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1884 * dead key's nominal character and re-post the original message.
1885 */
1886 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001887outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888{
1889 static MSG deadCharExpel;
1890
Anton Sharonov3f026672022-07-26 21:26:18 +01001891 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001892 return;
1893
Anton Sharonov3f026672022-07-26 21:26:18 +01001894 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001895
Bram Moolenaar734a8672019-12-02 22:49:38 +01001896 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001897 deadCharExpel.message = originalMsg.message;
1898 deadCharExpel.hwnd = originalMsg.hwnd;
1899 deadCharExpel.wParam = VK_SPACE;
1900
K.Takata4ac893f2022-01-20 12:44:28 +00001901 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001902
Bram Moolenaar734a8672019-12-02 22:49:38 +01001903 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001904 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1905 originalMsg.lParam);
1906}
1907
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001909 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1910 */
1911 static void
1912outputDeadKey_rePost(MSG originalMsg)
1913{
1914 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1915}
1916
1917/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001918 * Process a single Windows message.
1919 * If one is not available we hang until one is.
1920 */
1921 static void
1922process_message(void)
1923{
1924 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001925 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001926 char_u string[40];
1927 int i;
1928 int modifiers = 0;
1929 int key;
1930#ifdef FEAT_MENU
1931 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1932#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001933 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001934
K.Takatab7057bd2022-01-21 11:37:07 +00001935 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001936
1937#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001938 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001939 if (msg.message == WM_OLE)
1940 {
1941 char_u *str = (char_u *)msg.lParam;
1942 if (str == NULL || *str == NUL)
1943 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001944 // Message can't be ours, forward it. Fixes problem with Ultramon
1945 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001946 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 }
1948 else
1949 {
1950 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001951 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001952 }
1953 return;
1954 }
1955#endif
1956
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001957#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001958 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001959 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001960 {
1961 HandleMouseHide(msg.message, msg.lParam);
1962 return;
1963 }
1964#endif
1965
1966 /*
1967 * Check if it's a special key that we recognise. If not, call
1968 * TranslateMessage().
1969 */
1970 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1971 {
1972 vk = (int) msg.wParam;
1973
1974 /*
1975 * Handle dead keys in special conditions in other cases we let Windows
1976 * handle them and do not interfere.
1977 *
1978 * The dead_key flag must be reset on several occasions:
1979 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1980 * consumed at that point (This is when we let Windows combine the
1981 * dead character on its own)
1982 *
1983 * - Before doing something special such as regenerating keypresses to
1984 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001985 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001986 * immediately to _OnChar() (or _OnSysChar()).
1987 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001988
1989 /*
1990 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1991 * was handled by _WndProc, this keypress we want to process normally
1992 */
1993 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1994 dead_key = DEAD_KEY_OFF;
1995
1996 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001997 {
1998 /*
dundargocc57b5bc2022-11-02 13:30:51 +00001999 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01002000 *
2001 * After dead key was pressed with Ctrl in some cases, ESC was
2002 * artificially injected and handled by _OnChar(), now we are
2003 * dealing with completely new key press from the user. If we don't
2004 * do anything, ToUnicode() call will interpret this vk+scan_code
2005 * under influence of "dead-modifier". To prevent this we translate
2006 * this message replacing current char from user with VK_SPACE,
2007 * which will cause WM_CHAR with dead_key's character itself. Using
2008 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
2009 * ignore this one WM_CHAR event completely. Afterwards (due to
2010 * usage of PostMessage), this procedure is scheduled to be called
2011 * again with user char and on next entry we will clean
2012 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2013 * outputDeadKey_rePost() since we do not wish to reset dead_key
2014 * value.
2015 */
2016 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
2017 {
2018 outputDeadKey_rePost_Ex(msg,
2019 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2020 return;
2021 }
2022
2023 if (dead_key != DEAD_KEY_SET_DEFAULT)
2024 {
2025 // should never happen - is there a way to make ASSERT here?
2026 return;
2027 }
2028
2029 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 * If a dead key was pressed and the user presses VK_SPACE,
2031 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2032 * with the dead char now, so do nothing special and let Windows
2033 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002034 *
2035 * Note that VK_SPACE combines with the dead_key's character and
2036 * only one WM_CHAR will be generated by TranslateMessage(), in
2037 * the two other cases two WM_CHAR will be generated: the dead
2038 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2039 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002040 */
2041 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2042 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002043 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002044 TranslateMessage(&msg);
2045 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002046 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002047 // In modes where we are not typing, dead keys should behave
2048 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002049 else if ((get_real_state()
2050 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 {
2052 outputDeadKey_rePost(msg);
2053 return;
2054 }
2055 }
2056
Bram Moolenaar734a8672019-12-02 22:49:38 +01002057 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002058 if (vk == VK_CANCEL)
2059 {
2060 trash_input_buf();
2061 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002062 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002063 string[0] = Ctrl_C;
2064 add_to_input_buf(string, 1);
2065 }
2066
LemonBoy77fc0b02022-04-22 22:45:52 +01002067 // This is an IME event or a synthetic keystroke, let Windows handle it.
2068 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2069 {
2070 TranslateMessage(&msg);
2071 return;
2072 }
2073
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074 for (i = 0; special_keys[i].key_sym != 0; i++)
2075 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002076 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002077 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002078 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002079 {
2080 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002081 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002082 * is a key that would normally trigger the dead key nominal
2083 * character output (such as a NUMPAD printable character or
2084 * the TAB key, etc...).
2085 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002086 if (dead_key == DEAD_KEY_SET_DEFAULT
2087 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088 || vk == VK_TAB || vk == CAR))
2089 {
2090 outputDeadKey_rePost(msg);
2091 return;
2092 }
2093
2094#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002095 // Check for <F10>: Windows selects the menu. When <F10> is
2096 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002097 if (vk == VK_F10
2098 && gui.menu_is_active
2099 && check_map(k10, State, FALSE, TRUE, FALSE,
2100 NULL, NULL) == NULL)
2101 break;
2102#endif
LemonBoy45684c62022-04-24 15:46:42 +01002103 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104
2105 if (special_keys[i].vim_code1 == NUL)
2106 key = special_keys[i].vim_code0;
2107 else
2108 key = TO_SPECIAL(special_keys[i].vim_code0,
2109 special_keys[i].vim_code1);
2110 key = simplify_key(key, &modifiers);
2111 if (key == CSI)
2112 key = K_CSI;
2113
2114 if (modifiers)
2115 {
2116 string[0] = CSI;
2117 string[1] = KS_MODIFIER;
2118 string[2] = modifiers;
2119 add_to_input_buf(string, 3);
2120 }
2121
2122 if (IS_SPECIAL(key))
2123 {
2124 string[0] = CSI;
2125 string[1] = K_SECOND(key);
2126 string[2] = K_THIRD(key);
2127 add_to_input_buf(string, 3);
2128 }
2129 else
2130 {
2131 int len;
2132
Bram Moolenaar734a8672019-12-02 22:49:38 +01002133 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002134 len = char_to_string(key, string, 40, FALSE);
2135 add_to_input_buf(string, len);
2136 }
2137 break;
2138 }
2139 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002140
2141 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002142 if (special_keys[i].key_sym == 0)
2143 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002144 WCHAR ch[8];
2145 int len;
2146 int i;
2147 UINT scan_code;
2148
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002149 // Construct the state table with only a few modifiers, we don't
2150 // really care about the presence of Ctrl/Alt as those modifiers are
2151 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002152 memset(keyboard_state, 0, 256);
2153 if (GetKeyState(VK_SHIFT) & 0x8000)
2154 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002155 if (GetKeyState(VK_CAPITAL) & 0x0001)
2156 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002157 // Alt-Gr is synthesized as Alt + Ctrl.
2158 if ((GetKeyState(VK_RMENU) & 0x8000)
2159 && (GetKeyState(VK_CONTROL) & 0x8000))
2160 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002161 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002162 keyboard_state[VK_CONTROL] = 0x80;
2163 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002164
2165 // Translate the virtual key according to the current keyboard
2166 // layout.
2167 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2168 // Convert the scan-code into a sequence of zero or more unicode
2169 // codepoints.
2170 // If this is a dead key ToUnicode returns a negative value.
2171 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2172 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002173 if (len < 0)
2174 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002175
2176 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002177 {
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002178 int wm_char = NUL;
2179
2180 if (dead_key == DEAD_KEY_SET_DEFAULT
2181 && (GetKeyState(VK_CONTROL) & 0x8000))
2182 {
2183 if ( // AZERTY CTRL+dead_circumflex
2184 (vk == 221 && scan_code == 26)
2185 // QWERTZ CTRL+dead_circumflex
2186 || (vk == 220 && scan_code == 41))
2187 wm_char = '[';
2188 if ( // QWERTZ CTRL+dead_two-overdots
2189 (vk == 192 && scan_code == 27))
2190 wm_char = ']';
2191 }
2192 if (wm_char != NUL)
Anton Sharonov3f026672022-07-26 21:26:18 +01002193 {
2194 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002195 // still hold as ESC
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002196 PostMessageW(msg.hwnd, WM_CHAR, wm_char, msg.lParam);
Anton Sharonov3f026672022-07-26 21:26:18 +01002197 // ask _OnChar() to not touch this state, wait for next key
2198 // press and maintain knowledge that we are "poisoned" with
2199 // "dead state"
2200 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2201 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002202 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002203 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002204
2205 // Post the message as TranslateMessage would do.
2206 if (msg.message == WM_KEYDOWN)
2207 {
2208 for (i = 0; i < len; i++)
2209 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002210 }
2211 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002212 {
2213 for (i = 0; i < len; i++)
2214 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2215 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002216 }
2217 }
2218#ifdef FEAT_MBYTE_IME
2219 else if (msg.message == WM_IME_NOTIFY)
2220 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2221 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002222 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002223 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002224#endif
2225
2226#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002227 // Check for <F10>: Default effect is to select the menu. When <F10> is
2228 // mapped we need to stop it here to avoid strange effects (e.g., for the
2229 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2231 NULL, NULL) == NULL)
2232#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002233 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002234}
2235
2236/*
2237 * Catch up with any queued events. This may put keyboard input into the
2238 * input buffer, call resize call-backs, trigger timers etc. If there is
2239 * nothing in the event queue (& no timers pending), then we return
2240 * immediately.
2241 */
2242 void
2243gui_mch_update(void)
2244{
2245 MSG msg;
2246
2247 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002248 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002249 && !vim_is_input_buf_full())
2250 process_message();
2251}
2252
Bram Moolenaar4231da42016-06-02 14:30:04 +02002253 static void
2254remove_any_timer(void)
2255{
2256 MSG msg;
2257
2258 if (s_wait_timer != 0 && !s_timed_out)
2259 {
2260 KillTimer(NULL, s_wait_timer);
2261
Bram Moolenaar734a8672019-12-02 22:49:38 +01002262 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002263 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002264 ;
2265 s_wait_timer = 0;
2266 }
2267}
2268
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269/*
2270 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2271 * from the keyboard.
2272 * wtime == -1 Wait forever.
2273 * wtime == 0 This should never happen.
2274 * wtime > 0 Wait wtime milliseconds for a character.
2275 * Returns OK if a character was found to be available within the given time,
2276 * or FAIL otherwise.
2277 */
2278 int
2279gui_mch_wait_for_chars(int wtime)
2280{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002281 int focus;
2282
2283 s_timed_out = FALSE;
2284
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002285 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002286 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002287 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002288 if (s_busy_processing)
2289 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002290
2291 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002292 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2293 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002294 }
2295
2296 allow_scrollbar = TRUE;
2297
2298 focus = gui.in_focus;
2299 while (!s_timed_out)
2300 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002301 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302 if (gui.in_focus != focus)
2303 {
2304 if (gui.in_focus)
2305 gui_mch_start_blink();
2306 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002307 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002308 focus = gui.in_focus;
2309 }
2310
2311 if (s_need_activate)
2312 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002313 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314 s_need_activate = FALSE;
2315 }
2316
Bram Moolenaar4231da42016-06-02 14:30:04 +02002317#ifdef FEAT_TIMERS
2318 did_add_timer = FALSE;
2319#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002321 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002322 for (;;)
2323 {
2324 MSG msg;
2325
2326 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002327# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002328 if (did_add_timer)
2329 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002330# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002331 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002332 {
2333 process_message();
2334 break;
2335 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002336 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002337 // TODO: The 10 msec is a compromise between laggy response
2338 // and consuming more CPU time. Better would be to handle
2339 // channel messages when they arrive.
2340 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002341 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002342 break;
2343 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002344#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002345 // Don't use gui_mch_update() because then we will spin-lock until a
2346 // char arrives, instead we use GetMessage() to hang until an
2347 // event arrives. No need to check for input_buf_full because we are
2348 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002349 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002350#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351
2352 if (input_available())
2353 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002354 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355 allow_scrollbar = FALSE;
2356
Bram Moolenaar89c00032019-09-04 13:53:21 +02002357 // Clear pending mouse button, the release event may have been
2358 // taken by the dialog window. But don't do this when getting
2359 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002360 if (!s_getting_focus)
2361 s_button_pending = -1;
2362
2363 return OK;
2364 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002365
2366#ifdef FEAT_TIMERS
2367 if (did_add_timer)
2368 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002369 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002370 remove_any_timer();
2371 break;
2372 }
2373#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002374 }
2375 allow_scrollbar = FALSE;
2376 return FAIL;
2377}
2378
2379/*
2380 * Clear a rectangular region of the screen from text pos (row1, col1) to
2381 * (row2, col2) inclusive.
2382 */
2383 void
2384gui_mch_clear_block(
2385 int row1,
2386 int col1,
2387 int row2,
2388 int col2)
2389{
2390 RECT rc;
2391
2392 /*
2393 * Clear one extra pixel at the far right, for when bold characters have
2394 * spilled over to the window border.
2395 * Note: FillRect() excludes right and bottom of rectangle.
2396 */
2397 rc.left = FILL_X(col1);
2398 rc.top = FILL_Y(row1);
2399 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2400 rc.bottom = FILL_Y(row2 + 1);
2401 clear_rect(&rc);
2402}
2403
2404/*
2405 * Clear the whole text window.
2406 */
2407 void
2408gui_mch_clear_all(void)
2409{
2410 RECT rc;
2411
2412 rc.left = 0;
2413 rc.top = 0;
2414 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2415 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2416 clear_rect(&rc);
2417}
2418/*
2419 * Menu stuff.
2420 */
2421
2422 void
2423gui_mch_enable_menu(int flag)
2424{
2425#ifdef FEAT_MENU
2426 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2427#endif
2428}
2429
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002430 void
2431gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002432 int x UNUSED,
2433 int y UNUSED,
2434 int w UNUSED,
2435 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002437 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438}
2439
2440#if defined(FEAT_MENU) || defined(PROTO)
2441/*
2442 * Make menu item hidden or not hidden
2443 */
2444 void
2445gui_mch_menu_hidden(
2446 vimmenu_T *menu,
2447 int hidden)
2448{
2449 /*
2450 * This doesn't do what we want. Hmm, just grey the menu items for now.
2451 */
2452 /*
2453 if (hidden)
2454 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2455 else
2456 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2457 */
2458 gui_mch_menu_grey(menu, hidden);
2459}
2460
2461/*
2462 * This is called after setting all the menus to grey/hidden or not.
2463 */
2464 void
2465gui_mch_draw_menubar(void)
2466{
2467 DrawMenuBar(s_hwnd);
2468}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002469#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002470
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471/*
2472 * Return the RGB value of a pixel as a long.
2473 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002474 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475gui_mch_get_rgb(guicolor_T pixel)
2476{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002477 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2478 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479}
2480
2481#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002482/*
2483 * Convert pixels in X to dialog units
2484 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002485 static WORD
2486PixelToDialogX(int numPixels)
2487{
2488 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2489}
2490
Bram Moolenaar734a8672019-12-02 22:49:38 +01002491/*
2492 * Convert pixels in Y to dialog units
2493 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002494 static WORD
2495PixelToDialogY(int numPixels)
2496{
2497 return (WORD)((numPixels * 8) / s_dlgfntheight);
2498}
2499
Bram Moolenaar734a8672019-12-02 22:49:38 +01002500/*
2501 * Return the width in pixels of the given text in the given DC.
2502 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503 static int
2504GetTextWidth(HDC hdc, char_u *str, int len)
2505{
2506 SIZE size;
2507
2508 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2509 return size.cx;
2510}
2511
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002512/*
2513 * Return the width in pixels of the given text in the given DC, taking care
2514 * of 'encoding' to active codepage conversion.
2515 */
2516 static int
2517GetTextWidthEnc(HDC hdc, char_u *str, int len)
2518{
2519 SIZE size;
2520 WCHAR *wstr;
2521 int n;
2522 int wlen = len;
2523
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002524 wstr = enc_to_utf16(str, &wlen);
2525 if (wstr == NULL)
2526 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002527
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002528 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2529 vim_free(wstr);
2530 if (n)
2531 return size.cx;
2532 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002533}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002535static void get_work_area(RECT *spi_rect);
2536
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002537/*
2538 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002539 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2540 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541 */
2542 static BOOL
2543CenterWindow(
2544 HWND hwndChild,
2545 HWND hwndParent)
2546{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002547 HMONITOR mon;
2548 MONITORINFO moninfo;
2549 RECT rChild, rParent, rScreen;
2550 int wChild, hChild, wParent, hParent;
2551 int xNew, yNew;
2552 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553
2554 GetWindowRect(hwndChild, &rChild);
2555 wChild = rChild.right - rChild.left;
2556 hChild = rChild.bottom - rChild.top;
2557
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002560 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561 else
2562 GetWindowRect(hwndParent, &rParent);
2563 wParent = rParent.right - rParent.left;
2564 hParent = rParent.bottom - rParent.top;
2565
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002566 moninfo.cbSize = sizeof(MONITORINFO);
2567 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2568 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002569 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002570 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002571 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002572 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002573 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002574 hdc = GetDC(hwndChild);
2575 rScreen.left = 0;
2576 rScreen.top = 0;
2577 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2578 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2579 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002580 }
2581
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002582 xNew = rParent.left + ((wParent - wChild) / 2);
2583 if (xNew < rScreen.left)
2584 xNew = rScreen.left;
2585 else if ((xNew + wChild) > rScreen.right)
2586 xNew = rScreen.right - wChild;
2587
2588 yNew = rParent.top + ((hParent - hChild) / 2);
2589 if (yNew < rScreen.top)
2590 yNew = rScreen.top;
2591 else if ((yNew + hChild) > rScreen.bottom)
2592 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593
2594 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2595 SWP_NOSIZE | SWP_NOZORDER);
2596}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002597#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002598
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002599#if defined(FEAT_TOOLBAR) || defined(PROTO)
2600 void
2601gui_mch_show_toolbar(int showit)
2602{
2603 if (s_toolbarhwnd == NULL)
2604 return;
2605
2606 if (showit)
2607 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002608 // Enable unicode support
2609 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2610 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002611 ShowWindow(s_toolbarhwnd, SW_SHOW);
2612 }
2613 else
2614 ShowWindow(s_toolbarhwnd, SW_HIDE);
2615}
2616
Bram Moolenaar734a8672019-12-02 22:49:38 +01002617// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002618# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002619
2620#endif
2621
2622#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2623 static void
2624add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2625{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002626 WCHAR *wn;
2627 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002629 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002630 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002631 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002633 infow.cbSize = sizeof(infow);
2634 infow.fMask = MIIM_TYPE | MIIM_ID;
2635 infow.wID = item_id;
2636 infow.fType = MFT_STRING;
2637 infow.dwTypeData = wn;
2638 infow.cch = (UINT)wcslen(wn);
2639 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2640 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002641}
2642
2643 static void
2644show_tabline_popup_menu(void)
2645{
2646 HMENU tab_pmenu;
2647 long rval;
2648 POINT pt;
2649
Bram Moolenaar734a8672019-12-02 22:49:38 +01002650 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002651 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002652 return;
2653
2654 tab_pmenu = CreatePopupMenu();
2655 if (tab_pmenu == NULL)
2656 return;
2657
2658 if (first_tabpage->tp_next != NULL)
2659 add_tabline_popup_menu_entry(tab_pmenu,
2660 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2661 add_tabline_popup_menu_entry(tab_pmenu,
2662 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2663 add_tabline_popup_menu_entry(tab_pmenu,
2664 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2665
2666 GetCursorPos(&pt);
2667 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2668 NULL);
2669
2670 DestroyMenu(tab_pmenu);
2671
Bram Moolenaar734a8672019-12-02 22:49:38 +01002672 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002673 if (rval > 0)
2674 {
2675 TCHITTESTINFO htinfo;
2676 int idx;
2677
2678 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2679 return;
2680
2681 htinfo.pt.x = pt.x;
2682 htinfo.pt.y = pt.y;
2683 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2684 if (idx == -1)
2685 idx = 0;
2686 else
2687 idx += 1;
2688
2689 send_tabline_menu_event(idx, (int)rval);
2690 }
2691}
2692
2693/*
2694 * Show or hide the tabline.
2695 */
2696 void
2697gui_mch_show_tabline(int showit)
2698{
2699 if (s_tabhwnd == NULL)
2700 return;
2701
2702 if (!showit != !showing_tabline)
2703 {
2704 if (showit)
2705 ShowWindow(s_tabhwnd, SW_SHOW);
2706 else
2707 ShowWindow(s_tabhwnd, SW_HIDE);
2708 showing_tabline = showit;
2709 }
2710}
2711
2712/*
2713 * Return TRUE when tabline is displayed.
2714 */
2715 int
2716gui_mch_showing_tabline(void)
2717{
2718 return s_tabhwnd != NULL && showing_tabline;
2719}
2720
2721/*
2722 * Update the labels of the tabline.
2723 */
2724 void
2725gui_mch_update_tabline(void)
2726{
2727 tabpage_T *tp;
2728 TCITEM tie;
2729 int nr = 0;
2730 int curtabidx = 0;
2731 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002733
2734 if (s_tabhwnd == NULL)
2735 return;
2736
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002737 // Enable unicode support
2738 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739
2740 tie.mask = TCIF_TEXT;
2741 tie.iImage = -1;
2742
Bram Moolenaar734a8672019-12-02 22:49:38 +01002743 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2745
Bram Moolenaar734a8672019-12-02 22:49:38 +01002746 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2748 {
2749 if (tp == curtab)
2750 curtabidx = nr;
2751
2752 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2753 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002754 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002755 tie.pszText = "-Empty-";
2756 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2757 tabadded = 1;
2758 }
2759
2760 get_tabline_label(tp, FALSE);
2761 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002762
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002763 wstr = enc_to_utf16(NameBuff, NULL);
2764 if (wstr != NULL)
2765 {
2766 TCITEMW tiw;
2767
2768 tiw.mask = TCIF_TEXT;
2769 tiw.iImage = -1;
2770 tiw.pszText = wstr;
2771 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2772 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774 }
2775
Bram Moolenaar734a8672019-12-02 22:49:38 +01002776 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2778 TabCtrl_DeleteItem(s_tabhwnd, nr);
2779
2780 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2781 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2782
Bram Moolenaar734a8672019-12-02 22:49:38 +01002783 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002784 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2785 RedrawWindow(s_tabhwnd, NULL, NULL,
2786 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2787
2788 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2789 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2790}
2791
2792/*
2793 * Set the current tab to "nr". First tab is 1.
2794 */
2795 void
2796gui_mch_set_curtab(int nr)
2797{
2798 if (s_tabhwnd == NULL)
2799 return;
2800
2801 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2802 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2803}
2804
2805#endif
2806
2807/*
2808 * ":simalt" command.
2809 */
2810 void
2811ex_simalt(exarg_T *eap)
2812{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002813 char_u *keys = eap->arg;
2814 int fill_typebuf = FALSE;
2815 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816
2817 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2818 while (*keys)
2819 {
2820 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002821 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002822 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2823 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002824 fill_typebuf = TRUE;
2825 }
2826 if (fill_typebuf)
2827 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002828 // Put a NOP in the typeahead buffer so that the message will get
2829 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002830 key_name[0] = K_SPECIAL;
2831 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002832 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002833 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002834#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002835 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002836#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002837 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002838 }
2839}
2840
2841/*
2842 * Create the find & replace dialogs.
2843 * You can't have both at once: ":find" when replace is showing, destroys
2844 * the replace dialog first, and the other way around.
2845 */
2846#ifdef MSWIN_FIND_REPLACE
2847 static void
2848initialise_findrep(char_u *initial_string)
2849{
2850 int wword = FALSE;
2851 int mcase = !p_ic;
2852 char_u *entry_text;
2853
Bram Moolenaar734a8672019-12-02 22:49:38 +01002854 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002855 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2856
2857 s_findrep_struct.hwndOwner = s_hwnd;
2858 s_findrep_struct.Flags = FR_DOWN;
2859 if (mcase)
2860 s_findrep_struct.Flags |= FR_MATCHCASE;
2861 if (wword)
2862 s_findrep_struct.Flags |= FR_WHOLEWORD;
2863 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002864 {
2865 WCHAR *p = enc_to_utf16(entry_text, NULL);
2866 if (p != NULL)
2867 {
2868 int len = s_findrep_struct.wFindWhatLen - 1;
2869
2870 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2871 s_findrep_struct.lpstrFindWhat[len] = NUL;
2872 vim_free(p);
2873 }
2874 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 vim_free(entry_text);
2876}
2877#endif
2878
2879 static void
2880set_window_title(HWND hwnd, char *title)
2881{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002882 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002883 {
2884 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885
Bram Moolenaar734a8672019-12-02 22:49:38 +01002886 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002887 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2888 if (wbuf != NULL)
2889 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002890 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002891 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002892 }
2893 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002894 else
2895 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002896}
2897
2898 void
2899gui_mch_find_dialog(exarg_T *eap)
2900{
2901#ifdef MSWIN_FIND_REPLACE
2902 if (s_findrep_msg != 0)
2903 {
2904 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2905 DestroyWindow(s_findrep_hwnd);
2906
2907 if (!IsWindow(s_findrep_hwnd))
2908 {
2909 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002910 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002911 }
2912
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002913 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002914 (void)SetFocus(s_findrep_hwnd);
2915
2916 s_findrep_is_find = TRUE;
2917 }
2918#endif
2919}
2920
2921
2922 void
2923gui_mch_replace_dialog(exarg_T *eap)
2924{
2925#ifdef MSWIN_FIND_REPLACE
2926 if (s_findrep_msg != 0)
2927 {
2928 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2929 DestroyWindow(s_findrep_hwnd);
2930
2931 if (!IsWindow(s_findrep_hwnd))
2932 {
2933 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002934 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002935 }
2936
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002937 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 (void)SetFocus(s_findrep_hwnd);
2939
2940 s_findrep_is_find = FALSE;
2941 }
2942#endif
2943}
2944
2945
2946/*
2947 * Set visibility of the pointer.
2948 */
2949 void
2950gui_mch_mousehide(int hide)
2951{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002952 if (hide == gui.pointer_hidden)
2953 return;
2954
2955 ShowCursor(!hide);
2956 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002957}
2958
2959#ifdef FEAT_MENU
2960 static void
2961gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2962{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002963 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964 gui_mch_mousehide(FALSE);
2965
2966 (void)TrackPopupMenu(
2967 (HMENU)menu->submenu_id,
2968 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2969 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002970 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002971 s_hwnd,
2972 NULL);
2973 /*
2974 * NOTE: The pop-up menu can eat the mouse up event.
2975 * We deal with this in normal.c.
2976 */
2977}
2978#endif
2979
2980/*
2981 * Got a message when the system will go down.
2982 */
2983 static void
2984_OnEndSession(void)
2985{
2986 getout_preserve_modified(1);
2987}
2988
2989/*
2990 * Get this message when the user clicks on the cross in the top right corner
2991 * of a Windows95 window.
2992 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002993 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002994_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002995{
2996 gui_shell_closed();
2997}
2998
2999/*
3000 * Get a message when the window is being destroyed.
3001 */
3002 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003003_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003004{
3005 if (!destroying)
3006 _OnClose(hwnd);
3007}
3008
3009 static void
3010_OnPaint(
3011 HWND hwnd)
3012{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003013 if (IsMinimized(hwnd))
3014 return;
3015
3016 PAINTSTRUCT ps;
3017
3018 out_flush(); // make sure all output has been processed
3019 (void)BeginPaint(hwnd, &ps);
3020
3021 // prevent multi-byte characters from misprinting on an invalid
3022 // rectangle
3023 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003024 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003025 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003026
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003027 GetClientRect(hwnd, &rect);
3028 ps.rcPaint.left = rect.left;
3029 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003030 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003031
3032 if (!IsRectEmpty(&ps.rcPaint))
3033 {
3034 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3035 ps.rcPaint.right - ps.rcPaint.left + 1,
3036 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3037 }
3038
3039 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003040}
3041
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003042 static void
3043_OnSize(
3044 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003045 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003046 int cx,
3047 int cy)
3048{
K.Takatac81e9bf2022-01-16 14:15:49 +00003049 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003050 {
3051 gui_resize_shell(cx, cy);
3052
Bram Moolenaar734a8672019-12-02 22:49:38 +01003053 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003054 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055 }
3056}
3057
3058 static void
3059_OnSetFocus(
3060 HWND hwnd,
3061 HWND hwndOldFocus)
3062{
3063 gui_focus_change(TRUE);
3064 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003065 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003066}
3067
3068 static void
3069_OnKillFocus(
3070 HWND hwnd,
3071 HWND hwndNewFocus)
3072{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003073 if (destroying)
3074 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003075 gui_focus_change(FALSE);
3076 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003077 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003078}
3079
3080/*
3081 * Get a message when the user switches back to vim
3082 */
3083 static LRESULT
3084_OnActivateApp(
3085 HWND hwnd,
3086 BOOL fActivate,
3087 DWORD dwThreadId)
3088{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003089 // we call gui_focus_change() in _OnSetFocus()
3090 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003091 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003092}
3093
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003094 void
3095gui_mch_destroy_scrollbar(scrollbar_T *sb)
3096{
3097 DestroyWindow(sb->id);
3098}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003099
3100/*
3101 * Get current mouse coordinates in text window.
3102 */
3103 void
3104gui_mch_getmouse(int *x, int *y)
3105{
3106 RECT rct;
3107 POINT mp;
3108
3109 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003110 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003111 *x = (int)(mp.x - rct.left);
3112 *y = (int)(mp.y - rct.top);
3113}
3114
3115/*
3116 * Move mouse pointer to character at (x, y).
3117 */
3118 void
3119gui_mch_setmouse(int x, int y)
3120{
3121 RECT rct;
3122
3123 (void)GetWindowRect(s_textArea, &rct);
3124 (void)SetCursorPos(x + gui.border_offset + rct.left,
3125 y + gui.border_offset + rct.top);
3126}
3127
3128 static void
3129gui_mswin_get_valid_dimensions(
3130 int w,
3131 int h,
3132 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003133 int *valid_h,
3134 int *cols,
3135 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003136{
3137 int base_width, base_height;
3138
3139 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003140 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3141 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003142 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003143 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3144 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3145 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3146 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003147 *cols = (w - base_width) / gui.char_width;
3148 *rows = (h - base_height) / gui.char_height;
3149 *valid_w = base_width + *cols * gui.char_width;
3150 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151}
3152
3153 void
3154gui_mch_flash(int msec)
3155{
3156 RECT rc;
3157
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003158#if defined(FEAT_DIRECTX)
3159 if (IS_ENABLE_DIRECTX())
3160 DWriteContext_Flush(s_dwc);
3161#endif
3162
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003163 /*
3164 * Note: InvertRect() excludes right and bottom of rectangle.
3165 */
3166 rc.left = 0;
3167 rc.top = 0;
3168 rc.right = gui.num_cols * gui.char_width;
3169 rc.bottom = gui.num_rows * gui.char_height;
3170 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003171 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172
Bram Moolenaar734a8672019-12-02 22:49:38 +01003173 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003174
3175 InvertRect(s_hdc, &rc);
3176}
3177
3178/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003179 * Check if the specified point is on-screen. (multi-monitor aware)
3180 */
3181 static BOOL
3182is_point_onscreen(int x, int y)
3183{
3184 POINT pt = {x, y};
3185
3186 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3187}
3188
3189/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003190 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003191 *
3192 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3193 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3194 * only works when the whole window is on-screen.
3195 */
3196 static BOOL
3197is_window_onscreen(HWND hwnd)
3198{
3199 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003200 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003201
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003202 GetClientRect(hwnd, &rc);
3203 p1.x = rc.left;
3204 p1.y = rc.top;
3205 p2.x = rc.right - 1;
3206 p2.y = rc.bottom - 1;
3207 ClientToScreen(hwnd, &p1);
3208 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003209
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003210 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003211 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003212 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003213 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003214 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003215 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003216 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003217 return FALSE;
3218 return TRUE;
3219}
3220
3221/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003222 * Return flags used for scrolling.
3223 * The SW_INVALIDATE is required when part of the window is covered or
3224 * off-screen. Refer to MS KB Q75236.
3225 */
3226 static int
3227get_scroll_flags(void)
3228{
3229 HWND hwnd;
3230 RECT rcVim, rcOther, rcDest;
3231
Bram Moolenaar185577e2020-10-29 20:08:21 +01003232 // Check if the window is (partly) off-screen.
3233 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234 return SW_INVALIDATE;
3235
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003236 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003237 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3239 if (IsWindowVisible(hwnd))
3240 {
3241 GetWindowRect(hwnd, &rcOther);
3242 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3243 return SW_INVALIDATE;
3244 }
3245 return 0;
3246}
3247
3248/*
3249 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3250 * may not be scrolled out properly.
3251 * For gVim, when _OnScroll() is repeated, the character at the
3252 * previous cursor position may be left drawn after scroll.
3253 * The problem can be avoided by calling GetPixel() to get a pixel in
3254 * the region before ScrollWindowEx().
3255 */
3256 static void
3257intel_gpu_workaround(void)
3258{
3259 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3260}
3261
3262/*
3263 * Delete the given number of lines from the given row, scrolling up any
3264 * text further down within the scroll region.
3265 */
3266 void
3267gui_mch_delete_lines(
3268 int row,
3269 int num_lines)
3270{
3271 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003272
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003273 rc.left = FILL_X(gui.scroll_region_left);
3274 rc.right = FILL_X(gui.scroll_region_right + 1);
3275 rc.top = FILL_Y(row);
3276 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3277
Bram Moolenaar92467d32017-12-05 13:22:16 +01003278#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003279 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003280 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003281 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003282 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003283 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003284#endif
3285 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003286#if defined(FEAT_DIRECTX)
3287 if (IS_ENABLE_DIRECTX())
3288 DWriteContext_Flush(s_dwc);
3289#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003290 intel_gpu_workaround();
3291 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003292 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003293 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003294 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003295
Bram Moolenaar734a8672019-12-02 22:49:38 +01003296 // This seems to be required to avoid the cursor disappearing when
3297 // scrolling such that the cursor ends up in the top-left character on
3298 // the screen... But why? (Webb)
3299 // It's probably fixed by disabling drawing the cursor while scrolling.
3300 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003301
3302 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3303 gui.scroll_region_left,
3304 gui.scroll_region_bot, gui.scroll_region_right);
3305}
3306
3307/*
3308 * Insert the given number of lines before the given row, scrolling down any
3309 * following text within the scroll region.
3310 */
3311 void
3312gui_mch_insert_lines(
3313 int row,
3314 int num_lines)
3315{
3316 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003317
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003318 rc.left = FILL_X(gui.scroll_region_left);
3319 rc.right = FILL_X(gui.scroll_region_right + 1);
3320 rc.top = FILL_Y(row);
3321 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003322
3323#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003324 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003325 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003326 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003327 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003328 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003329#endif
3330 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003331#if defined(FEAT_DIRECTX)
3332 if (IS_ENABLE_DIRECTX())
3333 DWriteContext_Flush(s_dwc);
3334#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003335 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003336 // The SW_INVALIDATE is required when part of the window is covered or
3337 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003338 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003340 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003341 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343 gui_clear_block(row, gui.scroll_region_left,
3344 row + num_lines - 1, gui.scroll_region_right);
3345}
3346
3347
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003348 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003349gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350{
3351#if defined(FEAT_DIRECTX)
3352 DWriteContext_Close(s_dwc);
3353 DWrite_Final();
3354 s_dwc = NULL;
3355#endif
3356
3357 ReleaseDC(s_textArea, s_hdc);
3358 DeleteObject(s_brush);
3359
3360#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003361 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003362 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3363#endif
3364
Bram Moolenaar734a8672019-12-02 22:49:38 +01003365 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003366 if (s_hwnd != NULL)
3367 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003368 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 DestroyWindow(s_hwnd);
3370 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003371}
3372
3373 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003374logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375{
3376 char *p;
3377 char *res;
3378 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003379 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003380 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003381 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003382
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003383 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3384 if (font_name == NULL)
3385 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003386 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003387 quality_name = quality_id2name((int)lf.lfQuality);
3388
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003389 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003390 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003391 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392 if (res != NULL)
3393 {
3394 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003395 // make a normal font string out of the lf thing:
3396 points = pixels_to_points(
3397 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3398 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3399 sprintf((char *)p, "%s:h%d", font_name, points);
3400 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003401 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003402 while (*p)
3403 {
3404 if (*p == ' ')
3405 *p = '_';
3406 ++p;
3407 }
3408 if (lf.lfItalic)
3409 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003410 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411 STRCAT(p, ":b");
3412 if (lf.lfUnderline)
3413 STRCAT(p, ":u");
3414 if (lf.lfStrikeOut)
3415 STRCAT(p, ":s");
3416 if (charset_name != NULL)
3417 {
3418 STRCAT(p, ":c");
3419 STRCAT(p, charset_name);
3420 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003421 if (quality_name != NULL)
3422 {
3423 STRCAT(p, ":q");
3424 STRCAT(p, quality_name);
3425 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426 }
3427
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003428 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003429 return (char_u *)res;
3430}
3431
3432
3433#ifdef FEAT_MBYTE_IME
3434/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003435 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003436 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003437 */
3438 static void
3439update_im_font(void)
3440{
K.Takatac81e9bf2022-01-16 14:15:49 +00003441 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003442
3443 if (p_guifontwide != NULL && *p_guifontwide != NUL
3444 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003445 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446 norm_logfont = lf_wide;
3447 else
3448 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003449
3450 lf = norm_logfont;
3451 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3452 // Work around when PerMonitorV2 is not enabled in the process level.
3453 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3454 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003455}
3456#endif
3457
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003458/*
3459 * Handler of gui.wide_font (p_guifontwide) changed notification.
3460 */
3461 void
3462gui_mch_wide_font_changed(void)
3463{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003464 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003466#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003467 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003468#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469
3470 gui_mch_free_font(gui.wide_ital_font);
3471 gui.wide_ital_font = NOFONT;
3472 gui_mch_free_font(gui.wide_bold_font);
3473 gui.wide_bold_font = NOFONT;
3474 gui_mch_free_font(gui.wide_boldital_font);
3475 gui.wide_boldital_font = NOFONT;
3476
3477 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003478 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479 {
3480 if (!lf.lfItalic)
3481 {
3482 lf.lfItalic = TRUE;
3483 gui.wide_ital_font = get_font_handle(&lf);
3484 lf.lfItalic = FALSE;
3485 }
3486 if (lf.lfWeight < FW_BOLD)
3487 {
3488 lf.lfWeight = FW_BOLD;
3489 gui.wide_bold_font = get_font_handle(&lf);
3490 if (!lf.lfItalic)
3491 {
3492 lf.lfItalic = TRUE;
3493 gui.wide_boldital_font = get_font_handle(&lf);
3494 }
3495 }
3496 }
3497}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498
3499/*
3500 * Initialise vim to use the font with the given name.
3501 * Return FAIL if the font could not be loaded, OK otherwise.
3502 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003504gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003505{
K.Takatac81e9bf2022-01-16 14:15:49 +00003506 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 GuiFont font = NOFONT;
3508 char_u *p;
3509
Bram Moolenaar734a8672019-12-02 22:49:38 +01003510 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003512 {
3513 lfOrig = lf;
3514 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003516 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003517 if (font == NOFONT)
3518 return FAIL;
3519
3520 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003521 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003522#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523 norm_logfont = lf;
3524 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003525 if (!s_in_dpichanged)
3526 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527#endif
3528 gui_mch_free_font(gui.norm_font);
3529 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003530 current_font_height = lfOrig.lfHeight;
Ken Takatada5da652023-10-05 20:20:58 +02003531 UpdateFontSize(font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003532
K.Takatac81e9bf2022-01-16 14:15:49 +00003533 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003534 if (p != NULL)
3535 {
3536 hl_set_font_name(p);
3537
Bram Moolenaar734a8672019-12-02 22:49:38 +01003538 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3540 {
3541 vim_free(p_guifont);
3542 p_guifont = p;
3543 }
3544 else
3545 vim_free(p);
3546 }
3547
3548 gui_mch_free_font(gui.ital_font);
3549 gui.ital_font = NOFONT;
3550 gui_mch_free_font(gui.bold_font);
3551 gui.bold_font = NOFONT;
3552 gui_mch_free_font(gui.boldital_font);
3553 gui.boldital_font = NOFONT;
3554
3555 if (!lf.lfItalic)
3556 {
3557 lf.lfItalic = TRUE;
3558 gui.ital_font = get_font_handle(&lf);
3559 lf.lfItalic = FALSE;
3560 }
3561 if (lf.lfWeight < FW_BOLD)
3562 {
3563 lf.lfWeight = FW_BOLD;
3564 gui.bold_font = get_font_handle(&lf);
3565 if (!lf.lfItalic)
3566 {
3567 lf.lfItalic = TRUE;
3568 gui.boldital_font = get_font_handle(&lf);
3569 }
3570 }
3571
3572 return OK;
3573}
3574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003575/*
3576 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003577 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578 */
3579 int
3580gui_mch_maximized(void)
3581{
3582 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003583 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003584
3585 wp.length = sizeof(WINDOWPLACEMENT);
3586 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003587 {
3588 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003590 && wp.flags == WPF_RESTORETOMAXIMIZED))
3591 return TRUE;
3592 if (wp.showCmd == SW_SHOWMINIMIZED)
3593 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003595 // Assume the window is snapped when the sizes from two APIs differ.
3596 GetWindowRect(s_hwnd, &rc);
3597 if ((rc.right - rc.left !=
3598 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3599 || (rc.bottom - rc.top !=
3600 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3601 return TRUE;
3602 }
3603 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003604}
3605
3606/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003607 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3608 * is set. Compute the new Rows and Columns. This is like resizing the
3609 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610 */
3611 void
3612gui_mch_newfont(void)
3613{
3614 RECT rect;
3615
3616 GetWindowRect(s_hwnd, &rect);
3617 if (win_socket_id == 0)
3618 {
3619 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003620 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3621 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003623 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3624 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3625 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3626 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 }
3628 else
3629 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003630 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003632 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633 }
3634}
3635
3636/*
3637 * Set the window title
3638 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003639 void
3640gui_mch_settitle(
3641 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003642 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003643{
3644 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3645}
3646
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003647#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003648// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3649// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650static LPCSTR mshape_idcs[] =
3651{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003652 IDC_ARROW, // arrow
3653 MAKEINTRESOURCE(0), // blank
3654 IDC_IBEAM, // beam
3655 IDC_SIZENS, // updown
3656 IDC_SIZENS, // udsizing
3657 IDC_SIZEWE, // leftright
3658 IDC_SIZEWE, // lrsizing
3659 IDC_WAIT, // busy
3660 IDC_NO, // no
3661 IDC_ARROW, // crosshair
3662 IDC_ARROW, // hand1
3663 IDC_ARROW, // hand2
3664 IDC_ARROW, // pencil
3665 IDC_ARROW, // question
3666 IDC_ARROW, // right-arrow
3667 IDC_UPARROW, // up-arrow
3668 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003669};
3670
3671 void
3672mch_set_mouse_shape(int shape)
3673{
3674 LPCSTR idc;
3675
3676 if (shape == MSHAPE_HIDE)
3677 ShowCursor(FALSE);
3678 else
3679 {
3680 if (shape >= MSHAPE_NUMBERED)
3681 idc = IDC_ARROW;
3682 else
3683 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003684 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003685 if (!p_mh)
3686 {
3687 POINT mp;
3688
Bram Moolenaar734a8672019-12-02 22:49:38 +01003689 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003690 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 (void)SetCursorPos(mp.x, mp.y);
3692 ShowCursor(TRUE);
3693 }
3694 }
3695}
3696#endif
3697
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003698#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003699/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 * Wide version of convert_filter().
3701 */
3702 static WCHAR *
3703convert_filterW(char_u *s)
3704{
3705 char_u *tmp;
3706 int len;
3707 WCHAR *res;
3708
3709 tmp = convert_filter(s);
3710 if (tmp == NULL)
3711 return NULL;
3712 len = (int)STRLEN(s) + 3;
3713 res = enc_to_utf16(tmp, &len);
3714 vim_free(tmp);
3715 return res;
3716}
3717
3718/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003719 * Pop open a file browser and return the file selected, in allocated memory,
3720 * or NULL if Cancel is hit.
3721 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3722 * title - Title message for the file browser dialog.
3723 * dflt - Default name of file.
3724 * ext - Default extension to be added to files without extensions.
3725 * initdir - directory in which to open the browser (NULL = current dir)
3726 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003727 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003728 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003729gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003730 int saving,
3731 char_u *title,
3732 char_u *dflt,
3733 char_u *ext,
3734 char_u *initdir,
3735 char_u *filter)
3736{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003737 // We always use the wide function. This means enc_to_utf16() must work,
3738 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003739 OPENFILENAMEW fileStruct;
3740 WCHAR fileBuf[MAXPATHL];
3741 WCHAR *wp;
3742 int i;
3743 WCHAR *titlep = NULL;
3744 WCHAR *extp = NULL;
3745 WCHAR *initdirp = NULL;
3746 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003747 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003748 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003749
3750 if (dflt == NULL)
3751 fileBuf[0] = NUL;
3752 else
3753 {
3754 wp = enc_to_utf16(dflt, NULL);
3755 if (wp == NULL)
3756 fileBuf[0] = NUL;
3757 else
3758 {
3759 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3760 fileBuf[i] = wp[i];
3761 fileBuf[i] = NUL;
3762 vim_free(wp);
3763 }
3764 }
3765
Bram Moolenaar734a8672019-12-02 22:49:38 +01003766 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 filterp = convert_filterW(filter);
3768
Bram Moolenaara80faa82020-04-12 19:37:17 +02003769 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003770# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003771 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003772 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003773# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003774 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003775# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776
3777 if (title != NULL)
3778 titlep = enc_to_utf16(title, NULL);
3779 fileStruct.lpstrTitle = titlep;
3780
3781 if (ext != NULL)
3782 extp = enc_to_utf16(ext, NULL);
3783 fileStruct.lpstrDefExt = extp;
3784
3785 fileStruct.lpstrFile = fileBuf;
3786 fileStruct.nMaxFile = MAXPATHL;
3787 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003788 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3789 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 if (initdir != NULL && *initdir != NUL)
3791 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003792 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793 initdirp = enc_to_utf16(initdir, NULL);
3794 if (initdirp != NULL)
3795 {
3796 for (wp = initdirp; *wp != NUL; ++wp)
3797 if (*wp == '/')
3798 *wp = '\\';
3799 }
3800 fileStruct.lpstrInitialDir = initdirp;
3801 }
3802
3803 /*
3804 * TODO: Allow selection of multiple files. Needs another arg to this
3805 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3806 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3807 * files that don't exist yet, so I haven't put it in. What about
3808 * OFN_PATHMUSTEXIST?
3809 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3810 */
3811 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003812# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003813 if (curbuf->b_p_bin)
3814 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003815# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003816 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003817 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003818 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003819 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003820
3821 vim_free(filterp);
3822 vim_free(initdirp);
3823 vim_free(titlep);
3824 vim_free(extp);
3825
K.Takata14b8d6a2022-01-20 15:05:22 +00003826 if (!ret)
3827 return NULL;
3828
3829 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003831 if (p == NULL)
3832 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003833
Bram Moolenaar734a8672019-12-02 22:49:38 +01003834 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 SetFocus(s_hwnd);
3836
Bram Moolenaar734a8672019-12-02 22:49:38 +01003837 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003838 q = vim_strsave(shorten_fname1(p));
3839 vim_free(p);
3840 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003842
3843
3844/*
3845 * Convert the string s to the proper format for a filter string by replacing
3846 * the \t and \n delimiters with \0.
3847 * Returns the converted string in allocated memory.
3848 *
3849 * Keep in sync with convert_filterW() above!
3850 */
3851 static char_u *
3852convert_filter(char_u *s)
3853{
3854 char_u *res;
3855 unsigned s_len = (unsigned)STRLEN(s);
3856 unsigned i;
3857
3858 res = alloc(s_len + 3);
3859 if (res != NULL)
3860 {
3861 for (i = 0; i < s_len; ++i)
3862 if (s[i] == '\t' || s[i] == '\n')
3863 res[i] = '\0';
3864 else
3865 res[i] = s[i];
3866 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003867 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003868 res[s_len + 1] = NUL;
3869 res[s_len + 2] = NUL;
3870 }
3871 return res;
3872}
3873
3874/*
3875 * Select a directory.
3876 */
3877 char_u *
3878gui_mch_browsedir(char_u *title, char_u *initdir)
3879{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003880 // We fake this: Use a filter that doesn't select anything and a default
3881 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3883 initdir, (char_u *)_("Directory\t*.nothing\n"));
3884}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003885#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003886
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003887 static void
3888_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003889 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890 HDROP hDrop)
3891{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003892#define BUFPATHLEN _MAX_PATH
3893#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003894 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003895 char szFile[BUFPATHLEN];
3896 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3897 UINT i;
3898 char_u **fnames;
3899 POINT pt;
3900 int_u modifiers = 0;
3901
Bram Moolenaar734a8672019-12-02 22:49:38 +01003902 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003903 DragQueryPoint(hDrop, &pt);
3904 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3905
3906 reset_VIsual();
3907
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003908 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909
3910 if (fnames != NULL)
3911 for (i = 0; i < cFiles; ++i)
3912 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3914 fnames[i] = utf16_to_enc(wszFile, NULL);
3915 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003916 {
3917 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3918 fnames[i] = vim_strsave((char_u *)szFile);
3919 }
3920 }
3921
3922 DragFinish(hDrop);
3923
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003924 if (fnames == NULL)
3925 return;
LemonBoy45684c62022-04-24 15:46:42 +01003926
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003927 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003929 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
3930 modifiers |= MOUSE_SHIFT;
3931 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
3932 modifiers |= MOUSE_CTRL;
3933 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
3934 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003935
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003936 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3937
3938 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003939}
3940
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003941 static int
3942_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003943 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003944 HWND hwndCtl,
3945 UINT code,
3946 int pos)
3947{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003948 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003949 scrollbar_T *sb, *sb_info;
3950 long val;
3951 int dragging = FALSE;
3952 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003953 SCROLLINFO si;
3954
3955 si.cbSize = sizeof(si);
3956 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003957
3958 sb = gui_mswin_find_scrollbar(hwndCtl);
3959 if (sb == NULL)
3960 return 0;
3961
Bram Moolenaar734a8672019-12-02 22:49:38 +01003962 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003963 {
3964 /*
3965 * Careful: need to get scrollbar info out of first (left) scrollbar
3966 * for window, but keep real scrollbar too because we must pass it to
3967 * gui_drag_scrollbar().
3968 */
3969 sb_info = &sb->wp->w_scrollbars[0];
3970 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003971 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003972 sb_info = sb;
3973 val = sb_info->value;
3974
3975 switch (code)
3976 {
3977 case SB_THUMBTRACK:
3978 val = pos;
3979 dragging = TRUE;
3980 if (sb->scroll_shift > 0)
3981 val <<= sb->scroll_shift;
3982 break;
3983 case SB_LINEDOWN:
3984 val++;
3985 break;
3986 case SB_LINEUP:
3987 val--;
3988 break;
3989 case SB_PAGEDOWN:
3990 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3991 break;
3992 case SB_PAGEUP:
3993 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3994 break;
3995 case SB_TOP:
3996 val = 0;
3997 break;
3998 case SB_BOTTOM:
3999 val = sb_info->max;
4000 break;
4001 case SB_ENDSCROLL:
4002 if (prev_code == SB_THUMBTRACK)
4003 {
4004 /*
4005 * "pos" only gives us 16-bit data. In case of large file,
4006 * use GetScrollPos() which returns 32-bit. Unfortunately it
4007 * is not valid while the scrollbar is being dragged.
4008 */
4009 val = GetScrollPos(hwndCtl, SB_CTL);
4010 if (sb->scroll_shift > 0)
4011 val <<= sb->scroll_shift;
4012 }
4013 break;
4014
4015 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004016 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004017 return 0;
4018 }
4019 prev_code = code;
4020
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004021 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4022 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023
4024 /*
4025 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4026 */
4027 if (sb->wp != NULL)
4028 {
4029 scrollbar_T *sba = sb->wp->w_scrollbars;
4030 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4031
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004032 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004033 }
4034
Bram Moolenaar734a8672019-12-02 22:49:38 +01004035 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004036 s_busy_processing = TRUE;
4037
Bram Moolenaar734a8672019-12-02 22:49:38 +01004038 // When "allow_scrollbar" is FALSE still need to remember the new
4039 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004040 dont_scroll = !allow_scrollbar;
4041
Bram Moolenaara338adc2018-01-31 20:51:47 +01004042 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004043 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004044 mch_enable_flush();
4045 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004046
4047 s_busy_processing = FALSE;
4048 dont_scroll = dont_scroll_save;
4049
4050 return 0;
4051}
4052
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054#ifdef FEAT_XPM_W32
4055# include "xpm_w32.h"
4056#endif
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
Bram Moolenaar734a8672019-12-02 22:49:38 +01004059// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060#define TEAROFF_PADDING_X 2
4061#define TEAROFF_BUTTON_PAD_X 8
4062#define TEAROFF_MIN_WIDTH 200
4063#define TEAROFF_SUBMENU_LABEL ">>"
4064#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4065
4066
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004067#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068# define ID_BEVAL_TOOLTIP 200
4069# define BEVAL_TEXT_LEN MAXPATHL
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004072static UINT_PTR beval_timer_id = 0;
4073static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004074#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004075
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004076
Bram Moolenaar734a8672019-12-02 22:49:38 +01004077// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079#ifdef FEAT_MENU
4080static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083/*
4084 * Use the system font for dialogs and tear-off menus. Remove this line to
4085 * use DLG_FONT_NAME.
4086 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004087#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#define VIM_CLASSW L"Vim"
4091
Bram Moolenaar734a8672019-12-02 22:49:38 +01004092// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4093// thus there should be room for every dialog. For tearoffs it's made bigger
4094// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#define DLG_ALLOC_SIZE 16 * 1024
4096
4097/*
4098 * stuff for dialogs, menus, tearoffs etc.
4099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100static PWORD
4101add_dialog_element(
4102 PWORD p,
4103 DWORD lStyle,
4104 WORD x,
4105 WORD y,
4106 WORD w,
4107 WORD h,
4108 WORD Id,
4109 WORD clss,
4110 const char *caption);
4111static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004112static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004113#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116static void get_dialog_font_metrics(void);
4117
4118static int dialog_default_button = -1;
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120#ifdef FEAT_TOOLBAR
4121static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004122static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004123static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004125#else
4126# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#endif
4128
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004129#ifdef FEAT_GUI_TABLINE
4130static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004131static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004132#endif
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#ifdef FEAT_MBYTE_IME
4135static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4136static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4137#endif
4138#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4139# ifdef NOIME
4140typedef struct tagCOMPOSITIONFORM {
4141 DWORD dwStyle;
4142 POINT ptCurrentPos;
4143 RECT rcArea;
4144} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4145typedef HANDLE HIMC;
4146# endif
4147
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004148static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004149static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4150static HIMC (WINAPI *pImmGetContext)(HWND);
4151static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4152static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4153static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4154static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004155static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4156static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004157static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4158static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004159static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160static void dyn_imm_load(void);
4161#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162# define pImmGetCompositionStringW ImmGetCompositionStringW
4163# define pImmGetContext ImmGetContext
4164# define pImmAssociateContext ImmAssociateContext
4165# define pImmReleaseContext ImmReleaseContext
4166# define pImmGetOpenStatus ImmGetOpenStatus
4167# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004168# define pImmGetCompositionFontW ImmGetCompositionFontW
4169# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170# define pImmSetCompositionWindow ImmSetCompositionWindow
4171# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004172# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#endif
4174
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175#ifdef FEAT_MENU
4176/*
4177 * Figure out how high the menu bar is at the moment.
4178 */
4179 static int
4180gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004181 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 static int old_menu_height = -1;
4184
4185 RECT rc1, rc2;
4186 int num;
4187 int menu_height;
4188
4189 if (gui.menu_is_active)
4190 num = GetMenuItemCount(s_menuBar);
4191 else
4192 num = 0;
4193
4194 if (num == 0)
4195 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004196 else if (IsMinimized(s_hwnd))
4197 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004198 // The height of the menu cannot be determined while the window is
4199 // minimized. Take the previous height if the menu is changed in that
4200 // state, to avoid that Vim's vertical window size accidentally
4201 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004202 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 else
4205 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004206 /*
4207 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4208 * seem to have been set yet, so menu wraps in default window
4209 * width which is very narrow. Instead just return height of a
4210 * single menu item. Will still be wrong when the menu really
4211 * should wrap over more than one line.
4212 */
4213 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4214 if (gui.starting)
4215 menu_height = rc1.bottom - rc1.top + 1;
4216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004218 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4219 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 }
4222
4223 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004224 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004225 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 return menu_height;
4228}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004229#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231
4232/*
4233 * Setup for the Intellimouse
4234 */
LemonBoyc27747e2022-05-07 12:25:40 +01004235 static long
4236mouse_vertical_scroll_step(void)
4237{
4238 UINT val;
4239 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4240 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4241 return 3; // Safe default;
4242}
4243
4244 static long
4245mouse_horizontal_scroll_step(void)
4246{
4247 UINT val;
4248 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4249 return (long)val;
4250 return 3; // Safe default;
4251}
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 static void
4254init_mouse_wheel(void)
4255{
LemonBoyc27747e2022-05-07 12:25:40 +01004256 // Get the default values for the horizontal and vertical scroll steps from
4257 // the system.
4258 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4259 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260}
4261
Bram Moolenaarae20f342019-11-05 21:09:23 +01004262/*
LemonBoya773d842022-05-10 20:54:46 +01004263 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004266_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
LemonBoy365d8f72022-05-05 19:23:07 +01004268 int button;
4269 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004270 int modifiers = 0;
4271 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004272 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4273 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaarae20f342019-11-05 21:09:23 +01004275 wp = gui_mouse_window(FIND_POPUP);
4276
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004277#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004278 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004279 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004280 cmdarg_T cap;
4281 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004282
Bram Moolenaarae20f342019-11-05 21:09:23 +01004283 // Mouse hovers over popup window, scroll it if possible.
4284 mouse_row = wp->w_winrow;
4285 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004286 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004287 if (horizontal)
4288 {
4289 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4290 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4291 }
4292 else
4293 {
4294 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4295 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4296 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004297 clear_oparg(&oa);
4298 cap.oap = &oa;
4299 nv_mousescroll(&cap);
4300 update_screen(0);
4301 setcursor();
4302 out_flush();
4303 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004304 }
4305#endif
4306
Bram Moolenaarae20f342019-11-05 21:09:23 +01004307 if (wp == NULL || !p_scf)
4308 wp = curwin;
4309
LemonBoy365d8f72022-05-05 19:23:07 +01004310 // Translate the scroll event into an event that Vim can process so that
4311 // the user has a chance to map the scrollwheel buttons.
4312 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004313 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 else
LemonBoy365d8f72022-05-05 19:23:07 +01004315 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004316
4317 kbd_modifiers = get_active_modifiers();
4318
4319 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4320 modifiers |= MOUSE_SHIFT;
4321 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4322 modifiers |= MOUSE_CTRL;
4323 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4324 modifiers |= MOUSE_ALT;
4325
LemonBoya773d842022-05-10 20:54:46 +01004326 // The cursor position is relative to the upper-left corner of the screen.
4327 pt.x = GET_X_LPARAM(lParam);
4328 pt.y = GET_Y_LPARAM(lParam);
4329 ScreenToClient(s_textArea, &pt);
4330
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004331 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332}
4333
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004334#ifdef USE_SYSMENU_FONT
4335/*
4336 * Get Menu Font.
4337 * Return OK or FAIL.
4338 */
4339 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004340gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004341{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004342 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004343
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004344 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4345 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004346 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004347 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004348 &nm,
4349 0))
4350 return FAIL;
4351 *lf = nm.lfMenuFont;
4352 return OK;
4353}
4354#endif
4355
4356
4357#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4358/*
4359 * Set the GUI tabline font to the system menu font
4360 */
4361 static void
4362set_tabline_font(void)
4363{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004364 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004365 HFONT font;
4366 HWND hwnd;
4367 HDC hdc;
4368 HFONT hfntOld;
4369 TEXTMETRIC tm;
4370
4371 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4372 return;
4373
K.Takatac81e9bf2022-01-16 14:15:49 +00004374 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004375 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004376
4377 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4378
4379 /*
4380 * Compute the height of the font used for the tab text
4381 */
4382 hwnd = GetDesktopWindow();
4383 hdc = GetWindowDC(hwnd);
4384 hfntOld = SelectFont(hdc, font);
4385
4386 GetTextMetrics(hdc, &tm);
4387
4388 SelectFont(hdc, hfntOld);
4389 ReleaseDC(hwnd, hdc);
4390
4391 /*
4392 * The space used by the tab border and the space between the tab label
4393 * and the tab border is included as 7.
4394 */
4395 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4396}
K.Takatac81e9bf2022-01-16 14:15:49 +00004397#else
4398# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004399#endif
4400
Bram Moolenaar520470a2005-06-16 21:59:56 +00004401/*
4402 * Invoked when a setting was changed.
4403 */
4404 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004405_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004406{
LemonBoy365d8f72022-05-05 19:23:07 +01004407 switch (param)
4408 {
4409 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004410 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004411 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004412 case SPI_SETWHEELSCROLLCHARS:
4413 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004414 break;
4415 case SPI_SETNONCLIENTMETRICS:
4416 set_tabline_font();
4417 break;
4418 default:
4419 break;
4420 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004421 return 0;
4422}
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#ifdef FEAT_NETBEANS_INTG
4425 static void
4426_OnWindowPosChanged(
4427 HWND hwnd,
4428 const LPWINDOWPOS lpwpos)
4429{
4430 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004431 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4434 || lpwpos->cx != cx || lpwpos->cy != cy))
4435 {
4436 x = lpwpos->x;
4437 y = lpwpos->y;
4438 cx = lpwpos->cx;
4439 cy = lpwpos->cy;
4440 netbeans_frame_moved(x, y);
4441 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004442 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004443 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444}
4445#endif
4446
K.Takata4f2417f2021-06-05 16:25:32 +02004447
4448static HWND hwndTip = NULL;
4449
4450 static void
4451show_sizing_tip(int cols, int rows)
4452{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004453 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004454 char buf[32];
4455
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004456 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004457 ti.hwnd = s_hwnd;
4458 ti.uId = (UINT_PTR)s_hwnd;
4459 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4460 ti.lpszText = buf;
4461 sprintf(buf, "%dx%d", cols, rows);
4462 if (hwndTip == NULL)
4463 {
4464 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4465 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4466 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4467 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4468 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4469 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4470 }
4471 else
4472 {
4473 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4474 }
4475 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4476}
4477
4478 static void
4479destroy_sizing_tip(void)
4480{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004481 if (hwndTip == NULL)
4482 return;
4483
4484 DestroyWindow(hwndTip);
4485 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004486}
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 static int
4489_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 UINT fwSide,
4491 LPRECT lprc)
4492{
4493 int w, h;
4494 int valid_w, valid_h;
4495 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004496 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 w = lprc->right - lprc->left;
4499 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004500 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 w_offset = w - valid_w;
4502 h_offset = h - valid_h;
4503
4504 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4505 || fwSide == WMSZ_BOTTOMLEFT)
4506 lprc->left += w_offset;
4507 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4508 || fwSide == WMSZ_BOTTOMRIGHT)
4509 lprc->right -= w_offset;
4510
4511 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4512 || fwSide == WMSZ_TOPRIGHT)
4513 lprc->top += h_offset;
4514 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4515 || fwSide == WMSZ_BOTTOMRIGHT)
4516 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004517
4518 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 return TRUE;
4520}
4521
K.Takata92000e22022-01-20 15:10:57 +00004522#ifdef FEAT_GUI_TABLINE
4523 static void
4524_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4525{
4526 if (gui_mch_showing_tabline())
4527 {
4528 POINT pt;
4529 RECT rect;
4530
4531 /*
4532 * If the cursor is on the tabline, display the tab menu
4533 */
4534 GetCursorPos(&pt);
4535 GetWindowRect(s_textArea, &rect);
4536 if (pt.y < rect.top)
4537 {
4538 show_tabline_popup_menu();
4539 return;
4540 }
4541 }
4542 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4543}
4544
4545 static void
4546_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4547{
4548 /*
4549 * If the user double clicked the tabline, create a new tab
4550 */
4551 if (gui_mch_showing_tabline())
4552 {
4553 POINT pt;
4554 RECT rect;
4555
4556 GetCursorPos(&pt);
4557 GetWindowRect(s_textArea, &rect);
4558 if (pt.y < rect.top)
4559 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4560 }
4561 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4562}
4563#endif
4564
4565 static UINT
4566_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4567{
4568 UINT result;
4569 int x, y;
4570
4571 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4572 if (result != HTCLIENT)
4573 return result;
4574
4575#ifdef FEAT_GUI_TABLINE
4576 if (gui_mch_showing_tabline())
4577 {
4578 RECT rct;
4579
4580 // If the cursor is on the GUI tabline, don't process this event
4581 GetWindowRect(s_textArea, &rct);
4582 if (yPos < rct.top)
4583 return result;
4584 }
4585#endif
4586 (void)gui_mch_get_winpos(&x, &y);
4587 xPos -= x;
4588
4589 if (xPos < 48) // <VN> TODO should use system metric?
4590 return HTBOTTOMLEFT;
4591 else
4592 return HTBOTTOMRIGHT;
4593}
4594
4595#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4596 static LRESULT
4597_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4598{
4599 switch (hdr->code)
4600 {
4601 case TTN_GETDISPINFOW:
4602 case TTN_GETDISPINFO:
4603 {
4604 char_u *str = NULL;
4605 static void *tt_text = NULL;
4606
4607 VIM_CLEAR(tt_text);
4608
4609# ifdef FEAT_GUI_TABLINE
4610 if (gui_mch_showing_tabline()
4611 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4612 {
4613 POINT pt;
4614 /*
4615 * Mouse is over the GUI tabline. Display the
4616 * tooltip for the tab under the cursor
4617 *
4618 * Get the cursor position within the tab control
4619 */
4620 GetCursorPos(&pt);
4621 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4622 {
4623 TCHITTESTINFO htinfo;
4624 int idx;
4625
4626 /*
4627 * Get the tab under the cursor
4628 */
4629 htinfo.pt.x = pt.x;
4630 htinfo.pt.y = pt.y;
4631 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4632 if (idx != -1)
4633 {
4634 tabpage_T *tp;
4635
4636 tp = find_tabpage(idx + 1);
4637 if (tp != NULL)
4638 {
4639 get_tabline_label(tp, TRUE);
4640 str = NameBuff;
4641 }
4642 }
4643 }
4644 }
4645# endif
4646# ifdef FEAT_TOOLBAR
4647# ifdef FEAT_GUI_TABLINE
4648 else
4649# endif
4650 {
4651 UINT idButton;
4652 vimmenu_T *pMenu;
4653
4654 idButton = (UINT) hdr->idFrom;
4655 pMenu = gui_mswin_find_menu(root_menu, idButton);
4656 if (pMenu)
4657 str = pMenu->strings[MENU_INDEX_TIP];
4658 }
4659# endif
4660 if (str == NULL)
4661 break;
4662
4663 // Set the maximum width, this also enables using \n for
4664 // line break.
4665 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4666
4667 if (hdr->code == TTN_GETDISPINFOW)
4668 {
4669 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4670
4671 tt_text = enc_to_utf16(str, NULL);
4672 lpdi->lpszText = tt_text;
4673 // can't show tooltip if failed
4674 }
4675 else
4676 {
4677 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4678
4679 if (STRLEN(str) < sizeof(lpdi->szText)
4680 || ((tt_text = vim_strsave(str)) == NULL))
4681 vim_strncpy((char_u *)lpdi->szText, str,
4682 sizeof(lpdi->szText) - 1);
4683 else
4684 lpdi->lpszText = tt_text;
4685 }
4686 }
4687 break;
4688
4689# ifdef FEAT_GUI_TABLINE
4690 case TCN_SELCHANGE:
4691 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4692 {
4693 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4694 return 0L;
4695 }
4696 break;
4697
4698 case NM_RCLICK:
4699 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4700 {
4701 show_tabline_popup_menu();
4702 return 0L;
4703 }
4704 break;
4705# endif
4706
4707 default:
4708 break;
4709 }
4710 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4711}
4712#endif
4713
4714#if defined(MENUHINTS) && defined(FEAT_MENU)
4715 static LRESULT
4716_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4717{
4718 if (((UINT) HIWORD(wParam)
4719 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4720 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004721 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004722 {
4723 UINT idButton;
4724 vimmenu_T *pMenu;
4725 static int did_menu_tip = FALSE;
4726
4727 if (did_menu_tip)
4728 {
4729 msg_clr_cmdline();
4730 setcursor();
4731 out_flush();
4732 did_menu_tip = FALSE;
4733 }
4734
4735 idButton = (UINT)LOWORD(wParam);
4736 pMenu = gui_mswin_find_menu(root_menu, idButton);
4737 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4738 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4739 {
4740 ++msg_hist_off;
4741 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4742 --msg_hist_off;
4743 setcursor();
4744 out_flush();
4745 did_menu_tip = TRUE;
4746 }
4747 return 0L;
4748 }
4749 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4750}
4751#endif
4752
Ken Takata7086b3e2023-10-02 21:26:03 +02004753 static BOOL
4754_OnGetDpiScaledSize(HWND hwnd, UINT dpi, SIZE *size)
4755{
Ken Takatada5da652023-10-05 20:20:58 +02004756 int old_width, old_height;
4757 int new_width, new_height;
4758 LOGFONTW lf;
4759 HFONT font;
4760
Ken Takata7086b3e2023-10-02 21:26:03 +02004761 //TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
4762
4763 // Calculate new approximate size.
Ken Takatada5da652023-10-05 20:20:58 +02004764 GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
4765 GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
4766 lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
4767 font = CreateFontIndirectW(&lf);
4768 if (font)
4769 {
4770 GetFontSize((GuiFont)font, &new_width, &new_height); // New size
4771 DeleteFont(font);
4772 }
4773 else
4774 {
4775 new_width = old_width;
4776 new_height = old_height;
4777 }
4778 size->cx = size->cx * new_width / old_width;
4779 size->cy = size->cy * new_height / old_height;
Ken Takata7086b3e2023-10-02 21:26:03 +02004780 //TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
4781
Ken Takatada5da652023-10-05 20:20:58 +02004782 return TRUE;
Ken Takata7086b3e2023-10-02 21:26:03 +02004783}
4784
K.Takatac81e9bf2022-01-16 14:15:49 +00004785 static LRESULT
Ken Takata7086b3e2023-10-02 21:26:03 +02004786_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
K.Takatac81e9bf2022-01-16 14:15:49 +00004787{
4788 s_dpi = ydpi;
4789 s_in_dpichanged = TRUE;
4790 //TRACE("DPI: %d", ydpi);
4791
Ken Takata7086b3e2023-10-02 21:26:03 +02004792 s_suggested_rect = *rc;
4793 //TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
4794 // rc->right - rc->left, rc->bottom - rc->top);
4795
K.Takatac81e9bf2022-01-16 14:15:49 +00004796 update_scrollbar_size();
4797 update_toolbar_size();
4798 set_tabline_font();
4799
4800 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4801 gui_get_wide_font();
4802 gui_mswin_get_menu_height(FALSE);
4803#ifdef FEAT_MBYTE_IME
4804 im_set_position(gui.row, gui.col);
4805#endif
4806 InvalidateRect(hwnd, NULL, TRUE);
4807
4808 s_in_dpichanged = FALSE;
4809 return 0L;
4810}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812
4813 static LRESULT CALLBACK
4814_WndProc(
4815 HWND hwnd,
4816 UINT uMsg,
4817 WPARAM wParam,
4818 LPARAM lParam)
4819{
LemonBoya773d842022-05-10 20:54:46 +01004820 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4821 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 HandleMouseHide(uMsg, lParam);
4824
4825 s_uMsg = uMsg;
4826 s_wParam = wParam;
4827 s_lParam = lParam;
4828
4829 switch (uMsg)
4830 {
4831 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4832 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004833 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004835 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4837 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4838 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4839 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4840#ifdef FEAT_MENU
4841 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4842#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004843 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4844 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4846 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004847 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4848 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4850 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4851 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4852#ifdef FEAT_NETBEANS_INTG
4853 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4854#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004855#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004856 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4857 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004858#endif
K.Takata92000e22022-01-20 15:10:57 +00004859 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004860
Bram Moolenaar734a8672019-12-02 22:49:38 +01004861 case WM_QUERYENDSESSION: // System wants to go down.
4862 gui_shell_closed(); // Will exit when no changed buffers.
4863 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004866 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004869 return 0L;
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 break;
4872
4873 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004874 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4875 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004876 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return 0L;
4878
4879 case WM_SYSCHAR:
4880 /*
4881 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4882 * shortcut key, handle like a typed ALT key, otherwise call Windows
4883 * ALT key handling.
4884 */
4885#ifdef FEAT_MENU
4886 if ( !gui.menu_is_active
4887 || p_wak[0] == 'n'
4888 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4889 )
4890#endif
4891 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004892 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return 0L;
4894 }
4895#ifdef FEAT_MENU
4896 else
K.Takata4ac893f2022-01-20 12:44:28 +00004897 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
4899
4900 case WM_SYSKEYUP:
4901#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004902 // This used to be done only when menu is active: ALT key is used for
4903 // that. But that caused problems when menu is disabled and using
4904 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4905 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004906 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004908 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909#endif
4910
K.Takata4f2417f2021-06-05 16:25:32 +02004911 case WM_EXITSIZEMOVE:
4912 destroy_sizing_tip();
4913 break;
4914
Bram Moolenaar734a8672019-12-02 22:49:38 +01004915 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004919 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004920 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004921 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
Bram Moolenaar734a8672019-12-02 22:49:38 +01004923 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004924 case WM_SETTINGCHANGE:
4925 return _OnSettingChange((UINT)wParam);
4926
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004927#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004929 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930#endif
K.Takata92000e22022-01-20 15:10:57 +00004931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#if defined(MENUHINTS) && defined(FEAT_MENU)
4933 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004934 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937#ifdef FEAT_MBYTE_IME
4938 case WM_IME_NOTIFY:
4939 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004940 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004941 return 1L;
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 case WM_IME_COMPOSITION:
4944 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004945 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004946 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#endif
Ken Takata7086b3e2023-10-02 21:26:03 +02004948 case WM_GETDPISCALEDSIZE:
4949 return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
K.Takatac81e9bf2022-01-16 14:15:49 +00004950 case WM_DPICHANGED:
4951 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4952 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004956 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958#endif
K.Takata92000e22022-01-20 15:10:57 +00004959 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961
K.Takata4ac893f2022-01-20 12:44:28 +00004962 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963}
4964
4965/*
4966 * End of call-back routines
4967 */
4968
Bram Moolenaar734a8672019-12-02 22:49:38 +01004969// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970HWND vim_parent_hwnd = NULL;
4971
4972 static BOOL CALLBACK
4973FindWindowTitle(HWND hwnd, LPARAM lParam)
4974{
4975 char buf[2048];
4976 char *title = (char *)lParam;
4977
4978 if (GetWindowText(hwnd, buf, sizeof(buf)))
4979 {
4980 if (strstr(buf, title) != NULL)
4981 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004982 // Found it. Store the window ref. and quit searching if MDI
4983 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004985 if (vim_parent_hwnd != NULL)
4986 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004989 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990}
4991
4992/*
4993 * Invoked for '-P "title"' argument: search for parent application to open
4994 * our window in.
4995 */
4996 void
4997gui_mch_set_parent(char *title)
4998{
4999 EnumWindows(FindWindowTitle, (LPARAM)title);
5000 if (vim_parent_hwnd == NULL)
5001 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005002 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 mch_exit(2);
5004 }
5005}
5006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005007#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 static void
5009ole_error(char *arg)
5010{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005011 char buf[IOSIZE];
5012
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005013# ifdef VIMDLL
5014 gui.in_use = mch_is_gui_executable();
5015# endif
5016
Bram Moolenaar734a8672019-12-02 22:49:38 +01005017 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005018 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005019 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005020 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005024#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5025 static char *
5026gvim_error(void)
5027{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005028 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005029
5030 if (starting)
5031 {
5032 mch_errmsg(msg);
5033 mch_errmsg("\n");
5034 mch_exit(2);
5035 }
5036 return msg;
5037}
5038
5039 char *
5040gui_mch_do_spawn(char_u *arg)
5041{
5042 int len;
5043# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5044 char_u *session = NULL;
5045 LPWSTR tofree1 = NULL;
5046# endif
5047 WCHAR name[MAX_PATH];
5048 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5049 STARTUPINFOW si = {sizeof(si)};
5050 PROCESS_INFORMATION pi;
5051
5052 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5053 goto error;
5054 p = wcsrchr(name, L'\\');
5055 if (p == NULL)
5056 goto error;
5057 // Replace the executable name from vim(d).exe to gvim(d).exe.
5058# ifdef DEBUG
5059 wcscpy(p + 1, L"gvimd.exe");
5060# else
5061 wcscpy(p + 1, L"gvim.exe");
5062# endif
5063
5064# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5065 if (starting)
5066# endif
5067 {
5068 // Pass the command line to the new process.
5069 p = GetCommandLineW();
5070 // Skip 1st argument.
5071 while (*p && *p != L' ' && *p != L'\t')
5072 {
5073 if (*p == L'"')
5074 {
5075 while (*p && *p != L'"')
5076 ++p;
5077 if (*p)
5078 ++p;
5079 }
5080 else
5081 ++p;
5082 }
5083 cmd = p;
5084 }
5085# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5086 else
5087 {
5088 // Create a session file and pass it to the new process.
5089 LPWSTR wsession;
5090 char_u *savebg;
5091 int ret;
5092
5093 session = vim_tempname('s', FALSE);
5094 if (session == NULL)
5095 goto error;
5096 savebg = p_bg;
5097 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5098 ret = write_session_file(session);
5099 vim_free(p_bg);
5100 p_bg = savebg;
5101 if (!ret)
5102 goto error;
5103 wsession = enc_to_utf16(session, NULL);
5104 if (wsession == NULL)
5105 goto error;
5106 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005107 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005108 if (cmd == NULL)
5109 {
5110 vim_free(wsession);
5111 goto error;
5112 }
5113 tofree1 = cmd;
5114 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5115 wsession, wsession);
5116 vim_free(wsession);
5117 }
5118# endif
5119
5120 // Check additional arguments to the `:gui` command.
5121 if (arg != NULL)
5122 {
5123 warg = enc_to_utf16(arg, NULL);
5124 if (warg == NULL)
5125 goto error;
5126 tofree2 = warg;
5127 }
5128 else
5129 warg = L"";
5130
5131 // Set up the new command line.
5132 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005133 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005134 if (newcmd == NULL)
5135 goto error;
5136 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5137
5138 // Spawn a new GUI process.
5139 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5140 NULL, NULL, &si, &pi))
5141 goto error;
5142 CloseHandle(pi.hProcess);
5143 CloseHandle(pi.hThread);
5144 mch_exit(0);
5145
5146error:
5147# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5148 if (session)
5149 mch_remove(session);
5150 vim_free(session);
5151 vim_free(tofree1);
5152# endif
5153 vim_free(newcmd);
5154 vim_free(tofree2);
5155 return gvim_error();
5156}
5157#endif
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159/*
5160 * Parse the GUI related command-line arguments. Any arguments used are
5161 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005162 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 */
5164 void
5165gui_mch_prepare(int *argc, char **argv)
5166{
5167 int silent = FALSE;
5168 int idx;
5169
Bram Moolenaar734a8672019-12-02 22:49:38 +01005170 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5172 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005173 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5175 && (argv[2][0] == '-' || argv[2][0] == '/'))
5176 {
5177 silent = TRUE;
5178 idx = 2;
5179 }
5180 else
5181 idx = 1;
5182
Bram Moolenaar734a8672019-12-02 22:49:38 +01005183 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (STRICMP(argv[idx] + 1, "register") == 0)
5185 {
5186#ifdef FEAT_OLE
5187 RegisterMe(silent);
5188 mch_exit(0);
5189#else
5190 if (!silent)
5191 ole_error("register");
5192 mch_exit(2);
5193#endif
5194 }
5195
Bram Moolenaar734a8672019-12-02 22:49:38 +01005196 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5198 {
5199#ifdef FEAT_OLE
5200 UnregisterMe(!silent);
5201 mch_exit(0);
5202#else
5203 if (!silent)
5204 ole_error("unregister");
5205 mch_exit(2);
5206#endif
5207 }
5208
Bram Moolenaar734a8672019-12-02 22:49:38 +01005209 // Ignore an -embedding argument. It is only relevant if the
5210 // application wants to treat the case when it is started manually
5211 // differently from the case where it is started via automation (and
5212 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5214 {
5215#ifdef FEAT_OLE
5216 *argc = 1;
5217#else
5218 ole_error("embedding");
5219 mch_exit(2);
5220#endif
5221 }
5222 }
5223
5224#ifdef FEAT_OLE
5225 {
5226 int bDoRestart = FALSE;
5227
5228 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005229 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (bDoRestart)
5231 mch_exit(0);
5232 }
5233#endif
5234
5235#ifdef FEAT_NETBEANS_INTG
5236 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005237 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 int arg;
5239
5240 for (arg = 1; arg < *argc; arg++)
5241 if (strncmp("-nb", argv[arg], 3) == 0)
5242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 netbeansArg = argv[arg];
5244 mch_memmove(&argv[arg], &argv[arg + 1],
5245 (--*argc - arg) * sizeof(char *));
5246 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005247 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251}
5252
K.Takatac81e9bf2022-01-16 14:15:49 +00005253 static void
5254load_dpi_func(void)
5255{
5256 HMODULE hUser32;
5257
5258 hUser32 = GetModuleHandle("user32.dll");
5259 if (hUser32 == NULL)
5260 goto fail;
5261
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005262 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5263 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5264 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005265 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005266 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5267 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005268
5269 if (pSetThreadDpiAwarenessContext != NULL)
5270 {
5271 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5272 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5273 if (oldctx != NULL)
5274 {
5275 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5276 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5277#ifdef DEBUG
5278 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5279 {
5280 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5281 }
5282#endif
5283 return;
5284 }
5285 }
5286
5287fail:
5288 // Disable PerMonitorV2 APIs.
5289 pGetDpiForSystem = stubGetDpiForSystem;
5290 pGetDpiForWindow = NULL;
5291 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5292 pSetThreadDpiAwarenessContext = NULL;
5293 pGetAwarenessFromDpiAwarenessContext = NULL;
5294}
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296/*
5297 * Initialise the GUI. Create all the windows, set up all the call-backs
5298 * etc.
5299 */
5300 int
5301gui_mch_init(void)
5302{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005304 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar734a8672019-12-02 22:49:38 +01005307 // Return here if the window was already opened (happens when
5308 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005310 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 /*
5313 * Load the tearoff bitmap
5314 */
5315#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005316 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
5318
K.Takatac81e9bf2022-01-16 14:15:49 +00005319 load_dpi_func();
5320
5321 s_dpi = pGetDpiForSystem();
5322 update_scrollbar_size();
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005325 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#endif
5327 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005328#ifdef FEAT_TOOLBAR
5329 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5333
Bram Moolenaar734a8672019-12-02 22:49:38 +01005334 // First try using the wide version, so that we can use any title.
5335 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005336 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005338 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 wndclassw.lpfnWndProc = _WndProc;
5340 wndclassw.cbClsExtra = 0;
5341 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005342 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5344 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5345 wndclassw.hbrBackground = s_brush;
5346 wndclassw.lpszMenuName = NULL;
5347 wndclassw.lpszClassName = szVimWndClassW;
5348
K.Takata4ac893f2022-01-20 12:44:28 +00005349 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 if (vim_parent_hwnd != NULL)
5354 {
5355#ifdef HAVE_TRY_EXCEPT
5356 __try
5357 {
5358#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005359 // Open inside the specified parent window.
5360 // TODO: last argument should point to a CLIENTCREATESTRUCT
5361 // structure.
5362 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005364 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005365 WS_OVERLAPPEDWINDOW | WS_CHILD
5366 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5368 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005369 100, // Any value will do
5370 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005372 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#ifdef HAVE_TRY_EXCEPT
5374 }
5375 __except(EXCEPTION_EXECUTE_HANDLER)
5376 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005377 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379#endif
5380 if (s_hwnd == NULL)
5381 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005382 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 mch_exit(2);
5384 }
5385 }
5386 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005387 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005388 // If the provided windowid is not valid reset it to zero, so that it
5389 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005390 if (IsWindow((HWND)win_socket_id) <= 0)
5391 win_socket_id = 0;
5392
Bram Moolenaar734a8672019-12-02 22:49:38 +01005393 // Create a window. If win_socket_id is not zero without border and
5394 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005395 s_hwnd = CreateWindowW(
5396 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005397 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5398 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005399 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5400 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005401 100, // Any value will do
5402 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005403 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005404 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005405 if (s_hwnd != NULL && win_socket_id != 0)
5406 {
5407 SetParent(s_hwnd, (HWND)win_socket_id);
5408 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5409 }
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 if (s_hwnd == NULL)
5413 return FAIL;
5414
K.Takatac81e9bf2022-01-16 14:15:49 +00005415 if (pGetDpiForWindow != NULL)
5416 {
5417 s_dpi = pGetDpiForWindow(s_hwnd);
5418 update_scrollbar_size();
5419 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5420 }
5421
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5423 dyn_imm_load();
5424#endif
5425
Bram Moolenaar734a8672019-12-02 22:49:38 +01005426 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005427 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005428 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005429 wndclassw.style = CS_OWNDC;
5430 wndclassw.lpfnWndProc = _TextAreaWndProc;
5431 wndclassw.cbClsExtra = 0;
5432 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005433 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005434 wndclassw.hIcon = NULL;
5435 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5436 wndclassw.hbrBackground = NULL;
5437 wndclassw.lpszMenuName = NULL;
5438 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005439
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005440 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return FAIL;
5442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005444 s_textArea = CreateWindowExW(
5445 0,
5446 szTextAreaClassW, L"Vim text area",
5447 WS_CHILD | WS_VISIBLE, 0, 0,
5448 100, // Any value will do for now
5449 100, // Any value will do for now
5450 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005451 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if (s_textArea == NULL)
5454 return FAIL;
5455
Bram Moolenaar20321902016-02-17 12:30:17 +01005456#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005457 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005458 {
5459 HANDLE hIcon = NULL;
5460
5461 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005462 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005463 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005464#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005465
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#ifdef FEAT_MENU
5467 s_menuBar = CreateMenu();
5468#endif
5469 s_hdc = GetDC(s_textArea);
5470
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaar734a8672019-12-02 22:49:38 +01005473 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005474 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
Bram Moolenaar734a8672019-12-02 22:49:38 +01005476 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 gui_mch_def_colors();
5478
Bram Moolenaar734a8672019-12-02 22:49:38 +01005479 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5480 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 set_normal_colors();
5482
5483 /*
5484 * Check that none of the colors are the same as the background color.
5485 * Then store the current values as the defaults.
5486 */
5487 gui_check_colors();
5488 gui.def_norm_pixel = gui.norm_pixel;
5489 gui.def_back_pixel = gui.back_pixel;
5490
Bram Moolenaar734a8672019-12-02 22:49:38 +01005491 // Get the colors for the highlight groups (gui_check_colors() might have
5492 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 highlight_gui_started();
5494
5495 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005496 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005498 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500 /*
5501 * Set up for Intellimouse processing
5502 */
5503 init_mouse_wheel();
5504
5505 /*
5506 * compute a couple of metrics used for the dialogs
5507 */
5508 get_dialog_font_metrics();
5509#ifdef FEAT_TOOLBAR
5510 /*
5511 * Create the toolbar
5512 */
5513 initialise_toolbar();
5514#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005515#ifdef FEAT_GUI_TABLINE
5516 /*
5517 * Create the tabline
5518 */
5519 initialise_tabline();
5520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef MSWIN_FIND_REPLACE
5522 /*
5523 * Initialise the dialog box stuff
5524 */
5525 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5526
Bram Moolenaar734a8672019-12-02 22:49:38 +01005527 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005529 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005531 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5533 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5534 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005537#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005538 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005539 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005540#endif
5541
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005542#ifdef FEAT_RENDER_OPTIONS
5543 if (p_rop)
5544 (void)gui_mch_set_rendering_options(p_rop);
5545#endif
5546
Bram Moolenaar748bf032005-02-02 23:04:36 +00005547theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005548 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005549 display_errors();
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return OK;
5552}
5553
5554/*
5555 * Get the size of the screen, taking position on multiple monitors into
5556 * account (if supported).
5557 */
5558 static void
5559get_work_area(RECT *spi_rect)
5560{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005561 HMONITOR mon;
5562 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar734a8672019-12-02 22:49:38 +01005564 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005565 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005566 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005568 moninfo.cbSize = sizeof(MONITORINFO);
5569 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005571 *spi_rect = moninfo.rcWork;
5572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005575 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5577}
5578
5579/*
5580 * Set the size of the window to the given width and height in pixels.
5581 */
5582 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005583gui_mch_set_shellsize(
5584 int width,
5585 int height,
5586 int min_width UNUSED,
5587 int min_height UNUSED,
5588 int base_width UNUSED,
5589 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005590 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005593 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
Bram Moolenaar734a8672019-12-02 22:49:38 +01005596 // Try to keep window completely on screen.
5597 // Get position of the screen work area. This is the part that is not
5598 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 get_work_area(&workarea_rect);
5600
Bram Moolenaar734a8672019-12-02 22:49:38 +01005601 // Resizing a maximized window looks very strange, unzoom it first.
5602 // But don't do it when still starting up, it may have been requested in
5603 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005604 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005606
Ken Takata7086b3e2023-10-02 21:26:03 +02005607 if (s_in_dpichanged)
5608 // Use the suggested position when in WM_DPICHANGED.
5609 window_rect = s_suggested_rect;
5610 else
5611 // Use current position.
5612 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar734a8672019-12-02 22:49:38 +01005614 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005615 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5616 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5617 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5618 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5619 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5620 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar734a8672019-12-02 22:49:38 +01005622 // The following should take care of keeping Vim on the same monitor, no
5623 // matter if the secondary monitor is left or right of the primary
5624 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005625 window_rect.right = window_rect.left + win_width;
5626 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005627
Bram Moolenaar734a8672019-12-02 22:49:38 +01005628 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005629 // Don't adjust the position when in WM_DPICHANGED.
5630 if (!s_in_dpichanged)
5631 {
5632 if ((direction & RESIZE_HOR)
5633 && window_rect.right > workarea_rect.right)
5634 OffsetRect(&window_rect,
5635 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Ken Takata7086b3e2023-10-02 21:26:03 +02005637 if ((direction & RESIZE_HOR)
5638 && window_rect.left < workarea_rect.left)
5639 OffsetRect(&window_rect,
5640 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Ken Takata7086b3e2023-10-02 21:26:03 +02005642 if ((direction & RESIZE_VERT)
5643 && window_rect.bottom > workarea_rect.bottom)
5644 OffsetRect(&window_rect,
5645 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Ken Takata7086b3e2023-10-02 21:26:03 +02005647 if ((direction & RESIZE_VERT)
5648 && window_rect.top < workarea_rect.top)
5649 OffsetRect(&window_rect,
5650 0, workarea_rect.top - window_rect.top);
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005653 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5654 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
Ken Takata7086b3e2023-10-02 21:26:03 +02005656 //TRACE("New pos: %d,%d New size: %d,%d",
5657 // window_rect.left, window_rect.top, win_width, win_height);
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 SetActiveWindow(s_hwnd);
5660 SetFocus(s_hwnd);
5661
Bram Moolenaar734a8672019-12-02 22:49:38 +01005662 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
5666
5667 void
5668gui_mch_set_scrollbar_thumb(
5669 scrollbar_T *sb,
5670 long val,
5671 long size,
5672 long max)
5673{
5674 SCROLLINFO info;
5675
5676 sb->scroll_shift = 0;
5677 while (max > 32767)
5678 {
5679 max = (max + 1) >> 1;
5680 val >>= 1;
5681 size >>= 1;
5682 ++sb->scroll_shift;
5683 }
5684
5685 if (sb->scroll_shift > 0)
5686 ++size;
5687
5688 info.cbSize = sizeof(info);
5689 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5690 info.nPos = val;
5691 info.nMin = 0;
5692 info.nMax = max;
5693 info.nPage = size;
5694 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5695}
5696
5697
5698/*
5699 * Set the current text font.
5700 */
5701 void
5702gui_mch_set_font(GuiFont font)
5703{
5704 gui.currFont = font;
5705}
5706
5707
5708/*
5709 * Set the current text foreground color.
5710 */
5711 void
5712gui_mch_set_fg_color(guicolor_T color)
5713{
5714 gui.currFgColor = color;
5715}
5716
5717/*
5718 * Set the current text background color.
5719 */
5720 void
5721gui_mch_set_bg_color(guicolor_T color)
5722{
5723 gui.currBgColor = color;
5724}
5725
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005726/*
5727 * Set the current text special color.
5728 */
5729 void
5730gui_mch_set_sp_color(guicolor_T color)
5731{
5732 gui.currSpColor = color;
5733}
5734
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005735#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736/*
5737 * Multi-byte handling, originally by Sung-Hoon Baek.
5738 * First static functions (no prototypes generated).
5739 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005740# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005741# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005742# endif
5743# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * handle WM_IME_NOTIFY message
5747 */
5748 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005749_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 LRESULT lResult = 0;
5752 HIMC hImc;
5753
5754 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5755 return lResult;
5756 switch (dwCommand)
5757 {
5758 case IMN_SETOPENSTATUS:
5759 if (pImmGetOpenStatus(hImc))
5760 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005761 LOGFONTW lf = norm_logfont;
5762 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5763 // Work around when PerMonitorV2 is not enabled in the process level.
5764 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5765 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 im_set_position(gui.row, gui.col);
5767
Bram Moolenaar734a8672019-12-02 22:49:38 +01005768 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005769 State &= ~MODE_LANGMAP;
5770 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005772# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005773 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5775 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005776 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 int old_row = gui.row;
5778 int old_col = gui.col;
5779
5780 // This must be called here before
5781 // status_redraw_curbuf(), otherwise the mode
5782 // message may appear in the wrong position.
5783 showmode();
5784 status_redraw_curbuf();
5785 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005786 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 gui.row = old_row;
5788 gui.col = old_col;
5789 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 }
5792 }
5793 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005794 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 lResult = 0;
5796 break;
5797 }
5798 pImmReleaseContext(hWnd, hImc);
5799 return lResult;
5800}
5801
5802 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005803_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 char_u *ret;
5806 int len;
5807
Bram Moolenaar734a8672019-12-02 22:49:38 +01005808 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 return 0;
5810
5811 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5812 if (ret != NULL)
5813 {
5814 add_to_input_buf_csi(ret, len);
5815 vim_free(ret);
5816 return 1;
5817 }
5818 return 0;
5819}
5820
5821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 * void GetResultStr()
5823 *
5824 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5825 * get complete composition string
5826 */
5827 static char_u *
5828GetResultStr(HWND hwnd, int GCS, int *lenp)
5829{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005830 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005831 LONG ret;
5832 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 char_u *convbuf = NULL;
5834
5835 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5836 return NULL;
5837
K.Takatab0b2b732022-01-19 12:59:21 +00005838 // Get the length of the composition string.
5839 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5840 if (ret <= 0)
5841 return NULL;
5842
5843 // Allocate the requested buffer plus space for the NUL character.
5844 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 if (buf == NULL)
5846 return NULL;
5847
K.Takatab0b2b732022-01-19 12:59:21 +00005848 // Reads in the composition string.
5849 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5850 *lenp = ret / sizeof(WCHAR);
5851
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005852 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 pImmReleaseContext(hwnd, hIMC);
5854 vim_free(buf);
5855 return convbuf;
5856}
5857#endif
5858
Bram Moolenaar734a8672019-12-02 22:49:38 +01005859// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005860#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862/*
5863 * set font to IM.
5864 */
5865 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005866im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
5868 HIMC hImc;
5869
5870 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5871 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005872 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 pImmReleaseContext(s_hwnd, hImc);
5874 }
5875}
5876
5877/*
5878 * Notify cursor position to IM.
5879 */
5880 void
5881im_set_position(int row, int col)
5882{
5883 HIMC hImc;
5884
5885 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5886 {
5887 COMPOSITIONFORM cfs;
5888
5889 cfs.dwStyle = CFS_POINT;
5890 cfs.ptCurrentPos.x = FILL_X(col);
5891 cfs.ptCurrentPos.y = FILL_Y(row);
5892 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005893 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5894 {
5895 // Work around when PerMonitorV2 is not enabled in the process level.
5896 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5897 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 pImmSetCompositionWindow(hImc, &cfs);
5900
5901 pImmReleaseContext(s_hwnd, hImc);
5902 }
5903}
5904
5905/*
5906 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5907 */
5908 void
5909im_set_active(int active)
5910{
5911 HIMC hImc;
5912 static HIMC hImcOld = (HIMC)0;
5913
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005914# ifdef VIMDLL
5915 if (!gui.in_use && !gui.starting)
5916 {
5917 mbyte_im_set_active(active);
5918 return;
5919 }
5920# endif
5921
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005922 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
5923 return;
5924
5925 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005927 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005929 hImcOld = pImmGetContext(s_hwnd);
5930 if (hImcOld)
5931 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005933 active = FALSE;
5934 }
5935 else if (hImcOld != (HIMC)0)
5936 {
5937 pImmAssociateContext(s_hwnd, hImcOld);
5938 hImcOld = (HIMC)0;
5939 }
5940
5941 hImc = pImmGetContext(s_hwnd);
5942 if (!hImc)
5943 return;
5944
5945 /*
5946 * for Korean ime
5947 */
5948 HKL hKL = GetKeyboardLayout(0);
5949
5950 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5951 {
5952 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5953 static BOOL bSaved = FALSE;
5954
5955 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005957 // if we have a saved conversion status, restore it
5958 if (bSaved)
5959 pImmSetConversionStatus(hImc, dwConversionSaved,
5960 dwSentenceSaved);
5961 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005965 // save conversion status and disable korean
5966 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5967 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00005968 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005969 bSaved = TRUE;
5970 pImmSetConversionStatus(hImc,
5971 dwConversionSaved & ~(IME_CMODE_NATIVE
5972 | IME_CMODE_FULLSHAPE),
5973 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00005974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005977
5978 pImmSetOpenStatus(hImc, active);
5979 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981
5982/*
5983 * Get IM status. When IM is on, return not 0. Else return 0.
5984 */
5985 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005986im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
5988 int status = 0;
5989 HIMC hImc;
5990
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005991# ifdef VIMDLL
5992 if (!gui.in_use && !gui.starting)
5993 return mbyte_im_get_status();
5994# endif
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5997 {
5998 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5999 pImmReleaseContext(s_hwnd, hImc);
6000 }
6001 return status;
6002}
6003
Bram Moolenaar734a8672019-12-02 22:49:38 +01006004#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006007/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006008 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006009 */
6010 static void
6011latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6012{
6013 int c;
6014
Bram Moolenaarca003e12006-03-17 23:19:38 +00006015 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006016 {
6017 c = *text++;
6018 switch (c)
6019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006020 case 0xa4: c = 0x20ac; break; // euro
6021 case 0xa6: c = 0x0160; break; // S hat
6022 case 0xa8: c = 0x0161; break; // S -hat
6023 case 0xb4: c = 0x017d; break; // Z hat
6024 case 0xb8: c = 0x017e; break; // Z -hat
6025 case 0xbc: c = 0x0152; break; // OE
6026 case 0xbd: c = 0x0153; break; // oe
6027 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006028 }
6029 *unicodebuf++ = c;
6030 }
6031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033#ifdef FEAT_RIGHTLEFT
6034/*
6035 * What is this for? In the case where you are using Win98 or Win2K or later,
6036 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6037 * reverses the string sent to the TextOut... family. This sucks, because we
6038 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6039 * way to tell Windblows not to do this!
6040 *
6041 * The short of it is that this 'RevOut' only gets called if you are running
6042 * one of the new, "improved" MS OSes, and only if you are running in
6043 * 'rightleft' mode. It makes display take *slightly* longer, but not
6044 * noticeably so.
6045 */
6046 static void
K.Takata135e1522022-01-29 15:27:58 +00006047RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 int col,
6049 int row,
6050 UINT foptions,
6051 CONST RECT *pcliprect,
6052 LPCTSTR text,
6053 UINT len,
6054 CONST INT *padding)
6055{
6056 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006058 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006059 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006060 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061}
6062#endif
6063
Bram Moolenaar92467d32017-12-05 13:22:16 +01006064 static void
6065draw_line(
6066 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006067 int y1,
6068 int x2,
6069 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006070 COLORREF color)
6071{
6072#if defined(FEAT_DIRECTX)
6073 if (IS_ENABLE_DIRECTX())
6074 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6075 else
6076#endif
6077 {
6078 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6079 HPEN old_pen = SelectObject(s_hdc, hpen);
6080 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006081 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006082 LineTo(s_hdc, x2, y2);
6083 DeleteObject(SelectObject(s_hdc, old_pen));
6084 }
6085}
6086
6087 static void
6088set_pixel(
6089 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006090 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006091 COLORREF color)
6092{
6093#if defined(FEAT_DIRECTX)
6094 if (IS_ENABLE_DIRECTX())
6095 DWriteContext_SetPixel(s_dwc, x, y, color);
6096 else
6097#endif
6098 SetPixel(s_hdc, x, y, color);
6099}
6100
6101 static void
6102fill_rect(
6103 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006104 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006105 COLORREF color)
6106{
6107#if defined(FEAT_DIRECTX)
6108 if (IS_ENABLE_DIRECTX())
6109 DWriteContext_FillRect(s_dwc, rcp, color);
6110 else
6111#endif
6112 {
6113 HBRUSH hbr2;
6114
6115 if (hbr == NULL)
6116 hbr2 = CreateSolidBrush(color);
6117 else
6118 hbr2 = hbr;
6119 FillRect(s_hdc, rcp, hbr2);
6120 if (hbr == NULL)
6121 DeleteBrush(hbr2);
6122 }
6123}
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 void
6126gui_mch_draw_string(
6127 int row,
6128 int col,
6129 char_u *text,
6130 int len,
6131 int flags)
6132{
6133 static int *padding = NULL;
6134 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 const RECT *pcliprect = NULL;
6136 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 static WCHAR *unicodebuf = NULL;
6138 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006139 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 int y;
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 /*
6144 * Italic and bold text seems to have an extra row of pixels at the bottom
6145 * (below where the bottom of the character should be). If we draw the
6146 * characters with a solid background, the top row of pixels in the
6147 * character below will be overwritten. We can fix this by filling in the
6148 * background ourselves, to the correct character proportions, and then
6149 * writing the character in transparent mode. Still have a problem when
6150 * the character is "_", which gets written on to the character below.
6151 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6152 * pixel in their slots, which fixes the problem with the bottom row of
6153 * pixels. We still need this code because otherwise the top row of pixels
6154 * becomes a problem. - webb.
6155 */
6156 static HBRUSH hbr_cache[2] = {NULL, NULL};
6157 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6158 static int brush_lru = 0;
6159 HBRUSH hbr;
6160 RECT rc;
6161
6162 if (!(flags & DRAW_TRANSP))
6163 {
6164 /*
6165 * Clear background first.
6166 * Note: FillRect() excludes right and bottom of rectangle.
6167 */
6168 rc.left = FILL_X(col);
6169 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (has_mbyte)
6171 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006172 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006173 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
6175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 rc.right = FILL_X(col + len);
6177 rc.bottom = FILL_Y(row + 1);
6178
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179 // Cache the created brush, that saves a lot of time. We need two:
6180 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (gui.currBgColor == brush_color[0])
6182 {
6183 hbr = hbr_cache[0];
6184 brush_lru = 1;
6185 }
6186 else if (gui.currBgColor == brush_color[1])
6187 {
6188 hbr = hbr_cache[1];
6189 brush_lru = 0;
6190 }
6191 else
6192 {
6193 if (hbr_cache[brush_lru] != NULL)
6194 DeleteBrush(hbr_cache[brush_lru]);
6195 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6196 brush_color[brush_lru] = gui.currBgColor;
6197 hbr = hbr_cache[brush_lru];
6198 brush_lru = !brush_lru;
6199 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006200
Bram Moolenaar92467d32017-12-05 13:22:16 +01006201 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
6203 SetBkMode(s_hdc, TRANSPARENT);
6204
6205 /*
6206 * When drawing block cursor, prevent inverted character spilling
6207 * over character cell (can happen with bold/italic)
6208 */
6209 if (flags & DRAW_CURSOR)
6210 {
6211 pcliprect = &rc;
6212 foptions = ETO_CLIPPED;
6213 }
6214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 SetTextColor(s_hdc, gui.currFgColor);
6216 SelectFont(s_hdc, gui.currFont);
6217
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006218#ifdef FEAT_DIRECTX
6219 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006220 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006221#endif
6222
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6224 {
K.Takata135e1522022-01-29 15:27:58 +00006225 int i;
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 vim_free(padding);
6228 pad_size = Columns;
6229
Bram Moolenaar734a8672019-12-02 22:49:38 +01006230 // Don't give an out-of-memory message here, it would call us
6231 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006232 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (padding != NULL)
6234 for (i = 0; i < pad_size; i++)
6235 padding[i] = gui.char_width;
6236 }
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 /*
6239 * We have to provide the padding argument because italic and bold versions
6240 * of fixed-width fonts are often one pixel or so wider than their normal
6241 * versions.
6242 * No check for DRAW_BOLD, Windows will have done it already.
6243 */
6244
Bram Moolenaar734a8672019-12-02 22:49:38 +01006245 // Check if there are any UTF-8 characters. If not, use normal text
6246 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (enc_utf8)
6248 for (n = 0; n < len; ++n)
6249 if (text[n] >= 0x80)
6250 break;
6251
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006252#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006253 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6254 // required that unicode drawing routine, currently. So this forces it
6255 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006256 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006257 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006258#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006259
Bram Moolenaar734a8672019-12-02 22:49:38 +01006260 // Check if the Unicode buffer exists and is big enough. Create it
6261 // with the same length as the multi-byte string, the number of wide
6262 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006263 if ((enc_utf8
6264 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6265 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 && (unicodebuf == NULL || len > unibuflen))
6267 {
6268 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006269 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006272 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 unibuflen = len;
6275 }
6276
6277 if (enc_utf8 && n < len && unicodebuf != NULL)
6278 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006279 // Output UTF-8 characters. Composing characters should be
6280 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006281 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006282 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006283 int cells; // cell width of string up to composing char
6284 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006285 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006287 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006289 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006291 c = utf_ptr2char(text + i);
6292 if (c >= 0x10000)
6293 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006294 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006295 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6296 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006297 }
6298 else
6299 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006300 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006301 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006302
6303 if (utf_iscomposing(c))
6304 cw = 0;
6305 else
6306 {
6307 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006308 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006309 cw = 1;
6310 }
6311
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (unicodepdy != NULL)
6313 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006314 // Use unicodepdy to make characters fit as we expect, even
6315 // when the font uses different widths (e.g., bold character
6316 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006317 if (c >= 0x10000)
6318 {
6319 unicodepdy[wlen - 2] = cw * gui.char_width;
6320 unicodepdy[wlen - 1] = 0;
6321 }
6322 else
6323 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006326 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006328#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006329 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006330 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006331 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006332 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006333 TEXT_X(col), TEXT_Y(row),
6334 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006335 gui.char_width, gui.currFgColor,
6336 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006337 }
6338 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006339#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006340 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6341 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006342 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006344 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006346 // If we want to display codepage data, and the current CP is not the
6347 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (unicodebuf != NULL)
6349 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006350 if (enc_latin9)
6351 latin9_to_ucs(text, len, unicodebuf);
6352 else
6353 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 MB_PRECOMPOSED,
6355 (char *)text, len,
6356 (LPWSTR)unicodebuf, unibuflen);
6357 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006358 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006359 // Use unicodepdy to make characters fit as we expect, even
6360 // when the font uses different widths (e.g., bold character
6361 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006362 if (unicodepdy != NULL)
6363 {
6364 int i;
6365 int cw;
6366
6367 for (i = 0; i < len; ++i)
6368 {
6369 cw = utf_char2cells(unicodebuf[i]);
6370 if (cw > 2)
6371 cw = 1;
6372 unicodepdy[i] = cw * gui.char_width;
6373 }
6374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006376 foptions, pcliprect, unicodebuf, len, unicodepdy);
6377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 }
6379 }
6380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 {
6382#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006383 // Windows will mess up RL text, so we have to draw it character by
6384 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006385 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6387 foptions, pcliprect, (char *)text, len, padding);
6388 else
6389#endif
6390 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6391 foptions, pcliprect, (char *)text, len, padding);
6392 }
6393
Bram Moolenaar734a8672019-12-02 22:49:38 +01006394 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (flags & DRAW_UNDERL)
6396 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006397 // When p_linespace is 0, overwrite the bottom row of pixels.
6398 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (p_linespace > 1)
6401 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006402 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006404
Bram Moolenaar734a8672019-12-02 22:49:38 +01006405 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006406 if (flags & DRAW_STRIKE)
6407 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006408 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006409 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006410 }
6411
Bram Moolenaar734a8672019-12-02 22:49:38 +01006412 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006413 if (flags & DRAW_UNDERC)
6414 {
6415 int x;
6416 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006417 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006418
6419 y = FILL_Y(row + 1) - 1;
6420 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6421 {
6422 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006423 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006424 }
6425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426}
6427
6428
6429/*
6430 * Output routines.
6431 */
6432
Bram Moolenaar734a8672019-12-02 22:49:38 +01006433/*
6434 * Flush any output to the screen
6435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 void
6437gui_mch_flush(void)
6438{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006439#if defined(FEAT_DIRECTX)
6440 if (IS_ENABLE_DIRECTX())
6441 DWriteContext_Flush(s_dwc);
6442#endif
6443
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 GdiFlush();
6445}
6446
6447 static void
6448clear_rect(RECT *rcp)
6449{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006450 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451}
6452
6453
Bram Moolenaarc716c302006-01-21 22:12:51 +00006454 void
6455gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6456{
6457 RECT workarea_rect;
6458
6459 get_work_area(&workarea_rect);
6460
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006461 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006462 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6463 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006464
Bram Moolenaar734a8672019-12-02 22:49:38 +01006465 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6466 // the menubar for MSwin, we subtract it from the screen height, so that
6467 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006468 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006469 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6470 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6471 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6472 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006473}
6474
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476#if defined(FEAT_MENU) || defined(PROTO)
6477/*
6478 * Add a sub menu to the menu bar.
6479 */
6480 void
6481gui_mch_add_menu(
6482 vimmenu_T *menu,
6483 int pos)
6484{
6485 vimmenu_T *parent = menu->parent;
6486
6487 menu->submenu_id = CreatePopupMenu();
6488 menu->id = s_menu_id++;
6489
6490 if (menu_is_menubar(menu->name))
6491 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006492 WCHAR *wn;
6493 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006495 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006496 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006499 infow.cbSize = sizeof(infow);
6500 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6501 | MIIM_SUBMENU;
6502 infow.dwItemData = (long_u)menu;
6503 infow.wID = menu->id;
6504 infow.fType = MFT_STRING;
6505 infow.dwTypeData = wn;
6506 infow.cch = (UINT)wcslen(wn);
6507 infow.hSubMenu = menu->submenu_id;
6508 InsertMenuItemW((parent == NULL)
6509 ? s_menuBar : parent->submenu_id,
6510 (UINT)pos, TRUE, &infow);
6511 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 }
6513
Bram Moolenaar734a8672019-12-02 22:49:38 +01006514 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (parent == NULL)
6516 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006517# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 else if (IsWindow(parent->tearoff_handle))
6519 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521}
6522
6523 void
6524gui_mch_show_popupmenu(vimmenu_T *menu)
6525{
6526 POINT mp;
6527
K.Takata45f9cfb2022-01-21 11:11:00 +00006528 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6530}
6531
6532 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006533gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 vimmenu_T *menu = gui_find_menu(path_name);
6536
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006537 if (menu == NULL)
6538 return;
6539
6540 POINT p;
6541
6542 // Find the position of the current cursor
6543 GetDCOrgEx(s_hdc, &p);
6544 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006546 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006548 gui_mch_getmouse(&mx, &my);
6549 p.x += mx;
6550 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006552 else if (curwin != NULL)
6553 {
6554 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6555 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6556 }
6557 msg_scroll = FALSE;
6558 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559}
6560
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006561# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562/*
6563 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6564 * create it as a pseudo-"tearoff menu".
6565 */
6566 void
6567gui_make_tearoff(char_u *path_name)
6568{
6569 vimmenu_T *menu = gui_find_menu(path_name);
6570
Bram Moolenaar734a8672019-12-02 22:49:38 +01006571 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (menu != NULL)
6573 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6574}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577/*
6578 * Add a menu item to a menu
6579 */
6580 void
6581gui_mch_add_menu_item(
6582 vimmenu_T *menu,
6583 int idx)
6584{
6585 vimmenu_T *parent = menu->parent;
6586
6587 menu->id = s_menu_id++;
6588 menu->submenu_id = NULL;
6589
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006590# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6592 {
6593 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6594 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6595 }
6596 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006597# endif
6598# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 if (menu_is_toolbar(parent->name))
6600 {
6601 TBBUTTON newtb;
6602
Bram Moolenaara80faa82020-04-12 19:37:17 +02006603 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 if (menu_is_separator(menu->name))
6605 {
6606 newtb.iBitmap = 0;
6607 newtb.fsStyle = TBSTYLE_SEP;
6608 }
6609 else
6610 {
6611 newtb.iBitmap = get_toolbar_bitmap(menu);
6612 newtb.fsStyle = TBSTYLE_BUTTON;
6613 }
6614 newtb.idCommand = menu->id;
6615 newtb.fsState = TBSTATE_ENABLED;
6616 newtb.iString = 0;
6617 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6618 (LPARAM)&newtb);
6619 menu->submenu_id = (HMENU)-1;
6620 }
6621 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006624 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006626 wn = enc_to_utf16(menu->name, NULL);
6627 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006629 InsertMenuW(parent->submenu_id, (UINT)idx,
6630 (menu_is_separator(menu->name)
6631 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6632 (UINT)menu->id, wn);
6633 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006635# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (IsWindow(parent->tearoff_handle))
6637 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640}
6641
6642/*
6643 * Destroy the machine specific menu widget.
6644 */
6645 void
6646gui_mch_destroy_menu(vimmenu_T *menu)
6647{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006648# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 /*
6650 * is this a toolbar button?
6651 */
6652 if (menu->submenu_id == (HMENU)-1)
6653 {
6654 int iButton;
6655
6656 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6657 (WPARAM)menu->id, 0);
6658 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6659 }
6660 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006661# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 {
6663 if (menu->parent != NULL
6664 && menu_is_popup(menu->parent->dname)
6665 && menu->parent->submenu_id != NULL)
6666 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6667 else
6668 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6669 if (menu->submenu_id != NULL)
6670 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006671# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (IsWindow(menu->tearoff_handle))
6673 DestroyWindow(menu->tearoff_handle);
6674 if (menu->parent != NULL
6675 && menu->parent->children != NULL
6676 && IsWindow(menu->parent->tearoff_handle))
6677 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006678 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 menu->modes = 0;
6680 rebuild_tearoff(menu->parent);
6681 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684}
6685
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006686# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 static void
6688rebuild_tearoff(vimmenu_T *menu)
6689{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006690 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 char_u tbuf[128];
6692 RECT trect;
6693 RECT rct;
6694 RECT roct;
6695 int x, y;
6696
6697 HWND thwnd = menu->tearoff_handle;
6698
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006699 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (GetWindowRect(thwnd, &trect)
6701 && GetWindowRect(s_hwnd, &rct)
6702 && GetClientRect(s_hwnd, &roct))
6703 {
6704 x = trect.left - rct.left;
6705 y = (trect.top - rct.bottom + roct.bottom);
6706 }
6707 else
6708 {
6709 x = y = 0xffffL;
6710 }
6711 DestroyWindow(thwnd);
6712 if (menu->children != NULL)
6713 {
6714 gui_mch_tearoff(tbuf, menu, x, y);
6715 if (IsWindow(menu->tearoff_handle))
6716 (void) SetWindowPos(menu->tearoff_handle,
6717 NULL,
6718 (int)trect.left,
6719 (int)trect.top,
6720 0, 0,
6721 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6722 }
6723}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006724# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726/*
6727 * Make a menu either grey or not grey.
6728 */
6729 void
6730gui_mch_menu_grey(
6731 vimmenu_T *menu,
6732 int grey)
6733{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006734# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 /*
6736 * is this a toolbar button?
6737 */
6738 if (menu->submenu_id == (HMENU)-1)
6739 {
6740 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006741 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006744# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006745 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6746 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006748# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6750 {
6751 WORD menuID;
6752 HWND menuHandle;
6753
6754 /*
6755 * A tearoff button has changed state.
6756 */
6757 if (menu->children == NULL)
6758 menuID = (WORD)(menu->id);
6759 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006760 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6762 if (menuHandle)
6763 EnableWindow(menuHandle, !grey);
6764
6765 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767}
6768
Bram Moolenaar734a8672019-12-02 22:49:38 +01006769#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
6771
Bram Moolenaar734a8672019-12-02 22:49:38 +01006772// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006775#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6778/*
6779 * stuff for dialogs
6780 */
6781
6782/*
6783 * The callback routine used by all the dialogs. Very simple. First,
6784 * acknowledges the INITDIALOG message so that Windows knows to do standard
6785 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6786 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6787 * number.
6788 */
6789 static LRESULT CALLBACK
6790dialog_callback(
6791 HWND hwnd,
6792 UINT message,
6793 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006794 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 if (message == WM_INITDIALOG)
6797 {
6798 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006799 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 (void)SetFocus(hwnd);
6801 if (dialog_default_button > IDCANCEL)
6802 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006803 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006804 // We don't have a default, set focus on another element of the
6805 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006806 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 return FALSE;
6808 }
6809
6810 if (message == WM_COMMAND)
6811 {
6812 int button = LOWORD(wParam);
6813
Bram Moolenaar734a8672019-12-02 22:49:38 +01006814 // Don't end the dialog if something was selected that was
6815 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (button >= DLG_NONBUTTON_CONTROL)
6817 return TRUE;
6818
Bram Moolenaar734a8672019-12-02 22:49:38 +01006819 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006821 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006822 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006823 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006824
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006825 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6826 p = utf16_to_enc(wp, NULL);
6827 vim_strncpy(s_textfield, p, IOSIZE);
6828 vim_free(p);
6829 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831
6832 /*
6833 * Need to check for IDOK because if the user just hits Return to
6834 * accept the default value, some reason this is what we get.
6835 */
6836 if (button == IDOK)
6837 {
6838 if (dialog_default_button > IDCANCEL)
6839 EndDialog(hwnd, dialog_default_button);
6840 }
6841 else
6842 EndDialog(hwnd, button - IDCANCEL);
6843 return TRUE;
6844 }
6845
6846 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6847 {
6848 EndDialog(hwnd, 0);
6849 return TRUE;
6850 }
6851 return FALSE;
6852}
6853
6854/*
6855 * Create a dialog dynamically from the parameter strings.
6856 * type = type of dialog (question, alert, etc.)
6857 * title = dialog title. may be NULL for default title.
6858 * message = text to display. Dialog sizes to accommodate it.
6859 * buttons = '\n' separated list of button captions, default first.
6860 * dfltbutton = number of default button.
6861 *
6862 * This routine returns 1 if the first button is pressed,
6863 * 2 for the second, etc.
6864 *
6865 * 0 indicates Esc was pressed.
6866 * -1 for unexpected error
6867 *
6868 * If stubbing out this fn, return 1.
6869 */
6870
Bram Moolenaar734a8672019-12-02 22:49:38 +01006871static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 "IDR_VIM",
6874 "IDR_VIM_ERROR",
6875 "IDR_VIM_ALERT",
6876 "IDR_VIM_INFO",
6877 "IDR_VIM_QUESTION"
6878};
6879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 int
6881gui_mch_dialog(
6882 int type,
6883 char_u *title,
6884 char_u *message,
6885 char_u *buttons,
6886 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006887 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006888 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006891 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 int numButtons;
6893 int *buttonWidths, *buttonPositions;
6894 int buttonYpos;
6895 int nchar, i;
6896 DWORD lStyle;
6897 int dlgwidth = 0;
6898 int dlgheight;
6899 int editboxheight;
6900 int horizWidth = 0;
6901 int msgheight;
6902 char_u *pstart;
6903 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006904 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 char_u *tbuffer;
6906 RECT rect;
6907 HWND hwnd;
6908 HDC hdc;
6909 HFONT font, oldFont;
6910 TEXTMETRIC fontInfo;
6911 int fontHeight;
6912 int textWidth, minButtonWidth, messageWidth;
6913 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006914 int maxDialogHeight;
6915 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 int vertical;
6917 int dlgPaddingX;
6918 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006919# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006920 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006922# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006923 garray_T ga;
6924 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006925 int dlg_icon_width;
6926 int dlg_icon_height;
6927 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006929# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006930 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006931# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006932 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006933# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006934 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006935 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
Bram Moolenaar748bf032005-02-02 23:04:36 +00006938 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006939 {
6940 load_dpi_func();
6941 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006942 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006943 }
6944 else
6945 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947 if ((type < 0) || (type > VIM_LAST_TYPE))
6948 type = 0;
6949
Bram Moolenaar734a8672019-12-02 22:49:38 +01006950 // allocate some memory for dialog template
6951 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006952 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006953 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 if (p == NULL)
6956 return -1;
6957
6958 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006959 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6961 * const.
6962 */
6963 tbuffer = vim_strsave(buttons);
6964 if (tbuffer == NULL)
6965 return -1;
6966
Bram Moolenaar734a8672019-12-02 22:49:38 +01006967 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar734a8672019-12-02 22:49:38 +01006969 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 numButtons = 1;
6971 for (i = 0; tbuffer[i] != '\0'; i++)
6972 {
6973 if (tbuffer[i] == DLG_BUTTON_SEP)
6974 numButtons++;
6975 }
6976 if (dfltbutton >= numButtons)
6977 dfltbutton = -1;
6978
Bram Moolenaar734a8672019-12-02 22:49:38 +01006979 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006980 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (buttonWidths == NULL)
6982 return -1;
6983
Bram Moolenaar734a8672019-12-02 22:49:38 +01006984 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006985 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (buttonPositions == NULL)
6987 return -1;
6988
6989 /*
6990 * Calculate how big the dialog must be.
6991 */
6992 hwnd = GetDesktopWindow();
6993 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006994# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6996 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006997 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 use_lfSysmenu = TRUE;
6999 }
7000 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007001# endif
K.Takatad1c58992022-01-23 12:31:57 +00007002 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7003 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7004
7005 oldFont = SelectFont(hdc, font);
7006 dlgPaddingX = DLG_PADDING_X;
7007 dlgPaddingY = DLG_PADDING_Y;
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 GetTextMetrics(hdc, &fontInfo);
7010 fontHeight = fontInfo.tmHeight;
7011
Bram Moolenaar734a8672019-12-02 22:49:38 +01007012 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007013 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaar734a8672019-12-02 22:49:38 +01007015 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007016 if (s_hwnd == NULL)
7017 {
7018 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar734a8672019-12-02 22:49:38 +01007020 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007021 get_work_area(&workarea_rect);
7022 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007023 if (maxDialogWidth > adjust_by_system_dpi(600))
7024 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007025 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007026 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007027 }
7028 else
7029 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007030 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007031 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007032 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007033 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7034 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7035 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7036 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007037
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007038 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007039 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7040 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7041 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7042 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7043 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007044 }
7045
Bram Moolenaar734a8672019-12-02 22:49:38 +01007046 // Set dlgwidth to width of message.
7047 // Copy the message into "ga", changing NL to CR-NL and inserting line
7048 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 pstart = message;
7050 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007051 msgheight = 0;
7052 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 do
7054 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007055 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007056
Bram Moolenaar734a8672019-12-02 22:49:38 +01007057 // Need to figure out where to break the string. The system does it
7058 // at a word boundary, which would mean we can't compute the number of
7059 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007060 textWidth = 0;
7061 last_white = NULL;
7062 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007063 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007064 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007065 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007066 && textWidth > maxDialogWidth * 3 / 4)
7067 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007068 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007069 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007070 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007071 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007072 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007073 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007074 textWidth = 0;
7075
7076 if (last_white != NULL)
7077 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007078 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007079 if (pend > last_white)
7080 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007081 pend = last_white + 1;
7082 last_white = NULL;
7083 }
7084 ga_append(&ga, '\r');
7085 ga_append(&ga, '\n');
7086 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007087 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007088
7089 while (--l >= 0)
7090 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007091 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007092 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007094
7095 ga_append(&ga, '\r');
7096 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 pstart = pend + 1;
7098 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007099
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007100 if (ga.ga_data != NULL)
7101 message = ga.ga_data;
7102
Bram Moolenaar734a8672019-12-02 22:49:38 +01007103 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007104
K.Takatac81e9bf2022-01-16 14:15:49 +00007105 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7106 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
K.Takatac81e9bf2022-01-16 14:15:49 +00007108 // Add width of icon to dlgwidth, and some space
7109 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7110 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7111
7112 if (msgheight < dlg_icon_height)
7113 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
7115 /*
7116 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007117 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007119 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (!vertical)
7121 {
7122 // Place buttons horizontally if they fit.
7123 horizWidth = dlgPaddingX;
7124 pstart = tbuffer;
7125 i = 0;
7126 do
7127 {
7128 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7129 if (pend == NULL)
7130 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007131 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 if (textWidth < minButtonWidth)
7133 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007134 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 buttonWidths[i] = textWidth;
7136 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007137 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 pstart = pend + 1;
7139 } while (*pend != NUL);
7140
7141 if (horizWidth > maxDialogWidth)
7142 vertical = TRUE; // Too wide to fit on the screen.
7143 else if (horizWidth > dlgwidth)
7144 dlgwidth = horizWidth;
7145 }
7146
7147 if (vertical)
7148 {
7149 // Stack buttons vertically.
7150 pstart = tbuffer;
7151 do
7152 {
7153 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7154 if (pend == NULL)
7155 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007156 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007157 textWidth += dlgPaddingX; // Padding within button
7158 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (textWidth > dlgwidth)
7160 dlgwidth = textWidth;
7161 pstart = pend + 1;
7162 } while (*pend != NUL);
7163 }
7164
7165 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007166 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
Bram Moolenaar734a8672019-12-02 22:49:38 +01007168 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007169 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 add_long(lStyle);
7172 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007173 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 add_word(0); // NumberOfItems(will change later)
7175 add_word(10); // x
7176 add_word(10); // y
7177 add_word(PixelToDialogX(dlgwidth)); // cx
7178
7179 // Dialog height.
7180 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007181 dlgheight = msgheight + 2 * dlgPaddingY
7182 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 else
7184 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7185
7186 // Dialog needs to be taller if contains an edit box.
7187 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7188 if (textfield != NULL)
7189 dlgheight += editboxheight;
7190
Bram Moolenaar734a8672019-12-02 22:49:38 +01007191 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007192 if (dlgheight > maxDialogHeight)
7193 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007194 msgheight = msgheight - (dlgheight - maxDialogHeight);
7195 dlgheight = maxDialogHeight;
7196 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007197 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007198 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007199 }
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 add_word(PixelToDialogY(dlgheight));
7202
7203 add_word(0); // Menu
7204 add_word(0); // Class
7205
Bram Moolenaar734a8672019-12-02 22:49:38 +01007206 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007207 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7208 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 p += nchar;
7210
K.Takatad1c58992022-01-23 12:31:57 +00007211 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007212# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007213 if (use_lfSysmenu)
7214 {
7215 // point size
7216 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7217 GetDeviceCaps(hdc, LOGPIXELSY));
7218 wcscpy(p, lfSysmenu.lfFaceName);
7219 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 }
K.Takatad1c58992022-01-23 12:31:57 +00007221 else
7222# endif
7223 {
7224 *p++ = DLG_FONT_POINT_SIZE; // point size
7225 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7226 }
7227 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
7229 buttonYpos = msgheight + 2 * dlgPaddingY;
7230
7231 if (textfield != NULL)
7232 buttonYpos += editboxheight;
7233
7234 pstart = tbuffer;
7235 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007236 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 for (i = 0; i < numButtons; i++)
7238 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007239 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 for ( pend = pstart;
7241 *pend && (*pend != DLG_BUTTON_SEP);
7242 pend++)
7243 ;
7244
7245 if (*pend)
7246 *pend = '\0';
7247
7248 /*
7249 * old NOTE:
7250 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7251 * the focus to the first tab-able button and in so doing makes that
7252 * the default!! Grrr. Workaround: Make the default button the only
7253 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7254 * he/she can use arrow keys.
7255 *
7256 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007257 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 * dialog. Also needed for when the textfield is the default control.
7259 * It appears to work now (perhaps not on Win95?).
7260 */
7261 if (vertical)
7262 {
7263 p = add_dialog_element(p,
7264 (i == dfltbutton
7265 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7266 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007267 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 + 2 * fontHeight * i),
7269 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7270 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007271 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273 else
7274 {
7275 p = add_dialog_element(p,
7276 (i == dfltbutton
7277 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7278 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007279 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 PixelToDialogX(buttonWidths[i]),
7281 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007282 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007284 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286 *pnumitems += numButtons;
7287
Bram Moolenaar734a8672019-12-02 22:49:38 +01007288 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 p = add_dialog_element(p, SS_ICON,
7290 PixelToDialogX(dlgPaddingX),
7291 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007292 PixelToDialogX(dlg_icon_width),
7293 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7295 dlg_icons[type]);
7296
Bram Moolenaar734a8672019-12-02 22:49:38 +01007297 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007298 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007299 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007300 PixelToDialogY(dlgPaddingY),
7301 (WORD)(PixelToDialogX(messageWidth) + 1),
7302 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007303 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
Bram Moolenaar734a8672019-12-02 22:49:38 +01007305 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (textfield != NULL)
7307 {
7308 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7309 PixelToDialogX(2 * dlgPaddingX),
7310 PixelToDialogY(2 * dlgPaddingY + msgheight),
7311 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7312 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007313 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 *pnumitems += 1;
7315 }
7316
7317 *pnumitems += 2;
7318
7319 SelectFont(hdc, oldFont);
7320 DeleteObject(font);
7321 ReleaseDC(hwnd, hdc);
7322
Bram Moolenaar734a8672019-12-02 22:49:38 +01007323 // Let the dialog_callback() function know which button to make default
7324 // If we have an edit box, make that the default. We also need to tell
7325 // dialog_callback() if this dialog contains an edit box or not. We do
7326 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 if (textfield != NULL)
7328 {
7329 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7330 s_textfield = textfield;
7331 }
7332 else
7333 {
7334 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7335 s_textfield = NULL;
7336 }
7337
Bram Moolenaar734a8672019-12-02 22:49:38 +01007338 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007340 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 (LPDLGTEMPLATE)pdlgtemplate,
7342 s_hwnd,
7343 (DLGPROC)dialog_callback);
7344
7345 LocalFree(LocalHandle(pdlgtemplate));
7346 vim_free(tbuffer);
7347 vim_free(buttonWidths);
7348 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007349 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
Bram Moolenaar734a8672019-12-02 22:49:38 +01007351 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 (void)SetFocus(s_hwnd);
7353
7354 return nchar;
7355}
7356
Bram Moolenaar734a8672019-12-02 22:49:38 +01007357#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359/*
7360 * Put a simple element (basic class) onto a dialog template in memory.
7361 * return a pointer to where the next item should be added.
7362 *
7363 * parameters:
7364 * lStyle = additional style flags
7365 * (be careful, NT3.51 & Win32s will ignore the new ones)
7366 * x,y = x & y positions IN DIALOG UNITS
7367 * w,h = width and height IN DIALOG UNITS
7368 * Id = ID used in messages
7369 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7370 * caption = usually text or resource name
7371 *
7372 * TODO: use the length information noted here to enable the dialog creation
7373 * routines to work out more exactly how much memory they need to alloc.
7374 */
7375 static PWORD
7376add_dialog_element(
7377 PWORD p,
7378 DWORD lStyle,
7379 WORD x,
7380 WORD y,
7381 WORD w,
7382 WORD h,
7383 WORD Id,
7384 WORD clss,
7385 const char *caption)
7386{
7387 int nchar;
7388
Bram Moolenaar734a8672019-12-02 22:49:38 +01007389 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7391 *p++ = LOWORD(lStyle);
7392 *p++ = HIWORD(lStyle);
7393 *p++ = 0; // LOWORD (lExtendedStyle)
7394 *p++ = 0; // HIWORD (lExtendedStyle)
7395 *p++ = x;
7396 *p++ = y;
7397 *p++ = w;
7398 *p++ = h;
7399 *p++ = Id; //9 or 10 words in all
7400
7401 *p++ = (WORD)0xffff;
7402 *p++ = clss; //2 more here
7403
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007404 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 p += nchar;
7406
7407 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7408
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007409 return p; // total = 15 + strlen(caption) words
7410 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411}
7412
7413
7414/*
7415 * Helper routine. Take an input pointer, return closest pointer that is
7416 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7417 */
7418 static LPWORD
7419lpwAlign(
7420 LPWORD lpIn)
7421{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007422 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007424 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 ul += 3;
7426 ul >>= 2;
7427 ul <<= 2;
7428 return (LPWORD)ul;
7429}
7430
7431/*
7432 * Helper routine. Takes second parameter as Ansi string, copies it to first
7433 * parameter as wide character (16-bits / char) string, and returns integer
7434 * number of wide characters (words) in string (including the trailing wide
7435 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007436 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7437 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 static int
7439nCopyAnsiToWideChar(
7440 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007441 LPSTR lpAnsiIn,
7442 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007445 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 int i;
7447 WCHAR *wn;
7448
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007449 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007451 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007452 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (wn != NULL)
7454 {
7455 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007456 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 vim_free(wn);
7458 }
7459 }
7460 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007461 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 nChar = MultiByteToWideChar(
7463 enc_codepage > 0 ? enc_codepage : CP_ACP,
7464 MB_PRECOMPOSED,
7465 lpAnsiIn, len,
7466 lpWCStr, len);
7467 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007468 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471 return nChar;
7472}
7473
7474
7475#ifdef FEAT_TEAROFF
7476/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007477 * Lookup menu handle from "menu_id".
7478 */
7479 static HMENU
7480tearoff_lookup_menuhandle(
7481 vimmenu_T *menu,
7482 WORD menu_id)
7483{
7484 for ( ; menu != NULL; menu = menu->next)
7485 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007486 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007487 continue;
7488 if (menu_is_separator(menu->dname))
7489 continue;
7490 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7491 return menu->submenu_id;
7492 }
7493 return NULL;
7494}
7495
7496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 * The callback function for all the modeless dialogs that make up the
7498 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7499 * thinking its menus have been clicked), and go away when closed.
7500 */
7501 static LRESULT CALLBACK
7502tearoff_callback(
7503 HWND hwnd,
7504 UINT message,
7505 WPARAM wParam,
7506 LPARAM lParam)
7507{
7508 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007509 {
7510 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaar734a8672019-12-02 22:49:38 +01007514 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 HandleMouseHide(message, lParam);
7516
7517 if (message == WM_COMMAND)
7518 {
7519 if ((WORD)(LOWORD(wParam)) & 0x8000)
7520 {
7521 POINT mp;
7522 RECT rect;
7523
7524 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7525 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007526 vimmenu_T *menu;
7527
7528 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007530 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7532 (int)rect.right - 8,
7533 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007534 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 s_hwnd,
7536 NULL);
7537 /*
7538 * NOTE: The pop-up menu can eat the mouse up event.
7539 * We deal with this in normal.c.
7540 */
7541 }
7542 }
7543 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007544 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7546 /*
7547 * Give main window the focus back: this is so after
7548 * choosing a tearoff button you can start typing again
7549 * straight away.
7550 */
7551 (void)SetFocus(s_hwnd);
7552 return TRUE;
7553 }
7554 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7555 {
7556 DestroyWindow(hwnd);
7557 return TRUE;
7558 }
7559
Bram Moolenaar734a8672019-12-02 22:49:38 +01007560 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 if (message == WM_EXITSIZEMOVE)
7562 (void)SetActiveWindow(s_hwnd);
7563
7564 return FALSE;
7565}
7566#endif
7567
7568
7569/*
K.Takatad1c58992022-01-23 12:31:57 +00007570 * Computes the dialog base units based on the current dialog font.
7571 * We don't use the GetDialogBaseUnits() API, because we don't use the
7572 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 */
7574 static void
7575get_dialog_font_metrics(void)
7576{
7577 HDC hdc;
7578 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 SIZE size;
7580#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007581 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582#endif
7583
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007585 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007586 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007587 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588#endif
7589 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007590 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
K.Takatad1c58992022-01-23 12:31:57 +00007592 hdc = GetDC(s_hwnd);
7593 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007594 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007595 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
K.Takataabe628e2022-01-23 16:25:17 +00007597 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007598 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599}
7600
7601#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7602/*
7603 * Create a pseudo-"tearoff menu" based on the child
7604 * items of a given menu pointer.
7605 */
7606 static void
7607gui_mch_tearoff(
7608 char_u *title,
7609 vimmenu_T *menu,
7610 int initX,
7611 int initY)
7612{
7613 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7614 int template_len;
7615 int nchar, textWidth, submenuWidth;
7616 DWORD lStyle;
7617 DWORD lExtendedStyle;
7618 WORD dlgwidth;
7619 WORD menuID;
7620 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007621 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 vimmenu_T *the_menu = menu;
7623 HWND hwnd;
7624 HDC hdc;
7625 HFONT font, oldFont;
7626 int col, spaceWidth, len;
7627 int columnWidths[2];
7628 char_u *label, *text;
7629 int acLen = 0;
7630 int nameLen;
7631 int padding0, padding1, padding2 = 0;
7632 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007633 int x;
7634 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007635# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007636 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007638# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639
7640 /*
7641 * If this menu is already torn off, move it to the mouse position.
7642 */
7643 if (IsWindow(menu->tearoff_handle))
7644 {
7645 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007646 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
7648 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7649 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7650 }
7651 return;
7652 }
7653
7654 /*
7655 * Create a new tearoff.
7656 */
7657 if (*title == MNU_HIDDEN_CHAR)
7658 title++;
7659
Bram Moolenaar734a8672019-12-02 22:49:38 +01007660 // Allocate memory to store the dialog template. It's made bigger when
7661 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 template_len = DLG_ALLOC_SIZE;
7663 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7664 if (p == NULL)
7665 return;
7666
7667 hwnd = GetDesktopWindow();
7668 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007669# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7671 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007672 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 use_lfSysmenu = TRUE;
7674 }
7675 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007676# endif
K.Takatad1c58992022-01-23 12:31:57 +00007677 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7678 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7679
7680 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
Bram Moolenaar734a8672019-12-02 22:49:38 +01007682 // Calculate width of a single space. Used for padding columns to the
7683 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007684 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
Bram Moolenaar734a8672019-12-02 22:49:38 +01007686 // Figure out max width of the text column, the accelerator column and the
7687 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 submenuWidth = 0;
7689 for (col = 0; col < 2; col++)
7690 {
7691 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007692 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007694 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 text = (col == 0) ? pmenu->dname : pmenu->actext;
7696 if (text != NULL && *text != NUL)
7697 {
7698 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7699 if (textWidth > columnWidths[col])
7700 columnWidths[col] = textWidth;
7701 }
7702 if (pmenu->children != NULL)
7703 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7704 }
7705 }
7706 if (columnWidths[1] == 0)
7707 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007708 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 if (submenuWidth != 0)
7710 columnWidths[0] += submenuWidth;
7711 else
7712 columnWidths[0] += spaceWidth;
7713 }
7714 else
7715 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007716 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7718 columnWidths[1] += submenuWidth;
7719 }
7720
7721 /*
7722 * Now find the total width of our 'menu'.
7723 */
7724 textWidth = columnWidths[0] + columnWidths[1];
7725 if (submenuWidth != 0)
7726 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007727 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7729 textWidth += submenuWidth;
7730 }
7731 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7732 if (textWidth > dlgwidth)
7733 dlgwidth = textWidth;
7734 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7735
Bram Moolenaar734a8672019-12-02 22:49:38 +01007736 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007737 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
7739 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7740 *p++ = LOWORD(lStyle);
7741 *p++ = HIWORD(lStyle);
7742 *p++ = LOWORD(lExtendedStyle);
7743 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007744 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007746 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007748 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 else
7750 *p++ = PixelToDialogX(initX); // x
7751 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007752 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 else
7754 *p++ = PixelToDialogY(initY); // y
7755 *p++ = PixelToDialogX(dlgwidth); // cx
7756 ptrueheight = p;
7757 *p++ = 0; // dialog height: changed later anyway
7758 *p++ = 0; // Menu
7759 *p++ = 0; // Class
7760
Bram Moolenaar734a8672019-12-02 22:49:38 +01007761 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007763 ? (LPSTR)title
7764 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 p += nchar;
7766
K.Takatad1c58992022-01-23 12:31:57 +00007767 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007768# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007769 if (use_lfSysmenu)
7770 {
7771 // point size
7772 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7773 GetDeviceCaps(hdc, LOGPIXELSY));
7774 wcscpy(p, lfSysmenu.lfFaceName);
7775 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
K.Takatad1c58992022-01-23 12:31:57 +00007777 else
7778# endif
7779 {
7780 *p++ = DLG_FONT_POINT_SIZE; // point size
7781 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7782 }
7783 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
7785 /*
7786 * Loop over all the items in the menu.
7787 * But skip over the tearbar.
7788 */
7789 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7790 menu = menu->children->next;
7791 else
7792 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007793 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 for ( ; menu != NULL; menu = menu->next)
7795 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007796 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 continue;
7798 if (menu_is_separator(menu->dname))
7799 {
7800 sepPadding += 3;
7801 continue;
7802 }
7803
Bram Moolenaar734a8672019-12-02 22:49:38 +01007804 // Check if there still is plenty of room in the template. Make it
7805 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7807 {
7808 WORD *newp;
7809
7810 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7811 if (newp != NULL)
7812 {
7813 template_len += 4096;
7814 mch_memmove(newp, pdlgtemplate,
7815 (char *)p - (char *)pdlgtemplate);
7816 p = newp + (p - pdlgtemplate);
7817 pnumitems = newp + (pnumitems - pdlgtemplate);
7818 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7819 LocalFree(LocalHandle(pdlgtemplate));
7820 pdlgtemplate = newp;
7821 }
7822 }
7823
Bram Moolenaar734a8672019-12-02 22:49:38 +01007824 // Figure out minimal length of this menu label. Use "name" for the
7825 // actual text, "dname" for estimating the displayed size. "name"
7826 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 len = nameLen = (int)STRLEN(menu->name);
7828 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7829 (int)STRLEN(menu->dname))) / spaceWidth;
7830 len += padding0;
7831
7832 if (menu->actext != NULL)
7833 {
7834 acLen = (int)STRLEN(menu->actext);
7835 len += acLen;
7836 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7837 }
7838 else
7839 textWidth = 0;
7840 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7841 len += padding1;
7842
7843 if (menu->children == NULL)
7844 {
7845 padding2 = submenuWidth / spaceWidth;
7846 len += padding2;
7847 menuID = (WORD)(menu->id);
7848 }
7849 else
7850 {
7851 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007852 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
7854
Bram Moolenaar734a8672019-12-02 22:49:38 +01007855 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007856 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (label == NULL)
7858 break;
7859
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007860 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007861 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007863 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 while (padding0-- > 0)
7865 *text++ = ' ';
7866 if (menu->actext != NULL)
7867 {
7868 STRNCPY(text, menu->actext, acLen);
7869 text += acLen;
7870 }
7871 while (padding1-- > 0)
7872 *text++ = ' ';
7873 if (menu->children != NULL)
7874 {
7875 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7876 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7877 }
7878 else
7879 {
7880 while (padding2-- > 0)
7881 *text++ = ' ';
7882 }
7883 *text = NUL;
7884
7885 /*
7886 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7887 * W95/NT4 it makes the tear-off look more like a menu.
7888 */
7889 p = add_dialog_element(p,
7890 BS_PUSHBUTTON|BS_LEFT,
7891 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7892 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7893 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7894 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007895 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 vim_free(label);
7897 (*pnumitems)++;
7898 }
7899
7900 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7901
7902
Bram Moolenaar734a8672019-12-02 22:49:38 +01007903 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007904 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007905 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 (LPDLGTEMPLATE)pdlgtemplate,
7907 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007908 (DLGPROC)tearoff_callback,
7909 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
7911 LocalFree(LocalHandle(pdlgtemplate));
7912 SelectFont(hdc, oldFont);
7913 DeleteObject(font);
7914 ReleaseDC(hwnd, hdc);
7915
7916 /*
7917 * Reassert ourselves as the active window. This is so that after creating
7918 * a tearoff, the user doesn't have to click with the mouse just to start
7919 * typing again!
7920 */
7921 (void)SetActiveWindow(s_hwnd);
7922
Bram Moolenaar734a8672019-12-02 22:49:38 +01007923 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 force_menu_update = TRUE;
7925}
7926#endif
7927
7928#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007929# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931/*
7932 * Create the toolbar, initially unpopulated.
7933 * (just like the menu, there are no defaults, it's all
7934 * set up through menu.vim)
7935 */
7936 static void
7937initialise_toolbar(void)
7938{
7939 InitCommonControls();
7940 s_toolbarhwnd = CreateToolbarEx(
7941 s_hwnd,
7942 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7943 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007944 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007945 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 IDR_TOOLBAR1, // id of initial bitmap
7947 NULL,
7948 0, // initial number of buttons
7949 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7950 TOOLBAR_BUTTON_HEIGHT,
7951 TOOLBAR_BUTTON_WIDTH,
7952 TOOLBAR_BUTTON_HEIGHT,
7953 sizeof(TBBUTTON)
7954 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007955
7956 // Remove transparency from the toolbar to prevent the main window
7957 // background colour showing through
7958 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7959 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7960
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007961 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962
7963 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007964
7965 update_toolbar_size();
7966}
7967
7968 static void
7969update_toolbar_size(void)
7970{
7971 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007972 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007973
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007974 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007975 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7976 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7977 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7978 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7979
7980 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7981 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7982 //TRACE("button size: %d, %d", w, h);
7983 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7984 gui.toolbar_height = h + 6;
7985
7986 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7987 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7988
7989 // TODO:
7990 // Currently, this function only updates the size of toolbar buttons.
7991 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992}
7993
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007994 static LRESULT CALLBACK
7995toolbar_wndproc(
7996 HWND hwnd,
7997 UINT uMsg,
7998 WPARAM wParam,
7999 LPARAM lParam)
8000{
8001 HandleMouseHide(uMsg, lParam);
8002 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8003}
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 static int
8006get_toolbar_bitmap(vimmenu_T *menu)
8007{
8008 int i = -1;
8009
8010 /*
8011 * Check user bitmaps first, unless builtin is specified.
8012 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008013 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {
8015 char_u fname[MAXPATHL];
8016 HANDLE hbitmap = NULL;
8017
8018 if (menu->iconfile != NULL)
8019 {
8020 gui_find_iconfile(menu->iconfile, fname, "bmp");
8021 hbitmap = LoadImage(
8022 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008023 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 IMAGE_BITMAP,
8025 TOOLBAR_BUTTON_WIDTH,
8026 TOOLBAR_BUTTON_HEIGHT,
8027 LR_LOADFROMFILE |
8028 LR_LOADMAP3DCOLORS
8029 );
8030 }
8031
8032 /*
8033 * If the LoadImage call failed, or the "icon=" file
8034 * didn't exist or wasn't specified, try the menu name
8035 */
8036 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008037 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008038# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008039 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008040# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008041 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 hbitmap = LoadImage(
8043 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008044 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 IMAGE_BITMAP,
8046 TOOLBAR_BUTTON_WIDTH,
8047 TOOLBAR_BUTTON_HEIGHT,
8048 LR_LOADFROMFILE |
8049 LR_LOADMAP3DCOLORS
8050 );
8051
8052 if (hbitmap != NULL)
8053 {
8054 TBADDBITMAP tbAddBitmap;
8055
8056 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008057 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8060 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008061 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063 }
8064 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8065 i = menu->iconidx;
8066
8067 return i;
8068}
8069#endif
8070
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008071#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8072 static void
8073initialise_tabline(void)
8074{
8075 InitCommonControls();
8076
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008077 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008078 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008079 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008080 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008081 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008082
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008083 gui.tabline_height = TABLINE_HEIGHT;
8084
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008085 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008086}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008087
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008088/*
8089 * Get tabpage_T from POINT.
8090 */
8091 static tabpage_T *
8092GetTabFromPoint(
8093 HWND hWnd,
8094 POINT pt)
8095{
8096 tabpage_T *ptp = NULL;
8097
8098 if (gui_mch_showing_tabline())
8099 {
8100 TCHITTESTINFO htinfo;
8101 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008102 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008103 if (s_tabhwnd == hWnd)
8104 {
8105 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8106 if (idx != -1)
8107 ptp = find_tabpage(idx + 1);
8108 }
8109 }
8110 return ptp;
8111}
8112
8113static POINT s_pt = {0, 0};
8114static HCURSOR s_hCursor = NULL;
8115
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008116 static LRESULT CALLBACK
8117tabline_wndproc(
8118 HWND hwnd,
8119 UINT uMsg,
8120 WPARAM wParam,
8121 LPARAM lParam)
8122{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008123 POINT pt;
8124 tabpage_T *tp;
8125 RECT rect;
8126 int nCenter;
8127 int idx0;
8128 int idx1;
8129
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008130 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008131
8132 switch (uMsg)
8133 {
8134 case WM_LBUTTONDOWN:
8135 {
8136 s_pt.x = GET_X_LPARAM(lParam);
8137 s_pt.y = GET_Y_LPARAM(lParam);
8138 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008139 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008140 break;
8141 }
8142 case WM_MOUSEMOVE:
8143 if (GetCapture() == hwnd
8144 && ((wParam & MK_LBUTTON)) != 0)
8145 {
8146 pt.x = GET_X_LPARAM(lParam);
8147 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008148 if (abs(pt.x - s_pt.x) >
8149 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008150 {
8151 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8152
8153 tp = GetTabFromPoint(hwnd, pt);
8154 if (tp != NULL)
8155 {
8156 idx0 = tabpage_index(curtab) - 1;
8157 idx1 = tabpage_index(tp) - 1;
8158
8159 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8160 nCenter = rect.left + (rect.right - rect.left) / 2;
8161
Bram Moolenaar734a8672019-12-02 22:49:38 +01008162 // Check if the mouse cursor goes over the center of
8163 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008164 if ((idx0 < idx1) && (nCenter < pt.x))
8165 {
8166 tabpage_move(idx1 + 1);
8167 update_screen(0);
8168 }
8169 else if ((idx1 < idx0) && (pt.x < nCenter))
8170 {
8171 tabpage_move(idx1);
8172 update_screen(0);
8173 }
8174 }
8175 }
8176 }
8177 break;
8178 case WM_LBUTTONUP:
8179 {
8180 if (GetCapture() == hwnd)
8181 {
8182 SetCursor(s_hCursor);
8183 ReleaseCapture();
8184 }
8185 break;
8186 }
regomne3bdef102022-09-26 20:48:32 +01008187 case WM_MBUTTONUP:
8188 {
8189 TCHITTESTINFO htinfo;
8190
8191 htinfo.pt.x = GET_X_LPARAM(lParam);
8192 htinfo.pt.y = GET_Y_LPARAM(lParam);
8193 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8194 if (idx0 != -1)
8195 {
8196 idx0 += 1;
8197 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8198 }
8199 break;
8200 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008201 default:
8202 break;
8203 }
8204
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008205 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8206}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008207#endif
8208
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8210/*
8211 * Make the GUI window come to the foreground.
8212 */
8213 void
8214gui_mch_set_foreground(void)
8215{
8216 if (IsIconic(s_hwnd))
8217 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8218 SetForegroundWindow(s_hwnd);
8219}
8220#endif
8221
8222#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8223 static void
8224dyn_imm_load(void)
8225{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008226 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 if (hLibImm == NULL)
8228 return;
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008231 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008233 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008235 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008237 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008239 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008241 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008242 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008243 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008244 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008245 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008247 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008249 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008250 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008251 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
K.Takatab0b2b732022-01-19 12:59:21 +00008253 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 || pImmGetContext == NULL
8255 || pImmAssociateContext == NULL
8256 || pImmReleaseContext == NULL
8257 || pImmGetOpenStatus == NULL
8258 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008259 || pImmGetCompositionFontW == NULL
8260 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008262 || pImmGetConversionStatus == NULL
8263 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {
8265 FreeLibrary(hLibImm);
8266 hLibImm = NULL;
8267 pImmGetContext = NULL;
8268 return;
8269 }
8270
8271 return;
8272}
8273
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274#endif
8275
8276#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8277
8278# ifdef FEAT_XPM_W32
8279# define IMAGE_XPM 100
8280# endif
8281
8282typedef struct _signicon_t
8283{
8284 HANDLE hImage;
8285 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008286# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008287 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289} signicon_t;
8290
8291 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008292gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
8294 signicon_t *sign;
8295 int x, y, w, h;
8296
8297 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8298 return;
8299
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008300# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008301 if (IS_ENABLE_DIRECTX())
8302 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008303# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 x = TEXT_X(col);
8306 y = TEXT_Y(row);
8307 w = gui.char_width * 2;
8308 h = gui.char_height;
8309 switch (sign->uType)
8310 {
8311 case IMAGE_BITMAP:
8312 {
8313 HDC hdcMem;
8314 HBITMAP hbmpOld;
8315
8316 hdcMem = CreateCompatibleDC(s_hdc);
8317 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8318 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8319 SelectObject(hdcMem, hbmpOld);
8320 DeleteDC(hdcMem);
8321 }
8322 break;
8323 case IMAGE_ICON:
8324 case IMAGE_CURSOR:
8325 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8326 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008327# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 case IMAGE_XPM:
8329 {
8330 HDC hdcMem;
8331 HBITMAP hbmpOld;
8332
8333 hdcMem = CreateCompatibleDC(s_hdc);
8334 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008335 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8337
8338 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008339 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8341 SelectObject(hdcMem, hbmpOld);
8342 DeleteDC(hdcMem);
8343 }
8344 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 }
8347}
8348
8349 static void
8350close_signicon_image(signicon_t *sign)
8351{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008352 if (sign == NULL)
8353 return;
8354
8355 switch (sign->uType)
8356 {
8357 case IMAGE_BITMAP:
8358 DeleteObject((HGDIOBJ)sign->hImage);
8359 break;
8360 case IMAGE_CURSOR:
8361 DestroyCursor((HCURSOR)sign->hImage);
8362 break;
8363 case IMAGE_ICON:
8364 DestroyIcon((HICON)sign->hImage);
8365 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008366# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008367 case IMAGE_XPM:
8368 DeleteObject((HBITMAP)sign->hImage);
8369 DeleteObject((HBITMAP)sign->hShape);
8370 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008371# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373}
8374
8375 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008376gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
8378 signicon_t sign, *psign;
8379 char_u *ext;
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008382 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 if (ext > signfile)
8384 {
8385 int do_load = 1;
8386
8387 if (!STRICMP(ext, ".bmp"))
8388 sign.uType = IMAGE_BITMAP;
8389 else if (!STRICMP(ext, ".ico"))
8390 sign.uType = IMAGE_ICON;
8391 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8392 sign.uType = IMAGE_CURSOR;
8393 else
8394 do_load = 0;
8395
8396 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008397 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 gui.char_width * 2, gui.char_height,
8399 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008400# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 if (!STRICMP(ext, ".xpm"))
8402 {
8403 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008404 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8405 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 }
8409
8410 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008411 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 *psign = sign;
8413
8414 if (!psign)
8415 {
8416 if (sign.hImage)
8417 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008418 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 }
8420 return (void *)psign;
8421
8422}
8423
8424 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008425gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008427 if (sign == NULL)
8428 return;
8429
8430 close_signicon_image((signicon_t *)sign);
8431 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008435#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
Bram Moolenaar734a8672019-12-02 22:49:38 +01008437/*
8438 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008439 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008441 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8443 * to get current mouse position).
8444 *
8445 * Trying to use as more Windows services as possible, and as less
8446 * IE version as possible :)).
8447 *
8448 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8449 * BalloonEval struct.
8450 * 2) Enable/Disable simply create/kill BalloonEval Timer
8451 * 3) When there was enough inactivity, timer procedure posts
8452 * async request to debugger
8453 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8454 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008455 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 */
8457
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008458 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008459make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008460{
K.Takata76687d22022-01-25 10:31:37 +00008461 TOOLINFOW *pti;
8462 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008463
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008464 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008465 if (pti == NULL)
8466 return;
8467
8468 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8469 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8470 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008471 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008472
8473 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8474 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8475
K.Takata76687d22022-01-25 10:31:37 +00008476 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008477 pti->uFlags = TTF_SUBCLASS;
8478 pti->hwnd = beval->target;
8479 pti->hinst = 0; // Don't use string resources
8480 pti->uId = ID_BEVAL_TOOLTIP;
8481
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008482 pti->lpszText = LPSTR_TEXTCALLBACKW;
8483 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8484 pti->lParam = (LPARAM)beval->tofree;
8485 // switch multiline tooltips on
8486 if (GetClientRect(s_textArea, &rect))
8487 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8488 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008489
8490 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8491 pti->rect.left = pt.x - 3;
8492 pti->rect.top = pt.y - 3;
8493 pti->rect.right = pt.x + 3;
8494 pti->rect.bottom = pt.y + 3;
8495
8496 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8497 // Make tooltip appear sooner.
8498 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8499 // I've performed some tests and it seems the longest possible life time
8500 // of tooltip is 30 seconds.
8501 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8502 /*
8503 * HACK: force tooltip to appear, because it'll not appear until
8504 * first mouse move. D*mn M$
8505 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8506 */
8507 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8508 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8509 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008510}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008513delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008515 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516}
8517
8518 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008519beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008520 HWND hwnd UNUSED,
8521 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008522 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008523 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 POINT pt;
8526 RECT rect;
8527
8528 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8529 return;
8530
8531 GetCursorPos(&pt);
8532 if (WindowFromPoint(pt) != s_textArea)
8533 return;
8534
8535 ScreenToClient(s_textArea, &pt);
8536 GetClientRect(s_textArea, &rect);
8537 if (!PtInRect(&rect, pt))
8538 return;
8539
K.Takataa8ec4912022-02-03 14:32:33 +00008540 if (last_user_activity > 0
8541 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 && (cur_beval->showState != ShS_PENDING
8543 || abs(cur_beval->x - pt.x) > 3
8544 || abs(cur_beval->y - pt.y) > 3))
8545 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008546 // Pointer resting in one place long enough, it's time to show
8547 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 cur_beval->showState = ShS_PENDING;
8549 cur_beval->x = pt.x;
8550 cur_beval->y = pt.y;
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 if (cur_beval->msgCB != NULL)
8553 (*cur_beval->msgCB)(cur_beval, 0);
8554 }
8555}
8556
8557 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008558gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
K.Takataa8ec4912022-02-03 14:32:33 +00008560 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561}
8562
8563 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008564gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (beval == NULL)
8567 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008568 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8569 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570}
8571
8572 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008573gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008576
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008577 vim_free(beval->msg);
8578 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8579 if (beval->msg == NULL)
8580 {
8581 delete_tooltip(beval);
8582 beval->showState = ShS_NEUTRAL;
8583 return;
8584 }
8585
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (beval->showState == ShS_SHOWING)
8587 return;
8588 GetCursorPos(&pt);
8589 ScreenToClient(s_textArea, &pt);
8590
8591 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008593 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 gui_mch_disable_beval_area(cur_beval);
8595 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008596 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598}
8599
8600 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008601gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008602 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008603 char_u *mesg,
8604 void (*mesgCB)(BalloonEval *, int),
8605 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008607 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 BalloonEval *beval;
8609
8610 if (mesg != NULL && mesgCB != NULL)
8611 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008612 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 return NULL;
8614 }
8615
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008616 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (beval != NULL)
8618 {
8619 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 beval->msg = mesg;
8623 beval->msgCB = mesgCB;
8624 beval->clientData = clientData;
8625
8626 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 cur_beval = beval;
8628
8629 if (p_beval)
8630 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 }
8632 return beval;
8633}
8634
8635 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008636Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008638 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 return;
8640
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008641 if (cur_beval == NULL)
8642 return;
8643
8644 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008646 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008647 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008648 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 delete_tooltip(cur_beval);
8650 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
8652 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008653 break;
8654 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008655 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008656 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008657 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008658 info->lpszText = (LPSTR)info->lParam;
8659 info->uFlags |= TTF_DI_SETITEM;
8660 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008661 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008662 case TTN_GETDISPINFOW:
8663 {
8664 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008665 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008666 info->lpszText = (LPWSTR)info->lParam;
8667 info->uFlags |= TTF_DI_SETITEM;
8668 }
8669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 }
8671}
8672
8673 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008674track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675{
8676 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8677 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008678 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679}
8680
8681 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008682gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008684# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008685 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008686# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008687 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 vim_free(beval);
8689}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008690#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8693/*
8694 * We have multiple signs to draw at the same location. Draw the
8695 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8696 */
8697 void
8698netbeans_draw_multisign_indicator(int row)
8699{
8700 int i;
8701 int y;
8702 int x;
8703
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008704 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008705 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 x = 0;
8708 y = TEXT_Y(row);
8709
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008710# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008711 if (IS_ENABLE_DIRECTX())
8712 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008713# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008714
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 for (i = 0; i < gui.char_height - 3; i++)
8716 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8717
8718 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8719 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8720 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8721 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8722 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8723 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8724 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8725}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008726#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008727
8728#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008729
Christopher Plewright20b795e2022-12-20 20:01:58 +00008730// TODO: at the moment, this is just a copy of test_gui_mouse_event.
8731// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008732// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00008733 static int
8734test_gui_w32_sendevent_mouse(dict_T *args)
8735{
8736 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008737 return FALSE;
8738
Christopher Plewright20b795e2022-12-20 20:01:58 +00008739 // Note: "move" is optional, requires fewer arguments
8740 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008741
Christopher Plewright20b795e2022-12-20 20:01:58 +00008742 if (!move && (!dict_has_key(args, "button")
8743 || !dict_has_key(args, "multiclick")
8744 || !dict_has_key(args, "modifiers")))
8745 return FALSE;
8746
8747 int row = (int)dict_get_number(args, "row");
8748 int col = (int)dict_get_number(args, "col");
8749
8750 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008751 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00008752 // the "move" argument expects row and col coordnates to be in pixels,
8753 // unless "cell" is specified and is TRUE.
8754 if (dict_get_bool(args, "cell", FALSE))
8755 {
8756 // calculate the middle of the character cell
8757 // Note: Cell coordinates are 1-based from vimscript
8758 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
8759 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
8760 gui_mouse_moved(pX, pY);
8761 }
8762 else
8763 gui_mouse_moved(col, row);
8764 }
8765 else
8766 {
8767 int button = (int)dict_get_number(args, "button");
8768 int repeated_click = (int)dict_get_number(args, "multiclick");
8769 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008770
Christopher Plewright20b795e2022-12-20 20:01:58 +00008771 // Reset the scroll values to known values.
8772 // XXX: Remove this when/if the scroll step is made configurable.
8773 mouse_set_hor_scroll_step(6);
8774 mouse_set_vert_scroll_step(3);
8775
8776 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
8777 repeated_click, mods);
8778 }
8779 return TRUE;
8780}
8781
8782 static int
8783test_gui_w32_sendevent_keyboard(dict_T *args)
8784{
8785 INPUT inputs[1];
8786 INPUT modkeys[3];
8787 SecureZeroMemory(inputs, sizeof(INPUT));
8788 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
8789
8790 char_u *event = dict_get_string(args, "event", TRUE);
8791
8792 if (event && (STRICMP(event, "keydown") == 0
8793 || STRICMP(event, "keyup") == 0))
8794 {
8795 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008796 if (vkCode <= 0 || vkCode >= 0xFF)
8797 {
8798 semsg(_(e_invalid_argument_nr), (long)vkCode);
8799 return FALSE;
8800 }
8801
Christopher Plewright20b795e2022-12-20 20:01:58 +00008802 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
8803 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
8804 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
8805 || vkCode == VK_LMENU || vkCode == VK_RMENU );
8806
8807 BOOL unwrapMods = FALSE;
8808 int mods = (int)dict_get_number(args, "modifiers");
8809
8810 // If there are modifiers in the args, and it is not a keyup event and
8811 // vkCode is not a modifier key, then we generate virtual modifier key
8812 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01008813 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008814 {
8815 int n = 0;
8816 if (mods & MOD_MASK_SHIFT)
8817 {
8818 modkeys[n].type = INPUT_KEYBOARD;
8819 modkeys[n].ki.wVk = VK_LSHIFT;
8820 n++;
8821 }
8822 if (mods & MOD_MASK_CTRL)
8823 {
8824 modkeys[n].type = INPUT_KEYBOARD;
8825 modkeys[n].ki.wVk = VK_LCONTROL;
8826 n++;
8827 }
8828 if (mods & MOD_MASK_ALT)
8829 {
8830 modkeys[n].type = INPUT_KEYBOARD;
8831 modkeys[n].ki.wVk = VK_LMENU;
8832 n++;
8833 }
8834 if (n)
8835 {
8836 (void)SetForegroundWindow(s_hwnd);
8837 SendInput(n, modkeys, sizeof(INPUT));
8838 }
8839 }
8840
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008841 inputs[0].type = INPUT_KEYBOARD;
8842 inputs[0].ki.wVk = vkCode;
8843 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008844 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008845 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01008846 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008847 unwrapMods = TRUE;
8848 }
8849
K.Takatafef38d82022-09-07 19:03:42 +01008850 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008851 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00008852 vim_free(event);
8853
8854 if (unwrapMods)
8855 {
8856 modkeys[0].type = INPUT_KEYBOARD;
8857 modkeys[0].ki.wVk = VK_LSHIFT;
8858 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
8859
8860 modkeys[1].type = INPUT_KEYBOARD;
8861 modkeys[1].ki.wVk = VK_LCONTROL;
8862 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
8863
8864 modkeys[2].type = INPUT_KEYBOARD;
8865 modkeys[2].ki.wVk = VK_LMENU;
8866 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
8867
8868 (void)SetForegroundWindow(s_hwnd);
8869 SendInput(3, modkeys, sizeof(INPUT));
8870 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008871 }
8872 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00008873 {
8874 if (event == NULL)
8875 {
8876 semsg(_(e_missing_argument_str), "event");
8877 }
8878 else
8879 {
8880 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8881 vim_free(event);
8882 }
8883 return FALSE;
8884 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008885 return TRUE;
8886}
Christopher Plewright20b795e2022-12-20 20:01:58 +00008887
8888 int
8889test_gui_w32_sendevent(char_u *event, dict_T *args)
8890{
8891 if (STRICMP(event, "key") == 0)
8892 return test_gui_w32_sendevent_keyboard(args);
8893 else if (STRICMP(event, "mouse") == 0)
8894 return test_gui_w32_sendevent_mouse(args);
8895 else
8896 {
8897 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8898 return FALSE;
8899 }
8900}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008901#endif