blob: 20b0a5f64e077a2d9e500c560b652fe3847f5993 [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
201# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
202# include <commctrl.h>
203# endif
204# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
Bram Moolenaar734a8672019-12-02 22:49:38 +0100206#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100207
208#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100209# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100210#endif
211
Bram Moolenaar734a8672019-12-02 22:49:38 +0100212// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100213#define DLG_PADDING_X 10
214#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100215#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100216#define DLG_VERT_PADDING_Y 4
217#define DLG_ICON_WIDTH 34
218#define DLG_ICON_HEIGHT 34
219#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000220#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100221#define DLG_FONT_POINT_SIZE 8
222#define DLG_MIN_MAX_WIDTH 400
223#define DLG_MIN_MAX_HEIGHT 400
224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226
K.Takatac81e9bf2022-01-16 14:15:49 +0000227#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100228# define WM_DPICHANGED 0x02E0
229#endif
230
231#ifndef WM_MOUSEHWHEEL
232# define WM_MOUSEHWHEEL 0x020E
233#endif
234
235#ifndef SPI_GETWHEELSCROLLCHARS
236# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000237#endif
238
LemonBoyc27747e2022-05-07 12:25:40 +0100239#ifndef SPI_SETWHEELSCROLLCHARS
240# define SPI_SETWHEELSCROLLCHARS 0x006D
241#endif
242
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100243#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245 * Define a few things for generating prototypes. This is just to avoid
246 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100248# define APIENTRY
249# define CALLBACK
250# define CONST
251# define FAR
252# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200253# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200254# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100255# define _cdecl
256typedef int BOOL;
257typedef int BYTE;
258typedef int DWORD;
259typedef int WCHAR;
260typedef int ENUMLOGFONT;
261typedef int FINDREPLACE;
262typedef int HANDLE;
263typedef int HBITMAP;
264typedef int HBRUSH;
265typedef int HDROP;
266typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100267typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100268typedef int LPARAM;
269typedef int LPCREATESTRUCT;
270typedef int LPCSTR;
271typedef int LPCTSTR;
272typedef int LPRECT;
273typedef int LPSTR;
274typedef int LPWINDOWPOS;
275typedef int LPWORD;
276typedef int LRESULT;
277typedef int HRESULT;
278# undef MSG
279typedef int MSG;
280typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100281typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100282typedef int OSVERSIONINFO;
283typedef int PWORD;
284typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100285typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100286typedef int UINT;
287typedef int WORD;
288typedef int WPARAM;
289typedef int POINT;
290typedef void *HINSTANCE;
291typedef void *HMENU;
292typedef void *HWND;
293typedef void *HDC;
294typedef void VOID;
295typedef int LPNMHDR;
296typedef int LONG;
297typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200298typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200299typedef int COLORREF;
300typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301#endif
302
K.Takata45f9cfb2022-01-21 11:11:00 +0000303static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100304static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100305static void clear_rect(RECT *rcp);
306
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static WORD s_dlgfntheight; // height of the dialog font
308static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100309
310#ifdef FEAT_MENU
311static HMENU s_menuBar = NULL;
312#endif
313#ifdef FEAT_TEAROFF
314static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100315static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100316#endif
317
Bram Moolenaar734a8672019-12-02 22:49:38 +0100318// Flag that is set while processing a message that must not be interrupted by
319// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100320static int s_busy_processing = FALSE;
321
Bram Moolenaar734a8672019-12-02 22:49:38 +0100322static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100323
324#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000325static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200326static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200328static int s_findrep_is_find; // TRUE for find dialog, FALSE
329 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330#endif
331
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100332HWND s_hwnd = NULL;
333static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100334static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100335
336#ifdef FEAT_TOOLBAR
337static HWND s_toolbarhwnd = NULL;
338static WNDPROC s_toolbar_wndproc = NULL;
339#endif
340
341#ifdef FEAT_GUI_TABLINE
342static HWND s_tabhwnd = NULL;
343static WNDPROC s_tabline_wndproc = NULL;
344static int showing_tabline = 0;
345#endif
346
347static WPARAM s_wParam = 0;
348static LPARAM s_lParam = 0;
349
350static HWND s_textArea = NULL;
351static UINT s_uMsg = 0;
352
Bram Moolenaar734a8672019-12-02 22:49:38 +0100353static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100354
355static int s_need_activate = FALSE;
356
Bram Moolenaar734a8672019-12-02 22:49:38 +0100357// This variable is set when waiting for an event, which is the only moment
358// scrollbar dragging can be done directly. It's not allowed while commands
359// are executed, because it may move the cursor and that may cause unexpected
360// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100361static int allow_scrollbar = FALSE;
362
K.Takatac81e9bf2022-01-16 14:15:49 +0000363#ifndef _DPI_AWARENESS_CONTEXTS_
364typedef HANDLE DPI_AWARENESS_CONTEXT;
365
366typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000367 DPI_AWARENESS_INVALID = -1,
368 DPI_AWARENESS_UNAWARE = 0,
369 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000370 DPI_AWARENESS_PER_MONITOR_AWARE = 2
371} DPI_AWARENESS;
372
K.Takatab0b2b732022-01-19 12:59:21 +0000373# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
374# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000375# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
376# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
377# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
378#endif
379
380#define DEFAULT_DPI 96
381static int s_dpi = DEFAULT_DPI;
382static BOOL s_in_dpichanged = FALSE;
383static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
384
385static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
386static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
387static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
388//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
389static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
390static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
391
392 static UINT WINAPI
393stubGetDpiForSystem(void)
394{
395 HWND hwnd = GetDesktopWindow();
396 HDC hdc = GetWindowDC(hwnd);
397 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
398 ReleaseDC(hwnd, hdc);
399 return dpi;
400}
401
402 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100403stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000404{
405 return GetSystemMetrics(nIndex);
406}
407
408 static int
409adjust_fontsize_by_dpi(int size)
410{
411 return size * s_dpi / (int)pGetDpiForSystem();
412}
413
414 static int
415adjust_by_system_dpi(int size)
416{
417 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
418}
419
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100420#if defined(FEAT_DIRECTX)
421 static int
422directx_enabled(void)
423{
424 if (s_dwc != NULL)
425 return 1;
426 else if (s_directx_load_attempted)
427 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100428 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100429 DWrite_Init();
430 s_directx_load_attempted = 1;
431 s_dwc = DWriteContext_Open();
432 directx_binddc();
433 return s_dwc != NULL ? 1 : 0;
434}
435
436 static void
437directx_binddc(void)
438{
439 if (s_textArea != NULL)
440 {
441 RECT rect;
442 GetClientRect(s_textArea, &rect);
443 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
444 }
445}
446#endif
447
Bram Moolenaar734a8672019-12-02 22:49:38 +0100448extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100449
450static struct
451{
452 UINT key_sym;
453 char_u vim_code0;
454 char_u vim_code1;
455} special_keys[] =
456{
457 {VK_UP, 'k', 'u'},
458 {VK_DOWN, 'k', 'd'},
459 {VK_LEFT, 'k', 'l'},
460 {VK_RIGHT, 'k', 'r'},
461
462 {VK_F1, 'k', '1'},
463 {VK_F2, 'k', '2'},
464 {VK_F3, 'k', '3'},
465 {VK_F4, 'k', '4'},
466 {VK_F5, 'k', '5'},
467 {VK_F6, 'k', '6'},
468 {VK_F7, 'k', '7'},
469 {VK_F8, 'k', '8'},
470 {VK_F9, 'k', '9'},
471 {VK_F10, 'k', ';'},
472
473 {VK_F11, 'F', '1'},
474 {VK_F12, 'F', '2'},
475 {VK_F13, 'F', '3'},
476 {VK_F14, 'F', '4'},
477 {VK_F15, 'F', '5'},
478 {VK_F16, 'F', '6'},
479 {VK_F17, 'F', '7'},
480 {VK_F18, 'F', '8'},
481 {VK_F19, 'F', '9'},
482 {VK_F20, 'F', 'A'},
483
484 {VK_F21, 'F', 'B'},
485#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100486 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100487#endif
488 {VK_F22, 'F', 'C'},
489 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100490 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100491
492 {VK_HELP, '%', '1'},
493 {VK_BACK, 'k', 'b'},
494 {VK_INSERT, 'k', 'I'},
495 {VK_DELETE, 'k', 'D'},
496 {VK_HOME, 'k', 'h'},
497 {VK_END, '@', '7'},
498 {VK_PRIOR, 'k', 'P'},
499 {VK_NEXT, 'k', 'N'},
500 {VK_PRINT, '%', '9'},
501 {VK_ADD, 'K', '6'},
502 {VK_SUBTRACT, 'K', '7'},
503 {VK_DIVIDE, 'K', '8'},
504 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100505 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100506 {VK_DECIMAL, 'K', 'B'},
507
508 {VK_NUMPAD0, 'K', 'C'},
509 {VK_NUMPAD1, 'K', 'D'},
510 {VK_NUMPAD2, 'K', 'E'},
511 {VK_NUMPAD3, 'K', 'F'},
512 {VK_NUMPAD4, 'K', 'G'},
513 {VK_NUMPAD5, 'K', 'H'},
514 {VK_NUMPAD6, 'K', 'I'},
515 {VK_NUMPAD7, 'K', 'J'},
516 {VK_NUMPAD8, 'K', 'K'},
517 {VK_NUMPAD9, 'K', 'L'},
518
Bram Moolenaar734a8672019-12-02 22:49:38 +0100519 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100520 {VK_SPACE, ' ', NUL},
521 {VK_TAB, TAB, NUL},
522 {VK_ESCAPE, ESC, NUL},
523 {NL, NL, NUL},
524 {CAR, CAR, NUL},
525
Bram Moolenaar734a8672019-12-02 22:49:38 +0100526 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100527 {0, 0, 0}
528};
529
Bram Moolenaar734a8672019-12-02 22:49:38 +0100530// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100531static int s_button_pending = -1;
532
Bram Moolenaar734a8672019-12-02 22:49:38 +0100533// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
534// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100535static int s_getting_focus = FALSE;
536
537static int s_x_pending;
538static int s_y_pending;
539static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000540static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100541static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100542static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200543static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
544 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100545
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100546#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100547// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000549static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100550#endif
551
552/*
553 * For control IME.
554 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100555 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100556 */
K.Takata4ac893f2022-01-20 12:44:28 +0000557#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100558// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100559static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100560// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100561static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100562#endif
563
564#ifdef FEAT_MBYTE_IME
565static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
566#endif
567
568#if defined(FEAT_BROWSE)
569static char_u *convert_filter(char_u *s);
570#endif
571
572#ifdef DEBUG_PRINT_ERROR
573/*
574 * Print out the last Windows error message
575 */
576 static void
577print_windows_error(void)
578{
579 LPVOID lpMsgBuf;
580
581 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
582 NULL, GetLastError(),
583 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
584 (LPTSTR) &lpMsgBuf, 0, NULL);
585 TRACE1("Error: %s\n", lpMsgBuf);
586 LocalFree(lpMsgBuf);
587}
588#endif
589
590/*
591 * Cursor blink functions.
592 *
593 * This is a simple state machine:
594 * BLINK_NONE not blinking at all
595 * BLINK_OFF blinking, cursor is not shown
596 * BLINK_ON blinking, cursor is shown
597 */
598
599#define BLINK_NONE 0
600#define BLINK_OFF 1
601#define BLINK_ON 2
602
603static int blink_state = BLINK_NONE;
604static long_u blink_waittime = 700;
605static long_u blink_ontime = 400;
606static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000607static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100608
Bram Moolenaar703a8042016-06-04 16:24:32 +0200609 int
610gui_mch_is_blinking(void)
611{
612 return blink_state != BLINK_NONE;
613}
614
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200615 int
616gui_mch_is_blink_off(void)
617{
618 return blink_state == BLINK_OFF;
619}
620
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100621 void
622gui_mch_set_blinking(long wait, long on, long off)
623{
624 blink_waittime = wait;
625 blink_ontime = on;
626 blink_offtime = off;
627}
628
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100629 static VOID CALLBACK
630_OnBlinkTimer(
631 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100632 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000633 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100634 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635{
636 MSG msg;
637
638 /*
639 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
640 */
641
642 KillTimer(NULL, idEvent);
643
Bram Moolenaar734a8672019-12-02 22:49:38 +0100644 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000645 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646 ;
647
648 if (blink_state == BLINK_ON)
649 {
650 gui_undraw_cursor();
651 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000652 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 }
654 else
655 {
656 gui_update_cursor(TRUE, FALSE);
657 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000658 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100659 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100660 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100661}
662
663 static void
664gui_mswin_rm_blink_timer(void)
665{
666 MSG msg;
667
668 if (blink_timer != 0)
669 {
670 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100671 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000672 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673 ;
674 blink_timer = 0;
675 }
676}
677
678/*
679 * Stop the cursor blinking. Show the cursor if it wasn't shown.
680 */
681 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100682gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683{
684 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100685 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100686 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100687 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100688 gui_mch_flush();
689 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 blink_state = BLINK_NONE;
691}
692
693/*
694 * Start the cursor blinking. If it was already blinking, this restarts the
695 * waiting time and shows the cursor.
696 */
697 void
698gui_mch_start_blink(void)
699{
700 gui_mswin_rm_blink_timer();
701
Bram Moolenaar734a8672019-12-02 22:49:38 +0100702 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100703 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
704 {
K.Takataa8ec4912022-02-03 14:32:33 +0000705 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 blink_state = BLINK_ON;
707 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100708 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 }
710}
711
712/*
713 * Call-back routines.
714 */
715
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716 static VOID CALLBACK
717_OnTimer(
718 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100719 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000720 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100721 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100722{
723 MSG msg;
724
725 /*
726 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
727 */
728 KillTimer(NULL, idEvent);
729 s_timed_out = TRUE;
730
Bram Moolenaar734a8672019-12-02 22:49:38 +0100731 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000732 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100733 ;
734 if (idEvent == s_wait_timer)
735 s_wait_timer = 0;
736}
737
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100738 static void
739_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100740 HWND hwnd UNUSED,
741 UINT ch UNUSED,
742 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100743{
744 dead_key = 1;
745}
746
747/*
748 * Convert Unicode character "ch" to bytes in "string[slen]".
749 * When "had_alt" is TRUE the ALT key was included in "ch".
750 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200751 * Because the Windows API uses UTF-16, we have to deal with surrogate
752 * pairs; this is where we choose to deal with them: if "ch" is a high
753 * surrogate, it will be stored, and the length returned will be zero; the next
754 * char_to_string call will then include the high surrogate, decoding the pair
755 * of UTF-16 code units to a single Unicode code point, presuming it is the
756 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757 */
758 static int
759char_to_string(int ch, char_u *string, int slen, int had_alt)
760{
761 int len;
762 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100763 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200764 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200766 if (surrogate_pending_ch != 0)
767 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100768 // We don't guarantee ch is a low surrogate to match the high surrogate
769 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200770 wstring[0] = surrogate_pending_ch;
771 wstring[1] = ch;
772 surrogate_pending_ch = 0;
773 len = 2;
774 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200776 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100777 // We don't have the entire code point yet, only the first UTF-16 code
778 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200779 surrogate_pending_ch = ch;
780 return 0;
781 }
782 else
783 {
784 wstring[0] = ch;
785 len = 1;
786 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200787
Bram Moolenaar734a8672019-12-02 22:49:38 +0100788 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
789 // "enc_codepage" is non-zero use the standard Win32 function,
790 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100792 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
794 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100795 // If we had included the ALT key into the character but now the
796 // upper bit is no longer set, that probably means the conversion
797 // failed. Convert the original character and set the upper bit
798 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100800 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200801 wstring[0] = ch & 0x7f;
802 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
803 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100804 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200805 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100806 }
807 }
808 else
809 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200810 ws = utf16_to_enc(wstring, &len);
811 if (ws == NULL)
812 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100813 else
814 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100815 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200816 len = slen;
817 mch_memmove(string, ws, len);
818 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100819 }
820 }
821
822 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100823 {
824 string[0] = ch;
825 len = 1;
826 }
827
828 for (i = 0; i < len; ++i)
829 if (string[i] == CSI && len <= slen - 2)
830 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100831 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100832 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
833 string[++i] = KS_EXTRA;
834 string[++i] = (int)KE_CSI;
835 len += 2;
836 }
837
838 return len;
839}
840
LemonBoy45684c62022-04-24 15:46:42 +0100841 static int
842get_active_modifiers(void)
843{
844 int modifiers = 0;
845
846 if (GetKeyState(VK_CONTROL) & 0x8000)
847 modifiers |= MOD_MASK_CTRL;
848 if (GetKeyState(VK_SHIFT) & 0x8000)
849 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100850 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
851 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100852 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100853 if (GetKeyState(VK_LMENU) & 0x8000)
854 modifiers |= MOD_MASK_ALT;
855 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
856 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100857
858 return modifiers;
859}
860
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861/*
862 * Key hit, add it to the input buffer.
863 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100864 static void
865_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100866 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100867 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100868 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100869{
870 char_u string[40];
871 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100872 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100873 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100874
Anton Sharonov3f026672022-07-26 21:26:18 +0100875 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
876 return;
877
878 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
879 // process_message()
880 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
881 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100882
LemonBoy45684c62022-04-24 15:46:42 +0100883 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100884
885 ch = simplify_key(ch, &modifiers);
886 // remove the SHIFT modifier for keys where it's already included, e.g.,
887 // '(' and '*'
888 modifiers = may_remove_shift_modifier(modifiers, ch);
889
890 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
891 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
892 if (ch == CSI)
893 ch = K_CSI;
894
895 if (modifiers)
896 {
897 string[0] = CSI;
898 string[1] = KS_MODIFIER;
899 string[2] = modifiers;
900 add_to_input_buf(string, 3);
901 }
902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 len = char_to_string(ch, string, 40, FALSE);
904 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
905 {
906 trash_input_buf();
907 got_int = TRUE;
908 }
909
910 add_to_input_buf(string, len);
911}
912
913/*
914 * Alt-Key hit, add it to the input buffer.
915 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100916 static void
917_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100918 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100919 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100920 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100921{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100922 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100923 int len;
924 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100925 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100926
Anton Sharonov3f026672022-07-26 21:26:18 +0100927 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100928
Bram Moolenaar734a8672019-12-02 22:49:38 +0100929 // OK, we have a character key (given by ch) which was entered with the
930 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
931 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
932 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100933 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100934 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100935 // remove the SHIFT modifier for keys where it's already included, e.g.,
936 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200937 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100938
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200939 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
940 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100941 if (ch == CSI)
942 ch = K_CSI;
943
944 len = 0;
945 if (modifiers)
946 {
947 string[len++] = CSI;
948 string[len++] = KS_MODIFIER;
949 string[len++] = modifiers;
950 }
951
952 if (IS_SPECIAL((int)ch))
953 {
954 string[len++] = CSI;
955 string[len++] = K_SECOND((int)ch);
956 string[len++] = K_THIRD((int)ch);
957 }
958 else
959 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100960 // Although the documentation isn't clear about it, we assume "ch" is
961 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100962 len += char_to_string(ch, string + len, 40 - len, TRUE);
963 }
964
965 add_to_input_buf(string, len);
966}
967
968 static void
969_OnMouseEvent(
970 int button,
971 int x,
972 int y,
973 int repeated_click,
974 UINT keyFlags)
975{
976 int vim_modifiers = 0x0;
977
978 s_getting_focus = FALSE;
979
980 if (keyFlags & MK_SHIFT)
981 vim_modifiers |= MOUSE_SHIFT;
982 if (keyFlags & MK_CONTROL)
983 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100984 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100985 vim_modifiers |= MOUSE_ALT;
986
987 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
988}
989
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100990 static void
991_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100992 HWND hwnd UNUSED,
993 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100994 int x,
995 int y,
996 UINT keyFlags)
997{
998 static LONG s_prevTime = 0;
999
1000 LONG currentTime = GetMessageTime();
1001 int button = -1;
1002 int repeated_click;
1003
Bram Moolenaar734a8672019-12-02 22:49:38 +01001004 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001005 (void)SetFocus(s_hwnd);
1006
1007 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1008 button = MOUSE_LEFT;
1009 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1010 button = MOUSE_MIDDLE;
1011 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1012 button = MOUSE_RIGHT;
1013 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1014 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001015 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1016 }
1017 else if (s_uMsg == WM_CAPTURECHANGED)
1018 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001019 // on W95/NT4, somehow you get in here with an odd Msg
1020 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001021 if (s_button_pending == MOUSE_LEFT)
1022 button = MOUSE_RIGHT;
1023 else
1024 button = MOUSE_LEFT;
1025 }
1026 if (button >= 0)
1027 {
1028 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1029
1030 /*
1031 * Holding down the left and right buttons simulates pushing the middle
1032 * button.
1033 */
1034 if (repeated_click
1035 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1036 || (button == MOUSE_RIGHT
1037 && s_button_pending == MOUSE_LEFT)))
1038 {
1039 /*
1040 * Hmm, gui.c will ignore more than one button down at a time, so
1041 * pretend we let go of it first.
1042 */
1043 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1044 button = MOUSE_MIDDLE;
1045 repeated_click = FALSE;
1046 s_button_pending = -1;
1047 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1048 }
1049 else if ((repeated_click)
1050 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1051 {
1052 if (s_button_pending > -1)
1053 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001054 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1055 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001056 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001057 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001058 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1059 }
1060 else
1061 {
1062 /*
1063 * If this is the first press (i.e. not a multiple click) don't
1064 * action immediately, but store and wait for:
1065 * i) button-up
1066 * ii) mouse move
1067 * iii) another button press
1068 * before using it.
1069 * This enables us to make left+right simulate middle button,
1070 * without left or right being actioned first. The side-effect is
1071 * that if you click and hold the mouse without dragging, the
1072 * cursor doesn't move until you release the button. In practice
1073 * this is hardly a problem.
1074 */
1075 s_button_pending = button;
1076 s_x_pending = x;
1077 s_y_pending = y;
1078 s_kFlags_pending = keyFlags;
1079 }
1080
1081 s_prevTime = currentTime;
1082 }
1083}
1084
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001085 static void
1086_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001087 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001088 int x,
1089 int y,
1090 UINT keyFlags)
1091{
1092 int button;
1093
1094 s_getting_focus = FALSE;
1095 if (s_button_pending > -1)
1096 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001097 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001098 _OnMouseEvent(s_button_pending, s_x_pending,
1099 s_y_pending, FALSE, s_kFlags_pending);
1100 s_button_pending = -1;
1101 }
1102 if (s_uMsg == WM_MOUSEMOVE)
1103 {
1104 /*
1105 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1106 * down.
1107 */
1108 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1109 | MK_XBUTTON1 | MK_XBUTTON2)))
1110 {
1111 gui_mouse_moved(x, y);
1112 return;
1113 }
1114
1115 /*
1116 * While button is down, keep grabbing mouse move events when
1117 * the mouse goes outside the window
1118 */
1119 SetCapture(s_textArea);
1120 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001121 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001122 }
1123 else
1124 {
1125 ReleaseCapture();
1126 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001127 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001128 }
1129
1130 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1131}
1132
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001133 static void
1134_OnSizeTextArea(
1135 HWND hwnd UNUSED,
1136 UINT state UNUSED,
1137 int cx UNUSED,
1138 int cy UNUSED)
1139{
1140#if defined(FEAT_DIRECTX)
1141 if (IS_ENABLE_DIRECTX())
1142 directx_binddc();
1143#endif
1144}
1145
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001146#ifdef FEAT_MENU
1147/*
1148 * Find the vimmenu_T with the given id
1149 */
1150 static vimmenu_T *
1151gui_mswin_find_menu(
1152 vimmenu_T *pMenu,
1153 int id)
1154{
1155 vimmenu_T *pChildMenu;
1156
1157 while (pMenu)
1158 {
1159 if (pMenu->id == (UINT)id)
1160 break;
1161 if (pMenu->children != NULL)
1162 {
1163 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1164 if (pChildMenu)
1165 {
1166 pMenu = pChildMenu;
1167 break;
1168 }
1169 }
1170 pMenu = pMenu->next;
1171 }
1172 return pMenu;
1173}
1174
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001175 static void
1176_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001177 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001178 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001179 HWND hwndCtl UNUSED,
1180 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001181{
1182 vimmenu_T *pMenu;
1183
1184 pMenu = gui_mswin_find_menu(root_menu, id);
1185 if (pMenu)
1186 gui_menu_cb(pMenu);
1187}
1188#endif
1189
1190#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001191/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 * Handle a Find/Replace window message.
1193 */
1194 static void
1195_OnFindRepl(void)
1196{
1197 int flags = 0;
1198 int down;
1199
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001201 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001202 (void)SetFocus(s_hwnd);
1203
1204 if (s_findrep_struct.Flags & FR_FINDNEXT)
1205 {
1206 flags = FRD_FINDNEXT;
1207
Bram Moolenaar734a8672019-12-02 22:49:38 +01001208 // Give main window the focus back: this is so the cursor isn't
1209 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001210 (void)SetFocus(s_hwnd);
1211 }
1212 else if (s_findrep_struct.Flags & FR_REPLACE)
1213 {
1214 flags = FRD_REPLACE;
1215
Bram Moolenaar734a8672019-12-02 22:49:38 +01001216 // Give main window the focus back: this is so the cursor isn't
1217 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001218 (void)SetFocus(s_hwnd);
1219 }
1220 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1221 {
1222 flags = FRD_REPLACEALL;
1223 }
1224
1225 if (flags != 0)
1226 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001227 char_u *p, *q;
1228
Bram Moolenaar734a8672019-12-02 22:49:38 +01001229 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001230 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1231 flags |= FRD_WHOLE_WORD;
1232 if (s_findrep_struct.Flags & FR_MATCHCASE)
1233 flags |= FRD_MATCH_CASE;
1234 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001235 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1236 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1237 if (p != NULL && q != NULL)
1238 gui_do_findrepl(flags, p, q, down);
1239 vim_free(p);
1240 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001241 }
1242}
1243#endif
1244
1245 static void
1246HandleMouseHide(UINT uMsg, LPARAM lParam)
1247{
1248 static LPARAM last_lParam = 0L;
1249
Bram Moolenaar734a8672019-12-02 22:49:38 +01001250 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001251 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1252 {
1253 if (lParam == last_lParam)
1254 return;
1255 last_lParam = lParam;
1256 }
1257
Bram Moolenaar734a8672019-12-02 22:49:38 +01001258 // Handle specially, to centralise coding. We need to be sure we catch all
1259 // possible events which should cause us to restore the cursor (as it is a
1260 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261 switch (uMsg)
1262 {
1263 case WM_KEYUP:
1264 case WM_CHAR:
1265 /*
1266 * blank out the pointer if necessary
1267 */
1268 if (p_mh)
1269 gui_mch_mousehide(TRUE);
1270 break;
1271
Bram Moolenaar734a8672019-12-02 22:49:38 +01001272 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001274 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001275 case WM_LBUTTONDOWN:
1276 case WM_LBUTTONUP:
1277 case WM_MBUTTONDOWN:
1278 case WM_MBUTTONUP:
1279 case WM_RBUTTONDOWN:
1280 case WM_RBUTTONUP:
1281 case WM_XBUTTONDOWN:
1282 case WM_XBUTTONUP:
1283 case WM_NCMOUSEMOVE:
1284 case WM_NCLBUTTONDOWN:
1285 case WM_NCLBUTTONUP:
1286 case WM_NCMBUTTONDOWN:
1287 case WM_NCMBUTTONUP:
1288 case WM_NCRBUTTONDOWN:
1289 case WM_NCRBUTTONUP:
1290 case WM_KILLFOCUS:
1291 /*
1292 * if the pointer is currently hidden, then we should show it.
1293 */
1294 gui_mch_mousehide(FALSE);
1295 break;
1296 }
1297}
1298
1299 static LRESULT CALLBACK
1300_TextAreaWndProc(
1301 HWND hwnd,
1302 UINT uMsg,
1303 WPARAM wParam,
1304 LPARAM lParam)
1305{
1306 /*
1307 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1308 hwnd, uMsg, wParam, lParam);
1309 */
1310
1311 HandleMouseHide(uMsg, lParam);
1312
1313 s_uMsg = uMsg;
1314 s_wParam = wParam;
1315 s_lParam = lParam;
1316
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001317#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001318 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001319#endif
1320
1321 switch (uMsg)
1322 {
1323 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1324 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1325 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1326 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1327 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1328 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1329 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1330 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1331 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1332 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1333 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1334 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1335 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1336 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001337 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001338
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001339#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001340 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1341 return TRUE;
1342#endif
1343 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001344 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001345 }
1346}
1347
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001348/*
1349 * Called when the foreground or background color has been changed.
1350 */
1351 void
1352gui_mch_new_colors(void)
1353{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001354 HBRUSH prevBrush;
1355
1356 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001357 prevBrush = (HBRUSH)SetClassLongPtr(
1358 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001359 InvalidateRect(s_hwnd, NULL, TRUE);
1360 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001361}
1362
1363/*
1364 * Set the colors to their default values.
1365 */
1366 void
1367gui_mch_def_colors(void)
1368{
1369 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1370 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1371 gui.def_norm_pixel = gui.norm_pixel;
1372 gui.def_back_pixel = gui.back_pixel;
1373}
1374
1375/*
1376 * Open the GUI window which was created by a call to gui_mch_init().
1377 */
1378 int
1379gui_mch_open(void)
1380{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001381 // Actually open the window, if not already visible
1382 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001383 if (!IsWindowVisible(s_hwnd))
1384 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1385
1386#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001387 // Init replace string here, so that we keep it when re-opening the
1388 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001389 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1390#endif
1391
1392 return OK;
1393}
1394
1395/*
1396 * Get the position of the top left corner of the window.
1397 */
1398 int
1399gui_mch_get_winpos(int *x, int *y)
1400{
1401 RECT rect;
1402
1403 GetWindowRect(s_hwnd, &rect);
1404 *x = rect.left;
1405 *y = rect.top;
1406 return OK;
1407}
1408
1409/*
1410 * Set the position of the top left corner of the window to the given
1411 * coordinates.
1412 */
1413 void
1414gui_mch_set_winpos(int x, int y)
1415{
1416 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1417 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1418}
1419 void
1420gui_mch_set_text_area_pos(int x, int y, int w, int h)
1421{
1422 static int oldx = 0;
1423 static int oldy = 0;
1424
1425 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1426
1427#ifdef FEAT_TOOLBAR
1428 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1429 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001430 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001431#endif
1432#if defined(FEAT_GUI_TABLINE)
1433 if (showing_tabline)
1434 {
1435 int top = 0;
1436 RECT rect;
1437
1438# ifdef FEAT_TOOLBAR
1439 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001440 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001441# endif
1442 GetClientRect(s_hwnd, &rect);
1443 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1444 }
1445#endif
1446
Bram Moolenaar734a8672019-12-02 22:49:38 +01001447 // When side scroll bar is unshown, the size of window will change.
1448 // then, the text area move left or right. thus client rect should be
1449 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001450 if (oldx != x || oldy != y)
1451 {
1452 InvalidateRect(s_hwnd, NULL, FALSE);
1453 oldx = x;
1454 oldy = y;
1455 }
1456}
1457
1458
1459/*
1460 * Scrollbar stuff:
1461 */
1462
1463 void
1464gui_mch_enable_scrollbar(
1465 scrollbar_T *sb,
1466 int flag)
1467{
1468 ShowScrollBar(sb->id, SB_CTL, flag);
1469
Bram Moolenaar734a8672019-12-02 22:49:38 +01001470 // TODO: When the window is maximized, the size of the window stays the
1471 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1472 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001473}
1474
1475 void
1476gui_mch_set_scrollbar_pos(
1477 scrollbar_T *sb,
1478 int x,
1479 int y,
1480 int w,
1481 int h)
1482{
1483 SetWindowPos(sb->id, NULL, x, y, w, h,
1484 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1485}
1486
Bram Moolenaar203ec772020-07-17 20:43:43 +02001487 int
1488gui_mch_get_scrollbar_xpadding(void)
1489{
1490 RECT rcTxt, rcWnd;
1491 int xpad;
1492
1493 GetWindowRect(s_textArea, &rcTxt);
1494 GetWindowRect(s_hwnd, &rcWnd);
1495 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001496 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1497 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001498 return (xpad < 0) ? 0 : xpad;
1499}
1500
1501 int
1502gui_mch_get_scrollbar_ypadding(void)
1503{
1504 RECT rcTxt, rcWnd;
1505 int ypad;
1506
1507 GetWindowRect(s_textArea, &rcTxt);
1508 GetWindowRect(s_hwnd, &rcWnd);
1509 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001510 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1511 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001512 return (ypad < 0) ? 0 : ypad;
1513}
1514
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515 void
1516gui_mch_create_scrollbar(
1517 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001518 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519{
1520 sb->id = CreateWindow(
1521 "SCROLLBAR", "Scrollbar",
1522 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001523 10, // Any value will do for now
1524 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001526 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001527}
1528
1529/*
1530 * Find the scrollbar with the given hwnd.
1531 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001532 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001533gui_mswin_find_scrollbar(HWND hwnd)
1534{
1535 win_T *wp;
1536
1537 if (gui.bottom_sbar.id == hwnd)
1538 return &gui.bottom_sbar;
1539 FOR_ALL_WINDOWS(wp)
1540 {
1541 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1542 return &wp->w_scrollbars[SBAR_LEFT];
1543 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1544 return &wp->w_scrollbars[SBAR_RIGHT];
1545 }
1546 return NULL;
1547}
1548
K.Takatac81e9bf2022-01-16 14:15:49 +00001549 static void
1550update_scrollbar_size(void)
1551{
1552 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1553 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1554}
1555
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001556/*
K.Takataabe628e2022-01-23 16:25:17 +00001557 * Get the average character size of a font.
1558 */
1559 static void
1560GetAverageFontSize(HDC hdc, SIZE *size)
1561{
1562 // GetTextMetrics() may not return the right value in tmAveCharWidth
1563 // for some fonts. Do our own average computation.
1564 GetTextExtentPoint(hdc,
1565 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1566 52, size);
1567 size->cx = (size->cx / 26 + 1) / 2;
1568}
1569
1570/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001571 * Get the character size of a font.
1572 */
1573 static void
1574GetFontSize(GuiFont font)
1575{
1576 HWND hwnd = GetDesktopWindow();
1577 HDC hdc = GetWindowDC(hwnd);
1578 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001579 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001580 TEXTMETRIC tm;
1581
1582 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001583 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
K.Takataabe628e2022-01-23 16:25:17 +00001585 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001586 gui.char_height = tm.tmHeight + p_linespace;
1587
1588 SelectFont(hdc, hfntOld);
1589
1590 ReleaseDC(hwnd, hdc);
1591}
1592
1593/*
1594 * Adjust gui.char_height (after 'linespace' was changed).
1595 */
1596 int
1597gui_mch_adjust_charheight(void)
1598{
1599 GetFontSize(gui.norm_font);
1600 return OK;
1601}
1602
1603 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001604get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001605{
1606 HFONT font = NULL;
1607
Bram Moolenaar734a8672019-12-02 22:49:38 +01001608 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001609 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001610
1611 if (font == NULL)
1612 return NOFONT;
1613
1614 return (GuiFont)font;
1615}
1616
1617 static int
1618pixels_to_points(int pixels, int vertical)
1619{
1620 int points;
1621 HWND hwnd;
1622 HDC hdc;
1623
1624 hwnd = GetDesktopWindow();
1625 hdc = GetWindowDC(hwnd);
1626
1627 points = MulDiv(pixels, 72,
1628 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1629
1630 ReleaseDC(hwnd, hdc);
1631
1632 return points;
1633}
1634
1635 GuiFont
1636gui_mch_get_font(
1637 char_u *name,
1638 int giveErrorIfMissing)
1639{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001640 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001641 GuiFont font = NOFONT;
1642
1643 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001644 {
1645 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001647 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001649 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001650 return font;
1651}
1652
1653#if defined(FEAT_EVAL) || defined(PROTO)
1654/*
1655 * Return the name of font "font" in allocated memory.
1656 * Don't know how to get the actual name, thus use the provided name.
1657 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001658 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001659gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001660{
1661 if (name == NULL)
1662 return NULL;
1663 return vim_strsave(name);
1664}
1665#endif
1666
1667 void
1668gui_mch_free_font(GuiFont font)
1669{
1670 if (font)
1671 DeleteObject((HFONT)font);
1672}
1673
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001674/*
1675 * Return the Pixel value (color) for the given color name.
1676 * Return INVALCOLOR for error.
1677 */
1678 guicolor_T
1679gui_mch_get_color(char_u *name)
1680{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001681 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001682
1683 typedef struct SysColorTable
1684 {
1685 char *name;
1686 int color;
1687 } SysColorTable;
1688
1689 static SysColorTable sys_table[] =
1690 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1692 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001693#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001694 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1695#endif
1696 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1697 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1698 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1699 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1700 {"SYS_DESKTOP", COLOR_DESKTOP},
1701 {"SYS_INFOBK", COLOR_INFOBK},
1702 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1703 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001704 {"SYS_BTNFACE", COLOR_BTNFACE},
1705 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1706 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1707 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1708 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1709 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1710 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1711 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1712 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1713 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1714 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1715 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1716 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1717 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1718 {"SYS_MENU", COLOR_MENU},
1719 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1720 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1721 {"SYS_WINDOW", COLOR_WINDOW},
1722 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1723 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1724 };
1725
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001726 /*
1727 * Try to look up a system colour.
1728 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001729 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001730 if (STRICMP(name, sys_table[i].name) == 0)
1731 return GetSysColor(sys_table[i].color);
1732
Bram Moolenaarab302212016-04-26 20:59:29 +02001733 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001734}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001735
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001736 guicolor_T
1737gui_mch_get_rgb_color(int r, int g, int b)
1738{
1739 return gui_get_rgb_color_cmn(r, g, b);
1740}
1741
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001742/*
1743 * Return OK if the key with the termcap name "name" is supported.
1744 */
1745 int
1746gui_mch_haskey(char_u *name)
1747{
1748 int i;
1749
1750 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001751 if (name[0] == special_keys[i].vim_code0
1752 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753 return OK;
1754 return FAIL;
1755}
1756
1757 void
1758gui_mch_beep(void)
1759{
LemonBoy77771d32022-04-13 11:47:25 +01001760 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761}
1762/*
1763 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1764 */
1765 void
1766gui_mch_invert_rectangle(
1767 int r,
1768 int c,
1769 int nr,
1770 int nc)
1771{
1772 RECT rc;
1773
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001774#if defined(FEAT_DIRECTX)
1775 if (IS_ENABLE_DIRECTX())
1776 DWriteContext_Flush(s_dwc);
1777#endif
1778
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001779 /*
1780 * Note: InvertRect() excludes right and bottom of rectangle.
1781 */
1782 rc.left = FILL_X(c);
1783 rc.top = FILL_Y(r);
1784 rc.right = rc.left + nc * gui.char_width;
1785 rc.bottom = rc.top + nr * gui.char_height;
1786 InvertRect(s_hdc, &rc);
1787}
1788
1789/*
1790 * Iconify the GUI window.
1791 */
1792 void
1793gui_mch_iconify(void)
1794{
1795 ShowWindow(s_hwnd, SW_MINIMIZE);
1796}
1797
1798/*
1799 * Draw a cursor without focus.
1800 */
1801 void
1802gui_mch_draw_hollow_cursor(guicolor_T color)
1803{
1804 HBRUSH hbr;
1805 RECT rc;
1806
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001807#if defined(FEAT_DIRECTX)
1808 if (IS_ENABLE_DIRECTX())
1809 DWriteContext_Flush(s_dwc);
1810#endif
1811
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001812 /*
1813 * Note: FrameRect() excludes right and bottom of rectangle.
1814 */
1815 rc.left = FILL_X(gui.col);
1816 rc.top = FILL_Y(gui.row);
1817 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 if (mb_lefthalve(gui.row, gui.col))
1819 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001820 rc.bottom = rc.top + gui.char_height;
1821 hbr = CreateSolidBrush(color);
1822 FrameRect(s_hdc, &rc, hbr);
1823 DeleteBrush(hbr);
1824}
1825/*
1826 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1827 * color "color".
1828 */
1829 void
1830gui_mch_draw_part_cursor(
1831 int w,
1832 int h,
1833 guicolor_T color)
1834{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001835 RECT rc;
1836
1837 /*
1838 * Note: FillRect() excludes right and bottom of rectangle.
1839 */
1840 rc.left =
1841#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001842 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001843 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1844#endif
1845 FILL_X(gui.col);
1846 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1847 rc.right = rc.left + w;
1848 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001849
Bram Moolenaar92467d32017-12-05 13:22:16 +01001850 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851}
1852
1853
1854/*
1855 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1856 * dead key's nominal character and re-post the original message.
1857 */
1858 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001859outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001860{
1861 static MSG deadCharExpel;
1862
Anton Sharonov3f026672022-07-26 21:26:18 +01001863 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 return;
1865
Anton Sharonov3f026672022-07-26 21:26:18 +01001866 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001867
Bram Moolenaar734a8672019-12-02 22:49:38 +01001868 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869 deadCharExpel.message = originalMsg.message;
1870 deadCharExpel.hwnd = originalMsg.hwnd;
1871 deadCharExpel.wParam = VK_SPACE;
1872
K.Takata4ac893f2022-01-20 12:44:28 +00001873 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874
Bram Moolenaar734a8672019-12-02 22:49:38 +01001875 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001876 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1877 originalMsg.lParam);
1878}
1879
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001880/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001881 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1882 */
1883 static void
1884outputDeadKey_rePost(MSG originalMsg)
1885{
1886 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1887}
1888
1889/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001890 * Process a single Windows message.
1891 * If one is not available we hang until one is.
1892 */
1893 static void
1894process_message(void)
1895{
1896 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001897 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898 char_u string[40];
1899 int i;
1900 int modifiers = 0;
1901 int key;
1902#ifdef FEAT_MENU
1903 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1904#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001905 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906
K.Takatab7057bd2022-01-21 11:37:07 +00001907 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908
1909#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001910 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001911 if (msg.message == WM_OLE)
1912 {
1913 char_u *str = (char_u *)msg.lParam;
1914 if (str == NULL || *str == NUL)
1915 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001916 // Message can't be ours, forward it. Fixes problem with Ultramon
1917 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001918 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001919 }
1920 else
1921 {
1922 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001923 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001924 }
1925 return;
1926 }
1927#endif
1928
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001929#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001930 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001931 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001932 {
1933 HandleMouseHide(msg.message, msg.lParam);
1934 return;
1935 }
1936#endif
1937
1938 /*
1939 * Check if it's a special key that we recognise. If not, call
1940 * TranslateMessage().
1941 */
1942 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1943 {
1944 vk = (int) msg.wParam;
1945
1946 /*
1947 * Handle dead keys in special conditions in other cases we let Windows
1948 * handle them and do not interfere.
1949 *
1950 * The dead_key flag must be reset on several occasions:
1951 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1952 * consumed at that point (This is when we let Windows combine the
1953 * dead character on its own)
1954 *
1955 * - Before doing something special such as regenerating keypresses to
1956 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001957 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001958 * immediately to _OnChar() (or _OnSysChar()).
1959 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001960
1961 /*
1962 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1963 * was handled by _WndProc, this keypress we want to process normally
1964 */
1965 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1966 dead_key = DEAD_KEY_OFF;
1967
1968 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001969 {
1970 /*
Anton Sharonov3f026672022-07-26 21:26:18 +01001971 * Expell the dead key pressed with Ctrl in a special way.
1972 *
1973 * After dead key was pressed with Ctrl in some cases, ESC was
1974 * artificially injected and handled by _OnChar(), now we are
1975 * dealing with completely new key press from the user. If we don't
1976 * do anything, ToUnicode() call will interpret this vk+scan_code
1977 * under influence of "dead-modifier". To prevent this we translate
1978 * this message replacing current char from user with VK_SPACE,
1979 * which will cause WM_CHAR with dead_key's character itself. Using
1980 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1981 * ignore this one WM_CHAR event completely. Afterwards (due to
1982 * usage of PostMessage), this procedure is scheduled to be called
1983 * again with user char and on next entry we will clean
1984 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
1985 * outputDeadKey_rePost() since we do not wish to reset dead_key
1986 * value.
1987 */
1988 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1989 {
1990 outputDeadKey_rePost_Ex(msg,
1991 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
1992 return;
1993 }
1994
1995 if (dead_key != DEAD_KEY_SET_DEFAULT)
1996 {
1997 // should never happen - is there a way to make ASSERT here?
1998 return;
1999 }
2000
2001 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002002 * If a dead key was pressed and the user presses VK_SPACE,
2003 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2004 * with the dead char now, so do nothing special and let Windows
2005 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002006 *
2007 * Note that VK_SPACE combines with the dead_key's character and
2008 * only one WM_CHAR will be generated by TranslateMessage(), in
2009 * the two other cases two WM_CHAR will be generated: the dead
2010 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2011 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002012 */
2013 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2014 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002015 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002016 TranslateMessage(&msg);
2017 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002018 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002019 // In modes where we are not typing, dead keys should behave
2020 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002021 else if ((get_real_state()
2022 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002023 {
2024 outputDeadKey_rePost(msg);
2025 return;
2026 }
2027 }
2028
Bram Moolenaar734a8672019-12-02 22:49:38 +01002029 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 if (vk == VK_CANCEL)
2031 {
2032 trash_input_buf();
2033 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002034 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002035 string[0] = Ctrl_C;
2036 add_to_input_buf(string, 1);
2037 }
2038
LemonBoy77fc0b02022-04-22 22:45:52 +01002039 // This is an IME event or a synthetic keystroke, let Windows handle it.
2040 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2041 {
2042 TranslateMessage(&msg);
2043 return;
2044 }
2045
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002046 for (i = 0; special_keys[i].key_sym != 0; i++)
2047 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002048 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002049 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002050 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 {
2052 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002053 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002054 * is a key that would normally trigger the dead key nominal
2055 * character output (such as a NUMPAD printable character or
2056 * the TAB key, etc...).
2057 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002058 if (dead_key == DEAD_KEY_SET_DEFAULT
2059 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002060 || vk == VK_TAB || vk == CAR))
2061 {
2062 outputDeadKey_rePost(msg);
2063 return;
2064 }
2065
2066#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002067 // Check for <F10>: Windows selects the menu. When <F10> is
2068 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069 if (vk == VK_F10
2070 && gui.menu_is_active
2071 && check_map(k10, State, FALSE, TRUE, FALSE,
2072 NULL, NULL) == NULL)
2073 break;
2074#endif
LemonBoy45684c62022-04-24 15:46:42 +01002075 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002076
2077 if (special_keys[i].vim_code1 == NUL)
2078 key = special_keys[i].vim_code0;
2079 else
2080 key = TO_SPECIAL(special_keys[i].vim_code0,
2081 special_keys[i].vim_code1);
2082 key = simplify_key(key, &modifiers);
2083 if (key == CSI)
2084 key = K_CSI;
2085
2086 if (modifiers)
2087 {
2088 string[0] = CSI;
2089 string[1] = KS_MODIFIER;
2090 string[2] = modifiers;
2091 add_to_input_buf(string, 3);
2092 }
2093
2094 if (IS_SPECIAL(key))
2095 {
2096 string[0] = CSI;
2097 string[1] = K_SECOND(key);
2098 string[2] = K_THIRD(key);
2099 add_to_input_buf(string, 3);
2100 }
2101 else
2102 {
2103 int len;
2104
Bram Moolenaar734a8672019-12-02 22:49:38 +01002105 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106 len = char_to_string(key, string, 40, FALSE);
2107 add_to_input_buf(string, len);
2108 }
2109 break;
2110 }
2111 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002112
2113 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 if (special_keys[i].key_sym == 0)
2115 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002116 WCHAR ch[8];
2117 int len;
2118 int i;
2119 UINT scan_code;
2120
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002121 // Construct the state table with only a few modifiers, we don't
2122 // really care about the presence of Ctrl/Alt as those modifiers are
2123 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002124 memset(keyboard_state, 0, 256);
2125 if (GetKeyState(VK_SHIFT) & 0x8000)
2126 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002127 if (GetKeyState(VK_CAPITAL) & 0x0001)
2128 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002129 // Alt-Gr is synthesized as Alt + Ctrl.
2130 if ((GetKeyState(VK_RMENU) & 0x8000)
2131 && (GetKeyState(VK_CONTROL) & 0x8000))
2132 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002133 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002134 keyboard_state[VK_CONTROL] = 0x80;
2135 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002136
2137 // Translate the virtual key according to the current keyboard
2138 // layout.
2139 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2140 // Convert the scan-code into a sequence of zero or more unicode
2141 // codepoints.
2142 // If this is a dead key ToUnicode returns a negative value.
2143 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2144 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002145 if (len < 0)
2146 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002147
2148 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002149 {
2150 if ( dead_key == DEAD_KEY_SET_DEFAULT
2151 && (GetKeyState(VK_CONTROL) & 0x8000)
2152 && ( (vk == 221 && scan_code == 26) // AZERTY CTRL+dead_circumflex
2153 || (vk == 220 && scan_code == 41) // QWERTZ CTRL+dead_circumflex
2154 )
2155 )
2156 {
2157 // post WM_CHAR='[' - which will be interpreted with CTRL
2158 // stil hold as ESC
2159 PostMessageW(msg.hwnd, WM_CHAR, '[', msg.lParam);
2160 // ask _OnChar() to not touch this state, wait for next key
2161 // press and maintain knowledge that we are "poisoned" with
2162 // "dead state"
2163 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2164 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002165 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002166 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002167
2168 // Post the message as TranslateMessage would do.
2169 if (msg.message == WM_KEYDOWN)
2170 {
2171 for (i = 0; i < len; i++)
2172 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002173 }
2174 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002175 {
2176 for (i = 0; i < len; i++)
2177 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2178 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002179 }
2180 }
2181#ifdef FEAT_MBYTE_IME
2182 else if (msg.message == WM_IME_NOTIFY)
2183 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2184 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002185 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002186 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002187#endif
2188
2189#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002190 // Check for <F10>: Default effect is to select the menu. When <F10> is
2191 // mapped we need to stop it here to avoid strange effects (e.g., for the
2192 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002193 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2194 NULL, NULL) == NULL)
2195#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002196 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002197}
2198
2199/*
2200 * Catch up with any queued events. This may put keyboard input into the
2201 * input buffer, call resize call-backs, trigger timers etc. If there is
2202 * nothing in the event queue (& no timers pending), then we return
2203 * immediately.
2204 */
2205 void
2206gui_mch_update(void)
2207{
2208 MSG msg;
2209
2210 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002211 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002212 && !vim_is_input_buf_full())
2213 process_message();
2214}
2215
Bram Moolenaar4231da42016-06-02 14:30:04 +02002216 static void
2217remove_any_timer(void)
2218{
2219 MSG msg;
2220
2221 if (s_wait_timer != 0 && !s_timed_out)
2222 {
2223 KillTimer(NULL, s_wait_timer);
2224
Bram Moolenaar734a8672019-12-02 22:49:38 +01002225 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002226 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002227 ;
2228 s_wait_timer = 0;
2229 }
2230}
2231
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002232/*
2233 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2234 * from the keyboard.
2235 * wtime == -1 Wait forever.
2236 * wtime == 0 This should never happen.
2237 * wtime > 0 Wait wtime milliseconds for a character.
2238 * Returns OK if a character was found to be available within the given time,
2239 * or FAIL otherwise.
2240 */
2241 int
2242gui_mch_wait_for_chars(int wtime)
2243{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002244 int focus;
2245
2246 s_timed_out = FALSE;
2247
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002248 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002249 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002250 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002251 if (s_busy_processing)
2252 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002253
2254 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002255 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2256 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002257 }
2258
2259 allow_scrollbar = TRUE;
2260
2261 focus = gui.in_focus;
2262 while (!s_timed_out)
2263 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002264 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002265 if (gui.in_focus != focus)
2266 {
2267 if (gui.in_focus)
2268 gui_mch_start_blink();
2269 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002270 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271 focus = gui.in_focus;
2272 }
2273
2274 if (s_need_activate)
2275 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002276 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002277 s_need_activate = FALSE;
2278 }
2279
Bram Moolenaar4231da42016-06-02 14:30:04 +02002280#ifdef FEAT_TIMERS
2281 did_add_timer = FALSE;
2282#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002283#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002284 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002285 for (;;)
2286 {
2287 MSG msg;
2288
2289 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002290# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002291 if (did_add_timer)
2292 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002293# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002294 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002295 {
2296 process_message();
2297 break;
2298 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002299 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002300 // TODO: The 10 msec is a compromise between laggy response
2301 // and consuming more CPU time. Better would be to handle
2302 // channel messages when they arrive.
2303 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002304 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002305 break;
2306 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002307#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002308 // Don't use gui_mch_update() because then we will spin-lock until a
2309 // char arrives, instead we use GetMessage() to hang until an
2310 // event arrives. No need to check for input_buf_full because we are
2311 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002313#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314
2315 if (input_available())
2316 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002317 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002318 allow_scrollbar = FALSE;
2319
Bram Moolenaar89c00032019-09-04 13:53:21 +02002320 // Clear pending mouse button, the release event may have been
2321 // taken by the dialog window. But don't do this when getting
2322 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002323 if (!s_getting_focus)
2324 s_button_pending = -1;
2325
2326 return OK;
2327 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002328
2329#ifdef FEAT_TIMERS
2330 if (did_add_timer)
2331 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002332 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002333 remove_any_timer();
2334 break;
2335 }
2336#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337 }
2338 allow_scrollbar = FALSE;
2339 return FAIL;
2340}
2341
2342/*
2343 * Clear a rectangular region of the screen from text pos (row1, col1) to
2344 * (row2, col2) inclusive.
2345 */
2346 void
2347gui_mch_clear_block(
2348 int row1,
2349 int col1,
2350 int row2,
2351 int col2)
2352{
2353 RECT rc;
2354
2355 /*
2356 * Clear one extra pixel at the far right, for when bold characters have
2357 * spilled over to the window border.
2358 * Note: FillRect() excludes right and bottom of rectangle.
2359 */
2360 rc.left = FILL_X(col1);
2361 rc.top = FILL_Y(row1);
2362 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2363 rc.bottom = FILL_Y(row2 + 1);
2364 clear_rect(&rc);
2365}
2366
2367/*
2368 * Clear the whole text window.
2369 */
2370 void
2371gui_mch_clear_all(void)
2372{
2373 RECT rc;
2374
2375 rc.left = 0;
2376 rc.top = 0;
2377 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2378 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2379 clear_rect(&rc);
2380}
2381/*
2382 * Menu stuff.
2383 */
2384
2385 void
2386gui_mch_enable_menu(int flag)
2387{
2388#ifdef FEAT_MENU
2389 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2390#endif
2391}
2392
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393 void
2394gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002395 int x UNUSED,
2396 int y UNUSED,
2397 int w UNUSED,
2398 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002400 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002401}
2402
2403#if defined(FEAT_MENU) || defined(PROTO)
2404/*
2405 * Make menu item hidden or not hidden
2406 */
2407 void
2408gui_mch_menu_hidden(
2409 vimmenu_T *menu,
2410 int hidden)
2411{
2412 /*
2413 * This doesn't do what we want. Hmm, just grey the menu items for now.
2414 */
2415 /*
2416 if (hidden)
2417 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2418 else
2419 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2420 */
2421 gui_mch_menu_grey(menu, hidden);
2422}
2423
2424/*
2425 * This is called after setting all the menus to grey/hidden or not.
2426 */
2427 void
2428gui_mch_draw_menubar(void)
2429{
2430 DrawMenuBar(s_hwnd);
2431}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002432#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434/*
2435 * Return the RGB value of a pixel as a long.
2436 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002437 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438gui_mch_get_rgb(guicolor_T pixel)
2439{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002440 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2441 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442}
2443
2444#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002445/*
2446 * Convert pixels in X to dialog units
2447 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002448 static WORD
2449PixelToDialogX(int numPixels)
2450{
2451 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2452}
2453
Bram Moolenaar734a8672019-12-02 22:49:38 +01002454/*
2455 * Convert pixels in Y to dialog units
2456 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002457 static WORD
2458PixelToDialogY(int numPixels)
2459{
2460 return (WORD)((numPixels * 8) / s_dlgfntheight);
2461}
2462
Bram Moolenaar734a8672019-12-02 22:49:38 +01002463/*
2464 * Return the width in pixels of the given text in the given DC.
2465 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466 static int
2467GetTextWidth(HDC hdc, char_u *str, int len)
2468{
2469 SIZE size;
2470
2471 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2472 return size.cx;
2473}
2474
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475/*
2476 * Return the width in pixels of the given text in the given DC, taking care
2477 * of 'encoding' to active codepage conversion.
2478 */
2479 static int
2480GetTextWidthEnc(HDC hdc, char_u *str, int len)
2481{
2482 SIZE size;
2483 WCHAR *wstr;
2484 int n;
2485 int wlen = len;
2486
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002487 wstr = enc_to_utf16(str, &wlen);
2488 if (wstr == NULL)
2489 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002490
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002491 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2492 vim_free(wstr);
2493 if (n)
2494 return size.cx;
2495 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002497
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002498static void get_work_area(RECT *spi_rect);
2499
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002500/*
2501 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002502 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2503 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002504 */
2505 static BOOL
2506CenterWindow(
2507 HWND hwndChild,
2508 HWND hwndParent)
2509{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002510 HMONITOR mon;
2511 MONITORINFO moninfo;
2512 RECT rChild, rParent, rScreen;
2513 int wChild, hChild, wParent, hParent;
2514 int xNew, yNew;
2515 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516
2517 GetWindowRect(hwndChild, &rChild);
2518 wChild = rChild.right - rChild.left;
2519 hChild = rChild.bottom - rChild.top;
2520
Bram Moolenaar734a8672019-12-02 22:49:38 +01002521 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002522 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002523 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524 else
2525 GetWindowRect(hwndParent, &rParent);
2526 wParent = rParent.right - rParent.left;
2527 hParent = rParent.bottom - rParent.top;
2528
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002529 moninfo.cbSize = sizeof(MONITORINFO);
2530 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2531 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002532 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002533 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002535 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002536 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002537 hdc = GetDC(hwndChild);
2538 rScreen.left = 0;
2539 rScreen.top = 0;
2540 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2541 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2542 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002543 }
2544
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002545 xNew = rParent.left + ((wParent - wChild) / 2);
2546 if (xNew < rScreen.left)
2547 xNew = rScreen.left;
2548 else if ((xNew + wChild) > rScreen.right)
2549 xNew = rScreen.right - wChild;
2550
2551 yNew = rParent.top + ((hParent - hChild) / 2);
2552 if (yNew < rScreen.top)
2553 yNew = rScreen.top;
2554 else if ((yNew + hChild) > rScreen.bottom)
2555 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002556
2557 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2558 SWP_NOSIZE | SWP_NOZORDER);
2559}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002560#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002562#if defined(FEAT_TOOLBAR) || defined(PROTO)
2563 void
2564gui_mch_show_toolbar(int showit)
2565{
2566 if (s_toolbarhwnd == NULL)
2567 return;
2568
2569 if (showit)
2570 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002571 // Enable unicode support
2572 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2573 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002574 ShowWindow(s_toolbarhwnd, SW_SHOW);
2575 }
2576 else
2577 ShowWindow(s_toolbarhwnd, SW_HIDE);
2578}
2579
Bram Moolenaar734a8672019-12-02 22:49:38 +01002580// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002581# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582
2583#endif
2584
2585#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2586 static void
2587add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2588{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002589 WCHAR *wn;
2590 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002594 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002595
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002596 infow.cbSize = sizeof(infow);
2597 infow.fMask = MIIM_TYPE | MIIM_ID;
2598 infow.wID = item_id;
2599 infow.fType = MFT_STRING;
2600 infow.dwTypeData = wn;
2601 infow.cch = (UINT)wcslen(wn);
2602 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2603 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002604}
2605
2606 static void
2607show_tabline_popup_menu(void)
2608{
2609 HMENU tab_pmenu;
2610 long rval;
2611 POINT pt;
2612
Bram Moolenaar734a8672019-12-02 22:49:38 +01002613 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614 if (hold_gui_events
2615# ifdef FEAT_CMDWIN
2616 || cmdwin_type != 0
2617# endif
2618 )
2619 return;
2620
2621 tab_pmenu = CreatePopupMenu();
2622 if (tab_pmenu == NULL)
2623 return;
2624
2625 if (first_tabpage->tp_next != NULL)
2626 add_tabline_popup_menu_entry(tab_pmenu,
2627 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2628 add_tabline_popup_menu_entry(tab_pmenu,
2629 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2630 add_tabline_popup_menu_entry(tab_pmenu,
2631 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2632
2633 GetCursorPos(&pt);
2634 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2635 NULL);
2636
2637 DestroyMenu(tab_pmenu);
2638
Bram Moolenaar734a8672019-12-02 22:49:38 +01002639 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002640 if (rval > 0)
2641 {
2642 TCHITTESTINFO htinfo;
2643 int idx;
2644
2645 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2646 return;
2647
2648 htinfo.pt.x = pt.x;
2649 htinfo.pt.y = pt.y;
2650 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2651 if (idx == -1)
2652 idx = 0;
2653 else
2654 idx += 1;
2655
2656 send_tabline_menu_event(idx, (int)rval);
2657 }
2658}
2659
2660/*
2661 * Show or hide the tabline.
2662 */
2663 void
2664gui_mch_show_tabline(int showit)
2665{
2666 if (s_tabhwnd == NULL)
2667 return;
2668
2669 if (!showit != !showing_tabline)
2670 {
2671 if (showit)
2672 ShowWindow(s_tabhwnd, SW_SHOW);
2673 else
2674 ShowWindow(s_tabhwnd, SW_HIDE);
2675 showing_tabline = showit;
2676 }
2677}
2678
2679/*
2680 * Return TRUE when tabline is displayed.
2681 */
2682 int
2683gui_mch_showing_tabline(void)
2684{
2685 return s_tabhwnd != NULL && showing_tabline;
2686}
2687
2688/*
2689 * Update the labels of the tabline.
2690 */
2691 void
2692gui_mch_update_tabline(void)
2693{
2694 tabpage_T *tp;
2695 TCITEM tie;
2696 int nr = 0;
2697 int curtabidx = 0;
2698 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002700
2701 if (s_tabhwnd == NULL)
2702 return;
2703
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002704 // Enable unicode support
2705 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002706
2707 tie.mask = TCIF_TEXT;
2708 tie.iImage = -1;
2709
Bram Moolenaar734a8672019-12-02 22:49:38 +01002710 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002711 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2712
Bram Moolenaar734a8672019-12-02 22:49:38 +01002713 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002714 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2715 {
2716 if (tp == curtab)
2717 curtabidx = nr;
2718
2719 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2720 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002721 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722 tie.pszText = "-Empty-";
2723 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2724 tabadded = 1;
2725 }
2726
2727 get_tabline_label(tp, FALSE);
2728 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002729
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002730 wstr = enc_to_utf16(NameBuff, NULL);
2731 if (wstr != NULL)
2732 {
2733 TCITEMW tiw;
2734
2735 tiw.mask = TCIF_TEXT;
2736 tiw.iImage = -1;
2737 tiw.pszText = wstr;
2738 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2739 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002741 }
2742
Bram Moolenaar734a8672019-12-02 22:49:38 +01002743 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2745 TabCtrl_DeleteItem(s_tabhwnd, nr);
2746
2747 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2748 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2749
Bram Moolenaar734a8672019-12-02 22:49:38 +01002750 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2752 RedrawWindow(s_tabhwnd, NULL, NULL,
2753 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2754
2755 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2756 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2757}
2758
2759/*
2760 * Set the current tab to "nr". First tab is 1.
2761 */
2762 void
2763gui_mch_set_curtab(int nr)
2764{
2765 if (s_tabhwnd == NULL)
2766 return;
2767
2768 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2769 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2770}
2771
2772#endif
2773
2774/*
2775 * ":simalt" command.
2776 */
2777 void
2778ex_simalt(exarg_T *eap)
2779{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002780 char_u *keys = eap->arg;
2781 int fill_typebuf = FALSE;
2782 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783
2784 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2785 while (*keys)
2786 {
2787 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002788 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002789 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2790 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002791 fill_typebuf = TRUE;
2792 }
2793 if (fill_typebuf)
2794 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002795 // Put a NOP in the typeahead buffer so that the message will get
2796 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002797 key_name[0] = K_SPECIAL;
2798 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002799 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002800 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002801#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002802 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002803#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002804 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 }
2806}
2807
2808/*
2809 * Create the find & replace dialogs.
2810 * You can't have both at once: ":find" when replace is showing, destroys
2811 * the replace dialog first, and the other way around.
2812 */
2813#ifdef MSWIN_FIND_REPLACE
2814 static void
2815initialise_findrep(char_u *initial_string)
2816{
2817 int wword = FALSE;
2818 int mcase = !p_ic;
2819 char_u *entry_text;
2820
Bram Moolenaar734a8672019-12-02 22:49:38 +01002821 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002822 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2823
2824 s_findrep_struct.hwndOwner = s_hwnd;
2825 s_findrep_struct.Flags = FR_DOWN;
2826 if (mcase)
2827 s_findrep_struct.Flags |= FR_MATCHCASE;
2828 if (wword)
2829 s_findrep_struct.Flags |= FR_WHOLEWORD;
2830 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002831 {
2832 WCHAR *p = enc_to_utf16(entry_text, NULL);
2833 if (p != NULL)
2834 {
2835 int len = s_findrep_struct.wFindWhatLen - 1;
2836
2837 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2838 s_findrep_struct.lpstrFindWhat[len] = NUL;
2839 vim_free(p);
2840 }
2841 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842 vim_free(entry_text);
2843}
2844#endif
2845
2846 static void
2847set_window_title(HWND hwnd, char *title)
2848{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002849 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850 {
2851 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852
Bram Moolenaar734a8672019-12-02 22:49:38 +01002853 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002854 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2855 if (wbuf != NULL)
2856 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002857 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859 }
2860 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002861 else
2862 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002863}
2864
2865 void
2866gui_mch_find_dialog(exarg_T *eap)
2867{
2868#ifdef MSWIN_FIND_REPLACE
2869 if (s_findrep_msg != 0)
2870 {
2871 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2872 DestroyWindow(s_findrep_hwnd);
2873
2874 if (!IsWindow(s_findrep_hwnd))
2875 {
2876 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002877 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002878 }
2879
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002880 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881 (void)SetFocus(s_findrep_hwnd);
2882
2883 s_findrep_is_find = TRUE;
2884 }
2885#endif
2886}
2887
2888
2889 void
2890gui_mch_replace_dialog(exarg_T *eap)
2891{
2892#ifdef MSWIN_FIND_REPLACE
2893 if (s_findrep_msg != 0)
2894 {
2895 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2896 DestroyWindow(s_findrep_hwnd);
2897
2898 if (!IsWindow(s_findrep_hwnd))
2899 {
2900 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002901 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902 }
2903
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002904 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905 (void)SetFocus(s_findrep_hwnd);
2906
2907 s_findrep_is_find = FALSE;
2908 }
2909#endif
2910}
2911
2912
2913/*
2914 * Set visibility of the pointer.
2915 */
2916 void
2917gui_mch_mousehide(int hide)
2918{
2919 if (hide != gui.pointer_hidden)
2920 {
2921 ShowCursor(!hide);
2922 gui.pointer_hidden = hide;
2923 }
2924}
2925
2926#ifdef FEAT_MENU
2927 static void
2928gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2929{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002930 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931 gui_mch_mousehide(FALSE);
2932
2933 (void)TrackPopupMenu(
2934 (HMENU)menu->submenu_id,
2935 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2936 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002937 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 s_hwnd,
2939 NULL);
2940 /*
2941 * NOTE: The pop-up menu can eat the mouse up event.
2942 * We deal with this in normal.c.
2943 */
2944}
2945#endif
2946
2947/*
2948 * Got a message when the system will go down.
2949 */
2950 static void
2951_OnEndSession(void)
2952{
2953 getout_preserve_modified(1);
2954}
2955
2956/*
2957 * Get this message when the user clicks on the cross in the top right corner
2958 * of a Windows95 window.
2959 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002960 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002961_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002962{
2963 gui_shell_closed();
2964}
2965
2966/*
2967 * Get a message when the window is being destroyed.
2968 */
2969 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002970_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002971{
2972 if (!destroying)
2973 _OnClose(hwnd);
2974}
2975
2976 static void
2977_OnPaint(
2978 HWND hwnd)
2979{
2980 if (!IsMinimized(hwnd))
2981 {
2982 PAINTSTRUCT ps;
2983
Bram Moolenaar734a8672019-12-02 22:49:38 +01002984 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002986
Bram Moolenaar734a8672019-12-02 22:49:38 +01002987 // prevent multi-byte characters from misprinting on an invalid
2988 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002989 if (has_mbyte)
2990 {
2991 RECT rect;
2992
2993 GetClientRect(hwnd, &rect);
2994 ps.rcPaint.left = rect.left;
2995 ps.rcPaint.right = rect.right;
2996 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002997
2998 if (!IsRectEmpty(&ps.rcPaint))
2999 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003000 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3001 ps.rcPaint.right - ps.rcPaint.left + 1,
3002 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3003 }
3004
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003005 EndPaint(hwnd, &ps);
3006 }
3007}
3008
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003009 static void
3010_OnSize(
3011 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003012 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003013 int cx,
3014 int cy)
3015{
K.Takatac81e9bf2022-01-16 14:15:49 +00003016 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003017 {
3018 gui_resize_shell(cx, cy);
3019
Bram Moolenaar734a8672019-12-02 22:49:38 +01003020 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003022 }
3023}
3024
3025 static void
3026_OnSetFocus(
3027 HWND hwnd,
3028 HWND hwndOldFocus)
3029{
3030 gui_focus_change(TRUE);
3031 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003032 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003033}
3034
3035 static void
3036_OnKillFocus(
3037 HWND hwnd,
3038 HWND hwndNewFocus)
3039{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003040 if (destroying)
3041 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003042 gui_focus_change(FALSE);
3043 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003044 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003045}
3046
3047/*
3048 * Get a message when the user switches back to vim
3049 */
3050 static LRESULT
3051_OnActivateApp(
3052 HWND hwnd,
3053 BOOL fActivate,
3054 DWORD dwThreadId)
3055{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003056 // we call gui_focus_change() in _OnSetFocus()
3057 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003058 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059}
3060
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003061 void
3062gui_mch_destroy_scrollbar(scrollbar_T *sb)
3063{
3064 DestroyWindow(sb->id);
3065}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003066
3067/*
3068 * Get current mouse coordinates in text window.
3069 */
3070 void
3071gui_mch_getmouse(int *x, int *y)
3072{
3073 RECT rct;
3074 POINT mp;
3075
3076 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003077 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003078 *x = (int)(mp.x - rct.left);
3079 *y = (int)(mp.y - rct.top);
3080}
3081
3082/*
3083 * Move mouse pointer to character at (x, y).
3084 */
3085 void
3086gui_mch_setmouse(int x, int y)
3087{
3088 RECT rct;
3089
3090 (void)GetWindowRect(s_textArea, &rct);
3091 (void)SetCursorPos(x + gui.border_offset + rct.left,
3092 y + gui.border_offset + rct.top);
3093}
3094
3095 static void
3096gui_mswin_get_valid_dimensions(
3097 int w,
3098 int h,
3099 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003100 int *valid_h,
3101 int *cols,
3102 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003103{
3104 int base_width, base_height;
3105
3106 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003107 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3108 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003109 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003110 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3111 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3112 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3113 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003114 *cols = (w - base_width) / gui.char_width;
3115 *rows = (h - base_height) / gui.char_height;
3116 *valid_w = base_width + *cols * gui.char_width;
3117 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003118}
3119
3120 void
3121gui_mch_flash(int msec)
3122{
3123 RECT rc;
3124
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003125#if defined(FEAT_DIRECTX)
3126 if (IS_ENABLE_DIRECTX())
3127 DWriteContext_Flush(s_dwc);
3128#endif
3129
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 /*
3131 * Note: InvertRect() excludes right and bottom of rectangle.
3132 */
3133 rc.left = 0;
3134 rc.top = 0;
3135 rc.right = gui.num_cols * gui.char_width;
3136 rc.bottom = gui.num_rows * gui.char_height;
3137 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003138 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003139
Bram Moolenaar734a8672019-12-02 22:49:38 +01003140 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003141
3142 InvertRect(s_hdc, &rc);
3143}
3144
3145/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003146 * Check if the specified point is on-screen. (multi-monitor aware)
3147 */
3148 static BOOL
3149is_point_onscreen(int x, int y)
3150{
3151 POINT pt = {x, y};
3152
3153 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3154}
3155
3156/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003157 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003158 *
3159 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3160 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3161 * only works when the whole window is on-screen.
3162 */
3163 static BOOL
3164is_window_onscreen(HWND hwnd)
3165{
3166 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003167 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003168
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003169 GetClientRect(hwnd, &rc);
3170 p1.x = rc.left;
3171 p1.y = rc.top;
3172 p2.x = rc.right - 1;
3173 p2.y = rc.bottom - 1;
3174 ClientToScreen(hwnd, &p1);
3175 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003176
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003177 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003178 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003179 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003180 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003181 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003182 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003183 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003184 return FALSE;
3185 return TRUE;
3186}
3187
3188/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189 * Return flags used for scrolling.
3190 * The SW_INVALIDATE is required when part of the window is covered or
3191 * off-screen. Refer to MS KB Q75236.
3192 */
3193 static int
3194get_scroll_flags(void)
3195{
3196 HWND hwnd;
3197 RECT rcVim, rcOther, rcDest;
3198
Bram Moolenaar185577e2020-10-29 20:08:21 +01003199 // Check if the window is (partly) off-screen.
3200 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201 return SW_INVALIDATE;
3202
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003203 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003204 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3206 if (IsWindowVisible(hwnd))
3207 {
3208 GetWindowRect(hwnd, &rcOther);
3209 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3210 return SW_INVALIDATE;
3211 }
3212 return 0;
3213}
3214
3215/*
3216 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3217 * may not be scrolled out properly.
3218 * For gVim, when _OnScroll() is repeated, the character at the
3219 * previous cursor position may be left drawn after scroll.
3220 * The problem can be avoided by calling GetPixel() to get a pixel in
3221 * the region before ScrollWindowEx().
3222 */
3223 static void
3224intel_gpu_workaround(void)
3225{
3226 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3227}
3228
3229/*
3230 * Delete the given number of lines from the given row, scrolling up any
3231 * text further down within the scroll region.
3232 */
3233 void
3234gui_mch_delete_lines(
3235 int row,
3236 int num_lines)
3237{
3238 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003239
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 rc.left = FILL_X(gui.scroll_region_left);
3241 rc.right = FILL_X(gui.scroll_region_right + 1);
3242 rc.top = FILL_Y(row);
3243 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3244
Bram Moolenaar92467d32017-12-05 13:22:16 +01003245#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003246 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003247 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003248 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003249 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003250 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003251#endif
3252 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003253#if defined(FEAT_DIRECTX)
3254 if (IS_ENABLE_DIRECTX())
3255 DWriteContext_Flush(s_dwc);
3256#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003257 intel_gpu_workaround();
3258 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003259 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003260 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003261 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
Bram Moolenaar734a8672019-12-02 22:49:38 +01003263 // This seems to be required to avoid the cursor disappearing when
3264 // scrolling such that the cursor ends up in the top-left character on
3265 // the screen... But why? (Webb)
3266 // It's probably fixed by disabling drawing the cursor while scrolling.
3267 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268
3269 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3270 gui.scroll_region_left,
3271 gui.scroll_region_bot, gui.scroll_region_right);
3272}
3273
3274/*
3275 * Insert the given number of lines before the given row, scrolling down any
3276 * following text within the scroll region.
3277 */
3278 void
3279gui_mch_insert_lines(
3280 int row,
3281 int num_lines)
3282{
3283 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003284
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003285 rc.left = FILL_X(gui.scroll_region_left);
3286 rc.right = FILL_X(gui.scroll_region_right + 1);
3287 rc.top = FILL_Y(row);
3288 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003289
3290#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003291 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003292 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003293 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003294 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003295 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003296#endif
3297 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003298#if defined(FEAT_DIRECTX)
3299 if (IS_ENABLE_DIRECTX())
3300 DWriteContext_Flush(s_dwc);
3301#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003302 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003303 // The SW_INVALIDATE is required when part of the window is covered or
3304 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003305 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003306 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003307 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003308 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310 gui_clear_block(row, gui.scroll_region_left,
3311 row + num_lines - 1, gui.scroll_region_right);
3312}
3313
3314
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003315 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003316gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317{
3318#if defined(FEAT_DIRECTX)
3319 DWriteContext_Close(s_dwc);
3320 DWrite_Final();
3321 s_dwc = NULL;
3322#endif
3323
3324 ReleaseDC(s_textArea, s_hdc);
3325 DeleteObject(s_brush);
3326
3327#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003328 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003329 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3330#endif
3331
Bram Moolenaar734a8672019-12-02 22:49:38 +01003332 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003333 if (s_hwnd != NULL)
3334 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003335 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336 DestroyWindow(s_hwnd);
3337 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338}
3339
3340 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003341logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342{
3343 char *p;
3344 char *res;
3345 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003346 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003347 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003348 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003350 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3351 if (font_name == NULL)
3352 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003354 quality_name = quality_id2name((int)lf.lfQuality);
3355
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003356 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003357 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003358 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 if (res != NULL)
3360 {
3361 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003362 // make a normal font string out of the lf thing:
3363 points = pixels_to_points(
3364 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3365 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3366 sprintf((char *)p, "%s:h%d", font_name, points);
3367 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003368 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 while (*p)
3370 {
3371 if (*p == ' ')
3372 *p = '_';
3373 ++p;
3374 }
3375 if (lf.lfItalic)
3376 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003377 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378 STRCAT(p, ":b");
3379 if (lf.lfUnderline)
3380 STRCAT(p, ":u");
3381 if (lf.lfStrikeOut)
3382 STRCAT(p, ":s");
3383 if (charset_name != NULL)
3384 {
3385 STRCAT(p, ":c");
3386 STRCAT(p, charset_name);
3387 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003388 if (quality_name != NULL)
3389 {
3390 STRCAT(p, ":q");
3391 STRCAT(p, quality_name);
3392 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393 }
3394
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003395 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396 return (char_u *)res;
3397}
3398
3399
3400#ifdef FEAT_MBYTE_IME
3401/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003402 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003403 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404 */
3405 static void
3406update_im_font(void)
3407{
K.Takatac81e9bf2022-01-16 14:15:49 +00003408 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003409
3410 if (p_guifontwide != NULL && *p_guifontwide != NUL
3411 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003412 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413 norm_logfont = lf_wide;
3414 else
3415 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003416
3417 lf = norm_logfont;
3418 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3419 // Work around when PerMonitorV2 is not enabled in the process level.
3420 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3421 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003422}
3423#endif
3424
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003425/*
3426 * Handler of gui.wide_font (p_guifontwide) changed notification.
3427 */
3428 void
3429gui_mch_wide_font_changed(void)
3430{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003431 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003433#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003434 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003435#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436
3437 gui_mch_free_font(gui.wide_ital_font);
3438 gui.wide_ital_font = NOFONT;
3439 gui_mch_free_font(gui.wide_bold_font);
3440 gui.wide_bold_font = NOFONT;
3441 gui_mch_free_font(gui.wide_boldital_font);
3442 gui.wide_boldital_font = NOFONT;
3443
3444 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003445 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446 {
3447 if (!lf.lfItalic)
3448 {
3449 lf.lfItalic = TRUE;
3450 gui.wide_ital_font = get_font_handle(&lf);
3451 lf.lfItalic = FALSE;
3452 }
3453 if (lf.lfWeight < FW_BOLD)
3454 {
3455 lf.lfWeight = FW_BOLD;
3456 gui.wide_bold_font = get_font_handle(&lf);
3457 if (!lf.lfItalic)
3458 {
3459 lf.lfItalic = TRUE;
3460 gui.wide_boldital_font = get_font_handle(&lf);
3461 }
3462 }
3463 }
3464}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465
3466/*
3467 * Initialise vim to use the font with the given name.
3468 * Return FAIL if the font could not be loaded, OK otherwise.
3469 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003471gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472{
K.Takatac81e9bf2022-01-16 14:15:49 +00003473 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474 GuiFont font = NOFONT;
3475 char_u *p;
3476
Bram Moolenaar734a8672019-12-02 22:49:38 +01003477 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003478 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003479 {
3480 lfOrig = lf;
3481 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003483 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003484 if (font == NOFONT)
3485 return FAIL;
3486
3487 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003488 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003489#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003490 norm_logfont = lf;
3491 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003492 if (!s_in_dpichanged)
3493 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003494#endif
3495 gui_mch_free_font(gui.norm_font);
3496 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003497 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498 GetFontSize(font);
3499
K.Takatac81e9bf2022-01-16 14:15:49 +00003500 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 if (p != NULL)
3502 {
3503 hl_set_font_name(p);
3504
Bram Moolenaar734a8672019-12-02 22:49:38 +01003505 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3507 {
3508 vim_free(p_guifont);
3509 p_guifont = p;
3510 }
3511 else
3512 vim_free(p);
3513 }
3514
3515 gui_mch_free_font(gui.ital_font);
3516 gui.ital_font = NOFONT;
3517 gui_mch_free_font(gui.bold_font);
3518 gui.bold_font = NOFONT;
3519 gui_mch_free_font(gui.boldital_font);
3520 gui.boldital_font = NOFONT;
3521
3522 if (!lf.lfItalic)
3523 {
3524 lf.lfItalic = TRUE;
3525 gui.ital_font = get_font_handle(&lf);
3526 lf.lfItalic = FALSE;
3527 }
3528 if (lf.lfWeight < FW_BOLD)
3529 {
3530 lf.lfWeight = FW_BOLD;
3531 gui.bold_font = get_font_handle(&lf);
3532 if (!lf.lfItalic)
3533 {
3534 lf.lfItalic = TRUE;
3535 gui.boldital_font = get_font_handle(&lf);
3536 }
3537 }
3538
3539 return OK;
3540}
3541
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542/*
3543 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003544 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003545 */
3546 int
3547gui_mch_maximized(void)
3548{
3549 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003550 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003551
3552 wp.length = sizeof(WINDOWPLACEMENT);
3553 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003554 {
3555 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003557 && wp.flags == WPF_RESTORETOMAXIMIZED))
3558 return TRUE;
3559 if (wp.showCmd == SW_SHOWMINIMIZED)
3560 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003562 // Assume the window is snapped when the sizes from two APIs differ.
3563 GetWindowRect(s_hwnd, &rc);
3564 if ((rc.right - rc.left !=
3565 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3566 || (rc.bottom - rc.top !=
3567 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3568 return TRUE;
3569 }
3570 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571}
3572
3573/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003574 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3575 * is set. Compute the new Rows and Columns. This is like resizing the
3576 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577 */
3578 void
3579gui_mch_newfont(void)
3580{
3581 RECT rect;
3582
3583 GetWindowRect(s_hwnd, &rect);
3584 if (win_socket_id == 0)
3585 {
3586 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003587 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3588 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003590 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3591 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3592 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3593 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594 }
3595 else
3596 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003597 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003598 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003599 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003600 }
3601}
3602
3603/*
3604 * Set the window title
3605 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003606 void
3607gui_mch_settitle(
3608 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003609 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610{
3611 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3612}
3613
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003614#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003615// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3616// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617static LPCSTR mshape_idcs[] =
3618{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003619 IDC_ARROW, // arrow
3620 MAKEINTRESOURCE(0), // blank
3621 IDC_IBEAM, // beam
3622 IDC_SIZENS, // updown
3623 IDC_SIZENS, // udsizing
3624 IDC_SIZEWE, // leftright
3625 IDC_SIZEWE, // lrsizing
3626 IDC_WAIT, // busy
3627 IDC_NO, // no
3628 IDC_ARROW, // crosshair
3629 IDC_ARROW, // hand1
3630 IDC_ARROW, // hand2
3631 IDC_ARROW, // pencil
3632 IDC_ARROW, // question
3633 IDC_ARROW, // right-arrow
3634 IDC_UPARROW, // up-arrow
3635 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003636};
3637
3638 void
3639mch_set_mouse_shape(int shape)
3640{
3641 LPCSTR idc;
3642
3643 if (shape == MSHAPE_HIDE)
3644 ShowCursor(FALSE);
3645 else
3646 {
3647 if (shape >= MSHAPE_NUMBERED)
3648 idc = IDC_ARROW;
3649 else
3650 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003651 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652 if (!p_mh)
3653 {
3654 POINT mp;
3655
Bram Moolenaar734a8672019-12-02 22:49:38 +01003656 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003657 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 (void)SetCursorPos(mp.x, mp.y);
3659 ShowCursor(TRUE);
3660 }
3661 }
3662}
3663#endif
3664
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003665#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003667 * Wide version of convert_filter().
3668 */
3669 static WCHAR *
3670convert_filterW(char_u *s)
3671{
3672 char_u *tmp;
3673 int len;
3674 WCHAR *res;
3675
3676 tmp = convert_filter(s);
3677 if (tmp == NULL)
3678 return NULL;
3679 len = (int)STRLEN(s) + 3;
3680 res = enc_to_utf16(tmp, &len);
3681 vim_free(tmp);
3682 return res;
3683}
3684
3685/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003686 * Pop open a file browser and return the file selected, in allocated memory,
3687 * or NULL if Cancel is hit.
3688 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3689 * title - Title message for the file browser dialog.
3690 * dflt - Default name of file.
3691 * ext - Default extension to be added to files without extensions.
3692 * initdir - directory in which to open the browser (NULL = current dir)
3693 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003694 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003695 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003696gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003697 int saving,
3698 char_u *title,
3699 char_u *dflt,
3700 char_u *ext,
3701 char_u *initdir,
3702 char_u *filter)
3703{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003704 // We always use the wide function. This means enc_to_utf16() must work,
3705 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706 OPENFILENAMEW fileStruct;
3707 WCHAR fileBuf[MAXPATHL];
3708 WCHAR *wp;
3709 int i;
3710 WCHAR *titlep = NULL;
3711 WCHAR *extp = NULL;
3712 WCHAR *initdirp = NULL;
3713 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003714 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003715 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716
3717 if (dflt == NULL)
3718 fileBuf[0] = NUL;
3719 else
3720 {
3721 wp = enc_to_utf16(dflt, NULL);
3722 if (wp == NULL)
3723 fileBuf[0] = NUL;
3724 else
3725 {
3726 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3727 fileBuf[i] = wp[i];
3728 fileBuf[i] = NUL;
3729 vim_free(wp);
3730 }
3731 }
3732
Bram Moolenaar734a8672019-12-02 22:49:38 +01003733 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003734 filterp = convert_filterW(filter);
3735
Bram Moolenaara80faa82020-04-12 19:37:17 +02003736 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003737# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003738 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003739 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003740# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003741 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003742# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003743
3744 if (title != NULL)
3745 titlep = enc_to_utf16(title, NULL);
3746 fileStruct.lpstrTitle = titlep;
3747
3748 if (ext != NULL)
3749 extp = enc_to_utf16(ext, NULL);
3750 fileStruct.lpstrDefExt = extp;
3751
3752 fileStruct.lpstrFile = fileBuf;
3753 fileStruct.nMaxFile = MAXPATHL;
3754 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003755 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3756 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 if (initdir != NULL && *initdir != NUL)
3758 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003759 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 initdirp = enc_to_utf16(initdir, NULL);
3761 if (initdirp != NULL)
3762 {
3763 for (wp = initdirp; *wp != NUL; ++wp)
3764 if (*wp == '/')
3765 *wp = '\\';
3766 }
3767 fileStruct.lpstrInitialDir = initdirp;
3768 }
3769
3770 /*
3771 * TODO: Allow selection of multiple files. Needs another arg to this
3772 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3773 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3774 * files that don't exist yet, so I haven't put it in. What about
3775 * OFN_PATHMUSTEXIST?
3776 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3777 */
3778 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003779# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 if (curbuf->b_p_bin)
3781 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003782# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003784 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003786 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003787
3788 vim_free(filterp);
3789 vim_free(initdirp);
3790 vim_free(titlep);
3791 vim_free(extp);
3792
K.Takata14b8d6a2022-01-20 15:05:22 +00003793 if (!ret)
3794 return NULL;
3795
3796 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003798 if (p == NULL)
3799 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003800
Bram Moolenaar734a8672019-12-02 22:49:38 +01003801 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 SetFocus(s_hwnd);
3803
Bram Moolenaar734a8672019-12-02 22:49:38 +01003804 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003805 q = vim_strsave(shorten_fname1(p));
3806 vim_free(p);
3807 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809
3810
3811/*
3812 * Convert the string s to the proper format for a filter string by replacing
3813 * the \t and \n delimiters with \0.
3814 * Returns the converted string in allocated memory.
3815 *
3816 * Keep in sync with convert_filterW() above!
3817 */
3818 static char_u *
3819convert_filter(char_u *s)
3820{
3821 char_u *res;
3822 unsigned s_len = (unsigned)STRLEN(s);
3823 unsigned i;
3824
3825 res = alloc(s_len + 3);
3826 if (res != NULL)
3827 {
3828 for (i = 0; i < s_len; ++i)
3829 if (s[i] == '\t' || s[i] == '\n')
3830 res[i] = '\0';
3831 else
3832 res[i] = s[i];
3833 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003834 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 res[s_len + 1] = NUL;
3836 res[s_len + 2] = NUL;
3837 }
3838 return res;
3839}
3840
3841/*
3842 * Select a directory.
3843 */
3844 char_u *
3845gui_mch_browsedir(char_u *title, char_u *initdir)
3846{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003847 // We fake this: Use a filter that doesn't select anything and a default
3848 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003849 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3850 initdir, (char_u *)_("Directory\t*.nothing\n"));
3851}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003852#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003854 static void
3855_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003856 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003857 HDROP hDrop)
3858{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003859#define BUFPATHLEN _MAX_PATH
3860#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 char szFile[BUFPATHLEN];
3863 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3864 UINT i;
3865 char_u **fnames;
3866 POINT pt;
3867 int_u modifiers = 0;
3868
Bram Moolenaar734a8672019-12-02 22:49:38 +01003869 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870 DragQueryPoint(hDrop, &pt);
3871 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3872
3873 reset_VIsual();
3874
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003875 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003876
3877 if (fnames != NULL)
3878 for (i = 0; i < cFiles; ++i)
3879 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003880 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3881 fnames[i] = utf16_to_enc(wszFile, NULL);
3882 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003883 {
3884 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3885 fnames[i] = vim_strsave((char_u *)szFile);
3886 }
3887 }
3888
3889 DragFinish(hDrop);
3890
3891 if (fnames != NULL)
3892 {
LemonBoy45684c62022-04-24 15:46:42 +01003893 int kbd_modifiers = get_active_modifiers();
3894
3895 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003896 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003897 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003899 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003900 modifiers |= MOUSE_ALT;
3901
3902 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3903
3904 s_need_activate = TRUE;
3905 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906}
3907
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 static int
3909_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003910 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003911 HWND hwndCtl,
3912 UINT code,
3913 int pos)
3914{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003915 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003916 scrollbar_T *sb, *sb_info;
3917 long val;
3918 int dragging = FALSE;
3919 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003920 SCROLLINFO si;
3921
3922 si.cbSize = sizeof(si);
3923 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924
3925 sb = gui_mswin_find_scrollbar(hwndCtl);
3926 if (sb == NULL)
3927 return 0;
3928
Bram Moolenaar734a8672019-12-02 22:49:38 +01003929 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003930 {
3931 /*
3932 * Careful: need to get scrollbar info out of first (left) scrollbar
3933 * for window, but keep real scrollbar too because we must pass it to
3934 * gui_drag_scrollbar().
3935 */
3936 sb_info = &sb->wp->w_scrollbars[0];
3937 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003938 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003939 sb_info = sb;
3940 val = sb_info->value;
3941
3942 switch (code)
3943 {
3944 case SB_THUMBTRACK:
3945 val = pos;
3946 dragging = TRUE;
3947 if (sb->scroll_shift > 0)
3948 val <<= sb->scroll_shift;
3949 break;
3950 case SB_LINEDOWN:
3951 val++;
3952 break;
3953 case SB_LINEUP:
3954 val--;
3955 break;
3956 case SB_PAGEDOWN:
3957 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3958 break;
3959 case SB_PAGEUP:
3960 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3961 break;
3962 case SB_TOP:
3963 val = 0;
3964 break;
3965 case SB_BOTTOM:
3966 val = sb_info->max;
3967 break;
3968 case SB_ENDSCROLL:
3969 if (prev_code == SB_THUMBTRACK)
3970 {
3971 /*
3972 * "pos" only gives us 16-bit data. In case of large file,
3973 * use GetScrollPos() which returns 32-bit. Unfortunately it
3974 * is not valid while the scrollbar is being dragged.
3975 */
3976 val = GetScrollPos(hwndCtl, SB_CTL);
3977 if (sb->scroll_shift > 0)
3978 val <<= sb->scroll_shift;
3979 }
3980 break;
3981
3982 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003983 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003984 return 0;
3985 }
3986 prev_code = code;
3987
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003988 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3989 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003990
3991 /*
3992 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3993 */
3994 if (sb->wp != NULL)
3995 {
3996 scrollbar_T *sba = sb->wp->w_scrollbars;
3997 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3998
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003999 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004000 }
4001
Bram Moolenaar734a8672019-12-02 22:49:38 +01004002 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004003 s_busy_processing = TRUE;
4004
Bram Moolenaar734a8672019-12-02 22:49:38 +01004005 // When "allow_scrollbar" is FALSE still need to remember the new
4006 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007 dont_scroll = !allow_scrollbar;
4008
Bram Moolenaara338adc2018-01-31 20:51:47 +01004009 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004010 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004011 mch_enable_flush();
4012 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013
4014 s_busy_processing = FALSE;
4015 dont_scroll = dont_scroll_save;
4016
4017 return 0;
4018}
4019
4020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#ifdef FEAT_XPM_W32
4022# include "xpm_w32.h"
4023#endif
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Bram Moolenaar734a8672019-12-02 22:49:38 +01004026// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#define TEAROFF_PADDING_X 2
4028#define TEAROFF_BUTTON_PAD_X 8
4029#define TEAROFF_MIN_WIDTH 200
4030#define TEAROFF_SUBMENU_LABEL ">>"
4031#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4032
4033
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004034#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035# define ID_BEVAL_TOOLTIP 200
4036# define BEVAL_TEXT_LEN MAXPATHL
4037
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004039static UINT_PTR beval_timer_id = 0;
4040static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004041#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004042
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004043
Bram Moolenaar734a8672019-12-02 22:49:38 +01004044// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046#ifdef FEAT_MENU
4047static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050/*
4051 * Use the system font for dialogs and tear-off menus. Remove this line to
4052 * use DLG_FONT_NAME.
4053 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004054#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#define VIM_CLASSW L"Vim"
4058
Bram Moolenaar734a8672019-12-02 22:49:38 +01004059// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4060// thus there should be room for every dialog. For tearoffs it's made bigger
4061// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#define DLG_ALLOC_SIZE 16 * 1024
4063
4064/*
4065 * stuff for dialogs, menus, tearoffs etc.
4066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067static PWORD
4068add_dialog_element(
4069 PWORD p,
4070 DWORD lStyle,
4071 WORD x,
4072 WORD y,
4073 WORD w,
4074 WORD h,
4075 WORD Id,
4076 WORD clss,
4077 const char *caption);
4078static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004079static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004080#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083static void get_dialog_font_metrics(void);
4084
4085static int dialog_default_button = -1;
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_TOOLBAR
4088static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004089static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004090static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004092#else
4093# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#endif
4095
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004096#ifdef FEAT_GUI_TABLINE
4097static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004098static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004099#endif
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101#ifdef FEAT_MBYTE_IME
4102static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4103static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4104#endif
4105#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4106# ifdef NOIME
4107typedef struct tagCOMPOSITIONFORM {
4108 DWORD dwStyle;
4109 POINT ptCurrentPos;
4110 RECT rcArea;
4111} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4112typedef HANDLE HIMC;
4113# endif
4114
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004115static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004116static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4117static HIMC (WINAPI *pImmGetContext)(HWND);
4118static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4119static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4120static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4121static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004122static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4123static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004124static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4125static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004126static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127static void dyn_imm_load(void);
4128#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129# define pImmGetCompositionStringW ImmGetCompositionStringW
4130# define pImmGetContext ImmGetContext
4131# define pImmAssociateContext ImmAssociateContext
4132# define pImmReleaseContext ImmReleaseContext
4133# define pImmGetOpenStatus ImmGetOpenStatus
4134# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004135# define pImmGetCompositionFontW ImmGetCompositionFontW
4136# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137# define pImmSetCompositionWindow ImmSetCompositionWindow
4138# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004139# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#endif
4141
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#ifdef FEAT_MENU
4143/*
4144 * Figure out how high the menu bar is at the moment.
4145 */
4146 static int
4147gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004148 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
4150 static int old_menu_height = -1;
4151
4152 RECT rc1, rc2;
4153 int num;
4154 int menu_height;
4155
4156 if (gui.menu_is_active)
4157 num = GetMenuItemCount(s_menuBar);
4158 else
4159 num = 0;
4160
4161 if (num == 0)
4162 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004163 else if (IsMinimized(s_hwnd))
4164 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004165 // The height of the menu cannot be determined while the window is
4166 // minimized. Take the previous height if the menu is changed in that
4167 // state, to avoid that Vim's vertical window size accidentally
4168 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004169 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
4172 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004173 /*
4174 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4175 * seem to have been set yet, so menu wraps in default window
4176 * width which is very narrow. Instead just return height of a
4177 * single menu item. Will still be wrong when the menu really
4178 * should wrap over more than one line.
4179 */
4180 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4181 if (gui.starting)
4182 menu_height = rc1.bottom - rc1.top + 1;
4183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004185 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4186 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 }
4189
4190 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004191 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004192 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
4194 return menu_height;
4195}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004196#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198
4199/*
4200 * Setup for the Intellimouse
4201 */
LemonBoyc27747e2022-05-07 12:25:40 +01004202 static long
4203mouse_vertical_scroll_step(void)
4204{
4205 UINT val;
4206 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4207 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4208 return 3; // Safe default;
4209}
4210
4211 static long
4212mouse_horizontal_scroll_step(void)
4213{
4214 UINT val;
4215 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4216 return (long)val;
4217 return 3; // Safe default;
4218}
4219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 static void
4221init_mouse_wheel(void)
4222{
LemonBoyc27747e2022-05-07 12:25:40 +01004223 // Get the default values for the horizontal and vertical scroll steps from
4224 // the system.
4225 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4226 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228
Bram Moolenaarae20f342019-11-05 21:09:23 +01004229/*
LemonBoya773d842022-05-10 20:54:46 +01004230 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004233_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
LemonBoy365d8f72022-05-05 19:23:07 +01004235 int button;
4236 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004237 int modifiers = 0;
4238 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004239 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4240 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaarae20f342019-11-05 21:09:23 +01004242 wp = gui_mouse_window(FIND_POPUP);
4243
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004244#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004245 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004246 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004247 cmdarg_T cap;
4248 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004249
Bram Moolenaarae20f342019-11-05 21:09:23 +01004250 // Mouse hovers over popup window, scroll it if possible.
4251 mouse_row = wp->w_winrow;
4252 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004253 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004254 if (horizontal)
4255 {
4256 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4257 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4258 }
4259 else
4260 {
4261 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4262 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4263 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004264 clear_oparg(&oa);
4265 cap.oap = &oa;
4266 nv_mousescroll(&cap);
4267 update_screen(0);
4268 setcursor();
4269 out_flush();
4270 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004271 }
4272#endif
4273
Bram Moolenaarae20f342019-11-05 21:09:23 +01004274 if (wp == NULL || !p_scf)
4275 wp = curwin;
4276
LemonBoy365d8f72022-05-05 19:23:07 +01004277 // Translate the scroll event into an event that Vim can process so that
4278 // the user has a chance to map the scrollwheel buttons.
4279 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004280 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 else
LemonBoy365d8f72022-05-05 19:23:07 +01004282 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004283
4284 kbd_modifiers = get_active_modifiers();
4285
4286 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4287 modifiers |= MOUSE_SHIFT;
4288 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4289 modifiers |= MOUSE_CTRL;
4290 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4291 modifiers |= MOUSE_ALT;
4292
LemonBoya773d842022-05-10 20:54:46 +01004293 // The cursor position is relative to the upper-left corner of the screen.
4294 pt.x = GET_X_LPARAM(lParam);
4295 pt.y = GET_Y_LPARAM(lParam);
4296 ScreenToClient(s_textArea, &pt);
4297
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004298 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299}
4300
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004301#ifdef USE_SYSMENU_FONT
4302/*
4303 * Get Menu Font.
4304 * Return OK or FAIL.
4305 */
4306 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004307gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004308{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004309 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004310
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004311 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4312 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004313 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004314 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004315 &nm,
4316 0))
4317 return FAIL;
4318 *lf = nm.lfMenuFont;
4319 return OK;
4320}
4321#endif
4322
4323
4324#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4325/*
4326 * Set the GUI tabline font to the system menu font
4327 */
4328 static void
4329set_tabline_font(void)
4330{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004331 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004332 HFONT font;
4333 HWND hwnd;
4334 HDC hdc;
4335 HFONT hfntOld;
4336 TEXTMETRIC tm;
4337
4338 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4339 return;
4340
K.Takatac81e9bf2022-01-16 14:15:49 +00004341 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004342 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004343
4344 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4345
4346 /*
4347 * Compute the height of the font used for the tab text
4348 */
4349 hwnd = GetDesktopWindow();
4350 hdc = GetWindowDC(hwnd);
4351 hfntOld = SelectFont(hdc, font);
4352
4353 GetTextMetrics(hdc, &tm);
4354
4355 SelectFont(hdc, hfntOld);
4356 ReleaseDC(hwnd, hdc);
4357
4358 /*
4359 * The space used by the tab border and the space between the tab label
4360 * and the tab border is included as 7.
4361 */
4362 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4363}
K.Takatac81e9bf2022-01-16 14:15:49 +00004364#else
4365# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004366#endif
4367
Bram Moolenaar520470a2005-06-16 21:59:56 +00004368/*
4369 * Invoked when a setting was changed.
4370 */
4371 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004372_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004373{
LemonBoy365d8f72022-05-05 19:23:07 +01004374 switch (param)
4375 {
4376 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004377 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004378 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004379 case SPI_SETWHEELSCROLLCHARS:
4380 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004381 break;
4382 case SPI_SETNONCLIENTMETRICS:
4383 set_tabline_font();
4384 break;
4385 default:
4386 break;
4387 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004388 return 0;
4389}
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#ifdef FEAT_NETBEANS_INTG
4392 static void
4393_OnWindowPosChanged(
4394 HWND hwnd,
4395 const LPWINDOWPOS lpwpos)
4396{
4397 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004398 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4401 || lpwpos->cx != cx || lpwpos->cy != cy))
4402 {
4403 x = lpwpos->x;
4404 y = lpwpos->y;
4405 cx = lpwpos->cx;
4406 cy = lpwpos->cy;
4407 netbeans_frame_moved(x, y);
4408 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004409 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004410 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411}
4412#endif
4413
K.Takata4f2417f2021-06-05 16:25:32 +02004414
4415static HWND hwndTip = NULL;
4416
4417 static void
4418show_sizing_tip(int cols, int rows)
4419{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004420 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004421 char buf[32];
4422
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004423 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004424 ti.hwnd = s_hwnd;
4425 ti.uId = (UINT_PTR)s_hwnd;
4426 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4427 ti.lpszText = buf;
4428 sprintf(buf, "%dx%d", cols, rows);
4429 if (hwndTip == NULL)
4430 {
4431 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4432 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4433 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4434 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4435 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4436 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4437 }
4438 else
4439 {
4440 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4441 }
4442 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4443}
4444
4445 static void
4446destroy_sizing_tip(void)
4447{
4448 if (hwndTip != NULL)
4449 {
4450 DestroyWindow(hwndTip);
4451 hwndTip = NULL;
4452 }
4453}
4454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 static int
4456_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 UINT fwSide,
4458 LPRECT lprc)
4459{
4460 int w, h;
4461 int valid_w, valid_h;
4462 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004463 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 w = lprc->right - lprc->left;
4466 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004467 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 w_offset = w - valid_w;
4469 h_offset = h - valid_h;
4470
4471 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4472 || fwSide == WMSZ_BOTTOMLEFT)
4473 lprc->left += w_offset;
4474 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4475 || fwSide == WMSZ_BOTTOMRIGHT)
4476 lprc->right -= w_offset;
4477
4478 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4479 || fwSide == WMSZ_TOPRIGHT)
4480 lprc->top += h_offset;
4481 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4482 || fwSide == WMSZ_BOTTOMRIGHT)
4483 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004484
4485 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 return TRUE;
4487}
4488
K.Takata92000e22022-01-20 15:10:57 +00004489#ifdef FEAT_GUI_TABLINE
4490 static void
4491_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4492{
4493 if (gui_mch_showing_tabline())
4494 {
4495 POINT pt;
4496 RECT rect;
4497
4498 /*
4499 * If the cursor is on the tabline, display the tab menu
4500 */
4501 GetCursorPos(&pt);
4502 GetWindowRect(s_textArea, &rect);
4503 if (pt.y < rect.top)
4504 {
4505 show_tabline_popup_menu();
4506 return;
4507 }
4508 }
4509 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4510}
4511
4512 static void
4513_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4514{
4515 /*
4516 * If the user double clicked the tabline, create a new tab
4517 */
4518 if (gui_mch_showing_tabline())
4519 {
4520 POINT pt;
4521 RECT rect;
4522
4523 GetCursorPos(&pt);
4524 GetWindowRect(s_textArea, &rect);
4525 if (pt.y < rect.top)
4526 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4527 }
4528 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4529}
4530#endif
4531
4532 static UINT
4533_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4534{
4535 UINT result;
4536 int x, y;
4537
4538 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4539 if (result != HTCLIENT)
4540 return result;
4541
4542#ifdef FEAT_GUI_TABLINE
4543 if (gui_mch_showing_tabline())
4544 {
4545 RECT rct;
4546
4547 // If the cursor is on the GUI tabline, don't process this event
4548 GetWindowRect(s_textArea, &rct);
4549 if (yPos < rct.top)
4550 return result;
4551 }
4552#endif
4553 (void)gui_mch_get_winpos(&x, &y);
4554 xPos -= x;
4555
4556 if (xPos < 48) // <VN> TODO should use system metric?
4557 return HTBOTTOMLEFT;
4558 else
4559 return HTBOTTOMRIGHT;
4560}
4561
4562#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4563 static LRESULT
4564_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4565{
4566 switch (hdr->code)
4567 {
4568 case TTN_GETDISPINFOW:
4569 case TTN_GETDISPINFO:
4570 {
4571 char_u *str = NULL;
4572 static void *tt_text = NULL;
4573
4574 VIM_CLEAR(tt_text);
4575
4576# ifdef FEAT_GUI_TABLINE
4577 if (gui_mch_showing_tabline()
4578 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4579 {
4580 POINT pt;
4581 /*
4582 * Mouse is over the GUI tabline. Display the
4583 * tooltip for the tab under the cursor
4584 *
4585 * Get the cursor position within the tab control
4586 */
4587 GetCursorPos(&pt);
4588 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4589 {
4590 TCHITTESTINFO htinfo;
4591 int idx;
4592
4593 /*
4594 * Get the tab under the cursor
4595 */
4596 htinfo.pt.x = pt.x;
4597 htinfo.pt.y = pt.y;
4598 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4599 if (idx != -1)
4600 {
4601 tabpage_T *tp;
4602
4603 tp = find_tabpage(idx + 1);
4604 if (tp != NULL)
4605 {
4606 get_tabline_label(tp, TRUE);
4607 str = NameBuff;
4608 }
4609 }
4610 }
4611 }
4612# endif
4613# ifdef FEAT_TOOLBAR
4614# ifdef FEAT_GUI_TABLINE
4615 else
4616# endif
4617 {
4618 UINT idButton;
4619 vimmenu_T *pMenu;
4620
4621 idButton = (UINT) hdr->idFrom;
4622 pMenu = gui_mswin_find_menu(root_menu, idButton);
4623 if (pMenu)
4624 str = pMenu->strings[MENU_INDEX_TIP];
4625 }
4626# endif
4627 if (str == NULL)
4628 break;
4629
4630 // Set the maximum width, this also enables using \n for
4631 // line break.
4632 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4633
4634 if (hdr->code == TTN_GETDISPINFOW)
4635 {
4636 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4637
4638 tt_text = enc_to_utf16(str, NULL);
4639 lpdi->lpszText = tt_text;
4640 // can't show tooltip if failed
4641 }
4642 else
4643 {
4644 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4645
4646 if (STRLEN(str) < sizeof(lpdi->szText)
4647 || ((tt_text = vim_strsave(str)) == NULL))
4648 vim_strncpy((char_u *)lpdi->szText, str,
4649 sizeof(lpdi->szText) - 1);
4650 else
4651 lpdi->lpszText = tt_text;
4652 }
4653 }
4654 break;
4655
4656# ifdef FEAT_GUI_TABLINE
4657 case TCN_SELCHANGE:
4658 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4659 {
4660 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4661 return 0L;
4662 }
4663 break;
4664
4665 case NM_RCLICK:
4666 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4667 {
4668 show_tabline_popup_menu();
4669 return 0L;
4670 }
4671 break;
4672# endif
4673
4674 default:
4675 break;
4676 }
4677 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4678}
4679#endif
4680
4681#if defined(MENUHINTS) && defined(FEAT_MENU)
4682 static LRESULT
4683_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4684{
4685 if (((UINT) HIWORD(wParam)
4686 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4687 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004688 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004689 {
4690 UINT idButton;
4691 vimmenu_T *pMenu;
4692 static int did_menu_tip = FALSE;
4693
4694 if (did_menu_tip)
4695 {
4696 msg_clr_cmdline();
4697 setcursor();
4698 out_flush();
4699 did_menu_tip = FALSE;
4700 }
4701
4702 idButton = (UINT)LOWORD(wParam);
4703 pMenu = gui_mswin_find_menu(root_menu, idButton);
4704 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4705 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4706 {
4707 ++msg_hist_off;
4708 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4709 --msg_hist_off;
4710 setcursor();
4711 out_flush();
4712 did_menu_tip = TRUE;
4713 }
4714 return 0L;
4715 }
4716 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4717}
4718#endif
4719
K.Takatac81e9bf2022-01-16 14:15:49 +00004720 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004721_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004722{
4723 s_dpi = ydpi;
4724 s_in_dpichanged = TRUE;
4725 //TRACE("DPI: %d", ydpi);
4726
4727 update_scrollbar_size();
4728 update_toolbar_size();
4729 set_tabline_font();
4730
4731 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4732 gui_get_wide_font();
4733 gui_mswin_get_menu_height(FALSE);
4734#ifdef FEAT_MBYTE_IME
4735 im_set_position(gui.row, gui.col);
4736#endif
4737 InvalidateRect(hwnd, NULL, TRUE);
4738
4739 s_in_dpichanged = FALSE;
4740 return 0L;
4741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
4743
4744 static LRESULT CALLBACK
4745_WndProc(
4746 HWND hwnd,
4747 UINT uMsg,
4748 WPARAM wParam,
4749 LPARAM lParam)
4750{
LemonBoya773d842022-05-10 20:54:46 +01004751 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4752 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
4754 HandleMouseHide(uMsg, lParam);
4755
4756 s_uMsg = uMsg;
4757 s_wParam = wParam;
4758 s_lParam = lParam;
4759
4760 switch (uMsg)
4761 {
4762 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4763 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004764 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004766 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4768 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4769 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4770 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4771#ifdef FEAT_MENU
4772 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4773#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004774 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4775 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4777 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004778 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4779 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4781 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4782 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4783#ifdef FEAT_NETBEANS_INTG
4784 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4785#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004786#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004787 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4788 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004789#endif
K.Takata92000e22022-01-20 15:10:57 +00004790 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004791
Bram Moolenaar734a8672019-12-02 22:49:38 +01004792 case WM_QUERYENDSESSION: // System wants to go down.
4793 gui_shell_closed(); // Will exit when no changed buffers.
4794 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004797 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004800 return 0L;
4801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 break;
4803
4804 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004805 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4806 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004807 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 return 0L;
4809
4810 case WM_SYSCHAR:
4811 /*
4812 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4813 * shortcut key, handle like a typed ALT key, otherwise call Windows
4814 * ALT key handling.
4815 */
4816#ifdef FEAT_MENU
4817 if ( !gui.menu_is_active
4818 || p_wak[0] == 'n'
4819 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4820 )
4821#endif
4822 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004823 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return 0L;
4825 }
4826#ifdef FEAT_MENU
4827 else
K.Takata4ac893f2022-01-20 12:44:28 +00004828 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829#endif
4830
4831 case WM_SYSKEYUP:
4832#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004833 // This used to be done only when menu is active: ALT key is used for
4834 // that. But that caused problems when menu is disabled and using
4835 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4836 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004837 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004839 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840#endif
4841
K.Takata4f2417f2021-06-05 16:25:32 +02004842 case WM_EXITSIZEMOVE:
4843 destroy_sizing_tip();
4844 break;
4845
Bram Moolenaar734a8672019-12-02 22:49:38 +01004846 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004847 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004850 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004851 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004852 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
Bram Moolenaar734a8672019-12-02 22:49:38 +01004854 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004855 case WM_SETTINGCHANGE:
4856 return _OnSettingChange((UINT)wParam);
4857
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004858#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004860 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#endif
K.Takata92000e22022-01-20 15:10:57 +00004862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#if defined(MENUHINTS) && defined(FEAT_MENU)
4864 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004865 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868#ifdef FEAT_MBYTE_IME
4869 case WM_IME_NOTIFY:
4870 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004871 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004872 return 1L;
4873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 case WM_IME_COMPOSITION:
4875 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004876 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004877 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004879 case WM_DPICHANGED:
4880 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4881 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882
4883 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004885 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
K.Takata92000e22022-01-20 15:10:57 +00004888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890
K.Takata4ac893f2022-01-20 12:44:28 +00004891 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892}
4893
4894/*
4895 * End of call-back routines
4896 */
4897
Bram Moolenaar734a8672019-12-02 22:49:38 +01004898// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899HWND vim_parent_hwnd = NULL;
4900
4901 static BOOL CALLBACK
4902FindWindowTitle(HWND hwnd, LPARAM lParam)
4903{
4904 char buf[2048];
4905 char *title = (char *)lParam;
4906
4907 if (GetWindowText(hwnd, buf, sizeof(buf)))
4908 {
4909 if (strstr(buf, title) != NULL)
4910 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004911 // Found it. Store the window ref. and quit searching if MDI
4912 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004914 if (vim_parent_hwnd != NULL)
4915 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004918 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919}
4920
4921/*
4922 * Invoked for '-P "title"' argument: search for parent application to open
4923 * our window in.
4924 */
4925 void
4926gui_mch_set_parent(char *title)
4927{
4928 EnumWindows(FindWindowTitle, (LPARAM)title);
4929 if (vim_parent_hwnd == NULL)
4930 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004931 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 mch_exit(2);
4933 }
4934}
4935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004936#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 static void
4938ole_error(char *arg)
4939{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004940 char buf[IOSIZE];
4941
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004942# ifdef VIMDLL
4943 gui.in_use = mch_is_gui_executable();
4944# endif
4945
Bram Moolenaar734a8672019-12-02 22:49:38 +01004946 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004947 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004948 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004949 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004953#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4954 static char *
4955gvim_error(void)
4956{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004957 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004958
4959 if (starting)
4960 {
4961 mch_errmsg(msg);
4962 mch_errmsg("\n");
4963 mch_exit(2);
4964 }
4965 return msg;
4966}
4967
4968 char *
4969gui_mch_do_spawn(char_u *arg)
4970{
4971 int len;
4972# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4973 char_u *session = NULL;
4974 LPWSTR tofree1 = NULL;
4975# endif
4976 WCHAR name[MAX_PATH];
4977 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4978 STARTUPINFOW si = {sizeof(si)};
4979 PROCESS_INFORMATION pi;
4980
4981 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4982 goto error;
4983 p = wcsrchr(name, L'\\');
4984 if (p == NULL)
4985 goto error;
4986 // Replace the executable name from vim(d).exe to gvim(d).exe.
4987# ifdef DEBUG
4988 wcscpy(p + 1, L"gvimd.exe");
4989# else
4990 wcscpy(p + 1, L"gvim.exe");
4991# endif
4992
4993# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4994 if (starting)
4995# endif
4996 {
4997 // Pass the command line to the new process.
4998 p = GetCommandLineW();
4999 // Skip 1st argument.
5000 while (*p && *p != L' ' && *p != L'\t')
5001 {
5002 if (*p == L'"')
5003 {
5004 while (*p && *p != L'"')
5005 ++p;
5006 if (*p)
5007 ++p;
5008 }
5009 else
5010 ++p;
5011 }
5012 cmd = p;
5013 }
5014# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5015 else
5016 {
5017 // Create a session file and pass it to the new process.
5018 LPWSTR wsession;
5019 char_u *savebg;
5020 int ret;
5021
5022 session = vim_tempname('s', FALSE);
5023 if (session == NULL)
5024 goto error;
5025 savebg = p_bg;
5026 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5027 ret = write_session_file(session);
5028 vim_free(p_bg);
5029 p_bg = savebg;
5030 if (!ret)
5031 goto error;
5032 wsession = enc_to_utf16(session, NULL);
5033 if (wsession == NULL)
5034 goto error;
5035 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005036 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005037 if (cmd == NULL)
5038 {
5039 vim_free(wsession);
5040 goto error;
5041 }
5042 tofree1 = cmd;
5043 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5044 wsession, wsession);
5045 vim_free(wsession);
5046 }
5047# endif
5048
5049 // Check additional arguments to the `:gui` command.
5050 if (arg != NULL)
5051 {
5052 warg = enc_to_utf16(arg, NULL);
5053 if (warg == NULL)
5054 goto error;
5055 tofree2 = warg;
5056 }
5057 else
5058 warg = L"";
5059
5060 // Set up the new command line.
5061 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005062 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005063 if (newcmd == NULL)
5064 goto error;
5065 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5066
5067 // Spawn a new GUI process.
5068 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5069 NULL, NULL, &si, &pi))
5070 goto error;
5071 CloseHandle(pi.hProcess);
5072 CloseHandle(pi.hThread);
5073 mch_exit(0);
5074
5075error:
5076# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5077 if (session)
5078 mch_remove(session);
5079 vim_free(session);
5080 vim_free(tofree1);
5081# endif
5082 vim_free(newcmd);
5083 vim_free(tofree2);
5084 return gvim_error();
5085}
5086#endif
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088/*
5089 * Parse the GUI related command-line arguments. Any arguments used are
5090 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005091 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
5093 void
5094gui_mch_prepare(int *argc, char **argv)
5095{
5096 int silent = FALSE;
5097 int idx;
5098
Bram Moolenaar734a8672019-12-02 22:49:38 +01005099 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5101 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005102 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5104 && (argv[2][0] == '-' || argv[2][0] == '/'))
5105 {
5106 silent = TRUE;
5107 idx = 2;
5108 }
5109 else
5110 idx = 1;
5111
Bram Moolenaar734a8672019-12-02 22:49:38 +01005112 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (STRICMP(argv[idx] + 1, "register") == 0)
5114 {
5115#ifdef FEAT_OLE
5116 RegisterMe(silent);
5117 mch_exit(0);
5118#else
5119 if (!silent)
5120 ole_error("register");
5121 mch_exit(2);
5122#endif
5123 }
5124
Bram Moolenaar734a8672019-12-02 22:49:38 +01005125 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5127 {
5128#ifdef FEAT_OLE
5129 UnregisterMe(!silent);
5130 mch_exit(0);
5131#else
5132 if (!silent)
5133 ole_error("unregister");
5134 mch_exit(2);
5135#endif
5136 }
5137
Bram Moolenaar734a8672019-12-02 22:49:38 +01005138 // Ignore an -embedding argument. It is only relevant if the
5139 // application wants to treat the case when it is started manually
5140 // differently from the case where it is started via automation (and
5141 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5143 {
5144#ifdef FEAT_OLE
5145 *argc = 1;
5146#else
5147 ole_error("embedding");
5148 mch_exit(2);
5149#endif
5150 }
5151 }
5152
5153#ifdef FEAT_OLE
5154 {
5155 int bDoRestart = FALSE;
5156
5157 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005158 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (bDoRestart)
5160 mch_exit(0);
5161 }
5162#endif
5163
5164#ifdef FEAT_NETBEANS_INTG
5165 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005166 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 int arg;
5168
5169 for (arg = 1; arg < *argc; arg++)
5170 if (strncmp("-nb", argv[arg], 3) == 0)
5171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 netbeansArg = argv[arg];
5173 mch_memmove(&argv[arg], &argv[arg + 1],
5174 (--*argc - arg) * sizeof(char *));
5175 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005176 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180}
5181
K.Takatac81e9bf2022-01-16 14:15:49 +00005182 static void
5183load_dpi_func(void)
5184{
5185 HMODULE hUser32;
5186
5187 hUser32 = GetModuleHandle("user32.dll");
5188 if (hUser32 == NULL)
5189 goto fail;
5190
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005191 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5192 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5193 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005194 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005195 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5196 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005197
5198 if (pSetThreadDpiAwarenessContext != NULL)
5199 {
5200 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5201 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5202 if (oldctx != NULL)
5203 {
5204 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5205 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5206#ifdef DEBUG
5207 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5208 {
5209 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5210 }
5211#endif
5212 return;
5213 }
5214 }
5215
5216fail:
5217 // Disable PerMonitorV2 APIs.
5218 pGetDpiForSystem = stubGetDpiForSystem;
5219 pGetDpiForWindow = NULL;
5220 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5221 pSetThreadDpiAwarenessContext = NULL;
5222 pGetAwarenessFromDpiAwarenessContext = NULL;
5223}
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225/*
5226 * Initialise the GUI. Create all the windows, set up all the call-backs
5227 * etc.
5228 */
5229 int
5230gui_mch_init(void)
5231{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005233 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
Bram Moolenaar734a8672019-12-02 22:49:38 +01005236 // Return here if the window was already opened (happens when
5237 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005239 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 /*
5242 * Load the tearoff bitmap
5243 */
5244#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005245 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
5247
K.Takatac81e9bf2022-01-16 14:15:49 +00005248 load_dpi_func();
5249
5250 s_dpi = pGetDpiForSystem();
5251 update_scrollbar_size();
5252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005254 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#endif
5256 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005257#ifdef FEAT_TOOLBAR
5258 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5262
Bram Moolenaar734a8672019-12-02 22:49:38 +01005263 // First try using the wide version, so that we can use any title.
5264 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005265 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005267 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 wndclassw.lpfnWndProc = _WndProc;
5269 wndclassw.cbClsExtra = 0;
5270 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005271 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5273 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5274 wndclassw.hbrBackground = s_brush;
5275 wndclassw.lpszMenuName = NULL;
5276 wndclassw.lpszClassName = szVimWndClassW;
5277
K.Takata4ac893f2022-01-20 12:44:28 +00005278 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (vim_parent_hwnd != NULL)
5283 {
5284#ifdef HAVE_TRY_EXCEPT
5285 __try
5286 {
5287#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005288 // Open inside the specified parent window.
5289 // TODO: last argument should point to a CLIENTCREATESTRUCT
5290 // structure.
5291 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005293 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005294 WS_OVERLAPPEDWINDOW | WS_CHILD
5295 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5297 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005298 100, // Any value will do
5299 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005301 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#ifdef HAVE_TRY_EXCEPT
5303 }
5304 __except(EXCEPTION_EXECUTE_HANDLER)
5305 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005306 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308#endif
5309 if (s_hwnd == NULL)
5310 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005311 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 mch_exit(2);
5313 }
5314 }
5315 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005316 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005317 // If the provided windowid is not valid reset it to zero, so that it
5318 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005319 if (IsWindow((HWND)win_socket_id) <= 0)
5320 win_socket_id = 0;
5321
Bram Moolenaar734a8672019-12-02 22:49:38 +01005322 // Create a window. If win_socket_id is not zero without border and
5323 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005324 s_hwnd = CreateWindowW(
5325 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005326 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5327 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005328 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5329 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005330 100, // Any value will do
5331 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005332 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005333 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005334 if (s_hwnd != NULL && win_socket_id != 0)
5335 {
5336 SetParent(s_hwnd, (HWND)win_socket_id);
5337 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5338 }
5339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 if (s_hwnd == NULL)
5342 return FAIL;
5343
K.Takatac81e9bf2022-01-16 14:15:49 +00005344 if (pGetDpiForWindow != NULL)
5345 {
5346 s_dpi = pGetDpiForWindow(s_hwnd);
5347 update_scrollbar_size();
5348 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5349 }
5350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5352 dyn_imm_load();
5353#endif
5354
Bram Moolenaar734a8672019-12-02 22:49:38 +01005355 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005356 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005357 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005358 wndclassw.style = CS_OWNDC;
5359 wndclassw.lpfnWndProc = _TextAreaWndProc;
5360 wndclassw.cbClsExtra = 0;
5361 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005362 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005363 wndclassw.hIcon = NULL;
5364 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5365 wndclassw.hbrBackground = NULL;
5366 wndclassw.lpszMenuName = NULL;
5367 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005368
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005369 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 return FAIL;
5371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005373 s_textArea = CreateWindowExW(
5374 0,
5375 szTextAreaClassW, L"Vim text area",
5376 WS_CHILD | WS_VISIBLE, 0, 0,
5377 100, // Any value will do for now
5378 100, // Any value will do for now
5379 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005380 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 if (s_textArea == NULL)
5383 return FAIL;
5384
Bram Moolenaar20321902016-02-17 12:30:17 +01005385#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005386 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005387 {
5388 HANDLE hIcon = NULL;
5389
5390 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005391 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005392 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005393#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#ifdef FEAT_MENU
5396 s_menuBar = CreateMenu();
5397#endif
5398 s_hdc = GetDC(s_textArea);
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
Bram Moolenaar734a8672019-12-02 22:49:38 +01005402 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005403 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
Bram Moolenaar734a8672019-12-02 22:49:38 +01005405 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 gui_mch_def_colors();
5407
Bram Moolenaar734a8672019-12-02 22:49:38 +01005408 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5409 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 set_normal_colors();
5411
5412 /*
5413 * Check that none of the colors are the same as the background color.
5414 * Then store the current values as the defaults.
5415 */
5416 gui_check_colors();
5417 gui.def_norm_pixel = gui.norm_pixel;
5418 gui.def_back_pixel = gui.back_pixel;
5419
Bram Moolenaar734a8672019-12-02 22:49:38 +01005420 // Get the colors for the highlight groups (gui_check_colors() might have
5421 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 highlight_gui_started();
5423
5424 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005425 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005427 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429 /*
5430 * Set up for Intellimouse processing
5431 */
5432 init_mouse_wheel();
5433
5434 /*
5435 * compute a couple of metrics used for the dialogs
5436 */
5437 get_dialog_font_metrics();
5438#ifdef FEAT_TOOLBAR
5439 /*
5440 * Create the toolbar
5441 */
5442 initialise_toolbar();
5443#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005444#ifdef FEAT_GUI_TABLINE
5445 /*
5446 * Create the tabline
5447 */
5448 initialise_tabline();
5449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#ifdef MSWIN_FIND_REPLACE
5451 /*
5452 * Initialise the dialog box stuff
5453 */
5454 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5455
Bram Moolenaar734a8672019-12-02 22:49:38 +01005456 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005458 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005460 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5462 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5463 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005466#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005467 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005468 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005469#endif
5470
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005471#ifdef FEAT_RENDER_OPTIONS
5472 if (p_rop)
5473 (void)gui_mch_set_rendering_options(p_rop);
5474#endif
5475
Bram Moolenaar748bf032005-02-02 23:04:36 +00005476theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005477 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005478 display_errors();
5479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 return OK;
5481}
5482
5483/*
5484 * Get the size of the screen, taking position on multiple monitors into
5485 * account (if supported).
5486 */
5487 static void
5488get_work_area(RECT *spi_rect)
5489{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005490 HMONITOR mon;
5491 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaar734a8672019-12-02 22:49:38 +01005493 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005494 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005495 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005497 moninfo.cbSize = sizeof(MONITORINFO);
5498 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005500 *spi_rect = moninfo.rcWork;
5501 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
5503 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005504 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5506}
5507
5508/*
5509 * Set the size of the window to the given width and height in pixels.
5510 */
5511 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005512gui_mch_set_shellsize(
5513 int width,
5514 int height,
5515 int min_width UNUSED,
5516 int min_height UNUSED,
5517 int base_width UNUSED,
5518 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005519 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005522 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
Bram Moolenaar734a8672019-12-02 22:49:38 +01005525 // Try to keep window completely on screen.
5526 // Get position of the screen work area. This is the part that is not
5527 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 get_work_area(&workarea_rect);
5529
Bram Moolenaar734a8672019-12-02 22:49:38 +01005530 // Resizing a maximized window looks very strange, unzoom it first.
5531 // But don't do it when still starting up, it may have been requested in
5532 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005533 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005535
5536 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar734a8672019-12-02 22:49:38 +01005538 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005539 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5540 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5541 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5542 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5543 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5544 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaar734a8672019-12-02 22:49:38 +01005546 // The following should take care of keeping Vim on the same monitor, no
5547 // matter if the secondary monitor is left or right of the primary
5548 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005549 window_rect.right = window_rect.left + win_width;
5550 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005551
Bram Moolenaar734a8672019-12-02 22:49:38 +01005552 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005553 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5554 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005556 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5557 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005559 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5560 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005562 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5563 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005565 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5566 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
5568 SetActiveWindow(s_hwnd);
5569 SetFocus(s_hwnd);
5570
Bram Moolenaar734a8672019-12-02 22:49:38 +01005571 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573}
5574
5575
5576 void
5577gui_mch_set_scrollbar_thumb(
5578 scrollbar_T *sb,
5579 long val,
5580 long size,
5581 long max)
5582{
5583 SCROLLINFO info;
5584
5585 sb->scroll_shift = 0;
5586 while (max > 32767)
5587 {
5588 max = (max + 1) >> 1;
5589 val >>= 1;
5590 size >>= 1;
5591 ++sb->scroll_shift;
5592 }
5593
5594 if (sb->scroll_shift > 0)
5595 ++size;
5596
5597 info.cbSize = sizeof(info);
5598 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5599 info.nPos = val;
5600 info.nMin = 0;
5601 info.nMax = max;
5602 info.nPage = size;
5603 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5604}
5605
5606
5607/*
5608 * Set the current text font.
5609 */
5610 void
5611gui_mch_set_font(GuiFont font)
5612{
5613 gui.currFont = font;
5614}
5615
5616
5617/*
5618 * Set the current text foreground color.
5619 */
5620 void
5621gui_mch_set_fg_color(guicolor_T color)
5622{
5623 gui.currFgColor = color;
5624}
5625
5626/*
5627 * Set the current text background color.
5628 */
5629 void
5630gui_mch_set_bg_color(guicolor_T color)
5631{
5632 gui.currBgColor = color;
5633}
5634
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005635/*
5636 * Set the current text special color.
5637 */
5638 void
5639gui_mch_set_sp_color(guicolor_T color)
5640{
5641 gui.currSpColor = color;
5642}
5643
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005644#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645/*
5646 * Multi-byte handling, originally by Sung-Hoon Baek.
5647 * First static functions (no prototypes generated).
5648 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005649# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005650# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005651# endif
5652# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 * handle WM_IME_NOTIFY message
5656 */
5657 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005658_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 LRESULT lResult = 0;
5661 HIMC hImc;
5662
5663 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5664 return lResult;
5665 switch (dwCommand)
5666 {
5667 case IMN_SETOPENSTATUS:
5668 if (pImmGetOpenStatus(hImc))
5669 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005670 LOGFONTW lf = norm_logfont;
5671 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5672 // Work around when PerMonitorV2 is not enabled in the process level.
5673 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5674 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 im_set_position(gui.row, gui.col);
5676
Bram Moolenaar734a8672019-12-02 22:49:38 +01005677 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005678 State &= ~MODE_LANGMAP;
5679 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005681# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005682 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5684 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005685 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 int old_row = gui.row;
5687 int old_col = gui.col;
5688
5689 // This must be called here before
5690 // status_redraw_curbuf(), otherwise the mode
5691 // message may appear in the wrong position.
5692 showmode();
5693 status_redraw_curbuf();
5694 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005695 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 gui.row = old_row;
5697 gui.col = old_col;
5698 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701 }
5702 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005703 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 lResult = 0;
5705 break;
5706 }
5707 pImmReleaseContext(hWnd, hImc);
5708 return lResult;
5709}
5710
5711 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005712_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 char_u *ret;
5715 int len;
5716
Bram Moolenaar734a8672019-12-02 22:49:38 +01005717 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 return 0;
5719
5720 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5721 if (ret != NULL)
5722 {
5723 add_to_input_buf_csi(ret, len);
5724 vim_free(ret);
5725 return 1;
5726 }
5727 return 0;
5728}
5729
5730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 * void GetResultStr()
5732 *
5733 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5734 * get complete composition string
5735 */
5736 static char_u *
5737GetResultStr(HWND hwnd, int GCS, int *lenp)
5738{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005739 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005740 LONG ret;
5741 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 char_u *convbuf = NULL;
5743
5744 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5745 return NULL;
5746
K.Takatab0b2b732022-01-19 12:59:21 +00005747 // Get the length of the composition string.
5748 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5749 if (ret <= 0)
5750 return NULL;
5751
5752 // Allocate the requested buffer plus space for the NUL character.
5753 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (buf == NULL)
5755 return NULL;
5756
K.Takatab0b2b732022-01-19 12:59:21 +00005757 // Reads in the composition string.
5758 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5759 *lenp = ret / sizeof(WCHAR);
5760
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005761 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 pImmReleaseContext(hwnd, hIMC);
5763 vim_free(buf);
5764 return convbuf;
5765}
5766#endif
5767
Bram Moolenaar734a8672019-12-02 22:49:38 +01005768// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005769#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
5771/*
5772 * set font to IM.
5773 */
5774 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005775im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 HIMC hImc;
5778
5779 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5780 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005781 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 pImmReleaseContext(s_hwnd, hImc);
5783 }
5784}
5785
5786/*
5787 * Notify cursor position to IM.
5788 */
5789 void
5790im_set_position(int row, int col)
5791{
5792 HIMC hImc;
5793
5794 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5795 {
5796 COMPOSITIONFORM cfs;
5797
5798 cfs.dwStyle = CFS_POINT;
5799 cfs.ptCurrentPos.x = FILL_X(col);
5800 cfs.ptCurrentPos.y = FILL_Y(row);
5801 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005802 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5803 {
5804 // Work around when PerMonitorV2 is not enabled in the process level.
5805 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5806 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 pImmSetCompositionWindow(hImc, &cfs);
5809
5810 pImmReleaseContext(s_hwnd, hImc);
5811 }
5812}
5813
5814/*
5815 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5816 */
5817 void
5818im_set_active(int active)
5819{
5820 HIMC hImc;
5821 static HIMC hImcOld = (HIMC)0;
5822
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005823# ifdef VIMDLL
5824 if (!gui.in_use && !gui.starting)
5825 {
5826 mbyte_im_set_active(active);
5827 return;
5828 }
5829# endif
5830
Bram Moolenaar734a8672019-12-02 22:49:38 +01005831 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
5833 if (p_imdisable)
5834 {
5835 if (hImcOld == (HIMC)0)
5836 {
5837 hImcOld = pImmGetContext(s_hwnd);
5838 if (hImcOld)
5839 pImmAssociateContext(s_hwnd, (HIMC)0);
5840 }
5841 active = FALSE;
5842 }
5843 else if (hImcOld != (HIMC)0)
5844 {
5845 pImmAssociateContext(s_hwnd, hImcOld);
5846 hImcOld = (HIMC)0;
5847 }
5848
5849 hImc = pImmGetContext(s_hwnd);
5850 if (hImc)
5851 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005852 /*
5853 * for Korean ime
5854 */
5855 HKL hKL = GetKeyboardLayout(0);
5856
5857 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5858 {
5859 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5860 static BOOL bSaved = FALSE;
5861
5862 if (active)
5863 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005864 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005865 if (bSaved)
5866 pImmSetConversionStatus(hImc, dwConversionSaved,
5867 dwSentenceSaved);
5868 bSaved = FALSE;
5869 }
5870 else
5871 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005872 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005873 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5874 &dwSentenceSaved))
5875 {
5876 bSaved = TRUE;
5877 pImmSetConversionStatus(hImc,
5878 dwConversionSaved & ~(IME_CMODE_NATIVE
5879 | IME_CMODE_FULLSHAPE),
5880 dwSentenceSaved);
5881 }
5882 }
5883 }
5884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 pImmSetOpenStatus(hImc, active);
5886 pImmReleaseContext(s_hwnd, hImc);
5887 }
5888 }
5889}
5890
5891/*
5892 * Get IM status. When IM is on, return not 0. Else return 0.
5893 */
5894 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005895im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 int status = 0;
5898 HIMC hImc;
5899
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005900# ifdef VIMDLL
5901 if (!gui.in_use && !gui.starting)
5902 return mbyte_im_get_status();
5903# endif
5904
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5906 {
5907 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5908 pImmReleaseContext(s_hwnd, hImc);
5909 }
5910 return status;
5911}
5912
Bram Moolenaar734a8672019-12-02 22:49:38 +01005913#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005916/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005917 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005918 */
5919 static void
5920latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5921{
5922 int c;
5923
Bram Moolenaarca003e12006-03-17 23:19:38 +00005924 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005925 {
5926 c = *text++;
5927 switch (c)
5928 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005929 case 0xa4: c = 0x20ac; break; // euro
5930 case 0xa6: c = 0x0160; break; // S hat
5931 case 0xa8: c = 0x0161; break; // S -hat
5932 case 0xb4: c = 0x017d; break; // Z hat
5933 case 0xb8: c = 0x017e; break; // Z -hat
5934 case 0xbc: c = 0x0152; break; // OE
5935 case 0xbd: c = 0x0153; break; // oe
5936 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005937 }
5938 *unicodebuf++ = c;
5939 }
5940}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
5942#ifdef FEAT_RIGHTLEFT
5943/*
5944 * What is this for? In the case where you are using Win98 or Win2K or later,
5945 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5946 * reverses the string sent to the TextOut... family. This sucks, because we
5947 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5948 * way to tell Windblows not to do this!
5949 *
5950 * The short of it is that this 'RevOut' only gets called if you are running
5951 * one of the new, "improved" MS OSes, and only if you are running in
5952 * 'rightleft' mode. It makes display take *slightly* longer, but not
5953 * noticeably so.
5954 */
5955 static void
K.Takata135e1522022-01-29 15:27:58 +00005956RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 int col,
5958 int row,
5959 UINT foptions,
5960 CONST RECT *pcliprect,
5961 LPCTSTR text,
5962 UINT len,
5963 CONST INT *padding)
5964{
5965 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005967 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005968 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005969 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970}
5971#endif
5972
Bram Moolenaar92467d32017-12-05 13:22:16 +01005973 static void
5974draw_line(
5975 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005976 int y1,
5977 int x2,
5978 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005979 COLORREF color)
5980{
5981#if defined(FEAT_DIRECTX)
5982 if (IS_ENABLE_DIRECTX())
5983 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5984 else
5985#endif
5986 {
5987 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5988 HPEN old_pen = SelectObject(s_hdc, hpen);
5989 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005990 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005991 LineTo(s_hdc, x2, y2);
5992 DeleteObject(SelectObject(s_hdc, old_pen));
5993 }
5994}
5995
5996 static void
5997set_pixel(
5998 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005999 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006000 COLORREF color)
6001{
6002#if defined(FEAT_DIRECTX)
6003 if (IS_ENABLE_DIRECTX())
6004 DWriteContext_SetPixel(s_dwc, x, y, color);
6005 else
6006#endif
6007 SetPixel(s_hdc, x, y, color);
6008}
6009
6010 static void
6011fill_rect(
6012 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006013 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006014 COLORREF color)
6015{
6016#if defined(FEAT_DIRECTX)
6017 if (IS_ENABLE_DIRECTX())
6018 DWriteContext_FillRect(s_dwc, rcp, color);
6019 else
6020#endif
6021 {
6022 HBRUSH hbr2;
6023
6024 if (hbr == NULL)
6025 hbr2 = CreateSolidBrush(color);
6026 else
6027 hbr2 = hbr;
6028 FillRect(s_hdc, rcp, hbr2);
6029 if (hbr == NULL)
6030 DeleteBrush(hbr2);
6031 }
6032}
6033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 void
6035gui_mch_draw_string(
6036 int row,
6037 int col,
6038 char_u *text,
6039 int len,
6040 int flags)
6041{
6042 static int *padding = NULL;
6043 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 const RECT *pcliprect = NULL;
6045 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 static WCHAR *unicodebuf = NULL;
6047 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006048 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 int y;
6051
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 /*
6053 * Italic and bold text seems to have an extra row of pixels at the bottom
6054 * (below where the bottom of the character should be). If we draw the
6055 * characters with a solid background, the top row of pixels in the
6056 * character below will be overwritten. We can fix this by filling in the
6057 * background ourselves, to the correct character proportions, and then
6058 * writing the character in transparent mode. Still have a problem when
6059 * the character is "_", which gets written on to the character below.
6060 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6061 * pixel in their slots, which fixes the problem with the bottom row of
6062 * pixels. We still need this code because otherwise the top row of pixels
6063 * becomes a problem. - webb.
6064 */
6065 static HBRUSH hbr_cache[2] = {NULL, NULL};
6066 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6067 static int brush_lru = 0;
6068 HBRUSH hbr;
6069 RECT rc;
6070
6071 if (!(flags & DRAW_TRANSP))
6072 {
6073 /*
6074 * Clear background first.
6075 * Note: FillRect() excludes right and bottom of rectangle.
6076 */
6077 rc.left = FILL_X(col);
6078 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (has_mbyte)
6080 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006081 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006082 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 }
6084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 rc.right = FILL_X(col + len);
6086 rc.bottom = FILL_Y(row + 1);
6087
Bram Moolenaar734a8672019-12-02 22:49:38 +01006088 // Cache the created brush, that saves a lot of time. We need two:
6089 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (gui.currBgColor == brush_color[0])
6091 {
6092 hbr = hbr_cache[0];
6093 brush_lru = 1;
6094 }
6095 else if (gui.currBgColor == brush_color[1])
6096 {
6097 hbr = hbr_cache[1];
6098 brush_lru = 0;
6099 }
6100 else
6101 {
6102 if (hbr_cache[brush_lru] != NULL)
6103 DeleteBrush(hbr_cache[brush_lru]);
6104 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6105 brush_color[brush_lru] = gui.currBgColor;
6106 hbr = hbr_cache[brush_lru];
6107 brush_lru = !brush_lru;
6108 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006109
Bram Moolenaar92467d32017-12-05 13:22:16 +01006110 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 SetBkMode(s_hdc, TRANSPARENT);
6113
6114 /*
6115 * When drawing block cursor, prevent inverted character spilling
6116 * over character cell (can happen with bold/italic)
6117 */
6118 if (flags & DRAW_CURSOR)
6119 {
6120 pcliprect = &rc;
6121 foptions = ETO_CLIPPED;
6122 }
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 SetTextColor(s_hdc, gui.currFgColor);
6125 SelectFont(s_hdc, gui.currFont);
6126
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006127#ifdef FEAT_DIRECTX
6128 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006129 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006130#endif
6131
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6133 {
K.Takata135e1522022-01-29 15:27:58 +00006134 int i;
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 vim_free(padding);
6137 pad_size = Columns;
6138
Bram Moolenaar734a8672019-12-02 22:49:38 +01006139 // Don't give an out-of-memory message here, it would call us
6140 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006141 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (padding != NULL)
6143 for (i = 0; i < pad_size; i++)
6144 padding[i] = gui.char_width;
6145 }
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 /*
6148 * We have to provide the padding argument because italic and bold versions
6149 * of fixed-width fonts are often one pixel or so wider than their normal
6150 * versions.
6151 * No check for DRAW_BOLD, Windows will have done it already.
6152 */
6153
Bram Moolenaar734a8672019-12-02 22:49:38 +01006154 // Check if there are any UTF-8 characters. If not, use normal text
6155 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (enc_utf8)
6157 for (n = 0; n < len; ++n)
6158 if (text[n] >= 0x80)
6159 break;
6160
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006161#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006162 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6163 // required that unicode drawing routine, currently. So this forces it
6164 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006165 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006166 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006167#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006168
Bram Moolenaar734a8672019-12-02 22:49:38 +01006169 // Check if the Unicode buffer exists and is big enough. Create it
6170 // with the same length as the multi-byte string, the number of wide
6171 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006172 if ((enc_utf8
6173 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6174 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 && (unicodebuf == NULL || len > unibuflen))
6176 {
6177 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006178 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006181 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 unibuflen = len;
6184 }
6185
6186 if (enc_utf8 && n < len && unicodebuf != NULL)
6187 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006188 // Output UTF-8 characters. Composing characters should be
6189 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006190 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006191 int wlen; // string length in words
6192 int clen; // string length in characters
6193 int cells; // cell width of string up to composing char
6194 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006195 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006197 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006198 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006200 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006202 c = utf_ptr2char(text + i);
6203 if (c >= 0x10000)
6204 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006205 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006206 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6207 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006208 }
6209 else
6210 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006211 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006212 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006213
6214 if (utf_iscomposing(c))
6215 cw = 0;
6216 else
6217 {
6218 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006219 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006220 cw = 1;
6221 }
6222
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (unicodepdy != NULL)
6224 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006225 // Use unicodepdy to make characters fit as we expect, even
6226 // when the font uses different widths (e.g., bold character
6227 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006228 if (c >= 0x10000)
6229 {
6230 unicodepdy[wlen - 2] = cw * gui.char_width;
6231 unicodepdy[wlen - 1] = 0;
6232 }
6233 else
6234 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006237 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006238 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006240#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006241 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006242 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006243 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006244 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006245 TEXT_X(col), TEXT_Y(row),
6246 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006247 gui.char_width, gui.currFgColor,
6248 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006249 }
6250 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006251#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006252 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6253 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006254 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006256 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006258 // If we want to display codepage data, and the current CP is not the
6259 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (unicodebuf != NULL)
6261 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006262 if (enc_latin9)
6263 latin9_to_ucs(text, len, unicodebuf);
6264 else
6265 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 MB_PRECOMPOSED,
6267 (char *)text, len,
6268 (LPWSTR)unicodebuf, unibuflen);
6269 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006270 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006271 // Use unicodepdy to make characters fit as we expect, even
6272 // when the font uses different widths (e.g., bold character
6273 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006274 if (unicodepdy != NULL)
6275 {
6276 int i;
6277 int cw;
6278
6279 for (i = 0; i < len; ++i)
6280 {
6281 cw = utf_char2cells(unicodebuf[i]);
6282 if (cw > 2)
6283 cw = 1;
6284 unicodepdy[i] = cw * gui.char_width;
6285 }
6286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006288 foptions, pcliprect, unicodebuf, len, unicodepdy);
6289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 }
6291 }
6292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006295 // Windows will mess up RL text, so we have to draw it character by
6296 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006297 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6299 foptions, pcliprect, (char *)text, len, padding);
6300 else
6301#endif
6302 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6303 foptions, pcliprect, (char *)text, len, padding);
6304 }
6305
Bram Moolenaar734a8672019-12-02 22:49:38 +01006306 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (flags & DRAW_UNDERL)
6308 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006309 // When p_linespace is 0, overwrite the bottom row of pixels.
6310 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (p_linespace > 1)
6313 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006314 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006316
Bram Moolenaar734a8672019-12-02 22:49:38 +01006317 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006318 if (flags & DRAW_STRIKE)
6319 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006320 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006321 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006322 }
6323
Bram Moolenaar734a8672019-12-02 22:49:38 +01006324 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006325 if (flags & DRAW_UNDERC)
6326 {
6327 int x;
6328 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006329 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006330
6331 y = FILL_Y(row + 1) - 1;
6332 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6333 {
6334 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006335 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006336 }
6337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338}
6339
6340
6341/*
6342 * Output routines.
6343 */
6344
Bram Moolenaar734a8672019-12-02 22:49:38 +01006345/*
6346 * Flush any output to the screen
6347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 void
6349gui_mch_flush(void)
6350{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006351#if defined(FEAT_DIRECTX)
6352 if (IS_ENABLE_DIRECTX())
6353 DWriteContext_Flush(s_dwc);
6354#endif
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 GdiFlush();
6357}
6358
6359 static void
6360clear_rect(RECT *rcp)
6361{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006362 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365
Bram Moolenaarc716c302006-01-21 22:12:51 +00006366 void
6367gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6368{
6369 RECT workarea_rect;
6370
6371 get_work_area(&workarea_rect);
6372
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006373 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006374 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6375 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006376
Bram Moolenaar734a8672019-12-02 22:49:38 +01006377 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6378 // the menubar for MSwin, we subtract it from the screen height, so that
6379 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006380 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006381 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6382 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6383 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6384 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006385}
6386
6387
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388#if defined(FEAT_MENU) || defined(PROTO)
6389/*
6390 * Add a sub menu to the menu bar.
6391 */
6392 void
6393gui_mch_add_menu(
6394 vimmenu_T *menu,
6395 int pos)
6396{
6397 vimmenu_T *parent = menu->parent;
6398
6399 menu->submenu_id = CreatePopupMenu();
6400 menu->id = s_menu_id++;
6401
6402 if (menu_is_menubar(menu->name))
6403 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006404 WCHAR *wn;
6405 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006407 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006408 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006409 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006411 infow.cbSize = sizeof(infow);
6412 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6413 | MIIM_SUBMENU;
6414 infow.dwItemData = (long_u)menu;
6415 infow.wID = menu->id;
6416 infow.fType = MFT_STRING;
6417 infow.dwTypeData = wn;
6418 infow.cch = (UINT)wcslen(wn);
6419 infow.hSubMenu = menu->submenu_id;
6420 InsertMenuItemW((parent == NULL)
6421 ? s_menuBar : parent->submenu_id,
6422 (UINT)pos, TRUE, &infow);
6423 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425
Bram Moolenaar734a8672019-12-02 22:49:38 +01006426 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 if (parent == NULL)
6428 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006429# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 else if (IsWindow(parent->tearoff_handle))
6431 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433}
6434
6435 void
6436gui_mch_show_popupmenu(vimmenu_T *menu)
6437{
6438 POINT mp;
6439
K.Takata45f9cfb2022-01-21 11:11:00 +00006440 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6442}
6443
6444 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006445gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 vimmenu_T *menu = gui_find_menu(path_name);
6448
6449 if (menu != NULL)
6450 {
6451 POINT p;
6452
Bram Moolenaar734a8672019-12-02 22:49:38 +01006453 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006455 if (mouse_pos)
6456 {
6457 int mx, my;
6458
6459 gui_mch_getmouse(&mx, &my);
6460 p.x += mx;
6461 p.y += my;
6462 }
6463 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006465 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6467 }
6468 msg_scroll = FALSE;
6469 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6470 }
6471}
6472
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006473# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474/*
6475 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6476 * create it as a pseudo-"tearoff menu".
6477 */
6478 void
6479gui_make_tearoff(char_u *path_name)
6480{
6481 vimmenu_T *menu = gui_find_menu(path_name);
6482
Bram Moolenaar734a8672019-12-02 22:49:38 +01006483 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (menu != NULL)
6485 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6486}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
6489/*
6490 * Add a menu item to a menu
6491 */
6492 void
6493gui_mch_add_menu_item(
6494 vimmenu_T *menu,
6495 int idx)
6496{
6497 vimmenu_T *parent = menu->parent;
6498
6499 menu->id = s_menu_id++;
6500 menu->submenu_id = NULL;
6501
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006502# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6504 {
6505 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6506 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6507 }
6508 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006509# endif
6510# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (menu_is_toolbar(parent->name))
6512 {
6513 TBBUTTON newtb;
6514
Bram Moolenaara80faa82020-04-12 19:37:17 +02006515 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (menu_is_separator(menu->name))
6517 {
6518 newtb.iBitmap = 0;
6519 newtb.fsStyle = TBSTYLE_SEP;
6520 }
6521 else
6522 {
6523 newtb.iBitmap = get_toolbar_bitmap(menu);
6524 newtb.fsStyle = TBSTYLE_BUTTON;
6525 }
6526 newtb.idCommand = menu->id;
6527 newtb.fsState = TBSTATE_ENABLED;
6528 newtb.iString = 0;
6529 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6530 (LPARAM)&newtb);
6531 menu->submenu_id = (HMENU)-1;
6532 }
6533 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006536 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006538 wn = enc_to_utf16(menu->name, NULL);
6539 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006541 InsertMenuW(parent->submenu_id, (UINT)idx,
6542 (menu_is_separator(menu->name)
6543 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6544 (UINT)menu->id, wn);
6545 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006547# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if (IsWindow(parent->tearoff_handle))
6549 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552}
6553
6554/*
6555 * Destroy the machine specific menu widget.
6556 */
6557 void
6558gui_mch_destroy_menu(vimmenu_T *menu)
6559{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006560# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 /*
6562 * is this a toolbar button?
6563 */
6564 if (menu->submenu_id == (HMENU)-1)
6565 {
6566 int iButton;
6567
6568 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6569 (WPARAM)menu->id, 0);
6570 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6571 }
6572 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 {
6575 if (menu->parent != NULL
6576 && menu_is_popup(menu->parent->dname)
6577 && menu->parent->submenu_id != NULL)
6578 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6579 else
6580 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6581 if (menu->submenu_id != NULL)
6582 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006583# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if (IsWindow(menu->tearoff_handle))
6585 DestroyWindow(menu->tearoff_handle);
6586 if (menu->parent != NULL
6587 && menu->parent->children != NULL
6588 && IsWindow(menu->parent->tearoff_handle))
6589 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006590 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 menu->modes = 0;
6592 rebuild_tearoff(menu->parent);
6593 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596}
6597
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006598# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 static void
6600rebuild_tearoff(vimmenu_T *menu)
6601{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006602 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 char_u tbuf[128];
6604 RECT trect;
6605 RECT rct;
6606 RECT roct;
6607 int x, y;
6608
6609 HWND thwnd = menu->tearoff_handle;
6610
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006611 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (GetWindowRect(thwnd, &trect)
6613 && GetWindowRect(s_hwnd, &rct)
6614 && GetClientRect(s_hwnd, &roct))
6615 {
6616 x = trect.left - rct.left;
6617 y = (trect.top - rct.bottom + roct.bottom);
6618 }
6619 else
6620 {
6621 x = y = 0xffffL;
6622 }
6623 DestroyWindow(thwnd);
6624 if (menu->children != NULL)
6625 {
6626 gui_mch_tearoff(tbuf, menu, x, y);
6627 if (IsWindow(menu->tearoff_handle))
6628 (void) SetWindowPos(menu->tearoff_handle,
6629 NULL,
6630 (int)trect.left,
6631 (int)trect.top,
6632 0, 0,
6633 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6634 }
6635}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006636# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638/*
6639 * Make a menu either grey or not grey.
6640 */
6641 void
6642gui_mch_menu_grey(
6643 vimmenu_T *menu,
6644 int grey)
6645{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006646# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 /*
6648 * is this a toolbar button?
6649 */
6650 if (menu->submenu_id == (HMENU)-1)
6651 {
6652 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006653 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006656# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006657 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6658 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006660# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6662 {
6663 WORD menuID;
6664 HWND menuHandle;
6665
6666 /*
6667 * A tearoff button has changed state.
6668 */
6669 if (menu->children == NULL)
6670 menuID = (WORD)(menu->id);
6671 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006672 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6674 if (menuHandle)
6675 EnableWindow(menuHandle, !grey);
6676
6677 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
Bram Moolenaar734a8672019-12-02 22:49:38 +01006681#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683
Bram Moolenaar734a8672019-12-02 22:49:38 +01006684// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006687#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6690/*
6691 * stuff for dialogs
6692 */
6693
6694/*
6695 * The callback routine used by all the dialogs. Very simple. First,
6696 * acknowledges the INITDIALOG message so that Windows knows to do standard
6697 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6698 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6699 * number.
6700 */
6701 static LRESULT CALLBACK
6702dialog_callback(
6703 HWND hwnd,
6704 UINT message,
6705 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006706 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 if (message == WM_INITDIALOG)
6709 {
6710 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006711 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 (void)SetFocus(hwnd);
6713 if (dialog_default_button > IDCANCEL)
6714 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006715 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006716 // We don't have a default, set focus on another element of the
6717 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006718 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 return FALSE;
6720 }
6721
6722 if (message == WM_COMMAND)
6723 {
6724 int button = LOWORD(wParam);
6725
Bram Moolenaar734a8672019-12-02 22:49:38 +01006726 // Don't end the dialog if something was selected that was
6727 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 if (button >= DLG_NONBUTTON_CONTROL)
6729 return TRUE;
6730
Bram Moolenaar734a8672019-12-02 22:49:38 +01006731 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006733 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006734 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006735 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006736
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006737 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6738 p = utf16_to_enc(wp, NULL);
6739 vim_strncpy(s_textfield, p, IOSIZE);
6740 vim_free(p);
6741 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 /*
6745 * Need to check for IDOK because if the user just hits Return to
6746 * accept the default value, some reason this is what we get.
6747 */
6748 if (button == IDOK)
6749 {
6750 if (dialog_default_button > IDCANCEL)
6751 EndDialog(hwnd, dialog_default_button);
6752 }
6753 else
6754 EndDialog(hwnd, button - IDCANCEL);
6755 return TRUE;
6756 }
6757
6758 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6759 {
6760 EndDialog(hwnd, 0);
6761 return TRUE;
6762 }
6763 return FALSE;
6764}
6765
6766/*
6767 * Create a dialog dynamically from the parameter strings.
6768 * type = type of dialog (question, alert, etc.)
6769 * title = dialog title. may be NULL for default title.
6770 * message = text to display. Dialog sizes to accommodate it.
6771 * buttons = '\n' separated list of button captions, default first.
6772 * dfltbutton = number of default button.
6773 *
6774 * This routine returns 1 if the first button is pressed,
6775 * 2 for the second, etc.
6776 *
6777 * 0 indicates Esc was pressed.
6778 * -1 for unexpected error
6779 *
6780 * If stubbing out this fn, return 1.
6781 */
6782
Bram Moolenaar734a8672019-12-02 22:49:38 +01006783static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 "IDR_VIM",
6786 "IDR_VIM_ERROR",
6787 "IDR_VIM_ALERT",
6788 "IDR_VIM_INFO",
6789 "IDR_VIM_QUESTION"
6790};
6791
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 int
6793gui_mch_dialog(
6794 int type,
6795 char_u *title,
6796 char_u *message,
6797 char_u *buttons,
6798 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006799 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006800 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006803 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 int numButtons;
6805 int *buttonWidths, *buttonPositions;
6806 int buttonYpos;
6807 int nchar, i;
6808 DWORD lStyle;
6809 int dlgwidth = 0;
6810 int dlgheight;
6811 int editboxheight;
6812 int horizWidth = 0;
6813 int msgheight;
6814 char_u *pstart;
6815 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006816 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 char_u *tbuffer;
6818 RECT rect;
6819 HWND hwnd;
6820 HDC hdc;
6821 HFONT font, oldFont;
6822 TEXTMETRIC fontInfo;
6823 int fontHeight;
6824 int textWidth, minButtonWidth, messageWidth;
6825 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006826 int maxDialogHeight;
6827 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 int vertical;
6829 int dlgPaddingX;
6830 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006831# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006832 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006834# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006835 garray_T ga;
6836 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006837 int dlg_icon_width;
6838 int dlg_icon_height;
6839 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006841# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006842 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006843# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006844 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006845# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006846 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006847 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaar748bf032005-02-02 23:04:36 +00006850 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006851 {
6852 load_dpi_func();
6853 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006854 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006855 }
6856 else
6857 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
6859 if ((type < 0) || (type > VIM_LAST_TYPE))
6860 type = 0;
6861
Bram Moolenaar734a8672019-12-02 22:49:38 +01006862 // allocate some memory for dialog template
6863 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006864 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006865 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867 if (p == NULL)
6868 return -1;
6869
6870 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006871 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6873 * const.
6874 */
6875 tbuffer = vim_strsave(buttons);
6876 if (tbuffer == NULL)
6877 return -1;
6878
Bram Moolenaar734a8672019-12-02 22:49:38 +01006879 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
Bram Moolenaar734a8672019-12-02 22:49:38 +01006881 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 numButtons = 1;
6883 for (i = 0; tbuffer[i] != '\0'; i++)
6884 {
6885 if (tbuffer[i] == DLG_BUTTON_SEP)
6886 numButtons++;
6887 }
6888 if (dfltbutton >= numButtons)
6889 dfltbutton = -1;
6890
Bram Moolenaar734a8672019-12-02 22:49:38 +01006891 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006892 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 if (buttonWidths == NULL)
6894 return -1;
6895
Bram Moolenaar734a8672019-12-02 22:49:38 +01006896 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006897 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (buttonPositions == NULL)
6899 return -1;
6900
6901 /*
6902 * Calculate how big the dialog must be.
6903 */
6904 hwnd = GetDesktopWindow();
6905 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006906# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6908 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006909 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 use_lfSysmenu = TRUE;
6911 }
6912 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006913# endif
K.Takatad1c58992022-01-23 12:31:57 +00006914 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6915 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6916
6917 oldFont = SelectFont(hdc, font);
6918 dlgPaddingX = DLG_PADDING_X;
6919 dlgPaddingY = DLG_PADDING_Y;
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 GetTextMetrics(hdc, &fontInfo);
6922 fontHeight = fontInfo.tmHeight;
6923
Bram Moolenaar734a8672019-12-02 22:49:38 +01006924 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006925 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
Bram Moolenaar734a8672019-12-02 22:49:38 +01006927 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006928 if (s_hwnd == NULL)
6929 {
6930 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
Bram Moolenaar734a8672019-12-02 22:49:38 +01006932 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006933 get_work_area(&workarea_rect);
6934 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006935 if (maxDialogWidth > adjust_by_system_dpi(600))
6936 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006937 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006938 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006939 }
6940 else
6941 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006943 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006944 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006945 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6946 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6947 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6948 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006949
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006950 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006951 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6952 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6953 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6954 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6955 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006956 }
6957
Bram Moolenaar734a8672019-12-02 22:49:38 +01006958 // Set dlgwidth to width of message.
6959 // Copy the message into "ga", changing NL to CR-NL and inserting line
6960 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 pstart = message;
6962 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006963 msgheight = 0;
6964 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 do
6966 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006967 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006968
Bram Moolenaar734a8672019-12-02 22:49:38 +01006969 // Need to figure out where to break the string. The system does it
6970 // at a word boundary, which would mean we can't compute the number of
6971 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006972 textWidth = 0;
6973 last_white = NULL;
6974 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006975 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006976 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006977 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006978 && textWidth > maxDialogWidth * 3 / 4)
6979 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006980 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006981 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006982 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006983 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006984 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006985 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006986 textWidth = 0;
6987
6988 if (last_white != NULL)
6989 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006991 if (pend > last_white)
6992 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006993 pend = last_white + 1;
6994 last_white = NULL;
6995 }
6996 ga_append(&ga, '\r');
6997 ga_append(&ga, '\n');
6998 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006999 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007000
7001 while (--l >= 0)
7002 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007003 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007004 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007006
7007 ga_append(&ga, '\r');
7008 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 pstart = pend + 1;
7010 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007011
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007012 if (ga.ga_data != NULL)
7013 message = ga.ga_data;
7014
Bram Moolenaar734a8672019-12-02 22:49:38 +01007015 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007016
K.Takatac81e9bf2022-01-16 14:15:49 +00007017 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7018 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
K.Takatac81e9bf2022-01-16 14:15:49 +00007020 // Add width of icon to dlgwidth, and some space
7021 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7022 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7023
7024 if (msgheight < dlg_icon_height)
7025 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
7027 /*
7028 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007029 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007031 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 if (!vertical)
7033 {
7034 // Place buttons horizontally if they fit.
7035 horizWidth = dlgPaddingX;
7036 pstart = tbuffer;
7037 i = 0;
7038 do
7039 {
7040 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7041 if (pend == NULL)
7042 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007043 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (textWidth < minButtonWidth)
7045 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007046 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 buttonWidths[i] = textWidth;
7048 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007049 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 pstart = pend + 1;
7051 } while (*pend != NUL);
7052
7053 if (horizWidth > maxDialogWidth)
7054 vertical = TRUE; // Too wide to fit on the screen.
7055 else if (horizWidth > dlgwidth)
7056 dlgwidth = horizWidth;
7057 }
7058
7059 if (vertical)
7060 {
7061 // Stack buttons vertically.
7062 pstart = tbuffer;
7063 do
7064 {
7065 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7066 if (pend == NULL)
7067 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007068 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007069 textWidth += dlgPaddingX; // Padding within button
7070 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (textWidth > dlgwidth)
7072 dlgwidth = textWidth;
7073 pstart = pend + 1;
7074 } while (*pend != NUL);
7075 }
7076
7077 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007078 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar734a8672019-12-02 22:49:38 +01007080 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007081 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 add_long(lStyle);
7084 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007085 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 add_word(0); // NumberOfItems(will change later)
7087 add_word(10); // x
7088 add_word(10); // y
7089 add_word(PixelToDialogX(dlgwidth)); // cx
7090
7091 // Dialog height.
7092 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007093 dlgheight = msgheight + 2 * dlgPaddingY
7094 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 else
7096 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7097
7098 // Dialog needs to be taller if contains an edit box.
7099 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7100 if (textfield != NULL)
7101 dlgheight += editboxheight;
7102
Bram Moolenaar734a8672019-12-02 22:49:38 +01007103 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007104 if (dlgheight > maxDialogHeight)
7105 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007106 msgheight = msgheight - (dlgheight - maxDialogHeight);
7107 dlgheight = maxDialogHeight;
7108 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007109 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007110 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007111 }
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 add_word(PixelToDialogY(dlgheight));
7114
7115 add_word(0); // Menu
7116 add_word(0); // Class
7117
Bram Moolenaar734a8672019-12-02 22:49:38 +01007118 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007119 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7120 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 p += nchar;
7122
K.Takatad1c58992022-01-23 12:31:57 +00007123 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007124# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007125 if (use_lfSysmenu)
7126 {
7127 // point size
7128 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7129 GetDeviceCaps(hdc, LOGPIXELSY));
7130 wcscpy(p, lfSysmenu.lfFaceName);
7131 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
K.Takatad1c58992022-01-23 12:31:57 +00007133 else
7134# endif
7135 {
7136 *p++ = DLG_FONT_POINT_SIZE; // point size
7137 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7138 }
7139 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140
7141 buttonYpos = msgheight + 2 * dlgPaddingY;
7142
7143 if (textfield != NULL)
7144 buttonYpos += editboxheight;
7145
7146 pstart = tbuffer;
7147 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007148 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 for (i = 0; i < numButtons; i++)
7150 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007151 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 for ( pend = pstart;
7153 *pend && (*pend != DLG_BUTTON_SEP);
7154 pend++)
7155 ;
7156
7157 if (*pend)
7158 *pend = '\0';
7159
7160 /*
7161 * old NOTE:
7162 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7163 * the focus to the first tab-able button and in so doing makes that
7164 * the default!! Grrr. Workaround: Make the default button the only
7165 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7166 * he/she can use arrow keys.
7167 *
7168 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007169 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 * dialog. Also needed for when the textfield is the default control.
7171 * It appears to work now (perhaps not on Win95?).
7172 */
7173 if (vertical)
7174 {
7175 p = add_dialog_element(p,
7176 (i == dfltbutton
7177 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7178 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007179 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 + 2 * fontHeight * i),
7181 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7182 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007183 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
7185 else
7186 {
7187 p = add_dialog_element(p,
7188 (i == dfltbutton
7189 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7190 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007191 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 PixelToDialogX(buttonWidths[i]),
7193 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007194 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007196 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
7198 *pnumitems += numButtons;
7199
Bram Moolenaar734a8672019-12-02 22:49:38 +01007200 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 p = add_dialog_element(p, SS_ICON,
7202 PixelToDialogX(dlgPaddingX),
7203 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007204 PixelToDialogX(dlg_icon_width),
7205 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7207 dlg_icons[type]);
7208
Bram Moolenaar734a8672019-12-02 22:49:38 +01007209 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007210 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007211 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007212 PixelToDialogY(dlgPaddingY),
7213 (WORD)(PixelToDialogX(messageWidth) + 1),
7214 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007215 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
Bram Moolenaar734a8672019-12-02 22:49:38 +01007217 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 if (textfield != NULL)
7219 {
7220 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7221 PixelToDialogX(2 * dlgPaddingX),
7222 PixelToDialogY(2 * dlgPaddingY + msgheight),
7223 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7224 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007225 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 *pnumitems += 1;
7227 }
7228
7229 *pnumitems += 2;
7230
7231 SelectFont(hdc, oldFont);
7232 DeleteObject(font);
7233 ReleaseDC(hwnd, hdc);
7234
Bram Moolenaar734a8672019-12-02 22:49:38 +01007235 // Let the dialog_callback() function know which button to make default
7236 // If we have an edit box, make that the default. We also need to tell
7237 // dialog_callback() if this dialog contains an edit box or not. We do
7238 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 if (textfield != NULL)
7240 {
7241 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7242 s_textfield = textfield;
7243 }
7244 else
7245 {
7246 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7247 s_textfield = NULL;
7248 }
7249
Bram Moolenaar734a8672019-12-02 22:49:38 +01007250 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007252 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 (LPDLGTEMPLATE)pdlgtemplate,
7254 s_hwnd,
7255 (DLGPROC)dialog_callback);
7256
7257 LocalFree(LocalHandle(pdlgtemplate));
7258 vim_free(tbuffer);
7259 vim_free(buttonWidths);
7260 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007261 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
Bram Moolenaar734a8672019-12-02 22:49:38 +01007263 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 (void)SetFocus(s_hwnd);
7265
7266 return nchar;
7267}
7268
Bram Moolenaar734a8672019-12-02 22:49:38 +01007269#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271/*
7272 * Put a simple element (basic class) onto a dialog template in memory.
7273 * return a pointer to where the next item should be added.
7274 *
7275 * parameters:
7276 * lStyle = additional style flags
7277 * (be careful, NT3.51 & Win32s will ignore the new ones)
7278 * x,y = x & y positions IN DIALOG UNITS
7279 * w,h = width and height IN DIALOG UNITS
7280 * Id = ID used in messages
7281 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7282 * caption = usually text or resource name
7283 *
7284 * TODO: use the length information noted here to enable the dialog creation
7285 * routines to work out more exactly how much memory they need to alloc.
7286 */
7287 static PWORD
7288add_dialog_element(
7289 PWORD p,
7290 DWORD lStyle,
7291 WORD x,
7292 WORD y,
7293 WORD w,
7294 WORD h,
7295 WORD Id,
7296 WORD clss,
7297 const char *caption)
7298{
7299 int nchar;
7300
Bram Moolenaar734a8672019-12-02 22:49:38 +01007301 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7303 *p++ = LOWORD(lStyle);
7304 *p++ = HIWORD(lStyle);
7305 *p++ = 0; // LOWORD (lExtendedStyle)
7306 *p++ = 0; // HIWORD (lExtendedStyle)
7307 *p++ = x;
7308 *p++ = y;
7309 *p++ = w;
7310 *p++ = h;
7311 *p++ = Id; //9 or 10 words in all
7312
7313 *p++ = (WORD)0xffff;
7314 *p++ = clss; //2 more here
7315
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007316 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 p += nchar;
7318
7319 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7320
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007321 return p; // total = 15 + strlen(caption) words
7322 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323}
7324
7325
7326/*
7327 * Helper routine. Take an input pointer, return closest pointer that is
7328 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7329 */
7330 static LPWORD
7331lpwAlign(
7332 LPWORD lpIn)
7333{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007334 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007336 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 ul += 3;
7338 ul >>= 2;
7339 ul <<= 2;
7340 return (LPWORD)ul;
7341}
7342
7343/*
7344 * Helper routine. Takes second parameter as Ansi string, copies it to first
7345 * parameter as wide character (16-bits / char) string, and returns integer
7346 * number of wide characters (words) in string (including the trailing wide
7347 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007348 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7349 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 static int
7351nCopyAnsiToWideChar(
7352 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007353 LPSTR lpAnsiIn,
7354 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007357 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 int i;
7359 WCHAR *wn;
7360
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007361 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007363 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007364 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (wn != NULL)
7366 {
7367 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007368 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 vim_free(wn);
7370 }
7371 }
7372 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007373 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 nChar = MultiByteToWideChar(
7375 enc_codepage > 0 ? enc_codepage : CP_ACP,
7376 MB_PRECOMPOSED,
7377 lpAnsiIn, len,
7378 lpWCStr, len);
7379 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007380 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 return nChar;
7384}
7385
7386
7387#ifdef FEAT_TEAROFF
7388/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007389 * Lookup menu handle from "menu_id".
7390 */
7391 static HMENU
7392tearoff_lookup_menuhandle(
7393 vimmenu_T *menu,
7394 WORD menu_id)
7395{
7396 for ( ; menu != NULL; menu = menu->next)
7397 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007398 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007399 continue;
7400 if (menu_is_separator(menu->dname))
7401 continue;
7402 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7403 return menu->submenu_id;
7404 }
7405 return NULL;
7406}
7407
7408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 * The callback function for all the modeless dialogs that make up the
7410 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7411 * thinking its menus have been clicked), and go away when closed.
7412 */
7413 static LRESULT CALLBACK
7414tearoff_callback(
7415 HWND hwnd,
7416 UINT message,
7417 WPARAM wParam,
7418 LPARAM lParam)
7419{
7420 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007421 {
7422 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
Bram Moolenaar734a8672019-12-02 22:49:38 +01007426 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 HandleMouseHide(message, lParam);
7428
7429 if (message == WM_COMMAND)
7430 {
7431 if ((WORD)(LOWORD(wParam)) & 0x8000)
7432 {
7433 POINT mp;
7434 RECT rect;
7435
7436 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7437 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007438 vimmenu_T *menu;
7439
7440 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007442 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7444 (int)rect.right - 8,
7445 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007446 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 s_hwnd,
7448 NULL);
7449 /*
7450 * NOTE: The pop-up menu can eat the mouse up event.
7451 * We deal with this in normal.c.
7452 */
7453 }
7454 }
7455 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007456 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7458 /*
7459 * Give main window the focus back: this is so after
7460 * choosing a tearoff button you can start typing again
7461 * straight away.
7462 */
7463 (void)SetFocus(s_hwnd);
7464 return TRUE;
7465 }
7466 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7467 {
7468 DestroyWindow(hwnd);
7469 return TRUE;
7470 }
7471
Bram Moolenaar734a8672019-12-02 22:49:38 +01007472 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (message == WM_EXITSIZEMOVE)
7474 (void)SetActiveWindow(s_hwnd);
7475
7476 return FALSE;
7477}
7478#endif
7479
7480
7481/*
K.Takatad1c58992022-01-23 12:31:57 +00007482 * Computes the dialog base units based on the current dialog font.
7483 * We don't use the GetDialogBaseUnits() API, because we don't use the
7484 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 */
7486 static void
7487get_dialog_font_metrics(void)
7488{
7489 HDC hdc;
7490 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 SIZE size;
7492#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007493 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#endif
7495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007497 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007498 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007499 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500#endif
7501 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007502 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
K.Takatad1c58992022-01-23 12:31:57 +00007504 hdc = GetDC(s_hwnd);
7505 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007506 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007507 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
K.Takataabe628e2022-01-23 16:25:17 +00007509 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007510 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511}
7512
7513#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7514/*
7515 * Create a pseudo-"tearoff menu" based on the child
7516 * items of a given menu pointer.
7517 */
7518 static void
7519gui_mch_tearoff(
7520 char_u *title,
7521 vimmenu_T *menu,
7522 int initX,
7523 int initY)
7524{
7525 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7526 int template_len;
7527 int nchar, textWidth, submenuWidth;
7528 DWORD lStyle;
7529 DWORD lExtendedStyle;
7530 WORD dlgwidth;
7531 WORD menuID;
7532 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007533 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 vimmenu_T *the_menu = menu;
7535 HWND hwnd;
7536 HDC hdc;
7537 HFONT font, oldFont;
7538 int col, spaceWidth, len;
7539 int columnWidths[2];
7540 char_u *label, *text;
7541 int acLen = 0;
7542 int nameLen;
7543 int padding0, padding1, padding2 = 0;
7544 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007545 int x;
7546 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007547# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007548 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
7552 /*
7553 * If this menu is already torn off, move it to the mouse position.
7554 */
7555 if (IsWindow(menu->tearoff_handle))
7556 {
7557 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007558 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
7560 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7561 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7562 }
7563 return;
7564 }
7565
7566 /*
7567 * Create a new tearoff.
7568 */
7569 if (*title == MNU_HIDDEN_CHAR)
7570 title++;
7571
Bram Moolenaar734a8672019-12-02 22:49:38 +01007572 // Allocate memory to store the dialog template. It's made bigger when
7573 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 template_len = DLG_ALLOC_SIZE;
7575 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7576 if (p == NULL)
7577 return;
7578
7579 hwnd = GetDesktopWindow();
7580 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007581# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7583 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007584 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 use_lfSysmenu = TRUE;
7586 }
7587 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007588# endif
K.Takatad1c58992022-01-23 12:31:57 +00007589 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7590 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7591
7592 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593
Bram Moolenaar734a8672019-12-02 22:49:38 +01007594 // Calculate width of a single space. Used for padding columns to the
7595 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007596 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597
Bram Moolenaar734a8672019-12-02 22:49:38 +01007598 // Figure out max width of the text column, the accelerator column and the
7599 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 submenuWidth = 0;
7601 for (col = 0; col < 2; col++)
7602 {
7603 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007604 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007606 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 text = (col == 0) ? pmenu->dname : pmenu->actext;
7608 if (text != NULL && *text != NUL)
7609 {
7610 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7611 if (textWidth > columnWidths[col])
7612 columnWidths[col] = textWidth;
7613 }
7614 if (pmenu->children != NULL)
7615 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7616 }
7617 }
7618 if (columnWidths[1] == 0)
7619 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007620 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 if (submenuWidth != 0)
7622 columnWidths[0] += submenuWidth;
7623 else
7624 columnWidths[0] += spaceWidth;
7625 }
7626 else
7627 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007628 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7630 columnWidths[1] += submenuWidth;
7631 }
7632
7633 /*
7634 * Now find the total width of our 'menu'.
7635 */
7636 textWidth = columnWidths[0] + columnWidths[1];
7637 if (submenuWidth != 0)
7638 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007639 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7641 textWidth += submenuWidth;
7642 }
7643 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7644 if (textWidth > dlgwidth)
7645 dlgwidth = textWidth;
7646 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7647
Bram Moolenaar734a8672019-12-02 22:49:38 +01007648 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007649 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
7651 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7652 *p++ = LOWORD(lStyle);
7653 *p++ = HIWORD(lStyle);
7654 *p++ = LOWORD(lExtendedStyle);
7655 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007656 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007658 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007660 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 else
7662 *p++ = PixelToDialogX(initX); // x
7663 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007664 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 else
7666 *p++ = PixelToDialogY(initY); // y
7667 *p++ = PixelToDialogX(dlgwidth); // cx
7668 ptrueheight = p;
7669 *p++ = 0; // dialog height: changed later anyway
7670 *p++ = 0; // Menu
7671 *p++ = 0; // Class
7672
Bram Moolenaar734a8672019-12-02 22:49:38 +01007673 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007675 ? (LPSTR)title
7676 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 p += nchar;
7678
K.Takatad1c58992022-01-23 12:31:57 +00007679 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007680# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007681 if (use_lfSysmenu)
7682 {
7683 // point size
7684 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7685 GetDeviceCaps(hdc, LOGPIXELSY));
7686 wcscpy(p, lfSysmenu.lfFaceName);
7687 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
K.Takatad1c58992022-01-23 12:31:57 +00007689 else
7690# endif
7691 {
7692 *p++ = DLG_FONT_POINT_SIZE; // point size
7693 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7694 }
7695 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
7697 /*
7698 * Loop over all the items in the menu.
7699 * But skip over the tearbar.
7700 */
7701 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7702 menu = menu->children->next;
7703 else
7704 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007705 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 for ( ; menu != NULL; menu = menu->next)
7707 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007708 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 continue;
7710 if (menu_is_separator(menu->dname))
7711 {
7712 sepPadding += 3;
7713 continue;
7714 }
7715
Bram Moolenaar734a8672019-12-02 22:49:38 +01007716 // Check if there still is plenty of room in the template. Make it
7717 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7719 {
7720 WORD *newp;
7721
7722 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7723 if (newp != NULL)
7724 {
7725 template_len += 4096;
7726 mch_memmove(newp, pdlgtemplate,
7727 (char *)p - (char *)pdlgtemplate);
7728 p = newp + (p - pdlgtemplate);
7729 pnumitems = newp + (pnumitems - pdlgtemplate);
7730 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7731 LocalFree(LocalHandle(pdlgtemplate));
7732 pdlgtemplate = newp;
7733 }
7734 }
7735
Bram Moolenaar734a8672019-12-02 22:49:38 +01007736 // Figure out minimal length of this menu label. Use "name" for the
7737 // actual text, "dname" for estimating the displayed size. "name"
7738 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 len = nameLen = (int)STRLEN(menu->name);
7740 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7741 (int)STRLEN(menu->dname))) / spaceWidth;
7742 len += padding0;
7743
7744 if (menu->actext != NULL)
7745 {
7746 acLen = (int)STRLEN(menu->actext);
7747 len += acLen;
7748 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7749 }
7750 else
7751 textWidth = 0;
7752 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7753 len += padding1;
7754
7755 if (menu->children == NULL)
7756 {
7757 padding2 = submenuWidth / spaceWidth;
7758 len += padding2;
7759 menuID = (WORD)(menu->id);
7760 }
7761 else
7762 {
7763 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007764 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766
Bram Moolenaar734a8672019-12-02 22:49:38 +01007767 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007768 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 if (label == NULL)
7770 break;
7771
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007772 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007773 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007775 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 while (padding0-- > 0)
7777 *text++ = ' ';
7778 if (menu->actext != NULL)
7779 {
7780 STRNCPY(text, menu->actext, acLen);
7781 text += acLen;
7782 }
7783 while (padding1-- > 0)
7784 *text++ = ' ';
7785 if (menu->children != NULL)
7786 {
7787 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7788 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7789 }
7790 else
7791 {
7792 while (padding2-- > 0)
7793 *text++ = ' ';
7794 }
7795 *text = NUL;
7796
7797 /*
7798 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7799 * W95/NT4 it makes the tear-off look more like a menu.
7800 */
7801 p = add_dialog_element(p,
7802 BS_PUSHBUTTON|BS_LEFT,
7803 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7804 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7805 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7806 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007807 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 vim_free(label);
7809 (*pnumitems)++;
7810 }
7811
7812 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7813
7814
Bram Moolenaar734a8672019-12-02 22:49:38 +01007815 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007816 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007817 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 (LPDLGTEMPLATE)pdlgtemplate,
7819 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007820 (DLGPROC)tearoff_callback,
7821 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
7823 LocalFree(LocalHandle(pdlgtemplate));
7824 SelectFont(hdc, oldFont);
7825 DeleteObject(font);
7826 ReleaseDC(hwnd, hdc);
7827
7828 /*
7829 * Reassert ourselves as the active window. This is so that after creating
7830 * a tearoff, the user doesn't have to click with the mouse just to start
7831 * typing again!
7832 */
7833 (void)SetActiveWindow(s_hwnd);
7834
Bram Moolenaar734a8672019-12-02 22:49:38 +01007835 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 force_menu_update = TRUE;
7837}
7838#endif
7839
7840#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007841# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843/*
7844 * Create the toolbar, initially unpopulated.
7845 * (just like the menu, there are no defaults, it's all
7846 * set up through menu.vim)
7847 */
7848 static void
7849initialise_toolbar(void)
7850{
7851 InitCommonControls();
7852 s_toolbarhwnd = CreateToolbarEx(
7853 s_hwnd,
7854 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7855 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007856 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007857 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 IDR_TOOLBAR1, // id of initial bitmap
7859 NULL,
7860 0, // initial number of buttons
7861 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7862 TOOLBAR_BUTTON_HEIGHT,
7863 TOOLBAR_BUTTON_WIDTH,
7864 TOOLBAR_BUTTON_HEIGHT,
7865 sizeof(TBBUTTON)
7866 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007867
7868 // Remove transparency from the toolbar to prevent the main window
7869 // background colour showing through
7870 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7871 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7872
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007873 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
7875 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007876
7877 update_toolbar_size();
7878}
7879
7880 static void
7881update_toolbar_size(void)
7882{
7883 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007884 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007885
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007886 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007887 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7888 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7889 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7890 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7891
7892 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7893 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7894 //TRACE("button size: %d, %d", w, h);
7895 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7896 gui.toolbar_height = h + 6;
7897
7898 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7899 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7900
7901 // TODO:
7902 // Currently, this function only updates the size of toolbar buttons.
7903 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904}
7905
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007906 static LRESULT CALLBACK
7907toolbar_wndproc(
7908 HWND hwnd,
7909 UINT uMsg,
7910 WPARAM wParam,
7911 LPARAM lParam)
7912{
7913 HandleMouseHide(uMsg, lParam);
7914 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7915}
7916
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 static int
7918get_toolbar_bitmap(vimmenu_T *menu)
7919{
7920 int i = -1;
7921
7922 /*
7923 * Check user bitmaps first, unless builtin is specified.
7924 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007925 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {
7927 char_u fname[MAXPATHL];
7928 HANDLE hbitmap = NULL;
7929
7930 if (menu->iconfile != NULL)
7931 {
7932 gui_find_iconfile(menu->iconfile, fname, "bmp");
7933 hbitmap = LoadImage(
7934 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007935 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 IMAGE_BITMAP,
7937 TOOLBAR_BUTTON_WIDTH,
7938 TOOLBAR_BUTTON_HEIGHT,
7939 LR_LOADFROMFILE |
7940 LR_LOADMAP3DCOLORS
7941 );
7942 }
7943
7944 /*
7945 * If the LoadImage call failed, or the "icon=" file
7946 * didn't exist or wasn't specified, try the menu name
7947 */
7948 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007949 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007950# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007951 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007952# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007953 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 hbitmap = LoadImage(
7955 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007956 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 IMAGE_BITMAP,
7958 TOOLBAR_BUTTON_WIDTH,
7959 TOOLBAR_BUTTON_HEIGHT,
7960 LR_LOADFROMFILE |
7961 LR_LOADMAP3DCOLORS
7962 );
7963
7964 if (hbitmap != NULL)
7965 {
7966 TBADDBITMAP tbAddBitmap;
7967
7968 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007969 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
7971 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7972 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007973 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 }
7975 }
7976 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7977 i = menu->iconidx;
7978
7979 return i;
7980}
7981#endif
7982
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007983#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7984 static void
7985initialise_tabline(void)
7986{
7987 InitCommonControls();
7988
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007989 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007990 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007991 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007992 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007993 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007994
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007995 gui.tabline_height = TABLINE_HEIGHT;
7996
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007997 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007998}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007999
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008000/*
8001 * Get tabpage_T from POINT.
8002 */
8003 static tabpage_T *
8004GetTabFromPoint(
8005 HWND hWnd,
8006 POINT pt)
8007{
8008 tabpage_T *ptp = NULL;
8009
8010 if (gui_mch_showing_tabline())
8011 {
8012 TCHITTESTINFO htinfo;
8013 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008014 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008015 if (s_tabhwnd == hWnd)
8016 {
8017 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8018 if (idx != -1)
8019 ptp = find_tabpage(idx + 1);
8020 }
8021 }
8022 return ptp;
8023}
8024
8025static POINT s_pt = {0, 0};
8026static HCURSOR s_hCursor = NULL;
8027
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008028 static LRESULT CALLBACK
8029tabline_wndproc(
8030 HWND hwnd,
8031 UINT uMsg,
8032 WPARAM wParam,
8033 LPARAM lParam)
8034{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008035 POINT pt;
8036 tabpage_T *tp;
8037 RECT rect;
8038 int nCenter;
8039 int idx0;
8040 int idx1;
8041
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008042 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008043
8044 switch (uMsg)
8045 {
8046 case WM_LBUTTONDOWN:
8047 {
8048 s_pt.x = GET_X_LPARAM(lParam);
8049 s_pt.y = GET_Y_LPARAM(lParam);
8050 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008051 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008052 break;
8053 }
8054 case WM_MOUSEMOVE:
8055 if (GetCapture() == hwnd
8056 && ((wParam & MK_LBUTTON)) != 0)
8057 {
8058 pt.x = GET_X_LPARAM(lParam);
8059 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008060 if (abs(pt.x - s_pt.x) >
8061 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008062 {
8063 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8064
8065 tp = GetTabFromPoint(hwnd, pt);
8066 if (tp != NULL)
8067 {
8068 idx0 = tabpage_index(curtab) - 1;
8069 idx1 = tabpage_index(tp) - 1;
8070
8071 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8072 nCenter = rect.left + (rect.right - rect.left) / 2;
8073
Bram Moolenaar734a8672019-12-02 22:49:38 +01008074 // Check if the mouse cursor goes over the center of
8075 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008076 if ((idx0 < idx1) && (nCenter < pt.x))
8077 {
8078 tabpage_move(idx1 + 1);
8079 update_screen(0);
8080 }
8081 else if ((idx1 < idx0) && (pt.x < nCenter))
8082 {
8083 tabpage_move(idx1);
8084 update_screen(0);
8085 }
8086 }
8087 }
8088 }
8089 break;
8090 case WM_LBUTTONUP:
8091 {
8092 if (GetCapture() == hwnd)
8093 {
8094 SetCursor(s_hCursor);
8095 ReleaseCapture();
8096 }
8097 break;
8098 }
8099 default:
8100 break;
8101 }
8102
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008103 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8104}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008105#endif
8106
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8108/*
8109 * Make the GUI window come to the foreground.
8110 */
8111 void
8112gui_mch_set_foreground(void)
8113{
8114 if (IsIconic(s_hwnd))
8115 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8116 SetForegroundWindow(s_hwnd);
8117}
8118#endif
8119
8120#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8121 static void
8122dyn_imm_load(void)
8123{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008124 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (hLibImm == NULL)
8126 return;
8127
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008129 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008131 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008133 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008135 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008137 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008139 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008140 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008141 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008142 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008143 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008145 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008147 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008148 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008149 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
K.Takatab0b2b732022-01-19 12:59:21 +00008151 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 || pImmGetContext == NULL
8153 || pImmAssociateContext == NULL
8154 || pImmReleaseContext == NULL
8155 || pImmGetOpenStatus == NULL
8156 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008157 || pImmGetCompositionFontW == NULL
8158 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008160 || pImmGetConversionStatus == NULL
8161 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
8163 FreeLibrary(hLibImm);
8164 hLibImm = NULL;
8165 pImmGetContext = NULL;
8166 return;
8167 }
8168
8169 return;
8170}
8171
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172#endif
8173
8174#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8175
8176# ifdef FEAT_XPM_W32
8177# define IMAGE_XPM 100
8178# endif
8179
8180typedef struct _signicon_t
8181{
8182 HANDLE hImage;
8183 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008184# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008185 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187} signicon_t;
8188
8189 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008190gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 signicon_t *sign;
8193 int x, y, w, h;
8194
8195 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8196 return;
8197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008198# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008199 if (IS_ENABLE_DIRECTX())
8200 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008201# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008202
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 x = TEXT_X(col);
8204 y = TEXT_Y(row);
8205 w = gui.char_width * 2;
8206 h = gui.char_height;
8207 switch (sign->uType)
8208 {
8209 case IMAGE_BITMAP:
8210 {
8211 HDC hdcMem;
8212 HBITMAP hbmpOld;
8213
8214 hdcMem = CreateCompatibleDC(s_hdc);
8215 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8216 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8217 SelectObject(hdcMem, hbmpOld);
8218 DeleteDC(hdcMem);
8219 }
8220 break;
8221 case IMAGE_ICON:
8222 case IMAGE_CURSOR:
8223 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8224 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008225# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 case IMAGE_XPM:
8227 {
8228 HDC hdcMem;
8229 HBITMAP hbmpOld;
8230
8231 hdcMem = CreateCompatibleDC(s_hdc);
8232 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008233 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8235
8236 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008237 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8239 SelectObject(hdcMem, hbmpOld);
8240 DeleteDC(hdcMem);
8241 }
8242 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 }
8245}
8246
8247 static void
8248close_signicon_image(signicon_t *sign)
8249{
8250 if (sign)
8251 switch (sign->uType)
8252 {
8253 case IMAGE_BITMAP:
8254 DeleteObject((HGDIOBJ)sign->hImage);
8255 break;
8256 case IMAGE_CURSOR:
8257 DestroyCursor((HCURSOR)sign->hImage);
8258 break;
8259 case IMAGE_ICON:
8260 DestroyIcon((HICON)sign->hImage);
8261 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008262# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 case IMAGE_XPM:
8264 DeleteObject((HBITMAP)sign->hImage);
8265 DeleteObject((HBITMAP)sign->hShape);
8266 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 }
8269}
8270
8271 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008272gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 signicon_t sign, *psign;
8275 char_u *ext;
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008278 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 if (ext > signfile)
8280 {
8281 int do_load = 1;
8282
8283 if (!STRICMP(ext, ".bmp"))
8284 sign.uType = IMAGE_BITMAP;
8285 else if (!STRICMP(ext, ".ico"))
8286 sign.uType = IMAGE_ICON;
8287 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8288 sign.uType = IMAGE_CURSOR;
8289 else
8290 do_load = 0;
8291
8292 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008293 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 gui.char_width * 2, gui.char_height,
8295 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008296# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (!STRICMP(ext, ".xpm"))
8298 {
8299 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008300 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8301 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305
8306 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008307 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 *psign = sign;
8309
8310 if (!psign)
8311 {
8312 if (sign.hImage)
8313 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008314 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316 return (void *)psign;
8317
8318}
8319
8320 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008321gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
8323 if (sign)
8324 {
8325 close_signicon_image((signicon_t *)sign);
8326 vim_free(sign);
8327 }
8328}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008331#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332
Bram Moolenaar734a8672019-12-02 22:49:38 +01008333/*
8334 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008335 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008337 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8339 * to get current mouse position).
8340 *
8341 * Trying to use as more Windows services as possible, and as less
8342 * IE version as possible :)).
8343 *
8344 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8345 * BalloonEval struct.
8346 * 2) Enable/Disable simply create/kill BalloonEval Timer
8347 * 3) When there was enough inactivity, timer procedure posts
8348 * async request to debugger
8349 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8350 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008351 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 */
8353
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008354 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008355make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008356{
K.Takata76687d22022-01-25 10:31:37 +00008357 TOOLINFOW *pti;
8358 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008359
K.Takata76687d22022-01-25 10:31:37 +00008360 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008361 if (pti == NULL)
8362 return;
8363
8364 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8365 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8366 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008367 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008368
8369 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8370 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8371
K.Takata76687d22022-01-25 10:31:37 +00008372 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008373 pti->uFlags = TTF_SUBCLASS;
8374 pti->hwnd = beval->target;
8375 pti->hinst = 0; // Don't use string resources
8376 pti->uId = ID_BEVAL_TOOLTIP;
8377
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008378 pti->lpszText = LPSTR_TEXTCALLBACKW;
8379 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8380 pti->lParam = (LPARAM)beval->tofree;
8381 // switch multiline tooltips on
8382 if (GetClientRect(s_textArea, &rect))
8383 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8384 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008385
8386 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8387 pti->rect.left = pt.x - 3;
8388 pti->rect.top = pt.y - 3;
8389 pti->rect.right = pt.x + 3;
8390 pti->rect.bottom = pt.y + 3;
8391
8392 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8393 // Make tooltip appear sooner.
8394 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8395 // I've performed some tests and it seems the longest possible life time
8396 // of tooltip is 30 seconds.
8397 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8398 /*
8399 * HACK: force tooltip to appear, because it'll not appear until
8400 * first mouse move. D*mn M$
8401 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8402 */
8403 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8404 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8405 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008406}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008407
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008409delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008411 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412}
8413
8414 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008415beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008416 HWND hwnd UNUSED,
8417 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008418 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008419 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 POINT pt;
8422 RECT rect;
8423
8424 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8425 return;
8426
8427 GetCursorPos(&pt);
8428 if (WindowFromPoint(pt) != s_textArea)
8429 return;
8430
8431 ScreenToClient(s_textArea, &pt);
8432 GetClientRect(s_textArea, &rect);
8433 if (!PtInRect(&rect, pt))
8434 return;
8435
K.Takataa8ec4912022-02-03 14:32:33 +00008436 if (last_user_activity > 0
8437 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 && (cur_beval->showState != ShS_PENDING
8439 || abs(cur_beval->x - pt.x) > 3
8440 || abs(cur_beval->y - pt.y) > 3))
8441 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008442 // Pointer resting in one place long enough, it's time to show
8443 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 cur_beval->showState = ShS_PENDING;
8445 cur_beval->x = pt.x;
8446 cur_beval->y = pt.y;
8447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 if (cur_beval->msgCB != NULL)
8449 (*cur_beval->msgCB)(cur_beval, 0);
8450 }
8451}
8452
8453 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008454gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455{
K.Takataa8ec4912022-02-03 14:32:33 +00008456 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457}
8458
8459 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008460gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 if (beval == NULL)
8463 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008464 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8465 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466}
8467
8468 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008469gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008472
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008473 vim_free(beval->msg);
8474 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8475 if (beval->msg == NULL)
8476 {
8477 delete_tooltip(beval);
8478 beval->showState = ShS_NEUTRAL;
8479 return;
8480 }
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (beval->showState == ShS_SHOWING)
8483 return;
8484 GetCursorPos(&pt);
8485 ScreenToClient(s_textArea, &pt);
8486
8487 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008489 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 gui_mch_disable_beval_area(cur_beval);
8491 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008492 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494}
8495
8496 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008497gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008498 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008499 char_u *mesg,
8500 void (*mesgCB)(BalloonEval *, int),
8501 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008503 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 BalloonEval *beval;
8505
8506 if (mesg != NULL && mesgCB != NULL)
8507 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008508 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 return NULL;
8510 }
8511
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008512 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (beval != NULL)
8514 {
8515 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516
8517 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 beval->msg = mesg;
8519 beval->msgCB = mesgCB;
8520 beval->clientData = clientData;
8521
8522 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 cur_beval = beval;
8524
8525 if (p_beval)
8526 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 }
8528 return beval;
8529}
8530
8531 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008532Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008534 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 return;
8536
8537 if (cur_beval != NULL)
8538 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008539 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008541 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008542 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008543 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 delete_tooltip(cur_beval);
8545 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546
8547 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008548 break;
8549 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008550 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008551 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008552 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008553 info->lpszText = (LPSTR)info->lParam;
8554 info->uFlags |= TTF_DI_SETITEM;
8555 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008556 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008557 case TTN_GETDISPINFOW:
8558 {
8559 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008560 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008561 info->lpszText = (LPWSTR)info->lParam;
8562 info->uFlags |= TTF_DI_SETITEM;
8563 }
8564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 }
8567}
8568
8569 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008570track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8573 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008574 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575}
8576
8577 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008578gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008580# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008581 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008582# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008583 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 vim_free(beval);
8585}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008586#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
8588#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8589/*
8590 * We have multiple signs to draw at the same location. Draw the
8591 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8592 */
8593 void
8594netbeans_draw_multisign_indicator(int row)
8595{
8596 int i;
8597 int y;
8598 int x;
8599
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008600 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008601 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 x = 0;
8604 y = TEXT_Y(row);
8605
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008606# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008607 if (IS_ENABLE_DIRECTX())
8608 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008609# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 for (i = 0; i < gui.char_height - 3; i++)
8612 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8613
8614 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8615 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8616 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8617 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8618 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8619 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8620 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8621}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008622#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008623
8624#if defined(FEAT_EVAL) || defined(PROTO)
8625 int
8626test_gui_w32_sendevent(dict_T *args)
8627{
8628 char_u *event;
8629 INPUT inputs[1];
8630
Bram Moolenaard61efa52022-07-23 09:52:04 +01008631 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008632 if (event == NULL)
8633 return FALSE;
8634
8635 ZeroMemory(inputs, sizeof(inputs));
8636
8637 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8638 {
8639 WORD vkCode;
8640
Bram Moolenaard61efa52022-07-23 09:52:04 +01008641 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008642 if (vkCode <= 0 || vkCode >= 0xFF)
8643 {
8644 semsg(_(e_invalid_argument_nr), (long)vkCode);
8645 return FALSE;
8646 }
8647
8648 inputs[0].type = INPUT_KEYBOARD;
8649 inputs[0].ki.wVk = vkCode;
8650 if (STRICMP(event, "keyup") == 0)
8651 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
8652 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8653 }
8654 else
8655 semsg(_(e_invalid_argument_str), event);
8656
8657 vim_free(event);
8658
8659 return TRUE;
8660}
8661#endif