blob: 50ea8f9cd1a54e1fc9c1cf91437a0069a59237e3 [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
Bram Moolenaar734a8672019-12-02 22:49:38 +0100209#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100210#define DLG_VERT_PADDING_Y 4
211#define DLG_ICON_WIDTH 34
212#define DLG_ICON_HEIGHT 34
213#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000214#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100215#define DLG_FONT_POINT_SIZE 8
216#define DLG_MIN_MAX_WIDTH 400
217#define DLG_MIN_MAX_HEIGHT 400
218
Bram Moolenaar734a8672019-12-02 22:49:38 +0100219#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100220
Bram Moolenaar734a8672019-12-02 22:49:38 +0100221#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100222# define WM_XBUTTONDOWN 0x020B
223# define WM_XBUTTONUP 0x020C
224# define WM_XBUTTONDBLCLK 0x020D
225# define MK_XBUTTON1 0x0020
226# define MK_XBUTTON2 0x0040
227#endif
228
K.Takatac81e9bf2022-01-16 14:15:49 +0000229#ifndef WM_DPICHANGED
230# define WM_DPICHANGED 0x02E0
231#endif
232
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200243# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200244# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define _cdecl
246typedef int BOOL;
247typedef int BYTE;
248typedef int DWORD;
249typedef int WCHAR;
250typedef int ENUMLOGFONT;
251typedef int FINDREPLACE;
252typedef int HANDLE;
253typedef int HBITMAP;
254typedef int HBRUSH;
255typedef int HDROP;
256typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100257typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100258typedef int LPARAM;
259typedef int LPCREATESTRUCT;
260typedef int LPCSTR;
261typedef int LPCTSTR;
262typedef int LPRECT;
263typedef int LPSTR;
264typedef int LPWINDOWPOS;
265typedef int LPWORD;
266typedef int LRESULT;
267typedef int HRESULT;
268# undef MSG
269typedef int MSG;
270typedef int NEWTEXTMETRIC;
271typedef int OSVERSIONINFO;
272typedef int PWORD;
273typedef int RECT;
274typedef int UINT;
275typedef int WORD;
276typedef int WPARAM;
277typedef int POINT;
278typedef void *HINSTANCE;
279typedef void *HMENU;
280typedef void *HWND;
281typedef void *HDC;
282typedef void VOID;
283typedef int LPNMHDR;
284typedef int LONG;
285typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200286typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200287typedef int COLORREF;
288typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
K.Takata45f9cfb2022-01-21 11:11:00 +0000295static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000317static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
K.Takatac81e9bf2022-01-16 14:15:49 +0000355#ifndef _DPI_AWARENESS_CONTEXTS_
356typedef HANDLE DPI_AWARENESS_CONTEXT;
357
358typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000359 DPI_AWARENESS_INVALID = -1,
360 DPI_AWARENESS_UNAWARE = 0,
361 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000362 DPI_AWARENESS_PER_MONITOR_AWARE = 2
363} DPI_AWARENESS;
364
K.Takatab0b2b732022-01-19 12:59:21 +0000365# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
366# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000367# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
368# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
369# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
370#endif
371
372#define DEFAULT_DPI 96
373static int s_dpi = DEFAULT_DPI;
374static BOOL s_in_dpichanged = FALSE;
375static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
376
377static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
378static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
379static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
380//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
381static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
382static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
383
384 static UINT WINAPI
385stubGetDpiForSystem(void)
386{
387 HWND hwnd = GetDesktopWindow();
388 HDC hdc = GetWindowDC(hwnd);
389 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
390 ReleaseDC(hwnd, hdc);
391 return dpi;
392}
393
394 static int WINAPI
395stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
396{
397 return GetSystemMetrics(nIndex);
398}
399
400 static int
401adjust_fontsize_by_dpi(int size)
402{
403 return size * s_dpi / (int)pGetDpiForSystem();
404}
405
406 static int
407adjust_by_system_dpi(int size)
408{
409 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
410}
411
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100412#if defined(FEAT_DIRECTX)
413 static int
414directx_enabled(void)
415{
416 if (s_dwc != NULL)
417 return 1;
418 else if (s_directx_load_attempted)
419 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100420 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100421 DWrite_Init();
422 s_directx_load_attempted = 1;
423 s_dwc = DWriteContext_Open();
424 directx_binddc();
425 return s_dwc != NULL ? 1 : 0;
426}
427
428 static void
429directx_binddc(void)
430{
431 if (s_textArea != NULL)
432 {
433 RECT rect;
434 GetClientRect(s_textArea, &rect);
435 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
436 }
437}
438#endif
439
Bram Moolenaar734a8672019-12-02 22:49:38 +0100440extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100441
442static struct
443{
444 UINT key_sym;
445 char_u vim_code0;
446 char_u vim_code1;
447} special_keys[] =
448{
449 {VK_UP, 'k', 'u'},
450 {VK_DOWN, 'k', 'd'},
451 {VK_LEFT, 'k', 'l'},
452 {VK_RIGHT, 'k', 'r'},
453
454 {VK_F1, 'k', '1'},
455 {VK_F2, 'k', '2'},
456 {VK_F3, 'k', '3'},
457 {VK_F4, 'k', '4'},
458 {VK_F5, 'k', '5'},
459 {VK_F6, 'k', '6'},
460 {VK_F7, 'k', '7'},
461 {VK_F8, 'k', '8'},
462 {VK_F9, 'k', '9'},
463 {VK_F10, 'k', ';'},
464
465 {VK_F11, 'F', '1'},
466 {VK_F12, 'F', '2'},
467 {VK_F13, 'F', '3'},
468 {VK_F14, 'F', '4'},
469 {VK_F15, 'F', '5'},
470 {VK_F16, 'F', '6'},
471 {VK_F17, 'F', '7'},
472 {VK_F18, 'F', '8'},
473 {VK_F19, 'F', '9'},
474 {VK_F20, 'F', 'A'},
475
476 {VK_F21, 'F', 'B'},
477#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100478 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479#endif
480 {VK_F22, 'F', 'C'},
481 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100482 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100483
484 {VK_HELP, '%', '1'},
485 {VK_BACK, 'k', 'b'},
486 {VK_INSERT, 'k', 'I'},
487 {VK_DELETE, 'k', 'D'},
488 {VK_HOME, 'k', 'h'},
489 {VK_END, '@', '7'},
490 {VK_PRIOR, 'k', 'P'},
491 {VK_NEXT, 'k', 'N'},
492 {VK_PRINT, '%', '9'},
493 {VK_ADD, 'K', '6'},
494 {VK_SUBTRACT, 'K', '7'},
495 {VK_DIVIDE, 'K', '8'},
496 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100497 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100498 {VK_DECIMAL, 'K', 'B'},
499
500 {VK_NUMPAD0, 'K', 'C'},
501 {VK_NUMPAD1, 'K', 'D'},
502 {VK_NUMPAD2, 'K', 'E'},
503 {VK_NUMPAD3, 'K', 'F'},
504 {VK_NUMPAD4, 'K', 'G'},
505 {VK_NUMPAD5, 'K', 'H'},
506 {VK_NUMPAD6, 'K', 'I'},
507 {VK_NUMPAD7, 'K', 'J'},
508 {VK_NUMPAD8, 'K', 'K'},
509 {VK_NUMPAD9, 'K', 'L'},
510
Bram Moolenaar734a8672019-12-02 22:49:38 +0100511 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100512 {VK_SPACE, ' ', NUL},
513 {VK_TAB, TAB, NUL},
514 {VK_ESCAPE, ESC, NUL},
515 {NL, NL, NUL},
516 {CAR, CAR, NUL},
517
Bram Moolenaar734a8672019-12-02 22:49:38 +0100518 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100519 {0, 0, 0}
520};
521
Bram Moolenaar734a8672019-12-02 22:49:38 +0100522// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523static int s_button_pending = -1;
524
Bram Moolenaar734a8672019-12-02 22:49:38 +0100525// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
526// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100527static int s_getting_focus = FALSE;
528
529static int s_x_pending;
530static int s_y_pending;
531static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200532static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200534static int dead_key = 0; // 0: no dead key, 1: dead key pressed
535static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
536 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100537
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100538#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100539// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100540static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
541static void TrackUserActivity(UINT uMsg);
542#endif
543
544/*
545 * For control IME.
546 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100547 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548 */
K.Takata4ac893f2022-01-20 12:44:28 +0000549#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100550// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100551static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100554#endif
555
556#ifdef FEAT_MBYTE_IME
557static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
558#endif
559
560#if defined(FEAT_BROWSE)
561static char_u *convert_filter(char_u *s);
562#endif
563
564#ifdef DEBUG_PRINT_ERROR
565/*
566 * Print out the last Windows error message
567 */
568 static void
569print_windows_error(void)
570{
571 LPVOID lpMsgBuf;
572
573 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
574 NULL, GetLastError(),
575 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
576 (LPTSTR) &lpMsgBuf, 0, NULL);
577 TRACE1("Error: %s\n", lpMsgBuf);
578 LocalFree(lpMsgBuf);
579}
580#endif
581
582/*
583 * Cursor blink functions.
584 *
585 * This is a simple state machine:
586 * BLINK_NONE not blinking at all
587 * BLINK_OFF blinking, cursor is not shown
588 * BLINK_ON blinking, cursor is shown
589 */
590
591#define BLINK_NONE 0
592#define BLINK_OFF 1
593#define BLINK_ON 2
594
595static int blink_state = BLINK_NONE;
596static long_u blink_waittime = 700;
597static long_u blink_ontime = 400;
598static long_u blink_offtime = 250;
599static UINT blink_timer = 0;
600
Bram Moolenaar703a8042016-06-04 16:24:32 +0200601 int
602gui_mch_is_blinking(void)
603{
604 return blink_state != BLINK_NONE;
605}
606
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200607 int
608gui_mch_is_blink_off(void)
609{
610 return blink_state == BLINK_OFF;
611}
612
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100613 void
614gui_mch_set_blinking(long wait, long on, long off)
615{
616 blink_waittime = wait;
617 blink_ontime = on;
618 blink_offtime = off;
619}
620
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100621 static VOID CALLBACK
622_OnBlinkTimer(
623 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100624 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100625 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627{
628 MSG msg;
629
630 /*
631 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
632 */
633
634 KillTimer(NULL, idEvent);
635
Bram Moolenaar734a8672019-12-02 22:49:38 +0100636 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000637 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 ;
639
640 if (blink_state == BLINK_ON)
641 {
642 gui_undraw_cursor();
643 blink_state = BLINK_OFF;
644 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
645 (TIMERPROC)_OnBlinkTimer);
646 }
647 else
648 {
649 gui_update_cursor(TRUE, FALSE);
650 blink_state = BLINK_ON;
651 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100652 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100654 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100655}
656
657 static void
658gui_mswin_rm_blink_timer(void)
659{
660 MSG msg;
661
662 if (blink_timer != 0)
663 {
664 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100665 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000666 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100667 ;
668 blink_timer = 0;
669 }
670}
671
672/*
673 * Stop the cursor blinking. Show the cursor if it wasn't shown.
674 */
675 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100676gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100677{
678 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100679 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100680 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100682 gui_mch_flush();
683 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100684 blink_state = BLINK_NONE;
685}
686
687/*
688 * Start the cursor blinking. If it was already blinking, this restarts the
689 * waiting time and shows the cursor.
690 */
691 void
692gui_mch_start_blink(void)
693{
694 gui_mswin_rm_blink_timer();
695
Bram Moolenaar734a8672019-12-02 22:49:38 +0100696 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100697 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
698 {
699 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
700 (TIMERPROC)_OnBlinkTimer);
701 blink_state = BLINK_ON;
702 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100703 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100704 }
705}
706
707/*
708 * Call-back routines.
709 */
710
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100711 static VOID CALLBACK
712_OnTimer(
713 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100714 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100716 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717{
718 MSG msg;
719
720 /*
721 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
722 */
723 KillTimer(NULL, idEvent);
724 s_timed_out = TRUE;
725
Bram Moolenaar734a8672019-12-02 22:49:38 +0100726 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000727 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100728 ;
729 if (idEvent == s_wait_timer)
730 s_wait_timer = 0;
731}
732
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100733 static void
734_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100735 HWND hwnd UNUSED,
736 UINT ch UNUSED,
737 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100738{
739 dead_key = 1;
740}
741
742/*
743 * Convert Unicode character "ch" to bytes in "string[slen]".
744 * When "had_alt" is TRUE the ALT key was included in "ch".
745 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200746 * Because the Windows API uses UTF-16, we have to deal with surrogate
747 * pairs; this is where we choose to deal with them: if "ch" is a high
748 * surrogate, it will be stored, and the length returned will be zero; the next
749 * char_to_string call will then include the high surrogate, decoding the pair
750 * of UTF-16 code units to a single Unicode code point, presuming it is the
751 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752 */
753 static int
754char_to_string(int ch, char_u *string, int slen, int had_alt)
755{
756 int len;
757 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100758 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200759 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200761 if (surrogate_pending_ch != 0)
762 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100763 // We don't guarantee ch is a low surrogate to match the high surrogate
764 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200765 wstring[0] = surrogate_pending_ch;
766 wstring[1] = ch;
767 surrogate_pending_ch = 0;
768 len = 2;
769 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100770 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200771 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100772 // We don't have the entire code point yet, only the first UTF-16 code
773 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200774 surrogate_pending_ch = ch;
775 return 0;
776 }
777 else
778 {
779 wstring[0] = ch;
780 len = 1;
781 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200782
Bram Moolenaar734a8672019-12-02 22:49:38 +0100783 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
784 // "enc_codepage" is non-zero use the standard Win32 function,
785 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200786 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100787 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200788 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
789 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100790 // If we had included the ALT key into the character but now the
791 // upper bit is no longer set, that probably means the conversion
792 // failed. Convert the original character and set the upper bit
793 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200794 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100795 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 wstring[0] = ch & 0x7f;
797 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
798 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100799 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200800 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801 }
802 }
803 else
804 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200805 ws = utf16_to_enc(wstring, &len);
806 if (ws == NULL)
807 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100808 else
809 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100810 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200811 len = slen;
812 mch_memmove(string, ws, len);
813 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100814 }
815 }
816
817 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100818 {
819 string[0] = ch;
820 len = 1;
821 }
822
823 for (i = 0; i < len; ++i)
824 if (string[i] == CSI && len <= slen - 2)
825 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100826 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
828 string[++i] = KS_EXTRA;
829 string[++i] = (int)KE_CSI;
830 len += 2;
831 }
832
833 return len;
834}
835
836/*
837 * Key hit, add it to the input buffer.
838 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100839 static void
840_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100841 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100842 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100843 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844{
845 char_u string[40];
846 int len = 0;
847
848 dead_key = 0;
849
850 len = char_to_string(ch, string, 40, FALSE);
851 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
852 {
853 trash_input_buf();
854 got_int = TRUE;
855 }
856
857 add_to_input_buf(string, len);
858}
859
860/*
861 * Alt-Key hit, add it to the input buffer.
862 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100863 static void
864_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100865 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100866 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100867 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100868{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100869 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870 int len;
871 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100872 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100873
874 dead_key = 0;
875
Bram Moolenaar734a8672019-12-02 22:49:38 +0100876 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100877
Bram Moolenaar734a8672019-12-02 22:49:38 +0100878 // OK, we have a character key (given by ch) which was entered with the
879 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
880 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
881 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100882 modifiers = MOD_MASK_ALT;
883 if (GetKeyState(VK_SHIFT) & 0x8000)
884 modifiers |= MOD_MASK_SHIFT;
885 if (GetKeyState(VK_CONTROL) & 0x8000)
886 modifiers |= MOD_MASK_CTRL;
887
888 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100889 // remove the SHIFT modifier for keys where it's already included, e.g.,
890 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200891 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100892
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200893 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
894 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100895 if (ch == CSI)
896 ch = K_CSI;
897
898 len = 0;
899 if (modifiers)
900 {
901 string[len++] = CSI;
902 string[len++] = KS_MODIFIER;
903 string[len++] = modifiers;
904 }
905
906 if (IS_SPECIAL((int)ch))
907 {
908 string[len++] = CSI;
909 string[len++] = K_SECOND((int)ch);
910 string[len++] = K_THIRD((int)ch);
911 }
912 else
913 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100914 // Although the documentation isn't clear about it, we assume "ch" is
915 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100916 len += char_to_string(ch, string + len, 40 - len, TRUE);
917 }
918
919 add_to_input_buf(string, len);
920}
921
922 static void
923_OnMouseEvent(
924 int button,
925 int x,
926 int y,
927 int repeated_click,
928 UINT keyFlags)
929{
930 int vim_modifiers = 0x0;
931
932 s_getting_focus = FALSE;
933
934 if (keyFlags & MK_SHIFT)
935 vim_modifiers |= MOUSE_SHIFT;
936 if (keyFlags & MK_CONTROL)
937 vim_modifiers |= MOUSE_CTRL;
938 if (GetKeyState(VK_MENU) & 0x8000)
939 vim_modifiers |= MOUSE_ALT;
940
941 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
942}
943
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944 static void
945_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100946 HWND hwnd UNUSED,
947 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100948 int x,
949 int y,
950 UINT keyFlags)
951{
952 static LONG s_prevTime = 0;
953
954 LONG currentTime = GetMessageTime();
955 int button = -1;
956 int repeated_click;
957
Bram Moolenaar734a8672019-12-02 22:49:38 +0100958 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100959 (void)SetFocus(s_hwnd);
960
961 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
962 button = MOUSE_LEFT;
963 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
964 button = MOUSE_MIDDLE;
965 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
966 button = MOUSE_RIGHT;
967 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
968 {
969#ifndef GET_XBUTTON_WPARAM
970# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
971#endif
972 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
973 }
974 else if (s_uMsg == WM_CAPTURECHANGED)
975 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100976 // on W95/NT4, somehow you get in here with an odd Msg
977 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100978 if (s_button_pending == MOUSE_LEFT)
979 button = MOUSE_RIGHT;
980 else
981 button = MOUSE_LEFT;
982 }
983 if (button >= 0)
984 {
985 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
986
987 /*
988 * Holding down the left and right buttons simulates pushing the middle
989 * button.
990 */
991 if (repeated_click
992 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
993 || (button == MOUSE_RIGHT
994 && s_button_pending == MOUSE_LEFT)))
995 {
996 /*
997 * Hmm, gui.c will ignore more than one button down at a time, so
998 * pretend we let go of it first.
999 */
1000 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1001 button = MOUSE_MIDDLE;
1002 repeated_click = FALSE;
1003 s_button_pending = -1;
1004 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1005 }
1006 else if ((repeated_click)
1007 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1008 {
1009 if (s_button_pending > -1)
1010 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001011 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1012 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001013 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001014 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001015 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1016 }
1017 else
1018 {
1019 /*
1020 * If this is the first press (i.e. not a multiple click) don't
1021 * action immediately, but store and wait for:
1022 * i) button-up
1023 * ii) mouse move
1024 * iii) another button press
1025 * before using it.
1026 * This enables us to make left+right simulate middle button,
1027 * without left or right being actioned first. The side-effect is
1028 * that if you click and hold the mouse without dragging, the
1029 * cursor doesn't move until you release the button. In practice
1030 * this is hardly a problem.
1031 */
1032 s_button_pending = button;
1033 s_x_pending = x;
1034 s_y_pending = y;
1035 s_kFlags_pending = keyFlags;
1036 }
1037
1038 s_prevTime = currentTime;
1039 }
1040}
1041
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001042 static void
1043_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001044 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001045 int x,
1046 int y,
1047 UINT keyFlags)
1048{
1049 int button;
1050
1051 s_getting_focus = FALSE;
1052 if (s_button_pending > -1)
1053 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001054 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001055 _OnMouseEvent(s_button_pending, s_x_pending,
1056 s_y_pending, FALSE, s_kFlags_pending);
1057 s_button_pending = -1;
1058 }
1059 if (s_uMsg == WM_MOUSEMOVE)
1060 {
1061 /*
1062 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1063 * down.
1064 */
1065 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1066 | MK_XBUTTON1 | MK_XBUTTON2)))
1067 {
1068 gui_mouse_moved(x, y);
1069 return;
1070 }
1071
1072 /*
1073 * While button is down, keep grabbing mouse move events when
1074 * the mouse goes outside the window
1075 */
1076 SetCapture(s_textArea);
1077 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001078 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001079 }
1080 else
1081 {
1082 ReleaseCapture();
1083 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001084 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001085 }
1086
1087 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1088}
1089
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001090 static void
1091_OnSizeTextArea(
1092 HWND hwnd UNUSED,
1093 UINT state UNUSED,
1094 int cx UNUSED,
1095 int cy UNUSED)
1096{
1097#if defined(FEAT_DIRECTX)
1098 if (IS_ENABLE_DIRECTX())
1099 directx_binddc();
1100#endif
1101}
1102
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103#ifdef FEAT_MENU
1104/*
1105 * Find the vimmenu_T with the given id
1106 */
1107 static vimmenu_T *
1108gui_mswin_find_menu(
1109 vimmenu_T *pMenu,
1110 int id)
1111{
1112 vimmenu_T *pChildMenu;
1113
1114 while (pMenu)
1115 {
1116 if (pMenu->id == (UINT)id)
1117 break;
1118 if (pMenu->children != NULL)
1119 {
1120 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1121 if (pChildMenu)
1122 {
1123 pMenu = pChildMenu;
1124 break;
1125 }
1126 }
1127 pMenu = pMenu->next;
1128 }
1129 return pMenu;
1130}
1131
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001132 static void
1133_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001134 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001135 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001136 HWND hwndCtl UNUSED,
1137 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001138{
1139 vimmenu_T *pMenu;
1140
1141 pMenu = gui_mswin_find_menu(root_menu, id);
1142 if (pMenu)
1143 gui_menu_cb(pMenu);
1144}
1145#endif
1146
1147#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001148/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001149 * Handle a Find/Replace window message.
1150 */
1151 static void
1152_OnFindRepl(void)
1153{
1154 int flags = 0;
1155 int down;
1156
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001158 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001159 (void)SetFocus(s_hwnd);
1160
1161 if (s_findrep_struct.Flags & FR_FINDNEXT)
1162 {
1163 flags = FRD_FINDNEXT;
1164
Bram Moolenaar734a8672019-12-02 22:49:38 +01001165 // Give main window the focus back: this is so the cursor isn't
1166 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001167 (void)SetFocus(s_hwnd);
1168 }
1169 else if (s_findrep_struct.Flags & FR_REPLACE)
1170 {
1171 flags = FRD_REPLACE;
1172
Bram Moolenaar734a8672019-12-02 22:49:38 +01001173 // Give main window the focus back: this is so the cursor isn't
1174 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001175 (void)SetFocus(s_hwnd);
1176 }
1177 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1178 {
1179 flags = FRD_REPLACEALL;
1180 }
1181
1182 if (flags != 0)
1183 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001184 char_u *p, *q;
1185
Bram Moolenaar734a8672019-12-02 22:49:38 +01001186 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001187 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1188 flags |= FRD_WHOLE_WORD;
1189 if (s_findrep_struct.Flags & FR_MATCHCASE)
1190 flags |= FRD_MATCH_CASE;
1191 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001192 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1193 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1194 if (p != NULL && q != NULL)
1195 gui_do_findrepl(flags, p, q, down);
1196 vim_free(p);
1197 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198 }
1199}
1200#endif
1201
1202 static void
1203HandleMouseHide(UINT uMsg, LPARAM lParam)
1204{
1205 static LPARAM last_lParam = 0L;
1206
Bram Moolenaar734a8672019-12-02 22:49:38 +01001207 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001208 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1209 {
1210 if (lParam == last_lParam)
1211 return;
1212 last_lParam = lParam;
1213 }
1214
Bram Moolenaar734a8672019-12-02 22:49:38 +01001215 // Handle specially, to centralise coding. We need to be sure we catch all
1216 // possible events which should cause us to restore the cursor (as it is a
1217 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001218 switch (uMsg)
1219 {
1220 case WM_KEYUP:
1221 case WM_CHAR:
1222 /*
1223 * blank out the pointer if necessary
1224 */
1225 if (p_mh)
1226 gui_mch_mousehide(TRUE);
1227 break;
1228
Bram Moolenaar734a8672019-12-02 22:49:38 +01001229 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001230 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001231 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001232 case WM_LBUTTONDOWN:
1233 case WM_LBUTTONUP:
1234 case WM_MBUTTONDOWN:
1235 case WM_MBUTTONUP:
1236 case WM_RBUTTONDOWN:
1237 case WM_RBUTTONUP:
1238 case WM_XBUTTONDOWN:
1239 case WM_XBUTTONUP:
1240 case WM_NCMOUSEMOVE:
1241 case WM_NCLBUTTONDOWN:
1242 case WM_NCLBUTTONUP:
1243 case WM_NCMBUTTONDOWN:
1244 case WM_NCMBUTTONUP:
1245 case WM_NCRBUTTONDOWN:
1246 case WM_NCRBUTTONUP:
1247 case WM_KILLFOCUS:
1248 /*
1249 * if the pointer is currently hidden, then we should show it.
1250 */
1251 gui_mch_mousehide(FALSE);
1252 break;
1253 }
1254}
1255
1256 static LRESULT CALLBACK
1257_TextAreaWndProc(
1258 HWND hwnd,
1259 UINT uMsg,
1260 WPARAM wParam,
1261 LPARAM lParam)
1262{
1263 /*
1264 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1265 hwnd, uMsg, wParam, lParam);
1266 */
1267
1268 HandleMouseHide(uMsg, lParam);
1269
1270 s_uMsg = uMsg;
1271 s_wParam = wParam;
1272 s_lParam = lParam;
1273
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001274#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001275 TrackUserActivity(uMsg);
1276#endif
1277
1278 switch (uMsg)
1279 {
1280 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1281 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1282 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1283 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1284 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1285 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1286 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1287 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1288 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1289 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1290 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1291 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1292 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1293 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001294 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001295
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001296#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001297 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1298 return TRUE;
1299#endif
1300 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001301 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001302 }
1303}
1304
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001305/*
1306 * Called when the foreground or background color has been changed.
1307 */
1308 void
1309gui_mch_new_colors(void)
1310{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001311 HBRUSH prevBrush;
1312
1313 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001314 prevBrush = (HBRUSH)SetClassLongPtr(
1315 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001316 InvalidateRect(s_hwnd, NULL, TRUE);
1317 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001318}
1319
1320/*
1321 * Set the colors to their default values.
1322 */
1323 void
1324gui_mch_def_colors(void)
1325{
1326 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1327 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1328 gui.def_norm_pixel = gui.norm_pixel;
1329 gui.def_back_pixel = gui.back_pixel;
1330}
1331
1332/*
1333 * Open the GUI window which was created by a call to gui_mch_init().
1334 */
1335 int
1336gui_mch_open(void)
1337{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001338 // Actually open the window, if not already visible
1339 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001340 if (!IsWindowVisible(s_hwnd))
1341 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1342
1343#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001344 // Init replace string here, so that we keep it when re-opening the
1345 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001346 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1347#endif
1348
1349 return OK;
1350}
1351
1352/*
1353 * Get the position of the top left corner of the window.
1354 */
1355 int
1356gui_mch_get_winpos(int *x, int *y)
1357{
1358 RECT rect;
1359
1360 GetWindowRect(s_hwnd, &rect);
1361 *x = rect.left;
1362 *y = rect.top;
1363 return OK;
1364}
1365
1366/*
1367 * Set the position of the top left corner of the window to the given
1368 * coordinates.
1369 */
1370 void
1371gui_mch_set_winpos(int x, int y)
1372{
1373 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1374 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1375}
1376 void
1377gui_mch_set_text_area_pos(int x, int y, int w, int h)
1378{
1379 static int oldx = 0;
1380 static int oldy = 0;
1381
1382 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1383
1384#ifdef FEAT_TOOLBAR
1385 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1386 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001387 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001388#endif
1389#if defined(FEAT_GUI_TABLINE)
1390 if (showing_tabline)
1391 {
1392 int top = 0;
1393 RECT rect;
1394
1395# ifdef FEAT_TOOLBAR
1396 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001397 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001398# endif
1399 GetClientRect(s_hwnd, &rect);
1400 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1401 }
1402#endif
1403
Bram Moolenaar734a8672019-12-02 22:49:38 +01001404 // When side scroll bar is unshown, the size of window will change.
1405 // then, the text area move left or right. thus client rect should be
1406 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001407 if (oldx != x || oldy != y)
1408 {
1409 InvalidateRect(s_hwnd, NULL, FALSE);
1410 oldx = x;
1411 oldy = y;
1412 }
1413}
1414
1415
1416/*
1417 * Scrollbar stuff:
1418 */
1419
1420 void
1421gui_mch_enable_scrollbar(
1422 scrollbar_T *sb,
1423 int flag)
1424{
1425 ShowScrollBar(sb->id, SB_CTL, flag);
1426
Bram Moolenaar734a8672019-12-02 22:49:38 +01001427 // TODO: When the window is maximized, the size of the window stays the
1428 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1429 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001430}
1431
1432 void
1433gui_mch_set_scrollbar_pos(
1434 scrollbar_T *sb,
1435 int x,
1436 int y,
1437 int w,
1438 int h)
1439{
1440 SetWindowPos(sb->id, NULL, x, y, w, h,
1441 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1442}
1443
Bram Moolenaar203ec772020-07-17 20:43:43 +02001444 int
1445gui_mch_get_scrollbar_xpadding(void)
1446{
1447 RECT rcTxt, rcWnd;
1448 int xpad;
1449
1450 GetWindowRect(s_textArea, &rcTxt);
1451 GetWindowRect(s_hwnd, &rcWnd);
1452 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001453 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1454 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001455 return (xpad < 0) ? 0 : xpad;
1456}
1457
1458 int
1459gui_mch_get_scrollbar_ypadding(void)
1460{
1461 RECT rcTxt, rcWnd;
1462 int ypad;
1463
1464 GetWindowRect(s_textArea, &rcTxt);
1465 GetWindowRect(s_hwnd, &rcWnd);
1466 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001467 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1468 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001469 return (ypad < 0) ? 0 : ypad;
1470}
1471
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001472 void
1473gui_mch_create_scrollbar(
1474 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001475 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001476{
1477 sb->id = CreateWindow(
1478 "SCROLLBAR", "Scrollbar",
1479 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001480 10, // Any value will do for now
1481 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001482 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001483 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001484}
1485
1486/*
1487 * Find the scrollbar with the given hwnd.
1488 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001489 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001490gui_mswin_find_scrollbar(HWND hwnd)
1491{
1492 win_T *wp;
1493
1494 if (gui.bottom_sbar.id == hwnd)
1495 return &gui.bottom_sbar;
1496 FOR_ALL_WINDOWS(wp)
1497 {
1498 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1499 return &wp->w_scrollbars[SBAR_LEFT];
1500 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1501 return &wp->w_scrollbars[SBAR_RIGHT];
1502 }
1503 return NULL;
1504}
1505
K.Takatac81e9bf2022-01-16 14:15:49 +00001506 static void
1507update_scrollbar_size(void)
1508{
1509 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1510 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1511}
1512
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513/*
K.Takataabe628e2022-01-23 16:25:17 +00001514 * Get the average character size of a font.
1515 */
1516 static void
1517GetAverageFontSize(HDC hdc, SIZE *size)
1518{
1519 // GetTextMetrics() may not return the right value in tmAveCharWidth
1520 // for some fonts. Do our own average computation.
1521 GetTextExtentPoint(hdc,
1522 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1523 52, size);
1524 size->cx = (size->cx / 26 + 1) / 2;
1525}
1526
1527/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001528 * Get the character size of a font.
1529 */
1530 static void
1531GetFontSize(GuiFont font)
1532{
1533 HWND hwnd = GetDesktopWindow();
1534 HDC hdc = GetWindowDC(hwnd);
1535 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001536 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001537 TEXTMETRIC tm;
1538
1539 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001540 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001541
K.Takataabe628e2022-01-23 16:25:17 +00001542 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001543 gui.char_height = tm.tmHeight + p_linespace;
1544
1545 SelectFont(hdc, hfntOld);
1546
1547 ReleaseDC(hwnd, hdc);
1548}
1549
1550/*
1551 * Adjust gui.char_height (after 'linespace' was changed).
1552 */
1553 int
1554gui_mch_adjust_charheight(void)
1555{
1556 GetFontSize(gui.norm_font);
1557 return OK;
1558}
1559
1560 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001561get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001562{
1563 HFONT font = NULL;
1564
Bram Moolenaar734a8672019-12-02 22:49:38 +01001565 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001566 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001567
1568 if (font == NULL)
1569 return NOFONT;
1570
1571 return (GuiFont)font;
1572}
1573
1574 static int
1575pixels_to_points(int pixels, int vertical)
1576{
1577 int points;
1578 HWND hwnd;
1579 HDC hdc;
1580
1581 hwnd = GetDesktopWindow();
1582 hdc = GetWindowDC(hwnd);
1583
1584 points = MulDiv(pixels, 72,
1585 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1586
1587 ReleaseDC(hwnd, hdc);
1588
1589 return points;
1590}
1591
1592 GuiFont
1593gui_mch_get_font(
1594 char_u *name,
1595 int giveErrorIfMissing)
1596{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001597 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001598 GuiFont font = NOFONT;
1599
1600 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001601 {
1602 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001603 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001604 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001605 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001606 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001607 return font;
1608}
1609
1610#if defined(FEAT_EVAL) || defined(PROTO)
1611/*
1612 * Return the name of font "font" in allocated memory.
1613 * Don't know how to get the actual name, thus use the provided name.
1614 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001615 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001616gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001617{
1618 if (name == NULL)
1619 return NULL;
1620 return vim_strsave(name);
1621}
1622#endif
1623
1624 void
1625gui_mch_free_font(GuiFont font)
1626{
1627 if (font)
1628 DeleteObject((HFONT)font);
1629}
1630
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001631/*
1632 * Return the Pixel value (color) for the given color name.
1633 * Return INVALCOLOR for error.
1634 */
1635 guicolor_T
1636gui_mch_get_color(char_u *name)
1637{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001638 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001639
1640 typedef struct SysColorTable
1641 {
1642 char *name;
1643 int color;
1644 } SysColorTable;
1645
1646 static SysColorTable sys_table[] =
1647 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1649 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001650#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001651 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1652#endif
1653 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1654 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1655 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1656 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1657 {"SYS_DESKTOP", COLOR_DESKTOP},
1658 {"SYS_INFOBK", COLOR_INFOBK},
1659 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1660 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001661 {"SYS_BTNFACE", COLOR_BTNFACE},
1662 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1663 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1664 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1665 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1666 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1667 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1668 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1669 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1670 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1671 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1672 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1673 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1674 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1675 {"SYS_MENU", COLOR_MENU},
1676 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1677 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1678 {"SYS_WINDOW", COLOR_WINDOW},
1679 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1680 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1681 };
1682
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001683 /*
1684 * Try to look up a system colour.
1685 */
K.Takataeeec2542021-06-02 13:28:16 +02001686 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001687 if (STRICMP(name, sys_table[i].name) == 0)
1688 return GetSysColor(sys_table[i].color);
1689
Bram Moolenaarab302212016-04-26 20:59:29 +02001690 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001692
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001693 guicolor_T
1694gui_mch_get_rgb_color(int r, int g, int b)
1695{
1696 return gui_get_rgb_color_cmn(r, g, b);
1697}
1698
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001699/*
1700 * Return OK if the key with the termcap name "name" is supported.
1701 */
1702 int
1703gui_mch_haskey(char_u *name)
1704{
1705 int i;
1706
1707 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1708 if (name[0] == special_keys[i].vim_code0 &&
1709 name[1] == special_keys[i].vim_code1)
1710 return OK;
1711 return FAIL;
1712}
1713
1714 void
1715gui_mch_beep(void)
1716{
1717 MessageBeep(MB_OK);
1718}
1719/*
1720 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1721 */
1722 void
1723gui_mch_invert_rectangle(
1724 int r,
1725 int c,
1726 int nr,
1727 int nc)
1728{
1729 RECT rc;
1730
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001731#if defined(FEAT_DIRECTX)
1732 if (IS_ENABLE_DIRECTX())
1733 DWriteContext_Flush(s_dwc);
1734#endif
1735
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001736 /*
1737 * Note: InvertRect() excludes right and bottom of rectangle.
1738 */
1739 rc.left = FILL_X(c);
1740 rc.top = FILL_Y(r);
1741 rc.right = rc.left + nc * gui.char_width;
1742 rc.bottom = rc.top + nr * gui.char_height;
1743 InvertRect(s_hdc, &rc);
1744}
1745
1746/*
1747 * Iconify the GUI window.
1748 */
1749 void
1750gui_mch_iconify(void)
1751{
1752 ShowWindow(s_hwnd, SW_MINIMIZE);
1753}
1754
1755/*
1756 * Draw a cursor without focus.
1757 */
1758 void
1759gui_mch_draw_hollow_cursor(guicolor_T color)
1760{
1761 HBRUSH hbr;
1762 RECT rc;
1763
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001764#if defined(FEAT_DIRECTX)
1765 if (IS_ENABLE_DIRECTX())
1766 DWriteContext_Flush(s_dwc);
1767#endif
1768
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001769 /*
1770 * Note: FrameRect() excludes right and bottom of rectangle.
1771 */
1772 rc.left = FILL_X(gui.col);
1773 rc.top = FILL_Y(gui.row);
1774 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001775 if (mb_lefthalve(gui.row, gui.col))
1776 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 rc.bottom = rc.top + gui.char_height;
1778 hbr = CreateSolidBrush(color);
1779 FrameRect(s_hdc, &rc, hbr);
1780 DeleteBrush(hbr);
1781}
1782/*
1783 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1784 * color "color".
1785 */
1786 void
1787gui_mch_draw_part_cursor(
1788 int w,
1789 int h,
1790 guicolor_T color)
1791{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 RECT rc;
1793
1794 /*
1795 * Note: FillRect() excludes right and bottom of rectangle.
1796 */
1797 rc.left =
1798#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001799 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1801#endif
1802 FILL_X(gui.col);
1803 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1804 rc.right = rc.left + w;
1805 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001806
Bram Moolenaar92467d32017-12-05 13:22:16 +01001807 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001808}
1809
1810
1811/*
1812 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1813 * dead key's nominal character and re-post the original message.
1814 */
1815 static void
1816outputDeadKey_rePost(MSG originalMsg)
1817{
1818 static MSG deadCharExpel;
1819
1820 if (!dead_key)
1821 return;
1822
1823 dead_key = 0;
1824
Bram Moolenaar734a8672019-12-02 22:49:38 +01001825 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001826 deadCharExpel.message = originalMsg.message;
1827 deadCharExpel.hwnd = originalMsg.hwnd;
1828 deadCharExpel.wParam = VK_SPACE;
1829
K.Takata4ac893f2022-01-20 12:44:28 +00001830 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001831
Bram Moolenaar734a8672019-12-02 22:49:38 +01001832 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001833 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1834 originalMsg.lParam);
1835}
1836
1837
1838/*
1839 * Process a single Windows message.
1840 * If one is not available we hang until one is.
1841 */
1842 static void
1843process_message(void)
1844{
1845 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001846 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001847 char_u string[40];
1848 int i;
1849 int modifiers = 0;
1850 int key;
1851#ifdef FEAT_MENU
1852 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1853#endif
1854
K.Takatab7057bd2022-01-21 11:37:07 +00001855 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001856
1857#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001858 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001859 if (msg.message == WM_OLE)
1860 {
1861 char_u *str = (char_u *)msg.lParam;
1862 if (str == NULL || *str == NUL)
1863 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001864 // Message can't be ours, forward it. Fixes problem with Ultramon
1865 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001866 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001867 }
1868 else
1869 {
1870 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001871 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872 }
1873 return;
1874 }
1875#endif
1876
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001877#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001878 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001879 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001880 {
1881 HandleMouseHide(msg.message, msg.lParam);
1882 return;
1883 }
1884#endif
1885
1886 /*
1887 * Check if it's a special key that we recognise. If not, call
1888 * TranslateMessage().
1889 */
1890 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1891 {
1892 vk = (int) msg.wParam;
1893
1894 /*
1895 * Handle dead keys in special conditions in other cases we let Windows
1896 * handle them and do not interfere.
1897 *
1898 * The dead_key flag must be reset on several occasions:
1899 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1900 * consumed at that point (This is when we let Windows combine the
1901 * dead character on its own)
1902 *
1903 * - Before doing something special such as regenerating keypresses to
1904 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001905 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906 * immediately to _OnChar() (or _OnSysChar()).
1907 */
1908 if (dead_key)
1909 {
1910 /*
1911 * If a dead key was pressed and the user presses VK_SPACE,
1912 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1913 * with the dead char now, so do nothing special and let Windows
1914 * handle it.
1915 *
1916 * Note that VK_SPACE combines with the dead_key's character and
1917 * only one WM_CHAR will be generated by TranslateMessage(), in
1918 * the two other cases two WM_CHAR will be generated: the dead
1919 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1920 * user expects.
1921 */
1922 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1923 {
1924 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001925 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001926 return;
1927 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001928 // In modes where we are not typing, dead keys should behave
1929 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1931 {
1932 outputDeadKey_rePost(msg);
1933 return;
1934 }
1935 }
1936
Bram Moolenaar734a8672019-12-02 22:49:38 +01001937 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001938 if (vk == VK_CANCEL)
1939 {
1940 trash_input_buf();
1941 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001942 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001943 string[0] = Ctrl_C;
1944 add_to_input_buf(string, 1);
1945 }
1946
1947 for (i = 0; special_keys[i].key_sym != 0; i++)
1948 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001949 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001950 if (special_keys[i].key_sym == vk
1951 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1952 {
1953 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001954 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001955 * is a key that would normally trigger the dead key nominal
1956 * character output (such as a NUMPAD printable character or
1957 * the TAB key, etc...).
1958 */
1959 if (dead_key && (special_keys[i].vim_code0 == 'K'
1960 || vk == VK_TAB || vk == CAR))
1961 {
1962 outputDeadKey_rePost(msg);
1963 return;
1964 }
1965
1966#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001967 // Check for <F10>: Windows selects the menu. When <F10> is
1968 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001969 if (vk == VK_F10
1970 && gui.menu_is_active
1971 && check_map(k10, State, FALSE, TRUE, FALSE,
1972 NULL, NULL) == NULL)
1973 break;
1974#endif
1975 if (GetKeyState(VK_SHIFT) & 0x8000)
1976 modifiers |= MOD_MASK_SHIFT;
1977 /*
1978 * Don't use caps-lock as shift, because these are special keys
1979 * being considered here, and we only want letters to get
1980 * shifted -- webb
1981 */
1982 /*
1983 if (GetKeyState(VK_CAPITAL) & 0x0001)
1984 modifiers ^= MOD_MASK_SHIFT;
1985 */
1986 if (GetKeyState(VK_CONTROL) & 0x8000)
1987 modifiers |= MOD_MASK_CTRL;
1988 if (GetKeyState(VK_MENU) & 0x8000)
1989 modifiers |= MOD_MASK_ALT;
1990
1991 if (special_keys[i].vim_code1 == NUL)
1992 key = special_keys[i].vim_code0;
1993 else
1994 key = TO_SPECIAL(special_keys[i].vim_code0,
1995 special_keys[i].vim_code1);
1996 key = simplify_key(key, &modifiers);
1997 if (key == CSI)
1998 key = K_CSI;
1999
2000 if (modifiers)
2001 {
2002 string[0] = CSI;
2003 string[1] = KS_MODIFIER;
2004 string[2] = modifiers;
2005 add_to_input_buf(string, 3);
2006 }
2007
2008 if (IS_SPECIAL(key))
2009 {
2010 string[0] = CSI;
2011 string[1] = K_SECOND(key);
2012 string[2] = K_THIRD(key);
2013 add_to_input_buf(string, 3);
2014 }
2015 else
2016 {
2017 int len;
2018
Bram Moolenaar734a8672019-12-02 22:49:38 +01002019 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002020 len = char_to_string(key, string, 40, FALSE);
2021 add_to_input_buf(string, len);
2022 }
2023 break;
2024 }
2025 }
2026 if (special_keys[i].key_sym == 0)
2027 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002028 // Some keys need C-S- where they should only need C-.
2029 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2030 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002031 if (vk != 0xff
2032 && (GetKeyState(VK_CONTROL) & 0x8000)
2033 && !(GetKeyState(VK_SHIFT) & 0x8000)
2034 && !(GetKeyState(VK_MENU) & 0x8000))
2035 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002036 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002037 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2038 {
2039 string[0] = Ctrl_HAT;
2040 add_to_input_buf(string, 1);
2041 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002042 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2043 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002044 {
2045 string[0] = Ctrl__;
2046 add_to_input_buf(string, 1);
2047 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002048 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002049 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2050 {
2051 string[0] = Ctrl_AT;
2052 add_to_input_buf(string, 1);
2053 }
2054 else
K.Takata4ac893f2022-01-20 12:44:28 +00002055 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002056 }
2057 else
K.Takata4ac893f2022-01-20 12:44:28 +00002058 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059 }
2060 }
2061#ifdef FEAT_MBYTE_IME
2062 else if (msg.message == WM_IME_NOTIFY)
2063 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2064 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002066 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067#endif
2068
2069#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002070 // Check for <F10>: Default effect is to select the menu. When <F10> is
2071 // mapped we need to stop it here to avoid strange effects (e.g., for the
2072 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002073 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2074 NULL, NULL) == NULL)
2075#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002076 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002077}
2078
2079/*
2080 * Catch up with any queued events. This may put keyboard input into the
2081 * input buffer, call resize call-backs, trigger timers etc. If there is
2082 * nothing in the event queue (& no timers pending), then we return
2083 * immediately.
2084 */
2085 void
2086gui_mch_update(void)
2087{
2088 MSG msg;
2089
2090 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002091 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002092 && !vim_is_input_buf_full())
2093 process_message();
2094}
2095
Bram Moolenaar4231da42016-06-02 14:30:04 +02002096 static void
2097remove_any_timer(void)
2098{
2099 MSG msg;
2100
2101 if (s_wait_timer != 0 && !s_timed_out)
2102 {
2103 KillTimer(NULL, s_wait_timer);
2104
Bram Moolenaar734a8672019-12-02 22:49:38 +01002105 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002106 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002107 ;
2108 s_wait_timer = 0;
2109 }
2110}
2111
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112/*
2113 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2114 * from the keyboard.
2115 * wtime == -1 Wait forever.
2116 * wtime == 0 This should never happen.
2117 * wtime > 0 Wait wtime milliseconds for a character.
2118 * Returns OK if a character was found to be available within the given time,
2119 * or FAIL otherwise.
2120 */
2121 int
2122gui_mch_wait_for_chars(int wtime)
2123{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124 int focus;
2125
2126 s_timed_out = FALSE;
2127
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002128 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002129 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002130 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002131 if (s_busy_processing)
2132 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002133
2134 // When called with "wtime" zero, just want one msec.
2135 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002136 (TIMERPROC)_OnTimer);
2137 }
2138
2139 allow_scrollbar = TRUE;
2140
2141 focus = gui.in_focus;
2142 while (!s_timed_out)
2143 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002144 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002145 if (gui.in_focus != focus)
2146 {
2147 if (gui.in_focus)
2148 gui_mch_start_blink();
2149 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002150 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002151 focus = gui.in_focus;
2152 }
2153
2154 if (s_need_activate)
2155 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002156 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002157 s_need_activate = FALSE;
2158 }
2159
Bram Moolenaar4231da42016-06-02 14:30:04 +02002160#ifdef FEAT_TIMERS
2161 did_add_timer = FALSE;
2162#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002163#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002164 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002165 for (;;)
2166 {
2167 MSG msg;
2168
2169 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002170# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002171 if (did_add_timer)
2172 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002173# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002174 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002175 {
2176 process_message();
2177 break;
2178 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002179 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002180 // TODO: The 10 msec is a compromise between laggy response
2181 // and consuming more CPU time. Better would be to handle
2182 // channel messages when they arrive.
2183 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002184 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002185 break;
2186 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002187#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002188 // Don't use gui_mch_update() because then we will spin-lock until a
2189 // char arrives, instead we use GetMessage() to hang until an
2190 // event arrives. No need to check for input_buf_full because we are
2191 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002192 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002193#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002194
2195 if (input_available())
2196 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002197 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002198 allow_scrollbar = FALSE;
2199
Bram Moolenaar89c00032019-09-04 13:53:21 +02002200 // Clear pending mouse button, the release event may have been
2201 // taken by the dialog window. But don't do this when getting
2202 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002203 if (!s_getting_focus)
2204 s_button_pending = -1;
2205
2206 return OK;
2207 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002208
2209#ifdef FEAT_TIMERS
2210 if (did_add_timer)
2211 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002212 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002213 remove_any_timer();
2214 break;
2215 }
2216#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002217 }
2218 allow_scrollbar = FALSE;
2219 return FAIL;
2220}
2221
2222/*
2223 * Clear a rectangular region of the screen from text pos (row1, col1) to
2224 * (row2, col2) inclusive.
2225 */
2226 void
2227gui_mch_clear_block(
2228 int row1,
2229 int col1,
2230 int row2,
2231 int col2)
2232{
2233 RECT rc;
2234
2235 /*
2236 * Clear one extra pixel at the far right, for when bold characters have
2237 * spilled over to the window border.
2238 * Note: FillRect() excludes right and bottom of rectangle.
2239 */
2240 rc.left = FILL_X(col1);
2241 rc.top = FILL_Y(row1);
2242 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2243 rc.bottom = FILL_Y(row2 + 1);
2244 clear_rect(&rc);
2245}
2246
2247/*
2248 * Clear the whole text window.
2249 */
2250 void
2251gui_mch_clear_all(void)
2252{
2253 RECT rc;
2254
2255 rc.left = 0;
2256 rc.top = 0;
2257 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2258 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2259 clear_rect(&rc);
2260}
2261/*
2262 * Menu stuff.
2263 */
2264
2265 void
2266gui_mch_enable_menu(int flag)
2267{
2268#ifdef FEAT_MENU
2269 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2270#endif
2271}
2272
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273 void
2274gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002275 int x UNUSED,
2276 int y UNUSED,
2277 int w UNUSED,
2278 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002279{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002280 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002281}
2282
2283#if defined(FEAT_MENU) || defined(PROTO)
2284/*
2285 * Make menu item hidden or not hidden
2286 */
2287 void
2288gui_mch_menu_hidden(
2289 vimmenu_T *menu,
2290 int hidden)
2291{
2292 /*
2293 * This doesn't do what we want. Hmm, just grey the menu items for now.
2294 */
2295 /*
2296 if (hidden)
2297 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2298 else
2299 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2300 */
2301 gui_mch_menu_grey(menu, hidden);
2302}
2303
2304/*
2305 * This is called after setting all the menus to grey/hidden or not.
2306 */
2307 void
2308gui_mch_draw_menubar(void)
2309{
2310 DrawMenuBar(s_hwnd);
2311}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002312#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002313
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314/*
2315 * Return the RGB value of a pixel as a long.
2316 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002317 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002318gui_mch_get_rgb(guicolor_T pixel)
2319{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002320 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2321 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002322}
2323
2324#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002325/*
2326 * Convert pixels in X to dialog units
2327 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328 static WORD
2329PixelToDialogX(int numPixels)
2330{
2331 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2332}
2333
Bram Moolenaar734a8672019-12-02 22:49:38 +01002334/*
2335 * Convert pixels in Y to dialog units
2336 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337 static WORD
2338PixelToDialogY(int numPixels)
2339{
2340 return (WORD)((numPixels * 8) / s_dlgfntheight);
2341}
2342
Bram Moolenaar734a8672019-12-02 22:49:38 +01002343/*
2344 * Return the width in pixels of the given text in the given DC.
2345 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002346 static int
2347GetTextWidth(HDC hdc, char_u *str, int len)
2348{
2349 SIZE size;
2350
2351 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2352 return size.cx;
2353}
2354
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355/*
2356 * Return the width in pixels of the given text in the given DC, taking care
2357 * of 'encoding' to active codepage conversion.
2358 */
2359 static int
2360GetTextWidthEnc(HDC hdc, char_u *str, int len)
2361{
2362 SIZE size;
2363 WCHAR *wstr;
2364 int n;
2365 int wlen = len;
2366
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002367 wstr = enc_to_utf16(str, &wlen);
2368 if (wstr == NULL)
2369 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002370
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002371 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2372 vim_free(wstr);
2373 if (n)
2374 return size.cx;
2375 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002378static void get_work_area(RECT *spi_rect);
2379
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002380/*
2381 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002382 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2383 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384 */
2385 static BOOL
2386CenterWindow(
2387 HWND hwndChild,
2388 HWND hwndParent)
2389{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002390 HMONITOR mon;
2391 MONITORINFO moninfo;
2392 RECT rChild, rParent, rScreen;
2393 int wChild, hChild, wParent, hParent;
2394 int xNew, yNew;
2395 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396
2397 GetWindowRect(hwndChild, &rChild);
2398 wChild = rChild.right - rChild.left;
2399 hChild = rChild.bottom - rChild.top;
2400
Bram Moolenaar734a8672019-12-02 22:49:38 +01002401 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002402 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002403 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404 else
2405 GetWindowRect(hwndParent, &rParent);
2406 wParent = rParent.right - rParent.left;
2407 hParent = rParent.bottom - rParent.top;
2408
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002409 moninfo.cbSize = sizeof(MONITORINFO);
2410 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2411 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002412 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002413 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002415 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002416 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002417 hdc = GetDC(hwndChild);
2418 rScreen.left = 0;
2419 rScreen.top = 0;
2420 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2421 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2422 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423 }
2424
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002425 xNew = rParent.left + ((wParent - wChild) / 2);
2426 if (xNew < rScreen.left)
2427 xNew = rScreen.left;
2428 else if ((xNew + wChild) > rScreen.right)
2429 xNew = rScreen.right - wChild;
2430
2431 yNew = rParent.top + ((hParent - hChild) / 2);
2432 if (yNew < rScreen.top)
2433 yNew = rScreen.top;
2434 else if ((yNew + hChild) > rScreen.bottom)
2435 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436
2437 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2438 SWP_NOSIZE | SWP_NOZORDER);
2439}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002440#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002441
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442#if defined(FEAT_TOOLBAR) || defined(PROTO)
2443 void
2444gui_mch_show_toolbar(int showit)
2445{
2446 if (s_toolbarhwnd == NULL)
2447 return;
2448
2449 if (showit)
2450 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002451# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002452 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002453# define TB_SETUNICODEFORMAT 0x2005
2454# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002455 // Enable unicode support
2456 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2457 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458 ShowWindow(s_toolbarhwnd, SW_SHOW);
2459 }
2460 else
2461 ShowWindow(s_toolbarhwnd, SW_HIDE);
2462}
2463
Bram Moolenaar734a8672019-12-02 22:49:38 +01002464// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002465# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466
2467#endif
2468
2469#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2470 static void
2471add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2472{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002473 WCHAR *wn;
2474 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002476 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002477 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002478 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002480 infow.cbSize = sizeof(infow);
2481 infow.fMask = MIIM_TYPE | MIIM_ID;
2482 infow.wID = item_id;
2483 infow.fType = MFT_STRING;
2484 infow.dwTypeData = wn;
2485 infow.cch = (UINT)wcslen(wn);
2486 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2487 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488}
2489
2490 static void
2491show_tabline_popup_menu(void)
2492{
2493 HMENU tab_pmenu;
2494 long rval;
2495 POINT pt;
2496
Bram Moolenaar734a8672019-12-02 22:49:38 +01002497 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002498 if (hold_gui_events
2499# ifdef FEAT_CMDWIN
2500 || cmdwin_type != 0
2501# endif
2502 )
2503 return;
2504
2505 tab_pmenu = CreatePopupMenu();
2506 if (tab_pmenu == NULL)
2507 return;
2508
2509 if (first_tabpage->tp_next != NULL)
2510 add_tabline_popup_menu_entry(tab_pmenu,
2511 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2512 add_tabline_popup_menu_entry(tab_pmenu,
2513 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2514 add_tabline_popup_menu_entry(tab_pmenu,
2515 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2516
2517 GetCursorPos(&pt);
2518 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2519 NULL);
2520
2521 DestroyMenu(tab_pmenu);
2522
Bram Moolenaar734a8672019-12-02 22:49:38 +01002523 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524 if (rval > 0)
2525 {
2526 TCHITTESTINFO htinfo;
2527 int idx;
2528
2529 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2530 return;
2531
2532 htinfo.pt.x = pt.x;
2533 htinfo.pt.y = pt.y;
2534 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2535 if (idx == -1)
2536 idx = 0;
2537 else
2538 idx += 1;
2539
2540 send_tabline_menu_event(idx, (int)rval);
2541 }
2542}
2543
2544/*
2545 * Show or hide the tabline.
2546 */
2547 void
2548gui_mch_show_tabline(int showit)
2549{
2550 if (s_tabhwnd == NULL)
2551 return;
2552
2553 if (!showit != !showing_tabline)
2554 {
2555 if (showit)
2556 ShowWindow(s_tabhwnd, SW_SHOW);
2557 else
2558 ShowWindow(s_tabhwnd, SW_HIDE);
2559 showing_tabline = showit;
2560 }
2561}
2562
2563/*
2564 * Return TRUE when tabline is displayed.
2565 */
2566 int
2567gui_mch_showing_tabline(void)
2568{
2569 return s_tabhwnd != NULL && showing_tabline;
2570}
2571
2572/*
2573 * Update the labels of the tabline.
2574 */
2575 void
2576gui_mch_update_tabline(void)
2577{
2578 tabpage_T *tp;
2579 TCITEM tie;
2580 int nr = 0;
2581 int curtabidx = 0;
2582 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002584
2585 if (s_tabhwnd == NULL)
2586 return;
2587
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002588# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002589 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002590# define CCM_SETUNICODEFORMAT 0x2005
2591# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 // Enable unicode support
2593 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594
2595 tie.mask = TCIF_TEXT;
2596 tie.iImage = -1;
2597
Bram Moolenaar734a8672019-12-02 22:49:38 +01002598 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002599 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2600
Bram Moolenaar734a8672019-12-02 22:49:38 +01002601 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2603 {
2604 if (tp == curtab)
2605 curtabidx = nr;
2606
2607 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2608 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002609 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002610 tie.pszText = "-Empty-";
2611 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2612 tabadded = 1;
2613 }
2614
2615 get_tabline_label(tp, FALSE);
2616 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002618 wstr = enc_to_utf16(NameBuff, NULL);
2619 if (wstr != NULL)
2620 {
2621 TCITEMW tiw;
2622
2623 tiw.mask = TCIF_TEXT;
2624 tiw.iImage = -1;
2625 tiw.pszText = wstr;
2626 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2627 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002629 }
2630
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2633 TabCtrl_DeleteItem(s_tabhwnd, nr);
2634
2635 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2636 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2637
Bram Moolenaar734a8672019-12-02 22:49:38 +01002638 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002639 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2640 RedrawWindow(s_tabhwnd, NULL, NULL,
2641 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2642
2643 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2644 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2645}
2646
2647/*
2648 * Set the current tab to "nr". First tab is 1.
2649 */
2650 void
2651gui_mch_set_curtab(int nr)
2652{
2653 if (s_tabhwnd == NULL)
2654 return;
2655
2656 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2657 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2658}
2659
2660#endif
2661
2662/*
2663 * ":simalt" command.
2664 */
2665 void
2666ex_simalt(exarg_T *eap)
2667{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002668 char_u *keys = eap->arg;
2669 int fill_typebuf = FALSE;
2670 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002671
2672 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2673 while (*keys)
2674 {
2675 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002676 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002677 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2678 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002679 fill_typebuf = TRUE;
2680 }
2681 if (fill_typebuf)
2682 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002683 // Put a NOP in the typeahead buffer so that the message will get
2684 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002685 key_name[0] = K_SPECIAL;
2686 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002687 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002688 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002689#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002690 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002691#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002692 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693 }
2694}
2695
2696/*
2697 * Create the find & replace dialogs.
2698 * You can't have both at once: ":find" when replace is showing, destroys
2699 * the replace dialog first, and the other way around.
2700 */
2701#ifdef MSWIN_FIND_REPLACE
2702 static void
2703initialise_findrep(char_u *initial_string)
2704{
2705 int wword = FALSE;
2706 int mcase = !p_ic;
2707 char_u *entry_text;
2708
Bram Moolenaar734a8672019-12-02 22:49:38 +01002709 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002710 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2711
2712 s_findrep_struct.hwndOwner = s_hwnd;
2713 s_findrep_struct.Flags = FR_DOWN;
2714 if (mcase)
2715 s_findrep_struct.Flags |= FR_MATCHCASE;
2716 if (wword)
2717 s_findrep_struct.Flags |= FR_WHOLEWORD;
2718 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002719 {
2720 WCHAR *p = enc_to_utf16(entry_text, NULL);
2721 if (p != NULL)
2722 {
2723 int len = s_findrep_struct.wFindWhatLen - 1;
2724
2725 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2726 s_findrep_struct.lpstrFindWhat[len] = NUL;
2727 vim_free(p);
2728 }
2729 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 vim_free(entry_text);
2731}
2732#endif
2733
2734 static void
2735set_window_title(HWND hwnd, char *title)
2736{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002737 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 {
2739 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740
Bram Moolenaar734a8672019-12-02 22:49:38 +01002741 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002742 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2743 if (wbuf != NULL)
2744 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002745 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002746 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 }
2748 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002749 else
2750 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751}
2752
2753 void
2754gui_mch_find_dialog(exarg_T *eap)
2755{
2756#ifdef MSWIN_FIND_REPLACE
2757 if (s_findrep_msg != 0)
2758 {
2759 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2760 DestroyWindow(s_findrep_hwnd);
2761
2762 if (!IsWindow(s_findrep_hwnd))
2763 {
2764 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002765 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002766 }
2767
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002768 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002769 (void)SetFocus(s_findrep_hwnd);
2770
2771 s_findrep_is_find = TRUE;
2772 }
2773#endif
2774}
2775
2776
2777 void
2778gui_mch_replace_dialog(exarg_T *eap)
2779{
2780#ifdef MSWIN_FIND_REPLACE
2781 if (s_findrep_msg != 0)
2782 {
2783 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2784 DestroyWindow(s_findrep_hwnd);
2785
2786 if (!IsWindow(s_findrep_hwnd))
2787 {
2788 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002789 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002790 }
2791
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002792 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002793 (void)SetFocus(s_findrep_hwnd);
2794
2795 s_findrep_is_find = FALSE;
2796 }
2797#endif
2798}
2799
2800
2801/*
2802 * Set visibility of the pointer.
2803 */
2804 void
2805gui_mch_mousehide(int hide)
2806{
2807 if (hide != gui.pointer_hidden)
2808 {
2809 ShowCursor(!hide);
2810 gui.pointer_hidden = hide;
2811 }
2812}
2813
2814#ifdef FEAT_MENU
2815 static void
2816gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2817{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002818 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002819 gui_mch_mousehide(FALSE);
2820
2821 (void)TrackPopupMenu(
2822 (HMENU)menu->submenu_id,
2823 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2824 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002825 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002826 s_hwnd,
2827 NULL);
2828 /*
2829 * NOTE: The pop-up menu can eat the mouse up event.
2830 * We deal with this in normal.c.
2831 */
2832}
2833#endif
2834
2835/*
2836 * Got a message when the system will go down.
2837 */
2838 static void
2839_OnEndSession(void)
2840{
2841 getout_preserve_modified(1);
2842}
2843
2844/*
2845 * Get this message when the user clicks on the cross in the top right corner
2846 * of a Windows95 window.
2847 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002849_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850{
2851 gui_shell_closed();
2852}
2853
2854/*
2855 * Get a message when the window is being destroyed.
2856 */
2857 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002858_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859{
2860 if (!destroying)
2861 _OnClose(hwnd);
2862}
2863
2864 static void
2865_OnPaint(
2866 HWND hwnd)
2867{
2868 if (!IsMinimized(hwnd))
2869 {
2870 PAINTSTRUCT ps;
2871
Bram Moolenaar734a8672019-12-02 22:49:38 +01002872 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002874
Bram Moolenaar734a8672019-12-02 22:49:38 +01002875 // prevent multi-byte characters from misprinting on an invalid
2876 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877 if (has_mbyte)
2878 {
2879 RECT rect;
2880
2881 GetClientRect(hwnd, &rect);
2882 ps.rcPaint.left = rect.left;
2883 ps.rcPaint.right = rect.right;
2884 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885
2886 if (!IsRectEmpty(&ps.rcPaint))
2887 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002888 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2889 ps.rcPaint.right - ps.rcPaint.left + 1,
2890 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2891 }
2892
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002893 EndPaint(hwnd, &ps);
2894 }
2895}
2896
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897 static void
2898_OnSize(
2899 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002900 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 int cx,
2902 int cy)
2903{
K.Takatac81e9bf2022-01-16 14:15:49 +00002904 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905 {
2906 gui_resize_shell(cx, cy);
2907
Bram Moolenaar734a8672019-12-02 22:49:38 +01002908 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002909 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002910 }
2911}
2912
2913 static void
2914_OnSetFocus(
2915 HWND hwnd,
2916 HWND hwndOldFocus)
2917{
2918 gui_focus_change(TRUE);
2919 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002920 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921}
2922
2923 static void
2924_OnKillFocus(
2925 HWND hwnd,
2926 HWND hwndNewFocus)
2927{
2928 gui_focus_change(FALSE);
2929 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002930 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931}
2932
2933/*
2934 * Get a message when the user switches back to vim
2935 */
2936 static LRESULT
2937_OnActivateApp(
2938 HWND hwnd,
2939 BOOL fActivate,
2940 DWORD dwThreadId)
2941{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002942 // we call gui_focus_change() in _OnSetFocus()
2943 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002944 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002945}
2946
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002947 void
2948gui_mch_destroy_scrollbar(scrollbar_T *sb)
2949{
2950 DestroyWindow(sb->id);
2951}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002952
2953/*
2954 * Get current mouse coordinates in text window.
2955 */
2956 void
2957gui_mch_getmouse(int *x, int *y)
2958{
2959 RECT rct;
2960 POINT mp;
2961
2962 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002963 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964 *x = (int)(mp.x - rct.left);
2965 *y = (int)(mp.y - rct.top);
2966}
2967
2968/*
2969 * Move mouse pointer to character at (x, y).
2970 */
2971 void
2972gui_mch_setmouse(int x, int y)
2973{
2974 RECT rct;
2975
2976 (void)GetWindowRect(s_textArea, &rct);
2977 (void)SetCursorPos(x + gui.border_offset + rct.left,
2978 y + gui.border_offset + rct.top);
2979}
2980
2981 static void
2982gui_mswin_get_valid_dimensions(
2983 int w,
2984 int h,
2985 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002986 int *valid_h,
2987 int *cols,
2988 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002989{
2990 int base_width, base_height;
2991
2992 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002993 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2994 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002995 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002996 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2997 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2998 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2999 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003000 *cols = (w - base_width) / gui.char_width;
3001 *rows = (h - base_height) / gui.char_height;
3002 *valid_w = base_width + *cols * gui.char_width;
3003 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003004}
3005
3006 void
3007gui_mch_flash(int msec)
3008{
3009 RECT rc;
3010
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003011#if defined(FEAT_DIRECTX)
3012 if (IS_ENABLE_DIRECTX())
3013 DWriteContext_Flush(s_dwc);
3014#endif
3015
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 /*
3017 * Note: InvertRect() excludes right and bottom of rectangle.
3018 */
3019 rc.left = 0;
3020 rc.top = 0;
3021 rc.right = gui.num_cols * gui.char_width;
3022 rc.bottom = gui.num_rows * gui.char_height;
3023 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003024 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003025
Bram Moolenaar734a8672019-12-02 22:49:38 +01003026 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003027
3028 InvertRect(s_hdc, &rc);
3029}
3030
3031/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003032 * Check if the specified point is on-screen. (multi-monitor aware)
3033 */
3034 static BOOL
3035is_point_onscreen(int x, int y)
3036{
3037 POINT pt = {x, y};
3038
3039 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3040}
3041
3042/*
3043 * Check if the whole area of the specified window is on-screen.
3044 *
3045 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3046 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3047 * only works when the whole window is on-screen.
3048 */
3049 static BOOL
3050is_window_onscreen(HWND hwnd)
3051{
3052 RECT rc;
3053
3054 GetWindowRect(hwnd, &rc);
3055
3056 if (!is_point_onscreen(rc.left, rc.top))
3057 return FALSE;
3058 if (!is_point_onscreen(rc.left, rc.bottom))
3059 return FALSE;
3060 if (!is_point_onscreen(rc.right, rc.top))
3061 return FALSE;
3062 if (!is_point_onscreen(rc.right, rc.bottom))
3063 return FALSE;
3064 return TRUE;
3065}
3066
3067/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003068 * Return flags used for scrolling.
3069 * The SW_INVALIDATE is required when part of the window is covered or
3070 * off-screen. Refer to MS KB Q75236.
3071 */
3072 static int
3073get_scroll_flags(void)
3074{
3075 HWND hwnd;
3076 RECT rcVim, rcOther, rcDest;
3077
Bram Moolenaar185577e2020-10-29 20:08:21 +01003078 // Check if the window is (partly) off-screen.
3079 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003080 return SW_INVALIDATE;
3081
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003082 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003083 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003084 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3085 if (IsWindowVisible(hwnd))
3086 {
3087 GetWindowRect(hwnd, &rcOther);
3088 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3089 return SW_INVALIDATE;
3090 }
3091 return 0;
3092}
3093
3094/*
3095 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3096 * may not be scrolled out properly.
3097 * For gVim, when _OnScroll() is repeated, the character at the
3098 * previous cursor position may be left drawn after scroll.
3099 * The problem can be avoided by calling GetPixel() to get a pixel in
3100 * the region before ScrollWindowEx().
3101 */
3102 static void
3103intel_gpu_workaround(void)
3104{
3105 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3106}
3107
3108/*
3109 * Delete the given number of lines from the given row, scrolling up any
3110 * text further down within the scroll region.
3111 */
3112 void
3113gui_mch_delete_lines(
3114 int row,
3115 int num_lines)
3116{
3117 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003118
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119 rc.left = FILL_X(gui.scroll_region_left);
3120 rc.right = FILL_X(gui.scroll_region_right + 1);
3121 rc.top = FILL_Y(row);
3122 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3123
Bram Moolenaar92467d32017-12-05 13:22:16 +01003124#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003125 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003126 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003127 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003128 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003129 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003130#endif
3131 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003132#if defined(FEAT_DIRECTX)
3133 if (IS_ENABLE_DIRECTX())
3134 DWriteContext_Flush(s_dwc);
3135#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003136 intel_gpu_workaround();
3137 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003138 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003139 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003140 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003141
Bram Moolenaar734a8672019-12-02 22:49:38 +01003142 // This seems to be required to avoid the cursor disappearing when
3143 // scrolling such that the cursor ends up in the top-left character on
3144 // the screen... But why? (Webb)
3145 // It's probably fixed by disabling drawing the cursor while scrolling.
3146 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003147
3148 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3149 gui.scroll_region_left,
3150 gui.scroll_region_bot, gui.scroll_region_right);
3151}
3152
3153/*
3154 * Insert the given number of lines before the given row, scrolling down any
3155 * following text within the scroll region.
3156 */
3157 void
3158gui_mch_insert_lines(
3159 int row,
3160 int num_lines)
3161{
3162 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003163
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164 rc.left = FILL_X(gui.scroll_region_left);
3165 rc.right = FILL_X(gui.scroll_region_right + 1);
3166 rc.top = FILL_Y(row);
3167 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168
3169#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003170 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003171 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003172 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003173 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003174 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003175#endif
3176 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003177#if defined(FEAT_DIRECTX)
3178 if (IS_ENABLE_DIRECTX())
3179 DWriteContext_Flush(s_dwc);
3180#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003181 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003182 // The SW_INVALIDATE is required when part of the window is covered or
3183 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003184 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003185 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003186 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003187 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189 gui_clear_block(row, gui.scroll_region_left,
3190 row + num_lines - 1, gui.scroll_region_right);
3191}
3192
3193
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003195gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003196{
3197#if defined(FEAT_DIRECTX)
3198 DWriteContext_Close(s_dwc);
3199 DWrite_Final();
3200 s_dwc = NULL;
3201#endif
3202
3203 ReleaseDC(s_textArea, s_hdc);
3204 DeleteObject(s_brush);
3205
3206#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003207 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003208 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3209#endif
3210
Bram Moolenaar734a8672019-12-02 22:49:38 +01003211 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212 if (s_hwnd != NULL)
3213 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003214 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 DestroyWindow(s_hwnd);
3216 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003217}
3218
3219 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003220logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221{
3222 char *p;
3223 char *res;
3224 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003225 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003226 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003227 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003229 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3230 if (font_name == NULL)
3231 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003232 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003233 quality_name = quality_id2name((int)lf.lfQuality);
3234
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003235 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003236 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003237 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 if (res != NULL)
3239 {
3240 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003241 // make a normal font string out of the lf thing:
3242 points = pixels_to_points(
3243 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3244 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3245 sprintf((char *)p, "%s:h%d", font_name, points);
3246 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003247 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248 while (*p)
3249 {
3250 if (*p == ' ')
3251 *p = '_';
3252 ++p;
3253 }
3254 if (lf.lfItalic)
3255 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003256 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257 STRCAT(p, ":b");
3258 if (lf.lfUnderline)
3259 STRCAT(p, ":u");
3260 if (lf.lfStrikeOut)
3261 STRCAT(p, ":s");
3262 if (charset_name != NULL)
3263 {
3264 STRCAT(p, ":c");
3265 STRCAT(p, charset_name);
3266 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003267 if (quality_name != NULL)
3268 {
3269 STRCAT(p, ":q");
3270 STRCAT(p, quality_name);
3271 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 }
3273
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003274 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 return (char_u *)res;
3276}
3277
3278
3279#ifdef FEAT_MBYTE_IME
3280/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003281 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003282 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003283 */
3284 static void
3285update_im_font(void)
3286{
K.Takatac81e9bf2022-01-16 14:15:49 +00003287 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003288
3289 if (p_guifontwide != NULL && *p_guifontwide != NUL
3290 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003291 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003292 norm_logfont = lf_wide;
3293 else
3294 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003295
3296 lf = norm_logfont;
3297 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3298 // Work around when PerMonitorV2 is not enabled in the process level.
3299 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3300 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003301}
3302#endif
3303
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003304/*
3305 * Handler of gui.wide_font (p_guifontwide) changed notification.
3306 */
3307 void
3308gui_mch_wide_font_changed(void)
3309{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003310 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003311
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003312#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003313 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003314#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003315
3316 gui_mch_free_font(gui.wide_ital_font);
3317 gui.wide_ital_font = NOFONT;
3318 gui_mch_free_font(gui.wide_bold_font);
3319 gui.wide_bold_font = NOFONT;
3320 gui_mch_free_font(gui.wide_boldital_font);
3321 gui.wide_boldital_font = NOFONT;
3322
3323 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003324 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325 {
3326 if (!lf.lfItalic)
3327 {
3328 lf.lfItalic = TRUE;
3329 gui.wide_ital_font = get_font_handle(&lf);
3330 lf.lfItalic = FALSE;
3331 }
3332 if (lf.lfWeight < FW_BOLD)
3333 {
3334 lf.lfWeight = FW_BOLD;
3335 gui.wide_bold_font = get_font_handle(&lf);
3336 if (!lf.lfItalic)
3337 {
3338 lf.lfItalic = TRUE;
3339 gui.wide_boldital_font = get_font_handle(&lf);
3340 }
3341 }
3342 }
3343}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344
3345/*
3346 * Initialise vim to use the font with the given name.
3347 * Return FAIL if the font could not be loaded, OK otherwise.
3348 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003350gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351{
K.Takatac81e9bf2022-01-16 14:15:49 +00003352 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 GuiFont font = NOFONT;
3354 char_u *p;
3355
Bram Moolenaar734a8672019-12-02 22:49:38 +01003356 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003357 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003358 {
3359 lfOrig = lf;
3360 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003362 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003363 if (font == NOFONT)
3364 return FAIL;
3365
3366 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003367 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003368#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 norm_logfont = lf;
3370 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003371 if (!s_in_dpichanged)
3372 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003373#endif
3374 gui_mch_free_font(gui.norm_font);
3375 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003376 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 GetFontSize(font);
3378
K.Takatac81e9bf2022-01-16 14:15:49 +00003379 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003380 if (p != NULL)
3381 {
3382 hl_set_font_name(p);
3383
Bram Moolenaar734a8672019-12-02 22:49:38 +01003384 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3386 {
3387 vim_free(p_guifont);
3388 p_guifont = p;
3389 }
3390 else
3391 vim_free(p);
3392 }
3393
3394 gui_mch_free_font(gui.ital_font);
3395 gui.ital_font = NOFONT;
3396 gui_mch_free_font(gui.bold_font);
3397 gui.bold_font = NOFONT;
3398 gui_mch_free_font(gui.boldital_font);
3399 gui.boldital_font = NOFONT;
3400
3401 if (!lf.lfItalic)
3402 {
3403 lf.lfItalic = TRUE;
3404 gui.ital_font = get_font_handle(&lf);
3405 lf.lfItalic = FALSE;
3406 }
3407 if (lf.lfWeight < FW_BOLD)
3408 {
3409 lf.lfWeight = FW_BOLD;
3410 gui.bold_font = get_font_handle(&lf);
3411 if (!lf.lfItalic)
3412 {
3413 lf.lfItalic = TRUE;
3414 gui.boldital_font = get_font_handle(&lf);
3415 }
3416 }
3417
3418 return OK;
3419}
3420
3421#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003422# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423#endif
3424
3425/*
3426 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003427 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003428 */
3429 int
3430gui_mch_maximized(void)
3431{
3432 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003433 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003434
3435 wp.length = sizeof(WINDOWPLACEMENT);
3436 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003437 {
3438 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003439 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003440 && wp.flags == WPF_RESTORETOMAXIMIZED))
3441 return TRUE;
3442 if (wp.showCmd == SW_SHOWMINIMIZED)
3443 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003445 // Assume the window is snapped when the sizes from two APIs differ.
3446 GetWindowRect(s_hwnd, &rc);
3447 if ((rc.right - rc.left !=
3448 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3449 || (rc.bottom - rc.top !=
3450 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3451 return TRUE;
3452 }
3453 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003454}
3455
3456/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003457 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3458 * is set. Compute the new Rows and Columns. This is like resizing the
3459 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460 */
3461 void
3462gui_mch_newfont(void)
3463{
3464 RECT rect;
3465
3466 GetWindowRect(s_hwnd, &rect);
3467 if (win_socket_id == 0)
3468 {
3469 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003470 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3471 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003473 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3474 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3475 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3476 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003477 }
3478 else
3479 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003480 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003482 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483 }
3484}
3485
3486/*
3487 * Set the window title
3488 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003489 void
3490gui_mch_settitle(
3491 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003492 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003493{
3494 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3495}
3496
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003497#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003498// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3499// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500static LPCSTR mshape_idcs[] =
3501{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003502 IDC_ARROW, // arrow
3503 MAKEINTRESOURCE(0), // blank
3504 IDC_IBEAM, // beam
3505 IDC_SIZENS, // updown
3506 IDC_SIZENS, // udsizing
3507 IDC_SIZEWE, // leftright
3508 IDC_SIZEWE, // lrsizing
3509 IDC_WAIT, // busy
3510 IDC_NO, // no
3511 IDC_ARROW, // crosshair
3512 IDC_ARROW, // hand1
3513 IDC_ARROW, // hand2
3514 IDC_ARROW, // pencil
3515 IDC_ARROW, // question
3516 IDC_ARROW, // right-arrow
3517 IDC_UPARROW, // up-arrow
3518 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519};
3520
3521 void
3522mch_set_mouse_shape(int shape)
3523{
3524 LPCSTR idc;
3525
3526 if (shape == MSHAPE_HIDE)
3527 ShowCursor(FALSE);
3528 else
3529 {
3530 if (shape >= MSHAPE_NUMBERED)
3531 idc = IDC_ARROW;
3532 else
3533 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003534 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003535 if (!p_mh)
3536 {
3537 POINT mp;
3538
Bram Moolenaar734a8672019-12-02 22:49:38 +01003539 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003540 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541 (void)SetCursorPos(mp.x, mp.y);
3542 ShowCursor(TRUE);
3543 }
3544 }
3545}
3546#endif
3547
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003548#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003549/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550 * Wide version of convert_filter().
3551 */
3552 static WCHAR *
3553convert_filterW(char_u *s)
3554{
3555 char_u *tmp;
3556 int len;
3557 WCHAR *res;
3558
3559 tmp = convert_filter(s);
3560 if (tmp == NULL)
3561 return NULL;
3562 len = (int)STRLEN(s) + 3;
3563 res = enc_to_utf16(tmp, &len);
3564 vim_free(tmp);
3565 return res;
3566}
3567
3568/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003569 * Pop open a file browser and return the file selected, in allocated memory,
3570 * or NULL if Cancel is hit.
3571 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3572 * title - Title message for the file browser dialog.
3573 * dflt - Default name of file.
3574 * ext - Default extension to be added to files without extensions.
3575 * initdir - directory in which to open the browser (NULL = current dir)
3576 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003578 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003579gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580 int saving,
3581 char_u *title,
3582 char_u *dflt,
3583 char_u *ext,
3584 char_u *initdir,
3585 char_u *filter)
3586{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003587 // We always use the wide function. This means enc_to_utf16() must work,
3588 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 OPENFILENAMEW fileStruct;
3590 WCHAR fileBuf[MAXPATHL];
3591 WCHAR *wp;
3592 int i;
3593 WCHAR *titlep = NULL;
3594 WCHAR *extp = NULL;
3595 WCHAR *initdirp = NULL;
3596 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003597 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003598 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599
3600 if (dflt == NULL)
3601 fileBuf[0] = NUL;
3602 else
3603 {
3604 wp = enc_to_utf16(dflt, NULL);
3605 if (wp == NULL)
3606 fileBuf[0] = NUL;
3607 else
3608 {
3609 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3610 fileBuf[i] = wp[i];
3611 fileBuf[i] = NUL;
3612 vim_free(wp);
3613 }
3614 }
3615
Bram Moolenaar734a8672019-12-02 22:49:38 +01003616 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617 filterp = convert_filterW(filter);
3618
Bram Moolenaara80faa82020-04-12 19:37:17 +02003619 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003620# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003621 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003622 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003623# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003624 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003625# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003626
3627 if (title != NULL)
3628 titlep = enc_to_utf16(title, NULL);
3629 fileStruct.lpstrTitle = titlep;
3630
3631 if (ext != NULL)
3632 extp = enc_to_utf16(ext, NULL);
3633 fileStruct.lpstrDefExt = extp;
3634
3635 fileStruct.lpstrFile = fileBuf;
3636 fileStruct.nMaxFile = MAXPATHL;
3637 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003638 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3639 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003640 if (initdir != NULL && *initdir != NUL)
3641 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003642 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003643 initdirp = enc_to_utf16(initdir, NULL);
3644 if (initdirp != NULL)
3645 {
3646 for (wp = initdirp; *wp != NUL; ++wp)
3647 if (*wp == '/')
3648 *wp = '\\';
3649 }
3650 fileStruct.lpstrInitialDir = initdirp;
3651 }
3652
3653 /*
3654 * TODO: Allow selection of multiple files. Needs another arg to this
3655 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3656 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3657 * files that don't exist yet, so I haven't put it in. What about
3658 * OFN_PATHMUSTEXIST?
3659 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3660 */
3661 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003662# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663 if (curbuf->b_p_bin)
3664 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003665# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003667 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003669 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670
3671 vim_free(filterp);
3672 vim_free(initdirp);
3673 vim_free(titlep);
3674 vim_free(extp);
3675
K.Takata14b8d6a2022-01-20 15:05:22 +00003676 if (!ret)
3677 return NULL;
3678
3679 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003681 if (p == NULL)
3682 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683
Bram Moolenaar734a8672019-12-02 22:49:38 +01003684 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003685 SetFocus(s_hwnd);
3686
Bram Moolenaar734a8672019-12-02 22:49:38 +01003687 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003688 q = vim_strsave(shorten_fname1(p));
3689 vim_free(p);
3690 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692
3693
3694/*
3695 * Convert the string s to the proper format for a filter string by replacing
3696 * the \t and \n delimiters with \0.
3697 * Returns the converted string in allocated memory.
3698 *
3699 * Keep in sync with convert_filterW() above!
3700 */
3701 static char_u *
3702convert_filter(char_u *s)
3703{
3704 char_u *res;
3705 unsigned s_len = (unsigned)STRLEN(s);
3706 unsigned i;
3707
3708 res = alloc(s_len + 3);
3709 if (res != NULL)
3710 {
3711 for (i = 0; i < s_len; ++i)
3712 if (s[i] == '\t' || s[i] == '\n')
3713 res[i] = '\0';
3714 else
3715 res[i] = s[i];
3716 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003717 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718 res[s_len + 1] = NUL;
3719 res[s_len + 2] = NUL;
3720 }
3721 return res;
3722}
3723
3724/*
3725 * Select a directory.
3726 */
3727 char_u *
3728gui_mch_browsedir(char_u *title, char_u *initdir)
3729{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003730 // We fake this: Use a filter that doesn't select anything and a default
3731 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3733 initdir, (char_u *)_("Directory\t*.nothing\n"));
3734}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003735#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003736
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737 static void
3738_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003739 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003740 HDROP hDrop)
3741{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003742#define BUFPATHLEN _MAX_PATH
3743#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003744 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003745 char szFile[BUFPATHLEN];
3746 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3747 UINT i;
3748 char_u **fnames;
3749 POINT pt;
3750 int_u modifiers = 0;
3751
Bram Moolenaar734a8672019-12-02 22:49:38 +01003752 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003753
Bram Moolenaar734a8672019-12-02 22:49:38 +01003754 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755 DragQueryPoint(hDrop, &pt);
3756 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3757
3758 reset_VIsual();
3759
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003760 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761
3762 if (fnames != NULL)
3763 for (i = 0; i < cFiles; ++i)
3764 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003765 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3766 fnames[i] = utf16_to_enc(wszFile, NULL);
3767 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 {
3769 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3770 fnames[i] = vim_strsave((char_u *)szFile);
3771 }
3772 }
3773
3774 DragFinish(hDrop);
3775
3776 if (fnames != NULL)
3777 {
3778 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3779 modifiers |= MOUSE_SHIFT;
3780 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3781 modifiers |= MOUSE_CTRL;
3782 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3783 modifiers |= MOUSE_ALT;
3784
3785 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3786
3787 s_need_activate = TRUE;
3788 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789}
3790
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 static int
3792_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003793 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794 HWND hwndCtl,
3795 UINT code,
3796 int pos)
3797{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003798 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799 scrollbar_T *sb, *sb_info;
3800 long val;
3801 int dragging = FALSE;
3802 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 SCROLLINFO si;
3804
3805 si.cbSize = sizeof(si);
3806 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807
3808 sb = gui_mswin_find_scrollbar(hwndCtl);
3809 if (sb == NULL)
3810 return 0;
3811
Bram Moolenaar734a8672019-12-02 22:49:38 +01003812 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003813 {
3814 /*
3815 * Careful: need to get scrollbar info out of first (left) scrollbar
3816 * for window, but keep real scrollbar too because we must pass it to
3817 * gui_drag_scrollbar().
3818 */
3819 sb_info = &sb->wp->w_scrollbars[0];
3820 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003821 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003822 sb_info = sb;
3823 val = sb_info->value;
3824
3825 switch (code)
3826 {
3827 case SB_THUMBTRACK:
3828 val = pos;
3829 dragging = TRUE;
3830 if (sb->scroll_shift > 0)
3831 val <<= sb->scroll_shift;
3832 break;
3833 case SB_LINEDOWN:
3834 val++;
3835 break;
3836 case SB_LINEUP:
3837 val--;
3838 break;
3839 case SB_PAGEDOWN:
3840 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3841 break;
3842 case SB_PAGEUP:
3843 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3844 break;
3845 case SB_TOP:
3846 val = 0;
3847 break;
3848 case SB_BOTTOM:
3849 val = sb_info->max;
3850 break;
3851 case SB_ENDSCROLL:
3852 if (prev_code == SB_THUMBTRACK)
3853 {
3854 /*
3855 * "pos" only gives us 16-bit data. In case of large file,
3856 * use GetScrollPos() which returns 32-bit. Unfortunately it
3857 * is not valid while the scrollbar is being dragged.
3858 */
3859 val = GetScrollPos(hwndCtl, SB_CTL);
3860 if (sb->scroll_shift > 0)
3861 val <<= sb->scroll_shift;
3862 }
3863 break;
3864
3865 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003866 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003867 return 0;
3868 }
3869 prev_code = code;
3870
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003871 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3872 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003873
3874 /*
3875 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3876 */
3877 if (sb->wp != NULL)
3878 {
3879 scrollbar_T *sba = sb->wp->w_scrollbars;
3880 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3881
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003883 }
3884
Bram Moolenaar734a8672019-12-02 22:49:38 +01003885 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003886 s_busy_processing = TRUE;
3887
Bram Moolenaar734a8672019-12-02 22:49:38 +01003888 // When "allow_scrollbar" is FALSE still need to remember the new
3889 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890 dont_scroll = !allow_scrollbar;
3891
Bram Moolenaara338adc2018-01-31 20:51:47 +01003892 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003894 mch_enable_flush();
3895 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003896
3897 s_busy_processing = FALSE;
3898 dont_scroll = dont_scroll_save;
3899
3900 return 0;
3901}
3902
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#ifdef FEAT_XPM_W32
3905# include "xpm_w32.h"
3906#endif
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908#ifdef __MINGW32__
3909/*
3910 * Add a lot of missing defines.
3911 * They are not always missing, we need the #ifndef's.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913# ifndef IsMinimized
3914# define IsMinimized(hwnd) IsIconic(hwnd)
3915# endif
3916# ifndef IsMaximized
3917# define IsMaximized(hwnd) IsZoomed(hwnd)
3918# endif
3919# ifndef SelectFont
3920# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3921# endif
3922# ifndef GetStockBrush
3923# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3924# endif
3925# ifndef DeleteBrush
3926# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3927# endif
3928
3929# ifndef HANDLE_WM_RBUTTONDBLCLK
3930# define HANDLE_WM_RBUTTONDBLCLK(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_MBUTTONUP
3934# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3935 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3936# endif
3937# ifndef HANDLE_WM_MBUTTONDBLCLK
3938# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3939 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3940# endif
3941# ifndef HANDLE_WM_LBUTTONDBLCLK
3942# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3943 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3944# endif
3945# ifndef HANDLE_WM_RBUTTONDOWN
3946# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3947 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3948# endif
3949# ifndef HANDLE_WM_MOUSEMOVE
3950# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3951 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3952# endif
3953# ifndef HANDLE_WM_RBUTTONUP
3954# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3955 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3956# endif
3957# ifndef HANDLE_WM_MBUTTONDOWN
3958# define HANDLE_WM_MBUTTONDOWN(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_LBUTTONUP
3962# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3963 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3964# endif
3965# ifndef HANDLE_WM_LBUTTONDOWN
3966# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3967 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3968# endif
3969# ifndef HANDLE_WM_SYSCHAR
3970# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3971 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3972# endif
3973# ifndef HANDLE_WM_ACTIVATEAPP
3974# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3975 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3976# endif
3977# ifndef HANDLE_WM_WINDOWPOSCHANGING
3978# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3979 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3980# endif
3981# ifndef HANDLE_WM_VSCROLL
3982# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3983 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3984# endif
3985# ifndef HANDLE_WM_SETFOCUS
3986# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3987 ((fn)((hwnd), (HWND)(wParam)), 0L)
3988# endif
3989# ifndef HANDLE_WM_KILLFOCUS
3990# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3991 ((fn)((hwnd), (HWND)(wParam)), 0L)
3992# endif
3993# ifndef HANDLE_WM_HSCROLL
3994# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3995 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3996# endif
3997# ifndef HANDLE_WM_DROPFILES
3998# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3999 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4000# endif
4001# ifndef HANDLE_WM_CHAR
4002# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4003 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4004# endif
4005# ifndef HANDLE_WM_SYSDEADCHAR
4006# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4007 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4008# endif
4009# ifndef HANDLE_WM_DEADCHAR
4010# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4011 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4012# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004013#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015
Bram Moolenaar734a8672019-12-02 22:49:38 +01004016// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#define TEAROFF_PADDING_X 2
4018#define TEAROFF_BUTTON_PAD_X 8
4019#define TEAROFF_MIN_WIDTH 200
4020#define TEAROFF_SUBMENU_LABEL ">>"
4021#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4022
4023
Bram Moolenaar734a8672019-12-02 22:49:38 +01004024// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004026# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#endif
4028
4029
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004030#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031# define ID_BEVAL_TOOLTIP 200
4032# define BEVAL_TEXT_LEN MAXPATHL
4033
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004034# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004035// Work around old versions of basetsd.h which wrongly declares
4036// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004037# undef UINT_PTR
4038# define UINT_PTR UINT
4039# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004040
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004042static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004044
Bram Moolenaar82881492012-11-20 16:53:39 +01004045
Bram Moolenaar45360022005-07-21 21:08:21 +00004046typedef struct tagNMTTDISPINFO_NEW
4047{
4048 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004049 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004050 char szText[80];
4051 HINSTANCE hinst;
4052 UINT uFlags;
4053 LPARAM lParam;
4054} NMTTDISPINFO_NEW;
4055
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004056typedef struct tagTOOLINFOW_NEW
4057{
4058 UINT cbSize;
4059 UINT uFlags;
4060 HWND hwnd;
4061 UINT_PTR uId;
4062 RECT rect;
4063 HINSTANCE hinst;
4064 LPWSTR lpszText;
4065 LPARAM lParam;
4066 void *lpReserved;
4067} TOOLINFOW_NEW;
4068
4069typedef struct tagNMTTDISPINFOW_NEW
4070{
4071 NMHDR hdr;
4072 LPWSTR lpszText;
4073 WCHAR szText[80];
4074 HINSTANCE hinst;
4075 UINT uFlags;
4076 LPARAM lParam;
4077} NMTTDISPINFOW_NEW;
4078
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004079
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004080# ifndef TTM_SETMAXTIPWIDTH
4081# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004084# ifndef TTF_DI_SETITEM
4085# define TTF_DI_SETITEM 0x8000
4086# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004087
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004088# ifndef TTN_GETDISPINFO
4089# define TTN_GETDISPINFO (TTN_FIRST - 0)
4090# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004091
Bram Moolenaar734a8672019-12-02 22:49:38 +01004092#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004093
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004094#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004095// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4096// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004097// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004098// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004099# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4100typedef struct tagNMTTDISPINFOA {
4101 NMHDR hdr;
4102 LPSTR lpszText;
4103 char szText[80];
4104 HINSTANCE hinst;
4105 UINT uFlags;
4106 LPARAM lParam;
4107} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4108# define LPNMTTDISPINFO LPNMTTDISPINFOA
4109
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004110typedef struct tagNMTTDISPINFOW {
4111 NMHDR hdr;
4112 LPWSTR lpszText;
4113 WCHAR szText[80];
4114 HINSTANCE hinst;
4115 UINT uFlags;
4116 LPARAM lParam;
4117} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004118# endif
4119#endif
4120
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004121#ifndef TTN_GETDISPINFOW
4122# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4123#endif
4124
Bram Moolenaar734a8672019-12-02 22:49:38 +01004125// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127#ifdef FEAT_MENU
4128static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131/*
4132 * Use the system font for dialogs and tear-off menus. Remove this line to
4133 * use DLG_FONT_NAME.
4134 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004135#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
4137#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#define VIM_CLASSW L"Vim"
4139
Bram Moolenaar734a8672019-12-02 22:49:38 +01004140// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4141// thus there should be room for every dialog. For tearoffs it's made bigger
4142// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#define DLG_ALLOC_SIZE 16 * 1024
4144
4145/*
4146 * stuff for dialogs, menus, tearoffs etc.
4147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148static PWORD
4149add_dialog_element(
4150 PWORD p,
4151 DWORD lStyle,
4152 WORD x,
4153 WORD y,
4154 WORD w,
4155 WORD h,
4156 WORD Id,
4157 WORD clss,
4158 const char *caption);
4159static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004160static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004161#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164static void get_dialog_font_metrics(void);
4165
4166static int dialog_default_button = -1;
4167
Bram Moolenaar734a8672019-12-02 22:49:38 +01004168// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#ifdef FEAT_TOOLBAR
4172static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004173static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004174static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004176#else
4177# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#endif
4179
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004180#ifdef FEAT_GUI_TABLINE
4181static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004182static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004183#endif
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#ifdef FEAT_MBYTE_IME
4186static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4187static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4188#endif
4189#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4190# ifdef NOIME
4191typedef struct tagCOMPOSITIONFORM {
4192 DWORD dwStyle;
4193 POINT ptCurrentPos;
4194 RECT rcArea;
4195} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4196typedef HANDLE HIMC;
4197# endif
4198
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004199static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004200static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4201static HIMC (WINAPI *pImmGetContext)(HWND);
4202static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4203static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4204static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4205static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004206static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4207static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004208static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4209static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004210static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211static void dyn_imm_load(void);
4212#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213# define pImmGetCompositionStringW ImmGetCompositionStringW
4214# define pImmGetContext ImmGetContext
4215# define pImmAssociateContext ImmAssociateContext
4216# define pImmReleaseContext ImmReleaseContext
4217# define pImmGetOpenStatus ImmGetOpenStatus
4218# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004219# define pImmGetCompositionFontW ImmGetCompositionFontW
4220# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221# define pImmSetCompositionWindow ImmSetCompositionWindow
4222# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004223# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#endif
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#ifdef FEAT_MENU
4227/*
4228 * Figure out how high the menu bar is at the moment.
4229 */
4230 static int
4231gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004232 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
4234 static int old_menu_height = -1;
4235
4236 RECT rc1, rc2;
4237 int num;
4238 int menu_height;
4239
4240 if (gui.menu_is_active)
4241 num = GetMenuItemCount(s_menuBar);
4242 else
4243 num = 0;
4244
4245 if (num == 0)
4246 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004247 else if (IsMinimized(s_hwnd))
4248 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004249 // The height of the menu cannot be determined while the window is
4250 // minimized. Take the previous height if the menu is changed in that
4251 // state, to avoid that Vim's vertical window size accidentally
4252 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004253 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
4256 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004257 /*
4258 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4259 * seem to have been set yet, so menu wraps in default window
4260 * width which is very narrow. Instead just return height of a
4261 * single menu item. Will still be wrong when the menu really
4262 * should wrap over more than one line.
4263 */
4264 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4265 if (gui.starting)
4266 menu_height = rc1.bottom - rc1.top + 1;
4267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004269 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4270 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273
4274 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004275 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004276 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 return menu_height;
4279}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004280#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282
4283/*
4284 * Setup for the Intellimouse
4285 */
4286 static void
4287init_mouse_wheel(void)
4288{
4289
4290#ifndef SPI_GETWHEELSCROLLLINES
4291# define SPI_GETWHEELSCROLLLINES 104
4292#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004293#ifndef SPI_SETWHEELSCROLLLINES
4294# define SPI_SETWHEELSCROLLLINES 105
4295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar734a8672019-12-02 22:49:38 +01004297#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4298#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4300#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4301
Bram Moolenaar734a8672019-12-02 22:49:38 +01004302 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar734a8672019-12-02 22:49:38 +01004304 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004305 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4306 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307}
4308
4309
Bram Moolenaarae20f342019-11-05 21:09:23 +01004310/*
4311 * Intellimouse wheel handler.
4312 * Treat a mouse wheel event as if it were a scroll request.
4313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 static void
4315_OnMouseWheel(
4316 HWND hwnd,
4317 short zDelta)
4318{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 int i;
4320 int size;
4321 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004322 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (mouse_scroll_lines == 0)
4325 init_mouse_wheel();
4326
Bram Moolenaarae20f342019-11-05 21:09:23 +01004327 wp = gui_mouse_window(FIND_POPUP);
4328
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004329#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004330 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004331 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004332 cmdarg_T cap;
4333 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004334
Bram Moolenaarae20f342019-11-05 21:09:23 +01004335 // Mouse hovers over popup window, scroll it if possible.
4336 mouse_row = wp->w_winrow;
4337 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004338 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004339 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4340 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4341 clear_oparg(&oa);
4342 cap.oap = &oa;
4343 nv_mousescroll(&cap);
4344 update_screen(0);
4345 setcursor();
4346 out_flush();
4347 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004348 }
4349#endif
4350
Bram Moolenaarae20f342019-11-05 21:09:23 +01004351 if (wp == NULL || !p_scf)
4352 wp = curwin;
4353
4354 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4355 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4356 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4357 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4358 else
4359 return;
4360 size = wp->w_height;
4361
Bram Moolenaara338adc2018-01-31 20:51:47 +01004362 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 if (mouse_scroll_lines > 0
4364 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4365 {
4366 for (i = mouse_scroll_lines; i > 0; --i)
4367 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4368 }
4369 else
4370 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004371 mch_enable_flush();
4372 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373}
4374
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004375#ifdef USE_SYSMENU_FONT
4376/*
4377 * Get Menu Font.
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004381gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004382{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004383 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004384
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004385 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4386 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004387 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004388 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004389 &nm,
4390 0))
4391 return FAIL;
4392 *lf = nm.lfMenuFont;
4393 return OK;
4394}
4395#endif
4396
4397
4398#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4399/*
4400 * Set the GUI tabline font to the system menu font
4401 */
4402 static void
4403set_tabline_font(void)
4404{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004405 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004406 HFONT font;
4407 HWND hwnd;
4408 HDC hdc;
4409 HFONT hfntOld;
4410 TEXTMETRIC tm;
4411
4412 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4413 return;
4414
K.Takatac81e9bf2022-01-16 14:15:49 +00004415 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004416 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004417
4418 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4419
4420 /*
4421 * Compute the height of the font used for the tab text
4422 */
4423 hwnd = GetDesktopWindow();
4424 hdc = GetWindowDC(hwnd);
4425 hfntOld = SelectFont(hdc, font);
4426
4427 GetTextMetrics(hdc, &tm);
4428
4429 SelectFont(hdc, hfntOld);
4430 ReleaseDC(hwnd, hdc);
4431
4432 /*
4433 * The space used by the tab border and the space between the tab label
4434 * and the tab border is included as 7.
4435 */
4436 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4437}
K.Takatac81e9bf2022-01-16 14:15:49 +00004438#else
4439# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004440#endif
4441
Bram Moolenaar520470a2005-06-16 21:59:56 +00004442/*
4443 * Invoked when a setting was changed.
4444 */
4445 static LRESULT CALLBACK
4446_OnSettingChange(UINT n)
4447{
4448 if (n == SPI_SETWHEELSCROLLLINES)
4449 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4450 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004451 if (n == SPI_SETNONCLIENTMETRICS)
4452 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004453 return 0;
4454}
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#ifdef FEAT_NETBEANS_INTG
4457 static void
4458_OnWindowPosChanged(
4459 HWND hwnd,
4460 const LPWINDOWPOS lpwpos)
4461{
4462 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004463 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4466 || lpwpos->cx != cx || lpwpos->cy != cy))
4467 {
4468 x = lpwpos->x;
4469 y = lpwpos->y;
4470 cx = lpwpos->cx;
4471 cy = lpwpos->cy;
4472 netbeans_frame_moved(x, y);
4473 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004474 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004475 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
4477#endif
4478
K.Takata4f2417f2021-06-05 16:25:32 +02004479
4480static HWND hwndTip = NULL;
4481
4482 static void
4483show_sizing_tip(int cols, int rows)
4484{
4485 TOOLINFOA ti = {sizeof(ti)};
4486 char buf[32];
4487
4488 ti.hwnd = s_hwnd;
4489 ti.uId = (UINT_PTR)s_hwnd;
4490 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4491 ti.lpszText = buf;
4492 sprintf(buf, "%dx%d", cols, rows);
4493 if (hwndTip == NULL)
4494 {
4495 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4496 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4497 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4498 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4499 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4500 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4501 }
4502 else
4503 {
4504 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4505 }
4506 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4507}
4508
4509 static void
4510destroy_sizing_tip(void)
4511{
4512 if (hwndTip != NULL)
4513 {
4514 DestroyWindow(hwndTip);
4515 hwndTip = NULL;
4516 }
4517}
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 static int
4520_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 UINT fwSide,
4522 LPRECT lprc)
4523{
4524 int w, h;
4525 int valid_w, valid_h;
4526 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004527 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 w = lprc->right - lprc->left;
4530 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004531 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 w_offset = w - valid_w;
4533 h_offset = h - valid_h;
4534
4535 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4536 || fwSide == WMSZ_BOTTOMLEFT)
4537 lprc->left += w_offset;
4538 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4539 || fwSide == WMSZ_BOTTOMRIGHT)
4540 lprc->right -= w_offset;
4541
4542 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4543 || fwSide == WMSZ_TOPRIGHT)
4544 lprc->top += h_offset;
4545 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4546 || fwSide == WMSZ_BOTTOMRIGHT)
4547 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004548
4549 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return TRUE;
4551}
4552
K.Takata92000e22022-01-20 15:10:57 +00004553#ifdef FEAT_GUI_TABLINE
4554 static void
4555_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4556{
4557 if (gui_mch_showing_tabline())
4558 {
4559 POINT pt;
4560 RECT rect;
4561
4562 /*
4563 * If the cursor is on the tabline, display the tab menu
4564 */
4565 GetCursorPos(&pt);
4566 GetWindowRect(s_textArea, &rect);
4567 if (pt.y < rect.top)
4568 {
4569 show_tabline_popup_menu();
4570 return;
4571 }
4572 }
4573 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4574}
4575
4576 static void
4577_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4578{
4579 /*
4580 * If the user double clicked the tabline, create a new tab
4581 */
4582 if (gui_mch_showing_tabline())
4583 {
4584 POINT pt;
4585 RECT rect;
4586
4587 GetCursorPos(&pt);
4588 GetWindowRect(s_textArea, &rect);
4589 if (pt.y < rect.top)
4590 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4591 }
4592 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4593}
4594#endif
4595
4596 static UINT
4597_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4598{
4599 UINT result;
4600 int x, y;
4601
4602 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4603 if (result != HTCLIENT)
4604 return result;
4605
4606#ifdef FEAT_GUI_TABLINE
4607 if (gui_mch_showing_tabline())
4608 {
4609 RECT rct;
4610
4611 // If the cursor is on the GUI tabline, don't process this event
4612 GetWindowRect(s_textArea, &rct);
4613 if (yPos < rct.top)
4614 return result;
4615 }
4616#endif
4617 (void)gui_mch_get_winpos(&x, &y);
4618 xPos -= x;
4619
4620 if (xPos < 48) // <VN> TODO should use system metric?
4621 return HTBOTTOMLEFT;
4622 else
4623 return HTBOTTOMRIGHT;
4624}
4625
4626#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4627 static LRESULT
4628_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4629{
4630 switch (hdr->code)
4631 {
4632 case TTN_GETDISPINFOW:
4633 case TTN_GETDISPINFO:
4634 {
4635 char_u *str = NULL;
4636 static void *tt_text = NULL;
4637
4638 VIM_CLEAR(tt_text);
4639
4640# ifdef FEAT_GUI_TABLINE
4641 if (gui_mch_showing_tabline()
4642 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4643 {
4644 POINT pt;
4645 /*
4646 * Mouse is over the GUI tabline. Display the
4647 * tooltip for the tab under the cursor
4648 *
4649 * Get the cursor position within the tab control
4650 */
4651 GetCursorPos(&pt);
4652 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4653 {
4654 TCHITTESTINFO htinfo;
4655 int idx;
4656
4657 /*
4658 * Get the tab under the cursor
4659 */
4660 htinfo.pt.x = pt.x;
4661 htinfo.pt.y = pt.y;
4662 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4663 if (idx != -1)
4664 {
4665 tabpage_T *tp;
4666
4667 tp = find_tabpage(idx + 1);
4668 if (tp != NULL)
4669 {
4670 get_tabline_label(tp, TRUE);
4671 str = NameBuff;
4672 }
4673 }
4674 }
4675 }
4676# endif
4677# ifdef FEAT_TOOLBAR
4678# ifdef FEAT_GUI_TABLINE
4679 else
4680# endif
4681 {
4682 UINT idButton;
4683 vimmenu_T *pMenu;
4684
4685 idButton = (UINT) hdr->idFrom;
4686 pMenu = gui_mswin_find_menu(root_menu, idButton);
4687 if (pMenu)
4688 str = pMenu->strings[MENU_INDEX_TIP];
4689 }
4690# endif
4691 if (str == NULL)
4692 break;
4693
4694 // Set the maximum width, this also enables using \n for
4695 // line break.
4696 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4697
4698 if (hdr->code == TTN_GETDISPINFOW)
4699 {
4700 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4701
4702 tt_text = enc_to_utf16(str, NULL);
4703 lpdi->lpszText = tt_text;
4704 // can't show tooltip if failed
4705 }
4706 else
4707 {
4708 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4709
4710 if (STRLEN(str) < sizeof(lpdi->szText)
4711 || ((tt_text = vim_strsave(str)) == NULL))
4712 vim_strncpy((char_u *)lpdi->szText, str,
4713 sizeof(lpdi->szText) - 1);
4714 else
4715 lpdi->lpszText = tt_text;
4716 }
4717 }
4718 break;
4719
4720# ifdef FEAT_GUI_TABLINE
4721 case TCN_SELCHANGE:
4722 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4723 {
4724 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4725 return 0L;
4726 }
4727 break;
4728
4729 case NM_RCLICK:
4730 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4731 {
4732 show_tabline_popup_menu();
4733 return 0L;
4734 }
4735 break;
4736# endif
4737
4738 default:
4739 break;
4740 }
4741 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4742}
4743#endif
4744
4745#if defined(MENUHINTS) && defined(FEAT_MENU)
4746 static LRESULT
4747_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4748{
4749 if (((UINT) HIWORD(wParam)
4750 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4751 == MF_HILITE
4752 && (State & CMDLINE) == 0)
4753 {
4754 UINT idButton;
4755 vimmenu_T *pMenu;
4756 static int did_menu_tip = FALSE;
4757
4758 if (did_menu_tip)
4759 {
4760 msg_clr_cmdline();
4761 setcursor();
4762 out_flush();
4763 did_menu_tip = FALSE;
4764 }
4765
4766 idButton = (UINT)LOWORD(wParam);
4767 pMenu = gui_mswin_find_menu(root_menu, idButton);
4768 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4769 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4770 {
4771 ++msg_hist_off;
4772 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4773 --msg_hist_off;
4774 setcursor();
4775 out_flush();
4776 did_menu_tip = TRUE;
4777 }
4778 return 0L;
4779 }
4780 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4781}
4782#endif
4783
K.Takatac81e9bf2022-01-16 14:15:49 +00004784 static LRESULT
4785_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4786{
4787 s_dpi = ydpi;
4788 s_in_dpichanged = TRUE;
4789 //TRACE("DPI: %d", ydpi);
4790
4791 update_scrollbar_size();
4792 update_toolbar_size();
4793 set_tabline_font();
4794
4795 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4796 gui_get_wide_font();
4797 gui_mswin_get_menu_height(FALSE);
4798#ifdef FEAT_MBYTE_IME
4799 im_set_position(gui.row, gui.col);
4800#endif
4801 InvalidateRect(hwnd, NULL, TRUE);
4802
4803 s_in_dpichanged = FALSE;
4804 return 0L;
4805}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807
4808 static LRESULT CALLBACK
4809_WndProc(
4810 HWND hwnd,
4811 UINT uMsg,
4812 WPARAM wParam,
4813 LPARAM lParam)
4814{
4815 /*
4816 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4817 hwnd, uMsg, wParam, lParam);
4818 */
4819
4820 HandleMouseHide(uMsg, lParam);
4821
4822 s_uMsg = uMsg;
4823 s_wParam = wParam;
4824 s_lParam = lParam;
4825
4826 switch (uMsg)
4827 {
4828 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4829 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004830 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004832 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4834 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4835 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4836 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4837#ifdef FEAT_MENU
4838 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4839#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004840 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4841 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4843 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004844 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4845 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4847 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4848 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4849#ifdef FEAT_NETBEANS_INTG
4850 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4851#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004852#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004853 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4854 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004855#endif
K.Takata92000e22022-01-20 15:10:57 +00004856 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004857
Bram Moolenaar734a8672019-12-02 22:49:38 +01004858 case WM_QUERYENDSESSION: // System wants to go down.
4859 gui_shell_closed(); // Will exit when no changed buffers.
4860 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004863 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004866 return 0L;
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 break;
4869
4870 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004871 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4872 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004873 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 return 0L;
4875
4876 case WM_SYSCHAR:
4877 /*
4878 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4879 * shortcut key, handle like a typed ALT key, otherwise call Windows
4880 * ALT key handling.
4881 */
4882#ifdef FEAT_MENU
4883 if ( !gui.menu_is_active
4884 || p_wak[0] == 'n'
4885 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4886 )
4887#endif
4888 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004889 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 return 0L;
4891 }
4892#ifdef FEAT_MENU
4893 else
K.Takata4ac893f2022-01-20 12:44:28 +00004894 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
4896
4897 case WM_SYSKEYUP:
4898#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004899 // This used to be done only when menu is active: ALT key is used for
4900 // that. But that caused problems when menu is disabled and using
4901 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4902 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004903 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004905 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906#endif
4907
K.Takata4f2417f2021-06-05 16:25:32 +02004908 case WM_EXITSIZEMOVE:
4909 destroy_sizing_tip();
4910 break;
4911
Bram Moolenaar734a8672019-12-02 22:49:38 +01004912 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
4915 case WM_MOUSEWHEEL:
4916 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004917 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
Bram Moolenaar734a8672019-12-02 22:49:38 +01004919 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004920 case WM_SETTINGCHANGE:
4921 return _OnSettingChange((UINT)wParam);
4922
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004923#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004925 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#endif
K.Takata92000e22022-01-20 15:10:57 +00004927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928#if defined(MENUHINTS) && defined(FEAT_MENU)
4929 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004930 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933#ifdef FEAT_MBYTE_IME
4934 case WM_IME_NOTIFY:
4935 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004936 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004937 return 1L;
4938
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 case WM_IME_COMPOSITION:
4940 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004941 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004942 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004944 case WM_DPICHANGED:
4945 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4946 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004950 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952#endif
K.Takata92000e22022-01-20 15:10:57 +00004953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955
K.Takata4ac893f2022-01-20 12:44:28 +00004956 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957}
4958
4959/*
4960 * End of call-back routines
4961 */
4962
Bram Moolenaar734a8672019-12-02 22:49:38 +01004963// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964HWND vim_parent_hwnd = NULL;
4965
4966 static BOOL CALLBACK
4967FindWindowTitle(HWND hwnd, LPARAM lParam)
4968{
4969 char buf[2048];
4970 char *title = (char *)lParam;
4971
4972 if (GetWindowText(hwnd, buf, sizeof(buf)))
4973 {
4974 if (strstr(buf, title) != NULL)
4975 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004976 // Found it. Store the window ref. and quit searching if MDI
4977 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004979 if (vim_parent_hwnd != NULL)
4980 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004983 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984}
4985
4986/*
4987 * Invoked for '-P "title"' argument: search for parent application to open
4988 * our window in.
4989 */
4990 void
4991gui_mch_set_parent(char *title)
4992{
4993 EnumWindows(FindWindowTitle, (LPARAM)title);
4994 if (vim_parent_hwnd == NULL)
4995 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004996 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 mch_exit(2);
4998 }
4999}
5000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005001#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 static void
5003ole_error(char *arg)
5004{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005005 char buf[IOSIZE];
5006
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005007# ifdef VIMDLL
5008 gui.in_use = mch_is_gui_executable();
5009# endif
5010
Bram Moolenaar734a8672019-12-02 22:49:38 +01005011 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005012 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005013 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005014 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005018#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5019 static char *
5020gvim_error(void)
5021{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005022 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005023
5024 if (starting)
5025 {
5026 mch_errmsg(msg);
5027 mch_errmsg("\n");
5028 mch_exit(2);
5029 }
5030 return msg;
5031}
5032
5033 char *
5034gui_mch_do_spawn(char_u *arg)
5035{
5036 int len;
5037# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5038 char_u *session = NULL;
5039 LPWSTR tofree1 = NULL;
5040# endif
5041 WCHAR name[MAX_PATH];
5042 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5043 STARTUPINFOW si = {sizeof(si)};
5044 PROCESS_INFORMATION pi;
5045
5046 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5047 goto error;
5048 p = wcsrchr(name, L'\\');
5049 if (p == NULL)
5050 goto error;
5051 // Replace the executable name from vim(d).exe to gvim(d).exe.
5052# ifdef DEBUG
5053 wcscpy(p + 1, L"gvimd.exe");
5054# else
5055 wcscpy(p + 1, L"gvim.exe");
5056# endif
5057
5058# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5059 if (starting)
5060# endif
5061 {
5062 // Pass the command line to the new process.
5063 p = GetCommandLineW();
5064 // Skip 1st argument.
5065 while (*p && *p != L' ' && *p != L'\t')
5066 {
5067 if (*p == L'"')
5068 {
5069 while (*p && *p != L'"')
5070 ++p;
5071 if (*p)
5072 ++p;
5073 }
5074 else
5075 ++p;
5076 }
5077 cmd = p;
5078 }
5079# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5080 else
5081 {
5082 // Create a session file and pass it to the new process.
5083 LPWSTR wsession;
5084 char_u *savebg;
5085 int ret;
5086
5087 session = vim_tempname('s', FALSE);
5088 if (session == NULL)
5089 goto error;
5090 savebg = p_bg;
5091 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5092 ret = write_session_file(session);
5093 vim_free(p_bg);
5094 p_bg = savebg;
5095 if (!ret)
5096 goto error;
5097 wsession = enc_to_utf16(session, NULL);
5098 if (wsession == NULL)
5099 goto error;
5100 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005101 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005102 if (cmd == NULL)
5103 {
5104 vim_free(wsession);
5105 goto error;
5106 }
5107 tofree1 = cmd;
5108 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5109 wsession, wsession);
5110 vim_free(wsession);
5111 }
5112# endif
5113
5114 // Check additional arguments to the `:gui` command.
5115 if (arg != NULL)
5116 {
5117 warg = enc_to_utf16(arg, NULL);
5118 if (warg == NULL)
5119 goto error;
5120 tofree2 = warg;
5121 }
5122 else
5123 warg = L"";
5124
5125 // Set up the new command line.
5126 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005127 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005128 if (newcmd == NULL)
5129 goto error;
5130 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5131
5132 // Spawn a new GUI process.
5133 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5134 NULL, NULL, &si, &pi))
5135 goto error;
5136 CloseHandle(pi.hProcess);
5137 CloseHandle(pi.hThread);
5138 mch_exit(0);
5139
5140error:
5141# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5142 if (session)
5143 mch_remove(session);
5144 vim_free(session);
5145 vim_free(tofree1);
5146# endif
5147 vim_free(newcmd);
5148 vim_free(tofree2);
5149 return gvim_error();
5150}
5151#endif
5152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153/*
5154 * Parse the GUI related command-line arguments. Any arguments used are
5155 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005156 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 */
5158 void
5159gui_mch_prepare(int *argc, char **argv)
5160{
5161 int silent = FALSE;
5162 int idx;
5163
Bram Moolenaar734a8672019-12-02 22:49:38 +01005164 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5166 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005167 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5169 && (argv[2][0] == '-' || argv[2][0] == '/'))
5170 {
5171 silent = TRUE;
5172 idx = 2;
5173 }
5174 else
5175 idx = 1;
5176
Bram Moolenaar734a8672019-12-02 22:49:38 +01005177 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (STRICMP(argv[idx] + 1, "register") == 0)
5179 {
5180#ifdef FEAT_OLE
5181 RegisterMe(silent);
5182 mch_exit(0);
5183#else
5184 if (!silent)
5185 ole_error("register");
5186 mch_exit(2);
5187#endif
5188 }
5189
Bram Moolenaar734a8672019-12-02 22:49:38 +01005190 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5192 {
5193#ifdef FEAT_OLE
5194 UnregisterMe(!silent);
5195 mch_exit(0);
5196#else
5197 if (!silent)
5198 ole_error("unregister");
5199 mch_exit(2);
5200#endif
5201 }
5202
Bram Moolenaar734a8672019-12-02 22:49:38 +01005203 // Ignore an -embedding argument. It is only relevant if the
5204 // application wants to treat the case when it is started manually
5205 // differently from the case where it is started via automation (and
5206 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5208 {
5209#ifdef FEAT_OLE
5210 *argc = 1;
5211#else
5212 ole_error("embedding");
5213 mch_exit(2);
5214#endif
5215 }
5216 }
5217
5218#ifdef FEAT_OLE
5219 {
5220 int bDoRestart = FALSE;
5221
5222 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005223 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (bDoRestart)
5225 mch_exit(0);
5226 }
5227#endif
5228
5229#ifdef FEAT_NETBEANS_INTG
5230 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005231 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 int arg;
5233
5234 for (arg = 1; arg < *argc; arg++)
5235 if (strncmp("-nb", argv[arg], 3) == 0)
5236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 netbeansArg = argv[arg];
5238 mch_memmove(&argv[arg], &argv[arg + 1],
5239 (--*argc - arg) * sizeof(char *));
5240 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005241 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245}
5246
K.Takatac81e9bf2022-01-16 14:15:49 +00005247 static void
5248load_dpi_func(void)
5249{
5250 HMODULE hUser32;
5251
5252 hUser32 = GetModuleHandle("user32.dll");
5253 if (hUser32 == NULL)
5254 goto fail;
5255
5256 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5257 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5258 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5259 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5260 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5261 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5262
5263 if (pSetThreadDpiAwarenessContext != NULL)
5264 {
5265 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5266 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5267 if (oldctx != NULL)
5268 {
5269 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5270 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5271#ifdef DEBUG
5272 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5273 {
5274 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5275 }
5276#endif
5277 return;
5278 }
5279 }
5280
5281fail:
5282 // Disable PerMonitorV2 APIs.
5283 pGetDpiForSystem = stubGetDpiForSystem;
5284 pGetDpiForWindow = NULL;
5285 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5286 pSetThreadDpiAwarenessContext = NULL;
5287 pGetAwarenessFromDpiAwarenessContext = NULL;
5288}
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290/*
5291 * Initialise the GUI. Create all the windows, set up all the call-backs
5292 * etc.
5293 */
5294 int
5295gui_mch_init(void)
5296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005298 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
Bram Moolenaar734a8672019-12-02 22:49:38 +01005301 // Return here if the window was already opened (happens when
5302 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005304 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 /*
5307 * Load the tearoff bitmap
5308 */
5309#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005310 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#endif
5312
K.Takatac81e9bf2022-01-16 14:15:49 +00005313 load_dpi_func();
5314
5315 s_dpi = pGetDpiForSystem();
5316 update_scrollbar_size();
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005319 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#endif
5321 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005322#ifdef FEAT_TOOLBAR
5323 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5327
Bram Moolenaar734a8672019-12-02 22:49:38 +01005328 // First try using the wide version, so that we can use any title.
5329 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005330 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005332 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 wndclassw.lpfnWndProc = _WndProc;
5334 wndclassw.cbClsExtra = 0;
5335 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005336 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5338 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5339 wndclassw.hbrBackground = s_brush;
5340 wndclassw.lpszMenuName = NULL;
5341 wndclassw.lpszClassName = szVimWndClassW;
5342
K.Takata4ac893f2022-01-20 12:44:28 +00005343 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 if (vim_parent_hwnd != NULL)
5348 {
5349#ifdef HAVE_TRY_EXCEPT
5350 __try
5351 {
5352#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005353 // Open inside the specified parent window.
5354 // TODO: last argument should point to a CLIENTCREATESTRUCT
5355 // structure.
5356 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005358 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005359 WS_OVERLAPPEDWINDOW | WS_CHILD
5360 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5362 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005363 100, // Any value will do
5364 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005366 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367#ifdef HAVE_TRY_EXCEPT
5368 }
5369 __except(EXCEPTION_EXECUTE_HANDLER)
5370 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005371 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373#endif
5374 if (s_hwnd == NULL)
5375 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005376 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 mch_exit(2);
5378 }
5379 }
5380 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005381 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005382 // If the provided windowid is not valid reset it to zero, so that it
5383 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005384 if (IsWindow((HWND)win_socket_id) <= 0)
5385 win_socket_id = 0;
5386
Bram Moolenaar734a8672019-12-02 22:49:38 +01005387 // Create a window. If win_socket_id is not zero without border and
5388 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005389 s_hwnd = CreateWindowW(
5390 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005391 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5392 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005393 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5394 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005395 100, // Any value will do
5396 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005397 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005398 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005399 if (s_hwnd != NULL && win_socket_id != 0)
5400 {
5401 SetParent(s_hwnd, (HWND)win_socket_id);
5402 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5403 }
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406 if (s_hwnd == NULL)
5407 return FAIL;
5408
K.Takatac81e9bf2022-01-16 14:15:49 +00005409 if (pGetDpiForWindow != NULL)
5410 {
5411 s_dpi = pGetDpiForWindow(s_hwnd);
5412 update_scrollbar_size();
5413 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5414 }
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5417 dyn_imm_load();
5418#endif
5419
Bram Moolenaar734a8672019-12-02 22:49:38 +01005420 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005421 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005422 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005423 wndclassw.style = CS_OWNDC;
5424 wndclassw.lpfnWndProc = _TextAreaWndProc;
5425 wndclassw.cbClsExtra = 0;
5426 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005427 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005428 wndclassw.hIcon = NULL;
5429 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5430 wndclassw.hbrBackground = NULL;
5431 wndclassw.lpszMenuName = NULL;
5432 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005433
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005434 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 return FAIL;
5436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005438 s_textArea = CreateWindowExW(
5439 0,
5440 szTextAreaClassW, L"Vim text area",
5441 WS_CHILD | WS_VISIBLE, 0, 0,
5442 100, // Any value will do for now
5443 100, // Any value will do for now
5444 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005445 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (s_textArea == NULL)
5448 return FAIL;
5449
Bram Moolenaar20321902016-02-17 12:30:17 +01005450#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005451 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005452 {
5453 HANDLE hIcon = NULL;
5454
5455 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005456 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005457 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005458#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460#ifdef FEAT_MENU
5461 s_menuBar = CreateMenu();
5462#endif
5463 s_hdc = GetDC(s_textArea);
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar734a8672019-12-02 22:49:38 +01005467 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005468 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
Bram Moolenaar734a8672019-12-02 22:49:38 +01005470 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 gui_mch_def_colors();
5472
Bram Moolenaar734a8672019-12-02 22:49:38 +01005473 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5474 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 set_normal_colors();
5476
5477 /*
5478 * Check that none of the colors are the same as the background color.
5479 * Then store the current values as the defaults.
5480 */
5481 gui_check_colors();
5482 gui.def_norm_pixel = gui.norm_pixel;
5483 gui.def_back_pixel = gui.back_pixel;
5484
Bram Moolenaar734a8672019-12-02 22:49:38 +01005485 // Get the colors for the highlight groups (gui_check_colors() might have
5486 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 highlight_gui_started();
5488
5489 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005490 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005492 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
5494 /*
5495 * Set up for Intellimouse processing
5496 */
5497 init_mouse_wheel();
5498
5499 /*
5500 * compute a couple of metrics used for the dialogs
5501 */
5502 get_dialog_font_metrics();
5503#ifdef FEAT_TOOLBAR
5504 /*
5505 * Create the toolbar
5506 */
5507 initialise_toolbar();
5508#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005509#ifdef FEAT_GUI_TABLINE
5510 /*
5511 * Create the tabline
5512 */
5513 initialise_tabline();
5514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#ifdef MSWIN_FIND_REPLACE
5516 /*
5517 * Initialise the dialog box stuff
5518 */
5519 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5520
Bram Moolenaar734a8672019-12-02 22:49:38 +01005521 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005523 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005525 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5527 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5528 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005531#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005532# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005533// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005534# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005535# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005536# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005537# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005538 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005539 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005540#endif
5541
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005542#ifdef FEAT_RENDER_OPTIONS
5543 if (p_rop)
5544 (void)gui_mch_set_rendering_options(p_rop);
5545#endif
5546
Bram Moolenaar748bf032005-02-02 23:04:36 +00005547theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005548 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005549 display_errors();
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return OK;
5552}
5553
5554/*
5555 * Get the size of the screen, taking position on multiple monitors into
5556 * account (if supported).
5557 */
5558 static void
5559get_work_area(RECT *spi_rect)
5560{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005561 HMONITOR mon;
5562 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar734a8672019-12-02 22:49:38 +01005564 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005565 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005566 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005568 moninfo.cbSize = sizeof(MONITORINFO);
5569 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005571 *spi_rect = moninfo.rcWork;
5572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005575 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5577}
5578
5579/*
5580 * Set the size of the window to the given width and height in pixels.
5581 */
5582 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005583gui_mch_set_shellsize(
5584 int width,
5585 int height,
5586 int min_width UNUSED,
5587 int min_height UNUSED,
5588 int base_width UNUSED,
5589 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005590 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005593 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
Bram Moolenaar734a8672019-12-02 22:49:38 +01005596 // Try to keep window completely on screen.
5597 // Get position of the screen work area. This is the part that is not
5598 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 get_work_area(&workarea_rect);
5600
Bram Moolenaar734a8672019-12-02 22:49:38 +01005601 // Resizing a maximized window looks very strange, unzoom it first.
5602 // But don't do it when still starting up, it may have been requested in
5603 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005604 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005606
5607 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Bram Moolenaar734a8672019-12-02 22:49:38 +01005609 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005610 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5611 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5612 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5613 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5614 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5615 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
Bram Moolenaar734a8672019-12-02 22:49:38 +01005617 // The following should take care of keeping Vim on the same monitor, no
5618 // matter if the secondary monitor is left or right of the primary
5619 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005620 window_rect.right = window_rect.left + win_width;
5621 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005622
Bram Moolenaar734a8672019-12-02 22:49:38 +01005623 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005624 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5625 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005627 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5628 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005630 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5631 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005633 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5634 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005636 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5637 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
5639 SetActiveWindow(s_hwnd);
5640 SetFocus(s_hwnd);
5641
Bram Moolenaar734a8672019-12-02 22:49:38 +01005642 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
5645
5646
5647 void
5648gui_mch_set_scrollbar_thumb(
5649 scrollbar_T *sb,
5650 long val,
5651 long size,
5652 long max)
5653{
5654 SCROLLINFO info;
5655
5656 sb->scroll_shift = 0;
5657 while (max > 32767)
5658 {
5659 max = (max + 1) >> 1;
5660 val >>= 1;
5661 size >>= 1;
5662 ++sb->scroll_shift;
5663 }
5664
5665 if (sb->scroll_shift > 0)
5666 ++size;
5667
5668 info.cbSize = sizeof(info);
5669 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5670 info.nPos = val;
5671 info.nMin = 0;
5672 info.nMax = max;
5673 info.nPage = size;
5674 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5675}
5676
5677
5678/*
5679 * Set the current text font.
5680 */
5681 void
5682gui_mch_set_font(GuiFont font)
5683{
5684 gui.currFont = font;
5685}
5686
5687
5688/*
5689 * Set the current text foreground color.
5690 */
5691 void
5692gui_mch_set_fg_color(guicolor_T color)
5693{
5694 gui.currFgColor = color;
5695}
5696
5697/*
5698 * Set the current text background color.
5699 */
5700 void
5701gui_mch_set_bg_color(guicolor_T color)
5702{
5703 gui.currBgColor = color;
5704}
5705
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005706/*
5707 * Set the current text special color.
5708 */
5709 void
5710gui_mch_set_sp_color(guicolor_T color)
5711{
5712 gui.currSpColor = color;
5713}
5714
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005715#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716/*
5717 * Multi-byte handling, originally by Sung-Hoon Baek.
5718 * First static functions (no prototypes generated).
5719 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005720# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005721# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005722# endif
5723# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 * handle WM_IME_NOTIFY message
5727 */
5728 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005729_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 LRESULT lResult = 0;
5732 HIMC hImc;
5733
5734 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5735 return lResult;
5736 switch (dwCommand)
5737 {
5738 case IMN_SETOPENSTATUS:
5739 if (pImmGetOpenStatus(hImc))
5740 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005741 LOGFONTW lf = norm_logfont;
5742 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5743 // Work around when PerMonitorV2 is not enabled in the process level.
5744 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5745 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 im_set_position(gui.row, gui.col);
5747
Bram Moolenaar734a8672019-12-02 22:49:38 +01005748 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 State &= ~LANGMAP;
5750 if (State & INSERT)
5751 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005752# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005753 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5755 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005756 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 int old_row = gui.row;
5758 int old_col = gui.col;
5759
5760 // This must be called here before
5761 // status_redraw_curbuf(), otherwise the mode
5762 // message may appear in the wrong position.
5763 showmode();
5764 status_redraw_curbuf();
5765 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005766 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 gui.row = old_row;
5768 gui.col = old_col;
5769 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772 }
5773 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005774 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 lResult = 0;
5776 break;
5777 }
5778 pImmReleaseContext(hWnd, hImc);
5779 return lResult;
5780}
5781
5782 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005783_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 char_u *ret;
5786 int len;
5787
Bram Moolenaar734a8672019-12-02 22:49:38 +01005788 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 return 0;
5790
5791 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5792 if (ret != NULL)
5793 {
5794 add_to_input_buf_csi(ret, len);
5795 vim_free(ret);
5796 return 1;
5797 }
5798 return 0;
5799}
5800
5801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 * void GetResultStr()
5803 *
5804 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5805 * get complete composition string
5806 */
5807 static char_u *
5808GetResultStr(HWND hwnd, int GCS, int *lenp)
5809{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005810 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005811 LONG ret;
5812 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 char_u *convbuf = NULL;
5814
5815 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5816 return NULL;
5817
K.Takatab0b2b732022-01-19 12:59:21 +00005818 // Get the length of the composition string.
5819 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5820 if (ret <= 0)
5821 return NULL;
5822
5823 // Allocate the requested buffer plus space for the NUL character.
5824 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (buf == NULL)
5826 return NULL;
5827
K.Takatab0b2b732022-01-19 12:59:21 +00005828 // Reads in the composition string.
5829 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5830 *lenp = ret / sizeof(WCHAR);
5831
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005832 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 pImmReleaseContext(hwnd, hIMC);
5834 vim_free(buf);
5835 return convbuf;
5836}
5837#endif
5838
Bram Moolenaar734a8672019-12-02 22:49:38 +01005839// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005840#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842/*
5843 * set font to IM.
5844 */
5845 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005846im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 HIMC hImc;
5849
5850 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5851 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005852 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 pImmReleaseContext(s_hwnd, hImc);
5854 }
5855}
5856
5857/*
5858 * Notify cursor position to IM.
5859 */
5860 void
5861im_set_position(int row, int col)
5862{
5863 HIMC hImc;
5864
5865 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5866 {
5867 COMPOSITIONFORM cfs;
5868
5869 cfs.dwStyle = CFS_POINT;
5870 cfs.ptCurrentPos.x = FILL_X(col);
5871 cfs.ptCurrentPos.y = FILL_Y(row);
5872 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005873 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5874 {
5875 // Work around when PerMonitorV2 is not enabled in the process level.
5876 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5877 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 pImmSetCompositionWindow(hImc, &cfs);
5880
5881 pImmReleaseContext(s_hwnd, hImc);
5882 }
5883}
5884
5885/*
5886 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5887 */
5888 void
5889im_set_active(int active)
5890{
5891 HIMC hImc;
5892 static HIMC hImcOld = (HIMC)0;
5893
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005894# ifdef VIMDLL
5895 if (!gui.in_use && !gui.starting)
5896 {
5897 mbyte_im_set_active(active);
5898 return;
5899 }
5900# endif
5901
Bram Moolenaar734a8672019-12-02 22:49:38 +01005902 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
5904 if (p_imdisable)
5905 {
5906 if (hImcOld == (HIMC)0)
5907 {
5908 hImcOld = pImmGetContext(s_hwnd);
5909 if (hImcOld)
5910 pImmAssociateContext(s_hwnd, (HIMC)0);
5911 }
5912 active = FALSE;
5913 }
5914 else if (hImcOld != (HIMC)0)
5915 {
5916 pImmAssociateContext(s_hwnd, hImcOld);
5917 hImcOld = (HIMC)0;
5918 }
5919
5920 hImc = pImmGetContext(s_hwnd);
5921 if (hImc)
5922 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005923 /*
5924 * for Korean ime
5925 */
5926 HKL hKL = GetKeyboardLayout(0);
5927
5928 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5929 {
5930 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5931 static BOOL bSaved = FALSE;
5932
5933 if (active)
5934 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005935 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005936 if (bSaved)
5937 pImmSetConversionStatus(hImc, dwConversionSaved,
5938 dwSentenceSaved);
5939 bSaved = FALSE;
5940 }
5941 else
5942 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005943 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005944 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5945 &dwSentenceSaved))
5946 {
5947 bSaved = TRUE;
5948 pImmSetConversionStatus(hImc,
5949 dwConversionSaved & ~(IME_CMODE_NATIVE
5950 | IME_CMODE_FULLSHAPE),
5951 dwSentenceSaved);
5952 }
5953 }
5954 }
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 pImmSetOpenStatus(hImc, active);
5957 pImmReleaseContext(s_hwnd, hImc);
5958 }
5959 }
5960}
5961
5962/*
5963 * Get IM status. When IM is on, return not 0. Else return 0.
5964 */
5965 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005966im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
5968 int status = 0;
5969 HIMC hImc;
5970
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005971# ifdef VIMDLL
5972 if (!gui.in_use && !gui.starting)
5973 return mbyte_im_get_status();
5974# endif
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5977 {
5978 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5979 pImmReleaseContext(s_hwnd, hImc);
5980 }
5981 return status;
5982}
5983
Bram Moolenaar734a8672019-12-02 22:49:38 +01005984#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005987/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005988 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005989 */
5990 static void
5991latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5992{
5993 int c;
5994
Bram Moolenaarca003e12006-03-17 23:19:38 +00005995 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005996 {
5997 c = *text++;
5998 switch (c)
5999 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006000 case 0xa4: c = 0x20ac; break; // euro
6001 case 0xa6: c = 0x0160; break; // S hat
6002 case 0xa8: c = 0x0161; break; // S -hat
6003 case 0xb4: c = 0x017d; break; // Z hat
6004 case 0xb8: c = 0x017e; break; // Z -hat
6005 case 0xbc: c = 0x0152; break; // OE
6006 case 0xbd: c = 0x0153; break; // oe
6007 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006008 }
6009 *unicodebuf++ = c;
6010 }
6011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013#ifdef FEAT_RIGHTLEFT
6014/*
6015 * What is this for? In the case where you are using Win98 or Win2K or later,
6016 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6017 * reverses the string sent to the TextOut... family. This sucks, because we
6018 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6019 * way to tell Windblows not to do this!
6020 *
6021 * The short of it is that this 'RevOut' only gets called if you are running
6022 * one of the new, "improved" MS OSes, and only if you are running in
6023 * 'rightleft' mode. It makes display take *slightly* longer, but not
6024 * noticeably so.
6025 */
6026 static void
6027RevOut( HDC s_hdc,
6028 int col,
6029 int row,
6030 UINT foptions,
6031 CONST RECT *pcliprect,
6032 LPCTSTR text,
6033 UINT len,
6034 CONST INT *padding)
6035{
6036 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006038 for (ix = 0; ix < (int)len; ++ix)
6039 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6040 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041}
6042#endif
6043
Bram Moolenaar92467d32017-12-05 13:22:16 +01006044 static void
6045draw_line(
6046 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006047 int y1,
6048 int x2,
6049 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006050 COLORREF color)
6051{
6052#if defined(FEAT_DIRECTX)
6053 if (IS_ENABLE_DIRECTX())
6054 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6055 else
6056#endif
6057 {
6058 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6059 HPEN old_pen = SelectObject(s_hdc, hpen);
6060 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006061 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006062 LineTo(s_hdc, x2, y2);
6063 DeleteObject(SelectObject(s_hdc, old_pen));
6064 }
6065}
6066
6067 static void
6068set_pixel(
6069 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006070 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006071 COLORREF color)
6072{
6073#if defined(FEAT_DIRECTX)
6074 if (IS_ENABLE_DIRECTX())
6075 DWriteContext_SetPixel(s_dwc, x, y, color);
6076 else
6077#endif
6078 SetPixel(s_hdc, x, y, color);
6079}
6080
6081 static void
6082fill_rect(
6083 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006084 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006085 COLORREF color)
6086{
6087#if defined(FEAT_DIRECTX)
6088 if (IS_ENABLE_DIRECTX())
6089 DWriteContext_FillRect(s_dwc, rcp, color);
6090 else
6091#endif
6092 {
6093 HBRUSH hbr2;
6094
6095 if (hbr == NULL)
6096 hbr2 = CreateSolidBrush(color);
6097 else
6098 hbr2 = hbr;
6099 FillRect(s_hdc, rcp, hbr2);
6100 if (hbr == NULL)
6101 DeleteBrush(hbr2);
6102 }
6103}
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 void
6106gui_mch_draw_string(
6107 int row,
6108 int col,
6109 char_u *text,
6110 int len,
6111 int flags)
6112{
6113 static int *padding = NULL;
6114 static int pad_size = 0;
6115 int i;
6116 const RECT *pcliprect = NULL;
6117 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 static WCHAR *unicodebuf = NULL;
6119 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006120 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 int y;
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 /*
6125 * Italic and bold text seems to have an extra row of pixels at the bottom
6126 * (below where the bottom of the character should be). If we draw the
6127 * characters with a solid background, the top row of pixels in the
6128 * character below will be overwritten. We can fix this by filling in the
6129 * background ourselves, to the correct character proportions, and then
6130 * writing the character in transparent mode. Still have a problem when
6131 * the character is "_", which gets written on to the character below.
6132 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6133 * pixel in their slots, which fixes the problem with the bottom row of
6134 * pixels. We still need this code because otherwise the top row of pixels
6135 * becomes a problem. - webb.
6136 */
6137 static HBRUSH hbr_cache[2] = {NULL, NULL};
6138 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6139 static int brush_lru = 0;
6140 HBRUSH hbr;
6141 RECT rc;
6142
6143 if (!(flags & DRAW_TRANSP))
6144 {
6145 /*
6146 * Clear background first.
6147 * Note: FillRect() excludes right and bottom of rectangle.
6148 */
6149 rc.left = FILL_X(col);
6150 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (has_mbyte)
6152 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006153 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006154 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 }
6156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 rc.right = FILL_X(col + len);
6158 rc.bottom = FILL_Y(row + 1);
6159
Bram Moolenaar734a8672019-12-02 22:49:38 +01006160 // Cache the created brush, that saves a lot of time. We need two:
6161 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (gui.currBgColor == brush_color[0])
6163 {
6164 hbr = hbr_cache[0];
6165 brush_lru = 1;
6166 }
6167 else if (gui.currBgColor == brush_color[1])
6168 {
6169 hbr = hbr_cache[1];
6170 brush_lru = 0;
6171 }
6172 else
6173 {
6174 if (hbr_cache[brush_lru] != NULL)
6175 DeleteBrush(hbr_cache[brush_lru]);
6176 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6177 brush_color[brush_lru] = gui.currBgColor;
6178 hbr = hbr_cache[brush_lru];
6179 brush_lru = !brush_lru;
6180 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006181
Bram Moolenaar92467d32017-12-05 13:22:16 +01006182 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 SetBkMode(s_hdc, TRANSPARENT);
6185
6186 /*
6187 * When drawing block cursor, prevent inverted character spilling
6188 * over character cell (can happen with bold/italic)
6189 */
6190 if (flags & DRAW_CURSOR)
6191 {
6192 pcliprect = &rc;
6193 foptions = ETO_CLIPPED;
6194 }
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 SetTextColor(s_hdc, gui.currFgColor);
6197 SelectFont(s_hdc, gui.currFont);
6198
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006199#ifdef FEAT_DIRECTX
6200 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006201 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006202#endif
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6205 {
6206 vim_free(padding);
6207 pad_size = Columns;
6208
Bram Moolenaar734a8672019-12-02 22:49:38 +01006209 // Don't give an out-of-memory message here, it would call us
6210 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006211 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (padding != NULL)
6213 for (i = 0; i < pad_size; i++)
6214 padding[i] = gui.char_width;
6215 }
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 /*
6218 * We have to provide the padding argument because italic and bold versions
6219 * of fixed-width fonts are often one pixel or so wider than their normal
6220 * versions.
6221 * No check for DRAW_BOLD, Windows will have done it already.
6222 */
6223
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // Check if there are any UTF-8 characters. If not, use normal text
6225 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 if (enc_utf8)
6227 for (n = 0; n < len; ++n)
6228 if (text[n] >= 0x80)
6229 break;
6230
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006231#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6233 // required that unicode drawing routine, currently. So this forces it
6234 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006235 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006236 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006237#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006238
Bram Moolenaar734a8672019-12-02 22:49:38 +01006239 // Check if the Unicode buffer exists and is big enough. Create it
6240 // with the same length as the multi-byte string, the number of wide
6241 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006242 if ((enc_utf8
6243 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6244 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 && (unicodebuf == NULL || len > unibuflen))
6246 {
6247 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006248 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006251 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253 unibuflen = len;
6254 }
6255
6256 if (enc_utf8 && n < len && unicodebuf != NULL)
6257 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006258 // Output UTF-8 characters. Composing characters should be
6259 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006260 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006261 int wlen; // string length in words
6262 int clen; // string length in characters
6263 int cells; // cell width of string up to composing char
6264 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006265 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006267 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006268 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006270 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006272 c = utf_ptr2char(text + i);
6273 if (c >= 0x10000)
6274 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006275 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006276 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6277 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006278 }
6279 else
6280 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006281 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006282 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006283
6284 if (utf_iscomposing(c))
6285 cw = 0;
6286 else
6287 {
6288 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006289 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006290 cw = 1;
6291 }
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (unicodepdy != NULL)
6294 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006295 // Use unicodepdy to make characters fit as we expect, even
6296 // when the font uses different widths (e.g., bold character
6297 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006298 if (c >= 0x10000)
6299 {
6300 unicodepdy[wlen - 2] = cw * gui.char_width;
6301 unicodepdy[wlen - 1] = 0;
6302 }
6303 else
6304 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006307 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006308 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006310#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006311 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006312 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006313 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006314 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006315 TEXT_X(col), TEXT_Y(row),
6316 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006317 gui.char_width, gui.currFgColor,
6318 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006319 }
6320 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006321#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006322 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6323 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006324 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006326 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006328 // If we want to display codepage data, and the current CP is not the
6329 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (unicodebuf != NULL)
6331 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006332 if (enc_latin9)
6333 latin9_to_ucs(text, len, unicodebuf);
6334 else
6335 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 MB_PRECOMPOSED,
6337 (char *)text, len,
6338 (LPWSTR)unicodebuf, unibuflen);
6339 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006340 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006341 // Use unicodepdy to make characters fit as we expect, even
6342 // when the font uses different widths (e.g., bold character
6343 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006344 if (unicodepdy != NULL)
6345 {
6346 int i;
6347 int cw;
6348
6349 for (i = 0; i < len; ++i)
6350 {
6351 cw = utf_char2cells(unicodebuf[i]);
6352 if (cw > 2)
6353 cw = 1;
6354 unicodepdy[i] = cw * gui.char_width;
6355 }
6356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006358 foptions, pcliprect, unicodebuf, len, unicodepdy);
6359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 }
6361 }
6362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 {
6364#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006365 // Windows will mess up RL text, so we have to draw it character by
6366 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006367 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6369 foptions, pcliprect, (char *)text, len, padding);
6370 else
6371#endif
6372 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6373 foptions, pcliprect, (char *)text, len, padding);
6374 }
6375
Bram Moolenaar734a8672019-12-02 22:49:38 +01006376 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 if (flags & DRAW_UNDERL)
6378 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006379 // When p_linespace is 0, overwrite the bottom row of pixels.
6380 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (p_linespace > 1)
6383 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006384 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006386
Bram Moolenaar734a8672019-12-02 22:49:38 +01006387 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006388 if (flags & DRAW_STRIKE)
6389 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006390 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006391 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006392 }
6393
Bram Moolenaar734a8672019-12-02 22:49:38 +01006394 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006395 if (flags & DRAW_UNDERC)
6396 {
6397 int x;
6398 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006399 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006400
6401 y = FILL_Y(row + 1) - 1;
6402 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6403 {
6404 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006405 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006406 }
6407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408}
6409
6410
6411/*
6412 * Output routines.
6413 */
6414
Bram Moolenaar734a8672019-12-02 22:49:38 +01006415/*
6416 * Flush any output to the screen
6417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 void
6419gui_mch_flush(void)
6420{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006421#if defined(FEAT_DIRECTX)
6422 if (IS_ENABLE_DIRECTX())
6423 DWriteContext_Flush(s_dwc);
6424#endif
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 GdiFlush();
6427}
6428
6429 static void
6430clear_rect(RECT *rcp)
6431{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006432 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433}
6434
6435
Bram Moolenaarc716c302006-01-21 22:12:51 +00006436 void
6437gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6438{
6439 RECT workarea_rect;
6440
6441 get_work_area(&workarea_rect);
6442
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006443 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006444 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6445 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006446
Bram Moolenaar734a8672019-12-02 22:49:38 +01006447 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6448 // the menubar for MSwin, we subtract it from the screen height, so that
6449 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006450 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006451 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6452 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6453 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6454 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006455}
6456
6457
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458#if defined(FEAT_MENU) || defined(PROTO)
6459/*
6460 * Add a sub menu to the menu bar.
6461 */
6462 void
6463gui_mch_add_menu(
6464 vimmenu_T *menu,
6465 int pos)
6466{
6467 vimmenu_T *parent = menu->parent;
6468
6469 menu->submenu_id = CreatePopupMenu();
6470 menu->id = s_menu_id++;
6471
6472 if (menu_is_menubar(menu->name))
6473 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006474 WCHAR *wn;
6475 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006477 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006478 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006479 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006481 infow.cbSize = sizeof(infow);
6482 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6483 | MIIM_SUBMENU;
6484 infow.dwItemData = (long_u)menu;
6485 infow.wID = menu->id;
6486 infow.fType = MFT_STRING;
6487 infow.dwTypeData = wn;
6488 infow.cch = (UINT)wcslen(wn);
6489 infow.hSubMenu = menu->submenu_id;
6490 InsertMenuItemW((parent == NULL)
6491 ? s_menuBar : parent->submenu_id,
6492 (UINT)pos, TRUE, &infow);
6493 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
6495
Bram Moolenaar734a8672019-12-02 22:49:38 +01006496 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (parent == NULL)
6498 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006499# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 else if (IsWindow(parent->tearoff_handle))
6501 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
6505 void
6506gui_mch_show_popupmenu(vimmenu_T *menu)
6507{
6508 POINT mp;
6509
K.Takata45f9cfb2022-01-21 11:11:00 +00006510 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6512}
6513
6514 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006515gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 vimmenu_T *menu = gui_find_menu(path_name);
6518
6519 if (menu != NULL)
6520 {
6521 POINT p;
6522
Bram Moolenaar734a8672019-12-02 22:49:38 +01006523 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006525 if (mouse_pos)
6526 {
6527 int mx, my;
6528
6529 gui_mch_getmouse(&mx, &my);
6530 p.x += mx;
6531 p.y += my;
6532 }
6533 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006535 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6537 }
6538 msg_scroll = FALSE;
6539 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6540 }
6541}
6542
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006543# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544/*
6545 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6546 * create it as a pseudo-"tearoff menu".
6547 */
6548 void
6549gui_make_tearoff(char_u *path_name)
6550{
6551 vimmenu_T *menu = gui_find_menu(path_name);
6552
Bram Moolenaar734a8672019-12-02 22:49:38 +01006553 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (menu != NULL)
6555 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6556}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559/*
6560 * Add a menu item to a menu
6561 */
6562 void
6563gui_mch_add_menu_item(
6564 vimmenu_T *menu,
6565 int idx)
6566{
6567 vimmenu_T *parent = menu->parent;
6568
6569 menu->id = s_menu_id++;
6570 menu->submenu_id = NULL;
6571
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006572# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6574 {
6575 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6576 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6577 }
6578 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006579# endif
6580# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 if (menu_is_toolbar(parent->name))
6582 {
6583 TBBUTTON newtb;
6584
Bram Moolenaara80faa82020-04-12 19:37:17 +02006585 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (menu_is_separator(menu->name))
6587 {
6588 newtb.iBitmap = 0;
6589 newtb.fsStyle = TBSTYLE_SEP;
6590 }
6591 else
6592 {
6593 newtb.iBitmap = get_toolbar_bitmap(menu);
6594 newtb.fsStyle = TBSTYLE_BUTTON;
6595 }
6596 newtb.idCommand = menu->id;
6597 newtb.fsState = TBSTATE_ENABLED;
6598 newtb.iString = 0;
6599 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6600 (LPARAM)&newtb);
6601 menu->submenu_id = (HMENU)-1;
6602 }
6603 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006606 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006608 wn = enc_to_utf16(menu->name, NULL);
6609 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006611 InsertMenuW(parent->submenu_id, (UINT)idx,
6612 (menu_is_separator(menu->name)
6613 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6614 (UINT)menu->id, wn);
6615 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006617# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 if (IsWindow(parent->tearoff_handle))
6619 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 }
6622}
6623
6624/*
6625 * Destroy the machine specific menu widget.
6626 */
6627 void
6628gui_mch_destroy_menu(vimmenu_T *menu)
6629{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006630# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 /*
6632 * is this a toolbar button?
6633 */
6634 if (menu->submenu_id == (HMENU)-1)
6635 {
6636 int iButton;
6637
6638 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6639 (WPARAM)menu->id, 0);
6640 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6641 }
6642 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 {
6645 if (menu->parent != NULL
6646 && menu_is_popup(menu->parent->dname)
6647 && menu->parent->submenu_id != NULL)
6648 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6649 else
6650 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6651 if (menu->submenu_id != NULL)
6652 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006653# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (IsWindow(menu->tearoff_handle))
6655 DestroyWindow(menu->tearoff_handle);
6656 if (menu->parent != NULL
6657 && menu->parent->children != NULL
6658 && IsWindow(menu->parent->tearoff_handle))
6659 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006660 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 menu->modes = 0;
6662 rebuild_tearoff(menu->parent);
6663 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 }
6666}
6667
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006668# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 static void
6670rebuild_tearoff(vimmenu_T *menu)
6671{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006672 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 char_u tbuf[128];
6674 RECT trect;
6675 RECT rct;
6676 RECT roct;
6677 int x, y;
6678
6679 HWND thwnd = menu->tearoff_handle;
6680
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006681 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (GetWindowRect(thwnd, &trect)
6683 && GetWindowRect(s_hwnd, &rct)
6684 && GetClientRect(s_hwnd, &roct))
6685 {
6686 x = trect.left - rct.left;
6687 y = (trect.top - rct.bottom + roct.bottom);
6688 }
6689 else
6690 {
6691 x = y = 0xffffL;
6692 }
6693 DestroyWindow(thwnd);
6694 if (menu->children != NULL)
6695 {
6696 gui_mch_tearoff(tbuf, menu, x, y);
6697 if (IsWindow(menu->tearoff_handle))
6698 (void) SetWindowPos(menu->tearoff_handle,
6699 NULL,
6700 (int)trect.left,
6701 (int)trect.top,
6702 0, 0,
6703 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6704 }
6705}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006706# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708/*
6709 * Make a menu either grey or not grey.
6710 */
6711 void
6712gui_mch_menu_grey(
6713 vimmenu_T *menu,
6714 int grey)
6715{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006716# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 /*
6718 * is this a toolbar button?
6719 */
6720 if (menu->submenu_id == (HMENU)-1)
6721 {
6722 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006723 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006726# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006727 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6728 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006730# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6732 {
6733 WORD menuID;
6734 HWND menuHandle;
6735
6736 /*
6737 * A tearoff button has changed state.
6738 */
6739 if (menu->children == NULL)
6740 menuID = (WORD)(menu->id);
6741 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006742 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6744 if (menuHandle)
6745 EnableWindow(menuHandle, !grey);
6746
6747 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749}
6750
Bram Moolenaar734a8672019-12-02 22:49:38 +01006751#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
6753
Bram Moolenaar734a8672019-12-02 22:49:38 +01006754// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006757#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6760/*
6761 * stuff for dialogs
6762 */
6763
6764/*
6765 * The callback routine used by all the dialogs. Very simple. First,
6766 * acknowledges the INITDIALOG message so that Windows knows to do standard
6767 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6768 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6769 * number.
6770 */
6771 static LRESULT CALLBACK
6772dialog_callback(
6773 HWND hwnd,
6774 UINT message,
6775 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006776 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 if (message == WM_INITDIALOG)
6779 {
6780 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006781 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 (void)SetFocus(hwnd);
6783 if (dialog_default_button > IDCANCEL)
6784 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006785 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006786 // We don't have a default, set focus on another element of the
6787 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006788 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 return FALSE;
6790 }
6791
6792 if (message == WM_COMMAND)
6793 {
6794 int button = LOWORD(wParam);
6795
Bram Moolenaar734a8672019-12-02 22:49:38 +01006796 // Don't end the dialog if something was selected that was
6797 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (button >= DLG_NONBUTTON_CONTROL)
6799 return TRUE;
6800
Bram Moolenaar734a8672019-12-02 22:49:38 +01006801 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006803 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006804 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006805 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006806
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006807 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6808 p = utf16_to_enc(wp, NULL);
6809 vim_strncpy(s_textfield, p, IOSIZE);
6810 vim_free(p);
6811 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 /*
6815 * Need to check for IDOK because if the user just hits Return to
6816 * accept the default value, some reason this is what we get.
6817 */
6818 if (button == IDOK)
6819 {
6820 if (dialog_default_button > IDCANCEL)
6821 EndDialog(hwnd, dialog_default_button);
6822 }
6823 else
6824 EndDialog(hwnd, button - IDCANCEL);
6825 return TRUE;
6826 }
6827
6828 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6829 {
6830 EndDialog(hwnd, 0);
6831 return TRUE;
6832 }
6833 return FALSE;
6834}
6835
6836/*
6837 * Create a dialog dynamically from the parameter strings.
6838 * type = type of dialog (question, alert, etc.)
6839 * title = dialog title. may be NULL for default title.
6840 * message = text to display. Dialog sizes to accommodate it.
6841 * buttons = '\n' separated list of button captions, default first.
6842 * dfltbutton = number of default button.
6843 *
6844 * This routine returns 1 if the first button is pressed,
6845 * 2 for the second, etc.
6846 *
6847 * 0 indicates Esc was pressed.
6848 * -1 for unexpected error
6849 *
6850 * If stubbing out this fn, return 1.
6851 */
6852
Bram Moolenaar734a8672019-12-02 22:49:38 +01006853static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 "IDR_VIM",
6856 "IDR_VIM_ERROR",
6857 "IDR_VIM_ALERT",
6858 "IDR_VIM_INFO",
6859 "IDR_VIM_QUESTION"
6860};
6861
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 int
6863gui_mch_dialog(
6864 int type,
6865 char_u *title,
6866 char_u *message,
6867 char_u *buttons,
6868 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006869 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006870 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006873 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 int numButtons;
6875 int *buttonWidths, *buttonPositions;
6876 int buttonYpos;
6877 int nchar, i;
6878 DWORD lStyle;
6879 int dlgwidth = 0;
6880 int dlgheight;
6881 int editboxheight;
6882 int horizWidth = 0;
6883 int msgheight;
6884 char_u *pstart;
6885 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006886 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 char_u *tbuffer;
6888 RECT rect;
6889 HWND hwnd;
6890 HDC hdc;
6891 HFONT font, oldFont;
6892 TEXTMETRIC fontInfo;
6893 int fontHeight;
6894 int textWidth, minButtonWidth, messageWidth;
6895 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006896 int maxDialogHeight;
6897 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 int vertical;
6899 int dlgPaddingX;
6900 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006901# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006902 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006904# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006905 garray_T ga;
6906 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006907 int dlg_icon_width;
6908 int dlg_icon_height;
6909 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006911# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006912 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006913# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006914 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006915# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006916 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006917 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaar748bf032005-02-02 23:04:36 +00006920 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006921 {
6922 load_dpi_func();
6923 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006924 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006925 }
6926 else
6927 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
6929 if ((type < 0) || (type > VIM_LAST_TYPE))
6930 type = 0;
6931
Bram Moolenaar734a8672019-12-02 22:49:38 +01006932 // allocate some memory for dialog template
6933 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006934 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006935 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937 if (p == NULL)
6938 return -1;
6939
6940 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006941 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6943 * const.
6944 */
6945 tbuffer = vim_strsave(buttons);
6946 if (tbuffer == NULL)
6947 return -1;
6948
Bram Moolenaar734a8672019-12-02 22:49:38 +01006949 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar734a8672019-12-02 22:49:38 +01006951 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 numButtons = 1;
6953 for (i = 0; tbuffer[i] != '\0'; i++)
6954 {
6955 if (tbuffer[i] == DLG_BUTTON_SEP)
6956 numButtons++;
6957 }
6958 if (dfltbutton >= numButtons)
6959 dfltbutton = -1;
6960
Bram Moolenaar734a8672019-12-02 22:49:38 +01006961 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006962 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (buttonWidths == NULL)
6964 return -1;
6965
Bram Moolenaar734a8672019-12-02 22:49:38 +01006966 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006967 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (buttonPositions == NULL)
6969 return -1;
6970
6971 /*
6972 * Calculate how big the dialog must be.
6973 */
6974 hwnd = GetDesktopWindow();
6975 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006976# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6978 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006979 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 use_lfSysmenu = TRUE;
6981 }
6982 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006983# endif
K.Takatad1c58992022-01-23 12:31:57 +00006984 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6985 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6986
6987 oldFont = SelectFont(hdc, font);
6988 dlgPaddingX = DLG_PADDING_X;
6989 dlgPaddingY = DLG_PADDING_Y;
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 GetTextMetrics(hdc, &fontInfo);
6992 fontHeight = fontInfo.tmHeight;
6993
Bram Moolenaar734a8672019-12-02 22:49:38 +01006994 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006995 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998 if (s_hwnd == NULL)
6999 {
7000 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
Bram Moolenaar734a8672019-12-02 22:49:38 +01007002 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007003 get_work_area(&workarea_rect);
7004 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007005 if (maxDialogWidth > adjust_by_system_dpi(600))
7006 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007007 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007008 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007009 }
7010 else
7011 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007012 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007013 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007014 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007015 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7016 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7017 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7018 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007019
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007020 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007021 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7022 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7023 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7024 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7025 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007026 }
7027
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 // Set dlgwidth to width of message.
7029 // Copy the message into "ga", changing NL to CR-NL and inserting line
7030 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 pstart = message;
7032 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007033 msgheight = 0;
7034 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 do
7036 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007037 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007038
Bram Moolenaar734a8672019-12-02 22:49:38 +01007039 // Need to figure out where to break the string. The system does it
7040 // at a word boundary, which would mean we can't compute the number of
7041 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007042 textWidth = 0;
7043 last_white = NULL;
7044 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007045 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007046 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007047 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007048 && textWidth > maxDialogWidth * 3 / 4)
7049 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007050 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007051 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007052 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007053 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007054 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007055 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007056 textWidth = 0;
7057
7058 if (last_white != NULL)
7059 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007060 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007061 if (pend > last_white)
7062 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007063 pend = last_white + 1;
7064 last_white = NULL;
7065 }
7066 ga_append(&ga, '\r');
7067 ga_append(&ga, '\n');
7068 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007069 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007070
7071 while (--l >= 0)
7072 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007073 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007074 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007076
7077 ga_append(&ga, '\r');
7078 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 pstart = pend + 1;
7080 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007081
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007082 if (ga.ga_data != NULL)
7083 message = ga.ga_data;
7084
Bram Moolenaar734a8672019-12-02 22:49:38 +01007085 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007086
K.Takatac81e9bf2022-01-16 14:15:49 +00007087 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7088 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
K.Takatac81e9bf2022-01-16 14:15:49 +00007090 // Add width of icon to dlgwidth, and some space
7091 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7092 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7093
7094 if (msgheight < dlg_icon_height)
7095 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 /*
7098 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007099 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007101 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (!vertical)
7103 {
7104 // Place buttons horizontally if they fit.
7105 horizWidth = dlgPaddingX;
7106 pstart = tbuffer;
7107 i = 0;
7108 do
7109 {
7110 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7111 if (pend == NULL)
7112 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007113 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (textWidth < minButtonWidth)
7115 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 buttonWidths[i] = textWidth;
7118 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 pstart = pend + 1;
7121 } while (*pend != NUL);
7122
7123 if (horizWidth > maxDialogWidth)
7124 vertical = TRUE; // Too wide to fit on the screen.
7125 else if (horizWidth > dlgwidth)
7126 dlgwidth = horizWidth;
7127 }
7128
7129 if (vertical)
7130 {
7131 // Stack buttons vertically.
7132 pstart = tbuffer;
7133 do
7134 {
7135 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7136 if (pend == NULL)
7137 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007138 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007139 textWidth += dlgPaddingX; // Padding within button
7140 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (textWidth > dlgwidth)
7142 dlgwidth = textWidth;
7143 pstart = pend + 1;
7144 } while (*pend != NUL);
7145 }
7146
7147 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007148 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaar734a8672019-12-02 22:49:38 +01007150 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007151 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 add_long(lStyle);
7154 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007155 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 add_word(0); // NumberOfItems(will change later)
7157 add_word(10); // x
7158 add_word(10); // y
7159 add_word(PixelToDialogX(dlgwidth)); // cx
7160
7161 // Dialog height.
7162 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007163 dlgheight = msgheight + 2 * dlgPaddingY
7164 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 else
7166 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7167
7168 // Dialog needs to be taller if contains an edit box.
7169 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7170 if (textfield != NULL)
7171 dlgheight += editboxheight;
7172
Bram Moolenaar734a8672019-12-02 22:49:38 +01007173 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007174 if (dlgheight > maxDialogHeight)
7175 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007176 msgheight = msgheight - (dlgheight - maxDialogHeight);
7177 dlgheight = maxDialogHeight;
7178 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007179 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007180 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007181 }
7182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 add_word(PixelToDialogY(dlgheight));
7184
7185 add_word(0); // Menu
7186 add_word(0); // Class
7187
Bram Moolenaar734a8672019-12-02 22:49:38 +01007188 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007189 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7190 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 p += nchar;
7192
K.Takatad1c58992022-01-23 12:31:57 +00007193 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007194# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007195 if (use_lfSysmenu)
7196 {
7197 // point size
7198 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7199 GetDeviceCaps(hdc, LOGPIXELSY));
7200 wcscpy(p, lfSysmenu.lfFaceName);
7201 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
K.Takatad1c58992022-01-23 12:31:57 +00007203 else
7204# endif
7205 {
7206 *p++ = DLG_FONT_POINT_SIZE; // point size
7207 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7208 }
7209 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
7211 buttonYpos = msgheight + 2 * dlgPaddingY;
7212
7213 if (textfield != NULL)
7214 buttonYpos += editboxheight;
7215
7216 pstart = tbuffer;
7217 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007218 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 for (i = 0; i < numButtons; i++)
7220 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007221 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 for ( pend = pstart;
7223 *pend && (*pend != DLG_BUTTON_SEP);
7224 pend++)
7225 ;
7226
7227 if (*pend)
7228 *pend = '\0';
7229
7230 /*
7231 * old NOTE:
7232 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7233 * the focus to the first tab-able button and in so doing makes that
7234 * the default!! Grrr. Workaround: Make the default button the only
7235 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7236 * he/she can use arrow keys.
7237 *
7238 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007239 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 * dialog. Also needed for when the textfield is the default control.
7241 * It appears to work now (perhaps not on Win95?).
7242 */
7243 if (vertical)
7244 {
7245 p = add_dialog_element(p,
7246 (i == dfltbutton
7247 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7248 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007249 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 + 2 * fontHeight * i),
7251 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7252 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007253 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 else
7256 {
7257 p = add_dialog_element(p,
7258 (i == dfltbutton
7259 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7260 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007261 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 PixelToDialogX(buttonWidths[i]),
7263 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007264 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007266 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 *pnumitems += numButtons;
7269
Bram Moolenaar734a8672019-12-02 22:49:38 +01007270 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 p = add_dialog_element(p, SS_ICON,
7272 PixelToDialogX(dlgPaddingX),
7273 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007274 PixelToDialogX(dlg_icon_width),
7275 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7277 dlg_icons[type]);
7278
Bram Moolenaar734a8672019-12-02 22:49:38 +01007279 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007280 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007281 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007282 PixelToDialogY(dlgPaddingY),
7283 (WORD)(PixelToDialogX(messageWidth) + 1),
7284 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007285 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaar734a8672019-12-02 22:49:38 +01007287 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (textfield != NULL)
7289 {
7290 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7291 PixelToDialogX(2 * dlgPaddingX),
7292 PixelToDialogY(2 * dlgPaddingY + msgheight),
7293 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7294 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007295 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 *pnumitems += 1;
7297 }
7298
7299 *pnumitems += 2;
7300
7301 SelectFont(hdc, oldFont);
7302 DeleteObject(font);
7303 ReleaseDC(hwnd, hdc);
7304
Bram Moolenaar734a8672019-12-02 22:49:38 +01007305 // Let the dialog_callback() function know which button to make default
7306 // If we have an edit box, make that the default. We also need to tell
7307 // dialog_callback() if this dialog contains an edit box or not. We do
7308 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if (textfield != NULL)
7310 {
7311 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7312 s_textfield = textfield;
7313 }
7314 else
7315 {
7316 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7317 s_textfield = NULL;
7318 }
7319
Bram Moolenaar734a8672019-12-02 22:49:38 +01007320 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007322 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 (LPDLGTEMPLATE)pdlgtemplate,
7324 s_hwnd,
7325 (DLGPROC)dialog_callback);
7326
7327 LocalFree(LocalHandle(pdlgtemplate));
7328 vim_free(tbuffer);
7329 vim_free(buttonWidths);
7330 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007331 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
Bram Moolenaar734a8672019-12-02 22:49:38 +01007333 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 (void)SetFocus(s_hwnd);
7335
7336 return nchar;
7337}
7338
Bram Moolenaar734a8672019-12-02 22:49:38 +01007339#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007340
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341/*
7342 * Put a simple element (basic class) onto a dialog template in memory.
7343 * return a pointer to where the next item should be added.
7344 *
7345 * parameters:
7346 * lStyle = additional style flags
7347 * (be careful, NT3.51 & Win32s will ignore the new ones)
7348 * x,y = x & y positions IN DIALOG UNITS
7349 * w,h = width and height IN DIALOG UNITS
7350 * Id = ID used in messages
7351 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7352 * caption = usually text or resource name
7353 *
7354 * TODO: use the length information noted here to enable the dialog creation
7355 * routines to work out more exactly how much memory they need to alloc.
7356 */
7357 static PWORD
7358add_dialog_element(
7359 PWORD p,
7360 DWORD lStyle,
7361 WORD x,
7362 WORD y,
7363 WORD w,
7364 WORD h,
7365 WORD Id,
7366 WORD clss,
7367 const char *caption)
7368{
7369 int nchar;
7370
Bram Moolenaar734a8672019-12-02 22:49:38 +01007371 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7373 *p++ = LOWORD(lStyle);
7374 *p++ = HIWORD(lStyle);
7375 *p++ = 0; // LOWORD (lExtendedStyle)
7376 *p++ = 0; // HIWORD (lExtendedStyle)
7377 *p++ = x;
7378 *p++ = y;
7379 *p++ = w;
7380 *p++ = h;
7381 *p++ = Id; //9 or 10 words in all
7382
7383 *p++ = (WORD)0xffff;
7384 *p++ = clss; //2 more here
7385
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007386 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 p += nchar;
7388
7389 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7390
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007391 return p; // total = 15 + strlen(caption) words
7392 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393}
7394
7395
7396/*
7397 * Helper routine. Take an input pointer, return closest pointer that is
7398 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7399 */
7400 static LPWORD
7401lpwAlign(
7402 LPWORD lpIn)
7403{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007404 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007406 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 ul += 3;
7408 ul >>= 2;
7409 ul <<= 2;
7410 return (LPWORD)ul;
7411}
7412
7413/*
7414 * Helper routine. Takes second parameter as Ansi string, copies it to first
7415 * parameter as wide character (16-bits / char) string, and returns integer
7416 * number of wide characters (words) in string (including the trailing wide
7417 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007418 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7419 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 static int
7421nCopyAnsiToWideChar(
7422 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007423 LPSTR lpAnsiIn,
7424 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007427 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 int i;
7429 WCHAR *wn;
7430
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007431 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007433 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007434 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (wn != NULL)
7436 {
7437 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007438 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 vim_free(wn);
7440 }
7441 }
7442 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007443 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 nChar = MultiByteToWideChar(
7445 enc_codepage > 0 ? enc_codepage : CP_ACP,
7446 MB_PRECOMPOSED,
7447 lpAnsiIn, len,
7448 lpWCStr, len);
7449 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007450 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453 return nChar;
7454}
7455
7456
7457#ifdef FEAT_TEAROFF
7458/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007459 * Lookup menu handle from "menu_id".
7460 */
7461 static HMENU
7462tearoff_lookup_menuhandle(
7463 vimmenu_T *menu,
7464 WORD menu_id)
7465{
7466 for ( ; menu != NULL; menu = menu->next)
7467 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007468 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007469 continue;
7470 if (menu_is_separator(menu->dname))
7471 continue;
7472 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7473 return menu->submenu_id;
7474 }
7475 return NULL;
7476}
7477
7478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 * The callback function for all the modeless dialogs that make up the
7480 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7481 * thinking its menus have been clicked), and go away when closed.
7482 */
7483 static LRESULT CALLBACK
7484tearoff_callback(
7485 HWND hwnd,
7486 UINT message,
7487 WPARAM wParam,
7488 LPARAM lParam)
7489{
7490 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007491 {
7492 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Bram Moolenaar734a8672019-12-02 22:49:38 +01007496 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 HandleMouseHide(message, lParam);
7498
7499 if (message == WM_COMMAND)
7500 {
7501 if ((WORD)(LOWORD(wParam)) & 0x8000)
7502 {
7503 POINT mp;
7504 RECT rect;
7505
7506 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7507 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007508 vimmenu_T *menu;
7509
7510 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007512 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7514 (int)rect.right - 8,
7515 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007516 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 s_hwnd,
7518 NULL);
7519 /*
7520 * NOTE: The pop-up menu can eat the mouse up event.
7521 * We deal with this in normal.c.
7522 */
7523 }
7524 }
7525 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007526 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7528 /*
7529 * Give main window the focus back: this is so after
7530 * choosing a tearoff button you can start typing again
7531 * straight away.
7532 */
7533 (void)SetFocus(s_hwnd);
7534 return TRUE;
7535 }
7536 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7537 {
7538 DestroyWindow(hwnd);
7539 return TRUE;
7540 }
7541
Bram Moolenaar734a8672019-12-02 22:49:38 +01007542 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 if (message == WM_EXITSIZEMOVE)
7544 (void)SetActiveWindow(s_hwnd);
7545
7546 return FALSE;
7547}
7548#endif
7549
7550
7551/*
K.Takatad1c58992022-01-23 12:31:57 +00007552 * Computes the dialog base units based on the current dialog font.
7553 * We don't use the GetDialogBaseUnits() API, because we don't use the
7554 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 */
7556 static void
7557get_dialog_font_metrics(void)
7558{
7559 HDC hdc;
7560 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 SIZE size;
7562#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007563 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564#endif
7565
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007567 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007568 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570#endif
7571 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007572 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573
K.Takatad1c58992022-01-23 12:31:57 +00007574 hdc = GetDC(s_hwnd);
7575 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007576 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007577 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
K.Takataabe628e2022-01-23 16:25:17 +00007579 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007580 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581}
7582
7583#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7584/*
7585 * Create a pseudo-"tearoff menu" based on the child
7586 * items of a given menu pointer.
7587 */
7588 static void
7589gui_mch_tearoff(
7590 char_u *title,
7591 vimmenu_T *menu,
7592 int initX,
7593 int initY)
7594{
7595 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7596 int template_len;
7597 int nchar, textWidth, submenuWidth;
7598 DWORD lStyle;
7599 DWORD lExtendedStyle;
7600 WORD dlgwidth;
7601 WORD menuID;
7602 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007603 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 vimmenu_T *the_menu = menu;
7605 HWND hwnd;
7606 HDC hdc;
7607 HFONT font, oldFont;
7608 int col, spaceWidth, len;
7609 int columnWidths[2];
7610 char_u *label, *text;
7611 int acLen = 0;
7612 int nameLen;
7613 int padding0, padding1, padding2 = 0;
7614 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007615 int x;
7616 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007617# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007618 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
7622 /*
7623 * If this menu is already torn off, move it to the mouse position.
7624 */
7625 if (IsWindow(menu->tearoff_handle))
7626 {
7627 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007628 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7631 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7632 }
7633 return;
7634 }
7635
7636 /*
7637 * Create a new tearoff.
7638 */
7639 if (*title == MNU_HIDDEN_CHAR)
7640 title++;
7641
Bram Moolenaar734a8672019-12-02 22:49:38 +01007642 // Allocate memory to store the dialog template. It's made bigger when
7643 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 template_len = DLG_ALLOC_SIZE;
7645 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7646 if (p == NULL)
7647 return;
7648
7649 hwnd = GetDesktopWindow();
7650 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007651# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7653 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007654 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 use_lfSysmenu = TRUE;
7656 }
7657 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007658# endif
K.Takatad1c58992022-01-23 12:31:57 +00007659 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7660 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7661
7662 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
Bram Moolenaar734a8672019-12-02 22:49:38 +01007664 // Calculate width of a single space. Used for padding columns to the
7665 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007666 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667
Bram Moolenaar734a8672019-12-02 22:49:38 +01007668 // Figure out max width of the text column, the accelerator column and the
7669 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 submenuWidth = 0;
7671 for (col = 0; col < 2; col++)
7672 {
7673 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007674 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007676 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 text = (col == 0) ? pmenu->dname : pmenu->actext;
7678 if (text != NULL && *text != NUL)
7679 {
7680 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7681 if (textWidth > columnWidths[col])
7682 columnWidths[col] = textWidth;
7683 }
7684 if (pmenu->children != NULL)
7685 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7686 }
7687 }
7688 if (columnWidths[1] == 0)
7689 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007690 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 if (submenuWidth != 0)
7692 columnWidths[0] += submenuWidth;
7693 else
7694 columnWidths[0] += spaceWidth;
7695 }
7696 else
7697 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007698 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7700 columnWidths[1] += submenuWidth;
7701 }
7702
7703 /*
7704 * Now find the total width of our 'menu'.
7705 */
7706 textWidth = columnWidths[0] + columnWidths[1];
7707 if (submenuWidth != 0)
7708 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007709 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7711 textWidth += submenuWidth;
7712 }
7713 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7714 if (textWidth > dlgwidth)
7715 dlgwidth = textWidth;
7716 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7717
Bram Moolenaar734a8672019-12-02 22:49:38 +01007718 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007719 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7722 *p++ = LOWORD(lStyle);
7723 *p++ = HIWORD(lStyle);
7724 *p++ = LOWORD(lExtendedStyle);
7725 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007726 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007728 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007730 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 else
7732 *p++ = PixelToDialogX(initX); // x
7733 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007734 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 else
7736 *p++ = PixelToDialogY(initY); // y
7737 *p++ = PixelToDialogX(dlgwidth); // cx
7738 ptrueheight = p;
7739 *p++ = 0; // dialog height: changed later anyway
7740 *p++ = 0; // Menu
7741 *p++ = 0; // Class
7742
Bram Moolenaar734a8672019-12-02 22:49:38 +01007743 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007745 ? (LPSTR)title
7746 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 p += nchar;
7748
K.Takatad1c58992022-01-23 12:31:57 +00007749 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007750# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007751 if (use_lfSysmenu)
7752 {
7753 // point size
7754 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7755 GetDeviceCaps(hdc, LOGPIXELSY));
7756 wcscpy(p, lfSysmenu.lfFaceName);
7757 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 }
K.Takatad1c58992022-01-23 12:31:57 +00007759 else
7760# endif
7761 {
7762 *p++ = DLG_FONT_POINT_SIZE; // point size
7763 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7764 }
7765 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
7767 /*
7768 * Loop over all the items in the menu.
7769 * But skip over the tearbar.
7770 */
7771 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7772 menu = menu->children->next;
7773 else
7774 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007775 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 for ( ; menu != NULL; menu = menu->next)
7777 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007778 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 continue;
7780 if (menu_is_separator(menu->dname))
7781 {
7782 sepPadding += 3;
7783 continue;
7784 }
7785
Bram Moolenaar734a8672019-12-02 22:49:38 +01007786 // Check if there still is plenty of room in the template. Make it
7787 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7789 {
7790 WORD *newp;
7791
7792 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7793 if (newp != NULL)
7794 {
7795 template_len += 4096;
7796 mch_memmove(newp, pdlgtemplate,
7797 (char *)p - (char *)pdlgtemplate);
7798 p = newp + (p - pdlgtemplate);
7799 pnumitems = newp + (pnumitems - pdlgtemplate);
7800 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7801 LocalFree(LocalHandle(pdlgtemplate));
7802 pdlgtemplate = newp;
7803 }
7804 }
7805
Bram Moolenaar734a8672019-12-02 22:49:38 +01007806 // Figure out minimal length of this menu label. Use "name" for the
7807 // actual text, "dname" for estimating the displayed size. "name"
7808 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 len = nameLen = (int)STRLEN(menu->name);
7810 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7811 (int)STRLEN(menu->dname))) / spaceWidth;
7812 len += padding0;
7813
7814 if (menu->actext != NULL)
7815 {
7816 acLen = (int)STRLEN(menu->actext);
7817 len += acLen;
7818 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7819 }
7820 else
7821 textWidth = 0;
7822 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7823 len += padding1;
7824
7825 if (menu->children == NULL)
7826 {
7827 padding2 = submenuWidth / spaceWidth;
7828 len += padding2;
7829 menuID = (WORD)(menu->id);
7830 }
7831 else
7832 {
7833 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007834 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836
Bram Moolenaar734a8672019-12-02 22:49:38 +01007837 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007838 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (label == NULL)
7840 break;
7841
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007842 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007843 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007845 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 while (padding0-- > 0)
7847 *text++ = ' ';
7848 if (menu->actext != NULL)
7849 {
7850 STRNCPY(text, menu->actext, acLen);
7851 text += acLen;
7852 }
7853 while (padding1-- > 0)
7854 *text++ = ' ';
7855 if (menu->children != NULL)
7856 {
7857 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7858 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7859 }
7860 else
7861 {
7862 while (padding2-- > 0)
7863 *text++ = ' ';
7864 }
7865 *text = NUL;
7866
7867 /*
7868 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7869 * W95/NT4 it makes the tear-off look more like a menu.
7870 */
7871 p = add_dialog_element(p,
7872 BS_PUSHBUTTON|BS_LEFT,
7873 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7874 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7875 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7876 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007877 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 vim_free(label);
7879 (*pnumitems)++;
7880 }
7881
7882 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7883
7884
Bram Moolenaar734a8672019-12-02 22:49:38 +01007885 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007886 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007887 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 (LPDLGTEMPLATE)pdlgtemplate,
7889 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007890 (DLGPROC)tearoff_callback,
7891 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892
7893 LocalFree(LocalHandle(pdlgtemplate));
7894 SelectFont(hdc, oldFont);
7895 DeleteObject(font);
7896 ReleaseDC(hwnd, hdc);
7897
7898 /*
7899 * Reassert ourselves as the active window. This is so that after creating
7900 * a tearoff, the user doesn't have to click with the mouse just to start
7901 * typing again!
7902 */
7903 (void)SetActiveWindow(s_hwnd);
7904
Bram Moolenaar734a8672019-12-02 22:49:38 +01007905 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 force_menu_update = TRUE;
7907}
7908#endif
7909
7910#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007911# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
Bram Moolenaar734a8672019-12-02 22:49:38 +01007913// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914# ifndef TBSTYLE_FLAT
7915# define TBSTYLE_FLAT 0x0800
7916# endif
7917
7918/*
7919 * Create the toolbar, initially unpopulated.
7920 * (just like the menu, there are no defaults, it's all
7921 * set up through menu.vim)
7922 */
7923 static void
7924initialise_toolbar(void)
7925{
7926 InitCommonControls();
7927 s_toolbarhwnd = CreateToolbarEx(
7928 s_hwnd,
7929 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7930 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007931 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007932 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 IDR_TOOLBAR1, // id of initial bitmap
7934 NULL,
7935 0, // initial number of buttons
7936 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7937 TOOLBAR_BUTTON_HEIGHT,
7938 TOOLBAR_BUTTON_WIDTH,
7939 TOOLBAR_BUTTON_HEIGHT,
7940 sizeof(TBBUTTON)
7941 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007942
7943 // Remove transparency from the toolbar to prevent the main window
7944 // background colour showing through
7945 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7946 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7947
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007948 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
7950 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007951
7952 update_toolbar_size();
7953}
7954
7955 static void
7956update_toolbar_size(void)
7957{
7958 int w, h;
7959 TBMETRICS tbm = {sizeof(TBMETRICS)};
7960
7961 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7962 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7963 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7964 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7965
7966 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7967 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7968 //TRACE("button size: %d, %d", w, h);
7969 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7970 gui.toolbar_height = h + 6;
7971
7972 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7973 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7974
7975 // TODO:
7976 // Currently, this function only updates the size of toolbar buttons.
7977 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978}
7979
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007980 static LRESULT CALLBACK
7981toolbar_wndproc(
7982 HWND hwnd,
7983 UINT uMsg,
7984 WPARAM wParam,
7985 LPARAM lParam)
7986{
7987 HandleMouseHide(uMsg, lParam);
7988 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7989}
7990
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 static int
7992get_toolbar_bitmap(vimmenu_T *menu)
7993{
7994 int i = -1;
7995
7996 /*
7997 * Check user bitmaps first, unless builtin is specified.
7998 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007999 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
8001 char_u fname[MAXPATHL];
8002 HANDLE hbitmap = NULL;
8003
8004 if (menu->iconfile != NULL)
8005 {
8006 gui_find_iconfile(menu->iconfile, fname, "bmp");
8007 hbitmap = LoadImage(
8008 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008009 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 IMAGE_BITMAP,
8011 TOOLBAR_BUTTON_WIDTH,
8012 TOOLBAR_BUTTON_HEIGHT,
8013 LR_LOADFROMFILE |
8014 LR_LOADMAP3DCOLORS
8015 );
8016 }
8017
8018 /*
8019 * If the LoadImage call failed, or the "icon=" file
8020 * didn't exist or wasn't specified, try the menu name
8021 */
8022 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008023 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008024# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008025 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008026# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008027 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 hbitmap = LoadImage(
8029 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008030 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 IMAGE_BITMAP,
8032 TOOLBAR_BUTTON_WIDTH,
8033 TOOLBAR_BUTTON_HEIGHT,
8034 LR_LOADFROMFILE |
8035 LR_LOADMAP3DCOLORS
8036 );
8037
8038 if (hbitmap != NULL)
8039 {
8040 TBADDBITMAP tbAddBitmap;
8041
8042 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008043 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
8045 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8046 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008047 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
8049 }
8050 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8051 i = menu->iconidx;
8052
8053 return i;
8054}
8055#endif
8056
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008057#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8058 static void
8059initialise_tabline(void)
8060{
8061 InitCommonControls();
8062
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008063 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008064 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008065 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008066 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008067 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008068
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008069 gui.tabline_height = TABLINE_HEIGHT;
8070
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008071 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008072}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008073
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008074/*
8075 * Get tabpage_T from POINT.
8076 */
8077 static tabpage_T *
8078GetTabFromPoint(
8079 HWND hWnd,
8080 POINT pt)
8081{
8082 tabpage_T *ptp = NULL;
8083
8084 if (gui_mch_showing_tabline())
8085 {
8086 TCHITTESTINFO htinfo;
8087 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008088 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008089 if (s_tabhwnd == hWnd)
8090 {
8091 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8092 if (idx != -1)
8093 ptp = find_tabpage(idx + 1);
8094 }
8095 }
8096 return ptp;
8097}
8098
8099static POINT s_pt = {0, 0};
8100static HCURSOR s_hCursor = NULL;
8101
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008102 static LRESULT CALLBACK
8103tabline_wndproc(
8104 HWND hwnd,
8105 UINT uMsg,
8106 WPARAM wParam,
8107 LPARAM lParam)
8108{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008109 POINT pt;
8110 tabpage_T *tp;
8111 RECT rect;
8112 int nCenter;
8113 int idx0;
8114 int idx1;
8115
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008116 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008117
8118 switch (uMsg)
8119 {
8120 case WM_LBUTTONDOWN:
8121 {
8122 s_pt.x = GET_X_LPARAM(lParam);
8123 s_pt.y = GET_Y_LPARAM(lParam);
8124 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008125 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008126 break;
8127 }
8128 case WM_MOUSEMOVE:
8129 if (GetCapture() == hwnd
8130 && ((wParam & MK_LBUTTON)) != 0)
8131 {
8132 pt.x = GET_X_LPARAM(lParam);
8133 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008134 if (abs(pt.x - s_pt.x) >
8135 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008136 {
8137 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8138
8139 tp = GetTabFromPoint(hwnd, pt);
8140 if (tp != NULL)
8141 {
8142 idx0 = tabpage_index(curtab) - 1;
8143 idx1 = tabpage_index(tp) - 1;
8144
8145 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8146 nCenter = rect.left + (rect.right - rect.left) / 2;
8147
Bram Moolenaar734a8672019-12-02 22:49:38 +01008148 // Check if the mouse cursor goes over the center of
8149 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008150 if ((idx0 < idx1) && (nCenter < pt.x))
8151 {
8152 tabpage_move(idx1 + 1);
8153 update_screen(0);
8154 }
8155 else if ((idx1 < idx0) && (pt.x < nCenter))
8156 {
8157 tabpage_move(idx1);
8158 update_screen(0);
8159 }
8160 }
8161 }
8162 }
8163 break;
8164 case WM_LBUTTONUP:
8165 {
8166 if (GetCapture() == hwnd)
8167 {
8168 SetCursor(s_hCursor);
8169 ReleaseCapture();
8170 }
8171 break;
8172 }
8173 default:
8174 break;
8175 }
8176
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008177 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8178}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008179#endif
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8182/*
8183 * Make the GUI window come to the foreground.
8184 */
8185 void
8186gui_mch_set_foreground(void)
8187{
8188 if (IsIconic(s_hwnd))
8189 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8190 SetForegroundWindow(s_hwnd);
8191}
8192#endif
8193
8194#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8195 static void
8196dyn_imm_load(void)
8197{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008198 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (hLibImm == NULL)
8200 return;
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 pImmGetCompositionStringW
8203 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8204 pImmGetContext
8205 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8206 pImmAssociateContext
8207 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8208 pImmReleaseContext
8209 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8210 pImmGetOpenStatus
8211 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8212 pImmSetOpenStatus
8213 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008214 pImmGetCompositionFontW
8215 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8216 pImmSetCompositionFontW
8217 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 pImmSetCompositionWindow
8219 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8220 pImmGetConversionStatus
8221 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008222 pImmSetConversionStatus
8223 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
K.Takatab0b2b732022-01-19 12:59:21 +00008225 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 || pImmGetContext == NULL
8227 || pImmAssociateContext == NULL
8228 || pImmReleaseContext == NULL
8229 || pImmGetOpenStatus == NULL
8230 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008231 || pImmGetCompositionFontW == NULL
8232 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008234 || pImmGetConversionStatus == NULL
8235 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
8237 FreeLibrary(hLibImm);
8238 hLibImm = NULL;
8239 pImmGetContext = NULL;
8240 return;
8241 }
8242
8243 return;
8244}
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#endif
8247
8248#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8249
8250# ifdef FEAT_XPM_W32
8251# define IMAGE_XPM 100
8252# endif
8253
8254typedef struct _signicon_t
8255{
8256 HANDLE hImage;
8257 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008258# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008259 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261} signicon_t;
8262
8263 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008264gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 signicon_t *sign;
8267 int x, y, w, h;
8268
8269 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8270 return;
8271
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008272# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008273 if (IS_ENABLE_DIRECTX())
8274 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008275# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 x = TEXT_X(col);
8278 y = TEXT_Y(row);
8279 w = gui.char_width * 2;
8280 h = gui.char_height;
8281 switch (sign->uType)
8282 {
8283 case IMAGE_BITMAP:
8284 {
8285 HDC hdcMem;
8286 HBITMAP hbmpOld;
8287
8288 hdcMem = CreateCompatibleDC(s_hdc);
8289 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8290 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8291 SelectObject(hdcMem, hbmpOld);
8292 DeleteDC(hdcMem);
8293 }
8294 break;
8295 case IMAGE_ICON:
8296 case IMAGE_CURSOR:
8297 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8298 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008299# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 case IMAGE_XPM:
8301 {
8302 HDC hdcMem;
8303 HBITMAP hbmpOld;
8304
8305 hdcMem = CreateCompatibleDC(s_hdc);
8306 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008307 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8309
8310 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008311 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8313 SelectObject(hdcMem, hbmpOld);
8314 DeleteDC(hdcMem);
8315 }
8316 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319}
8320
8321 static void
8322close_signicon_image(signicon_t *sign)
8323{
8324 if (sign)
8325 switch (sign->uType)
8326 {
8327 case IMAGE_BITMAP:
8328 DeleteObject((HGDIOBJ)sign->hImage);
8329 break;
8330 case IMAGE_CURSOR:
8331 DestroyCursor((HCURSOR)sign->hImage);
8332 break;
8333 case IMAGE_ICON:
8334 DestroyIcon((HICON)sign->hImage);
8335 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008336# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 case IMAGE_XPM:
8338 DeleteObject((HBITMAP)sign->hImage);
8339 DeleteObject((HBITMAP)sign->hShape);
8340 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343}
8344
8345 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008346gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347{
8348 signicon_t sign, *psign;
8349 char_u *ext;
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008352 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 if (ext > signfile)
8354 {
8355 int do_load = 1;
8356
8357 if (!STRICMP(ext, ".bmp"))
8358 sign.uType = IMAGE_BITMAP;
8359 else if (!STRICMP(ext, ".ico"))
8360 sign.uType = IMAGE_ICON;
8361 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8362 sign.uType = IMAGE_CURSOR;
8363 else
8364 do_load = 0;
8365
8366 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008367 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 gui.char_width * 2, gui.char_height,
8369 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008370# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 if (!STRICMP(ext, ".xpm"))
8372 {
8373 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008374 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8375 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
8379
8380 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008381 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 *psign = sign;
8383
8384 if (!psign)
8385 {
8386 if (sign.hImage)
8387 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008388 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390 return (void *)psign;
8391
8392}
8393
8394 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008395gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 if (sign)
8398 {
8399 close_signicon_image((signicon_t *)sign);
8400 vim_free(sign);
8401 }
8402}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008405#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
Bram Moolenaar734a8672019-12-02 22:49:38 +01008407/*
8408 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008409 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008411 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8413 * to get current mouse position).
8414 *
8415 * Trying to use as more Windows services as possible, and as less
8416 * IE version as possible :)).
8417 *
8418 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8419 * BalloonEval struct.
8420 * 2) Enable/Disable simply create/kill BalloonEval Timer
8421 * 3) When there was enough inactivity, timer procedure posts
8422 * async request to debugger
8423 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8424 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008425 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 */
8427
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008428 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008429make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008430{
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008431 TOOLINFOW_NEW *pti;
8432 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008433
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008434 pti = alloc(sizeof(TOOLINFOW_NEW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008435 if (pti == NULL)
8436 return;
8437
8438 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8439 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8440 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008441 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008442
8443 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8444 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8445
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008446 pti->cbSize = sizeof(TOOLINFOW_NEW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008447 pti->uFlags = TTF_SUBCLASS;
8448 pti->hwnd = beval->target;
8449 pti->hinst = 0; // Don't use string resources
8450 pti->uId = ID_BEVAL_TOOLTIP;
8451
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008452 pti->lpszText = LPSTR_TEXTCALLBACKW;
8453 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8454 pti->lParam = (LPARAM)beval->tofree;
8455 // switch multiline tooltips on
8456 if (GetClientRect(s_textArea, &rect))
8457 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8458 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008459
8460 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8461 pti->rect.left = pt.x - 3;
8462 pti->rect.top = pt.y - 3;
8463 pti->rect.right = pt.x + 3;
8464 pti->rect.bottom = pt.y + 3;
8465
8466 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8467 // Make tooltip appear sooner.
8468 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8469 // I've performed some tests and it seems the longest possible life time
8470 // of tooltip is 30 seconds.
8471 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8472 /*
8473 * HACK: force tooltip to appear, because it'll not appear until
8474 * first mouse move. D*mn M$
8475 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8476 */
8477 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8478 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8479 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008480}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008483delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008485 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486}
8487
8488 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008489BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008490 HWND hwnd UNUSED,
8491 UINT uMsg UNUSED,
8492 UINT_PTR idEvent UNUSED,
8493 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495 POINT pt;
8496 RECT rect;
8497
8498 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8499 return;
8500
8501 GetCursorPos(&pt);
8502 if (WindowFromPoint(pt) != s_textArea)
8503 return;
8504
8505 ScreenToClient(s_textArea, &pt);
8506 GetClientRect(s_textArea, &rect);
8507 if (!PtInRect(&rect, pt))
8508 return;
8509
8510 if (LastActivity > 0
8511 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8512 && (cur_beval->showState != ShS_PENDING
8513 || abs(cur_beval->x - pt.x) > 3
8514 || abs(cur_beval->y - pt.y) > 3))
8515 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008516 // Pointer resting in one place long enough, it's time to show
8517 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 cur_beval->showState = ShS_PENDING;
8519 cur_beval->x = pt.x;
8520 cur_beval->y = pt.y;
8521
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008522 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523
8524 if (cur_beval->msgCB != NULL)
8525 (*cur_beval->msgCB)(cur_beval, 0);
8526 }
8527}
8528
8529 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008530gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008532 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008534 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535}
8536
8537 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008538gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008540 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 if (beval == NULL)
8542 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008543 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008544 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008545 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546}
8547
8548 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008549gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008552
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008553 vim_free(beval->msg);
8554 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8555 if (beval->msg == NULL)
8556 {
8557 delete_tooltip(beval);
8558 beval->showState = ShS_NEUTRAL;
8559 return;
8560 }
8561
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008562 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 if (beval->showState == ShS_SHOWING)
8564 return;
8565 GetCursorPos(&pt);
8566 ScreenToClient(s_textArea, &pt);
8567
8568 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008570 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 gui_mch_disable_beval_area(cur_beval);
8572 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008573 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008575 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576}
8577
8578 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008579gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008580 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008581 char_u *mesg,
8582 void (*mesgCB)(BalloonEval *, int),
8583 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008585 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 BalloonEval *beval;
8587
8588 if (mesg != NULL && mesgCB != NULL)
8589 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008590 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 return NULL;
8592 }
8593
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008594 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 if (beval != NULL)
8596 {
8597 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 beval->msg = mesg;
8601 beval->msgCB = mesgCB;
8602 beval->clientData = clientData;
8603
8604 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 cur_beval = beval;
8606
8607 if (p_beval)
8608 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
8610 return beval;
8611}
8612
8613 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008614Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008616 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 return;
8618
8619 if (cur_beval != NULL)
8620 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008621 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008623 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008624 // TRACE0("TTN_SHOW {{{");
8625 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008626 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008627 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008628 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 delete_tooltip(cur_beval);
8630 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008631 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632
8633 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008634 break;
8635 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008636 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008637 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008638 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8639 info->lpszText = (LPSTR)info->lParam;
8640 info->uFlags |= TTF_DI_SETITEM;
8641 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008642 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008643 case TTN_GETDISPINFOW:
8644 {
8645 // if we get here then we have new common controls
8646 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8647 info->lpszText = (LPWSTR)info->lParam;
8648 info->uFlags |= TTF_DI_SETITEM;
8649 }
8650 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652 }
8653}
8654
8655 static void
8656TrackUserActivity(UINT uMsg)
8657{
8658 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8659 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8660 LastActivity = GetTickCount();
8661}
8662
8663 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008664gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008666# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008667 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008668# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008669 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 vim_free(beval);
8671}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008672#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673
8674#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8675/*
8676 * We have multiple signs to draw at the same location. Draw the
8677 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8678 */
8679 void
8680netbeans_draw_multisign_indicator(int row)
8681{
8682 int i;
8683 int y;
8684 int x;
8685
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008686 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008687 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 x = 0;
8690 y = TEXT_Y(row);
8691
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008692# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008693 if (IS_ENABLE_DIRECTX())
8694 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008695# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 for (i = 0; i < gui.char_height - 3; i++)
8698 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8699
8700 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8701 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8702 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8703 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8704 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8705 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8706 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8707}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008708#endif