blob: 397dd7f9028cd2d60ecd4727c5323dd9a86f6bcd [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
LemonBoy202b4bd2022-04-28 19:50:54 +01002050 && (vk != VK_SPACE || !(GetKeyState(VK_LMENU) & 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 Sharonov3f026672022-07-26 21:26:18 +01002058 if (dead_key == DEAD_KEY_SET_DEFAULT && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059 || vk == VK_TAB || vk == CAR))
2060 {
2061 outputDeadKey_rePost(msg);
2062 return;
2063 }
2064
2065#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002066 // Check for <F10>: Windows selects the menu. When <F10> is
2067 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068 if (vk == VK_F10
2069 && gui.menu_is_active
2070 && check_map(k10, State, FALSE, TRUE, FALSE,
2071 NULL, NULL) == NULL)
2072 break;
2073#endif
LemonBoy45684c62022-04-24 15:46:42 +01002074 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002075
2076 if (special_keys[i].vim_code1 == NUL)
2077 key = special_keys[i].vim_code0;
2078 else
2079 key = TO_SPECIAL(special_keys[i].vim_code0,
2080 special_keys[i].vim_code1);
2081 key = simplify_key(key, &modifiers);
2082 if (key == CSI)
2083 key = K_CSI;
2084
2085 if (modifiers)
2086 {
2087 string[0] = CSI;
2088 string[1] = KS_MODIFIER;
2089 string[2] = modifiers;
2090 add_to_input_buf(string, 3);
2091 }
2092
2093 if (IS_SPECIAL(key))
2094 {
2095 string[0] = CSI;
2096 string[1] = K_SECOND(key);
2097 string[2] = K_THIRD(key);
2098 add_to_input_buf(string, 3);
2099 }
2100 else
2101 {
2102 int len;
2103
Bram Moolenaar734a8672019-12-02 22:49:38 +01002104 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002105 len = char_to_string(key, string, 40, FALSE);
2106 add_to_input_buf(string, len);
2107 }
2108 break;
2109 }
2110 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002111
2112 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 if (special_keys[i].key_sym == 0)
2114 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002115 WCHAR ch[8];
2116 int len;
2117 int i;
2118 UINT scan_code;
2119
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002120 // Construct the state table with only a few modifiers, we don't
2121 // really care about the presence of Ctrl/Alt as those modifiers are
2122 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002123 memset(keyboard_state, 0, 256);
2124 if (GetKeyState(VK_SHIFT) & 0x8000)
2125 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002126 if (GetKeyState(VK_CAPITAL) & 0x0001)
2127 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002128 // Alt-Gr is synthesized as Alt + Ctrl.
2129 if ((GetKeyState(VK_RMENU) & 0x8000)
2130 && (GetKeyState(VK_CONTROL) & 0x8000))
2131 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002132 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002133 keyboard_state[VK_CONTROL] = 0x80;
2134 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002135
2136 // Translate the virtual key according to the current keyboard
2137 // layout.
2138 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2139 // Convert the scan-code into a sequence of zero or more unicode
2140 // codepoints.
2141 // If this is a dead key ToUnicode returns a negative value.
2142 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2143 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002144 if (len < 0)
2145 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002146
2147 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002148 {
2149 if ( dead_key == DEAD_KEY_SET_DEFAULT
2150 && (GetKeyState(VK_CONTROL) & 0x8000)
2151 && ( (vk == 221 && scan_code == 26) // AZERTY CTRL+dead_circumflex
2152 || (vk == 220 && scan_code == 41) // QWERTZ CTRL+dead_circumflex
2153 )
2154 )
2155 {
2156 // post WM_CHAR='[' - which will be interpreted with CTRL
2157 // stil hold as ESC
2158 PostMessageW(msg.hwnd, WM_CHAR, '[', msg.lParam);
2159 // ask _OnChar() to not touch this state, wait for next key
2160 // press and maintain knowledge that we are "poisoned" with
2161 // "dead state"
2162 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2163 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002164 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002165 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002166
2167 // Post the message as TranslateMessage would do.
2168 if (msg.message == WM_KEYDOWN)
2169 {
2170 for (i = 0; i < len; i++)
2171 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002172 }
2173 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002174 {
2175 for (i = 0; i < len; i++)
2176 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2177 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002178 }
2179 }
2180#ifdef FEAT_MBYTE_IME
2181 else if (msg.message == WM_IME_NOTIFY)
2182 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2183 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002184 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002185 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186#endif
2187
2188#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002189 // Check for <F10>: Default effect is to select the menu. When <F10> is
2190 // mapped we need to stop it here to avoid strange effects (e.g., for the
2191 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002192 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2193 NULL, NULL) == NULL)
2194#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002195 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002196}
2197
2198/*
2199 * Catch up with any queued events. This may put keyboard input into the
2200 * input buffer, call resize call-backs, trigger timers etc. If there is
2201 * nothing in the event queue (& no timers pending), then we return
2202 * immediately.
2203 */
2204 void
2205gui_mch_update(void)
2206{
2207 MSG msg;
2208
2209 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002210 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002211 && !vim_is_input_buf_full())
2212 process_message();
2213}
2214
Bram Moolenaar4231da42016-06-02 14:30:04 +02002215 static void
2216remove_any_timer(void)
2217{
2218 MSG msg;
2219
2220 if (s_wait_timer != 0 && !s_timed_out)
2221 {
2222 KillTimer(NULL, s_wait_timer);
2223
Bram Moolenaar734a8672019-12-02 22:49:38 +01002224 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002225 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002226 ;
2227 s_wait_timer = 0;
2228 }
2229}
2230
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002231/*
2232 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2233 * from the keyboard.
2234 * wtime == -1 Wait forever.
2235 * wtime == 0 This should never happen.
2236 * wtime > 0 Wait wtime milliseconds for a character.
2237 * Returns OK if a character was found to be available within the given time,
2238 * or FAIL otherwise.
2239 */
2240 int
2241gui_mch_wait_for_chars(int wtime)
2242{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002243 int focus;
2244
2245 s_timed_out = FALSE;
2246
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002247 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002248 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002249 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002250 if (s_busy_processing)
2251 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002252
2253 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002254 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2255 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002256 }
2257
2258 allow_scrollbar = TRUE;
2259
2260 focus = gui.in_focus;
2261 while (!s_timed_out)
2262 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002263 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002264 if (gui.in_focus != focus)
2265 {
2266 if (gui.in_focus)
2267 gui_mch_start_blink();
2268 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002269 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002270 focus = gui.in_focus;
2271 }
2272
2273 if (s_need_activate)
2274 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002276 s_need_activate = FALSE;
2277 }
2278
Bram Moolenaar4231da42016-06-02 14:30:04 +02002279#ifdef FEAT_TIMERS
2280 did_add_timer = FALSE;
2281#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002282#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002283 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002284 for (;;)
2285 {
2286 MSG msg;
2287
2288 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002289# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002290 if (did_add_timer)
2291 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002292# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002293 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002294 {
2295 process_message();
2296 break;
2297 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002298 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002299 // TODO: The 10 msec is a compromise between laggy response
2300 // and consuming more CPU time. Better would be to handle
2301 // channel messages when they arrive.
2302 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002303 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002304 break;
2305 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002306#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002307 // Don't use gui_mch_update() because then we will spin-lock until a
2308 // char arrives, instead we use GetMessage() to hang until an
2309 // event arrives. No need to check for input_buf_full because we are
2310 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002311 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002312#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002313
2314 if (input_available())
2315 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002316 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002317 allow_scrollbar = FALSE;
2318
Bram Moolenaar89c00032019-09-04 13:53:21 +02002319 // Clear pending mouse button, the release event may have been
2320 // taken by the dialog window. But don't do this when getting
2321 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002322 if (!s_getting_focus)
2323 s_button_pending = -1;
2324
2325 return OK;
2326 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002327
2328#ifdef FEAT_TIMERS
2329 if (did_add_timer)
2330 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002331 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002332 remove_any_timer();
2333 break;
2334 }
2335#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002336 }
2337 allow_scrollbar = FALSE;
2338 return FAIL;
2339}
2340
2341/*
2342 * Clear a rectangular region of the screen from text pos (row1, col1) to
2343 * (row2, col2) inclusive.
2344 */
2345 void
2346gui_mch_clear_block(
2347 int row1,
2348 int col1,
2349 int row2,
2350 int col2)
2351{
2352 RECT rc;
2353
2354 /*
2355 * Clear one extra pixel at the far right, for when bold characters have
2356 * spilled over to the window border.
2357 * Note: FillRect() excludes right and bottom of rectangle.
2358 */
2359 rc.left = FILL_X(col1);
2360 rc.top = FILL_Y(row1);
2361 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2362 rc.bottom = FILL_Y(row2 + 1);
2363 clear_rect(&rc);
2364}
2365
2366/*
2367 * Clear the whole text window.
2368 */
2369 void
2370gui_mch_clear_all(void)
2371{
2372 RECT rc;
2373
2374 rc.left = 0;
2375 rc.top = 0;
2376 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2377 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2378 clear_rect(&rc);
2379}
2380/*
2381 * Menu stuff.
2382 */
2383
2384 void
2385gui_mch_enable_menu(int flag)
2386{
2387#ifdef FEAT_MENU
2388 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2389#endif
2390}
2391
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002392 void
2393gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002394 int x UNUSED,
2395 int y UNUSED,
2396 int w UNUSED,
2397 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002399 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400}
2401
2402#if defined(FEAT_MENU) || defined(PROTO)
2403/*
2404 * Make menu item hidden or not hidden
2405 */
2406 void
2407gui_mch_menu_hidden(
2408 vimmenu_T *menu,
2409 int hidden)
2410{
2411 /*
2412 * This doesn't do what we want. Hmm, just grey the menu items for now.
2413 */
2414 /*
2415 if (hidden)
2416 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2417 else
2418 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2419 */
2420 gui_mch_menu_grey(menu, hidden);
2421}
2422
2423/*
2424 * This is called after setting all the menus to grey/hidden or not.
2425 */
2426 void
2427gui_mch_draw_menubar(void)
2428{
2429 DrawMenuBar(s_hwnd);
2430}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002431#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433/*
2434 * Return the RGB value of a pixel as a long.
2435 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002436 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002437gui_mch_get_rgb(guicolor_T pixel)
2438{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002439 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2440 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002441}
2442
2443#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002444/*
2445 * Convert pixels in X to dialog units
2446 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002447 static WORD
2448PixelToDialogX(int numPixels)
2449{
2450 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2451}
2452
Bram Moolenaar734a8672019-12-02 22:49:38 +01002453/*
2454 * Convert pixels in Y to dialog units
2455 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002456 static WORD
2457PixelToDialogY(int numPixels)
2458{
2459 return (WORD)((numPixels * 8) / s_dlgfntheight);
2460}
2461
Bram Moolenaar734a8672019-12-02 22:49:38 +01002462/*
2463 * Return the width in pixels of the given text in the given DC.
2464 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002465 static int
2466GetTextWidth(HDC hdc, char_u *str, int len)
2467{
2468 SIZE size;
2469
2470 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2471 return size.cx;
2472}
2473
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002474/*
2475 * Return the width in pixels of the given text in the given DC, taking care
2476 * of 'encoding' to active codepage conversion.
2477 */
2478 static int
2479GetTextWidthEnc(HDC hdc, char_u *str, int len)
2480{
2481 SIZE size;
2482 WCHAR *wstr;
2483 int n;
2484 int wlen = len;
2485
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002486 wstr = enc_to_utf16(str, &wlen);
2487 if (wstr == NULL)
2488 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002489
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002490 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2491 vim_free(wstr);
2492 if (n)
2493 return size.cx;
2494 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002495}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002497static void get_work_area(RECT *spi_rect);
2498
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002499/*
2500 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002501 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2502 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503 */
2504 static BOOL
2505CenterWindow(
2506 HWND hwndChild,
2507 HWND hwndParent)
2508{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002509 HMONITOR mon;
2510 MONITORINFO moninfo;
2511 RECT rChild, rParent, rScreen;
2512 int wChild, hChild, wParent, hParent;
2513 int xNew, yNew;
2514 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002515
2516 GetWindowRect(hwndChild, &rChild);
2517 wChild = rChild.right - rChild.left;
2518 hChild = rChild.bottom - rChild.top;
2519
Bram Moolenaar734a8672019-12-02 22:49:38 +01002520 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002521 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002522 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002523 else
2524 GetWindowRect(hwndParent, &rParent);
2525 wParent = rParent.right - rParent.left;
2526 hParent = rParent.bottom - rParent.top;
2527
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002528 moninfo.cbSize = sizeof(MONITORINFO);
2529 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2530 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002531 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002532 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002533 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002534 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002535 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002536 hdc = GetDC(hwndChild);
2537 rScreen.left = 0;
2538 rScreen.top = 0;
2539 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2540 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2541 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002542 }
2543
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002544 xNew = rParent.left + ((wParent - wChild) / 2);
2545 if (xNew < rScreen.left)
2546 xNew = rScreen.left;
2547 else if ((xNew + wChild) > rScreen.right)
2548 xNew = rScreen.right - wChild;
2549
2550 yNew = rParent.top + ((hParent - hChild) / 2);
2551 if (yNew < rScreen.top)
2552 yNew = rScreen.top;
2553 else if ((yNew + hChild) > rScreen.bottom)
2554 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002555
2556 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2557 SWP_NOSIZE | SWP_NOZORDER);
2558}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002559#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002560
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561#if defined(FEAT_TOOLBAR) || defined(PROTO)
2562 void
2563gui_mch_show_toolbar(int showit)
2564{
2565 if (s_toolbarhwnd == NULL)
2566 return;
2567
2568 if (showit)
2569 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002570 // Enable unicode support
2571 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2572 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002573 ShowWindow(s_toolbarhwnd, SW_SHOW);
2574 }
2575 else
2576 ShowWindow(s_toolbarhwnd, SW_HIDE);
2577}
2578
Bram Moolenaar734a8672019-12-02 22:49:38 +01002579// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002580# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002581
2582#endif
2583
2584#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2585 static void
2586add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2587{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002588 WCHAR *wn;
2589 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002590
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002591 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002592 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002593 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002595 infow.cbSize = sizeof(infow);
2596 infow.fMask = MIIM_TYPE | MIIM_ID;
2597 infow.wID = item_id;
2598 infow.fType = MFT_STRING;
2599 infow.dwTypeData = wn;
2600 infow.cch = (UINT)wcslen(wn);
2601 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2602 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002603}
2604
2605 static void
2606show_tabline_popup_menu(void)
2607{
2608 HMENU tab_pmenu;
2609 long rval;
2610 POINT pt;
2611
Bram Moolenaar734a8672019-12-02 22:49:38 +01002612 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 if (hold_gui_events
2614# ifdef FEAT_CMDWIN
2615 || cmdwin_type != 0
2616# endif
2617 )
2618 return;
2619
2620 tab_pmenu = CreatePopupMenu();
2621 if (tab_pmenu == NULL)
2622 return;
2623
2624 if (first_tabpage->tp_next != NULL)
2625 add_tabline_popup_menu_entry(tab_pmenu,
2626 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2627 add_tabline_popup_menu_entry(tab_pmenu,
2628 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2629 add_tabline_popup_menu_entry(tab_pmenu,
2630 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2631
2632 GetCursorPos(&pt);
2633 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2634 NULL);
2635
2636 DestroyMenu(tab_pmenu);
2637
Bram Moolenaar734a8672019-12-02 22:49:38 +01002638 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002639 if (rval > 0)
2640 {
2641 TCHITTESTINFO htinfo;
2642 int idx;
2643
2644 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2645 return;
2646
2647 htinfo.pt.x = pt.x;
2648 htinfo.pt.y = pt.y;
2649 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2650 if (idx == -1)
2651 idx = 0;
2652 else
2653 idx += 1;
2654
2655 send_tabline_menu_event(idx, (int)rval);
2656 }
2657}
2658
2659/*
2660 * Show or hide the tabline.
2661 */
2662 void
2663gui_mch_show_tabline(int showit)
2664{
2665 if (s_tabhwnd == NULL)
2666 return;
2667
2668 if (!showit != !showing_tabline)
2669 {
2670 if (showit)
2671 ShowWindow(s_tabhwnd, SW_SHOW);
2672 else
2673 ShowWindow(s_tabhwnd, SW_HIDE);
2674 showing_tabline = showit;
2675 }
2676}
2677
2678/*
2679 * Return TRUE when tabline is displayed.
2680 */
2681 int
2682gui_mch_showing_tabline(void)
2683{
2684 return s_tabhwnd != NULL && showing_tabline;
2685}
2686
2687/*
2688 * Update the labels of the tabline.
2689 */
2690 void
2691gui_mch_update_tabline(void)
2692{
2693 tabpage_T *tp;
2694 TCITEM tie;
2695 int nr = 0;
2696 int curtabidx = 0;
2697 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002698 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699
2700 if (s_tabhwnd == NULL)
2701 return;
2702
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002703 // Enable unicode support
2704 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002705
2706 tie.mask = TCIF_TEXT;
2707 tie.iImage = -1;
2708
Bram Moolenaar734a8672019-12-02 22:49:38 +01002709 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002710 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2711
Bram Moolenaar734a8672019-12-02 22:49:38 +01002712 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002713 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2714 {
2715 if (tp == curtab)
2716 curtabidx = nr;
2717
2718 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2719 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002720 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 tie.pszText = "-Empty-";
2722 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2723 tabadded = 1;
2724 }
2725
2726 get_tabline_label(tp, FALSE);
2727 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002728
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002729 wstr = enc_to_utf16(NameBuff, NULL);
2730 if (wstr != NULL)
2731 {
2732 TCITEMW tiw;
2733
2734 tiw.mask = TCIF_TEXT;
2735 tiw.iImage = -1;
2736 tiw.pszText = wstr;
2737 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2738 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 }
2741
Bram Moolenaar734a8672019-12-02 22:49:38 +01002742 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2744 TabCtrl_DeleteItem(s_tabhwnd, nr);
2745
2746 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2747 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2748
Bram Moolenaar734a8672019-12-02 22:49:38 +01002749 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002750 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2751 RedrawWindow(s_tabhwnd, NULL, NULL,
2752 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2753
2754 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2755 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2756}
2757
2758/*
2759 * Set the current tab to "nr". First tab is 1.
2760 */
2761 void
2762gui_mch_set_curtab(int nr)
2763{
2764 if (s_tabhwnd == NULL)
2765 return;
2766
2767 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2768 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2769}
2770
2771#endif
2772
2773/*
2774 * ":simalt" command.
2775 */
2776 void
2777ex_simalt(exarg_T *eap)
2778{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002779 char_u *keys = eap->arg;
2780 int fill_typebuf = FALSE;
2781 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782
2783 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2784 while (*keys)
2785 {
2786 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002787 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002788 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2789 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002790 fill_typebuf = TRUE;
2791 }
2792 if (fill_typebuf)
2793 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002794 // Put a NOP in the typeahead buffer so that the message will get
2795 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002796 key_name[0] = K_SPECIAL;
2797 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002798 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002799 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002800#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002801 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002802#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002803 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002804 }
2805}
2806
2807/*
2808 * Create the find & replace dialogs.
2809 * You can't have both at once: ":find" when replace is showing, destroys
2810 * the replace dialog first, and the other way around.
2811 */
2812#ifdef MSWIN_FIND_REPLACE
2813 static void
2814initialise_findrep(char_u *initial_string)
2815{
2816 int wword = FALSE;
2817 int mcase = !p_ic;
2818 char_u *entry_text;
2819
Bram Moolenaar734a8672019-12-02 22:49:38 +01002820 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002821 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2822
2823 s_findrep_struct.hwndOwner = s_hwnd;
2824 s_findrep_struct.Flags = FR_DOWN;
2825 if (mcase)
2826 s_findrep_struct.Flags |= FR_MATCHCASE;
2827 if (wword)
2828 s_findrep_struct.Flags |= FR_WHOLEWORD;
2829 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002830 {
2831 WCHAR *p = enc_to_utf16(entry_text, NULL);
2832 if (p != NULL)
2833 {
2834 int len = s_findrep_struct.wFindWhatLen - 1;
2835
2836 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2837 s_findrep_struct.lpstrFindWhat[len] = NUL;
2838 vim_free(p);
2839 }
2840 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002841 vim_free(entry_text);
2842}
2843#endif
2844
2845 static void
2846set_window_title(HWND hwnd, char *title)
2847{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002848 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002849 {
2850 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851
Bram Moolenaar734a8672019-12-02 22:49:38 +01002852 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002853 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2854 if (wbuf != NULL)
2855 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002856 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002857 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 }
2859 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002860 else
2861 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862}
2863
2864 void
2865gui_mch_find_dialog(exarg_T *eap)
2866{
2867#ifdef MSWIN_FIND_REPLACE
2868 if (s_findrep_msg != 0)
2869 {
2870 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2871 DestroyWindow(s_findrep_hwnd);
2872
2873 if (!IsWindow(s_findrep_hwnd))
2874 {
2875 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002876 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877 }
2878
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002879 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880 (void)SetFocus(s_findrep_hwnd);
2881
2882 s_findrep_is_find = TRUE;
2883 }
2884#endif
2885}
2886
2887
2888 void
2889gui_mch_replace_dialog(exarg_T *eap)
2890{
2891#ifdef MSWIN_FIND_REPLACE
2892 if (s_findrep_msg != 0)
2893 {
2894 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2895 DestroyWindow(s_findrep_hwnd);
2896
2897 if (!IsWindow(s_findrep_hwnd))
2898 {
2899 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002900 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 }
2902
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002903 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 (void)SetFocus(s_findrep_hwnd);
2905
2906 s_findrep_is_find = FALSE;
2907 }
2908#endif
2909}
2910
2911
2912/*
2913 * Set visibility of the pointer.
2914 */
2915 void
2916gui_mch_mousehide(int hide)
2917{
2918 if (hide != gui.pointer_hidden)
2919 {
2920 ShowCursor(!hide);
2921 gui.pointer_hidden = hide;
2922 }
2923}
2924
2925#ifdef FEAT_MENU
2926 static void
2927gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2928{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002929 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002930 gui_mch_mousehide(FALSE);
2931
2932 (void)TrackPopupMenu(
2933 (HMENU)menu->submenu_id,
2934 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2935 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002936 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002937 s_hwnd,
2938 NULL);
2939 /*
2940 * NOTE: The pop-up menu can eat the mouse up event.
2941 * We deal with this in normal.c.
2942 */
2943}
2944#endif
2945
2946/*
2947 * Got a message when the system will go down.
2948 */
2949 static void
2950_OnEndSession(void)
2951{
2952 getout_preserve_modified(1);
2953}
2954
2955/*
2956 * Get this message when the user clicks on the cross in the top right corner
2957 * of a Windows95 window.
2958 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002959 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002960_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002961{
2962 gui_shell_closed();
2963}
2964
2965/*
2966 * Get a message when the window is being destroyed.
2967 */
2968 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002969_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002970{
2971 if (!destroying)
2972 _OnClose(hwnd);
2973}
2974
2975 static void
2976_OnPaint(
2977 HWND hwnd)
2978{
2979 if (!IsMinimized(hwnd))
2980 {
2981 PAINTSTRUCT ps;
2982
Bram Moolenaar734a8672019-12-02 22:49:38 +01002983 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985
Bram Moolenaar734a8672019-12-02 22:49:38 +01002986 // prevent multi-byte characters from misprinting on an invalid
2987 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002988 if (has_mbyte)
2989 {
2990 RECT rect;
2991
2992 GetClientRect(hwnd, &rect);
2993 ps.rcPaint.left = rect.left;
2994 ps.rcPaint.right = rect.right;
2995 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002996
2997 if (!IsRectEmpty(&ps.rcPaint))
2998 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002999 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3000 ps.rcPaint.right - ps.rcPaint.left + 1,
3001 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3002 }
3003
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003004 EndPaint(hwnd, &ps);
3005 }
3006}
3007
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003008 static void
3009_OnSize(
3010 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003011 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003012 int cx,
3013 int cy)
3014{
K.Takatac81e9bf2022-01-16 14:15:49 +00003015 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 {
3017 gui_resize_shell(cx, cy);
3018
Bram Moolenaar734a8672019-12-02 22:49:38 +01003019 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003020 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021 }
3022}
3023
3024 static void
3025_OnSetFocus(
3026 HWND hwnd,
3027 HWND hwndOldFocus)
3028{
3029 gui_focus_change(TRUE);
3030 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003031 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032}
3033
3034 static void
3035_OnKillFocus(
3036 HWND hwnd,
3037 HWND hwndNewFocus)
3038{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003039 if (destroying)
3040 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003041 gui_focus_change(FALSE);
3042 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003043 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044}
3045
3046/*
3047 * Get a message when the user switches back to vim
3048 */
3049 static LRESULT
3050_OnActivateApp(
3051 HWND hwnd,
3052 BOOL fActivate,
3053 DWORD dwThreadId)
3054{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003055 // we call gui_focus_change() in _OnSetFocus()
3056 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003057 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003058}
3059
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 void
3061gui_mch_destroy_scrollbar(scrollbar_T *sb)
3062{
3063 DestroyWindow(sb->id);
3064}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003065
3066/*
3067 * Get current mouse coordinates in text window.
3068 */
3069 void
3070gui_mch_getmouse(int *x, int *y)
3071{
3072 RECT rct;
3073 POINT mp;
3074
3075 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003076 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003077 *x = (int)(mp.x - rct.left);
3078 *y = (int)(mp.y - rct.top);
3079}
3080
3081/*
3082 * Move mouse pointer to character at (x, y).
3083 */
3084 void
3085gui_mch_setmouse(int x, int y)
3086{
3087 RECT rct;
3088
3089 (void)GetWindowRect(s_textArea, &rct);
3090 (void)SetCursorPos(x + gui.border_offset + rct.left,
3091 y + gui.border_offset + rct.top);
3092}
3093
3094 static void
3095gui_mswin_get_valid_dimensions(
3096 int w,
3097 int h,
3098 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003099 int *valid_h,
3100 int *cols,
3101 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003102{
3103 int base_width, base_height;
3104
3105 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003106 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3107 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003108 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003109 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3110 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3111 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3112 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003113 *cols = (w - base_width) / gui.char_width;
3114 *rows = (h - base_height) / gui.char_height;
3115 *valid_w = base_width + *cols * gui.char_width;
3116 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003117}
3118
3119 void
3120gui_mch_flash(int msec)
3121{
3122 RECT rc;
3123
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003124#if defined(FEAT_DIRECTX)
3125 if (IS_ENABLE_DIRECTX())
3126 DWriteContext_Flush(s_dwc);
3127#endif
3128
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003129 /*
3130 * Note: InvertRect() excludes right and bottom of rectangle.
3131 */
3132 rc.left = 0;
3133 rc.top = 0;
3134 rc.right = gui.num_cols * gui.char_width;
3135 rc.bottom = gui.num_rows * gui.char_height;
3136 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003137 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003138
Bram Moolenaar734a8672019-12-02 22:49:38 +01003139 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140
3141 InvertRect(s_hdc, &rc);
3142}
3143
3144/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003145 * Check if the specified point is on-screen. (multi-monitor aware)
3146 */
3147 static BOOL
3148is_point_onscreen(int x, int y)
3149{
3150 POINT pt = {x, y};
3151
3152 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3153}
3154
3155/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003156 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003157 *
3158 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3159 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3160 * only works when the whole window is on-screen.
3161 */
3162 static BOOL
3163is_window_onscreen(HWND hwnd)
3164{
3165 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003166 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003167
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003168 GetClientRect(hwnd, &rc);
3169 p1.x = rc.left;
3170 p1.y = rc.top;
3171 p2.x = rc.right - 1;
3172 p2.y = rc.bottom - 1;
3173 ClientToScreen(hwnd, &p1);
3174 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003175
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003176 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003177 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003178 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003179 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003180 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003181 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003182 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003183 return FALSE;
3184 return TRUE;
3185}
3186
3187/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188 * Return flags used for scrolling.
3189 * The SW_INVALIDATE is required when part of the window is covered or
3190 * off-screen. Refer to MS KB Q75236.
3191 */
3192 static int
3193get_scroll_flags(void)
3194{
3195 HWND hwnd;
3196 RECT rcVim, rcOther, rcDest;
3197
Bram Moolenaar185577e2020-10-29 20:08:21 +01003198 // Check if the window is (partly) off-screen.
3199 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200 return SW_INVALIDATE;
3201
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003202 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003203 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3205 if (IsWindowVisible(hwnd))
3206 {
3207 GetWindowRect(hwnd, &rcOther);
3208 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3209 return SW_INVALIDATE;
3210 }
3211 return 0;
3212}
3213
3214/*
3215 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3216 * may not be scrolled out properly.
3217 * For gVim, when _OnScroll() is repeated, the character at the
3218 * previous cursor position may be left drawn after scroll.
3219 * The problem can be avoided by calling GetPixel() to get a pixel in
3220 * the region before ScrollWindowEx().
3221 */
3222 static void
3223intel_gpu_workaround(void)
3224{
3225 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3226}
3227
3228/*
3229 * Delete the given number of lines from the given row, scrolling up any
3230 * text further down within the scroll region.
3231 */
3232 void
3233gui_mch_delete_lines(
3234 int row,
3235 int num_lines)
3236{
3237 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003238
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 rc.left = FILL_X(gui.scroll_region_left);
3240 rc.right = FILL_X(gui.scroll_region_right + 1);
3241 rc.top = FILL_Y(row);
3242 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3243
Bram Moolenaar92467d32017-12-05 13:22:16 +01003244#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003245 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003246 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003247 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003248 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003249 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003250#endif
3251 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003252#if defined(FEAT_DIRECTX)
3253 if (IS_ENABLE_DIRECTX())
3254 DWriteContext_Flush(s_dwc);
3255#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003256 intel_gpu_workaround();
3257 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003258 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003259 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003260 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261
Bram Moolenaar734a8672019-12-02 22:49:38 +01003262 // This seems to be required to avoid the cursor disappearing when
3263 // scrolling such that the cursor ends up in the top-left character on
3264 // the screen... But why? (Webb)
3265 // It's probably fixed by disabling drawing the cursor while scrolling.
3266 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267
3268 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3269 gui.scroll_region_left,
3270 gui.scroll_region_bot, gui.scroll_region_right);
3271}
3272
3273/*
3274 * Insert the given number of lines before the given row, scrolling down any
3275 * following text within the scroll region.
3276 */
3277 void
3278gui_mch_insert_lines(
3279 int row,
3280 int num_lines)
3281{
3282 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003283
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284 rc.left = FILL_X(gui.scroll_region_left);
3285 rc.right = FILL_X(gui.scroll_region_right + 1);
3286 rc.top = FILL_Y(row);
3287 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003288
3289#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003290 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003291 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003292 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003293 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003294 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003295#endif
3296 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003297#if defined(FEAT_DIRECTX)
3298 if (IS_ENABLE_DIRECTX())
3299 DWriteContext_Flush(s_dwc);
3300#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003301 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003302 // The SW_INVALIDATE is required when part of the window is covered or
3303 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003304 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003305 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003306 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003307 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309 gui_clear_block(row, gui.scroll_region_left,
3310 row + num_lines - 1, gui.scroll_region_right);
3311}
3312
3313
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003314 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003315gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003316{
3317#if defined(FEAT_DIRECTX)
3318 DWriteContext_Close(s_dwc);
3319 DWrite_Final();
3320 s_dwc = NULL;
3321#endif
3322
3323 ReleaseDC(s_textArea, s_hdc);
3324 DeleteObject(s_brush);
3325
3326#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003327 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3329#endif
3330
Bram Moolenaar734a8672019-12-02 22:49:38 +01003331 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 if (s_hwnd != NULL)
3333 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003334 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003335 DestroyWindow(s_hwnd);
3336 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337}
3338
3339 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003340logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341{
3342 char *p;
3343 char *res;
3344 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003345 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003346 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003347 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003348
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003349 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3350 if (font_name == NULL)
3351 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003353 quality_name = quality_id2name((int)lf.lfQuality);
3354
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003355 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003356 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003357 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003358 if (res != NULL)
3359 {
3360 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003361 // make a normal font string out of the lf thing:
3362 points = pixels_to_points(
3363 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3364 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3365 sprintf((char *)p, "%s:h%d", font_name, points);
3366 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003367 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003368 while (*p)
3369 {
3370 if (*p == ' ')
3371 *p = '_';
3372 ++p;
3373 }
3374 if (lf.lfItalic)
3375 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003376 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 STRCAT(p, ":b");
3378 if (lf.lfUnderline)
3379 STRCAT(p, ":u");
3380 if (lf.lfStrikeOut)
3381 STRCAT(p, ":s");
3382 if (charset_name != NULL)
3383 {
3384 STRCAT(p, ":c");
3385 STRCAT(p, charset_name);
3386 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003387 if (quality_name != NULL)
3388 {
3389 STRCAT(p, ":q");
3390 STRCAT(p, quality_name);
3391 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392 }
3393
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003394 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 return (char_u *)res;
3396}
3397
3398
3399#ifdef FEAT_MBYTE_IME
3400/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003401 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003402 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 */
3404 static void
3405update_im_font(void)
3406{
K.Takatac81e9bf2022-01-16 14:15:49 +00003407 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003408
3409 if (p_guifontwide != NULL && *p_guifontwide != NUL
3410 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003411 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412 norm_logfont = lf_wide;
3413 else
3414 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003415
3416 lf = norm_logfont;
3417 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3418 // Work around when PerMonitorV2 is not enabled in the process level.
3419 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3420 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003421}
3422#endif
3423
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003424/*
3425 * Handler of gui.wide_font (p_guifontwide) changed notification.
3426 */
3427 void
3428gui_mch_wide_font_changed(void)
3429{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003430 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003432#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003434#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435
3436 gui_mch_free_font(gui.wide_ital_font);
3437 gui.wide_ital_font = NOFONT;
3438 gui_mch_free_font(gui.wide_bold_font);
3439 gui.wide_bold_font = NOFONT;
3440 gui_mch_free_font(gui.wide_boldital_font);
3441 gui.wide_boldital_font = NOFONT;
3442
3443 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003444 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003445 {
3446 if (!lf.lfItalic)
3447 {
3448 lf.lfItalic = TRUE;
3449 gui.wide_ital_font = get_font_handle(&lf);
3450 lf.lfItalic = FALSE;
3451 }
3452 if (lf.lfWeight < FW_BOLD)
3453 {
3454 lf.lfWeight = FW_BOLD;
3455 gui.wide_bold_font = get_font_handle(&lf);
3456 if (!lf.lfItalic)
3457 {
3458 lf.lfItalic = TRUE;
3459 gui.wide_boldital_font = get_font_handle(&lf);
3460 }
3461 }
3462 }
3463}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464
3465/*
3466 * Initialise vim to use the font with the given name.
3467 * Return FAIL if the font could not be loaded, OK otherwise.
3468 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003470gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003471{
K.Takatac81e9bf2022-01-16 14:15:49 +00003472 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473 GuiFont font = NOFONT;
3474 char_u *p;
3475
Bram Moolenaar734a8672019-12-02 22:49:38 +01003476 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003477 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003478 {
3479 lfOrig = lf;
3480 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003482 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483 if (font == NOFONT)
3484 return FAIL;
3485
3486 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003487 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003488#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003489 norm_logfont = lf;
3490 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003491 if (!s_in_dpichanged)
3492 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003493#endif
3494 gui_mch_free_font(gui.norm_font);
3495 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003496 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497 GetFontSize(font);
3498
K.Takatac81e9bf2022-01-16 14:15:49 +00003499 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 if (p != NULL)
3501 {
3502 hl_set_font_name(p);
3503
Bram Moolenaar734a8672019-12-02 22:49:38 +01003504 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003505 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3506 {
3507 vim_free(p_guifont);
3508 p_guifont = p;
3509 }
3510 else
3511 vim_free(p);
3512 }
3513
3514 gui_mch_free_font(gui.ital_font);
3515 gui.ital_font = NOFONT;
3516 gui_mch_free_font(gui.bold_font);
3517 gui.bold_font = NOFONT;
3518 gui_mch_free_font(gui.boldital_font);
3519 gui.boldital_font = NOFONT;
3520
3521 if (!lf.lfItalic)
3522 {
3523 lf.lfItalic = TRUE;
3524 gui.ital_font = get_font_handle(&lf);
3525 lf.lfItalic = FALSE;
3526 }
3527 if (lf.lfWeight < FW_BOLD)
3528 {
3529 lf.lfWeight = FW_BOLD;
3530 gui.bold_font = get_font_handle(&lf);
3531 if (!lf.lfItalic)
3532 {
3533 lf.lfItalic = TRUE;
3534 gui.boldital_font = get_font_handle(&lf);
3535 }
3536 }
3537
3538 return OK;
3539}
3540
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541/*
3542 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003543 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003544 */
3545 int
3546gui_mch_maximized(void)
3547{
3548 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003549 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550
3551 wp.length = sizeof(WINDOWPLACEMENT);
3552 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003553 {
3554 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003556 && wp.flags == WPF_RESTORETOMAXIMIZED))
3557 return TRUE;
3558 if (wp.showCmd == SW_SHOWMINIMIZED)
3559 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003560
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003561 // Assume the window is snapped when the sizes from two APIs differ.
3562 GetWindowRect(s_hwnd, &rc);
3563 if ((rc.right - rc.left !=
3564 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3565 || (rc.bottom - rc.top !=
3566 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3567 return TRUE;
3568 }
3569 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003570}
3571
3572/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003573 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3574 * is set. Compute the new Rows and Columns. This is like resizing the
3575 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003576 */
3577 void
3578gui_mch_newfont(void)
3579{
3580 RECT rect;
3581
3582 GetWindowRect(s_hwnd, &rect);
3583 if (win_socket_id == 0)
3584 {
3585 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003586 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3587 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003589 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3590 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3591 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3592 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003593 }
3594 else
3595 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003596 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003597 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003598 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599 }
3600}
3601
3602/*
3603 * Set the window title
3604 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605 void
3606gui_mch_settitle(
3607 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003608 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003609{
3610 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3611}
3612
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003613#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003614// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3615// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616static LPCSTR mshape_idcs[] =
3617{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003618 IDC_ARROW, // arrow
3619 MAKEINTRESOURCE(0), // blank
3620 IDC_IBEAM, // beam
3621 IDC_SIZENS, // updown
3622 IDC_SIZENS, // udsizing
3623 IDC_SIZEWE, // leftright
3624 IDC_SIZEWE, // lrsizing
3625 IDC_WAIT, // busy
3626 IDC_NO, // no
3627 IDC_ARROW, // crosshair
3628 IDC_ARROW, // hand1
3629 IDC_ARROW, // hand2
3630 IDC_ARROW, // pencil
3631 IDC_ARROW, // question
3632 IDC_ARROW, // right-arrow
3633 IDC_UPARROW, // up-arrow
3634 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635};
3636
3637 void
3638mch_set_mouse_shape(int shape)
3639{
3640 LPCSTR idc;
3641
3642 if (shape == MSHAPE_HIDE)
3643 ShowCursor(FALSE);
3644 else
3645 {
3646 if (shape >= MSHAPE_NUMBERED)
3647 idc = IDC_ARROW;
3648 else
3649 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003650 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003651 if (!p_mh)
3652 {
3653 POINT mp;
3654
Bram Moolenaar734a8672019-12-02 22:49:38 +01003655 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003656 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657 (void)SetCursorPos(mp.x, mp.y);
3658 ShowCursor(TRUE);
3659 }
3660 }
3661}
3662#endif
3663
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003664#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003665/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666 * Wide version of convert_filter().
3667 */
3668 static WCHAR *
3669convert_filterW(char_u *s)
3670{
3671 char_u *tmp;
3672 int len;
3673 WCHAR *res;
3674
3675 tmp = convert_filter(s);
3676 if (tmp == NULL)
3677 return NULL;
3678 len = (int)STRLEN(s) + 3;
3679 res = enc_to_utf16(tmp, &len);
3680 vim_free(tmp);
3681 return res;
3682}
3683
3684/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003685 * Pop open a file browser and return the file selected, in allocated memory,
3686 * or NULL if Cancel is hit.
3687 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3688 * title - Title message for the file browser dialog.
3689 * dflt - Default name of file.
3690 * ext - Default extension to be added to files without extensions.
3691 * initdir - directory in which to open the browser (NULL = current dir)
3692 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003693 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003694 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003695gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003696 int saving,
3697 char_u *title,
3698 char_u *dflt,
3699 char_u *ext,
3700 char_u *initdir,
3701 char_u *filter)
3702{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003703 // We always use the wide function. This means enc_to_utf16() must work,
3704 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705 OPENFILENAMEW fileStruct;
3706 WCHAR fileBuf[MAXPATHL];
3707 WCHAR *wp;
3708 int i;
3709 WCHAR *titlep = NULL;
3710 WCHAR *extp = NULL;
3711 WCHAR *initdirp = NULL;
3712 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003713 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003714 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715
3716 if (dflt == NULL)
3717 fileBuf[0] = NUL;
3718 else
3719 {
3720 wp = enc_to_utf16(dflt, NULL);
3721 if (wp == NULL)
3722 fileBuf[0] = NUL;
3723 else
3724 {
3725 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3726 fileBuf[i] = wp[i];
3727 fileBuf[i] = NUL;
3728 vim_free(wp);
3729 }
3730 }
3731
Bram Moolenaar734a8672019-12-02 22:49:38 +01003732 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733 filterp = convert_filterW(filter);
3734
Bram Moolenaara80faa82020-04-12 19:37:17 +02003735 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003736# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003737 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003738 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003739# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003740 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003741# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003742
3743 if (title != NULL)
3744 titlep = enc_to_utf16(title, NULL);
3745 fileStruct.lpstrTitle = titlep;
3746
3747 if (ext != NULL)
3748 extp = enc_to_utf16(ext, NULL);
3749 fileStruct.lpstrDefExt = extp;
3750
3751 fileStruct.lpstrFile = fileBuf;
3752 fileStruct.nMaxFile = MAXPATHL;
3753 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003754 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3755 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756 if (initdir != NULL && *initdir != NUL)
3757 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003758 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759 initdirp = enc_to_utf16(initdir, NULL);
3760 if (initdirp != NULL)
3761 {
3762 for (wp = initdirp; *wp != NUL; ++wp)
3763 if (*wp == '/')
3764 *wp = '\\';
3765 }
3766 fileStruct.lpstrInitialDir = initdirp;
3767 }
3768
3769 /*
3770 * TODO: Allow selection of multiple files. Needs another arg to this
3771 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3772 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3773 * files that don't exist yet, so I haven't put it in. What about
3774 * OFN_PATHMUSTEXIST?
3775 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3776 */
3777 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003778# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 if (curbuf->b_p_bin)
3780 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003781# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003783 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003785 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786
3787 vim_free(filterp);
3788 vim_free(initdirp);
3789 vim_free(titlep);
3790 vim_free(extp);
3791
K.Takata14b8d6a2022-01-20 15:05:22 +00003792 if (!ret)
3793 return NULL;
3794
3795 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003797 if (p == NULL)
3798 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799
Bram Moolenaar734a8672019-12-02 22:49:38 +01003800 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801 SetFocus(s_hwnd);
3802
Bram Moolenaar734a8672019-12-02 22:49:38 +01003803 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003804 q = vim_strsave(shorten_fname1(p));
3805 vim_free(p);
3806 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808
3809
3810/*
3811 * Convert the string s to the proper format for a filter string by replacing
3812 * the \t and \n delimiters with \0.
3813 * Returns the converted string in allocated memory.
3814 *
3815 * Keep in sync with convert_filterW() above!
3816 */
3817 static char_u *
3818convert_filter(char_u *s)
3819{
3820 char_u *res;
3821 unsigned s_len = (unsigned)STRLEN(s);
3822 unsigned i;
3823
3824 res = alloc(s_len + 3);
3825 if (res != NULL)
3826 {
3827 for (i = 0; i < s_len; ++i)
3828 if (s[i] == '\t' || s[i] == '\n')
3829 res[i] = '\0';
3830 else
3831 res[i] = s[i];
3832 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003833 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834 res[s_len + 1] = NUL;
3835 res[s_len + 2] = NUL;
3836 }
3837 return res;
3838}
3839
3840/*
3841 * Select a directory.
3842 */
3843 char_u *
3844gui_mch_browsedir(char_u *title, char_u *initdir)
3845{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003846 // We fake this: Use a filter that doesn't select anything and a default
3847 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003848 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3849 initdir, (char_u *)_("Directory\t*.nothing\n"));
3850}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003851#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853 static void
3854_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003855 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003856 HDROP hDrop)
3857{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003858#define BUFPATHLEN _MAX_PATH
3859#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003860 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 char szFile[BUFPATHLEN];
3862 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3863 UINT i;
3864 char_u **fnames;
3865 POINT pt;
3866 int_u modifiers = 0;
3867
Bram Moolenaar734a8672019-12-02 22:49:38 +01003868 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003869 DragQueryPoint(hDrop, &pt);
3870 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3871
3872 reset_VIsual();
3873
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003874 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875
3876 if (fnames != NULL)
3877 for (i = 0; i < cFiles; ++i)
3878 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003879 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3880 fnames[i] = utf16_to_enc(wszFile, NULL);
3881 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 {
3883 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3884 fnames[i] = vim_strsave((char_u *)szFile);
3885 }
3886 }
3887
3888 DragFinish(hDrop);
3889
3890 if (fnames != NULL)
3891 {
LemonBoy45684c62022-04-24 15:46:42 +01003892 int kbd_modifiers = get_active_modifiers();
3893
3894 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003895 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003896 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003897 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003898 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003899 modifiers |= MOUSE_ALT;
3900
3901 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3902
3903 s_need_activate = TRUE;
3904 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905}
3906
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907 static int
3908_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003909 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910 HWND hwndCtl,
3911 UINT code,
3912 int pos)
3913{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003914 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003915 scrollbar_T *sb, *sb_info;
3916 long val;
3917 int dragging = FALSE;
3918 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919 SCROLLINFO si;
3920
3921 si.cbSize = sizeof(si);
3922 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003923
3924 sb = gui_mswin_find_scrollbar(hwndCtl);
3925 if (sb == NULL)
3926 return 0;
3927
Bram Moolenaar734a8672019-12-02 22:49:38 +01003928 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929 {
3930 /*
3931 * Careful: need to get scrollbar info out of first (left) scrollbar
3932 * for window, but keep real scrollbar too because we must pass it to
3933 * gui_drag_scrollbar().
3934 */
3935 sb_info = &sb->wp->w_scrollbars[0];
3936 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003937 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003938 sb_info = sb;
3939 val = sb_info->value;
3940
3941 switch (code)
3942 {
3943 case SB_THUMBTRACK:
3944 val = pos;
3945 dragging = TRUE;
3946 if (sb->scroll_shift > 0)
3947 val <<= sb->scroll_shift;
3948 break;
3949 case SB_LINEDOWN:
3950 val++;
3951 break;
3952 case SB_LINEUP:
3953 val--;
3954 break;
3955 case SB_PAGEDOWN:
3956 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3957 break;
3958 case SB_PAGEUP:
3959 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3960 break;
3961 case SB_TOP:
3962 val = 0;
3963 break;
3964 case SB_BOTTOM:
3965 val = sb_info->max;
3966 break;
3967 case SB_ENDSCROLL:
3968 if (prev_code == SB_THUMBTRACK)
3969 {
3970 /*
3971 * "pos" only gives us 16-bit data. In case of large file,
3972 * use GetScrollPos() which returns 32-bit. Unfortunately it
3973 * is not valid while the scrollbar is being dragged.
3974 */
3975 val = GetScrollPos(hwndCtl, SB_CTL);
3976 if (sb->scroll_shift > 0)
3977 val <<= sb->scroll_shift;
3978 }
3979 break;
3980
3981 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003982 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003983 return 0;
3984 }
3985 prev_code = code;
3986
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003987 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3988 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003989
3990 /*
3991 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3992 */
3993 if (sb->wp != NULL)
3994 {
3995 scrollbar_T *sba = sb->wp->w_scrollbars;
3996 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3997
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003998 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003999 }
4000
Bram Moolenaar734a8672019-12-02 22:49:38 +01004001 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004002 s_busy_processing = TRUE;
4003
Bram Moolenaar734a8672019-12-02 22:49:38 +01004004 // When "allow_scrollbar" is FALSE still need to remember the new
4005 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004006 dont_scroll = !allow_scrollbar;
4007
Bram Moolenaara338adc2018-01-31 20:51:47 +01004008 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004009 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004010 mch_enable_flush();
4011 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004012
4013 s_busy_processing = FALSE;
4014 dont_scroll = dont_scroll_save;
4015
4016 return 0;
4017}
4018
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_XPM_W32
4021# include "xpm_w32.h"
4022#endif
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
Bram Moolenaar734a8672019-12-02 22:49:38 +01004025// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#define TEAROFF_PADDING_X 2
4027#define TEAROFF_BUTTON_PAD_X 8
4028#define TEAROFF_MIN_WIDTH 200
4029#define TEAROFF_SUBMENU_LABEL ">>"
4030#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4031
4032
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004033#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034# define ID_BEVAL_TOOLTIP 200
4035# define BEVAL_TEXT_LEN MAXPATHL
4036
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004038static UINT_PTR beval_timer_id = 0;
4039static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004040#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004041
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004042
Bram Moolenaar734a8672019-12-02 22:49:38 +01004043// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045#ifdef FEAT_MENU
4046static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
4049/*
4050 * Use the system font for dialogs and tear-off menus. Remove this line to
4051 * use DLG_FONT_NAME.
4052 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004053#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#define VIM_CLASSW L"Vim"
4057
Bram Moolenaar734a8672019-12-02 22:49:38 +01004058// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4059// thus there should be room for every dialog. For tearoffs it's made bigger
4060// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#define DLG_ALLOC_SIZE 16 * 1024
4062
4063/*
4064 * stuff for dialogs, menus, tearoffs etc.
4065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066static PWORD
4067add_dialog_element(
4068 PWORD p,
4069 DWORD lStyle,
4070 WORD x,
4071 WORD y,
4072 WORD w,
4073 WORD h,
4074 WORD Id,
4075 WORD clss,
4076 const char *caption);
4077static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004078static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004079#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082static void get_dialog_font_metrics(void);
4083
4084static int dialog_default_button = -1;
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086#ifdef FEAT_TOOLBAR
4087static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004088static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004089static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004091#else
4092# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#endif
4094
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004095#ifdef FEAT_GUI_TABLINE
4096static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004097static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004098#endif
4099
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#ifdef FEAT_MBYTE_IME
4101static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4102static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4103#endif
4104#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4105# ifdef NOIME
4106typedef struct tagCOMPOSITIONFORM {
4107 DWORD dwStyle;
4108 POINT ptCurrentPos;
4109 RECT rcArea;
4110} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4111typedef HANDLE HIMC;
4112# endif
4113
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004114static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004115static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4116static HIMC (WINAPI *pImmGetContext)(HWND);
4117static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4118static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4119static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4120static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004121static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4122static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004123static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4124static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004125static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126static void dyn_imm_load(void);
4127#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128# define pImmGetCompositionStringW ImmGetCompositionStringW
4129# define pImmGetContext ImmGetContext
4130# define pImmAssociateContext ImmAssociateContext
4131# define pImmReleaseContext ImmReleaseContext
4132# define pImmGetOpenStatus ImmGetOpenStatus
4133# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004134# define pImmGetCompositionFontW ImmGetCompositionFontW
4135# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136# define pImmSetCompositionWindow ImmSetCompositionWindow
4137# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004138# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#endif
4140
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141#ifdef FEAT_MENU
4142/*
4143 * Figure out how high the menu bar is at the moment.
4144 */
4145 static int
4146gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004147 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148{
4149 static int old_menu_height = -1;
4150
4151 RECT rc1, rc2;
4152 int num;
4153 int menu_height;
4154
4155 if (gui.menu_is_active)
4156 num = GetMenuItemCount(s_menuBar);
4157 else
4158 num = 0;
4159
4160 if (num == 0)
4161 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004162 else if (IsMinimized(s_hwnd))
4163 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004164 // The height of the menu cannot be determined while the window is
4165 // minimized. Take the previous height if the menu is changed in that
4166 // state, to avoid that Vim's vertical window size accidentally
4167 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004168 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 else
4171 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004172 /*
4173 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4174 * seem to have been set yet, so menu wraps in default window
4175 * width which is very narrow. Instead just return height of a
4176 * single menu item. Will still be wrong when the menu really
4177 * should wrap over more than one line.
4178 */
4179 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4180 if (gui.starting)
4181 menu_height = rc1.bottom - rc1.top + 1;
4182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004184 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4185 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 }
4188
4189 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004190 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004191 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
4193 return menu_height;
4194}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004195#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197
4198/*
4199 * Setup for the Intellimouse
4200 */
LemonBoyc27747e2022-05-07 12:25:40 +01004201 static long
4202mouse_vertical_scroll_step(void)
4203{
4204 UINT val;
4205 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4206 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4207 return 3; // Safe default;
4208}
4209
4210 static long
4211mouse_horizontal_scroll_step(void)
4212{
4213 UINT val;
4214 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4215 return (long)val;
4216 return 3; // Safe default;
4217}
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 static void
4220init_mouse_wheel(void)
4221{
LemonBoyc27747e2022-05-07 12:25:40 +01004222 // Get the default values for the horizontal and vertical scroll steps from
4223 // the system.
4224 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4225 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226}
4227
Bram Moolenaarae20f342019-11-05 21:09:23 +01004228/*
LemonBoya773d842022-05-10 20:54:46 +01004229 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004232_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
LemonBoy365d8f72022-05-05 19:23:07 +01004234 int button;
4235 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004236 int modifiers = 0;
4237 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004238 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4239 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
Bram Moolenaarae20f342019-11-05 21:09:23 +01004241 wp = gui_mouse_window(FIND_POPUP);
4242
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004243#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004244 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004245 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004246 cmdarg_T cap;
4247 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004248
Bram Moolenaarae20f342019-11-05 21:09:23 +01004249 // Mouse hovers over popup window, scroll it if possible.
4250 mouse_row = wp->w_winrow;
4251 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004252 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004253 if (horizontal)
4254 {
4255 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4256 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4257 }
4258 else
4259 {
4260 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4261 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4262 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004263 clear_oparg(&oa);
4264 cap.oap = &oa;
4265 nv_mousescroll(&cap);
4266 update_screen(0);
4267 setcursor();
4268 out_flush();
4269 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004270 }
4271#endif
4272
Bram Moolenaarae20f342019-11-05 21:09:23 +01004273 if (wp == NULL || !p_scf)
4274 wp = curwin;
4275
LemonBoy365d8f72022-05-05 19:23:07 +01004276 // Translate the scroll event into an event that Vim can process so that
4277 // the user has a chance to map the scrollwheel buttons.
4278 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004279 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 else
LemonBoy365d8f72022-05-05 19:23:07 +01004281 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004282
4283 kbd_modifiers = get_active_modifiers();
4284
4285 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4286 modifiers |= MOUSE_SHIFT;
4287 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4288 modifiers |= MOUSE_CTRL;
4289 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4290 modifiers |= MOUSE_ALT;
4291
LemonBoya773d842022-05-10 20:54:46 +01004292 // The cursor position is relative to the upper-left corner of the screen.
4293 pt.x = GET_X_LPARAM(lParam);
4294 pt.y = GET_Y_LPARAM(lParam);
4295 ScreenToClient(s_textArea, &pt);
4296
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004297 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298}
4299
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004300#ifdef USE_SYSMENU_FONT
4301/*
4302 * Get Menu Font.
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004306gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004307{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004308 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004309
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004310 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4311 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004312 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004313 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004314 &nm,
4315 0))
4316 return FAIL;
4317 *lf = nm.lfMenuFont;
4318 return OK;
4319}
4320#endif
4321
4322
4323#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4324/*
4325 * Set the GUI tabline font to the system menu font
4326 */
4327 static void
4328set_tabline_font(void)
4329{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004330 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004331 HFONT font;
4332 HWND hwnd;
4333 HDC hdc;
4334 HFONT hfntOld;
4335 TEXTMETRIC tm;
4336
4337 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4338 return;
4339
K.Takatac81e9bf2022-01-16 14:15:49 +00004340 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004341 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004342
4343 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4344
4345 /*
4346 * Compute the height of the font used for the tab text
4347 */
4348 hwnd = GetDesktopWindow();
4349 hdc = GetWindowDC(hwnd);
4350 hfntOld = SelectFont(hdc, font);
4351
4352 GetTextMetrics(hdc, &tm);
4353
4354 SelectFont(hdc, hfntOld);
4355 ReleaseDC(hwnd, hdc);
4356
4357 /*
4358 * The space used by the tab border and the space between the tab label
4359 * and the tab border is included as 7.
4360 */
4361 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4362}
K.Takatac81e9bf2022-01-16 14:15:49 +00004363#else
4364# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004365#endif
4366
Bram Moolenaar520470a2005-06-16 21:59:56 +00004367/*
4368 * Invoked when a setting was changed.
4369 */
4370 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004371_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004372{
LemonBoy365d8f72022-05-05 19:23:07 +01004373 switch (param)
4374 {
4375 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004376 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004377 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004378 case SPI_SETWHEELSCROLLCHARS:
4379 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004380 break;
4381 case SPI_SETNONCLIENTMETRICS:
4382 set_tabline_font();
4383 break;
4384 default:
4385 break;
4386 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004387 return 0;
4388}
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_NETBEANS_INTG
4391 static void
4392_OnWindowPosChanged(
4393 HWND hwnd,
4394 const LPWINDOWPOS lpwpos)
4395{
4396 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004397 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4400 || lpwpos->cx != cx || lpwpos->cy != cy))
4401 {
4402 x = lpwpos->x;
4403 y = lpwpos->y;
4404 cx = lpwpos->cx;
4405 cy = lpwpos->cy;
4406 netbeans_frame_moved(x, y);
4407 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004408 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004409 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410}
4411#endif
4412
K.Takata4f2417f2021-06-05 16:25:32 +02004413
4414static HWND hwndTip = NULL;
4415
4416 static void
4417show_sizing_tip(int cols, int rows)
4418{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004419 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004420 char buf[32];
4421
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004422 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004423 ti.hwnd = s_hwnd;
4424 ti.uId = (UINT_PTR)s_hwnd;
4425 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4426 ti.lpszText = buf;
4427 sprintf(buf, "%dx%d", cols, rows);
4428 if (hwndTip == NULL)
4429 {
4430 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4431 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4432 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4433 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4434 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4435 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4436 }
4437 else
4438 {
4439 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4440 }
4441 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4442}
4443
4444 static void
4445destroy_sizing_tip(void)
4446{
4447 if (hwndTip != NULL)
4448 {
4449 DestroyWindow(hwndTip);
4450 hwndTip = NULL;
4451 }
4452}
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 static int
4455_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 UINT fwSide,
4457 LPRECT lprc)
4458{
4459 int w, h;
4460 int valid_w, valid_h;
4461 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004462 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 w = lprc->right - lprc->left;
4465 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004466 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 w_offset = w - valid_w;
4468 h_offset = h - valid_h;
4469
4470 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4471 || fwSide == WMSZ_BOTTOMLEFT)
4472 lprc->left += w_offset;
4473 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4474 || fwSide == WMSZ_BOTTOMRIGHT)
4475 lprc->right -= w_offset;
4476
4477 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4478 || fwSide == WMSZ_TOPRIGHT)
4479 lprc->top += h_offset;
4480 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4481 || fwSide == WMSZ_BOTTOMRIGHT)
4482 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004483
4484 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 return TRUE;
4486}
4487
K.Takata92000e22022-01-20 15:10:57 +00004488#ifdef FEAT_GUI_TABLINE
4489 static void
4490_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4491{
4492 if (gui_mch_showing_tabline())
4493 {
4494 POINT pt;
4495 RECT rect;
4496
4497 /*
4498 * If the cursor is on the tabline, display the tab menu
4499 */
4500 GetCursorPos(&pt);
4501 GetWindowRect(s_textArea, &rect);
4502 if (pt.y < rect.top)
4503 {
4504 show_tabline_popup_menu();
4505 return;
4506 }
4507 }
4508 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4509}
4510
4511 static void
4512_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4513{
4514 /*
4515 * If the user double clicked the tabline, create a new tab
4516 */
4517 if (gui_mch_showing_tabline())
4518 {
4519 POINT pt;
4520 RECT rect;
4521
4522 GetCursorPos(&pt);
4523 GetWindowRect(s_textArea, &rect);
4524 if (pt.y < rect.top)
4525 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4526 }
4527 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4528}
4529#endif
4530
4531 static UINT
4532_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4533{
4534 UINT result;
4535 int x, y;
4536
4537 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4538 if (result != HTCLIENT)
4539 return result;
4540
4541#ifdef FEAT_GUI_TABLINE
4542 if (gui_mch_showing_tabline())
4543 {
4544 RECT rct;
4545
4546 // If the cursor is on the GUI tabline, don't process this event
4547 GetWindowRect(s_textArea, &rct);
4548 if (yPos < rct.top)
4549 return result;
4550 }
4551#endif
4552 (void)gui_mch_get_winpos(&x, &y);
4553 xPos -= x;
4554
4555 if (xPos < 48) // <VN> TODO should use system metric?
4556 return HTBOTTOMLEFT;
4557 else
4558 return HTBOTTOMRIGHT;
4559}
4560
4561#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4562 static LRESULT
4563_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4564{
4565 switch (hdr->code)
4566 {
4567 case TTN_GETDISPINFOW:
4568 case TTN_GETDISPINFO:
4569 {
4570 char_u *str = NULL;
4571 static void *tt_text = NULL;
4572
4573 VIM_CLEAR(tt_text);
4574
4575# ifdef FEAT_GUI_TABLINE
4576 if (gui_mch_showing_tabline()
4577 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4578 {
4579 POINT pt;
4580 /*
4581 * Mouse is over the GUI tabline. Display the
4582 * tooltip for the tab under the cursor
4583 *
4584 * Get the cursor position within the tab control
4585 */
4586 GetCursorPos(&pt);
4587 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4588 {
4589 TCHITTESTINFO htinfo;
4590 int idx;
4591
4592 /*
4593 * Get the tab under the cursor
4594 */
4595 htinfo.pt.x = pt.x;
4596 htinfo.pt.y = pt.y;
4597 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4598 if (idx != -1)
4599 {
4600 tabpage_T *tp;
4601
4602 tp = find_tabpage(idx + 1);
4603 if (tp != NULL)
4604 {
4605 get_tabline_label(tp, TRUE);
4606 str = NameBuff;
4607 }
4608 }
4609 }
4610 }
4611# endif
4612# ifdef FEAT_TOOLBAR
4613# ifdef FEAT_GUI_TABLINE
4614 else
4615# endif
4616 {
4617 UINT idButton;
4618 vimmenu_T *pMenu;
4619
4620 idButton = (UINT) hdr->idFrom;
4621 pMenu = gui_mswin_find_menu(root_menu, idButton);
4622 if (pMenu)
4623 str = pMenu->strings[MENU_INDEX_TIP];
4624 }
4625# endif
4626 if (str == NULL)
4627 break;
4628
4629 // Set the maximum width, this also enables using \n for
4630 // line break.
4631 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4632
4633 if (hdr->code == TTN_GETDISPINFOW)
4634 {
4635 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4636
4637 tt_text = enc_to_utf16(str, NULL);
4638 lpdi->lpszText = tt_text;
4639 // can't show tooltip if failed
4640 }
4641 else
4642 {
4643 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4644
4645 if (STRLEN(str) < sizeof(lpdi->szText)
4646 || ((tt_text = vim_strsave(str)) == NULL))
4647 vim_strncpy((char_u *)lpdi->szText, str,
4648 sizeof(lpdi->szText) - 1);
4649 else
4650 lpdi->lpszText = tt_text;
4651 }
4652 }
4653 break;
4654
4655# ifdef FEAT_GUI_TABLINE
4656 case TCN_SELCHANGE:
4657 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4658 {
4659 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4660 return 0L;
4661 }
4662 break;
4663
4664 case NM_RCLICK:
4665 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4666 {
4667 show_tabline_popup_menu();
4668 return 0L;
4669 }
4670 break;
4671# endif
4672
4673 default:
4674 break;
4675 }
4676 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4677}
4678#endif
4679
4680#if defined(MENUHINTS) && defined(FEAT_MENU)
4681 static LRESULT
4682_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4683{
4684 if (((UINT) HIWORD(wParam)
4685 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4686 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004687 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004688 {
4689 UINT idButton;
4690 vimmenu_T *pMenu;
4691 static int did_menu_tip = FALSE;
4692
4693 if (did_menu_tip)
4694 {
4695 msg_clr_cmdline();
4696 setcursor();
4697 out_flush();
4698 did_menu_tip = FALSE;
4699 }
4700
4701 idButton = (UINT)LOWORD(wParam);
4702 pMenu = gui_mswin_find_menu(root_menu, idButton);
4703 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4704 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4705 {
4706 ++msg_hist_off;
4707 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4708 --msg_hist_off;
4709 setcursor();
4710 out_flush();
4711 did_menu_tip = TRUE;
4712 }
4713 return 0L;
4714 }
4715 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4716}
4717#endif
4718
K.Takatac81e9bf2022-01-16 14:15:49 +00004719 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004720_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004721{
4722 s_dpi = ydpi;
4723 s_in_dpichanged = TRUE;
4724 //TRACE("DPI: %d", ydpi);
4725
4726 update_scrollbar_size();
4727 update_toolbar_size();
4728 set_tabline_font();
4729
4730 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4731 gui_get_wide_font();
4732 gui_mswin_get_menu_height(FALSE);
4733#ifdef FEAT_MBYTE_IME
4734 im_set_position(gui.row, gui.col);
4735#endif
4736 InvalidateRect(hwnd, NULL, TRUE);
4737
4738 s_in_dpichanged = FALSE;
4739 return 0L;
4740}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
4742
4743 static LRESULT CALLBACK
4744_WndProc(
4745 HWND hwnd,
4746 UINT uMsg,
4747 WPARAM wParam,
4748 LPARAM lParam)
4749{
LemonBoya773d842022-05-10 20:54:46 +01004750 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4751 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 HandleMouseHide(uMsg, lParam);
4754
4755 s_uMsg = uMsg;
4756 s_wParam = wParam;
4757 s_lParam = lParam;
4758
4759 switch (uMsg)
4760 {
4761 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4762 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004763 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004765 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4767 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4768 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4769 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4770#ifdef FEAT_MENU
4771 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4772#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004773 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4774 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4776 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004777 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4778 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4780 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4781 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4782#ifdef FEAT_NETBEANS_INTG
4783 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4784#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004785#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004786 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4787 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004788#endif
K.Takata92000e22022-01-20 15:10:57 +00004789 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004790
Bram Moolenaar734a8672019-12-02 22:49:38 +01004791 case WM_QUERYENDSESSION: // System wants to go down.
4792 gui_shell_closed(); // Will exit when no changed buffers.
4793 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004796 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004799 return 0L;
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 break;
4802
4803 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004804 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4805 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004806 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return 0L;
4808
4809 case WM_SYSCHAR:
4810 /*
4811 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4812 * shortcut key, handle like a typed ALT key, otherwise call Windows
4813 * ALT key handling.
4814 */
4815#ifdef FEAT_MENU
4816 if ( !gui.menu_is_active
4817 || p_wak[0] == 'n'
4818 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4819 )
4820#endif
4821 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004822 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return 0L;
4824 }
4825#ifdef FEAT_MENU
4826 else
K.Takata4ac893f2022-01-20 12:44:28 +00004827 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#endif
4829
4830 case WM_SYSKEYUP:
4831#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004832 // This used to be done only when menu is active: ALT key is used for
4833 // that. But that caused problems when menu is disabled and using
4834 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4835 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004836 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004838 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#endif
4840
K.Takata4f2417f2021-06-05 16:25:32 +02004841 case WM_EXITSIZEMOVE:
4842 destroy_sizing_tip();
4843 break;
4844
Bram Moolenaar734a8672019-12-02 22:49:38 +01004845 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004849 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004850 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004851 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852
Bram Moolenaar734a8672019-12-02 22:49:38 +01004853 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004854 case WM_SETTINGCHANGE:
4855 return _OnSettingChange((UINT)wParam);
4856
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004857#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004859 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860#endif
K.Takata92000e22022-01-20 15:10:57 +00004861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#if defined(MENUHINTS) && defined(FEAT_MENU)
4863 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004864 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867#ifdef FEAT_MBYTE_IME
4868 case WM_IME_NOTIFY:
4869 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004870 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004871 return 1L;
4872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 case WM_IME_COMPOSITION:
4874 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004875 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004876 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004878 case WM_DPICHANGED:
4879 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4880 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004884 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#endif
K.Takata92000e22022-01-20 15:10:57 +00004887 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889
K.Takata4ac893f2022-01-20 12:44:28 +00004890 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891}
4892
4893/*
4894 * End of call-back routines
4895 */
4896
Bram Moolenaar734a8672019-12-02 22:49:38 +01004897// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898HWND vim_parent_hwnd = NULL;
4899
4900 static BOOL CALLBACK
4901FindWindowTitle(HWND hwnd, LPARAM lParam)
4902{
4903 char buf[2048];
4904 char *title = (char *)lParam;
4905
4906 if (GetWindowText(hwnd, buf, sizeof(buf)))
4907 {
4908 if (strstr(buf, title) != NULL)
4909 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004910 // Found it. Store the window ref. and quit searching if MDI
4911 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004913 if (vim_parent_hwnd != NULL)
4914 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004917 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918}
4919
4920/*
4921 * Invoked for '-P "title"' argument: search for parent application to open
4922 * our window in.
4923 */
4924 void
4925gui_mch_set_parent(char *title)
4926{
4927 EnumWindows(FindWindowTitle, (LPARAM)title);
4928 if (vim_parent_hwnd == NULL)
4929 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004930 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 mch_exit(2);
4932 }
4933}
4934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 static void
4937ole_error(char *arg)
4938{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004939 char buf[IOSIZE];
4940
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004941# ifdef VIMDLL
4942 gui.in_use = mch_is_gui_executable();
4943# endif
4944
Bram Moolenaar734a8672019-12-02 22:49:38 +01004945 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004946 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004947 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004948 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004952#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4953 static char *
4954gvim_error(void)
4955{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004956 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004957
4958 if (starting)
4959 {
4960 mch_errmsg(msg);
4961 mch_errmsg("\n");
4962 mch_exit(2);
4963 }
4964 return msg;
4965}
4966
4967 char *
4968gui_mch_do_spawn(char_u *arg)
4969{
4970 int len;
4971# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4972 char_u *session = NULL;
4973 LPWSTR tofree1 = NULL;
4974# endif
4975 WCHAR name[MAX_PATH];
4976 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4977 STARTUPINFOW si = {sizeof(si)};
4978 PROCESS_INFORMATION pi;
4979
4980 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4981 goto error;
4982 p = wcsrchr(name, L'\\');
4983 if (p == NULL)
4984 goto error;
4985 // Replace the executable name from vim(d).exe to gvim(d).exe.
4986# ifdef DEBUG
4987 wcscpy(p + 1, L"gvimd.exe");
4988# else
4989 wcscpy(p + 1, L"gvim.exe");
4990# endif
4991
4992# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4993 if (starting)
4994# endif
4995 {
4996 // Pass the command line to the new process.
4997 p = GetCommandLineW();
4998 // Skip 1st argument.
4999 while (*p && *p != L' ' && *p != L'\t')
5000 {
5001 if (*p == L'"')
5002 {
5003 while (*p && *p != L'"')
5004 ++p;
5005 if (*p)
5006 ++p;
5007 }
5008 else
5009 ++p;
5010 }
5011 cmd = p;
5012 }
5013# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5014 else
5015 {
5016 // Create a session file and pass it to the new process.
5017 LPWSTR wsession;
5018 char_u *savebg;
5019 int ret;
5020
5021 session = vim_tempname('s', FALSE);
5022 if (session == NULL)
5023 goto error;
5024 savebg = p_bg;
5025 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5026 ret = write_session_file(session);
5027 vim_free(p_bg);
5028 p_bg = savebg;
5029 if (!ret)
5030 goto error;
5031 wsession = enc_to_utf16(session, NULL);
5032 if (wsession == NULL)
5033 goto error;
5034 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005035 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005036 if (cmd == NULL)
5037 {
5038 vim_free(wsession);
5039 goto error;
5040 }
5041 tofree1 = cmd;
5042 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5043 wsession, wsession);
5044 vim_free(wsession);
5045 }
5046# endif
5047
5048 // Check additional arguments to the `:gui` command.
5049 if (arg != NULL)
5050 {
5051 warg = enc_to_utf16(arg, NULL);
5052 if (warg == NULL)
5053 goto error;
5054 tofree2 = warg;
5055 }
5056 else
5057 warg = L"";
5058
5059 // Set up the new command line.
5060 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005061 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005062 if (newcmd == NULL)
5063 goto error;
5064 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5065
5066 // Spawn a new GUI process.
5067 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5068 NULL, NULL, &si, &pi))
5069 goto error;
5070 CloseHandle(pi.hProcess);
5071 CloseHandle(pi.hThread);
5072 mch_exit(0);
5073
5074error:
5075# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5076 if (session)
5077 mch_remove(session);
5078 vim_free(session);
5079 vim_free(tofree1);
5080# endif
5081 vim_free(newcmd);
5082 vim_free(tofree2);
5083 return gvim_error();
5084}
5085#endif
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087/*
5088 * Parse the GUI related command-line arguments. Any arguments used are
5089 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005090 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 */
5092 void
5093gui_mch_prepare(int *argc, char **argv)
5094{
5095 int silent = FALSE;
5096 int idx;
5097
Bram Moolenaar734a8672019-12-02 22:49:38 +01005098 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5100 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005101 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5103 && (argv[2][0] == '-' || argv[2][0] == '/'))
5104 {
5105 silent = TRUE;
5106 idx = 2;
5107 }
5108 else
5109 idx = 1;
5110
Bram Moolenaar734a8672019-12-02 22:49:38 +01005111 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (STRICMP(argv[idx] + 1, "register") == 0)
5113 {
5114#ifdef FEAT_OLE
5115 RegisterMe(silent);
5116 mch_exit(0);
5117#else
5118 if (!silent)
5119 ole_error("register");
5120 mch_exit(2);
5121#endif
5122 }
5123
Bram Moolenaar734a8672019-12-02 22:49:38 +01005124 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5126 {
5127#ifdef FEAT_OLE
5128 UnregisterMe(!silent);
5129 mch_exit(0);
5130#else
5131 if (!silent)
5132 ole_error("unregister");
5133 mch_exit(2);
5134#endif
5135 }
5136
Bram Moolenaar734a8672019-12-02 22:49:38 +01005137 // Ignore an -embedding argument. It is only relevant if the
5138 // application wants to treat the case when it is started manually
5139 // differently from the case where it is started via automation (and
5140 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5142 {
5143#ifdef FEAT_OLE
5144 *argc = 1;
5145#else
5146 ole_error("embedding");
5147 mch_exit(2);
5148#endif
5149 }
5150 }
5151
5152#ifdef FEAT_OLE
5153 {
5154 int bDoRestart = FALSE;
5155
5156 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005157 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (bDoRestart)
5159 mch_exit(0);
5160 }
5161#endif
5162
5163#ifdef FEAT_NETBEANS_INTG
5164 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005165 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 int arg;
5167
5168 for (arg = 1; arg < *argc; arg++)
5169 if (strncmp("-nb", argv[arg], 3) == 0)
5170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 netbeansArg = argv[arg];
5172 mch_memmove(&argv[arg], &argv[arg + 1],
5173 (--*argc - arg) * sizeof(char *));
5174 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005175 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179}
5180
K.Takatac81e9bf2022-01-16 14:15:49 +00005181 static void
5182load_dpi_func(void)
5183{
5184 HMODULE hUser32;
5185
5186 hUser32 = GetModuleHandle("user32.dll");
5187 if (hUser32 == NULL)
5188 goto fail;
5189
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005190 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5191 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5192 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005193 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005194 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5195 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005196
5197 if (pSetThreadDpiAwarenessContext != NULL)
5198 {
5199 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5200 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5201 if (oldctx != NULL)
5202 {
5203 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5204 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5205#ifdef DEBUG
5206 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5207 {
5208 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5209 }
5210#endif
5211 return;
5212 }
5213 }
5214
5215fail:
5216 // Disable PerMonitorV2 APIs.
5217 pGetDpiForSystem = stubGetDpiForSystem;
5218 pGetDpiForWindow = NULL;
5219 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5220 pSetThreadDpiAwarenessContext = NULL;
5221 pGetAwarenessFromDpiAwarenessContext = NULL;
5222}
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224/*
5225 * Initialise the GUI. Create all the windows, set up all the call-backs
5226 * etc.
5227 */
5228 int
5229gui_mch_init(void)
5230{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005232 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // Return here if the window was already opened (happens when
5236 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005238 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 /*
5241 * Load the tearoff bitmap
5242 */
5243#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005244 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#endif
5246
K.Takatac81e9bf2022-01-16 14:15:49 +00005247 load_dpi_func();
5248
5249 s_dpi = pGetDpiForSystem();
5250 update_scrollbar_size();
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#endif
5255 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005256#ifdef FEAT_TOOLBAR
5257 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5261
Bram Moolenaar734a8672019-12-02 22:49:38 +01005262 // First try using the wide version, so that we can use any title.
5263 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005264 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005266 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 wndclassw.lpfnWndProc = _WndProc;
5268 wndclassw.cbClsExtra = 0;
5269 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005270 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5272 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5273 wndclassw.hbrBackground = s_brush;
5274 wndclassw.lpszMenuName = NULL;
5275 wndclassw.lpszClassName = szVimWndClassW;
5276
K.Takata4ac893f2022-01-20 12:44:28 +00005277 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (vim_parent_hwnd != NULL)
5282 {
5283#ifdef HAVE_TRY_EXCEPT
5284 __try
5285 {
5286#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005287 // Open inside the specified parent window.
5288 // TODO: last argument should point to a CLIENTCREATESTRUCT
5289 // structure.
5290 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005293 WS_OVERLAPPEDWINDOW | WS_CHILD
5294 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5296 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005297 100, // Any value will do
5298 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005300 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#ifdef HAVE_TRY_EXCEPT
5302 }
5303 __except(EXCEPTION_EXECUTE_HANDLER)
5304 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005305 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307#endif
5308 if (s_hwnd == NULL)
5309 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005310 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 mch_exit(2);
5312 }
5313 }
5314 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005315 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005316 // If the provided windowid is not valid reset it to zero, so that it
5317 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005318 if (IsWindow((HWND)win_socket_id) <= 0)
5319 win_socket_id = 0;
5320
Bram Moolenaar734a8672019-12-02 22:49:38 +01005321 // Create a window. If win_socket_id is not zero without border and
5322 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005323 s_hwnd = CreateWindowW(
5324 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005325 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5326 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005327 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5328 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005329 100, // Any value will do
5330 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005331 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005332 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005333 if (s_hwnd != NULL && win_socket_id != 0)
5334 {
5335 SetParent(s_hwnd, (HWND)win_socket_id);
5336 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5337 }
5338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 if (s_hwnd == NULL)
5341 return FAIL;
5342
K.Takatac81e9bf2022-01-16 14:15:49 +00005343 if (pGetDpiForWindow != NULL)
5344 {
5345 s_dpi = pGetDpiForWindow(s_hwnd);
5346 update_scrollbar_size();
5347 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5348 }
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5351 dyn_imm_load();
5352#endif
5353
Bram Moolenaar734a8672019-12-02 22:49:38 +01005354 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005355 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005356 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005357 wndclassw.style = CS_OWNDC;
5358 wndclassw.lpfnWndProc = _TextAreaWndProc;
5359 wndclassw.cbClsExtra = 0;
5360 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005361 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005362 wndclassw.hIcon = NULL;
5363 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5364 wndclassw.hbrBackground = NULL;
5365 wndclassw.lpszMenuName = NULL;
5366 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005367
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005368 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 return FAIL;
5370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005372 s_textArea = CreateWindowExW(
5373 0,
5374 szTextAreaClassW, L"Vim text area",
5375 WS_CHILD | WS_VISIBLE, 0, 0,
5376 100, // Any value will do for now
5377 100, // Any value will do for now
5378 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005379 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (s_textArea == NULL)
5382 return FAIL;
5383
Bram Moolenaar20321902016-02-17 12:30:17 +01005384#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005385 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005386 {
5387 HANDLE hIcon = NULL;
5388
5389 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005390 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005391 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005392#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#ifdef FEAT_MENU
5395 s_menuBar = CreateMenu();
5396#endif
5397 s_hdc = GetDC(s_textArea);
5398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar734a8672019-12-02 22:49:38 +01005401 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005402 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
Bram Moolenaar734a8672019-12-02 22:49:38 +01005404 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 gui_mch_def_colors();
5406
Bram Moolenaar734a8672019-12-02 22:49:38 +01005407 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5408 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 set_normal_colors();
5410
5411 /*
5412 * Check that none of the colors are the same as the background color.
5413 * Then store the current values as the defaults.
5414 */
5415 gui_check_colors();
5416 gui.def_norm_pixel = gui.norm_pixel;
5417 gui.def_back_pixel = gui.back_pixel;
5418
Bram Moolenaar734a8672019-12-02 22:49:38 +01005419 // Get the colors for the highlight groups (gui_check_colors() might have
5420 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 highlight_gui_started();
5422
5423 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005424 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005426 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
5428 /*
5429 * Set up for Intellimouse processing
5430 */
5431 init_mouse_wheel();
5432
5433 /*
5434 * compute a couple of metrics used for the dialogs
5435 */
5436 get_dialog_font_metrics();
5437#ifdef FEAT_TOOLBAR
5438 /*
5439 * Create the toolbar
5440 */
5441 initialise_toolbar();
5442#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005443#ifdef FEAT_GUI_TABLINE
5444 /*
5445 * Create the tabline
5446 */
5447 initialise_tabline();
5448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449#ifdef MSWIN_FIND_REPLACE
5450 /*
5451 * Initialise the dialog box stuff
5452 */
5453 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5454
Bram Moolenaar734a8672019-12-02 22:49:38 +01005455 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005457 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005459 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5461 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5462 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005465#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005466 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005467 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005468#endif
5469
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005470#ifdef FEAT_RENDER_OPTIONS
5471 if (p_rop)
5472 (void)gui_mch_set_rendering_options(p_rop);
5473#endif
5474
Bram Moolenaar748bf032005-02-02 23:04:36 +00005475theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005476 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005477 display_errors();
5478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 return OK;
5480}
5481
5482/*
5483 * Get the size of the screen, taking position on multiple monitors into
5484 * account (if supported).
5485 */
5486 static void
5487get_work_area(RECT *spi_rect)
5488{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005489 HMONITOR mon;
5490 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
Bram Moolenaar734a8672019-12-02 22:49:38 +01005492 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005493 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005494 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005496 moninfo.cbSize = sizeof(MONITORINFO);
5497 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005499 *spi_rect = moninfo.rcWork;
5500 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005503 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5505}
5506
5507/*
5508 * Set the size of the window to the given width and height in pixels.
5509 */
5510 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005511gui_mch_set_shellsize(
5512 int width,
5513 int height,
5514 int min_width UNUSED,
5515 int min_height UNUSED,
5516 int base_width UNUSED,
5517 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005518 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005521 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
Bram Moolenaar734a8672019-12-02 22:49:38 +01005524 // Try to keep window completely on screen.
5525 // Get position of the screen work area. This is the part that is not
5526 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 get_work_area(&workarea_rect);
5528
Bram Moolenaar734a8672019-12-02 22:49:38 +01005529 // Resizing a maximized window looks very strange, unzoom it first.
5530 // But don't do it when still starting up, it may have been requested in
5531 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005532 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005534
5535 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaar734a8672019-12-02 22:49:38 +01005537 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005538 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5539 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5540 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5541 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5542 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5543 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar734a8672019-12-02 22:49:38 +01005545 // The following should take care of keeping Vim on the same monitor, no
5546 // matter if the secondary monitor is left or right of the primary
5547 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005548 window_rect.right = window_rect.left + win_width;
5549 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005550
Bram Moolenaar734a8672019-12-02 22:49:38 +01005551 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005552 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5553 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005555 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5556 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005558 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5559 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005561 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5562 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005564 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5565 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 SetActiveWindow(s_hwnd);
5568 SetFocus(s_hwnd);
5569
Bram Moolenaar734a8672019-12-02 22:49:38 +01005570 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572}
5573
5574
5575 void
5576gui_mch_set_scrollbar_thumb(
5577 scrollbar_T *sb,
5578 long val,
5579 long size,
5580 long max)
5581{
5582 SCROLLINFO info;
5583
5584 sb->scroll_shift = 0;
5585 while (max > 32767)
5586 {
5587 max = (max + 1) >> 1;
5588 val >>= 1;
5589 size >>= 1;
5590 ++sb->scroll_shift;
5591 }
5592
5593 if (sb->scroll_shift > 0)
5594 ++size;
5595
5596 info.cbSize = sizeof(info);
5597 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5598 info.nPos = val;
5599 info.nMin = 0;
5600 info.nMax = max;
5601 info.nPage = size;
5602 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5603}
5604
5605
5606/*
5607 * Set the current text font.
5608 */
5609 void
5610gui_mch_set_font(GuiFont font)
5611{
5612 gui.currFont = font;
5613}
5614
5615
5616/*
5617 * Set the current text foreground color.
5618 */
5619 void
5620gui_mch_set_fg_color(guicolor_T color)
5621{
5622 gui.currFgColor = color;
5623}
5624
5625/*
5626 * Set the current text background color.
5627 */
5628 void
5629gui_mch_set_bg_color(guicolor_T color)
5630{
5631 gui.currBgColor = color;
5632}
5633
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005634/*
5635 * Set the current text special color.
5636 */
5637 void
5638gui_mch_set_sp_color(guicolor_T color)
5639{
5640 gui.currSpColor = color;
5641}
5642
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005643#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644/*
5645 * Multi-byte handling, originally by Sung-Hoon Baek.
5646 * First static functions (no prototypes generated).
5647 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005648# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005649# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005650# endif
5651# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 * handle WM_IME_NOTIFY message
5655 */
5656 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005657_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
5659 LRESULT lResult = 0;
5660 HIMC hImc;
5661
5662 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5663 return lResult;
5664 switch (dwCommand)
5665 {
5666 case IMN_SETOPENSTATUS:
5667 if (pImmGetOpenStatus(hImc))
5668 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005669 LOGFONTW lf = norm_logfont;
5670 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5671 // Work around when PerMonitorV2 is not enabled in the process level.
5672 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5673 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 im_set_position(gui.row, gui.col);
5675
Bram Moolenaar734a8672019-12-02 22:49:38 +01005676 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005677 State &= ~MODE_LANGMAP;
5678 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005680# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005681 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5683 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005684 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 int old_row = gui.row;
5686 int old_col = gui.col;
5687
5688 // This must be called here before
5689 // status_redraw_curbuf(), otherwise the mode
5690 // message may appear in the wrong position.
5691 showmode();
5692 status_redraw_curbuf();
5693 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005694 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 gui.row = old_row;
5696 gui.col = old_col;
5697 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 }
5701 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005702 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 lResult = 0;
5704 break;
5705 }
5706 pImmReleaseContext(hWnd, hImc);
5707 return lResult;
5708}
5709
5710 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005711_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *ret;
5714 int len;
5715
Bram Moolenaar734a8672019-12-02 22:49:38 +01005716 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 return 0;
5718
5719 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5720 if (ret != NULL)
5721 {
5722 add_to_input_buf_csi(ret, len);
5723 vim_free(ret);
5724 return 1;
5725 }
5726 return 0;
5727}
5728
5729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 * void GetResultStr()
5731 *
5732 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5733 * get complete composition string
5734 */
5735 static char_u *
5736GetResultStr(HWND hwnd, int GCS, int *lenp)
5737{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005738 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005739 LONG ret;
5740 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 char_u *convbuf = NULL;
5742
5743 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5744 return NULL;
5745
K.Takatab0b2b732022-01-19 12:59:21 +00005746 // Get the length of the composition string.
5747 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5748 if (ret <= 0)
5749 return NULL;
5750
5751 // Allocate the requested buffer plus space for the NUL character.
5752 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (buf == NULL)
5754 return NULL;
5755
K.Takatab0b2b732022-01-19 12:59:21 +00005756 // Reads in the composition string.
5757 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5758 *lenp = ret / sizeof(WCHAR);
5759
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005760 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 pImmReleaseContext(hwnd, hIMC);
5762 vim_free(buf);
5763 return convbuf;
5764}
5765#endif
5766
Bram Moolenaar734a8672019-12-02 22:49:38 +01005767// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005768#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770/*
5771 * set font to IM.
5772 */
5773 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005774im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 HIMC hImc;
5777
5778 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5779 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005780 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 pImmReleaseContext(s_hwnd, hImc);
5782 }
5783}
5784
5785/*
5786 * Notify cursor position to IM.
5787 */
5788 void
5789im_set_position(int row, int col)
5790{
5791 HIMC hImc;
5792
5793 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5794 {
5795 COMPOSITIONFORM cfs;
5796
5797 cfs.dwStyle = CFS_POINT;
5798 cfs.ptCurrentPos.x = FILL_X(col);
5799 cfs.ptCurrentPos.y = FILL_Y(row);
5800 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005801 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5802 {
5803 // Work around when PerMonitorV2 is not enabled in the process level.
5804 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5805 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 pImmSetCompositionWindow(hImc, &cfs);
5808
5809 pImmReleaseContext(s_hwnd, hImc);
5810 }
5811}
5812
5813/*
5814 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5815 */
5816 void
5817im_set_active(int active)
5818{
5819 HIMC hImc;
5820 static HIMC hImcOld = (HIMC)0;
5821
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005822# ifdef VIMDLL
5823 if (!gui.in_use && !gui.starting)
5824 {
5825 mbyte_im_set_active(active);
5826 return;
5827 }
5828# endif
5829
Bram Moolenaar734a8672019-12-02 22:49:38 +01005830 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 if (p_imdisable)
5833 {
5834 if (hImcOld == (HIMC)0)
5835 {
5836 hImcOld = pImmGetContext(s_hwnd);
5837 if (hImcOld)
5838 pImmAssociateContext(s_hwnd, (HIMC)0);
5839 }
5840 active = FALSE;
5841 }
5842 else if (hImcOld != (HIMC)0)
5843 {
5844 pImmAssociateContext(s_hwnd, hImcOld);
5845 hImcOld = (HIMC)0;
5846 }
5847
5848 hImc = pImmGetContext(s_hwnd);
5849 if (hImc)
5850 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005851 /*
5852 * for Korean ime
5853 */
5854 HKL hKL = GetKeyboardLayout(0);
5855
5856 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5857 {
5858 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5859 static BOOL bSaved = FALSE;
5860
5861 if (active)
5862 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005863 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005864 if (bSaved)
5865 pImmSetConversionStatus(hImc, dwConversionSaved,
5866 dwSentenceSaved);
5867 bSaved = FALSE;
5868 }
5869 else
5870 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005871 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005872 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5873 &dwSentenceSaved))
5874 {
5875 bSaved = TRUE;
5876 pImmSetConversionStatus(hImc,
5877 dwConversionSaved & ~(IME_CMODE_NATIVE
5878 | IME_CMODE_FULLSHAPE),
5879 dwSentenceSaved);
5880 }
5881 }
5882 }
5883
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 pImmSetOpenStatus(hImc, active);
5885 pImmReleaseContext(s_hwnd, hImc);
5886 }
5887 }
5888}
5889
5890/*
5891 * Get IM status. When IM is on, return not 0. Else return 0.
5892 */
5893 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005894im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 int status = 0;
5897 HIMC hImc;
5898
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005899# ifdef VIMDLL
5900 if (!gui.in_use && !gui.starting)
5901 return mbyte_im_get_status();
5902# endif
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5905 {
5906 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5907 pImmReleaseContext(s_hwnd, hImc);
5908 }
5909 return status;
5910}
5911
Bram Moolenaar734a8672019-12-02 22:49:38 +01005912#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005915/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005916 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005917 */
5918 static void
5919latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5920{
5921 int c;
5922
Bram Moolenaarca003e12006-03-17 23:19:38 +00005923 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005924 {
5925 c = *text++;
5926 switch (c)
5927 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005928 case 0xa4: c = 0x20ac; break; // euro
5929 case 0xa6: c = 0x0160; break; // S hat
5930 case 0xa8: c = 0x0161; break; // S -hat
5931 case 0xb4: c = 0x017d; break; // Z hat
5932 case 0xb8: c = 0x017e; break; // Z -hat
5933 case 0xbc: c = 0x0152; break; // OE
5934 case 0xbd: c = 0x0153; break; // oe
5935 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005936 }
5937 *unicodebuf++ = c;
5938 }
5939}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
5941#ifdef FEAT_RIGHTLEFT
5942/*
5943 * What is this for? In the case where you are using Win98 or Win2K or later,
5944 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5945 * reverses the string sent to the TextOut... family. This sucks, because we
5946 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5947 * way to tell Windblows not to do this!
5948 *
5949 * The short of it is that this 'RevOut' only gets called if you are running
5950 * one of the new, "improved" MS OSes, and only if you are running in
5951 * 'rightleft' mode. It makes display take *slightly* longer, but not
5952 * noticeably so.
5953 */
5954 static void
K.Takata135e1522022-01-29 15:27:58 +00005955RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 int col,
5957 int row,
5958 UINT foptions,
5959 CONST RECT *pcliprect,
5960 LPCTSTR text,
5961 UINT len,
5962 CONST INT *padding)
5963{
5964 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005966 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005967 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005968 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969}
5970#endif
5971
Bram Moolenaar92467d32017-12-05 13:22:16 +01005972 static void
5973draw_line(
5974 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005975 int y1,
5976 int x2,
5977 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005978 COLORREF color)
5979{
5980#if defined(FEAT_DIRECTX)
5981 if (IS_ENABLE_DIRECTX())
5982 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5983 else
5984#endif
5985 {
5986 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5987 HPEN old_pen = SelectObject(s_hdc, hpen);
5988 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005989 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005990 LineTo(s_hdc, x2, y2);
5991 DeleteObject(SelectObject(s_hdc, old_pen));
5992 }
5993}
5994
5995 static void
5996set_pixel(
5997 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005998 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005999 COLORREF color)
6000{
6001#if defined(FEAT_DIRECTX)
6002 if (IS_ENABLE_DIRECTX())
6003 DWriteContext_SetPixel(s_dwc, x, y, color);
6004 else
6005#endif
6006 SetPixel(s_hdc, x, y, color);
6007}
6008
6009 static void
6010fill_rect(
6011 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006012 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006013 COLORREF color)
6014{
6015#if defined(FEAT_DIRECTX)
6016 if (IS_ENABLE_DIRECTX())
6017 DWriteContext_FillRect(s_dwc, rcp, color);
6018 else
6019#endif
6020 {
6021 HBRUSH hbr2;
6022
6023 if (hbr == NULL)
6024 hbr2 = CreateSolidBrush(color);
6025 else
6026 hbr2 = hbr;
6027 FillRect(s_hdc, rcp, hbr2);
6028 if (hbr == NULL)
6029 DeleteBrush(hbr2);
6030 }
6031}
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 void
6034gui_mch_draw_string(
6035 int row,
6036 int col,
6037 char_u *text,
6038 int len,
6039 int flags)
6040{
6041 static int *padding = NULL;
6042 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 const RECT *pcliprect = NULL;
6044 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 static WCHAR *unicodebuf = NULL;
6046 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006047 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 int y;
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 /*
6052 * Italic and bold text seems to have an extra row of pixels at the bottom
6053 * (below where the bottom of the character should be). If we draw the
6054 * characters with a solid background, the top row of pixels in the
6055 * character below will be overwritten. We can fix this by filling in the
6056 * background ourselves, to the correct character proportions, and then
6057 * writing the character in transparent mode. Still have a problem when
6058 * the character is "_", which gets written on to the character below.
6059 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6060 * pixel in their slots, which fixes the problem with the bottom row of
6061 * pixels. We still need this code because otherwise the top row of pixels
6062 * becomes a problem. - webb.
6063 */
6064 static HBRUSH hbr_cache[2] = {NULL, NULL};
6065 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6066 static int brush_lru = 0;
6067 HBRUSH hbr;
6068 RECT rc;
6069
6070 if (!(flags & DRAW_TRANSP))
6071 {
6072 /*
6073 * Clear background first.
6074 * Note: FillRect() excludes right and bottom of rectangle.
6075 */
6076 rc.left = FILL_X(col);
6077 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (has_mbyte)
6079 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006080 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006081 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 }
6083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 rc.right = FILL_X(col + len);
6085 rc.bottom = FILL_Y(row + 1);
6086
Bram Moolenaar734a8672019-12-02 22:49:38 +01006087 // Cache the created brush, that saves a lot of time. We need two:
6088 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 if (gui.currBgColor == brush_color[0])
6090 {
6091 hbr = hbr_cache[0];
6092 brush_lru = 1;
6093 }
6094 else if (gui.currBgColor == brush_color[1])
6095 {
6096 hbr = hbr_cache[1];
6097 brush_lru = 0;
6098 }
6099 else
6100 {
6101 if (hbr_cache[brush_lru] != NULL)
6102 DeleteBrush(hbr_cache[brush_lru]);
6103 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6104 brush_color[brush_lru] = gui.currBgColor;
6105 hbr = hbr_cache[brush_lru];
6106 brush_lru = !brush_lru;
6107 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006108
Bram Moolenaar92467d32017-12-05 13:22:16 +01006109 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
6111 SetBkMode(s_hdc, TRANSPARENT);
6112
6113 /*
6114 * When drawing block cursor, prevent inverted character spilling
6115 * over character cell (can happen with bold/italic)
6116 */
6117 if (flags & DRAW_CURSOR)
6118 {
6119 pcliprect = &rc;
6120 foptions = ETO_CLIPPED;
6121 }
6122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 SetTextColor(s_hdc, gui.currFgColor);
6124 SelectFont(s_hdc, gui.currFont);
6125
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006126#ifdef FEAT_DIRECTX
6127 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006128 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006129#endif
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6132 {
K.Takata135e1522022-01-29 15:27:58 +00006133 int i;
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 vim_free(padding);
6136 pad_size = Columns;
6137
Bram Moolenaar734a8672019-12-02 22:49:38 +01006138 // Don't give an out-of-memory message here, it would call us
6139 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006140 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (padding != NULL)
6142 for (i = 0; i < pad_size; i++)
6143 padding[i] = gui.char_width;
6144 }
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 /*
6147 * We have to provide the padding argument because italic and bold versions
6148 * of fixed-width fonts are often one pixel or so wider than their normal
6149 * versions.
6150 * No check for DRAW_BOLD, Windows will have done it already.
6151 */
6152
Bram Moolenaar734a8672019-12-02 22:49:38 +01006153 // Check if there are any UTF-8 characters. If not, use normal text
6154 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (enc_utf8)
6156 for (n = 0; n < len; ++n)
6157 if (text[n] >= 0x80)
6158 break;
6159
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006160#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006161 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6162 // required that unicode drawing routine, currently. So this forces it
6163 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006164 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006165 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006166#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006167
Bram Moolenaar734a8672019-12-02 22:49:38 +01006168 // Check if the Unicode buffer exists and is big enough. Create it
6169 // with the same length as the multi-byte string, the number of wide
6170 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006171 if ((enc_utf8
6172 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6173 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 && (unicodebuf == NULL || len > unibuflen))
6175 {
6176 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006177 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006180 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182 unibuflen = len;
6183 }
6184
6185 if (enc_utf8 && n < len && unicodebuf != NULL)
6186 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006187 // Output UTF-8 characters. Composing characters should be
6188 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006189 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006190 int wlen; // string length in words
6191 int clen; // string length in characters
6192 int cells; // cell width of string up to composing char
6193 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006194 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006196 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006197 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006199 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006201 c = utf_ptr2char(text + i);
6202 if (c >= 0x10000)
6203 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006204 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006205 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6206 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006207 }
6208 else
6209 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006210 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006211 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006212
6213 if (utf_iscomposing(c))
6214 cw = 0;
6215 else
6216 {
6217 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006218 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006219 cw = 1;
6220 }
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (unicodepdy != NULL)
6223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // Use unicodepdy to make characters fit as we expect, even
6225 // when the font uses different widths (e.g., bold character
6226 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006227 if (c >= 0x10000)
6228 {
6229 unicodepdy[wlen - 2] = cw * gui.char_width;
6230 unicodepdy[wlen - 1] = 0;
6231 }
6232 else
6233 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
6235 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006236 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006237 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006239#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006240 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006241 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006242 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006243 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006244 TEXT_X(col), TEXT_Y(row),
6245 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006246 gui.char_width, gui.currFgColor,
6247 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006248 }
6249 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006250#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006251 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6252 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006253 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006255 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006257 // If we want to display codepage data, and the current CP is not the
6258 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (unicodebuf != NULL)
6260 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006261 if (enc_latin9)
6262 latin9_to_ucs(text, len, unicodebuf);
6263 else
6264 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 MB_PRECOMPOSED,
6266 (char *)text, len,
6267 (LPWSTR)unicodebuf, unibuflen);
6268 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006269 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006270 // Use unicodepdy to make characters fit as we expect, even
6271 // when the font uses different widths (e.g., bold character
6272 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006273 if (unicodepdy != NULL)
6274 {
6275 int i;
6276 int cw;
6277
6278 for (i = 0; i < len; ++i)
6279 {
6280 cw = utf_char2cells(unicodebuf[i]);
6281 if (cw > 2)
6282 cw = 1;
6283 unicodepdy[i] = cw * gui.char_width;
6284 }
6285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006287 foptions, pcliprect, unicodebuf, len, unicodepdy);
6288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290 }
6291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
6293#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006294 // Windows will mess up RL text, so we have to draw it character by
6295 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006296 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6298 foptions, pcliprect, (char *)text, len, padding);
6299 else
6300#endif
6301 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6302 foptions, pcliprect, (char *)text, len, padding);
6303 }
6304
Bram Moolenaar734a8672019-12-02 22:49:38 +01006305 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 if (flags & DRAW_UNDERL)
6307 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006308 // When p_linespace is 0, overwrite the bottom row of pixels.
6309 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (p_linespace > 1)
6312 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006313 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006315
Bram Moolenaar734a8672019-12-02 22:49:38 +01006316 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006317 if (flags & DRAW_STRIKE)
6318 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006319 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006320 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006321 }
6322
Bram Moolenaar734a8672019-12-02 22:49:38 +01006323 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006324 if (flags & DRAW_UNDERC)
6325 {
6326 int x;
6327 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006328 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006329
6330 y = FILL_Y(row + 1) - 1;
6331 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6332 {
6333 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006334 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006335 }
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
6339
6340/*
6341 * Output routines.
6342 */
6343
Bram Moolenaar734a8672019-12-02 22:49:38 +01006344/*
6345 * Flush any output to the screen
6346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 void
6348gui_mch_flush(void)
6349{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006350#if defined(FEAT_DIRECTX)
6351 if (IS_ENABLE_DIRECTX())
6352 DWriteContext_Flush(s_dwc);
6353#endif
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 GdiFlush();
6356}
6357
6358 static void
6359clear_rect(RECT *rcp)
6360{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006361 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362}
6363
6364
Bram Moolenaarc716c302006-01-21 22:12:51 +00006365 void
6366gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6367{
6368 RECT workarea_rect;
6369
6370 get_work_area(&workarea_rect);
6371
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006372 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006373 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6374 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006375
Bram Moolenaar734a8672019-12-02 22:49:38 +01006376 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6377 // the menubar for MSwin, we subtract it from the screen height, so that
6378 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006379 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006380 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6381 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6382 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6383 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006384}
6385
6386
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387#if defined(FEAT_MENU) || defined(PROTO)
6388/*
6389 * Add a sub menu to the menu bar.
6390 */
6391 void
6392gui_mch_add_menu(
6393 vimmenu_T *menu,
6394 int pos)
6395{
6396 vimmenu_T *parent = menu->parent;
6397
6398 menu->submenu_id = CreatePopupMenu();
6399 menu->id = s_menu_id++;
6400
6401 if (menu_is_menubar(menu->name))
6402 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006403 WCHAR *wn;
6404 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006406 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006407 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006408 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006410 infow.cbSize = sizeof(infow);
6411 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6412 | MIIM_SUBMENU;
6413 infow.dwItemData = (long_u)menu;
6414 infow.wID = menu->id;
6415 infow.fType = MFT_STRING;
6416 infow.dwTypeData = wn;
6417 infow.cch = (UINT)wcslen(wn);
6418 infow.hSubMenu = menu->submenu_id;
6419 InsertMenuItemW((parent == NULL)
6420 ? s_menuBar : parent->submenu_id,
6421 (UINT)pos, TRUE, &infow);
6422 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424
Bram Moolenaar734a8672019-12-02 22:49:38 +01006425 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (parent == NULL)
6427 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006428# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 else if (IsWindow(parent->tearoff_handle))
6430 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433
6434 void
6435gui_mch_show_popupmenu(vimmenu_T *menu)
6436{
6437 POINT mp;
6438
K.Takata45f9cfb2022-01-21 11:11:00 +00006439 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6441}
6442
6443 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006444gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 vimmenu_T *menu = gui_find_menu(path_name);
6447
6448 if (menu != NULL)
6449 {
6450 POINT p;
6451
Bram Moolenaar734a8672019-12-02 22:49:38 +01006452 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006454 if (mouse_pos)
6455 {
6456 int mx, my;
6457
6458 gui_mch_getmouse(&mx, &my);
6459 p.x += mx;
6460 p.y += my;
6461 }
6462 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006464 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6466 }
6467 msg_scroll = FALSE;
6468 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6469 }
6470}
6471
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006472# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473/*
6474 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6475 * create it as a pseudo-"tearoff menu".
6476 */
6477 void
6478gui_make_tearoff(char_u *path_name)
6479{
6480 vimmenu_T *menu = gui_find_menu(path_name);
6481
Bram Moolenaar734a8672019-12-02 22:49:38 +01006482 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (menu != NULL)
6484 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6485}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
6488/*
6489 * Add a menu item to a menu
6490 */
6491 void
6492gui_mch_add_menu_item(
6493 vimmenu_T *menu,
6494 int idx)
6495{
6496 vimmenu_T *parent = menu->parent;
6497
6498 menu->id = s_menu_id++;
6499 menu->submenu_id = NULL;
6500
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006501# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6503 {
6504 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6505 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6506 }
6507 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006508# endif
6509# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (menu_is_toolbar(parent->name))
6511 {
6512 TBBUTTON newtb;
6513
Bram Moolenaara80faa82020-04-12 19:37:17 +02006514 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (menu_is_separator(menu->name))
6516 {
6517 newtb.iBitmap = 0;
6518 newtb.fsStyle = TBSTYLE_SEP;
6519 }
6520 else
6521 {
6522 newtb.iBitmap = get_toolbar_bitmap(menu);
6523 newtb.fsStyle = TBSTYLE_BUTTON;
6524 }
6525 newtb.idCommand = menu->id;
6526 newtb.fsState = TBSTATE_ENABLED;
6527 newtb.iString = 0;
6528 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6529 (LPARAM)&newtb);
6530 menu->submenu_id = (HMENU)-1;
6531 }
6532 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006533# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006535 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006537 wn = enc_to_utf16(menu->name, NULL);
6538 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 InsertMenuW(parent->submenu_id, (UINT)idx,
6541 (menu_is_separator(menu->name)
6542 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6543 (UINT)menu->id, wn);
6544 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006546# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (IsWindow(parent->tearoff_handle))
6548 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551}
6552
6553/*
6554 * Destroy the machine specific menu widget.
6555 */
6556 void
6557gui_mch_destroy_menu(vimmenu_T *menu)
6558{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006559# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 /*
6561 * is this a toolbar button?
6562 */
6563 if (menu->submenu_id == (HMENU)-1)
6564 {
6565 int iButton;
6566
6567 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6568 (WPARAM)menu->id, 0);
6569 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6570 }
6571 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
6574 if (menu->parent != NULL
6575 && menu_is_popup(menu->parent->dname)
6576 && menu->parent->submenu_id != NULL)
6577 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6578 else
6579 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6580 if (menu->submenu_id != NULL)
6581 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006582# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (IsWindow(menu->tearoff_handle))
6584 DestroyWindow(menu->tearoff_handle);
6585 if (menu->parent != NULL
6586 && menu->parent->children != NULL
6587 && IsWindow(menu->parent->tearoff_handle))
6588 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006589 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 menu->modes = 0;
6591 rebuild_tearoff(menu->parent);
6592 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595}
6596
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006597# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 static void
6599rebuild_tearoff(vimmenu_T *menu)
6600{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006601 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 char_u tbuf[128];
6603 RECT trect;
6604 RECT rct;
6605 RECT roct;
6606 int x, y;
6607
6608 HWND thwnd = menu->tearoff_handle;
6609
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006610 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 if (GetWindowRect(thwnd, &trect)
6612 && GetWindowRect(s_hwnd, &rct)
6613 && GetClientRect(s_hwnd, &roct))
6614 {
6615 x = trect.left - rct.left;
6616 y = (trect.top - rct.bottom + roct.bottom);
6617 }
6618 else
6619 {
6620 x = y = 0xffffL;
6621 }
6622 DestroyWindow(thwnd);
6623 if (menu->children != NULL)
6624 {
6625 gui_mch_tearoff(tbuf, menu, x, y);
6626 if (IsWindow(menu->tearoff_handle))
6627 (void) SetWindowPos(menu->tearoff_handle,
6628 NULL,
6629 (int)trect.left,
6630 (int)trect.top,
6631 0, 0,
6632 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6633 }
6634}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006635# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637/*
6638 * Make a menu either grey or not grey.
6639 */
6640 void
6641gui_mch_menu_grey(
6642 vimmenu_T *menu,
6643 int grey)
6644{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006645# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 /*
6647 * is this a toolbar button?
6648 */
6649 if (menu->submenu_id == (HMENU)-1)
6650 {
6651 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006652 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006655# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006656 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6657 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006659# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6661 {
6662 WORD menuID;
6663 HWND menuHandle;
6664
6665 /*
6666 * A tearoff button has changed state.
6667 */
6668 if (menu->children == NULL)
6669 menuID = (WORD)(menu->id);
6670 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006671 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6673 if (menuHandle)
6674 EnableWindow(menuHandle, !grey);
6675
6676 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
Bram Moolenaar734a8672019-12-02 22:49:38 +01006680#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
6682
Bram Moolenaar734a8672019-12-02 22:49:38 +01006683// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006686#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6689/*
6690 * stuff for dialogs
6691 */
6692
6693/*
6694 * The callback routine used by all the dialogs. Very simple. First,
6695 * acknowledges the INITDIALOG message so that Windows knows to do standard
6696 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6697 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6698 * number.
6699 */
6700 static LRESULT CALLBACK
6701dialog_callback(
6702 HWND hwnd,
6703 UINT message,
6704 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006705 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 if (message == WM_INITDIALOG)
6708 {
6709 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006710 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 (void)SetFocus(hwnd);
6712 if (dialog_default_button > IDCANCEL)
6713 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006714 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006715 // We don't have a default, set focus on another element of the
6716 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006717 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 return FALSE;
6719 }
6720
6721 if (message == WM_COMMAND)
6722 {
6723 int button = LOWORD(wParam);
6724
Bram Moolenaar734a8672019-12-02 22:49:38 +01006725 // Don't end the dialog if something was selected that was
6726 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (button >= DLG_NONBUTTON_CONTROL)
6728 return TRUE;
6729
Bram Moolenaar734a8672019-12-02 22:49:38 +01006730 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006732 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006733 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006734 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006735
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006736 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6737 p = utf16_to_enc(wp, NULL);
6738 vim_strncpy(s_textfield, p, IOSIZE);
6739 vim_free(p);
6740 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743 /*
6744 * Need to check for IDOK because if the user just hits Return to
6745 * accept the default value, some reason this is what we get.
6746 */
6747 if (button == IDOK)
6748 {
6749 if (dialog_default_button > IDCANCEL)
6750 EndDialog(hwnd, dialog_default_button);
6751 }
6752 else
6753 EndDialog(hwnd, button - IDCANCEL);
6754 return TRUE;
6755 }
6756
6757 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6758 {
6759 EndDialog(hwnd, 0);
6760 return TRUE;
6761 }
6762 return FALSE;
6763}
6764
6765/*
6766 * Create a dialog dynamically from the parameter strings.
6767 * type = type of dialog (question, alert, etc.)
6768 * title = dialog title. may be NULL for default title.
6769 * message = text to display. Dialog sizes to accommodate it.
6770 * buttons = '\n' separated list of button captions, default first.
6771 * dfltbutton = number of default button.
6772 *
6773 * This routine returns 1 if the first button is pressed,
6774 * 2 for the second, etc.
6775 *
6776 * 0 indicates Esc was pressed.
6777 * -1 for unexpected error
6778 *
6779 * If stubbing out this fn, return 1.
6780 */
6781
Bram Moolenaar734a8672019-12-02 22:49:38 +01006782static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 "IDR_VIM",
6785 "IDR_VIM_ERROR",
6786 "IDR_VIM_ALERT",
6787 "IDR_VIM_INFO",
6788 "IDR_VIM_QUESTION"
6789};
6790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 int
6792gui_mch_dialog(
6793 int type,
6794 char_u *title,
6795 char_u *message,
6796 char_u *buttons,
6797 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006798 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006799 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006802 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 int numButtons;
6804 int *buttonWidths, *buttonPositions;
6805 int buttonYpos;
6806 int nchar, i;
6807 DWORD lStyle;
6808 int dlgwidth = 0;
6809 int dlgheight;
6810 int editboxheight;
6811 int horizWidth = 0;
6812 int msgheight;
6813 char_u *pstart;
6814 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006815 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 char_u *tbuffer;
6817 RECT rect;
6818 HWND hwnd;
6819 HDC hdc;
6820 HFONT font, oldFont;
6821 TEXTMETRIC fontInfo;
6822 int fontHeight;
6823 int textWidth, minButtonWidth, messageWidth;
6824 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006825 int maxDialogHeight;
6826 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 int vertical;
6828 int dlgPaddingX;
6829 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006830# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006831 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006833# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006834 garray_T ga;
6835 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006836 int dlg_icon_width;
6837 int dlg_icon_height;
6838 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006840# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006841 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006842# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006843 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006844# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006845 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006846 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
Bram Moolenaar748bf032005-02-02 23:04:36 +00006849 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006850 {
6851 load_dpi_func();
6852 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006853 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006854 }
6855 else
6856 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
6858 if ((type < 0) || (type > VIM_LAST_TYPE))
6859 type = 0;
6860
Bram Moolenaar734a8672019-12-02 22:49:38 +01006861 // allocate some memory for dialog template
6862 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006863 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006864 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
6866 if (p == NULL)
6867 return -1;
6868
6869 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006870 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6872 * const.
6873 */
6874 tbuffer = vim_strsave(buttons);
6875 if (tbuffer == NULL)
6876 return -1;
6877
Bram Moolenaar734a8672019-12-02 22:49:38 +01006878 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
Bram Moolenaar734a8672019-12-02 22:49:38 +01006880 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 numButtons = 1;
6882 for (i = 0; tbuffer[i] != '\0'; i++)
6883 {
6884 if (tbuffer[i] == DLG_BUTTON_SEP)
6885 numButtons++;
6886 }
6887 if (dfltbutton >= numButtons)
6888 dfltbutton = -1;
6889
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006891 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (buttonWidths == NULL)
6893 return -1;
6894
Bram Moolenaar734a8672019-12-02 22:49:38 +01006895 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006896 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (buttonPositions == NULL)
6898 return -1;
6899
6900 /*
6901 * Calculate how big the dialog must be.
6902 */
6903 hwnd = GetDesktopWindow();
6904 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006905# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6907 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006908 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 use_lfSysmenu = TRUE;
6910 }
6911 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006912# endif
K.Takatad1c58992022-01-23 12:31:57 +00006913 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6914 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6915
6916 oldFont = SelectFont(hdc, font);
6917 dlgPaddingX = DLG_PADDING_X;
6918 dlgPaddingY = DLG_PADDING_Y;
6919
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 GetTextMetrics(hdc, &fontInfo);
6921 fontHeight = fontInfo.tmHeight;
6922
Bram Moolenaar734a8672019-12-02 22:49:38 +01006923 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006924 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
Bram Moolenaar734a8672019-12-02 22:49:38 +01006926 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 if (s_hwnd == NULL)
6928 {
6929 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
Bram Moolenaar734a8672019-12-02 22:49:38 +01006931 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006932 get_work_area(&workarea_rect);
6933 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006934 if (maxDialogWidth > adjust_by_system_dpi(600))
6935 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006936 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006937 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006938 }
6939 else
6940 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006941 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006942 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006943 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006944 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6945 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6946 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6947 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006948
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006949 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006950 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6951 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6952 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6953 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6954 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006955 }
6956
Bram Moolenaar734a8672019-12-02 22:49:38 +01006957 // Set dlgwidth to width of message.
6958 // Copy the message into "ga", changing NL to CR-NL and inserting line
6959 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 pstart = message;
6961 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006962 msgheight = 0;
6963 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 do
6965 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006966 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006967
Bram Moolenaar734a8672019-12-02 22:49:38 +01006968 // Need to figure out where to break the string. The system does it
6969 // at a word boundary, which would mean we can't compute the number of
6970 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006971 textWidth = 0;
6972 last_white = NULL;
6973 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006974 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006975 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006976 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006977 && textWidth > maxDialogWidth * 3 / 4)
6978 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006979 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006980 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006981 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006982 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006983 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006984 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006985 textWidth = 0;
6986
6987 if (last_white != NULL)
6988 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006989 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006990 if (pend > last_white)
6991 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006992 pend = last_white + 1;
6993 last_white = NULL;
6994 }
6995 ga_append(&ga, '\r');
6996 ga_append(&ga, '\n');
6997 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006998 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006999
7000 while (--l >= 0)
7001 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007002 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007003 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007005
7006 ga_append(&ga, '\r');
7007 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 pstart = pend + 1;
7009 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007010
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007011 if (ga.ga_data != NULL)
7012 message = ga.ga_data;
7013
Bram Moolenaar734a8672019-12-02 22:49:38 +01007014 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007015
K.Takatac81e9bf2022-01-16 14:15:49 +00007016 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7017 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
K.Takatac81e9bf2022-01-16 14:15:49 +00007019 // Add width of icon to dlgwidth, and some space
7020 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7021 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7022
7023 if (msgheight < dlg_icon_height)
7024 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
7026 /*
7027 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007028 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007030 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (!vertical)
7032 {
7033 // Place buttons horizontally if they fit.
7034 horizWidth = dlgPaddingX;
7035 pstart = tbuffer;
7036 i = 0;
7037 do
7038 {
7039 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7040 if (pend == NULL)
7041 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007042 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (textWidth < minButtonWidth)
7044 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007045 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 buttonWidths[i] = textWidth;
7047 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007048 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 pstart = pend + 1;
7050 } while (*pend != NUL);
7051
7052 if (horizWidth > maxDialogWidth)
7053 vertical = TRUE; // Too wide to fit on the screen.
7054 else if (horizWidth > dlgwidth)
7055 dlgwidth = horizWidth;
7056 }
7057
7058 if (vertical)
7059 {
7060 // Stack buttons vertically.
7061 pstart = tbuffer;
7062 do
7063 {
7064 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7065 if (pend == NULL)
7066 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007067 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007068 textWidth += dlgPaddingX; // Padding within button
7069 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (textWidth > dlgwidth)
7071 dlgwidth = textWidth;
7072 pstart = pend + 1;
7073 } while (*pend != NUL);
7074 }
7075
7076 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007077 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaar734a8672019-12-02 22:49:38 +01007079 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007080 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
7082 add_long(lStyle);
7083 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007084 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 add_word(0); // NumberOfItems(will change later)
7086 add_word(10); // x
7087 add_word(10); // y
7088 add_word(PixelToDialogX(dlgwidth)); // cx
7089
7090 // Dialog height.
7091 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007092 dlgheight = msgheight + 2 * dlgPaddingY
7093 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 else
7095 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7096
7097 // Dialog needs to be taller if contains an edit box.
7098 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7099 if (textfield != NULL)
7100 dlgheight += editboxheight;
7101
Bram Moolenaar734a8672019-12-02 22:49:38 +01007102 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007103 if (dlgheight > maxDialogHeight)
7104 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007105 msgheight = msgheight - (dlgheight - maxDialogHeight);
7106 dlgheight = maxDialogHeight;
7107 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007108 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007109 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007110 }
7111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 add_word(PixelToDialogY(dlgheight));
7113
7114 add_word(0); // Menu
7115 add_word(0); // Class
7116
Bram Moolenaar734a8672019-12-02 22:49:38 +01007117 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007118 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7119 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 p += nchar;
7121
K.Takatad1c58992022-01-23 12:31:57 +00007122 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007123# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007124 if (use_lfSysmenu)
7125 {
7126 // point size
7127 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7128 GetDeviceCaps(hdc, LOGPIXELSY));
7129 wcscpy(p, lfSysmenu.lfFaceName);
7130 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 }
K.Takatad1c58992022-01-23 12:31:57 +00007132 else
7133# endif
7134 {
7135 *p++ = DLG_FONT_POINT_SIZE; // point size
7136 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7137 }
7138 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
7140 buttonYpos = msgheight + 2 * dlgPaddingY;
7141
7142 if (textfield != NULL)
7143 buttonYpos += editboxheight;
7144
7145 pstart = tbuffer;
7146 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007147 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 for (i = 0; i < numButtons; i++)
7149 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007150 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 for ( pend = pstart;
7152 *pend && (*pend != DLG_BUTTON_SEP);
7153 pend++)
7154 ;
7155
7156 if (*pend)
7157 *pend = '\0';
7158
7159 /*
7160 * old NOTE:
7161 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7162 * the focus to the first tab-able button and in so doing makes that
7163 * the default!! Grrr. Workaround: Make the default button the only
7164 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7165 * he/she can use arrow keys.
7166 *
7167 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007168 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 * dialog. Also needed for when the textfield is the default control.
7170 * It appears to work now (perhaps not on Win95?).
7171 */
7172 if (vertical)
7173 {
7174 p = add_dialog_element(p,
7175 (i == dfltbutton
7176 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7177 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007178 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 + 2 * fontHeight * i),
7180 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7181 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007182 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184 else
7185 {
7186 p = add_dialog_element(p,
7187 (i == dfltbutton
7188 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7189 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007190 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 PixelToDialogX(buttonWidths[i]),
7192 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007193 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007195 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197 *pnumitems += numButtons;
7198
Bram Moolenaar734a8672019-12-02 22:49:38 +01007199 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 p = add_dialog_element(p, SS_ICON,
7201 PixelToDialogX(dlgPaddingX),
7202 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007203 PixelToDialogX(dlg_icon_width),
7204 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7206 dlg_icons[type]);
7207
Bram Moolenaar734a8672019-12-02 22:49:38 +01007208 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007209 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007210 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007211 PixelToDialogY(dlgPaddingY),
7212 (WORD)(PixelToDialogX(messageWidth) + 1),
7213 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007214 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar734a8672019-12-02 22:49:38 +01007216 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (textfield != NULL)
7218 {
7219 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7220 PixelToDialogX(2 * dlgPaddingX),
7221 PixelToDialogY(2 * dlgPaddingY + msgheight),
7222 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7223 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007224 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 *pnumitems += 1;
7226 }
7227
7228 *pnumitems += 2;
7229
7230 SelectFont(hdc, oldFont);
7231 DeleteObject(font);
7232 ReleaseDC(hwnd, hdc);
7233
Bram Moolenaar734a8672019-12-02 22:49:38 +01007234 // Let the dialog_callback() function know which button to make default
7235 // If we have an edit box, make that the default. We also need to tell
7236 // dialog_callback() if this dialog contains an edit box or not. We do
7237 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 if (textfield != NULL)
7239 {
7240 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7241 s_textfield = textfield;
7242 }
7243 else
7244 {
7245 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7246 s_textfield = NULL;
7247 }
7248
Bram Moolenaar734a8672019-12-02 22:49:38 +01007249 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007251 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 (LPDLGTEMPLATE)pdlgtemplate,
7253 s_hwnd,
7254 (DLGPROC)dialog_callback);
7255
7256 LocalFree(LocalHandle(pdlgtemplate));
7257 vim_free(tbuffer);
7258 vim_free(buttonWidths);
7259 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007260 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
Bram Moolenaar734a8672019-12-02 22:49:38 +01007262 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 (void)SetFocus(s_hwnd);
7264
7265 return nchar;
7266}
7267
Bram Moolenaar734a8672019-12-02 22:49:38 +01007268#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270/*
7271 * Put a simple element (basic class) onto a dialog template in memory.
7272 * return a pointer to where the next item should be added.
7273 *
7274 * parameters:
7275 * lStyle = additional style flags
7276 * (be careful, NT3.51 & Win32s will ignore the new ones)
7277 * x,y = x & y positions IN DIALOG UNITS
7278 * w,h = width and height IN DIALOG UNITS
7279 * Id = ID used in messages
7280 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7281 * caption = usually text or resource name
7282 *
7283 * TODO: use the length information noted here to enable the dialog creation
7284 * routines to work out more exactly how much memory they need to alloc.
7285 */
7286 static PWORD
7287add_dialog_element(
7288 PWORD p,
7289 DWORD lStyle,
7290 WORD x,
7291 WORD y,
7292 WORD w,
7293 WORD h,
7294 WORD Id,
7295 WORD clss,
7296 const char *caption)
7297{
7298 int nchar;
7299
Bram Moolenaar734a8672019-12-02 22:49:38 +01007300 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7302 *p++ = LOWORD(lStyle);
7303 *p++ = HIWORD(lStyle);
7304 *p++ = 0; // LOWORD (lExtendedStyle)
7305 *p++ = 0; // HIWORD (lExtendedStyle)
7306 *p++ = x;
7307 *p++ = y;
7308 *p++ = w;
7309 *p++ = h;
7310 *p++ = Id; //9 or 10 words in all
7311
7312 *p++ = (WORD)0xffff;
7313 *p++ = clss; //2 more here
7314
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007315 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 p += nchar;
7317
7318 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7319
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007320 return p; // total = 15 + strlen(caption) words
7321 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322}
7323
7324
7325/*
7326 * Helper routine. Take an input pointer, return closest pointer that is
7327 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7328 */
7329 static LPWORD
7330lpwAlign(
7331 LPWORD lpIn)
7332{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007333 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007335 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 ul += 3;
7337 ul >>= 2;
7338 ul <<= 2;
7339 return (LPWORD)ul;
7340}
7341
7342/*
7343 * Helper routine. Takes second parameter as Ansi string, copies it to first
7344 * parameter as wide character (16-bits / char) string, and returns integer
7345 * number of wide characters (words) in string (including the trailing wide
7346 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007347 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7348 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 static int
7350nCopyAnsiToWideChar(
7351 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007352 LPSTR lpAnsiIn,
7353 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
7355 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007356 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 int i;
7358 WCHAR *wn;
7359
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007360 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007362 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007363 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (wn != NULL)
7365 {
7366 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007367 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 vim_free(wn);
7369 }
7370 }
7371 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007372 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 nChar = MultiByteToWideChar(
7374 enc_codepage > 0 ? enc_codepage : CP_ACP,
7375 MB_PRECOMPOSED,
7376 lpAnsiIn, len,
7377 lpWCStr, len);
7378 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007379 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
7382 return nChar;
7383}
7384
7385
7386#ifdef FEAT_TEAROFF
7387/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007388 * Lookup menu handle from "menu_id".
7389 */
7390 static HMENU
7391tearoff_lookup_menuhandle(
7392 vimmenu_T *menu,
7393 WORD menu_id)
7394{
7395 for ( ; menu != NULL; menu = menu->next)
7396 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007397 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007398 continue;
7399 if (menu_is_separator(menu->dname))
7400 continue;
7401 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7402 return menu->submenu_id;
7403 }
7404 return NULL;
7405}
7406
7407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 * The callback function for all the modeless dialogs that make up the
7409 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7410 * thinking its menus have been clicked), and go away when closed.
7411 */
7412 static LRESULT CALLBACK
7413tearoff_callback(
7414 HWND hwnd,
7415 UINT message,
7416 WPARAM wParam,
7417 LPARAM lParam)
7418{
7419 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007420 {
7421 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
Bram Moolenaar734a8672019-12-02 22:49:38 +01007425 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 HandleMouseHide(message, lParam);
7427
7428 if (message == WM_COMMAND)
7429 {
7430 if ((WORD)(LOWORD(wParam)) & 0x8000)
7431 {
7432 POINT mp;
7433 RECT rect;
7434
7435 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7436 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007437 vimmenu_T *menu;
7438
7439 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007441 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7443 (int)rect.right - 8,
7444 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007445 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 s_hwnd,
7447 NULL);
7448 /*
7449 * NOTE: The pop-up menu can eat the mouse up event.
7450 * We deal with this in normal.c.
7451 */
7452 }
7453 }
7454 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007455 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7457 /*
7458 * Give main window the focus back: this is so after
7459 * choosing a tearoff button you can start typing again
7460 * straight away.
7461 */
7462 (void)SetFocus(s_hwnd);
7463 return TRUE;
7464 }
7465 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7466 {
7467 DestroyWindow(hwnd);
7468 return TRUE;
7469 }
7470
Bram Moolenaar734a8672019-12-02 22:49:38 +01007471 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 if (message == WM_EXITSIZEMOVE)
7473 (void)SetActiveWindow(s_hwnd);
7474
7475 return FALSE;
7476}
7477#endif
7478
7479
7480/*
K.Takatad1c58992022-01-23 12:31:57 +00007481 * Computes the dialog base units based on the current dialog font.
7482 * We don't use the GetDialogBaseUnits() API, because we don't use the
7483 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 */
7485 static void
7486get_dialog_font_metrics(void)
7487{
7488 HDC hdc;
7489 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 SIZE size;
7491#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007492 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493#endif
7494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007496 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007497 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007498 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499#endif
7500 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007501 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
K.Takatad1c58992022-01-23 12:31:57 +00007503 hdc = GetDC(s_hwnd);
7504 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007505 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007506 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
K.Takataabe628e2022-01-23 16:25:17 +00007508 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007509 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510}
7511
7512#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7513/*
7514 * Create a pseudo-"tearoff menu" based on the child
7515 * items of a given menu pointer.
7516 */
7517 static void
7518gui_mch_tearoff(
7519 char_u *title,
7520 vimmenu_T *menu,
7521 int initX,
7522 int initY)
7523{
7524 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7525 int template_len;
7526 int nchar, textWidth, submenuWidth;
7527 DWORD lStyle;
7528 DWORD lExtendedStyle;
7529 WORD dlgwidth;
7530 WORD menuID;
7531 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007532 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 vimmenu_T *the_menu = menu;
7534 HWND hwnd;
7535 HDC hdc;
7536 HFONT font, oldFont;
7537 int col, spaceWidth, len;
7538 int columnWidths[2];
7539 char_u *label, *text;
7540 int acLen = 0;
7541 int nameLen;
7542 int padding0, padding1, padding2 = 0;
7543 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007544 int x;
7545 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007546# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007547 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
7551 /*
7552 * If this menu is already torn off, move it to the mouse position.
7553 */
7554 if (IsWindow(menu->tearoff_handle))
7555 {
7556 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007557 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {
7559 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7560 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7561 }
7562 return;
7563 }
7564
7565 /*
7566 * Create a new tearoff.
7567 */
7568 if (*title == MNU_HIDDEN_CHAR)
7569 title++;
7570
Bram Moolenaar734a8672019-12-02 22:49:38 +01007571 // Allocate memory to store the dialog template. It's made bigger when
7572 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 template_len = DLG_ALLOC_SIZE;
7574 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7575 if (p == NULL)
7576 return;
7577
7578 hwnd = GetDesktopWindow();
7579 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007580# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7582 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007583 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 use_lfSysmenu = TRUE;
7585 }
7586 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007587# endif
K.Takatad1c58992022-01-23 12:31:57 +00007588 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7589 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7590
7591 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
Bram Moolenaar734a8672019-12-02 22:49:38 +01007593 // Calculate width of a single space. Used for padding columns to the
7594 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007595 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
Bram Moolenaar734a8672019-12-02 22:49:38 +01007597 // Figure out max width of the text column, the accelerator column and the
7598 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 submenuWidth = 0;
7600 for (col = 0; col < 2; col++)
7601 {
7602 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007603 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007605 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 text = (col == 0) ? pmenu->dname : pmenu->actext;
7607 if (text != NULL && *text != NUL)
7608 {
7609 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7610 if (textWidth > columnWidths[col])
7611 columnWidths[col] = textWidth;
7612 }
7613 if (pmenu->children != NULL)
7614 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7615 }
7616 }
7617 if (columnWidths[1] == 0)
7618 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007619 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 if (submenuWidth != 0)
7621 columnWidths[0] += submenuWidth;
7622 else
7623 columnWidths[0] += spaceWidth;
7624 }
7625 else
7626 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007627 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7629 columnWidths[1] += submenuWidth;
7630 }
7631
7632 /*
7633 * Now find the total width of our 'menu'.
7634 */
7635 textWidth = columnWidths[0] + columnWidths[1];
7636 if (submenuWidth != 0)
7637 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007638 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7640 textWidth += submenuWidth;
7641 }
7642 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7643 if (textWidth > dlgwidth)
7644 dlgwidth = textWidth;
7645 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7646
Bram Moolenaar734a8672019-12-02 22:49:38 +01007647 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007648 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7651 *p++ = LOWORD(lStyle);
7652 *p++ = HIWORD(lStyle);
7653 *p++ = LOWORD(lExtendedStyle);
7654 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007655 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007657 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007659 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 else
7661 *p++ = PixelToDialogX(initX); // x
7662 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007663 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 else
7665 *p++ = PixelToDialogY(initY); // y
7666 *p++ = PixelToDialogX(dlgwidth); // cx
7667 ptrueheight = p;
7668 *p++ = 0; // dialog height: changed later anyway
7669 *p++ = 0; // Menu
7670 *p++ = 0; // Class
7671
Bram Moolenaar734a8672019-12-02 22:49:38 +01007672 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007674 ? (LPSTR)title
7675 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 p += nchar;
7677
K.Takatad1c58992022-01-23 12:31:57 +00007678 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007679# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007680 if (use_lfSysmenu)
7681 {
7682 // point size
7683 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7684 GetDeviceCaps(hdc, LOGPIXELSY));
7685 wcscpy(p, lfSysmenu.lfFaceName);
7686 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
K.Takatad1c58992022-01-23 12:31:57 +00007688 else
7689# endif
7690 {
7691 *p++ = DLG_FONT_POINT_SIZE; // point size
7692 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7693 }
7694 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
7696 /*
7697 * Loop over all the items in the menu.
7698 * But skip over the tearbar.
7699 */
7700 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7701 menu = menu->children->next;
7702 else
7703 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007704 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 for ( ; menu != NULL; menu = menu->next)
7706 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007707 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 continue;
7709 if (menu_is_separator(menu->dname))
7710 {
7711 sepPadding += 3;
7712 continue;
7713 }
7714
Bram Moolenaar734a8672019-12-02 22:49:38 +01007715 // Check if there still is plenty of room in the template. Make it
7716 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7718 {
7719 WORD *newp;
7720
7721 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7722 if (newp != NULL)
7723 {
7724 template_len += 4096;
7725 mch_memmove(newp, pdlgtemplate,
7726 (char *)p - (char *)pdlgtemplate);
7727 p = newp + (p - pdlgtemplate);
7728 pnumitems = newp + (pnumitems - pdlgtemplate);
7729 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7730 LocalFree(LocalHandle(pdlgtemplate));
7731 pdlgtemplate = newp;
7732 }
7733 }
7734
Bram Moolenaar734a8672019-12-02 22:49:38 +01007735 // Figure out minimal length of this menu label. Use "name" for the
7736 // actual text, "dname" for estimating the displayed size. "name"
7737 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 len = nameLen = (int)STRLEN(menu->name);
7739 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7740 (int)STRLEN(menu->dname))) / spaceWidth;
7741 len += padding0;
7742
7743 if (menu->actext != NULL)
7744 {
7745 acLen = (int)STRLEN(menu->actext);
7746 len += acLen;
7747 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7748 }
7749 else
7750 textWidth = 0;
7751 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7752 len += padding1;
7753
7754 if (menu->children == NULL)
7755 {
7756 padding2 = submenuWidth / spaceWidth;
7757 len += padding2;
7758 menuID = (WORD)(menu->id);
7759 }
7760 else
7761 {
7762 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007763 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 }
7765
Bram Moolenaar734a8672019-12-02 22:49:38 +01007766 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007767 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 if (label == NULL)
7769 break;
7770
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007771 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007772 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007774 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 while (padding0-- > 0)
7776 *text++ = ' ';
7777 if (menu->actext != NULL)
7778 {
7779 STRNCPY(text, menu->actext, acLen);
7780 text += acLen;
7781 }
7782 while (padding1-- > 0)
7783 *text++ = ' ';
7784 if (menu->children != NULL)
7785 {
7786 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7787 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7788 }
7789 else
7790 {
7791 while (padding2-- > 0)
7792 *text++ = ' ';
7793 }
7794 *text = NUL;
7795
7796 /*
7797 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7798 * W95/NT4 it makes the tear-off look more like a menu.
7799 */
7800 p = add_dialog_element(p,
7801 BS_PUSHBUTTON|BS_LEFT,
7802 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7803 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7804 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7805 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007806 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 vim_free(label);
7808 (*pnumitems)++;
7809 }
7810
7811 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7812
7813
Bram Moolenaar734a8672019-12-02 22:49:38 +01007814 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007815 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007816 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 (LPDLGTEMPLATE)pdlgtemplate,
7818 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007819 (DLGPROC)tearoff_callback,
7820 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821
7822 LocalFree(LocalHandle(pdlgtemplate));
7823 SelectFont(hdc, oldFont);
7824 DeleteObject(font);
7825 ReleaseDC(hwnd, hdc);
7826
7827 /*
7828 * Reassert ourselves as the active window. This is so that after creating
7829 * a tearoff, the user doesn't have to click with the mouse just to start
7830 * typing again!
7831 */
7832 (void)SetActiveWindow(s_hwnd);
7833
Bram Moolenaar734a8672019-12-02 22:49:38 +01007834 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 force_menu_update = TRUE;
7836}
7837#endif
7838
7839#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007840# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842/*
7843 * Create the toolbar, initially unpopulated.
7844 * (just like the menu, there are no defaults, it's all
7845 * set up through menu.vim)
7846 */
7847 static void
7848initialise_toolbar(void)
7849{
7850 InitCommonControls();
7851 s_toolbarhwnd = CreateToolbarEx(
7852 s_hwnd,
7853 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7854 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007855 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007856 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 IDR_TOOLBAR1, // id of initial bitmap
7858 NULL,
7859 0, // initial number of buttons
7860 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7861 TOOLBAR_BUTTON_HEIGHT,
7862 TOOLBAR_BUTTON_WIDTH,
7863 TOOLBAR_BUTTON_HEIGHT,
7864 sizeof(TBBUTTON)
7865 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007866
7867 // Remove transparency from the toolbar to prevent the main window
7868 // background colour showing through
7869 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7870 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7871
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007872 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
7874 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007875
7876 update_toolbar_size();
7877}
7878
7879 static void
7880update_toolbar_size(void)
7881{
7882 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007883 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007884
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007885 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007886 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7887 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7888 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7889 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7890
7891 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7892 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7893 //TRACE("button size: %d, %d", w, h);
7894 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7895 gui.toolbar_height = h + 6;
7896
7897 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7898 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7899
7900 // TODO:
7901 // Currently, this function only updates the size of toolbar buttons.
7902 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
7904
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007905 static LRESULT CALLBACK
7906toolbar_wndproc(
7907 HWND hwnd,
7908 UINT uMsg,
7909 WPARAM wParam,
7910 LPARAM lParam)
7911{
7912 HandleMouseHide(uMsg, lParam);
7913 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7914}
7915
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 static int
7917get_toolbar_bitmap(vimmenu_T *menu)
7918{
7919 int i = -1;
7920
7921 /*
7922 * Check user bitmaps first, unless builtin is specified.
7923 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007924 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
7926 char_u fname[MAXPATHL];
7927 HANDLE hbitmap = NULL;
7928
7929 if (menu->iconfile != NULL)
7930 {
7931 gui_find_iconfile(menu->iconfile, fname, "bmp");
7932 hbitmap = LoadImage(
7933 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007934 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 IMAGE_BITMAP,
7936 TOOLBAR_BUTTON_WIDTH,
7937 TOOLBAR_BUTTON_HEIGHT,
7938 LR_LOADFROMFILE |
7939 LR_LOADMAP3DCOLORS
7940 );
7941 }
7942
7943 /*
7944 * If the LoadImage call failed, or the "icon=" file
7945 * didn't exist or wasn't specified, try the menu name
7946 */
7947 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007948 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007949# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007950 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007951# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007952 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 hbitmap = LoadImage(
7954 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007955 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 IMAGE_BITMAP,
7957 TOOLBAR_BUTTON_WIDTH,
7958 TOOLBAR_BUTTON_HEIGHT,
7959 LR_LOADFROMFILE |
7960 LR_LOADMAP3DCOLORS
7961 );
7962
7963 if (hbitmap != NULL)
7964 {
7965 TBADDBITMAP tbAddBitmap;
7966
7967 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007968 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7971 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007972 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 }
7974 }
7975 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7976 i = menu->iconidx;
7977
7978 return i;
7979}
7980#endif
7981
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007982#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7983 static void
7984initialise_tabline(void)
7985{
7986 InitCommonControls();
7987
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007988 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007989 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007990 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007991 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007992 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007993
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007994 gui.tabline_height = TABLINE_HEIGHT;
7995
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007996 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007997}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007998
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007999/*
8000 * Get tabpage_T from POINT.
8001 */
8002 static tabpage_T *
8003GetTabFromPoint(
8004 HWND hWnd,
8005 POINT pt)
8006{
8007 tabpage_T *ptp = NULL;
8008
8009 if (gui_mch_showing_tabline())
8010 {
8011 TCHITTESTINFO htinfo;
8012 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008013 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008014 if (s_tabhwnd == hWnd)
8015 {
8016 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8017 if (idx != -1)
8018 ptp = find_tabpage(idx + 1);
8019 }
8020 }
8021 return ptp;
8022}
8023
8024static POINT s_pt = {0, 0};
8025static HCURSOR s_hCursor = NULL;
8026
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008027 static LRESULT CALLBACK
8028tabline_wndproc(
8029 HWND hwnd,
8030 UINT uMsg,
8031 WPARAM wParam,
8032 LPARAM lParam)
8033{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008034 POINT pt;
8035 tabpage_T *tp;
8036 RECT rect;
8037 int nCenter;
8038 int idx0;
8039 int idx1;
8040
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008041 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008042
8043 switch (uMsg)
8044 {
8045 case WM_LBUTTONDOWN:
8046 {
8047 s_pt.x = GET_X_LPARAM(lParam);
8048 s_pt.y = GET_Y_LPARAM(lParam);
8049 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008050 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008051 break;
8052 }
8053 case WM_MOUSEMOVE:
8054 if (GetCapture() == hwnd
8055 && ((wParam & MK_LBUTTON)) != 0)
8056 {
8057 pt.x = GET_X_LPARAM(lParam);
8058 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008059 if (abs(pt.x - s_pt.x) >
8060 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008061 {
8062 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8063
8064 tp = GetTabFromPoint(hwnd, pt);
8065 if (tp != NULL)
8066 {
8067 idx0 = tabpage_index(curtab) - 1;
8068 idx1 = tabpage_index(tp) - 1;
8069
8070 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8071 nCenter = rect.left + (rect.right - rect.left) / 2;
8072
Bram Moolenaar734a8672019-12-02 22:49:38 +01008073 // Check if the mouse cursor goes over the center of
8074 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008075 if ((idx0 < idx1) && (nCenter < pt.x))
8076 {
8077 tabpage_move(idx1 + 1);
8078 update_screen(0);
8079 }
8080 else if ((idx1 < idx0) && (pt.x < nCenter))
8081 {
8082 tabpage_move(idx1);
8083 update_screen(0);
8084 }
8085 }
8086 }
8087 }
8088 break;
8089 case WM_LBUTTONUP:
8090 {
8091 if (GetCapture() == hwnd)
8092 {
8093 SetCursor(s_hCursor);
8094 ReleaseCapture();
8095 }
8096 break;
8097 }
8098 default:
8099 break;
8100 }
8101
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008102 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8103}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008104#endif
8105
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8107/*
8108 * Make the GUI window come to the foreground.
8109 */
8110 void
8111gui_mch_set_foreground(void)
8112{
8113 if (IsIconic(s_hwnd))
8114 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8115 SetForegroundWindow(s_hwnd);
8116}
8117#endif
8118
8119#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8120 static void
8121dyn_imm_load(void)
8122{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008123 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 if (hLibImm == NULL)
8125 return;
8126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008128 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008130 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008132 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008134 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008136 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008138 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008139 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008140 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008141 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008142 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008144 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008146 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008147 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008148 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
K.Takatab0b2b732022-01-19 12:59:21 +00008150 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 || pImmGetContext == NULL
8152 || pImmAssociateContext == NULL
8153 || pImmReleaseContext == NULL
8154 || pImmGetOpenStatus == NULL
8155 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008156 || pImmGetCompositionFontW == NULL
8157 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008159 || pImmGetConversionStatus == NULL
8160 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {
8162 FreeLibrary(hLibImm);
8163 hLibImm = NULL;
8164 pImmGetContext = NULL;
8165 return;
8166 }
8167
8168 return;
8169}
8170
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171#endif
8172
8173#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8174
8175# ifdef FEAT_XPM_W32
8176# define IMAGE_XPM 100
8177# endif
8178
8179typedef struct _signicon_t
8180{
8181 HANDLE hImage;
8182 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008183# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008184 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186} signicon_t;
8187
8188 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008189gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 signicon_t *sign;
8192 int x, y, w, h;
8193
8194 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8195 return;
8196
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008197# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008198 if (IS_ENABLE_DIRECTX())
8199 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008200# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 x = TEXT_X(col);
8203 y = TEXT_Y(row);
8204 w = gui.char_width * 2;
8205 h = gui.char_height;
8206 switch (sign->uType)
8207 {
8208 case IMAGE_BITMAP:
8209 {
8210 HDC hdcMem;
8211 HBITMAP hbmpOld;
8212
8213 hdcMem = CreateCompatibleDC(s_hdc);
8214 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8215 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8216 SelectObject(hdcMem, hbmpOld);
8217 DeleteDC(hdcMem);
8218 }
8219 break;
8220 case IMAGE_ICON:
8221 case IMAGE_CURSOR:
8222 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8223 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008224# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 case IMAGE_XPM:
8226 {
8227 HDC hdcMem;
8228 HBITMAP hbmpOld;
8229
8230 hdcMem = CreateCompatibleDC(s_hdc);
8231 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008232 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8234
8235 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008236 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8238 SelectObject(hdcMem, hbmpOld);
8239 DeleteDC(hdcMem);
8240 }
8241 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 }
8244}
8245
8246 static void
8247close_signicon_image(signicon_t *sign)
8248{
8249 if (sign)
8250 switch (sign->uType)
8251 {
8252 case IMAGE_BITMAP:
8253 DeleteObject((HGDIOBJ)sign->hImage);
8254 break;
8255 case IMAGE_CURSOR:
8256 DestroyCursor((HCURSOR)sign->hImage);
8257 break;
8258 case IMAGE_ICON:
8259 DestroyIcon((HICON)sign->hImage);
8260 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008261# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 case IMAGE_XPM:
8263 DeleteObject((HBITMAP)sign->hImage);
8264 DeleteObject((HBITMAP)sign->hShape);
8265 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 }
8268}
8269
8270 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008271gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 signicon_t sign, *psign;
8274 char_u *ext;
8275
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008277 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 if (ext > signfile)
8279 {
8280 int do_load = 1;
8281
8282 if (!STRICMP(ext, ".bmp"))
8283 sign.uType = IMAGE_BITMAP;
8284 else if (!STRICMP(ext, ".ico"))
8285 sign.uType = IMAGE_ICON;
8286 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8287 sign.uType = IMAGE_CURSOR;
8288 else
8289 do_load = 0;
8290
8291 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008292 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 gui.char_width * 2, gui.char_height,
8294 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008295# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (!STRICMP(ext, ".xpm"))
8297 {
8298 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008299 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8300 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
8304
8305 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008306 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 *psign = sign;
8308
8309 if (!psign)
8310 {
8311 if (sign.hImage)
8312 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008313 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
8315 return (void *)psign;
8316
8317}
8318
8319 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008320gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321{
8322 if (sign)
8323 {
8324 close_signicon_image((signicon_t *)sign);
8325 vim_free(sign);
8326 }
8327}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008330#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331
Bram Moolenaar734a8672019-12-02 22:49:38 +01008332/*
8333 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008334 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008336 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8338 * to get current mouse position).
8339 *
8340 * Trying to use as more Windows services as possible, and as less
8341 * IE version as possible :)).
8342 *
8343 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8344 * BalloonEval struct.
8345 * 2) Enable/Disable simply create/kill BalloonEval Timer
8346 * 3) When there was enough inactivity, timer procedure posts
8347 * async request to debugger
8348 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8349 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008350 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 */
8352
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008353 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008354make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008355{
K.Takata76687d22022-01-25 10:31:37 +00008356 TOOLINFOW *pti;
8357 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008358
K.Takata76687d22022-01-25 10:31:37 +00008359 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008360 if (pti == NULL)
8361 return;
8362
8363 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8364 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8365 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008366 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008367
8368 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8369 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8370
K.Takata76687d22022-01-25 10:31:37 +00008371 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008372 pti->uFlags = TTF_SUBCLASS;
8373 pti->hwnd = beval->target;
8374 pti->hinst = 0; // Don't use string resources
8375 pti->uId = ID_BEVAL_TOOLTIP;
8376
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008377 pti->lpszText = LPSTR_TEXTCALLBACKW;
8378 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8379 pti->lParam = (LPARAM)beval->tofree;
8380 // switch multiline tooltips on
8381 if (GetClientRect(s_textArea, &rect))
8382 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8383 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008384
8385 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8386 pti->rect.left = pt.x - 3;
8387 pti->rect.top = pt.y - 3;
8388 pti->rect.right = pt.x + 3;
8389 pti->rect.bottom = pt.y + 3;
8390
8391 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8392 // Make tooltip appear sooner.
8393 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8394 // I've performed some tests and it seems the longest possible life time
8395 // of tooltip is 30 seconds.
8396 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8397 /*
8398 * HACK: force tooltip to appear, because it'll not appear until
8399 * first mouse move. D*mn M$
8400 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8401 */
8402 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8403 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8404 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008405}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008408delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008410 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008414beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008415 HWND hwnd UNUSED,
8416 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008417 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008418 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 POINT pt;
8421 RECT rect;
8422
8423 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8424 return;
8425
8426 GetCursorPos(&pt);
8427 if (WindowFromPoint(pt) != s_textArea)
8428 return;
8429
8430 ScreenToClient(s_textArea, &pt);
8431 GetClientRect(s_textArea, &rect);
8432 if (!PtInRect(&rect, pt))
8433 return;
8434
K.Takataa8ec4912022-02-03 14:32:33 +00008435 if (last_user_activity > 0
8436 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 && (cur_beval->showState != ShS_PENDING
8438 || abs(cur_beval->x - pt.x) > 3
8439 || abs(cur_beval->y - pt.y) > 3))
8440 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008441 // Pointer resting in one place long enough, it's time to show
8442 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 cur_beval->showState = ShS_PENDING;
8444 cur_beval->x = pt.x;
8445 cur_beval->y = pt.y;
8446
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 if (cur_beval->msgCB != NULL)
8448 (*cur_beval->msgCB)(cur_beval, 0);
8449 }
8450}
8451
8452 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008453gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
K.Takataa8ec4912022-02-03 14:32:33 +00008455 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456}
8457
8458 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008459gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 if (beval == NULL)
8462 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008463 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8464 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465}
8466
8467 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008468gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
8470 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008471
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008472 vim_free(beval->msg);
8473 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8474 if (beval->msg == NULL)
8475 {
8476 delete_tooltip(beval);
8477 beval->showState = ShS_NEUTRAL;
8478 return;
8479 }
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (beval->showState == ShS_SHOWING)
8482 return;
8483 GetCursorPos(&pt);
8484 ScreenToClient(s_textArea, &pt);
8485
8486 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008488 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 gui_mch_disable_beval_area(cur_beval);
8490 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008491 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493}
8494
8495 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008496gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008497 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008498 char_u *mesg,
8499 void (*mesgCB)(BalloonEval *, int),
8500 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008502 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 BalloonEval *beval;
8504
8505 if (mesg != NULL && mesgCB != NULL)
8506 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008507 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 return NULL;
8509 }
8510
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008511 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (beval != NULL)
8513 {
8514 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
8516 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 beval->msg = mesg;
8518 beval->msgCB = mesgCB;
8519 beval->clientData = clientData;
8520
8521 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 cur_beval = beval;
8523
8524 if (p_beval)
8525 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
8527 return beval;
8528}
8529
8530 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008531Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008533 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 return;
8535
8536 if (cur_beval != NULL)
8537 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008538 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008540 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008541 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008542 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 delete_tooltip(cur_beval);
8544 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
8546 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008547 break;
8548 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008549 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008550 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008551 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008552 info->lpszText = (LPSTR)info->lParam;
8553 info->uFlags |= TTF_DI_SETITEM;
8554 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008555 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008556 case TTN_GETDISPINFOW:
8557 {
8558 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008559 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008560 info->lpszText = (LPWSTR)info->lParam;
8561 info->uFlags |= TTF_DI_SETITEM;
8562 }
8563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 }
8565 }
8566}
8567
8568 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008569track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8572 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008573 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574}
8575
8576 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008577gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008579# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008580 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008581# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008582 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 vim_free(beval);
8584}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008585#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586
8587#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8588/*
8589 * We have multiple signs to draw at the same location. Draw the
8590 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8591 */
8592 void
8593netbeans_draw_multisign_indicator(int row)
8594{
8595 int i;
8596 int y;
8597 int x;
8598
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008599 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008600 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 x = 0;
8603 y = TEXT_Y(row);
8604
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008605# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008606 if (IS_ENABLE_DIRECTX())
8607 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008608# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 for (i = 0; i < gui.char_height - 3; i++)
8611 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8612
8613 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8614 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8615 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8616 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8617 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8618 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8619 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8620}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008621#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008622
8623#if defined(FEAT_EVAL) || defined(PROTO)
8624 int
8625test_gui_w32_sendevent(dict_T *args)
8626{
8627 char_u *event;
8628 INPUT inputs[1];
8629
Bram Moolenaard61efa52022-07-23 09:52:04 +01008630 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008631 if (event == NULL)
8632 return FALSE;
8633
8634 ZeroMemory(inputs, sizeof(inputs));
8635
8636 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8637 {
8638 WORD vkCode;
8639
Bram Moolenaard61efa52022-07-23 09:52:04 +01008640 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008641 if (vkCode <= 0 || vkCode >= 0xFF)
8642 {
8643 semsg(_(e_invalid_argument_nr), (long)vkCode);
8644 return FALSE;
8645 }
8646
8647 inputs[0].type = INPUT_KEYBOARD;
8648 inputs[0].ki.wVk = vkCode;
8649 if (STRICMP(event, "keyup") == 0)
8650 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
8651 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8652 }
8653 else
8654 semsg(_(e_invalid_argument_str), event);
8655
8656 vim_free(event);
8657
8658 return TRUE;
8659}
8660#endif