blob: c6a8b6d6e7bc7a0c9ea98d497342d24cc1961d8e [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
K.Takatac81e9bf2022-01-16 14:15:49 +0000221#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100222# define WM_DPICHANGED 0x02E0
223#endif
224
225#ifndef WM_MOUSEHWHEEL
226# define WM_MOUSEHWHEEL 0x020E
227#endif
228
229#ifndef SPI_GETWHEELSCROLLCHARS
230# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000231#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
K.Takata45f9cfb2022-01-21 11:11:00 +0000291static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100292static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100293static void clear_rect(RECT *rcp);
294
Bram Moolenaar734a8672019-12-02 22:49:38 +0100295static WORD s_dlgfntheight; // height of the dialog font
296static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297
298#ifdef FEAT_MENU
299static HMENU s_menuBar = NULL;
300#endif
301#ifdef FEAT_TEAROFF
302static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100303static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100304#endif
305
Bram Moolenaar734a8672019-12-02 22:49:38 +0100306// Flag that is set while processing a message that must not be interrupted by
307// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308static int s_busy_processing = FALSE;
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100311
312#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000313static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200314static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200316static int s_findrep_is_find; // TRUE for find dialog, FALSE
317 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318#endif
319
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100320HWND s_hwnd = NULL;
321static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100322static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100323
324#ifdef FEAT_TOOLBAR
325static HWND s_toolbarhwnd = NULL;
326static WNDPROC s_toolbar_wndproc = NULL;
327#endif
328
329#ifdef FEAT_GUI_TABLINE
330static HWND s_tabhwnd = NULL;
331static WNDPROC s_tabline_wndproc = NULL;
332static int showing_tabline = 0;
333#endif
334
335static WPARAM s_wParam = 0;
336static LPARAM s_lParam = 0;
337
338static HWND s_textArea = NULL;
339static UINT s_uMsg = 0;
340
Bram Moolenaar734a8672019-12-02 22:49:38 +0100341static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100342
343static int s_need_activate = FALSE;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345// This variable is set when waiting for an event, which is the only moment
346// scrollbar dragging can be done directly. It's not allowed while commands
347// are executed, because it may move the cursor and that may cause unexpected
348// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100349static int allow_scrollbar = FALSE;
350
K.Takatac81e9bf2022-01-16 14:15:49 +0000351#ifndef _DPI_AWARENESS_CONTEXTS_
352typedef HANDLE DPI_AWARENESS_CONTEXT;
353
354typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000355 DPI_AWARENESS_INVALID = -1,
356 DPI_AWARENESS_UNAWARE = 0,
357 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000358 DPI_AWARENESS_PER_MONITOR_AWARE = 2
359} DPI_AWARENESS;
360
K.Takatab0b2b732022-01-19 12:59:21 +0000361# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
362# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000363# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
364# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
365# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
366#endif
367
368#define DEFAULT_DPI 96
369static int s_dpi = DEFAULT_DPI;
370static BOOL s_in_dpichanged = FALSE;
371static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
372
373static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
374static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
375static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
376//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
377static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
378static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
379
380 static UINT WINAPI
381stubGetDpiForSystem(void)
382{
383 HWND hwnd = GetDesktopWindow();
384 HDC hdc = GetWindowDC(hwnd);
385 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
386 ReleaseDC(hwnd, hdc);
387 return dpi;
388}
389
390 static int WINAPI
391stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
392{
393 return GetSystemMetrics(nIndex);
394}
395
396 static int
397adjust_fontsize_by_dpi(int size)
398{
399 return size * s_dpi / (int)pGetDpiForSystem();
400}
401
402 static int
403adjust_by_system_dpi(int size)
404{
405 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
406}
407
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100408#if defined(FEAT_DIRECTX)
409 static int
410directx_enabled(void)
411{
412 if (s_dwc != NULL)
413 return 1;
414 else if (s_directx_load_attempted)
415 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100416 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100417 DWrite_Init();
418 s_directx_load_attempted = 1;
419 s_dwc = DWriteContext_Open();
420 directx_binddc();
421 return s_dwc != NULL ? 1 : 0;
422}
423
424 static void
425directx_binddc(void)
426{
427 if (s_textArea != NULL)
428 {
429 RECT rect;
430 GetClientRect(s_textArea, &rect);
431 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
432 }
433}
434#endif
435
Bram Moolenaar734a8672019-12-02 22:49:38 +0100436extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100437
438static struct
439{
440 UINT key_sym;
441 char_u vim_code0;
442 char_u vim_code1;
443} special_keys[] =
444{
445 {VK_UP, 'k', 'u'},
446 {VK_DOWN, 'k', 'd'},
447 {VK_LEFT, 'k', 'l'},
448 {VK_RIGHT, 'k', 'r'},
449
450 {VK_F1, 'k', '1'},
451 {VK_F2, 'k', '2'},
452 {VK_F3, 'k', '3'},
453 {VK_F4, 'k', '4'},
454 {VK_F5, 'k', '5'},
455 {VK_F6, 'k', '6'},
456 {VK_F7, 'k', '7'},
457 {VK_F8, 'k', '8'},
458 {VK_F9, 'k', '9'},
459 {VK_F10, 'k', ';'},
460
461 {VK_F11, 'F', '1'},
462 {VK_F12, 'F', '2'},
463 {VK_F13, 'F', '3'},
464 {VK_F14, 'F', '4'},
465 {VK_F15, 'F', '5'},
466 {VK_F16, 'F', '6'},
467 {VK_F17, 'F', '7'},
468 {VK_F18, 'F', '8'},
469 {VK_F19, 'F', '9'},
470 {VK_F20, 'F', 'A'},
471
472 {VK_F21, 'F', 'B'},
473#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100474 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100475#endif
476 {VK_F22, 'F', 'C'},
477 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100478 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479
480 {VK_HELP, '%', '1'},
481 {VK_BACK, 'k', 'b'},
482 {VK_INSERT, 'k', 'I'},
483 {VK_DELETE, 'k', 'D'},
484 {VK_HOME, 'k', 'h'},
485 {VK_END, '@', '7'},
486 {VK_PRIOR, 'k', 'P'},
487 {VK_NEXT, 'k', 'N'},
488 {VK_PRINT, '%', '9'},
489 {VK_ADD, 'K', '6'},
490 {VK_SUBTRACT, 'K', '7'},
491 {VK_DIVIDE, 'K', '8'},
492 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100493 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494 {VK_DECIMAL, 'K', 'B'},
495
496 {VK_NUMPAD0, 'K', 'C'},
497 {VK_NUMPAD1, 'K', 'D'},
498 {VK_NUMPAD2, 'K', 'E'},
499 {VK_NUMPAD3, 'K', 'F'},
500 {VK_NUMPAD4, 'K', 'G'},
501 {VK_NUMPAD5, 'K', 'H'},
502 {VK_NUMPAD6, 'K', 'I'},
503 {VK_NUMPAD7, 'K', 'J'},
504 {VK_NUMPAD8, 'K', 'K'},
505 {VK_NUMPAD9, 'K', 'L'},
506
Bram Moolenaar734a8672019-12-02 22:49:38 +0100507 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100508 {VK_SPACE, ' ', NUL},
509 {VK_TAB, TAB, NUL},
510 {VK_ESCAPE, ESC, NUL},
511 {NL, NL, NUL},
512 {CAR, CAR, NUL},
513
Bram Moolenaar734a8672019-12-02 22:49:38 +0100514 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100515 {0, 0, 0}
516};
517
Bram Moolenaar734a8672019-12-02 22:49:38 +0100518// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100519static int s_button_pending = -1;
520
Bram Moolenaar734a8672019-12-02 22:49:38 +0100521// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
522// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523static int s_getting_focus = FALSE;
524
525static int s_x_pending;
526static int s_y_pending;
527static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000528static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200530static int dead_key = 0; // 0: no dead key, 1: dead key pressed
531static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
532 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100534#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100535// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100536static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000537static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100538#endif
539
540/*
541 * For control IME.
542 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100543 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100544 */
K.Takata4ac893f2022-01-20 12:44:28 +0000545#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100546// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100547static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100548// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100549static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100550#endif
551
552#ifdef FEAT_MBYTE_IME
553static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
554#endif
555
556#if defined(FEAT_BROWSE)
557static char_u *convert_filter(char_u *s);
558#endif
559
560#ifdef DEBUG_PRINT_ERROR
561/*
562 * Print out the last Windows error message
563 */
564 static void
565print_windows_error(void)
566{
567 LPVOID lpMsgBuf;
568
569 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
570 NULL, GetLastError(),
571 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
572 (LPTSTR) &lpMsgBuf, 0, NULL);
573 TRACE1("Error: %s\n", lpMsgBuf);
574 LocalFree(lpMsgBuf);
575}
576#endif
577
578/*
579 * Cursor blink functions.
580 *
581 * This is a simple state machine:
582 * BLINK_NONE not blinking at all
583 * BLINK_OFF blinking, cursor is not shown
584 * BLINK_ON blinking, cursor is shown
585 */
586
587#define BLINK_NONE 0
588#define BLINK_OFF 1
589#define BLINK_ON 2
590
591static int blink_state = BLINK_NONE;
592static long_u blink_waittime = 700;
593static long_u blink_ontime = 400;
594static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000595static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100596
Bram Moolenaar703a8042016-06-04 16:24:32 +0200597 int
598gui_mch_is_blinking(void)
599{
600 return blink_state != BLINK_NONE;
601}
602
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200603 int
604gui_mch_is_blink_off(void)
605{
606 return blink_state == BLINK_OFF;
607}
608
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609 void
610gui_mch_set_blinking(long wait, long on, long off)
611{
612 blink_waittime = wait;
613 blink_ontime = on;
614 blink_offtime = off;
615}
616
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100617 static VOID CALLBACK
618_OnBlinkTimer(
619 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100620 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000621 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100622 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100623{
624 MSG msg;
625
626 /*
627 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
628 */
629
630 KillTimer(NULL, idEvent);
631
Bram Moolenaar734a8672019-12-02 22:49:38 +0100632 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000633 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100634 ;
635
636 if (blink_state == BLINK_ON)
637 {
638 gui_undraw_cursor();
639 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000640 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641 }
642 else
643 {
644 gui_update_cursor(TRUE, FALSE);
645 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000646 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100647 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100648 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100649}
650
651 static void
652gui_mswin_rm_blink_timer(void)
653{
654 MSG msg;
655
656 if (blink_timer != 0)
657 {
658 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100659 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000660 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100661 ;
662 blink_timer = 0;
663 }
664}
665
666/*
667 * Stop the cursor blinking. Show the cursor if it wasn't shown.
668 */
669 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100670gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671{
672 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100673 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100674 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100675 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100676 gui_mch_flush();
677 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100678 blink_state = BLINK_NONE;
679}
680
681/*
682 * Start the cursor blinking. If it was already blinking, this restarts the
683 * waiting time and shows the cursor.
684 */
685 void
686gui_mch_start_blink(void)
687{
688 gui_mswin_rm_blink_timer();
689
Bram Moolenaar734a8672019-12-02 22:49:38 +0100690 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100691 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
692 {
K.Takataa8ec4912022-02-03 14:32:33 +0000693 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100694 blink_state = BLINK_ON;
695 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100696 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100697 }
698}
699
700/*
701 * Call-back routines.
702 */
703
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100704 static VOID CALLBACK
705_OnTimer(
706 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100707 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000708 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100709 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100710{
711 MSG msg;
712
713 /*
714 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
715 */
716 KillTimer(NULL, idEvent);
717 s_timed_out = TRUE;
718
Bram Moolenaar734a8672019-12-02 22:49:38 +0100719 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000720 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100721 ;
722 if (idEvent == s_wait_timer)
723 s_wait_timer = 0;
724}
725
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100726 static void
727_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100728 HWND hwnd UNUSED,
729 UINT ch UNUSED,
730 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100731{
732 dead_key = 1;
733}
734
735/*
736 * Convert Unicode character "ch" to bytes in "string[slen]".
737 * When "had_alt" is TRUE the ALT key was included in "ch".
738 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200739 * Because the Windows API uses UTF-16, we have to deal with surrogate
740 * pairs; this is where we choose to deal with them: if "ch" is a high
741 * surrogate, it will be stored, and the length returned will be zero; the next
742 * char_to_string call will then include the high surrogate, decoding the pair
743 * of UTF-16 code units to a single Unicode code point, presuming it is the
744 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100745 */
746 static int
747char_to_string(int ch, char_u *string, int slen, int had_alt)
748{
749 int len;
750 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100751 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200752 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100753
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200754 if (surrogate_pending_ch != 0)
755 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100756 // We don't guarantee ch is a low surrogate to match the high surrogate
757 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200758 wstring[0] = surrogate_pending_ch;
759 wstring[1] = ch;
760 surrogate_pending_ch = 0;
761 len = 2;
762 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100763 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200764 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100765 // We don't have the entire code point yet, only the first UTF-16 code
766 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200767 surrogate_pending_ch = ch;
768 return 0;
769 }
770 else
771 {
772 wstring[0] = ch;
773 len = 1;
774 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200775
Bram Moolenaar734a8672019-12-02 22:49:38 +0100776 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
777 // "enc_codepage" is non-zero use the standard Win32 function,
778 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200779 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100780 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200781 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
782 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100783 // If we had included the ALT key into the character but now the
784 // upper bit is no longer set, that probably means the conversion
785 // failed. Convert the original character and set the upper bit
786 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200787 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100788 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200789 wstring[0] = ch & 0x7f;
790 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
791 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100792 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100794 }
795 }
796 else
797 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798 ws = utf16_to_enc(wstring, &len);
799 if (ws == NULL)
800 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801 else
802 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100803 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200804 len = slen;
805 mch_memmove(string, ws, len);
806 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100807 }
808 }
809
810 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 {
812 string[0] = ch;
813 len = 1;
814 }
815
816 for (i = 0; i < len; ++i)
817 if (string[i] == CSI && len <= slen - 2)
818 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100819 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
821 string[++i] = KS_EXTRA;
822 string[++i] = (int)KE_CSI;
823 len += 2;
824 }
825
826 return len;
827}
828
LemonBoy45684c62022-04-24 15:46:42 +0100829 static int
830get_active_modifiers(void)
831{
832 int modifiers = 0;
833
834 if (GetKeyState(VK_CONTROL) & 0x8000)
835 modifiers |= MOD_MASK_CTRL;
836 if (GetKeyState(VK_SHIFT) & 0x8000)
837 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100838 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
839 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100840 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100841 if (GetKeyState(VK_LMENU) & 0x8000)
842 modifiers |= MOD_MASK_ALT;
843 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
844 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100845
846 return modifiers;
847}
848
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100849/*
850 * Key hit, add it to the input buffer.
851 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100852 static void
853_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100854 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100855 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100856 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100857{
858 char_u string[40];
859 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100860 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100861 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100862
863 dead_key = 0;
864
LemonBoy45684c62022-04-24 15:46:42 +0100865 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100866
867 ch = simplify_key(ch, &modifiers);
868 // remove the SHIFT modifier for keys where it's already included, e.g.,
869 // '(' and '*'
870 modifiers = may_remove_shift_modifier(modifiers, ch);
871
872 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
873 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
874 if (ch == CSI)
875 ch = K_CSI;
876
877 if (modifiers)
878 {
879 string[0] = CSI;
880 string[1] = KS_MODIFIER;
881 string[2] = modifiers;
882 add_to_input_buf(string, 3);
883 }
884
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100885 len = char_to_string(ch, string, 40, FALSE);
886 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
887 {
888 trash_input_buf();
889 got_int = TRUE;
890 }
891
892 add_to_input_buf(string, len);
893}
894
895/*
896 * Alt-Key hit, add it to the input buffer.
897 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100898 static void
899_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100900 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100902 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100904 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100905 int len;
906 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100907 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100908
909 dead_key = 0;
910
Bram Moolenaar734a8672019-12-02 22:49:38 +0100911 // OK, we have a character key (given by ch) which was entered with the
912 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
913 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
914 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100915 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100916 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100917 // remove the SHIFT modifier for keys where it's already included, e.g.,
918 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200919 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100920
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200921 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
922 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100923 if (ch == CSI)
924 ch = K_CSI;
925
926 len = 0;
927 if (modifiers)
928 {
929 string[len++] = CSI;
930 string[len++] = KS_MODIFIER;
931 string[len++] = modifiers;
932 }
933
934 if (IS_SPECIAL((int)ch))
935 {
936 string[len++] = CSI;
937 string[len++] = K_SECOND((int)ch);
938 string[len++] = K_THIRD((int)ch);
939 }
940 else
941 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100942 // Although the documentation isn't clear about it, we assume "ch" is
943 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944 len += char_to_string(ch, string + len, 40 - len, TRUE);
945 }
946
947 add_to_input_buf(string, len);
948}
949
950 static void
951_OnMouseEvent(
952 int button,
953 int x,
954 int y,
955 int repeated_click,
956 UINT keyFlags)
957{
958 int vim_modifiers = 0x0;
959
960 s_getting_focus = FALSE;
961
962 if (keyFlags & MK_SHIFT)
963 vim_modifiers |= MOUSE_SHIFT;
964 if (keyFlags & MK_CONTROL)
965 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100966 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100967 vim_modifiers |= MOUSE_ALT;
968
969 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
970}
971
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100972 static void
973_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100974 HWND hwnd UNUSED,
975 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100976 int x,
977 int y,
978 UINT keyFlags)
979{
980 static LONG s_prevTime = 0;
981
982 LONG currentTime = GetMessageTime();
983 int button = -1;
984 int repeated_click;
985
Bram Moolenaar734a8672019-12-02 22:49:38 +0100986 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100987 (void)SetFocus(s_hwnd);
988
989 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
990 button = MOUSE_LEFT;
991 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
992 button = MOUSE_MIDDLE;
993 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
994 button = MOUSE_RIGHT;
995 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
996 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100997 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
998 }
999 else if (s_uMsg == WM_CAPTURECHANGED)
1000 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001001 // on W95/NT4, somehow you get in here with an odd Msg
1002 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001003 if (s_button_pending == MOUSE_LEFT)
1004 button = MOUSE_RIGHT;
1005 else
1006 button = MOUSE_LEFT;
1007 }
1008 if (button >= 0)
1009 {
1010 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1011
1012 /*
1013 * Holding down the left and right buttons simulates pushing the middle
1014 * button.
1015 */
1016 if (repeated_click
1017 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1018 || (button == MOUSE_RIGHT
1019 && s_button_pending == MOUSE_LEFT)))
1020 {
1021 /*
1022 * Hmm, gui.c will ignore more than one button down at a time, so
1023 * pretend we let go of it first.
1024 */
1025 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1026 button = MOUSE_MIDDLE;
1027 repeated_click = FALSE;
1028 s_button_pending = -1;
1029 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1030 }
1031 else if ((repeated_click)
1032 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1033 {
1034 if (s_button_pending > -1)
1035 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001036 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1037 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001038 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001039 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001040 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1041 }
1042 else
1043 {
1044 /*
1045 * If this is the first press (i.e. not a multiple click) don't
1046 * action immediately, but store and wait for:
1047 * i) button-up
1048 * ii) mouse move
1049 * iii) another button press
1050 * before using it.
1051 * This enables us to make left+right simulate middle button,
1052 * without left or right being actioned first. The side-effect is
1053 * that if you click and hold the mouse without dragging, the
1054 * cursor doesn't move until you release the button. In practice
1055 * this is hardly a problem.
1056 */
1057 s_button_pending = button;
1058 s_x_pending = x;
1059 s_y_pending = y;
1060 s_kFlags_pending = keyFlags;
1061 }
1062
1063 s_prevTime = currentTime;
1064 }
1065}
1066
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001067 static void
1068_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001069 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001070 int x,
1071 int y,
1072 UINT keyFlags)
1073{
1074 int button;
1075
1076 s_getting_focus = FALSE;
1077 if (s_button_pending > -1)
1078 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001079 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001080 _OnMouseEvent(s_button_pending, s_x_pending,
1081 s_y_pending, FALSE, s_kFlags_pending);
1082 s_button_pending = -1;
1083 }
1084 if (s_uMsg == WM_MOUSEMOVE)
1085 {
1086 /*
1087 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1088 * down.
1089 */
1090 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1091 | MK_XBUTTON1 | MK_XBUTTON2)))
1092 {
1093 gui_mouse_moved(x, y);
1094 return;
1095 }
1096
1097 /*
1098 * While button is down, keep grabbing mouse move events when
1099 * the mouse goes outside the window
1100 */
1101 SetCapture(s_textArea);
1102 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001103 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001104 }
1105 else
1106 {
1107 ReleaseCapture();
1108 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001109 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001110 }
1111
1112 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1113}
1114
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001115 static void
1116_OnSizeTextArea(
1117 HWND hwnd UNUSED,
1118 UINT state UNUSED,
1119 int cx UNUSED,
1120 int cy UNUSED)
1121{
1122#if defined(FEAT_DIRECTX)
1123 if (IS_ENABLE_DIRECTX())
1124 directx_binddc();
1125#endif
1126}
1127
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001128#ifdef FEAT_MENU
1129/*
1130 * Find the vimmenu_T with the given id
1131 */
1132 static vimmenu_T *
1133gui_mswin_find_menu(
1134 vimmenu_T *pMenu,
1135 int id)
1136{
1137 vimmenu_T *pChildMenu;
1138
1139 while (pMenu)
1140 {
1141 if (pMenu->id == (UINT)id)
1142 break;
1143 if (pMenu->children != NULL)
1144 {
1145 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1146 if (pChildMenu)
1147 {
1148 pMenu = pChildMenu;
1149 break;
1150 }
1151 }
1152 pMenu = pMenu->next;
1153 }
1154 return pMenu;
1155}
1156
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 static void
1158_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001159 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001160 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001161 HWND hwndCtl UNUSED,
1162 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001163{
1164 vimmenu_T *pMenu;
1165
1166 pMenu = gui_mswin_find_menu(root_menu, id);
1167 if (pMenu)
1168 gui_menu_cb(pMenu);
1169}
1170#endif
1171
1172#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001174 * Handle a Find/Replace window message.
1175 */
1176 static void
1177_OnFindRepl(void)
1178{
1179 int flags = 0;
1180 int down;
1181
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001182 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001183 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 (void)SetFocus(s_hwnd);
1185
1186 if (s_findrep_struct.Flags & FR_FINDNEXT)
1187 {
1188 flags = FRD_FINDNEXT;
1189
Bram Moolenaar734a8672019-12-02 22:49:38 +01001190 // Give main window the focus back: this is so the cursor isn't
1191 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 (void)SetFocus(s_hwnd);
1193 }
1194 else if (s_findrep_struct.Flags & FR_REPLACE)
1195 {
1196 flags = FRD_REPLACE;
1197
Bram Moolenaar734a8672019-12-02 22:49:38 +01001198 // Give main window the focus back: this is so the cursor isn't
1199 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 (void)SetFocus(s_hwnd);
1201 }
1202 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1203 {
1204 flags = FRD_REPLACEALL;
1205 }
1206
1207 if (flags != 0)
1208 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001209 char_u *p, *q;
1210
Bram Moolenaar734a8672019-12-02 22:49:38 +01001211 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001212 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1213 flags |= FRD_WHOLE_WORD;
1214 if (s_findrep_struct.Flags & FR_MATCHCASE)
1215 flags |= FRD_MATCH_CASE;
1216 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001217 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1218 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1219 if (p != NULL && q != NULL)
1220 gui_do_findrepl(flags, p, q, down);
1221 vim_free(p);
1222 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001223 }
1224}
1225#endif
1226
1227 static void
1228HandleMouseHide(UINT uMsg, LPARAM lParam)
1229{
1230 static LPARAM last_lParam = 0L;
1231
Bram Moolenaar734a8672019-12-02 22:49:38 +01001232 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001233 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1234 {
1235 if (lParam == last_lParam)
1236 return;
1237 last_lParam = lParam;
1238 }
1239
Bram Moolenaar734a8672019-12-02 22:49:38 +01001240 // Handle specially, to centralise coding. We need to be sure we catch all
1241 // possible events which should cause us to restore the cursor (as it is a
1242 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001243 switch (uMsg)
1244 {
1245 case WM_KEYUP:
1246 case WM_CHAR:
1247 /*
1248 * blank out the pointer if necessary
1249 */
1250 if (p_mh)
1251 gui_mch_mousehide(TRUE);
1252 break;
1253
Bram Moolenaar734a8672019-12-02 22:49:38 +01001254 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001255 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001256 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001257 case WM_LBUTTONDOWN:
1258 case WM_LBUTTONUP:
1259 case WM_MBUTTONDOWN:
1260 case WM_MBUTTONUP:
1261 case WM_RBUTTONDOWN:
1262 case WM_RBUTTONUP:
1263 case WM_XBUTTONDOWN:
1264 case WM_XBUTTONUP:
1265 case WM_NCMOUSEMOVE:
1266 case WM_NCLBUTTONDOWN:
1267 case WM_NCLBUTTONUP:
1268 case WM_NCMBUTTONDOWN:
1269 case WM_NCMBUTTONUP:
1270 case WM_NCRBUTTONDOWN:
1271 case WM_NCRBUTTONUP:
1272 case WM_KILLFOCUS:
1273 /*
1274 * if the pointer is currently hidden, then we should show it.
1275 */
1276 gui_mch_mousehide(FALSE);
1277 break;
1278 }
1279}
1280
1281 static LRESULT CALLBACK
1282_TextAreaWndProc(
1283 HWND hwnd,
1284 UINT uMsg,
1285 WPARAM wParam,
1286 LPARAM lParam)
1287{
1288 /*
1289 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1290 hwnd, uMsg, wParam, lParam);
1291 */
1292
1293 HandleMouseHide(uMsg, lParam);
1294
1295 s_uMsg = uMsg;
1296 s_wParam = wParam;
1297 s_lParam = lParam;
1298
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001299#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001300 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001301#endif
1302
1303 switch (uMsg)
1304 {
1305 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1306 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1307 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1308 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1309 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1310 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1311 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1312 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1313 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1314 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1315 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1316 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1317 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1318 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001319 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001320
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001321#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001322 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1323 return TRUE;
1324#endif
1325 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001326 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001327 }
1328}
1329
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001330/*
1331 * Called when the foreground or background color has been changed.
1332 */
1333 void
1334gui_mch_new_colors(void)
1335{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001336 HBRUSH prevBrush;
1337
1338 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001339 prevBrush = (HBRUSH)SetClassLongPtr(
1340 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001341 InvalidateRect(s_hwnd, NULL, TRUE);
1342 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001343}
1344
1345/*
1346 * Set the colors to their default values.
1347 */
1348 void
1349gui_mch_def_colors(void)
1350{
1351 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1352 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1353 gui.def_norm_pixel = gui.norm_pixel;
1354 gui.def_back_pixel = gui.back_pixel;
1355}
1356
1357/*
1358 * Open the GUI window which was created by a call to gui_mch_init().
1359 */
1360 int
1361gui_mch_open(void)
1362{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001363 // Actually open the window, if not already visible
1364 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001365 if (!IsWindowVisible(s_hwnd))
1366 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1367
1368#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001369 // Init replace string here, so that we keep it when re-opening the
1370 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001371 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1372#endif
1373
1374 return OK;
1375}
1376
1377/*
1378 * Get the position of the top left corner of the window.
1379 */
1380 int
1381gui_mch_get_winpos(int *x, int *y)
1382{
1383 RECT rect;
1384
1385 GetWindowRect(s_hwnd, &rect);
1386 *x = rect.left;
1387 *y = rect.top;
1388 return OK;
1389}
1390
1391/*
1392 * Set the position of the top left corner of the window to the given
1393 * coordinates.
1394 */
1395 void
1396gui_mch_set_winpos(int x, int y)
1397{
1398 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1399 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1400}
1401 void
1402gui_mch_set_text_area_pos(int x, int y, int w, int h)
1403{
1404 static int oldx = 0;
1405 static int oldy = 0;
1406
1407 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1408
1409#ifdef FEAT_TOOLBAR
1410 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1411 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001412 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001413#endif
1414#if defined(FEAT_GUI_TABLINE)
1415 if (showing_tabline)
1416 {
1417 int top = 0;
1418 RECT rect;
1419
1420# ifdef FEAT_TOOLBAR
1421 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001422 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001423# endif
1424 GetClientRect(s_hwnd, &rect);
1425 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1426 }
1427#endif
1428
Bram Moolenaar734a8672019-12-02 22:49:38 +01001429 // When side scroll bar is unshown, the size of window will change.
1430 // then, the text area move left or right. thus client rect should be
1431 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001432 if (oldx != x || oldy != y)
1433 {
1434 InvalidateRect(s_hwnd, NULL, FALSE);
1435 oldx = x;
1436 oldy = y;
1437 }
1438}
1439
1440
1441/*
1442 * Scrollbar stuff:
1443 */
1444
1445 void
1446gui_mch_enable_scrollbar(
1447 scrollbar_T *sb,
1448 int flag)
1449{
1450 ShowScrollBar(sb->id, SB_CTL, flag);
1451
Bram Moolenaar734a8672019-12-02 22:49:38 +01001452 // TODO: When the window is maximized, the size of the window stays the
1453 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1454 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001455}
1456
1457 void
1458gui_mch_set_scrollbar_pos(
1459 scrollbar_T *sb,
1460 int x,
1461 int y,
1462 int w,
1463 int h)
1464{
1465 SetWindowPos(sb->id, NULL, x, y, w, h,
1466 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1467}
1468
Bram Moolenaar203ec772020-07-17 20:43:43 +02001469 int
1470gui_mch_get_scrollbar_xpadding(void)
1471{
1472 RECT rcTxt, rcWnd;
1473 int xpad;
1474
1475 GetWindowRect(s_textArea, &rcTxt);
1476 GetWindowRect(s_hwnd, &rcWnd);
1477 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001478 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1479 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001480 return (xpad < 0) ? 0 : xpad;
1481}
1482
1483 int
1484gui_mch_get_scrollbar_ypadding(void)
1485{
1486 RECT rcTxt, rcWnd;
1487 int ypad;
1488
1489 GetWindowRect(s_textArea, &rcTxt);
1490 GetWindowRect(s_hwnd, &rcWnd);
1491 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001492 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1493 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001494 return (ypad < 0) ? 0 : ypad;
1495}
1496
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001497 void
1498gui_mch_create_scrollbar(
1499 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001500 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001501{
1502 sb->id = CreateWindow(
1503 "SCROLLBAR", "Scrollbar",
1504 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001505 10, // Any value will do for now
1506 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001507 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001508 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001509}
1510
1511/*
1512 * Find the scrollbar with the given hwnd.
1513 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001514 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515gui_mswin_find_scrollbar(HWND hwnd)
1516{
1517 win_T *wp;
1518
1519 if (gui.bottom_sbar.id == hwnd)
1520 return &gui.bottom_sbar;
1521 FOR_ALL_WINDOWS(wp)
1522 {
1523 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1524 return &wp->w_scrollbars[SBAR_LEFT];
1525 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1526 return &wp->w_scrollbars[SBAR_RIGHT];
1527 }
1528 return NULL;
1529}
1530
K.Takatac81e9bf2022-01-16 14:15:49 +00001531 static void
1532update_scrollbar_size(void)
1533{
1534 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1535 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1536}
1537
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001538/*
K.Takataabe628e2022-01-23 16:25:17 +00001539 * Get the average character size of a font.
1540 */
1541 static void
1542GetAverageFontSize(HDC hdc, SIZE *size)
1543{
1544 // GetTextMetrics() may not return the right value in tmAveCharWidth
1545 // for some fonts. Do our own average computation.
1546 GetTextExtentPoint(hdc,
1547 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1548 52, size);
1549 size->cx = (size->cx / 26 + 1) / 2;
1550}
1551
1552/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001553 * Get the character size of a font.
1554 */
1555 static void
1556GetFontSize(GuiFont font)
1557{
1558 HWND hwnd = GetDesktopWindow();
1559 HDC hdc = GetWindowDC(hwnd);
1560 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001561 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001562 TEXTMETRIC tm;
1563
1564 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001565 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566
K.Takataabe628e2022-01-23 16:25:17 +00001567 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568 gui.char_height = tm.tmHeight + p_linespace;
1569
1570 SelectFont(hdc, hfntOld);
1571
1572 ReleaseDC(hwnd, hdc);
1573}
1574
1575/*
1576 * Adjust gui.char_height (after 'linespace' was changed).
1577 */
1578 int
1579gui_mch_adjust_charheight(void)
1580{
1581 GetFontSize(gui.norm_font);
1582 return OK;
1583}
1584
1585 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001586get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587{
1588 HFONT font = NULL;
1589
Bram Moolenaar734a8672019-12-02 22:49:38 +01001590 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001591 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592
1593 if (font == NULL)
1594 return NOFONT;
1595
1596 return (GuiFont)font;
1597}
1598
1599 static int
1600pixels_to_points(int pixels, int vertical)
1601{
1602 int points;
1603 HWND hwnd;
1604 HDC hdc;
1605
1606 hwnd = GetDesktopWindow();
1607 hdc = GetWindowDC(hwnd);
1608
1609 points = MulDiv(pixels, 72,
1610 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1611
1612 ReleaseDC(hwnd, hdc);
1613
1614 return points;
1615}
1616
1617 GuiFont
1618gui_mch_get_font(
1619 char_u *name,
1620 int giveErrorIfMissing)
1621{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001622 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001623 GuiFont font = NOFONT;
1624
1625 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001626 {
1627 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001628 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001629 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001631 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001632 return font;
1633}
1634
1635#if defined(FEAT_EVAL) || defined(PROTO)
1636/*
1637 * Return the name of font "font" in allocated memory.
1638 * Don't know how to get the actual name, thus use the provided name.
1639 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001641gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001642{
1643 if (name == NULL)
1644 return NULL;
1645 return vim_strsave(name);
1646}
1647#endif
1648
1649 void
1650gui_mch_free_font(GuiFont font)
1651{
1652 if (font)
1653 DeleteObject((HFONT)font);
1654}
1655
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656/*
1657 * Return the Pixel value (color) for the given color name.
1658 * Return INVALCOLOR for error.
1659 */
1660 guicolor_T
1661gui_mch_get_color(char_u *name)
1662{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001663 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001664
1665 typedef struct SysColorTable
1666 {
1667 char *name;
1668 int color;
1669 } SysColorTable;
1670
1671 static SysColorTable sys_table[] =
1672 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001673 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1674 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001675#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001676 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1677#endif
1678 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1679 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1680 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1681 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1682 {"SYS_DESKTOP", COLOR_DESKTOP},
1683 {"SYS_INFOBK", COLOR_INFOBK},
1684 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1685 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001686 {"SYS_BTNFACE", COLOR_BTNFACE},
1687 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1688 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1689 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1690 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1691 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1692 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1693 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1694 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1695 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1696 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1697 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1698 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1699 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1700 {"SYS_MENU", COLOR_MENU},
1701 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1702 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1703 {"SYS_WINDOW", COLOR_WINDOW},
1704 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1705 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1706 };
1707
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708 /*
1709 * Try to look up a system colour.
1710 */
K.Takataeeec2542021-06-02 13:28:16 +02001711 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001712 if (STRICMP(name, sys_table[i].name) == 0)
1713 return GetSysColor(sys_table[i].color);
1714
Bram Moolenaarab302212016-04-26 20:59:29 +02001715 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001717
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001718 guicolor_T
1719gui_mch_get_rgb_color(int r, int g, int b)
1720{
1721 return gui_get_rgb_color_cmn(r, g, b);
1722}
1723
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001724/*
1725 * Return OK if the key with the termcap name "name" is supported.
1726 */
1727 int
1728gui_mch_haskey(char_u *name)
1729{
1730 int i;
1731
1732 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001733 if (name[0] == special_keys[i].vim_code0
1734 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001735 return OK;
1736 return FAIL;
1737}
1738
1739 void
1740gui_mch_beep(void)
1741{
LemonBoy77771d32022-04-13 11:47:25 +01001742 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001743}
1744/*
1745 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1746 */
1747 void
1748gui_mch_invert_rectangle(
1749 int r,
1750 int c,
1751 int nr,
1752 int nc)
1753{
1754 RECT rc;
1755
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001756#if defined(FEAT_DIRECTX)
1757 if (IS_ENABLE_DIRECTX())
1758 DWriteContext_Flush(s_dwc);
1759#endif
1760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761 /*
1762 * Note: InvertRect() excludes right and bottom of rectangle.
1763 */
1764 rc.left = FILL_X(c);
1765 rc.top = FILL_Y(r);
1766 rc.right = rc.left + nc * gui.char_width;
1767 rc.bottom = rc.top + nr * gui.char_height;
1768 InvertRect(s_hdc, &rc);
1769}
1770
1771/*
1772 * Iconify the GUI window.
1773 */
1774 void
1775gui_mch_iconify(void)
1776{
1777 ShowWindow(s_hwnd, SW_MINIMIZE);
1778}
1779
1780/*
1781 * Draw a cursor without focus.
1782 */
1783 void
1784gui_mch_draw_hollow_cursor(guicolor_T color)
1785{
1786 HBRUSH hbr;
1787 RECT rc;
1788
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001789#if defined(FEAT_DIRECTX)
1790 if (IS_ENABLE_DIRECTX())
1791 DWriteContext_Flush(s_dwc);
1792#endif
1793
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001794 /*
1795 * Note: FrameRect() excludes right and bottom of rectangle.
1796 */
1797 rc.left = FILL_X(gui.col);
1798 rc.top = FILL_Y(gui.row);
1799 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800 if (mb_lefthalve(gui.row, gui.col))
1801 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001802 rc.bottom = rc.top + gui.char_height;
1803 hbr = CreateSolidBrush(color);
1804 FrameRect(s_hdc, &rc, hbr);
1805 DeleteBrush(hbr);
1806}
1807/*
1808 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1809 * color "color".
1810 */
1811 void
1812gui_mch_draw_part_cursor(
1813 int w,
1814 int h,
1815 guicolor_T color)
1816{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 RECT rc;
1818
1819 /*
1820 * Note: FillRect() excludes right and bottom of rectangle.
1821 */
1822 rc.left =
1823#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001824 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1826#endif
1827 FILL_X(gui.col);
1828 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1829 rc.right = rc.left + w;
1830 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001831
Bram Moolenaar92467d32017-12-05 13:22:16 +01001832 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001833}
1834
1835
1836/*
1837 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1838 * dead key's nominal character and re-post the original message.
1839 */
1840 static void
1841outputDeadKey_rePost(MSG originalMsg)
1842{
1843 static MSG deadCharExpel;
1844
1845 if (!dead_key)
1846 return;
1847
1848 dead_key = 0;
1849
Bram Moolenaar734a8672019-12-02 22:49:38 +01001850 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851 deadCharExpel.message = originalMsg.message;
1852 deadCharExpel.hwnd = originalMsg.hwnd;
1853 deadCharExpel.wParam = VK_SPACE;
1854
K.Takata4ac893f2022-01-20 12:44:28 +00001855 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001856
Bram Moolenaar734a8672019-12-02 22:49:38 +01001857 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1859 originalMsg.lParam);
1860}
1861
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862/*
1863 * Process a single Windows message.
1864 * If one is not available we hang until one is.
1865 */
1866 static void
1867process_message(void)
1868{
1869 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001870 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871 char_u string[40];
1872 int i;
1873 int modifiers = 0;
1874 int key;
1875#ifdef FEAT_MENU
1876 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1877#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001878 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001879
K.Takatab7057bd2022-01-21 11:37:07 +00001880 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001881
1882#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001883 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001884 if (msg.message == WM_OLE)
1885 {
1886 char_u *str = (char_u *)msg.lParam;
1887 if (str == NULL || *str == NUL)
1888 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001889 // Message can't be ours, forward it. Fixes problem with Ultramon
1890 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001891 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001892 }
1893 else
1894 {
1895 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001896 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001897 }
1898 return;
1899 }
1900#endif
1901
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001902#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001903 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001904 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001905 {
1906 HandleMouseHide(msg.message, msg.lParam);
1907 return;
1908 }
1909#endif
1910
1911 /*
1912 * Check if it's a special key that we recognise. If not, call
1913 * TranslateMessage().
1914 */
1915 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1916 {
1917 vk = (int) msg.wParam;
1918
1919 /*
1920 * Handle dead keys in special conditions in other cases we let Windows
1921 * handle them and do not interfere.
1922 *
1923 * The dead_key flag must be reset on several occasions:
1924 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1925 * consumed at that point (This is when we let Windows combine the
1926 * dead character on its own)
1927 *
1928 * - Before doing something special such as regenerating keypresses to
1929 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001930 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001931 * immediately to _OnChar() (or _OnSysChar()).
1932 */
1933 if (dead_key)
1934 {
1935 /*
1936 * If a dead key was pressed and the user presses VK_SPACE,
1937 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1938 * with the dead char now, so do nothing special and let Windows
1939 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01001940 *
1941 * Note that VK_SPACE combines with the dead_key's character and
1942 * only one WM_CHAR will be generated by TranslateMessage(), in
1943 * the two other cases two WM_CHAR will be generated: the dead
1944 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1945 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001946 */
1947 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1948 {
1949 dead_key = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001950 TranslateMessage(&msg);
1951 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001952 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001953 // In modes where we are not typing, dead keys should behave
1954 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001955 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1956 {
1957 outputDeadKey_rePost(msg);
1958 return;
1959 }
1960 }
1961
Bram Moolenaar734a8672019-12-02 22:49:38 +01001962 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001963 if (vk == VK_CANCEL)
1964 {
1965 trash_input_buf();
1966 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001967 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001968 string[0] = Ctrl_C;
1969 add_to_input_buf(string, 1);
1970 }
1971
LemonBoy77fc0b02022-04-22 22:45:52 +01001972 // This is an IME event or a synthetic keystroke, let Windows handle it.
1973 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1974 {
1975 TranslateMessage(&msg);
1976 return;
1977 }
1978
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001979 for (i = 0; special_keys[i].key_sym != 0; i++)
1980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001981 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001982 if (special_keys[i].key_sym == vk
LemonBoy202b4bd2022-04-28 19:50:54 +01001983 && (vk != VK_SPACE || !(GetKeyState(VK_LMENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001984 {
1985 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001986 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001987 * is a key that would normally trigger the dead key nominal
1988 * character output (such as a NUMPAD printable character or
1989 * the TAB key, etc...).
1990 */
1991 if (dead_key && (special_keys[i].vim_code0 == 'K'
1992 || vk == VK_TAB || vk == CAR))
1993 {
1994 outputDeadKey_rePost(msg);
1995 return;
1996 }
1997
1998#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001999 // Check for <F10>: Windows selects the menu. When <F10> is
2000 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002001 if (vk == VK_F10
2002 && gui.menu_is_active
2003 && check_map(k10, State, FALSE, TRUE, FALSE,
2004 NULL, NULL) == NULL)
2005 break;
2006#endif
LemonBoy45684c62022-04-24 15:46:42 +01002007 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002008
2009 if (special_keys[i].vim_code1 == NUL)
2010 key = special_keys[i].vim_code0;
2011 else
2012 key = TO_SPECIAL(special_keys[i].vim_code0,
2013 special_keys[i].vim_code1);
2014 key = simplify_key(key, &modifiers);
2015 if (key == CSI)
2016 key = K_CSI;
2017
2018 if (modifiers)
2019 {
2020 string[0] = CSI;
2021 string[1] = KS_MODIFIER;
2022 string[2] = modifiers;
2023 add_to_input_buf(string, 3);
2024 }
2025
2026 if (IS_SPECIAL(key))
2027 {
2028 string[0] = CSI;
2029 string[1] = K_SECOND(key);
2030 string[2] = K_THIRD(key);
2031 add_to_input_buf(string, 3);
2032 }
2033 else
2034 {
2035 int len;
2036
Bram Moolenaar734a8672019-12-02 22:49:38 +01002037 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002038 len = char_to_string(key, string, 40, FALSE);
2039 add_to_input_buf(string, len);
2040 }
2041 break;
2042 }
2043 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002044
2045 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002046 if (special_keys[i].key_sym == 0)
2047 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002048 WCHAR ch[8];
2049 int len;
2050 int i;
2051 UINT scan_code;
2052
LemonBoy77fc0b02022-04-22 22:45:52 +01002053 // Construct the state table with only a few modifiers, we don't
2054 // really care about the presence of Ctrl/Alt as those modifiers are
2055 // handled by Vim separately.
2056 memset(keyboard_state, 0, 256);
2057 if (GetKeyState(VK_SHIFT) & 0x8000)
2058 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002059 if (GetKeyState(VK_CAPITAL) & 0x0001)
2060 keyboard_state[VK_CAPITAL] = 0x01;
LemonBoy45684c62022-04-24 15:46:42 +01002061 // Alt-Gr is synthesized as Alt + Ctrl.
LemonBoy202b4bd2022-04-28 19:50:54 +01002062 if ((GetKeyState(VK_RMENU) & 0x8000)
2063 && (GetKeyState(VK_CONTROL) & 0x8000))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002064 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002065 keyboard_state[VK_MENU] = 0x80;
2066 keyboard_state[VK_CONTROL] = 0x80;
2067 }
2068
2069 // Translate the virtual key according to the current keyboard
2070 // layout.
2071 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2072 // Convert the scan-code into a sequence of zero or more unicode
2073 // codepoints.
2074 // If this is a dead key ToUnicode returns a negative value.
2075 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2076 0);
2077 dead_key = len < 0;
2078
2079 if (len <= 0)
2080 return;
2081
2082 // Post the message as TranslateMessage would do.
2083 if (msg.message == WM_KEYDOWN)
2084 {
2085 for (i = 0; i < len; i++)
2086 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 }
2088 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002089 {
2090 for (i = 0; i < len; i++)
2091 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2092 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 }
2094 }
2095#ifdef FEAT_MBYTE_IME
2096 else if (msg.message == WM_IME_NOTIFY)
2097 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2098 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002099 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002100 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002101#endif
2102
2103#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002104 // Check for <F10>: Default effect is to select the menu. When <F10> is
2105 // mapped we need to stop it here to avoid strange effects (e.g., for the
2106 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002107 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2108 NULL, NULL) == NULL)
2109#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002110 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002111}
2112
2113/*
2114 * Catch up with any queued events. This may put keyboard input into the
2115 * input buffer, call resize call-backs, trigger timers etc. If there is
2116 * nothing in the event queue (& no timers pending), then we return
2117 * immediately.
2118 */
2119 void
2120gui_mch_update(void)
2121{
2122 MSG msg;
2123
2124 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002125 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002126 && !vim_is_input_buf_full())
2127 process_message();
2128}
2129
Bram Moolenaar4231da42016-06-02 14:30:04 +02002130 static void
2131remove_any_timer(void)
2132{
2133 MSG msg;
2134
2135 if (s_wait_timer != 0 && !s_timed_out)
2136 {
2137 KillTimer(NULL, s_wait_timer);
2138
Bram Moolenaar734a8672019-12-02 22:49:38 +01002139 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002140 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002141 ;
2142 s_wait_timer = 0;
2143 }
2144}
2145
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002146/*
2147 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2148 * from the keyboard.
2149 * wtime == -1 Wait forever.
2150 * wtime == 0 This should never happen.
2151 * wtime > 0 Wait wtime milliseconds for a character.
2152 * Returns OK if a character was found to be available within the given time,
2153 * or FAIL otherwise.
2154 */
2155 int
2156gui_mch_wait_for_chars(int wtime)
2157{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002158 int focus;
2159
2160 s_timed_out = FALSE;
2161
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002162 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002163 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002164 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002165 if (s_busy_processing)
2166 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002167
2168 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002169 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2170 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002171 }
2172
2173 allow_scrollbar = TRUE;
2174
2175 focus = gui.in_focus;
2176 while (!s_timed_out)
2177 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002178 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002179 if (gui.in_focus != focus)
2180 {
2181 if (gui.in_focus)
2182 gui_mch_start_blink();
2183 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002184 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185 focus = gui.in_focus;
2186 }
2187
2188 if (s_need_activate)
2189 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002190 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002191 s_need_activate = FALSE;
2192 }
2193
Bram Moolenaar4231da42016-06-02 14:30:04 +02002194#ifdef FEAT_TIMERS
2195 did_add_timer = FALSE;
2196#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002197#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002198 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002199 for (;;)
2200 {
2201 MSG msg;
2202
2203 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002204# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002205 if (did_add_timer)
2206 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002207# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002208 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002209 {
2210 process_message();
2211 break;
2212 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002213 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002214 // TODO: The 10 msec is a compromise between laggy response
2215 // and consuming more CPU time. Better would be to handle
2216 // channel messages when they arrive.
2217 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002218 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002219 break;
2220 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002221#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002222 // Don't use gui_mch_update() because then we will spin-lock until a
2223 // char arrives, instead we use GetMessage() to hang until an
2224 // event arrives. No need to check for input_buf_full because we are
2225 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002226 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002227#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002228
2229 if (input_available())
2230 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002231 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002232 allow_scrollbar = FALSE;
2233
Bram Moolenaar89c00032019-09-04 13:53:21 +02002234 // Clear pending mouse button, the release event may have been
2235 // taken by the dialog window. But don't do this when getting
2236 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002237 if (!s_getting_focus)
2238 s_button_pending = -1;
2239
2240 return OK;
2241 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002242
2243#ifdef FEAT_TIMERS
2244 if (did_add_timer)
2245 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002246 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002247 remove_any_timer();
2248 break;
2249 }
2250#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002251 }
2252 allow_scrollbar = FALSE;
2253 return FAIL;
2254}
2255
2256/*
2257 * Clear a rectangular region of the screen from text pos (row1, col1) to
2258 * (row2, col2) inclusive.
2259 */
2260 void
2261gui_mch_clear_block(
2262 int row1,
2263 int col1,
2264 int row2,
2265 int col2)
2266{
2267 RECT rc;
2268
2269 /*
2270 * Clear one extra pixel at the far right, for when bold characters have
2271 * spilled over to the window border.
2272 * Note: FillRect() excludes right and bottom of rectangle.
2273 */
2274 rc.left = FILL_X(col1);
2275 rc.top = FILL_Y(row1);
2276 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2277 rc.bottom = FILL_Y(row2 + 1);
2278 clear_rect(&rc);
2279}
2280
2281/*
2282 * Clear the whole text window.
2283 */
2284 void
2285gui_mch_clear_all(void)
2286{
2287 RECT rc;
2288
2289 rc.left = 0;
2290 rc.top = 0;
2291 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2292 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2293 clear_rect(&rc);
2294}
2295/*
2296 * Menu stuff.
2297 */
2298
2299 void
2300gui_mch_enable_menu(int flag)
2301{
2302#ifdef FEAT_MENU
2303 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2304#endif
2305}
2306
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307 void
2308gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002309 int x UNUSED,
2310 int y UNUSED,
2311 int w UNUSED,
2312 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002313{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002314 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002315}
2316
2317#if defined(FEAT_MENU) || defined(PROTO)
2318/*
2319 * Make menu item hidden or not hidden
2320 */
2321 void
2322gui_mch_menu_hidden(
2323 vimmenu_T *menu,
2324 int hidden)
2325{
2326 /*
2327 * This doesn't do what we want. Hmm, just grey the menu items for now.
2328 */
2329 /*
2330 if (hidden)
2331 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2332 else
2333 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2334 */
2335 gui_mch_menu_grey(menu, hidden);
2336}
2337
2338/*
2339 * This is called after setting all the menus to grey/hidden or not.
2340 */
2341 void
2342gui_mch_draw_menubar(void)
2343{
2344 DrawMenuBar(s_hwnd);
2345}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002346#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002347
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002348/*
2349 * Return the RGB value of a pixel as a long.
2350 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002351 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352gui_mch_get_rgb(guicolor_T pixel)
2353{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002354 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2355 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002356}
2357
2358#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002359/*
2360 * Convert pixels in X to dialog units
2361 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362 static WORD
2363PixelToDialogX(int numPixels)
2364{
2365 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2366}
2367
Bram Moolenaar734a8672019-12-02 22:49:38 +01002368/*
2369 * Convert pixels in Y to dialog units
2370 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371 static WORD
2372PixelToDialogY(int numPixels)
2373{
2374 return (WORD)((numPixels * 8) / s_dlgfntheight);
2375}
2376
Bram Moolenaar734a8672019-12-02 22:49:38 +01002377/*
2378 * Return the width in pixels of the given text in the given DC.
2379 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002380 static int
2381GetTextWidth(HDC hdc, char_u *str, int len)
2382{
2383 SIZE size;
2384
2385 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2386 return size.cx;
2387}
2388
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002389/*
2390 * Return the width in pixels of the given text in the given DC, taking care
2391 * of 'encoding' to active codepage conversion.
2392 */
2393 static int
2394GetTextWidthEnc(HDC hdc, char_u *str, int len)
2395{
2396 SIZE size;
2397 WCHAR *wstr;
2398 int n;
2399 int wlen = len;
2400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002401 wstr = enc_to_utf16(str, &wlen);
2402 if (wstr == NULL)
2403 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002405 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2406 vim_free(wstr);
2407 if (n)
2408 return size.cx;
2409 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002410}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002411
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002412static void get_work_area(RECT *spi_rect);
2413
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414/*
2415 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002416 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2417 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002418 */
2419 static BOOL
2420CenterWindow(
2421 HWND hwndChild,
2422 HWND hwndParent)
2423{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002424 HMONITOR mon;
2425 MONITORINFO moninfo;
2426 RECT rChild, rParent, rScreen;
2427 int wChild, hChild, wParent, hParent;
2428 int xNew, yNew;
2429 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002430
2431 GetWindowRect(hwndChild, &rChild);
2432 wChild = rChild.right - rChild.left;
2433 hChild = rChild.bottom - rChild.top;
2434
Bram Moolenaar734a8672019-12-02 22:49:38 +01002435 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002437 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438 else
2439 GetWindowRect(hwndParent, &rParent);
2440 wParent = rParent.right - rParent.left;
2441 hParent = rParent.bottom - rParent.top;
2442
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002443 moninfo.cbSize = sizeof(MONITORINFO);
2444 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2445 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002446 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002447 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002448 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002449 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002451 hdc = GetDC(hwndChild);
2452 rScreen.left = 0;
2453 rScreen.top = 0;
2454 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2455 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2456 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002457 }
2458
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002459 xNew = rParent.left + ((wParent - wChild) / 2);
2460 if (xNew < rScreen.left)
2461 xNew = rScreen.left;
2462 else if ((xNew + wChild) > rScreen.right)
2463 xNew = rScreen.right - wChild;
2464
2465 yNew = rParent.top + ((hParent - hChild) / 2);
2466 if (yNew < rScreen.top)
2467 yNew = rScreen.top;
2468 else if ((yNew + hChild) > rScreen.bottom)
2469 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002470
2471 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2472 SWP_NOSIZE | SWP_NOZORDER);
2473}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002474#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002476#if defined(FEAT_TOOLBAR) || defined(PROTO)
2477 void
2478gui_mch_show_toolbar(int showit)
2479{
2480 if (s_toolbarhwnd == NULL)
2481 return;
2482
2483 if (showit)
2484 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002485 // Enable unicode support
2486 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2487 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488 ShowWindow(s_toolbarhwnd, SW_SHOW);
2489 }
2490 else
2491 ShowWindow(s_toolbarhwnd, SW_HIDE);
2492}
2493
Bram Moolenaar734a8672019-12-02 22:49:38 +01002494// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002495# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496
2497#endif
2498
2499#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2500 static void
2501add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2502{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002503 WCHAR *wn;
2504 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002506 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002507 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002508 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002510 infow.cbSize = sizeof(infow);
2511 infow.fMask = MIIM_TYPE | MIIM_ID;
2512 infow.wID = item_id;
2513 infow.fType = MFT_STRING;
2514 infow.dwTypeData = wn;
2515 infow.cch = (UINT)wcslen(wn);
2516 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2517 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518}
2519
2520 static void
2521show_tabline_popup_menu(void)
2522{
2523 HMENU tab_pmenu;
2524 long rval;
2525 POINT pt;
2526
Bram Moolenaar734a8672019-12-02 22:49:38 +01002527 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002528 if (hold_gui_events
2529# ifdef FEAT_CMDWIN
2530 || cmdwin_type != 0
2531# endif
2532 )
2533 return;
2534
2535 tab_pmenu = CreatePopupMenu();
2536 if (tab_pmenu == NULL)
2537 return;
2538
2539 if (first_tabpage->tp_next != NULL)
2540 add_tabline_popup_menu_entry(tab_pmenu,
2541 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2542 add_tabline_popup_menu_entry(tab_pmenu,
2543 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2544 add_tabline_popup_menu_entry(tab_pmenu,
2545 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2546
2547 GetCursorPos(&pt);
2548 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2549 NULL);
2550
2551 DestroyMenu(tab_pmenu);
2552
Bram Moolenaar734a8672019-12-02 22:49:38 +01002553 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 if (rval > 0)
2555 {
2556 TCHITTESTINFO htinfo;
2557 int idx;
2558
2559 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2560 return;
2561
2562 htinfo.pt.x = pt.x;
2563 htinfo.pt.y = pt.y;
2564 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2565 if (idx == -1)
2566 idx = 0;
2567 else
2568 idx += 1;
2569
2570 send_tabline_menu_event(idx, (int)rval);
2571 }
2572}
2573
2574/*
2575 * Show or hide the tabline.
2576 */
2577 void
2578gui_mch_show_tabline(int showit)
2579{
2580 if (s_tabhwnd == NULL)
2581 return;
2582
2583 if (!showit != !showing_tabline)
2584 {
2585 if (showit)
2586 ShowWindow(s_tabhwnd, SW_SHOW);
2587 else
2588 ShowWindow(s_tabhwnd, SW_HIDE);
2589 showing_tabline = showit;
2590 }
2591}
2592
2593/*
2594 * Return TRUE when tabline is displayed.
2595 */
2596 int
2597gui_mch_showing_tabline(void)
2598{
2599 return s_tabhwnd != NULL && showing_tabline;
2600}
2601
2602/*
2603 * Update the labels of the tabline.
2604 */
2605 void
2606gui_mch_update_tabline(void)
2607{
2608 tabpage_T *tp;
2609 TCITEM tie;
2610 int nr = 0;
2611 int curtabidx = 0;
2612 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614
2615 if (s_tabhwnd == NULL)
2616 return;
2617
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002618 // Enable unicode support
2619 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620
2621 tie.mask = TCIF_TEXT;
2622 tie.iImage = -1;
2623
Bram Moolenaar734a8672019-12-02 22:49:38 +01002624 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2626
Bram Moolenaar734a8672019-12-02 22:49:38 +01002627 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2629 {
2630 if (tp == curtab)
2631 curtabidx = nr;
2632
2633 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2634 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002635 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002636 tie.pszText = "-Empty-";
2637 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2638 tabadded = 1;
2639 }
2640
2641 get_tabline_label(tp, FALSE);
2642 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002643
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002644 wstr = enc_to_utf16(NameBuff, NULL);
2645 if (wstr != NULL)
2646 {
2647 TCITEMW tiw;
2648
2649 tiw.mask = TCIF_TEXT;
2650 tiw.iImage = -1;
2651 tiw.pszText = wstr;
2652 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2653 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002654 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002655 }
2656
Bram Moolenaar734a8672019-12-02 22:49:38 +01002657 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002658 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2659 TabCtrl_DeleteItem(s_tabhwnd, nr);
2660
2661 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2662 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2663
Bram Moolenaar734a8672019-12-02 22:49:38 +01002664 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002665 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2666 RedrawWindow(s_tabhwnd, NULL, NULL,
2667 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2668
2669 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2670 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2671}
2672
2673/*
2674 * Set the current tab to "nr". First tab is 1.
2675 */
2676 void
2677gui_mch_set_curtab(int nr)
2678{
2679 if (s_tabhwnd == NULL)
2680 return;
2681
2682 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2683 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2684}
2685
2686#endif
2687
2688/*
2689 * ":simalt" command.
2690 */
2691 void
2692ex_simalt(exarg_T *eap)
2693{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002694 char_u *keys = eap->arg;
2695 int fill_typebuf = FALSE;
2696 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697
2698 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2699 while (*keys)
2700 {
2701 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002702 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002703 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2704 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002705 fill_typebuf = TRUE;
2706 }
2707 if (fill_typebuf)
2708 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002709 // Put a NOP in the typeahead buffer so that the message will get
2710 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002711 key_name[0] = K_SPECIAL;
2712 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002713 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002714 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002715#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002716 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002717#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002718 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002719 }
2720}
2721
2722/*
2723 * Create the find & replace dialogs.
2724 * You can't have both at once: ":find" when replace is showing, destroys
2725 * the replace dialog first, and the other way around.
2726 */
2727#ifdef MSWIN_FIND_REPLACE
2728 static void
2729initialise_findrep(char_u *initial_string)
2730{
2731 int wword = FALSE;
2732 int mcase = !p_ic;
2733 char_u *entry_text;
2734
Bram Moolenaar734a8672019-12-02 22:49:38 +01002735 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002736 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2737
2738 s_findrep_struct.hwndOwner = s_hwnd;
2739 s_findrep_struct.Flags = FR_DOWN;
2740 if (mcase)
2741 s_findrep_struct.Flags |= FR_MATCHCASE;
2742 if (wword)
2743 s_findrep_struct.Flags |= FR_WHOLEWORD;
2744 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002745 {
2746 WCHAR *p = enc_to_utf16(entry_text, NULL);
2747 if (p != NULL)
2748 {
2749 int len = s_findrep_struct.wFindWhatLen - 1;
2750
2751 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2752 s_findrep_struct.lpstrFindWhat[len] = NUL;
2753 vim_free(p);
2754 }
2755 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002756 vim_free(entry_text);
2757}
2758#endif
2759
2760 static void
2761set_window_title(HWND hwnd, char *title)
2762{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002763 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002764 {
2765 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002766
Bram Moolenaar734a8672019-12-02 22:49:38 +01002767 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002768 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2769 if (wbuf != NULL)
2770 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002771 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002772 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 }
2774 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002775 else
2776 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777}
2778
2779 void
2780gui_mch_find_dialog(exarg_T *eap)
2781{
2782#ifdef MSWIN_FIND_REPLACE
2783 if (s_findrep_msg != 0)
2784 {
2785 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2786 DestroyWindow(s_findrep_hwnd);
2787
2788 if (!IsWindow(s_findrep_hwnd))
2789 {
2790 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002791 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002792 }
2793
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002794 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002795 (void)SetFocus(s_findrep_hwnd);
2796
2797 s_findrep_is_find = TRUE;
2798 }
2799#endif
2800}
2801
2802
2803 void
2804gui_mch_replace_dialog(exarg_T *eap)
2805{
2806#ifdef MSWIN_FIND_REPLACE
2807 if (s_findrep_msg != 0)
2808 {
2809 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2810 DestroyWindow(s_findrep_hwnd);
2811
2812 if (!IsWindow(s_findrep_hwnd))
2813 {
2814 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002815 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816 }
2817
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002818 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002819 (void)SetFocus(s_findrep_hwnd);
2820
2821 s_findrep_is_find = FALSE;
2822 }
2823#endif
2824}
2825
2826
2827/*
2828 * Set visibility of the pointer.
2829 */
2830 void
2831gui_mch_mousehide(int hide)
2832{
2833 if (hide != gui.pointer_hidden)
2834 {
2835 ShowCursor(!hide);
2836 gui.pointer_hidden = hide;
2837 }
2838}
2839
2840#ifdef FEAT_MENU
2841 static void
2842gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2843{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002844 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002845 gui_mch_mousehide(FALSE);
2846
2847 (void)TrackPopupMenu(
2848 (HMENU)menu->submenu_id,
2849 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2850 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002851 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 s_hwnd,
2853 NULL);
2854 /*
2855 * NOTE: The pop-up menu can eat the mouse up event.
2856 * We deal with this in normal.c.
2857 */
2858}
2859#endif
2860
2861/*
2862 * Got a message when the system will go down.
2863 */
2864 static void
2865_OnEndSession(void)
2866{
2867 getout_preserve_modified(1);
2868}
2869
2870/*
2871 * Get this message when the user clicks on the cross in the top right corner
2872 * of a Windows95 window.
2873 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002874 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002875_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002876{
2877 gui_shell_closed();
2878}
2879
2880/*
2881 * Get a message when the window is being destroyed.
2882 */
2883 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002884_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885{
2886 if (!destroying)
2887 _OnClose(hwnd);
2888}
2889
2890 static void
2891_OnPaint(
2892 HWND hwnd)
2893{
2894 if (!IsMinimized(hwnd))
2895 {
2896 PAINTSTRUCT ps;
2897
Bram Moolenaar734a8672019-12-02 22:49:38 +01002898 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002899 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900
Bram Moolenaar734a8672019-12-02 22:49:38 +01002901 // prevent multi-byte characters from misprinting on an invalid
2902 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 if (has_mbyte)
2904 {
2905 RECT rect;
2906
2907 GetClientRect(hwnd, &rect);
2908 ps.rcPaint.left = rect.left;
2909 ps.rcPaint.right = rect.right;
2910 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002911
2912 if (!IsRectEmpty(&ps.rcPaint))
2913 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002914 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2915 ps.rcPaint.right - ps.rcPaint.left + 1,
2916 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2917 }
2918
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919 EndPaint(hwnd, &ps);
2920 }
2921}
2922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 static void
2924_OnSize(
2925 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002926 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002927 int cx,
2928 int cy)
2929{
K.Takatac81e9bf2022-01-16 14:15:49 +00002930 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931 {
2932 gui_resize_shell(cx, cy);
2933
Bram Moolenaar734a8672019-12-02 22:49:38 +01002934 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002935 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936 }
2937}
2938
2939 static void
2940_OnSetFocus(
2941 HWND hwnd,
2942 HWND hwndOldFocus)
2943{
2944 gui_focus_change(TRUE);
2945 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002946 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002947}
2948
2949 static void
2950_OnKillFocus(
2951 HWND hwnd,
2952 HWND hwndNewFocus)
2953{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002954 if (destroying)
2955 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002956 gui_focus_change(FALSE);
2957 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002958 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002959}
2960
2961/*
2962 * Get a message when the user switches back to vim
2963 */
2964 static LRESULT
2965_OnActivateApp(
2966 HWND hwnd,
2967 BOOL fActivate,
2968 DWORD dwThreadId)
2969{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002970 // we call gui_focus_change() in _OnSetFocus()
2971 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002972 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002973}
2974
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002975 void
2976gui_mch_destroy_scrollbar(scrollbar_T *sb)
2977{
2978 DestroyWindow(sb->id);
2979}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980
2981/*
2982 * Get current mouse coordinates in text window.
2983 */
2984 void
2985gui_mch_getmouse(int *x, int *y)
2986{
2987 RECT rct;
2988 POINT mp;
2989
2990 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002991 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002992 *x = (int)(mp.x - rct.left);
2993 *y = (int)(mp.y - rct.top);
2994}
2995
2996/*
2997 * Move mouse pointer to character at (x, y).
2998 */
2999 void
3000gui_mch_setmouse(int x, int y)
3001{
3002 RECT rct;
3003
3004 (void)GetWindowRect(s_textArea, &rct);
3005 (void)SetCursorPos(x + gui.border_offset + rct.left,
3006 y + gui.border_offset + rct.top);
3007}
3008
3009 static void
3010gui_mswin_get_valid_dimensions(
3011 int w,
3012 int h,
3013 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003014 int *valid_h,
3015 int *cols,
3016 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003017{
3018 int base_width, base_height;
3019
3020 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003021 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3022 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003023 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003024 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3025 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3026 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3027 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003028 *cols = (w - base_width) / gui.char_width;
3029 *rows = (h - base_height) / gui.char_height;
3030 *valid_w = base_width + *cols * gui.char_width;
3031 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032}
3033
3034 void
3035gui_mch_flash(int msec)
3036{
3037 RECT rc;
3038
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003039#if defined(FEAT_DIRECTX)
3040 if (IS_ENABLE_DIRECTX())
3041 DWriteContext_Flush(s_dwc);
3042#endif
3043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 /*
3045 * Note: InvertRect() excludes right and bottom of rectangle.
3046 */
3047 rc.left = 0;
3048 rc.top = 0;
3049 rc.right = gui.num_cols * gui.char_width;
3050 rc.bottom = gui.num_rows * gui.char_height;
3051 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003052 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003053
Bram Moolenaar734a8672019-12-02 22:49:38 +01003054 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055
3056 InvertRect(s_hdc, &rc);
3057}
3058
3059/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003060 * Check if the specified point is on-screen. (multi-monitor aware)
3061 */
3062 static BOOL
3063is_point_onscreen(int x, int y)
3064{
3065 POINT pt = {x, y};
3066
3067 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3068}
3069
3070/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003071 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003072 *
3073 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3074 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3075 * only works when the whole window is on-screen.
3076 */
3077 static BOOL
3078is_window_onscreen(HWND hwnd)
3079{
3080 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003081 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003082
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003083 GetClientRect(hwnd, &rc);
3084 p1.x = rc.left;
3085 p1.y = rc.top;
3086 p2.x = rc.right - 1;
3087 p2.y = rc.bottom - 1;
3088 ClientToScreen(hwnd, &p1);
3089 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003090
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003091 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003092 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003093 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003094 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003095 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003096 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003097 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003098 return FALSE;
3099 return TRUE;
3100}
3101
3102/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003103 * Return flags used for scrolling.
3104 * The SW_INVALIDATE is required when part of the window is covered or
3105 * off-screen. Refer to MS KB Q75236.
3106 */
3107 static int
3108get_scroll_flags(void)
3109{
3110 HWND hwnd;
3111 RECT rcVim, rcOther, rcDest;
3112
Bram Moolenaar185577e2020-10-29 20:08:21 +01003113 // Check if the window is (partly) off-screen.
3114 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003115 return SW_INVALIDATE;
3116
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003117 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003118 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3120 if (IsWindowVisible(hwnd))
3121 {
3122 GetWindowRect(hwnd, &rcOther);
3123 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3124 return SW_INVALIDATE;
3125 }
3126 return 0;
3127}
3128
3129/*
3130 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3131 * may not be scrolled out properly.
3132 * For gVim, when _OnScroll() is repeated, the character at the
3133 * previous cursor position may be left drawn after scroll.
3134 * The problem can be avoided by calling GetPixel() to get a pixel in
3135 * the region before ScrollWindowEx().
3136 */
3137 static void
3138intel_gpu_workaround(void)
3139{
3140 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3141}
3142
3143/*
3144 * Delete the given number of lines from the given row, scrolling up any
3145 * text further down within the scroll region.
3146 */
3147 void
3148gui_mch_delete_lines(
3149 int row,
3150 int num_lines)
3151{
3152 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003153
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003154 rc.left = FILL_X(gui.scroll_region_left);
3155 rc.right = FILL_X(gui.scroll_region_right + 1);
3156 rc.top = FILL_Y(row);
3157 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3158
Bram Moolenaar92467d32017-12-05 13:22:16 +01003159#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003160 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003161 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003162 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003164 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003165#endif
3166 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003167#if defined(FEAT_DIRECTX)
3168 if (IS_ENABLE_DIRECTX())
3169 DWriteContext_Flush(s_dwc);
3170#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003171 intel_gpu_workaround();
3172 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003173 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003174 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003175 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003176
Bram Moolenaar734a8672019-12-02 22:49:38 +01003177 // This seems to be required to avoid the cursor disappearing when
3178 // scrolling such that the cursor ends up in the top-left character on
3179 // the screen... But why? (Webb)
3180 // It's probably fixed by disabling drawing the cursor while scrolling.
3181 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003182
3183 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3184 gui.scroll_region_left,
3185 gui.scroll_region_bot, gui.scroll_region_right);
3186}
3187
3188/*
3189 * Insert the given number of lines before the given row, scrolling down any
3190 * following text within the scroll region.
3191 */
3192 void
3193gui_mch_insert_lines(
3194 int row,
3195 int num_lines)
3196{
3197 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003198
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 rc.left = FILL_X(gui.scroll_region_left);
3200 rc.right = FILL_X(gui.scroll_region_right + 1);
3201 rc.top = FILL_Y(row);
3202 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003203
3204#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003205 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003206 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003207 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003209 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003210#endif
3211 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003212#if defined(FEAT_DIRECTX)
3213 if (IS_ENABLE_DIRECTX())
3214 DWriteContext_Flush(s_dwc);
3215#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003216 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003217 // The SW_INVALIDATE is required when part of the window is covered or
3218 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003219 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003221 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003222 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003223
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 gui_clear_block(row, gui.scroll_region_left,
3225 row + num_lines - 1, gui.scroll_region_right);
3226}
3227
3228
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003230gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231{
3232#if defined(FEAT_DIRECTX)
3233 DWriteContext_Close(s_dwc);
3234 DWrite_Final();
3235 s_dwc = NULL;
3236#endif
3237
3238 ReleaseDC(s_textArea, s_hdc);
3239 DeleteObject(s_brush);
3240
3241#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003242 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003243 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3244#endif
3245
Bram Moolenaar734a8672019-12-02 22:49:38 +01003246 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003247 if (s_hwnd != NULL)
3248 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003249 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003250 DestroyWindow(s_hwnd);
3251 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252}
3253
3254 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003255logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256{
3257 char *p;
3258 char *res;
3259 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003260 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003261 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003262 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003263
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003264 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3265 if (font_name == NULL)
3266 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003268 quality_name = quality_id2name((int)lf.lfQuality);
3269
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003270 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003271 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003272 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003273 if (res != NULL)
3274 {
3275 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003276 // make a normal font string out of the lf thing:
3277 points = pixels_to_points(
3278 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3279 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3280 sprintf((char *)p, "%s:h%d", font_name, points);
3281 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003282 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003283 while (*p)
3284 {
3285 if (*p == ' ')
3286 *p = '_';
3287 ++p;
3288 }
3289 if (lf.lfItalic)
3290 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003291 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003292 STRCAT(p, ":b");
3293 if (lf.lfUnderline)
3294 STRCAT(p, ":u");
3295 if (lf.lfStrikeOut)
3296 STRCAT(p, ":s");
3297 if (charset_name != NULL)
3298 {
3299 STRCAT(p, ":c");
3300 STRCAT(p, charset_name);
3301 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003302 if (quality_name != NULL)
3303 {
3304 STRCAT(p, ":q");
3305 STRCAT(p, quality_name);
3306 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307 }
3308
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003309 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310 return (char_u *)res;
3311}
3312
3313
3314#ifdef FEAT_MBYTE_IME
3315/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003316 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003317 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003318 */
3319 static void
3320update_im_font(void)
3321{
K.Takatac81e9bf2022-01-16 14:15:49 +00003322 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003323
3324 if (p_guifontwide != NULL && *p_guifontwide != NUL
3325 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003326 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327 norm_logfont = lf_wide;
3328 else
3329 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003330
3331 lf = norm_logfont;
3332 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3333 // Work around when PerMonitorV2 is not enabled in the process level.
3334 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3335 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336}
3337#endif
3338
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339/*
3340 * Handler of gui.wide_font (p_guifontwide) changed notification.
3341 */
3342 void
3343gui_mch_wide_font_changed(void)
3344{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003345 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003347#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003348 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003349#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350
3351 gui_mch_free_font(gui.wide_ital_font);
3352 gui.wide_ital_font = NOFONT;
3353 gui_mch_free_font(gui.wide_bold_font);
3354 gui.wide_bold_font = NOFONT;
3355 gui_mch_free_font(gui.wide_boldital_font);
3356 gui.wide_boldital_font = NOFONT;
3357
3358 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003359 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003360 {
3361 if (!lf.lfItalic)
3362 {
3363 lf.lfItalic = TRUE;
3364 gui.wide_ital_font = get_font_handle(&lf);
3365 lf.lfItalic = FALSE;
3366 }
3367 if (lf.lfWeight < FW_BOLD)
3368 {
3369 lf.lfWeight = FW_BOLD;
3370 gui.wide_bold_font = get_font_handle(&lf);
3371 if (!lf.lfItalic)
3372 {
3373 lf.lfItalic = TRUE;
3374 gui.wide_boldital_font = get_font_handle(&lf);
3375 }
3376 }
3377 }
3378}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379
3380/*
3381 * Initialise vim to use the font with the given name.
3382 * Return FAIL if the font could not be loaded, OK otherwise.
3383 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003385gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003386{
K.Takatac81e9bf2022-01-16 14:15:49 +00003387 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 GuiFont font = NOFONT;
3389 char_u *p;
3390
Bram Moolenaar734a8672019-12-02 22:49:38 +01003391 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003393 {
3394 lfOrig = lf;
3395 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003397 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398 if (font == NOFONT)
3399 return FAIL;
3400
3401 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003402 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003403#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404 norm_logfont = lf;
3405 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003406 if (!s_in_dpichanged)
3407 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003408#endif
3409 gui_mch_free_font(gui.norm_font);
3410 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003411 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412 GetFontSize(font);
3413
K.Takatac81e9bf2022-01-16 14:15:49 +00003414 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 if (p != NULL)
3416 {
3417 hl_set_font_name(p);
3418
Bram Moolenaar734a8672019-12-02 22:49:38 +01003419 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3421 {
3422 vim_free(p_guifont);
3423 p_guifont = p;
3424 }
3425 else
3426 vim_free(p);
3427 }
3428
3429 gui_mch_free_font(gui.ital_font);
3430 gui.ital_font = NOFONT;
3431 gui_mch_free_font(gui.bold_font);
3432 gui.bold_font = NOFONT;
3433 gui_mch_free_font(gui.boldital_font);
3434 gui.boldital_font = NOFONT;
3435
3436 if (!lf.lfItalic)
3437 {
3438 lf.lfItalic = TRUE;
3439 gui.ital_font = get_font_handle(&lf);
3440 lf.lfItalic = FALSE;
3441 }
3442 if (lf.lfWeight < FW_BOLD)
3443 {
3444 lf.lfWeight = FW_BOLD;
3445 gui.bold_font = get_font_handle(&lf);
3446 if (!lf.lfItalic)
3447 {
3448 lf.lfItalic = TRUE;
3449 gui.boldital_font = get_font_handle(&lf);
3450 }
3451 }
3452
3453 return OK;
3454}
3455
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456/*
3457 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003458 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459 */
3460 int
3461gui_mch_maximized(void)
3462{
3463 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003464 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465
3466 wp.length = sizeof(WINDOWPLACEMENT);
3467 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003468 {
3469 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003471 && wp.flags == WPF_RESTORETOMAXIMIZED))
3472 return TRUE;
3473 if (wp.showCmd == SW_SHOWMINIMIZED)
3474 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003476 // Assume the window is snapped when the sizes from two APIs differ.
3477 GetWindowRect(s_hwnd, &rc);
3478 if ((rc.right - rc.left !=
3479 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3480 || (rc.bottom - rc.top !=
3481 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3482 return TRUE;
3483 }
3484 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485}
3486
3487/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003488 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3489 * is set. Compute the new Rows and Columns. This is like resizing the
3490 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003491 */
3492 void
3493gui_mch_newfont(void)
3494{
3495 RECT rect;
3496
3497 GetWindowRect(s_hwnd, &rect);
3498 if (win_socket_id == 0)
3499 {
3500 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003501 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3502 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003504 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3505 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3506 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3507 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508 }
3509 else
3510 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003511 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003513 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 }
3515}
3516
3517/*
3518 * Set the window title
3519 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520 void
3521gui_mch_settitle(
3522 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003523 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003524{
3525 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3526}
3527
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003528#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003529// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3530// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531static LPCSTR mshape_idcs[] =
3532{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003533 IDC_ARROW, // arrow
3534 MAKEINTRESOURCE(0), // blank
3535 IDC_IBEAM, // beam
3536 IDC_SIZENS, // updown
3537 IDC_SIZENS, // udsizing
3538 IDC_SIZEWE, // leftright
3539 IDC_SIZEWE, // lrsizing
3540 IDC_WAIT, // busy
3541 IDC_NO, // no
3542 IDC_ARROW, // crosshair
3543 IDC_ARROW, // hand1
3544 IDC_ARROW, // hand2
3545 IDC_ARROW, // pencil
3546 IDC_ARROW, // question
3547 IDC_ARROW, // right-arrow
3548 IDC_UPARROW, // up-arrow
3549 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550};
3551
3552 void
3553mch_set_mouse_shape(int shape)
3554{
3555 LPCSTR idc;
3556
3557 if (shape == MSHAPE_HIDE)
3558 ShowCursor(FALSE);
3559 else
3560 {
3561 if (shape >= MSHAPE_NUMBERED)
3562 idc = IDC_ARROW;
3563 else
3564 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003565 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566 if (!p_mh)
3567 {
3568 POINT mp;
3569
Bram Moolenaar734a8672019-12-02 22:49:38 +01003570 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003571 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572 (void)SetCursorPos(mp.x, mp.y);
3573 ShowCursor(TRUE);
3574 }
3575 }
3576}
3577#endif
3578
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003579#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581 * Wide version of convert_filter().
3582 */
3583 static WCHAR *
3584convert_filterW(char_u *s)
3585{
3586 char_u *tmp;
3587 int len;
3588 WCHAR *res;
3589
3590 tmp = convert_filter(s);
3591 if (tmp == NULL)
3592 return NULL;
3593 len = (int)STRLEN(s) + 3;
3594 res = enc_to_utf16(tmp, &len);
3595 vim_free(tmp);
3596 return res;
3597}
3598
3599/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003600 * Pop open a file browser and return the file selected, in allocated memory,
3601 * or NULL if Cancel is hit.
3602 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3603 * title - Title message for the file browser dialog.
3604 * dflt - Default name of file.
3605 * ext - Default extension to be added to files without extensions.
3606 * initdir - directory in which to open the browser (NULL = current dir)
3607 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003608 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003609 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003610gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611 int saving,
3612 char_u *title,
3613 char_u *dflt,
3614 char_u *ext,
3615 char_u *initdir,
3616 char_u *filter)
3617{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003618 // We always use the wide function. This means enc_to_utf16() must work,
3619 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620 OPENFILENAMEW fileStruct;
3621 WCHAR fileBuf[MAXPATHL];
3622 WCHAR *wp;
3623 int i;
3624 WCHAR *titlep = NULL;
3625 WCHAR *extp = NULL;
3626 WCHAR *initdirp = NULL;
3627 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003628 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003629 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630
3631 if (dflt == NULL)
3632 fileBuf[0] = NUL;
3633 else
3634 {
3635 wp = enc_to_utf16(dflt, NULL);
3636 if (wp == NULL)
3637 fileBuf[0] = NUL;
3638 else
3639 {
3640 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3641 fileBuf[i] = wp[i];
3642 fileBuf[i] = NUL;
3643 vim_free(wp);
3644 }
3645 }
3646
Bram Moolenaar734a8672019-12-02 22:49:38 +01003647 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003648 filterp = convert_filterW(filter);
3649
Bram Moolenaara80faa82020-04-12 19:37:17 +02003650 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003651# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003652 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003653 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003654# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003656# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657
3658 if (title != NULL)
3659 titlep = enc_to_utf16(title, NULL);
3660 fileStruct.lpstrTitle = titlep;
3661
3662 if (ext != NULL)
3663 extp = enc_to_utf16(ext, NULL);
3664 fileStruct.lpstrDefExt = extp;
3665
3666 fileStruct.lpstrFile = fileBuf;
3667 fileStruct.nMaxFile = MAXPATHL;
3668 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003669 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3670 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671 if (initdir != NULL && *initdir != NUL)
3672 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003673 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674 initdirp = enc_to_utf16(initdir, NULL);
3675 if (initdirp != NULL)
3676 {
3677 for (wp = initdirp; *wp != NUL; ++wp)
3678 if (*wp == '/')
3679 *wp = '\\';
3680 }
3681 fileStruct.lpstrInitialDir = initdirp;
3682 }
3683
3684 /*
3685 * TODO: Allow selection of multiple files. Needs another arg to this
3686 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3687 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3688 * files that don't exist yet, so I haven't put it in. What about
3689 * OFN_PATHMUSTEXIST?
3690 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3691 */
3692 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003693# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003694 if (curbuf->b_p_bin)
3695 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003696# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003697 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003698 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003699 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003700 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003701
3702 vim_free(filterp);
3703 vim_free(initdirp);
3704 vim_free(titlep);
3705 vim_free(extp);
3706
K.Takata14b8d6a2022-01-20 15:05:22 +00003707 if (!ret)
3708 return NULL;
3709
3710 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003711 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003712 if (p == NULL)
3713 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714
Bram Moolenaar734a8672019-12-02 22:49:38 +01003715 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 SetFocus(s_hwnd);
3717
Bram Moolenaar734a8672019-12-02 22:49:38 +01003718 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003719 q = vim_strsave(shorten_fname1(p));
3720 vim_free(p);
3721 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003722}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723
3724
3725/*
3726 * Convert the string s to the proper format for a filter string by replacing
3727 * the \t and \n delimiters with \0.
3728 * Returns the converted string in allocated memory.
3729 *
3730 * Keep in sync with convert_filterW() above!
3731 */
3732 static char_u *
3733convert_filter(char_u *s)
3734{
3735 char_u *res;
3736 unsigned s_len = (unsigned)STRLEN(s);
3737 unsigned i;
3738
3739 res = alloc(s_len + 3);
3740 if (res != NULL)
3741 {
3742 for (i = 0; i < s_len; ++i)
3743 if (s[i] == '\t' || s[i] == '\n')
3744 res[i] = '\0';
3745 else
3746 res[i] = s[i];
3747 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003748 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003749 res[s_len + 1] = NUL;
3750 res[s_len + 2] = NUL;
3751 }
3752 return res;
3753}
3754
3755/*
3756 * Select a directory.
3757 */
3758 char_u *
3759gui_mch_browsedir(char_u *title, char_u *initdir)
3760{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003761 // We fake this: Use a filter that doesn't select anything and a default
3762 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003763 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3764 initdir, (char_u *)_("Directory\t*.nothing\n"));
3765}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003766#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 static void
3769_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003770 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 HDROP hDrop)
3772{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003773#define BUFPATHLEN _MAX_PATH
3774#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 char szFile[BUFPATHLEN];
3777 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3778 UINT i;
3779 char_u **fnames;
3780 POINT pt;
3781 int_u modifiers = 0;
3782
Bram Moolenaar734a8672019-12-02 22:49:38 +01003783 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 DragQueryPoint(hDrop, &pt);
3785 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3786
3787 reset_VIsual();
3788
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003789 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790
3791 if (fnames != NULL)
3792 for (i = 0; i < cFiles; ++i)
3793 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3795 fnames[i] = utf16_to_enc(wszFile, NULL);
3796 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797 {
3798 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3799 fnames[i] = vim_strsave((char_u *)szFile);
3800 }
3801 }
3802
3803 DragFinish(hDrop);
3804
3805 if (fnames != NULL)
3806 {
LemonBoy45684c62022-04-24 15:46:42 +01003807 int kbd_modifiers = get_active_modifiers();
3808
3809 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003810 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003811 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003813 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 modifiers |= MOUSE_ALT;
3815
3816 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3817
3818 s_need_activate = TRUE;
3819 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003820}
3821
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003822 static int
3823_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003824 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825 HWND hwndCtl,
3826 UINT code,
3827 int pos)
3828{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003829 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830 scrollbar_T *sb, *sb_info;
3831 long val;
3832 int dragging = FALSE;
3833 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834 SCROLLINFO si;
3835
3836 si.cbSize = sizeof(si);
3837 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838
3839 sb = gui_mswin_find_scrollbar(hwndCtl);
3840 if (sb == NULL)
3841 return 0;
3842
Bram Moolenaar734a8672019-12-02 22:49:38 +01003843 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003844 {
3845 /*
3846 * Careful: need to get scrollbar info out of first (left) scrollbar
3847 * for window, but keep real scrollbar too because we must pass it to
3848 * gui_drag_scrollbar().
3849 */
3850 sb_info = &sb->wp->w_scrollbars[0];
3851 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003852 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853 sb_info = sb;
3854 val = sb_info->value;
3855
3856 switch (code)
3857 {
3858 case SB_THUMBTRACK:
3859 val = pos;
3860 dragging = TRUE;
3861 if (sb->scroll_shift > 0)
3862 val <<= sb->scroll_shift;
3863 break;
3864 case SB_LINEDOWN:
3865 val++;
3866 break;
3867 case SB_LINEUP:
3868 val--;
3869 break;
3870 case SB_PAGEDOWN:
3871 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3872 break;
3873 case SB_PAGEUP:
3874 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3875 break;
3876 case SB_TOP:
3877 val = 0;
3878 break;
3879 case SB_BOTTOM:
3880 val = sb_info->max;
3881 break;
3882 case SB_ENDSCROLL:
3883 if (prev_code == SB_THUMBTRACK)
3884 {
3885 /*
3886 * "pos" only gives us 16-bit data. In case of large file,
3887 * use GetScrollPos() which returns 32-bit. Unfortunately it
3888 * is not valid while the scrollbar is being dragged.
3889 */
3890 val = GetScrollPos(hwndCtl, SB_CTL);
3891 if (sb->scroll_shift > 0)
3892 val <<= sb->scroll_shift;
3893 }
3894 break;
3895
3896 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003897 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898 return 0;
3899 }
3900 prev_code = code;
3901
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003902 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3903 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003904
3905 /*
3906 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3907 */
3908 if (sb->wp != NULL)
3909 {
3910 scrollbar_T *sba = sb->wp->w_scrollbars;
3911 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3912
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914 }
3915
Bram Moolenaar734a8672019-12-02 22:49:38 +01003916 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917 s_busy_processing = TRUE;
3918
Bram Moolenaar734a8672019-12-02 22:49:38 +01003919 // When "allow_scrollbar" is FALSE still need to remember the new
3920 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921 dont_scroll = !allow_scrollbar;
3922
Bram Moolenaara338adc2018-01-31 20:51:47 +01003923 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003925 mch_enable_flush();
3926 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003927
3928 s_busy_processing = FALSE;
3929 dont_scroll = dont_scroll_save;
3930
3931 return 0;
3932}
3933
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#ifdef FEAT_XPM_W32
3936# include "xpm_w32.h"
3937#endif
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
Bram Moolenaar734a8672019-12-02 22:49:38 +01003940// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#define TEAROFF_PADDING_X 2
3942#define TEAROFF_BUTTON_PAD_X 8
3943#define TEAROFF_MIN_WIDTH 200
3944#define TEAROFF_SUBMENU_LABEL ">>"
3945#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3946
3947
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003948#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949# define ID_BEVAL_TOOLTIP 200
3950# define BEVAL_TEXT_LEN MAXPATHL
3951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003953static UINT_PTR beval_timer_id = 0;
3954static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003955#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003956
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003957
Bram Moolenaar734a8672019-12-02 22:49:38 +01003958// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960#ifdef FEAT_MENU
3961static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964/*
3965 * Use the system font for dialogs and tear-off menus. Remove this line to
3966 * use DLG_FONT_NAME.
3967 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003968#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971#define VIM_CLASSW L"Vim"
3972
Bram Moolenaar734a8672019-12-02 22:49:38 +01003973// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3974// thus there should be room for every dialog. For tearoffs it's made bigger
3975// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#define DLG_ALLOC_SIZE 16 * 1024
3977
3978/*
3979 * stuff for dialogs, menus, tearoffs etc.
3980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981static PWORD
3982add_dialog_element(
3983 PWORD p,
3984 DWORD lStyle,
3985 WORD x,
3986 WORD y,
3987 WORD w,
3988 WORD h,
3989 WORD Id,
3990 WORD clss,
3991 const char *caption);
3992static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003993static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003994#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997static void get_dialog_font_metrics(void);
3998
3999static int dialog_default_button = -1;
4000
Bram Moolenaar734a8672019-12-02 22:49:38 +01004001// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002static int mouse_scroll_lines = 0;
LemonBoy365d8f72022-05-05 19:23:07 +01004003static int mouse_scroll_chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#ifdef FEAT_TOOLBAR
4006static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004007static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004008static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004010#else
4011# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#endif
4013
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004014#ifdef FEAT_GUI_TABLINE
4015static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004016static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004017#endif
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#ifdef FEAT_MBYTE_IME
4020static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4021static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4022#endif
4023#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4024# ifdef NOIME
4025typedef struct tagCOMPOSITIONFORM {
4026 DWORD dwStyle;
4027 POINT ptCurrentPos;
4028 RECT rcArea;
4029} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4030typedef HANDLE HIMC;
4031# endif
4032
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004033static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004034static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4035static HIMC (WINAPI *pImmGetContext)(HWND);
4036static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4037static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4038static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4039static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004040static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4041static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004042static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4043static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004044static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045static void dyn_imm_load(void);
4046#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047# define pImmGetCompositionStringW ImmGetCompositionStringW
4048# define pImmGetContext ImmGetContext
4049# define pImmAssociateContext ImmAssociateContext
4050# define pImmReleaseContext ImmReleaseContext
4051# define pImmGetOpenStatus ImmGetOpenStatus
4052# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004053# define pImmGetCompositionFontW ImmGetCompositionFontW
4054# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055# define pImmSetCompositionWindow ImmSetCompositionWindow
4056# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004057# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058#endif
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060#ifdef FEAT_MENU
4061/*
4062 * Figure out how high the menu bar is at the moment.
4063 */
4064 static int
4065gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004066 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
4068 static int old_menu_height = -1;
4069
4070 RECT rc1, rc2;
4071 int num;
4072 int menu_height;
4073
4074 if (gui.menu_is_active)
4075 num = GetMenuItemCount(s_menuBar);
4076 else
4077 num = 0;
4078
4079 if (num == 0)
4080 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004081 else if (IsMinimized(s_hwnd))
4082 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004083 // The height of the menu cannot be determined while the window is
4084 // minimized. Take the previous height if the menu is changed in that
4085 // state, to avoid that Vim's vertical window size accidentally
4086 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004087 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 else
4090 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004091 /*
4092 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4093 * seem to have been set yet, so menu wraps in default window
4094 * width which is very narrow. Instead just return height of a
4095 * single menu item. Will still be wrong when the menu really
4096 * should wrap over more than one line.
4097 */
4098 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4099 if (gui.starting)
4100 menu_height = rc1.bottom - rc1.top + 1;
4101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004103 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4104 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 }
4107
4108 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004109 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004110 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 return menu_height;
4113}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004114#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116
4117/*
4118 * Setup for the Intellimouse
4119 */
4120 static void
4121init_mouse_wheel(void)
4122{
LemonBoy365d8f72022-05-05 19:23:07 +01004123 // Reasonable default values.
4124 mouse_scroll_lines = 3;
4125 mouse_scroll_chars = 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaar734a8672019-12-02 22:49:38 +01004127 // if NT 4.0+ (or Win98) get scroll lines directly from system
LemonBoy365d8f72022-05-05 19:23:07 +01004128 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &mouse_scroll_lines, 0);
4129 SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &mouse_scroll_chars, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130}
4131
4132
Bram Moolenaarae20f342019-11-05 21:09:23 +01004133/*
4134 * Intellimouse wheel handler.
4135 * Treat a mouse wheel event as if it were a scroll request.
4136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 static void
LemonBoy365d8f72022-05-05 19:23:07 +01004138_OnMouseWheel(HWND hwnd, short zDelta, LPARAM param, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
LemonBoy365d8f72022-05-05 19:23:07 +01004140 int i;
4141 int amount;
4142 int button;
4143 win_T *wp;
4144 int modifiers, kbd_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
LemonBoy365d8f72022-05-05 19:23:07 +01004146 // Initializes mouse_scroll_chars too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (mouse_scroll_lines == 0)
4148 init_mouse_wheel();
4149
Bram Moolenaarae20f342019-11-05 21:09:23 +01004150 wp = gui_mouse_window(FIND_POPUP);
4151
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004152#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004153 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004154 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004155 cmdarg_T cap;
4156 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004157
Bram Moolenaarae20f342019-11-05 21:09:23 +01004158 // Mouse hovers over popup window, scroll it if possible.
4159 mouse_row = wp->w_winrow;
4160 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004161 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004162 if (horizontal)
4163 {
4164 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4165 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4166 }
4167 else
4168 {
4169 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4170 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4171 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004172 clear_oparg(&oa);
4173 cap.oap = &oa;
4174 nv_mousescroll(&cap);
4175 update_screen(0);
4176 setcursor();
4177 out_flush();
4178 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004179 }
4180#endif
4181
Bram Moolenaarae20f342019-11-05 21:09:23 +01004182 if (wp == NULL || !p_scf)
4183 wp = curwin;
4184
LemonBoy365d8f72022-05-05 19:23:07 +01004185 // Translate the scroll event into an event that Vim can process so that
4186 // the user has a chance to map the scrollwheel buttons.
4187 if (horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
LemonBoy365d8f72022-05-05 19:23:07 +01004189 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
4190 if (mouse_scroll_chars > 0
4191 && mouse_scroll_chars < MAX(wp->w_width - 2, 1))
4192 amount = mouse_scroll_chars;
4193 else
4194 amount = MAX(wp->w_width - 2, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 else
LemonBoy365d8f72022-05-05 19:23:07 +01004197 {
4198 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
4199 if (mouse_scroll_lines > 0
4200 && mouse_scroll_lines < MAX(wp->w_height - 2, 1))
4201 amount = mouse_scroll_lines;
4202 else
4203 amount = MAX(wp->w_height - 2, 1);
4204 }
4205
4206 kbd_modifiers = get_active_modifiers();
4207
4208 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4209 modifiers |= MOUSE_SHIFT;
4210 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4211 modifiers |= MOUSE_CTRL;
4212 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4213 modifiers |= MOUSE_ALT;
4214
4215 mch_disable_flush();
4216 for (i = amount; i > 0; --i)
4217 gui_send_mouse_event(button, GET_X_LPARAM(param), GET_Y_LPARAM(param),
4218 FALSE, kbd_modifiers);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004219 mch_enable_flush();
4220 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004223#ifdef USE_SYSMENU_FONT
4224/*
4225 * Get Menu Font.
4226 * Return OK or FAIL.
4227 */
4228 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004229gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004230{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004231 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004232
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004233 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4234 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004235 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004236 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004237 &nm,
4238 0))
4239 return FAIL;
4240 *lf = nm.lfMenuFont;
4241 return OK;
4242}
4243#endif
4244
4245
4246#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4247/*
4248 * Set the GUI tabline font to the system menu font
4249 */
4250 static void
4251set_tabline_font(void)
4252{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004253 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004254 HFONT font;
4255 HWND hwnd;
4256 HDC hdc;
4257 HFONT hfntOld;
4258 TEXTMETRIC tm;
4259
4260 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4261 return;
4262
K.Takatac81e9bf2022-01-16 14:15:49 +00004263 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004264 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004265
4266 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4267
4268 /*
4269 * Compute the height of the font used for the tab text
4270 */
4271 hwnd = GetDesktopWindow();
4272 hdc = GetWindowDC(hwnd);
4273 hfntOld = SelectFont(hdc, font);
4274
4275 GetTextMetrics(hdc, &tm);
4276
4277 SelectFont(hdc, hfntOld);
4278 ReleaseDC(hwnd, hdc);
4279
4280 /*
4281 * The space used by the tab border and the space between the tab label
4282 * and the tab border is included as 7.
4283 */
4284 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4285}
K.Takatac81e9bf2022-01-16 14:15:49 +00004286#else
4287# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004288#endif
4289
Bram Moolenaar520470a2005-06-16 21:59:56 +00004290/*
4291 * Invoked when a setting was changed.
4292 */
4293 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004294_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004295{
LemonBoy365d8f72022-05-05 19:23:07 +01004296 switch (param)
4297 {
4298 case SPI_SETWHEELSCROLLLINES:
4299 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4300 &mouse_scroll_lines, 0);
4301 break;
4302 case SPI_GETWHEELSCROLLCHARS:
4303 SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0,
4304 &mouse_scroll_chars, 0);
4305 break;
4306 case SPI_SETNONCLIENTMETRICS:
4307 set_tabline_font();
4308 break;
4309 default:
4310 break;
4311 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004312 return 0;
4313}
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#ifdef FEAT_NETBEANS_INTG
4316 static void
4317_OnWindowPosChanged(
4318 HWND hwnd,
4319 const LPWINDOWPOS lpwpos)
4320{
4321 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004322 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4325 || lpwpos->cx != cx || lpwpos->cy != cy))
4326 {
4327 x = lpwpos->x;
4328 y = lpwpos->y;
4329 cx = lpwpos->cx;
4330 cy = lpwpos->cy;
4331 netbeans_frame_moved(x, y);
4332 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004333 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004334 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335}
4336#endif
4337
K.Takata4f2417f2021-06-05 16:25:32 +02004338
4339static HWND hwndTip = NULL;
4340
4341 static void
4342show_sizing_tip(int cols, int rows)
4343{
4344 TOOLINFOA ti = {sizeof(ti)};
4345 char buf[32];
4346
4347 ti.hwnd = s_hwnd;
4348 ti.uId = (UINT_PTR)s_hwnd;
4349 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4350 ti.lpszText = buf;
4351 sprintf(buf, "%dx%d", cols, rows);
4352 if (hwndTip == NULL)
4353 {
4354 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4355 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4356 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4357 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4358 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4359 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4360 }
4361 else
4362 {
4363 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4364 }
4365 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4366}
4367
4368 static void
4369destroy_sizing_tip(void)
4370{
4371 if (hwndTip != NULL)
4372 {
4373 DestroyWindow(hwndTip);
4374 hwndTip = NULL;
4375 }
4376}
4377
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 static int
4379_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 UINT fwSide,
4381 LPRECT lprc)
4382{
4383 int w, h;
4384 int valid_w, valid_h;
4385 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004386 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 w = lprc->right - lprc->left;
4389 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004390 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 w_offset = w - valid_w;
4392 h_offset = h - valid_h;
4393
4394 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4395 || fwSide == WMSZ_BOTTOMLEFT)
4396 lprc->left += w_offset;
4397 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4398 || fwSide == WMSZ_BOTTOMRIGHT)
4399 lprc->right -= w_offset;
4400
4401 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4402 || fwSide == WMSZ_TOPRIGHT)
4403 lprc->top += h_offset;
4404 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4405 || fwSide == WMSZ_BOTTOMRIGHT)
4406 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004407
4408 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return TRUE;
4410}
4411
K.Takata92000e22022-01-20 15:10:57 +00004412#ifdef FEAT_GUI_TABLINE
4413 static void
4414_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4415{
4416 if (gui_mch_showing_tabline())
4417 {
4418 POINT pt;
4419 RECT rect;
4420
4421 /*
4422 * If the cursor is on the tabline, display the tab menu
4423 */
4424 GetCursorPos(&pt);
4425 GetWindowRect(s_textArea, &rect);
4426 if (pt.y < rect.top)
4427 {
4428 show_tabline_popup_menu();
4429 return;
4430 }
4431 }
4432 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4433}
4434
4435 static void
4436_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4437{
4438 /*
4439 * If the user double clicked the tabline, create a new tab
4440 */
4441 if (gui_mch_showing_tabline())
4442 {
4443 POINT pt;
4444 RECT rect;
4445
4446 GetCursorPos(&pt);
4447 GetWindowRect(s_textArea, &rect);
4448 if (pt.y < rect.top)
4449 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4450 }
4451 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4452}
4453#endif
4454
4455 static UINT
4456_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4457{
4458 UINT result;
4459 int x, y;
4460
4461 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4462 if (result != HTCLIENT)
4463 return result;
4464
4465#ifdef FEAT_GUI_TABLINE
4466 if (gui_mch_showing_tabline())
4467 {
4468 RECT rct;
4469
4470 // If the cursor is on the GUI tabline, don't process this event
4471 GetWindowRect(s_textArea, &rct);
4472 if (yPos < rct.top)
4473 return result;
4474 }
4475#endif
4476 (void)gui_mch_get_winpos(&x, &y);
4477 xPos -= x;
4478
4479 if (xPos < 48) // <VN> TODO should use system metric?
4480 return HTBOTTOMLEFT;
4481 else
4482 return HTBOTTOMRIGHT;
4483}
4484
4485#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4486 static LRESULT
4487_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4488{
4489 switch (hdr->code)
4490 {
4491 case TTN_GETDISPINFOW:
4492 case TTN_GETDISPINFO:
4493 {
4494 char_u *str = NULL;
4495 static void *tt_text = NULL;
4496
4497 VIM_CLEAR(tt_text);
4498
4499# ifdef FEAT_GUI_TABLINE
4500 if (gui_mch_showing_tabline()
4501 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4502 {
4503 POINT pt;
4504 /*
4505 * Mouse is over the GUI tabline. Display the
4506 * tooltip for the tab under the cursor
4507 *
4508 * Get the cursor position within the tab control
4509 */
4510 GetCursorPos(&pt);
4511 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4512 {
4513 TCHITTESTINFO htinfo;
4514 int idx;
4515
4516 /*
4517 * Get the tab under the cursor
4518 */
4519 htinfo.pt.x = pt.x;
4520 htinfo.pt.y = pt.y;
4521 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4522 if (idx != -1)
4523 {
4524 tabpage_T *tp;
4525
4526 tp = find_tabpage(idx + 1);
4527 if (tp != NULL)
4528 {
4529 get_tabline_label(tp, TRUE);
4530 str = NameBuff;
4531 }
4532 }
4533 }
4534 }
4535# endif
4536# ifdef FEAT_TOOLBAR
4537# ifdef FEAT_GUI_TABLINE
4538 else
4539# endif
4540 {
4541 UINT idButton;
4542 vimmenu_T *pMenu;
4543
4544 idButton = (UINT) hdr->idFrom;
4545 pMenu = gui_mswin_find_menu(root_menu, idButton);
4546 if (pMenu)
4547 str = pMenu->strings[MENU_INDEX_TIP];
4548 }
4549# endif
4550 if (str == NULL)
4551 break;
4552
4553 // Set the maximum width, this also enables using \n for
4554 // line break.
4555 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4556
4557 if (hdr->code == TTN_GETDISPINFOW)
4558 {
4559 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4560
4561 tt_text = enc_to_utf16(str, NULL);
4562 lpdi->lpszText = tt_text;
4563 // can't show tooltip if failed
4564 }
4565 else
4566 {
4567 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4568
4569 if (STRLEN(str) < sizeof(lpdi->szText)
4570 || ((tt_text = vim_strsave(str)) == NULL))
4571 vim_strncpy((char_u *)lpdi->szText, str,
4572 sizeof(lpdi->szText) - 1);
4573 else
4574 lpdi->lpszText = tt_text;
4575 }
4576 }
4577 break;
4578
4579# ifdef FEAT_GUI_TABLINE
4580 case TCN_SELCHANGE:
4581 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4582 {
4583 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4584 return 0L;
4585 }
4586 break;
4587
4588 case NM_RCLICK:
4589 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4590 {
4591 show_tabline_popup_menu();
4592 return 0L;
4593 }
4594 break;
4595# endif
4596
4597 default:
4598 break;
4599 }
4600 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4601}
4602#endif
4603
4604#if defined(MENUHINTS) && defined(FEAT_MENU)
4605 static LRESULT
4606_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4607{
4608 if (((UINT) HIWORD(wParam)
4609 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4610 == MF_HILITE
4611 && (State & CMDLINE) == 0)
4612 {
4613 UINT idButton;
4614 vimmenu_T *pMenu;
4615 static int did_menu_tip = FALSE;
4616
4617 if (did_menu_tip)
4618 {
4619 msg_clr_cmdline();
4620 setcursor();
4621 out_flush();
4622 did_menu_tip = FALSE;
4623 }
4624
4625 idButton = (UINT)LOWORD(wParam);
4626 pMenu = gui_mswin_find_menu(root_menu, idButton);
4627 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4628 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4629 {
4630 ++msg_hist_off;
4631 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4632 --msg_hist_off;
4633 setcursor();
4634 out_flush();
4635 did_menu_tip = TRUE;
4636 }
4637 return 0L;
4638 }
4639 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4640}
4641#endif
4642
K.Takatac81e9bf2022-01-16 14:15:49 +00004643 static LRESULT
4644_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4645{
4646 s_dpi = ydpi;
4647 s_in_dpichanged = TRUE;
4648 //TRACE("DPI: %d", ydpi);
4649
4650 update_scrollbar_size();
4651 update_toolbar_size();
4652 set_tabline_font();
4653
4654 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4655 gui_get_wide_font();
4656 gui_mswin_get_menu_height(FALSE);
4657#ifdef FEAT_MBYTE_IME
4658 im_set_position(gui.row, gui.col);
4659#endif
4660 InvalidateRect(hwnd, NULL, TRUE);
4661
4662 s_in_dpichanged = FALSE;
4663 return 0L;
4664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666
4667 static LRESULT CALLBACK
4668_WndProc(
4669 HWND hwnd,
4670 UINT uMsg,
4671 WPARAM wParam,
4672 LPARAM lParam)
4673{
LemonBoy77fc0b02022-04-22 22:45:52 +01004674 // TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4675 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677 HandleMouseHide(uMsg, lParam);
4678
4679 s_uMsg = uMsg;
4680 s_wParam = wParam;
4681 s_lParam = lParam;
4682
4683 switch (uMsg)
4684 {
4685 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4686 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004687 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004689 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4691 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4692 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4693 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4694#ifdef FEAT_MENU
4695 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4696#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004697 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4698 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4700 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004701 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4702 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4704 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4705 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4706#ifdef FEAT_NETBEANS_INTG
4707 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4708#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004709#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004710 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4711 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004712#endif
K.Takata92000e22022-01-20 15:10:57 +00004713 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004714
Bram Moolenaar734a8672019-12-02 22:49:38 +01004715 case WM_QUERYENDSESSION: // System wants to go down.
4716 gui_shell_closed(); // Will exit when no changed buffers.
4717 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
4719 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004720 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004723 return 0L;
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 break;
4726
4727 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004728 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4729 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004730 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 return 0L;
4732
4733 case WM_SYSCHAR:
4734 /*
4735 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4736 * shortcut key, handle like a typed ALT key, otherwise call Windows
4737 * ALT key handling.
4738 */
4739#ifdef FEAT_MENU
4740 if ( !gui.menu_is_active
4741 || p_wak[0] == 'n'
4742 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4743 )
4744#endif
4745 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004746 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 return 0L;
4748 }
4749#ifdef FEAT_MENU
4750 else
K.Takata4ac893f2022-01-20 12:44:28 +00004751 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#endif
4753
4754 case WM_SYSKEYUP:
4755#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004756 // This used to be done only when menu is active: ALT key is used for
4757 // that. But that caused problems when menu is disabled and using
4758 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4759 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004760 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004762 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#endif
4764
K.Takata4f2417f2021-06-05 16:25:32 +02004765 case WM_EXITSIZEMOVE:
4766 destroy_sizing_tip();
4767 break;
4768
Bram Moolenaar734a8672019-12-02 22:49:38 +01004769 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004773 case WM_MOUSEHWHEEL:
4774 _OnMouseWheel(hwnd, HIWORD(wParam), lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004775 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar734a8672019-12-02 22:49:38 +01004777 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004778 case WM_SETTINGCHANGE:
4779 return _OnSettingChange((UINT)wParam);
4780
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004781#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004783 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#endif
K.Takata92000e22022-01-20 15:10:57 +00004785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#if defined(MENUHINTS) && defined(FEAT_MENU)
4787 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004788 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791#ifdef FEAT_MBYTE_IME
4792 case WM_IME_NOTIFY:
4793 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004794 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004795 return 1L;
4796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 case WM_IME_COMPOSITION:
4798 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004799 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004800 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004802 case WM_DPICHANGED:
4803 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4804 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004808 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810#endif
K.Takata92000e22022-01-20 15:10:57 +00004811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 }
4813
K.Takata4ac893f2022-01-20 12:44:28 +00004814 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815}
4816
4817/*
4818 * End of call-back routines
4819 */
4820
Bram Moolenaar734a8672019-12-02 22:49:38 +01004821// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822HWND vim_parent_hwnd = NULL;
4823
4824 static BOOL CALLBACK
4825FindWindowTitle(HWND hwnd, LPARAM lParam)
4826{
4827 char buf[2048];
4828 char *title = (char *)lParam;
4829
4830 if (GetWindowText(hwnd, buf, sizeof(buf)))
4831 {
4832 if (strstr(buf, title) != NULL)
4833 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004834 // Found it. Store the window ref. and quit searching if MDI
4835 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004837 if (vim_parent_hwnd != NULL)
4838 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004841 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842}
4843
4844/*
4845 * Invoked for '-P "title"' argument: search for parent application to open
4846 * our window in.
4847 */
4848 void
4849gui_mch_set_parent(char *title)
4850{
4851 EnumWindows(FindWindowTitle, (LPARAM)title);
4852 if (vim_parent_hwnd == NULL)
4853 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004854 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 mch_exit(2);
4856 }
4857}
4858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004859#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 static void
4861ole_error(char *arg)
4862{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004863 char buf[IOSIZE];
4864
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004865# ifdef VIMDLL
4866 gui.in_use = mch_is_gui_executable();
4867# endif
4868
Bram Moolenaar734a8672019-12-02 22:49:38 +01004869 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004870 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004871 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004872 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004876#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4877 static char *
4878gvim_error(void)
4879{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004880 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004881
4882 if (starting)
4883 {
4884 mch_errmsg(msg);
4885 mch_errmsg("\n");
4886 mch_exit(2);
4887 }
4888 return msg;
4889}
4890
4891 char *
4892gui_mch_do_spawn(char_u *arg)
4893{
4894 int len;
4895# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4896 char_u *session = NULL;
4897 LPWSTR tofree1 = NULL;
4898# endif
4899 WCHAR name[MAX_PATH];
4900 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4901 STARTUPINFOW si = {sizeof(si)};
4902 PROCESS_INFORMATION pi;
4903
4904 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4905 goto error;
4906 p = wcsrchr(name, L'\\');
4907 if (p == NULL)
4908 goto error;
4909 // Replace the executable name from vim(d).exe to gvim(d).exe.
4910# ifdef DEBUG
4911 wcscpy(p + 1, L"gvimd.exe");
4912# else
4913 wcscpy(p + 1, L"gvim.exe");
4914# endif
4915
4916# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4917 if (starting)
4918# endif
4919 {
4920 // Pass the command line to the new process.
4921 p = GetCommandLineW();
4922 // Skip 1st argument.
4923 while (*p && *p != L' ' && *p != L'\t')
4924 {
4925 if (*p == L'"')
4926 {
4927 while (*p && *p != L'"')
4928 ++p;
4929 if (*p)
4930 ++p;
4931 }
4932 else
4933 ++p;
4934 }
4935 cmd = p;
4936 }
4937# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4938 else
4939 {
4940 // Create a session file and pass it to the new process.
4941 LPWSTR wsession;
4942 char_u *savebg;
4943 int ret;
4944
4945 session = vim_tempname('s', FALSE);
4946 if (session == NULL)
4947 goto error;
4948 savebg = p_bg;
4949 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4950 ret = write_session_file(session);
4951 vim_free(p_bg);
4952 p_bg = savebg;
4953 if (!ret)
4954 goto error;
4955 wsession = enc_to_utf16(session, NULL);
4956 if (wsession == NULL)
4957 goto error;
4958 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004959 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004960 if (cmd == NULL)
4961 {
4962 vim_free(wsession);
4963 goto error;
4964 }
4965 tofree1 = cmd;
4966 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4967 wsession, wsession);
4968 vim_free(wsession);
4969 }
4970# endif
4971
4972 // Check additional arguments to the `:gui` command.
4973 if (arg != NULL)
4974 {
4975 warg = enc_to_utf16(arg, NULL);
4976 if (warg == NULL)
4977 goto error;
4978 tofree2 = warg;
4979 }
4980 else
4981 warg = L"";
4982
4983 // Set up the new command line.
4984 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004985 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004986 if (newcmd == NULL)
4987 goto error;
4988 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4989
4990 // Spawn a new GUI process.
4991 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4992 NULL, NULL, &si, &pi))
4993 goto error;
4994 CloseHandle(pi.hProcess);
4995 CloseHandle(pi.hThread);
4996 mch_exit(0);
4997
4998error:
4999# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5000 if (session)
5001 mch_remove(session);
5002 vim_free(session);
5003 vim_free(tofree1);
5004# endif
5005 vim_free(newcmd);
5006 vim_free(tofree2);
5007 return gvim_error();
5008}
5009#endif
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011/*
5012 * Parse the GUI related command-line arguments. Any arguments used are
5013 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005014 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 */
5016 void
5017gui_mch_prepare(int *argc, char **argv)
5018{
5019 int silent = FALSE;
5020 int idx;
5021
Bram Moolenaar734a8672019-12-02 22:49:38 +01005022 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5024 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005025 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5027 && (argv[2][0] == '-' || argv[2][0] == '/'))
5028 {
5029 silent = TRUE;
5030 idx = 2;
5031 }
5032 else
5033 idx = 1;
5034
Bram Moolenaar734a8672019-12-02 22:49:38 +01005035 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (STRICMP(argv[idx] + 1, "register") == 0)
5037 {
5038#ifdef FEAT_OLE
5039 RegisterMe(silent);
5040 mch_exit(0);
5041#else
5042 if (!silent)
5043 ole_error("register");
5044 mch_exit(2);
5045#endif
5046 }
5047
Bram Moolenaar734a8672019-12-02 22:49:38 +01005048 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5050 {
5051#ifdef FEAT_OLE
5052 UnregisterMe(!silent);
5053 mch_exit(0);
5054#else
5055 if (!silent)
5056 ole_error("unregister");
5057 mch_exit(2);
5058#endif
5059 }
5060
Bram Moolenaar734a8672019-12-02 22:49:38 +01005061 // Ignore an -embedding argument. It is only relevant if the
5062 // application wants to treat the case when it is started manually
5063 // differently from the case where it is started via automation (and
5064 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5066 {
5067#ifdef FEAT_OLE
5068 *argc = 1;
5069#else
5070 ole_error("embedding");
5071 mch_exit(2);
5072#endif
5073 }
5074 }
5075
5076#ifdef FEAT_OLE
5077 {
5078 int bDoRestart = FALSE;
5079
5080 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005081 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (bDoRestart)
5083 mch_exit(0);
5084 }
5085#endif
5086
5087#ifdef FEAT_NETBEANS_INTG
5088 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005089 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 int arg;
5091
5092 for (arg = 1; arg < *argc; arg++)
5093 if (strncmp("-nb", argv[arg], 3) == 0)
5094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 netbeansArg = argv[arg];
5096 mch_memmove(&argv[arg], &argv[arg + 1],
5097 (--*argc - arg) * sizeof(char *));
5098 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005099 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103}
5104
K.Takatac81e9bf2022-01-16 14:15:49 +00005105 static void
5106load_dpi_func(void)
5107{
5108 HMODULE hUser32;
5109
5110 hUser32 = GetModuleHandle("user32.dll");
5111 if (hUser32 == NULL)
5112 goto fail;
5113
5114 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5115 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5116 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5117 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5118 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5119 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5120
5121 if (pSetThreadDpiAwarenessContext != NULL)
5122 {
5123 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5124 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5125 if (oldctx != NULL)
5126 {
5127 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5128 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5129#ifdef DEBUG
5130 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5131 {
5132 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5133 }
5134#endif
5135 return;
5136 }
5137 }
5138
5139fail:
5140 // Disable PerMonitorV2 APIs.
5141 pGetDpiForSystem = stubGetDpiForSystem;
5142 pGetDpiForWindow = NULL;
5143 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5144 pSetThreadDpiAwarenessContext = NULL;
5145 pGetAwarenessFromDpiAwarenessContext = NULL;
5146}
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148/*
5149 * Initialise the GUI. Create all the windows, set up all the call-backs
5150 * etc.
5151 */
5152 int
5153gui_mch_init(void)
5154{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005156 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
Bram Moolenaar734a8672019-12-02 22:49:38 +01005159 // Return here if the window was already opened (happens when
5160 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005162 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * Load the tearoff bitmap
5166 */
5167#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005168 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169#endif
5170
K.Takatac81e9bf2022-01-16 14:15:49 +00005171 load_dpi_func();
5172
5173 s_dpi = pGetDpiForSystem();
5174 update_scrollbar_size();
5175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005177 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#endif
5179 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005180#ifdef FEAT_TOOLBAR
5181 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
5184 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5185
Bram Moolenaar734a8672019-12-02 22:49:38 +01005186 // First try using the wide version, so that we can use any title.
5187 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005188 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005190 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 wndclassw.lpfnWndProc = _WndProc;
5192 wndclassw.cbClsExtra = 0;
5193 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005194 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5196 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5197 wndclassw.hbrBackground = s_brush;
5198 wndclassw.lpszMenuName = NULL;
5199 wndclassw.lpszClassName = szVimWndClassW;
5200
K.Takata4ac893f2022-01-20 12:44:28 +00005201 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (vim_parent_hwnd != NULL)
5206 {
5207#ifdef HAVE_TRY_EXCEPT
5208 __try
5209 {
5210#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005211 // Open inside the specified parent window.
5212 // TODO: last argument should point to a CLIENTCREATESTRUCT
5213 // structure.
5214 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005216 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005217 WS_OVERLAPPEDWINDOW | WS_CHILD
5218 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5220 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005221 100, // Any value will do
5222 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005224 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#ifdef HAVE_TRY_EXCEPT
5226 }
5227 __except(EXCEPTION_EXECUTE_HANDLER)
5228 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005229 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
5231#endif
5232 if (s_hwnd == NULL)
5233 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005234 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 mch_exit(2);
5236 }
5237 }
5238 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005239 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005240 // If the provided windowid is not valid reset it to zero, so that it
5241 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005242 if (IsWindow((HWND)win_socket_id) <= 0)
5243 win_socket_id = 0;
5244
Bram Moolenaar734a8672019-12-02 22:49:38 +01005245 // Create a window. If win_socket_id is not zero without border and
5246 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005247 s_hwnd = CreateWindowW(
5248 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005249 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5250 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005251 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5252 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 100, // Any value will do
5254 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005255 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005256 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005257 if (s_hwnd != NULL && win_socket_id != 0)
5258 {
5259 SetParent(s_hwnd, (HWND)win_socket_id);
5260 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5261 }
5262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 if (s_hwnd == NULL)
5265 return FAIL;
5266
K.Takatac81e9bf2022-01-16 14:15:49 +00005267 if (pGetDpiForWindow != NULL)
5268 {
5269 s_dpi = pGetDpiForWindow(s_hwnd);
5270 update_scrollbar_size();
5271 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5272 }
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5275 dyn_imm_load();
5276#endif
5277
Bram Moolenaar734a8672019-12-02 22:49:38 +01005278 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005279 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005280 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005281 wndclassw.style = CS_OWNDC;
5282 wndclassw.lpfnWndProc = _TextAreaWndProc;
5283 wndclassw.cbClsExtra = 0;
5284 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005285 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005286 wndclassw.hIcon = NULL;
5287 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5288 wndclassw.hbrBackground = NULL;
5289 wndclassw.lpszMenuName = NULL;
5290 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005291
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 return FAIL;
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005296 s_textArea = CreateWindowExW(
5297 0,
5298 szTextAreaClassW, L"Vim text area",
5299 WS_CHILD | WS_VISIBLE, 0, 0,
5300 100, // Any value will do for now
5301 100, // Any value will do for now
5302 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005303 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (s_textArea == NULL)
5306 return FAIL;
5307
Bram Moolenaar20321902016-02-17 12:30:17 +01005308#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005309 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005310 {
5311 HANDLE hIcon = NULL;
5312
5313 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005314 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005315 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005316#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#ifdef FEAT_MENU
5319 s_menuBar = CreateMenu();
5320#endif
5321 s_hdc = GetDC(s_textArea);
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
Bram Moolenaar734a8672019-12-02 22:49:38 +01005325 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005326 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
Bram Moolenaar734a8672019-12-02 22:49:38 +01005328 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 gui_mch_def_colors();
5330
Bram Moolenaar734a8672019-12-02 22:49:38 +01005331 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5332 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 set_normal_colors();
5334
5335 /*
5336 * Check that none of the colors are the same as the background color.
5337 * Then store the current values as the defaults.
5338 */
5339 gui_check_colors();
5340 gui.def_norm_pixel = gui.norm_pixel;
5341 gui.def_back_pixel = gui.back_pixel;
5342
Bram Moolenaar734a8672019-12-02 22:49:38 +01005343 // Get the colors for the highlight groups (gui_check_colors() might have
5344 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 highlight_gui_started();
5346
5347 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005348 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005350 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 /*
5353 * Set up for Intellimouse processing
5354 */
5355 init_mouse_wheel();
5356
5357 /*
5358 * compute a couple of metrics used for the dialogs
5359 */
5360 get_dialog_font_metrics();
5361#ifdef FEAT_TOOLBAR
5362 /*
5363 * Create the toolbar
5364 */
5365 initialise_toolbar();
5366#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005367#ifdef FEAT_GUI_TABLINE
5368 /*
5369 * Create the tabline
5370 */
5371 initialise_tabline();
5372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#ifdef MSWIN_FIND_REPLACE
5374 /*
5375 * Initialise the dialog box stuff
5376 */
5377 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5378
Bram Moolenaar734a8672019-12-02 22:49:38 +01005379 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005381 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005383 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5385 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5386 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005389#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005390 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005391 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005392#endif
5393
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005394#ifdef FEAT_RENDER_OPTIONS
5395 if (p_rop)
5396 (void)gui_mch_set_rendering_options(p_rop);
5397#endif
5398
Bram Moolenaar748bf032005-02-02 23:04:36 +00005399theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005401 display_errors();
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return OK;
5404}
5405
5406/*
5407 * Get the size of the screen, taking position on multiple monitors into
5408 * account (if supported).
5409 */
5410 static void
5411get_work_area(RECT *spi_rect)
5412{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005413 HMONITOR mon;
5414 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
Bram Moolenaar734a8672019-12-02 22:49:38 +01005416 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005417 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005418 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005420 moninfo.cbSize = sizeof(MONITORINFO);
5421 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005423 *spi_rect = moninfo.rcWork;
5424 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
5426 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005427 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5429}
5430
5431/*
5432 * Set the size of the window to the given width and height in pixels.
5433 */
5434 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005435gui_mch_set_shellsize(
5436 int width,
5437 int height,
5438 int min_width UNUSED,
5439 int min_height UNUSED,
5440 int base_width UNUSED,
5441 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005442 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005445 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar734a8672019-12-02 22:49:38 +01005448 // Try to keep window completely on screen.
5449 // Get position of the screen work area. This is the part that is not
5450 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 get_work_area(&workarea_rect);
5452
Bram Moolenaar734a8672019-12-02 22:49:38 +01005453 // Resizing a maximized window looks very strange, unzoom it first.
5454 // But don't do it when still starting up, it may have been requested in
5455 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005456 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005458
5459 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar734a8672019-12-02 22:49:38 +01005461 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005462 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5463 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5464 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5465 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5466 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5467 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
Bram Moolenaar734a8672019-12-02 22:49:38 +01005469 // The following should take care of keeping Vim on the same monitor, no
5470 // matter if the secondary monitor is left or right of the primary
5471 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005472 window_rect.right = window_rect.left + win_width;
5473 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005474
Bram Moolenaar734a8672019-12-02 22:49:38 +01005475 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005476 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5477 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005479 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5480 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005482 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5483 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005485 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5486 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005488 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5489 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 SetActiveWindow(s_hwnd);
5492 SetFocus(s_hwnd);
5493
Bram Moolenaar734a8672019-12-02 22:49:38 +01005494 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
5498
5499 void
5500gui_mch_set_scrollbar_thumb(
5501 scrollbar_T *sb,
5502 long val,
5503 long size,
5504 long max)
5505{
5506 SCROLLINFO info;
5507
5508 sb->scroll_shift = 0;
5509 while (max > 32767)
5510 {
5511 max = (max + 1) >> 1;
5512 val >>= 1;
5513 size >>= 1;
5514 ++sb->scroll_shift;
5515 }
5516
5517 if (sb->scroll_shift > 0)
5518 ++size;
5519
5520 info.cbSize = sizeof(info);
5521 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5522 info.nPos = val;
5523 info.nMin = 0;
5524 info.nMax = max;
5525 info.nPage = size;
5526 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5527}
5528
5529
5530/*
5531 * Set the current text font.
5532 */
5533 void
5534gui_mch_set_font(GuiFont font)
5535{
5536 gui.currFont = font;
5537}
5538
5539
5540/*
5541 * Set the current text foreground color.
5542 */
5543 void
5544gui_mch_set_fg_color(guicolor_T color)
5545{
5546 gui.currFgColor = color;
5547}
5548
5549/*
5550 * Set the current text background color.
5551 */
5552 void
5553gui_mch_set_bg_color(guicolor_T color)
5554{
5555 gui.currBgColor = color;
5556}
5557
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005558/*
5559 * Set the current text special color.
5560 */
5561 void
5562gui_mch_set_sp_color(guicolor_T color)
5563{
5564 gui.currSpColor = color;
5565}
5566
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005567#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568/*
5569 * Multi-byte handling, originally by Sung-Hoon Baek.
5570 * First static functions (no prototypes generated).
5571 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005572# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005573# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005574# endif
5575# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 * handle WM_IME_NOTIFY message
5579 */
5580 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005581_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 LRESULT lResult = 0;
5584 HIMC hImc;
5585
5586 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5587 return lResult;
5588 switch (dwCommand)
5589 {
5590 case IMN_SETOPENSTATUS:
5591 if (pImmGetOpenStatus(hImc))
5592 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005593 LOGFONTW lf = norm_logfont;
5594 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5595 // Work around when PerMonitorV2 is not enabled in the process level.
5596 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5597 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 im_set_position(gui.row, gui.col);
5599
Bram Moolenaar734a8672019-12-02 22:49:38 +01005600 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 State &= ~LANGMAP;
5602 if (State & INSERT)
5603 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005604# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005605 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5607 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005608 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 int old_row = gui.row;
5610 int old_col = gui.col;
5611
5612 // This must be called here before
5613 // status_redraw_curbuf(), otherwise the mode
5614 // message may appear in the wrong position.
5615 showmode();
5616 status_redraw_curbuf();
5617 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005618 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 gui.row = old_row;
5620 gui.col = old_col;
5621 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624 }
5625 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005626 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 lResult = 0;
5628 break;
5629 }
5630 pImmReleaseContext(hWnd, hImc);
5631 return lResult;
5632}
5633
5634 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005635_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 char_u *ret;
5638 int len;
5639
Bram Moolenaar734a8672019-12-02 22:49:38 +01005640 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 return 0;
5642
5643 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5644 if (ret != NULL)
5645 {
5646 add_to_input_buf_csi(ret, len);
5647 vim_free(ret);
5648 return 1;
5649 }
5650 return 0;
5651}
5652
5653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 * void GetResultStr()
5655 *
5656 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5657 * get complete composition string
5658 */
5659 static char_u *
5660GetResultStr(HWND hwnd, int GCS, int *lenp)
5661{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005662 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005663 LONG ret;
5664 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 char_u *convbuf = NULL;
5666
5667 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5668 return NULL;
5669
K.Takatab0b2b732022-01-19 12:59:21 +00005670 // Get the length of the composition string.
5671 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5672 if (ret <= 0)
5673 return NULL;
5674
5675 // Allocate the requested buffer plus space for the NUL character.
5676 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (buf == NULL)
5678 return NULL;
5679
K.Takatab0b2b732022-01-19 12:59:21 +00005680 // Reads in the composition string.
5681 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5682 *lenp = ret / sizeof(WCHAR);
5683
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005684 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 pImmReleaseContext(hwnd, hIMC);
5686 vim_free(buf);
5687 return convbuf;
5688}
5689#endif
5690
Bram Moolenaar734a8672019-12-02 22:49:38 +01005691// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005692#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694/*
5695 * set font to IM.
5696 */
5697 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005698im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 HIMC hImc;
5701
5702 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5703 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005704 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 pImmReleaseContext(s_hwnd, hImc);
5706 }
5707}
5708
5709/*
5710 * Notify cursor position to IM.
5711 */
5712 void
5713im_set_position(int row, int col)
5714{
5715 HIMC hImc;
5716
5717 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5718 {
5719 COMPOSITIONFORM cfs;
5720
5721 cfs.dwStyle = CFS_POINT;
5722 cfs.ptCurrentPos.x = FILL_X(col);
5723 cfs.ptCurrentPos.y = FILL_Y(row);
5724 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005725 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5726 {
5727 // Work around when PerMonitorV2 is not enabled in the process level.
5728 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5729 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 pImmSetCompositionWindow(hImc, &cfs);
5732
5733 pImmReleaseContext(s_hwnd, hImc);
5734 }
5735}
5736
5737/*
5738 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5739 */
5740 void
5741im_set_active(int active)
5742{
5743 HIMC hImc;
5744 static HIMC hImcOld = (HIMC)0;
5745
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005746# ifdef VIMDLL
5747 if (!gui.in_use && !gui.starting)
5748 {
5749 mbyte_im_set_active(active);
5750 return;
5751 }
5752# endif
5753
Bram Moolenaar734a8672019-12-02 22:49:38 +01005754 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 {
5756 if (p_imdisable)
5757 {
5758 if (hImcOld == (HIMC)0)
5759 {
5760 hImcOld = pImmGetContext(s_hwnd);
5761 if (hImcOld)
5762 pImmAssociateContext(s_hwnd, (HIMC)0);
5763 }
5764 active = FALSE;
5765 }
5766 else if (hImcOld != (HIMC)0)
5767 {
5768 pImmAssociateContext(s_hwnd, hImcOld);
5769 hImcOld = (HIMC)0;
5770 }
5771
5772 hImc = pImmGetContext(s_hwnd);
5773 if (hImc)
5774 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005775 /*
5776 * for Korean ime
5777 */
5778 HKL hKL = GetKeyboardLayout(0);
5779
5780 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5781 {
5782 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5783 static BOOL bSaved = FALSE;
5784
5785 if (active)
5786 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005787 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005788 if (bSaved)
5789 pImmSetConversionStatus(hImc, dwConversionSaved,
5790 dwSentenceSaved);
5791 bSaved = FALSE;
5792 }
5793 else
5794 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005795 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005796 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5797 &dwSentenceSaved))
5798 {
5799 bSaved = TRUE;
5800 pImmSetConversionStatus(hImc,
5801 dwConversionSaved & ~(IME_CMODE_NATIVE
5802 | IME_CMODE_FULLSHAPE),
5803 dwSentenceSaved);
5804 }
5805 }
5806 }
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 pImmSetOpenStatus(hImc, active);
5809 pImmReleaseContext(s_hwnd, hImc);
5810 }
5811 }
5812}
5813
5814/*
5815 * Get IM status. When IM is on, return not 0. Else return 0.
5816 */
5817 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005818im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 int status = 0;
5821 HIMC hImc;
5822
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005823# ifdef VIMDLL
5824 if (!gui.in_use && !gui.starting)
5825 return mbyte_im_get_status();
5826# endif
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5829 {
5830 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5831 pImmReleaseContext(s_hwnd, hImc);
5832 }
5833 return status;
5834}
5835
Bram Moolenaar734a8672019-12-02 22:49:38 +01005836#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005839/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005840 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005841 */
5842 static void
5843latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5844{
5845 int c;
5846
Bram Moolenaarca003e12006-03-17 23:19:38 +00005847 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005848 {
5849 c = *text++;
5850 switch (c)
5851 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005852 case 0xa4: c = 0x20ac; break; // euro
5853 case 0xa6: c = 0x0160; break; // S hat
5854 case 0xa8: c = 0x0161; break; // S -hat
5855 case 0xb4: c = 0x017d; break; // Z hat
5856 case 0xb8: c = 0x017e; break; // Z -hat
5857 case 0xbc: c = 0x0152; break; // OE
5858 case 0xbd: c = 0x0153; break; // oe
5859 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005860 }
5861 *unicodebuf++ = c;
5862 }
5863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865#ifdef FEAT_RIGHTLEFT
5866/*
5867 * What is this for? In the case where you are using Win98 or Win2K or later,
5868 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5869 * reverses the string sent to the TextOut... family. This sucks, because we
5870 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5871 * way to tell Windblows not to do this!
5872 *
5873 * The short of it is that this 'RevOut' only gets called if you are running
5874 * one of the new, "improved" MS OSes, and only if you are running in
5875 * 'rightleft' mode. It makes display take *slightly* longer, but not
5876 * noticeably so.
5877 */
5878 static void
K.Takata135e1522022-01-29 15:27:58 +00005879RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 int col,
5881 int row,
5882 UINT foptions,
5883 CONST RECT *pcliprect,
5884 LPCTSTR text,
5885 UINT len,
5886 CONST INT *padding)
5887{
5888 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005890 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005891 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005892 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893}
5894#endif
5895
Bram Moolenaar92467d32017-12-05 13:22:16 +01005896 static void
5897draw_line(
5898 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005899 int y1,
5900 int x2,
5901 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005902 COLORREF color)
5903{
5904#if defined(FEAT_DIRECTX)
5905 if (IS_ENABLE_DIRECTX())
5906 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5907 else
5908#endif
5909 {
5910 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5911 HPEN old_pen = SelectObject(s_hdc, hpen);
5912 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005913 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005914 LineTo(s_hdc, x2, y2);
5915 DeleteObject(SelectObject(s_hdc, old_pen));
5916 }
5917}
5918
5919 static void
5920set_pixel(
5921 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005922 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005923 COLORREF color)
5924{
5925#if defined(FEAT_DIRECTX)
5926 if (IS_ENABLE_DIRECTX())
5927 DWriteContext_SetPixel(s_dwc, x, y, color);
5928 else
5929#endif
5930 SetPixel(s_hdc, x, y, color);
5931}
5932
5933 static void
5934fill_rect(
5935 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005936 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005937 COLORREF color)
5938{
5939#if defined(FEAT_DIRECTX)
5940 if (IS_ENABLE_DIRECTX())
5941 DWriteContext_FillRect(s_dwc, rcp, color);
5942 else
5943#endif
5944 {
5945 HBRUSH hbr2;
5946
5947 if (hbr == NULL)
5948 hbr2 = CreateSolidBrush(color);
5949 else
5950 hbr2 = hbr;
5951 FillRect(s_hdc, rcp, hbr2);
5952 if (hbr == NULL)
5953 DeleteBrush(hbr2);
5954 }
5955}
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 void
5958gui_mch_draw_string(
5959 int row,
5960 int col,
5961 char_u *text,
5962 int len,
5963 int flags)
5964{
5965 static int *padding = NULL;
5966 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 const RECT *pcliprect = NULL;
5968 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 static WCHAR *unicodebuf = NULL;
5970 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005971 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 int y;
5974
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 /*
5976 * Italic and bold text seems to have an extra row of pixels at the bottom
5977 * (below where the bottom of the character should be). If we draw the
5978 * characters with a solid background, the top row of pixels in the
5979 * character below will be overwritten. We can fix this by filling in the
5980 * background ourselves, to the correct character proportions, and then
5981 * writing the character in transparent mode. Still have a problem when
5982 * the character is "_", which gets written on to the character below.
5983 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5984 * pixel in their slots, which fixes the problem with the bottom row of
5985 * pixels. We still need this code because otherwise the top row of pixels
5986 * becomes a problem. - webb.
5987 */
5988 static HBRUSH hbr_cache[2] = {NULL, NULL};
5989 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5990 static int brush_lru = 0;
5991 HBRUSH hbr;
5992 RECT rc;
5993
5994 if (!(flags & DRAW_TRANSP))
5995 {
5996 /*
5997 * Clear background first.
5998 * Note: FillRect() excludes right and bottom of rectangle.
5999 */
6000 rc.left = FILL_X(col);
6001 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (has_mbyte)
6003 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006004 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006005 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
6007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 rc.right = FILL_X(col + len);
6009 rc.bottom = FILL_Y(row + 1);
6010
Bram Moolenaar734a8672019-12-02 22:49:38 +01006011 // Cache the created brush, that saves a lot of time. We need two:
6012 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 if (gui.currBgColor == brush_color[0])
6014 {
6015 hbr = hbr_cache[0];
6016 brush_lru = 1;
6017 }
6018 else if (gui.currBgColor == brush_color[1])
6019 {
6020 hbr = hbr_cache[1];
6021 brush_lru = 0;
6022 }
6023 else
6024 {
6025 if (hbr_cache[brush_lru] != NULL)
6026 DeleteBrush(hbr_cache[brush_lru]);
6027 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6028 brush_color[brush_lru] = gui.currBgColor;
6029 hbr = hbr_cache[brush_lru];
6030 brush_lru = !brush_lru;
6031 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006032
Bram Moolenaar92467d32017-12-05 13:22:16 +01006033 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035 SetBkMode(s_hdc, TRANSPARENT);
6036
6037 /*
6038 * When drawing block cursor, prevent inverted character spilling
6039 * over character cell (can happen with bold/italic)
6040 */
6041 if (flags & DRAW_CURSOR)
6042 {
6043 pcliprect = &rc;
6044 foptions = ETO_CLIPPED;
6045 }
6046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 SetTextColor(s_hdc, gui.currFgColor);
6048 SelectFont(s_hdc, gui.currFont);
6049
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006050#ifdef FEAT_DIRECTX
6051 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006052 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006053#endif
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6056 {
K.Takata135e1522022-01-29 15:27:58 +00006057 int i;
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 vim_free(padding);
6060 pad_size = Columns;
6061
Bram Moolenaar734a8672019-12-02 22:49:38 +01006062 // Don't give an out-of-memory message here, it would call us
6063 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006064 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 if (padding != NULL)
6066 for (i = 0; i < pad_size; i++)
6067 padding[i] = gui.char_width;
6068 }
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 /*
6071 * We have to provide the padding argument because italic and bold versions
6072 * of fixed-width fonts are often one pixel or so wider than their normal
6073 * versions.
6074 * No check for DRAW_BOLD, Windows will have done it already.
6075 */
6076
Bram Moolenaar734a8672019-12-02 22:49:38 +01006077 // Check if there are any UTF-8 characters. If not, use normal text
6078 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (enc_utf8)
6080 for (n = 0; n < len; ++n)
6081 if (text[n] >= 0x80)
6082 break;
6083
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006084#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006085 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6086 // required that unicode drawing routine, currently. So this forces it
6087 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006088 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006089 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006090#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006091
Bram Moolenaar734a8672019-12-02 22:49:38 +01006092 // Check if the Unicode buffer exists and is big enough. Create it
6093 // with the same length as the multi-byte string, the number of wide
6094 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006095 if ((enc_utf8
6096 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6097 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 && (unicodebuf == NULL || len > unibuflen))
6099 {
6100 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006101 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006104 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 unibuflen = len;
6107 }
6108
6109 if (enc_utf8 && n < len && unicodebuf != NULL)
6110 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006111 // Output UTF-8 characters. Composing characters should be
6112 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006113 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006114 int wlen; // string length in words
6115 int clen; // string length in characters
6116 int cells; // cell width of string up to composing char
6117 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006118 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006120 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006121 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006123 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006125 c = utf_ptr2char(text + i);
6126 if (c >= 0x10000)
6127 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006128 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006129 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6130 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006131 }
6132 else
6133 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006134 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006135 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006136
6137 if (utf_iscomposing(c))
6138 cw = 0;
6139 else
6140 {
6141 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006142 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006143 cw = 1;
6144 }
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (unicodepdy != NULL)
6147 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006148 // Use unicodepdy to make characters fit as we expect, even
6149 // when the font uses different widths (e.g., bold character
6150 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006151 if (c >= 0x10000)
6152 {
6153 unicodepdy[wlen - 2] = cw * gui.char_width;
6154 unicodepdy[wlen - 1] = 0;
6155 }
6156 else
6157 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006160 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006161 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006163#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006164 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006165 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006166 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006167 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006168 TEXT_X(col), TEXT_Y(row),
6169 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006170 gui.char_width, gui.currFgColor,
6171 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006172 }
6173 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006174#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006175 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6176 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006177 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006179 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006181 // If we want to display codepage data, and the current CP is not the
6182 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (unicodebuf != NULL)
6184 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006185 if (enc_latin9)
6186 latin9_to_ucs(text, len, unicodebuf);
6187 else
6188 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 MB_PRECOMPOSED,
6190 (char *)text, len,
6191 (LPWSTR)unicodebuf, unibuflen);
6192 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006193 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006194 // Use unicodepdy to make characters fit as we expect, even
6195 // when the font uses different widths (e.g., bold character
6196 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006197 if (unicodepdy != NULL)
6198 {
6199 int i;
6200 int cw;
6201
6202 for (i = 0; i < len; ++i)
6203 {
6204 cw = utf_char2cells(unicodebuf[i]);
6205 if (cw > 2)
6206 cw = 1;
6207 unicodepdy[i] = cw * gui.char_width;
6208 }
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006211 foptions, pcliprect, unicodebuf, len, unicodepdy);
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
6214 }
6215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
6217#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006218 // Windows will mess up RL text, so we have to draw it character by
6219 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006220 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6222 foptions, pcliprect, (char *)text, len, padding);
6223 else
6224#endif
6225 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6226 foptions, pcliprect, (char *)text, len, padding);
6227 }
6228
Bram Moolenaar734a8672019-12-02 22:49:38 +01006229 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (flags & DRAW_UNDERL)
6231 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // When p_linespace is 0, overwrite the bottom row of pixels.
6233 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (p_linespace > 1)
6236 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006237 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006239
Bram Moolenaar734a8672019-12-02 22:49:38 +01006240 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006241 if (flags & DRAW_STRIKE)
6242 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006243 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006244 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006245 }
6246
Bram Moolenaar734a8672019-12-02 22:49:38 +01006247 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006248 if (flags & DRAW_UNDERC)
6249 {
6250 int x;
6251 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006252 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006253
6254 y = FILL_Y(row + 1) - 1;
6255 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6256 {
6257 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006258 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006259 }
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
6262
6263
6264/*
6265 * Output routines.
6266 */
6267
Bram Moolenaar734a8672019-12-02 22:49:38 +01006268/*
6269 * Flush any output to the screen
6270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 void
6272gui_mch_flush(void)
6273{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006274#if defined(FEAT_DIRECTX)
6275 if (IS_ENABLE_DIRECTX())
6276 DWriteContext_Flush(s_dwc);
6277#endif
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 GdiFlush();
6280}
6281
6282 static void
6283clear_rect(RECT *rcp)
6284{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006285 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286}
6287
6288
Bram Moolenaarc716c302006-01-21 22:12:51 +00006289 void
6290gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6291{
6292 RECT workarea_rect;
6293
6294 get_work_area(&workarea_rect);
6295
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006296 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006297 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6298 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006299
Bram Moolenaar734a8672019-12-02 22:49:38 +01006300 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6301 // the menubar for MSwin, we subtract it from the screen height, so that
6302 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006303 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006304 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6305 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6306 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6307 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006308}
6309
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#if defined(FEAT_MENU) || defined(PROTO)
6312/*
6313 * Add a sub menu to the menu bar.
6314 */
6315 void
6316gui_mch_add_menu(
6317 vimmenu_T *menu,
6318 int pos)
6319{
6320 vimmenu_T *parent = menu->parent;
6321
6322 menu->submenu_id = CreatePopupMenu();
6323 menu->id = s_menu_id++;
6324
6325 if (menu_is_menubar(menu->name))
6326 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006327 WCHAR *wn;
6328 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006330 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006331 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006332 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006334 infow.cbSize = sizeof(infow);
6335 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6336 | MIIM_SUBMENU;
6337 infow.dwItemData = (long_u)menu;
6338 infow.wID = menu->id;
6339 infow.fType = MFT_STRING;
6340 infow.dwTypeData = wn;
6341 infow.cch = (UINT)wcslen(wn);
6342 infow.hSubMenu = menu->submenu_id;
6343 InsertMenuItemW((parent == NULL)
6344 ? s_menuBar : parent->submenu_id,
6345 (UINT)pos, TRUE, &infow);
6346 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
6348
Bram Moolenaar734a8672019-12-02 22:49:38 +01006349 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 if (parent == NULL)
6351 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006352# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 else if (IsWindow(parent->tearoff_handle))
6354 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
6358 void
6359gui_mch_show_popupmenu(vimmenu_T *menu)
6360{
6361 POINT mp;
6362
K.Takata45f9cfb2022-01-21 11:11:00 +00006363 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6365}
6366
6367 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006368gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 vimmenu_T *menu = gui_find_menu(path_name);
6371
6372 if (menu != NULL)
6373 {
6374 POINT p;
6375
Bram Moolenaar734a8672019-12-02 22:49:38 +01006376 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006378 if (mouse_pos)
6379 {
6380 int mx, my;
6381
6382 gui_mch_getmouse(&mx, &my);
6383 p.x += mx;
6384 p.y += my;
6385 }
6386 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006388 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6390 }
6391 msg_scroll = FALSE;
6392 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6393 }
6394}
6395
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006396# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397/*
6398 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6399 * create it as a pseudo-"tearoff menu".
6400 */
6401 void
6402gui_make_tearoff(char_u *path_name)
6403{
6404 vimmenu_T *menu = gui_find_menu(path_name);
6405
Bram Moolenaar734a8672019-12-02 22:49:38 +01006406 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (menu != NULL)
6408 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6409}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
6412/*
6413 * Add a menu item to a menu
6414 */
6415 void
6416gui_mch_add_menu_item(
6417 vimmenu_T *menu,
6418 int idx)
6419{
6420 vimmenu_T *parent = menu->parent;
6421
6422 menu->id = s_menu_id++;
6423 menu->submenu_id = NULL;
6424
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006425# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6427 {
6428 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6429 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6430 }
6431 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006432# endif
6433# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (menu_is_toolbar(parent->name))
6435 {
6436 TBBUTTON newtb;
6437
Bram Moolenaara80faa82020-04-12 19:37:17 +02006438 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (menu_is_separator(menu->name))
6440 {
6441 newtb.iBitmap = 0;
6442 newtb.fsStyle = TBSTYLE_SEP;
6443 }
6444 else
6445 {
6446 newtb.iBitmap = get_toolbar_bitmap(menu);
6447 newtb.fsStyle = TBSTYLE_BUTTON;
6448 }
6449 newtb.idCommand = menu->id;
6450 newtb.fsState = TBSTATE_ENABLED;
6451 newtb.iString = 0;
6452 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6453 (LPARAM)&newtb);
6454 menu->submenu_id = (HMENU)-1;
6455 }
6456 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006459 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006461 wn = enc_to_utf16(menu->name, NULL);
6462 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006464 InsertMenuW(parent->submenu_id, (UINT)idx,
6465 (menu_is_separator(menu->name)
6466 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6467 (UINT)menu->id, wn);
6468 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006470# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (IsWindow(parent->tearoff_handle))
6472 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475}
6476
6477/*
6478 * Destroy the machine specific menu widget.
6479 */
6480 void
6481gui_mch_destroy_menu(vimmenu_T *menu)
6482{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006483# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 /*
6485 * is this a toolbar button?
6486 */
6487 if (menu->submenu_id == (HMENU)-1)
6488 {
6489 int iButton;
6490
6491 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6492 (WPARAM)menu->id, 0);
6493 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6494 }
6495 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 {
6498 if (menu->parent != NULL
6499 && menu_is_popup(menu->parent->dname)
6500 && menu->parent->submenu_id != NULL)
6501 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6502 else
6503 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6504 if (menu->submenu_id != NULL)
6505 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006506# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (IsWindow(menu->tearoff_handle))
6508 DestroyWindow(menu->tearoff_handle);
6509 if (menu->parent != NULL
6510 && menu->parent->children != NULL
6511 && IsWindow(menu->parent->tearoff_handle))
6512 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006513 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 menu->modes = 0;
6515 rebuild_tearoff(menu->parent);
6516 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519}
6520
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006521# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 static void
6523rebuild_tearoff(vimmenu_T *menu)
6524{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006525 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 char_u tbuf[128];
6527 RECT trect;
6528 RECT rct;
6529 RECT roct;
6530 int x, y;
6531
6532 HWND thwnd = menu->tearoff_handle;
6533
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006534 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (GetWindowRect(thwnd, &trect)
6536 && GetWindowRect(s_hwnd, &rct)
6537 && GetClientRect(s_hwnd, &roct))
6538 {
6539 x = trect.left - rct.left;
6540 y = (trect.top - rct.bottom + roct.bottom);
6541 }
6542 else
6543 {
6544 x = y = 0xffffL;
6545 }
6546 DestroyWindow(thwnd);
6547 if (menu->children != NULL)
6548 {
6549 gui_mch_tearoff(tbuf, menu, x, y);
6550 if (IsWindow(menu->tearoff_handle))
6551 (void) SetWindowPos(menu->tearoff_handle,
6552 NULL,
6553 (int)trect.left,
6554 (int)trect.top,
6555 0, 0,
6556 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6557 }
6558}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006559# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561/*
6562 * Make a menu either grey or not grey.
6563 */
6564 void
6565gui_mch_menu_grey(
6566 vimmenu_T *menu,
6567 int grey)
6568{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006569# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 /*
6571 * is this a toolbar button?
6572 */
6573 if (menu->submenu_id == (HMENU)-1)
6574 {
6575 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006576 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 }
6578 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006579# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006580 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6581 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006583# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6585 {
6586 WORD menuID;
6587 HWND menuHandle;
6588
6589 /*
6590 * A tearoff button has changed state.
6591 */
6592 if (menu->children == NULL)
6593 menuID = (WORD)(menu->id);
6594 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006595 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6597 if (menuHandle)
6598 EnableWindow(menuHandle, !grey);
6599
6600 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602}
6603
Bram Moolenaar734a8672019-12-02 22:49:38 +01006604#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605
6606
Bram Moolenaar734a8672019-12-02 22:49:38 +01006607// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006610#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
6612#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6613/*
6614 * stuff for dialogs
6615 */
6616
6617/*
6618 * The callback routine used by all the dialogs. Very simple. First,
6619 * acknowledges the INITDIALOG message so that Windows knows to do standard
6620 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6621 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6622 * number.
6623 */
6624 static LRESULT CALLBACK
6625dialog_callback(
6626 HWND hwnd,
6627 UINT message,
6628 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006629 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 if (message == WM_INITDIALOG)
6632 {
6633 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006634 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 (void)SetFocus(hwnd);
6636 if (dialog_default_button > IDCANCEL)
6637 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006638 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006639 // We don't have a default, set focus on another element of the
6640 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006641 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 return FALSE;
6643 }
6644
6645 if (message == WM_COMMAND)
6646 {
6647 int button = LOWORD(wParam);
6648
Bram Moolenaar734a8672019-12-02 22:49:38 +01006649 // Don't end the dialog if something was selected that was
6650 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (button >= DLG_NONBUTTON_CONTROL)
6652 return TRUE;
6653
Bram Moolenaar734a8672019-12-02 22:49:38 +01006654 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006656 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006657 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006658 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006659
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006660 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6661 p = utf16_to_enc(wp, NULL);
6662 vim_strncpy(s_textfield, p, IOSIZE);
6663 vim_free(p);
6664 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /*
6668 * Need to check for IDOK because if the user just hits Return to
6669 * accept the default value, some reason this is what we get.
6670 */
6671 if (button == IDOK)
6672 {
6673 if (dialog_default_button > IDCANCEL)
6674 EndDialog(hwnd, dialog_default_button);
6675 }
6676 else
6677 EndDialog(hwnd, button - IDCANCEL);
6678 return TRUE;
6679 }
6680
6681 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6682 {
6683 EndDialog(hwnd, 0);
6684 return TRUE;
6685 }
6686 return FALSE;
6687}
6688
6689/*
6690 * Create a dialog dynamically from the parameter strings.
6691 * type = type of dialog (question, alert, etc.)
6692 * title = dialog title. may be NULL for default title.
6693 * message = text to display. Dialog sizes to accommodate it.
6694 * buttons = '\n' separated list of button captions, default first.
6695 * dfltbutton = number of default button.
6696 *
6697 * This routine returns 1 if the first button is pressed,
6698 * 2 for the second, etc.
6699 *
6700 * 0 indicates Esc was pressed.
6701 * -1 for unexpected error
6702 *
6703 * If stubbing out this fn, return 1.
6704 */
6705
Bram Moolenaar734a8672019-12-02 22:49:38 +01006706static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 "IDR_VIM",
6709 "IDR_VIM_ERROR",
6710 "IDR_VIM_ALERT",
6711 "IDR_VIM_INFO",
6712 "IDR_VIM_QUESTION"
6713};
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 int
6716gui_mch_dialog(
6717 int type,
6718 char_u *title,
6719 char_u *message,
6720 char_u *buttons,
6721 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006722 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006723 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006726 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 int numButtons;
6728 int *buttonWidths, *buttonPositions;
6729 int buttonYpos;
6730 int nchar, i;
6731 DWORD lStyle;
6732 int dlgwidth = 0;
6733 int dlgheight;
6734 int editboxheight;
6735 int horizWidth = 0;
6736 int msgheight;
6737 char_u *pstart;
6738 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006739 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 char_u *tbuffer;
6741 RECT rect;
6742 HWND hwnd;
6743 HDC hdc;
6744 HFONT font, oldFont;
6745 TEXTMETRIC fontInfo;
6746 int fontHeight;
6747 int textWidth, minButtonWidth, messageWidth;
6748 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006749 int maxDialogHeight;
6750 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 int vertical;
6752 int dlgPaddingX;
6753 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006754# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006755 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006757# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006758 garray_T ga;
6759 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006760 int dlg_icon_width;
6761 int dlg_icon_height;
6762 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006764# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006765 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006766# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006767 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006768# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006769 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006770 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772
Bram Moolenaar748bf032005-02-02 23:04:36 +00006773 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006774 {
6775 load_dpi_func();
6776 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006777 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006778 }
6779 else
6780 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
6782 if ((type < 0) || (type > VIM_LAST_TYPE))
6783 type = 0;
6784
Bram Moolenaar734a8672019-12-02 22:49:38 +01006785 // allocate some memory for dialog template
6786 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006787 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006788 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
6790 if (p == NULL)
6791 return -1;
6792
6793 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006794 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6796 * const.
6797 */
6798 tbuffer = vim_strsave(buttons);
6799 if (tbuffer == NULL)
6800 return -1;
6801
Bram Moolenaar734a8672019-12-02 22:49:38 +01006802 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
Bram Moolenaar734a8672019-12-02 22:49:38 +01006804 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 numButtons = 1;
6806 for (i = 0; tbuffer[i] != '\0'; i++)
6807 {
6808 if (tbuffer[i] == DLG_BUTTON_SEP)
6809 numButtons++;
6810 }
6811 if (dfltbutton >= numButtons)
6812 dfltbutton = -1;
6813
Bram Moolenaar734a8672019-12-02 22:49:38 +01006814 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006815 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (buttonWidths == NULL)
6817 return -1;
6818
Bram Moolenaar734a8672019-12-02 22:49:38 +01006819 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006820 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (buttonPositions == NULL)
6822 return -1;
6823
6824 /*
6825 * Calculate how big the dialog must be.
6826 */
6827 hwnd = GetDesktopWindow();
6828 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006829# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6831 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006832 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 use_lfSysmenu = TRUE;
6834 }
6835 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006836# endif
K.Takatad1c58992022-01-23 12:31:57 +00006837 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6838 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6839
6840 oldFont = SelectFont(hdc, font);
6841 dlgPaddingX = DLG_PADDING_X;
6842 dlgPaddingY = DLG_PADDING_Y;
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 GetTextMetrics(hdc, &fontInfo);
6845 fontHeight = fontInfo.tmHeight;
6846
Bram Moolenaar734a8672019-12-02 22:49:38 +01006847 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006848 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaar734a8672019-12-02 22:49:38 +01006850 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006851 if (s_hwnd == NULL)
6852 {
6853 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
Bram Moolenaar734a8672019-12-02 22:49:38 +01006855 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006856 get_work_area(&workarea_rect);
6857 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006858 if (maxDialogWidth > adjust_by_system_dpi(600))
6859 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006861 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006862 }
6863 else
6864 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006865 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006866 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006867 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006868 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6869 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6870 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6871 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006872
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006873 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006874 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6875 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6876 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6877 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6878 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006879 }
6880
Bram Moolenaar734a8672019-12-02 22:49:38 +01006881 // Set dlgwidth to width of message.
6882 // Copy the message into "ga", changing NL to CR-NL and inserting line
6883 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 pstart = message;
6885 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006886 msgheight = 0;
6887 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 do
6889 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006891
Bram Moolenaar734a8672019-12-02 22:49:38 +01006892 // Need to figure out where to break the string. The system does it
6893 // at a word boundary, which would mean we can't compute the number of
6894 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006895 textWidth = 0;
6896 last_white = NULL;
6897 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006898 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006899 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006900 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006901 && textWidth > maxDialogWidth * 3 / 4)
6902 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006903 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006904 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006905 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006906 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006907 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006908 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006909 textWidth = 0;
6910
6911 if (last_white != NULL)
6912 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006913 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006914 if (pend > last_white)
6915 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006916 pend = last_white + 1;
6917 last_white = NULL;
6918 }
6919 ga_append(&ga, '\r');
6920 ga_append(&ga, '\n');
6921 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006922 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006923
6924 while (--l >= 0)
6925 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006926 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929
6930 ga_append(&ga, '\r');
6931 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 pstart = pend + 1;
6933 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006934
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006935 if (ga.ga_data != NULL)
6936 message = ga.ga_data;
6937
Bram Moolenaar734a8672019-12-02 22:49:38 +01006938 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006939
K.Takatac81e9bf2022-01-16 14:15:49 +00006940 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6941 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942
K.Takatac81e9bf2022-01-16 14:15:49 +00006943 // Add width of icon to dlgwidth, and some space
6944 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6945 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6946
6947 if (msgheight < dlg_icon_height)
6948 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
6950 /*
6951 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006952 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006954 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (!vertical)
6956 {
6957 // Place buttons horizontally if they fit.
6958 horizWidth = dlgPaddingX;
6959 pstart = tbuffer;
6960 i = 0;
6961 do
6962 {
6963 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6964 if (pend == NULL)
6965 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006966 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (textWidth < minButtonWidth)
6968 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006969 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 buttonWidths[i] = textWidth;
6971 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006972 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 pstart = pend + 1;
6974 } while (*pend != NUL);
6975
6976 if (horizWidth > maxDialogWidth)
6977 vertical = TRUE; // Too wide to fit on the screen.
6978 else if (horizWidth > dlgwidth)
6979 dlgwidth = horizWidth;
6980 }
6981
6982 if (vertical)
6983 {
6984 // Stack buttons vertically.
6985 pstart = tbuffer;
6986 do
6987 {
6988 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6989 if (pend == NULL)
6990 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006991 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006992 textWidth += dlgPaddingX; // Padding within button
6993 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 if (textWidth > dlgwidth)
6995 dlgwidth = textWidth;
6996 pstart = pend + 1;
6997 } while (*pend != NUL);
6998 }
6999
7000 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007001 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
Bram Moolenaar734a8672019-12-02 22:49:38 +01007003 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007004 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 add_long(lStyle);
7007 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007008 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 add_word(0); // NumberOfItems(will change later)
7010 add_word(10); // x
7011 add_word(10); // y
7012 add_word(PixelToDialogX(dlgwidth)); // cx
7013
7014 // Dialog height.
7015 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007016 dlgheight = msgheight + 2 * dlgPaddingY
7017 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 else
7019 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7020
7021 // Dialog needs to be taller if contains an edit box.
7022 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7023 if (textfield != NULL)
7024 dlgheight += editboxheight;
7025
Bram Moolenaar734a8672019-12-02 22:49:38 +01007026 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007027 if (dlgheight > maxDialogHeight)
7028 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007029 msgheight = msgheight - (dlgheight - maxDialogHeight);
7030 dlgheight = maxDialogHeight;
7031 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007032 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007033 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007034 }
7035
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 add_word(PixelToDialogY(dlgheight));
7037
7038 add_word(0); // Menu
7039 add_word(0); // Class
7040
Bram Moolenaar734a8672019-12-02 22:49:38 +01007041 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007042 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7043 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 p += nchar;
7045
K.Takatad1c58992022-01-23 12:31:57 +00007046 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007047# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007048 if (use_lfSysmenu)
7049 {
7050 // point size
7051 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7052 GetDeviceCaps(hdc, LOGPIXELSY));
7053 wcscpy(p, lfSysmenu.lfFaceName);
7054 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
K.Takatad1c58992022-01-23 12:31:57 +00007056 else
7057# endif
7058 {
7059 *p++ = DLG_FONT_POINT_SIZE; // point size
7060 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7061 }
7062 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 buttonYpos = msgheight + 2 * dlgPaddingY;
7065
7066 if (textfield != NULL)
7067 buttonYpos += editboxheight;
7068
7069 pstart = tbuffer;
7070 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007071 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 for (i = 0; i < numButtons; i++)
7073 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007074 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 for ( pend = pstart;
7076 *pend && (*pend != DLG_BUTTON_SEP);
7077 pend++)
7078 ;
7079
7080 if (*pend)
7081 *pend = '\0';
7082
7083 /*
7084 * old NOTE:
7085 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7086 * the focus to the first tab-able button and in so doing makes that
7087 * the default!! Grrr. Workaround: Make the default button the only
7088 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7089 * he/she can use arrow keys.
7090 *
7091 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007092 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 * dialog. Also needed for when the textfield is the default control.
7094 * It appears to work now (perhaps not on Win95?).
7095 */
7096 if (vertical)
7097 {
7098 p = add_dialog_element(p,
7099 (i == dfltbutton
7100 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7101 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007102 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 + 2 * fontHeight * i),
7104 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7105 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007106 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 }
7108 else
7109 {
7110 p = add_dialog_element(p,
7111 (i == dfltbutton
7112 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7113 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007114 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 PixelToDialogX(buttonWidths[i]),
7116 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007117 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121 *pnumitems += numButtons;
7122
Bram Moolenaar734a8672019-12-02 22:49:38 +01007123 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 p = add_dialog_element(p, SS_ICON,
7125 PixelToDialogX(dlgPaddingX),
7126 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007127 PixelToDialogX(dlg_icon_width),
7128 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7130 dlg_icons[type]);
7131
Bram Moolenaar734a8672019-12-02 22:49:38 +01007132 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007133 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007134 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007135 PixelToDialogY(dlgPaddingY),
7136 (WORD)(PixelToDialogX(messageWidth) + 1),
7137 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007138 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
Bram Moolenaar734a8672019-12-02 22:49:38 +01007140 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (textfield != NULL)
7142 {
7143 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7144 PixelToDialogX(2 * dlgPaddingX),
7145 PixelToDialogY(2 * dlgPaddingY + msgheight),
7146 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7147 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007148 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 *pnumitems += 1;
7150 }
7151
7152 *pnumitems += 2;
7153
7154 SelectFont(hdc, oldFont);
7155 DeleteObject(font);
7156 ReleaseDC(hwnd, hdc);
7157
Bram Moolenaar734a8672019-12-02 22:49:38 +01007158 // Let the dialog_callback() function know which button to make default
7159 // If we have an edit box, make that the default. We also need to tell
7160 // dialog_callback() if this dialog contains an edit box or not. We do
7161 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 if (textfield != NULL)
7163 {
7164 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7165 s_textfield = textfield;
7166 }
7167 else
7168 {
7169 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7170 s_textfield = NULL;
7171 }
7172
Bram Moolenaar734a8672019-12-02 22:49:38 +01007173 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007175 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 (LPDLGTEMPLATE)pdlgtemplate,
7177 s_hwnd,
7178 (DLGPROC)dialog_callback);
7179
7180 LocalFree(LocalHandle(pdlgtemplate));
7181 vim_free(tbuffer);
7182 vim_free(buttonWidths);
7183 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007184 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaar734a8672019-12-02 22:49:38 +01007186 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 (void)SetFocus(s_hwnd);
7188
7189 return nchar;
7190}
7191
Bram Moolenaar734a8672019-12-02 22:49:38 +01007192#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194/*
7195 * Put a simple element (basic class) onto a dialog template in memory.
7196 * return a pointer to where the next item should be added.
7197 *
7198 * parameters:
7199 * lStyle = additional style flags
7200 * (be careful, NT3.51 & Win32s will ignore the new ones)
7201 * x,y = x & y positions IN DIALOG UNITS
7202 * w,h = width and height IN DIALOG UNITS
7203 * Id = ID used in messages
7204 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7205 * caption = usually text or resource name
7206 *
7207 * TODO: use the length information noted here to enable the dialog creation
7208 * routines to work out more exactly how much memory they need to alloc.
7209 */
7210 static PWORD
7211add_dialog_element(
7212 PWORD p,
7213 DWORD lStyle,
7214 WORD x,
7215 WORD y,
7216 WORD w,
7217 WORD h,
7218 WORD Id,
7219 WORD clss,
7220 const char *caption)
7221{
7222 int nchar;
7223
Bram Moolenaar734a8672019-12-02 22:49:38 +01007224 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7226 *p++ = LOWORD(lStyle);
7227 *p++ = HIWORD(lStyle);
7228 *p++ = 0; // LOWORD (lExtendedStyle)
7229 *p++ = 0; // HIWORD (lExtendedStyle)
7230 *p++ = x;
7231 *p++ = y;
7232 *p++ = w;
7233 *p++ = h;
7234 *p++ = Id; //9 or 10 words in all
7235
7236 *p++ = (WORD)0xffff;
7237 *p++ = clss; //2 more here
7238
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007239 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 p += nchar;
7241
7242 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7243
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007244 return p; // total = 15 + strlen(caption) words
7245 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246}
7247
7248
7249/*
7250 * Helper routine. Take an input pointer, return closest pointer that is
7251 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7252 */
7253 static LPWORD
7254lpwAlign(
7255 LPWORD lpIn)
7256{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007257 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007259 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 ul += 3;
7261 ul >>= 2;
7262 ul <<= 2;
7263 return (LPWORD)ul;
7264}
7265
7266/*
7267 * Helper routine. Takes second parameter as Ansi string, copies it to first
7268 * parameter as wide character (16-bits / char) string, and returns integer
7269 * number of wide characters (words) in string (including the trailing wide
7270 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007271 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7272 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 static int
7274nCopyAnsiToWideChar(
7275 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007276 LPSTR lpAnsiIn,
7277 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
7279 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007280 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 int i;
7282 WCHAR *wn;
7283
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007284 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007286 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007287 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (wn != NULL)
7289 {
7290 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007291 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 vim_free(wn);
7293 }
7294 }
7295 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007296 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 nChar = MultiByteToWideChar(
7298 enc_codepage > 0 ? enc_codepage : CP_ACP,
7299 MB_PRECOMPOSED,
7300 lpAnsiIn, len,
7301 lpWCStr, len);
7302 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007303 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305
7306 return nChar;
7307}
7308
7309
7310#ifdef FEAT_TEAROFF
7311/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007312 * Lookup menu handle from "menu_id".
7313 */
7314 static HMENU
7315tearoff_lookup_menuhandle(
7316 vimmenu_T *menu,
7317 WORD menu_id)
7318{
7319 for ( ; menu != NULL; menu = menu->next)
7320 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007321 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007322 continue;
7323 if (menu_is_separator(menu->dname))
7324 continue;
7325 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7326 return menu->submenu_id;
7327 }
7328 return NULL;
7329}
7330
7331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 * The callback function for all the modeless dialogs that make up the
7333 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7334 * thinking its menus have been clicked), and go away when closed.
7335 */
7336 static LRESULT CALLBACK
7337tearoff_callback(
7338 HWND hwnd,
7339 UINT message,
7340 WPARAM wParam,
7341 LPARAM lParam)
7342{
7343 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007344 {
7345 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
Bram Moolenaar734a8672019-12-02 22:49:38 +01007349 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 HandleMouseHide(message, lParam);
7351
7352 if (message == WM_COMMAND)
7353 {
7354 if ((WORD)(LOWORD(wParam)) & 0x8000)
7355 {
7356 POINT mp;
7357 RECT rect;
7358
7359 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7360 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007361 vimmenu_T *menu;
7362
7363 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007365 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7367 (int)rect.right - 8,
7368 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007369 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 s_hwnd,
7371 NULL);
7372 /*
7373 * NOTE: The pop-up menu can eat the mouse up event.
7374 * We deal with this in normal.c.
7375 */
7376 }
7377 }
7378 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007379 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7381 /*
7382 * Give main window the focus back: this is so after
7383 * choosing a tearoff button you can start typing again
7384 * straight away.
7385 */
7386 (void)SetFocus(s_hwnd);
7387 return TRUE;
7388 }
7389 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7390 {
7391 DestroyWindow(hwnd);
7392 return TRUE;
7393 }
7394
Bram Moolenaar734a8672019-12-02 22:49:38 +01007395 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (message == WM_EXITSIZEMOVE)
7397 (void)SetActiveWindow(s_hwnd);
7398
7399 return FALSE;
7400}
7401#endif
7402
7403
7404/*
K.Takatad1c58992022-01-23 12:31:57 +00007405 * Computes the dialog base units based on the current dialog font.
7406 * We don't use the GetDialogBaseUnits() API, because we don't use the
7407 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 */
7409 static void
7410get_dialog_font_metrics(void)
7411{
7412 HDC hdc;
7413 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 SIZE size;
7415#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007416 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#endif
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007420 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007421 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423#endif
7424 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007425 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
K.Takatad1c58992022-01-23 12:31:57 +00007427 hdc = GetDC(s_hwnd);
7428 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007429 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007430 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
K.Takataabe628e2022-01-23 16:25:17 +00007432 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007433 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
7436#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7437/*
7438 * Create a pseudo-"tearoff menu" based on the child
7439 * items of a given menu pointer.
7440 */
7441 static void
7442gui_mch_tearoff(
7443 char_u *title,
7444 vimmenu_T *menu,
7445 int initX,
7446 int initY)
7447{
7448 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7449 int template_len;
7450 int nchar, textWidth, submenuWidth;
7451 DWORD lStyle;
7452 DWORD lExtendedStyle;
7453 WORD dlgwidth;
7454 WORD menuID;
7455 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007456 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 vimmenu_T *the_menu = menu;
7458 HWND hwnd;
7459 HDC hdc;
7460 HFONT font, oldFont;
7461 int col, spaceWidth, len;
7462 int columnWidths[2];
7463 char_u *label, *text;
7464 int acLen = 0;
7465 int nameLen;
7466 int padding0, padding1, padding2 = 0;
7467 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007468 int x;
7469 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007470# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007471 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 /*
7476 * If this menu is already torn off, move it to the mouse position.
7477 */
7478 if (IsWindow(menu->tearoff_handle))
7479 {
7480 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007481 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
7483 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7484 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7485 }
7486 return;
7487 }
7488
7489 /*
7490 * Create a new tearoff.
7491 */
7492 if (*title == MNU_HIDDEN_CHAR)
7493 title++;
7494
Bram Moolenaar734a8672019-12-02 22:49:38 +01007495 // Allocate memory to store the dialog template. It's made bigger when
7496 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 template_len = DLG_ALLOC_SIZE;
7498 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7499 if (p == NULL)
7500 return;
7501
7502 hwnd = GetDesktopWindow();
7503 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007504# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7506 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007507 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 use_lfSysmenu = TRUE;
7509 }
7510 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007511# endif
K.Takatad1c58992022-01-23 12:31:57 +00007512 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7513 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7514
7515 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Bram Moolenaar734a8672019-12-02 22:49:38 +01007517 // Calculate width of a single space. Used for padding columns to the
7518 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007519 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
Bram Moolenaar734a8672019-12-02 22:49:38 +01007521 // Figure out max width of the text column, the accelerator column and the
7522 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 submenuWidth = 0;
7524 for (col = 0; col < 2; col++)
7525 {
7526 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007527 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007529 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 text = (col == 0) ? pmenu->dname : pmenu->actext;
7531 if (text != NULL && *text != NUL)
7532 {
7533 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7534 if (textWidth > columnWidths[col])
7535 columnWidths[col] = textWidth;
7536 }
7537 if (pmenu->children != NULL)
7538 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7539 }
7540 }
7541 if (columnWidths[1] == 0)
7542 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007543 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (submenuWidth != 0)
7545 columnWidths[0] += submenuWidth;
7546 else
7547 columnWidths[0] += spaceWidth;
7548 }
7549 else
7550 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007551 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7553 columnWidths[1] += submenuWidth;
7554 }
7555
7556 /*
7557 * Now find the total width of our 'menu'.
7558 */
7559 textWidth = columnWidths[0] + columnWidths[1];
7560 if (submenuWidth != 0)
7561 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007562 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7564 textWidth += submenuWidth;
7565 }
7566 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7567 if (textWidth > dlgwidth)
7568 dlgwidth = textWidth;
7569 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7570
Bram Moolenaar734a8672019-12-02 22:49:38 +01007571 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007572 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573
7574 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7575 *p++ = LOWORD(lStyle);
7576 *p++ = HIWORD(lStyle);
7577 *p++ = LOWORD(lExtendedStyle);
7578 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007579 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007581 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007583 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 else
7585 *p++ = PixelToDialogX(initX); // x
7586 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007587 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 *p++ = PixelToDialogY(initY); // y
7590 *p++ = PixelToDialogX(dlgwidth); // cx
7591 ptrueheight = p;
7592 *p++ = 0; // dialog height: changed later anyway
7593 *p++ = 0; // Menu
7594 *p++ = 0; // Class
7595
Bram Moolenaar734a8672019-12-02 22:49:38 +01007596 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007598 ? (LPSTR)title
7599 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 p += nchar;
7601
K.Takatad1c58992022-01-23 12:31:57 +00007602 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007603# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007604 if (use_lfSysmenu)
7605 {
7606 // point size
7607 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7608 GetDeviceCaps(hdc, LOGPIXELSY));
7609 wcscpy(p, lfSysmenu.lfFaceName);
7610 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
K.Takatad1c58992022-01-23 12:31:57 +00007612 else
7613# endif
7614 {
7615 *p++ = DLG_FONT_POINT_SIZE; // point size
7616 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7617 }
7618 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620 /*
7621 * Loop over all the items in the menu.
7622 * But skip over the tearbar.
7623 */
7624 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7625 menu = menu->children->next;
7626 else
7627 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007628 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 for ( ; menu != NULL; menu = menu->next)
7630 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007631 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 continue;
7633 if (menu_is_separator(menu->dname))
7634 {
7635 sepPadding += 3;
7636 continue;
7637 }
7638
Bram Moolenaar734a8672019-12-02 22:49:38 +01007639 // Check if there still is plenty of room in the template. Make it
7640 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7642 {
7643 WORD *newp;
7644
7645 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7646 if (newp != NULL)
7647 {
7648 template_len += 4096;
7649 mch_memmove(newp, pdlgtemplate,
7650 (char *)p - (char *)pdlgtemplate);
7651 p = newp + (p - pdlgtemplate);
7652 pnumitems = newp + (pnumitems - pdlgtemplate);
7653 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7654 LocalFree(LocalHandle(pdlgtemplate));
7655 pdlgtemplate = newp;
7656 }
7657 }
7658
Bram Moolenaar734a8672019-12-02 22:49:38 +01007659 // Figure out minimal length of this menu label. Use "name" for the
7660 // actual text, "dname" for estimating the displayed size. "name"
7661 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 len = nameLen = (int)STRLEN(menu->name);
7663 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7664 (int)STRLEN(menu->dname))) / spaceWidth;
7665 len += padding0;
7666
7667 if (menu->actext != NULL)
7668 {
7669 acLen = (int)STRLEN(menu->actext);
7670 len += acLen;
7671 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7672 }
7673 else
7674 textWidth = 0;
7675 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7676 len += padding1;
7677
7678 if (menu->children == NULL)
7679 {
7680 padding2 = submenuWidth / spaceWidth;
7681 len += padding2;
7682 menuID = (WORD)(menu->id);
7683 }
7684 else
7685 {
7686 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007687 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689
Bram Moolenaar734a8672019-12-02 22:49:38 +01007690 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007691 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 if (label == NULL)
7693 break;
7694
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007695 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007696 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007698 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 while (padding0-- > 0)
7700 *text++ = ' ';
7701 if (menu->actext != NULL)
7702 {
7703 STRNCPY(text, menu->actext, acLen);
7704 text += acLen;
7705 }
7706 while (padding1-- > 0)
7707 *text++ = ' ';
7708 if (menu->children != NULL)
7709 {
7710 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7711 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7712 }
7713 else
7714 {
7715 while (padding2-- > 0)
7716 *text++ = ' ';
7717 }
7718 *text = NUL;
7719
7720 /*
7721 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7722 * W95/NT4 it makes the tear-off look more like a menu.
7723 */
7724 p = add_dialog_element(p,
7725 BS_PUSHBUTTON|BS_LEFT,
7726 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7727 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7728 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7729 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007730 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 vim_free(label);
7732 (*pnumitems)++;
7733 }
7734
7735 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7736
7737
Bram Moolenaar734a8672019-12-02 22:49:38 +01007738 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007739 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007740 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 (LPDLGTEMPLATE)pdlgtemplate,
7742 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007743 (DLGPROC)tearoff_callback,
7744 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
7746 LocalFree(LocalHandle(pdlgtemplate));
7747 SelectFont(hdc, oldFont);
7748 DeleteObject(font);
7749 ReleaseDC(hwnd, hdc);
7750
7751 /*
7752 * Reassert ourselves as the active window. This is so that after creating
7753 * a tearoff, the user doesn't have to click with the mouse just to start
7754 * typing again!
7755 */
7756 (void)SetActiveWindow(s_hwnd);
7757
Bram Moolenaar734a8672019-12-02 22:49:38 +01007758 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 force_menu_update = TRUE;
7760}
7761#endif
7762
7763#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007764# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766/*
7767 * Create the toolbar, initially unpopulated.
7768 * (just like the menu, there are no defaults, it's all
7769 * set up through menu.vim)
7770 */
7771 static void
7772initialise_toolbar(void)
7773{
7774 InitCommonControls();
7775 s_toolbarhwnd = CreateToolbarEx(
7776 s_hwnd,
7777 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7778 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007779 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007780 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 IDR_TOOLBAR1, // id of initial bitmap
7782 NULL,
7783 0, // initial number of buttons
7784 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7785 TOOLBAR_BUTTON_HEIGHT,
7786 TOOLBAR_BUTTON_WIDTH,
7787 TOOLBAR_BUTTON_HEIGHT,
7788 sizeof(TBBUTTON)
7789 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007790
7791 // Remove transparency from the toolbar to prevent the main window
7792 // background colour showing through
7793 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7794 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7795
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007796 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007799
7800 update_toolbar_size();
7801}
7802
7803 static void
7804update_toolbar_size(void)
7805{
7806 int w, h;
7807 TBMETRICS tbm = {sizeof(TBMETRICS)};
7808
7809 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7810 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7811 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7812 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7813
7814 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7815 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7816 //TRACE("button size: %d, %d", w, h);
7817 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7818 gui.toolbar_height = h + 6;
7819
7820 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7821 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7822
7823 // TODO:
7824 // Currently, this function only updates the size of toolbar buttons.
7825 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826}
7827
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007828 static LRESULT CALLBACK
7829toolbar_wndproc(
7830 HWND hwnd,
7831 UINT uMsg,
7832 WPARAM wParam,
7833 LPARAM lParam)
7834{
7835 HandleMouseHide(uMsg, lParam);
7836 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7837}
7838
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 static int
7840get_toolbar_bitmap(vimmenu_T *menu)
7841{
7842 int i = -1;
7843
7844 /*
7845 * Check user bitmaps first, unless builtin is specified.
7846 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007847 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
7849 char_u fname[MAXPATHL];
7850 HANDLE hbitmap = NULL;
7851
7852 if (menu->iconfile != NULL)
7853 {
7854 gui_find_iconfile(menu->iconfile, fname, "bmp");
7855 hbitmap = LoadImage(
7856 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007857 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 IMAGE_BITMAP,
7859 TOOLBAR_BUTTON_WIDTH,
7860 TOOLBAR_BUTTON_HEIGHT,
7861 LR_LOADFROMFILE |
7862 LR_LOADMAP3DCOLORS
7863 );
7864 }
7865
7866 /*
7867 * If the LoadImage call failed, or the "icon=" file
7868 * didn't exist or wasn't specified, try the menu name
7869 */
7870 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007871 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007872# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007873 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007874# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007875 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 hbitmap = LoadImage(
7877 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007878 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 IMAGE_BITMAP,
7880 TOOLBAR_BUTTON_WIDTH,
7881 TOOLBAR_BUTTON_HEIGHT,
7882 LR_LOADFROMFILE |
7883 LR_LOADMAP3DCOLORS
7884 );
7885
7886 if (hbitmap != NULL)
7887 {
7888 TBADDBITMAP tbAddBitmap;
7889
7890 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007891 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892
7893 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7894 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007895 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897 }
7898 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7899 i = menu->iconidx;
7900
7901 return i;
7902}
7903#endif
7904
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007905#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7906 static void
7907initialise_tabline(void)
7908{
7909 InitCommonControls();
7910
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007911 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007912 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007913 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007914 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007915 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007916
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007917 gui.tabline_height = TABLINE_HEIGHT;
7918
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007919 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007920}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007921
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007922/*
7923 * Get tabpage_T from POINT.
7924 */
7925 static tabpage_T *
7926GetTabFromPoint(
7927 HWND hWnd,
7928 POINT pt)
7929{
7930 tabpage_T *ptp = NULL;
7931
7932 if (gui_mch_showing_tabline())
7933 {
7934 TCHITTESTINFO htinfo;
7935 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007936 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007937 if (s_tabhwnd == hWnd)
7938 {
7939 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7940 if (idx != -1)
7941 ptp = find_tabpage(idx + 1);
7942 }
7943 }
7944 return ptp;
7945}
7946
7947static POINT s_pt = {0, 0};
7948static HCURSOR s_hCursor = NULL;
7949
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007950 static LRESULT CALLBACK
7951tabline_wndproc(
7952 HWND hwnd,
7953 UINT uMsg,
7954 WPARAM wParam,
7955 LPARAM lParam)
7956{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007957 POINT pt;
7958 tabpage_T *tp;
7959 RECT rect;
7960 int nCenter;
7961 int idx0;
7962 int idx1;
7963
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007964 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007965
7966 switch (uMsg)
7967 {
7968 case WM_LBUTTONDOWN:
7969 {
7970 s_pt.x = GET_X_LPARAM(lParam);
7971 s_pt.y = GET_Y_LPARAM(lParam);
7972 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007973 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007974 break;
7975 }
7976 case WM_MOUSEMOVE:
7977 if (GetCapture() == hwnd
7978 && ((wParam & MK_LBUTTON)) != 0)
7979 {
7980 pt.x = GET_X_LPARAM(lParam);
7981 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007982 if (abs(pt.x - s_pt.x) >
7983 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007984 {
7985 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7986
7987 tp = GetTabFromPoint(hwnd, pt);
7988 if (tp != NULL)
7989 {
7990 idx0 = tabpage_index(curtab) - 1;
7991 idx1 = tabpage_index(tp) - 1;
7992
7993 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7994 nCenter = rect.left + (rect.right - rect.left) / 2;
7995
Bram Moolenaar734a8672019-12-02 22:49:38 +01007996 // Check if the mouse cursor goes over the center of
7997 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007998 if ((idx0 < idx1) && (nCenter < pt.x))
7999 {
8000 tabpage_move(idx1 + 1);
8001 update_screen(0);
8002 }
8003 else if ((idx1 < idx0) && (pt.x < nCenter))
8004 {
8005 tabpage_move(idx1);
8006 update_screen(0);
8007 }
8008 }
8009 }
8010 }
8011 break;
8012 case WM_LBUTTONUP:
8013 {
8014 if (GetCapture() == hwnd)
8015 {
8016 SetCursor(s_hCursor);
8017 ReleaseCapture();
8018 }
8019 break;
8020 }
8021 default:
8022 break;
8023 }
8024
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008025 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8026}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008027#endif
8028
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8030/*
8031 * Make the GUI window come to the foreground.
8032 */
8033 void
8034gui_mch_set_foreground(void)
8035{
8036 if (IsIconic(s_hwnd))
8037 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8038 SetForegroundWindow(s_hwnd);
8039}
8040#endif
8041
8042#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8043 static void
8044dyn_imm_load(void)
8045{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008046 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (hLibImm == NULL)
8048 return;
8049
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 pImmGetCompositionStringW
8051 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8052 pImmGetContext
8053 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8054 pImmAssociateContext
8055 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8056 pImmReleaseContext
8057 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8058 pImmGetOpenStatus
8059 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8060 pImmSetOpenStatus
8061 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008062 pImmGetCompositionFontW
8063 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8064 pImmSetCompositionFontW
8065 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 pImmSetCompositionWindow
8067 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8068 pImmGetConversionStatus
8069 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008070 pImmSetConversionStatus
8071 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
K.Takatab0b2b732022-01-19 12:59:21 +00008073 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 || pImmGetContext == NULL
8075 || pImmAssociateContext == NULL
8076 || pImmReleaseContext == NULL
8077 || pImmGetOpenStatus == NULL
8078 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008079 || pImmGetCompositionFontW == NULL
8080 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008082 || pImmGetConversionStatus == NULL
8083 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {
8085 FreeLibrary(hLibImm);
8086 hLibImm = NULL;
8087 pImmGetContext = NULL;
8088 return;
8089 }
8090
8091 return;
8092}
8093
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#endif
8095
8096#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8097
8098# ifdef FEAT_XPM_W32
8099# define IMAGE_XPM 100
8100# endif
8101
8102typedef struct _signicon_t
8103{
8104 HANDLE hImage;
8105 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008106# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008107 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109} signicon_t;
8110
8111 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008112gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113{
8114 signicon_t *sign;
8115 int x, y, w, h;
8116
8117 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8118 return;
8119
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008120# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008121 if (IS_ENABLE_DIRECTX())
8122 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008123# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008124
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 x = TEXT_X(col);
8126 y = TEXT_Y(row);
8127 w = gui.char_width * 2;
8128 h = gui.char_height;
8129 switch (sign->uType)
8130 {
8131 case IMAGE_BITMAP:
8132 {
8133 HDC hdcMem;
8134 HBITMAP hbmpOld;
8135
8136 hdcMem = CreateCompatibleDC(s_hdc);
8137 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8138 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8139 SelectObject(hdcMem, hbmpOld);
8140 DeleteDC(hdcMem);
8141 }
8142 break;
8143 case IMAGE_ICON:
8144 case IMAGE_CURSOR:
8145 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8146 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008147# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 case IMAGE_XPM:
8149 {
8150 HDC hdcMem;
8151 HBITMAP hbmpOld;
8152
8153 hdcMem = CreateCompatibleDC(s_hdc);
8154 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008155 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8157
8158 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008159 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8161 SelectObject(hdcMem, hbmpOld);
8162 DeleteDC(hdcMem);
8163 }
8164 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
8167}
8168
8169 static void
8170close_signicon_image(signicon_t *sign)
8171{
8172 if (sign)
8173 switch (sign->uType)
8174 {
8175 case IMAGE_BITMAP:
8176 DeleteObject((HGDIOBJ)sign->hImage);
8177 break;
8178 case IMAGE_CURSOR:
8179 DestroyCursor((HCURSOR)sign->hImage);
8180 break;
8181 case IMAGE_ICON:
8182 DestroyIcon((HICON)sign->hImage);
8183 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008184# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 case IMAGE_XPM:
8186 DeleteObject((HBITMAP)sign->hImage);
8187 DeleteObject((HBITMAP)sign->hShape);
8188 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 }
8191}
8192
8193 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008194gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 signicon_t sign, *psign;
8197 char_u *ext;
8198
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008200 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (ext > signfile)
8202 {
8203 int do_load = 1;
8204
8205 if (!STRICMP(ext, ".bmp"))
8206 sign.uType = IMAGE_BITMAP;
8207 else if (!STRICMP(ext, ".ico"))
8208 sign.uType = IMAGE_ICON;
8209 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8210 sign.uType = IMAGE_CURSOR;
8211 else
8212 do_load = 0;
8213
8214 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008215 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 gui.char_width * 2, gui.char_height,
8217 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008218# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 if (!STRICMP(ext, ".xpm"))
8220 {
8221 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008222 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8223 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227
8228 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008229 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 *psign = sign;
8231
8232 if (!psign)
8233 {
8234 if (sign.hImage)
8235 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008236 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
8238 return (void *)psign;
8239
8240}
8241
8242 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008243gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 if (sign)
8246 {
8247 close_signicon_image((signicon_t *)sign);
8248 vim_free(sign);
8249 }
8250}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008253#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
Bram Moolenaar734a8672019-12-02 22:49:38 +01008255/*
8256 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008257 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008259 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8261 * to get current mouse position).
8262 *
8263 * Trying to use as more Windows services as possible, and as less
8264 * IE version as possible :)).
8265 *
8266 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8267 * BalloonEval struct.
8268 * 2) Enable/Disable simply create/kill BalloonEval Timer
8269 * 3) When there was enough inactivity, timer procedure posts
8270 * async request to debugger
8271 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8272 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008273 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 */
8275
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008276 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008277make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008278{
K.Takata76687d22022-01-25 10:31:37 +00008279 TOOLINFOW *pti;
8280 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008281
K.Takata76687d22022-01-25 10:31:37 +00008282 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008283 if (pti == NULL)
8284 return;
8285
8286 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8287 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8288 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008289 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008290
8291 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8292 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8293
K.Takata76687d22022-01-25 10:31:37 +00008294 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008295 pti->uFlags = TTF_SUBCLASS;
8296 pti->hwnd = beval->target;
8297 pti->hinst = 0; // Don't use string resources
8298 pti->uId = ID_BEVAL_TOOLTIP;
8299
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008300 pti->lpszText = LPSTR_TEXTCALLBACKW;
8301 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8302 pti->lParam = (LPARAM)beval->tofree;
8303 // switch multiline tooltips on
8304 if (GetClientRect(s_textArea, &rect))
8305 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8306 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008307
8308 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8309 pti->rect.left = pt.x - 3;
8310 pti->rect.top = pt.y - 3;
8311 pti->rect.right = pt.x + 3;
8312 pti->rect.bottom = pt.y + 3;
8313
8314 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8315 // Make tooltip appear sooner.
8316 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8317 // I've performed some tests and it seems the longest possible life time
8318 // of tooltip is 30 seconds.
8319 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8320 /*
8321 * HACK: force tooltip to appear, because it'll not appear until
8322 * first mouse move. D*mn M$
8323 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8324 */
8325 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8326 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8327 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008328}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008331delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008333 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334}
8335
8336 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008337beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008338 HWND hwnd UNUSED,
8339 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008340 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008341 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
8343 POINT pt;
8344 RECT rect;
8345
8346 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8347 return;
8348
8349 GetCursorPos(&pt);
8350 if (WindowFromPoint(pt) != s_textArea)
8351 return;
8352
8353 ScreenToClient(s_textArea, &pt);
8354 GetClientRect(s_textArea, &rect);
8355 if (!PtInRect(&rect, pt))
8356 return;
8357
K.Takataa8ec4912022-02-03 14:32:33 +00008358 if (last_user_activity > 0
8359 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 && (cur_beval->showState != ShS_PENDING
8361 || abs(cur_beval->x - pt.x) > 3
8362 || abs(cur_beval->y - pt.y) > 3))
8363 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008364 // Pointer resting in one place long enough, it's time to show
8365 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 cur_beval->showState = ShS_PENDING;
8367 cur_beval->x = pt.x;
8368 cur_beval->y = pt.y;
8369
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (cur_beval->msgCB != NULL)
8371 (*cur_beval->msgCB)(cur_beval, 0);
8372 }
8373}
8374
8375 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008376gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
K.Takataa8ec4912022-02-03 14:32:33 +00008378 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379}
8380
8381 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008382gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (beval == NULL)
8385 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008386 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8387 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388}
8389
8390 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008391gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
8393 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008394
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008395 vim_free(beval->msg);
8396 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8397 if (beval->msg == NULL)
8398 {
8399 delete_tooltip(beval);
8400 beval->showState = ShS_NEUTRAL;
8401 return;
8402 }
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 if (beval->showState == ShS_SHOWING)
8405 return;
8406 GetCursorPos(&pt);
8407 ScreenToClient(s_textArea, &pt);
8408
8409 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008411 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 gui_mch_disable_beval_area(cur_beval);
8413 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008414 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416}
8417
8418 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008419gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008420 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008421 char_u *mesg,
8422 void (*mesgCB)(BalloonEval *, int),
8423 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008425 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 BalloonEval *beval;
8427
8428 if (mesg != NULL && mesgCB != NULL)
8429 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008430 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 return NULL;
8432 }
8433
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008434 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (beval != NULL)
8436 {
8437 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
8439 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 beval->msg = mesg;
8441 beval->msgCB = mesgCB;
8442 beval->clientData = clientData;
8443
8444 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 cur_beval = beval;
8446
8447 if (p_beval)
8448 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 }
8450 return beval;
8451}
8452
8453 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008454Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008456 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 return;
8458
8459 if (cur_beval != NULL)
8460 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008461 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008463 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008464 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008465 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 delete_tooltip(cur_beval);
8467 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
8469 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008470 break;
8471 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008472 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008473 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008474 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008475 info->lpszText = (LPSTR)info->lParam;
8476 info->uFlags |= TTF_DI_SETITEM;
8477 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008478 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008479 case TTN_GETDISPINFOW:
8480 {
8481 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008482 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008483 info->lpszText = (LPWSTR)info->lParam;
8484 info->uFlags |= TTF_DI_SETITEM;
8485 }
8486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 }
8489}
8490
8491 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008492track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
8494 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8495 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008496 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497}
8498
8499 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008500gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008502# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008503 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008504# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008505 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 vim_free(beval);
8507}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008508#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
8510#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8511/*
8512 * We have multiple signs to draw at the same location. Draw the
8513 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8514 */
8515 void
8516netbeans_draw_multisign_indicator(int row)
8517{
8518 int i;
8519 int y;
8520 int x;
8521
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008522 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008523 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 x = 0;
8526 y = TEXT_Y(row);
8527
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008528# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008529 if (IS_ENABLE_DIRECTX())
8530 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008531# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 for (i = 0; i < gui.char_height - 3; i++)
8534 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8535
8536 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8537 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8538 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8539 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8540 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8541 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8542 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8543}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008544#endif