blob: 588b5fa3e5497c21ae3becbd376a94f742a18a28 [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
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000043#else
44# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010045#endif
46
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020047#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
48 int
49gui_mch_set_rendering_options(char_u *s)
50{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010051# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020052 char_u *p, *q;
53
54 int dx_enable = 0;
55 int dx_flags = 0;
56 float dx_gamma = 0.0f;
57 float dx_contrast = 0.0f;
58 float dx_level = 0.0f;
59 int dx_geom = 0;
60 int dx_renmode = 0;
61 int dx_taamode = 0;
62
Bram Moolenaar734a8672019-12-02 22:49:38 +010063 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100124 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100132
Bram Moolenaar734a8672019-12-02 22:49:38 +0100133 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100163# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183
Bram Moolenaar734a8672019-12-02 22:49:38 +0100184#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100185#ifdef DEBUG
186# include <tchar.h>
187#endif
188
Bram Moolenaar734a8672019-12-02 22:49:38 +0100189// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100190#ifndef PROTO
191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100192# ifndef __MINGW32__
193# include <shellapi.h>
194# endif
195# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
196# include <commctrl.h>
197# endif
198# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100199
Bram Moolenaar734a8672019-12-02 22:49:38 +0100200#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
202#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100203# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100204#endif
205
Bram Moolenaar734a8672019-12-02 22:49:38 +0100206// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100207#define DLG_PADDING_X 10
208#define DLG_PADDING_Y 10
209#define DLG_OLD_STYLE_PADDING_X 5
210#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100211#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100212#define DLG_VERT_PADDING_Y 4
213#define DLG_ICON_WIDTH 34
214#define DLG_ICON_HEIGHT 34
215#define DLG_MIN_WIDTH 150
216#define DLG_FONT_NAME "MS Sans Serif"
217#define DLG_FONT_POINT_SIZE 8
218#define DLG_MIN_MAX_WIDTH 400
219#define DLG_MIN_MAX_HEIGHT 400
220
Bram Moolenaar734a8672019-12-02 22:49:38 +0100221#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224# define WM_XBUTTONDOWN 0x020B
225# define WM_XBUTTONUP 0x020C
226# define WM_XBUTTONDBLCLK 0x020D
227# define MK_XBUTTON1 0x0020
228# define MK_XBUTTON2 0x0040
229#endif
230
K.Takatac81e9bf2022-01-16 14:15:49 +0000231#ifndef WM_DPICHANGED
232# define WM_DPICHANGED 0x02E0
233#endif
234
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237 * Define a few things for generating prototypes. This is just to avoid
238 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100240# define APIENTRY
241# define CALLBACK
242# define CONST
243# define FAR
244# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200245# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200246# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247# define _cdecl
248typedef int BOOL;
249typedef int BYTE;
250typedef int DWORD;
251typedef int WCHAR;
252typedef int ENUMLOGFONT;
253typedef int FINDREPLACE;
254typedef int HANDLE;
255typedef int HBITMAP;
256typedef int HBRUSH;
257typedef int HDROP;
258typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100259typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100260typedef int LPARAM;
261typedef int LPCREATESTRUCT;
262typedef int LPCSTR;
263typedef int LPCTSTR;
264typedef int LPRECT;
265typedef int LPSTR;
266typedef int LPWINDOWPOS;
267typedef int LPWORD;
268typedef int LRESULT;
269typedef int HRESULT;
270# undef MSG
271typedef int MSG;
272typedef int NEWTEXTMETRIC;
273typedef int OSVERSIONINFO;
274typedef int PWORD;
275typedef int RECT;
276typedef int UINT;
277typedef int WORD;
278typedef int WPARAM;
279typedef int POINT;
280typedef void *HINSTANCE;
281typedef void *HMENU;
282typedef void *HWND;
283typedef void *HDC;
284typedef void VOID;
285typedef int LPNMHDR;
286typedef int LONG;
287typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200288typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200289typedef int COLORREF;
290typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100291#endif
292
293#ifndef GET_X_LPARAM
294# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
295#endif
296
K.Takata45f9cfb2022-01-21 11:11:00 +0000297static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100298static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299static void clear_rect(RECT *rcp);
300
Bram Moolenaar734a8672019-12-02 22:49:38 +0100301static WORD s_dlgfntheight; // height of the dialog font
302static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef FEAT_MENU
305static HMENU s_menuBar = NULL;
306#endif
307#ifdef FEAT_TEAROFF
308static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaar734a8672019-12-02 22:49:38 +0100312// Flag that is set while processing a message that must not be interrupted by
313// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314static int s_busy_processing = FALSE;
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100317
318#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000319static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200322static int s_findrep_is_find; // TRUE for find dialog, FALSE
323 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324#endif
325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
Bram Moolenaar734a8672019-12-02 22:49:38 +0100347static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100348
349static int s_need_activate = FALSE;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351// This variable is set when waiting for an event, which is the only moment
352// scrollbar dragging can be done directly. It's not allowed while commands
353// are executed, because it may move the cursor and that may cause unexpected
354// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100355static int allow_scrollbar = FALSE;
356
K.Takatac81e9bf2022-01-16 14:15:49 +0000357#ifndef _DPI_AWARENESS_CONTEXTS_
358typedef HANDLE DPI_AWARENESS_CONTEXT;
359
360typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000361 DPI_AWARENESS_INVALID = -1,
362 DPI_AWARENESS_UNAWARE = 0,
363 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000364 DPI_AWARENESS_PER_MONITOR_AWARE = 2
365} DPI_AWARENESS;
366
K.Takatab0b2b732022-01-19 12:59:21 +0000367# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
368# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000369# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
370# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
371# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
372#endif
373
374#define DEFAULT_DPI 96
375static int s_dpi = DEFAULT_DPI;
376static BOOL s_in_dpichanged = FALSE;
377static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
378
379static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
380static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
381static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
382//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
383static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
384static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
385
386 static UINT WINAPI
387stubGetDpiForSystem(void)
388{
389 HWND hwnd = GetDesktopWindow();
390 HDC hdc = GetWindowDC(hwnd);
391 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
392 ReleaseDC(hwnd, hdc);
393 return dpi;
394}
395
396 static int WINAPI
397stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
398{
399 return GetSystemMetrics(nIndex);
400}
401
402 static int
403adjust_fontsize_by_dpi(int size)
404{
405 return size * s_dpi / (int)pGetDpiForSystem();
406}
407
408 static int
409adjust_by_system_dpi(int size)
410{
411 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
412}
413
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100414#if defined(FEAT_DIRECTX)
415 static int
416directx_enabled(void)
417{
418 if (s_dwc != NULL)
419 return 1;
420 else if (s_directx_load_attempted)
421 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100422 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100423 DWrite_Init();
424 s_directx_load_attempted = 1;
425 s_dwc = DWriteContext_Open();
426 directx_binddc();
427 return s_dwc != NULL ? 1 : 0;
428}
429
430 static void
431directx_binddc(void)
432{
433 if (s_textArea != NULL)
434 {
435 RECT rect;
436 GetClientRect(s_textArea, &rect);
437 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
438 }
439}
440#endif
441
Bram Moolenaar734a8672019-12-02 22:49:38 +0100442extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100443
444static struct
445{
446 UINT key_sym;
447 char_u vim_code0;
448 char_u vim_code1;
449} special_keys[] =
450{
451 {VK_UP, 'k', 'u'},
452 {VK_DOWN, 'k', 'd'},
453 {VK_LEFT, 'k', 'l'},
454 {VK_RIGHT, 'k', 'r'},
455
456 {VK_F1, 'k', '1'},
457 {VK_F2, 'k', '2'},
458 {VK_F3, 'k', '3'},
459 {VK_F4, 'k', '4'},
460 {VK_F5, 'k', '5'},
461 {VK_F6, 'k', '6'},
462 {VK_F7, 'k', '7'},
463 {VK_F8, 'k', '8'},
464 {VK_F9, 'k', '9'},
465 {VK_F10, 'k', ';'},
466
467 {VK_F11, 'F', '1'},
468 {VK_F12, 'F', '2'},
469 {VK_F13, 'F', '3'},
470 {VK_F14, 'F', '4'},
471 {VK_F15, 'F', '5'},
472 {VK_F16, 'F', '6'},
473 {VK_F17, 'F', '7'},
474 {VK_F18, 'F', '8'},
475 {VK_F19, 'F', '9'},
476 {VK_F20, 'F', 'A'},
477
478 {VK_F21, 'F', 'B'},
479#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100480 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100481#endif
482 {VK_F22, 'F', 'C'},
483 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485
486 {VK_HELP, '%', '1'},
487 {VK_BACK, 'k', 'b'},
488 {VK_INSERT, 'k', 'I'},
489 {VK_DELETE, 'k', 'D'},
490 {VK_HOME, 'k', 'h'},
491 {VK_END, '@', '7'},
492 {VK_PRIOR, 'k', 'P'},
493 {VK_NEXT, 'k', 'N'},
494 {VK_PRINT, '%', '9'},
495 {VK_ADD, 'K', '6'},
496 {VK_SUBTRACT, 'K', '7'},
497 {VK_DIVIDE, 'K', '8'},
498 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_DECIMAL, 'K', 'B'},
501
502 {VK_NUMPAD0, 'K', 'C'},
503 {VK_NUMPAD1, 'K', 'D'},
504 {VK_NUMPAD2, 'K', 'E'},
505 {VK_NUMPAD3, 'K', 'F'},
506 {VK_NUMPAD4, 'K', 'G'},
507 {VK_NUMPAD5, 'K', 'H'},
508 {VK_NUMPAD6, 'K', 'I'},
509 {VK_NUMPAD7, 'K', 'J'},
510 {VK_NUMPAD8, 'K', 'K'},
511 {VK_NUMPAD9, 'K', 'L'},
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100514 {VK_SPACE, ' ', NUL},
515 {VK_TAB, TAB, NUL},
516 {VK_ESCAPE, ESC, NUL},
517 {NL, NL, NUL},
518 {CAR, CAR, NUL},
519
Bram Moolenaar734a8672019-12-02 22:49:38 +0100520 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521 {0, 0, 0}
522};
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525static int s_button_pending = -1;
526
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
528// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_getting_focus = FALSE;
530
531static int s_x_pending;
532static int s_y_pending;
533static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200534static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100535static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200536static int dead_key = 0; // 0: no dead key, 1: dead key pressed
537static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
538 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100540#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100541// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
543static void TrackUserActivity(UINT uMsg);
544#endif
545
546/*
547 * For control IME.
548 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100549 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100550 */
K.Takata4ac893f2022-01-20 12:44:28 +0000551#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100554// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100555static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100556#endif
557
558#ifdef FEAT_MBYTE_IME
559static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
560#endif
561
562#if defined(FEAT_BROWSE)
563static char_u *convert_filter(char_u *s);
564#endif
565
566#ifdef DEBUG_PRINT_ERROR
567/*
568 * Print out the last Windows error message
569 */
570 static void
571print_windows_error(void)
572{
573 LPVOID lpMsgBuf;
574
575 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
576 NULL, GetLastError(),
577 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
578 (LPTSTR) &lpMsgBuf, 0, NULL);
579 TRACE1("Error: %s\n", lpMsgBuf);
580 LocalFree(lpMsgBuf);
581}
582#endif
583
584/*
585 * Cursor blink functions.
586 *
587 * This is a simple state machine:
588 * BLINK_NONE not blinking at all
589 * BLINK_OFF blinking, cursor is not shown
590 * BLINK_ON blinking, cursor is shown
591 */
592
593#define BLINK_NONE 0
594#define BLINK_OFF 1
595#define BLINK_ON 2
596
597static int blink_state = BLINK_NONE;
598static long_u blink_waittime = 700;
599static long_u blink_ontime = 400;
600static long_u blink_offtime = 250;
601static UINT blink_timer = 0;
602
Bram Moolenaar703a8042016-06-04 16:24:32 +0200603 int
604gui_mch_is_blinking(void)
605{
606 return blink_state != BLINK_NONE;
607}
608
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200609 int
610gui_mch_is_blink_off(void)
611{
612 return blink_state == BLINK_OFF;
613}
614
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615 void
616gui_mch_set_blinking(long wait, long on, long off)
617{
618 blink_waittime = wait;
619 blink_ontime = on;
620 blink_offtime = off;
621}
622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100623 static VOID CALLBACK
624_OnBlinkTimer(
625 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100628 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100629{
630 MSG msg;
631
632 /*
633 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
634 */
635
636 KillTimer(NULL, idEvent);
637
Bram Moolenaar734a8672019-12-02 22:49:38 +0100638 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000639 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100640 ;
641
642 if (blink_state == BLINK_ON)
643 {
644 gui_undraw_cursor();
645 blink_state = BLINK_OFF;
646 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
647 (TIMERPROC)_OnBlinkTimer);
648 }
649 else
650 {
651 gui_update_cursor(TRUE, FALSE);
652 blink_state = BLINK_ON;
653 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100654 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100655 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100656 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100657}
658
659 static void
660gui_mswin_rm_blink_timer(void)
661{
662 MSG msg;
663
664 if (blink_timer != 0)
665 {
666 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100667 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000668 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 ;
670 blink_timer = 0;
671 }
672}
673
674/*
675 * Stop the cursor blinking. Show the cursor if it wasn't shown.
676 */
677 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100678gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679{
680 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100681 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100682 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100684 gui_mch_flush();
685 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 blink_state = BLINK_NONE;
687}
688
689/*
690 * Start the cursor blinking. If it was already blinking, this restarts the
691 * waiting time and shows the cursor.
692 */
693 void
694gui_mch_start_blink(void)
695{
696 gui_mswin_rm_blink_timer();
697
Bram Moolenaar734a8672019-12-02 22:49:38 +0100698 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100699 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
700 {
701 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
702 (TIMERPROC)_OnBlinkTimer);
703 blink_state = BLINK_ON;
704 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100705 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 }
707}
708
709/*
710 * Call-back routines.
711 */
712
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100713 static VOID CALLBACK
714_OnTimer(
715 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100716 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100718 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719{
720 MSG msg;
721
722 /*
723 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
724 */
725 KillTimer(NULL, idEvent);
726 s_timed_out = TRUE;
727
Bram Moolenaar734a8672019-12-02 22:49:38 +0100728 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000729 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730 ;
731 if (idEvent == s_wait_timer)
732 s_wait_timer = 0;
733}
734
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100735 static void
736_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100737 HWND hwnd UNUSED,
738 UINT ch UNUSED,
739 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100740{
741 dead_key = 1;
742}
743
744/*
745 * Convert Unicode character "ch" to bytes in "string[slen]".
746 * When "had_alt" is TRUE the ALT key was included in "ch".
747 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200748 * Because the Windows API uses UTF-16, we have to deal with surrogate
749 * pairs; this is where we choose to deal with them: if "ch" is a high
750 * surrogate, it will be stored, and the length returned will be zero; the next
751 * char_to_string call will then include the high surrogate, decoding the pair
752 * of UTF-16 code units to a single Unicode code point, presuming it is the
753 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100754 */
755 static int
756char_to_string(int ch, char_u *string, int slen, int had_alt)
757{
758 int len;
759 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200761 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200763 if (surrogate_pending_ch != 0)
764 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100765 // We don't guarantee ch is a low surrogate to match the high surrogate
766 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200767 wstring[0] = surrogate_pending_ch;
768 wstring[1] = ch;
769 surrogate_pending_ch = 0;
770 len = 2;
771 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100772 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200773 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100774 // We don't have the entire code point yet, only the first UTF-16 code
775 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200776 surrogate_pending_ch = ch;
777 return 0;
778 }
779 else
780 {
781 wstring[0] = ch;
782 len = 1;
783 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200784
Bram Moolenaar734a8672019-12-02 22:49:38 +0100785 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
786 // "enc_codepage" is non-zero use the standard Win32 function,
787 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200788 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100789 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
791 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100792 // If we had included the ALT key into the character but now the
793 // upper bit is no longer set, that probably means the conversion
794 // failed. Convert the original character and set the upper bit
795 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100797 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798 wstring[0] = ch & 0x7f;
799 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
800 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100801 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 }
804 }
805 else
806 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200807 ws = utf16_to_enc(wstring, &len);
808 if (ws == NULL)
809 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100810 else
811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100812 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 len = slen;
814 mch_memmove(string, ws, len);
815 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816 }
817 }
818
819 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 {
821 string[0] = ch;
822 len = 1;
823 }
824
825 for (i = 0; i < len; ++i)
826 if (string[i] == CSI && len <= slen - 2)
827 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100828 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
830 string[++i] = KS_EXTRA;
831 string[++i] = (int)KE_CSI;
832 len += 2;
833 }
834
835 return len;
836}
837
838/*
839 * Key hit, add it to the input buffer.
840 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100841 static void
842_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100843 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100845 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100846{
847 char_u string[40];
848 int len = 0;
849
850 dead_key = 0;
851
852 len = char_to_string(ch, string, 40, FALSE);
853 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
854 {
855 trash_input_buf();
856 got_int = TRUE;
857 }
858
859 add_to_input_buf(string, len);
860}
861
862/*
863 * Alt-Key hit, add it to the input buffer.
864 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100865 static void
866_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100867 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100868 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100869 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100871 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872 int len;
873 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100874 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100875
876 dead_key = 0;
877
Bram Moolenaar734a8672019-12-02 22:49:38 +0100878 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879
Bram Moolenaar734a8672019-12-02 22:49:38 +0100880 // OK, we have a character key (given by ch) which was entered with the
881 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
882 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
883 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100884 modifiers = MOD_MASK_ALT;
885 if (GetKeyState(VK_SHIFT) & 0x8000)
886 modifiers |= MOD_MASK_SHIFT;
887 if (GetKeyState(VK_CONTROL) & 0x8000)
888 modifiers |= MOD_MASK_CTRL;
889
890 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100891 // remove the SHIFT modifier for keys where it's already included, e.g.,
892 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200893 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100894
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200895 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
896 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100897 if (ch == CSI)
898 ch = K_CSI;
899
900 len = 0;
901 if (modifiers)
902 {
903 string[len++] = CSI;
904 string[len++] = KS_MODIFIER;
905 string[len++] = modifiers;
906 }
907
908 if (IS_SPECIAL((int)ch))
909 {
910 string[len++] = CSI;
911 string[len++] = K_SECOND((int)ch);
912 string[len++] = K_THIRD((int)ch);
913 }
914 else
915 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100916 // Although the documentation isn't clear about it, we assume "ch" is
917 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100918 len += char_to_string(ch, string + len, 40 - len, TRUE);
919 }
920
921 add_to_input_buf(string, len);
922}
923
924 static void
925_OnMouseEvent(
926 int button,
927 int x,
928 int y,
929 int repeated_click,
930 UINT keyFlags)
931{
932 int vim_modifiers = 0x0;
933
934 s_getting_focus = FALSE;
935
936 if (keyFlags & MK_SHIFT)
937 vim_modifiers |= MOUSE_SHIFT;
938 if (keyFlags & MK_CONTROL)
939 vim_modifiers |= MOUSE_CTRL;
940 if (GetKeyState(VK_MENU) & 0x8000)
941 vim_modifiers |= MOUSE_ALT;
942
943 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
944}
945
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100946 static void
947_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100948 HWND hwnd UNUSED,
949 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950 int x,
951 int y,
952 UINT keyFlags)
953{
954 static LONG s_prevTime = 0;
955
956 LONG currentTime = GetMessageTime();
957 int button = -1;
958 int repeated_click;
959
Bram Moolenaar734a8672019-12-02 22:49:38 +0100960 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100961 (void)SetFocus(s_hwnd);
962
963 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
964 button = MOUSE_LEFT;
965 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
966 button = MOUSE_MIDDLE;
967 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
968 button = MOUSE_RIGHT;
969 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
970 {
971#ifndef GET_XBUTTON_WPARAM
972# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
973#endif
974 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
975 }
976 else if (s_uMsg == WM_CAPTURECHANGED)
977 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100978 // on W95/NT4, somehow you get in here with an odd Msg
979 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100980 if (s_button_pending == MOUSE_LEFT)
981 button = MOUSE_RIGHT;
982 else
983 button = MOUSE_LEFT;
984 }
985 if (button >= 0)
986 {
987 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
988
989 /*
990 * Holding down the left and right buttons simulates pushing the middle
991 * button.
992 */
993 if (repeated_click
994 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
995 || (button == MOUSE_RIGHT
996 && s_button_pending == MOUSE_LEFT)))
997 {
998 /*
999 * Hmm, gui.c will ignore more than one button down at a time, so
1000 * pretend we let go of it first.
1001 */
1002 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1003 button = MOUSE_MIDDLE;
1004 repeated_click = FALSE;
1005 s_button_pending = -1;
1006 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1007 }
1008 else if ((repeated_click)
1009 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1010 {
1011 if (s_button_pending > -1)
1012 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001013 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1014 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001015 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001016 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001017 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1018 }
1019 else
1020 {
1021 /*
1022 * If this is the first press (i.e. not a multiple click) don't
1023 * action immediately, but store and wait for:
1024 * i) button-up
1025 * ii) mouse move
1026 * iii) another button press
1027 * before using it.
1028 * This enables us to make left+right simulate middle button,
1029 * without left or right being actioned first. The side-effect is
1030 * that if you click and hold the mouse without dragging, the
1031 * cursor doesn't move until you release the button. In practice
1032 * this is hardly a problem.
1033 */
1034 s_button_pending = button;
1035 s_x_pending = x;
1036 s_y_pending = y;
1037 s_kFlags_pending = keyFlags;
1038 }
1039
1040 s_prevTime = currentTime;
1041 }
1042}
1043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001044 static void
1045_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001046 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001047 int x,
1048 int y,
1049 UINT keyFlags)
1050{
1051 int button;
1052
1053 s_getting_focus = FALSE;
1054 if (s_button_pending > -1)
1055 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001056 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057 _OnMouseEvent(s_button_pending, s_x_pending,
1058 s_y_pending, FALSE, s_kFlags_pending);
1059 s_button_pending = -1;
1060 }
1061 if (s_uMsg == WM_MOUSEMOVE)
1062 {
1063 /*
1064 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1065 * down.
1066 */
1067 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1068 | MK_XBUTTON1 | MK_XBUTTON2)))
1069 {
1070 gui_mouse_moved(x, y);
1071 return;
1072 }
1073
1074 /*
1075 * While button is down, keep grabbing mouse move events when
1076 * the mouse goes outside the window
1077 */
1078 SetCapture(s_textArea);
1079 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001080 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001081 }
1082 else
1083 {
1084 ReleaseCapture();
1085 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001086 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001087 }
1088
1089 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1090}
1091
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001092 static void
1093_OnSizeTextArea(
1094 HWND hwnd UNUSED,
1095 UINT state UNUSED,
1096 int cx UNUSED,
1097 int cy UNUSED)
1098{
1099#if defined(FEAT_DIRECTX)
1100 if (IS_ENABLE_DIRECTX())
1101 directx_binddc();
1102#endif
1103}
1104
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105#ifdef FEAT_MENU
1106/*
1107 * Find the vimmenu_T with the given id
1108 */
1109 static vimmenu_T *
1110gui_mswin_find_menu(
1111 vimmenu_T *pMenu,
1112 int id)
1113{
1114 vimmenu_T *pChildMenu;
1115
1116 while (pMenu)
1117 {
1118 if (pMenu->id == (UINT)id)
1119 break;
1120 if (pMenu->children != NULL)
1121 {
1122 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1123 if (pChildMenu)
1124 {
1125 pMenu = pChildMenu;
1126 break;
1127 }
1128 }
1129 pMenu = pMenu->next;
1130 }
1131 return pMenu;
1132}
1133
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001134 static void
1135_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001136 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001137 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001138 HWND hwndCtl UNUSED,
1139 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001140{
1141 vimmenu_T *pMenu;
1142
1143 pMenu = gui_mswin_find_menu(root_menu, id);
1144 if (pMenu)
1145 gui_menu_cb(pMenu);
1146}
1147#endif
1148
1149#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001150/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001151 * Handle a Find/Replace window message.
1152 */
1153 static void
1154_OnFindRepl(void)
1155{
1156 int flags = 0;
1157 int down;
1158
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001159 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001160 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001161 (void)SetFocus(s_hwnd);
1162
1163 if (s_findrep_struct.Flags & FR_FINDNEXT)
1164 {
1165 flags = FRD_FINDNEXT;
1166
Bram Moolenaar734a8672019-12-02 22:49:38 +01001167 // Give main window the focus back: this is so the cursor isn't
1168 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001169 (void)SetFocus(s_hwnd);
1170 }
1171 else if (s_findrep_struct.Flags & FR_REPLACE)
1172 {
1173 flags = FRD_REPLACE;
1174
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Give main window the focus back: this is so the cursor isn't
1176 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001177 (void)SetFocus(s_hwnd);
1178 }
1179 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1180 {
1181 flags = FRD_REPLACEALL;
1182 }
1183
1184 if (flags != 0)
1185 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001186 char_u *p, *q;
1187
Bram Moolenaar734a8672019-12-02 22:49:38 +01001188 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1190 flags |= FRD_WHOLE_WORD;
1191 if (s_findrep_struct.Flags & FR_MATCHCASE)
1192 flags |= FRD_MATCH_CASE;
1193 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001194 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1195 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1196 if (p != NULL && q != NULL)
1197 gui_do_findrepl(flags, p, q, down);
1198 vim_free(p);
1199 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 }
1201}
1202#endif
1203
1204 static void
1205HandleMouseHide(UINT uMsg, LPARAM lParam)
1206{
1207 static LPARAM last_lParam = 0L;
1208
Bram Moolenaar734a8672019-12-02 22:49:38 +01001209 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001210 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1211 {
1212 if (lParam == last_lParam)
1213 return;
1214 last_lParam = lParam;
1215 }
1216
Bram Moolenaar734a8672019-12-02 22:49:38 +01001217 // Handle specially, to centralise coding. We need to be sure we catch all
1218 // possible events which should cause us to restore the cursor (as it is a
1219 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001220 switch (uMsg)
1221 {
1222 case WM_KEYUP:
1223 case WM_CHAR:
1224 /*
1225 * blank out the pointer if necessary
1226 */
1227 if (p_mh)
1228 gui_mch_mousehide(TRUE);
1229 break;
1230
Bram Moolenaar734a8672019-12-02 22:49:38 +01001231 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001232 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001233 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001234 case WM_LBUTTONDOWN:
1235 case WM_LBUTTONUP:
1236 case WM_MBUTTONDOWN:
1237 case WM_MBUTTONUP:
1238 case WM_RBUTTONDOWN:
1239 case WM_RBUTTONUP:
1240 case WM_XBUTTONDOWN:
1241 case WM_XBUTTONUP:
1242 case WM_NCMOUSEMOVE:
1243 case WM_NCLBUTTONDOWN:
1244 case WM_NCLBUTTONUP:
1245 case WM_NCMBUTTONDOWN:
1246 case WM_NCMBUTTONUP:
1247 case WM_NCRBUTTONDOWN:
1248 case WM_NCRBUTTONUP:
1249 case WM_KILLFOCUS:
1250 /*
1251 * if the pointer is currently hidden, then we should show it.
1252 */
1253 gui_mch_mousehide(FALSE);
1254 break;
1255 }
1256}
1257
1258 static LRESULT CALLBACK
1259_TextAreaWndProc(
1260 HWND hwnd,
1261 UINT uMsg,
1262 WPARAM wParam,
1263 LPARAM lParam)
1264{
1265 /*
1266 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1267 hwnd, uMsg, wParam, lParam);
1268 */
1269
1270 HandleMouseHide(uMsg, lParam);
1271
1272 s_uMsg = uMsg;
1273 s_wParam = wParam;
1274 s_lParam = lParam;
1275
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001276#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001277 TrackUserActivity(uMsg);
1278#endif
1279
1280 switch (uMsg)
1281 {
1282 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1283 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1284 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1285 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1286 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1287 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1288 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1289 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1290 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1291 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1292 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1293 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1294 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1295 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001296 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001297
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001298#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001299 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1300 return TRUE;
1301#endif
1302 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001303 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304 }
1305}
1306
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001307/*
1308 * Called when the foreground or background color has been changed.
1309 */
1310 void
1311gui_mch_new_colors(void)
1312{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001313 HBRUSH prevBrush;
1314
1315 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001316 prevBrush = (HBRUSH)SetClassLongPtr(
1317 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001318 InvalidateRect(s_hwnd, NULL, TRUE);
1319 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001320}
1321
1322/*
1323 * Set the colors to their default values.
1324 */
1325 void
1326gui_mch_def_colors(void)
1327{
1328 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1329 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1330 gui.def_norm_pixel = gui.norm_pixel;
1331 gui.def_back_pixel = gui.back_pixel;
1332}
1333
1334/*
1335 * Open the GUI window which was created by a call to gui_mch_init().
1336 */
1337 int
1338gui_mch_open(void)
1339{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001340 // Actually open the window, if not already visible
1341 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001342 if (!IsWindowVisible(s_hwnd))
1343 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1344
1345#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001346 // Init replace string here, so that we keep it when re-opening the
1347 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001348 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1349#endif
1350
1351 return OK;
1352}
1353
1354/*
1355 * Get the position of the top left corner of the window.
1356 */
1357 int
1358gui_mch_get_winpos(int *x, int *y)
1359{
1360 RECT rect;
1361
1362 GetWindowRect(s_hwnd, &rect);
1363 *x = rect.left;
1364 *y = rect.top;
1365 return OK;
1366}
1367
1368/*
1369 * Set the position of the top left corner of the window to the given
1370 * coordinates.
1371 */
1372 void
1373gui_mch_set_winpos(int x, int y)
1374{
1375 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1376 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1377}
1378 void
1379gui_mch_set_text_area_pos(int x, int y, int w, int h)
1380{
1381 static int oldx = 0;
1382 static int oldy = 0;
1383
1384 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1385
1386#ifdef FEAT_TOOLBAR
1387 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1388 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001389 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001390#endif
1391#if defined(FEAT_GUI_TABLINE)
1392 if (showing_tabline)
1393 {
1394 int top = 0;
1395 RECT rect;
1396
1397# ifdef FEAT_TOOLBAR
1398 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001399 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001400# endif
1401 GetClientRect(s_hwnd, &rect);
1402 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1403 }
1404#endif
1405
Bram Moolenaar734a8672019-12-02 22:49:38 +01001406 // When side scroll bar is unshown, the size of window will change.
1407 // then, the text area move left or right. thus client rect should be
1408 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001409 if (oldx != x || oldy != y)
1410 {
1411 InvalidateRect(s_hwnd, NULL, FALSE);
1412 oldx = x;
1413 oldy = y;
1414 }
1415}
1416
1417
1418/*
1419 * Scrollbar stuff:
1420 */
1421
1422 void
1423gui_mch_enable_scrollbar(
1424 scrollbar_T *sb,
1425 int flag)
1426{
1427 ShowScrollBar(sb->id, SB_CTL, flag);
1428
Bram Moolenaar734a8672019-12-02 22:49:38 +01001429 // TODO: When the window is maximized, the size of the window stays the
1430 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1431 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001432}
1433
1434 void
1435gui_mch_set_scrollbar_pos(
1436 scrollbar_T *sb,
1437 int x,
1438 int y,
1439 int w,
1440 int h)
1441{
1442 SetWindowPos(sb->id, NULL, x, y, w, h,
1443 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1444}
1445
Bram Moolenaar203ec772020-07-17 20:43:43 +02001446 int
1447gui_mch_get_scrollbar_xpadding(void)
1448{
1449 RECT rcTxt, rcWnd;
1450 int xpad;
1451
1452 GetWindowRect(s_textArea, &rcTxt);
1453 GetWindowRect(s_hwnd, &rcWnd);
1454 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001455 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1456 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001457 return (xpad < 0) ? 0 : xpad;
1458}
1459
1460 int
1461gui_mch_get_scrollbar_ypadding(void)
1462{
1463 RECT rcTxt, rcWnd;
1464 int ypad;
1465
1466 GetWindowRect(s_textArea, &rcTxt);
1467 GetWindowRect(s_hwnd, &rcWnd);
1468 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001469 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1470 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001471 return (ypad < 0) ? 0 : ypad;
1472}
1473
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001474 void
1475gui_mch_create_scrollbar(
1476 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001477 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001478{
1479 sb->id = CreateWindow(
1480 "SCROLLBAR", "Scrollbar",
1481 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001482 10, // Any value will do for now
1483 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001484 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001485 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001486}
1487
1488/*
1489 * Find the scrollbar with the given hwnd.
1490 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001491 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001492gui_mswin_find_scrollbar(HWND hwnd)
1493{
1494 win_T *wp;
1495
1496 if (gui.bottom_sbar.id == hwnd)
1497 return &gui.bottom_sbar;
1498 FOR_ALL_WINDOWS(wp)
1499 {
1500 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1501 return &wp->w_scrollbars[SBAR_LEFT];
1502 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1503 return &wp->w_scrollbars[SBAR_RIGHT];
1504 }
1505 return NULL;
1506}
1507
K.Takatac81e9bf2022-01-16 14:15:49 +00001508 static void
1509update_scrollbar_size(void)
1510{
1511 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1512 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1513}
1514
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515/*
1516 * Get the character size of a font.
1517 */
1518 static void
1519GetFontSize(GuiFont font)
1520{
1521 HWND hwnd = GetDesktopWindow();
1522 HDC hdc = GetWindowDC(hwnd);
1523 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001524 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 TEXTMETRIC tm;
1526
1527 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001528 // GetTextMetrics() may not return the right value in tmAveCharWidth
1529 // for some fonts. Do our own average computation.
1530 GetTextExtentPoint(hdc,
1531 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1532 52, &size);
1533 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001534
1535 gui.char_height = tm.tmHeight + p_linespace;
1536
1537 SelectFont(hdc, hfntOld);
1538
1539 ReleaseDC(hwnd, hdc);
1540}
1541
1542/*
1543 * Adjust gui.char_height (after 'linespace' was changed).
1544 */
1545 int
1546gui_mch_adjust_charheight(void)
1547{
1548 GetFontSize(gui.norm_font);
1549 return OK;
1550}
1551
1552 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001553get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554{
1555 HFONT font = NULL;
1556
Bram Moolenaar734a8672019-12-02 22:49:38 +01001557 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001558 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559
1560 if (font == NULL)
1561 return NOFONT;
1562
1563 return (GuiFont)font;
1564}
1565
1566 static int
1567pixels_to_points(int pixels, int vertical)
1568{
1569 int points;
1570 HWND hwnd;
1571 HDC hdc;
1572
1573 hwnd = GetDesktopWindow();
1574 hdc = GetWindowDC(hwnd);
1575
1576 points = MulDiv(pixels, 72,
1577 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1578
1579 ReleaseDC(hwnd, hdc);
1580
1581 return points;
1582}
1583
1584 GuiFont
1585gui_mch_get_font(
1586 char_u *name,
1587 int giveErrorIfMissing)
1588{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001589 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 GuiFont font = NOFONT;
1591
1592 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001593 {
1594 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001596 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001598 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599 return font;
1600}
1601
1602#if defined(FEAT_EVAL) || defined(PROTO)
1603/*
1604 * Return the name of font "font" in allocated memory.
1605 * Don't know how to get the actual name, thus use the provided name.
1606 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001607 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001608gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001609{
1610 if (name == NULL)
1611 return NULL;
1612 return vim_strsave(name);
1613}
1614#endif
1615
1616 void
1617gui_mch_free_font(GuiFont font)
1618{
1619 if (font)
1620 DeleteObject((HFONT)font);
1621}
1622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001623/*
1624 * Return the Pixel value (color) for the given color name.
1625 * Return INVALCOLOR for error.
1626 */
1627 guicolor_T
1628gui_mch_get_color(char_u *name)
1629{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001630 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001631
1632 typedef struct SysColorTable
1633 {
1634 char *name;
1635 int color;
1636 } SysColorTable;
1637
1638 static SysColorTable sys_table[] =
1639 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1641 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001642#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1644#endif
1645 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1646 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1647 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1648 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1649 {"SYS_DESKTOP", COLOR_DESKTOP},
1650 {"SYS_INFOBK", COLOR_INFOBK},
1651 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1652 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 {"SYS_BTNFACE", COLOR_BTNFACE},
1654 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1655 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1656 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1657 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1658 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1659 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1660 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1661 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1662 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1663 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1664 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1665 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1666 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1667 {"SYS_MENU", COLOR_MENU},
1668 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1669 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1670 {"SYS_WINDOW", COLOR_WINDOW},
1671 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1672 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1673 };
1674
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001675 /*
1676 * Try to look up a system colour.
1677 */
K.Takataeeec2542021-06-02 13:28:16 +02001678 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001679 if (STRICMP(name, sys_table[i].name) == 0)
1680 return GetSysColor(sys_table[i].color);
1681
Bram Moolenaarab302212016-04-26 20:59:29 +02001682 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001683}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001684
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001685 guicolor_T
1686gui_mch_get_rgb_color(int r, int g, int b)
1687{
1688 return gui_get_rgb_color_cmn(r, g, b);
1689}
1690
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691/*
1692 * Return OK if the key with the termcap name "name" is supported.
1693 */
1694 int
1695gui_mch_haskey(char_u *name)
1696{
1697 int i;
1698
1699 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1700 if (name[0] == special_keys[i].vim_code0 &&
1701 name[1] == special_keys[i].vim_code1)
1702 return OK;
1703 return FAIL;
1704}
1705
1706 void
1707gui_mch_beep(void)
1708{
1709 MessageBeep(MB_OK);
1710}
1711/*
1712 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1713 */
1714 void
1715gui_mch_invert_rectangle(
1716 int r,
1717 int c,
1718 int nr,
1719 int nc)
1720{
1721 RECT rc;
1722
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001723#if defined(FEAT_DIRECTX)
1724 if (IS_ENABLE_DIRECTX())
1725 DWriteContext_Flush(s_dwc);
1726#endif
1727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 /*
1729 * Note: InvertRect() excludes right and bottom of rectangle.
1730 */
1731 rc.left = FILL_X(c);
1732 rc.top = FILL_Y(r);
1733 rc.right = rc.left + nc * gui.char_width;
1734 rc.bottom = rc.top + nr * gui.char_height;
1735 InvertRect(s_hdc, &rc);
1736}
1737
1738/*
1739 * Iconify the GUI window.
1740 */
1741 void
1742gui_mch_iconify(void)
1743{
1744 ShowWindow(s_hwnd, SW_MINIMIZE);
1745}
1746
1747/*
1748 * Draw a cursor without focus.
1749 */
1750 void
1751gui_mch_draw_hollow_cursor(guicolor_T color)
1752{
1753 HBRUSH hbr;
1754 RECT rc;
1755
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001756#if defined(FEAT_DIRECTX)
1757 if (IS_ENABLE_DIRECTX())
1758 DWriteContext_Flush(s_dwc);
1759#endif
1760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761 /*
1762 * Note: FrameRect() excludes right and bottom of rectangle.
1763 */
1764 rc.left = FILL_X(gui.col);
1765 rc.top = FILL_Y(gui.row);
1766 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001767 if (mb_lefthalve(gui.row, gui.col))
1768 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001769 rc.bottom = rc.top + gui.char_height;
1770 hbr = CreateSolidBrush(color);
1771 FrameRect(s_hdc, &rc, hbr);
1772 DeleteBrush(hbr);
1773}
1774/*
1775 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1776 * color "color".
1777 */
1778 void
1779gui_mch_draw_part_cursor(
1780 int w,
1781 int h,
1782 guicolor_T color)
1783{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001784 RECT rc;
1785
1786 /*
1787 * Note: FillRect() excludes right and bottom of rectangle.
1788 */
1789 rc.left =
1790#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001791 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1793#endif
1794 FILL_X(gui.col);
1795 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1796 rc.right = rc.left + w;
1797 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001798
Bram Moolenaar92467d32017-12-05 13:22:16 +01001799 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800}
1801
1802
1803/*
1804 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1805 * dead key's nominal character and re-post the original message.
1806 */
1807 static void
1808outputDeadKey_rePost(MSG originalMsg)
1809{
1810 static MSG deadCharExpel;
1811
1812 if (!dead_key)
1813 return;
1814
1815 dead_key = 0;
1816
Bram Moolenaar734a8672019-12-02 22:49:38 +01001817 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 deadCharExpel.message = originalMsg.message;
1819 deadCharExpel.hwnd = originalMsg.hwnd;
1820 deadCharExpel.wParam = VK_SPACE;
1821
K.Takata4ac893f2022-01-20 12:44:28 +00001822 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823
Bram Moolenaar734a8672019-12-02 22:49:38 +01001824 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1826 originalMsg.lParam);
1827}
1828
1829
1830/*
1831 * Process a single Windows message.
1832 * If one is not available we hang until one is.
1833 */
1834 static void
1835process_message(void)
1836{
1837 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001838 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001839 char_u string[40];
1840 int i;
1841 int modifiers = 0;
1842 int key;
1843#ifdef FEAT_MENU
1844 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1845#endif
1846
K.Takatab7057bd2022-01-21 11:37:07 +00001847 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001848
1849#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001850 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851 if (msg.message == WM_OLE)
1852 {
1853 char_u *str = (char_u *)msg.lParam;
1854 if (str == NULL || *str == NUL)
1855 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001856 // Message can't be ours, forward it. Fixes problem with Ultramon
1857 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001858 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001859 }
1860 else
1861 {
1862 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 }
1865 return;
1866 }
1867#endif
1868
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001870 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001871 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872 {
1873 HandleMouseHide(msg.message, msg.lParam);
1874 return;
1875 }
1876#endif
1877
1878 /*
1879 * Check if it's a special key that we recognise. If not, call
1880 * TranslateMessage().
1881 */
1882 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1883 {
1884 vk = (int) msg.wParam;
1885
1886 /*
1887 * Handle dead keys in special conditions in other cases we let Windows
1888 * handle them and do not interfere.
1889 *
1890 * The dead_key flag must be reset on several occasions:
1891 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1892 * consumed at that point (This is when we let Windows combine the
1893 * dead character on its own)
1894 *
1895 * - Before doing something special such as regenerating keypresses to
1896 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001897 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898 * immediately to _OnChar() (or _OnSysChar()).
1899 */
1900 if (dead_key)
1901 {
1902 /*
1903 * If a dead key was pressed and the user presses VK_SPACE,
1904 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1905 * with the dead char now, so do nothing special and let Windows
1906 * handle it.
1907 *
1908 * Note that VK_SPACE combines with the dead_key's character and
1909 * only one WM_CHAR will be generated by TranslateMessage(), in
1910 * the two other cases two WM_CHAR will be generated: the dead
1911 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1912 * user expects.
1913 */
1914 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1915 {
1916 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001917 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001918 return;
1919 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001920 // In modes where we are not typing, dead keys should behave
1921 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001922 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1923 {
1924 outputDeadKey_rePost(msg);
1925 return;
1926 }
1927 }
1928
Bram Moolenaar734a8672019-12-02 22:49:38 +01001929 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 if (vk == VK_CANCEL)
1931 {
1932 trash_input_buf();
1933 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001934 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 string[0] = Ctrl_C;
1936 add_to_input_buf(string, 1);
1937 }
1938
1939 for (i = 0; special_keys[i].key_sym != 0; i++)
1940 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001941 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001942 if (special_keys[i].key_sym == vk
1943 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1944 {
1945 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001946 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 * is a key that would normally trigger the dead key nominal
1948 * character output (such as a NUMPAD printable character or
1949 * the TAB key, etc...).
1950 */
1951 if (dead_key && (special_keys[i].vim_code0 == 'K'
1952 || vk == VK_TAB || vk == CAR))
1953 {
1954 outputDeadKey_rePost(msg);
1955 return;
1956 }
1957
1958#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001959 // Check for <F10>: Windows selects the menu. When <F10> is
1960 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001961 if (vk == VK_F10
1962 && gui.menu_is_active
1963 && check_map(k10, State, FALSE, TRUE, FALSE,
1964 NULL, NULL) == NULL)
1965 break;
1966#endif
1967 if (GetKeyState(VK_SHIFT) & 0x8000)
1968 modifiers |= MOD_MASK_SHIFT;
1969 /*
1970 * Don't use caps-lock as shift, because these are special keys
1971 * being considered here, and we only want letters to get
1972 * shifted -- webb
1973 */
1974 /*
1975 if (GetKeyState(VK_CAPITAL) & 0x0001)
1976 modifiers ^= MOD_MASK_SHIFT;
1977 */
1978 if (GetKeyState(VK_CONTROL) & 0x8000)
1979 modifiers |= MOD_MASK_CTRL;
1980 if (GetKeyState(VK_MENU) & 0x8000)
1981 modifiers |= MOD_MASK_ALT;
1982
1983 if (special_keys[i].vim_code1 == NUL)
1984 key = special_keys[i].vim_code0;
1985 else
1986 key = TO_SPECIAL(special_keys[i].vim_code0,
1987 special_keys[i].vim_code1);
1988 key = simplify_key(key, &modifiers);
1989 if (key == CSI)
1990 key = K_CSI;
1991
1992 if (modifiers)
1993 {
1994 string[0] = CSI;
1995 string[1] = KS_MODIFIER;
1996 string[2] = modifiers;
1997 add_to_input_buf(string, 3);
1998 }
1999
2000 if (IS_SPECIAL(key))
2001 {
2002 string[0] = CSI;
2003 string[1] = K_SECOND(key);
2004 string[2] = K_THIRD(key);
2005 add_to_input_buf(string, 3);
2006 }
2007 else
2008 {
2009 int len;
2010
Bram Moolenaar734a8672019-12-02 22:49:38 +01002011 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002012 len = char_to_string(key, string, 40, FALSE);
2013 add_to_input_buf(string, len);
2014 }
2015 break;
2016 }
2017 }
2018 if (special_keys[i].key_sym == 0)
2019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002020 // Some keys need C-S- where they should only need C-.
2021 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2022 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002023 if (vk != 0xff
2024 && (GetKeyState(VK_CONTROL) & 0x8000)
2025 && !(GetKeyState(VK_SHIFT) & 0x8000)
2026 && !(GetKeyState(VK_MENU) & 0x8000))
2027 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002028 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002029 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2030 {
2031 string[0] = Ctrl_HAT;
2032 add_to_input_buf(string, 1);
2033 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002034 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2035 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002036 {
2037 string[0] = Ctrl__;
2038 add_to_input_buf(string, 1);
2039 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002040 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2042 {
2043 string[0] = Ctrl_AT;
2044 add_to_input_buf(string, 1);
2045 }
2046 else
K.Takata4ac893f2022-01-20 12:44:28 +00002047 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048 }
2049 else
K.Takata4ac893f2022-01-20 12:44:28 +00002050 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 }
2052 }
2053#ifdef FEAT_MBYTE_IME
2054 else if (msg.message == WM_IME_NOTIFY)
2055 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2056 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002057 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002058 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059#endif
2060
2061#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002062 // Check for <F10>: Default effect is to select the menu. When <F10> is
2063 // mapped we need to stop it here to avoid strange effects (e.g., for the
2064 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002065 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2066 NULL, NULL) == NULL)
2067#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002068 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069}
2070
2071/*
2072 * Catch up with any queued events. This may put keyboard input into the
2073 * input buffer, call resize call-backs, trigger timers etc. If there is
2074 * nothing in the event queue (& no timers pending), then we return
2075 * immediately.
2076 */
2077 void
2078gui_mch_update(void)
2079{
2080 MSG msg;
2081
2082 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002083 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002084 && !vim_is_input_buf_full())
2085 process_message();
2086}
2087
Bram Moolenaar4231da42016-06-02 14:30:04 +02002088 static void
2089remove_any_timer(void)
2090{
2091 MSG msg;
2092
2093 if (s_wait_timer != 0 && !s_timed_out)
2094 {
2095 KillTimer(NULL, s_wait_timer);
2096
Bram Moolenaar734a8672019-12-02 22:49:38 +01002097 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002098 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002099 ;
2100 s_wait_timer = 0;
2101 }
2102}
2103
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104/*
2105 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2106 * from the keyboard.
2107 * wtime == -1 Wait forever.
2108 * wtime == 0 This should never happen.
2109 * wtime > 0 Wait wtime milliseconds for a character.
2110 * Returns OK if a character was found to be available within the given time,
2111 * or FAIL otherwise.
2112 */
2113 int
2114gui_mch_wait_for_chars(int wtime)
2115{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116 int focus;
2117
2118 s_timed_out = FALSE;
2119
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002120 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002122 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123 if (s_busy_processing)
2124 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002125
2126 // When called with "wtime" zero, just want one msec.
2127 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002128 (TIMERPROC)_OnTimer);
2129 }
2130
2131 allow_scrollbar = TRUE;
2132
2133 focus = gui.in_focus;
2134 while (!s_timed_out)
2135 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002137 if (gui.in_focus != focus)
2138 {
2139 if (gui.in_focus)
2140 gui_mch_start_blink();
2141 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002142 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002143 focus = gui.in_focus;
2144 }
2145
2146 if (s_need_activate)
2147 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 s_need_activate = FALSE;
2150 }
2151
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152#ifdef FEAT_TIMERS
2153 did_add_timer = FALSE;
2154#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002156 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002157 for (;;)
2158 {
2159 MSG msg;
2160
2161 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002162# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002163 if (did_add_timer)
2164 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002165# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002166 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002167 {
2168 process_message();
2169 break;
2170 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002171 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002172 // TODO: The 10 msec is a compromise between laggy response
2173 // and consuming more CPU time. Better would be to handle
2174 // channel messages when they arrive.
2175 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002176 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002177 break;
2178 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002179#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002180 // Don't use gui_mch_update() because then we will spin-lock until a
2181 // char arrives, instead we use GetMessage() to hang until an
2182 // event arrives. No need to check for input_buf_full because we are
2183 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002184 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002185#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186
2187 if (input_available())
2188 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002189 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002190 allow_scrollbar = FALSE;
2191
Bram Moolenaar89c00032019-09-04 13:53:21 +02002192 // Clear pending mouse button, the release event may have been
2193 // taken by the dialog window. But don't do this when getting
2194 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195 if (!s_getting_focus)
2196 s_button_pending = -1;
2197
2198 return OK;
2199 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002200
2201#ifdef FEAT_TIMERS
2202 if (did_add_timer)
2203 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002204 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002205 remove_any_timer();
2206 break;
2207 }
2208#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002209 }
2210 allow_scrollbar = FALSE;
2211 return FAIL;
2212}
2213
2214/*
2215 * Clear a rectangular region of the screen from text pos (row1, col1) to
2216 * (row2, col2) inclusive.
2217 */
2218 void
2219gui_mch_clear_block(
2220 int row1,
2221 int col1,
2222 int row2,
2223 int col2)
2224{
2225 RECT rc;
2226
2227 /*
2228 * Clear one extra pixel at the far right, for when bold characters have
2229 * spilled over to the window border.
2230 * Note: FillRect() excludes right and bottom of rectangle.
2231 */
2232 rc.left = FILL_X(col1);
2233 rc.top = FILL_Y(row1);
2234 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2235 rc.bottom = FILL_Y(row2 + 1);
2236 clear_rect(&rc);
2237}
2238
2239/*
2240 * Clear the whole text window.
2241 */
2242 void
2243gui_mch_clear_all(void)
2244{
2245 RECT rc;
2246
2247 rc.left = 0;
2248 rc.top = 0;
2249 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2250 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2251 clear_rect(&rc);
2252}
2253/*
2254 * Menu stuff.
2255 */
2256
2257 void
2258gui_mch_enable_menu(int flag)
2259{
2260#ifdef FEAT_MENU
2261 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2262#endif
2263}
2264
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002265 void
2266gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002267 int x UNUSED,
2268 int y UNUSED,
2269 int w UNUSED,
2270 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002272 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273}
2274
2275#if defined(FEAT_MENU) || defined(PROTO)
2276/*
2277 * Make menu item hidden or not hidden
2278 */
2279 void
2280gui_mch_menu_hidden(
2281 vimmenu_T *menu,
2282 int hidden)
2283{
2284 /*
2285 * This doesn't do what we want. Hmm, just grey the menu items for now.
2286 */
2287 /*
2288 if (hidden)
2289 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2290 else
2291 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2292 */
2293 gui_mch_menu_grey(menu, hidden);
2294}
2295
2296/*
2297 * This is called after setting all the menus to grey/hidden or not.
2298 */
2299 void
2300gui_mch_draw_menubar(void)
2301{
2302 DrawMenuBar(s_hwnd);
2303}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002304#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002306/*
2307 * Return the RGB value of a pixel as a long.
2308 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002309 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310gui_mch_get_rgb(guicolor_T pixel)
2311{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002312 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2313 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314}
2315
2316#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002317/*
2318 * Convert pixels in X to dialog units
2319 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320 static WORD
2321PixelToDialogX(int numPixels)
2322{
2323 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2324}
2325
Bram Moolenaar734a8672019-12-02 22:49:38 +01002326/*
2327 * Convert pixels in Y to dialog units
2328 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002329 static WORD
2330PixelToDialogY(int numPixels)
2331{
2332 return (WORD)((numPixels * 8) / s_dlgfntheight);
2333}
2334
Bram Moolenaar734a8672019-12-02 22:49:38 +01002335/*
2336 * Return the width in pixels of the given text in the given DC.
2337 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 static int
2339GetTextWidth(HDC hdc, char_u *str, int len)
2340{
2341 SIZE size;
2342
2343 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2344 return size.cx;
2345}
2346
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002347/*
2348 * Return the width in pixels of the given text in the given DC, taking care
2349 * of 'encoding' to active codepage conversion.
2350 */
2351 static int
2352GetTextWidthEnc(HDC hdc, char_u *str, int len)
2353{
2354 SIZE size;
2355 WCHAR *wstr;
2356 int n;
2357 int wlen = len;
2358
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002359 wstr = enc_to_utf16(str, &wlen);
2360 if (wstr == NULL)
2361 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002363 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2364 vim_free(wstr);
2365 if (n)
2366 return size.cx;
2367 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002368}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370static void get_work_area(RECT *spi_rect);
2371
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002372/*
2373 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2375 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 */
2377 static BOOL
2378CenterWindow(
2379 HWND hwndChild,
2380 HWND hwndParent)
2381{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002382 HMONITOR mon;
2383 MONITORINFO moninfo;
2384 RECT rChild, rParent, rScreen;
2385 int wChild, hChild, wParent, hParent;
2386 int xNew, yNew;
2387 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002388
2389 GetWindowRect(hwndChild, &rChild);
2390 wChild = rChild.right - rChild.left;
2391 hChild = rChild.bottom - rChild.top;
2392
Bram Moolenaar734a8672019-12-02 22:49:38 +01002393 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002395 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396 else
2397 GetWindowRect(hwndParent, &rParent);
2398 wParent = rParent.right - rParent.left;
2399 hParent = rParent.bottom - rParent.top;
2400
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002401 moninfo.cbSize = sizeof(MONITORINFO);
2402 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2403 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002405 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002407 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002409 hdc = GetDC(hwndChild);
2410 rScreen.left = 0;
2411 rScreen.top = 0;
2412 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2413 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2414 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 }
2416
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002417 xNew = rParent.left + ((wParent - wChild) / 2);
2418 if (xNew < rScreen.left)
2419 xNew = rScreen.left;
2420 else if ((xNew + wChild) > rScreen.right)
2421 xNew = rScreen.right - wChild;
2422
2423 yNew = rParent.top + ((hParent - hChild) / 2);
2424 if (yNew < rScreen.top)
2425 yNew = rScreen.top;
2426 else if ((yNew + hChild) > rScreen.bottom)
2427 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002428
2429 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2430 SWP_NOSIZE | SWP_NOZORDER);
2431}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002432#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434#if defined(FEAT_TOOLBAR) || defined(PROTO)
2435 void
2436gui_mch_show_toolbar(int showit)
2437{
2438 if (s_toolbarhwnd == NULL)
2439 return;
2440
2441 if (showit)
2442 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002443# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002444 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002445# define TB_SETUNICODEFORMAT 0x2005
2446# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002447 // Enable unicode support
2448 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2449 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 ShowWindow(s_toolbarhwnd, SW_SHOW);
2451 }
2452 else
2453 ShowWindow(s_toolbarhwnd, SW_HIDE);
2454}
2455
Bram Moolenaar734a8672019-12-02 22:49:38 +01002456// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002457# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458
2459#endif
2460
2461#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2462 static void
2463add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2464{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002465 WCHAR *wn;
2466 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002467
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002468 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002470 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002472 infow.cbSize = sizeof(infow);
2473 infow.fMask = MIIM_TYPE | MIIM_ID;
2474 infow.wID = item_id;
2475 infow.fType = MFT_STRING;
2476 infow.dwTypeData = wn;
2477 infow.cch = (UINT)wcslen(wn);
2478 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2479 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480}
2481
2482 static void
2483show_tabline_popup_menu(void)
2484{
2485 HMENU tab_pmenu;
2486 long rval;
2487 POINT pt;
2488
Bram Moolenaar734a8672019-12-02 22:49:38 +01002489 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002490 if (hold_gui_events
2491# ifdef FEAT_CMDWIN
2492 || cmdwin_type != 0
2493# endif
2494 )
2495 return;
2496
2497 tab_pmenu = CreatePopupMenu();
2498 if (tab_pmenu == NULL)
2499 return;
2500
2501 if (first_tabpage->tp_next != NULL)
2502 add_tabline_popup_menu_entry(tab_pmenu,
2503 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2504 add_tabline_popup_menu_entry(tab_pmenu,
2505 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2506 add_tabline_popup_menu_entry(tab_pmenu,
2507 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2508
2509 GetCursorPos(&pt);
2510 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2511 NULL);
2512
2513 DestroyMenu(tab_pmenu);
2514
Bram Moolenaar734a8672019-12-02 22:49:38 +01002515 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516 if (rval > 0)
2517 {
2518 TCHITTESTINFO htinfo;
2519 int idx;
2520
2521 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2522 return;
2523
2524 htinfo.pt.x = pt.x;
2525 htinfo.pt.y = pt.y;
2526 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2527 if (idx == -1)
2528 idx = 0;
2529 else
2530 idx += 1;
2531
2532 send_tabline_menu_event(idx, (int)rval);
2533 }
2534}
2535
2536/*
2537 * Show or hide the tabline.
2538 */
2539 void
2540gui_mch_show_tabline(int showit)
2541{
2542 if (s_tabhwnd == NULL)
2543 return;
2544
2545 if (!showit != !showing_tabline)
2546 {
2547 if (showit)
2548 ShowWindow(s_tabhwnd, SW_SHOW);
2549 else
2550 ShowWindow(s_tabhwnd, SW_HIDE);
2551 showing_tabline = showit;
2552 }
2553}
2554
2555/*
2556 * Return TRUE when tabline is displayed.
2557 */
2558 int
2559gui_mch_showing_tabline(void)
2560{
2561 return s_tabhwnd != NULL && showing_tabline;
2562}
2563
2564/*
2565 * Update the labels of the tabline.
2566 */
2567 void
2568gui_mch_update_tabline(void)
2569{
2570 tabpage_T *tp;
2571 TCITEM tie;
2572 int nr = 0;
2573 int curtabidx = 0;
2574 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002575 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576
2577 if (s_tabhwnd == NULL)
2578 return;
2579
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002580# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002581 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002582# define CCM_SETUNICODEFORMAT 0x2005
2583# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002584 // Enable unicode support
2585 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586
2587 tie.mask = TCIF_TEXT;
2588 tie.iImage = -1;
2589
Bram Moolenaar734a8672019-12-02 22:49:38 +01002590 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2592
Bram Moolenaar734a8672019-12-02 22:49:38 +01002593 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2595 {
2596 if (tp == curtab)
2597 curtabidx = nr;
2598
2599 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2600 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002601 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 tie.pszText = "-Empty-";
2603 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2604 tabadded = 1;
2605 }
2606
2607 get_tabline_label(tp, FALSE);
2608 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002610 wstr = enc_to_utf16(NameBuff, NULL);
2611 if (wstr != NULL)
2612 {
2613 TCITEMW tiw;
2614
2615 tiw.mask = TCIF_TEXT;
2616 tiw.iImage = -1;
2617 tiw.pszText = wstr;
2618 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2619 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002621 }
2622
Bram Moolenaar734a8672019-12-02 22:49:38 +01002623 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2625 TabCtrl_DeleteItem(s_tabhwnd, nr);
2626
2627 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2628 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2629
Bram Moolenaar734a8672019-12-02 22:49:38 +01002630 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002631 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2632 RedrawWindow(s_tabhwnd, NULL, NULL,
2633 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2634
2635 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2636 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2637}
2638
2639/*
2640 * Set the current tab to "nr". First tab is 1.
2641 */
2642 void
2643gui_mch_set_curtab(int nr)
2644{
2645 if (s_tabhwnd == NULL)
2646 return;
2647
2648 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2649 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2650}
2651
2652#endif
2653
2654/*
2655 * ":simalt" command.
2656 */
2657 void
2658ex_simalt(exarg_T *eap)
2659{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002660 char_u *keys = eap->arg;
2661 int fill_typebuf = FALSE;
2662 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663
2664 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2665 while (*keys)
2666 {
2667 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2670 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002671 fill_typebuf = TRUE;
2672 }
2673 if (fill_typebuf)
2674 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002675 // Put a NOP in the typeahead buffer so that the message will get
2676 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002677 key_name[0] = K_SPECIAL;
2678 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002679 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002680 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002681#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002682 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002683#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002684 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002685 }
2686}
2687
2688/*
2689 * Create the find & replace dialogs.
2690 * You can't have both at once: ":find" when replace is showing, destroys
2691 * the replace dialog first, and the other way around.
2692 */
2693#ifdef MSWIN_FIND_REPLACE
2694 static void
2695initialise_findrep(char_u *initial_string)
2696{
2697 int wword = FALSE;
2698 int mcase = !p_ic;
2699 char_u *entry_text;
2700
Bram Moolenaar734a8672019-12-02 22:49:38 +01002701 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2703
2704 s_findrep_struct.hwndOwner = s_hwnd;
2705 s_findrep_struct.Flags = FR_DOWN;
2706 if (mcase)
2707 s_findrep_struct.Flags |= FR_MATCHCASE;
2708 if (wword)
2709 s_findrep_struct.Flags |= FR_WHOLEWORD;
2710 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002711 {
2712 WCHAR *p = enc_to_utf16(entry_text, NULL);
2713 if (p != NULL)
2714 {
2715 int len = s_findrep_struct.wFindWhatLen - 1;
2716
2717 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2718 s_findrep_struct.lpstrFindWhat[len] = NUL;
2719 vim_free(p);
2720 }
2721 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722 vim_free(entry_text);
2723}
2724#endif
2725
2726 static void
2727set_window_title(HWND hwnd, char *title)
2728{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002729 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 {
2731 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732
Bram Moolenaar734a8672019-12-02 22:49:38 +01002733 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2735 if (wbuf != NULL)
2736 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002737 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739 }
2740 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002741 else
2742 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743}
2744
2745 void
2746gui_mch_find_dialog(exarg_T *eap)
2747{
2748#ifdef MSWIN_FIND_REPLACE
2749 if (s_findrep_msg != 0)
2750 {
2751 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2752 DestroyWindow(s_findrep_hwnd);
2753
2754 if (!IsWindow(s_findrep_hwnd))
2755 {
2756 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002757 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758 }
2759
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002760 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 (void)SetFocus(s_findrep_hwnd);
2762
2763 s_findrep_is_find = TRUE;
2764 }
2765#endif
2766}
2767
2768
2769 void
2770gui_mch_replace_dialog(exarg_T *eap)
2771{
2772#ifdef MSWIN_FIND_REPLACE
2773 if (s_findrep_msg != 0)
2774 {
2775 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2776 DestroyWindow(s_findrep_hwnd);
2777
2778 if (!IsWindow(s_findrep_hwnd))
2779 {
2780 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002781 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782 }
2783
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002784 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002785 (void)SetFocus(s_findrep_hwnd);
2786
2787 s_findrep_is_find = FALSE;
2788 }
2789#endif
2790}
2791
2792
2793/*
2794 * Set visibility of the pointer.
2795 */
2796 void
2797gui_mch_mousehide(int hide)
2798{
2799 if (hide != gui.pointer_hidden)
2800 {
2801 ShowCursor(!hide);
2802 gui.pointer_hidden = hide;
2803 }
2804}
2805
2806#ifdef FEAT_MENU
2807 static void
2808gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2809{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002810 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811 gui_mch_mousehide(FALSE);
2812
2813 (void)TrackPopupMenu(
2814 (HMENU)menu->submenu_id,
2815 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2816 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002817 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002818 s_hwnd,
2819 NULL);
2820 /*
2821 * NOTE: The pop-up menu can eat the mouse up event.
2822 * We deal with this in normal.c.
2823 */
2824}
2825#endif
2826
2827/*
2828 * Got a message when the system will go down.
2829 */
2830 static void
2831_OnEndSession(void)
2832{
2833 getout_preserve_modified(1);
2834}
2835
2836/*
2837 * Get this message when the user clicks on the cross in the top right corner
2838 * of a Windows95 window.
2839 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002841_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842{
2843 gui_shell_closed();
2844}
2845
2846/*
2847 * Get a message when the window is being destroyed.
2848 */
2849 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002850_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851{
2852 if (!destroying)
2853 _OnClose(hwnd);
2854}
2855
2856 static void
2857_OnPaint(
2858 HWND hwnd)
2859{
2860 if (!IsMinimized(hwnd))
2861 {
2862 PAINTSTRUCT ps;
2863
Bram Moolenaar734a8672019-12-02 22:49:38 +01002864 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002866
Bram Moolenaar734a8672019-12-02 22:49:38 +01002867 // prevent multi-byte characters from misprinting on an invalid
2868 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002869 if (has_mbyte)
2870 {
2871 RECT rect;
2872
2873 GetClientRect(hwnd, &rect);
2874 ps.rcPaint.left = rect.left;
2875 ps.rcPaint.right = rect.right;
2876 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877
2878 if (!IsRectEmpty(&ps.rcPaint))
2879 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2881 ps.rcPaint.right - ps.rcPaint.left + 1,
2882 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2883 }
2884
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885 EndPaint(hwnd, &ps);
2886 }
2887}
2888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889 static void
2890_OnSize(
2891 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002892 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002893 int cx,
2894 int cy)
2895{
K.Takatac81e9bf2022-01-16 14:15:49 +00002896 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897 {
2898 gui_resize_shell(cx, cy);
2899
Bram Moolenaar734a8672019-12-02 22:49:38 +01002900 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902 }
2903}
2904
2905 static void
2906_OnSetFocus(
2907 HWND hwnd,
2908 HWND hwndOldFocus)
2909{
2910 gui_focus_change(TRUE);
2911 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002912 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913}
2914
2915 static void
2916_OnKillFocus(
2917 HWND hwnd,
2918 HWND hwndNewFocus)
2919{
2920 gui_focus_change(FALSE);
2921 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002922 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923}
2924
2925/*
2926 * Get a message when the user switches back to vim
2927 */
2928 static LRESULT
2929_OnActivateApp(
2930 HWND hwnd,
2931 BOOL fActivate,
2932 DWORD dwThreadId)
2933{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002934 // we call gui_focus_change() in _OnSetFocus()
2935 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002936 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002937}
2938
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 void
2940gui_mch_destroy_scrollbar(scrollbar_T *sb)
2941{
2942 DestroyWindow(sb->id);
2943}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002944
2945/*
2946 * Get current mouse coordinates in text window.
2947 */
2948 void
2949gui_mch_getmouse(int *x, int *y)
2950{
2951 RECT rct;
2952 POINT mp;
2953
2954 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002955 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002956 *x = (int)(mp.x - rct.left);
2957 *y = (int)(mp.y - rct.top);
2958}
2959
2960/*
2961 * Move mouse pointer to character at (x, y).
2962 */
2963 void
2964gui_mch_setmouse(int x, int y)
2965{
2966 RECT rct;
2967
2968 (void)GetWindowRect(s_textArea, &rct);
2969 (void)SetCursorPos(x + gui.border_offset + rct.left,
2970 y + gui.border_offset + rct.top);
2971}
2972
2973 static void
2974gui_mswin_get_valid_dimensions(
2975 int w,
2976 int h,
2977 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002978 int *valid_h,
2979 int *cols,
2980 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002981{
2982 int base_width, base_height;
2983
2984 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002985 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2986 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002988 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2989 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2990 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2991 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002992 *cols = (w - base_width) / gui.char_width;
2993 *rows = (h - base_height) / gui.char_height;
2994 *valid_w = base_width + *cols * gui.char_width;
2995 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002996}
2997
2998 void
2999gui_mch_flash(int msec)
3000{
3001 RECT rc;
3002
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003003#if defined(FEAT_DIRECTX)
3004 if (IS_ENABLE_DIRECTX())
3005 DWriteContext_Flush(s_dwc);
3006#endif
3007
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003008 /*
3009 * Note: InvertRect() excludes right and bottom of rectangle.
3010 */
3011 rc.left = 0;
3012 rc.top = 0;
3013 rc.right = gui.num_cols * gui.char_width;
3014 rc.bottom = gui.num_rows * gui.char_height;
3015 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003016 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003017
Bram Moolenaar734a8672019-12-02 22:49:38 +01003018 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019
3020 InvertRect(s_hdc, &rc);
3021}
3022
3023/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003024 * Check if the specified point is on-screen. (multi-monitor aware)
3025 */
3026 static BOOL
3027is_point_onscreen(int x, int y)
3028{
3029 POINT pt = {x, y};
3030
3031 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3032}
3033
3034/*
3035 * Check if the whole area of the specified window is on-screen.
3036 *
3037 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3038 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3039 * only works when the whole window is on-screen.
3040 */
3041 static BOOL
3042is_window_onscreen(HWND hwnd)
3043{
3044 RECT rc;
3045
3046 GetWindowRect(hwnd, &rc);
3047
3048 if (!is_point_onscreen(rc.left, rc.top))
3049 return FALSE;
3050 if (!is_point_onscreen(rc.left, rc.bottom))
3051 return FALSE;
3052 if (!is_point_onscreen(rc.right, rc.top))
3053 return FALSE;
3054 if (!is_point_onscreen(rc.right, rc.bottom))
3055 return FALSE;
3056 return TRUE;
3057}
3058
3059/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 * Return flags used for scrolling.
3061 * The SW_INVALIDATE is required when part of the window is covered or
3062 * off-screen. Refer to MS KB Q75236.
3063 */
3064 static int
3065get_scroll_flags(void)
3066{
3067 HWND hwnd;
3068 RECT rcVim, rcOther, rcDest;
3069
Bram Moolenaar185577e2020-10-29 20:08:21 +01003070 // Check if the window is (partly) off-screen.
3071 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003072 return SW_INVALIDATE;
3073
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003074 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003075 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3077 if (IsWindowVisible(hwnd))
3078 {
3079 GetWindowRect(hwnd, &rcOther);
3080 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3081 return SW_INVALIDATE;
3082 }
3083 return 0;
3084}
3085
3086/*
3087 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3088 * may not be scrolled out properly.
3089 * For gVim, when _OnScroll() is repeated, the character at the
3090 * previous cursor position may be left drawn after scroll.
3091 * The problem can be avoided by calling GetPixel() to get a pixel in
3092 * the region before ScrollWindowEx().
3093 */
3094 static void
3095intel_gpu_workaround(void)
3096{
3097 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3098}
3099
3100/*
3101 * Delete the given number of lines from the given row, scrolling up any
3102 * text further down within the scroll region.
3103 */
3104 void
3105gui_mch_delete_lines(
3106 int row,
3107 int num_lines)
3108{
3109 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003110
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003111 rc.left = FILL_X(gui.scroll_region_left);
3112 rc.right = FILL_X(gui.scroll_region_right + 1);
3113 rc.top = FILL_Y(row);
3114 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3115
Bram Moolenaar92467d32017-12-05 13:22:16 +01003116#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003117 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003118 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003119 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003120 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003121 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003122#endif
3123 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003124#if defined(FEAT_DIRECTX)
3125 if (IS_ENABLE_DIRECTX())
3126 DWriteContext_Flush(s_dwc);
3127#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003128 intel_gpu_workaround();
3129 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003131 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003132 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133
Bram Moolenaar734a8672019-12-02 22:49:38 +01003134 // This seems to be required to avoid the cursor disappearing when
3135 // scrolling such that the cursor ends up in the top-left character on
3136 // the screen... But why? (Webb)
3137 // It's probably fixed by disabling drawing the cursor while scrolling.
3138 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003139
3140 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3141 gui.scroll_region_left,
3142 gui.scroll_region_bot, gui.scroll_region_right);
3143}
3144
3145/*
3146 * Insert the given number of lines before the given row, scrolling down any
3147 * following text within the scroll region.
3148 */
3149 void
3150gui_mch_insert_lines(
3151 int row,
3152 int num_lines)
3153{
3154 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003155
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156 rc.left = FILL_X(gui.scroll_region_left);
3157 rc.right = FILL_X(gui.scroll_region_right + 1);
3158 rc.top = FILL_Y(row);
3159 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160
3161#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003164 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003165 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003166 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003167#endif
3168 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003169#if defined(FEAT_DIRECTX)
3170 if (IS_ENABLE_DIRECTX())
3171 DWriteContext_Flush(s_dwc);
3172#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003173 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003174 // The SW_INVALIDATE is required when part of the window is covered or
3175 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003178 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003179 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003181 gui_clear_block(row, gui.scroll_region_left,
3182 row + num_lines - 1, gui.scroll_region_right);
3183}
3184
3185
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003187gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188{
3189#if defined(FEAT_DIRECTX)
3190 DWriteContext_Close(s_dwc);
3191 DWrite_Final();
3192 s_dwc = NULL;
3193#endif
3194
3195 ReleaseDC(s_textArea, s_hdc);
3196 DeleteObject(s_brush);
3197
3198#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003199 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3201#endif
3202
Bram Moolenaar734a8672019-12-02 22:49:38 +01003203 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 if (s_hwnd != NULL)
3205 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003206 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003207 DestroyWindow(s_hwnd);
3208 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209}
3210
3211 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003212logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213{
3214 char *p;
3215 char *res;
3216 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003217 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003218 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003219 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003221 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3222 if (font_name == NULL)
3223 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003225 quality_name = quality_id2name((int)lf.lfQuality);
3226
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003227 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003228 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003229 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003230 if (res != NULL)
3231 {
3232 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003233 // make a normal font string out of the lf thing:
3234 points = pixels_to_points(
3235 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3236 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3237 sprintf((char *)p, "%s:h%d", font_name, points);
3238 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003239 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 while (*p)
3241 {
3242 if (*p == ' ')
3243 *p = '_';
3244 ++p;
3245 }
3246 if (lf.lfItalic)
3247 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003248 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 STRCAT(p, ":b");
3250 if (lf.lfUnderline)
3251 STRCAT(p, ":u");
3252 if (lf.lfStrikeOut)
3253 STRCAT(p, ":s");
3254 if (charset_name != NULL)
3255 {
3256 STRCAT(p, ":c");
3257 STRCAT(p, charset_name);
3258 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003259 if (quality_name != NULL)
3260 {
3261 STRCAT(p, ":q");
3262 STRCAT(p, quality_name);
3263 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003264 }
3265
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003266 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 return (char_u *)res;
3268}
3269
3270
3271#ifdef FEAT_MBYTE_IME
3272/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003273 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003274 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 */
3276 static void
3277update_im_font(void)
3278{
K.Takatac81e9bf2022-01-16 14:15:49 +00003279 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003280
3281 if (p_guifontwide != NULL && *p_guifontwide != NUL
3282 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003283 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284 norm_logfont = lf_wide;
3285 else
3286 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003287
3288 lf = norm_logfont;
3289 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3290 // Work around when PerMonitorV2 is not enabled in the process level.
3291 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3292 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003293}
3294#endif
3295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296/*
3297 * Handler of gui.wide_font (p_guifontwide) changed notification.
3298 */
3299 void
3300gui_mch_wide_font_changed(void)
3301{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003302 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003304#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003305 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003306#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307
3308 gui_mch_free_font(gui.wide_ital_font);
3309 gui.wide_ital_font = NOFONT;
3310 gui_mch_free_font(gui.wide_bold_font);
3311 gui.wide_bold_font = NOFONT;
3312 gui_mch_free_font(gui.wide_boldital_font);
3313 gui.wide_boldital_font = NOFONT;
3314
3315 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003316 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317 {
3318 if (!lf.lfItalic)
3319 {
3320 lf.lfItalic = TRUE;
3321 gui.wide_ital_font = get_font_handle(&lf);
3322 lf.lfItalic = FALSE;
3323 }
3324 if (lf.lfWeight < FW_BOLD)
3325 {
3326 lf.lfWeight = FW_BOLD;
3327 gui.wide_bold_font = get_font_handle(&lf);
3328 if (!lf.lfItalic)
3329 {
3330 lf.lfItalic = TRUE;
3331 gui.wide_boldital_font = get_font_handle(&lf);
3332 }
3333 }
3334 }
3335}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336
3337/*
3338 * Initialise vim to use the font with the given name.
3339 * Return FAIL if the font could not be loaded, OK otherwise.
3340 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003342gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343{
K.Takatac81e9bf2022-01-16 14:15:49 +00003344 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345 GuiFont font = NOFONT;
3346 char_u *p;
3347
Bram Moolenaar734a8672019-12-02 22:49:38 +01003348 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003350 {
3351 lfOrig = lf;
3352 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003354 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355 if (font == NOFONT)
3356 return FAIL;
3357
3358 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003359 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003360#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361 norm_logfont = lf;
3362 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003363 if (!s_in_dpichanged)
3364 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365#endif
3366 gui_mch_free_font(gui.norm_font);
3367 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003368 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 GetFontSize(font);
3370
K.Takatac81e9bf2022-01-16 14:15:49 +00003371 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372 if (p != NULL)
3373 {
3374 hl_set_font_name(p);
3375
Bram Moolenaar734a8672019-12-02 22:49:38 +01003376 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3378 {
3379 vim_free(p_guifont);
3380 p_guifont = p;
3381 }
3382 else
3383 vim_free(p);
3384 }
3385
3386 gui_mch_free_font(gui.ital_font);
3387 gui.ital_font = NOFONT;
3388 gui_mch_free_font(gui.bold_font);
3389 gui.bold_font = NOFONT;
3390 gui_mch_free_font(gui.boldital_font);
3391 gui.boldital_font = NOFONT;
3392
3393 if (!lf.lfItalic)
3394 {
3395 lf.lfItalic = TRUE;
3396 gui.ital_font = get_font_handle(&lf);
3397 lf.lfItalic = FALSE;
3398 }
3399 if (lf.lfWeight < FW_BOLD)
3400 {
3401 lf.lfWeight = FW_BOLD;
3402 gui.bold_font = get_font_handle(&lf);
3403 if (!lf.lfItalic)
3404 {
3405 lf.lfItalic = TRUE;
3406 gui.boldital_font = get_font_handle(&lf);
3407 }
3408 }
3409
3410 return OK;
3411}
3412
3413#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003414# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415#endif
3416
3417/*
3418 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003419 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 */
3421 int
3422gui_mch_maximized(void)
3423{
3424 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003425 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426
3427 wp.length = sizeof(WINDOWPLACEMENT);
3428 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003429 {
3430 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003432 && wp.flags == WPF_RESTORETOMAXIMIZED))
3433 return TRUE;
3434 if (wp.showCmd == SW_SHOWMINIMIZED)
3435 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003437 // Assume the window is snapped when the sizes from two APIs differ.
3438 GetWindowRect(s_hwnd, &rc);
3439 if ((rc.right - rc.left !=
3440 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3441 || (rc.bottom - rc.top !=
3442 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3443 return TRUE;
3444 }
3445 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446}
3447
3448/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003449 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3450 * is set. Compute the new Rows and Columns. This is like resizing the
3451 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452 */
3453 void
3454gui_mch_newfont(void)
3455{
3456 RECT rect;
3457
3458 GetWindowRect(s_hwnd, &rect);
3459 if (win_socket_id == 0)
3460 {
3461 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003462 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3463 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003465 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3466 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3467 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3468 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469 }
3470 else
3471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003472 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003474 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 }
3476}
3477
3478/*
3479 * Set the window title
3480 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481 void
3482gui_mch_settitle(
3483 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003484 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485{
3486 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3487}
3488
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003489#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003490// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3491// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492static LPCSTR mshape_idcs[] =
3493{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003494 IDC_ARROW, // arrow
3495 MAKEINTRESOURCE(0), // blank
3496 IDC_IBEAM, // beam
3497 IDC_SIZENS, // updown
3498 IDC_SIZENS, // udsizing
3499 IDC_SIZEWE, // leftright
3500 IDC_SIZEWE, // lrsizing
3501 IDC_WAIT, // busy
3502 IDC_NO, // no
3503 IDC_ARROW, // crosshair
3504 IDC_ARROW, // hand1
3505 IDC_ARROW, // hand2
3506 IDC_ARROW, // pencil
3507 IDC_ARROW, // question
3508 IDC_ARROW, // right-arrow
3509 IDC_UPARROW, // up-arrow
3510 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511};
3512
3513 void
3514mch_set_mouse_shape(int shape)
3515{
3516 LPCSTR idc;
3517
3518 if (shape == MSHAPE_HIDE)
3519 ShowCursor(FALSE);
3520 else
3521 {
3522 if (shape >= MSHAPE_NUMBERED)
3523 idc = IDC_ARROW;
3524 else
3525 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003526 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 if (!p_mh)
3528 {
3529 POINT mp;
3530
Bram Moolenaar734a8672019-12-02 22:49:38 +01003531 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003532 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003533 (void)SetCursorPos(mp.x, mp.y);
3534 ShowCursor(TRUE);
3535 }
3536 }
3537}
3538#endif
3539
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003540#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542 * Wide version of convert_filter().
3543 */
3544 static WCHAR *
3545convert_filterW(char_u *s)
3546{
3547 char_u *tmp;
3548 int len;
3549 WCHAR *res;
3550
3551 tmp = convert_filter(s);
3552 if (tmp == NULL)
3553 return NULL;
3554 len = (int)STRLEN(s) + 3;
3555 res = enc_to_utf16(tmp, &len);
3556 vim_free(tmp);
3557 return res;
3558}
3559
3560/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003561 * Pop open a file browser and return the file selected, in allocated memory,
3562 * or NULL if Cancel is hit.
3563 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3564 * title - Title message for the file browser dialog.
3565 * dflt - Default name of file.
3566 * ext - Default extension to be added to files without extensions.
3567 * initdir - directory in which to open the browser (NULL = current dir)
3568 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003570 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003571gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572 int saving,
3573 char_u *title,
3574 char_u *dflt,
3575 char_u *ext,
3576 char_u *initdir,
3577 char_u *filter)
3578{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003579 // We always use the wide function. This means enc_to_utf16() must work,
3580 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581 OPENFILENAMEW fileStruct;
3582 WCHAR fileBuf[MAXPATHL];
3583 WCHAR *wp;
3584 int i;
3585 WCHAR *titlep = NULL;
3586 WCHAR *extp = NULL;
3587 WCHAR *initdirp = NULL;
3588 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003589 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003590 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591
3592 if (dflt == NULL)
3593 fileBuf[0] = NUL;
3594 else
3595 {
3596 wp = enc_to_utf16(dflt, NULL);
3597 if (wp == NULL)
3598 fileBuf[0] = NUL;
3599 else
3600 {
3601 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3602 fileBuf[i] = wp[i];
3603 fileBuf[i] = NUL;
3604 vim_free(wp);
3605 }
3606 }
3607
Bram Moolenaar734a8672019-12-02 22:49:38 +01003608 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003609 filterp = convert_filterW(filter);
3610
Bram Moolenaara80faa82020-04-12 19:37:17 +02003611 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003612# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003613 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003614 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003615# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003617# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618
3619 if (title != NULL)
3620 titlep = enc_to_utf16(title, NULL);
3621 fileStruct.lpstrTitle = titlep;
3622
3623 if (ext != NULL)
3624 extp = enc_to_utf16(ext, NULL);
3625 fileStruct.lpstrDefExt = extp;
3626
3627 fileStruct.lpstrFile = fileBuf;
3628 fileStruct.nMaxFile = MAXPATHL;
3629 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003630 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3631 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632 if (initdir != NULL && *initdir != NUL)
3633 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003634 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635 initdirp = enc_to_utf16(initdir, NULL);
3636 if (initdirp != NULL)
3637 {
3638 for (wp = initdirp; *wp != NUL; ++wp)
3639 if (*wp == '/')
3640 *wp = '\\';
3641 }
3642 fileStruct.lpstrInitialDir = initdirp;
3643 }
3644
3645 /*
3646 * TODO: Allow selection of multiple files. Needs another arg to this
3647 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3648 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3649 * files that don't exist yet, so I haven't put it in. What about
3650 * OFN_PATHMUSTEXIST?
3651 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3652 */
3653 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003654# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 if (curbuf->b_p_bin)
3656 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003657# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003659 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003661 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663 vim_free(filterp);
3664 vim_free(initdirp);
3665 vim_free(titlep);
3666 vim_free(extp);
3667
K.Takata14b8d6a2022-01-20 15:05:22 +00003668 if (!ret)
3669 return NULL;
3670
3671 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003673 if (p == NULL)
3674 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675
Bram Moolenaar734a8672019-12-02 22:49:38 +01003676 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 SetFocus(s_hwnd);
3678
Bram Moolenaar734a8672019-12-02 22:49:38 +01003679 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003680 q = vim_strsave(shorten_fname1(p));
3681 vim_free(p);
3682 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003684
3685
3686/*
3687 * Convert the string s to the proper format for a filter string by replacing
3688 * the \t and \n delimiters with \0.
3689 * Returns the converted string in allocated memory.
3690 *
3691 * Keep in sync with convert_filterW() above!
3692 */
3693 static char_u *
3694convert_filter(char_u *s)
3695{
3696 char_u *res;
3697 unsigned s_len = (unsigned)STRLEN(s);
3698 unsigned i;
3699
3700 res = alloc(s_len + 3);
3701 if (res != NULL)
3702 {
3703 for (i = 0; i < s_len; ++i)
3704 if (s[i] == '\t' || s[i] == '\n')
3705 res[i] = '\0';
3706 else
3707 res[i] = s[i];
3708 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003709 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 res[s_len + 1] = NUL;
3711 res[s_len + 2] = NUL;
3712 }
3713 return res;
3714}
3715
3716/*
3717 * Select a directory.
3718 */
3719 char_u *
3720gui_mch_browsedir(char_u *title, char_u *initdir)
3721{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // We fake this: Use a filter that doesn't select anything and a default
3723 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003724 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3725 initdir, (char_u *)_("Directory\t*.nothing\n"));
3726}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729 static void
3730_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003731 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 HDROP hDrop)
3733{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003734#define BUFPATHLEN _MAX_PATH
3735#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003736 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737 char szFile[BUFPATHLEN];
3738 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3739 UINT i;
3740 char_u **fnames;
3741 POINT pt;
3742 int_u modifiers = 0;
3743
Bram Moolenaar734a8672019-12-02 22:49:38 +01003744 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003745
Bram Moolenaar734a8672019-12-02 22:49:38 +01003746 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003747 DragQueryPoint(hDrop, &pt);
3748 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3749
3750 reset_VIsual();
3751
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003752 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003753
3754 if (fnames != NULL)
3755 for (i = 0; i < cFiles; ++i)
3756 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3758 fnames[i] = utf16_to_enc(wszFile, NULL);
3759 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 {
3761 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3762 fnames[i] = vim_strsave((char_u *)szFile);
3763 }
3764 }
3765
3766 DragFinish(hDrop);
3767
3768 if (fnames != NULL)
3769 {
3770 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3771 modifiers |= MOUSE_SHIFT;
3772 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3773 modifiers |= MOUSE_CTRL;
3774 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3775 modifiers |= MOUSE_ALT;
3776
3777 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3778
3779 s_need_activate = TRUE;
3780 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781}
3782
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 static int
3784_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003785 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 HWND hwndCtl,
3787 UINT code,
3788 int pos)
3789{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003790 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 scrollbar_T *sb, *sb_info;
3792 long val;
3793 int dragging = FALSE;
3794 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 SCROLLINFO si;
3796
3797 si.cbSize = sizeof(si);
3798 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799
3800 sb = gui_mswin_find_scrollbar(hwndCtl);
3801 if (sb == NULL)
3802 return 0;
3803
Bram Moolenaar734a8672019-12-02 22:49:38 +01003804 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805 {
3806 /*
3807 * Careful: need to get scrollbar info out of first (left) scrollbar
3808 * for window, but keep real scrollbar too because we must pass it to
3809 * gui_drag_scrollbar().
3810 */
3811 sb_info = &sb->wp->w_scrollbars[0];
3812 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003813 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 sb_info = sb;
3815 val = sb_info->value;
3816
3817 switch (code)
3818 {
3819 case SB_THUMBTRACK:
3820 val = pos;
3821 dragging = TRUE;
3822 if (sb->scroll_shift > 0)
3823 val <<= sb->scroll_shift;
3824 break;
3825 case SB_LINEDOWN:
3826 val++;
3827 break;
3828 case SB_LINEUP:
3829 val--;
3830 break;
3831 case SB_PAGEDOWN:
3832 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3833 break;
3834 case SB_PAGEUP:
3835 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3836 break;
3837 case SB_TOP:
3838 val = 0;
3839 break;
3840 case SB_BOTTOM:
3841 val = sb_info->max;
3842 break;
3843 case SB_ENDSCROLL:
3844 if (prev_code == SB_THUMBTRACK)
3845 {
3846 /*
3847 * "pos" only gives us 16-bit data. In case of large file,
3848 * use GetScrollPos() which returns 32-bit. Unfortunately it
3849 * is not valid while the scrollbar is being dragged.
3850 */
3851 val = GetScrollPos(hwndCtl, SB_CTL);
3852 if (sb->scroll_shift > 0)
3853 val <<= sb->scroll_shift;
3854 }
3855 break;
3856
3857 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003858 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003859 return 0;
3860 }
3861 prev_code = code;
3862
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3864 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003865
3866 /*
3867 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3868 */
3869 if (sb->wp != NULL)
3870 {
3871 scrollbar_T *sba = sb->wp->w_scrollbars;
3872 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3873
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003874 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875 }
3876
Bram Moolenaar734a8672019-12-02 22:49:38 +01003877 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 s_busy_processing = TRUE;
3879
Bram Moolenaar734a8672019-12-02 22:49:38 +01003880 // When "allow_scrollbar" is FALSE still need to remember the new
3881 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 dont_scroll = !allow_scrollbar;
3883
Bram Moolenaara338adc2018-01-31 20:51:47 +01003884 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003885 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003886 mch_enable_flush();
3887 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003888
3889 s_busy_processing = FALSE;
3890 dont_scroll = dont_scroll_save;
3891
3892 return 0;
3893}
3894
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896#ifdef FEAT_XPM_W32
3897# include "xpm_w32.h"
3898#endif
3899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900#ifdef __MINGW32__
3901/*
3902 * Add a lot of missing defines.
3903 * They are not always missing, we need the #ifndef's.
3904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905# ifndef IsMinimized
3906# define IsMinimized(hwnd) IsIconic(hwnd)
3907# endif
3908# ifndef IsMaximized
3909# define IsMaximized(hwnd) IsZoomed(hwnd)
3910# endif
3911# ifndef SelectFont
3912# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3913# endif
3914# ifndef GetStockBrush
3915# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3916# endif
3917# ifndef DeleteBrush
3918# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3919# endif
3920
3921# ifndef HANDLE_WM_RBUTTONDBLCLK
3922# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3923 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3924# endif
3925# ifndef HANDLE_WM_MBUTTONUP
3926# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3927 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3928# endif
3929# ifndef HANDLE_WM_MBUTTONDBLCLK
3930# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3931 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3932# endif
3933# ifndef HANDLE_WM_LBUTTONDBLCLK
3934# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3935 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3936# endif
3937# ifndef HANDLE_WM_RBUTTONDOWN
3938# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3939 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3940# endif
3941# ifndef HANDLE_WM_MOUSEMOVE
3942# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3943 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3944# endif
3945# ifndef HANDLE_WM_RBUTTONUP
3946# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3947 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3948# endif
3949# ifndef HANDLE_WM_MBUTTONDOWN
3950# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3951 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3952# endif
3953# ifndef HANDLE_WM_LBUTTONUP
3954# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3955 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3956# endif
3957# ifndef HANDLE_WM_LBUTTONDOWN
3958# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3959 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3960# endif
3961# ifndef HANDLE_WM_SYSCHAR
3962# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3963 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3964# endif
3965# ifndef HANDLE_WM_ACTIVATEAPP
3966# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3967 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3968# endif
3969# ifndef HANDLE_WM_WINDOWPOSCHANGING
3970# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3971 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3972# endif
3973# ifndef HANDLE_WM_VSCROLL
3974# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3975 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3976# endif
3977# ifndef HANDLE_WM_SETFOCUS
3978# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3979 ((fn)((hwnd), (HWND)(wParam)), 0L)
3980# endif
3981# ifndef HANDLE_WM_KILLFOCUS
3982# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3983 ((fn)((hwnd), (HWND)(wParam)), 0L)
3984# endif
3985# ifndef HANDLE_WM_HSCROLL
3986# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3987 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3988# endif
3989# ifndef HANDLE_WM_DROPFILES
3990# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3991 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3992# endif
3993# ifndef HANDLE_WM_CHAR
3994# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3995 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3996# endif
3997# ifndef HANDLE_WM_SYSDEADCHAR
3998# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3999 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4000# endif
4001# ifndef HANDLE_WM_DEADCHAR
4002# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4003 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4004# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004005#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007
Bram Moolenaar734a8672019-12-02 22:49:38 +01004008// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#define TEAROFF_PADDING_X 2
4010#define TEAROFF_BUTTON_PAD_X 8
4011#define TEAROFF_MIN_WIDTH 200
4012#define TEAROFF_SUBMENU_LABEL ">>"
4013#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4014
4015
Bram Moolenaar734a8672019-12-02 22:49:38 +01004016// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004018# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#endif
4020
4021
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004022#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023# define ID_BEVAL_TOOLTIP 200
4024# define BEVAL_TEXT_LEN MAXPATHL
4025
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004026# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004027// Work around old versions of basetsd.h which wrongly declares
4028// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004029# undef UINT_PTR
4030# define UINT_PTR UINT
4031# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004036
Bram Moolenaar82881492012-11-20 16:53:39 +01004037
Bram Moolenaar45360022005-07-21 21:08:21 +00004038typedef struct tagNMTTDISPINFO_NEW
4039{
4040 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004041 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004042 char szText[80];
4043 HINSTANCE hinst;
4044 UINT uFlags;
4045 LPARAM lParam;
4046} NMTTDISPINFO_NEW;
4047
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004048typedef struct tagTOOLINFOW_NEW
4049{
4050 UINT cbSize;
4051 UINT uFlags;
4052 HWND hwnd;
4053 UINT_PTR uId;
4054 RECT rect;
4055 HINSTANCE hinst;
4056 LPWSTR lpszText;
4057 LPARAM lParam;
4058 void *lpReserved;
4059} TOOLINFOW_NEW;
4060
4061typedef struct tagNMTTDISPINFOW_NEW
4062{
4063 NMHDR hdr;
4064 LPWSTR lpszText;
4065 WCHAR szText[80];
4066 HINSTANCE hinst;
4067 UINT uFlags;
4068 LPARAM lParam;
4069} NMTTDISPINFOW_NEW;
4070
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004071
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004072# ifndef TTM_SETMAXTIPWIDTH
4073# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004076# ifndef TTF_DI_SETITEM
4077# define TTF_DI_SETITEM 0x8000
4078# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004079
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004080# ifndef TTN_GETDISPINFO
4081# define TTN_GETDISPINFO (TTN_FIRST - 0)
4082# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004083
Bram Moolenaar734a8672019-12-02 22:49:38 +01004084#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004085
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004086#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004087// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4088// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004089// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004090// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004091# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4092typedef struct tagNMTTDISPINFOA {
4093 NMHDR hdr;
4094 LPSTR lpszText;
4095 char szText[80];
4096 HINSTANCE hinst;
4097 UINT uFlags;
4098 LPARAM lParam;
4099} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4100# define LPNMTTDISPINFO LPNMTTDISPINFOA
4101
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004102typedef struct tagNMTTDISPINFOW {
4103 NMHDR hdr;
4104 LPWSTR lpszText;
4105 WCHAR szText[80];
4106 HINSTANCE hinst;
4107 UINT uFlags;
4108 LPARAM lParam;
4109} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004110# endif
4111#endif
4112
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004113#ifndef TTN_GETDISPINFOW
4114# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4115#endif
4116
Bram Moolenaar734a8672019-12-02 22:49:38 +01004117// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119#ifdef FEAT_MENU
4120static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123/*
4124 * Use the system font for dialogs and tear-off menus. Remove this line to
4125 * use DLG_FONT_NAME.
4126 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004127#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#define VIM_CLASSW L"Vim"
4131
Bram Moolenaar734a8672019-12-02 22:49:38 +01004132// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4133// thus there should be room for every dialog. For tearoffs it's made bigger
4134// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#define DLG_ALLOC_SIZE 16 * 1024
4136
4137/*
4138 * stuff for dialogs, menus, tearoffs etc.
4139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140static PWORD
4141add_dialog_element(
4142 PWORD p,
4143 DWORD lStyle,
4144 WORD x,
4145 WORD y,
4146 WORD w,
4147 WORD h,
4148 WORD Id,
4149 WORD clss,
4150 const char *caption);
4151static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004152static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004153#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156static void get_dialog_font_metrics(void);
4157
4158static int dialog_default_button = -1;
4159
Bram Moolenaar734a8672019-12-02 22:49:38 +01004160// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Bram Moolenaar734a8672019-12-02 22:49:38 +01004163static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#ifdef FEAT_TOOLBAR
4165static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004166static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004167static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004169#else
4170# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#endif
4172
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004173#ifdef FEAT_GUI_TABLINE
4174static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004175static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004176#endif
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#ifdef FEAT_MBYTE_IME
4179static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4180static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4181#endif
4182#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4183# ifdef NOIME
4184typedef struct tagCOMPOSITIONFORM {
4185 DWORD dwStyle;
4186 POINT ptCurrentPos;
4187 RECT rcArea;
4188} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4189typedef HANDLE HIMC;
4190# endif
4191
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004192static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004193static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4194static HIMC (WINAPI *pImmGetContext)(HWND);
4195static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4196static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4197static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4198static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004199static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4200static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004201static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4202static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004203static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204static void dyn_imm_load(void);
4205#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206# define pImmGetCompositionStringW ImmGetCompositionStringW
4207# define pImmGetContext ImmGetContext
4208# define pImmAssociateContext ImmAssociateContext
4209# define pImmReleaseContext ImmReleaseContext
4210# define pImmGetOpenStatus ImmGetOpenStatus
4211# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004212# define pImmGetCompositionFontW ImmGetCompositionFontW
4213# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214# define pImmSetCompositionWindow ImmSetCompositionWindow
4215# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004216# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#endif
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#ifdef FEAT_MENU
4220/*
4221 * Figure out how high the menu bar is at the moment.
4222 */
4223 static int
4224gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004225 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 static int old_menu_height = -1;
4228
4229 RECT rc1, rc2;
4230 int num;
4231 int menu_height;
4232
4233 if (gui.menu_is_active)
4234 num = GetMenuItemCount(s_menuBar);
4235 else
4236 num = 0;
4237
4238 if (num == 0)
4239 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004240 else if (IsMinimized(s_hwnd))
4241 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004242 // The height of the menu cannot be determined while the window is
4243 // minimized. Take the previous height if the menu is changed in that
4244 // state, to avoid that Vim's vertical window size accidentally
4245 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004246 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 else
4249 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004250 /*
4251 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4252 * seem to have been set yet, so menu wraps in default window
4253 * width which is very narrow. Instead just return height of a
4254 * single menu item. Will still be wrong when the menu really
4255 * should wrap over more than one line.
4256 */
4257 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4258 if (gui.starting)
4259 menu_height = rc1.bottom - rc1.top + 1;
4260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004262 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4263 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 }
4266
4267 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004268 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004269 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 return menu_height;
4272}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004273#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275
4276/*
4277 * Setup for the Intellimouse
4278 */
4279 static void
4280init_mouse_wheel(void)
4281{
4282
4283#ifndef SPI_GETWHEELSCROLLLINES
4284# define SPI_GETWHEELSCROLLLINES 104
4285#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004286#ifndef SPI_SETWHEELSCROLLLINES
4287# define SPI_SETWHEELSCROLLLINES 105
4288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
Bram Moolenaar734a8672019-12-02 22:49:38 +01004290#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4291#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4293#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4294
Bram Moolenaar734a8672019-12-02 22:49:38 +01004295 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar734a8672019-12-02 22:49:38 +01004297 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004298 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4299 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300}
4301
4302
Bram Moolenaarae20f342019-11-05 21:09:23 +01004303/*
4304 * Intellimouse wheel handler.
4305 * Treat a mouse wheel event as if it were a scroll request.
4306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 static void
4308_OnMouseWheel(
4309 HWND hwnd,
4310 short zDelta)
4311{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 int i;
4313 int size;
4314 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004315 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (mouse_scroll_lines == 0)
4318 init_mouse_wheel();
4319
Bram Moolenaarae20f342019-11-05 21:09:23 +01004320 wp = gui_mouse_window(FIND_POPUP);
4321
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004322#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004323 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004324 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004325 cmdarg_T cap;
4326 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004327
Bram Moolenaarae20f342019-11-05 21:09:23 +01004328 // Mouse hovers over popup window, scroll it if possible.
4329 mouse_row = wp->w_winrow;
4330 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004331 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004332 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4333 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4334 clear_oparg(&oa);
4335 cap.oap = &oa;
4336 nv_mousescroll(&cap);
4337 update_screen(0);
4338 setcursor();
4339 out_flush();
4340 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004341 }
4342#endif
4343
Bram Moolenaarae20f342019-11-05 21:09:23 +01004344 if (wp == NULL || !p_scf)
4345 wp = curwin;
4346
4347 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4348 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4349 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4350 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4351 else
4352 return;
4353 size = wp->w_height;
4354
Bram Moolenaara338adc2018-01-31 20:51:47 +01004355 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (mouse_scroll_lines > 0
4357 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4358 {
4359 for (i = mouse_scroll_lines; i > 0; --i)
4360 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4361 }
4362 else
4363 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004364 mch_enable_flush();
4365 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366}
4367
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004368#ifdef USE_SYSMENU_FONT
4369/*
4370 * Get Menu Font.
4371 * Return OK or FAIL.
4372 */
4373 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004374gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004375{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004376 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004377
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004378 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4379 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004380 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004381 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004382 &nm,
4383 0))
4384 return FAIL;
4385 *lf = nm.lfMenuFont;
4386 return OK;
4387}
4388#endif
4389
4390
4391#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4392/*
4393 * Set the GUI tabline font to the system menu font
4394 */
4395 static void
4396set_tabline_font(void)
4397{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004398 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004399 HFONT font;
4400 HWND hwnd;
4401 HDC hdc;
4402 HFONT hfntOld;
4403 TEXTMETRIC tm;
4404
4405 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4406 return;
4407
K.Takatac81e9bf2022-01-16 14:15:49 +00004408 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004409 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004410
4411 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4412
4413 /*
4414 * Compute the height of the font used for the tab text
4415 */
4416 hwnd = GetDesktopWindow();
4417 hdc = GetWindowDC(hwnd);
4418 hfntOld = SelectFont(hdc, font);
4419
4420 GetTextMetrics(hdc, &tm);
4421
4422 SelectFont(hdc, hfntOld);
4423 ReleaseDC(hwnd, hdc);
4424
4425 /*
4426 * The space used by the tab border and the space between the tab label
4427 * and the tab border is included as 7.
4428 */
4429 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4430}
K.Takatac81e9bf2022-01-16 14:15:49 +00004431#else
4432# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004433#endif
4434
Bram Moolenaar520470a2005-06-16 21:59:56 +00004435/*
4436 * Invoked when a setting was changed.
4437 */
4438 static LRESULT CALLBACK
4439_OnSettingChange(UINT n)
4440{
4441 if (n == SPI_SETWHEELSCROLLLINES)
4442 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4443 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004444 if (n == SPI_SETNONCLIENTMETRICS)
4445 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004446 return 0;
4447}
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449#ifdef FEAT_NETBEANS_INTG
4450 static void
4451_OnWindowPosChanged(
4452 HWND hwnd,
4453 const LPWINDOWPOS lpwpos)
4454{
4455 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004456 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4459 || lpwpos->cx != cx || lpwpos->cy != cy))
4460 {
4461 x = lpwpos->x;
4462 y = lpwpos->y;
4463 cx = lpwpos->cx;
4464 cy = lpwpos->cy;
4465 netbeans_frame_moved(x, y);
4466 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004467 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004468 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469}
4470#endif
4471
K.Takata4f2417f2021-06-05 16:25:32 +02004472
4473static HWND hwndTip = NULL;
4474
4475 static void
4476show_sizing_tip(int cols, int rows)
4477{
4478 TOOLINFOA ti = {sizeof(ti)};
4479 char buf[32];
4480
4481 ti.hwnd = s_hwnd;
4482 ti.uId = (UINT_PTR)s_hwnd;
4483 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4484 ti.lpszText = buf;
4485 sprintf(buf, "%dx%d", cols, rows);
4486 if (hwndTip == NULL)
4487 {
4488 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4489 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4490 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4491 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4492 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4493 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4494 }
4495 else
4496 {
4497 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4498 }
4499 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4500}
4501
4502 static void
4503destroy_sizing_tip(void)
4504{
4505 if (hwndTip != NULL)
4506 {
4507 DestroyWindow(hwndTip);
4508 hwndTip = NULL;
4509 }
4510}
4511
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 static int
4513_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 UINT fwSide,
4515 LPRECT lprc)
4516{
4517 int w, h;
4518 int valid_w, valid_h;
4519 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004520 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 w = lprc->right - lprc->left;
4523 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004524 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 w_offset = w - valid_w;
4526 h_offset = h - valid_h;
4527
4528 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4529 || fwSide == WMSZ_BOTTOMLEFT)
4530 lprc->left += w_offset;
4531 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4532 || fwSide == WMSZ_BOTTOMRIGHT)
4533 lprc->right -= w_offset;
4534
4535 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4536 || fwSide == WMSZ_TOPRIGHT)
4537 lprc->top += h_offset;
4538 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4539 || fwSide == WMSZ_BOTTOMRIGHT)
4540 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004541
4542 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 return TRUE;
4544}
4545
K.Takata92000e22022-01-20 15:10:57 +00004546#ifdef FEAT_GUI_TABLINE
4547 static void
4548_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4549{
4550 if (gui_mch_showing_tabline())
4551 {
4552 POINT pt;
4553 RECT rect;
4554
4555 /*
4556 * If the cursor is on the tabline, display the tab menu
4557 */
4558 GetCursorPos(&pt);
4559 GetWindowRect(s_textArea, &rect);
4560 if (pt.y < rect.top)
4561 {
4562 show_tabline_popup_menu();
4563 return;
4564 }
4565 }
4566 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4567}
4568
4569 static void
4570_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4571{
4572 /*
4573 * If the user double clicked the tabline, create a new tab
4574 */
4575 if (gui_mch_showing_tabline())
4576 {
4577 POINT pt;
4578 RECT rect;
4579
4580 GetCursorPos(&pt);
4581 GetWindowRect(s_textArea, &rect);
4582 if (pt.y < rect.top)
4583 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4584 }
4585 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4586}
4587#endif
4588
4589 static UINT
4590_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4591{
4592 UINT result;
4593 int x, y;
4594
4595 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4596 if (result != HTCLIENT)
4597 return result;
4598
4599#ifdef FEAT_GUI_TABLINE
4600 if (gui_mch_showing_tabline())
4601 {
4602 RECT rct;
4603
4604 // If the cursor is on the GUI tabline, don't process this event
4605 GetWindowRect(s_textArea, &rct);
4606 if (yPos < rct.top)
4607 return result;
4608 }
4609#endif
4610 (void)gui_mch_get_winpos(&x, &y);
4611 xPos -= x;
4612
4613 if (xPos < 48) // <VN> TODO should use system metric?
4614 return HTBOTTOMLEFT;
4615 else
4616 return HTBOTTOMRIGHT;
4617}
4618
4619#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4620 static LRESULT
4621_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4622{
4623 switch (hdr->code)
4624 {
4625 case TTN_GETDISPINFOW:
4626 case TTN_GETDISPINFO:
4627 {
4628 char_u *str = NULL;
4629 static void *tt_text = NULL;
4630
4631 VIM_CLEAR(tt_text);
4632
4633# ifdef FEAT_GUI_TABLINE
4634 if (gui_mch_showing_tabline()
4635 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4636 {
4637 POINT pt;
4638 /*
4639 * Mouse is over the GUI tabline. Display the
4640 * tooltip for the tab under the cursor
4641 *
4642 * Get the cursor position within the tab control
4643 */
4644 GetCursorPos(&pt);
4645 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4646 {
4647 TCHITTESTINFO htinfo;
4648 int idx;
4649
4650 /*
4651 * Get the tab under the cursor
4652 */
4653 htinfo.pt.x = pt.x;
4654 htinfo.pt.y = pt.y;
4655 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4656 if (idx != -1)
4657 {
4658 tabpage_T *tp;
4659
4660 tp = find_tabpage(idx + 1);
4661 if (tp != NULL)
4662 {
4663 get_tabline_label(tp, TRUE);
4664 str = NameBuff;
4665 }
4666 }
4667 }
4668 }
4669# endif
4670# ifdef FEAT_TOOLBAR
4671# ifdef FEAT_GUI_TABLINE
4672 else
4673# endif
4674 {
4675 UINT idButton;
4676 vimmenu_T *pMenu;
4677
4678 idButton = (UINT) hdr->idFrom;
4679 pMenu = gui_mswin_find_menu(root_menu, idButton);
4680 if (pMenu)
4681 str = pMenu->strings[MENU_INDEX_TIP];
4682 }
4683# endif
4684 if (str == NULL)
4685 break;
4686
4687 // Set the maximum width, this also enables using \n for
4688 // line break.
4689 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4690
4691 if (hdr->code == TTN_GETDISPINFOW)
4692 {
4693 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4694
4695 tt_text = enc_to_utf16(str, NULL);
4696 lpdi->lpszText = tt_text;
4697 // can't show tooltip if failed
4698 }
4699 else
4700 {
4701 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4702
4703 if (STRLEN(str) < sizeof(lpdi->szText)
4704 || ((tt_text = vim_strsave(str)) == NULL))
4705 vim_strncpy((char_u *)lpdi->szText, str,
4706 sizeof(lpdi->szText) - 1);
4707 else
4708 lpdi->lpszText = tt_text;
4709 }
4710 }
4711 break;
4712
4713# ifdef FEAT_GUI_TABLINE
4714 case TCN_SELCHANGE:
4715 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4716 {
4717 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4718 return 0L;
4719 }
4720 break;
4721
4722 case NM_RCLICK:
4723 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4724 {
4725 show_tabline_popup_menu();
4726 return 0L;
4727 }
4728 break;
4729# endif
4730
4731 default:
4732 break;
4733 }
4734 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4735}
4736#endif
4737
4738#if defined(MENUHINTS) && defined(FEAT_MENU)
4739 static LRESULT
4740_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4741{
4742 if (((UINT) HIWORD(wParam)
4743 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4744 == MF_HILITE
4745 && (State & CMDLINE) == 0)
4746 {
4747 UINT idButton;
4748 vimmenu_T *pMenu;
4749 static int did_menu_tip = FALSE;
4750
4751 if (did_menu_tip)
4752 {
4753 msg_clr_cmdline();
4754 setcursor();
4755 out_flush();
4756 did_menu_tip = FALSE;
4757 }
4758
4759 idButton = (UINT)LOWORD(wParam);
4760 pMenu = gui_mswin_find_menu(root_menu, idButton);
4761 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4762 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4763 {
4764 ++msg_hist_off;
4765 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4766 --msg_hist_off;
4767 setcursor();
4768 out_flush();
4769 did_menu_tip = TRUE;
4770 }
4771 return 0L;
4772 }
4773 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4774}
4775#endif
4776
K.Takatac81e9bf2022-01-16 14:15:49 +00004777 static LRESULT
4778_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4779{
4780 s_dpi = ydpi;
4781 s_in_dpichanged = TRUE;
4782 //TRACE("DPI: %d", ydpi);
4783
4784 update_scrollbar_size();
4785 update_toolbar_size();
4786 set_tabline_font();
4787
4788 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4789 gui_get_wide_font();
4790 gui_mswin_get_menu_height(FALSE);
4791#ifdef FEAT_MBYTE_IME
4792 im_set_position(gui.row, gui.col);
4793#endif
4794 InvalidateRect(hwnd, NULL, TRUE);
4795
4796 s_in_dpichanged = FALSE;
4797 return 0L;
4798}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800
4801 static LRESULT CALLBACK
4802_WndProc(
4803 HWND hwnd,
4804 UINT uMsg,
4805 WPARAM wParam,
4806 LPARAM lParam)
4807{
4808 /*
4809 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4810 hwnd, uMsg, wParam, lParam);
4811 */
4812
4813 HandleMouseHide(uMsg, lParam);
4814
4815 s_uMsg = uMsg;
4816 s_wParam = wParam;
4817 s_lParam = lParam;
4818
4819 switch (uMsg)
4820 {
4821 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4822 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004823 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004825 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4827 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4828 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4829 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4830#ifdef FEAT_MENU
4831 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4832#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004833 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4834 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4836 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004837 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4838 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4840 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4841 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4842#ifdef FEAT_NETBEANS_INTG
4843 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4844#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004845#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004846 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4847 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004848#endif
K.Takata92000e22022-01-20 15:10:57 +00004849 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004850
Bram Moolenaar734a8672019-12-02 22:49:38 +01004851 case WM_QUERYENDSESSION: // System wants to go down.
4852 gui_shell_closed(); // Will exit when no changed buffers.
4853 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004856 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004859 return 0L;
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 break;
4862
4863 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004864 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4865 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004866 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return 0L;
4868
4869 case WM_SYSCHAR:
4870 /*
4871 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4872 * shortcut key, handle like a typed ALT key, otherwise call Windows
4873 * ALT key handling.
4874 */
4875#ifdef FEAT_MENU
4876 if ( !gui.menu_is_active
4877 || p_wak[0] == 'n'
4878 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4879 )
4880#endif
4881 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004882 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return 0L;
4884 }
4885#ifdef FEAT_MENU
4886 else
K.Takata4ac893f2022-01-20 12:44:28 +00004887 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
4889
4890 case WM_SYSKEYUP:
4891#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004892 // This used to be done only when menu is active: ALT key is used for
4893 // that. But that caused problems when menu is disabled and using
4894 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4895 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004896 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004898 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#endif
4900
K.Takata4f2417f2021-06-05 16:25:32 +02004901 case WM_EXITSIZEMOVE:
4902 destroy_sizing_tip();
4903 break;
4904
Bram Moolenaar734a8672019-12-02 22:49:38 +01004905 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908 case WM_MOUSEWHEEL:
4909 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004910 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
Bram Moolenaar734a8672019-12-02 22:49:38 +01004912 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004913 case WM_SETTINGCHANGE:
4914 return _OnSettingChange((UINT)wParam);
4915
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004916#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004918 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919#endif
K.Takata92000e22022-01-20 15:10:57 +00004920
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921#if defined(MENUHINTS) && defined(FEAT_MENU)
4922 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004923 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926#ifdef FEAT_MBYTE_IME
4927 case WM_IME_NOTIFY:
4928 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004929 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004930 return 1L;
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 case WM_IME_COMPOSITION:
4933 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004934 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004935 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004937 case WM_DPICHANGED:
4938 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4939 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004943 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945#endif
K.Takata92000e22022-01-20 15:10:57 +00004946 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948
K.Takata4ac893f2022-01-20 12:44:28 +00004949 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950}
4951
4952/*
4953 * End of call-back routines
4954 */
4955
Bram Moolenaar734a8672019-12-02 22:49:38 +01004956// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957HWND vim_parent_hwnd = NULL;
4958
4959 static BOOL CALLBACK
4960FindWindowTitle(HWND hwnd, LPARAM lParam)
4961{
4962 char buf[2048];
4963 char *title = (char *)lParam;
4964
4965 if (GetWindowText(hwnd, buf, sizeof(buf)))
4966 {
4967 if (strstr(buf, title) != NULL)
4968 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004969 // Found it. Store the window ref. and quit searching if MDI
4970 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004972 if (vim_parent_hwnd != NULL)
4973 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004976 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977}
4978
4979/*
4980 * Invoked for '-P "title"' argument: search for parent application to open
4981 * our window in.
4982 */
4983 void
4984gui_mch_set_parent(char *title)
4985{
4986 EnumWindows(FindWindowTitle, (LPARAM)title);
4987 if (vim_parent_hwnd == NULL)
4988 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004989 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 mch_exit(2);
4991 }
4992}
4993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004994#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 static void
4996ole_error(char *arg)
4997{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004998 char buf[IOSIZE];
4999
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005000# ifdef VIMDLL
5001 gui.in_use = mch_is_gui_executable();
5002# endif
5003
Bram Moolenaar734a8672019-12-02 22:49:38 +01005004 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005005 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005006 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005007 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005011#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5012 static char *
5013gvim_error(void)
5014{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005015 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005016
5017 if (starting)
5018 {
5019 mch_errmsg(msg);
5020 mch_errmsg("\n");
5021 mch_exit(2);
5022 }
5023 return msg;
5024}
5025
5026 char *
5027gui_mch_do_spawn(char_u *arg)
5028{
5029 int len;
5030# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5031 char_u *session = NULL;
5032 LPWSTR tofree1 = NULL;
5033# endif
5034 WCHAR name[MAX_PATH];
5035 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5036 STARTUPINFOW si = {sizeof(si)};
5037 PROCESS_INFORMATION pi;
5038
5039 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5040 goto error;
5041 p = wcsrchr(name, L'\\');
5042 if (p == NULL)
5043 goto error;
5044 // Replace the executable name from vim(d).exe to gvim(d).exe.
5045# ifdef DEBUG
5046 wcscpy(p + 1, L"gvimd.exe");
5047# else
5048 wcscpy(p + 1, L"gvim.exe");
5049# endif
5050
5051# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5052 if (starting)
5053# endif
5054 {
5055 // Pass the command line to the new process.
5056 p = GetCommandLineW();
5057 // Skip 1st argument.
5058 while (*p && *p != L' ' && *p != L'\t')
5059 {
5060 if (*p == L'"')
5061 {
5062 while (*p && *p != L'"')
5063 ++p;
5064 if (*p)
5065 ++p;
5066 }
5067 else
5068 ++p;
5069 }
5070 cmd = p;
5071 }
5072# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5073 else
5074 {
5075 // Create a session file and pass it to the new process.
5076 LPWSTR wsession;
5077 char_u *savebg;
5078 int ret;
5079
5080 session = vim_tempname('s', FALSE);
5081 if (session == NULL)
5082 goto error;
5083 savebg = p_bg;
5084 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5085 ret = write_session_file(session);
5086 vim_free(p_bg);
5087 p_bg = savebg;
5088 if (!ret)
5089 goto error;
5090 wsession = enc_to_utf16(session, NULL);
5091 if (wsession == NULL)
5092 goto error;
5093 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005094 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005095 if (cmd == NULL)
5096 {
5097 vim_free(wsession);
5098 goto error;
5099 }
5100 tofree1 = cmd;
5101 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5102 wsession, wsession);
5103 vim_free(wsession);
5104 }
5105# endif
5106
5107 // Check additional arguments to the `:gui` command.
5108 if (arg != NULL)
5109 {
5110 warg = enc_to_utf16(arg, NULL);
5111 if (warg == NULL)
5112 goto error;
5113 tofree2 = warg;
5114 }
5115 else
5116 warg = L"";
5117
5118 // Set up the new command line.
5119 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005120 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005121 if (newcmd == NULL)
5122 goto error;
5123 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5124
5125 // Spawn a new GUI process.
5126 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5127 NULL, NULL, &si, &pi))
5128 goto error;
5129 CloseHandle(pi.hProcess);
5130 CloseHandle(pi.hThread);
5131 mch_exit(0);
5132
5133error:
5134# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5135 if (session)
5136 mch_remove(session);
5137 vim_free(session);
5138 vim_free(tofree1);
5139# endif
5140 vim_free(newcmd);
5141 vim_free(tofree2);
5142 return gvim_error();
5143}
5144#endif
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146/*
5147 * Parse the GUI related command-line arguments. Any arguments used are
5148 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005149 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
5151 void
5152gui_mch_prepare(int *argc, char **argv)
5153{
5154 int silent = FALSE;
5155 int idx;
5156
Bram Moolenaar734a8672019-12-02 22:49:38 +01005157 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5159 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005160 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5162 && (argv[2][0] == '-' || argv[2][0] == '/'))
5163 {
5164 silent = TRUE;
5165 idx = 2;
5166 }
5167 else
5168 idx = 1;
5169
Bram Moolenaar734a8672019-12-02 22:49:38 +01005170 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 if (STRICMP(argv[idx] + 1, "register") == 0)
5172 {
5173#ifdef FEAT_OLE
5174 RegisterMe(silent);
5175 mch_exit(0);
5176#else
5177 if (!silent)
5178 ole_error("register");
5179 mch_exit(2);
5180#endif
5181 }
5182
Bram Moolenaar734a8672019-12-02 22:49:38 +01005183 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5185 {
5186#ifdef FEAT_OLE
5187 UnregisterMe(!silent);
5188 mch_exit(0);
5189#else
5190 if (!silent)
5191 ole_error("unregister");
5192 mch_exit(2);
5193#endif
5194 }
5195
Bram Moolenaar734a8672019-12-02 22:49:38 +01005196 // Ignore an -embedding argument. It is only relevant if the
5197 // application wants to treat the case when it is started manually
5198 // differently from the case where it is started via automation (and
5199 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5201 {
5202#ifdef FEAT_OLE
5203 *argc = 1;
5204#else
5205 ole_error("embedding");
5206 mch_exit(2);
5207#endif
5208 }
5209 }
5210
5211#ifdef FEAT_OLE
5212 {
5213 int bDoRestart = FALSE;
5214
5215 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005216 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (bDoRestart)
5218 mch_exit(0);
5219 }
5220#endif
5221
5222#ifdef FEAT_NETBEANS_INTG
5223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005224 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 int arg;
5226
5227 for (arg = 1; arg < *argc; arg++)
5228 if (strncmp("-nb", argv[arg], 3) == 0)
5229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 netbeansArg = argv[arg];
5231 mch_memmove(&argv[arg], &argv[arg + 1],
5232 (--*argc - arg) * sizeof(char *));
5233 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005234 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238}
5239
K.Takatac81e9bf2022-01-16 14:15:49 +00005240 static void
5241load_dpi_func(void)
5242{
5243 HMODULE hUser32;
5244
5245 hUser32 = GetModuleHandle("user32.dll");
5246 if (hUser32 == NULL)
5247 goto fail;
5248
5249 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5250 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5251 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5252 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5253 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5254 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5255
5256 if (pSetThreadDpiAwarenessContext != NULL)
5257 {
5258 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5259 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5260 if (oldctx != NULL)
5261 {
5262 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5263 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5264#ifdef DEBUG
5265 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5266 {
5267 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5268 }
5269#endif
5270 return;
5271 }
5272 }
5273
5274fail:
5275 // Disable PerMonitorV2 APIs.
5276 pGetDpiForSystem = stubGetDpiForSystem;
5277 pGetDpiForWindow = NULL;
5278 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5279 pSetThreadDpiAwarenessContext = NULL;
5280 pGetAwarenessFromDpiAwarenessContext = NULL;
5281}
5282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283/*
5284 * Initialise the GUI. Create all the windows, set up all the call-backs
5285 * etc.
5286 */
5287 int
5288gui_mch_init(void)
5289{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005291 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
Bram Moolenaar734a8672019-12-02 22:49:38 +01005294 // Return here if the window was already opened (happens when
5295 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005297 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 /*
5300 * Load the tearoff bitmap
5301 */
5302#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005303 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#endif
5305
K.Takatac81e9bf2022-01-16 14:15:49 +00005306 load_dpi_func();
5307
5308 s_dpi = pGetDpiForSystem();
5309 update_scrollbar_size();
5310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005312 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#endif
5314 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005315#ifdef FEAT_TOOLBAR
5316 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
5319 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5320
Bram Moolenaar734a8672019-12-02 22:49:38 +01005321 // First try using the wide version, so that we can use any title.
5322 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005323 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005325 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 wndclassw.lpfnWndProc = _WndProc;
5327 wndclassw.cbClsExtra = 0;
5328 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005329 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5331 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5332 wndclassw.hbrBackground = s_brush;
5333 wndclassw.lpszMenuName = NULL;
5334 wndclassw.lpszClassName = szVimWndClassW;
5335
K.Takata4ac893f2022-01-20 12:44:28 +00005336 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005337 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 if (vim_parent_hwnd != NULL)
5341 {
5342#ifdef HAVE_TRY_EXCEPT
5343 __try
5344 {
5345#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005346 // Open inside the specified parent window.
5347 // TODO: last argument should point to a CLIENTCREATESTRUCT
5348 // structure.
5349 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005351 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005352 WS_OVERLAPPEDWINDOW | WS_CHILD
5353 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5355 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005356 100, // Any value will do
5357 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005359 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360#ifdef HAVE_TRY_EXCEPT
5361 }
5362 __except(EXCEPTION_EXECUTE_HANDLER)
5363 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005364 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366#endif
5367 if (s_hwnd == NULL)
5368 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005369 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 mch_exit(2);
5371 }
5372 }
5373 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005374 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005375 // If the provided windowid is not valid reset it to zero, so that it
5376 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005377 if (IsWindow((HWND)win_socket_id) <= 0)
5378 win_socket_id = 0;
5379
Bram Moolenaar734a8672019-12-02 22:49:38 +01005380 // Create a window. If win_socket_id is not zero without border and
5381 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005382 s_hwnd = CreateWindowW(
5383 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005384 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5385 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005386 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5387 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005388 100, // Any value will do
5389 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005390 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005391 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005392 if (s_hwnd != NULL && win_socket_id != 0)
5393 {
5394 SetParent(s_hwnd, (HWND)win_socket_id);
5395 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5396 }
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
5399 if (s_hwnd == NULL)
5400 return FAIL;
5401
K.Takatac81e9bf2022-01-16 14:15:49 +00005402 if (pGetDpiForWindow != NULL)
5403 {
5404 s_dpi = pGetDpiForWindow(s_hwnd);
5405 update_scrollbar_size();
5406 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5407 }
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5410 dyn_imm_load();
5411#endif
5412
Bram Moolenaar734a8672019-12-02 22:49:38 +01005413 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005414 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005415 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005416 wndclassw.style = CS_OWNDC;
5417 wndclassw.lpfnWndProc = _TextAreaWndProc;
5418 wndclassw.cbClsExtra = 0;
5419 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005420 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005421 wndclassw.hIcon = NULL;
5422 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5423 wndclassw.hbrBackground = NULL;
5424 wndclassw.lpszMenuName = NULL;
5425 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005426
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005427 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 return FAIL;
5429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005431 s_textArea = CreateWindowExW(
5432 0,
5433 szTextAreaClassW, L"Vim text area",
5434 WS_CHILD | WS_VISIBLE, 0, 0,
5435 100, // Any value will do for now
5436 100, // Any value will do for now
5437 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005438 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005439
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 if (s_textArea == NULL)
5441 return FAIL;
5442
Bram Moolenaar20321902016-02-17 12:30:17 +01005443#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005444 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005445 {
5446 HANDLE hIcon = NULL;
5447
5448 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005449 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005450 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005451#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453#ifdef FEAT_MENU
5454 s_menuBar = CreateMenu();
5455#endif
5456 s_hdc = GetDC(s_textArea);
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
Bram Moolenaar734a8672019-12-02 22:49:38 +01005460 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005461 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
Bram Moolenaar734a8672019-12-02 22:49:38 +01005463 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 gui_mch_def_colors();
5465
Bram Moolenaar734a8672019-12-02 22:49:38 +01005466 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5467 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 set_normal_colors();
5469
5470 /*
5471 * Check that none of the colors are the same as the background color.
5472 * Then store the current values as the defaults.
5473 */
5474 gui_check_colors();
5475 gui.def_norm_pixel = gui.norm_pixel;
5476 gui.def_back_pixel = gui.back_pixel;
5477
Bram Moolenaar734a8672019-12-02 22:49:38 +01005478 // Get the colors for the highlight groups (gui_check_colors() might have
5479 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 highlight_gui_started();
5481
5482 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005483 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005485 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487 /*
5488 * Set up for Intellimouse processing
5489 */
5490 init_mouse_wheel();
5491
5492 /*
5493 * compute a couple of metrics used for the dialogs
5494 */
5495 get_dialog_font_metrics();
5496#ifdef FEAT_TOOLBAR
5497 /*
5498 * Create the toolbar
5499 */
5500 initialise_toolbar();
5501#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005502#ifdef FEAT_GUI_TABLINE
5503 /*
5504 * Create the tabline
5505 */
5506 initialise_tabline();
5507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508#ifdef MSWIN_FIND_REPLACE
5509 /*
5510 * Initialise the dialog box stuff
5511 */
5512 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5513
Bram Moolenaar734a8672019-12-02 22:49:38 +01005514 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005516 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005518 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5520 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5521 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005524#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005525# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005526// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005527# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005528# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005529# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005530# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005531 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005532 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005533#endif
5534
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005535#ifdef FEAT_RENDER_OPTIONS
5536 if (p_rop)
5537 (void)gui_mch_set_rendering_options(p_rop);
5538#endif
5539
Bram Moolenaar748bf032005-02-02 23:04:36 +00005540theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005541 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005542 display_errors();
5543
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 return OK;
5545}
5546
5547/*
5548 * Get the size of the screen, taking position on multiple monitors into
5549 * account (if supported).
5550 */
5551 static void
5552get_work_area(RECT *spi_rect)
5553{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005554 HMONITOR mon;
5555 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaar734a8672019-12-02 22:49:38 +01005557 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005558 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005559 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005561 moninfo.cbSize = sizeof(MONITORINFO);
5562 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005564 *spi_rect = moninfo.rcWork;
5565 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005568 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5570}
5571
5572/*
5573 * Set the size of the window to the given width and height in pixels.
5574 */
5575 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005576gui_mch_set_shellsize(
5577 int width,
5578 int height,
5579 int min_width UNUSED,
5580 int min_height UNUSED,
5581 int base_width UNUSED,
5582 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005583 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005586 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar734a8672019-12-02 22:49:38 +01005589 // Try to keep window completely on screen.
5590 // Get position of the screen work area. This is the part that is not
5591 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 get_work_area(&workarea_rect);
5593
Bram Moolenaar734a8672019-12-02 22:49:38 +01005594 // Resizing a maximized window looks very strange, unzoom it first.
5595 // But don't do it when still starting up, it may have been requested in
5596 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005597 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005599
5600 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
Bram Moolenaar734a8672019-12-02 22:49:38 +01005602 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005603 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5604 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5605 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5606 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5607 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5608 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar734a8672019-12-02 22:49:38 +01005610 // The following should take care of keeping Vim on the same monitor, no
5611 // matter if the secondary monitor is left or right of the primary
5612 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005613 window_rect.right = window_rect.left + win_width;
5614 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005615
Bram Moolenaar734a8672019-12-02 22:49:38 +01005616 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005617 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5618 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005620 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5621 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005623 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5624 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005626 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5627 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005629 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5630 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 SetActiveWindow(s_hwnd);
5633 SetFocus(s_hwnd);
5634
Bram Moolenaar734a8672019-12-02 22:49:38 +01005635 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637}
5638
5639
5640 void
5641gui_mch_set_scrollbar_thumb(
5642 scrollbar_T *sb,
5643 long val,
5644 long size,
5645 long max)
5646{
5647 SCROLLINFO info;
5648
5649 sb->scroll_shift = 0;
5650 while (max > 32767)
5651 {
5652 max = (max + 1) >> 1;
5653 val >>= 1;
5654 size >>= 1;
5655 ++sb->scroll_shift;
5656 }
5657
5658 if (sb->scroll_shift > 0)
5659 ++size;
5660
5661 info.cbSize = sizeof(info);
5662 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5663 info.nPos = val;
5664 info.nMin = 0;
5665 info.nMax = max;
5666 info.nPage = size;
5667 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5668}
5669
5670
5671/*
5672 * Set the current text font.
5673 */
5674 void
5675gui_mch_set_font(GuiFont font)
5676{
5677 gui.currFont = font;
5678}
5679
5680
5681/*
5682 * Set the current text foreground color.
5683 */
5684 void
5685gui_mch_set_fg_color(guicolor_T color)
5686{
5687 gui.currFgColor = color;
5688}
5689
5690/*
5691 * Set the current text background color.
5692 */
5693 void
5694gui_mch_set_bg_color(guicolor_T color)
5695{
5696 gui.currBgColor = color;
5697}
5698
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005699/*
5700 * Set the current text special color.
5701 */
5702 void
5703gui_mch_set_sp_color(guicolor_T color)
5704{
5705 gui.currSpColor = color;
5706}
5707
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005708#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709/*
5710 * Multi-byte handling, originally by Sung-Hoon Baek.
5711 * First static functions (no prototypes generated).
5712 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005713# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005714# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005715# endif
5716# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 * handle WM_IME_NOTIFY message
5720 */
5721 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005722_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 LRESULT lResult = 0;
5725 HIMC hImc;
5726
5727 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5728 return lResult;
5729 switch (dwCommand)
5730 {
5731 case IMN_SETOPENSTATUS:
5732 if (pImmGetOpenStatus(hImc))
5733 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005734 LOGFONTW lf = norm_logfont;
5735 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5736 // Work around when PerMonitorV2 is not enabled in the process level.
5737 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5738 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 im_set_position(gui.row, gui.col);
5740
Bram Moolenaar734a8672019-12-02 22:49:38 +01005741 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 State &= ~LANGMAP;
5743 if (State & INSERT)
5744 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005745# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005746 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5748 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005749 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 int old_row = gui.row;
5751 int old_col = gui.col;
5752
5753 // This must be called here before
5754 // status_redraw_curbuf(), otherwise the mode
5755 // message may appear in the wrong position.
5756 showmode();
5757 status_redraw_curbuf();
5758 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005759 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 gui.row = old_row;
5761 gui.col = old_col;
5762 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 }
5766 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005767 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 lResult = 0;
5769 break;
5770 }
5771 pImmReleaseContext(hWnd, hImc);
5772 return lResult;
5773}
5774
5775 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005776_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 char_u *ret;
5779 int len;
5780
Bram Moolenaar734a8672019-12-02 22:49:38 +01005781 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 return 0;
5783
5784 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5785 if (ret != NULL)
5786 {
5787 add_to_input_buf_csi(ret, len);
5788 vim_free(ret);
5789 return 1;
5790 }
5791 return 0;
5792}
5793
5794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 * void GetResultStr()
5796 *
5797 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5798 * get complete composition string
5799 */
5800 static char_u *
5801GetResultStr(HWND hwnd, int GCS, int *lenp)
5802{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005803 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005804 LONG ret;
5805 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 char_u *convbuf = NULL;
5807
5808 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5809 return NULL;
5810
K.Takatab0b2b732022-01-19 12:59:21 +00005811 // Get the length of the composition string.
5812 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5813 if (ret <= 0)
5814 return NULL;
5815
5816 // Allocate the requested buffer plus space for the NUL character.
5817 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (buf == NULL)
5819 return NULL;
5820
K.Takatab0b2b732022-01-19 12:59:21 +00005821 // Reads in the composition string.
5822 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5823 *lenp = ret / sizeof(WCHAR);
5824
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005825 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 pImmReleaseContext(hwnd, hIMC);
5827 vim_free(buf);
5828 return convbuf;
5829}
5830#endif
5831
Bram Moolenaar734a8672019-12-02 22:49:38 +01005832// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005833#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835/*
5836 * set font to IM.
5837 */
5838 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005839im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 HIMC hImc;
5842
5843 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5844 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005845 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 pImmReleaseContext(s_hwnd, hImc);
5847 }
5848}
5849
5850/*
5851 * Notify cursor position to IM.
5852 */
5853 void
5854im_set_position(int row, int col)
5855{
5856 HIMC hImc;
5857
5858 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5859 {
5860 COMPOSITIONFORM cfs;
5861
5862 cfs.dwStyle = CFS_POINT;
5863 cfs.ptCurrentPos.x = FILL_X(col);
5864 cfs.ptCurrentPos.y = FILL_Y(row);
5865 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005866 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5867 {
5868 // Work around when PerMonitorV2 is not enabled in the process level.
5869 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5870 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 pImmSetCompositionWindow(hImc, &cfs);
5873
5874 pImmReleaseContext(s_hwnd, hImc);
5875 }
5876}
5877
5878/*
5879 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5880 */
5881 void
5882im_set_active(int active)
5883{
5884 HIMC hImc;
5885 static HIMC hImcOld = (HIMC)0;
5886
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005887# ifdef VIMDLL
5888 if (!gui.in_use && !gui.starting)
5889 {
5890 mbyte_im_set_active(active);
5891 return;
5892 }
5893# endif
5894
Bram Moolenaar734a8672019-12-02 22:49:38 +01005895 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
5897 if (p_imdisable)
5898 {
5899 if (hImcOld == (HIMC)0)
5900 {
5901 hImcOld = pImmGetContext(s_hwnd);
5902 if (hImcOld)
5903 pImmAssociateContext(s_hwnd, (HIMC)0);
5904 }
5905 active = FALSE;
5906 }
5907 else if (hImcOld != (HIMC)0)
5908 {
5909 pImmAssociateContext(s_hwnd, hImcOld);
5910 hImcOld = (HIMC)0;
5911 }
5912
5913 hImc = pImmGetContext(s_hwnd);
5914 if (hImc)
5915 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005916 /*
5917 * for Korean ime
5918 */
5919 HKL hKL = GetKeyboardLayout(0);
5920
5921 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5922 {
5923 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5924 static BOOL bSaved = FALSE;
5925
5926 if (active)
5927 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005928 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005929 if (bSaved)
5930 pImmSetConversionStatus(hImc, dwConversionSaved,
5931 dwSentenceSaved);
5932 bSaved = FALSE;
5933 }
5934 else
5935 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005936 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005937 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5938 &dwSentenceSaved))
5939 {
5940 bSaved = TRUE;
5941 pImmSetConversionStatus(hImc,
5942 dwConversionSaved & ~(IME_CMODE_NATIVE
5943 | IME_CMODE_FULLSHAPE),
5944 dwSentenceSaved);
5945 }
5946 }
5947 }
5948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 pImmSetOpenStatus(hImc, active);
5950 pImmReleaseContext(s_hwnd, hImc);
5951 }
5952 }
5953}
5954
5955/*
5956 * Get IM status. When IM is on, return not 0. Else return 0.
5957 */
5958 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005959im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961 int status = 0;
5962 HIMC hImc;
5963
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005964# ifdef VIMDLL
5965 if (!gui.in_use && !gui.starting)
5966 return mbyte_im_get_status();
5967# endif
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5970 {
5971 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5972 pImmReleaseContext(s_hwnd, hImc);
5973 }
5974 return status;
5975}
5976
Bram Moolenaar734a8672019-12-02 22:49:38 +01005977#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005980/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005981 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005982 */
5983 static void
5984latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5985{
5986 int c;
5987
Bram Moolenaarca003e12006-03-17 23:19:38 +00005988 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005989 {
5990 c = *text++;
5991 switch (c)
5992 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005993 case 0xa4: c = 0x20ac; break; // euro
5994 case 0xa6: c = 0x0160; break; // S hat
5995 case 0xa8: c = 0x0161; break; // S -hat
5996 case 0xb4: c = 0x017d; break; // Z hat
5997 case 0xb8: c = 0x017e; break; // Z -hat
5998 case 0xbc: c = 0x0152; break; // OE
5999 case 0xbd: c = 0x0153; break; // oe
6000 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006001 }
6002 *unicodebuf++ = c;
6003 }
6004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006#ifdef FEAT_RIGHTLEFT
6007/*
6008 * What is this for? In the case where you are using Win98 or Win2K or later,
6009 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6010 * reverses the string sent to the TextOut... family. This sucks, because we
6011 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6012 * way to tell Windblows not to do this!
6013 *
6014 * The short of it is that this 'RevOut' only gets called if you are running
6015 * one of the new, "improved" MS OSes, and only if you are running in
6016 * 'rightleft' mode. It makes display take *slightly* longer, but not
6017 * noticeably so.
6018 */
6019 static void
6020RevOut( HDC s_hdc,
6021 int col,
6022 int row,
6023 UINT foptions,
6024 CONST RECT *pcliprect,
6025 LPCTSTR text,
6026 UINT len,
6027 CONST INT *padding)
6028{
6029 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006031 for (ix = 0; ix < (int)len; ++ix)
6032 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6033 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034}
6035#endif
6036
Bram Moolenaar92467d32017-12-05 13:22:16 +01006037 static void
6038draw_line(
6039 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006040 int y1,
6041 int x2,
6042 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006043 COLORREF color)
6044{
6045#if defined(FEAT_DIRECTX)
6046 if (IS_ENABLE_DIRECTX())
6047 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6048 else
6049#endif
6050 {
6051 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6052 HPEN old_pen = SelectObject(s_hdc, hpen);
6053 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006054 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006055 LineTo(s_hdc, x2, y2);
6056 DeleteObject(SelectObject(s_hdc, old_pen));
6057 }
6058}
6059
6060 static void
6061set_pixel(
6062 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006063 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006064 COLORREF color)
6065{
6066#if defined(FEAT_DIRECTX)
6067 if (IS_ENABLE_DIRECTX())
6068 DWriteContext_SetPixel(s_dwc, x, y, color);
6069 else
6070#endif
6071 SetPixel(s_hdc, x, y, color);
6072}
6073
6074 static void
6075fill_rect(
6076 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006077 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006078 COLORREF color)
6079{
6080#if defined(FEAT_DIRECTX)
6081 if (IS_ENABLE_DIRECTX())
6082 DWriteContext_FillRect(s_dwc, rcp, color);
6083 else
6084#endif
6085 {
6086 HBRUSH hbr2;
6087
6088 if (hbr == NULL)
6089 hbr2 = CreateSolidBrush(color);
6090 else
6091 hbr2 = hbr;
6092 FillRect(s_hdc, rcp, hbr2);
6093 if (hbr == NULL)
6094 DeleteBrush(hbr2);
6095 }
6096}
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 void
6099gui_mch_draw_string(
6100 int row,
6101 int col,
6102 char_u *text,
6103 int len,
6104 int flags)
6105{
6106 static int *padding = NULL;
6107 static int pad_size = 0;
6108 int i;
6109 const RECT *pcliprect = NULL;
6110 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 static WCHAR *unicodebuf = NULL;
6112 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006113 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int y;
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 /*
6118 * Italic and bold text seems to have an extra row of pixels at the bottom
6119 * (below where the bottom of the character should be). If we draw the
6120 * characters with a solid background, the top row of pixels in the
6121 * character below will be overwritten. We can fix this by filling in the
6122 * background ourselves, to the correct character proportions, and then
6123 * writing the character in transparent mode. Still have a problem when
6124 * the character is "_", which gets written on to the character below.
6125 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6126 * pixel in their slots, which fixes the problem with the bottom row of
6127 * pixels. We still need this code because otherwise the top row of pixels
6128 * becomes a problem. - webb.
6129 */
6130 static HBRUSH hbr_cache[2] = {NULL, NULL};
6131 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6132 static int brush_lru = 0;
6133 HBRUSH hbr;
6134 RECT rc;
6135
6136 if (!(flags & DRAW_TRANSP))
6137 {
6138 /*
6139 * Clear background first.
6140 * Note: FillRect() excludes right and bottom of rectangle.
6141 */
6142 rc.left = FILL_X(col);
6143 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (has_mbyte)
6145 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006146 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006147 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 rc.right = FILL_X(col + len);
6151 rc.bottom = FILL_Y(row + 1);
6152
Bram Moolenaar734a8672019-12-02 22:49:38 +01006153 // Cache the created brush, that saves a lot of time. We need two:
6154 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (gui.currBgColor == brush_color[0])
6156 {
6157 hbr = hbr_cache[0];
6158 brush_lru = 1;
6159 }
6160 else if (gui.currBgColor == brush_color[1])
6161 {
6162 hbr = hbr_cache[1];
6163 brush_lru = 0;
6164 }
6165 else
6166 {
6167 if (hbr_cache[brush_lru] != NULL)
6168 DeleteBrush(hbr_cache[brush_lru]);
6169 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6170 brush_color[brush_lru] = gui.currBgColor;
6171 hbr = hbr_cache[brush_lru];
6172 brush_lru = !brush_lru;
6173 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006174
Bram Moolenaar92467d32017-12-05 13:22:16 +01006175 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177 SetBkMode(s_hdc, TRANSPARENT);
6178
6179 /*
6180 * When drawing block cursor, prevent inverted character spilling
6181 * over character cell (can happen with bold/italic)
6182 */
6183 if (flags & DRAW_CURSOR)
6184 {
6185 pcliprect = &rc;
6186 foptions = ETO_CLIPPED;
6187 }
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 SetTextColor(s_hdc, gui.currFgColor);
6190 SelectFont(s_hdc, gui.currFont);
6191
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006192#ifdef FEAT_DIRECTX
6193 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006194 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006195#endif
6196
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6198 {
6199 vim_free(padding);
6200 pad_size = Columns;
6201
Bram Moolenaar734a8672019-12-02 22:49:38 +01006202 // Don't give an out-of-memory message here, it would call us
6203 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006204 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 if (padding != NULL)
6206 for (i = 0; i < pad_size; i++)
6207 padding[i] = gui.char_width;
6208 }
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /*
6211 * We have to provide the padding argument because italic and bold versions
6212 * of fixed-width fonts are often one pixel or so wider than their normal
6213 * versions.
6214 * No check for DRAW_BOLD, Windows will have done it already.
6215 */
6216
Bram Moolenaar734a8672019-12-02 22:49:38 +01006217 // Check if there are any UTF-8 characters. If not, use normal text
6218 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (enc_utf8)
6220 for (n = 0; n < len; ++n)
6221 if (text[n] >= 0x80)
6222 break;
6223
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006224#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006225 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6226 // required that unicode drawing routine, currently. So this forces it
6227 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006228 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006229 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006230#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006231
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // Check if the Unicode buffer exists and is big enough. Create it
6233 // with the same length as the multi-byte string, the number of wide
6234 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006235 if ((enc_utf8
6236 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6237 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 && (unicodebuf == NULL || len > unibuflen))
6239 {
6240 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006241 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006244 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 unibuflen = len;
6247 }
6248
6249 if (enc_utf8 && n < len && unicodebuf != NULL)
6250 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006251 // Output UTF-8 characters. Composing characters should be
6252 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006253 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006254 int wlen; // string length in words
6255 int clen; // string length in characters
6256 int cells; // cell width of string up to composing char
6257 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006258 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006260 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006261 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006263 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006265 c = utf_ptr2char(text + i);
6266 if (c >= 0x10000)
6267 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006268 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006269 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6270 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006271 }
6272 else
6273 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006274 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006275 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006276
6277 if (utf_iscomposing(c))
6278 cw = 0;
6279 else
6280 {
6281 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006282 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006283 cw = 1;
6284 }
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (unicodepdy != NULL)
6287 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006288 // Use unicodepdy to make characters fit as we expect, even
6289 // when the font uses different widths (e.g., bold character
6290 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006291 if (c >= 0x10000)
6292 {
6293 unicodepdy[wlen - 2] = cw * gui.char_width;
6294 unicodepdy[wlen - 1] = 0;
6295 }
6296 else
6297 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 }
6299 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006300 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006301 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006303#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006304 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006305 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006306 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006307 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006308 TEXT_X(col), TEXT_Y(row),
6309 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006310 gui.char_width, gui.currFgColor,
6311 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006312 }
6313 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006314#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006315 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6316 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006317 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006319 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006321 // If we want to display codepage data, and the current CP is not the
6322 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (unicodebuf != NULL)
6324 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006325 if (enc_latin9)
6326 latin9_to_ucs(text, len, unicodebuf);
6327 else
6328 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 MB_PRECOMPOSED,
6330 (char *)text, len,
6331 (LPWSTR)unicodebuf, unibuflen);
6332 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006333 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006334 // Use unicodepdy to make characters fit as we expect, even
6335 // when the font uses different widths (e.g., bold character
6336 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006337 if (unicodepdy != NULL)
6338 {
6339 int i;
6340 int cw;
6341
6342 for (i = 0; i < len; ++i)
6343 {
6344 cw = utf_char2cells(unicodebuf[i]);
6345 if (cw > 2)
6346 cw = 1;
6347 unicodepdy[i] = cw * gui.char_width;
6348 }
6349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006351 foptions, pcliprect, unicodebuf, len, unicodepdy);
6352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354 }
6355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 {
6357#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006358 // Windows will mess up RL text, so we have to draw it character by
6359 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006360 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6362 foptions, pcliprect, (char *)text, len, padding);
6363 else
6364#endif
6365 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6366 foptions, pcliprect, (char *)text, len, padding);
6367 }
6368
Bram Moolenaar734a8672019-12-02 22:49:38 +01006369 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (flags & DRAW_UNDERL)
6371 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006372 // When p_linespace is 0, overwrite the bottom row of pixels.
6373 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 if (p_linespace > 1)
6376 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006377 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006379
Bram Moolenaar734a8672019-12-02 22:49:38 +01006380 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006381 if (flags & DRAW_STRIKE)
6382 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006383 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006384 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006385 }
6386
Bram Moolenaar734a8672019-12-02 22:49:38 +01006387 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006388 if (flags & DRAW_UNDERC)
6389 {
6390 int x;
6391 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006392 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006393
6394 y = FILL_Y(row + 1) - 1;
6395 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6396 {
6397 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006398 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006399 }
6400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401}
6402
6403
6404/*
6405 * Output routines.
6406 */
6407
Bram Moolenaar734a8672019-12-02 22:49:38 +01006408/*
6409 * Flush any output to the screen
6410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 void
6412gui_mch_flush(void)
6413{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006414#if defined(FEAT_DIRECTX)
6415 if (IS_ENABLE_DIRECTX())
6416 DWriteContext_Flush(s_dwc);
6417#endif
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 GdiFlush();
6420}
6421
6422 static void
6423clear_rect(RECT *rcp)
6424{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006425 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426}
6427
6428
Bram Moolenaarc716c302006-01-21 22:12:51 +00006429 void
6430gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6431{
6432 RECT workarea_rect;
6433
6434 get_work_area(&workarea_rect);
6435
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006436 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006437 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6438 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006439
Bram Moolenaar734a8672019-12-02 22:49:38 +01006440 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6441 // the menubar for MSwin, we subtract it from the screen height, so that
6442 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006443 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006444 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6445 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6446 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6447 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006448}
6449
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451#if defined(FEAT_MENU) || defined(PROTO)
6452/*
6453 * Add a sub menu to the menu bar.
6454 */
6455 void
6456gui_mch_add_menu(
6457 vimmenu_T *menu,
6458 int pos)
6459{
6460 vimmenu_T *parent = menu->parent;
6461
6462 menu->submenu_id = CreatePopupMenu();
6463 menu->id = s_menu_id++;
6464
6465 if (menu_is_menubar(menu->name))
6466 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006467 WCHAR *wn;
6468 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006470 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006471 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006472 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006474 infow.cbSize = sizeof(infow);
6475 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6476 | MIIM_SUBMENU;
6477 infow.dwItemData = (long_u)menu;
6478 infow.wID = menu->id;
6479 infow.fType = MFT_STRING;
6480 infow.dwTypeData = wn;
6481 infow.cch = (UINT)wcslen(wn);
6482 infow.hSubMenu = menu->submenu_id;
6483 InsertMenuItemW((parent == NULL)
6484 ? s_menuBar : parent->submenu_id,
6485 (UINT)pos, TRUE, &infow);
6486 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488
Bram Moolenaar734a8672019-12-02 22:49:38 +01006489 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (parent == NULL)
6491 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006492# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 else if (IsWindow(parent->tearoff_handle))
6494 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006495# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496}
6497
6498 void
6499gui_mch_show_popupmenu(vimmenu_T *menu)
6500{
6501 POINT mp;
6502
K.Takata45f9cfb2022-01-21 11:11:00 +00006503 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6505}
6506
6507 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006508gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 vimmenu_T *menu = gui_find_menu(path_name);
6511
6512 if (menu != NULL)
6513 {
6514 POINT p;
6515
Bram Moolenaar734a8672019-12-02 22:49:38 +01006516 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006518 if (mouse_pos)
6519 {
6520 int mx, my;
6521
6522 gui_mch_getmouse(&mx, &my);
6523 p.x += mx;
6524 p.y += my;
6525 }
6526 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006528 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6530 }
6531 msg_scroll = FALSE;
6532 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6533 }
6534}
6535
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006536# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537/*
6538 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6539 * create it as a pseudo-"tearoff menu".
6540 */
6541 void
6542gui_make_tearoff(char_u *path_name)
6543{
6544 vimmenu_T *menu = gui_find_menu(path_name);
6545
Bram Moolenaar734a8672019-12-02 22:49:38 +01006546 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (menu != NULL)
6548 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6549}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
6552/*
6553 * Add a menu item to a menu
6554 */
6555 void
6556gui_mch_add_menu_item(
6557 vimmenu_T *menu,
6558 int idx)
6559{
6560 vimmenu_T *parent = menu->parent;
6561
6562 menu->id = s_menu_id++;
6563 menu->submenu_id = NULL;
6564
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006565# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6567 {
6568 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6569 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6570 }
6571 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006572# endif
6573# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (menu_is_toolbar(parent->name))
6575 {
6576 TBBUTTON newtb;
6577
Bram Moolenaara80faa82020-04-12 19:37:17 +02006578 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (menu_is_separator(menu->name))
6580 {
6581 newtb.iBitmap = 0;
6582 newtb.fsStyle = TBSTYLE_SEP;
6583 }
6584 else
6585 {
6586 newtb.iBitmap = get_toolbar_bitmap(menu);
6587 newtb.fsStyle = TBSTYLE_BUTTON;
6588 }
6589 newtb.idCommand = menu->id;
6590 newtb.fsState = TBSTATE_ENABLED;
6591 newtb.iString = 0;
6592 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6593 (LPARAM)&newtb);
6594 menu->submenu_id = (HMENU)-1;
6595 }
6596 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006599 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006601 wn = enc_to_utf16(menu->name, NULL);
6602 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006604 InsertMenuW(parent->submenu_id, (UINT)idx,
6605 (menu_is_separator(menu->name)
6606 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6607 (UINT)menu->id, wn);
6608 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006610# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 if (IsWindow(parent->tearoff_handle))
6612 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615}
6616
6617/*
6618 * Destroy the machine specific menu widget.
6619 */
6620 void
6621gui_mch_destroy_menu(vimmenu_T *menu)
6622{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006623# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 /*
6625 * is this a toolbar button?
6626 */
6627 if (menu->submenu_id == (HMENU)-1)
6628 {
6629 int iButton;
6630
6631 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6632 (WPARAM)menu->id, 0);
6633 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6634 }
6635 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 {
6638 if (menu->parent != NULL
6639 && menu_is_popup(menu->parent->dname)
6640 && menu->parent->submenu_id != NULL)
6641 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6642 else
6643 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6644 if (menu->submenu_id != NULL)
6645 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006646# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (IsWindow(menu->tearoff_handle))
6648 DestroyWindow(menu->tearoff_handle);
6649 if (menu->parent != NULL
6650 && menu->parent->children != NULL
6651 && IsWindow(menu->parent->tearoff_handle))
6652 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006653 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 menu->modes = 0;
6655 rebuild_tearoff(menu->parent);
6656 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659}
6660
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006661# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 static void
6663rebuild_tearoff(vimmenu_T *menu)
6664{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006665 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 char_u tbuf[128];
6667 RECT trect;
6668 RECT rct;
6669 RECT roct;
6670 int x, y;
6671
6672 HWND thwnd = menu->tearoff_handle;
6673
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006674 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (GetWindowRect(thwnd, &trect)
6676 && GetWindowRect(s_hwnd, &rct)
6677 && GetClientRect(s_hwnd, &roct))
6678 {
6679 x = trect.left - rct.left;
6680 y = (trect.top - rct.bottom + roct.bottom);
6681 }
6682 else
6683 {
6684 x = y = 0xffffL;
6685 }
6686 DestroyWindow(thwnd);
6687 if (menu->children != NULL)
6688 {
6689 gui_mch_tearoff(tbuf, menu, x, y);
6690 if (IsWindow(menu->tearoff_handle))
6691 (void) SetWindowPos(menu->tearoff_handle,
6692 NULL,
6693 (int)trect.left,
6694 (int)trect.top,
6695 0, 0,
6696 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6697 }
6698}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006699# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701/*
6702 * Make a menu either grey or not grey.
6703 */
6704 void
6705gui_mch_menu_grey(
6706 vimmenu_T *menu,
6707 int grey)
6708{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006709# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 /*
6711 * is this a toolbar button?
6712 */
6713 if (menu->submenu_id == (HMENU)-1)
6714 {
6715 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006716 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 }
6718 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006719# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006720 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6721 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006723# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6725 {
6726 WORD menuID;
6727 HWND menuHandle;
6728
6729 /*
6730 * A tearoff button has changed state.
6731 */
6732 if (menu->children == NULL)
6733 menuID = (WORD)(menu->id);
6734 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006735 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6737 if (menuHandle)
6738 EnableWindow(menuHandle, !grey);
6739
6740 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742}
6743
Bram Moolenaar734a8672019-12-02 22:49:38 +01006744#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746
Bram Moolenaar734a8672019-12-02 22:49:38 +01006747// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006750#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6753/*
6754 * stuff for dialogs
6755 */
6756
6757/*
6758 * The callback routine used by all the dialogs. Very simple. First,
6759 * acknowledges the INITDIALOG message so that Windows knows to do standard
6760 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6761 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6762 * number.
6763 */
6764 static LRESULT CALLBACK
6765dialog_callback(
6766 HWND hwnd,
6767 UINT message,
6768 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006769 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771 if (message == WM_INITDIALOG)
6772 {
6773 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006774 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 (void)SetFocus(hwnd);
6776 if (dialog_default_button > IDCANCEL)
6777 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006778 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006779 // We don't have a default, set focus on another element of the
6780 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006781 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 return FALSE;
6783 }
6784
6785 if (message == WM_COMMAND)
6786 {
6787 int button = LOWORD(wParam);
6788
Bram Moolenaar734a8672019-12-02 22:49:38 +01006789 // Don't end the dialog if something was selected that was
6790 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (button >= DLG_NONBUTTON_CONTROL)
6792 return TRUE;
6793
Bram Moolenaar734a8672019-12-02 22:49:38 +01006794 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006796 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006797 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006798 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006799
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006800 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6801 p = utf16_to_enc(wp, NULL);
6802 vim_strncpy(s_textfield, p, IOSIZE);
6803 vim_free(p);
6804 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 /*
6808 * Need to check for IDOK because if the user just hits Return to
6809 * accept the default value, some reason this is what we get.
6810 */
6811 if (button == IDOK)
6812 {
6813 if (dialog_default_button > IDCANCEL)
6814 EndDialog(hwnd, dialog_default_button);
6815 }
6816 else
6817 EndDialog(hwnd, button - IDCANCEL);
6818 return TRUE;
6819 }
6820
6821 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6822 {
6823 EndDialog(hwnd, 0);
6824 return TRUE;
6825 }
6826 return FALSE;
6827}
6828
6829/*
6830 * Create a dialog dynamically from the parameter strings.
6831 * type = type of dialog (question, alert, etc.)
6832 * title = dialog title. may be NULL for default title.
6833 * message = text to display. Dialog sizes to accommodate it.
6834 * buttons = '\n' separated list of button captions, default first.
6835 * dfltbutton = number of default button.
6836 *
6837 * This routine returns 1 if the first button is pressed,
6838 * 2 for the second, etc.
6839 *
6840 * 0 indicates Esc was pressed.
6841 * -1 for unexpected error
6842 *
6843 * If stubbing out this fn, return 1.
6844 */
6845
Bram Moolenaar734a8672019-12-02 22:49:38 +01006846static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 "IDR_VIM",
6849 "IDR_VIM_ERROR",
6850 "IDR_VIM_ALERT",
6851 "IDR_VIM_INFO",
6852 "IDR_VIM_QUESTION"
6853};
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 int
6856gui_mch_dialog(
6857 int type,
6858 char_u *title,
6859 char_u *message,
6860 char_u *buttons,
6861 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006862 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006863 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
6865 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006866 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 int numButtons;
6868 int *buttonWidths, *buttonPositions;
6869 int buttonYpos;
6870 int nchar, i;
6871 DWORD lStyle;
6872 int dlgwidth = 0;
6873 int dlgheight;
6874 int editboxheight;
6875 int horizWidth = 0;
6876 int msgheight;
6877 char_u *pstart;
6878 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006879 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 char_u *tbuffer;
6881 RECT rect;
6882 HWND hwnd;
6883 HDC hdc;
6884 HFONT font, oldFont;
6885 TEXTMETRIC fontInfo;
6886 int fontHeight;
6887 int textWidth, minButtonWidth, messageWidth;
6888 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006889 int maxDialogHeight;
6890 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 int vertical;
6892 int dlgPaddingX;
6893 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006894# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006895 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006897# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006898 garray_T ga;
6899 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006900 int dlg_icon_width;
6901 int dlg_icon_height;
6902 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006904# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006905 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006906# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006907 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006908# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006909 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006910 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
Bram Moolenaar748bf032005-02-02 23:04:36 +00006913 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006914 {
6915 load_dpi_func();
6916 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006917 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006918 }
6919 else
6920 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 if ((type < 0) || (type > VIM_LAST_TYPE))
6923 type = 0;
6924
Bram Moolenaar734a8672019-12-02 22:49:38 +01006925 // allocate some memory for dialog template
6926 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006928 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 if (p == NULL)
6931 return -1;
6932
6933 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006934 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6936 * const.
6937 */
6938 tbuffer = vim_strsave(buttons);
6939 if (tbuffer == NULL)
6940 return -1;
6941
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 numButtons = 1;
6946 for (i = 0; tbuffer[i] != '\0'; i++)
6947 {
6948 if (tbuffer[i] == DLG_BUTTON_SEP)
6949 numButtons++;
6950 }
6951 if (dfltbutton >= numButtons)
6952 dfltbutton = -1;
6953
Bram Moolenaar734a8672019-12-02 22:49:38 +01006954 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006955 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (buttonWidths == NULL)
6957 return -1;
6958
Bram Moolenaar734a8672019-12-02 22:49:38 +01006959 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006960 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (buttonPositions == NULL)
6962 return -1;
6963
6964 /*
6965 * Calculate how big the dialog must be.
6966 */
6967 hwnd = GetDesktopWindow();
6968 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006969# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6971 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006972 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 use_lfSysmenu = TRUE;
6974 }
6975 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00006978 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (s_usenewlook)
6980 {
6981 oldFont = SelectFont(hdc, font);
6982 dlgPaddingX = DLG_PADDING_X;
6983 dlgPaddingY = DLG_PADDING_Y;
6984 }
6985 else
6986 {
6987 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6988 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6989 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6990 }
6991 GetTextMetrics(hdc, &fontInfo);
6992 fontHeight = fontInfo.tmHeight;
6993
Bram Moolenaar734a8672019-12-02 22:49:38 +01006994 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006995 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998 if (s_hwnd == NULL)
6999 {
7000 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
Bram Moolenaar734a8672019-12-02 22:49:38 +01007002 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007003 get_work_area(&workarea_rect);
7004 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007005 if (maxDialogWidth > adjust_by_system_dpi(600))
7006 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007007 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007008 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007009 }
7010 else
7011 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007012 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007013 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007014 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007015 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7016 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7017 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7018 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007019
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007020 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007021 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7022 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7023 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7024 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7025 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007026 }
7027
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 // Set dlgwidth to width of message.
7029 // Copy the message into "ga", changing NL to CR-NL and inserting line
7030 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 pstart = message;
7032 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007033 msgheight = 0;
7034 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 do
7036 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007037 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007038
Bram Moolenaar734a8672019-12-02 22:49:38 +01007039 // Need to figure out where to break the string. The system does it
7040 // at a word boundary, which would mean we can't compute the number of
7041 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007042 textWidth = 0;
7043 last_white = NULL;
7044 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007045 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007046 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007047 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007048 && textWidth > maxDialogWidth * 3 / 4)
7049 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007050 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007051 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007052 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007053 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007054 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007055 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007056 textWidth = 0;
7057
7058 if (last_white != NULL)
7059 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007060 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007061 if (pend > last_white)
7062 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007063 pend = last_white + 1;
7064 last_white = NULL;
7065 }
7066 ga_append(&ga, '\r');
7067 ga_append(&ga, '\n');
7068 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007069 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007070
7071 while (--l >= 0)
7072 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007073 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007074 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007076
7077 ga_append(&ga, '\r');
7078 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 pstart = pend + 1;
7080 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007081
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007082 if (ga.ga_data != NULL)
7083 message = ga.ga_data;
7084
Bram Moolenaar734a8672019-12-02 22:49:38 +01007085 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007086
K.Takatac81e9bf2022-01-16 14:15:49 +00007087 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7088 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
K.Takatac81e9bf2022-01-16 14:15:49 +00007090 // Add width of icon to dlgwidth, and some space
7091 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7092 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7093
7094 if (msgheight < dlg_icon_height)
7095 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 /*
7098 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007099 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007101 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (!vertical)
7103 {
7104 // Place buttons horizontally if they fit.
7105 horizWidth = dlgPaddingX;
7106 pstart = tbuffer;
7107 i = 0;
7108 do
7109 {
7110 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7111 if (pend == NULL)
7112 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007113 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (textWidth < minButtonWidth)
7115 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 buttonWidths[i] = textWidth;
7118 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 pstart = pend + 1;
7121 } while (*pend != NUL);
7122
7123 if (horizWidth > maxDialogWidth)
7124 vertical = TRUE; // Too wide to fit on the screen.
7125 else if (horizWidth > dlgwidth)
7126 dlgwidth = horizWidth;
7127 }
7128
7129 if (vertical)
7130 {
7131 // Stack buttons vertically.
7132 pstart = tbuffer;
7133 do
7134 {
7135 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7136 if (pend == NULL)
7137 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007138 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007139 textWidth += dlgPaddingX; // Padding within button
7140 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (textWidth > dlgwidth)
7142 dlgwidth = textWidth;
7143 pstart = pend + 1;
7144 } while (*pend != NUL);
7145 }
7146
7147 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007148 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaar734a8672019-12-02 22:49:38 +01007150 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (s_usenewlook)
7152 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7153 else
7154 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7155
7156 add_long(lStyle);
7157 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007158 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 add_word(0); // NumberOfItems(will change later)
7160 add_word(10); // x
7161 add_word(10); // y
7162 add_word(PixelToDialogX(dlgwidth)); // cx
7163
7164 // Dialog height.
7165 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007166 dlgheight = msgheight + 2 * dlgPaddingY
7167 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 else
7169 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7170
7171 // Dialog needs to be taller if contains an edit box.
7172 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7173 if (textfield != NULL)
7174 dlgheight += editboxheight;
7175
Bram Moolenaar734a8672019-12-02 22:49:38 +01007176 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007177 if (dlgheight > maxDialogHeight)
7178 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007179 msgheight = msgheight - (dlgheight - maxDialogHeight);
7180 dlgheight = maxDialogHeight;
7181 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007182 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007183 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007184 }
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 add_word(PixelToDialogY(dlgheight));
7187
7188 add_word(0); // Menu
7189 add_word(0); // Class
7190
Bram Moolenaar734a8672019-12-02 22:49:38 +01007191 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007192 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7193 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 p += nchar;
7195
7196 if (s_usenewlook)
7197 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007199# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (use_lfSysmenu)
7201 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007202 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7204 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007205 wcscpy(p, lfSysmenu.lfFaceName);
7206 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
7211 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007212 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214 p += nchar;
7215 }
7216
7217 buttonYpos = msgheight + 2 * dlgPaddingY;
7218
7219 if (textfield != NULL)
7220 buttonYpos += editboxheight;
7221
7222 pstart = tbuffer;
7223 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007224 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 for (i = 0; i < numButtons; i++)
7226 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007227 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 for ( pend = pstart;
7229 *pend && (*pend != DLG_BUTTON_SEP);
7230 pend++)
7231 ;
7232
7233 if (*pend)
7234 *pend = '\0';
7235
7236 /*
7237 * old NOTE:
7238 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7239 * the focus to the first tab-able button and in so doing makes that
7240 * the default!! Grrr. Workaround: Make the default button the only
7241 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7242 * he/she can use arrow keys.
7243 *
7244 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007245 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 * dialog. Also needed for when the textfield is the default control.
7247 * It appears to work now (perhaps not on Win95?).
7248 */
7249 if (vertical)
7250 {
7251 p = add_dialog_element(p,
7252 (i == dfltbutton
7253 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7254 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007255 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 + 2 * fontHeight * i),
7257 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7258 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007259 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 else
7262 {
7263 p = add_dialog_element(p,
7264 (i == dfltbutton
7265 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7266 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007267 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 PixelToDialogX(buttonWidths[i]),
7269 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007270 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007272 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 *pnumitems += numButtons;
7275
Bram Moolenaar734a8672019-12-02 22:49:38 +01007276 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 p = add_dialog_element(p, SS_ICON,
7278 PixelToDialogX(dlgPaddingX),
7279 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007280 PixelToDialogX(dlg_icon_width),
7281 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7283 dlg_icons[type]);
7284
Bram Moolenaar734a8672019-12-02 22:49:38 +01007285 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007286 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007287 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007288 PixelToDialogY(dlgPaddingY),
7289 (WORD)(PixelToDialogX(messageWidth) + 1),
7290 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007291 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Bram Moolenaar734a8672019-12-02 22:49:38 +01007293 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (textfield != NULL)
7295 {
7296 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7297 PixelToDialogX(2 * dlgPaddingX),
7298 PixelToDialogY(2 * dlgPaddingY + msgheight),
7299 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7300 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007301 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 *pnumitems += 1;
7303 }
7304
7305 *pnumitems += 2;
7306
7307 SelectFont(hdc, oldFont);
7308 DeleteObject(font);
7309 ReleaseDC(hwnd, hdc);
7310
Bram Moolenaar734a8672019-12-02 22:49:38 +01007311 // Let the dialog_callback() function know which button to make default
7312 // If we have an edit box, make that the default. We also need to tell
7313 // dialog_callback() if this dialog contains an edit box or not. We do
7314 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 if (textfield != NULL)
7316 {
7317 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7318 s_textfield = textfield;
7319 }
7320 else
7321 {
7322 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7323 s_textfield = NULL;
7324 }
7325
Bram Moolenaar734a8672019-12-02 22:49:38 +01007326 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007328 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 (LPDLGTEMPLATE)pdlgtemplate,
7330 s_hwnd,
7331 (DLGPROC)dialog_callback);
7332
7333 LocalFree(LocalHandle(pdlgtemplate));
7334 vim_free(tbuffer);
7335 vim_free(buttonWidths);
7336 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007337 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
Bram Moolenaar734a8672019-12-02 22:49:38 +01007339 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 (void)SetFocus(s_hwnd);
7341
7342 return nchar;
7343}
7344
Bram Moolenaar734a8672019-12-02 22:49:38 +01007345#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007346
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347/*
7348 * Put a simple element (basic class) onto a dialog template in memory.
7349 * return a pointer to where the next item should be added.
7350 *
7351 * parameters:
7352 * lStyle = additional style flags
7353 * (be careful, NT3.51 & Win32s will ignore the new ones)
7354 * x,y = x & y positions IN DIALOG UNITS
7355 * w,h = width and height IN DIALOG UNITS
7356 * Id = ID used in messages
7357 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7358 * caption = usually text or resource name
7359 *
7360 * TODO: use the length information noted here to enable the dialog creation
7361 * routines to work out more exactly how much memory they need to alloc.
7362 */
7363 static PWORD
7364add_dialog_element(
7365 PWORD p,
7366 DWORD lStyle,
7367 WORD x,
7368 WORD y,
7369 WORD w,
7370 WORD h,
7371 WORD Id,
7372 WORD clss,
7373 const char *caption)
7374{
7375 int nchar;
7376
Bram Moolenaar734a8672019-12-02 22:49:38 +01007377 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7379 *p++ = LOWORD(lStyle);
7380 *p++ = HIWORD(lStyle);
7381 *p++ = 0; // LOWORD (lExtendedStyle)
7382 *p++ = 0; // HIWORD (lExtendedStyle)
7383 *p++ = x;
7384 *p++ = y;
7385 *p++ = w;
7386 *p++ = h;
7387 *p++ = Id; //9 or 10 words in all
7388
7389 *p++ = (WORD)0xffff;
7390 *p++ = clss; //2 more here
7391
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007392 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 p += nchar;
7394
7395 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7396
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007397 return p; // total = 15 + strlen(caption) words
7398 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401
7402/*
7403 * Helper routine. Take an input pointer, return closest pointer that is
7404 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7405 */
7406 static LPWORD
7407lpwAlign(
7408 LPWORD lpIn)
7409{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007410 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007412 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 ul += 3;
7414 ul >>= 2;
7415 ul <<= 2;
7416 return (LPWORD)ul;
7417}
7418
7419/*
7420 * Helper routine. Takes second parameter as Ansi string, copies it to first
7421 * parameter as wide character (16-bits / char) string, and returns integer
7422 * number of wide characters (words) in string (including the trailing wide
7423 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007424 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7425 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 static int
7427nCopyAnsiToWideChar(
7428 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007429 LPSTR lpAnsiIn,
7430 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
7432 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007433 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 int i;
7435 WCHAR *wn;
7436
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007437 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007439 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007440 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (wn != NULL)
7442 {
7443 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007444 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 vim_free(wn);
7446 }
7447 }
7448 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007449 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 nChar = MultiByteToWideChar(
7451 enc_codepage > 0 ? enc_codepage : CP_ACP,
7452 MB_PRECOMPOSED,
7453 lpAnsiIn, len,
7454 lpWCStr, len);
7455 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007456 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 return nChar;
7460}
7461
7462
7463#ifdef FEAT_TEAROFF
7464/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007465 * Lookup menu handle from "menu_id".
7466 */
7467 static HMENU
7468tearoff_lookup_menuhandle(
7469 vimmenu_T *menu,
7470 WORD menu_id)
7471{
7472 for ( ; menu != NULL; menu = menu->next)
7473 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007474 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007475 continue;
7476 if (menu_is_separator(menu->dname))
7477 continue;
7478 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7479 return menu->submenu_id;
7480 }
7481 return NULL;
7482}
7483
7484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 * The callback function for all the modeless dialogs that make up the
7486 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7487 * thinking its menus have been clicked), and go away when closed.
7488 */
7489 static LRESULT CALLBACK
7490tearoff_callback(
7491 HWND hwnd,
7492 UINT message,
7493 WPARAM wParam,
7494 LPARAM lParam)
7495{
7496 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007497 {
7498 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaar734a8672019-12-02 22:49:38 +01007502 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 HandleMouseHide(message, lParam);
7504
7505 if (message == WM_COMMAND)
7506 {
7507 if ((WORD)(LOWORD(wParam)) & 0x8000)
7508 {
7509 POINT mp;
7510 RECT rect;
7511
7512 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7513 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007514 vimmenu_T *menu;
7515
7516 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007518 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7520 (int)rect.right - 8,
7521 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007522 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 s_hwnd,
7524 NULL);
7525 /*
7526 * NOTE: The pop-up menu can eat the mouse up event.
7527 * We deal with this in normal.c.
7528 */
7529 }
7530 }
7531 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007532 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7534 /*
7535 * Give main window the focus back: this is so after
7536 * choosing a tearoff button you can start typing again
7537 * straight away.
7538 */
7539 (void)SetFocus(s_hwnd);
7540 return TRUE;
7541 }
7542 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7543 {
7544 DestroyWindow(hwnd);
7545 return TRUE;
7546 }
7547
Bram Moolenaar734a8672019-12-02 22:49:38 +01007548 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (message == WM_EXITSIZEMOVE)
7550 (void)SetActiveWindow(s_hwnd);
7551
7552 return FALSE;
7553}
7554#endif
7555
7556
7557/*
7558 * Decide whether to use the "new look" (small, non-bold font) or the "old
7559 * look" (big, clanky font) for dialogs, and work out a few values for use
7560 * later accordingly.
7561 */
7562 static void
7563get_dialog_font_metrics(void)
7564{
7565 HDC hdc;
7566 HFONT hfontTools = 0;
7567 DWORD dlgFontSize;
7568 SIZE size;
7569#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007570 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571#endif
7572
7573 s_usenewlook = FALSE;
7574
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007576 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007577 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007578 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579#endif
7580 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007581 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007583 if (hfontTools)
7584 {
7585 hdc = GetDC(s_hwnd);
7586 SelectObject(hdc, hfontTools);
7587 /*
7588 * GetTextMetrics() doesn't return the right value in
7589 * tmAveCharWidth, so we have to figure out the dialog base units
7590 * ourselves.
7591 */
7592 GetTextExtentPoint(hdc,
7593 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7594 52, &size);
7595 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007597 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7598 s_dlgfntheight = (WORD)size.cy;
7599 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601
7602 if (!s_usenewlook)
7603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007604 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 s_dlgfntwidth = LOWORD(dlgFontSize);
7606 s_dlgfntheight = HIWORD(dlgFontSize);
7607 }
7608}
7609
7610#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7611/*
7612 * Create a pseudo-"tearoff menu" based on the child
7613 * items of a given menu pointer.
7614 */
7615 static void
7616gui_mch_tearoff(
7617 char_u *title,
7618 vimmenu_T *menu,
7619 int initX,
7620 int initY)
7621{
7622 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7623 int template_len;
7624 int nchar, textWidth, submenuWidth;
7625 DWORD lStyle;
7626 DWORD lExtendedStyle;
7627 WORD dlgwidth;
7628 WORD menuID;
7629 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007630 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 vimmenu_T *the_menu = menu;
7632 HWND hwnd;
7633 HDC hdc;
7634 HFONT font, oldFont;
7635 int col, spaceWidth, len;
7636 int columnWidths[2];
7637 char_u *label, *text;
7638 int acLen = 0;
7639 int nameLen;
7640 int padding0, padding1, padding2 = 0;
7641 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007642 int x;
7643 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007644# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007645 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649 /*
7650 * If this menu is already torn off, move it to the mouse position.
7651 */
7652 if (IsWindow(menu->tearoff_handle))
7653 {
7654 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007655 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
7657 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7658 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7659 }
7660 return;
7661 }
7662
7663 /*
7664 * Create a new tearoff.
7665 */
7666 if (*title == MNU_HIDDEN_CHAR)
7667 title++;
7668
Bram Moolenaar734a8672019-12-02 22:49:38 +01007669 // Allocate memory to store the dialog template. It's made bigger when
7670 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 template_len = DLG_ALLOC_SIZE;
7672 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7673 if (p == NULL)
7674 return;
7675
7676 hwnd = GetDesktopWindow();
7677 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007678# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7680 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007681 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 use_lfSysmenu = TRUE;
7683 }
7684 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007687 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (s_usenewlook)
7689 oldFont = SelectFont(hdc, font);
7690 else
7691 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7692
Bram Moolenaar734a8672019-12-02 22:49:38 +01007693 // Calculate width of a single space. Used for padding columns to the
7694 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007695 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
Bram Moolenaar734a8672019-12-02 22:49:38 +01007697 // Figure out max width of the text column, the accelerator column and the
7698 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 submenuWidth = 0;
7700 for (col = 0; col < 2; col++)
7701 {
7702 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007703 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007705 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 text = (col == 0) ? pmenu->dname : pmenu->actext;
7707 if (text != NULL && *text != NUL)
7708 {
7709 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7710 if (textWidth > columnWidths[col])
7711 columnWidths[col] = textWidth;
7712 }
7713 if (pmenu->children != NULL)
7714 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7715 }
7716 }
7717 if (columnWidths[1] == 0)
7718 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007719 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (submenuWidth != 0)
7721 columnWidths[0] += submenuWidth;
7722 else
7723 columnWidths[0] += spaceWidth;
7724 }
7725 else
7726 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007727 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7729 columnWidths[1] += submenuWidth;
7730 }
7731
7732 /*
7733 * Now find the total width of our 'menu'.
7734 */
7735 textWidth = columnWidths[0] + columnWidths[1];
7736 if (submenuWidth != 0)
7737 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007738 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7740 textWidth += submenuWidth;
7741 }
7742 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7743 if (textWidth > dlgwidth)
7744 dlgwidth = textWidth;
7745 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7746
Bram Moolenaar734a8672019-12-02 22:49:38 +01007747 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (s_usenewlook)
7749 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7750 else
7751 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7752
7753 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7754 *p++ = LOWORD(lStyle);
7755 *p++ = HIWORD(lStyle);
7756 *p++ = LOWORD(lExtendedStyle);
7757 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007758 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007760 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007762 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 else
7764 *p++ = PixelToDialogX(initX); // x
7765 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007766 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 else
7768 *p++ = PixelToDialogY(initY); // y
7769 *p++ = PixelToDialogX(dlgwidth); // cx
7770 ptrueheight = p;
7771 *p++ = 0; // dialog height: changed later anyway
7772 *p++ = 0; // Menu
7773 *p++ = 0; // Class
7774
Bram Moolenaar734a8672019-12-02 22:49:38 +01007775 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007777 ? (LPSTR)title
7778 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 p += nchar;
7780
7781 if (s_usenewlook)
7782 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007783 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007784# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (use_lfSysmenu)
7786 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007787 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7789 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007790 wcscpy(p, lfSysmenu.lfFaceName);
7791 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {
7796 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007797 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799 p += nchar;
7800 }
7801
7802 /*
7803 * Loop over all the items in the menu.
7804 * But skip over the tearbar.
7805 */
7806 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7807 menu = menu->children->next;
7808 else
7809 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007810 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 for ( ; menu != NULL; menu = menu->next)
7812 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007813 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 continue;
7815 if (menu_is_separator(menu->dname))
7816 {
7817 sepPadding += 3;
7818 continue;
7819 }
7820
Bram Moolenaar734a8672019-12-02 22:49:38 +01007821 // Check if there still is plenty of room in the template. Make it
7822 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7824 {
7825 WORD *newp;
7826
7827 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7828 if (newp != NULL)
7829 {
7830 template_len += 4096;
7831 mch_memmove(newp, pdlgtemplate,
7832 (char *)p - (char *)pdlgtemplate);
7833 p = newp + (p - pdlgtemplate);
7834 pnumitems = newp + (pnumitems - pdlgtemplate);
7835 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7836 LocalFree(LocalHandle(pdlgtemplate));
7837 pdlgtemplate = newp;
7838 }
7839 }
7840
Bram Moolenaar734a8672019-12-02 22:49:38 +01007841 // Figure out minimal length of this menu label. Use "name" for the
7842 // actual text, "dname" for estimating the displayed size. "name"
7843 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 len = nameLen = (int)STRLEN(menu->name);
7845 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7846 (int)STRLEN(menu->dname))) / spaceWidth;
7847 len += padding0;
7848
7849 if (menu->actext != NULL)
7850 {
7851 acLen = (int)STRLEN(menu->actext);
7852 len += acLen;
7853 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7854 }
7855 else
7856 textWidth = 0;
7857 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7858 len += padding1;
7859
7860 if (menu->children == NULL)
7861 {
7862 padding2 = submenuWidth / spaceWidth;
7863 len += padding2;
7864 menuID = (WORD)(menu->id);
7865 }
7866 else
7867 {
7868 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007869 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871
Bram Moolenaar734a8672019-12-02 22:49:38 +01007872 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007873 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if (label == NULL)
7875 break;
7876
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007877 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007878 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007880 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 while (padding0-- > 0)
7882 *text++ = ' ';
7883 if (menu->actext != NULL)
7884 {
7885 STRNCPY(text, menu->actext, acLen);
7886 text += acLen;
7887 }
7888 while (padding1-- > 0)
7889 *text++ = ' ';
7890 if (menu->children != NULL)
7891 {
7892 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7893 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7894 }
7895 else
7896 {
7897 while (padding2-- > 0)
7898 *text++ = ' ';
7899 }
7900 *text = NUL;
7901
7902 /*
7903 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7904 * W95/NT4 it makes the tear-off look more like a menu.
7905 */
7906 p = add_dialog_element(p,
7907 BS_PUSHBUTTON|BS_LEFT,
7908 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7909 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7910 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7911 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007912 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 vim_free(label);
7914 (*pnumitems)++;
7915 }
7916
7917 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7918
7919
Bram Moolenaar734a8672019-12-02 22:49:38 +01007920 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007921 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007922 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 (LPDLGTEMPLATE)pdlgtemplate,
7924 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007925 (DLGPROC)tearoff_callback,
7926 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
7928 LocalFree(LocalHandle(pdlgtemplate));
7929 SelectFont(hdc, oldFont);
7930 DeleteObject(font);
7931 ReleaseDC(hwnd, hdc);
7932
7933 /*
7934 * Reassert ourselves as the active window. This is so that after creating
7935 * a tearoff, the user doesn't have to click with the mouse just to start
7936 * typing again!
7937 */
7938 (void)SetActiveWindow(s_hwnd);
7939
Bram Moolenaar734a8672019-12-02 22:49:38 +01007940 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 force_menu_update = TRUE;
7942}
7943#endif
7944
7945#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007946# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
Bram Moolenaar734a8672019-12-02 22:49:38 +01007948// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949# ifndef TBSTYLE_FLAT
7950# define TBSTYLE_FLAT 0x0800
7951# endif
7952
7953/*
7954 * Create the toolbar, initially unpopulated.
7955 * (just like the menu, there are no defaults, it's all
7956 * set up through menu.vim)
7957 */
7958 static void
7959initialise_toolbar(void)
7960{
7961 InitCommonControls();
7962 s_toolbarhwnd = CreateToolbarEx(
7963 s_hwnd,
7964 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7965 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007966 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007967 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 IDR_TOOLBAR1, // id of initial bitmap
7969 NULL,
7970 0, // initial number of buttons
7971 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7972 TOOLBAR_BUTTON_HEIGHT,
7973 TOOLBAR_BUTTON_WIDTH,
7974 TOOLBAR_BUTTON_HEIGHT,
7975 sizeof(TBBUTTON)
7976 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007977
7978 // Remove transparency from the toolbar to prevent the main window
7979 // background colour showing through
7980 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7981 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7982
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007983 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
7985 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007986
7987 update_toolbar_size();
7988}
7989
7990 static void
7991update_toolbar_size(void)
7992{
7993 int w, h;
7994 TBMETRICS tbm = {sizeof(TBMETRICS)};
7995
7996 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7997 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7998 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7999 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8000
8001 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8002 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8003 //TRACE("button size: %d, %d", w, h);
8004 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8005 gui.toolbar_height = h + 6;
8006
8007 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8008 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8009
8010 // TODO:
8011 // Currently, this function only updates the size of toolbar buttons.
8012 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013}
8014
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008015 static LRESULT CALLBACK
8016toolbar_wndproc(
8017 HWND hwnd,
8018 UINT uMsg,
8019 WPARAM wParam,
8020 LPARAM lParam)
8021{
8022 HandleMouseHide(uMsg, lParam);
8023 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8024}
8025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 static int
8027get_toolbar_bitmap(vimmenu_T *menu)
8028{
8029 int i = -1;
8030
8031 /*
8032 * Check user bitmaps first, unless builtin is specified.
8033 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008034 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {
8036 char_u fname[MAXPATHL];
8037 HANDLE hbitmap = NULL;
8038
8039 if (menu->iconfile != NULL)
8040 {
8041 gui_find_iconfile(menu->iconfile, fname, "bmp");
8042 hbitmap = LoadImage(
8043 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008044 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 IMAGE_BITMAP,
8046 TOOLBAR_BUTTON_WIDTH,
8047 TOOLBAR_BUTTON_HEIGHT,
8048 LR_LOADFROMFILE |
8049 LR_LOADMAP3DCOLORS
8050 );
8051 }
8052
8053 /*
8054 * If the LoadImage call failed, or the "icon=" file
8055 * didn't exist or wasn't specified, try the menu name
8056 */
8057 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008058 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008059# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008060 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008061# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008062 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 hbitmap = LoadImage(
8064 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008065 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 IMAGE_BITMAP,
8067 TOOLBAR_BUTTON_WIDTH,
8068 TOOLBAR_BUTTON_HEIGHT,
8069 LR_LOADFROMFILE |
8070 LR_LOADMAP3DCOLORS
8071 );
8072
8073 if (hbitmap != NULL)
8074 {
8075 TBADDBITMAP tbAddBitmap;
8076
8077 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008078 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8081 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008082 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
8084 }
8085 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8086 i = menu->iconidx;
8087
8088 return i;
8089}
8090#endif
8091
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008092#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8093 static void
8094initialise_tabline(void)
8095{
8096 InitCommonControls();
8097
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008098 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008099 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008100 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008101 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008102 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008103
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008104 gui.tabline_height = TABLINE_HEIGHT;
8105
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008106 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008107}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008108
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008109/*
8110 * Get tabpage_T from POINT.
8111 */
8112 static tabpage_T *
8113GetTabFromPoint(
8114 HWND hWnd,
8115 POINT pt)
8116{
8117 tabpage_T *ptp = NULL;
8118
8119 if (gui_mch_showing_tabline())
8120 {
8121 TCHITTESTINFO htinfo;
8122 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008123 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008124 if (s_tabhwnd == hWnd)
8125 {
8126 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8127 if (idx != -1)
8128 ptp = find_tabpage(idx + 1);
8129 }
8130 }
8131 return ptp;
8132}
8133
8134static POINT s_pt = {0, 0};
8135static HCURSOR s_hCursor = NULL;
8136
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008137 static LRESULT CALLBACK
8138tabline_wndproc(
8139 HWND hwnd,
8140 UINT uMsg,
8141 WPARAM wParam,
8142 LPARAM lParam)
8143{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008144 POINT pt;
8145 tabpage_T *tp;
8146 RECT rect;
8147 int nCenter;
8148 int idx0;
8149 int idx1;
8150
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008151 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008152
8153 switch (uMsg)
8154 {
8155 case WM_LBUTTONDOWN:
8156 {
8157 s_pt.x = GET_X_LPARAM(lParam);
8158 s_pt.y = GET_Y_LPARAM(lParam);
8159 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008160 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008161 break;
8162 }
8163 case WM_MOUSEMOVE:
8164 if (GetCapture() == hwnd
8165 && ((wParam & MK_LBUTTON)) != 0)
8166 {
8167 pt.x = GET_X_LPARAM(lParam);
8168 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008169 if (abs(pt.x - s_pt.x) >
8170 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008171 {
8172 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8173
8174 tp = GetTabFromPoint(hwnd, pt);
8175 if (tp != NULL)
8176 {
8177 idx0 = tabpage_index(curtab) - 1;
8178 idx1 = tabpage_index(tp) - 1;
8179
8180 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8181 nCenter = rect.left + (rect.right - rect.left) / 2;
8182
Bram Moolenaar734a8672019-12-02 22:49:38 +01008183 // Check if the mouse cursor goes over the center of
8184 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008185 if ((idx0 < idx1) && (nCenter < pt.x))
8186 {
8187 tabpage_move(idx1 + 1);
8188 update_screen(0);
8189 }
8190 else if ((idx1 < idx0) && (pt.x < nCenter))
8191 {
8192 tabpage_move(idx1);
8193 update_screen(0);
8194 }
8195 }
8196 }
8197 }
8198 break;
8199 case WM_LBUTTONUP:
8200 {
8201 if (GetCapture() == hwnd)
8202 {
8203 SetCursor(s_hCursor);
8204 ReleaseCapture();
8205 }
8206 break;
8207 }
8208 default:
8209 break;
8210 }
8211
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008212 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8213}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008214#endif
8215
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8217/*
8218 * Make the GUI window come to the foreground.
8219 */
8220 void
8221gui_mch_set_foreground(void)
8222{
8223 if (IsIconic(s_hwnd))
8224 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8225 SetForegroundWindow(s_hwnd);
8226}
8227#endif
8228
8229#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8230 static void
8231dyn_imm_load(void)
8232{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008233 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (hLibImm == NULL)
8235 return;
8236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 pImmGetCompositionStringW
8238 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8239 pImmGetContext
8240 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8241 pImmAssociateContext
8242 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8243 pImmReleaseContext
8244 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8245 pImmGetOpenStatus
8246 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8247 pImmSetOpenStatus
8248 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008249 pImmGetCompositionFontW
8250 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8251 pImmSetCompositionFontW
8252 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 pImmSetCompositionWindow
8254 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8255 pImmGetConversionStatus
8256 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008257 pImmSetConversionStatus
8258 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259
K.Takatab0b2b732022-01-19 12:59:21 +00008260 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 || pImmGetContext == NULL
8262 || pImmAssociateContext == NULL
8263 || pImmReleaseContext == NULL
8264 || pImmGetOpenStatus == NULL
8265 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008266 || pImmGetCompositionFontW == NULL
8267 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008269 || pImmGetConversionStatus == NULL
8270 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
8272 FreeLibrary(hLibImm);
8273 hLibImm = NULL;
8274 pImmGetContext = NULL;
8275 return;
8276 }
8277
8278 return;
8279}
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281#endif
8282
8283#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8284
8285# ifdef FEAT_XPM_W32
8286# define IMAGE_XPM 100
8287# endif
8288
8289typedef struct _signicon_t
8290{
8291 HANDLE hImage;
8292 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008293# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008294 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296} signicon_t;
8297
8298 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008299gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 signicon_t *sign;
8302 int x, y, w, h;
8303
8304 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8305 return;
8306
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008307# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008308 if (IS_ENABLE_DIRECTX())
8309 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008310# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 x = TEXT_X(col);
8313 y = TEXT_Y(row);
8314 w = gui.char_width * 2;
8315 h = gui.char_height;
8316 switch (sign->uType)
8317 {
8318 case IMAGE_BITMAP:
8319 {
8320 HDC hdcMem;
8321 HBITMAP hbmpOld;
8322
8323 hdcMem = CreateCompatibleDC(s_hdc);
8324 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8325 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8326 SelectObject(hdcMem, hbmpOld);
8327 DeleteDC(hdcMem);
8328 }
8329 break;
8330 case IMAGE_ICON:
8331 case IMAGE_CURSOR:
8332 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8333 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008334# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 case IMAGE_XPM:
8336 {
8337 HDC hdcMem;
8338 HBITMAP hbmpOld;
8339
8340 hdcMem = CreateCompatibleDC(s_hdc);
8341 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008342 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8344
8345 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008346 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8348 SelectObject(hdcMem, hbmpOld);
8349 DeleteDC(hdcMem);
8350 }
8351 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
8354}
8355
8356 static void
8357close_signicon_image(signicon_t *sign)
8358{
8359 if (sign)
8360 switch (sign->uType)
8361 {
8362 case IMAGE_BITMAP:
8363 DeleteObject((HGDIOBJ)sign->hImage);
8364 break;
8365 case IMAGE_CURSOR:
8366 DestroyCursor((HCURSOR)sign->hImage);
8367 break;
8368 case IMAGE_ICON:
8369 DestroyIcon((HICON)sign->hImage);
8370 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008371# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 case IMAGE_XPM:
8373 DeleteObject((HBITMAP)sign->hImage);
8374 DeleteObject((HBITMAP)sign->hShape);
8375 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
8378}
8379
8380 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008381gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382{
8383 signicon_t sign, *psign;
8384 char_u *ext;
8385
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008387 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 if (ext > signfile)
8389 {
8390 int do_load = 1;
8391
8392 if (!STRICMP(ext, ".bmp"))
8393 sign.uType = IMAGE_BITMAP;
8394 else if (!STRICMP(ext, ".ico"))
8395 sign.uType = IMAGE_ICON;
8396 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8397 sign.uType = IMAGE_CURSOR;
8398 else
8399 do_load = 0;
8400
8401 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008402 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 gui.char_width * 2, gui.char_height,
8404 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008405# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 if (!STRICMP(ext, ".xpm"))
8407 {
8408 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008409 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8410 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414
8415 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008416 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 *psign = sign;
8418
8419 if (!psign)
8420 {
8421 if (sign.hImage)
8422 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008423 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 return (void *)psign;
8426
8427}
8428
8429 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008430gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431{
8432 if (sign)
8433 {
8434 close_signicon_image((signicon_t *)sign);
8435 vim_free(sign);
8436 }
8437}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008440#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441
Bram Moolenaar734a8672019-12-02 22:49:38 +01008442/*
8443 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008444 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008446 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8448 * to get current mouse position).
8449 *
8450 * Trying to use as more Windows services as possible, and as less
8451 * IE version as possible :)).
8452 *
8453 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8454 * BalloonEval struct.
8455 * 2) Enable/Disable simply create/kill BalloonEval Timer
8456 * 3) When there was enough inactivity, timer procedure posts
8457 * async request to debugger
8458 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8459 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008460 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 */
8462
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008463 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008464make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008465{
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008466 TOOLINFOW_NEW *pti;
8467 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008468
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008469 pti = alloc(sizeof(TOOLINFOW_NEW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008470 if (pti == NULL)
8471 return;
8472
8473 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8474 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8475 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008476 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008477
8478 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8479 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8480
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008481 pti->cbSize = sizeof(TOOLINFOW_NEW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008482 pti->uFlags = TTF_SUBCLASS;
8483 pti->hwnd = beval->target;
8484 pti->hinst = 0; // Don't use string resources
8485 pti->uId = ID_BEVAL_TOOLTIP;
8486
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008487 pti->lpszText = LPSTR_TEXTCALLBACKW;
8488 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8489 pti->lParam = (LPARAM)beval->tofree;
8490 // switch multiline tooltips on
8491 if (GetClientRect(s_textArea, &rect))
8492 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8493 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008494
8495 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8496 pti->rect.left = pt.x - 3;
8497 pti->rect.top = pt.y - 3;
8498 pti->rect.right = pt.x + 3;
8499 pti->rect.bottom = pt.y + 3;
8500
8501 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8502 // Make tooltip appear sooner.
8503 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8504 // I've performed some tests and it seems the longest possible life time
8505 // of tooltip is 30 seconds.
8506 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8507 /*
8508 * HACK: force tooltip to appear, because it'll not appear until
8509 * first mouse move. D*mn M$
8510 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8511 */
8512 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8513 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8514 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008515}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008518delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008520 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521}
8522
8523 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008524BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008525 HWND hwnd UNUSED,
8526 UINT uMsg UNUSED,
8527 UINT_PTR idEvent UNUSED,
8528 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 POINT pt;
8531 RECT rect;
8532
8533 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8534 return;
8535
8536 GetCursorPos(&pt);
8537 if (WindowFromPoint(pt) != s_textArea)
8538 return;
8539
8540 ScreenToClient(s_textArea, &pt);
8541 GetClientRect(s_textArea, &rect);
8542 if (!PtInRect(&rect, pt))
8543 return;
8544
8545 if (LastActivity > 0
8546 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8547 && (cur_beval->showState != ShS_PENDING
8548 || abs(cur_beval->x - pt.x) > 3
8549 || abs(cur_beval->y - pt.y) > 3))
8550 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008551 // Pointer resting in one place long enough, it's time to show
8552 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 cur_beval->showState = ShS_PENDING;
8554 cur_beval->x = pt.x;
8555 cur_beval->y = pt.y;
8556
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008557 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 if (cur_beval->msgCB != NULL)
8560 (*cur_beval->msgCB)(cur_beval, 0);
8561 }
8562}
8563
8564 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008565gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008567 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008569 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570}
8571
8572 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008573gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008575 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (beval == NULL)
8577 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008578 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008579 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008580 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008584gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008587
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008588 vim_free(beval->msg);
8589 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8590 if (beval->msg == NULL)
8591 {
8592 delete_tooltip(beval);
8593 beval->showState = ShS_NEUTRAL;
8594 return;
8595 }
8596
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008597 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (beval->showState == ShS_SHOWING)
8599 return;
8600 GetCursorPos(&pt);
8601 ScreenToClient(s_textArea, &pt);
8602
8603 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008605 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 gui_mch_disable_beval_area(cur_beval);
8607 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008608 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008610 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611}
8612
8613 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008614gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008615 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008616 char_u *mesg,
8617 void (*mesgCB)(BalloonEval *, int),
8618 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008620 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 BalloonEval *beval;
8622
8623 if (mesg != NULL && mesgCB != NULL)
8624 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008625 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 return NULL;
8627 }
8628
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008629 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 if (beval != NULL)
8631 {
8632 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 beval->msg = mesg;
8636 beval->msgCB = mesgCB;
8637 beval->clientData = clientData;
8638
8639 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 cur_beval = beval;
8641
8642 if (p_beval)
8643 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645 return beval;
8646}
8647
8648 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008649Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008651 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 return;
8653
8654 if (cur_beval != NULL)
8655 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008656 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008658 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008659 // TRACE0("TTN_SHOW {{{");
8660 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008661 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008662 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008663 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 delete_tooltip(cur_beval);
8665 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008666 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
8668 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008669 break;
8670 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008671 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008672 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008673 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8674 info->lpszText = (LPSTR)info->lParam;
8675 info->uFlags |= TTF_DI_SETITEM;
8676 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008677 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008678 case TTN_GETDISPINFOW:
8679 {
8680 // if we get here then we have new common controls
8681 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8682 info->lpszText = (LPWSTR)info->lParam;
8683 info->uFlags |= TTF_DI_SETITEM;
8684 }
8685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 }
8687 }
8688}
8689
8690 static void
8691TrackUserActivity(UINT uMsg)
8692{
8693 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8694 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8695 LastActivity = GetTickCount();
8696}
8697
8698 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008699gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008701# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008702 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008703# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008704 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 vim_free(beval);
8706}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008707#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8710/*
8711 * We have multiple signs to draw at the same location. Draw the
8712 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8713 */
8714 void
8715netbeans_draw_multisign_indicator(int row)
8716{
8717 int i;
8718 int y;
8719 int x;
8720
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008721 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008722 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 x = 0;
8725 y = TEXT_Y(row);
8726
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008727# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008728 if (IS_ENABLE_DIRECTX())
8729 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008730# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 for (i = 0; i < gui.char_height - 3; i++)
8733 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8734
8735 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8736 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8737 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8738 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8739 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8740 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8741 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8742}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008743#endif