blob: 97abfab429f51ae0896159510f2179ab25f6a254 [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 Moolenaar734a8672019-12-02 22:49:38 +01004038// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004039# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004040
Bram Moolenaar45360022005-07-21 21:08:21 +00004041/*
4042 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004043 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004044 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004045# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004046
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004047# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004048
4049typedef struct _DllVersionInfo
4050{
4051 DWORD cbSize;
4052 DWORD dwMajorVersion;
4053 DWORD dwMinorVersion;
4054 DWORD dwBuildNumber;
4055 DWORD dwPlatformID;
4056} DLLVERSIONINFO;
4057
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004058# ifndef PROTO
4059# include <poppack.h>
4060# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004061
Bram Moolenaar45360022005-07-21 21:08:21 +00004062typedef struct tagTOOLINFOA_NEW
4063{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004064 UINT cbSize;
4065 UINT uFlags;
4066 HWND hwnd;
4067 UINT_PTR uId;
4068 RECT rect;
4069 HINSTANCE hinst;
4070 LPSTR lpszText;
4071 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004072} TOOLINFO_NEW;
4073
4074typedef struct tagNMTTDISPINFO_NEW
4075{
4076 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004077 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004078 char szText[80];
4079 HINSTANCE hinst;
4080 UINT uFlags;
4081 LPARAM lParam;
4082} NMTTDISPINFO_NEW;
4083
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004084typedef struct tagTOOLINFOW_NEW
4085{
4086 UINT cbSize;
4087 UINT uFlags;
4088 HWND hwnd;
4089 UINT_PTR uId;
4090 RECT rect;
4091 HINSTANCE hinst;
4092 LPWSTR lpszText;
4093 LPARAM lParam;
4094 void *lpReserved;
4095} TOOLINFOW_NEW;
4096
4097typedef struct tagNMTTDISPINFOW_NEW
4098{
4099 NMHDR hdr;
4100 LPWSTR lpszText;
4101 WCHAR szText[80];
4102 HINSTANCE hinst;
4103 UINT uFlags;
4104 LPARAM lParam;
4105} NMTTDISPINFOW_NEW;
4106
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004107
Bram Moolenaar45360022005-07-21 21:08:21 +00004108typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004109# ifndef TTM_SETMAXTIPWIDTH
4110# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004113# ifndef TTF_DI_SETITEM
4114# define TTF_DI_SETITEM 0x8000
4115# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004116
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004117# ifndef TTN_GETDISPINFO
4118# define TTN_GETDISPINFO (TTN_FIRST - 0)
4119# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004120
Bram Moolenaar734a8672019-12-02 22:49:38 +01004121#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004122
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004123#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004124// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4125// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004126// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004127// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004128# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4129typedef struct tagNMTTDISPINFOA {
4130 NMHDR hdr;
4131 LPSTR lpszText;
4132 char szText[80];
4133 HINSTANCE hinst;
4134 UINT uFlags;
4135 LPARAM lParam;
4136} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4137# define LPNMTTDISPINFO LPNMTTDISPINFOA
4138
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004139typedef struct tagNMTTDISPINFOW {
4140 NMHDR hdr;
4141 LPWSTR lpszText;
4142 WCHAR szText[80];
4143 HINSTANCE hinst;
4144 UINT uFlags;
4145 LPARAM lParam;
4146} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004147# endif
4148#endif
4149
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004150#ifndef TTN_GETDISPINFOW
4151# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4152#endif
4153
Bram Moolenaar734a8672019-12-02 22:49:38 +01004154// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156#ifdef FEAT_MENU
4157static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160/*
4161 * Use the system font for dialogs and tear-off menus. Remove this line to
4162 * use DLG_FONT_NAME.
4163 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004164#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#define VIM_CLASSW L"Vim"
4168
Bram Moolenaar734a8672019-12-02 22:49:38 +01004169// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4170// thus there should be room for every dialog. For tearoffs it's made bigger
4171// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#define DLG_ALLOC_SIZE 16 * 1024
4173
4174/*
4175 * stuff for dialogs, menus, tearoffs etc.
4176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177static PWORD
4178add_dialog_element(
4179 PWORD p,
4180 DWORD lStyle,
4181 WORD x,
4182 WORD y,
4183 WORD w,
4184 WORD h,
4185 WORD Id,
4186 WORD clss,
4187 const char *caption);
4188static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004189static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004190#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193static void get_dialog_font_metrics(void);
4194
4195static int dialog_default_button = -1;
4196
Bram Moolenaar734a8672019-12-02 22:49:38 +01004197// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaar734a8672019-12-02 22:49:38 +01004200static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#ifdef FEAT_TOOLBAR
4202static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004203static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004204static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004206#else
4207# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#endif
4209
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004210#ifdef FEAT_GUI_TABLINE
4211static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004212static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004213#endif
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#ifdef FEAT_MBYTE_IME
4216static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4217static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4218#endif
4219#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4220# ifdef NOIME
4221typedef struct tagCOMPOSITIONFORM {
4222 DWORD dwStyle;
4223 POINT ptCurrentPos;
4224 RECT rcArea;
4225} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4226typedef HANDLE HIMC;
4227# endif
4228
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004229static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004230static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4231static HIMC (WINAPI *pImmGetContext)(HWND);
4232static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4233static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4234static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4235static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004236static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4237static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004238static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4239static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004240static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241static void dyn_imm_load(void);
4242#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243# define pImmGetCompositionStringW ImmGetCompositionStringW
4244# define pImmGetContext ImmGetContext
4245# define pImmAssociateContext ImmAssociateContext
4246# define pImmReleaseContext ImmReleaseContext
4247# define pImmGetOpenStatus ImmGetOpenStatus
4248# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004249# define pImmGetCompositionFontW ImmGetCompositionFontW
4250# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251# define pImmSetCompositionWindow ImmSetCompositionWindow
4252# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004253# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#endif
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#ifdef FEAT_MENU
4257/*
4258 * Figure out how high the menu bar is at the moment.
4259 */
4260 static int
4261gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004262 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 static int old_menu_height = -1;
4265
4266 RECT rc1, rc2;
4267 int num;
4268 int menu_height;
4269
4270 if (gui.menu_is_active)
4271 num = GetMenuItemCount(s_menuBar);
4272 else
4273 num = 0;
4274
4275 if (num == 0)
4276 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004277 else if (IsMinimized(s_hwnd))
4278 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004279 // The height of the menu cannot be determined while the window is
4280 // minimized. Take the previous height if the menu is changed in that
4281 // state, to avoid that Vim's vertical window size accidentally
4282 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004283 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 else
4286 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004287 /*
4288 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4289 * seem to have been set yet, so menu wraps in default window
4290 * width which is very narrow. Instead just return height of a
4291 * single menu item. Will still be wrong when the menu really
4292 * should wrap over more than one line.
4293 */
4294 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4295 if (gui.starting)
4296 menu_height = rc1.bottom - rc1.top + 1;
4297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004299 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4300 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004305 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004306 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 return menu_height;
4309}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004310#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312
4313/*
4314 * Setup for the Intellimouse
4315 */
4316 static void
4317init_mouse_wheel(void)
4318{
4319
4320#ifndef SPI_GETWHEELSCROLLLINES
4321# define SPI_GETWHEELSCROLLLINES 104
4322#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004323#ifndef SPI_SETWHEELSCROLLLINES
4324# define SPI_SETWHEELSCROLLLINES 105
4325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaar734a8672019-12-02 22:49:38 +01004327#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4328#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4330#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4331
Bram Moolenaar734a8672019-12-02 22:49:38 +01004332 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaar734a8672019-12-02 22:49:38 +01004334 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004335 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4336 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337}
4338
4339
Bram Moolenaarae20f342019-11-05 21:09:23 +01004340/*
4341 * Intellimouse wheel handler.
4342 * Treat a mouse wheel event as if it were a scroll request.
4343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 static void
4345_OnMouseWheel(
4346 HWND hwnd,
4347 short zDelta)
4348{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int i;
4350 int size;
4351 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004352 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (mouse_scroll_lines == 0)
4355 init_mouse_wheel();
4356
Bram Moolenaarae20f342019-11-05 21:09:23 +01004357 wp = gui_mouse_window(FIND_POPUP);
4358
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004359#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004360 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004361 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004362 cmdarg_T cap;
4363 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004364
Bram Moolenaarae20f342019-11-05 21:09:23 +01004365 // Mouse hovers over popup window, scroll it if possible.
4366 mouse_row = wp->w_winrow;
4367 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004368 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004369 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4370 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4371 clear_oparg(&oa);
4372 cap.oap = &oa;
4373 nv_mousescroll(&cap);
4374 update_screen(0);
4375 setcursor();
4376 out_flush();
4377 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004378 }
4379#endif
4380
Bram Moolenaarae20f342019-11-05 21:09:23 +01004381 if (wp == NULL || !p_scf)
4382 wp = curwin;
4383
4384 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4385 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4386 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4387 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4388 else
4389 return;
4390 size = wp->w_height;
4391
Bram Moolenaara338adc2018-01-31 20:51:47 +01004392 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (mouse_scroll_lines > 0
4394 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4395 {
4396 for (i = mouse_scroll_lines; i > 0; --i)
4397 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4398 }
4399 else
4400 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004401 mch_enable_flush();
4402 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403}
4404
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004405#ifdef USE_SYSMENU_FONT
4406/*
4407 * Get Menu Font.
4408 * Return OK or FAIL.
4409 */
4410 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004411gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004412{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004413 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004414
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004415 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4416 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004417 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004418 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004419 &nm,
4420 0))
4421 return FAIL;
4422 *lf = nm.lfMenuFont;
4423 return OK;
4424}
4425#endif
4426
4427
4428#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4429/*
4430 * Set the GUI tabline font to the system menu font
4431 */
4432 static void
4433set_tabline_font(void)
4434{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004435 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004436 HFONT font;
4437 HWND hwnd;
4438 HDC hdc;
4439 HFONT hfntOld;
4440 TEXTMETRIC tm;
4441
4442 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4443 return;
4444
K.Takatac81e9bf2022-01-16 14:15:49 +00004445 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004446 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004447
4448 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4449
4450 /*
4451 * Compute the height of the font used for the tab text
4452 */
4453 hwnd = GetDesktopWindow();
4454 hdc = GetWindowDC(hwnd);
4455 hfntOld = SelectFont(hdc, font);
4456
4457 GetTextMetrics(hdc, &tm);
4458
4459 SelectFont(hdc, hfntOld);
4460 ReleaseDC(hwnd, hdc);
4461
4462 /*
4463 * The space used by the tab border and the space between the tab label
4464 * and the tab border is included as 7.
4465 */
4466 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4467}
K.Takatac81e9bf2022-01-16 14:15:49 +00004468#else
4469# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004470#endif
4471
Bram Moolenaar520470a2005-06-16 21:59:56 +00004472/*
4473 * Invoked when a setting was changed.
4474 */
4475 static LRESULT CALLBACK
4476_OnSettingChange(UINT n)
4477{
4478 if (n == SPI_SETWHEELSCROLLLINES)
4479 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4480 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004481 if (n == SPI_SETNONCLIENTMETRICS)
4482 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004483 return 0;
4484}
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#ifdef FEAT_NETBEANS_INTG
4487 static void
4488_OnWindowPosChanged(
4489 HWND hwnd,
4490 const LPWINDOWPOS lpwpos)
4491{
4492 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004493 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
4495 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4496 || lpwpos->cx != cx || lpwpos->cy != cy))
4497 {
4498 x = lpwpos->x;
4499 y = lpwpos->y;
4500 cx = lpwpos->cx;
4501 cy = lpwpos->cy;
4502 netbeans_frame_moved(x, y);
4503 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004504 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004505 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506}
4507#endif
4508
K.Takata4f2417f2021-06-05 16:25:32 +02004509
4510static HWND hwndTip = NULL;
4511
4512 static void
4513show_sizing_tip(int cols, int rows)
4514{
4515 TOOLINFOA ti = {sizeof(ti)};
4516 char buf[32];
4517
4518 ti.hwnd = s_hwnd;
4519 ti.uId = (UINT_PTR)s_hwnd;
4520 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4521 ti.lpszText = buf;
4522 sprintf(buf, "%dx%d", cols, rows);
4523 if (hwndTip == NULL)
4524 {
4525 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4526 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4527 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4528 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4529 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4530 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4531 }
4532 else
4533 {
4534 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4535 }
4536 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4537}
4538
4539 static void
4540destroy_sizing_tip(void)
4541{
4542 if (hwndTip != NULL)
4543 {
4544 DestroyWindow(hwndTip);
4545 hwndTip = NULL;
4546 }
4547}
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 static int
4550_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 UINT fwSide,
4552 LPRECT lprc)
4553{
4554 int w, h;
4555 int valid_w, valid_h;
4556 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004557 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 w = lprc->right - lprc->left;
4560 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004561 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 w_offset = w - valid_w;
4563 h_offset = h - valid_h;
4564
4565 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4566 || fwSide == WMSZ_BOTTOMLEFT)
4567 lprc->left += w_offset;
4568 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4569 || fwSide == WMSZ_BOTTOMRIGHT)
4570 lprc->right -= w_offset;
4571
4572 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4573 || fwSide == WMSZ_TOPRIGHT)
4574 lprc->top += h_offset;
4575 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4576 || fwSide == WMSZ_BOTTOMRIGHT)
4577 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004578
4579 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return TRUE;
4581}
4582
K.Takata92000e22022-01-20 15:10:57 +00004583#ifdef FEAT_GUI_TABLINE
4584 static void
4585_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4586{
4587 if (gui_mch_showing_tabline())
4588 {
4589 POINT pt;
4590 RECT rect;
4591
4592 /*
4593 * If the cursor is on the tabline, display the tab menu
4594 */
4595 GetCursorPos(&pt);
4596 GetWindowRect(s_textArea, &rect);
4597 if (pt.y < rect.top)
4598 {
4599 show_tabline_popup_menu();
4600 return;
4601 }
4602 }
4603 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4604}
4605
4606 static void
4607_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4608{
4609 /*
4610 * If the user double clicked the tabline, create a new tab
4611 */
4612 if (gui_mch_showing_tabline())
4613 {
4614 POINT pt;
4615 RECT rect;
4616
4617 GetCursorPos(&pt);
4618 GetWindowRect(s_textArea, &rect);
4619 if (pt.y < rect.top)
4620 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4621 }
4622 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4623}
4624#endif
4625
4626 static UINT
4627_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4628{
4629 UINT result;
4630 int x, y;
4631
4632 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4633 if (result != HTCLIENT)
4634 return result;
4635
4636#ifdef FEAT_GUI_TABLINE
4637 if (gui_mch_showing_tabline())
4638 {
4639 RECT rct;
4640
4641 // If the cursor is on the GUI tabline, don't process this event
4642 GetWindowRect(s_textArea, &rct);
4643 if (yPos < rct.top)
4644 return result;
4645 }
4646#endif
4647 (void)gui_mch_get_winpos(&x, &y);
4648 xPos -= x;
4649
4650 if (xPos < 48) // <VN> TODO should use system metric?
4651 return HTBOTTOMLEFT;
4652 else
4653 return HTBOTTOMRIGHT;
4654}
4655
4656#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4657 static LRESULT
4658_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4659{
4660 switch (hdr->code)
4661 {
4662 case TTN_GETDISPINFOW:
4663 case TTN_GETDISPINFO:
4664 {
4665 char_u *str = NULL;
4666 static void *tt_text = NULL;
4667
4668 VIM_CLEAR(tt_text);
4669
4670# ifdef FEAT_GUI_TABLINE
4671 if (gui_mch_showing_tabline()
4672 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4673 {
4674 POINT pt;
4675 /*
4676 * Mouse is over the GUI tabline. Display the
4677 * tooltip for the tab under the cursor
4678 *
4679 * Get the cursor position within the tab control
4680 */
4681 GetCursorPos(&pt);
4682 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4683 {
4684 TCHITTESTINFO htinfo;
4685 int idx;
4686
4687 /*
4688 * Get the tab under the cursor
4689 */
4690 htinfo.pt.x = pt.x;
4691 htinfo.pt.y = pt.y;
4692 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4693 if (idx != -1)
4694 {
4695 tabpage_T *tp;
4696
4697 tp = find_tabpage(idx + 1);
4698 if (tp != NULL)
4699 {
4700 get_tabline_label(tp, TRUE);
4701 str = NameBuff;
4702 }
4703 }
4704 }
4705 }
4706# endif
4707# ifdef FEAT_TOOLBAR
4708# ifdef FEAT_GUI_TABLINE
4709 else
4710# endif
4711 {
4712 UINT idButton;
4713 vimmenu_T *pMenu;
4714
4715 idButton = (UINT) hdr->idFrom;
4716 pMenu = gui_mswin_find_menu(root_menu, idButton);
4717 if (pMenu)
4718 str = pMenu->strings[MENU_INDEX_TIP];
4719 }
4720# endif
4721 if (str == NULL)
4722 break;
4723
4724 // Set the maximum width, this also enables using \n for
4725 // line break.
4726 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4727
4728 if (hdr->code == TTN_GETDISPINFOW)
4729 {
4730 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4731
4732 tt_text = enc_to_utf16(str, NULL);
4733 lpdi->lpszText = tt_text;
4734 // can't show tooltip if failed
4735 }
4736 else
4737 {
4738 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4739
4740 if (STRLEN(str) < sizeof(lpdi->szText)
4741 || ((tt_text = vim_strsave(str)) == NULL))
4742 vim_strncpy((char_u *)lpdi->szText, str,
4743 sizeof(lpdi->szText) - 1);
4744 else
4745 lpdi->lpszText = tt_text;
4746 }
4747 }
4748 break;
4749
4750# ifdef FEAT_GUI_TABLINE
4751 case TCN_SELCHANGE:
4752 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4753 {
4754 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4755 return 0L;
4756 }
4757 break;
4758
4759 case NM_RCLICK:
4760 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4761 {
4762 show_tabline_popup_menu();
4763 return 0L;
4764 }
4765 break;
4766# endif
4767
4768 default:
4769 break;
4770 }
4771 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4772}
4773#endif
4774
4775#if defined(MENUHINTS) && defined(FEAT_MENU)
4776 static LRESULT
4777_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4778{
4779 if (((UINT) HIWORD(wParam)
4780 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4781 == MF_HILITE
4782 && (State & CMDLINE) == 0)
4783 {
4784 UINT idButton;
4785 vimmenu_T *pMenu;
4786 static int did_menu_tip = FALSE;
4787
4788 if (did_menu_tip)
4789 {
4790 msg_clr_cmdline();
4791 setcursor();
4792 out_flush();
4793 did_menu_tip = FALSE;
4794 }
4795
4796 idButton = (UINT)LOWORD(wParam);
4797 pMenu = gui_mswin_find_menu(root_menu, idButton);
4798 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4799 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4800 {
4801 ++msg_hist_off;
4802 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4803 --msg_hist_off;
4804 setcursor();
4805 out_flush();
4806 did_menu_tip = TRUE;
4807 }
4808 return 0L;
4809 }
4810 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4811}
4812#endif
4813
K.Takatac81e9bf2022-01-16 14:15:49 +00004814 static LRESULT
4815_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4816{
4817 s_dpi = ydpi;
4818 s_in_dpichanged = TRUE;
4819 //TRACE("DPI: %d", ydpi);
4820
4821 update_scrollbar_size();
4822 update_toolbar_size();
4823 set_tabline_font();
4824
4825 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4826 gui_get_wide_font();
4827 gui_mswin_get_menu_height(FALSE);
4828#ifdef FEAT_MBYTE_IME
4829 im_set_position(gui.row, gui.col);
4830#endif
4831 InvalidateRect(hwnd, NULL, TRUE);
4832
4833 s_in_dpichanged = FALSE;
4834 return 0L;
4835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837
4838 static LRESULT CALLBACK
4839_WndProc(
4840 HWND hwnd,
4841 UINT uMsg,
4842 WPARAM wParam,
4843 LPARAM lParam)
4844{
4845 /*
4846 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4847 hwnd, uMsg, wParam, lParam);
4848 */
4849
4850 HandleMouseHide(uMsg, lParam);
4851
4852 s_uMsg = uMsg;
4853 s_wParam = wParam;
4854 s_lParam = lParam;
4855
4856 switch (uMsg)
4857 {
4858 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4859 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004860 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004862 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4864 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4865 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4866 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4867#ifdef FEAT_MENU
4868 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4869#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004870 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4871 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4873 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004874 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4875 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4877 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4878 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4879#ifdef FEAT_NETBEANS_INTG
4880 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4881#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004882#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004883 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4884 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004885#endif
K.Takata92000e22022-01-20 15:10:57 +00004886 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004887
Bram Moolenaar734a8672019-12-02 22:49:38 +01004888 case WM_QUERYENDSESSION: // System wants to go down.
4889 gui_shell_closed(); // Will exit when no changed buffers.
4890 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
4892 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004893 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004896 return 0L;
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 break;
4899
4900 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004901 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4902 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004903 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 return 0L;
4905
4906 case WM_SYSCHAR:
4907 /*
4908 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4909 * shortcut key, handle like a typed ALT key, otherwise call Windows
4910 * ALT key handling.
4911 */
4912#ifdef FEAT_MENU
4913 if ( !gui.menu_is_active
4914 || p_wak[0] == 'n'
4915 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4916 )
4917#endif
4918 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004919 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 return 0L;
4921 }
4922#ifdef FEAT_MENU
4923 else
K.Takata4ac893f2022-01-20 12:44:28 +00004924 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#endif
4926
4927 case WM_SYSKEYUP:
4928#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004929 // This used to be done only when menu is active: ALT key is used for
4930 // that. But that caused problems when menu is disabled and using
4931 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4932 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004933 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004935 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
4937
K.Takata4f2417f2021-06-05 16:25:32 +02004938 case WM_EXITSIZEMOVE:
4939 destroy_sizing_tip();
4940 break;
4941
Bram Moolenaar734a8672019-12-02 22:49:38 +01004942 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004943 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 case WM_MOUSEWHEEL:
4946 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004947 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaar734a8672019-12-02 22:49:38 +01004949 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004950 case WM_SETTINGCHANGE:
4951 return _OnSettingChange((UINT)wParam);
4952
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004953#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004955 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#endif
K.Takata92000e22022-01-20 15:10:57 +00004957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958#if defined(MENUHINTS) && defined(FEAT_MENU)
4959 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004960 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963#ifdef FEAT_MBYTE_IME
4964 case WM_IME_NOTIFY:
4965 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004966 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004967 return 1L;
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 case WM_IME_COMPOSITION:
4970 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004971 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004972 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004974 case WM_DPICHANGED:
4975 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4976 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004980 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
K.Takata92000e22022-01-20 15:10:57 +00004983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985
K.Takata4ac893f2022-01-20 12:44:28 +00004986 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987}
4988
4989/*
4990 * End of call-back routines
4991 */
4992
Bram Moolenaar734a8672019-12-02 22:49:38 +01004993// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994HWND vim_parent_hwnd = NULL;
4995
4996 static BOOL CALLBACK
4997FindWindowTitle(HWND hwnd, LPARAM lParam)
4998{
4999 char buf[2048];
5000 char *title = (char *)lParam;
5001
5002 if (GetWindowText(hwnd, buf, sizeof(buf)))
5003 {
5004 if (strstr(buf, title) != NULL)
5005 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005006 // Found it. Store the window ref. and quit searching if MDI
5007 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005009 if (vim_parent_hwnd != NULL)
5010 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005013 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014}
5015
5016/*
5017 * Invoked for '-P "title"' argument: search for parent application to open
5018 * our window in.
5019 */
5020 void
5021gui_mch_set_parent(char *title)
5022{
5023 EnumWindows(FindWindowTitle, (LPARAM)title);
5024 if (vim_parent_hwnd == NULL)
5025 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005026 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 mch_exit(2);
5028 }
5029}
5030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005031#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 static void
5033ole_error(char *arg)
5034{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005035 char buf[IOSIZE];
5036
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005037# ifdef VIMDLL
5038 gui.in_use = mch_is_gui_executable();
5039# endif
5040
Bram Moolenaar734a8672019-12-02 22:49:38 +01005041 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005042 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005043 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005044 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005048#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5049 static char *
5050gvim_error(void)
5051{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005052 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005053
5054 if (starting)
5055 {
5056 mch_errmsg(msg);
5057 mch_errmsg("\n");
5058 mch_exit(2);
5059 }
5060 return msg;
5061}
5062
5063 char *
5064gui_mch_do_spawn(char_u *arg)
5065{
5066 int len;
5067# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5068 char_u *session = NULL;
5069 LPWSTR tofree1 = NULL;
5070# endif
5071 WCHAR name[MAX_PATH];
5072 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5073 STARTUPINFOW si = {sizeof(si)};
5074 PROCESS_INFORMATION pi;
5075
5076 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5077 goto error;
5078 p = wcsrchr(name, L'\\');
5079 if (p == NULL)
5080 goto error;
5081 // Replace the executable name from vim(d).exe to gvim(d).exe.
5082# ifdef DEBUG
5083 wcscpy(p + 1, L"gvimd.exe");
5084# else
5085 wcscpy(p + 1, L"gvim.exe");
5086# endif
5087
5088# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5089 if (starting)
5090# endif
5091 {
5092 // Pass the command line to the new process.
5093 p = GetCommandLineW();
5094 // Skip 1st argument.
5095 while (*p && *p != L' ' && *p != L'\t')
5096 {
5097 if (*p == L'"')
5098 {
5099 while (*p && *p != L'"')
5100 ++p;
5101 if (*p)
5102 ++p;
5103 }
5104 else
5105 ++p;
5106 }
5107 cmd = p;
5108 }
5109# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5110 else
5111 {
5112 // Create a session file and pass it to the new process.
5113 LPWSTR wsession;
5114 char_u *savebg;
5115 int ret;
5116
5117 session = vim_tempname('s', FALSE);
5118 if (session == NULL)
5119 goto error;
5120 savebg = p_bg;
5121 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5122 ret = write_session_file(session);
5123 vim_free(p_bg);
5124 p_bg = savebg;
5125 if (!ret)
5126 goto error;
5127 wsession = enc_to_utf16(session, NULL);
5128 if (wsession == NULL)
5129 goto error;
5130 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005131 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005132 if (cmd == NULL)
5133 {
5134 vim_free(wsession);
5135 goto error;
5136 }
5137 tofree1 = cmd;
5138 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5139 wsession, wsession);
5140 vim_free(wsession);
5141 }
5142# endif
5143
5144 // Check additional arguments to the `:gui` command.
5145 if (arg != NULL)
5146 {
5147 warg = enc_to_utf16(arg, NULL);
5148 if (warg == NULL)
5149 goto error;
5150 tofree2 = warg;
5151 }
5152 else
5153 warg = L"";
5154
5155 // Set up the new command line.
5156 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005157 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005158 if (newcmd == NULL)
5159 goto error;
5160 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5161
5162 // Spawn a new GUI process.
5163 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5164 NULL, NULL, &si, &pi))
5165 goto error;
5166 CloseHandle(pi.hProcess);
5167 CloseHandle(pi.hThread);
5168 mch_exit(0);
5169
5170error:
5171# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5172 if (session)
5173 mch_remove(session);
5174 vim_free(session);
5175 vim_free(tofree1);
5176# endif
5177 vim_free(newcmd);
5178 vim_free(tofree2);
5179 return gvim_error();
5180}
5181#endif
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183/*
5184 * Parse the GUI related command-line arguments. Any arguments used are
5185 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005186 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 */
5188 void
5189gui_mch_prepare(int *argc, char **argv)
5190{
5191 int silent = FALSE;
5192 int idx;
5193
Bram Moolenaar734a8672019-12-02 22:49:38 +01005194 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5196 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005197 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5199 && (argv[2][0] == '-' || argv[2][0] == '/'))
5200 {
5201 silent = TRUE;
5202 idx = 2;
5203 }
5204 else
5205 idx = 1;
5206
Bram Moolenaar734a8672019-12-02 22:49:38 +01005207 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (STRICMP(argv[idx] + 1, "register") == 0)
5209 {
5210#ifdef FEAT_OLE
5211 RegisterMe(silent);
5212 mch_exit(0);
5213#else
5214 if (!silent)
5215 ole_error("register");
5216 mch_exit(2);
5217#endif
5218 }
5219
Bram Moolenaar734a8672019-12-02 22:49:38 +01005220 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5222 {
5223#ifdef FEAT_OLE
5224 UnregisterMe(!silent);
5225 mch_exit(0);
5226#else
5227 if (!silent)
5228 ole_error("unregister");
5229 mch_exit(2);
5230#endif
5231 }
5232
Bram Moolenaar734a8672019-12-02 22:49:38 +01005233 // Ignore an -embedding argument. It is only relevant if the
5234 // application wants to treat the case when it is started manually
5235 // differently from the case where it is started via automation (and
5236 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5238 {
5239#ifdef FEAT_OLE
5240 *argc = 1;
5241#else
5242 ole_error("embedding");
5243 mch_exit(2);
5244#endif
5245 }
5246 }
5247
5248#ifdef FEAT_OLE
5249 {
5250 int bDoRestart = FALSE;
5251
5252 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (bDoRestart)
5255 mch_exit(0);
5256 }
5257#endif
5258
5259#ifdef FEAT_NETBEANS_INTG
5260 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005261 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 int arg;
5263
5264 for (arg = 1; arg < *argc; arg++)
5265 if (strncmp("-nb", argv[arg], 3) == 0)
5266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 netbeansArg = argv[arg];
5268 mch_memmove(&argv[arg], &argv[arg + 1],
5269 (--*argc - arg) * sizeof(char *));
5270 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005271 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275}
5276
K.Takatac81e9bf2022-01-16 14:15:49 +00005277 static void
5278load_dpi_func(void)
5279{
5280 HMODULE hUser32;
5281
5282 hUser32 = GetModuleHandle("user32.dll");
5283 if (hUser32 == NULL)
5284 goto fail;
5285
5286 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5287 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5288 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5289 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5290 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5291 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5292
5293 if (pSetThreadDpiAwarenessContext != NULL)
5294 {
5295 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5296 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5297 if (oldctx != NULL)
5298 {
5299 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5300 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5301#ifdef DEBUG
5302 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5303 {
5304 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5305 }
5306#endif
5307 return;
5308 }
5309 }
5310
5311fail:
5312 // Disable PerMonitorV2 APIs.
5313 pGetDpiForSystem = stubGetDpiForSystem;
5314 pGetDpiForWindow = NULL;
5315 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5316 pSetThreadDpiAwarenessContext = NULL;
5317 pGetAwarenessFromDpiAwarenessContext = NULL;
5318}
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
5321 * Initialise the GUI. Create all the windows, set up all the call-backs
5322 * etc.
5323 */
5324 int
5325gui_mch_init(void)
5326{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005328 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaar734a8672019-12-02 22:49:38 +01005331 // Return here if the window was already opened (happens when
5332 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005334 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
5336 /*
5337 * Load the tearoff bitmap
5338 */
5339#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005340 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#endif
5342
K.Takatac81e9bf2022-01-16 14:15:49 +00005343 load_dpi_func();
5344
5345 s_dpi = pGetDpiForSystem();
5346 update_scrollbar_size();
5347
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350#endif
5351 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005352#ifdef FEAT_TOOLBAR
5353 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5357
Bram Moolenaar734a8672019-12-02 22:49:38 +01005358 // First try using the wide version, so that we can use any title.
5359 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005360 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005362 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 wndclassw.lpfnWndProc = _WndProc;
5364 wndclassw.cbClsExtra = 0;
5365 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005366 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5368 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5369 wndclassw.hbrBackground = s_brush;
5370 wndclassw.lpszMenuName = NULL;
5371 wndclassw.lpszClassName = szVimWndClassW;
5372
K.Takata4ac893f2022-01-20 12:44:28 +00005373 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (vim_parent_hwnd != NULL)
5378 {
5379#ifdef HAVE_TRY_EXCEPT
5380 __try
5381 {
5382#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005383 // Open inside the specified parent window.
5384 // TODO: last argument should point to a CLIENTCREATESTRUCT
5385 // structure.
5386 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005388 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005389 WS_OVERLAPPEDWINDOW | WS_CHILD
5390 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5392 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005393 100, // Any value will do
5394 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005396 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#ifdef HAVE_TRY_EXCEPT
5398 }
5399 __except(EXCEPTION_EXECUTE_HANDLER)
5400 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005401 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403#endif
5404 if (s_hwnd == NULL)
5405 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005406 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 mch_exit(2);
5408 }
5409 }
5410 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005411 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005412 // If the provided windowid is not valid reset it to zero, so that it
5413 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005414 if (IsWindow((HWND)win_socket_id) <= 0)
5415 win_socket_id = 0;
5416
Bram Moolenaar734a8672019-12-02 22:49:38 +01005417 // Create a window. If win_socket_id is not zero without border and
5418 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005419 s_hwnd = CreateWindowW(
5420 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005421 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5422 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005423 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5424 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005425 100, // Any value will do
5426 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005427 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005428 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005429 if (s_hwnd != NULL && win_socket_id != 0)
5430 {
5431 SetParent(s_hwnd, (HWND)win_socket_id);
5432 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5433 }
5434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436 if (s_hwnd == NULL)
5437 return FAIL;
5438
K.Takatac81e9bf2022-01-16 14:15:49 +00005439 if (pGetDpiForWindow != NULL)
5440 {
5441 s_dpi = pGetDpiForWindow(s_hwnd);
5442 update_scrollbar_size();
5443 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5444 }
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5447 dyn_imm_load();
5448#endif
5449
Bram Moolenaar734a8672019-12-02 22:49:38 +01005450 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005451 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005452 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005453 wndclassw.style = CS_OWNDC;
5454 wndclassw.lpfnWndProc = _TextAreaWndProc;
5455 wndclassw.cbClsExtra = 0;
5456 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005457 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005458 wndclassw.hIcon = NULL;
5459 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5460 wndclassw.hbrBackground = NULL;
5461 wndclassw.lpszMenuName = NULL;
5462 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005463
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005464 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return FAIL;
5466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005468 s_textArea = CreateWindowExW(
5469 0,
5470 szTextAreaClassW, L"Vim text area",
5471 WS_CHILD | WS_VISIBLE, 0, 0,
5472 100, // Any value will do for now
5473 100, // Any value will do for now
5474 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005475 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (s_textArea == NULL)
5478 return FAIL;
5479
Bram Moolenaar20321902016-02-17 12:30:17 +01005480#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005481 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005482 {
5483 HANDLE hIcon = NULL;
5484
5485 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005486 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005487 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005488#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005489
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490#ifdef FEAT_MENU
5491 s_menuBar = CreateMenu();
5492#endif
5493 s_hdc = GetDC(s_textArea);
5494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Bram Moolenaar734a8672019-12-02 22:49:38 +01005497 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005498 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
Bram Moolenaar734a8672019-12-02 22:49:38 +01005500 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 gui_mch_def_colors();
5502
Bram Moolenaar734a8672019-12-02 22:49:38 +01005503 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5504 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 set_normal_colors();
5506
5507 /*
5508 * Check that none of the colors are the same as the background color.
5509 * Then store the current values as the defaults.
5510 */
5511 gui_check_colors();
5512 gui.def_norm_pixel = gui.norm_pixel;
5513 gui.def_back_pixel = gui.back_pixel;
5514
Bram Moolenaar734a8672019-12-02 22:49:38 +01005515 // Get the colors for the highlight groups (gui_check_colors() might have
5516 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 highlight_gui_started();
5518
5519 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005520 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005522 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 /*
5525 * Set up for Intellimouse processing
5526 */
5527 init_mouse_wheel();
5528
5529 /*
5530 * compute a couple of metrics used for the dialogs
5531 */
5532 get_dialog_font_metrics();
5533#ifdef FEAT_TOOLBAR
5534 /*
5535 * Create the toolbar
5536 */
5537 initialise_toolbar();
5538#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005539#ifdef FEAT_GUI_TABLINE
5540 /*
5541 * Create the tabline
5542 */
5543 initialise_tabline();
5544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545#ifdef MSWIN_FIND_REPLACE
5546 /*
5547 * Initialise the dialog box stuff
5548 */
5549 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5550
Bram Moolenaar734a8672019-12-02 22:49:38 +01005551 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005553 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005555 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5557 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5558 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005561#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005562# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005563// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005564# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005565# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005566# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005567# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005568 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005569 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005570#endif
5571
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005572#ifdef FEAT_RENDER_OPTIONS
5573 if (p_rop)
5574 (void)gui_mch_set_rendering_options(p_rop);
5575#endif
5576
Bram Moolenaar748bf032005-02-02 23:04:36 +00005577theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005578 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005579 display_errors();
5580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return OK;
5582}
5583
5584/*
5585 * Get the size of the screen, taking position on multiple monitors into
5586 * account (if supported).
5587 */
5588 static void
5589get_work_area(RECT *spi_rect)
5590{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005591 HMONITOR mon;
5592 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaar734a8672019-12-02 22:49:38 +01005594 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005595 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005596 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005598 moninfo.cbSize = sizeof(MONITORINFO);
5599 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005601 *spi_rect = moninfo.rcWork;
5602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005605 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5607}
5608
5609/*
5610 * Set the size of the window to the given width and height in pixels.
5611 */
5612 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005613gui_mch_set_shellsize(
5614 int width,
5615 int height,
5616 int min_width UNUSED,
5617 int min_height UNUSED,
5618 int base_width UNUSED,
5619 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005620 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005623 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
Bram Moolenaar734a8672019-12-02 22:49:38 +01005626 // Try to keep window completely on screen.
5627 // Get position of the screen work area. This is the part that is not
5628 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 get_work_area(&workarea_rect);
5630
Bram Moolenaar734a8672019-12-02 22:49:38 +01005631 // Resizing a maximized window looks very strange, unzoom it first.
5632 // But don't do it when still starting up, it may have been requested in
5633 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005634 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005636
5637 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
Bram Moolenaar734a8672019-12-02 22:49:38 +01005639 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005640 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5641 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5642 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5643 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5644 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5645 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar734a8672019-12-02 22:49:38 +01005647 // The following should take care of keeping Vim on the same monitor, no
5648 // matter if the secondary monitor is left or right of the primary
5649 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005650 window_rect.right = window_rect.left + win_width;
5651 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005652
Bram Moolenaar734a8672019-12-02 22:49:38 +01005653 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005654 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5655 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005657 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5658 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005660 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5661 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005663 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5664 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005666 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5667 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 SetActiveWindow(s_hwnd);
5670 SetFocus(s_hwnd);
5671
Bram Moolenaar734a8672019-12-02 22:49:38 +01005672 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674}
5675
5676
5677 void
5678gui_mch_set_scrollbar_thumb(
5679 scrollbar_T *sb,
5680 long val,
5681 long size,
5682 long max)
5683{
5684 SCROLLINFO info;
5685
5686 sb->scroll_shift = 0;
5687 while (max > 32767)
5688 {
5689 max = (max + 1) >> 1;
5690 val >>= 1;
5691 size >>= 1;
5692 ++sb->scroll_shift;
5693 }
5694
5695 if (sb->scroll_shift > 0)
5696 ++size;
5697
5698 info.cbSize = sizeof(info);
5699 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5700 info.nPos = val;
5701 info.nMin = 0;
5702 info.nMax = max;
5703 info.nPage = size;
5704 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5705}
5706
5707
5708/*
5709 * Set the current text font.
5710 */
5711 void
5712gui_mch_set_font(GuiFont font)
5713{
5714 gui.currFont = font;
5715}
5716
5717
5718/*
5719 * Set the current text foreground color.
5720 */
5721 void
5722gui_mch_set_fg_color(guicolor_T color)
5723{
5724 gui.currFgColor = color;
5725}
5726
5727/*
5728 * Set the current text background color.
5729 */
5730 void
5731gui_mch_set_bg_color(guicolor_T color)
5732{
5733 gui.currBgColor = color;
5734}
5735
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005736/*
5737 * Set the current text special color.
5738 */
5739 void
5740gui_mch_set_sp_color(guicolor_T color)
5741{
5742 gui.currSpColor = color;
5743}
5744
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005745#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746/*
5747 * Multi-byte handling, originally by Sung-Hoon Baek.
5748 * First static functions (no prototypes generated).
5749 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005750# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005751# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005752# endif
5753# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * handle WM_IME_NOTIFY message
5757 */
5758 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005759_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 LRESULT lResult = 0;
5762 HIMC hImc;
5763
5764 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5765 return lResult;
5766 switch (dwCommand)
5767 {
5768 case IMN_SETOPENSTATUS:
5769 if (pImmGetOpenStatus(hImc))
5770 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005771 LOGFONTW lf = norm_logfont;
5772 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5773 // Work around when PerMonitorV2 is not enabled in the process level.
5774 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5775 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 im_set_position(gui.row, gui.col);
5777
Bram Moolenaar734a8672019-12-02 22:49:38 +01005778 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 State &= ~LANGMAP;
5780 if (State & INSERT)
5781 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005782# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005783 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5785 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005786 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 int old_row = gui.row;
5788 int old_col = gui.col;
5789
5790 // This must be called here before
5791 // status_redraw_curbuf(), otherwise the mode
5792 // message may appear in the wrong position.
5793 showmode();
5794 status_redraw_curbuf();
5795 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005796 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 gui.row = old_row;
5798 gui.col = old_col;
5799 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802 }
5803 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005804 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 lResult = 0;
5806 break;
5807 }
5808 pImmReleaseContext(hWnd, hImc);
5809 return lResult;
5810}
5811
5812 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005813_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 char_u *ret;
5816 int len;
5817
Bram Moolenaar734a8672019-12-02 22:49:38 +01005818 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return 0;
5820
5821 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5822 if (ret != NULL)
5823 {
5824 add_to_input_buf_csi(ret, len);
5825 vim_free(ret);
5826 return 1;
5827 }
5828 return 0;
5829}
5830
5831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 * void GetResultStr()
5833 *
5834 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5835 * get complete composition string
5836 */
5837 static char_u *
5838GetResultStr(HWND hwnd, int GCS, int *lenp)
5839{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005840 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005841 LONG ret;
5842 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 char_u *convbuf = NULL;
5844
5845 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5846 return NULL;
5847
K.Takatab0b2b732022-01-19 12:59:21 +00005848 // Get the length of the composition string.
5849 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5850 if (ret <= 0)
5851 return NULL;
5852
5853 // Allocate the requested buffer plus space for the NUL character.
5854 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (buf == NULL)
5856 return NULL;
5857
K.Takatab0b2b732022-01-19 12:59:21 +00005858 // Reads in the composition string.
5859 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5860 *lenp = ret / sizeof(WCHAR);
5861
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005862 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 pImmReleaseContext(hwnd, hIMC);
5864 vim_free(buf);
5865 return convbuf;
5866}
5867#endif
5868
Bram Moolenaar734a8672019-12-02 22:49:38 +01005869// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005870#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
5872/*
5873 * set font to IM.
5874 */
5875 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005876im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 HIMC hImc;
5879
5880 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5881 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005882 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 pImmReleaseContext(s_hwnd, hImc);
5884 }
5885}
5886
5887/*
5888 * Notify cursor position to IM.
5889 */
5890 void
5891im_set_position(int row, int col)
5892{
5893 HIMC hImc;
5894
5895 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5896 {
5897 COMPOSITIONFORM cfs;
5898
5899 cfs.dwStyle = CFS_POINT;
5900 cfs.ptCurrentPos.x = FILL_X(col);
5901 cfs.ptCurrentPos.y = FILL_Y(row);
5902 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005903 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5904 {
5905 // Work around when PerMonitorV2 is not enabled in the process level.
5906 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5907 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 pImmSetCompositionWindow(hImc, &cfs);
5910
5911 pImmReleaseContext(s_hwnd, hImc);
5912 }
5913}
5914
5915/*
5916 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5917 */
5918 void
5919im_set_active(int active)
5920{
5921 HIMC hImc;
5922 static HIMC hImcOld = (HIMC)0;
5923
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005924# ifdef VIMDLL
5925 if (!gui.in_use && !gui.starting)
5926 {
5927 mbyte_im_set_active(active);
5928 return;
5929 }
5930# endif
5931
Bram Moolenaar734a8672019-12-02 22:49:38 +01005932 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {
5934 if (p_imdisable)
5935 {
5936 if (hImcOld == (HIMC)0)
5937 {
5938 hImcOld = pImmGetContext(s_hwnd);
5939 if (hImcOld)
5940 pImmAssociateContext(s_hwnd, (HIMC)0);
5941 }
5942 active = FALSE;
5943 }
5944 else if (hImcOld != (HIMC)0)
5945 {
5946 pImmAssociateContext(s_hwnd, hImcOld);
5947 hImcOld = (HIMC)0;
5948 }
5949
5950 hImc = pImmGetContext(s_hwnd);
5951 if (hImc)
5952 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005953 /*
5954 * for Korean ime
5955 */
5956 HKL hKL = GetKeyboardLayout(0);
5957
5958 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5959 {
5960 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5961 static BOOL bSaved = FALSE;
5962
5963 if (active)
5964 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005965 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005966 if (bSaved)
5967 pImmSetConversionStatus(hImc, dwConversionSaved,
5968 dwSentenceSaved);
5969 bSaved = FALSE;
5970 }
5971 else
5972 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005973 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005974 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5975 &dwSentenceSaved))
5976 {
5977 bSaved = TRUE;
5978 pImmSetConversionStatus(hImc,
5979 dwConversionSaved & ~(IME_CMODE_NATIVE
5980 | IME_CMODE_FULLSHAPE),
5981 dwSentenceSaved);
5982 }
5983 }
5984 }
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 pImmSetOpenStatus(hImc, active);
5987 pImmReleaseContext(s_hwnd, hImc);
5988 }
5989 }
5990}
5991
5992/*
5993 * Get IM status. When IM is on, return not 0. Else return 0.
5994 */
5995 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005996im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 int status = 0;
5999 HIMC hImc;
6000
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006001# ifdef VIMDLL
6002 if (!gui.in_use && !gui.starting)
6003 return mbyte_im_get_status();
6004# endif
6005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6007 {
6008 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6009 pImmReleaseContext(s_hwnd, hImc);
6010 }
6011 return status;
6012}
6013
Bram Moolenaar734a8672019-12-02 22:49:38 +01006014#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006017/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006018 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006019 */
6020 static void
6021latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6022{
6023 int c;
6024
Bram Moolenaarca003e12006-03-17 23:19:38 +00006025 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006026 {
6027 c = *text++;
6028 switch (c)
6029 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006030 case 0xa4: c = 0x20ac; break; // euro
6031 case 0xa6: c = 0x0160; break; // S hat
6032 case 0xa8: c = 0x0161; break; // S -hat
6033 case 0xb4: c = 0x017d; break; // Z hat
6034 case 0xb8: c = 0x017e; break; // Z -hat
6035 case 0xbc: c = 0x0152; break; // OE
6036 case 0xbd: c = 0x0153; break; // oe
6037 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006038 }
6039 *unicodebuf++ = c;
6040 }
6041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042
6043#ifdef FEAT_RIGHTLEFT
6044/*
6045 * What is this for? In the case where you are using Win98 or Win2K or later,
6046 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6047 * reverses the string sent to the TextOut... family. This sucks, because we
6048 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6049 * way to tell Windblows not to do this!
6050 *
6051 * The short of it is that this 'RevOut' only gets called if you are running
6052 * one of the new, "improved" MS OSes, and only if you are running in
6053 * 'rightleft' mode. It makes display take *slightly* longer, but not
6054 * noticeably so.
6055 */
6056 static void
6057RevOut( HDC s_hdc,
6058 int col,
6059 int row,
6060 UINT foptions,
6061 CONST RECT *pcliprect,
6062 LPCTSTR text,
6063 UINT len,
6064 CONST INT *padding)
6065{
6066 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006068 for (ix = 0; ix < (int)len; ++ix)
6069 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6070 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071}
6072#endif
6073
Bram Moolenaar92467d32017-12-05 13:22:16 +01006074 static void
6075draw_line(
6076 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006077 int y1,
6078 int x2,
6079 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006080 COLORREF color)
6081{
6082#if defined(FEAT_DIRECTX)
6083 if (IS_ENABLE_DIRECTX())
6084 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6085 else
6086#endif
6087 {
6088 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6089 HPEN old_pen = SelectObject(s_hdc, hpen);
6090 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006091 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006092 LineTo(s_hdc, x2, y2);
6093 DeleteObject(SelectObject(s_hdc, old_pen));
6094 }
6095}
6096
6097 static void
6098set_pixel(
6099 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006100 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006101 COLORREF color)
6102{
6103#if defined(FEAT_DIRECTX)
6104 if (IS_ENABLE_DIRECTX())
6105 DWriteContext_SetPixel(s_dwc, x, y, color);
6106 else
6107#endif
6108 SetPixel(s_hdc, x, y, color);
6109}
6110
6111 static void
6112fill_rect(
6113 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006114 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006115 COLORREF color)
6116{
6117#if defined(FEAT_DIRECTX)
6118 if (IS_ENABLE_DIRECTX())
6119 DWriteContext_FillRect(s_dwc, rcp, color);
6120 else
6121#endif
6122 {
6123 HBRUSH hbr2;
6124
6125 if (hbr == NULL)
6126 hbr2 = CreateSolidBrush(color);
6127 else
6128 hbr2 = hbr;
6129 FillRect(s_hdc, rcp, hbr2);
6130 if (hbr == NULL)
6131 DeleteBrush(hbr2);
6132 }
6133}
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 void
6136gui_mch_draw_string(
6137 int row,
6138 int col,
6139 char_u *text,
6140 int len,
6141 int flags)
6142{
6143 static int *padding = NULL;
6144 static int pad_size = 0;
6145 int i;
6146 const RECT *pcliprect = NULL;
6147 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 static WCHAR *unicodebuf = NULL;
6149 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006150 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 int y;
6153
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 /*
6155 * Italic and bold text seems to have an extra row of pixels at the bottom
6156 * (below where the bottom of the character should be). If we draw the
6157 * characters with a solid background, the top row of pixels in the
6158 * character below will be overwritten. We can fix this by filling in the
6159 * background ourselves, to the correct character proportions, and then
6160 * writing the character in transparent mode. Still have a problem when
6161 * the character is "_", which gets written on to the character below.
6162 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6163 * pixel in their slots, which fixes the problem with the bottom row of
6164 * pixels. We still need this code because otherwise the top row of pixels
6165 * becomes a problem. - webb.
6166 */
6167 static HBRUSH hbr_cache[2] = {NULL, NULL};
6168 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6169 static int brush_lru = 0;
6170 HBRUSH hbr;
6171 RECT rc;
6172
6173 if (!(flags & DRAW_TRANSP))
6174 {
6175 /*
6176 * Clear background first.
6177 * Note: FillRect() excludes right and bottom of rectangle.
6178 */
6179 rc.left = FILL_X(col);
6180 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (has_mbyte)
6182 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006184 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
6186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 rc.right = FILL_X(col + len);
6188 rc.bottom = FILL_Y(row + 1);
6189
Bram Moolenaar734a8672019-12-02 22:49:38 +01006190 // Cache the created brush, that saves a lot of time. We need two:
6191 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (gui.currBgColor == brush_color[0])
6193 {
6194 hbr = hbr_cache[0];
6195 brush_lru = 1;
6196 }
6197 else if (gui.currBgColor == brush_color[1])
6198 {
6199 hbr = hbr_cache[1];
6200 brush_lru = 0;
6201 }
6202 else
6203 {
6204 if (hbr_cache[brush_lru] != NULL)
6205 DeleteBrush(hbr_cache[brush_lru]);
6206 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6207 brush_color[brush_lru] = gui.currBgColor;
6208 hbr = hbr_cache[brush_lru];
6209 brush_lru = !brush_lru;
6210 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006211
Bram Moolenaar92467d32017-12-05 13:22:16 +01006212 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 SetBkMode(s_hdc, TRANSPARENT);
6215
6216 /*
6217 * When drawing block cursor, prevent inverted character spilling
6218 * over character cell (can happen with bold/italic)
6219 */
6220 if (flags & DRAW_CURSOR)
6221 {
6222 pcliprect = &rc;
6223 foptions = ETO_CLIPPED;
6224 }
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 SetTextColor(s_hdc, gui.currFgColor);
6227 SelectFont(s_hdc, gui.currFont);
6228
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006229#ifdef FEAT_DIRECTX
6230 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006231 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006232#endif
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6235 {
6236 vim_free(padding);
6237 pad_size = Columns;
6238
Bram Moolenaar734a8672019-12-02 22:49:38 +01006239 // Don't give an out-of-memory message here, it would call us
6240 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006241 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 if (padding != NULL)
6243 for (i = 0; i < pad_size; i++)
6244 padding[i] = gui.char_width;
6245 }
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 /*
6248 * We have to provide the padding argument because italic and bold versions
6249 * of fixed-width fonts are often one pixel or so wider than their normal
6250 * versions.
6251 * No check for DRAW_BOLD, Windows will have done it already.
6252 */
6253
Bram Moolenaar734a8672019-12-02 22:49:38 +01006254 // Check if there are any UTF-8 characters. If not, use normal text
6255 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 if (enc_utf8)
6257 for (n = 0; n < len; ++n)
6258 if (text[n] >= 0x80)
6259 break;
6260
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006261#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006262 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6263 // required that unicode drawing routine, currently. So this forces it
6264 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006265 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006266 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006267#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006268
Bram Moolenaar734a8672019-12-02 22:49:38 +01006269 // Check if the Unicode buffer exists and is big enough. Create it
6270 // with the same length as the multi-byte string, the number of wide
6271 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006272 if ((enc_utf8
6273 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6274 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 && (unicodebuf == NULL || len > unibuflen))
6276 {
6277 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006278 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279
6280 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006281 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
6283 unibuflen = len;
6284 }
6285
6286 if (enc_utf8 && n < len && unicodebuf != NULL)
6287 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006288 // Output UTF-8 characters. Composing characters should be
6289 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006290 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006291 int wlen; // string length in words
6292 int clen; // string length in characters
6293 int cells; // cell width of string up to composing char
6294 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006295 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006297 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006298 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006300 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006302 c = utf_ptr2char(text + i);
6303 if (c >= 0x10000)
6304 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006305 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006306 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6307 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006308 }
6309 else
6310 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006311 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006312 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006313
6314 if (utf_iscomposing(c))
6315 cw = 0;
6316 else
6317 {
6318 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006319 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006320 cw = 1;
6321 }
6322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (unicodepdy != NULL)
6324 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006325 // Use unicodepdy to make characters fit as we expect, even
6326 // when the font uses different widths (e.g., bold character
6327 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006328 if (c >= 0x10000)
6329 {
6330 unicodepdy[wlen - 2] = cw * gui.char_width;
6331 unicodepdy[wlen - 1] = 0;
6332 }
6333 else
6334 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006337 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006338 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006340#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006341 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006342 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006343 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006344 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006345 TEXT_X(col), TEXT_Y(row),
6346 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006347 gui.char_width, gui.currFgColor,
6348 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006349 }
6350 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006351#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006352 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6353 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006354 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006356 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006358 // If we want to display codepage data, and the current CP is not the
6359 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (unicodebuf != NULL)
6361 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006362 if (enc_latin9)
6363 latin9_to_ucs(text, len, unicodebuf);
6364 else
6365 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 MB_PRECOMPOSED,
6367 (char *)text, len,
6368 (LPWSTR)unicodebuf, unibuflen);
6369 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006370 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006371 // Use unicodepdy to make characters fit as we expect, even
6372 // when the font uses different widths (e.g., bold character
6373 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006374 if (unicodepdy != NULL)
6375 {
6376 int i;
6377 int cw;
6378
6379 for (i = 0; i < len; ++i)
6380 {
6381 cw = utf_char2cells(unicodebuf[i]);
6382 if (cw > 2)
6383 cw = 1;
6384 unicodepdy[i] = cw * gui.char_width;
6385 }
6386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006388 foptions, pcliprect, unicodebuf, len, unicodepdy);
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 }
6391 }
6392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 {
6394#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006395 // Windows will mess up RL text, so we have to draw it character by
6396 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006397 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6399 foptions, pcliprect, (char *)text, len, padding);
6400 else
6401#endif
6402 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6403 foptions, pcliprect, (char *)text, len, padding);
6404 }
6405
Bram Moolenaar734a8672019-12-02 22:49:38 +01006406 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (flags & DRAW_UNDERL)
6408 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006409 // When p_linespace is 0, overwrite the bottom row of pixels.
6410 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (p_linespace > 1)
6413 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006414 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006416
Bram Moolenaar734a8672019-12-02 22:49:38 +01006417 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006418 if (flags & DRAW_STRIKE)
6419 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006420 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006421 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006422 }
6423
Bram Moolenaar734a8672019-12-02 22:49:38 +01006424 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006425 if (flags & DRAW_UNDERC)
6426 {
6427 int x;
6428 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006429 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006430
6431 y = FILL_Y(row + 1) - 1;
6432 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6433 {
6434 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006435 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006436 }
6437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438}
6439
6440
6441/*
6442 * Output routines.
6443 */
6444
Bram Moolenaar734a8672019-12-02 22:49:38 +01006445/*
6446 * Flush any output to the screen
6447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 void
6449gui_mch_flush(void)
6450{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006451#if defined(FEAT_DIRECTX)
6452 if (IS_ENABLE_DIRECTX())
6453 DWriteContext_Flush(s_dwc);
6454#endif
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 GdiFlush();
6457}
6458
6459 static void
6460clear_rect(RECT *rcp)
6461{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006462 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
6465
Bram Moolenaarc716c302006-01-21 22:12:51 +00006466 void
6467gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6468{
6469 RECT workarea_rect;
6470
6471 get_work_area(&workarea_rect);
6472
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006473 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006474 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6475 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006476
Bram Moolenaar734a8672019-12-02 22:49:38 +01006477 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6478 // the menubar for MSwin, we subtract it from the screen height, so that
6479 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006480 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006481 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6482 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6483 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6484 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006485}
6486
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488#if defined(FEAT_MENU) || defined(PROTO)
6489/*
6490 * Add a sub menu to the menu bar.
6491 */
6492 void
6493gui_mch_add_menu(
6494 vimmenu_T *menu,
6495 int pos)
6496{
6497 vimmenu_T *parent = menu->parent;
6498
6499 menu->submenu_id = CreatePopupMenu();
6500 menu->id = s_menu_id++;
6501
6502 if (menu_is_menubar(menu->name))
6503 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006504 WCHAR *wn;
6505 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006507 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006508 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006509 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006511 infow.cbSize = sizeof(infow);
6512 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6513 | MIIM_SUBMENU;
6514 infow.dwItemData = (long_u)menu;
6515 infow.wID = menu->id;
6516 infow.fType = MFT_STRING;
6517 infow.dwTypeData = wn;
6518 infow.cch = (UINT)wcslen(wn);
6519 infow.hSubMenu = menu->submenu_id;
6520 InsertMenuItemW((parent == NULL)
6521 ? s_menuBar : parent->submenu_id,
6522 (UINT)pos, TRUE, &infow);
6523 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525
Bram Moolenaar734a8672019-12-02 22:49:38 +01006526 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (parent == NULL)
6528 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006529# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 else if (IsWindow(parent->tearoff_handle))
6531 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533}
6534
6535 void
6536gui_mch_show_popupmenu(vimmenu_T *menu)
6537{
6538 POINT mp;
6539
K.Takata45f9cfb2022-01-21 11:11:00 +00006540 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6542}
6543
6544 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006545gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 vimmenu_T *menu = gui_find_menu(path_name);
6548
6549 if (menu != NULL)
6550 {
6551 POINT p;
6552
Bram Moolenaar734a8672019-12-02 22:49:38 +01006553 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006555 if (mouse_pos)
6556 {
6557 int mx, my;
6558
6559 gui_mch_getmouse(&mx, &my);
6560 p.x += mx;
6561 p.y += my;
6562 }
6563 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006565 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6567 }
6568 msg_scroll = FALSE;
6569 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6570 }
6571}
6572
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006573# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574/*
6575 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6576 * create it as a pseudo-"tearoff menu".
6577 */
6578 void
6579gui_make_tearoff(char_u *path_name)
6580{
6581 vimmenu_T *menu = gui_find_menu(path_name);
6582
Bram Moolenaar734a8672019-12-02 22:49:38 +01006583 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if (menu != NULL)
6585 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6586}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589/*
6590 * Add a menu item to a menu
6591 */
6592 void
6593gui_mch_add_menu_item(
6594 vimmenu_T *menu,
6595 int idx)
6596{
6597 vimmenu_T *parent = menu->parent;
6598
6599 menu->id = s_menu_id++;
6600 menu->submenu_id = NULL;
6601
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006602# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6604 {
6605 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6606 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6607 }
6608 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006609# endif
6610# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 if (menu_is_toolbar(parent->name))
6612 {
6613 TBBUTTON newtb;
6614
Bram Moolenaara80faa82020-04-12 19:37:17 +02006615 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (menu_is_separator(menu->name))
6617 {
6618 newtb.iBitmap = 0;
6619 newtb.fsStyle = TBSTYLE_SEP;
6620 }
6621 else
6622 {
6623 newtb.iBitmap = get_toolbar_bitmap(menu);
6624 newtb.fsStyle = TBSTYLE_BUTTON;
6625 }
6626 newtb.idCommand = menu->id;
6627 newtb.fsState = TBSTATE_ENABLED;
6628 newtb.iString = 0;
6629 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6630 (LPARAM)&newtb);
6631 menu->submenu_id = (HMENU)-1;
6632 }
6633 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006636 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006638 wn = enc_to_utf16(menu->name, NULL);
6639 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006641 InsertMenuW(parent->submenu_id, (UINT)idx,
6642 (menu_is_separator(menu->name)
6643 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6644 (UINT)menu->id, wn);
6645 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006647# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (IsWindow(parent->tearoff_handle))
6649 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006650# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 }
6652}
6653
6654/*
6655 * Destroy the machine specific menu widget.
6656 */
6657 void
6658gui_mch_destroy_menu(vimmenu_T *menu)
6659{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006660# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 /*
6662 * is this a toolbar button?
6663 */
6664 if (menu->submenu_id == (HMENU)-1)
6665 {
6666 int iButton;
6667
6668 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6669 (WPARAM)menu->id, 0);
6670 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6671 }
6672 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 if (menu->parent != NULL
6676 && menu_is_popup(menu->parent->dname)
6677 && menu->parent->submenu_id != NULL)
6678 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6679 else
6680 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6681 if (menu->submenu_id != NULL)
6682 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006683# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (IsWindow(menu->tearoff_handle))
6685 DestroyWindow(menu->tearoff_handle);
6686 if (menu->parent != NULL
6687 && menu->parent->children != NULL
6688 && IsWindow(menu->parent->tearoff_handle))
6689 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006690 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 menu->modes = 0;
6692 rebuild_tearoff(menu->parent);
6693 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 }
6696}
6697
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006698# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 static void
6700rebuild_tearoff(vimmenu_T *menu)
6701{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006702 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 char_u tbuf[128];
6704 RECT trect;
6705 RECT rct;
6706 RECT roct;
6707 int x, y;
6708
6709 HWND thwnd = menu->tearoff_handle;
6710
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006711 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 if (GetWindowRect(thwnd, &trect)
6713 && GetWindowRect(s_hwnd, &rct)
6714 && GetClientRect(s_hwnd, &roct))
6715 {
6716 x = trect.left - rct.left;
6717 y = (trect.top - rct.bottom + roct.bottom);
6718 }
6719 else
6720 {
6721 x = y = 0xffffL;
6722 }
6723 DestroyWindow(thwnd);
6724 if (menu->children != NULL)
6725 {
6726 gui_mch_tearoff(tbuf, menu, x, y);
6727 if (IsWindow(menu->tearoff_handle))
6728 (void) SetWindowPos(menu->tearoff_handle,
6729 NULL,
6730 (int)trect.left,
6731 (int)trect.top,
6732 0, 0,
6733 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6734 }
6735}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006736# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738/*
6739 * Make a menu either grey or not grey.
6740 */
6741 void
6742gui_mch_menu_grey(
6743 vimmenu_T *menu,
6744 int grey)
6745{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006746# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 /*
6748 * is this a toolbar button?
6749 */
6750 if (menu->submenu_id == (HMENU)-1)
6751 {
6752 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006753 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006756# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006757 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6758 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006760# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6762 {
6763 WORD menuID;
6764 HWND menuHandle;
6765
6766 /*
6767 * A tearoff button has changed state.
6768 */
6769 if (menu->children == NULL)
6770 menuID = (WORD)(menu->id);
6771 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006772 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6774 if (menuHandle)
6775 EnableWindow(menuHandle, !grey);
6776
6777 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780
Bram Moolenaar734a8672019-12-02 22:49:38 +01006781#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783
Bram Moolenaar734a8672019-12-02 22:49:38 +01006784// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006787#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
6789#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6790/*
6791 * stuff for dialogs
6792 */
6793
6794/*
6795 * The callback routine used by all the dialogs. Very simple. First,
6796 * acknowledges the INITDIALOG message so that Windows knows to do standard
6797 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6798 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6799 * number.
6800 */
6801 static LRESULT CALLBACK
6802dialog_callback(
6803 HWND hwnd,
6804 UINT message,
6805 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006806 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 if (message == WM_INITDIALOG)
6809 {
6810 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006811 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 (void)SetFocus(hwnd);
6813 if (dialog_default_button > IDCANCEL)
6814 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006815 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006816 // We don't have a default, set focus on another element of the
6817 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006818 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 return FALSE;
6820 }
6821
6822 if (message == WM_COMMAND)
6823 {
6824 int button = LOWORD(wParam);
6825
Bram Moolenaar734a8672019-12-02 22:49:38 +01006826 // Don't end the dialog if something was selected that was
6827 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 if (button >= DLG_NONBUTTON_CONTROL)
6829 return TRUE;
6830
Bram Moolenaar734a8672019-12-02 22:49:38 +01006831 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006833 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006834 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006835 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006836
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006837 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6838 p = utf16_to_enc(wp, NULL);
6839 vim_strncpy(s_textfield, p, IOSIZE);
6840 vim_free(p);
6841 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
6844 /*
6845 * Need to check for IDOK because if the user just hits Return to
6846 * accept the default value, some reason this is what we get.
6847 */
6848 if (button == IDOK)
6849 {
6850 if (dialog_default_button > IDCANCEL)
6851 EndDialog(hwnd, dialog_default_button);
6852 }
6853 else
6854 EndDialog(hwnd, button - IDCANCEL);
6855 return TRUE;
6856 }
6857
6858 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6859 {
6860 EndDialog(hwnd, 0);
6861 return TRUE;
6862 }
6863 return FALSE;
6864}
6865
6866/*
6867 * Create a dialog dynamically from the parameter strings.
6868 * type = type of dialog (question, alert, etc.)
6869 * title = dialog title. may be NULL for default title.
6870 * message = text to display. Dialog sizes to accommodate it.
6871 * buttons = '\n' separated list of button captions, default first.
6872 * dfltbutton = number of default button.
6873 *
6874 * This routine returns 1 if the first button is pressed,
6875 * 2 for the second, etc.
6876 *
6877 * 0 indicates Esc was pressed.
6878 * -1 for unexpected error
6879 *
6880 * If stubbing out this fn, return 1.
6881 */
6882
Bram Moolenaar734a8672019-12-02 22:49:38 +01006883static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 "IDR_VIM",
6886 "IDR_VIM_ERROR",
6887 "IDR_VIM_ALERT",
6888 "IDR_VIM_INFO",
6889 "IDR_VIM_QUESTION"
6890};
6891
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 int
6893gui_mch_dialog(
6894 int type,
6895 char_u *title,
6896 char_u *message,
6897 char_u *buttons,
6898 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006899 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006900 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
6902 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006903 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 int numButtons;
6905 int *buttonWidths, *buttonPositions;
6906 int buttonYpos;
6907 int nchar, i;
6908 DWORD lStyle;
6909 int dlgwidth = 0;
6910 int dlgheight;
6911 int editboxheight;
6912 int horizWidth = 0;
6913 int msgheight;
6914 char_u *pstart;
6915 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006916 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 char_u *tbuffer;
6918 RECT rect;
6919 HWND hwnd;
6920 HDC hdc;
6921 HFONT font, oldFont;
6922 TEXTMETRIC fontInfo;
6923 int fontHeight;
6924 int textWidth, minButtonWidth, messageWidth;
6925 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006926 int maxDialogHeight;
6927 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 int vertical;
6929 int dlgPaddingX;
6930 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006931# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006932 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006934# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006935 garray_T ga;
6936 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006937 int dlg_icon_width;
6938 int dlg_icon_height;
6939 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006941# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006943# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006944 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006945# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006946 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006947 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar748bf032005-02-02 23:04:36 +00006950 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006951 {
6952 load_dpi_func();
6953 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006954 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006955 }
6956 else
6957 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959 if ((type < 0) || (type > VIM_LAST_TYPE))
6960 type = 0;
6961
Bram Moolenaar734a8672019-12-02 22:49:38 +01006962 // allocate some memory for dialog template
6963 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006964 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006965 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 if (p == NULL)
6968 return -1;
6969
6970 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006971 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6973 * const.
6974 */
6975 tbuffer = vim_strsave(buttons);
6976 if (tbuffer == NULL)
6977 return -1;
6978
Bram Moolenaar734a8672019-12-02 22:49:38 +01006979 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar734a8672019-12-02 22:49:38 +01006981 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 numButtons = 1;
6983 for (i = 0; tbuffer[i] != '\0'; i++)
6984 {
6985 if (tbuffer[i] == DLG_BUTTON_SEP)
6986 numButtons++;
6987 }
6988 if (dfltbutton >= numButtons)
6989 dfltbutton = -1;
6990
Bram Moolenaar734a8672019-12-02 22:49:38 +01006991 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006992 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (buttonWidths == NULL)
6994 return -1;
6995
Bram Moolenaar734a8672019-12-02 22:49:38 +01006996 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006997 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (buttonPositions == NULL)
6999 return -1;
7000
7001 /*
7002 * Calculate how big the dialog must be.
7003 */
7004 hwnd = GetDesktopWindow();
7005 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007006# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7008 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007009 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 use_lfSysmenu = TRUE;
7011 }
7012 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007015 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (s_usenewlook)
7017 {
7018 oldFont = SelectFont(hdc, font);
7019 dlgPaddingX = DLG_PADDING_X;
7020 dlgPaddingY = DLG_PADDING_Y;
7021 }
7022 else
7023 {
7024 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7025 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7026 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7027 }
7028 GetTextMetrics(hdc, &fontInfo);
7029 fontHeight = fontInfo.tmHeight;
7030
Bram Moolenaar734a8672019-12-02 22:49:38 +01007031 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007032 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
Bram Moolenaar734a8672019-12-02 22:49:38 +01007034 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007035 if (s_hwnd == NULL)
7036 {
7037 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
Bram Moolenaar734a8672019-12-02 22:49:38 +01007039 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007040 get_work_area(&workarea_rect);
7041 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007042 if (maxDialogWidth > adjust_by_system_dpi(600))
7043 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007044 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007045 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007046 }
7047 else
7048 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007049 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007050 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007051 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007052 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7053 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7054 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7055 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007056
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007057 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007058 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7059 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7060 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7061 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7062 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007063 }
7064
Bram Moolenaar734a8672019-12-02 22:49:38 +01007065 // Set dlgwidth to width of message.
7066 // Copy the message into "ga", changing NL to CR-NL and inserting line
7067 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 pstart = message;
7069 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007070 msgheight = 0;
7071 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 do
7073 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007074 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007075
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076 // Need to figure out where to break the string. The system does it
7077 // at a word boundary, which would mean we can't compute the number of
7078 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007079 textWidth = 0;
7080 last_white = NULL;
7081 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007082 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007083 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007084 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007085 && textWidth > maxDialogWidth * 3 / 4)
7086 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007087 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007088 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007089 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007090 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007091 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007092 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007093 textWidth = 0;
7094
7095 if (last_white != NULL)
7096 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007097 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007098 if (pend > last_white)
7099 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007100 pend = last_white + 1;
7101 last_white = NULL;
7102 }
7103 ga_append(&ga, '\r');
7104 ga_append(&ga, '\n');
7105 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007106 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007107
7108 while (--l >= 0)
7109 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007110 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007111 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007113
7114 ga_append(&ga, '\r');
7115 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 pstart = pend + 1;
7117 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007118
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007119 if (ga.ga_data != NULL)
7120 message = ga.ga_data;
7121
Bram Moolenaar734a8672019-12-02 22:49:38 +01007122 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007123
K.Takatac81e9bf2022-01-16 14:15:49 +00007124 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7125 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
K.Takatac81e9bf2022-01-16 14:15:49 +00007127 // Add width of icon to dlgwidth, and some space
7128 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7129 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7130
7131 if (msgheight < dlg_icon_height)
7132 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133
7134 /*
7135 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007136 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007138 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (!vertical)
7140 {
7141 // Place buttons horizontally if they fit.
7142 horizWidth = dlgPaddingX;
7143 pstart = tbuffer;
7144 i = 0;
7145 do
7146 {
7147 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7148 if (pend == NULL)
7149 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007150 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (textWidth < minButtonWidth)
7152 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007153 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 buttonWidths[i] = textWidth;
7155 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007156 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 pstart = pend + 1;
7158 } while (*pend != NUL);
7159
7160 if (horizWidth > maxDialogWidth)
7161 vertical = TRUE; // Too wide to fit on the screen.
7162 else if (horizWidth > dlgwidth)
7163 dlgwidth = horizWidth;
7164 }
7165
7166 if (vertical)
7167 {
7168 // Stack buttons vertically.
7169 pstart = tbuffer;
7170 do
7171 {
7172 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7173 if (pend == NULL)
7174 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007175 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007176 textWidth += dlgPaddingX; // Padding within button
7177 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 if (textWidth > dlgwidth)
7179 dlgwidth = textWidth;
7180 pstart = pend + 1;
7181 } while (*pend != NUL);
7182 }
7183
7184 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007185 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 if (s_usenewlook)
7189 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7190 else
7191 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7192
7193 add_long(lStyle);
7194 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007195 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 add_word(0); // NumberOfItems(will change later)
7197 add_word(10); // x
7198 add_word(10); // y
7199 add_word(PixelToDialogX(dlgwidth)); // cx
7200
7201 // Dialog height.
7202 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007203 dlgheight = msgheight + 2 * dlgPaddingY
7204 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 else
7206 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7207
7208 // Dialog needs to be taller if contains an edit box.
7209 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7210 if (textfield != NULL)
7211 dlgheight += editboxheight;
7212
Bram Moolenaar734a8672019-12-02 22:49:38 +01007213 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007214 if (dlgheight > maxDialogHeight)
7215 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007216 msgheight = msgheight - (dlgheight - maxDialogHeight);
7217 dlgheight = maxDialogHeight;
7218 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007219 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007220 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007221 }
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 add_word(PixelToDialogY(dlgheight));
7224
7225 add_word(0); // Menu
7226 add_word(0); // Class
7227
Bram Moolenaar734a8672019-12-02 22:49:38 +01007228 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007229 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7230 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 p += nchar;
7232
7233 if (s_usenewlook)
7234 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007235 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007236# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (use_lfSysmenu)
7238 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007239 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7241 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007242 wcscpy(p, lfSysmenu.lfFaceName);
7243 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
7248 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007249 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251 p += nchar;
7252 }
7253
7254 buttonYpos = msgheight + 2 * dlgPaddingY;
7255
7256 if (textfield != NULL)
7257 buttonYpos += editboxheight;
7258
7259 pstart = tbuffer;
7260 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007261 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 for (i = 0; i < numButtons; i++)
7263 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007264 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 for ( pend = pstart;
7266 *pend && (*pend != DLG_BUTTON_SEP);
7267 pend++)
7268 ;
7269
7270 if (*pend)
7271 *pend = '\0';
7272
7273 /*
7274 * old NOTE:
7275 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7276 * the focus to the first tab-able button and in so doing makes that
7277 * the default!! Grrr. Workaround: Make the default button the only
7278 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7279 * he/she can use arrow keys.
7280 *
7281 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007282 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 * dialog. Also needed for when the textfield is the default control.
7284 * It appears to work now (perhaps not on Win95?).
7285 */
7286 if (vertical)
7287 {
7288 p = add_dialog_element(p,
7289 (i == dfltbutton
7290 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7291 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007292 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 + 2 * fontHeight * i),
7294 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7295 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007296 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 }
7298 else
7299 {
7300 p = add_dialog_element(p,
7301 (i == dfltbutton
7302 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7303 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007304 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 PixelToDialogX(buttonWidths[i]),
7306 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007307 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007309 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 }
7311 *pnumitems += numButtons;
7312
Bram Moolenaar734a8672019-12-02 22:49:38 +01007313 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 p = add_dialog_element(p, SS_ICON,
7315 PixelToDialogX(dlgPaddingX),
7316 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007317 PixelToDialogX(dlg_icon_width),
7318 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7320 dlg_icons[type]);
7321
Bram Moolenaar734a8672019-12-02 22:49:38 +01007322 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007323 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007324 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007325 PixelToDialogY(dlgPaddingY),
7326 (WORD)(PixelToDialogX(messageWidth) + 1),
7327 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007328 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaar734a8672019-12-02 22:49:38 +01007330 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (textfield != NULL)
7332 {
7333 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7334 PixelToDialogX(2 * dlgPaddingX),
7335 PixelToDialogY(2 * dlgPaddingY + msgheight),
7336 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7337 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007338 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 *pnumitems += 1;
7340 }
7341
7342 *pnumitems += 2;
7343
7344 SelectFont(hdc, oldFont);
7345 DeleteObject(font);
7346 ReleaseDC(hwnd, hdc);
7347
Bram Moolenaar734a8672019-12-02 22:49:38 +01007348 // Let the dialog_callback() function know which button to make default
7349 // If we have an edit box, make that the default. We also need to tell
7350 // dialog_callback() if this dialog contains an edit box or not. We do
7351 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (textfield != NULL)
7353 {
7354 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7355 s_textfield = textfield;
7356 }
7357 else
7358 {
7359 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7360 s_textfield = NULL;
7361 }
7362
Bram Moolenaar734a8672019-12-02 22:49:38 +01007363 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007365 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 (LPDLGTEMPLATE)pdlgtemplate,
7367 s_hwnd,
7368 (DLGPROC)dialog_callback);
7369
7370 LocalFree(LocalHandle(pdlgtemplate));
7371 vim_free(tbuffer);
7372 vim_free(buttonWidths);
7373 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007374 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar734a8672019-12-02 22:49:38 +01007376 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 (void)SetFocus(s_hwnd);
7378
7379 return nchar;
7380}
7381
Bram Moolenaar734a8672019-12-02 22:49:38 +01007382#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007383
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384/*
7385 * Put a simple element (basic class) onto a dialog template in memory.
7386 * return a pointer to where the next item should be added.
7387 *
7388 * parameters:
7389 * lStyle = additional style flags
7390 * (be careful, NT3.51 & Win32s will ignore the new ones)
7391 * x,y = x & y positions IN DIALOG UNITS
7392 * w,h = width and height IN DIALOG UNITS
7393 * Id = ID used in messages
7394 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7395 * caption = usually text or resource name
7396 *
7397 * TODO: use the length information noted here to enable the dialog creation
7398 * routines to work out more exactly how much memory they need to alloc.
7399 */
7400 static PWORD
7401add_dialog_element(
7402 PWORD p,
7403 DWORD lStyle,
7404 WORD x,
7405 WORD y,
7406 WORD w,
7407 WORD h,
7408 WORD Id,
7409 WORD clss,
7410 const char *caption)
7411{
7412 int nchar;
7413
Bram Moolenaar734a8672019-12-02 22:49:38 +01007414 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7416 *p++ = LOWORD(lStyle);
7417 *p++ = HIWORD(lStyle);
7418 *p++ = 0; // LOWORD (lExtendedStyle)
7419 *p++ = 0; // HIWORD (lExtendedStyle)
7420 *p++ = x;
7421 *p++ = y;
7422 *p++ = w;
7423 *p++ = h;
7424 *p++ = Id; //9 or 10 words in all
7425
7426 *p++ = (WORD)0xffff;
7427 *p++ = clss; //2 more here
7428
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007429 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 p += nchar;
7431
7432 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7433
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007434 return p; // total = 15 + strlen(caption) words
7435 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436}
7437
7438
7439/*
7440 * Helper routine. Take an input pointer, return closest pointer that is
7441 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7442 */
7443 static LPWORD
7444lpwAlign(
7445 LPWORD lpIn)
7446{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007447 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007449 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 ul += 3;
7451 ul >>= 2;
7452 ul <<= 2;
7453 return (LPWORD)ul;
7454}
7455
7456/*
7457 * Helper routine. Takes second parameter as Ansi string, copies it to first
7458 * parameter as wide character (16-bits / char) string, and returns integer
7459 * number of wide characters (words) in string (including the trailing wide
7460 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007461 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7462 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 static int
7464nCopyAnsiToWideChar(
7465 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007466 LPSTR lpAnsiIn,
7467 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int i;
7472 WCHAR *wn;
7473
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007474 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007476 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007477 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (wn != NULL)
7479 {
7480 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007481 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 vim_free(wn);
7483 }
7484 }
7485 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007486 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 nChar = MultiByteToWideChar(
7488 enc_codepage > 0 ? enc_codepage : CP_ACP,
7489 MB_PRECOMPOSED,
7490 lpAnsiIn, len,
7491 lpWCStr, len);
7492 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007493 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
7496 return nChar;
7497}
7498
7499
7500#ifdef FEAT_TEAROFF
7501/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007502 * Lookup menu handle from "menu_id".
7503 */
7504 static HMENU
7505tearoff_lookup_menuhandle(
7506 vimmenu_T *menu,
7507 WORD menu_id)
7508{
7509 for ( ; menu != NULL; menu = menu->next)
7510 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007511 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007512 continue;
7513 if (menu_is_separator(menu->dname))
7514 continue;
7515 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7516 return menu->submenu_id;
7517 }
7518 return NULL;
7519}
7520
7521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 * The callback function for all the modeless dialogs that make up the
7523 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7524 * thinking its menus have been clicked), and go away when closed.
7525 */
7526 static LRESULT CALLBACK
7527tearoff_callback(
7528 HWND hwnd,
7529 UINT message,
7530 WPARAM wParam,
7531 LPARAM lParam)
7532{
7533 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007534 {
7535 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
Bram Moolenaar734a8672019-12-02 22:49:38 +01007539 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 HandleMouseHide(message, lParam);
7541
7542 if (message == WM_COMMAND)
7543 {
7544 if ((WORD)(LOWORD(wParam)) & 0x8000)
7545 {
7546 POINT mp;
7547 RECT rect;
7548
7549 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7550 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007551 vimmenu_T *menu;
7552
7553 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007555 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7557 (int)rect.right - 8,
7558 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007559 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 s_hwnd,
7561 NULL);
7562 /*
7563 * NOTE: The pop-up menu can eat the mouse up event.
7564 * We deal with this in normal.c.
7565 */
7566 }
7567 }
7568 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007569 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7571 /*
7572 * Give main window the focus back: this is so after
7573 * choosing a tearoff button you can start typing again
7574 * straight away.
7575 */
7576 (void)SetFocus(s_hwnd);
7577 return TRUE;
7578 }
7579 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7580 {
7581 DestroyWindow(hwnd);
7582 return TRUE;
7583 }
7584
Bram Moolenaar734a8672019-12-02 22:49:38 +01007585 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (message == WM_EXITSIZEMOVE)
7587 (void)SetActiveWindow(s_hwnd);
7588
7589 return FALSE;
7590}
7591#endif
7592
7593
7594/*
7595 * Decide whether to use the "new look" (small, non-bold font) or the "old
7596 * look" (big, clanky font) for dialogs, and work out a few values for use
7597 * later accordingly.
7598 */
7599 static void
7600get_dialog_font_metrics(void)
7601{
7602 HDC hdc;
7603 HFONT hfontTools = 0;
7604 DWORD dlgFontSize;
7605 SIZE size;
7606#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007607 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608#endif
7609
7610 s_usenewlook = FALSE;
7611
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007613 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007614 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616#endif
7617 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007618 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007620 if (hfontTools)
7621 {
7622 hdc = GetDC(s_hwnd);
7623 SelectObject(hdc, hfontTools);
7624 /*
7625 * GetTextMetrics() doesn't return the right value in
7626 * tmAveCharWidth, so we have to figure out the dialog base units
7627 * ourselves.
7628 */
7629 GetTextExtentPoint(hdc,
7630 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7631 52, &size);
7632 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007634 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7635 s_dlgfntheight = (WORD)size.cy;
7636 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
7638
7639 if (!s_usenewlook)
7640 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007641 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 s_dlgfntwidth = LOWORD(dlgFontSize);
7643 s_dlgfntheight = HIWORD(dlgFontSize);
7644 }
7645}
7646
7647#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7648/*
7649 * Create a pseudo-"tearoff menu" based on the child
7650 * items of a given menu pointer.
7651 */
7652 static void
7653gui_mch_tearoff(
7654 char_u *title,
7655 vimmenu_T *menu,
7656 int initX,
7657 int initY)
7658{
7659 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7660 int template_len;
7661 int nchar, textWidth, submenuWidth;
7662 DWORD lStyle;
7663 DWORD lExtendedStyle;
7664 WORD dlgwidth;
7665 WORD menuID;
7666 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007667 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 vimmenu_T *the_menu = menu;
7669 HWND hwnd;
7670 HDC hdc;
7671 HFONT font, oldFont;
7672 int col, spaceWidth, len;
7673 int columnWidths[2];
7674 char_u *label, *text;
7675 int acLen = 0;
7676 int nameLen;
7677 int padding0, padding1, padding2 = 0;
7678 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007679 int x;
7680 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007681# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007682 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
7686 /*
7687 * If this menu is already torn off, move it to the mouse position.
7688 */
7689 if (IsWindow(menu->tearoff_handle))
7690 {
7691 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007692 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
7694 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7695 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7696 }
7697 return;
7698 }
7699
7700 /*
7701 * Create a new tearoff.
7702 */
7703 if (*title == MNU_HIDDEN_CHAR)
7704 title++;
7705
Bram Moolenaar734a8672019-12-02 22:49:38 +01007706 // Allocate memory to store the dialog template. It's made bigger when
7707 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 template_len = DLG_ALLOC_SIZE;
7709 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7710 if (p == NULL)
7711 return;
7712
7713 hwnd = GetDesktopWindow();
7714 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007715# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7717 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007718 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 use_lfSysmenu = TRUE;
7720 }
7721 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007724 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (s_usenewlook)
7726 oldFont = SelectFont(hdc, font);
7727 else
7728 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7729
Bram Moolenaar734a8672019-12-02 22:49:38 +01007730 // Calculate width of a single space. Used for padding columns to the
7731 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007732 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 // Figure out max width of the text column, the accelerator column and the
7735 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 submenuWidth = 0;
7737 for (col = 0; col < 2; col++)
7738 {
7739 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007740 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007742 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 text = (col == 0) ? pmenu->dname : pmenu->actext;
7744 if (text != NULL && *text != NUL)
7745 {
7746 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7747 if (textWidth > columnWidths[col])
7748 columnWidths[col] = textWidth;
7749 }
7750 if (pmenu->children != NULL)
7751 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7752 }
7753 }
7754 if (columnWidths[1] == 0)
7755 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007756 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 if (submenuWidth != 0)
7758 columnWidths[0] += submenuWidth;
7759 else
7760 columnWidths[0] += spaceWidth;
7761 }
7762 else
7763 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007764 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7766 columnWidths[1] += submenuWidth;
7767 }
7768
7769 /*
7770 * Now find the total width of our 'menu'.
7771 */
7772 textWidth = columnWidths[0] + columnWidths[1];
7773 if (submenuWidth != 0)
7774 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007775 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7777 textWidth += submenuWidth;
7778 }
7779 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7780 if (textWidth > dlgwidth)
7781 dlgwidth = textWidth;
7782 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7783
Bram Moolenaar734a8672019-12-02 22:49:38 +01007784 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (s_usenewlook)
7786 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7787 else
7788 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7789
7790 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7791 *p++ = LOWORD(lStyle);
7792 *p++ = HIWORD(lStyle);
7793 *p++ = LOWORD(lExtendedStyle);
7794 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007795 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007797 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007799 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801 *p++ = PixelToDialogX(initX); // x
7802 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007803 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 else
7805 *p++ = PixelToDialogY(initY); // y
7806 *p++ = PixelToDialogX(dlgwidth); // cx
7807 ptrueheight = p;
7808 *p++ = 0; // dialog height: changed later anyway
7809 *p++ = 0; // Menu
7810 *p++ = 0; // Class
7811
Bram Moolenaar734a8672019-12-02 22:49:38 +01007812 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007814 ? (LPSTR)title
7815 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 p += nchar;
7817
7818 if (s_usenewlook)
7819 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007820 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007821# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (use_lfSysmenu)
7823 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007824 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7826 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007827 wcscpy(p, lfSysmenu.lfFaceName);
7828 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 {
7833 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007834 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836 p += nchar;
7837 }
7838
7839 /*
7840 * Loop over all the items in the menu.
7841 * But skip over the tearbar.
7842 */
7843 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7844 menu = menu->children->next;
7845 else
7846 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007847 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 for ( ; menu != NULL; menu = menu->next)
7849 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007850 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 continue;
7852 if (menu_is_separator(menu->dname))
7853 {
7854 sepPadding += 3;
7855 continue;
7856 }
7857
Bram Moolenaar734a8672019-12-02 22:49:38 +01007858 // Check if there still is plenty of room in the template. Make it
7859 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7861 {
7862 WORD *newp;
7863
7864 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7865 if (newp != NULL)
7866 {
7867 template_len += 4096;
7868 mch_memmove(newp, pdlgtemplate,
7869 (char *)p - (char *)pdlgtemplate);
7870 p = newp + (p - pdlgtemplate);
7871 pnumitems = newp + (pnumitems - pdlgtemplate);
7872 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7873 LocalFree(LocalHandle(pdlgtemplate));
7874 pdlgtemplate = newp;
7875 }
7876 }
7877
Bram Moolenaar734a8672019-12-02 22:49:38 +01007878 // Figure out minimal length of this menu label. Use "name" for the
7879 // actual text, "dname" for estimating the displayed size. "name"
7880 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 len = nameLen = (int)STRLEN(menu->name);
7882 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7883 (int)STRLEN(menu->dname))) / spaceWidth;
7884 len += padding0;
7885
7886 if (menu->actext != NULL)
7887 {
7888 acLen = (int)STRLEN(menu->actext);
7889 len += acLen;
7890 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7891 }
7892 else
7893 textWidth = 0;
7894 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7895 len += padding1;
7896
7897 if (menu->children == NULL)
7898 {
7899 padding2 = submenuWidth / spaceWidth;
7900 len += padding2;
7901 menuID = (WORD)(menu->id);
7902 }
7903 else
7904 {
7905 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007906 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 }
7908
Bram Moolenaar734a8672019-12-02 22:49:38 +01007909 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007910 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 if (label == NULL)
7912 break;
7913
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007914 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007915 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007917 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 while (padding0-- > 0)
7919 *text++ = ' ';
7920 if (menu->actext != NULL)
7921 {
7922 STRNCPY(text, menu->actext, acLen);
7923 text += acLen;
7924 }
7925 while (padding1-- > 0)
7926 *text++ = ' ';
7927 if (menu->children != NULL)
7928 {
7929 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7930 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7931 }
7932 else
7933 {
7934 while (padding2-- > 0)
7935 *text++ = ' ';
7936 }
7937 *text = NUL;
7938
7939 /*
7940 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7941 * W95/NT4 it makes the tear-off look more like a menu.
7942 */
7943 p = add_dialog_element(p,
7944 BS_PUSHBUTTON|BS_LEFT,
7945 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7946 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7947 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7948 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007949 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 vim_free(label);
7951 (*pnumitems)++;
7952 }
7953
7954 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7955
7956
Bram Moolenaar734a8672019-12-02 22:49:38 +01007957 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007958 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007959 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 (LPDLGTEMPLATE)pdlgtemplate,
7961 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007962 (DLGPROC)tearoff_callback,
7963 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
7965 LocalFree(LocalHandle(pdlgtemplate));
7966 SelectFont(hdc, oldFont);
7967 DeleteObject(font);
7968 ReleaseDC(hwnd, hdc);
7969
7970 /*
7971 * Reassert ourselves as the active window. This is so that after creating
7972 * a tearoff, the user doesn't have to click with the mouse just to start
7973 * typing again!
7974 */
7975 (void)SetActiveWindow(s_hwnd);
7976
Bram Moolenaar734a8672019-12-02 22:49:38 +01007977 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 force_menu_update = TRUE;
7979}
7980#endif
7981
7982#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007983# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
Bram Moolenaar734a8672019-12-02 22:49:38 +01007985// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986# ifndef TBSTYLE_FLAT
7987# define TBSTYLE_FLAT 0x0800
7988# endif
7989
7990/*
7991 * Create the toolbar, initially unpopulated.
7992 * (just like the menu, there are no defaults, it's all
7993 * set up through menu.vim)
7994 */
7995 static void
7996initialise_toolbar(void)
7997{
7998 InitCommonControls();
7999 s_toolbarhwnd = CreateToolbarEx(
8000 s_hwnd,
8001 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8002 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008003 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008004 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 IDR_TOOLBAR1, // id of initial bitmap
8006 NULL,
8007 0, // initial number of buttons
8008 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8009 TOOLBAR_BUTTON_HEIGHT,
8010 TOOLBAR_BUTTON_WIDTH,
8011 TOOLBAR_BUTTON_HEIGHT,
8012 sizeof(TBBUTTON)
8013 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008014
8015 // Remove transparency from the toolbar to prevent the main window
8016 // background colour showing through
8017 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8018 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8019
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008020 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008023
8024 update_toolbar_size();
8025}
8026
8027 static void
8028update_toolbar_size(void)
8029{
8030 int w, h;
8031 TBMETRICS tbm = {sizeof(TBMETRICS)};
8032
8033 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8034 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8035 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8036 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8037
8038 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8039 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8040 //TRACE("button size: %d, %d", w, h);
8041 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8042 gui.toolbar_height = h + 6;
8043
8044 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8045 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8046
8047 // TODO:
8048 // Currently, this function only updates the size of toolbar buttons.
8049 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
8051
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008052 static LRESULT CALLBACK
8053toolbar_wndproc(
8054 HWND hwnd,
8055 UINT uMsg,
8056 WPARAM wParam,
8057 LPARAM lParam)
8058{
8059 HandleMouseHide(uMsg, lParam);
8060 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8061}
8062
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 static int
8064get_toolbar_bitmap(vimmenu_T *menu)
8065{
8066 int i = -1;
8067
8068 /*
8069 * Check user bitmaps first, unless builtin is specified.
8070 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008071 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {
8073 char_u fname[MAXPATHL];
8074 HANDLE hbitmap = NULL;
8075
8076 if (menu->iconfile != NULL)
8077 {
8078 gui_find_iconfile(menu->iconfile, fname, "bmp");
8079 hbitmap = LoadImage(
8080 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008081 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 IMAGE_BITMAP,
8083 TOOLBAR_BUTTON_WIDTH,
8084 TOOLBAR_BUTTON_HEIGHT,
8085 LR_LOADFROMFILE |
8086 LR_LOADMAP3DCOLORS
8087 );
8088 }
8089
8090 /*
8091 * If the LoadImage call failed, or the "icon=" file
8092 * didn't exist or wasn't specified, try the menu name
8093 */
8094 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008095 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008096# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008097 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008098# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008099 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 hbitmap = LoadImage(
8101 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008102 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 IMAGE_BITMAP,
8104 TOOLBAR_BUTTON_WIDTH,
8105 TOOLBAR_BUTTON_HEIGHT,
8106 LR_LOADFROMFILE |
8107 LR_LOADMAP3DCOLORS
8108 );
8109
8110 if (hbitmap != NULL)
8111 {
8112 TBADDBITMAP tbAddBitmap;
8113
8114 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008115 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116
8117 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8118 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008119 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 }
8121 }
8122 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8123 i = menu->iconidx;
8124
8125 return i;
8126}
8127#endif
8128
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008129#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8130 static void
8131initialise_tabline(void)
8132{
8133 InitCommonControls();
8134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008135 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008136 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008137 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008138 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008139 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008140
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008141 gui.tabline_height = TABLINE_HEIGHT;
8142
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008143 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008144}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008145
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008146/*
8147 * Get tabpage_T from POINT.
8148 */
8149 static tabpage_T *
8150GetTabFromPoint(
8151 HWND hWnd,
8152 POINT pt)
8153{
8154 tabpage_T *ptp = NULL;
8155
8156 if (gui_mch_showing_tabline())
8157 {
8158 TCHITTESTINFO htinfo;
8159 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008160 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008161 if (s_tabhwnd == hWnd)
8162 {
8163 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8164 if (idx != -1)
8165 ptp = find_tabpage(idx + 1);
8166 }
8167 }
8168 return ptp;
8169}
8170
8171static POINT s_pt = {0, 0};
8172static HCURSOR s_hCursor = NULL;
8173
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008174 static LRESULT CALLBACK
8175tabline_wndproc(
8176 HWND hwnd,
8177 UINT uMsg,
8178 WPARAM wParam,
8179 LPARAM lParam)
8180{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008181 POINT pt;
8182 tabpage_T *tp;
8183 RECT rect;
8184 int nCenter;
8185 int idx0;
8186 int idx1;
8187
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008188 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008189
8190 switch (uMsg)
8191 {
8192 case WM_LBUTTONDOWN:
8193 {
8194 s_pt.x = GET_X_LPARAM(lParam);
8195 s_pt.y = GET_Y_LPARAM(lParam);
8196 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008197 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008198 break;
8199 }
8200 case WM_MOUSEMOVE:
8201 if (GetCapture() == hwnd
8202 && ((wParam & MK_LBUTTON)) != 0)
8203 {
8204 pt.x = GET_X_LPARAM(lParam);
8205 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008206 if (abs(pt.x - s_pt.x) >
8207 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008208 {
8209 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8210
8211 tp = GetTabFromPoint(hwnd, pt);
8212 if (tp != NULL)
8213 {
8214 idx0 = tabpage_index(curtab) - 1;
8215 idx1 = tabpage_index(tp) - 1;
8216
8217 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8218 nCenter = rect.left + (rect.right - rect.left) / 2;
8219
Bram Moolenaar734a8672019-12-02 22:49:38 +01008220 // Check if the mouse cursor goes over the center of
8221 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008222 if ((idx0 < idx1) && (nCenter < pt.x))
8223 {
8224 tabpage_move(idx1 + 1);
8225 update_screen(0);
8226 }
8227 else if ((idx1 < idx0) && (pt.x < nCenter))
8228 {
8229 tabpage_move(idx1);
8230 update_screen(0);
8231 }
8232 }
8233 }
8234 }
8235 break;
8236 case WM_LBUTTONUP:
8237 {
8238 if (GetCapture() == hwnd)
8239 {
8240 SetCursor(s_hCursor);
8241 ReleaseCapture();
8242 }
8243 break;
8244 }
8245 default:
8246 break;
8247 }
8248
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008249 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8250}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008251#endif
8252
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8254/*
8255 * Make the GUI window come to the foreground.
8256 */
8257 void
8258gui_mch_set_foreground(void)
8259{
8260 if (IsIconic(s_hwnd))
8261 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8262 SetForegroundWindow(s_hwnd);
8263}
8264#endif
8265
8266#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8267 static void
8268dyn_imm_load(void)
8269{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008270 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (hLibImm == NULL)
8272 return;
8273
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 pImmGetCompositionStringW
8275 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8276 pImmGetContext
8277 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8278 pImmAssociateContext
8279 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8280 pImmReleaseContext
8281 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8282 pImmGetOpenStatus
8283 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8284 pImmSetOpenStatus
8285 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008286 pImmGetCompositionFontW
8287 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8288 pImmSetCompositionFontW
8289 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 pImmSetCompositionWindow
8291 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8292 pImmGetConversionStatus
8293 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008294 pImmSetConversionStatus
8295 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296
K.Takatab0b2b732022-01-19 12:59:21 +00008297 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 || pImmGetContext == NULL
8299 || pImmAssociateContext == NULL
8300 || pImmReleaseContext == NULL
8301 || pImmGetOpenStatus == NULL
8302 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008303 || pImmGetCompositionFontW == NULL
8304 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008306 || pImmGetConversionStatus == NULL
8307 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {
8309 FreeLibrary(hLibImm);
8310 hLibImm = NULL;
8311 pImmGetContext = NULL;
8312 return;
8313 }
8314
8315 return;
8316}
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318#endif
8319
8320#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8321
8322# ifdef FEAT_XPM_W32
8323# define IMAGE_XPM 100
8324# endif
8325
8326typedef struct _signicon_t
8327{
8328 HANDLE hImage;
8329 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008330# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008331 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333} signicon_t;
8334
8335 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008336gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
8338 signicon_t *sign;
8339 int x, y, w, h;
8340
8341 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8342 return;
8343
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008344# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008345 if (IS_ENABLE_DIRECTX())
8346 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008347# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 x = TEXT_X(col);
8350 y = TEXT_Y(row);
8351 w = gui.char_width * 2;
8352 h = gui.char_height;
8353 switch (sign->uType)
8354 {
8355 case IMAGE_BITMAP:
8356 {
8357 HDC hdcMem;
8358 HBITMAP hbmpOld;
8359
8360 hdcMem = CreateCompatibleDC(s_hdc);
8361 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8362 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8363 SelectObject(hdcMem, hbmpOld);
8364 DeleteDC(hdcMem);
8365 }
8366 break;
8367 case IMAGE_ICON:
8368 case IMAGE_CURSOR:
8369 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
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 {
8374 HDC hdcMem;
8375 HBITMAP hbmpOld;
8376
8377 hdcMem = CreateCompatibleDC(s_hdc);
8378 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008379 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8381
8382 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008383 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8385 SelectObject(hdcMem, hbmpOld);
8386 DeleteDC(hdcMem);
8387 }
8388 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 }
8391}
8392
8393 static void
8394close_signicon_image(signicon_t *sign)
8395{
8396 if (sign)
8397 switch (sign->uType)
8398 {
8399 case IMAGE_BITMAP:
8400 DeleteObject((HGDIOBJ)sign->hImage);
8401 break;
8402 case IMAGE_CURSOR:
8403 DestroyCursor((HCURSOR)sign->hImage);
8404 break;
8405 case IMAGE_ICON:
8406 DestroyIcon((HICON)sign->hImage);
8407 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008408# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 case IMAGE_XPM:
8410 DeleteObject((HBITMAP)sign->hImage);
8411 DeleteObject((HBITMAP)sign->hShape);
8412 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415}
8416
8417 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008418gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 signicon_t sign, *psign;
8421 char_u *ext;
8422
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008424 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 if (ext > signfile)
8426 {
8427 int do_load = 1;
8428
8429 if (!STRICMP(ext, ".bmp"))
8430 sign.uType = IMAGE_BITMAP;
8431 else if (!STRICMP(ext, ".ico"))
8432 sign.uType = IMAGE_ICON;
8433 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8434 sign.uType = IMAGE_CURSOR;
8435 else
8436 do_load = 0;
8437
8438 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008439 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 gui.char_width * 2, gui.char_height,
8441 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008442# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 if (!STRICMP(ext, ".xpm"))
8444 {
8445 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008446 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8447 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451
8452 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008453 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 *psign = sign;
8455
8456 if (!psign)
8457 {
8458 if (sign.hImage)
8459 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008460 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
8462 return (void *)psign;
8463
8464}
8465
8466 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008467gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 if (sign)
8470 {
8471 close_signicon_image((signicon_t *)sign);
8472 vim_free(sign);
8473 }
8474}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008477#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
Bram Moolenaar734a8672019-12-02 22:49:38 +01008479/*
8480 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008481 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008483 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8485 * to get current mouse position).
8486 *
8487 * Trying to use as more Windows services as possible, and as less
8488 * IE version as possible :)).
8489 *
8490 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8491 * BalloonEval struct.
8492 * 2) Enable/Disable simply create/kill BalloonEval Timer
8493 * 3) When there was enough inactivity, timer procedure posts
8494 * async request to debugger
8495 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8496 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008497 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 */
8499
Bram Moolenaar45360022005-07-21 21:08:21 +00008500/*
8501 * determine whether installed Common Controls support multiline tooltips
8502 * (i.e. their version is >= 4.70
8503 */
8504 int
8505multiline_balloon_available(void)
8506{
8507 HINSTANCE hDll;
8508 static char comctl_dll[] = "comctl32.dll";
8509 static int multiline_tip = MAYBE;
8510
8511 if (multiline_tip != MAYBE)
8512 return multiline_tip;
8513
8514 hDll = GetModuleHandle(comctl_dll);
8515 if (hDll != NULL)
8516 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008517 DLLGETVERSIONPROC pGetVer;
8518 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008519
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008520 if (pGetVer != NULL)
8521 {
8522 DLLVERSIONINFO dvi;
8523 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008524
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008525 ZeroMemory(&dvi, sizeof(dvi));
8526 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008527
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008528 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008529
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008530 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008531 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008532 || (dvi.dwMajorVersion == 4
8533 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008534 {
8535 multiline_tip = TRUE;
8536 return multiline_tip;
8537 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008538 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008539 else
8540 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008541 // there is chance we have ancient CommCtl 4.70
8542 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008543 DWORD dwHandle = 0;
8544 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8545 if (len > 0)
8546 {
8547 VS_FIXEDFILEINFO *ver;
8548 UINT vlen = 0;
8549 void *data = alloc(len);
8550
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008551 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008552 && GetFileVersionInfo(comctl_dll, 0, len, data)
8553 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8554 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008555 && HIWORD(ver->dwFileVersionMS) > 4)
8556 || ((HIWORD(ver->dwFileVersionMS) == 4
8557 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008558 {
8559 vim_free(data);
8560 multiline_tip = TRUE;
8561 return multiline_tip;
8562 }
8563 vim_free(data);
8564 }
8565 }
8566 }
8567 multiline_tip = FALSE;
8568 return multiline_tip;
8569}
8570
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008571 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008572make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008573{
8574 TOOLINFOW *pti;
8575 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008576
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008577 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008578 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8579 else
8580 ToolInfoSize = sizeof(TOOLINFOW);
8581
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008582 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008583 if (pti == NULL)
8584 return;
8585
8586 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8587 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8588 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008589 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008590
8591 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8592 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8593
8594 pti->cbSize = ToolInfoSize;
8595 pti->uFlags = TTF_SUBCLASS;
8596 pti->hwnd = beval->target;
8597 pti->hinst = 0; // Don't use string resources
8598 pti->uId = ID_BEVAL_TOOLTIP;
8599
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008600 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008601 {
8602 RECT rect;
8603 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8604 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008605 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8606 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008607 // switch multiline tooltips on
8608 if (GetClientRect(s_textArea, &rect))
8609 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8610 (LPARAM)rect.right);
8611 }
8612 else
8613 {
8614 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008615 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8616 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008617 }
8618
8619 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8620 pti->rect.left = pt.x - 3;
8621 pti->rect.top = pt.y - 3;
8622 pti->rect.right = pt.x + 3;
8623 pti->rect.bottom = pt.y + 3;
8624
8625 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8626 // Make tooltip appear sooner.
8627 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8628 // I've performed some tests and it seems the longest possible life time
8629 // of tooltip is 30 seconds.
8630 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8631 /*
8632 * HACK: force tooltip to appear, because it'll not appear until
8633 * first mouse move. D*mn M$
8634 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8635 */
8636 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8637 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8638 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008639}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008642delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008644 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645}
8646
8647 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008648BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008649 HWND hwnd UNUSED,
8650 UINT uMsg UNUSED,
8651 UINT_PTR idEvent UNUSED,
8652 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653{
8654 POINT pt;
8655 RECT rect;
8656
8657 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8658 return;
8659
8660 GetCursorPos(&pt);
8661 if (WindowFromPoint(pt) != s_textArea)
8662 return;
8663
8664 ScreenToClient(s_textArea, &pt);
8665 GetClientRect(s_textArea, &rect);
8666 if (!PtInRect(&rect, pt))
8667 return;
8668
8669 if (LastActivity > 0
8670 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8671 && (cur_beval->showState != ShS_PENDING
8672 || abs(cur_beval->x - pt.x) > 3
8673 || abs(cur_beval->y - pt.y) > 3))
8674 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008675 // Pointer resting in one place long enough, it's time to show
8676 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 cur_beval->showState = ShS_PENDING;
8678 cur_beval->x = pt.x;
8679 cur_beval->y = pt.y;
8680
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008681 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
8683 if (cur_beval->msgCB != NULL)
8684 (*cur_beval->msgCB)(cur_beval, 0);
8685 }
8686}
8687
8688 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008689gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008691 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008693 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694}
8695
8696 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008697gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008699 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 if (beval == NULL)
8701 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008702 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008703 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008704 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705}
8706
8707 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008708gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
8710 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008711
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008712 vim_free(beval->msg);
8713 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8714 if (beval->msg == NULL)
8715 {
8716 delete_tooltip(beval);
8717 beval->showState = ShS_NEUTRAL;
8718 return;
8719 }
8720
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008721 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 if (beval->showState == ShS_SHOWING)
8723 return;
8724 GetCursorPos(&pt);
8725 ScreenToClient(s_textArea, &pt);
8726
8727 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008729 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 gui_mch_disable_beval_area(cur_beval);
8731 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008732 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008734 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
8736
8737 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008738gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008739 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008740 char_u *mesg,
8741 void (*mesgCB)(BalloonEval *, int),
8742 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008744 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 BalloonEval *beval;
8746
8747 if (mesg != NULL && mesgCB != NULL)
8748 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008749 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 return NULL;
8751 }
8752
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008753 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 if (beval != NULL)
8755 {
8756 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
8758 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 beval->msg = mesg;
8760 beval->msgCB = mesgCB;
8761 beval->clientData = clientData;
8762
8763 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 cur_beval = beval;
8765
8766 if (p_beval)
8767 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
8769 return beval;
8770}
8771
8772 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008773Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008775 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 return;
8777
8778 if (cur_beval != NULL)
8779 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008780 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008782 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008783 // TRACE0("TTN_SHOW {{{");
8784 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008785 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008786 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008787 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 delete_tooltip(cur_beval);
8789 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008790 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
8792 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008793 break;
8794 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008795 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008796 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008797 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8798 info->lpszText = (LPSTR)info->lParam;
8799 info->uFlags |= TTF_DI_SETITEM;
8800 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008801 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008802 case TTN_GETDISPINFOW:
8803 {
8804 // if we get here then we have new common controls
8805 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8806 info->lpszText = (LPWSTR)info->lParam;
8807 info->uFlags |= TTF_DI_SETITEM;
8808 }
8809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 }
8811 }
8812}
8813
8814 static void
8815TrackUserActivity(UINT uMsg)
8816{
8817 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8818 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8819 LastActivity = GetTickCount();
8820}
8821
8822 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008823gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008825# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008826 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008827# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008828 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 vim_free(beval);
8830}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008831#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8834/*
8835 * We have multiple signs to draw at the same location. Draw the
8836 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8837 */
8838 void
8839netbeans_draw_multisign_indicator(int row)
8840{
8841 int i;
8842 int y;
8843 int x;
8844
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008845 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008846 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 x = 0;
8849 y = TEXT_Y(row);
8850
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008851# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008852 if (IS_ENABLE_DIRECTX())
8853 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008854# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008855
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 for (i = 0; i < gui.char_height - 3; i++)
8857 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8858
8859 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8860 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8861 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8862 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8863 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8864 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8865 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8866}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008867#endif