blob: 7bca9e7aca9c5897c62165c38f3567282c62fac6 [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
LemonBoyc27747e2022-05-07 12:25:40 +0100233#ifndef SPI_SETWHEELSCROLLCHARS
234# define SPI_SETWHEELSCROLLCHARS 0x006D
235#endif
236
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100239 * Define a few things for generating prototypes. This is just to avoid
240 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100242# define APIENTRY
243# define CALLBACK
244# define CONST
245# define FAR
246# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200247# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200248# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100249# define _cdecl
250typedef int BOOL;
251typedef int BYTE;
252typedef int DWORD;
253typedef int WCHAR;
254typedef int ENUMLOGFONT;
255typedef int FINDREPLACE;
256typedef int HANDLE;
257typedef int HBITMAP;
258typedef int HBRUSH;
259typedef int HDROP;
260typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100261typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100262typedef int LPARAM;
263typedef int LPCREATESTRUCT;
264typedef int LPCSTR;
265typedef int LPCTSTR;
266typedef int LPRECT;
267typedef int LPSTR;
268typedef int LPWINDOWPOS;
269typedef int LPWORD;
270typedef int LRESULT;
271typedef int HRESULT;
272# undef MSG
273typedef int MSG;
274typedef int NEWTEXTMETRIC;
275typedef int OSVERSIONINFO;
276typedef int PWORD;
277typedef int RECT;
278typedef int UINT;
279typedef int WORD;
280typedef int WPARAM;
281typedef int POINT;
282typedef void *HINSTANCE;
283typedef void *HMENU;
284typedef void *HWND;
285typedef void *HDC;
286typedef void VOID;
287typedef int LPNMHDR;
288typedef int LONG;
289typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200290typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200291typedef int COLORREF;
292typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100293#endif
294
K.Takata45f9cfb2022-01-21 11:11:00 +0000295static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000317static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
K.Takatac81e9bf2022-01-16 14:15:49 +0000355#ifndef _DPI_AWARENESS_CONTEXTS_
356typedef HANDLE DPI_AWARENESS_CONTEXT;
357
358typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000359 DPI_AWARENESS_INVALID = -1,
360 DPI_AWARENESS_UNAWARE = 0,
361 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000362 DPI_AWARENESS_PER_MONITOR_AWARE = 2
363} DPI_AWARENESS;
364
K.Takatab0b2b732022-01-19 12:59:21 +0000365# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
366# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000367# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
368# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
369# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
370#endif
371
372#define DEFAULT_DPI 96
373static int s_dpi = DEFAULT_DPI;
374static BOOL s_in_dpichanged = FALSE;
375static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
376
377static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
378static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
379static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
380//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
381static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
382static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
383
384 static UINT WINAPI
385stubGetDpiForSystem(void)
386{
387 HWND hwnd = GetDesktopWindow();
388 HDC hdc = GetWindowDC(hwnd);
389 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
390 ReleaseDC(hwnd, hdc);
391 return dpi;
392}
393
394 static int WINAPI
395stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
396{
397 return GetSystemMetrics(nIndex);
398}
399
400 static int
401adjust_fontsize_by_dpi(int size)
402{
403 return size * s_dpi / (int)pGetDpiForSystem();
404}
405
406 static int
407adjust_by_system_dpi(int size)
408{
409 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
410}
411
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100412#if defined(FEAT_DIRECTX)
413 static int
414directx_enabled(void)
415{
416 if (s_dwc != NULL)
417 return 1;
418 else if (s_directx_load_attempted)
419 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100420 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100421 DWrite_Init();
422 s_directx_load_attempted = 1;
423 s_dwc = DWriteContext_Open();
424 directx_binddc();
425 return s_dwc != NULL ? 1 : 0;
426}
427
428 static void
429directx_binddc(void)
430{
431 if (s_textArea != NULL)
432 {
433 RECT rect;
434 GetClientRect(s_textArea, &rect);
435 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
436 }
437}
438#endif
439
Bram Moolenaar734a8672019-12-02 22:49:38 +0100440extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100441
442static struct
443{
444 UINT key_sym;
445 char_u vim_code0;
446 char_u vim_code1;
447} special_keys[] =
448{
449 {VK_UP, 'k', 'u'},
450 {VK_DOWN, 'k', 'd'},
451 {VK_LEFT, 'k', 'l'},
452 {VK_RIGHT, 'k', 'r'},
453
454 {VK_F1, 'k', '1'},
455 {VK_F2, 'k', '2'},
456 {VK_F3, 'k', '3'},
457 {VK_F4, 'k', '4'},
458 {VK_F5, 'k', '5'},
459 {VK_F6, 'k', '6'},
460 {VK_F7, 'k', '7'},
461 {VK_F8, 'k', '8'},
462 {VK_F9, 'k', '9'},
463 {VK_F10, 'k', ';'},
464
465 {VK_F11, 'F', '1'},
466 {VK_F12, 'F', '2'},
467 {VK_F13, 'F', '3'},
468 {VK_F14, 'F', '4'},
469 {VK_F15, 'F', '5'},
470 {VK_F16, 'F', '6'},
471 {VK_F17, 'F', '7'},
472 {VK_F18, 'F', '8'},
473 {VK_F19, 'F', '9'},
474 {VK_F20, 'F', 'A'},
475
476 {VK_F21, 'F', 'B'},
477#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100478 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479#endif
480 {VK_F22, 'F', 'C'},
481 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100482 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100483
484 {VK_HELP, '%', '1'},
485 {VK_BACK, 'k', 'b'},
486 {VK_INSERT, 'k', 'I'},
487 {VK_DELETE, 'k', 'D'},
488 {VK_HOME, 'k', 'h'},
489 {VK_END, '@', '7'},
490 {VK_PRIOR, 'k', 'P'},
491 {VK_NEXT, 'k', 'N'},
492 {VK_PRINT, '%', '9'},
493 {VK_ADD, 'K', '6'},
494 {VK_SUBTRACT, 'K', '7'},
495 {VK_DIVIDE, 'K', '8'},
496 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100497 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100498 {VK_DECIMAL, 'K', 'B'},
499
500 {VK_NUMPAD0, 'K', 'C'},
501 {VK_NUMPAD1, 'K', 'D'},
502 {VK_NUMPAD2, 'K', 'E'},
503 {VK_NUMPAD3, 'K', 'F'},
504 {VK_NUMPAD4, 'K', 'G'},
505 {VK_NUMPAD5, 'K', 'H'},
506 {VK_NUMPAD6, 'K', 'I'},
507 {VK_NUMPAD7, 'K', 'J'},
508 {VK_NUMPAD8, 'K', 'K'},
509 {VK_NUMPAD9, 'K', 'L'},
510
Bram Moolenaar734a8672019-12-02 22:49:38 +0100511 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100512 {VK_SPACE, ' ', NUL},
513 {VK_TAB, TAB, NUL},
514 {VK_ESCAPE, ESC, NUL},
515 {NL, NL, NUL},
516 {CAR, CAR, NUL},
517
Bram Moolenaar734a8672019-12-02 22:49:38 +0100518 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100519 {0, 0, 0}
520};
521
Bram Moolenaar734a8672019-12-02 22:49:38 +0100522// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523static int s_button_pending = -1;
524
Bram Moolenaar734a8672019-12-02 22:49:38 +0100525// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
526// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100527static int s_getting_focus = FALSE;
528
529static int s_x_pending;
530static int s_y_pending;
531static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000532static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200534static int dead_key = 0; // 0: no dead key, 1: dead key pressed
535static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
536 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100537
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100538#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100539// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100540static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000541static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542#endif
543
544/*
545 * For control IME.
546 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100547 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548 */
K.Takata4ac893f2022-01-20 12:44:28 +0000549#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100550// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100551static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100554#endif
555
556#ifdef FEAT_MBYTE_IME
557static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
558#endif
559
560#if defined(FEAT_BROWSE)
561static char_u *convert_filter(char_u *s);
562#endif
563
564#ifdef DEBUG_PRINT_ERROR
565/*
566 * Print out the last Windows error message
567 */
568 static void
569print_windows_error(void)
570{
571 LPVOID lpMsgBuf;
572
573 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
574 NULL, GetLastError(),
575 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
576 (LPTSTR) &lpMsgBuf, 0, NULL);
577 TRACE1("Error: %s\n", lpMsgBuf);
578 LocalFree(lpMsgBuf);
579}
580#endif
581
582/*
583 * Cursor blink functions.
584 *
585 * This is a simple state machine:
586 * BLINK_NONE not blinking at all
587 * BLINK_OFF blinking, cursor is not shown
588 * BLINK_ON blinking, cursor is shown
589 */
590
591#define BLINK_NONE 0
592#define BLINK_OFF 1
593#define BLINK_ON 2
594
595static int blink_state = BLINK_NONE;
596static long_u blink_waittime = 700;
597static long_u blink_ontime = 400;
598static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000599static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100600
Bram Moolenaar703a8042016-06-04 16:24:32 +0200601 int
602gui_mch_is_blinking(void)
603{
604 return blink_state != BLINK_NONE;
605}
606
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200607 int
608gui_mch_is_blink_off(void)
609{
610 return blink_state == BLINK_OFF;
611}
612
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100613 void
614gui_mch_set_blinking(long wait, long on, long off)
615{
616 blink_waittime = wait;
617 blink_ontime = on;
618 blink_offtime = off;
619}
620
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100621 static VOID CALLBACK
622_OnBlinkTimer(
623 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100624 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000625 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627{
628 MSG msg;
629
630 /*
631 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
632 */
633
634 KillTimer(NULL, idEvent);
635
Bram Moolenaar734a8672019-12-02 22:49:38 +0100636 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000637 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 ;
639
640 if (blink_state == BLINK_ON)
641 {
642 gui_undraw_cursor();
643 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000644 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100645 }
646 else
647 {
648 gui_update_cursor(TRUE, FALSE);
649 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000650 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100652 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653}
654
655 static void
656gui_mswin_rm_blink_timer(void)
657{
658 MSG msg;
659
660 if (blink_timer != 0)
661 {
662 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100663 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000664 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665 ;
666 blink_timer = 0;
667 }
668}
669
670/*
671 * Stop the cursor blinking. Show the cursor if it wasn't shown.
672 */
673 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100674gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100675{
676 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100677 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100678 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100680 gui_mch_flush();
681 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100682 blink_state = BLINK_NONE;
683}
684
685/*
686 * Start the cursor blinking. If it was already blinking, this restarts the
687 * waiting time and shows the cursor.
688 */
689 void
690gui_mch_start_blink(void)
691{
692 gui_mswin_rm_blink_timer();
693
Bram Moolenaar734a8672019-12-02 22:49:38 +0100694 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
696 {
K.Takataa8ec4912022-02-03 14:32:33 +0000697 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 blink_state = BLINK_ON;
699 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100700 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 }
702}
703
704/*
705 * Call-back routines.
706 */
707
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100708 static VOID CALLBACK
709_OnTimer(
710 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100711 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000712 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100713 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714{
715 MSG msg;
716
717 /*
718 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
719 */
720 KillTimer(NULL, idEvent);
721 s_timed_out = TRUE;
722
Bram Moolenaar734a8672019-12-02 22:49:38 +0100723 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000724 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100725 ;
726 if (idEvent == s_wait_timer)
727 s_wait_timer = 0;
728}
729
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730 static void
731_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100732 HWND hwnd UNUSED,
733 UINT ch UNUSED,
734 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100735{
736 dead_key = 1;
737}
738
739/*
740 * Convert Unicode character "ch" to bytes in "string[slen]".
741 * When "had_alt" is TRUE the ALT key was included in "ch".
742 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200743 * Because the Windows API uses UTF-16, we have to deal with surrogate
744 * pairs; this is where we choose to deal with them: if "ch" is a high
745 * surrogate, it will be stored, and the length returned will be zero; the next
746 * char_to_string call will then include the high surrogate, decoding the pair
747 * of UTF-16 code units to a single Unicode code point, presuming it is the
748 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749 */
750 static int
751char_to_string(int ch, char_u *string, int slen, int had_alt)
752{
753 int len;
754 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200756 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200758 if (surrogate_pending_ch != 0)
759 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100760 // We don't guarantee ch is a low surrogate to match the high surrogate
761 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200762 wstring[0] = surrogate_pending_ch;
763 wstring[1] = ch;
764 surrogate_pending_ch = 0;
765 len = 2;
766 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100767 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200768 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100769 // We don't have the entire code point yet, only the first UTF-16 code
770 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200771 surrogate_pending_ch = ch;
772 return 0;
773 }
774 else
775 {
776 wstring[0] = ch;
777 len = 1;
778 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200779
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
781 // "enc_codepage" is non-zero use the standard Win32 function,
782 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200783 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100784 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
786 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100787 // If we had included the ALT key into the character but now the
788 // upper bit is no longer set, that probably means the conversion
789 // failed. Convert the original character and set the upper bit
790 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100792 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 wstring[0] = ch & 0x7f;
794 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
795 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100796 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200797 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 }
799 }
800 else
801 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 ws = utf16_to_enc(wstring, &len);
803 if (ws == NULL)
804 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100805 else
806 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100807 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 len = slen;
809 mch_memmove(string, ws, len);
810 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 }
812 }
813
814 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100815 {
816 string[0] = ch;
817 len = 1;
818 }
819
820 for (i = 0; i < len; ++i)
821 if (string[i] == CSI && len <= slen - 2)
822 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100823 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
825 string[++i] = KS_EXTRA;
826 string[++i] = (int)KE_CSI;
827 len += 2;
828 }
829
830 return len;
831}
832
LemonBoy45684c62022-04-24 15:46:42 +0100833 static int
834get_active_modifiers(void)
835{
836 int modifiers = 0;
837
838 if (GetKeyState(VK_CONTROL) & 0x8000)
839 modifiers |= MOD_MASK_CTRL;
840 if (GetKeyState(VK_SHIFT) & 0x8000)
841 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100842 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
843 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100844 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100845 if (GetKeyState(VK_LMENU) & 0x8000)
846 modifiers |= MOD_MASK_ALT;
847 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
848 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100849
850 return modifiers;
851}
852
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100853/*
854 * Key hit, add it to the input buffer.
855 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100856 static void
857_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100858 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100859 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100860 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861{
862 char_u string[40];
863 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100864 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100865 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100866
867 dead_key = 0;
868
LemonBoy45684c62022-04-24 15:46:42 +0100869 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100870
871 ch = simplify_key(ch, &modifiers);
872 // remove the SHIFT modifier for keys where it's already included, e.g.,
873 // '(' and '*'
874 modifiers = may_remove_shift_modifier(modifiers, ch);
875
876 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
877 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
878 if (ch == CSI)
879 ch = K_CSI;
880
881 if (modifiers)
882 {
883 string[0] = CSI;
884 string[1] = KS_MODIFIER;
885 string[2] = modifiers;
886 add_to_input_buf(string, 3);
887 }
888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100889 len = char_to_string(ch, string, 40, FALSE);
890 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
891 {
892 trash_input_buf();
893 got_int = TRUE;
894 }
895
896 add_to_input_buf(string, len);
897}
898
899/*
900 * Alt-Key hit, add it to the input buffer.
901 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100902 static void
903_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100904 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100905 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100906 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100908 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909 int len;
910 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100911 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100912
913 dead_key = 0;
914
Bram Moolenaar734a8672019-12-02 22:49:38 +0100915 // OK, we have a character key (given by ch) which was entered with the
916 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
917 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
918 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100919 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100920 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100921 // remove the SHIFT modifier for keys where it's already included, e.g.,
922 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200923 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100924
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200925 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
926 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100927 if (ch == CSI)
928 ch = K_CSI;
929
930 len = 0;
931 if (modifiers)
932 {
933 string[len++] = CSI;
934 string[len++] = KS_MODIFIER;
935 string[len++] = modifiers;
936 }
937
938 if (IS_SPECIAL((int)ch))
939 {
940 string[len++] = CSI;
941 string[len++] = K_SECOND((int)ch);
942 string[len++] = K_THIRD((int)ch);
943 }
944 else
945 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100946 // Although the documentation isn't clear about it, we assume "ch" is
947 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100948 len += char_to_string(ch, string + len, 40 - len, TRUE);
949 }
950
951 add_to_input_buf(string, len);
952}
953
954 static void
955_OnMouseEvent(
956 int button,
957 int x,
958 int y,
959 int repeated_click,
960 UINT keyFlags)
961{
962 int vim_modifiers = 0x0;
963
964 s_getting_focus = FALSE;
965
966 if (keyFlags & MK_SHIFT)
967 vim_modifiers |= MOUSE_SHIFT;
968 if (keyFlags & MK_CONTROL)
969 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100970 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100971 vim_modifiers |= MOUSE_ALT;
972
973 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
974}
975
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100976 static void
977_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100978 HWND hwnd UNUSED,
979 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100980 int x,
981 int y,
982 UINT keyFlags)
983{
984 static LONG s_prevTime = 0;
985
986 LONG currentTime = GetMessageTime();
987 int button = -1;
988 int repeated_click;
989
Bram Moolenaar734a8672019-12-02 22:49:38 +0100990 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100991 (void)SetFocus(s_hwnd);
992
993 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
994 button = MOUSE_LEFT;
995 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
996 button = MOUSE_MIDDLE;
997 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
998 button = MOUSE_RIGHT;
999 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1000 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1002 }
1003 else if (s_uMsg == WM_CAPTURECHANGED)
1004 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001005 // on W95/NT4, somehow you get in here with an odd Msg
1006 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001007 if (s_button_pending == MOUSE_LEFT)
1008 button = MOUSE_RIGHT;
1009 else
1010 button = MOUSE_LEFT;
1011 }
1012 if (button >= 0)
1013 {
1014 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1015
1016 /*
1017 * Holding down the left and right buttons simulates pushing the middle
1018 * button.
1019 */
1020 if (repeated_click
1021 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1022 || (button == MOUSE_RIGHT
1023 && s_button_pending == MOUSE_LEFT)))
1024 {
1025 /*
1026 * Hmm, gui.c will ignore more than one button down at a time, so
1027 * pretend we let go of it first.
1028 */
1029 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1030 button = MOUSE_MIDDLE;
1031 repeated_click = FALSE;
1032 s_button_pending = -1;
1033 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1034 }
1035 else if ((repeated_click)
1036 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1037 {
1038 if (s_button_pending > -1)
1039 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001040 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1041 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001042 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001043 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001044 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1045 }
1046 else
1047 {
1048 /*
1049 * If this is the first press (i.e. not a multiple click) don't
1050 * action immediately, but store and wait for:
1051 * i) button-up
1052 * ii) mouse move
1053 * iii) another button press
1054 * before using it.
1055 * This enables us to make left+right simulate middle button,
1056 * without left or right being actioned first. The side-effect is
1057 * that if you click and hold the mouse without dragging, the
1058 * cursor doesn't move until you release the button. In practice
1059 * this is hardly a problem.
1060 */
1061 s_button_pending = button;
1062 s_x_pending = x;
1063 s_y_pending = y;
1064 s_kFlags_pending = keyFlags;
1065 }
1066
1067 s_prevTime = currentTime;
1068 }
1069}
1070
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001071 static void
1072_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001073 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001074 int x,
1075 int y,
1076 UINT keyFlags)
1077{
1078 int button;
1079
1080 s_getting_focus = FALSE;
1081 if (s_button_pending > -1)
1082 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001083 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001084 _OnMouseEvent(s_button_pending, s_x_pending,
1085 s_y_pending, FALSE, s_kFlags_pending);
1086 s_button_pending = -1;
1087 }
1088 if (s_uMsg == WM_MOUSEMOVE)
1089 {
1090 /*
1091 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1092 * down.
1093 */
1094 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1095 | MK_XBUTTON1 | MK_XBUTTON2)))
1096 {
1097 gui_mouse_moved(x, y);
1098 return;
1099 }
1100
1101 /*
1102 * While button is down, keep grabbing mouse move events when
1103 * the mouse goes outside the window
1104 */
1105 SetCapture(s_textArea);
1106 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001107 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 }
1109 else
1110 {
1111 ReleaseCapture();
1112 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001113 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 }
1115
1116 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1117}
1118
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001119 static void
1120_OnSizeTextArea(
1121 HWND hwnd UNUSED,
1122 UINT state UNUSED,
1123 int cx UNUSED,
1124 int cy UNUSED)
1125{
1126#if defined(FEAT_DIRECTX)
1127 if (IS_ENABLE_DIRECTX())
1128 directx_binddc();
1129#endif
1130}
1131
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001132#ifdef FEAT_MENU
1133/*
1134 * Find the vimmenu_T with the given id
1135 */
1136 static vimmenu_T *
1137gui_mswin_find_menu(
1138 vimmenu_T *pMenu,
1139 int id)
1140{
1141 vimmenu_T *pChildMenu;
1142
1143 while (pMenu)
1144 {
1145 if (pMenu->id == (UINT)id)
1146 break;
1147 if (pMenu->children != NULL)
1148 {
1149 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1150 if (pChildMenu)
1151 {
1152 pMenu = pChildMenu;
1153 break;
1154 }
1155 }
1156 pMenu = pMenu->next;
1157 }
1158 return pMenu;
1159}
1160
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001161 static void
1162_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001163 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001164 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001165 HWND hwndCtl UNUSED,
1166 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001167{
1168 vimmenu_T *pMenu;
1169
1170 pMenu = gui_mswin_find_menu(root_menu, id);
1171 if (pMenu)
1172 gui_menu_cb(pMenu);
1173}
1174#endif
1175
1176#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001177/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001178 * Handle a Find/Replace window message.
1179 */
1180 static void
1181_OnFindRepl(void)
1182{
1183 int flags = 0;
1184 int down;
1185
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001187 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001188 (void)SetFocus(s_hwnd);
1189
1190 if (s_findrep_struct.Flags & FR_FINDNEXT)
1191 {
1192 flags = FRD_FINDNEXT;
1193
Bram Moolenaar734a8672019-12-02 22:49:38 +01001194 // Give main window the focus back: this is so the cursor isn't
1195 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001196 (void)SetFocus(s_hwnd);
1197 }
1198 else if (s_findrep_struct.Flags & FR_REPLACE)
1199 {
1200 flags = FRD_REPLACE;
1201
Bram Moolenaar734a8672019-12-02 22:49:38 +01001202 // Give main window the focus back: this is so the cursor isn't
1203 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001204 (void)SetFocus(s_hwnd);
1205 }
1206 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1207 {
1208 flags = FRD_REPLACEALL;
1209 }
1210
1211 if (flags != 0)
1212 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001213 char_u *p, *q;
1214
Bram Moolenaar734a8672019-12-02 22:49:38 +01001215 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001216 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1217 flags |= FRD_WHOLE_WORD;
1218 if (s_findrep_struct.Flags & FR_MATCHCASE)
1219 flags |= FRD_MATCH_CASE;
1220 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001221 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1222 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1223 if (p != NULL && q != NULL)
1224 gui_do_findrepl(flags, p, q, down);
1225 vim_free(p);
1226 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001227 }
1228}
1229#endif
1230
1231 static void
1232HandleMouseHide(UINT uMsg, LPARAM lParam)
1233{
1234 static LPARAM last_lParam = 0L;
1235
Bram Moolenaar734a8672019-12-02 22:49:38 +01001236 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001237 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1238 {
1239 if (lParam == last_lParam)
1240 return;
1241 last_lParam = lParam;
1242 }
1243
Bram Moolenaar734a8672019-12-02 22:49:38 +01001244 // Handle specially, to centralise coding. We need to be sure we catch all
1245 // possible events which should cause us to restore the cursor (as it is a
1246 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001247 switch (uMsg)
1248 {
1249 case WM_KEYUP:
1250 case WM_CHAR:
1251 /*
1252 * blank out the pointer if necessary
1253 */
1254 if (p_mh)
1255 gui_mch_mousehide(TRUE);
1256 break;
1257
Bram Moolenaar734a8672019-12-02 22:49:38 +01001258 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001259 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001260 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261 case WM_LBUTTONDOWN:
1262 case WM_LBUTTONUP:
1263 case WM_MBUTTONDOWN:
1264 case WM_MBUTTONUP:
1265 case WM_RBUTTONDOWN:
1266 case WM_RBUTTONUP:
1267 case WM_XBUTTONDOWN:
1268 case WM_XBUTTONUP:
1269 case WM_NCMOUSEMOVE:
1270 case WM_NCLBUTTONDOWN:
1271 case WM_NCLBUTTONUP:
1272 case WM_NCMBUTTONDOWN:
1273 case WM_NCMBUTTONUP:
1274 case WM_NCRBUTTONDOWN:
1275 case WM_NCRBUTTONUP:
1276 case WM_KILLFOCUS:
1277 /*
1278 * if the pointer is currently hidden, then we should show it.
1279 */
1280 gui_mch_mousehide(FALSE);
1281 break;
1282 }
1283}
1284
1285 static LRESULT CALLBACK
1286_TextAreaWndProc(
1287 HWND hwnd,
1288 UINT uMsg,
1289 WPARAM wParam,
1290 LPARAM lParam)
1291{
1292 /*
1293 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1294 hwnd, uMsg, wParam, lParam);
1295 */
1296
1297 HandleMouseHide(uMsg, lParam);
1298
1299 s_uMsg = uMsg;
1300 s_wParam = wParam;
1301 s_lParam = lParam;
1302
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001303#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001304 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001305#endif
1306
1307 switch (uMsg)
1308 {
1309 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1310 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1311 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1312 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1313 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1314 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1315 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1316 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1317 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1318 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1319 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1320 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1321 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1322 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001323 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001324
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001325#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001326 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1327 return TRUE;
1328#endif
1329 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001330 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331 }
1332}
1333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001334/*
1335 * Called when the foreground or background color has been changed.
1336 */
1337 void
1338gui_mch_new_colors(void)
1339{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001340 HBRUSH prevBrush;
1341
1342 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001343 prevBrush = (HBRUSH)SetClassLongPtr(
1344 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001345 InvalidateRect(s_hwnd, NULL, TRUE);
1346 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001347}
1348
1349/*
1350 * Set the colors to their default values.
1351 */
1352 void
1353gui_mch_def_colors(void)
1354{
1355 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1356 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1357 gui.def_norm_pixel = gui.norm_pixel;
1358 gui.def_back_pixel = gui.back_pixel;
1359}
1360
1361/*
1362 * Open the GUI window which was created by a call to gui_mch_init().
1363 */
1364 int
1365gui_mch_open(void)
1366{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001367 // Actually open the window, if not already visible
1368 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001369 if (!IsWindowVisible(s_hwnd))
1370 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1371
1372#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001373 // Init replace string here, so that we keep it when re-opening the
1374 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001375 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1376#endif
1377
1378 return OK;
1379}
1380
1381/*
1382 * Get the position of the top left corner of the window.
1383 */
1384 int
1385gui_mch_get_winpos(int *x, int *y)
1386{
1387 RECT rect;
1388
1389 GetWindowRect(s_hwnd, &rect);
1390 *x = rect.left;
1391 *y = rect.top;
1392 return OK;
1393}
1394
1395/*
1396 * Set the position of the top left corner of the window to the given
1397 * coordinates.
1398 */
1399 void
1400gui_mch_set_winpos(int x, int y)
1401{
1402 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1403 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1404}
1405 void
1406gui_mch_set_text_area_pos(int x, int y, int w, int h)
1407{
1408 static int oldx = 0;
1409 static int oldy = 0;
1410
1411 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1412
1413#ifdef FEAT_TOOLBAR
1414 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1415 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001416 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001417#endif
1418#if defined(FEAT_GUI_TABLINE)
1419 if (showing_tabline)
1420 {
1421 int top = 0;
1422 RECT rect;
1423
1424# ifdef FEAT_TOOLBAR
1425 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001426 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001427# endif
1428 GetClientRect(s_hwnd, &rect);
1429 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1430 }
1431#endif
1432
Bram Moolenaar734a8672019-12-02 22:49:38 +01001433 // When side scroll bar is unshown, the size of window will change.
1434 // then, the text area move left or right. thus client rect should be
1435 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001436 if (oldx != x || oldy != y)
1437 {
1438 InvalidateRect(s_hwnd, NULL, FALSE);
1439 oldx = x;
1440 oldy = y;
1441 }
1442}
1443
1444
1445/*
1446 * Scrollbar stuff:
1447 */
1448
1449 void
1450gui_mch_enable_scrollbar(
1451 scrollbar_T *sb,
1452 int flag)
1453{
1454 ShowScrollBar(sb->id, SB_CTL, flag);
1455
Bram Moolenaar734a8672019-12-02 22:49:38 +01001456 // TODO: When the window is maximized, the size of the window stays the
1457 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1458 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001459}
1460
1461 void
1462gui_mch_set_scrollbar_pos(
1463 scrollbar_T *sb,
1464 int x,
1465 int y,
1466 int w,
1467 int h)
1468{
1469 SetWindowPos(sb->id, NULL, x, y, w, h,
1470 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1471}
1472
Bram Moolenaar203ec772020-07-17 20:43:43 +02001473 int
1474gui_mch_get_scrollbar_xpadding(void)
1475{
1476 RECT rcTxt, rcWnd;
1477 int xpad;
1478
1479 GetWindowRect(s_textArea, &rcTxt);
1480 GetWindowRect(s_hwnd, &rcWnd);
1481 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001482 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1483 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001484 return (xpad < 0) ? 0 : xpad;
1485}
1486
1487 int
1488gui_mch_get_scrollbar_ypadding(void)
1489{
1490 RECT rcTxt, rcWnd;
1491 int ypad;
1492
1493 GetWindowRect(s_textArea, &rcTxt);
1494 GetWindowRect(s_hwnd, &rcWnd);
1495 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001496 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1497 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001498 return (ypad < 0) ? 0 : ypad;
1499}
1500
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001501 void
1502gui_mch_create_scrollbar(
1503 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001504 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001505{
1506 sb->id = CreateWindow(
1507 "SCROLLBAR", "Scrollbar",
1508 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001509 10, // Any value will do for now
1510 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001511 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001512 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513}
1514
1515/*
1516 * Find the scrollbar with the given hwnd.
1517 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001518 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519gui_mswin_find_scrollbar(HWND hwnd)
1520{
1521 win_T *wp;
1522
1523 if (gui.bottom_sbar.id == hwnd)
1524 return &gui.bottom_sbar;
1525 FOR_ALL_WINDOWS(wp)
1526 {
1527 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1528 return &wp->w_scrollbars[SBAR_LEFT];
1529 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1530 return &wp->w_scrollbars[SBAR_RIGHT];
1531 }
1532 return NULL;
1533}
1534
K.Takatac81e9bf2022-01-16 14:15:49 +00001535 static void
1536update_scrollbar_size(void)
1537{
1538 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1539 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1540}
1541
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001542/*
K.Takataabe628e2022-01-23 16:25:17 +00001543 * Get the average character size of a font.
1544 */
1545 static void
1546GetAverageFontSize(HDC hdc, SIZE *size)
1547{
1548 // GetTextMetrics() may not return the right value in tmAveCharWidth
1549 // for some fonts. Do our own average computation.
1550 GetTextExtentPoint(hdc,
1551 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1552 52, size);
1553 size->cx = (size->cx / 26 + 1) / 2;
1554}
1555
1556/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001557 * Get the character size of a font.
1558 */
1559 static void
1560GetFontSize(GuiFont font)
1561{
1562 HWND hwnd = GetDesktopWindow();
1563 HDC hdc = GetWindowDC(hwnd);
1564 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001565 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566 TEXTMETRIC tm;
1567
1568 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001569 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001570
K.Takataabe628e2022-01-23 16:25:17 +00001571 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001572 gui.char_height = tm.tmHeight + p_linespace;
1573
1574 SelectFont(hdc, hfntOld);
1575
1576 ReleaseDC(hwnd, hdc);
1577}
1578
1579/*
1580 * Adjust gui.char_height (after 'linespace' was changed).
1581 */
1582 int
1583gui_mch_adjust_charheight(void)
1584{
1585 GetFontSize(gui.norm_font);
1586 return OK;
1587}
1588
1589 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001590get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001591{
1592 HFONT font = NULL;
1593
Bram Moolenaar734a8672019-12-02 22:49:38 +01001594 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001595 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596
1597 if (font == NULL)
1598 return NOFONT;
1599
1600 return (GuiFont)font;
1601}
1602
1603 static int
1604pixels_to_points(int pixels, int vertical)
1605{
1606 int points;
1607 HWND hwnd;
1608 HDC hdc;
1609
1610 hwnd = GetDesktopWindow();
1611 hdc = GetWindowDC(hwnd);
1612
1613 points = MulDiv(pixels, 72,
1614 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1615
1616 ReleaseDC(hwnd, hdc);
1617
1618 return points;
1619}
1620
1621 GuiFont
1622gui_mch_get_font(
1623 char_u *name,
1624 int giveErrorIfMissing)
1625{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001626 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 GuiFont font = NOFONT;
1628
1629 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001630 {
1631 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001632 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001633 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001635 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636 return font;
1637}
1638
1639#if defined(FEAT_EVAL) || defined(PROTO)
1640/*
1641 * Return the name of font "font" in allocated memory.
1642 * Don't know how to get the actual name, thus use the provided name.
1643 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001645gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646{
1647 if (name == NULL)
1648 return NULL;
1649 return vim_strsave(name);
1650}
1651#endif
1652
1653 void
1654gui_mch_free_font(GuiFont font)
1655{
1656 if (font)
1657 DeleteObject((HFONT)font);
1658}
1659
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001660/*
1661 * Return the Pixel value (color) for the given color name.
1662 * Return INVALCOLOR for error.
1663 */
1664 guicolor_T
1665gui_mch_get_color(char_u *name)
1666{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001667 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668
1669 typedef struct SysColorTable
1670 {
1671 char *name;
1672 int color;
1673 } SysColorTable;
1674
1675 static SysColorTable sys_table[] =
1676 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001677 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1678 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001679#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1681#endif
1682 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1683 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1684 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1685 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1686 {"SYS_DESKTOP", COLOR_DESKTOP},
1687 {"SYS_INFOBK", COLOR_INFOBK},
1688 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1689 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001690 {"SYS_BTNFACE", COLOR_BTNFACE},
1691 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1692 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1693 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1694 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1695 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1696 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1697 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1698 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1699 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1700 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1701 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1702 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1703 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1704 {"SYS_MENU", COLOR_MENU},
1705 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1706 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1707 {"SYS_WINDOW", COLOR_WINDOW},
1708 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1709 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1710 };
1711
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001712 /*
1713 * Try to look up a system colour.
1714 */
K.Takataeeec2542021-06-02 13:28:16 +02001715 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716 if (STRICMP(name, sys_table[i].name) == 0)
1717 return GetSysColor(sys_table[i].color);
1718
Bram Moolenaarab302212016-04-26 20:59:29 +02001719 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001720}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001721
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001722 guicolor_T
1723gui_mch_get_rgb_color(int r, int g, int b)
1724{
1725 return gui_get_rgb_color_cmn(r, g, b);
1726}
1727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728/*
1729 * Return OK if the key with the termcap name "name" is supported.
1730 */
1731 int
1732gui_mch_haskey(char_u *name)
1733{
1734 int i;
1735
1736 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001737 if (name[0] == special_keys[i].vim_code0
1738 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001739 return OK;
1740 return FAIL;
1741}
1742
1743 void
1744gui_mch_beep(void)
1745{
LemonBoy77771d32022-04-13 11:47:25 +01001746 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001747}
1748/*
1749 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1750 */
1751 void
1752gui_mch_invert_rectangle(
1753 int r,
1754 int c,
1755 int nr,
1756 int nc)
1757{
1758 RECT rc;
1759
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001760#if defined(FEAT_DIRECTX)
1761 if (IS_ENABLE_DIRECTX())
1762 DWriteContext_Flush(s_dwc);
1763#endif
1764
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001765 /*
1766 * Note: InvertRect() excludes right and bottom of rectangle.
1767 */
1768 rc.left = FILL_X(c);
1769 rc.top = FILL_Y(r);
1770 rc.right = rc.left + nc * gui.char_width;
1771 rc.bottom = rc.top + nr * gui.char_height;
1772 InvertRect(s_hdc, &rc);
1773}
1774
1775/*
1776 * Iconify the GUI window.
1777 */
1778 void
1779gui_mch_iconify(void)
1780{
1781 ShowWindow(s_hwnd, SW_MINIMIZE);
1782}
1783
1784/*
1785 * Draw a cursor without focus.
1786 */
1787 void
1788gui_mch_draw_hollow_cursor(guicolor_T color)
1789{
1790 HBRUSH hbr;
1791 RECT rc;
1792
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001793#if defined(FEAT_DIRECTX)
1794 if (IS_ENABLE_DIRECTX())
1795 DWriteContext_Flush(s_dwc);
1796#endif
1797
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001798 /*
1799 * Note: FrameRect() excludes right and bottom of rectangle.
1800 */
1801 rc.left = FILL_X(gui.col);
1802 rc.top = FILL_Y(gui.row);
1803 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001804 if (mb_lefthalve(gui.row, gui.col))
1805 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001806 rc.bottom = rc.top + gui.char_height;
1807 hbr = CreateSolidBrush(color);
1808 FrameRect(s_hdc, &rc, hbr);
1809 DeleteBrush(hbr);
1810}
1811/*
1812 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1813 * color "color".
1814 */
1815 void
1816gui_mch_draw_part_cursor(
1817 int w,
1818 int h,
1819 guicolor_T color)
1820{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821 RECT rc;
1822
1823 /*
1824 * Note: FillRect() excludes right and bottom of rectangle.
1825 */
1826 rc.left =
1827#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001828 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001829 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1830#endif
1831 FILL_X(gui.col);
1832 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1833 rc.right = rc.left + w;
1834 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001835
Bram Moolenaar92467d32017-12-05 13:22:16 +01001836 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001837}
1838
1839
1840/*
1841 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1842 * dead key's nominal character and re-post the original message.
1843 */
1844 static void
1845outputDeadKey_rePost(MSG originalMsg)
1846{
1847 static MSG deadCharExpel;
1848
1849 if (!dead_key)
1850 return;
1851
1852 dead_key = 0;
1853
Bram Moolenaar734a8672019-12-02 22:49:38 +01001854 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001855 deadCharExpel.message = originalMsg.message;
1856 deadCharExpel.hwnd = originalMsg.hwnd;
1857 deadCharExpel.wParam = VK_SPACE;
1858
K.Takata4ac893f2022-01-20 12:44:28 +00001859 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001860
Bram Moolenaar734a8672019-12-02 22:49:38 +01001861 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1863 originalMsg.lParam);
1864}
1865
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001866/*
1867 * Process a single Windows message.
1868 * If one is not available we hang until one is.
1869 */
1870 static void
1871process_message(void)
1872{
1873 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001874 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001875 char_u string[40];
1876 int i;
1877 int modifiers = 0;
1878 int key;
1879#ifdef FEAT_MENU
1880 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1881#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001882 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001883
K.Takatab7057bd2022-01-21 11:37:07 +00001884 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885
1886#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001887 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 if (msg.message == WM_OLE)
1889 {
1890 char_u *str = (char_u *)msg.lParam;
1891 if (str == NULL || *str == NUL)
1892 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // Message can't be ours, forward it. Fixes problem with Ultramon
1894 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001895 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 }
1897 else
1898 {
1899 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001900 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001901 }
1902 return;
1903 }
1904#endif
1905
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001907 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001908 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001909 {
1910 HandleMouseHide(msg.message, msg.lParam);
1911 return;
1912 }
1913#endif
1914
1915 /*
1916 * Check if it's a special key that we recognise. If not, call
1917 * TranslateMessage().
1918 */
1919 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1920 {
1921 vk = (int) msg.wParam;
1922
1923 /*
1924 * Handle dead keys in special conditions in other cases we let Windows
1925 * handle them and do not interfere.
1926 *
1927 * The dead_key flag must be reset on several occasions:
1928 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1929 * consumed at that point (This is when we let Windows combine the
1930 * dead character on its own)
1931 *
1932 * - Before doing something special such as regenerating keypresses to
1933 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001934 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 * immediately to _OnChar() (or _OnSysChar()).
1936 */
1937 if (dead_key)
1938 {
1939 /*
1940 * If a dead key was pressed and the user presses VK_SPACE,
1941 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1942 * with the dead char now, so do nothing special and let Windows
1943 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01001944 *
1945 * Note that VK_SPACE combines with the dead_key's character and
1946 * only one WM_CHAR will be generated by TranslateMessage(), in
1947 * the two other cases two WM_CHAR will be generated: the dead
1948 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1949 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001950 */
1951 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1952 {
1953 dead_key = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001954 TranslateMessage(&msg);
1955 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001956 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001957 // In modes where we are not typing, dead keys should behave
1958 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001959 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1960 {
1961 outputDeadKey_rePost(msg);
1962 return;
1963 }
1964 }
1965
Bram Moolenaar734a8672019-12-02 22:49:38 +01001966 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001967 if (vk == VK_CANCEL)
1968 {
1969 trash_input_buf();
1970 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001971 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001972 string[0] = Ctrl_C;
1973 add_to_input_buf(string, 1);
1974 }
1975
LemonBoy77fc0b02022-04-22 22:45:52 +01001976 // This is an IME event or a synthetic keystroke, let Windows handle it.
1977 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1978 {
1979 TranslateMessage(&msg);
1980 return;
1981 }
1982
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001983 for (i = 0; special_keys[i].key_sym != 0; i++)
1984 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001985 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001986 if (special_keys[i].key_sym == vk
LemonBoy202b4bd2022-04-28 19:50:54 +01001987 && (vk != VK_SPACE || !(GetKeyState(VK_LMENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001988 {
1989 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001990 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001991 * is a key that would normally trigger the dead key nominal
1992 * character output (such as a NUMPAD printable character or
1993 * the TAB key, etc...).
1994 */
1995 if (dead_key && (special_keys[i].vim_code0 == 'K'
1996 || vk == VK_TAB || vk == CAR))
1997 {
1998 outputDeadKey_rePost(msg);
1999 return;
2000 }
2001
2002#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002003 // Check for <F10>: Windows selects the menu. When <F10> is
2004 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002005 if (vk == VK_F10
2006 && gui.menu_is_active
2007 && check_map(k10, State, FALSE, TRUE, FALSE,
2008 NULL, NULL) == NULL)
2009 break;
2010#endif
LemonBoy45684c62022-04-24 15:46:42 +01002011 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002012
2013 if (special_keys[i].vim_code1 == NUL)
2014 key = special_keys[i].vim_code0;
2015 else
2016 key = TO_SPECIAL(special_keys[i].vim_code0,
2017 special_keys[i].vim_code1);
2018 key = simplify_key(key, &modifiers);
2019 if (key == CSI)
2020 key = K_CSI;
2021
2022 if (modifiers)
2023 {
2024 string[0] = CSI;
2025 string[1] = KS_MODIFIER;
2026 string[2] = modifiers;
2027 add_to_input_buf(string, 3);
2028 }
2029
2030 if (IS_SPECIAL(key))
2031 {
2032 string[0] = CSI;
2033 string[1] = K_SECOND(key);
2034 string[2] = K_THIRD(key);
2035 add_to_input_buf(string, 3);
2036 }
2037 else
2038 {
2039 int len;
2040
Bram Moolenaar734a8672019-12-02 22:49:38 +01002041 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002042 len = char_to_string(key, string, 40, FALSE);
2043 add_to_input_buf(string, len);
2044 }
2045 break;
2046 }
2047 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002048
2049 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002050 if (special_keys[i].key_sym == 0)
2051 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002052 WCHAR ch[8];
2053 int len;
2054 int i;
2055 UINT scan_code;
2056
LemonBoy77fc0b02022-04-22 22:45:52 +01002057 // Construct the state table with only a few modifiers, we don't
2058 // really care about the presence of Ctrl/Alt as those modifiers are
2059 // handled by Vim separately.
2060 memset(keyboard_state, 0, 256);
2061 if (GetKeyState(VK_SHIFT) & 0x8000)
2062 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002063 if (GetKeyState(VK_CAPITAL) & 0x0001)
2064 keyboard_state[VK_CAPITAL] = 0x01;
LemonBoy45684c62022-04-24 15:46:42 +01002065 // Alt-Gr is synthesized as Alt + Ctrl.
LemonBoy202b4bd2022-04-28 19:50:54 +01002066 if ((GetKeyState(VK_RMENU) & 0x8000)
2067 && (GetKeyState(VK_CONTROL) & 0x8000))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002069 keyboard_state[VK_MENU] = 0x80;
2070 keyboard_state[VK_CONTROL] = 0x80;
2071 }
2072
2073 // Translate the virtual key according to the current keyboard
2074 // layout.
2075 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2076 // Convert the scan-code into a sequence of zero or more unicode
2077 // codepoints.
2078 // If this is a dead key ToUnicode returns a negative value.
2079 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2080 0);
2081 dead_key = len < 0;
2082
2083 if (len <= 0)
2084 return;
2085
2086 // Post the message as TranslateMessage would do.
2087 if (msg.message == WM_KEYDOWN)
2088 {
2089 for (i = 0; i < len; i++)
2090 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002091 }
2092 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002093 {
2094 for (i = 0; i < len; i++)
2095 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2096 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002097 }
2098 }
2099#ifdef FEAT_MBYTE_IME
2100 else if (msg.message == WM_IME_NOTIFY)
2101 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2102 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002103 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002104 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002105#endif
2106
2107#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002108 // Check for <F10>: Default effect is to select the menu. When <F10> is
2109 // mapped we need to stop it here to avoid strange effects (e.g., for the
2110 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002111 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2112 NULL, NULL) == NULL)
2113#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002114 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002115}
2116
2117/*
2118 * Catch up with any queued events. This may put keyboard input into the
2119 * input buffer, call resize call-backs, trigger timers etc. If there is
2120 * nothing in the event queue (& no timers pending), then we return
2121 * immediately.
2122 */
2123 void
2124gui_mch_update(void)
2125{
2126 MSG msg;
2127
2128 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002129 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002130 && !vim_is_input_buf_full())
2131 process_message();
2132}
2133
Bram Moolenaar4231da42016-06-02 14:30:04 +02002134 static void
2135remove_any_timer(void)
2136{
2137 MSG msg;
2138
2139 if (s_wait_timer != 0 && !s_timed_out)
2140 {
2141 KillTimer(NULL, s_wait_timer);
2142
Bram Moolenaar734a8672019-12-02 22:49:38 +01002143 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002144 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002145 ;
2146 s_wait_timer = 0;
2147 }
2148}
2149
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002150/*
2151 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2152 * from the keyboard.
2153 * wtime == -1 Wait forever.
2154 * wtime == 0 This should never happen.
2155 * wtime > 0 Wait wtime milliseconds for a character.
2156 * Returns OK if a character was found to be available within the given time,
2157 * or FAIL otherwise.
2158 */
2159 int
2160gui_mch_wait_for_chars(int wtime)
2161{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002162 int focus;
2163
2164 s_timed_out = FALSE;
2165
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002166 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002167 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002168 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002169 if (s_busy_processing)
2170 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002171
2172 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002173 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2174 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002175 }
2176
2177 allow_scrollbar = TRUE;
2178
2179 focus = gui.in_focus;
2180 while (!s_timed_out)
2181 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002182 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002183 if (gui.in_focus != focus)
2184 {
2185 if (gui.in_focus)
2186 gui_mch_start_blink();
2187 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002188 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002189 focus = gui.in_focus;
2190 }
2191
2192 if (s_need_activate)
2193 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002194 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195 s_need_activate = FALSE;
2196 }
2197
Bram Moolenaar4231da42016-06-02 14:30:04 +02002198#ifdef FEAT_TIMERS
2199 did_add_timer = FALSE;
2200#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002201#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002202 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002203 for (;;)
2204 {
2205 MSG msg;
2206
2207 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002208# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002209 if (did_add_timer)
2210 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002211# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002212 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002213 {
2214 process_message();
2215 break;
2216 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002217 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002218 // TODO: The 10 msec is a compromise between laggy response
2219 // and consuming more CPU time. Better would be to handle
2220 // channel messages when they arrive.
2221 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002222 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002223 break;
2224 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002225#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002226 // Don't use gui_mch_update() because then we will spin-lock until a
2227 // char arrives, instead we use GetMessage() to hang until an
2228 // event arrives. No need to check for input_buf_full because we are
2229 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002231#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002232
2233 if (input_available())
2234 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002235 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002236 allow_scrollbar = FALSE;
2237
Bram Moolenaar89c00032019-09-04 13:53:21 +02002238 // Clear pending mouse button, the release event may have been
2239 // taken by the dialog window. But don't do this when getting
2240 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002241 if (!s_getting_focus)
2242 s_button_pending = -1;
2243
2244 return OK;
2245 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002246
2247#ifdef FEAT_TIMERS
2248 if (did_add_timer)
2249 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002250 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002251 remove_any_timer();
2252 break;
2253 }
2254#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002255 }
2256 allow_scrollbar = FALSE;
2257 return FAIL;
2258}
2259
2260/*
2261 * Clear a rectangular region of the screen from text pos (row1, col1) to
2262 * (row2, col2) inclusive.
2263 */
2264 void
2265gui_mch_clear_block(
2266 int row1,
2267 int col1,
2268 int row2,
2269 int col2)
2270{
2271 RECT rc;
2272
2273 /*
2274 * Clear one extra pixel at the far right, for when bold characters have
2275 * spilled over to the window border.
2276 * Note: FillRect() excludes right and bottom of rectangle.
2277 */
2278 rc.left = FILL_X(col1);
2279 rc.top = FILL_Y(row1);
2280 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2281 rc.bottom = FILL_Y(row2 + 1);
2282 clear_rect(&rc);
2283}
2284
2285/*
2286 * Clear the whole text window.
2287 */
2288 void
2289gui_mch_clear_all(void)
2290{
2291 RECT rc;
2292
2293 rc.left = 0;
2294 rc.top = 0;
2295 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2296 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2297 clear_rect(&rc);
2298}
2299/*
2300 * Menu stuff.
2301 */
2302
2303 void
2304gui_mch_enable_menu(int flag)
2305{
2306#ifdef FEAT_MENU
2307 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2308#endif
2309}
2310
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002311 void
2312gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002313 int x UNUSED,
2314 int y UNUSED,
2315 int w UNUSED,
2316 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002317{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002318 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319}
2320
2321#if defined(FEAT_MENU) || defined(PROTO)
2322/*
2323 * Make menu item hidden or not hidden
2324 */
2325 void
2326gui_mch_menu_hidden(
2327 vimmenu_T *menu,
2328 int hidden)
2329{
2330 /*
2331 * This doesn't do what we want. Hmm, just grey the menu items for now.
2332 */
2333 /*
2334 if (hidden)
2335 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2336 else
2337 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2338 */
2339 gui_mch_menu_grey(menu, hidden);
2340}
2341
2342/*
2343 * This is called after setting all the menus to grey/hidden or not.
2344 */
2345 void
2346gui_mch_draw_menubar(void)
2347{
2348 DrawMenuBar(s_hwnd);
2349}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002350#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352/*
2353 * Return the RGB value of a pixel as a long.
2354 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002355 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002356gui_mch_get_rgb(guicolor_T pixel)
2357{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002358 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2359 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002360}
2361
2362#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002363/*
2364 * Convert pixels in X to dialog units
2365 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366 static WORD
2367PixelToDialogX(int numPixels)
2368{
2369 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2370}
2371
Bram Moolenaar734a8672019-12-02 22:49:38 +01002372/*
2373 * Convert pixels in Y to dialog units
2374 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002375 static WORD
2376PixelToDialogY(int numPixels)
2377{
2378 return (WORD)((numPixels * 8) / s_dlgfntheight);
2379}
2380
Bram Moolenaar734a8672019-12-02 22:49:38 +01002381/*
2382 * Return the width in pixels of the given text in the given DC.
2383 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384 static int
2385GetTextWidth(HDC hdc, char_u *str, int len)
2386{
2387 SIZE size;
2388
2389 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2390 return size.cx;
2391}
2392
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393/*
2394 * Return the width in pixels of the given text in the given DC, taking care
2395 * of 'encoding' to active codepage conversion.
2396 */
2397 static int
2398GetTextWidthEnc(HDC hdc, char_u *str, int len)
2399{
2400 SIZE size;
2401 WCHAR *wstr;
2402 int n;
2403 int wlen = len;
2404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002405 wstr = enc_to_utf16(str, &wlen);
2406 if (wstr == NULL)
2407 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002409 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2410 vim_free(wstr);
2411 if (n)
2412 return size.cx;
2413 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002416static void get_work_area(RECT *spi_rect);
2417
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002418/*
2419 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002420 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2421 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002422 */
2423 static BOOL
2424CenterWindow(
2425 HWND hwndChild,
2426 HWND hwndParent)
2427{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002428 HMONITOR mon;
2429 MONITORINFO moninfo;
2430 RECT rChild, rParent, rScreen;
2431 int wChild, hChild, wParent, hParent;
2432 int xNew, yNew;
2433 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434
2435 GetWindowRect(hwndChild, &rChild);
2436 wChild = rChild.right - rChild.left;
2437 hChild = rChild.bottom - rChild.top;
2438
Bram Moolenaar734a8672019-12-02 22:49:38 +01002439 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002440 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002441 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442 else
2443 GetWindowRect(hwndParent, &rParent);
2444 wParent = rParent.right - rParent.left;
2445 hParent = rParent.bottom - rParent.top;
2446
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002447 moninfo.cbSize = sizeof(MONITORINFO);
2448 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2449 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002451 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002452 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002453 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002454 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002455 hdc = GetDC(hwndChild);
2456 rScreen.left = 0;
2457 rScreen.top = 0;
2458 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2459 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2460 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002461 }
2462
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002463 xNew = rParent.left + ((wParent - wChild) / 2);
2464 if (xNew < rScreen.left)
2465 xNew = rScreen.left;
2466 else if ((xNew + wChild) > rScreen.right)
2467 xNew = rScreen.right - wChild;
2468
2469 yNew = rParent.top + ((hParent - hChild) / 2);
2470 if (yNew < rScreen.top)
2471 yNew = rScreen.top;
2472 else if ((yNew + hChild) > rScreen.bottom)
2473 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002474
2475 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2476 SWP_NOSIZE | SWP_NOZORDER);
2477}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002478#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480#if defined(FEAT_TOOLBAR) || defined(PROTO)
2481 void
2482gui_mch_show_toolbar(int showit)
2483{
2484 if (s_toolbarhwnd == NULL)
2485 return;
2486
2487 if (showit)
2488 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002489 // Enable unicode support
2490 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2491 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002492 ShowWindow(s_toolbarhwnd, SW_SHOW);
2493 }
2494 else
2495 ShowWindow(s_toolbarhwnd, SW_HIDE);
2496}
2497
Bram Moolenaar734a8672019-12-02 22:49:38 +01002498// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002499# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002500
2501#endif
2502
2503#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2504 static void
2505add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2506{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002507 WCHAR *wn;
2508 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002510 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002511 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002512 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002513
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002514 infow.cbSize = sizeof(infow);
2515 infow.fMask = MIIM_TYPE | MIIM_ID;
2516 infow.wID = item_id;
2517 infow.fType = MFT_STRING;
2518 infow.dwTypeData = wn;
2519 infow.cch = (UINT)wcslen(wn);
2520 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2521 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002522}
2523
2524 static void
2525show_tabline_popup_menu(void)
2526{
2527 HMENU tab_pmenu;
2528 long rval;
2529 POINT pt;
2530
Bram Moolenaar734a8672019-12-02 22:49:38 +01002531 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002532 if (hold_gui_events
2533# ifdef FEAT_CMDWIN
2534 || cmdwin_type != 0
2535# endif
2536 )
2537 return;
2538
2539 tab_pmenu = CreatePopupMenu();
2540 if (tab_pmenu == NULL)
2541 return;
2542
2543 if (first_tabpage->tp_next != NULL)
2544 add_tabline_popup_menu_entry(tab_pmenu,
2545 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2546 add_tabline_popup_menu_entry(tab_pmenu,
2547 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2548 add_tabline_popup_menu_entry(tab_pmenu,
2549 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2550
2551 GetCursorPos(&pt);
2552 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2553 NULL);
2554
2555 DestroyMenu(tab_pmenu);
2556
Bram Moolenaar734a8672019-12-02 22:49:38 +01002557 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002558 if (rval > 0)
2559 {
2560 TCHITTESTINFO htinfo;
2561 int idx;
2562
2563 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2564 return;
2565
2566 htinfo.pt.x = pt.x;
2567 htinfo.pt.y = pt.y;
2568 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2569 if (idx == -1)
2570 idx = 0;
2571 else
2572 idx += 1;
2573
2574 send_tabline_menu_event(idx, (int)rval);
2575 }
2576}
2577
2578/*
2579 * Show or hide the tabline.
2580 */
2581 void
2582gui_mch_show_tabline(int showit)
2583{
2584 if (s_tabhwnd == NULL)
2585 return;
2586
2587 if (!showit != !showing_tabline)
2588 {
2589 if (showit)
2590 ShowWindow(s_tabhwnd, SW_SHOW);
2591 else
2592 ShowWindow(s_tabhwnd, SW_HIDE);
2593 showing_tabline = showit;
2594 }
2595}
2596
2597/*
2598 * Return TRUE when tabline is displayed.
2599 */
2600 int
2601gui_mch_showing_tabline(void)
2602{
2603 return s_tabhwnd != NULL && showing_tabline;
2604}
2605
2606/*
2607 * Update the labels of the tabline.
2608 */
2609 void
2610gui_mch_update_tabline(void)
2611{
2612 tabpage_T *tp;
2613 TCITEM tie;
2614 int nr = 0;
2615 int curtabidx = 0;
2616 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002618
2619 if (s_tabhwnd == NULL)
2620 return;
2621
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002622 // Enable unicode support
2623 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624
2625 tie.mask = TCIF_TEXT;
2626 tie.iImage = -1;
2627
Bram Moolenaar734a8672019-12-02 22:49:38 +01002628 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002629 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2630
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2633 {
2634 if (tp == curtab)
2635 curtabidx = nr;
2636
2637 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2638 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002639 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002640 tie.pszText = "-Empty-";
2641 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2642 tabadded = 1;
2643 }
2644
2645 get_tabline_label(tp, FALSE);
2646 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002648 wstr = enc_to_utf16(NameBuff, NULL);
2649 if (wstr != NULL)
2650 {
2651 TCITEMW tiw;
2652
2653 tiw.mask = TCIF_TEXT;
2654 tiw.iImage = -1;
2655 tiw.pszText = wstr;
2656 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2657 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002658 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002659 }
2660
Bram Moolenaar734a8672019-12-02 22:49:38 +01002661 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2663 TabCtrl_DeleteItem(s_tabhwnd, nr);
2664
2665 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2666 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2667
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2670 RedrawWindow(s_tabhwnd, NULL, NULL,
2671 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2672
2673 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2674 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2675}
2676
2677/*
2678 * Set the current tab to "nr". First tab is 1.
2679 */
2680 void
2681gui_mch_set_curtab(int nr)
2682{
2683 if (s_tabhwnd == NULL)
2684 return;
2685
2686 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2687 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2688}
2689
2690#endif
2691
2692/*
2693 * ":simalt" command.
2694 */
2695 void
2696ex_simalt(exarg_T *eap)
2697{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002698 char_u *keys = eap->arg;
2699 int fill_typebuf = FALSE;
2700 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701
2702 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2703 while (*keys)
2704 {
2705 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002706 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2708 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002709 fill_typebuf = TRUE;
2710 }
2711 if (fill_typebuf)
2712 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002713 // Put a NOP in the typeahead buffer so that the message will get
2714 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002715 key_name[0] = K_SPECIAL;
2716 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002717 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002718 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002719#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002720 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002721#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002722 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 }
2724}
2725
2726/*
2727 * Create the find & replace dialogs.
2728 * You can't have both at once: ":find" when replace is showing, destroys
2729 * the replace dialog first, and the other way around.
2730 */
2731#ifdef MSWIN_FIND_REPLACE
2732 static void
2733initialise_findrep(char_u *initial_string)
2734{
2735 int wword = FALSE;
2736 int mcase = !p_ic;
2737 char_u *entry_text;
2738
Bram Moolenaar734a8672019-12-02 22:49:38 +01002739 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2741
2742 s_findrep_struct.hwndOwner = s_hwnd;
2743 s_findrep_struct.Flags = FR_DOWN;
2744 if (mcase)
2745 s_findrep_struct.Flags |= FR_MATCHCASE;
2746 if (wword)
2747 s_findrep_struct.Flags |= FR_WHOLEWORD;
2748 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002749 {
2750 WCHAR *p = enc_to_utf16(entry_text, NULL);
2751 if (p != NULL)
2752 {
2753 int len = s_findrep_struct.wFindWhatLen - 1;
2754
2755 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2756 s_findrep_struct.lpstrFindWhat[len] = NUL;
2757 vim_free(p);
2758 }
2759 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 vim_free(entry_text);
2761}
2762#endif
2763
2764 static void
2765set_window_title(HWND hwnd, char *title)
2766{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002767 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002768 {
2769 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002770
Bram Moolenaar734a8672019-12-02 22:49:38 +01002771 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002772 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2773 if (wbuf != NULL)
2774 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002775 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002776 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777 }
2778 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002779 else
2780 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781}
2782
2783 void
2784gui_mch_find_dialog(exarg_T *eap)
2785{
2786#ifdef MSWIN_FIND_REPLACE
2787 if (s_findrep_msg != 0)
2788 {
2789 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2790 DestroyWindow(s_findrep_hwnd);
2791
2792 if (!IsWindow(s_findrep_hwnd))
2793 {
2794 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002795 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002796 }
2797
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002798 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 (void)SetFocus(s_findrep_hwnd);
2800
2801 s_findrep_is_find = TRUE;
2802 }
2803#endif
2804}
2805
2806
2807 void
2808gui_mch_replace_dialog(exarg_T *eap)
2809{
2810#ifdef MSWIN_FIND_REPLACE
2811 if (s_findrep_msg != 0)
2812 {
2813 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2814 DestroyWindow(s_findrep_hwnd);
2815
2816 if (!IsWindow(s_findrep_hwnd))
2817 {
2818 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002819 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820 }
2821
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002822 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 (void)SetFocus(s_findrep_hwnd);
2824
2825 s_findrep_is_find = FALSE;
2826 }
2827#endif
2828}
2829
2830
2831/*
2832 * Set visibility of the pointer.
2833 */
2834 void
2835gui_mch_mousehide(int hide)
2836{
2837 if (hide != gui.pointer_hidden)
2838 {
2839 ShowCursor(!hide);
2840 gui.pointer_hidden = hide;
2841 }
2842}
2843
2844#ifdef FEAT_MENU
2845 static void
2846gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2847{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002848 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002849 gui_mch_mousehide(FALSE);
2850
2851 (void)TrackPopupMenu(
2852 (HMENU)menu->submenu_id,
2853 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2854 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002855 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002856 s_hwnd,
2857 NULL);
2858 /*
2859 * NOTE: The pop-up menu can eat the mouse up event.
2860 * We deal with this in normal.c.
2861 */
2862}
2863#endif
2864
2865/*
2866 * Got a message when the system will go down.
2867 */
2868 static void
2869_OnEndSession(void)
2870{
2871 getout_preserve_modified(1);
2872}
2873
2874/*
2875 * Get this message when the user clicks on the cross in the top right corner
2876 * of a Windows95 window.
2877 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002878 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002879_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880{
2881 gui_shell_closed();
2882}
2883
2884/*
2885 * Get a message when the window is being destroyed.
2886 */
2887 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002888_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889{
2890 if (!destroying)
2891 _OnClose(hwnd);
2892}
2893
2894 static void
2895_OnPaint(
2896 HWND hwnd)
2897{
2898 if (!IsMinimized(hwnd))
2899 {
2900 PAINTSTRUCT ps;
2901
Bram Moolenaar734a8672019-12-02 22:49:38 +01002902 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904
Bram Moolenaar734a8672019-12-02 22:49:38 +01002905 // prevent multi-byte characters from misprinting on an invalid
2906 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907 if (has_mbyte)
2908 {
2909 RECT rect;
2910
2911 GetClientRect(hwnd, &rect);
2912 ps.rcPaint.left = rect.left;
2913 ps.rcPaint.right = rect.right;
2914 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915
2916 if (!IsRectEmpty(&ps.rcPaint))
2917 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2919 ps.rcPaint.right - ps.rcPaint.left + 1,
2920 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2921 }
2922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 EndPaint(hwnd, &ps);
2924 }
2925}
2926
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002927 static void
2928_OnSize(
2929 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002930 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931 int cx,
2932 int cy)
2933{
K.Takatac81e9bf2022-01-16 14:15:49 +00002934 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002935 {
2936 gui_resize_shell(cx, cy);
2937
Bram Moolenaar734a8672019-12-02 22:49:38 +01002938 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 }
2941}
2942
2943 static void
2944_OnSetFocus(
2945 HWND hwnd,
2946 HWND hwndOldFocus)
2947{
2948 gui_focus_change(TRUE);
2949 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002950 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002951}
2952
2953 static void
2954_OnKillFocus(
2955 HWND hwnd,
2956 HWND hwndNewFocus)
2957{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002958 if (destroying)
2959 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002960 gui_focus_change(FALSE);
2961 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002962 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002963}
2964
2965/*
2966 * Get a message when the user switches back to vim
2967 */
2968 static LRESULT
2969_OnActivateApp(
2970 HWND hwnd,
2971 BOOL fActivate,
2972 DWORD dwThreadId)
2973{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002974 // we call gui_focus_change() in _OnSetFocus()
2975 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002976 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002977}
2978
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002979 void
2980gui_mch_destroy_scrollbar(scrollbar_T *sb)
2981{
2982 DestroyWindow(sb->id);
2983}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984
2985/*
2986 * Get current mouse coordinates in text window.
2987 */
2988 void
2989gui_mch_getmouse(int *x, int *y)
2990{
2991 RECT rct;
2992 POINT mp;
2993
2994 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002995 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002996 *x = (int)(mp.x - rct.left);
2997 *y = (int)(mp.y - rct.top);
2998}
2999
3000/*
3001 * Move mouse pointer to character at (x, y).
3002 */
3003 void
3004gui_mch_setmouse(int x, int y)
3005{
3006 RECT rct;
3007
3008 (void)GetWindowRect(s_textArea, &rct);
3009 (void)SetCursorPos(x + gui.border_offset + rct.left,
3010 y + gui.border_offset + rct.top);
3011}
3012
3013 static void
3014gui_mswin_get_valid_dimensions(
3015 int w,
3016 int h,
3017 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003018 int *valid_h,
3019 int *cols,
3020 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021{
3022 int base_width, base_height;
3023
3024 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003025 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3026 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003027 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003028 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3029 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3030 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3031 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003032 *cols = (w - base_width) / gui.char_width;
3033 *rows = (h - base_height) / gui.char_height;
3034 *valid_w = base_width + *cols * gui.char_width;
3035 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036}
3037
3038 void
3039gui_mch_flash(int msec)
3040{
3041 RECT rc;
3042
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003043#if defined(FEAT_DIRECTX)
3044 if (IS_ENABLE_DIRECTX())
3045 DWriteContext_Flush(s_dwc);
3046#endif
3047
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003048 /*
3049 * Note: InvertRect() excludes right and bottom of rectangle.
3050 */
3051 rc.left = 0;
3052 rc.top = 0;
3053 rc.right = gui.num_cols * gui.char_width;
3054 rc.bottom = gui.num_rows * gui.char_height;
3055 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003056 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057
Bram Moolenaar734a8672019-12-02 22:49:38 +01003058 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059
3060 InvertRect(s_hdc, &rc);
3061}
3062
3063/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003064 * Check if the specified point is on-screen. (multi-monitor aware)
3065 */
3066 static BOOL
3067is_point_onscreen(int x, int y)
3068{
3069 POINT pt = {x, y};
3070
3071 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3072}
3073
3074/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003075 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003076 *
3077 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3078 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3079 * only works when the whole window is on-screen.
3080 */
3081 static BOOL
3082is_window_onscreen(HWND hwnd)
3083{
3084 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003085 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003086
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003087 GetClientRect(hwnd, &rc);
3088 p1.x = rc.left;
3089 p1.y = rc.top;
3090 p2.x = rc.right - 1;
3091 p2.y = rc.bottom - 1;
3092 ClientToScreen(hwnd, &p1);
3093 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003094
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003095 if (!is_point_onscreen(p1.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(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003098 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003099 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003100 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003101 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003102 return FALSE;
3103 return TRUE;
3104}
3105
3106/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107 * Return flags used for scrolling.
3108 * The SW_INVALIDATE is required when part of the window is covered or
3109 * off-screen. Refer to MS KB Q75236.
3110 */
3111 static int
3112get_scroll_flags(void)
3113{
3114 HWND hwnd;
3115 RECT rcVim, rcOther, rcDest;
3116
Bram Moolenaar185577e2020-10-29 20:08:21 +01003117 // Check if the window is (partly) off-screen.
3118 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119 return SW_INVALIDATE;
3120
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003121 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003122 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003123 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3124 if (IsWindowVisible(hwnd))
3125 {
3126 GetWindowRect(hwnd, &rcOther);
3127 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3128 return SW_INVALIDATE;
3129 }
3130 return 0;
3131}
3132
3133/*
3134 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3135 * may not be scrolled out properly.
3136 * For gVim, when _OnScroll() is repeated, the character at the
3137 * previous cursor position may be left drawn after scroll.
3138 * The problem can be avoided by calling GetPixel() to get a pixel in
3139 * the region before ScrollWindowEx().
3140 */
3141 static void
3142intel_gpu_workaround(void)
3143{
3144 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3145}
3146
3147/*
3148 * Delete the given number of lines from the given row, scrolling up any
3149 * text further down within the scroll region.
3150 */
3151 void
3152gui_mch_delete_lines(
3153 int row,
3154 int num_lines)
3155{
3156 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003157
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003158 rc.left = FILL_X(gui.scroll_region_left);
3159 rc.right = FILL_X(gui.scroll_region_right + 1);
3160 rc.top = FILL_Y(row);
3161 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3162
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003164 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003165 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003166 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003167 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003168 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003169#endif
3170 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003171#if defined(FEAT_DIRECTX)
3172 if (IS_ENABLE_DIRECTX())
3173 DWriteContext_Flush(s_dwc);
3174#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003175 intel_gpu_workaround();
3176 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003178 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003179 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180
Bram Moolenaar734a8672019-12-02 22:49:38 +01003181 // This seems to be required to avoid the cursor disappearing when
3182 // scrolling such that the cursor ends up in the top-left character on
3183 // the screen... But why? (Webb)
3184 // It's probably fixed by disabling drawing the cursor while scrolling.
3185 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186
3187 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3188 gui.scroll_region_left,
3189 gui.scroll_region_bot, gui.scroll_region_right);
3190}
3191
3192/*
3193 * Insert the given number of lines before the given row, scrolling down any
3194 * following text within the scroll region.
3195 */
3196 void
3197gui_mch_insert_lines(
3198 int row,
3199 int num_lines)
3200{
3201 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003202
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003203 rc.left = FILL_X(gui.scroll_region_left);
3204 rc.right = FILL_X(gui.scroll_region_right + 1);
3205 rc.top = FILL_Y(row);
3206 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003207
3208#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003209 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003210 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003211 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003212 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003213 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003214#endif
3215 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003216#if defined(FEAT_DIRECTX)
3217 if (IS_ENABLE_DIRECTX())
3218 DWriteContext_Flush(s_dwc);
3219#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003220 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003221 // The SW_INVALIDATE is required when part of the window is covered or
3222 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003223 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003225 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003226 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003227
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228 gui_clear_block(row, gui.scroll_region_left,
3229 row + num_lines - 1, gui.scroll_region_right);
3230}
3231
3232
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003234gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235{
3236#if defined(FEAT_DIRECTX)
3237 DWriteContext_Close(s_dwc);
3238 DWrite_Final();
3239 s_dwc = NULL;
3240#endif
3241
3242 ReleaseDC(s_textArea, s_hdc);
3243 DeleteObject(s_brush);
3244
3245#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003246 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003247 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3248#endif
3249
Bram Moolenaar734a8672019-12-02 22:49:38 +01003250 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003251 if (s_hwnd != NULL)
3252 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003253 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003254 DestroyWindow(s_hwnd);
3255 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256}
3257
3258 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003259logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003260{
3261 char *p;
3262 char *res;
3263 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003264 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003265 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003266 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003268 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3269 if (font_name == NULL)
3270 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003272 quality_name = quality_id2name((int)lf.lfQuality);
3273
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003274 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003275 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003276 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003277 if (res != NULL)
3278 {
3279 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003280 // make a normal font string out of the lf thing:
3281 points = pixels_to_points(
3282 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3283 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3284 sprintf((char *)p, "%s:h%d", font_name, points);
3285 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003286 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003287 while (*p)
3288 {
3289 if (*p == ' ')
3290 *p = '_';
3291 ++p;
3292 }
3293 if (lf.lfItalic)
3294 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003295 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296 STRCAT(p, ":b");
3297 if (lf.lfUnderline)
3298 STRCAT(p, ":u");
3299 if (lf.lfStrikeOut)
3300 STRCAT(p, ":s");
3301 if (charset_name != NULL)
3302 {
3303 STRCAT(p, ":c");
3304 STRCAT(p, charset_name);
3305 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003306 if (quality_name != NULL)
3307 {
3308 STRCAT(p, ":q");
3309 STRCAT(p, quality_name);
3310 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003311 }
3312
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003313 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003314 return (char_u *)res;
3315}
3316
3317
3318#ifdef FEAT_MBYTE_IME
3319/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003320 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003321 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003322 */
3323 static void
3324update_im_font(void)
3325{
K.Takatac81e9bf2022-01-16 14:15:49 +00003326 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327
3328 if (p_guifontwide != NULL && *p_guifontwide != NUL
3329 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003330 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331 norm_logfont = lf_wide;
3332 else
3333 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003334
3335 lf = norm_logfont;
3336 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3337 // Work around when PerMonitorV2 is not enabled in the process level.
3338 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3339 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340}
3341#endif
3342
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343/*
3344 * Handler of gui.wide_font (p_guifontwide) changed notification.
3345 */
3346 void
3347gui_mch_wide_font_changed(void)
3348{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003349 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003351#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003353#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354
3355 gui_mch_free_font(gui.wide_ital_font);
3356 gui.wide_ital_font = NOFONT;
3357 gui_mch_free_font(gui.wide_bold_font);
3358 gui.wide_bold_font = NOFONT;
3359 gui_mch_free_font(gui.wide_boldital_font);
3360 gui.wide_boldital_font = NOFONT;
3361
3362 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003363 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003364 {
3365 if (!lf.lfItalic)
3366 {
3367 lf.lfItalic = TRUE;
3368 gui.wide_ital_font = get_font_handle(&lf);
3369 lf.lfItalic = FALSE;
3370 }
3371 if (lf.lfWeight < FW_BOLD)
3372 {
3373 lf.lfWeight = FW_BOLD;
3374 gui.wide_bold_font = get_font_handle(&lf);
3375 if (!lf.lfItalic)
3376 {
3377 lf.lfItalic = TRUE;
3378 gui.wide_boldital_font = get_font_handle(&lf);
3379 }
3380 }
3381 }
3382}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003383
3384/*
3385 * Initialise vim to use the font with the given name.
3386 * Return FAIL if the font could not be loaded, OK otherwise.
3387 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003389gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003390{
K.Takatac81e9bf2022-01-16 14:15:49 +00003391 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392 GuiFont font = NOFONT;
3393 char_u *p;
3394
Bram Moolenaar734a8672019-12-02 22:49:38 +01003395 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003397 {
3398 lfOrig = lf;
3399 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003401 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003402 if (font == NOFONT)
3403 return FAIL;
3404
3405 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003406 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003407#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003408 norm_logfont = lf;
3409 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003410 if (!s_in_dpichanged)
3411 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412#endif
3413 gui_mch_free_font(gui.norm_font);
3414 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003415 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416 GetFontSize(font);
3417
K.Takatac81e9bf2022-01-16 14:15:49 +00003418 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419 if (p != NULL)
3420 {
3421 hl_set_font_name(p);
3422
Bram Moolenaar734a8672019-12-02 22:49:38 +01003423 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003424 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3425 {
3426 vim_free(p_guifont);
3427 p_guifont = p;
3428 }
3429 else
3430 vim_free(p);
3431 }
3432
3433 gui_mch_free_font(gui.ital_font);
3434 gui.ital_font = NOFONT;
3435 gui_mch_free_font(gui.bold_font);
3436 gui.bold_font = NOFONT;
3437 gui_mch_free_font(gui.boldital_font);
3438 gui.boldital_font = NOFONT;
3439
3440 if (!lf.lfItalic)
3441 {
3442 lf.lfItalic = TRUE;
3443 gui.ital_font = get_font_handle(&lf);
3444 lf.lfItalic = FALSE;
3445 }
3446 if (lf.lfWeight < FW_BOLD)
3447 {
3448 lf.lfWeight = FW_BOLD;
3449 gui.bold_font = get_font_handle(&lf);
3450 if (!lf.lfItalic)
3451 {
3452 lf.lfItalic = TRUE;
3453 gui.boldital_font = get_font_handle(&lf);
3454 }
3455 }
3456
3457 return OK;
3458}
3459
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460/*
3461 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003462 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463 */
3464 int
3465gui_mch_maximized(void)
3466{
3467 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003468 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469
3470 wp.length = sizeof(WINDOWPLACEMENT);
3471 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003472 {
3473 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003475 && wp.flags == WPF_RESTORETOMAXIMIZED))
3476 return TRUE;
3477 if (wp.showCmd == SW_SHOWMINIMIZED)
3478 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003480 // Assume the window is snapped when the sizes from two APIs differ.
3481 GetWindowRect(s_hwnd, &rc);
3482 if ((rc.right - rc.left !=
3483 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3484 || (rc.bottom - rc.top !=
3485 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3486 return TRUE;
3487 }
3488 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003489}
3490
3491/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003492 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3493 * is set. Compute the new Rows and Columns. This is like resizing the
3494 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003495 */
3496 void
3497gui_mch_newfont(void)
3498{
3499 RECT rect;
3500
3501 GetWindowRect(s_hwnd, &rect);
3502 if (win_socket_id == 0)
3503 {
3504 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003505 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3506 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003508 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3509 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3510 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3511 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512 }
3513 else
3514 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003515 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003517 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 }
3519}
3520
3521/*
3522 * Set the window title
3523 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003524 void
3525gui_mch_settitle(
3526 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003527 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003528{
3529 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3530}
3531
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003532#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003533// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3534// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003535static LPCSTR mshape_idcs[] =
3536{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003537 IDC_ARROW, // arrow
3538 MAKEINTRESOURCE(0), // blank
3539 IDC_IBEAM, // beam
3540 IDC_SIZENS, // updown
3541 IDC_SIZENS, // udsizing
3542 IDC_SIZEWE, // leftright
3543 IDC_SIZEWE, // lrsizing
3544 IDC_WAIT, // busy
3545 IDC_NO, // no
3546 IDC_ARROW, // crosshair
3547 IDC_ARROW, // hand1
3548 IDC_ARROW, // hand2
3549 IDC_ARROW, // pencil
3550 IDC_ARROW, // question
3551 IDC_ARROW, // right-arrow
3552 IDC_UPARROW, // up-arrow
3553 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003554};
3555
3556 void
3557mch_set_mouse_shape(int shape)
3558{
3559 LPCSTR idc;
3560
3561 if (shape == MSHAPE_HIDE)
3562 ShowCursor(FALSE);
3563 else
3564 {
3565 if (shape >= MSHAPE_NUMBERED)
3566 idc = IDC_ARROW;
3567 else
3568 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003569 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003570 if (!p_mh)
3571 {
3572 POINT mp;
3573
Bram Moolenaar734a8672019-12-02 22:49:38 +01003574 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003575 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003576 (void)SetCursorPos(mp.x, mp.y);
3577 ShowCursor(TRUE);
3578 }
3579 }
3580}
3581#endif
3582
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003583#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003584/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003585 * Wide version of convert_filter().
3586 */
3587 static WCHAR *
3588convert_filterW(char_u *s)
3589{
3590 char_u *tmp;
3591 int len;
3592 WCHAR *res;
3593
3594 tmp = convert_filter(s);
3595 if (tmp == NULL)
3596 return NULL;
3597 len = (int)STRLEN(s) + 3;
3598 res = enc_to_utf16(tmp, &len);
3599 vim_free(tmp);
3600 return res;
3601}
3602
3603/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003604 * Pop open a file browser and return the file selected, in allocated memory,
3605 * or NULL if Cancel is hit.
3606 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3607 * title - Title message for the file browser dialog.
3608 * dflt - Default name of file.
3609 * ext - Default extension to be added to files without extensions.
3610 * initdir - directory in which to open the browser (NULL = current dir)
3611 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003612 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003613 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003614gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 int saving,
3616 char_u *title,
3617 char_u *dflt,
3618 char_u *ext,
3619 char_u *initdir,
3620 char_u *filter)
3621{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003622 // We always use the wide function. This means enc_to_utf16() must work,
3623 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003624 OPENFILENAMEW fileStruct;
3625 WCHAR fileBuf[MAXPATHL];
3626 WCHAR *wp;
3627 int i;
3628 WCHAR *titlep = NULL;
3629 WCHAR *extp = NULL;
3630 WCHAR *initdirp = NULL;
3631 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003632 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003633 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003634
3635 if (dflt == NULL)
3636 fileBuf[0] = NUL;
3637 else
3638 {
3639 wp = enc_to_utf16(dflt, NULL);
3640 if (wp == NULL)
3641 fileBuf[0] = NUL;
3642 else
3643 {
3644 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3645 fileBuf[i] = wp[i];
3646 fileBuf[i] = NUL;
3647 vim_free(wp);
3648 }
3649 }
3650
Bram Moolenaar734a8672019-12-02 22:49:38 +01003651 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652 filterp = convert_filterW(filter);
3653
Bram Moolenaara80faa82020-04-12 19:37:17 +02003654 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003655# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003656 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003657 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003658# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003660# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003661
3662 if (title != NULL)
3663 titlep = enc_to_utf16(title, NULL);
3664 fileStruct.lpstrTitle = titlep;
3665
3666 if (ext != NULL)
3667 extp = enc_to_utf16(ext, NULL);
3668 fileStruct.lpstrDefExt = extp;
3669
3670 fileStruct.lpstrFile = fileBuf;
3671 fileStruct.nMaxFile = MAXPATHL;
3672 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003673 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3674 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675 if (initdir != NULL && *initdir != NUL)
3676 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003677 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003678 initdirp = enc_to_utf16(initdir, NULL);
3679 if (initdirp != NULL)
3680 {
3681 for (wp = initdirp; *wp != NUL; ++wp)
3682 if (*wp == '/')
3683 *wp = '\\';
3684 }
3685 fileStruct.lpstrInitialDir = initdirp;
3686 }
3687
3688 /*
3689 * TODO: Allow selection of multiple files. Needs another arg to this
3690 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3691 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3692 * files that don't exist yet, so I haven't put it in. What about
3693 * OFN_PATHMUSTEXIST?
3694 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3695 */
3696 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003697# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003698 if (curbuf->b_p_bin)
3699 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003700# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003701 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003702 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003703 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003704 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705
3706 vim_free(filterp);
3707 vim_free(initdirp);
3708 vim_free(titlep);
3709 vim_free(extp);
3710
K.Takata14b8d6a2022-01-20 15:05:22 +00003711 if (!ret)
3712 return NULL;
3713
3714 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003716 if (p == NULL)
3717 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718
Bram Moolenaar734a8672019-12-02 22:49:38 +01003719 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 SetFocus(s_hwnd);
3721
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003723 q = vim_strsave(shorten_fname1(p));
3724 vim_free(p);
3725 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003727
3728
3729/*
3730 * Convert the string s to the proper format for a filter string by replacing
3731 * the \t and \n delimiters with \0.
3732 * Returns the converted string in allocated memory.
3733 *
3734 * Keep in sync with convert_filterW() above!
3735 */
3736 static char_u *
3737convert_filter(char_u *s)
3738{
3739 char_u *res;
3740 unsigned s_len = (unsigned)STRLEN(s);
3741 unsigned i;
3742
3743 res = alloc(s_len + 3);
3744 if (res != NULL)
3745 {
3746 for (i = 0; i < s_len; ++i)
3747 if (s[i] == '\t' || s[i] == '\n')
3748 res[i] = '\0';
3749 else
3750 res[i] = s[i];
3751 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003752 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003753 res[s_len + 1] = NUL;
3754 res[s_len + 2] = NUL;
3755 }
3756 return res;
3757}
3758
3759/*
3760 * Select a directory.
3761 */
3762 char_u *
3763gui_mch_browsedir(char_u *title, char_u *initdir)
3764{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003765 // We fake this: Use a filter that doesn't select anything and a default
3766 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3768 initdir, (char_u *)_("Directory\t*.nothing\n"));
3769}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003770#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 static void
3773_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003774 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775 HDROP hDrop)
3776{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003777#define BUFPATHLEN _MAX_PATH
3778#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 char szFile[BUFPATHLEN];
3781 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3782 UINT i;
3783 char_u **fnames;
3784 POINT pt;
3785 int_u modifiers = 0;
3786
Bram Moolenaar734a8672019-12-02 22:49:38 +01003787 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003788 DragQueryPoint(hDrop, &pt);
3789 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3790
3791 reset_VIsual();
3792
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003793 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794
3795 if (fnames != NULL)
3796 for (i = 0; i < cFiles; ++i)
3797 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003798 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3799 fnames[i] = utf16_to_enc(wszFile, NULL);
3800 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801 {
3802 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3803 fnames[i] = vim_strsave((char_u *)szFile);
3804 }
3805 }
3806
3807 DragFinish(hDrop);
3808
3809 if (fnames != NULL)
3810 {
LemonBoy45684c62022-04-24 15:46:42 +01003811 int kbd_modifiers = get_active_modifiers();
3812
3813 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003815 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003816 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003817 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003818 modifiers |= MOUSE_ALT;
3819
3820 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3821
3822 s_need_activate = TRUE;
3823 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003824}
3825
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003826 static int
3827_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003828 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829 HWND hwndCtl,
3830 UINT code,
3831 int pos)
3832{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003833 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834 scrollbar_T *sb, *sb_info;
3835 long val;
3836 int dragging = FALSE;
3837 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838 SCROLLINFO si;
3839
3840 si.cbSize = sizeof(si);
3841 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003842
3843 sb = gui_mswin_find_scrollbar(hwndCtl);
3844 if (sb == NULL)
3845 return 0;
3846
Bram Moolenaar734a8672019-12-02 22:49:38 +01003847 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003848 {
3849 /*
3850 * Careful: need to get scrollbar info out of first (left) scrollbar
3851 * for window, but keep real scrollbar too because we must pass it to
3852 * gui_drag_scrollbar().
3853 */
3854 sb_info = &sb->wp->w_scrollbars[0];
3855 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003856 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003857 sb_info = sb;
3858 val = sb_info->value;
3859
3860 switch (code)
3861 {
3862 case SB_THUMBTRACK:
3863 val = pos;
3864 dragging = TRUE;
3865 if (sb->scroll_shift > 0)
3866 val <<= sb->scroll_shift;
3867 break;
3868 case SB_LINEDOWN:
3869 val++;
3870 break;
3871 case SB_LINEUP:
3872 val--;
3873 break;
3874 case SB_PAGEDOWN:
3875 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3876 break;
3877 case SB_PAGEUP:
3878 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3879 break;
3880 case SB_TOP:
3881 val = 0;
3882 break;
3883 case SB_BOTTOM:
3884 val = sb_info->max;
3885 break;
3886 case SB_ENDSCROLL:
3887 if (prev_code == SB_THUMBTRACK)
3888 {
3889 /*
3890 * "pos" only gives us 16-bit data. In case of large file,
3891 * use GetScrollPos() which returns 32-bit. Unfortunately it
3892 * is not valid while the scrollbar is being dragged.
3893 */
3894 val = GetScrollPos(hwndCtl, SB_CTL);
3895 if (sb->scroll_shift > 0)
3896 val <<= sb->scroll_shift;
3897 }
3898 break;
3899
3900 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003901 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003902 return 0;
3903 }
3904 prev_code = code;
3905
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3907 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908
3909 /*
3910 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3911 */
3912 if (sb->wp != NULL)
3913 {
3914 scrollbar_T *sba = sb->wp->w_scrollbars;
3915 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3916
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918 }
3919
Bram Moolenaar734a8672019-12-02 22:49:38 +01003920 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921 s_busy_processing = TRUE;
3922
Bram Moolenaar734a8672019-12-02 22:49:38 +01003923 // When "allow_scrollbar" is FALSE still need to remember the new
3924 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003925 dont_scroll = !allow_scrollbar;
3926
Bram Moolenaara338adc2018-01-31 20:51:47 +01003927 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003929 mch_enable_flush();
3930 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931
3932 s_busy_processing = FALSE;
3933 dont_scroll = dont_scroll_save;
3934
3935 return 0;
3936}
3937
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939#ifdef FEAT_XPM_W32
3940# include "xpm_w32.h"
3941#endif
3942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar734a8672019-12-02 22:49:38 +01003944// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945#define TEAROFF_PADDING_X 2
3946#define TEAROFF_BUTTON_PAD_X 8
3947#define TEAROFF_MIN_WIDTH 200
3948#define TEAROFF_SUBMENU_LABEL ">>"
3949#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3950
3951
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003952#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953# define ID_BEVAL_TOOLTIP 200
3954# define BEVAL_TEXT_LEN MAXPATHL
3955
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003957static UINT_PTR beval_timer_id = 0;
3958static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003959#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003960
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003961
Bram Moolenaar734a8672019-12-02 22:49:38 +01003962// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964#ifdef FEAT_MENU
3965static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
3968/*
3969 * Use the system font for dialogs and tear-off menus. Remove this line to
3970 * use DLG_FONT_NAME.
3971 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003972#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#define VIM_CLASSW L"Vim"
3976
Bram Moolenaar734a8672019-12-02 22:49:38 +01003977// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3978// thus there should be room for every dialog. For tearoffs it's made bigger
3979// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#define DLG_ALLOC_SIZE 16 * 1024
3981
3982/*
3983 * stuff for dialogs, menus, tearoffs etc.
3984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985static PWORD
3986add_dialog_element(
3987 PWORD p,
3988 DWORD lStyle,
3989 WORD x,
3990 WORD y,
3991 WORD w,
3992 WORD h,
3993 WORD Id,
3994 WORD clss,
3995 const char *caption);
3996static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003997static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003998#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001static void get_dialog_font_metrics(void);
4002
4003static int dialog_default_button = -1;
4004
Bram Moolenaar734a8672019-12-02 22:49:38 +01004005// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006static int mouse_scroll_lines = 0;
LemonBoy365d8f72022-05-05 19:23:07 +01004007static int mouse_scroll_chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#ifdef FEAT_TOOLBAR
4010static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004011static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004012static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004014#else
4015# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
4017
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004018#ifdef FEAT_GUI_TABLINE
4019static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004020static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004021#endif
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#ifdef FEAT_MBYTE_IME
4024static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4025static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4026#endif
4027#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4028# ifdef NOIME
4029typedef struct tagCOMPOSITIONFORM {
4030 DWORD dwStyle;
4031 POINT ptCurrentPos;
4032 RECT rcArea;
4033} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4034typedef HANDLE HIMC;
4035# endif
4036
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004037static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004038static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4039static HIMC (WINAPI *pImmGetContext)(HWND);
4040static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4041static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4042static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4043static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004044static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4045static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004046static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4047static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004048static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049static void dyn_imm_load(void);
4050#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051# define pImmGetCompositionStringW ImmGetCompositionStringW
4052# define pImmGetContext ImmGetContext
4053# define pImmAssociateContext ImmAssociateContext
4054# define pImmReleaseContext ImmReleaseContext
4055# define pImmGetOpenStatus ImmGetOpenStatus
4056# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004057# define pImmGetCompositionFontW ImmGetCompositionFontW
4058# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059# define pImmSetCompositionWindow ImmSetCompositionWindow
4060# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004061# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#endif
4063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064#ifdef FEAT_MENU
4065/*
4066 * Figure out how high the menu bar is at the moment.
4067 */
4068 static int
4069gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004070 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
4072 static int old_menu_height = -1;
4073
4074 RECT rc1, rc2;
4075 int num;
4076 int menu_height;
4077
4078 if (gui.menu_is_active)
4079 num = GetMenuItemCount(s_menuBar);
4080 else
4081 num = 0;
4082
4083 if (num == 0)
4084 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004085 else if (IsMinimized(s_hwnd))
4086 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004087 // The height of the menu cannot be determined while the window is
4088 // minimized. Take the previous height if the menu is changed in that
4089 // state, to avoid that Vim's vertical window size accidentally
4090 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004091 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else
4094 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004095 /*
4096 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4097 * seem to have been set yet, so menu wraps in default window
4098 * width which is very narrow. Instead just return height of a
4099 * single menu item. Will still be wrong when the menu really
4100 * should wrap over more than one line.
4101 */
4102 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4103 if (gui.starting)
4104 menu_height = rc1.bottom - rc1.top + 1;
4105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004107 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4108 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 }
4111
4112 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004113 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004114 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 return menu_height;
4117}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004118#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120
4121/*
4122 * Setup for the Intellimouse
4123 */
LemonBoyc27747e2022-05-07 12:25:40 +01004124 static long
4125mouse_vertical_scroll_step(void)
4126{
4127 UINT val;
4128 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4129 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4130 return 3; // Safe default;
4131}
4132
4133 static long
4134mouse_horizontal_scroll_step(void)
4135{
4136 UINT val;
4137 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4138 return (long)val;
4139 return 3; // Safe default;
4140}
4141
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 static void
4143init_mouse_wheel(void)
4144{
LemonBoyc27747e2022-05-07 12:25:40 +01004145 // Get the default values for the horizontal and vertical scroll steps from
4146 // the system.
4147 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4148 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149}
4150
Bram Moolenaarae20f342019-11-05 21:09:23 +01004151/*
4152 * Intellimouse wheel handler.
4153 * Treat a mouse wheel event as if it were a scroll request.
4154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 static void
LemonBoy365d8f72022-05-05 19:23:07 +01004156_OnMouseWheel(HWND hwnd, short zDelta, LPARAM param, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
LemonBoy365d8f72022-05-05 19:23:07 +01004158 int button;
4159 win_T *wp;
4160 int modifiers, kbd_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
Bram Moolenaarae20f342019-11-05 21:09:23 +01004162 wp = gui_mouse_window(FIND_POPUP);
4163
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004164#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004165 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004166 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004167 cmdarg_T cap;
4168 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004169
Bram Moolenaarae20f342019-11-05 21:09:23 +01004170 // Mouse hovers over popup window, scroll it if possible.
4171 mouse_row = wp->w_winrow;
4172 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004173 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004174 if (horizontal)
4175 {
4176 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4177 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4178 }
4179 else
4180 {
4181 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4182 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4183 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004184 clear_oparg(&oa);
4185 cap.oap = &oa;
4186 nv_mousescroll(&cap);
4187 update_screen(0);
4188 setcursor();
4189 out_flush();
4190 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004191 }
4192#endif
4193
Bram Moolenaarae20f342019-11-05 21:09:23 +01004194 if (wp == NULL || !p_scf)
4195 wp = curwin;
4196
LemonBoy365d8f72022-05-05 19:23:07 +01004197 // Translate the scroll event into an event that Vim can process so that
4198 // the user has a chance to map the scrollwheel buttons.
4199 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004200 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 else
LemonBoy365d8f72022-05-05 19:23:07 +01004202 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004203
4204 kbd_modifiers = get_active_modifiers();
4205
4206 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4207 modifiers |= MOUSE_SHIFT;
4208 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4209 modifiers |= MOUSE_CTRL;
4210 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4211 modifiers |= MOUSE_ALT;
4212
4213 mch_disable_flush();
LemonBoyc27747e2022-05-07 12:25:40 +01004214 gui_send_mouse_event(button, GET_X_LPARAM(param), GET_Y_LPARAM(param),
LemonBoy365d8f72022-05-05 19:23:07 +01004215 FALSE, kbd_modifiers);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004216 mch_enable_flush();
4217 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218}
4219
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004220#ifdef USE_SYSMENU_FONT
4221/*
4222 * Get Menu Font.
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004226gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004227{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004228 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004229
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004230 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4231 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004232 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004233 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004234 &nm,
4235 0))
4236 return FAIL;
4237 *lf = nm.lfMenuFont;
4238 return OK;
4239}
4240#endif
4241
4242
4243#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4244/*
4245 * Set the GUI tabline font to the system menu font
4246 */
4247 static void
4248set_tabline_font(void)
4249{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004250 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004251 HFONT font;
4252 HWND hwnd;
4253 HDC hdc;
4254 HFONT hfntOld;
4255 TEXTMETRIC tm;
4256
4257 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4258 return;
4259
K.Takatac81e9bf2022-01-16 14:15:49 +00004260 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004261 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004262
4263 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4264
4265 /*
4266 * Compute the height of the font used for the tab text
4267 */
4268 hwnd = GetDesktopWindow();
4269 hdc = GetWindowDC(hwnd);
4270 hfntOld = SelectFont(hdc, font);
4271
4272 GetTextMetrics(hdc, &tm);
4273
4274 SelectFont(hdc, hfntOld);
4275 ReleaseDC(hwnd, hdc);
4276
4277 /*
4278 * The space used by the tab border and the space between the tab label
4279 * and the tab border is included as 7.
4280 */
4281 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4282}
K.Takatac81e9bf2022-01-16 14:15:49 +00004283#else
4284# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004285#endif
4286
Bram Moolenaar520470a2005-06-16 21:59:56 +00004287/*
4288 * Invoked when a setting was changed.
4289 */
4290 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004291_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004292{
LemonBoy365d8f72022-05-05 19:23:07 +01004293 switch (param)
4294 {
4295 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004296 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004297 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004298 case SPI_SETWHEELSCROLLCHARS:
4299 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004300 break;
4301 case SPI_SETNONCLIENTMETRICS:
4302 set_tabline_font();
4303 break;
4304 default:
4305 break;
4306 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004307 return 0;
4308}
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_NETBEANS_INTG
4311 static void
4312_OnWindowPosChanged(
4313 HWND hwnd,
4314 const LPWINDOWPOS lpwpos)
4315{
4316 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004317 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4320 || lpwpos->cx != cx || lpwpos->cy != cy))
4321 {
4322 x = lpwpos->x;
4323 y = lpwpos->y;
4324 cx = lpwpos->cx;
4325 cy = lpwpos->cy;
4326 netbeans_frame_moved(x, y);
4327 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004328 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004329 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330}
4331#endif
4332
K.Takata4f2417f2021-06-05 16:25:32 +02004333
4334static HWND hwndTip = NULL;
4335
4336 static void
4337show_sizing_tip(int cols, int rows)
4338{
4339 TOOLINFOA ti = {sizeof(ti)};
4340 char buf[32];
4341
4342 ti.hwnd = s_hwnd;
4343 ti.uId = (UINT_PTR)s_hwnd;
4344 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4345 ti.lpszText = buf;
4346 sprintf(buf, "%dx%d", cols, rows);
4347 if (hwndTip == NULL)
4348 {
4349 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4350 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4351 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4352 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4353 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4354 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4355 }
4356 else
4357 {
4358 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4359 }
4360 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4361}
4362
4363 static void
4364destroy_sizing_tip(void)
4365{
4366 if (hwndTip != NULL)
4367 {
4368 DestroyWindow(hwndTip);
4369 hwndTip = NULL;
4370 }
4371}
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 static int
4374_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 UINT fwSide,
4376 LPRECT lprc)
4377{
4378 int w, h;
4379 int valid_w, valid_h;
4380 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004381 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383 w = lprc->right - lprc->left;
4384 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004385 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 w_offset = w - valid_w;
4387 h_offset = h - valid_h;
4388
4389 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4390 || fwSide == WMSZ_BOTTOMLEFT)
4391 lprc->left += w_offset;
4392 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4393 || fwSide == WMSZ_BOTTOMRIGHT)
4394 lprc->right -= w_offset;
4395
4396 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4397 || fwSide == WMSZ_TOPRIGHT)
4398 lprc->top += h_offset;
4399 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4400 || fwSide == WMSZ_BOTTOMRIGHT)
4401 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004402
4403 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return TRUE;
4405}
4406
K.Takata92000e22022-01-20 15:10:57 +00004407#ifdef FEAT_GUI_TABLINE
4408 static void
4409_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4410{
4411 if (gui_mch_showing_tabline())
4412 {
4413 POINT pt;
4414 RECT rect;
4415
4416 /*
4417 * If the cursor is on the tabline, display the tab menu
4418 */
4419 GetCursorPos(&pt);
4420 GetWindowRect(s_textArea, &rect);
4421 if (pt.y < rect.top)
4422 {
4423 show_tabline_popup_menu();
4424 return;
4425 }
4426 }
4427 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4428}
4429
4430 static void
4431_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4432{
4433 /*
4434 * If the user double clicked the tabline, create a new tab
4435 */
4436 if (gui_mch_showing_tabline())
4437 {
4438 POINT pt;
4439 RECT rect;
4440
4441 GetCursorPos(&pt);
4442 GetWindowRect(s_textArea, &rect);
4443 if (pt.y < rect.top)
4444 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4445 }
4446 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4447}
4448#endif
4449
4450 static UINT
4451_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4452{
4453 UINT result;
4454 int x, y;
4455
4456 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4457 if (result != HTCLIENT)
4458 return result;
4459
4460#ifdef FEAT_GUI_TABLINE
4461 if (gui_mch_showing_tabline())
4462 {
4463 RECT rct;
4464
4465 // If the cursor is on the GUI tabline, don't process this event
4466 GetWindowRect(s_textArea, &rct);
4467 if (yPos < rct.top)
4468 return result;
4469 }
4470#endif
4471 (void)gui_mch_get_winpos(&x, &y);
4472 xPos -= x;
4473
4474 if (xPos < 48) // <VN> TODO should use system metric?
4475 return HTBOTTOMLEFT;
4476 else
4477 return HTBOTTOMRIGHT;
4478}
4479
4480#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4481 static LRESULT
4482_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4483{
4484 switch (hdr->code)
4485 {
4486 case TTN_GETDISPINFOW:
4487 case TTN_GETDISPINFO:
4488 {
4489 char_u *str = NULL;
4490 static void *tt_text = NULL;
4491
4492 VIM_CLEAR(tt_text);
4493
4494# ifdef FEAT_GUI_TABLINE
4495 if (gui_mch_showing_tabline()
4496 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4497 {
4498 POINT pt;
4499 /*
4500 * Mouse is over the GUI tabline. Display the
4501 * tooltip for the tab under the cursor
4502 *
4503 * Get the cursor position within the tab control
4504 */
4505 GetCursorPos(&pt);
4506 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4507 {
4508 TCHITTESTINFO htinfo;
4509 int idx;
4510
4511 /*
4512 * Get the tab under the cursor
4513 */
4514 htinfo.pt.x = pt.x;
4515 htinfo.pt.y = pt.y;
4516 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4517 if (idx != -1)
4518 {
4519 tabpage_T *tp;
4520
4521 tp = find_tabpage(idx + 1);
4522 if (tp != NULL)
4523 {
4524 get_tabline_label(tp, TRUE);
4525 str = NameBuff;
4526 }
4527 }
4528 }
4529 }
4530# endif
4531# ifdef FEAT_TOOLBAR
4532# ifdef FEAT_GUI_TABLINE
4533 else
4534# endif
4535 {
4536 UINT idButton;
4537 vimmenu_T *pMenu;
4538
4539 idButton = (UINT) hdr->idFrom;
4540 pMenu = gui_mswin_find_menu(root_menu, idButton);
4541 if (pMenu)
4542 str = pMenu->strings[MENU_INDEX_TIP];
4543 }
4544# endif
4545 if (str == NULL)
4546 break;
4547
4548 // Set the maximum width, this also enables using \n for
4549 // line break.
4550 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4551
4552 if (hdr->code == TTN_GETDISPINFOW)
4553 {
4554 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4555
4556 tt_text = enc_to_utf16(str, NULL);
4557 lpdi->lpszText = tt_text;
4558 // can't show tooltip if failed
4559 }
4560 else
4561 {
4562 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4563
4564 if (STRLEN(str) < sizeof(lpdi->szText)
4565 || ((tt_text = vim_strsave(str)) == NULL))
4566 vim_strncpy((char_u *)lpdi->szText, str,
4567 sizeof(lpdi->szText) - 1);
4568 else
4569 lpdi->lpszText = tt_text;
4570 }
4571 }
4572 break;
4573
4574# ifdef FEAT_GUI_TABLINE
4575 case TCN_SELCHANGE:
4576 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4577 {
4578 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4579 return 0L;
4580 }
4581 break;
4582
4583 case NM_RCLICK:
4584 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4585 {
4586 show_tabline_popup_menu();
4587 return 0L;
4588 }
4589 break;
4590# endif
4591
4592 default:
4593 break;
4594 }
4595 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4596}
4597#endif
4598
4599#if defined(MENUHINTS) && defined(FEAT_MENU)
4600 static LRESULT
4601_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4602{
4603 if (((UINT) HIWORD(wParam)
4604 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4605 == MF_HILITE
4606 && (State & CMDLINE) == 0)
4607 {
4608 UINT idButton;
4609 vimmenu_T *pMenu;
4610 static int did_menu_tip = FALSE;
4611
4612 if (did_menu_tip)
4613 {
4614 msg_clr_cmdline();
4615 setcursor();
4616 out_flush();
4617 did_menu_tip = FALSE;
4618 }
4619
4620 idButton = (UINT)LOWORD(wParam);
4621 pMenu = gui_mswin_find_menu(root_menu, idButton);
4622 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4623 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4624 {
4625 ++msg_hist_off;
4626 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4627 --msg_hist_off;
4628 setcursor();
4629 out_flush();
4630 did_menu_tip = TRUE;
4631 }
4632 return 0L;
4633 }
4634 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4635}
4636#endif
4637
K.Takatac81e9bf2022-01-16 14:15:49 +00004638 static LRESULT
4639_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4640{
4641 s_dpi = ydpi;
4642 s_in_dpichanged = TRUE;
4643 //TRACE("DPI: %d", ydpi);
4644
4645 update_scrollbar_size();
4646 update_toolbar_size();
4647 set_tabline_font();
4648
4649 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4650 gui_get_wide_font();
4651 gui_mswin_get_menu_height(FALSE);
4652#ifdef FEAT_MBYTE_IME
4653 im_set_position(gui.row, gui.col);
4654#endif
4655 InvalidateRect(hwnd, NULL, TRUE);
4656
4657 s_in_dpichanged = FALSE;
4658 return 0L;
4659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661
4662 static LRESULT CALLBACK
4663_WndProc(
4664 HWND hwnd,
4665 UINT uMsg,
4666 WPARAM wParam,
4667 LPARAM lParam)
4668{
LemonBoy77fc0b02022-04-22 22:45:52 +01004669 // TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4670 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 HandleMouseHide(uMsg, lParam);
4673
4674 s_uMsg = uMsg;
4675 s_wParam = wParam;
4676 s_lParam = lParam;
4677
4678 switch (uMsg)
4679 {
4680 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4681 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004682 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004684 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4686 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4687 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4688 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4689#ifdef FEAT_MENU
4690 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4691#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004692 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4693 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4695 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004696 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4697 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4699 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4700 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4701#ifdef FEAT_NETBEANS_INTG
4702 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4703#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004704#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004705 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4706 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004707#endif
K.Takata92000e22022-01-20 15:10:57 +00004708 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004709
Bram Moolenaar734a8672019-12-02 22:49:38 +01004710 case WM_QUERYENDSESSION: // System wants to go down.
4711 gui_shell_closed(); // Will exit when no changed buffers.
4712 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004715 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004718 return 0L;
4719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 break;
4721
4722 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004723 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4724 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004725 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return 0L;
4727
4728 case WM_SYSCHAR:
4729 /*
4730 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4731 * shortcut key, handle like a typed ALT key, otherwise call Windows
4732 * ALT key handling.
4733 */
4734#ifdef FEAT_MENU
4735 if ( !gui.menu_is_active
4736 || p_wak[0] == 'n'
4737 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4738 )
4739#endif
4740 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004741 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return 0L;
4743 }
4744#ifdef FEAT_MENU
4745 else
K.Takata4ac893f2022-01-20 12:44:28 +00004746 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#endif
4748
4749 case WM_SYSKEYUP:
4750#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004751 // This used to be done only when menu is active: ALT key is used for
4752 // that. But that caused problems when menu is disabled and using
4753 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4754 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004755 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004757 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759
K.Takata4f2417f2021-06-05 16:25:32 +02004760 case WM_EXITSIZEMOVE:
4761 destroy_sizing_tip();
4762 break;
4763
Bram Moolenaar734a8672019-12-02 22:49:38 +01004764 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766
4767 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004768 case WM_MOUSEHWHEEL:
4769 _OnMouseWheel(hwnd, HIWORD(wParam), lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004770 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
Bram Moolenaar734a8672019-12-02 22:49:38 +01004772 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004773 case WM_SETTINGCHANGE:
4774 return _OnSettingChange((UINT)wParam);
4775
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004776#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004778 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779#endif
K.Takata92000e22022-01-20 15:10:57 +00004780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#if defined(MENUHINTS) && defined(FEAT_MENU)
4782 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004783 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786#ifdef FEAT_MBYTE_IME
4787 case WM_IME_NOTIFY:
4788 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004789 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004790 return 1L;
4791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 case WM_IME_COMPOSITION:
4793 if (!_OnImeComposition(hwnd, wParam, 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004797 case WM_DPICHANGED:
4798 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4799 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004803 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#endif
K.Takata92000e22022-01-20 15:10:57 +00004806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808
K.Takata4ac893f2022-01-20 12:44:28 +00004809 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810}
4811
4812/*
4813 * End of call-back routines
4814 */
4815
Bram Moolenaar734a8672019-12-02 22:49:38 +01004816// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817HWND vim_parent_hwnd = NULL;
4818
4819 static BOOL CALLBACK
4820FindWindowTitle(HWND hwnd, LPARAM lParam)
4821{
4822 char buf[2048];
4823 char *title = (char *)lParam;
4824
4825 if (GetWindowText(hwnd, buf, sizeof(buf)))
4826 {
4827 if (strstr(buf, title) != NULL)
4828 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004829 // Found it. Store the window ref. and quit searching if MDI
4830 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004832 if (vim_parent_hwnd != NULL)
4833 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004836 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837}
4838
4839/*
4840 * Invoked for '-P "title"' argument: search for parent application to open
4841 * our window in.
4842 */
4843 void
4844gui_mch_set_parent(char *title)
4845{
4846 EnumWindows(FindWindowTitle, (LPARAM)title);
4847 if (vim_parent_hwnd == NULL)
4848 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004849 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 mch_exit(2);
4851 }
4852}
4853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 static void
4856ole_error(char *arg)
4857{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004858 char buf[IOSIZE];
4859
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004860# ifdef VIMDLL
4861 gui.in_use = mch_is_gui_executable();
4862# endif
4863
Bram Moolenaar734a8672019-12-02 22:49:38 +01004864 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004865 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004866 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004867 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004871#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4872 static char *
4873gvim_error(void)
4874{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004875 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004876
4877 if (starting)
4878 {
4879 mch_errmsg(msg);
4880 mch_errmsg("\n");
4881 mch_exit(2);
4882 }
4883 return msg;
4884}
4885
4886 char *
4887gui_mch_do_spawn(char_u *arg)
4888{
4889 int len;
4890# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4891 char_u *session = NULL;
4892 LPWSTR tofree1 = NULL;
4893# endif
4894 WCHAR name[MAX_PATH];
4895 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4896 STARTUPINFOW si = {sizeof(si)};
4897 PROCESS_INFORMATION pi;
4898
4899 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4900 goto error;
4901 p = wcsrchr(name, L'\\');
4902 if (p == NULL)
4903 goto error;
4904 // Replace the executable name from vim(d).exe to gvim(d).exe.
4905# ifdef DEBUG
4906 wcscpy(p + 1, L"gvimd.exe");
4907# else
4908 wcscpy(p + 1, L"gvim.exe");
4909# endif
4910
4911# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4912 if (starting)
4913# endif
4914 {
4915 // Pass the command line to the new process.
4916 p = GetCommandLineW();
4917 // Skip 1st argument.
4918 while (*p && *p != L' ' && *p != L'\t')
4919 {
4920 if (*p == L'"')
4921 {
4922 while (*p && *p != L'"')
4923 ++p;
4924 if (*p)
4925 ++p;
4926 }
4927 else
4928 ++p;
4929 }
4930 cmd = p;
4931 }
4932# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4933 else
4934 {
4935 // Create a session file and pass it to the new process.
4936 LPWSTR wsession;
4937 char_u *savebg;
4938 int ret;
4939
4940 session = vim_tempname('s', FALSE);
4941 if (session == NULL)
4942 goto error;
4943 savebg = p_bg;
4944 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4945 ret = write_session_file(session);
4946 vim_free(p_bg);
4947 p_bg = savebg;
4948 if (!ret)
4949 goto error;
4950 wsession = enc_to_utf16(session, NULL);
4951 if (wsession == NULL)
4952 goto error;
4953 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004954 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004955 if (cmd == NULL)
4956 {
4957 vim_free(wsession);
4958 goto error;
4959 }
4960 tofree1 = cmd;
4961 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4962 wsession, wsession);
4963 vim_free(wsession);
4964 }
4965# endif
4966
4967 // Check additional arguments to the `:gui` command.
4968 if (arg != NULL)
4969 {
4970 warg = enc_to_utf16(arg, NULL);
4971 if (warg == NULL)
4972 goto error;
4973 tofree2 = warg;
4974 }
4975 else
4976 warg = L"";
4977
4978 // Set up the new command line.
4979 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004980 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004981 if (newcmd == NULL)
4982 goto error;
4983 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4984
4985 // Spawn a new GUI process.
4986 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4987 NULL, NULL, &si, &pi))
4988 goto error;
4989 CloseHandle(pi.hProcess);
4990 CloseHandle(pi.hThread);
4991 mch_exit(0);
4992
4993error:
4994# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4995 if (session)
4996 mch_remove(session);
4997 vim_free(session);
4998 vim_free(tofree1);
4999# endif
5000 vim_free(newcmd);
5001 vim_free(tofree2);
5002 return gvim_error();
5003}
5004#endif
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006/*
5007 * Parse the GUI related command-line arguments. Any arguments used are
5008 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005009 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 */
5011 void
5012gui_mch_prepare(int *argc, char **argv)
5013{
5014 int silent = FALSE;
5015 int idx;
5016
Bram Moolenaar734a8672019-12-02 22:49:38 +01005017 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005020 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5022 && (argv[2][0] == '-' || argv[2][0] == '/'))
5023 {
5024 silent = TRUE;
5025 idx = 2;
5026 }
5027 else
5028 idx = 1;
5029
Bram Moolenaar734a8672019-12-02 22:49:38 +01005030 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (STRICMP(argv[idx] + 1, "register") == 0)
5032 {
5033#ifdef FEAT_OLE
5034 RegisterMe(silent);
5035 mch_exit(0);
5036#else
5037 if (!silent)
5038 ole_error("register");
5039 mch_exit(2);
5040#endif
5041 }
5042
Bram Moolenaar734a8672019-12-02 22:49:38 +01005043 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5045 {
5046#ifdef FEAT_OLE
5047 UnregisterMe(!silent);
5048 mch_exit(0);
5049#else
5050 if (!silent)
5051 ole_error("unregister");
5052 mch_exit(2);
5053#endif
5054 }
5055
Bram Moolenaar734a8672019-12-02 22:49:38 +01005056 // Ignore an -embedding argument. It is only relevant if the
5057 // application wants to treat the case when it is started manually
5058 // differently from the case where it is started via automation (and
5059 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5061 {
5062#ifdef FEAT_OLE
5063 *argc = 1;
5064#else
5065 ole_error("embedding");
5066 mch_exit(2);
5067#endif
5068 }
5069 }
5070
5071#ifdef FEAT_OLE
5072 {
5073 int bDoRestart = FALSE;
5074
5075 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005076 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (bDoRestart)
5078 mch_exit(0);
5079 }
5080#endif
5081
5082#ifdef FEAT_NETBEANS_INTG
5083 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005084 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 int arg;
5086
5087 for (arg = 1; arg < *argc; arg++)
5088 if (strncmp("-nb", argv[arg], 3) == 0)
5089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 netbeansArg = argv[arg];
5091 mch_memmove(&argv[arg], &argv[arg + 1],
5092 (--*argc - arg) * sizeof(char *));
5093 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005094 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098}
5099
K.Takatac81e9bf2022-01-16 14:15:49 +00005100 static void
5101load_dpi_func(void)
5102{
5103 HMODULE hUser32;
5104
5105 hUser32 = GetModuleHandle("user32.dll");
5106 if (hUser32 == NULL)
5107 goto fail;
5108
5109 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5110 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5111 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5112 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5113 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5114 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5115
5116 if (pSetThreadDpiAwarenessContext != NULL)
5117 {
5118 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5119 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5120 if (oldctx != NULL)
5121 {
5122 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5123 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5124#ifdef DEBUG
5125 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5126 {
5127 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5128 }
5129#endif
5130 return;
5131 }
5132 }
5133
5134fail:
5135 // Disable PerMonitorV2 APIs.
5136 pGetDpiForSystem = stubGetDpiForSystem;
5137 pGetDpiForWindow = NULL;
5138 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5139 pSetThreadDpiAwarenessContext = NULL;
5140 pGetAwarenessFromDpiAwarenessContext = NULL;
5141}
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143/*
5144 * Initialise the GUI. Create all the windows, set up all the call-backs
5145 * etc.
5146 */
5147 int
5148gui_mch_init(void)
5149{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005151 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
Bram Moolenaar734a8672019-12-02 22:49:38 +01005154 // Return here if the window was already opened (happens when
5155 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005157 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159 /*
5160 * Load the tearoff bitmap
5161 */
5162#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005163 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
5165
K.Takatac81e9bf2022-01-16 14:15:49 +00005166 load_dpi_func();
5167
5168 s_dpi = pGetDpiForSystem();
5169 update_scrollbar_size();
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005172 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#endif
5174 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005175#ifdef FEAT_TOOLBAR
5176 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
5179 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5180
Bram Moolenaar734a8672019-12-02 22:49:38 +01005181 // First try using the wide version, so that we can use any title.
5182 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005183 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005185 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 wndclassw.lpfnWndProc = _WndProc;
5187 wndclassw.cbClsExtra = 0;
5188 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005189 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5191 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5192 wndclassw.hbrBackground = s_brush;
5193 wndclassw.lpszMenuName = NULL;
5194 wndclassw.lpszClassName = szVimWndClassW;
5195
K.Takata4ac893f2022-01-20 12:44:28 +00005196 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (vim_parent_hwnd != NULL)
5201 {
5202#ifdef HAVE_TRY_EXCEPT
5203 __try
5204 {
5205#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005206 // Open inside the specified parent window.
5207 // TODO: last argument should point to a CLIENTCREATESTRUCT
5208 // structure.
5209 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005211 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005212 WS_OVERLAPPEDWINDOW | WS_CHILD
5213 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5215 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005216 100, // Any value will do
5217 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005219 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#ifdef HAVE_TRY_EXCEPT
5221 }
5222 __except(EXCEPTION_EXECUTE_HANDLER)
5223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005224 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226#endif
5227 if (s_hwnd == NULL)
5228 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005229 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 mch_exit(2);
5231 }
5232 }
5233 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005234 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // If the provided windowid is not valid reset it to zero, so that it
5236 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005237 if (IsWindow((HWND)win_socket_id) <= 0)
5238 win_socket_id = 0;
5239
Bram Moolenaar734a8672019-12-02 22:49:38 +01005240 // Create a window. If win_socket_id is not zero without border and
5241 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005242 s_hwnd = CreateWindowW(
5243 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005244 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5245 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005246 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5247 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005248 100, // Any value will do
5249 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005250 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005251 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005252 if (s_hwnd != NULL && win_socket_id != 0)
5253 {
5254 SetParent(s_hwnd, (HWND)win_socket_id);
5255 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5256 }
5257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 if (s_hwnd == NULL)
5260 return FAIL;
5261
K.Takatac81e9bf2022-01-16 14:15:49 +00005262 if (pGetDpiForWindow != NULL)
5263 {
5264 s_dpi = pGetDpiForWindow(s_hwnd);
5265 update_scrollbar_size();
5266 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5267 }
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5270 dyn_imm_load();
5271#endif
5272
Bram Moolenaar734a8672019-12-02 22:49:38 +01005273 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005274 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005275 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005276 wndclassw.style = CS_OWNDC;
5277 wndclassw.lpfnWndProc = _TextAreaWndProc;
5278 wndclassw.cbClsExtra = 0;
5279 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005280 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005281 wndclassw.hIcon = NULL;
5282 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5283 wndclassw.hbrBackground = NULL;
5284 wndclassw.lpszMenuName = NULL;
5285 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005286
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005287 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return FAIL;
5289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005291 s_textArea = CreateWindowExW(
5292 0,
5293 szTextAreaClassW, L"Vim text area",
5294 WS_CHILD | WS_VISIBLE, 0, 0,
5295 100, // Any value will do for now
5296 100, // Any value will do for now
5297 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005298 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (s_textArea == NULL)
5301 return FAIL;
5302
Bram Moolenaar20321902016-02-17 12:30:17 +01005303#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005304 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005305 {
5306 HANDLE hIcon = NULL;
5307
5308 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005309 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005310 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005311#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#ifdef FEAT_MENU
5314 s_menuBar = CreateMenu();
5315#endif
5316 s_hdc = GetDC(s_textArea);
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar734a8672019-12-02 22:49:38 +01005320 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005321 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
Bram Moolenaar734a8672019-12-02 22:49:38 +01005323 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 gui_mch_def_colors();
5325
Bram Moolenaar734a8672019-12-02 22:49:38 +01005326 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5327 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 set_normal_colors();
5329
5330 /*
5331 * Check that none of the colors are the same as the background color.
5332 * Then store the current values as the defaults.
5333 */
5334 gui_check_colors();
5335 gui.def_norm_pixel = gui.norm_pixel;
5336 gui.def_back_pixel = gui.back_pixel;
5337
Bram Moolenaar734a8672019-12-02 22:49:38 +01005338 // Get the colors for the highlight groups (gui_check_colors() might have
5339 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 highlight_gui_started();
5341
5342 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005343 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005345 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 /*
5348 * Set up for Intellimouse processing
5349 */
5350 init_mouse_wheel();
5351
5352 /*
5353 * compute a couple of metrics used for the dialogs
5354 */
5355 get_dialog_font_metrics();
5356#ifdef FEAT_TOOLBAR
5357 /*
5358 * Create the toolbar
5359 */
5360 initialise_toolbar();
5361#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005362#ifdef FEAT_GUI_TABLINE
5363 /*
5364 * Create the tabline
5365 */
5366 initialise_tabline();
5367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#ifdef MSWIN_FIND_REPLACE
5369 /*
5370 * Initialise the dialog box stuff
5371 */
5372 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5373
Bram Moolenaar734a8672019-12-02 22:49:38 +01005374 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005376 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005378 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5380 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5381 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005384#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005385 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005386 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005387#endif
5388
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005389#ifdef FEAT_RENDER_OPTIONS
5390 if (p_rop)
5391 (void)gui_mch_set_rendering_options(p_rop);
5392#endif
5393
Bram Moolenaar748bf032005-02-02 23:04:36 +00005394theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005395 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005396 display_errors();
5397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 return OK;
5399}
5400
5401/*
5402 * Get the size of the screen, taking position on multiple monitors into
5403 * account (if supported).
5404 */
5405 static void
5406get_work_area(RECT *spi_rect)
5407{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005408 HMONITOR mon;
5409 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
Bram Moolenaar734a8672019-12-02 22:49:38 +01005411 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005412 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005413 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005415 moninfo.cbSize = sizeof(MONITORINFO);
5416 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005418 *spi_rect = moninfo.rcWork;
5419 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005422 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5424}
5425
5426/*
5427 * Set the size of the window to the given width and height in pixels.
5428 */
5429 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005430gui_mch_set_shellsize(
5431 int width,
5432 int height,
5433 int min_width UNUSED,
5434 int min_height UNUSED,
5435 int base_width UNUSED,
5436 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005437 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005440 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
Bram Moolenaar734a8672019-12-02 22:49:38 +01005443 // Try to keep window completely on screen.
5444 // Get position of the screen work area. This is the part that is not
5445 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 get_work_area(&workarea_rect);
5447
Bram Moolenaar734a8672019-12-02 22:49:38 +01005448 // Resizing a maximized window looks very strange, unzoom it first.
5449 // But don't do it when still starting up, it may have been requested in
5450 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005451 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005453
5454 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaar734a8672019-12-02 22:49:38 +01005456 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005457 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5458 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5459 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5460 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5461 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5462 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
Bram Moolenaar734a8672019-12-02 22:49:38 +01005464 // The following should take care of keeping Vim on the same monitor, no
5465 // matter if the secondary monitor is left or right of the primary
5466 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005467 window_rect.right = window_rect.left + win_width;
5468 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005469
Bram Moolenaar734a8672019-12-02 22:49:38 +01005470 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005471 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5472 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005474 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5475 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005477 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5478 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005480 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5481 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005483 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5484 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486 SetActiveWindow(s_hwnd);
5487 SetFocus(s_hwnd);
5488
Bram Moolenaar734a8672019-12-02 22:49:38 +01005489 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491}
5492
5493
5494 void
5495gui_mch_set_scrollbar_thumb(
5496 scrollbar_T *sb,
5497 long val,
5498 long size,
5499 long max)
5500{
5501 SCROLLINFO info;
5502
5503 sb->scroll_shift = 0;
5504 while (max > 32767)
5505 {
5506 max = (max + 1) >> 1;
5507 val >>= 1;
5508 size >>= 1;
5509 ++sb->scroll_shift;
5510 }
5511
5512 if (sb->scroll_shift > 0)
5513 ++size;
5514
5515 info.cbSize = sizeof(info);
5516 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5517 info.nPos = val;
5518 info.nMin = 0;
5519 info.nMax = max;
5520 info.nPage = size;
5521 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5522}
5523
5524
5525/*
5526 * Set the current text font.
5527 */
5528 void
5529gui_mch_set_font(GuiFont font)
5530{
5531 gui.currFont = font;
5532}
5533
5534
5535/*
5536 * Set the current text foreground color.
5537 */
5538 void
5539gui_mch_set_fg_color(guicolor_T color)
5540{
5541 gui.currFgColor = color;
5542}
5543
5544/*
5545 * Set the current text background color.
5546 */
5547 void
5548gui_mch_set_bg_color(guicolor_T color)
5549{
5550 gui.currBgColor = color;
5551}
5552
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005553/*
5554 * Set the current text special color.
5555 */
5556 void
5557gui_mch_set_sp_color(guicolor_T color)
5558{
5559 gui.currSpColor = color;
5560}
5561
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005562#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563/*
5564 * Multi-byte handling, originally by Sung-Hoon Baek.
5565 * First static functions (no prototypes generated).
5566 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005567# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005568# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005569# endif
5570# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
5572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 * handle WM_IME_NOTIFY message
5574 */
5575 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005576_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 LRESULT lResult = 0;
5579 HIMC hImc;
5580
5581 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5582 return lResult;
5583 switch (dwCommand)
5584 {
5585 case IMN_SETOPENSTATUS:
5586 if (pImmGetOpenStatus(hImc))
5587 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005588 LOGFONTW lf = norm_logfont;
5589 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5590 // Work around when PerMonitorV2 is not enabled in the process level.
5591 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5592 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 im_set_position(gui.row, gui.col);
5594
Bram Moolenaar734a8672019-12-02 22:49:38 +01005595 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 State &= ~LANGMAP;
5597 if (State & INSERT)
5598 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005599# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005600 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5602 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005603 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 int old_row = gui.row;
5605 int old_col = gui.col;
5606
5607 // This must be called here before
5608 // status_redraw_curbuf(), otherwise the mode
5609 // message may appear in the wrong position.
5610 showmode();
5611 status_redraw_curbuf();
5612 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005613 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 gui.row = old_row;
5615 gui.col = old_col;
5616 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 }
5620 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005621 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 lResult = 0;
5623 break;
5624 }
5625 pImmReleaseContext(hWnd, hImc);
5626 return lResult;
5627}
5628
5629 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005630_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 char_u *ret;
5633 int len;
5634
Bram Moolenaar734a8672019-12-02 22:49:38 +01005635 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 return 0;
5637
5638 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5639 if (ret != NULL)
5640 {
5641 add_to_input_buf_csi(ret, len);
5642 vim_free(ret);
5643 return 1;
5644 }
5645 return 0;
5646}
5647
5648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 * void GetResultStr()
5650 *
5651 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5652 * get complete composition string
5653 */
5654 static char_u *
5655GetResultStr(HWND hwnd, int GCS, int *lenp)
5656{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005657 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005658 LONG ret;
5659 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 char_u *convbuf = NULL;
5661
5662 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5663 return NULL;
5664
K.Takatab0b2b732022-01-19 12:59:21 +00005665 // Get the length of the composition string.
5666 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5667 if (ret <= 0)
5668 return NULL;
5669
5670 // Allocate the requested buffer plus space for the NUL character.
5671 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if (buf == NULL)
5673 return NULL;
5674
K.Takatab0b2b732022-01-19 12:59:21 +00005675 // Reads in the composition string.
5676 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5677 *lenp = ret / sizeof(WCHAR);
5678
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005679 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 pImmReleaseContext(hwnd, hIMC);
5681 vim_free(buf);
5682 return convbuf;
5683}
5684#endif
5685
Bram Moolenaar734a8672019-12-02 22:49:38 +01005686// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005687#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
5689/*
5690 * set font to IM.
5691 */
5692 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005693im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 HIMC hImc;
5696
5697 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5698 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005699 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 pImmReleaseContext(s_hwnd, hImc);
5701 }
5702}
5703
5704/*
5705 * Notify cursor position to IM.
5706 */
5707 void
5708im_set_position(int row, int col)
5709{
5710 HIMC hImc;
5711
5712 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5713 {
5714 COMPOSITIONFORM cfs;
5715
5716 cfs.dwStyle = CFS_POINT;
5717 cfs.ptCurrentPos.x = FILL_X(col);
5718 cfs.ptCurrentPos.y = FILL_Y(row);
5719 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005720 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5721 {
5722 // Work around when PerMonitorV2 is not enabled in the process level.
5723 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5724 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 pImmSetCompositionWindow(hImc, &cfs);
5727
5728 pImmReleaseContext(s_hwnd, hImc);
5729 }
5730}
5731
5732/*
5733 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5734 */
5735 void
5736im_set_active(int active)
5737{
5738 HIMC hImc;
5739 static HIMC hImcOld = (HIMC)0;
5740
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005741# ifdef VIMDLL
5742 if (!gui.in_use && !gui.starting)
5743 {
5744 mbyte_im_set_active(active);
5745 return;
5746 }
5747# endif
5748
Bram Moolenaar734a8672019-12-02 22:49:38 +01005749 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 if (p_imdisable)
5752 {
5753 if (hImcOld == (HIMC)0)
5754 {
5755 hImcOld = pImmGetContext(s_hwnd);
5756 if (hImcOld)
5757 pImmAssociateContext(s_hwnd, (HIMC)0);
5758 }
5759 active = FALSE;
5760 }
5761 else if (hImcOld != (HIMC)0)
5762 {
5763 pImmAssociateContext(s_hwnd, hImcOld);
5764 hImcOld = (HIMC)0;
5765 }
5766
5767 hImc = pImmGetContext(s_hwnd);
5768 if (hImc)
5769 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005770 /*
5771 * for Korean ime
5772 */
5773 HKL hKL = GetKeyboardLayout(0);
5774
5775 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5776 {
5777 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5778 static BOOL bSaved = FALSE;
5779
5780 if (active)
5781 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005782 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005783 if (bSaved)
5784 pImmSetConversionStatus(hImc, dwConversionSaved,
5785 dwSentenceSaved);
5786 bSaved = FALSE;
5787 }
5788 else
5789 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005790 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005791 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5792 &dwSentenceSaved))
5793 {
5794 bSaved = TRUE;
5795 pImmSetConversionStatus(hImc,
5796 dwConversionSaved & ~(IME_CMODE_NATIVE
5797 | IME_CMODE_FULLSHAPE),
5798 dwSentenceSaved);
5799 }
5800 }
5801 }
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 pImmSetOpenStatus(hImc, active);
5804 pImmReleaseContext(s_hwnd, hImc);
5805 }
5806 }
5807}
5808
5809/*
5810 * Get IM status. When IM is on, return not 0. Else return 0.
5811 */
5812 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005813im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 int status = 0;
5816 HIMC hImc;
5817
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005818# ifdef VIMDLL
5819 if (!gui.in_use && !gui.starting)
5820 return mbyte_im_get_status();
5821# endif
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5824 {
5825 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5826 pImmReleaseContext(s_hwnd, hImc);
5827 }
5828 return status;
5829}
5830
Bram Moolenaar734a8672019-12-02 22:49:38 +01005831#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005834/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005835 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005836 */
5837 static void
5838latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5839{
5840 int c;
5841
Bram Moolenaarca003e12006-03-17 23:19:38 +00005842 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005843 {
5844 c = *text++;
5845 switch (c)
5846 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005847 case 0xa4: c = 0x20ac; break; // euro
5848 case 0xa6: c = 0x0160; break; // S hat
5849 case 0xa8: c = 0x0161; break; // S -hat
5850 case 0xb4: c = 0x017d; break; // Z hat
5851 case 0xb8: c = 0x017e; break; // Z -hat
5852 case 0xbc: c = 0x0152; break; // OE
5853 case 0xbd: c = 0x0153; break; // oe
5854 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005855 }
5856 *unicodebuf++ = c;
5857 }
5858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
5860#ifdef FEAT_RIGHTLEFT
5861/*
5862 * What is this for? In the case where you are using Win98 or Win2K or later,
5863 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5864 * reverses the string sent to the TextOut... family. This sucks, because we
5865 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5866 * way to tell Windblows not to do this!
5867 *
5868 * The short of it is that this 'RevOut' only gets called if you are running
5869 * one of the new, "improved" MS OSes, and only if you are running in
5870 * 'rightleft' mode. It makes display take *slightly* longer, but not
5871 * noticeably so.
5872 */
5873 static void
K.Takata135e1522022-01-29 15:27:58 +00005874RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 int col,
5876 int row,
5877 UINT foptions,
5878 CONST RECT *pcliprect,
5879 LPCTSTR text,
5880 UINT len,
5881 CONST INT *padding)
5882{
5883 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005885 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005886 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005887 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888}
5889#endif
5890
Bram Moolenaar92467d32017-12-05 13:22:16 +01005891 static void
5892draw_line(
5893 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005894 int y1,
5895 int x2,
5896 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005897 COLORREF color)
5898{
5899#if defined(FEAT_DIRECTX)
5900 if (IS_ENABLE_DIRECTX())
5901 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5902 else
5903#endif
5904 {
5905 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5906 HPEN old_pen = SelectObject(s_hdc, hpen);
5907 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005908 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005909 LineTo(s_hdc, x2, y2);
5910 DeleteObject(SelectObject(s_hdc, old_pen));
5911 }
5912}
5913
5914 static void
5915set_pixel(
5916 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005917 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005918 COLORREF color)
5919{
5920#if defined(FEAT_DIRECTX)
5921 if (IS_ENABLE_DIRECTX())
5922 DWriteContext_SetPixel(s_dwc, x, y, color);
5923 else
5924#endif
5925 SetPixel(s_hdc, x, y, color);
5926}
5927
5928 static void
5929fill_rect(
5930 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005931 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005932 COLORREF color)
5933{
5934#if defined(FEAT_DIRECTX)
5935 if (IS_ENABLE_DIRECTX())
5936 DWriteContext_FillRect(s_dwc, rcp, color);
5937 else
5938#endif
5939 {
5940 HBRUSH hbr2;
5941
5942 if (hbr == NULL)
5943 hbr2 = CreateSolidBrush(color);
5944 else
5945 hbr2 = hbr;
5946 FillRect(s_hdc, rcp, hbr2);
5947 if (hbr == NULL)
5948 DeleteBrush(hbr2);
5949 }
5950}
5951
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 void
5953gui_mch_draw_string(
5954 int row,
5955 int col,
5956 char_u *text,
5957 int len,
5958 int flags)
5959{
5960 static int *padding = NULL;
5961 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 const RECT *pcliprect = NULL;
5963 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 static WCHAR *unicodebuf = NULL;
5965 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005966 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 int y;
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 /*
5971 * Italic and bold text seems to have an extra row of pixels at the bottom
5972 * (below where the bottom of the character should be). If we draw the
5973 * characters with a solid background, the top row of pixels in the
5974 * character below will be overwritten. We can fix this by filling in the
5975 * background ourselves, to the correct character proportions, and then
5976 * writing the character in transparent mode. Still have a problem when
5977 * the character is "_", which gets written on to the character below.
5978 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5979 * pixel in their slots, which fixes the problem with the bottom row of
5980 * pixels. We still need this code because otherwise the top row of pixels
5981 * becomes a problem. - webb.
5982 */
5983 static HBRUSH hbr_cache[2] = {NULL, NULL};
5984 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5985 static int brush_lru = 0;
5986 HBRUSH hbr;
5987 RECT rc;
5988
5989 if (!(flags & DRAW_TRANSP))
5990 {
5991 /*
5992 * Clear background first.
5993 * Note: FillRect() excludes right and bottom of rectangle.
5994 */
5995 rc.left = FILL_X(col);
5996 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (has_mbyte)
5998 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005999 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006000 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 rc.right = FILL_X(col + len);
6004 rc.bottom = FILL_Y(row + 1);
6005
Bram Moolenaar734a8672019-12-02 22:49:38 +01006006 // Cache the created brush, that saves a lot of time. We need two:
6007 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (gui.currBgColor == brush_color[0])
6009 {
6010 hbr = hbr_cache[0];
6011 brush_lru = 1;
6012 }
6013 else if (gui.currBgColor == brush_color[1])
6014 {
6015 hbr = hbr_cache[1];
6016 brush_lru = 0;
6017 }
6018 else
6019 {
6020 if (hbr_cache[brush_lru] != NULL)
6021 DeleteBrush(hbr_cache[brush_lru]);
6022 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6023 brush_color[brush_lru] = gui.currBgColor;
6024 hbr = hbr_cache[brush_lru];
6025 brush_lru = !brush_lru;
6026 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006027
Bram Moolenaar92467d32017-12-05 13:22:16 +01006028 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
6030 SetBkMode(s_hdc, TRANSPARENT);
6031
6032 /*
6033 * When drawing block cursor, prevent inverted character spilling
6034 * over character cell (can happen with bold/italic)
6035 */
6036 if (flags & DRAW_CURSOR)
6037 {
6038 pcliprect = &rc;
6039 foptions = ETO_CLIPPED;
6040 }
6041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 SetTextColor(s_hdc, gui.currFgColor);
6043 SelectFont(s_hdc, gui.currFont);
6044
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006045#ifdef FEAT_DIRECTX
6046 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006047 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006048#endif
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6051 {
K.Takata135e1522022-01-29 15:27:58 +00006052 int i;
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 vim_free(padding);
6055 pad_size = Columns;
6056
Bram Moolenaar734a8672019-12-02 22:49:38 +01006057 // Don't give an out-of-memory message here, it would call us
6058 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006059 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (padding != NULL)
6061 for (i = 0; i < pad_size; i++)
6062 padding[i] = gui.char_width;
6063 }
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 /*
6066 * We have to provide the padding argument because italic and bold versions
6067 * of fixed-width fonts are often one pixel or so wider than their normal
6068 * versions.
6069 * No check for DRAW_BOLD, Windows will have done it already.
6070 */
6071
Bram Moolenaar734a8672019-12-02 22:49:38 +01006072 // Check if there are any UTF-8 characters. If not, use normal text
6073 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (enc_utf8)
6075 for (n = 0; n < len; ++n)
6076 if (text[n] >= 0x80)
6077 break;
6078
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006079#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006080 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6081 // required that unicode drawing routine, currently. So this forces it
6082 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006083 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006084 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006085#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006086
Bram Moolenaar734a8672019-12-02 22:49:38 +01006087 // Check if the Unicode buffer exists and is big enough. Create it
6088 // with the same length as the multi-byte string, the number of wide
6089 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006090 if ((enc_utf8
6091 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6092 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 && (unicodebuf == NULL || len > unibuflen))
6094 {
6095 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006096 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006099 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 unibuflen = len;
6102 }
6103
6104 if (enc_utf8 && n < len && unicodebuf != NULL)
6105 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006106 // Output UTF-8 characters. Composing characters should be
6107 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006108 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006109 int wlen; // string length in words
6110 int clen; // string length in characters
6111 int cells; // cell width of string up to composing char
6112 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006113 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006115 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006116 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006118 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006120 c = utf_ptr2char(text + i);
6121 if (c >= 0x10000)
6122 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006123 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006124 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6125 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006126 }
6127 else
6128 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006129 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006130 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006131
6132 if (utf_iscomposing(c))
6133 cw = 0;
6134 else
6135 {
6136 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006137 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006138 cw = 1;
6139 }
6140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (unicodepdy != NULL)
6142 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006143 // Use unicodepdy to make characters fit as we expect, even
6144 // when the font uses different widths (e.g., bold character
6145 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006146 if (c >= 0x10000)
6147 {
6148 unicodepdy[wlen - 2] = cw * gui.char_width;
6149 unicodepdy[wlen - 1] = 0;
6150 }
6151 else
6152 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 }
6154 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006155 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006156 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006158#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006159 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006160 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006161 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006162 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006163 TEXT_X(col), TEXT_Y(row),
6164 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006165 gui.char_width, gui.currFgColor,
6166 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006167 }
6168 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006169#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006170 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6171 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006172 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006174 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006176 // If we want to display codepage data, and the current CP is not the
6177 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (unicodebuf != NULL)
6179 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006180 if (enc_latin9)
6181 latin9_to_ucs(text, len, unicodebuf);
6182 else
6183 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 MB_PRECOMPOSED,
6185 (char *)text, len,
6186 (LPWSTR)unicodebuf, unibuflen);
6187 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006188 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006189 // Use unicodepdy to make characters fit as we expect, even
6190 // when the font uses different widths (e.g., bold character
6191 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006192 if (unicodepdy != NULL)
6193 {
6194 int i;
6195 int cw;
6196
6197 for (i = 0; i < len; ++i)
6198 {
6199 cw = utf_char2cells(unicodebuf[i]);
6200 if (cw > 2)
6201 cw = 1;
6202 unicodepdy[i] = cw * gui.char_width;
6203 }
6204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006206 foptions, pcliprect, unicodebuf, len, unicodepdy);
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
6209 }
6210 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
6212#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006213 // Windows will mess up RL text, so we have to draw it character by
6214 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006215 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6217 foptions, pcliprect, (char *)text, len, padding);
6218 else
6219#endif
6220 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6221 foptions, pcliprect, (char *)text, len, padding);
6222 }
6223
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (flags & DRAW_UNDERL)
6226 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006227 // When p_linespace is 0, overwrite the bottom row of pixels.
6228 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (p_linespace > 1)
6231 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006232 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006234
Bram Moolenaar734a8672019-12-02 22:49:38 +01006235 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006236 if (flags & DRAW_STRIKE)
6237 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006238 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006239 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006240 }
6241
Bram Moolenaar734a8672019-12-02 22:49:38 +01006242 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006243 if (flags & DRAW_UNDERC)
6244 {
6245 int x;
6246 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006247 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006248
6249 y = FILL_Y(row + 1) - 1;
6250 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6251 {
6252 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006253 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006254 }
6255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256}
6257
6258
6259/*
6260 * Output routines.
6261 */
6262
Bram Moolenaar734a8672019-12-02 22:49:38 +01006263/*
6264 * Flush any output to the screen
6265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 void
6267gui_mch_flush(void)
6268{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006269#if defined(FEAT_DIRECTX)
6270 if (IS_ENABLE_DIRECTX())
6271 DWriteContext_Flush(s_dwc);
6272#endif
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 GdiFlush();
6275}
6276
6277 static void
6278clear_rect(RECT *rcp)
6279{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006280 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281}
6282
6283
Bram Moolenaarc716c302006-01-21 22:12:51 +00006284 void
6285gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6286{
6287 RECT workarea_rect;
6288
6289 get_work_area(&workarea_rect);
6290
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006291 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006292 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6293 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006294
Bram Moolenaar734a8672019-12-02 22:49:38 +01006295 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6296 // the menubar for MSwin, we subtract it from the screen height, so that
6297 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006298 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006299 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6300 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6301 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6302 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006303}
6304
6305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306#if defined(FEAT_MENU) || defined(PROTO)
6307/*
6308 * Add a sub menu to the menu bar.
6309 */
6310 void
6311gui_mch_add_menu(
6312 vimmenu_T *menu,
6313 int pos)
6314{
6315 vimmenu_T *parent = menu->parent;
6316
6317 menu->submenu_id = CreatePopupMenu();
6318 menu->id = s_menu_id++;
6319
6320 if (menu_is_menubar(menu->name))
6321 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006322 WCHAR *wn;
6323 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006325 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006326 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006327 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006329 infow.cbSize = sizeof(infow);
6330 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6331 | MIIM_SUBMENU;
6332 infow.dwItemData = (long_u)menu;
6333 infow.wID = menu->id;
6334 infow.fType = MFT_STRING;
6335 infow.dwTypeData = wn;
6336 infow.cch = (UINT)wcslen(wn);
6337 infow.hSubMenu = menu->submenu_id;
6338 InsertMenuItemW((parent == NULL)
6339 ? s_menuBar : parent->submenu_id,
6340 (UINT)pos, TRUE, &infow);
6341 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
6343
Bram Moolenaar734a8672019-12-02 22:49:38 +01006344 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (parent == NULL)
6346 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006347# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 else if (IsWindow(parent->tearoff_handle))
6349 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351}
6352
6353 void
6354gui_mch_show_popupmenu(vimmenu_T *menu)
6355{
6356 POINT mp;
6357
K.Takata45f9cfb2022-01-21 11:11:00 +00006358 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6360}
6361
6362 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006363gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 vimmenu_T *menu = gui_find_menu(path_name);
6366
6367 if (menu != NULL)
6368 {
6369 POINT p;
6370
Bram Moolenaar734a8672019-12-02 22:49:38 +01006371 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006373 if (mouse_pos)
6374 {
6375 int mx, my;
6376
6377 gui_mch_getmouse(&mx, &my);
6378 p.x += mx;
6379 p.y += my;
6380 }
6381 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006383 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6385 }
6386 msg_scroll = FALSE;
6387 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6388 }
6389}
6390
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006391# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392/*
6393 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6394 * create it as a pseudo-"tearoff menu".
6395 */
6396 void
6397gui_make_tearoff(char_u *path_name)
6398{
6399 vimmenu_T *menu = gui_find_menu(path_name);
6400
Bram Moolenaar734a8672019-12-02 22:49:38 +01006401 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (menu != NULL)
6403 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6404}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407/*
6408 * Add a menu item to a menu
6409 */
6410 void
6411gui_mch_add_menu_item(
6412 vimmenu_T *menu,
6413 int idx)
6414{
6415 vimmenu_T *parent = menu->parent;
6416
6417 menu->id = s_menu_id++;
6418 menu->submenu_id = NULL;
6419
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006420# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6422 {
6423 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6424 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6425 }
6426 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006427# endif
6428# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (menu_is_toolbar(parent->name))
6430 {
6431 TBBUTTON newtb;
6432
Bram Moolenaara80faa82020-04-12 19:37:17 +02006433 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (menu_is_separator(menu->name))
6435 {
6436 newtb.iBitmap = 0;
6437 newtb.fsStyle = TBSTYLE_SEP;
6438 }
6439 else
6440 {
6441 newtb.iBitmap = get_toolbar_bitmap(menu);
6442 newtb.fsStyle = TBSTYLE_BUTTON;
6443 }
6444 newtb.idCommand = menu->id;
6445 newtb.fsState = TBSTATE_ENABLED;
6446 newtb.iString = 0;
6447 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6448 (LPARAM)&newtb);
6449 menu->submenu_id = (HMENU)-1;
6450 }
6451 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006454 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006456 wn = enc_to_utf16(menu->name, NULL);
6457 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006459 InsertMenuW(parent->submenu_id, (UINT)idx,
6460 (menu_is_separator(menu->name)
6461 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6462 (UINT)menu->id, wn);
6463 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006465# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (IsWindow(parent->tearoff_handle))
6467 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470}
6471
6472/*
6473 * Destroy the machine specific menu widget.
6474 */
6475 void
6476gui_mch_destroy_menu(vimmenu_T *menu)
6477{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006478# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 /*
6480 * is this a toolbar button?
6481 */
6482 if (menu->submenu_id == (HMENU)-1)
6483 {
6484 int iButton;
6485
6486 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6487 (WPARAM)menu->id, 0);
6488 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6489 }
6490 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 if (menu->parent != NULL
6494 && menu_is_popup(menu->parent->dname)
6495 && menu->parent->submenu_id != NULL)
6496 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6497 else
6498 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6499 if (menu->submenu_id != NULL)
6500 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006501# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (IsWindow(menu->tearoff_handle))
6503 DestroyWindow(menu->tearoff_handle);
6504 if (menu->parent != NULL
6505 && menu->parent->children != NULL
6506 && IsWindow(menu->parent->tearoff_handle))
6507 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006508 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 menu->modes = 0;
6510 rebuild_tearoff(menu->parent);
6511 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
6514}
6515
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006516# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 static void
6518rebuild_tearoff(vimmenu_T *menu)
6519{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006520 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 char_u tbuf[128];
6522 RECT trect;
6523 RECT rct;
6524 RECT roct;
6525 int x, y;
6526
6527 HWND thwnd = menu->tearoff_handle;
6528
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006529 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (GetWindowRect(thwnd, &trect)
6531 && GetWindowRect(s_hwnd, &rct)
6532 && GetClientRect(s_hwnd, &roct))
6533 {
6534 x = trect.left - rct.left;
6535 y = (trect.top - rct.bottom + roct.bottom);
6536 }
6537 else
6538 {
6539 x = y = 0xffffL;
6540 }
6541 DestroyWindow(thwnd);
6542 if (menu->children != NULL)
6543 {
6544 gui_mch_tearoff(tbuf, menu, x, y);
6545 if (IsWindow(menu->tearoff_handle))
6546 (void) SetWindowPos(menu->tearoff_handle,
6547 NULL,
6548 (int)trect.left,
6549 (int)trect.top,
6550 0, 0,
6551 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6552 }
6553}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006554# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556/*
6557 * Make a menu either grey or not grey.
6558 */
6559 void
6560gui_mch_menu_grey(
6561 vimmenu_T *menu,
6562 int grey)
6563{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006564# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 /*
6566 * is this a toolbar button?
6567 */
6568 if (menu->submenu_id == (HMENU)-1)
6569 {
6570 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006571 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006574# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006575 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6576 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006578# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6580 {
6581 WORD menuID;
6582 HWND menuHandle;
6583
6584 /*
6585 * A tearoff button has changed state.
6586 */
6587 if (menu->children == NULL)
6588 menuID = (WORD)(menu->id);
6589 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006590 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6592 if (menuHandle)
6593 EnableWindow(menuHandle, !grey);
6594
6595 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597}
6598
Bram Moolenaar734a8672019-12-02 22:49:38 +01006599#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601
Bram Moolenaar734a8672019-12-02 22:49:38 +01006602// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006605#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
6607#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6608/*
6609 * stuff for dialogs
6610 */
6611
6612/*
6613 * The callback routine used by all the dialogs. Very simple. First,
6614 * acknowledges the INITDIALOG message so that Windows knows to do standard
6615 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6616 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6617 * number.
6618 */
6619 static LRESULT CALLBACK
6620dialog_callback(
6621 HWND hwnd,
6622 UINT message,
6623 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006624 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 if (message == WM_INITDIALOG)
6627 {
6628 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006629 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 (void)SetFocus(hwnd);
6631 if (dialog_default_button > IDCANCEL)
6632 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006633 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006634 // We don't have a default, set focus on another element of the
6635 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006636 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 return FALSE;
6638 }
6639
6640 if (message == WM_COMMAND)
6641 {
6642 int button = LOWORD(wParam);
6643
Bram Moolenaar734a8672019-12-02 22:49:38 +01006644 // Don't end the dialog if something was selected that was
6645 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (button >= DLG_NONBUTTON_CONTROL)
6647 return TRUE;
6648
Bram Moolenaar734a8672019-12-02 22:49:38 +01006649 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006651 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006652 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006653 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006654
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006655 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6656 p = utf16_to_enc(wp, NULL);
6657 vim_strncpy(s_textfield, p, IOSIZE);
6658 vim_free(p);
6659 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 /*
6663 * Need to check for IDOK because if the user just hits Return to
6664 * accept the default value, some reason this is what we get.
6665 */
6666 if (button == IDOK)
6667 {
6668 if (dialog_default_button > IDCANCEL)
6669 EndDialog(hwnd, dialog_default_button);
6670 }
6671 else
6672 EndDialog(hwnd, button - IDCANCEL);
6673 return TRUE;
6674 }
6675
6676 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6677 {
6678 EndDialog(hwnd, 0);
6679 return TRUE;
6680 }
6681 return FALSE;
6682}
6683
6684/*
6685 * Create a dialog dynamically from the parameter strings.
6686 * type = type of dialog (question, alert, etc.)
6687 * title = dialog title. may be NULL for default title.
6688 * message = text to display. Dialog sizes to accommodate it.
6689 * buttons = '\n' separated list of button captions, default first.
6690 * dfltbutton = number of default button.
6691 *
6692 * This routine returns 1 if the first button is pressed,
6693 * 2 for the second, etc.
6694 *
6695 * 0 indicates Esc was pressed.
6696 * -1 for unexpected error
6697 *
6698 * If stubbing out this fn, return 1.
6699 */
6700
Bram Moolenaar734a8672019-12-02 22:49:38 +01006701static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 "IDR_VIM",
6704 "IDR_VIM_ERROR",
6705 "IDR_VIM_ALERT",
6706 "IDR_VIM_INFO",
6707 "IDR_VIM_QUESTION"
6708};
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 int
6711gui_mch_dialog(
6712 int type,
6713 char_u *title,
6714 char_u *message,
6715 char_u *buttons,
6716 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006717 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006718 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
6720 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006721 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 int numButtons;
6723 int *buttonWidths, *buttonPositions;
6724 int buttonYpos;
6725 int nchar, i;
6726 DWORD lStyle;
6727 int dlgwidth = 0;
6728 int dlgheight;
6729 int editboxheight;
6730 int horizWidth = 0;
6731 int msgheight;
6732 char_u *pstart;
6733 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006734 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 char_u *tbuffer;
6736 RECT rect;
6737 HWND hwnd;
6738 HDC hdc;
6739 HFONT font, oldFont;
6740 TEXTMETRIC fontInfo;
6741 int fontHeight;
6742 int textWidth, minButtonWidth, messageWidth;
6743 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006744 int maxDialogHeight;
6745 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 int vertical;
6747 int dlgPaddingX;
6748 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006749# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006750 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006752# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006753 garray_T ga;
6754 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006755 int dlg_icon_width;
6756 int dlg_icon_height;
6757 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006759# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006760 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006761# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006762 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006763# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006764 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006765 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
Bram Moolenaar748bf032005-02-02 23:04:36 +00006768 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006769 {
6770 load_dpi_func();
6771 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006772 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006773 }
6774 else
6775 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777 if ((type < 0) || (type > VIM_LAST_TYPE))
6778 type = 0;
6779
Bram Moolenaar734a8672019-12-02 22:49:38 +01006780 // allocate some memory for dialog template
6781 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006782 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006783 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
6785 if (p == NULL)
6786 return -1;
6787
6788 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006789 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6791 * const.
6792 */
6793 tbuffer = vim_strsave(buttons);
6794 if (tbuffer == NULL)
6795 return -1;
6796
Bram Moolenaar734a8672019-12-02 22:49:38 +01006797 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaar734a8672019-12-02 22:49:38 +01006799 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 numButtons = 1;
6801 for (i = 0; tbuffer[i] != '\0'; i++)
6802 {
6803 if (tbuffer[i] == DLG_BUTTON_SEP)
6804 numButtons++;
6805 }
6806 if (dfltbutton >= numButtons)
6807 dfltbutton = -1;
6808
Bram Moolenaar734a8672019-12-02 22:49:38 +01006809 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006810 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (buttonWidths == NULL)
6812 return -1;
6813
Bram Moolenaar734a8672019-12-02 22:49:38 +01006814 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006815 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 if (buttonPositions == NULL)
6817 return -1;
6818
6819 /*
6820 * Calculate how big the dialog must be.
6821 */
6822 hwnd = GetDesktopWindow();
6823 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006824# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6826 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006827 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 use_lfSysmenu = TRUE;
6829 }
6830 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006831# endif
K.Takatad1c58992022-01-23 12:31:57 +00006832 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6833 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6834
6835 oldFont = SelectFont(hdc, font);
6836 dlgPaddingX = DLG_PADDING_X;
6837 dlgPaddingY = DLG_PADDING_Y;
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 GetTextMetrics(hdc, &fontInfo);
6840 fontHeight = fontInfo.tmHeight;
6841
Bram Moolenaar734a8672019-12-02 22:49:38 +01006842 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006843 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
Bram Moolenaar734a8672019-12-02 22:49:38 +01006845 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006846 if (s_hwnd == NULL)
6847 {
6848 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaar734a8672019-12-02 22:49:38 +01006850 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006851 get_work_area(&workarea_rect);
6852 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006853 if (maxDialogWidth > adjust_by_system_dpi(600))
6854 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006855 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006856 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006857 }
6858 else
6859 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006861 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006862 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006863 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6864 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6865 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6866 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006867
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006868 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006869 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6870 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6871 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6872 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6873 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006874 }
6875
Bram Moolenaar734a8672019-12-02 22:49:38 +01006876 // Set dlgwidth to width of message.
6877 // Copy the message into "ga", changing NL to CR-NL and inserting line
6878 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 pstart = message;
6880 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006881 msgheight = 0;
6882 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 do
6884 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006885 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006886
Bram Moolenaar734a8672019-12-02 22:49:38 +01006887 // Need to figure out where to break the string. The system does it
6888 // at a word boundary, which would mean we can't compute the number of
6889 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006890 textWidth = 0;
6891 last_white = NULL;
6892 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006893 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006894 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006895 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006896 && textWidth > maxDialogWidth * 3 / 4)
6897 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006898 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006899 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006900 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006901 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006902 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006903 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006904 textWidth = 0;
6905
6906 if (last_white != NULL)
6907 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006908 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006909 if (pend > last_white)
6910 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006911 pend = last_white + 1;
6912 last_white = NULL;
6913 }
6914 ga_append(&ga, '\r');
6915 ga_append(&ga, '\n');
6916 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006917 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006918
6919 while (--l >= 0)
6920 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006921 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006922 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006924
6925 ga_append(&ga, '\r');
6926 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 pstart = pend + 1;
6928 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006929
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006930 if (ga.ga_data != NULL)
6931 message = ga.ga_data;
6932
Bram Moolenaar734a8672019-12-02 22:49:38 +01006933 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006934
K.Takatac81e9bf2022-01-16 14:15:49 +00006935 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6936 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
K.Takatac81e9bf2022-01-16 14:15:49 +00006938 // Add width of icon to dlgwidth, and some space
6939 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6940 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6941
6942 if (msgheight < dlg_icon_height)
6943 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945 /*
6946 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006947 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006949 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 if (!vertical)
6951 {
6952 // Place buttons horizontally if they fit.
6953 horizWidth = dlgPaddingX;
6954 pstart = tbuffer;
6955 i = 0;
6956 do
6957 {
6958 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6959 if (pend == NULL)
6960 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006961 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (textWidth < minButtonWidth)
6963 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006964 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 buttonWidths[i] = textWidth;
6966 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006967 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 pstart = pend + 1;
6969 } while (*pend != NUL);
6970
6971 if (horizWidth > maxDialogWidth)
6972 vertical = TRUE; // Too wide to fit on the screen.
6973 else if (horizWidth > dlgwidth)
6974 dlgwidth = horizWidth;
6975 }
6976
6977 if (vertical)
6978 {
6979 // Stack buttons vertically.
6980 pstart = tbuffer;
6981 do
6982 {
6983 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6984 if (pend == NULL)
6985 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006986 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006987 textWidth += dlgPaddingX; // Padding within button
6988 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (textWidth > dlgwidth)
6990 dlgwidth = textWidth;
6991 pstart = pend + 1;
6992 } while (*pend != NUL);
6993 }
6994
6995 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006996 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar734a8672019-12-02 22:49:38 +01006998 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006999 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 add_long(lStyle);
7002 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007003 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 add_word(0); // NumberOfItems(will change later)
7005 add_word(10); // x
7006 add_word(10); // y
7007 add_word(PixelToDialogX(dlgwidth)); // cx
7008
7009 // Dialog height.
7010 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007011 dlgheight = msgheight + 2 * dlgPaddingY
7012 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 else
7014 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7015
7016 // Dialog needs to be taller if contains an edit box.
7017 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7018 if (textfield != NULL)
7019 dlgheight += editboxheight;
7020
Bram Moolenaar734a8672019-12-02 22:49:38 +01007021 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007022 if (dlgheight > maxDialogHeight)
7023 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007024 msgheight = msgheight - (dlgheight - maxDialogHeight);
7025 dlgheight = maxDialogHeight;
7026 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007027 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007028 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007029 }
7030
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 add_word(PixelToDialogY(dlgheight));
7032
7033 add_word(0); // Menu
7034 add_word(0); // Class
7035
Bram Moolenaar734a8672019-12-02 22:49:38 +01007036 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007037 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7038 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 p += nchar;
7040
K.Takatad1c58992022-01-23 12:31:57 +00007041 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007042# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007043 if (use_lfSysmenu)
7044 {
7045 // point size
7046 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7047 GetDeviceCaps(hdc, LOGPIXELSY));
7048 wcscpy(p, lfSysmenu.lfFaceName);
7049 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
K.Takatad1c58992022-01-23 12:31:57 +00007051 else
7052# endif
7053 {
7054 *p++ = DLG_FONT_POINT_SIZE; // point size
7055 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7056 }
7057 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058
7059 buttonYpos = msgheight + 2 * dlgPaddingY;
7060
7061 if (textfield != NULL)
7062 buttonYpos += editboxheight;
7063
7064 pstart = tbuffer;
7065 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007066 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 for (i = 0; i < numButtons; i++)
7068 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007069 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 for ( pend = pstart;
7071 *pend && (*pend != DLG_BUTTON_SEP);
7072 pend++)
7073 ;
7074
7075 if (*pend)
7076 *pend = '\0';
7077
7078 /*
7079 * old NOTE:
7080 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7081 * the focus to the first tab-able button and in so doing makes that
7082 * the default!! Grrr. Workaround: Make the default button the only
7083 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7084 * he/she can use arrow keys.
7085 *
7086 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007087 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 * dialog. Also needed for when the textfield is the default control.
7089 * It appears to work now (perhaps not on Win95?).
7090 */
7091 if (vertical)
7092 {
7093 p = add_dialog_element(p,
7094 (i == dfltbutton
7095 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7096 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007097 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 + 2 * fontHeight * i),
7099 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7100 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007101 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 }
7103 else
7104 {
7105 p = add_dialog_element(p,
7106 (i == dfltbutton
7107 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7108 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007109 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 PixelToDialogX(buttonWidths[i]),
7111 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007112 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007114 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116 *pnumitems += numButtons;
7117
Bram Moolenaar734a8672019-12-02 22:49:38 +01007118 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 p = add_dialog_element(p, SS_ICON,
7120 PixelToDialogX(dlgPaddingX),
7121 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007122 PixelToDialogX(dlg_icon_width),
7123 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7125 dlg_icons[type]);
7126
Bram Moolenaar734a8672019-12-02 22:49:38 +01007127 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007128 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007129 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007130 PixelToDialogY(dlgPaddingY),
7131 (WORD)(PixelToDialogX(messageWidth) + 1),
7132 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007133 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
Bram Moolenaar734a8672019-12-02 22:49:38 +01007135 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (textfield != NULL)
7137 {
7138 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7139 PixelToDialogX(2 * dlgPaddingX),
7140 PixelToDialogY(2 * dlgPaddingY + msgheight),
7141 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7142 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007143 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 *pnumitems += 1;
7145 }
7146
7147 *pnumitems += 2;
7148
7149 SelectFont(hdc, oldFont);
7150 DeleteObject(font);
7151 ReleaseDC(hwnd, hdc);
7152
Bram Moolenaar734a8672019-12-02 22:49:38 +01007153 // Let the dialog_callback() function know which button to make default
7154 // If we have an edit box, make that the default. We also need to tell
7155 // dialog_callback() if this dialog contains an edit box or not. We do
7156 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (textfield != NULL)
7158 {
7159 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7160 s_textfield = textfield;
7161 }
7162 else
7163 {
7164 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7165 s_textfield = NULL;
7166 }
7167
Bram Moolenaar734a8672019-12-02 22:49:38 +01007168 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007170 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 (LPDLGTEMPLATE)pdlgtemplate,
7172 s_hwnd,
7173 (DLGPROC)dialog_callback);
7174
7175 LocalFree(LocalHandle(pdlgtemplate));
7176 vim_free(tbuffer);
7177 vim_free(buttonWidths);
7178 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007179 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar734a8672019-12-02 22:49:38 +01007181 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 (void)SetFocus(s_hwnd);
7183
7184 return nchar;
7185}
7186
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * Put a simple element (basic class) onto a dialog template in memory.
7191 * return a pointer to where the next item should be added.
7192 *
7193 * parameters:
7194 * lStyle = additional style flags
7195 * (be careful, NT3.51 & Win32s will ignore the new ones)
7196 * x,y = x & y positions IN DIALOG UNITS
7197 * w,h = width and height IN DIALOG UNITS
7198 * Id = ID used in messages
7199 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7200 * caption = usually text or resource name
7201 *
7202 * TODO: use the length information noted here to enable the dialog creation
7203 * routines to work out more exactly how much memory they need to alloc.
7204 */
7205 static PWORD
7206add_dialog_element(
7207 PWORD p,
7208 DWORD lStyle,
7209 WORD x,
7210 WORD y,
7211 WORD w,
7212 WORD h,
7213 WORD Id,
7214 WORD clss,
7215 const char *caption)
7216{
7217 int nchar;
7218
Bram Moolenaar734a8672019-12-02 22:49:38 +01007219 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7221 *p++ = LOWORD(lStyle);
7222 *p++ = HIWORD(lStyle);
7223 *p++ = 0; // LOWORD (lExtendedStyle)
7224 *p++ = 0; // HIWORD (lExtendedStyle)
7225 *p++ = x;
7226 *p++ = y;
7227 *p++ = w;
7228 *p++ = h;
7229 *p++ = Id; //9 or 10 words in all
7230
7231 *p++ = (WORD)0xffff;
7232 *p++ = clss; //2 more here
7233
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007234 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 p += nchar;
7236
7237 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7238
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007239 return p; // total = 15 + strlen(caption) words
7240 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
7243
7244/*
7245 * Helper routine. Take an input pointer, return closest pointer that is
7246 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7247 */
7248 static LPWORD
7249lpwAlign(
7250 LPWORD lpIn)
7251{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007252 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007254 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 ul += 3;
7256 ul >>= 2;
7257 ul <<= 2;
7258 return (LPWORD)ul;
7259}
7260
7261/*
7262 * Helper routine. Takes second parameter as Ansi string, copies it to first
7263 * parameter as wide character (16-bits / char) string, and returns integer
7264 * number of wide characters (words) in string (including the trailing wide
7265 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007266 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7267 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 static int
7269nCopyAnsiToWideChar(
7270 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007271 LPSTR lpAnsiIn,
7272 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273{
7274 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007275 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 int i;
7277 WCHAR *wn;
7278
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007279 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007281 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007282 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (wn != NULL)
7284 {
7285 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 vim_free(wn);
7288 }
7289 }
7290 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007291 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 nChar = MultiByteToWideChar(
7293 enc_codepage > 0 ? enc_codepage : CP_ACP,
7294 MB_PRECOMPOSED,
7295 lpAnsiIn, len,
7296 lpWCStr, len);
7297 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007298 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301 return nChar;
7302}
7303
7304
7305#ifdef FEAT_TEAROFF
7306/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007307 * Lookup menu handle from "menu_id".
7308 */
7309 static HMENU
7310tearoff_lookup_menuhandle(
7311 vimmenu_T *menu,
7312 WORD menu_id)
7313{
7314 for ( ; menu != NULL; menu = menu->next)
7315 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007316 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007317 continue;
7318 if (menu_is_separator(menu->dname))
7319 continue;
7320 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7321 return menu->submenu_id;
7322 }
7323 return NULL;
7324}
7325
7326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 * The callback function for all the modeless dialogs that make up the
7328 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7329 * thinking its menus have been clicked), and go away when closed.
7330 */
7331 static LRESULT CALLBACK
7332tearoff_callback(
7333 HWND hwnd,
7334 UINT message,
7335 WPARAM wParam,
7336 LPARAM lParam)
7337{
7338 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007339 {
7340 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
Bram Moolenaar734a8672019-12-02 22:49:38 +01007344 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 HandleMouseHide(message, lParam);
7346
7347 if (message == WM_COMMAND)
7348 {
7349 if ((WORD)(LOWORD(wParam)) & 0x8000)
7350 {
7351 POINT mp;
7352 RECT rect;
7353
7354 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7355 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007356 vimmenu_T *menu;
7357
7358 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007360 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7362 (int)rect.right - 8,
7363 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007364 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 s_hwnd,
7366 NULL);
7367 /*
7368 * NOTE: The pop-up menu can eat the mouse up event.
7369 * We deal with this in normal.c.
7370 */
7371 }
7372 }
7373 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007374 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7376 /*
7377 * Give main window the focus back: this is so after
7378 * choosing a tearoff button you can start typing again
7379 * straight away.
7380 */
7381 (void)SetFocus(s_hwnd);
7382 return TRUE;
7383 }
7384 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7385 {
7386 DestroyWindow(hwnd);
7387 return TRUE;
7388 }
7389
Bram Moolenaar734a8672019-12-02 22:49:38 +01007390 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (message == WM_EXITSIZEMOVE)
7392 (void)SetActiveWindow(s_hwnd);
7393
7394 return FALSE;
7395}
7396#endif
7397
7398
7399/*
K.Takatad1c58992022-01-23 12:31:57 +00007400 * Computes the dialog base units based on the current dialog font.
7401 * We don't use the GetDialogBaseUnits() API, because we don't use the
7402 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 */
7404 static void
7405get_dialog_font_metrics(void)
7406{
7407 HDC hdc;
7408 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 SIZE size;
7410#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007411 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412#endif
7413
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007415 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007416 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418#endif
7419 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007420 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
K.Takatad1c58992022-01-23 12:31:57 +00007422 hdc = GetDC(s_hwnd);
7423 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007424 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007425 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
K.Takataabe628e2022-01-23 16:25:17 +00007427 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007428 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429}
7430
7431#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7432/*
7433 * Create a pseudo-"tearoff menu" based on the child
7434 * items of a given menu pointer.
7435 */
7436 static void
7437gui_mch_tearoff(
7438 char_u *title,
7439 vimmenu_T *menu,
7440 int initX,
7441 int initY)
7442{
7443 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7444 int template_len;
7445 int nchar, textWidth, submenuWidth;
7446 DWORD lStyle;
7447 DWORD lExtendedStyle;
7448 WORD dlgwidth;
7449 WORD menuID;
7450 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007451 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 vimmenu_T *the_menu = menu;
7453 HWND hwnd;
7454 HDC hdc;
7455 HFONT font, oldFont;
7456 int col, spaceWidth, len;
7457 int columnWidths[2];
7458 char_u *label, *text;
7459 int acLen = 0;
7460 int nameLen;
7461 int padding0, padding1, padding2 = 0;
7462 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007463 int x;
7464 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007465# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007466 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469
7470 /*
7471 * If this menu is already torn off, move it to the mouse position.
7472 */
7473 if (IsWindow(menu->tearoff_handle))
7474 {
7475 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007476 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
7478 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7479 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7480 }
7481 return;
7482 }
7483
7484 /*
7485 * Create a new tearoff.
7486 */
7487 if (*title == MNU_HIDDEN_CHAR)
7488 title++;
7489
Bram Moolenaar734a8672019-12-02 22:49:38 +01007490 // Allocate memory to store the dialog template. It's made bigger when
7491 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 template_len = DLG_ALLOC_SIZE;
7493 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7494 if (p == NULL)
7495 return;
7496
7497 hwnd = GetDesktopWindow();
7498 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007499# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7501 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007502 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 use_lfSysmenu = TRUE;
7504 }
7505 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007506# endif
K.Takatad1c58992022-01-23 12:31:57 +00007507 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7508 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7509
7510 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar734a8672019-12-02 22:49:38 +01007512 // Calculate width of a single space. Used for padding columns to the
7513 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007514 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
Bram Moolenaar734a8672019-12-02 22:49:38 +01007516 // Figure out max width of the text column, the accelerator column and the
7517 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 submenuWidth = 0;
7519 for (col = 0; col < 2; col++)
7520 {
7521 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007522 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007524 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 text = (col == 0) ? pmenu->dname : pmenu->actext;
7526 if (text != NULL && *text != NUL)
7527 {
7528 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7529 if (textWidth > columnWidths[col])
7530 columnWidths[col] = textWidth;
7531 }
7532 if (pmenu->children != NULL)
7533 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7534 }
7535 }
7536 if (columnWidths[1] == 0)
7537 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007538 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (submenuWidth != 0)
7540 columnWidths[0] += submenuWidth;
7541 else
7542 columnWidths[0] += spaceWidth;
7543 }
7544 else
7545 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007546 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7548 columnWidths[1] += submenuWidth;
7549 }
7550
7551 /*
7552 * Now find the total width of our 'menu'.
7553 */
7554 textWidth = columnWidths[0] + columnWidths[1];
7555 if (submenuWidth != 0)
7556 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007557 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7559 textWidth += submenuWidth;
7560 }
7561 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7562 if (textWidth > dlgwidth)
7563 dlgwidth = textWidth;
7564 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7565
Bram Moolenaar734a8672019-12-02 22:49:38 +01007566 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007567 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568
7569 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7570 *p++ = LOWORD(lStyle);
7571 *p++ = HIWORD(lStyle);
7572 *p++ = LOWORD(lExtendedStyle);
7573 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007574 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007576 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007578 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 else
7580 *p++ = PixelToDialogX(initX); // x
7581 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007582 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 else
7584 *p++ = PixelToDialogY(initY); // y
7585 *p++ = PixelToDialogX(dlgwidth); // cx
7586 ptrueheight = p;
7587 *p++ = 0; // dialog height: changed later anyway
7588 *p++ = 0; // Menu
7589 *p++ = 0; // Class
7590
Bram Moolenaar734a8672019-12-02 22:49:38 +01007591 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007593 ? (LPSTR)title
7594 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 p += nchar;
7596
K.Takatad1c58992022-01-23 12:31:57 +00007597 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007598# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007599 if (use_lfSysmenu)
7600 {
7601 // point size
7602 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7603 GetDeviceCaps(hdc, LOGPIXELSY));
7604 wcscpy(p, lfSysmenu.lfFaceName);
7605 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
K.Takatad1c58992022-01-23 12:31:57 +00007607 else
7608# endif
7609 {
7610 *p++ = DLG_FONT_POINT_SIZE; // point size
7611 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7612 }
7613 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614
7615 /*
7616 * Loop over all the items in the menu.
7617 * But skip over the tearbar.
7618 */
7619 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7620 menu = menu->children->next;
7621 else
7622 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007623 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 for ( ; menu != NULL; menu = menu->next)
7625 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007626 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 continue;
7628 if (menu_is_separator(menu->dname))
7629 {
7630 sepPadding += 3;
7631 continue;
7632 }
7633
Bram Moolenaar734a8672019-12-02 22:49:38 +01007634 // Check if there still is plenty of room in the template. Make it
7635 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7637 {
7638 WORD *newp;
7639
7640 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7641 if (newp != NULL)
7642 {
7643 template_len += 4096;
7644 mch_memmove(newp, pdlgtemplate,
7645 (char *)p - (char *)pdlgtemplate);
7646 p = newp + (p - pdlgtemplate);
7647 pnumitems = newp + (pnumitems - pdlgtemplate);
7648 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7649 LocalFree(LocalHandle(pdlgtemplate));
7650 pdlgtemplate = newp;
7651 }
7652 }
7653
Bram Moolenaar734a8672019-12-02 22:49:38 +01007654 // Figure out minimal length of this menu label. Use "name" for the
7655 // actual text, "dname" for estimating the displayed size. "name"
7656 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 len = nameLen = (int)STRLEN(menu->name);
7658 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7659 (int)STRLEN(menu->dname))) / spaceWidth;
7660 len += padding0;
7661
7662 if (menu->actext != NULL)
7663 {
7664 acLen = (int)STRLEN(menu->actext);
7665 len += acLen;
7666 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7667 }
7668 else
7669 textWidth = 0;
7670 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7671 len += padding1;
7672
7673 if (menu->children == NULL)
7674 {
7675 padding2 = submenuWidth / spaceWidth;
7676 len += padding2;
7677 menuID = (WORD)(menu->id);
7678 }
7679 else
7680 {
7681 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007682 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 }
7684
Bram Moolenaar734a8672019-12-02 22:49:38 +01007685 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007686 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 if (label == NULL)
7688 break;
7689
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007690 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007691 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007693 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 while (padding0-- > 0)
7695 *text++ = ' ';
7696 if (menu->actext != NULL)
7697 {
7698 STRNCPY(text, menu->actext, acLen);
7699 text += acLen;
7700 }
7701 while (padding1-- > 0)
7702 *text++ = ' ';
7703 if (menu->children != NULL)
7704 {
7705 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7706 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7707 }
7708 else
7709 {
7710 while (padding2-- > 0)
7711 *text++ = ' ';
7712 }
7713 *text = NUL;
7714
7715 /*
7716 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7717 * W95/NT4 it makes the tear-off look more like a menu.
7718 */
7719 p = add_dialog_element(p,
7720 BS_PUSHBUTTON|BS_LEFT,
7721 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7722 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7723 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7724 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007725 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 vim_free(label);
7727 (*pnumitems)++;
7728 }
7729
7730 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7731
7732
Bram Moolenaar734a8672019-12-02 22:49:38 +01007733 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007734 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007735 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 (LPDLGTEMPLATE)pdlgtemplate,
7737 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007738 (DLGPROC)tearoff_callback,
7739 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740
7741 LocalFree(LocalHandle(pdlgtemplate));
7742 SelectFont(hdc, oldFont);
7743 DeleteObject(font);
7744 ReleaseDC(hwnd, hdc);
7745
7746 /*
7747 * Reassert ourselves as the active window. This is so that after creating
7748 * a tearoff, the user doesn't have to click with the mouse just to start
7749 * typing again!
7750 */
7751 (void)SetActiveWindow(s_hwnd);
7752
Bram Moolenaar734a8672019-12-02 22:49:38 +01007753 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 force_menu_update = TRUE;
7755}
7756#endif
7757
7758#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007759# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761/*
7762 * Create the toolbar, initially unpopulated.
7763 * (just like the menu, there are no defaults, it's all
7764 * set up through menu.vim)
7765 */
7766 static void
7767initialise_toolbar(void)
7768{
7769 InitCommonControls();
7770 s_toolbarhwnd = CreateToolbarEx(
7771 s_hwnd,
7772 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7773 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007774 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007775 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 IDR_TOOLBAR1, // id of initial bitmap
7777 NULL,
7778 0, // initial number of buttons
7779 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7780 TOOLBAR_BUTTON_HEIGHT,
7781 TOOLBAR_BUTTON_WIDTH,
7782 TOOLBAR_BUTTON_HEIGHT,
7783 sizeof(TBBUTTON)
7784 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007785
7786 // Remove transparency from the toolbar to prevent the main window
7787 // background colour showing through
7788 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7789 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7790
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007791 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007794
7795 update_toolbar_size();
7796}
7797
7798 static void
7799update_toolbar_size(void)
7800{
7801 int w, h;
7802 TBMETRICS tbm = {sizeof(TBMETRICS)};
7803
7804 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7805 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7806 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7807 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7808
7809 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7810 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7811 //TRACE("button size: %d, %d", w, h);
7812 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7813 gui.toolbar_height = h + 6;
7814
7815 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7816 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7817
7818 // TODO:
7819 // Currently, this function only updates the size of toolbar buttons.
7820 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821}
7822
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007823 static LRESULT CALLBACK
7824toolbar_wndproc(
7825 HWND hwnd,
7826 UINT uMsg,
7827 WPARAM wParam,
7828 LPARAM lParam)
7829{
7830 HandleMouseHide(uMsg, lParam);
7831 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7832}
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 static int
7835get_toolbar_bitmap(vimmenu_T *menu)
7836{
7837 int i = -1;
7838
7839 /*
7840 * Check user bitmaps first, unless builtin is specified.
7841 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007842 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
7844 char_u fname[MAXPATHL];
7845 HANDLE hbitmap = NULL;
7846
7847 if (menu->iconfile != NULL)
7848 {
7849 gui_find_iconfile(menu->iconfile, fname, "bmp");
7850 hbitmap = LoadImage(
7851 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007852 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 IMAGE_BITMAP,
7854 TOOLBAR_BUTTON_WIDTH,
7855 TOOLBAR_BUTTON_HEIGHT,
7856 LR_LOADFROMFILE |
7857 LR_LOADMAP3DCOLORS
7858 );
7859 }
7860
7861 /*
7862 * If the LoadImage call failed, or the "icon=" file
7863 * didn't exist or wasn't specified, try the menu name
7864 */
7865 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007866 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007867# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007868 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007869# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007870 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 hbitmap = LoadImage(
7872 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007873 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 IMAGE_BITMAP,
7875 TOOLBAR_BUTTON_WIDTH,
7876 TOOLBAR_BUTTON_HEIGHT,
7877 LR_LOADFROMFILE |
7878 LR_LOADMAP3DCOLORS
7879 );
7880
7881 if (hbitmap != NULL)
7882 {
7883 TBADDBITMAP tbAddBitmap;
7884
7885 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007886 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887
7888 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7889 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007890 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 }
7893 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7894 i = menu->iconidx;
7895
7896 return i;
7897}
7898#endif
7899
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007900#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7901 static void
7902initialise_tabline(void)
7903{
7904 InitCommonControls();
7905
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007906 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007907 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007908 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007909 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007910 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007911
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007912 gui.tabline_height = TABLINE_HEIGHT;
7913
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007914 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007915}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007916
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007917/*
7918 * Get tabpage_T from POINT.
7919 */
7920 static tabpage_T *
7921GetTabFromPoint(
7922 HWND hWnd,
7923 POINT pt)
7924{
7925 tabpage_T *ptp = NULL;
7926
7927 if (gui_mch_showing_tabline())
7928 {
7929 TCHITTESTINFO htinfo;
7930 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007931 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007932 if (s_tabhwnd == hWnd)
7933 {
7934 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7935 if (idx != -1)
7936 ptp = find_tabpage(idx + 1);
7937 }
7938 }
7939 return ptp;
7940}
7941
7942static POINT s_pt = {0, 0};
7943static HCURSOR s_hCursor = NULL;
7944
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007945 static LRESULT CALLBACK
7946tabline_wndproc(
7947 HWND hwnd,
7948 UINT uMsg,
7949 WPARAM wParam,
7950 LPARAM lParam)
7951{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007952 POINT pt;
7953 tabpage_T *tp;
7954 RECT rect;
7955 int nCenter;
7956 int idx0;
7957 int idx1;
7958
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007959 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007960
7961 switch (uMsg)
7962 {
7963 case WM_LBUTTONDOWN:
7964 {
7965 s_pt.x = GET_X_LPARAM(lParam);
7966 s_pt.y = GET_Y_LPARAM(lParam);
7967 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007968 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007969 break;
7970 }
7971 case WM_MOUSEMOVE:
7972 if (GetCapture() == hwnd
7973 && ((wParam & MK_LBUTTON)) != 0)
7974 {
7975 pt.x = GET_X_LPARAM(lParam);
7976 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007977 if (abs(pt.x - s_pt.x) >
7978 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007979 {
7980 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7981
7982 tp = GetTabFromPoint(hwnd, pt);
7983 if (tp != NULL)
7984 {
7985 idx0 = tabpage_index(curtab) - 1;
7986 idx1 = tabpage_index(tp) - 1;
7987
7988 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7989 nCenter = rect.left + (rect.right - rect.left) / 2;
7990
Bram Moolenaar734a8672019-12-02 22:49:38 +01007991 // Check if the mouse cursor goes over the center of
7992 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007993 if ((idx0 < idx1) && (nCenter < pt.x))
7994 {
7995 tabpage_move(idx1 + 1);
7996 update_screen(0);
7997 }
7998 else if ((idx1 < idx0) && (pt.x < nCenter))
7999 {
8000 tabpage_move(idx1);
8001 update_screen(0);
8002 }
8003 }
8004 }
8005 }
8006 break;
8007 case WM_LBUTTONUP:
8008 {
8009 if (GetCapture() == hwnd)
8010 {
8011 SetCursor(s_hCursor);
8012 ReleaseCapture();
8013 }
8014 break;
8015 }
8016 default:
8017 break;
8018 }
8019
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008020 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8021}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008022#endif
8023
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8025/*
8026 * Make the GUI window come to the foreground.
8027 */
8028 void
8029gui_mch_set_foreground(void)
8030{
8031 if (IsIconic(s_hwnd))
8032 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8033 SetForegroundWindow(s_hwnd);
8034}
8035#endif
8036
8037#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8038 static void
8039dyn_imm_load(void)
8040{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008041 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 if (hLibImm == NULL)
8043 return;
8044
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 pImmGetCompositionStringW
8046 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8047 pImmGetContext
8048 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8049 pImmAssociateContext
8050 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8051 pImmReleaseContext
8052 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8053 pImmGetOpenStatus
8054 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8055 pImmSetOpenStatus
8056 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008057 pImmGetCompositionFontW
8058 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8059 pImmSetCompositionFontW
8060 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 pImmSetCompositionWindow
8062 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8063 pImmGetConversionStatus
8064 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008065 pImmSetConversionStatus
8066 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067
K.Takatab0b2b732022-01-19 12:59:21 +00008068 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 || pImmGetContext == NULL
8070 || pImmAssociateContext == NULL
8071 || pImmReleaseContext == NULL
8072 || pImmGetOpenStatus == NULL
8073 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008074 || pImmGetCompositionFontW == NULL
8075 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008077 || pImmGetConversionStatus == NULL
8078 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
8080 FreeLibrary(hLibImm);
8081 hLibImm = NULL;
8082 pImmGetContext = NULL;
8083 return;
8084 }
8085
8086 return;
8087}
8088
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#endif
8090
8091#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8092
8093# ifdef FEAT_XPM_W32
8094# define IMAGE_XPM 100
8095# endif
8096
8097typedef struct _signicon_t
8098{
8099 HANDLE hImage;
8100 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008101# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008102 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104} signicon_t;
8105
8106 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008107gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 signicon_t *sign;
8110 int x, y, w, h;
8111
8112 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8113 return;
8114
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008115# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008116 if (IS_ENABLE_DIRECTX())
8117 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008118# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008119
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 x = TEXT_X(col);
8121 y = TEXT_Y(row);
8122 w = gui.char_width * 2;
8123 h = gui.char_height;
8124 switch (sign->uType)
8125 {
8126 case IMAGE_BITMAP:
8127 {
8128 HDC hdcMem;
8129 HBITMAP hbmpOld;
8130
8131 hdcMem = CreateCompatibleDC(s_hdc);
8132 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8133 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8134 SelectObject(hdcMem, hbmpOld);
8135 DeleteDC(hdcMem);
8136 }
8137 break;
8138 case IMAGE_ICON:
8139 case IMAGE_CURSOR:
8140 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8141 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008142# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 case IMAGE_XPM:
8144 {
8145 HDC hdcMem;
8146 HBITMAP hbmpOld;
8147
8148 hdcMem = CreateCompatibleDC(s_hdc);
8149 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008150 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8152
8153 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008154 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8156 SelectObject(hdcMem, hbmpOld);
8157 DeleteDC(hdcMem);
8158 }
8159 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 }
8162}
8163
8164 static void
8165close_signicon_image(signicon_t *sign)
8166{
8167 if (sign)
8168 switch (sign->uType)
8169 {
8170 case IMAGE_BITMAP:
8171 DeleteObject((HGDIOBJ)sign->hImage);
8172 break;
8173 case IMAGE_CURSOR:
8174 DestroyCursor((HCURSOR)sign->hImage);
8175 break;
8176 case IMAGE_ICON:
8177 DestroyIcon((HICON)sign->hImage);
8178 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008179# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 case IMAGE_XPM:
8181 DeleteObject((HBITMAP)sign->hImage);
8182 DeleteObject((HBITMAP)sign->hShape);
8183 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 }
8186}
8187
8188 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008189gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190{
8191 signicon_t sign, *psign;
8192 char_u *ext;
8193
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008195 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 if (ext > signfile)
8197 {
8198 int do_load = 1;
8199
8200 if (!STRICMP(ext, ".bmp"))
8201 sign.uType = IMAGE_BITMAP;
8202 else if (!STRICMP(ext, ".ico"))
8203 sign.uType = IMAGE_ICON;
8204 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8205 sign.uType = IMAGE_CURSOR;
8206 else
8207 do_load = 0;
8208
8209 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008210 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 gui.char_width * 2, gui.char_height,
8212 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008213# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 if (!STRICMP(ext, ".xpm"))
8215 {
8216 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008217 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8218 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
8222
8223 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008224 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 *psign = sign;
8226
8227 if (!psign)
8228 {
8229 if (sign.hImage)
8230 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008231 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
8233 return (void *)psign;
8234
8235}
8236
8237 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008238gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
8240 if (sign)
8241 {
8242 close_signicon_image((signicon_t *)sign);
8243 vim_free(sign);
8244 }
8245}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008248#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249
Bram Moolenaar734a8672019-12-02 22:49:38 +01008250/*
8251 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008252 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008254 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8256 * to get current mouse position).
8257 *
8258 * Trying to use as more Windows services as possible, and as less
8259 * IE version as possible :)).
8260 *
8261 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8262 * BalloonEval struct.
8263 * 2) Enable/Disable simply create/kill BalloonEval Timer
8264 * 3) When there was enough inactivity, timer procedure posts
8265 * async request to debugger
8266 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8267 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008268 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 */
8270
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008271 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008272make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008273{
K.Takata76687d22022-01-25 10:31:37 +00008274 TOOLINFOW *pti;
8275 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008276
K.Takata76687d22022-01-25 10:31:37 +00008277 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008278 if (pti == NULL)
8279 return;
8280
8281 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8282 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8283 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008284 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008285
8286 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8287 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8288
K.Takata76687d22022-01-25 10:31:37 +00008289 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008290 pti->uFlags = TTF_SUBCLASS;
8291 pti->hwnd = beval->target;
8292 pti->hinst = 0; // Don't use string resources
8293 pti->uId = ID_BEVAL_TOOLTIP;
8294
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008295 pti->lpszText = LPSTR_TEXTCALLBACKW;
8296 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8297 pti->lParam = (LPARAM)beval->tofree;
8298 // switch multiline tooltips on
8299 if (GetClientRect(s_textArea, &rect))
8300 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8301 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008302
8303 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8304 pti->rect.left = pt.x - 3;
8305 pti->rect.top = pt.y - 3;
8306 pti->rect.right = pt.x + 3;
8307 pti->rect.bottom = pt.y + 3;
8308
8309 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8310 // Make tooltip appear sooner.
8311 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8312 // I've performed some tests and it seems the longest possible life time
8313 // of tooltip is 30 seconds.
8314 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8315 /*
8316 * HACK: force tooltip to appear, because it'll not appear until
8317 * first mouse move. D*mn M$
8318 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8319 */
8320 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8321 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8322 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008323}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008326delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008328 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329}
8330
8331 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008332beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008333 HWND hwnd UNUSED,
8334 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008335 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008336 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
8338 POINT pt;
8339 RECT rect;
8340
8341 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8342 return;
8343
8344 GetCursorPos(&pt);
8345 if (WindowFromPoint(pt) != s_textArea)
8346 return;
8347
8348 ScreenToClient(s_textArea, &pt);
8349 GetClientRect(s_textArea, &rect);
8350 if (!PtInRect(&rect, pt))
8351 return;
8352
K.Takataa8ec4912022-02-03 14:32:33 +00008353 if (last_user_activity > 0
8354 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 && (cur_beval->showState != ShS_PENDING
8356 || abs(cur_beval->x - pt.x) > 3
8357 || abs(cur_beval->y - pt.y) > 3))
8358 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008359 // Pointer resting in one place long enough, it's time to show
8360 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 cur_beval->showState = ShS_PENDING;
8362 cur_beval->x = pt.x;
8363 cur_beval->y = pt.y;
8364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 if (cur_beval->msgCB != NULL)
8366 (*cur_beval->msgCB)(cur_beval, 0);
8367 }
8368}
8369
8370 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008371gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372{
K.Takataa8ec4912022-02-03 14:32:33 +00008373 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374}
8375
8376 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008377gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (beval == NULL)
8380 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008381 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8382 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383}
8384
8385 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008386gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
8388 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008389
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008390 vim_free(beval->msg);
8391 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8392 if (beval->msg == NULL)
8393 {
8394 delete_tooltip(beval);
8395 beval->showState = ShS_NEUTRAL;
8396 return;
8397 }
8398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (beval->showState == ShS_SHOWING)
8400 return;
8401 GetCursorPos(&pt);
8402 ScreenToClient(s_textArea, &pt);
8403
8404 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008406 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 gui_mch_disable_beval_area(cur_beval);
8408 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008409 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008414gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008415 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008416 char_u *mesg,
8417 void (*mesgCB)(BalloonEval *, int),
8418 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008420 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 BalloonEval *beval;
8422
8423 if (mesg != NULL && mesgCB != NULL)
8424 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008425 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 return NULL;
8427 }
8428
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008429 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 if (beval != NULL)
8431 {
8432 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433
8434 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 beval->msg = mesg;
8436 beval->msgCB = mesgCB;
8437 beval->clientData = clientData;
8438
8439 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 cur_beval = beval;
8441
8442 if (p_beval)
8443 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 }
8445 return beval;
8446}
8447
8448 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008449Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008451 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 return;
8453
8454 if (cur_beval != NULL)
8455 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008456 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008458 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008459 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008460 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 delete_tooltip(cur_beval);
8462 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
8464 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008465 break;
8466 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008467 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008468 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008469 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008470 info->lpszText = (LPSTR)info->lParam;
8471 info->uFlags |= TTF_DI_SETITEM;
8472 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008473 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008474 case TTN_GETDISPINFOW:
8475 {
8476 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008477 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008478 info->lpszText = (LPWSTR)info->lParam;
8479 info->uFlags |= TTF_DI_SETITEM;
8480 }
8481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 }
8483 }
8484}
8485
8486 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008487track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8490 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008491 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
8494 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008495gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008497# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008498 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008499# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008500 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 vim_free(beval);
8502}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008503#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504
8505#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8506/*
8507 * We have multiple signs to draw at the same location. Draw the
8508 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8509 */
8510 void
8511netbeans_draw_multisign_indicator(int row)
8512{
8513 int i;
8514 int y;
8515 int x;
8516
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008517 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008518 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 x = 0;
8521 y = TEXT_Y(row);
8522
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008523# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008524 if (IS_ENABLE_DIRECTX())
8525 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008526# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 for (i = 0; i < gui.char_height - 3; i++)
8529 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8530
8531 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8532 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8533 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8534 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8535 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8536 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8537 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8538}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008539#endif