blob: ae419053b2f5e17110293b61a7cbcaa656a49035 [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
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100395stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000396{
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 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001715 for (i = 0; i < (int)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 Moolenaar24959102022-05-07 20:01:16 +01001959 else if ((get_real_state()
1960 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001961 {
1962 outputDeadKey_rePost(msg);
1963 return;
1964 }
1965 }
1966
Bram Moolenaar734a8672019-12-02 22:49:38 +01001967 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001968 if (vk == VK_CANCEL)
1969 {
1970 trash_input_buf();
1971 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001972 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001973 string[0] = Ctrl_C;
1974 add_to_input_buf(string, 1);
1975 }
1976
LemonBoy77fc0b02022-04-22 22:45:52 +01001977 // This is an IME event or a synthetic keystroke, let Windows handle it.
1978 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1979 {
1980 TranslateMessage(&msg);
1981 return;
1982 }
1983
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001984 for (i = 0; special_keys[i].key_sym != 0; i++)
1985 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001986 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001987 if (special_keys[i].key_sym == vk
LemonBoy202b4bd2022-04-28 19:50:54 +01001988 && (vk != VK_SPACE || !(GetKeyState(VK_LMENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001989 {
1990 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001991 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001992 * is a key that would normally trigger the dead key nominal
1993 * character output (such as a NUMPAD printable character or
1994 * the TAB key, etc...).
1995 */
1996 if (dead_key && (special_keys[i].vim_code0 == 'K'
1997 || vk == VK_TAB || vk == CAR))
1998 {
1999 outputDeadKey_rePost(msg);
2000 return;
2001 }
2002
2003#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002004 // Check for <F10>: Windows selects the menu. When <F10> is
2005 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002006 if (vk == VK_F10
2007 && gui.menu_is_active
2008 && check_map(k10, State, FALSE, TRUE, FALSE,
2009 NULL, NULL) == NULL)
2010 break;
2011#endif
LemonBoy45684c62022-04-24 15:46:42 +01002012 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002013
2014 if (special_keys[i].vim_code1 == NUL)
2015 key = special_keys[i].vim_code0;
2016 else
2017 key = TO_SPECIAL(special_keys[i].vim_code0,
2018 special_keys[i].vim_code1);
2019 key = simplify_key(key, &modifiers);
2020 if (key == CSI)
2021 key = K_CSI;
2022
2023 if (modifiers)
2024 {
2025 string[0] = CSI;
2026 string[1] = KS_MODIFIER;
2027 string[2] = modifiers;
2028 add_to_input_buf(string, 3);
2029 }
2030
2031 if (IS_SPECIAL(key))
2032 {
2033 string[0] = CSI;
2034 string[1] = K_SECOND(key);
2035 string[2] = K_THIRD(key);
2036 add_to_input_buf(string, 3);
2037 }
2038 else
2039 {
2040 int len;
2041
Bram Moolenaar734a8672019-12-02 22:49:38 +01002042 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002043 len = char_to_string(key, string, 40, FALSE);
2044 add_to_input_buf(string, len);
2045 }
2046 break;
2047 }
2048 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002049
2050 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 if (special_keys[i].key_sym == 0)
2052 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002053 WCHAR ch[8];
2054 int len;
2055 int i;
2056 UINT scan_code;
2057
LemonBoy4e0fc892022-06-24 20:18:09 +01002058 // Construct the keyboard state table, the modifiers can and will
2059 // affect the character translation performed by ToUnicode.
2060 // Eg. With a Russian keyboard layout pressing 'n' produces 'Ñ‚' but
2061 // Ctrl+p produces 'p', this is essential for the keybindings to
2062 // work.
LemonBoy77fc0b02022-04-22 22:45:52 +01002063 memset(keyboard_state, 0, 256);
LemonBoy4e0fc892022-06-24 20:18:09 +01002064 if (GetKeyState(VK_CONTROL) & 0x8000)
2065 keyboard_state[VK_CONTROL] = 0x80;
LemonBoy77fc0b02022-04-22 22:45:52 +01002066 if (GetKeyState(VK_SHIFT) & 0x8000)
2067 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002068 if (GetKeyState(VK_CAPITAL) & 0x0001)
2069 keyboard_state[VK_CAPITAL] = 0x01;
LemonBoy4e0fc892022-06-24 20:18:09 +01002070 // Alt-Gr is synthesized as (Right)Alt + Ctrl.
2071 if ((GetKeyState(VK_RMENU) & 0x8000) && keyboard_state[VK_CONTROL])
LemonBoy77fc0b02022-04-22 22:45:52 +01002072 keyboard_state[VK_MENU] = 0x80;
LemonBoy77fc0b02022-04-22 22:45:52 +01002073
2074 // Translate the virtual key according to the current keyboard
2075 // layout.
2076 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2077 // Convert the scan-code into a sequence of zero or more unicode
2078 // codepoints.
2079 // If this is a dead key ToUnicode returns a negative value.
2080 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2081 0);
LemonBoy4e0fc892022-06-24 20:18:09 +01002082 if (len == 0 && keyboard_state[VK_CONTROL])
2083 {
2084 // Handle one more special case: pressing Ctrl+key may
2085 // generate an unprintable ASCII character, try again without
2086 // the modifier to get the pressed key value.
2087 keyboard_state[VK_CONTROL] = 0;
2088 len = ToUnicode(vk, scan_code, keyboard_state, ch,
2089 ARRAY_LENGTH(ch), 0);
2090 keyboard_state[VK_CONTROL] = 0x80;
2091 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002092 dead_key = len < 0;
2093
2094 if (len <= 0)
2095 return;
2096
2097 // Post the message as TranslateMessage would do.
2098 if (msg.message == WM_KEYDOWN)
2099 {
2100 for (i = 0; i < len; i++)
2101 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002102 }
2103 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002104 {
2105 for (i = 0; i < len; i++)
2106 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2107 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108 }
2109 }
2110#ifdef FEAT_MBYTE_IME
2111 else if (msg.message == WM_IME_NOTIFY)
2112 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2113 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002114 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002115 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116#endif
2117
2118#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002119 // Check for <F10>: Default effect is to select the menu. When <F10> is
2120 // mapped we need to stop it here to avoid strange effects (e.g., for the
2121 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002122 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2123 NULL, NULL) == NULL)
2124#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002125 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002126}
2127
2128/*
2129 * Catch up with any queued events. This may put keyboard input into the
2130 * input buffer, call resize call-backs, trigger timers etc. If there is
2131 * nothing in the event queue (& no timers pending), then we return
2132 * immediately.
2133 */
2134 void
2135gui_mch_update(void)
2136{
2137 MSG msg;
2138
2139 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002140 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002141 && !vim_is_input_buf_full())
2142 process_message();
2143}
2144
Bram Moolenaar4231da42016-06-02 14:30:04 +02002145 static void
2146remove_any_timer(void)
2147{
2148 MSG msg;
2149
2150 if (s_wait_timer != 0 && !s_timed_out)
2151 {
2152 KillTimer(NULL, s_wait_timer);
2153
Bram Moolenaar734a8672019-12-02 22:49:38 +01002154 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002155 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002156 ;
2157 s_wait_timer = 0;
2158 }
2159}
2160
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002161/*
2162 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2163 * from the keyboard.
2164 * wtime == -1 Wait forever.
2165 * wtime == 0 This should never happen.
2166 * wtime > 0 Wait wtime milliseconds for a character.
2167 * Returns OK if a character was found to be available within the given time,
2168 * or FAIL otherwise.
2169 */
2170 int
2171gui_mch_wait_for_chars(int wtime)
2172{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002173 int focus;
2174
2175 s_timed_out = FALSE;
2176
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002177 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002178 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002179 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002180 if (s_busy_processing)
2181 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002182
2183 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002184 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2185 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186 }
2187
2188 allow_scrollbar = TRUE;
2189
2190 focus = gui.in_focus;
2191 while (!s_timed_out)
2192 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002193 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002194 if (gui.in_focus != focus)
2195 {
2196 if (gui.in_focus)
2197 gui_mch_start_blink();
2198 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002199 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002200 focus = gui.in_focus;
2201 }
2202
2203 if (s_need_activate)
2204 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002205 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002206 s_need_activate = FALSE;
2207 }
2208
Bram Moolenaar4231da42016-06-02 14:30:04 +02002209#ifdef FEAT_TIMERS
2210 did_add_timer = FALSE;
2211#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002212#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002213 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002214 for (;;)
2215 {
2216 MSG msg;
2217
2218 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002219# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002220 if (did_add_timer)
2221 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002222# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002223 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002224 {
2225 process_message();
2226 break;
2227 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002228 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002229 // TODO: The 10 msec is a compromise between laggy response
2230 // and consuming more CPU time. Better would be to handle
2231 // channel messages when they arrive.
2232 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002233 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002234 break;
2235 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002236#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002237 // Don't use gui_mch_update() because then we will spin-lock until a
2238 // char arrives, instead we use GetMessage() to hang until an
2239 // event arrives. No need to check for input_buf_full because we are
2240 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002241 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002242#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002243
2244 if (input_available())
2245 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002246 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002247 allow_scrollbar = FALSE;
2248
Bram Moolenaar89c00032019-09-04 13:53:21 +02002249 // Clear pending mouse button, the release event may have been
2250 // taken by the dialog window. But don't do this when getting
2251 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002252 if (!s_getting_focus)
2253 s_button_pending = -1;
2254
2255 return OK;
2256 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002257
2258#ifdef FEAT_TIMERS
2259 if (did_add_timer)
2260 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002261 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002262 remove_any_timer();
2263 break;
2264 }
2265#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002266 }
2267 allow_scrollbar = FALSE;
2268 return FAIL;
2269}
2270
2271/*
2272 * Clear a rectangular region of the screen from text pos (row1, col1) to
2273 * (row2, col2) inclusive.
2274 */
2275 void
2276gui_mch_clear_block(
2277 int row1,
2278 int col1,
2279 int row2,
2280 int col2)
2281{
2282 RECT rc;
2283
2284 /*
2285 * Clear one extra pixel at the far right, for when bold characters have
2286 * spilled over to the window border.
2287 * Note: FillRect() excludes right and bottom of rectangle.
2288 */
2289 rc.left = FILL_X(col1);
2290 rc.top = FILL_Y(row1);
2291 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2292 rc.bottom = FILL_Y(row2 + 1);
2293 clear_rect(&rc);
2294}
2295
2296/*
2297 * Clear the whole text window.
2298 */
2299 void
2300gui_mch_clear_all(void)
2301{
2302 RECT rc;
2303
2304 rc.left = 0;
2305 rc.top = 0;
2306 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2307 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2308 clear_rect(&rc);
2309}
2310/*
2311 * Menu stuff.
2312 */
2313
2314 void
2315gui_mch_enable_menu(int flag)
2316{
2317#ifdef FEAT_MENU
2318 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2319#endif
2320}
2321
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002322 void
2323gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002324 int x UNUSED,
2325 int y UNUSED,
2326 int w UNUSED,
2327 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002329 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002330}
2331
2332#if defined(FEAT_MENU) || defined(PROTO)
2333/*
2334 * Make menu item hidden or not hidden
2335 */
2336 void
2337gui_mch_menu_hidden(
2338 vimmenu_T *menu,
2339 int hidden)
2340{
2341 /*
2342 * This doesn't do what we want. Hmm, just grey the menu items for now.
2343 */
2344 /*
2345 if (hidden)
2346 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2347 else
2348 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2349 */
2350 gui_mch_menu_grey(menu, hidden);
2351}
2352
2353/*
2354 * This is called after setting all the menus to grey/hidden or not.
2355 */
2356 void
2357gui_mch_draw_menubar(void)
2358{
2359 DrawMenuBar(s_hwnd);
2360}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002361#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002363/*
2364 * Return the RGB value of a pixel as a long.
2365 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002366 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367gui_mch_get_rgb(guicolor_T pixel)
2368{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002369 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2370 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371}
2372
2373#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002374/*
2375 * Convert pixels in X to dialog units
2376 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377 static WORD
2378PixelToDialogX(int numPixels)
2379{
2380 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2381}
2382
Bram Moolenaar734a8672019-12-02 22:49:38 +01002383/*
2384 * Convert pixels in Y to dialog units
2385 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002386 static WORD
2387PixelToDialogY(int numPixels)
2388{
2389 return (WORD)((numPixels * 8) / s_dlgfntheight);
2390}
2391
Bram Moolenaar734a8672019-12-02 22:49:38 +01002392/*
2393 * Return the width in pixels of the given text in the given DC.
2394 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002395 static int
2396GetTextWidth(HDC hdc, char_u *str, int len)
2397{
2398 SIZE size;
2399
2400 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2401 return size.cx;
2402}
2403
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404/*
2405 * Return the width in pixels of the given text in the given DC, taking care
2406 * of 'encoding' to active codepage conversion.
2407 */
2408 static int
2409GetTextWidthEnc(HDC hdc, char_u *str, int len)
2410{
2411 SIZE size;
2412 WCHAR *wstr;
2413 int n;
2414 int wlen = len;
2415
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002416 wstr = enc_to_utf16(str, &wlen);
2417 if (wstr == NULL)
2418 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002420 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2421 vim_free(wstr);
2422 if (n)
2423 return size.cx;
2424 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002425}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002427static void get_work_area(RECT *spi_rect);
2428
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002429/*
2430 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002431 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2432 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433 */
2434 static BOOL
2435CenterWindow(
2436 HWND hwndChild,
2437 HWND hwndParent)
2438{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002439 HMONITOR mon;
2440 MONITORINFO moninfo;
2441 RECT rChild, rParent, rScreen;
2442 int wChild, hChild, wParent, hParent;
2443 int xNew, yNew;
2444 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445
2446 GetWindowRect(hwndChild, &rChild);
2447 wChild = rChild.right - rChild.left;
2448 hChild = rChild.bottom - rChild.top;
2449
Bram Moolenaar734a8672019-12-02 22:49:38 +01002450 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002451 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002452 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 else
2454 GetWindowRect(hwndParent, &rParent);
2455 wParent = rParent.right - rParent.left;
2456 hParent = rParent.bottom - rParent.top;
2457
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002458 moninfo.cbSize = sizeof(MONITORINFO);
2459 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2460 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002461 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002462 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002463 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002464 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002465 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002466 hdc = GetDC(hwndChild);
2467 rScreen.left = 0;
2468 rScreen.top = 0;
2469 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2470 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2471 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002472 }
2473
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002474 xNew = rParent.left + ((wParent - wChild) / 2);
2475 if (xNew < rScreen.left)
2476 xNew = rScreen.left;
2477 else if ((xNew + wChild) > rScreen.right)
2478 xNew = rScreen.right - wChild;
2479
2480 yNew = rParent.top + ((hParent - hChild) / 2);
2481 if (yNew < rScreen.top)
2482 yNew = rScreen.top;
2483 else if ((yNew + hChild) > rScreen.bottom)
2484 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002485
2486 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2487 SWP_NOSIZE | SWP_NOZORDER);
2488}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002489#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002490
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002491#if defined(FEAT_TOOLBAR) || defined(PROTO)
2492 void
2493gui_mch_show_toolbar(int showit)
2494{
2495 if (s_toolbarhwnd == NULL)
2496 return;
2497
2498 if (showit)
2499 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002500 // Enable unicode support
2501 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2502 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503 ShowWindow(s_toolbarhwnd, SW_SHOW);
2504 }
2505 else
2506 ShowWindow(s_toolbarhwnd, SW_HIDE);
2507}
2508
Bram Moolenaar734a8672019-12-02 22:49:38 +01002509// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002510# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002511
2512#endif
2513
2514#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2515 static void
2516add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2517{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002518 WCHAR *wn;
2519 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002520
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002521 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002522 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002523 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002525 infow.cbSize = sizeof(infow);
2526 infow.fMask = MIIM_TYPE | MIIM_ID;
2527 infow.wID = item_id;
2528 infow.fType = MFT_STRING;
2529 infow.dwTypeData = wn;
2530 infow.cch = (UINT)wcslen(wn);
2531 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2532 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002533}
2534
2535 static void
2536show_tabline_popup_menu(void)
2537{
2538 HMENU tab_pmenu;
2539 long rval;
2540 POINT pt;
2541
Bram Moolenaar734a8672019-12-02 22:49:38 +01002542 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002543 if (hold_gui_events
2544# ifdef FEAT_CMDWIN
2545 || cmdwin_type != 0
2546# endif
2547 )
2548 return;
2549
2550 tab_pmenu = CreatePopupMenu();
2551 if (tab_pmenu == NULL)
2552 return;
2553
2554 if (first_tabpage->tp_next != NULL)
2555 add_tabline_popup_menu_entry(tab_pmenu,
2556 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2557 add_tabline_popup_menu_entry(tab_pmenu,
2558 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2559 add_tabline_popup_menu_entry(tab_pmenu,
2560 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2561
2562 GetCursorPos(&pt);
2563 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2564 NULL);
2565
2566 DestroyMenu(tab_pmenu);
2567
Bram Moolenaar734a8672019-12-02 22:49:38 +01002568 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002569 if (rval > 0)
2570 {
2571 TCHITTESTINFO htinfo;
2572 int idx;
2573
2574 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2575 return;
2576
2577 htinfo.pt.x = pt.x;
2578 htinfo.pt.y = pt.y;
2579 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2580 if (idx == -1)
2581 idx = 0;
2582 else
2583 idx += 1;
2584
2585 send_tabline_menu_event(idx, (int)rval);
2586 }
2587}
2588
2589/*
2590 * Show or hide the tabline.
2591 */
2592 void
2593gui_mch_show_tabline(int showit)
2594{
2595 if (s_tabhwnd == NULL)
2596 return;
2597
2598 if (!showit != !showing_tabline)
2599 {
2600 if (showit)
2601 ShowWindow(s_tabhwnd, SW_SHOW);
2602 else
2603 ShowWindow(s_tabhwnd, SW_HIDE);
2604 showing_tabline = showit;
2605 }
2606}
2607
2608/*
2609 * Return TRUE when tabline is displayed.
2610 */
2611 int
2612gui_mch_showing_tabline(void)
2613{
2614 return s_tabhwnd != NULL && showing_tabline;
2615}
2616
2617/*
2618 * Update the labels of the tabline.
2619 */
2620 void
2621gui_mch_update_tabline(void)
2622{
2623 tabpage_T *tp;
2624 TCITEM tie;
2625 int nr = 0;
2626 int curtabidx = 0;
2627 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002629
2630 if (s_tabhwnd == NULL)
2631 return;
2632
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002633 // Enable unicode support
2634 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002635
2636 tie.mask = TCIF_TEXT;
2637 tie.iImage = -1;
2638
Bram Moolenaar734a8672019-12-02 22:49:38 +01002639 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002640 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2641
Bram Moolenaar734a8672019-12-02 22:49:38 +01002642 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002643 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2644 {
2645 if (tp == curtab)
2646 curtabidx = nr;
2647
2648 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2649 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002650 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002651 tie.pszText = "-Empty-";
2652 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2653 tabadded = 1;
2654 }
2655
2656 get_tabline_label(tp, FALSE);
2657 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002658
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002659 wstr = enc_to_utf16(NameBuff, NULL);
2660 if (wstr != NULL)
2661 {
2662 TCITEMW tiw;
2663
2664 tiw.mask = TCIF_TEXT;
2665 tiw.iImage = -1;
2666 tiw.pszText = wstr;
2667 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2668 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670 }
2671
Bram Moolenaar734a8672019-12-02 22:49:38 +01002672 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002673 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2674 TabCtrl_DeleteItem(s_tabhwnd, nr);
2675
2676 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2677 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2678
Bram Moolenaar734a8672019-12-02 22:49:38 +01002679 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002680 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2681 RedrawWindow(s_tabhwnd, NULL, NULL,
2682 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2683
2684 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2685 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2686}
2687
2688/*
2689 * Set the current tab to "nr". First tab is 1.
2690 */
2691 void
2692gui_mch_set_curtab(int nr)
2693{
2694 if (s_tabhwnd == NULL)
2695 return;
2696
2697 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2698 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2699}
2700
2701#endif
2702
2703/*
2704 * ":simalt" command.
2705 */
2706 void
2707ex_simalt(exarg_T *eap)
2708{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002709 char_u *keys = eap->arg;
2710 int fill_typebuf = FALSE;
2711 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712
2713 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2714 while (*keys)
2715 {
2716 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002717 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002718 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2719 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002720 fill_typebuf = TRUE;
2721 }
2722 if (fill_typebuf)
2723 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002724 // Put a NOP in the typeahead buffer so that the message will get
2725 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002726 key_name[0] = K_SPECIAL;
2727 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002728 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002729 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002730#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002731 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002732#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002733 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 }
2735}
2736
2737/*
2738 * Create the find & replace dialogs.
2739 * You can't have both at once: ":find" when replace is showing, destroys
2740 * the replace dialog first, and the other way around.
2741 */
2742#ifdef MSWIN_FIND_REPLACE
2743 static void
2744initialise_findrep(char_u *initial_string)
2745{
2746 int wword = FALSE;
2747 int mcase = !p_ic;
2748 char_u *entry_text;
2749
Bram Moolenaar734a8672019-12-02 22:49:38 +01002750 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2752
2753 s_findrep_struct.hwndOwner = s_hwnd;
2754 s_findrep_struct.Flags = FR_DOWN;
2755 if (mcase)
2756 s_findrep_struct.Flags |= FR_MATCHCASE;
2757 if (wword)
2758 s_findrep_struct.Flags |= FR_WHOLEWORD;
2759 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002760 {
2761 WCHAR *p = enc_to_utf16(entry_text, NULL);
2762 if (p != NULL)
2763 {
2764 int len = s_findrep_struct.wFindWhatLen - 1;
2765
2766 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2767 s_findrep_struct.lpstrFindWhat[len] = NUL;
2768 vim_free(p);
2769 }
2770 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002771 vim_free(entry_text);
2772}
2773#endif
2774
2775 static void
2776set_window_title(HWND hwnd, char *title)
2777{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002778 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002779 {
2780 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781
Bram Moolenaar734a8672019-12-02 22:49:38 +01002782 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2784 if (wbuf != NULL)
2785 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002786 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002787 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002788 }
2789 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002790 else
2791 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002792}
2793
2794 void
2795gui_mch_find_dialog(exarg_T *eap)
2796{
2797#ifdef MSWIN_FIND_REPLACE
2798 if (s_findrep_msg != 0)
2799 {
2800 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2801 DestroyWindow(s_findrep_hwnd);
2802
2803 if (!IsWindow(s_findrep_hwnd))
2804 {
2805 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002806 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002807 }
2808
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002809 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 (void)SetFocus(s_findrep_hwnd);
2811
2812 s_findrep_is_find = TRUE;
2813 }
2814#endif
2815}
2816
2817
2818 void
2819gui_mch_replace_dialog(exarg_T *eap)
2820{
2821#ifdef MSWIN_FIND_REPLACE
2822 if (s_findrep_msg != 0)
2823 {
2824 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2825 DestroyWindow(s_findrep_hwnd);
2826
2827 if (!IsWindow(s_findrep_hwnd))
2828 {
2829 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002830 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831 }
2832
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002833 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002834 (void)SetFocus(s_findrep_hwnd);
2835
2836 s_findrep_is_find = FALSE;
2837 }
2838#endif
2839}
2840
2841
2842/*
2843 * Set visibility of the pointer.
2844 */
2845 void
2846gui_mch_mousehide(int hide)
2847{
2848 if (hide != gui.pointer_hidden)
2849 {
2850 ShowCursor(!hide);
2851 gui.pointer_hidden = hide;
2852 }
2853}
2854
2855#ifdef FEAT_MENU
2856 static void
2857gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2858{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002859 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002860 gui_mch_mousehide(FALSE);
2861
2862 (void)TrackPopupMenu(
2863 (HMENU)menu->submenu_id,
2864 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2865 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002866 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867 s_hwnd,
2868 NULL);
2869 /*
2870 * NOTE: The pop-up menu can eat the mouse up event.
2871 * We deal with this in normal.c.
2872 */
2873}
2874#endif
2875
2876/*
2877 * Got a message when the system will go down.
2878 */
2879 static void
2880_OnEndSession(void)
2881{
2882 getout_preserve_modified(1);
2883}
2884
2885/*
2886 * Get this message when the user clicks on the cross in the top right corner
2887 * of a Windows95 window.
2888 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002890_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002891{
2892 gui_shell_closed();
2893}
2894
2895/*
2896 * Get a message when the window is being destroyed.
2897 */
2898 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002899_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900{
2901 if (!destroying)
2902 _OnClose(hwnd);
2903}
2904
2905 static void
2906_OnPaint(
2907 HWND hwnd)
2908{
2909 if (!IsMinimized(hwnd))
2910 {
2911 PAINTSTRUCT ps;
2912
Bram Moolenaar734a8672019-12-02 22:49:38 +01002913 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002914 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915
Bram Moolenaar734a8672019-12-02 22:49:38 +01002916 // prevent multi-byte characters from misprinting on an invalid
2917 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918 if (has_mbyte)
2919 {
2920 RECT rect;
2921
2922 GetClientRect(hwnd, &rect);
2923 ps.rcPaint.left = rect.left;
2924 ps.rcPaint.right = rect.right;
2925 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002926
2927 if (!IsRectEmpty(&ps.rcPaint))
2928 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002929 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2930 ps.rcPaint.right - ps.rcPaint.left + 1,
2931 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2932 }
2933
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002934 EndPaint(hwnd, &ps);
2935 }
2936}
2937
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 static void
2939_OnSize(
2940 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002941 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002942 int cx,
2943 int cy)
2944{
K.Takatac81e9bf2022-01-16 14:15:49 +00002945 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002946 {
2947 gui_resize_shell(cx, cy);
2948
Bram Moolenaar734a8672019-12-02 22:49:38 +01002949 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002950 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002951 }
2952}
2953
2954 static void
2955_OnSetFocus(
2956 HWND hwnd,
2957 HWND hwndOldFocus)
2958{
2959 gui_focus_change(TRUE);
2960 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002961 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002962}
2963
2964 static void
2965_OnKillFocus(
2966 HWND hwnd,
2967 HWND hwndNewFocus)
2968{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002969 if (destroying)
2970 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002971 gui_focus_change(FALSE);
2972 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002973 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002974}
2975
2976/*
2977 * Get a message when the user switches back to vim
2978 */
2979 static LRESULT
2980_OnActivateApp(
2981 HWND hwnd,
2982 BOOL fActivate,
2983 DWORD dwThreadId)
2984{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002985 // we call gui_focus_change() in _OnSetFocus()
2986 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002987 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002988}
2989
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002990 void
2991gui_mch_destroy_scrollbar(scrollbar_T *sb)
2992{
2993 DestroyWindow(sb->id);
2994}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002995
2996/*
2997 * Get current mouse coordinates in text window.
2998 */
2999 void
3000gui_mch_getmouse(int *x, int *y)
3001{
3002 RECT rct;
3003 POINT mp;
3004
3005 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003006 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003007 *x = (int)(mp.x - rct.left);
3008 *y = (int)(mp.y - rct.top);
3009}
3010
3011/*
3012 * Move mouse pointer to character at (x, y).
3013 */
3014 void
3015gui_mch_setmouse(int x, int y)
3016{
3017 RECT rct;
3018
3019 (void)GetWindowRect(s_textArea, &rct);
3020 (void)SetCursorPos(x + gui.border_offset + rct.left,
3021 y + gui.border_offset + rct.top);
3022}
3023
3024 static void
3025gui_mswin_get_valid_dimensions(
3026 int w,
3027 int h,
3028 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003029 int *valid_h,
3030 int *cols,
3031 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032{
3033 int base_width, base_height;
3034
3035 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003036 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3037 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003038 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003039 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3040 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3041 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3042 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003043 *cols = (w - base_width) / gui.char_width;
3044 *rows = (h - base_height) / gui.char_height;
3045 *valid_w = base_width + *cols * gui.char_width;
3046 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003047}
3048
3049 void
3050gui_mch_flash(int msec)
3051{
3052 RECT rc;
3053
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003054#if defined(FEAT_DIRECTX)
3055 if (IS_ENABLE_DIRECTX())
3056 DWriteContext_Flush(s_dwc);
3057#endif
3058
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059 /*
3060 * Note: InvertRect() excludes right and bottom of rectangle.
3061 */
3062 rc.left = 0;
3063 rc.top = 0;
3064 rc.right = gui.num_cols * gui.char_width;
3065 rc.bottom = gui.num_rows * gui.char_height;
3066 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003067 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003068
Bram Moolenaar734a8672019-12-02 22:49:38 +01003069 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003070
3071 InvertRect(s_hdc, &rc);
3072}
3073
3074/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003075 * Check if the specified point is on-screen. (multi-monitor aware)
3076 */
3077 static BOOL
3078is_point_onscreen(int x, int y)
3079{
3080 POINT pt = {x, y};
3081
3082 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3083}
3084
3085/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003086 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003087 *
3088 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3089 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3090 * only works when the whole window is on-screen.
3091 */
3092 static BOOL
3093is_window_onscreen(HWND hwnd)
3094{
3095 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003096 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003097
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003098 GetClientRect(hwnd, &rc);
3099 p1.x = rc.left;
3100 p1.y = rc.top;
3101 p2.x = rc.right - 1;
3102 p2.y = rc.bottom - 1;
3103 ClientToScreen(hwnd, &p1);
3104 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003105
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003106 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003107 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003108 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003109 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003110 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003111 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003112 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003113 return FALSE;
3114 return TRUE;
3115}
3116
3117/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003118 * Return flags used for scrolling.
3119 * The SW_INVALIDATE is required when part of the window is covered or
3120 * off-screen. Refer to MS KB Q75236.
3121 */
3122 static int
3123get_scroll_flags(void)
3124{
3125 HWND hwnd;
3126 RECT rcVim, rcOther, rcDest;
3127
Bram Moolenaar185577e2020-10-29 20:08:21 +01003128 // Check if the window is (partly) off-screen.
3129 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 return SW_INVALIDATE;
3131
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003132 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003133 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003134 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3135 if (IsWindowVisible(hwnd))
3136 {
3137 GetWindowRect(hwnd, &rcOther);
3138 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3139 return SW_INVALIDATE;
3140 }
3141 return 0;
3142}
3143
3144/*
3145 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3146 * may not be scrolled out properly.
3147 * For gVim, when _OnScroll() is repeated, the character at the
3148 * previous cursor position may be left drawn after scroll.
3149 * The problem can be avoided by calling GetPixel() to get a pixel in
3150 * the region before ScrollWindowEx().
3151 */
3152 static void
3153intel_gpu_workaround(void)
3154{
3155 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3156}
3157
3158/*
3159 * Delete the given number of lines from the given row, scrolling up any
3160 * text further down within the scroll region.
3161 */
3162 void
3163gui_mch_delete_lines(
3164 int row,
3165 int num_lines)
3166{
3167 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003168
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003169 rc.left = FILL_X(gui.scroll_region_left);
3170 rc.right = FILL_X(gui.scroll_region_right + 1);
3171 rc.top = FILL_Y(row);
3172 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3173
Bram Moolenaar92467d32017-12-05 13:22:16 +01003174#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003175 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003177 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003178 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003179 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003180#endif
3181 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003182#if defined(FEAT_DIRECTX)
3183 if (IS_ENABLE_DIRECTX())
3184 DWriteContext_Flush(s_dwc);
3185#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003186 intel_gpu_workaround();
3187 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003189 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003190 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191
Bram Moolenaar734a8672019-12-02 22:49:38 +01003192 // This seems to be required to avoid the cursor disappearing when
3193 // scrolling such that the cursor ends up in the top-left character on
3194 // the screen... But why? (Webb)
3195 // It's probably fixed by disabling drawing the cursor while scrolling.
3196 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003197
3198 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3199 gui.scroll_region_left,
3200 gui.scroll_region_bot, gui.scroll_region_right);
3201}
3202
3203/*
3204 * Insert the given number of lines before the given row, scrolling down any
3205 * following text within the scroll region.
3206 */
3207 void
3208gui_mch_insert_lines(
3209 int row,
3210 int num_lines)
3211{
3212 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003213
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 rc.left = FILL_X(gui.scroll_region_left);
3215 rc.right = FILL_X(gui.scroll_region_right + 1);
3216 rc.top = FILL_Y(row);
3217 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003218
3219#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003220 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003221 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003222 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003223 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003224 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003225#endif
3226 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003227#if defined(FEAT_DIRECTX)
3228 if (IS_ENABLE_DIRECTX())
3229 DWriteContext_Flush(s_dwc);
3230#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003231 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003232 // The SW_INVALIDATE is required when part of the window is covered or
3233 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003234 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003236 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003237 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 gui_clear_block(row, gui.scroll_region_left,
3240 row + num_lines - 1, gui.scroll_region_right);
3241}
3242
3243
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003244 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003245gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003246{
3247#if defined(FEAT_DIRECTX)
3248 DWriteContext_Close(s_dwc);
3249 DWrite_Final();
3250 s_dwc = NULL;
3251#endif
3252
3253 ReleaseDC(s_textArea, s_hdc);
3254 DeleteObject(s_brush);
3255
3256#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003257 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003258 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3259#endif
3260
Bram Moolenaar734a8672019-12-02 22:49:38 +01003261 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262 if (s_hwnd != NULL)
3263 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003264 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003265 DestroyWindow(s_hwnd);
3266 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267}
3268
3269 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003270logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271{
3272 char *p;
3273 char *res;
3274 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003275 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003276 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003277 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003279 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3280 if (font_name == NULL)
3281 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003283 quality_name = quality_id2name((int)lf.lfQuality);
3284
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003285 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003286 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003287 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003288 if (res != NULL)
3289 {
3290 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003291 // make a normal font string out of the lf thing:
3292 points = pixels_to_points(
3293 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3294 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3295 sprintf((char *)p, "%s:h%d", font_name, points);
3296 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003297 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003298 while (*p)
3299 {
3300 if (*p == ' ')
3301 *p = '_';
3302 ++p;
3303 }
3304 if (lf.lfItalic)
3305 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003306 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307 STRCAT(p, ":b");
3308 if (lf.lfUnderline)
3309 STRCAT(p, ":u");
3310 if (lf.lfStrikeOut)
3311 STRCAT(p, ":s");
3312 if (charset_name != NULL)
3313 {
3314 STRCAT(p, ":c");
3315 STRCAT(p, charset_name);
3316 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003317 if (quality_name != NULL)
3318 {
3319 STRCAT(p, ":q");
3320 STRCAT(p, quality_name);
3321 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003322 }
3323
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003324 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325 return (char_u *)res;
3326}
3327
3328
3329#ifdef FEAT_MBYTE_IME
3330/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003331 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003332 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003333 */
3334 static void
3335update_im_font(void)
3336{
K.Takatac81e9bf2022-01-16 14:15:49 +00003337 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338
3339 if (p_guifontwide != NULL && *p_guifontwide != NUL
3340 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003341 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342 norm_logfont = lf_wide;
3343 else
3344 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003345
3346 lf = norm_logfont;
3347 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3348 // Work around when PerMonitorV2 is not enabled in the process level.
3349 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3350 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351}
3352#endif
3353
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354/*
3355 * Handler of gui.wide_font (p_guifontwide) changed notification.
3356 */
3357 void
3358gui_mch_wide_font_changed(void)
3359{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003360 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003362#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003363 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003364#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365
3366 gui_mch_free_font(gui.wide_ital_font);
3367 gui.wide_ital_font = NOFONT;
3368 gui_mch_free_font(gui.wide_bold_font);
3369 gui.wide_bold_font = NOFONT;
3370 gui_mch_free_font(gui.wide_boldital_font);
3371 gui.wide_boldital_font = NOFONT;
3372
3373 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003374 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375 {
3376 if (!lf.lfItalic)
3377 {
3378 lf.lfItalic = TRUE;
3379 gui.wide_ital_font = get_font_handle(&lf);
3380 lf.lfItalic = FALSE;
3381 }
3382 if (lf.lfWeight < FW_BOLD)
3383 {
3384 lf.lfWeight = FW_BOLD;
3385 gui.wide_bold_font = get_font_handle(&lf);
3386 if (!lf.lfItalic)
3387 {
3388 lf.lfItalic = TRUE;
3389 gui.wide_boldital_font = get_font_handle(&lf);
3390 }
3391 }
3392 }
3393}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394
3395/*
3396 * Initialise vim to use the font with the given name.
3397 * Return FAIL if the font could not be loaded, OK otherwise.
3398 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003399 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003400gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401{
K.Takatac81e9bf2022-01-16 14:15:49 +00003402 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 GuiFont font = NOFONT;
3404 char_u *p;
3405
Bram Moolenaar734a8672019-12-02 22:49:38 +01003406 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003408 {
3409 lfOrig = lf;
3410 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003412 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413 if (font == NOFONT)
3414 return FAIL;
3415
3416 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003417 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003418#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419 norm_logfont = lf;
3420 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003421 if (!s_in_dpichanged)
3422 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423#endif
3424 gui_mch_free_font(gui.norm_font);
3425 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003426 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003427 GetFontSize(font);
3428
K.Takatac81e9bf2022-01-16 14:15:49 +00003429 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430 if (p != NULL)
3431 {
3432 hl_set_font_name(p);
3433
Bram Moolenaar734a8672019-12-02 22:49:38 +01003434 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3436 {
3437 vim_free(p_guifont);
3438 p_guifont = p;
3439 }
3440 else
3441 vim_free(p);
3442 }
3443
3444 gui_mch_free_font(gui.ital_font);
3445 gui.ital_font = NOFONT;
3446 gui_mch_free_font(gui.bold_font);
3447 gui.bold_font = NOFONT;
3448 gui_mch_free_font(gui.boldital_font);
3449 gui.boldital_font = NOFONT;
3450
3451 if (!lf.lfItalic)
3452 {
3453 lf.lfItalic = TRUE;
3454 gui.ital_font = get_font_handle(&lf);
3455 lf.lfItalic = FALSE;
3456 }
3457 if (lf.lfWeight < FW_BOLD)
3458 {
3459 lf.lfWeight = FW_BOLD;
3460 gui.bold_font = get_font_handle(&lf);
3461 if (!lf.lfItalic)
3462 {
3463 lf.lfItalic = TRUE;
3464 gui.boldital_font = get_font_handle(&lf);
3465 }
3466 }
3467
3468 return OK;
3469}
3470
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003471/*
3472 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003473 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474 */
3475 int
3476gui_mch_maximized(void)
3477{
3478 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003479 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480
3481 wp.length = sizeof(WINDOWPLACEMENT);
3482 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003483 {
3484 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003486 && wp.flags == WPF_RESTORETOMAXIMIZED))
3487 return TRUE;
3488 if (wp.showCmd == SW_SHOWMINIMIZED)
3489 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003490
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003491 // Assume the window is snapped when the sizes from two APIs differ.
3492 GetWindowRect(s_hwnd, &rc);
3493 if ((rc.right - rc.left !=
3494 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3495 || (rc.bottom - rc.top !=
3496 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3497 return TRUE;
3498 }
3499 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500}
3501
3502/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003503 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3504 * is set. Compute the new Rows and Columns. This is like resizing the
3505 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 */
3507 void
3508gui_mch_newfont(void)
3509{
3510 RECT rect;
3511
3512 GetWindowRect(s_hwnd, &rect);
3513 if (win_socket_id == 0)
3514 {
3515 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003516 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3517 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003519 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3520 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3521 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3522 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523 }
3524 else
3525 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003526 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003528 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003529 }
3530}
3531
3532/*
3533 * Set the window title
3534 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003535 void
3536gui_mch_settitle(
3537 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003538 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539{
3540 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3541}
3542
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003543#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003544// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3545// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546static LPCSTR mshape_idcs[] =
3547{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003548 IDC_ARROW, // arrow
3549 MAKEINTRESOURCE(0), // blank
3550 IDC_IBEAM, // beam
3551 IDC_SIZENS, // updown
3552 IDC_SIZENS, // udsizing
3553 IDC_SIZEWE, // leftright
3554 IDC_SIZEWE, // lrsizing
3555 IDC_WAIT, // busy
3556 IDC_NO, // no
3557 IDC_ARROW, // crosshair
3558 IDC_ARROW, // hand1
3559 IDC_ARROW, // hand2
3560 IDC_ARROW, // pencil
3561 IDC_ARROW, // question
3562 IDC_ARROW, // right-arrow
3563 IDC_UPARROW, // up-arrow
3564 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003565};
3566
3567 void
3568mch_set_mouse_shape(int shape)
3569{
3570 LPCSTR idc;
3571
3572 if (shape == MSHAPE_HIDE)
3573 ShowCursor(FALSE);
3574 else
3575 {
3576 if (shape >= MSHAPE_NUMBERED)
3577 idc = IDC_ARROW;
3578 else
3579 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003580 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581 if (!p_mh)
3582 {
3583 POINT mp;
3584
Bram Moolenaar734a8672019-12-02 22:49:38 +01003585 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003586 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587 (void)SetCursorPos(mp.x, mp.y);
3588 ShowCursor(TRUE);
3589 }
3590 }
3591}
3592#endif
3593
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003594#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003595/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596 * Wide version of convert_filter().
3597 */
3598 static WCHAR *
3599convert_filterW(char_u *s)
3600{
3601 char_u *tmp;
3602 int len;
3603 WCHAR *res;
3604
3605 tmp = convert_filter(s);
3606 if (tmp == NULL)
3607 return NULL;
3608 len = (int)STRLEN(s) + 3;
3609 res = enc_to_utf16(tmp, &len);
3610 vim_free(tmp);
3611 return res;
3612}
3613
3614/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003615 * Pop open a file browser and return the file selected, in allocated memory,
3616 * or NULL if Cancel is hit.
3617 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3618 * title - Title message for the file browser dialog.
3619 * dflt - Default name of file.
3620 * ext - Default extension to be added to files without extensions.
3621 * initdir - directory in which to open the browser (NULL = current dir)
3622 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003624 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003625gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003626 int saving,
3627 char_u *title,
3628 char_u *dflt,
3629 char_u *ext,
3630 char_u *initdir,
3631 char_u *filter)
3632{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003633 // We always use the wide function. This means enc_to_utf16() must work,
3634 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635 OPENFILENAMEW fileStruct;
3636 WCHAR fileBuf[MAXPATHL];
3637 WCHAR *wp;
3638 int i;
3639 WCHAR *titlep = NULL;
3640 WCHAR *extp = NULL;
3641 WCHAR *initdirp = NULL;
3642 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003643 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003644 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003645
3646 if (dflt == NULL)
3647 fileBuf[0] = NUL;
3648 else
3649 {
3650 wp = enc_to_utf16(dflt, NULL);
3651 if (wp == NULL)
3652 fileBuf[0] = NUL;
3653 else
3654 {
3655 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3656 fileBuf[i] = wp[i];
3657 fileBuf[i] = NUL;
3658 vim_free(wp);
3659 }
3660 }
3661
Bram Moolenaar734a8672019-12-02 22:49:38 +01003662 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663 filterp = convert_filterW(filter);
3664
Bram Moolenaara80faa82020-04-12 19:37:17 +02003665 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003666# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003667 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003668 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003669# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003671# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672
3673 if (title != NULL)
3674 titlep = enc_to_utf16(title, NULL);
3675 fileStruct.lpstrTitle = titlep;
3676
3677 if (ext != NULL)
3678 extp = enc_to_utf16(ext, NULL);
3679 fileStruct.lpstrDefExt = extp;
3680
3681 fileStruct.lpstrFile = fileBuf;
3682 fileStruct.nMaxFile = MAXPATHL;
3683 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003684 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3685 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 if (initdir != NULL && *initdir != NUL)
3687 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003688 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689 initdirp = enc_to_utf16(initdir, NULL);
3690 if (initdirp != NULL)
3691 {
3692 for (wp = initdirp; *wp != NUL; ++wp)
3693 if (*wp == '/')
3694 *wp = '\\';
3695 }
3696 fileStruct.lpstrInitialDir = initdirp;
3697 }
3698
3699 /*
3700 * TODO: Allow selection of multiple files. Needs another arg to this
3701 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3702 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3703 * files that don't exist yet, so I haven't put it in. What about
3704 * OFN_PATHMUSTEXIST?
3705 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3706 */
3707 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003708# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003709 if (curbuf->b_p_bin)
3710 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003711# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003713 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003715 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716
3717 vim_free(filterp);
3718 vim_free(initdirp);
3719 vim_free(titlep);
3720 vim_free(extp);
3721
K.Takata14b8d6a2022-01-20 15:05:22 +00003722 if (!ret)
3723 return NULL;
3724
3725 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003727 if (p == NULL)
3728 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729
Bram Moolenaar734a8672019-12-02 22:49:38 +01003730 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003731 SetFocus(s_hwnd);
3732
Bram Moolenaar734a8672019-12-02 22:49:38 +01003733 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003734 q = vim_strsave(shorten_fname1(p));
3735 vim_free(p);
3736 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738
3739
3740/*
3741 * Convert the string s to the proper format for a filter string by replacing
3742 * the \t and \n delimiters with \0.
3743 * Returns the converted string in allocated memory.
3744 *
3745 * Keep in sync with convert_filterW() above!
3746 */
3747 static char_u *
3748convert_filter(char_u *s)
3749{
3750 char_u *res;
3751 unsigned s_len = (unsigned)STRLEN(s);
3752 unsigned i;
3753
3754 res = alloc(s_len + 3);
3755 if (res != NULL)
3756 {
3757 for (i = 0; i < s_len; ++i)
3758 if (s[i] == '\t' || s[i] == '\n')
3759 res[i] = '\0';
3760 else
3761 res[i] = s[i];
3762 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003763 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 res[s_len + 1] = NUL;
3765 res[s_len + 2] = NUL;
3766 }
3767 return res;
3768}
3769
3770/*
3771 * Select a directory.
3772 */
3773 char_u *
3774gui_mch_browsedir(char_u *title, char_u *initdir)
3775{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003776 // We fake this: Use a filter that doesn't select anything and a default
3777 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3779 initdir, (char_u *)_("Directory\t*.nothing\n"));
3780}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003781#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 static void
3784_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003785 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 HDROP hDrop)
3787{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003788#define BUFPATHLEN _MAX_PATH
3789#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 char szFile[BUFPATHLEN];
3792 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3793 UINT i;
3794 char_u **fnames;
3795 POINT pt;
3796 int_u modifiers = 0;
3797
Bram Moolenaar734a8672019-12-02 22:49:38 +01003798 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799 DragQueryPoint(hDrop, &pt);
3800 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3801
3802 reset_VIsual();
3803
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003804 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805
3806 if (fnames != NULL)
3807 for (i = 0; i < cFiles; ++i)
3808 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3810 fnames[i] = utf16_to_enc(wszFile, NULL);
3811 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 {
3813 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3814 fnames[i] = vim_strsave((char_u *)szFile);
3815 }
3816 }
3817
3818 DragFinish(hDrop);
3819
3820 if (fnames != NULL)
3821 {
LemonBoy45684c62022-04-24 15:46:42 +01003822 int kbd_modifiers = get_active_modifiers();
3823
3824 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003826 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003827 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003828 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829 modifiers |= MOUSE_ALT;
3830
3831 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3832
3833 s_need_activate = TRUE;
3834 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835}
3836
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003837 static int
3838_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003839 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003840 HWND hwndCtl,
3841 UINT code,
3842 int pos)
3843{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003844 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845 scrollbar_T *sb, *sb_info;
3846 long val;
3847 int dragging = FALSE;
3848 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003849 SCROLLINFO si;
3850
3851 si.cbSize = sizeof(si);
3852 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853
3854 sb = gui_mswin_find_scrollbar(hwndCtl);
3855 if (sb == NULL)
3856 return 0;
3857
Bram Moolenaar734a8672019-12-02 22:49:38 +01003858 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003859 {
3860 /*
3861 * Careful: need to get scrollbar info out of first (left) scrollbar
3862 * for window, but keep real scrollbar too because we must pass it to
3863 * gui_drag_scrollbar().
3864 */
3865 sb_info = &sb->wp->w_scrollbars[0];
3866 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003867 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003868 sb_info = sb;
3869 val = sb_info->value;
3870
3871 switch (code)
3872 {
3873 case SB_THUMBTRACK:
3874 val = pos;
3875 dragging = TRUE;
3876 if (sb->scroll_shift > 0)
3877 val <<= sb->scroll_shift;
3878 break;
3879 case SB_LINEDOWN:
3880 val++;
3881 break;
3882 case SB_LINEUP:
3883 val--;
3884 break;
3885 case SB_PAGEDOWN:
3886 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3887 break;
3888 case SB_PAGEUP:
3889 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3890 break;
3891 case SB_TOP:
3892 val = 0;
3893 break;
3894 case SB_BOTTOM:
3895 val = sb_info->max;
3896 break;
3897 case SB_ENDSCROLL:
3898 if (prev_code == SB_THUMBTRACK)
3899 {
3900 /*
3901 * "pos" only gives us 16-bit data. In case of large file,
3902 * use GetScrollPos() which returns 32-bit. Unfortunately it
3903 * is not valid while the scrollbar is being dragged.
3904 */
3905 val = GetScrollPos(hwndCtl, SB_CTL);
3906 if (sb->scroll_shift > 0)
3907 val <<= sb->scroll_shift;
3908 }
3909 break;
3910
3911 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003912 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913 return 0;
3914 }
3915 prev_code = code;
3916
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3918 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919
3920 /*
3921 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3922 */
3923 if (sb->wp != NULL)
3924 {
3925 scrollbar_T *sba = sb->wp->w_scrollbars;
3926 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3927
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929 }
3930
Bram Moolenaar734a8672019-12-02 22:49:38 +01003931 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003932 s_busy_processing = TRUE;
3933
Bram Moolenaar734a8672019-12-02 22:49:38 +01003934 // When "allow_scrollbar" is FALSE still need to remember the new
3935 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003936 dont_scroll = !allow_scrollbar;
3937
Bram Moolenaara338adc2018-01-31 20:51:47 +01003938 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003939 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003940 mch_enable_flush();
3941 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003942
3943 s_busy_processing = FALSE;
3944 dont_scroll = dont_scroll_save;
3945
3946 return 0;
3947}
3948
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#ifdef FEAT_XPM_W32
3951# include "xpm_w32.h"
3952#endif
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
Bram Moolenaar734a8672019-12-02 22:49:38 +01003955// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#define TEAROFF_PADDING_X 2
3957#define TEAROFF_BUTTON_PAD_X 8
3958#define TEAROFF_MIN_WIDTH 200
3959#define TEAROFF_SUBMENU_LABEL ">>"
3960#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3961
3962
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003963#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964# define ID_BEVAL_TOOLTIP 200
3965# define BEVAL_TEXT_LEN MAXPATHL
3966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003968static UINT_PTR beval_timer_id = 0;
3969static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003970#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003971
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003972
Bram Moolenaar734a8672019-12-02 22:49:38 +01003973// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975#ifdef FEAT_MENU
3976static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979/*
3980 * Use the system font for dialogs and tear-off menus. Remove this line to
3981 * use DLG_FONT_NAME.
3982 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003983#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#define VIM_CLASSW L"Vim"
3987
Bram Moolenaar734a8672019-12-02 22:49:38 +01003988// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3989// thus there should be room for every dialog. For tearoffs it's made bigger
3990// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#define DLG_ALLOC_SIZE 16 * 1024
3992
3993/*
3994 * stuff for dialogs, menus, tearoffs etc.
3995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996static PWORD
3997add_dialog_element(
3998 PWORD p,
3999 DWORD lStyle,
4000 WORD x,
4001 WORD y,
4002 WORD w,
4003 WORD h,
4004 WORD Id,
4005 WORD clss,
4006 const char *caption);
4007static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004008static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004009#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012static void get_dialog_font_metrics(void);
4013
4014static int dialog_default_button = -1;
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#ifdef FEAT_TOOLBAR
4017static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004018static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004019static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004021#else
4022# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
4024
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004025#ifdef FEAT_GUI_TABLINE
4026static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004027static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004028#endif
4029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#ifdef FEAT_MBYTE_IME
4031static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4032static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4033#endif
4034#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4035# ifdef NOIME
4036typedef struct tagCOMPOSITIONFORM {
4037 DWORD dwStyle;
4038 POINT ptCurrentPos;
4039 RECT rcArea;
4040} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4041typedef HANDLE HIMC;
4042# endif
4043
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004044static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004045static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4046static HIMC (WINAPI *pImmGetContext)(HWND);
4047static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4048static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4049static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4050static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004051static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4052static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004053static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4054static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004055static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056static void dyn_imm_load(void);
4057#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058# define pImmGetCompositionStringW ImmGetCompositionStringW
4059# define pImmGetContext ImmGetContext
4060# define pImmAssociateContext ImmAssociateContext
4061# define pImmReleaseContext ImmReleaseContext
4062# define pImmGetOpenStatus ImmGetOpenStatus
4063# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004064# define pImmGetCompositionFontW ImmGetCompositionFontW
4065# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066# define pImmSetCompositionWindow ImmSetCompositionWindow
4067# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004068# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069#endif
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#ifdef FEAT_MENU
4072/*
4073 * Figure out how high the menu bar is at the moment.
4074 */
4075 static int
4076gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004077 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078{
4079 static int old_menu_height = -1;
4080
4081 RECT rc1, rc2;
4082 int num;
4083 int menu_height;
4084
4085 if (gui.menu_is_active)
4086 num = GetMenuItemCount(s_menuBar);
4087 else
4088 num = 0;
4089
4090 if (num == 0)
4091 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004092 else if (IsMinimized(s_hwnd))
4093 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004094 // The height of the menu cannot be determined while the window is
4095 // minimized. Take the previous height if the menu is changed in that
4096 // state, to avoid that Vim's vertical window size accidentally
4097 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004098 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 else
4101 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004102 /*
4103 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4104 * seem to have been set yet, so menu wraps in default window
4105 * width which is very narrow. Instead just return height of a
4106 * single menu item. Will still be wrong when the menu really
4107 * should wrap over more than one line.
4108 */
4109 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4110 if (gui.starting)
4111 menu_height = rc1.bottom - rc1.top + 1;
4112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004114 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4115 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
4118
4119 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004120 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004121 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 return menu_height;
4124}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004125#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127
4128/*
4129 * Setup for the Intellimouse
4130 */
LemonBoyc27747e2022-05-07 12:25:40 +01004131 static long
4132mouse_vertical_scroll_step(void)
4133{
4134 UINT val;
4135 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4136 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4137 return 3; // Safe default;
4138}
4139
4140 static long
4141mouse_horizontal_scroll_step(void)
4142{
4143 UINT val;
4144 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4145 return (long)val;
4146 return 3; // Safe default;
4147}
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 static void
4150init_mouse_wheel(void)
4151{
LemonBoyc27747e2022-05-07 12:25:40 +01004152 // Get the default values for the horizontal and vertical scroll steps from
4153 // the system.
4154 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4155 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
Bram Moolenaarae20f342019-11-05 21:09:23 +01004158/*
LemonBoya773d842022-05-10 20:54:46 +01004159 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004162_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
LemonBoy365d8f72022-05-05 19:23:07 +01004164 int button;
4165 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004166 int modifiers = 0;
4167 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004168 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4169 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaarae20f342019-11-05 21:09:23 +01004171 wp = gui_mouse_window(FIND_POPUP);
4172
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004173#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004174 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004175 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004176 cmdarg_T cap;
4177 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004178
Bram Moolenaarae20f342019-11-05 21:09:23 +01004179 // Mouse hovers over popup window, scroll it if possible.
4180 mouse_row = wp->w_winrow;
4181 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004182 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004183 if (horizontal)
4184 {
4185 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4186 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4187 }
4188 else
4189 {
4190 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4191 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4192 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004193 clear_oparg(&oa);
4194 cap.oap = &oa;
4195 nv_mousescroll(&cap);
4196 update_screen(0);
4197 setcursor();
4198 out_flush();
4199 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004200 }
4201#endif
4202
Bram Moolenaarae20f342019-11-05 21:09:23 +01004203 if (wp == NULL || !p_scf)
4204 wp = curwin;
4205
LemonBoy365d8f72022-05-05 19:23:07 +01004206 // Translate the scroll event into an event that Vim can process so that
4207 // the user has a chance to map the scrollwheel buttons.
4208 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004209 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 else
LemonBoy365d8f72022-05-05 19:23:07 +01004211 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004212
4213 kbd_modifiers = get_active_modifiers();
4214
4215 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4216 modifiers |= MOUSE_SHIFT;
4217 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4218 modifiers |= MOUSE_CTRL;
4219 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4220 modifiers |= MOUSE_ALT;
4221
LemonBoya773d842022-05-10 20:54:46 +01004222 // The cursor position is relative to the upper-left corner of the screen.
4223 pt.x = GET_X_LPARAM(lParam);
4224 pt.y = GET_Y_LPARAM(lParam);
4225 ScreenToClient(s_textArea, &pt);
4226
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004227 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228}
4229
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004230#ifdef USE_SYSMENU_FONT
4231/*
4232 * Get Menu Font.
4233 * Return OK or FAIL.
4234 */
4235 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004236gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004237{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004238 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004239
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004240 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4241 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004242 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004243 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004244 &nm,
4245 0))
4246 return FAIL;
4247 *lf = nm.lfMenuFont;
4248 return OK;
4249}
4250#endif
4251
4252
4253#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4254/*
4255 * Set the GUI tabline font to the system menu font
4256 */
4257 static void
4258set_tabline_font(void)
4259{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004260 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004261 HFONT font;
4262 HWND hwnd;
4263 HDC hdc;
4264 HFONT hfntOld;
4265 TEXTMETRIC tm;
4266
4267 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4268 return;
4269
K.Takatac81e9bf2022-01-16 14:15:49 +00004270 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004271 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004272
4273 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4274
4275 /*
4276 * Compute the height of the font used for the tab text
4277 */
4278 hwnd = GetDesktopWindow();
4279 hdc = GetWindowDC(hwnd);
4280 hfntOld = SelectFont(hdc, font);
4281
4282 GetTextMetrics(hdc, &tm);
4283
4284 SelectFont(hdc, hfntOld);
4285 ReleaseDC(hwnd, hdc);
4286
4287 /*
4288 * The space used by the tab border and the space between the tab label
4289 * and the tab border is included as 7.
4290 */
4291 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4292}
K.Takatac81e9bf2022-01-16 14:15:49 +00004293#else
4294# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004295#endif
4296
Bram Moolenaar520470a2005-06-16 21:59:56 +00004297/*
4298 * Invoked when a setting was changed.
4299 */
4300 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004301_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004302{
LemonBoy365d8f72022-05-05 19:23:07 +01004303 switch (param)
4304 {
4305 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004306 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004307 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004308 case SPI_SETWHEELSCROLLCHARS:
4309 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004310 break;
4311 case SPI_SETNONCLIENTMETRICS:
4312 set_tabline_font();
4313 break;
4314 default:
4315 break;
4316 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004317 return 0;
4318}
4319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320#ifdef FEAT_NETBEANS_INTG
4321 static void
4322_OnWindowPosChanged(
4323 HWND hwnd,
4324 const LPWINDOWPOS lpwpos)
4325{
4326 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004327 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4330 || lpwpos->cx != cx || lpwpos->cy != cy))
4331 {
4332 x = lpwpos->x;
4333 y = lpwpos->y;
4334 cx = lpwpos->cx;
4335 cy = lpwpos->cy;
4336 netbeans_frame_moved(x, y);
4337 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004338 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004339 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340}
4341#endif
4342
K.Takata4f2417f2021-06-05 16:25:32 +02004343
4344static HWND hwndTip = NULL;
4345
4346 static void
4347show_sizing_tip(int cols, int rows)
4348{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004349 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004350 char buf[32];
4351
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004352 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004353 ti.hwnd = s_hwnd;
4354 ti.uId = (UINT_PTR)s_hwnd;
4355 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4356 ti.lpszText = buf;
4357 sprintf(buf, "%dx%d", cols, rows);
4358 if (hwndTip == NULL)
4359 {
4360 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4361 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4362 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4363 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4364 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4365 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4366 }
4367 else
4368 {
4369 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4370 }
4371 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4372}
4373
4374 static void
4375destroy_sizing_tip(void)
4376{
4377 if (hwndTip != NULL)
4378 {
4379 DestroyWindow(hwndTip);
4380 hwndTip = NULL;
4381 }
4382}
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 static int
4385_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 UINT fwSide,
4387 LPRECT lprc)
4388{
4389 int w, h;
4390 int valid_w, valid_h;
4391 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004392 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 w = lprc->right - lprc->left;
4395 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004396 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 w_offset = w - valid_w;
4398 h_offset = h - valid_h;
4399
4400 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4401 || fwSide == WMSZ_BOTTOMLEFT)
4402 lprc->left += w_offset;
4403 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4404 || fwSide == WMSZ_BOTTOMRIGHT)
4405 lprc->right -= w_offset;
4406
4407 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4408 || fwSide == WMSZ_TOPRIGHT)
4409 lprc->top += h_offset;
4410 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4411 || fwSide == WMSZ_BOTTOMRIGHT)
4412 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004413
4414 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return TRUE;
4416}
4417
K.Takata92000e22022-01-20 15:10:57 +00004418#ifdef FEAT_GUI_TABLINE
4419 static void
4420_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4421{
4422 if (gui_mch_showing_tabline())
4423 {
4424 POINT pt;
4425 RECT rect;
4426
4427 /*
4428 * If the cursor is on the tabline, display the tab menu
4429 */
4430 GetCursorPos(&pt);
4431 GetWindowRect(s_textArea, &rect);
4432 if (pt.y < rect.top)
4433 {
4434 show_tabline_popup_menu();
4435 return;
4436 }
4437 }
4438 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4439}
4440
4441 static void
4442_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4443{
4444 /*
4445 * If the user double clicked the tabline, create a new tab
4446 */
4447 if (gui_mch_showing_tabline())
4448 {
4449 POINT pt;
4450 RECT rect;
4451
4452 GetCursorPos(&pt);
4453 GetWindowRect(s_textArea, &rect);
4454 if (pt.y < rect.top)
4455 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4456 }
4457 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4458}
4459#endif
4460
4461 static UINT
4462_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4463{
4464 UINT result;
4465 int x, y;
4466
4467 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4468 if (result != HTCLIENT)
4469 return result;
4470
4471#ifdef FEAT_GUI_TABLINE
4472 if (gui_mch_showing_tabline())
4473 {
4474 RECT rct;
4475
4476 // If the cursor is on the GUI tabline, don't process this event
4477 GetWindowRect(s_textArea, &rct);
4478 if (yPos < rct.top)
4479 return result;
4480 }
4481#endif
4482 (void)gui_mch_get_winpos(&x, &y);
4483 xPos -= x;
4484
4485 if (xPos < 48) // <VN> TODO should use system metric?
4486 return HTBOTTOMLEFT;
4487 else
4488 return HTBOTTOMRIGHT;
4489}
4490
4491#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4492 static LRESULT
4493_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4494{
4495 switch (hdr->code)
4496 {
4497 case TTN_GETDISPINFOW:
4498 case TTN_GETDISPINFO:
4499 {
4500 char_u *str = NULL;
4501 static void *tt_text = NULL;
4502
4503 VIM_CLEAR(tt_text);
4504
4505# ifdef FEAT_GUI_TABLINE
4506 if (gui_mch_showing_tabline()
4507 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4508 {
4509 POINT pt;
4510 /*
4511 * Mouse is over the GUI tabline. Display the
4512 * tooltip for the tab under the cursor
4513 *
4514 * Get the cursor position within the tab control
4515 */
4516 GetCursorPos(&pt);
4517 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4518 {
4519 TCHITTESTINFO htinfo;
4520 int idx;
4521
4522 /*
4523 * Get the tab under the cursor
4524 */
4525 htinfo.pt.x = pt.x;
4526 htinfo.pt.y = pt.y;
4527 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4528 if (idx != -1)
4529 {
4530 tabpage_T *tp;
4531
4532 tp = find_tabpage(idx + 1);
4533 if (tp != NULL)
4534 {
4535 get_tabline_label(tp, TRUE);
4536 str = NameBuff;
4537 }
4538 }
4539 }
4540 }
4541# endif
4542# ifdef FEAT_TOOLBAR
4543# ifdef FEAT_GUI_TABLINE
4544 else
4545# endif
4546 {
4547 UINT idButton;
4548 vimmenu_T *pMenu;
4549
4550 idButton = (UINT) hdr->idFrom;
4551 pMenu = gui_mswin_find_menu(root_menu, idButton);
4552 if (pMenu)
4553 str = pMenu->strings[MENU_INDEX_TIP];
4554 }
4555# endif
4556 if (str == NULL)
4557 break;
4558
4559 // Set the maximum width, this also enables using \n for
4560 // line break.
4561 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4562
4563 if (hdr->code == TTN_GETDISPINFOW)
4564 {
4565 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4566
4567 tt_text = enc_to_utf16(str, NULL);
4568 lpdi->lpszText = tt_text;
4569 // can't show tooltip if failed
4570 }
4571 else
4572 {
4573 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4574
4575 if (STRLEN(str) < sizeof(lpdi->szText)
4576 || ((tt_text = vim_strsave(str)) == NULL))
4577 vim_strncpy((char_u *)lpdi->szText, str,
4578 sizeof(lpdi->szText) - 1);
4579 else
4580 lpdi->lpszText = tt_text;
4581 }
4582 }
4583 break;
4584
4585# ifdef FEAT_GUI_TABLINE
4586 case TCN_SELCHANGE:
4587 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4588 {
4589 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4590 return 0L;
4591 }
4592 break;
4593
4594 case NM_RCLICK:
4595 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4596 {
4597 show_tabline_popup_menu();
4598 return 0L;
4599 }
4600 break;
4601# endif
4602
4603 default:
4604 break;
4605 }
4606 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4607}
4608#endif
4609
4610#if defined(MENUHINTS) && defined(FEAT_MENU)
4611 static LRESULT
4612_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4613{
4614 if (((UINT) HIWORD(wParam)
4615 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4616 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004617 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004618 {
4619 UINT idButton;
4620 vimmenu_T *pMenu;
4621 static int did_menu_tip = FALSE;
4622
4623 if (did_menu_tip)
4624 {
4625 msg_clr_cmdline();
4626 setcursor();
4627 out_flush();
4628 did_menu_tip = FALSE;
4629 }
4630
4631 idButton = (UINT)LOWORD(wParam);
4632 pMenu = gui_mswin_find_menu(root_menu, idButton);
4633 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4634 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4635 {
4636 ++msg_hist_off;
4637 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4638 --msg_hist_off;
4639 setcursor();
4640 out_flush();
4641 did_menu_tip = TRUE;
4642 }
4643 return 0L;
4644 }
4645 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4646}
4647#endif
4648
K.Takatac81e9bf2022-01-16 14:15:49 +00004649 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004650_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004651{
4652 s_dpi = ydpi;
4653 s_in_dpichanged = TRUE;
4654 //TRACE("DPI: %d", ydpi);
4655
4656 update_scrollbar_size();
4657 update_toolbar_size();
4658 set_tabline_font();
4659
4660 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4661 gui_get_wide_font();
4662 gui_mswin_get_menu_height(FALSE);
4663#ifdef FEAT_MBYTE_IME
4664 im_set_position(gui.row, gui.col);
4665#endif
4666 InvalidateRect(hwnd, NULL, TRUE);
4667
4668 s_in_dpichanged = FALSE;
4669 return 0L;
4670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672
4673 static LRESULT CALLBACK
4674_WndProc(
4675 HWND hwnd,
4676 UINT uMsg,
4677 WPARAM wParam,
4678 LPARAM lParam)
4679{
LemonBoya773d842022-05-10 20:54:46 +01004680 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4681 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 HandleMouseHide(uMsg, lParam);
4684
4685 s_uMsg = uMsg;
4686 s_wParam = wParam;
4687 s_lParam = lParam;
4688
4689 switch (uMsg)
4690 {
4691 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4692 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004693 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004695 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4697 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4698 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4699 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4700#ifdef FEAT_MENU
4701 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4702#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004703 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4704 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4706 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004707 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4708 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4710 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4711 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4712#ifdef FEAT_NETBEANS_INTG
4713 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4714#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004715#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004716 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4717 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004718#endif
K.Takata92000e22022-01-20 15:10:57 +00004719 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004720
Bram Moolenaar734a8672019-12-02 22:49:38 +01004721 case WM_QUERYENDSESSION: // System wants to go down.
4722 gui_shell_closed(); // Will exit when no changed buffers.
4723 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004726 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004729 return 0L;
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 break;
4732
4733 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004734 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4735 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004736 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 return 0L;
4738
4739 case WM_SYSCHAR:
4740 /*
4741 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4742 * shortcut key, handle like a typed ALT key, otherwise call Windows
4743 * ALT key handling.
4744 */
4745#ifdef FEAT_MENU
4746 if ( !gui.menu_is_active
4747 || p_wak[0] == 'n'
4748 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4749 )
4750#endif
4751 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004752 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 return 0L;
4754 }
4755#ifdef FEAT_MENU
4756 else
K.Takata4ac893f2022-01-20 12:44:28 +00004757 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759
4760 case WM_SYSKEYUP:
4761#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004762 // This used to be done only when menu is active: ALT key is used for
4763 // that. But that caused problems when menu is disabled and using
4764 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4765 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004766 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004768 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769#endif
4770
K.Takata4f2417f2021-06-05 16:25:32 +02004771 case WM_EXITSIZEMOVE:
4772 destroy_sizing_tip();
4773 break;
4774
Bram Moolenaar734a8672019-12-02 22:49:38 +01004775 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
4778 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004779 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004780 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004781 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
Bram Moolenaar734a8672019-12-02 22:49:38 +01004783 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004784 case WM_SETTINGCHANGE:
4785 return _OnSettingChange((UINT)wParam);
4786
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004787#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004789 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#endif
K.Takata92000e22022-01-20 15:10:57 +00004791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#if defined(MENUHINTS) && defined(FEAT_MENU)
4793 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004794 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797#ifdef FEAT_MBYTE_IME
4798 case WM_IME_NOTIFY:
4799 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004800 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004801 return 1L;
4802
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 case WM_IME_COMPOSITION:
4804 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004805 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004806 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004808 case WM_DPICHANGED:
4809 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4810 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004814 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#endif
K.Takata92000e22022-01-20 15:10:57 +00004817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819
K.Takata4ac893f2022-01-20 12:44:28 +00004820 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821}
4822
4823/*
4824 * End of call-back routines
4825 */
4826
Bram Moolenaar734a8672019-12-02 22:49:38 +01004827// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828HWND vim_parent_hwnd = NULL;
4829
4830 static BOOL CALLBACK
4831FindWindowTitle(HWND hwnd, LPARAM lParam)
4832{
4833 char buf[2048];
4834 char *title = (char *)lParam;
4835
4836 if (GetWindowText(hwnd, buf, sizeof(buf)))
4837 {
4838 if (strstr(buf, title) != NULL)
4839 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004840 // Found it. Store the window ref. and quit searching if MDI
4841 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004843 if (vim_parent_hwnd != NULL)
4844 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004847 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848}
4849
4850/*
4851 * Invoked for '-P "title"' argument: search for parent application to open
4852 * our window in.
4853 */
4854 void
4855gui_mch_set_parent(char *title)
4856{
4857 EnumWindows(FindWindowTitle, (LPARAM)title);
4858 if (vim_parent_hwnd == NULL)
4859 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004860 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 mch_exit(2);
4862 }
4863}
4864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 static void
4867ole_error(char *arg)
4868{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004869 char buf[IOSIZE];
4870
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004871# ifdef VIMDLL
4872 gui.in_use = mch_is_gui_executable();
4873# endif
4874
Bram Moolenaar734a8672019-12-02 22:49:38 +01004875 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004876 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004877 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004878 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004882#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4883 static char *
4884gvim_error(void)
4885{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004886 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004887
4888 if (starting)
4889 {
4890 mch_errmsg(msg);
4891 mch_errmsg("\n");
4892 mch_exit(2);
4893 }
4894 return msg;
4895}
4896
4897 char *
4898gui_mch_do_spawn(char_u *arg)
4899{
4900 int len;
4901# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4902 char_u *session = NULL;
4903 LPWSTR tofree1 = NULL;
4904# endif
4905 WCHAR name[MAX_PATH];
4906 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4907 STARTUPINFOW si = {sizeof(si)};
4908 PROCESS_INFORMATION pi;
4909
4910 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4911 goto error;
4912 p = wcsrchr(name, L'\\');
4913 if (p == NULL)
4914 goto error;
4915 // Replace the executable name from vim(d).exe to gvim(d).exe.
4916# ifdef DEBUG
4917 wcscpy(p + 1, L"gvimd.exe");
4918# else
4919 wcscpy(p + 1, L"gvim.exe");
4920# endif
4921
4922# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4923 if (starting)
4924# endif
4925 {
4926 // Pass the command line to the new process.
4927 p = GetCommandLineW();
4928 // Skip 1st argument.
4929 while (*p && *p != L' ' && *p != L'\t')
4930 {
4931 if (*p == L'"')
4932 {
4933 while (*p && *p != L'"')
4934 ++p;
4935 if (*p)
4936 ++p;
4937 }
4938 else
4939 ++p;
4940 }
4941 cmd = p;
4942 }
4943# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4944 else
4945 {
4946 // Create a session file and pass it to the new process.
4947 LPWSTR wsession;
4948 char_u *savebg;
4949 int ret;
4950
4951 session = vim_tempname('s', FALSE);
4952 if (session == NULL)
4953 goto error;
4954 savebg = p_bg;
4955 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4956 ret = write_session_file(session);
4957 vim_free(p_bg);
4958 p_bg = savebg;
4959 if (!ret)
4960 goto error;
4961 wsession = enc_to_utf16(session, NULL);
4962 if (wsession == NULL)
4963 goto error;
4964 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004965 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004966 if (cmd == NULL)
4967 {
4968 vim_free(wsession);
4969 goto error;
4970 }
4971 tofree1 = cmd;
4972 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4973 wsession, wsession);
4974 vim_free(wsession);
4975 }
4976# endif
4977
4978 // Check additional arguments to the `:gui` command.
4979 if (arg != NULL)
4980 {
4981 warg = enc_to_utf16(arg, NULL);
4982 if (warg == NULL)
4983 goto error;
4984 tofree2 = warg;
4985 }
4986 else
4987 warg = L"";
4988
4989 // Set up the new command line.
4990 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004991 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004992 if (newcmd == NULL)
4993 goto error;
4994 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4995
4996 // Spawn a new GUI process.
4997 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4998 NULL, NULL, &si, &pi))
4999 goto error;
5000 CloseHandle(pi.hProcess);
5001 CloseHandle(pi.hThread);
5002 mch_exit(0);
5003
5004error:
5005# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5006 if (session)
5007 mch_remove(session);
5008 vim_free(session);
5009 vim_free(tofree1);
5010# endif
5011 vim_free(newcmd);
5012 vim_free(tofree2);
5013 return gvim_error();
5014}
5015#endif
5016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017/*
5018 * Parse the GUI related command-line arguments. Any arguments used are
5019 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005020 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 */
5022 void
5023gui_mch_prepare(int *argc, char **argv)
5024{
5025 int silent = FALSE;
5026 int idx;
5027
Bram Moolenaar734a8672019-12-02 22:49:38 +01005028 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5030 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005031 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5033 && (argv[2][0] == '-' || argv[2][0] == '/'))
5034 {
5035 silent = TRUE;
5036 idx = 2;
5037 }
5038 else
5039 idx = 1;
5040
Bram Moolenaar734a8672019-12-02 22:49:38 +01005041 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (STRICMP(argv[idx] + 1, "register") == 0)
5043 {
5044#ifdef FEAT_OLE
5045 RegisterMe(silent);
5046 mch_exit(0);
5047#else
5048 if (!silent)
5049 ole_error("register");
5050 mch_exit(2);
5051#endif
5052 }
5053
Bram Moolenaar734a8672019-12-02 22:49:38 +01005054 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5056 {
5057#ifdef FEAT_OLE
5058 UnregisterMe(!silent);
5059 mch_exit(0);
5060#else
5061 if (!silent)
5062 ole_error("unregister");
5063 mch_exit(2);
5064#endif
5065 }
5066
Bram Moolenaar734a8672019-12-02 22:49:38 +01005067 // Ignore an -embedding argument. It is only relevant if the
5068 // application wants to treat the case when it is started manually
5069 // differently from the case where it is started via automation (and
5070 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5072 {
5073#ifdef FEAT_OLE
5074 *argc = 1;
5075#else
5076 ole_error("embedding");
5077 mch_exit(2);
5078#endif
5079 }
5080 }
5081
5082#ifdef FEAT_OLE
5083 {
5084 int bDoRestart = FALSE;
5085
5086 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005087 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (bDoRestart)
5089 mch_exit(0);
5090 }
5091#endif
5092
5093#ifdef FEAT_NETBEANS_INTG
5094 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005095 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 int arg;
5097
5098 for (arg = 1; arg < *argc; arg++)
5099 if (strncmp("-nb", argv[arg], 3) == 0)
5100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 netbeansArg = argv[arg];
5102 mch_memmove(&argv[arg], &argv[arg + 1],
5103 (--*argc - arg) * sizeof(char *));
5104 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005105 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109}
5110
K.Takatac81e9bf2022-01-16 14:15:49 +00005111 static void
5112load_dpi_func(void)
5113{
5114 HMODULE hUser32;
5115
5116 hUser32 = GetModuleHandle("user32.dll");
5117 if (hUser32 == NULL)
5118 goto fail;
5119
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005120 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5121 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5122 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005123 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005124 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5125 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005126
5127 if (pSetThreadDpiAwarenessContext != NULL)
5128 {
5129 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5130 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5131 if (oldctx != NULL)
5132 {
5133 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5134 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5135#ifdef DEBUG
5136 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5137 {
5138 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5139 }
5140#endif
5141 return;
5142 }
5143 }
5144
5145fail:
5146 // Disable PerMonitorV2 APIs.
5147 pGetDpiForSystem = stubGetDpiForSystem;
5148 pGetDpiForWindow = NULL;
5149 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5150 pSetThreadDpiAwarenessContext = NULL;
5151 pGetAwarenessFromDpiAwarenessContext = NULL;
5152}
5153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154/*
5155 * Initialise the GUI. Create all the windows, set up all the call-backs
5156 * etc.
5157 */
5158 int
5159gui_mch_init(void)
5160{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005162 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
Bram Moolenaar734a8672019-12-02 22:49:38 +01005165 // Return here if the window was already opened (happens when
5166 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005168 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * Load the tearoff bitmap
5172 */
5173#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005174 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#endif
5176
K.Takatac81e9bf2022-01-16 14:15:49 +00005177 load_dpi_func();
5178
5179 s_dpi = pGetDpiForSystem();
5180 update_scrollbar_size();
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005183 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#endif
5185 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005186#ifdef FEAT_TOOLBAR
5187 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5191
Bram Moolenaar734a8672019-12-02 22:49:38 +01005192 // First try using the wide version, so that we can use any title.
5193 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005194 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005196 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 wndclassw.lpfnWndProc = _WndProc;
5198 wndclassw.cbClsExtra = 0;
5199 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005200 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5202 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5203 wndclassw.hbrBackground = s_brush;
5204 wndclassw.lpszMenuName = NULL;
5205 wndclassw.lpszClassName = szVimWndClassW;
5206
K.Takata4ac893f2022-01-20 12:44:28 +00005207 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (vim_parent_hwnd != NULL)
5212 {
5213#ifdef HAVE_TRY_EXCEPT
5214 __try
5215 {
5216#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005217 // Open inside the specified parent window.
5218 // TODO: last argument should point to a CLIENTCREATESTRUCT
5219 // structure.
5220 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005222 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005223 WS_OVERLAPPEDWINDOW | WS_CHILD
5224 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5226 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005227 100, // Any value will do
5228 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005230 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231#ifdef HAVE_TRY_EXCEPT
5232 }
5233 __except(EXCEPTION_EXECUTE_HANDLER)
5234 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237#endif
5238 if (s_hwnd == NULL)
5239 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005240 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 mch_exit(2);
5242 }
5243 }
5244 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005245 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005246 // If the provided windowid is not valid reset it to zero, so that it
5247 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005248 if (IsWindow((HWND)win_socket_id) <= 0)
5249 win_socket_id = 0;
5250
Bram Moolenaar734a8672019-12-02 22:49:38 +01005251 // Create a window. If win_socket_id is not zero without border and
5252 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005253 s_hwnd = CreateWindowW(
5254 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005255 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5256 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005257 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5258 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005259 100, // Any value will do
5260 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005261 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005262 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005263 if (s_hwnd != NULL && win_socket_id != 0)
5264 {
5265 SetParent(s_hwnd, (HWND)win_socket_id);
5266 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5267 }
5268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 if (s_hwnd == NULL)
5271 return FAIL;
5272
K.Takatac81e9bf2022-01-16 14:15:49 +00005273 if (pGetDpiForWindow != NULL)
5274 {
5275 s_dpi = pGetDpiForWindow(s_hwnd);
5276 update_scrollbar_size();
5277 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5278 }
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5281 dyn_imm_load();
5282#endif
5283
Bram Moolenaar734a8672019-12-02 22:49:38 +01005284 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005285 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005286 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005287 wndclassw.style = CS_OWNDC;
5288 wndclassw.lpfnWndProc = _TextAreaWndProc;
5289 wndclassw.cbClsExtra = 0;
5290 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005291 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 wndclassw.hIcon = NULL;
5293 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5294 wndclassw.hbrBackground = NULL;
5295 wndclassw.lpszMenuName = NULL;
5296 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005297
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005298 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 return FAIL;
5300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005302 s_textArea = CreateWindowExW(
5303 0,
5304 szTextAreaClassW, L"Vim text area",
5305 WS_CHILD | WS_VISIBLE, 0, 0,
5306 100, // Any value will do for now
5307 100, // Any value will do for now
5308 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005309 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (s_textArea == NULL)
5312 return FAIL;
5313
Bram Moolenaar20321902016-02-17 12:30:17 +01005314#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005315 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005316 {
5317 HANDLE hIcon = NULL;
5318
5319 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005320 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005321 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005322#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324#ifdef FEAT_MENU
5325 s_menuBar = CreateMenu();
5326#endif
5327 s_hdc = GetDC(s_textArea);
5328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
Bram Moolenaar734a8672019-12-02 22:49:38 +01005331 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005332 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
Bram Moolenaar734a8672019-12-02 22:49:38 +01005334 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 gui_mch_def_colors();
5336
Bram Moolenaar734a8672019-12-02 22:49:38 +01005337 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5338 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 set_normal_colors();
5340
5341 /*
5342 * Check that none of the colors are the same as the background color.
5343 * Then store the current values as the defaults.
5344 */
5345 gui_check_colors();
5346 gui.def_norm_pixel = gui.norm_pixel;
5347 gui.def_back_pixel = gui.back_pixel;
5348
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 // Get the colors for the highlight groups (gui_check_colors() might have
5350 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 highlight_gui_started();
5352
5353 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005354 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005356 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 /*
5359 * Set up for Intellimouse processing
5360 */
5361 init_mouse_wheel();
5362
5363 /*
5364 * compute a couple of metrics used for the dialogs
5365 */
5366 get_dialog_font_metrics();
5367#ifdef FEAT_TOOLBAR
5368 /*
5369 * Create the toolbar
5370 */
5371 initialise_toolbar();
5372#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005373#ifdef FEAT_GUI_TABLINE
5374 /*
5375 * Create the tabline
5376 */
5377 initialise_tabline();
5378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379#ifdef MSWIN_FIND_REPLACE
5380 /*
5381 * Initialise the dialog box stuff
5382 */
5383 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5384
Bram Moolenaar734a8672019-12-02 22:49:38 +01005385 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005387 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005389 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5391 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5392 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005395#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005396 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005397 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005398#endif
5399
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005400#ifdef FEAT_RENDER_OPTIONS
5401 if (p_rop)
5402 (void)gui_mch_set_rendering_options(p_rop);
5403#endif
5404
Bram Moolenaar748bf032005-02-02 23:04:36 +00005405theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005406 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005407 display_errors();
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 return OK;
5410}
5411
5412/*
5413 * Get the size of the screen, taking position on multiple monitors into
5414 * account (if supported).
5415 */
5416 static void
5417get_work_area(RECT *spi_rect)
5418{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005419 HMONITOR mon;
5420 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
Bram Moolenaar734a8672019-12-02 22:49:38 +01005422 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005423 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005424 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005426 moninfo.cbSize = sizeof(MONITORINFO);
5427 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005429 *spi_rect = moninfo.rcWork;
5430 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005433 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5435}
5436
5437/*
5438 * Set the size of the window to the given width and height in pixels.
5439 */
5440 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005441gui_mch_set_shellsize(
5442 int width,
5443 int height,
5444 int min_width UNUSED,
5445 int min_height UNUSED,
5446 int base_width UNUSED,
5447 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005448 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005451 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
Bram Moolenaar734a8672019-12-02 22:49:38 +01005454 // Try to keep window completely on screen.
5455 // Get position of the screen work area. This is the part that is not
5456 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 get_work_area(&workarea_rect);
5458
Bram Moolenaar734a8672019-12-02 22:49:38 +01005459 // Resizing a maximized window looks very strange, unzoom it first.
5460 // But don't do it when still starting up, it may have been requested in
5461 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005462 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005464
5465 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar734a8672019-12-02 22:49:38 +01005467 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005468 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5469 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5470 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5471 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5472 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5473 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaar734a8672019-12-02 22:49:38 +01005475 // The following should take care of keeping Vim on the same monitor, no
5476 // matter if the secondary monitor is left or right of the primary
5477 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005478 window_rect.right = window_rect.left + win_width;
5479 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005480
Bram Moolenaar734a8672019-12-02 22:49:38 +01005481 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005482 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5483 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005485 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5486 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005488 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5489 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005491 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5492 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005494 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5495 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 SetActiveWindow(s_hwnd);
5498 SetFocus(s_hwnd);
5499
Bram Moolenaar734a8672019-12-02 22:49:38 +01005500 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502}
5503
5504
5505 void
5506gui_mch_set_scrollbar_thumb(
5507 scrollbar_T *sb,
5508 long val,
5509 long size,
5510 long max)
5511{
5512 SCROLLINFO info;
5513
5514 sb->scroll_shift = 0;
5515 while (max > 32767)
5516 {
5517 max = (max + 1) >> 1;
5518 val >>= 1;
5519 size >>= 1;
5520 ++sb->scroll_shift;
5521 }
5522
5523 if (sb->scroll_shift > 0)
5524 ++size;
5525
5526 info.cbSize = sizeof(info);
5527 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5528 info.nPos = val;
5529 info.nMin = 0;
5530 info.nMax = max;
5531 info.nPage = size;
5532 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5533}
5534
5535
5536/*
5537 * Set the current text font.
5538 */
5539 void
5540gui_mch_set_font(GuiFont font)
5541{
5542 gui.currFont = font;
5543}
5544
5545
5546/*
5547 * Set the current text foreground color.
5548 */
5549 void
5550gui_mch_set_fg_color(guicolor_T color)
5551{
5552 gui.currFgColor = color;
5553}
5554
5555/*
5556 * Set the current text background color.
5557 */
5558 void
5559gui_mch_set_bg_color(guicolor_T color)
5560{
5561 gui.currBgColor = color;
5562}
5563
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005564/*
5565 * Set the current text special color.
5566 */
5567 void
5568gui_mch_set_sp_color(guicolor_T color)
5569{
5570 gui.currSpColor = color;
5571}
5572
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005573#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574/*
5575 * Multi-byte handling, originally by Sung-Hoon Baek.
5576 * First static functions (no prototypes generated).
5577 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005578# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005579# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005580# endif
5581# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 * handle WM_IME_NOTIFY message
5585 */
5586 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005587_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 LRESULT lResult = 0;
5590 HIMC hImc;
5591
5592 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5593 return lResult;
5594 switch (dwCommand)
5595 {
5596 case IMN_SETOPENSTATUS:
5597 if (pImmGetOpenStatus(hImc))
5598 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005599 LOGFONTW lf = norm_logfont;
5600 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5601 // Work around when PerMonitorV2 is not enabled in the process level.
5602 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5603 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 im_set_position(gui.row, gui.col);
5605
Bram Moolenaar734a8672019-12-02 22:49:38 +01005606 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005607 State &= ~MODE_LANGMAP;
5608 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005610# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005611 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5613 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005614 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int old_row = gui.row;
5616 int old_col = gui.col;
5617
5618 // This must be called here before
5619 // status_redraw_curbuf(), otherwise the mode
5620 // message may appear in the wrong position.
5621 showmode();
5622 status_redraw_curbuf();
5623 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005624 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 gui.row = old_row;
5626 gui.col = old_col;
5627 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630 }
5631 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005632 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 lResult = 0;
5634 break;
5635 }
5636 pImmReleaseContext(hWnd, hImc);
5637 return lResult;
5638}
5639
5640 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005641_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *ret;
5644 int len;
5645
Bram Moolenaar734a8672019-12-02 22:49:38 +01005646 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 return 0;
5648
5649 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5650 if (ret != NULL)
5651 {
5652 add_to_input_buf_csi(ret, len);
5653 vim_free(ret);
5654 return 1;
5655 }
5656 return 0;
5657}
5658
5659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 * void GetResultStr()
5661 *
5662 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5663 * get complete composition string
5664 */
5665 static char_u *
5666GetResultStr(HWND hwnd, int GCS, int *lenp)
5667{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005668 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005669 LONG ret;
5670 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 char_u *convbuf = NULL;
5672
5673 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5674 return NULL;
5675
K.Takatab0b2b732022-01-19 12:59:21 +00005676 // Get the length of the composition string.
5677 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5678 if (ret <= 0)
5679 return NULL;
5680
5681 // Allocate the requested buffer plus space for the NUL character.
5682 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (buf == NULL)
5684 return NULL;
5685
K.Takatab0b2b732022-01-19 12:59:21 +00005686 // Reads in the composition string.
5687 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5688 *lenp = ret / sizeof(WCHAR);
5689
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005690 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 pImmReleaseContext(hwnd, hIMC);
5692 vim_free(buf);
5693 return convbuf;
5694}
5695#endif
5696
Bram Moolenaar734a8672019-12-02 22:49:38 +01005697// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005698#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700/*
5701 * set font to IM.
5702 */
5703 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005704im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705{
5706 HIMC hImc;
5707
5708 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5709 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005710 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 pImmReleaseContext(s_hwnd, hImc);
5712 }
5713}
5714
5715/*
5716 * Notify cursor position to IM.
5717 */
5718 void
5719im_set_position(int row, int col)
5720{
5721 HIMC hImc;
5722
5723 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5724 {
5725 COMPOSITIONFORM cfs;
5726
5727 cfs.dwStyle = CFS_POINT;
5728 cfs.ptCurrentPos.x = FILL_X(col);
5729 cfs.ptCurrentPos.y = FILL_Y(row);
5730 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005731 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5732 {
5733 // Work around when PerMonitorV2 is not enabled in the process level.
5734 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5735 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 pImmSetCompositionWindow(hImc, &cfs);
5738
5739 pImmReleaseContext(s_hwnd, hImc);
5740 }
5741}
5742
5743/*
5744 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5745 */
5746 void
5747im_set_active(int active)
5748{
5749 HIMC hImc;
5750 static HIMC hImcOld = (HIMC)0;
5751
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005752# ifdef VIMDLL
5753 if (!gui.in_use && !gui.starting)
5754 {
5755 mbyte_im_set_active(active);
5756 return;
5757 }
5758# endif
5759
Bram Moolenaar734a8672019-12-02 22:49:38 +01005760 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 if (p_imdisable)
5763 {
5764 if (hImcOld == (HIMC)0)
5765 {
5766 hImcOld = pImmGetContext(s_hwnd);
5767 if (hImcOld)
5768 pImmAssociateContext(s_hwnd, (HIMC)0);
5769 }
5770 active = FALSE;
5771 }
5772 else if (hImcOld != (HIMC)0)
5773 {
5774 pImmAssociateContext(s_hwnd, hImcOld);
5775 hImcOld = (HIMC)0;
5776 }
5777
5778 hImc = pImmGetContext(s_hwnd);
5779 if (hImc)
5780 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005781 /*
5782 * for Korean ime
5783 */
5784 HKL hKL = GetKeyboardLayout(0);
5785
5786 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5787 {
5788 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5789 static BOOL bSaved = FALSE;
5790
5791 if (active)
5792 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005793 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005794 if (bSaved)
5795 pImmSetConversionStatus(hImc, dwConversionSaved,
5796 dwSentenceSaved);
5797 bSaved = FALSE;
5798 }
5799 else
5800 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005801 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005802 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5803 &dwSentenceSaved))
5804 {
5805 bSaved = TRUE;
5806 pImmSetConversionStatus(hImc,
5807 dwConversionSaved & ~(IME_CMODE_NATIVE
5808 | IME_CMODE_FULLSHAPE),
5809 dwSentenceSaved);
5810 }
5811 }
5812 }
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 pImmSetOpenStatus(hImc, active);
5815 pImmReleaseContext(s_hwnd, hImc);
5816 }
5817 }
5818}
5819
5820/*
5821 * Get IM status. When IM is on, return not 0. Else return 0.
5822 */
5823 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005824im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 int status = 0;
5827 HIMC hImc;
5828
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005829# ifdef VIMDLL
5830 if (!gui.in_use && !gui.starting)
5831 return mbyte_im_get_status();
5832# endif
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5835 {
5836 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5837 pImmReleaseContext(s_hwnd, hImc);
5838 }
5839 return status;
5840}
5841
Bram Moolenaar734a8672019-12-02 22:49:38 +01005842#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005845/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005846 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005847 */
5848 static void
5849latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5850{
5851 int c;
5852
Bram Moolenaarca003e12006-03-17 23:19:38 +00005853 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005854 {
5855 c = *text++;
5856 switch (c)
5857 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005858 case 0xa4: c = 0x20ac; break; // euro
5859 case 0xa6: c = 0x0160; break; // S hat
5860 case 0xa8: c = 0x0161; break; // S -hat
5861 case 0xb4: c = 0x017d; break; // Z hat
5862 case 0xb8: c = 0x017e; break; // Z -hat
5863 case 0xbc: c = 0x0152; break; // OE
5864 case 0xbd: c = 0x0153; break; // oe
5865 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005866 }
5867 *unicodebuf++ = c;
5868 }
5869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871#ifdef FEAT_RIGHTLEFT
5872/*
5873 * What is this for? In the case where you are using Win98 or Win2K or later,
5874 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5875 * reverses the string sent to the TextOut... family. This sucks, because we
5876 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5877 * way to tell Windblows not to do this!
5878 *
5879 * The short of it is that this 'RevOut' only gets called if you are running
5880 * one of the new, "improved" MS OSes, and only if you are running in
5881 * 'rightleft' mode. It makes display take *slightly* longer, but not
5882 * noticeably so.
5883 */
5884 static void
K.Takata135e1522022-01-29 15:27:58 +00005885RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 int col,
5887 int row,
5888 UINT foptions,
5889 CONST RECT *pcliprect,
5890 LPCTSTR text,
5891 UINT len,
5892 CONST INT *padding)
5893{
5894 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005896 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005897 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005898 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899}
5900#endif
5901
Bram Moolenaar92467d32017-12-05 13:22:16 +01005902 static void
5903draw_line(
5904 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005905 int y1,
5906 int x2,
5907 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005908 COLORREF color)
5909{
5910#if defined(FEAT_DIRECTX)
5911 if (IS_ENABLE_DIRECTX())
5912 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5913 else
5914#endif
5915 {
5916 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5917 HPEN old_pen = SelectObject(s_hdc, hpen);
5918 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005919 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005920 LineTo(s_hdc, x2, y2);
5921 DeleteObject(SelectObject(s_hdc, old_pen));
5922 }
5923}
5924
5925 static void
5926set_pixel(
5927 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005928 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005929 COLORREF color)
5930{
5931#if defined(FEAT_DIRECTX)
5932 if (IS_ENABLE_DIRECTX())
5933 DWriteContext_SetPixel(s_dwc, x, y, color);
5934 else
5935#endif
5936 SetPixel(s_hdc, x, y, color);
5937}
5938
5939 static void
5940fill_rect(
5941 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005942 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005943 COLORREF color)
5944{
5945#if defined(FEAT_DIRECTX)
5946 if (IS_ENABLE_DIRECTX())
5947 DWriteContext_FillRect(s_dwc, rcp, color);
5948 else
5949#endif
5950 {
5951 HBRUSH hbr2;
5952
5953 if (hbr == NULL)
5954 hbr2 = CreateSolidBrush(color);
5955 else
5956 hbr2 = hbr;
5957 FillRect(s_hdc, rcp, hbr2);
5958 if (hbr == NULL)
5959 DeleteBrush(hbr2);
5960 }
5961}
5962
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 void
5964gui_mch_draw_string(
5965 int row,
5966 int col,
5967 char_u *text,
5968 int len,
5969 int flags)
5970{
5971 static int *padding = NULL;
5972 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 const RECT *pcliprect = NULL;
5974 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 static WCHAR *unicodebuf = NULL;
5976 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005977 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 int y;
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 /*
5982 * Italic and bold text seems to have an extra row of pixels at the bottom
5983 * (below where the bottom of the character should be). If we draw the
5984 * characters with a solid background, the top row of pixels in the
5985 * character below will be overwritten. We can fix this by filling in the
5986 * background ourselves, to the correct character proportions, and then
5987 * writing the character in transparent mode. Still have a problem when
5988 * the character is "_", which gets written on to the character below.
5989 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5990 * pixel in their slots, which fixes the problem with the bottom row of
5991 * pixels. We still need this code because otherwise the top row of pixels
5992 * becomes a problem. - webb.
5993 */
5994 static HBRUSH hbr_cache[2] = {NULL, NULL};
5995 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5996 static int brush_lru = 0;
5997 HBRUSH hbr;
5998 RECT rc;
5999
6000 if (!(flags & DRAW_TRANSP))
6001 {
6002 /*
6003 * Clear background first.
6004 * Note: FillRect() excludes right and bottom of rectangle.
6005 */
6006 rc.left = FILL_X(col);
6007 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (has_mbyte)
6009 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006010 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006011 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 }
6013 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 rc.right = FILL_X(col + len);
6015 rc.bottom = FILL_Y(row + 1);
6016
Bram Moolenaar734a8672019-12-02 22:49:38 +01006017 // Cache the created brush, that saves a lot of time. We need two:
6018 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (gui.currBgColor == brush_color[0])
6020 {
6021 hbr = hbr_cache[0];
6022 brush_lru = 1;
6023 }
6024 else if (gui.currBgColor == brush_color[1])
6025 {
6026 hbr = hbr_cache[1];
6027 brush_lru = 0;
6028 }
6029 else
6030 {
6031 if (hbr_cache[brush_lru] != NULL)
6032 DeleteBrush(hbr_cache[brush_lru]);
6033 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6034 brush_color[brush_lru] = gui.currBgColor;
6035 hbr = hbr_cache[brush_lru];
6036 brush_lru = !brush_lru;
6037 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006038
Bram Moolenaar92467d32017-12-05 13:22:16 +01006039 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040
6041 SetBkMode(s_hdc, TRANSPARENT);
6042
6043 /*
6044 * When drawing block cursor, prevent inverted character spilling
6045 * over character cell (can happen with bold/italic)
6046 */
6047 if (flags & DRAW_CURSOR)
6048 {
6049 pcliprect = &rc;
6050 foptions = ETO_CLIPPED;
6051 }
6052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 SetTextColor(s_hdc, gui.currFgColor);
6054 SelectFont(s_hdc, gui.currFont);
6055
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006056#ifdef FEAT_DIRECTX
6057 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006058 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006059#endif
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6062 {
K.Takata135e1522022-01-29 15:27:58 +00006063 int i;
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 vim_free(padding);
6066 pad_size = Columns;
6067
Bram Moolenaar734a8672019-12-02 22:49:38 +01006068 // Don't give an out-of-memory message here, it would call us
6069 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006070 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (padding != NULL)
6072 for (i = 0; i < pad_size; i++)
6073 padding[i] = gui.char_width;
6074 }
6075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 /*
6077 * We have to provide the padding argument because italic and bold versions
6078 * of fixed-width fonts are often one pixel or so wider than their normal
6079 * versions.
6080 * No check for DRAW_BOLD, Windows will have done it already.
6081 */
6082
Bram Moolenaar734a8672019-12-02 22:49:38 +01006083 // Check if there are any UTF-8 characters. If not, use normal text
6084 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 if (enc_utf8)
6086 for (n = 0; n < len; ++n)
6087 if (text[n] >= 0x80)
6088 break;
6089
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006090#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006091 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6092 // required that unicode drawing routine, currently. So this forces it
6093 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006094 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006095 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006096#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006097
Bram Moolenaar734a8672019-12-02 22:49:38 +01006098 // Check if the Unicode buffer exists and is big enough. Create it
6099 // with the same length as the multi-byte string, the number of wide
6100 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006101 if ((enc_utf8
6102 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6103 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 && (unicodebuf == NULL || len > unibuflen))
6105 {
6106 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006107 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006110 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 unibuflen = len;
6113 }
6114
6115 if (enc_utf8 && n < len && unicodebuf != NULL)
6116 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006117 // Output UTF-8 characters. Composing characters should be
6118 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006119 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006120 int wlen; // string length in words
6121 int clen; // string length in characters
6122 int cells; // cell width of string up to composing char
6123 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006124 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006126 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006127 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006129 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006131 c = utf_ptr2char(text + i);
6132 if (c >= 0x10000)
6133 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006134 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006135 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6136 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006137 }
6138 else
6139 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006140 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006141 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006142
6143 if (utf_iscomposing(c))
6144 cw = 0;
6145 else
6146 {
6147 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006148 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006149 cw = 1;
6150 }
6151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (unicodepdy != NULL)
6153 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006154 // Use unicodepdy to make characters fit as we expect, even
6155 // when the font uses different widths (e.g., bold character
6156 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006157 if (c >= 0x10000)
6158 {
6159 unicodepdy[wlen - 2] = cw * gui.char_width;
6160 unicodepdy[wlen - 1] = 0;
6161 }
6162 else
6163 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
6165 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006166 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006167 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006169#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006170 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006171 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006172 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006173 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006174 TEXT_X(col), TEXT_Y(row),
6175 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006176 gui.char_width, gui.currFgColor,
6177 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006178 }
6179 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006180#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006181 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6182 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006185 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006187 // If we want to display codepage data, and the current CP is not the
6188 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (unicodebuf != NULL)
6190 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006191 if (enc_latin9)
6192 latin9_to_ucs(text, len, unicodebuf);
6193 else
6194 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 MB_PRECOMPOSED,
6196 (char *)text, len,
6197 (LPWSTR)unicodebuf, unibuflen);
6198 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006199 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006200 // Use unicodepdy to make characters fit as we expect, even
6201 // when the font uses different widths (e.g., bold character
6202 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006203 if (unicodepdy != NULL)
6204 {
6205 int i;
6206 int cw;
6207
6208 for (i = 0; i < len; ++i)
6209 {
6210 cw = utf_char2cells(unicodebuf[i]);
6211 if (cw > 2)
6212 cw = 1;
6213 unicodepdy[i] = cw * gui.char_width;
6214 }
6215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006217 foptions, pcliprect, unicodebuf, len, unicodepdy);
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220 }
6221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 {
6223#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // Windows will mess up RL text, so we have to draw it character by
6225 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006226 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6228 foptions, pcliprect, (char *)text, len, padding);
6229 else
6230#endif
6231 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6232 foptions, pcliprect, (char *)text, len, padding);
6233 }
6234
Bram Moolenaar734a8672019-12-02 22:49:38 +01006235 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 if (flags & DRAW_UNDERL)
6237 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006238 // When p_linespace is 0, overwrite the bottom row of pixels.
6239 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (p_linespace > 1)
6242 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006243 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006245
Bram Moolenaar734a8672019-12-02 22:49:38 +01006246 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006247 if (flags & DRAW_STRIKE)
6248 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006249 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006250 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006251 }
6252
Bram Moolenaar734a8672019-12-02 22:49:38 +01006253 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006254 if (flags & DRAW_UNDERC)
6255 {
6256 int x;
6257 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006258 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006259
6260 y = FILL_Y(row + 1) - 1;
6261 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6262 {
6263 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006264 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006265 }
6266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267}
6268
6269
6270/*
6271 * Output routines.
6272 */
6273
Bram Moolenaar734a8672019-12-02 22:49:38 +01006274/*
6275 * Flush any output to the screen
6276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 void
6278gui_mch_flush(void)
6279{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006280#if defined(FEAT_DIRECTX)
6281 if (IS_ENABLE_DIRECTX())
6282 DWriteContext_Flush(s_dwc);
6283#endif
6284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 GdiFlush();
6286}
6287
6288 static void
6289clear_rect(RECT *rcp)
6290{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006291 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292}
6293
6294
Bram Moolenaarc716c302006-01-21 22:12:51 +00006295 void
6296gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6297{
6298 RECT workarea_rect;
6299
6300 get_work_area(&workarea_rect);
6301
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006302 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006303 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6304 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006305
Bram Moolenaar734a8672019-12-02 22:49:38 +01006306 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6307 // the menubar for MSwin, we subtract it from the screen height, so that
6308 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006309 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006310 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6311 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6312 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6313 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006314}
6315
6316
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317#if defined(FEAT_MENU) || defined(PROTO)
6318/*
6319 * Add a sub menu to the menu bar.
6320 */
6321 void
6322gui_mch_add_menu(
6323 vimmenu_T *menu,
6324 int pos)
6325{
6326 vimmenu_T *parent = menu->parent;
6327
6328 menu->submenu_id = CreatePopupMenu();
6329 menu->id = s_menu_id++;
6330
6331 if (menu_is_menubar(menu->name))
6332 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006333 WCHAR *wn;
6334 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006336 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006337 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006338 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006340 infow.cbSize = sizeof(infow);
6341 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6342 | MIIM_SUBMENU;
6343 infow.dwItemData = (long_u)menu;
6344 infow.wID = menu->id;
6345 infow.fType = MFT_STRING;
6346 infow.dwTypeData = wn;
6347 infow.cch = (UINT)wcslen(wn);
6348 infow.hSubMenu = menu->submenu_id;
6349 InsertMenuItemW((parent == NULL)
6350 ? s_menuBar : parent->submenu_id,
6351 (UINT)pos, TRUE, &infow);
6352 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354
Bram Moolenaar734a8672019-12-02 22:49:38 +01006355 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (parent == NULL)
6357 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006358# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 else if (IsWindow(parent->tearoff_handle))
6360 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362}
6363
6364 void
6365gui_mch_show_popupmenu(vimmenu_T *menu)
6366{
6367 POINT mp;
6368
K.Takata45f9cfb2022-01-21 11:11:00 +00006369 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6371}
6372
6373 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006374gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376 vimmenu_T *menu = gui_find_menu(path_name);
6377
6378 if (menu != NULL)
6379 {
6380 POINT p;
6381
Bram Moolenaar734a8672019-12-02 22:49:38 +01006382 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006384 if (mouse_pos)
6385 {
6386 int mx, my;
6387
6388 gui_mch_getmouse(&mx, &my);
6389 p.x += mx;
6390 p.y += my;
6391 }
6392 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006394 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6396 }
6397 msg_scroll = FALSE;
6398 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6399 }
6400}
6401
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006402# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403/*
6404 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6405 * create it as a pseudo-"tearoff menu".
6406 */
6407 void
6408gui_make_tearoff(char_u *path_name)
6409{
6410 vimmenu_T *menu = gui_find_menu(path_name);
6411
Bram Moolenaar734a8672019-12-02 22:49:38 +01006412 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (menu != NULL)
6414 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6415}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418/*
6419 * Add a menu item to a menu
6420 */
6421 void
6422gui_mch_add_menu_item(
6423 vimmenu_T *menu,
6424 int idx)
6425{
6426 vimmenu_T *parent = menu->parent;
6427
6428 menu->id = s_menu_id++;
6429 menu->submenu_id = NULL;
6430
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006431# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6433 {
6434 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6435 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6436 }
6437 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006438# endif
6439# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 if (menu_is_toolbar(parent->name))
6441 {
6442 TBBUTTON newtb;
6443
Bram Moolenaara80faa82020-04-12 19:37:17 +02006444 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (menu_is_separator(menu->name))
6446 {
6447 newtb.iBitmap = 0;
6448 newtb.fsStyle = TBSTYLE_SEP;
6449 }
6450 else
6451 {
6452 newtb.iBitmap = get_toolbar_bitmap(menu);
6453 newtb.fsStyle = TBSTYLE_BUTTON;
6454 }
6455 newtb.idCommand = menu->id;
6456 newtb.fsState = TBSTATE_ENABLED;
6457 newtb.iString = 0;
6458 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6459 (LPARAM)&newtb);
6460 menu->submenu_id = (HMENU)-1;
6461 }
6462 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006465 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006467 wn = enc_to_utf16(menu->name, NULL);
6468 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006470 InsertMenuW(parent->submenu_id, (UINT)idx,
6471 (menu_is_separator(menu->name)
6472 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6473 (UINT)menu->id, wn);
6474 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006476# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (IsWindow(parent->tearoff_handle))
6478 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481}
6482
6483/*
6484 * Destroy the machine specific menu widget.
6485 */
6486 void
6487gui_mch_destroy_menu(vimmenu_T *menu)
6488{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006489# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 /*
6491 * is this a toolbar button?
6492 */
6493 if (menu->submenu_id == (HMENU)-1)
6494 {
6495 int iButton;
6496
6497 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6498 (WPARAM)menu->id, 0);
6499 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6500 }
6501 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
6504 if (menu->parent != NULL
6505 && menu_is_popup(menu->parent->dname)
6506 && menu->parent->submenu_id != NULL)
6507 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6508 else
6509 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6510 if (menu->submenu_id != NULL)
6511 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006512# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 if (IsWindow(menu->tearoff_handle))
6514 DestroyWindow(menu->tearoff_handle);
6515 if (menu->parent != NULL
6516 && menu->parent->children != NULL
6517 && IsWindow(menu->parent->tearoff_handle))
6518 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006519 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 menu->modes = 0;
6521 rebuild_tearoff(menu->parent);
6522 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525}
6526
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006527# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 static void
6529rebuild_tearoff(vimmenu_T *menu)
6530{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006531 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 char_u tbuf[128];
6533 RECT trect;
6534 RECT rct;
6535 RECT roct;
6536 int x, y;
6537
6538 HWND thwnd = menu->tearoff_handle;
6539
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006540 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (GetWindowRect(thwnd, &trect)
6542 && GetWindowRect(s_hwnd, &rct)
6543 && GetClientRect(s_hwnd, &roct))
6544 {
6545 x = trect.left - rct.left;
6546 y = (trect.top - rct.bottom + roct.bottom);
6547 }
6548 else
6549 {
6550 x = y = 0xffffL;
6551 }
6552 DestroyWindow(thwnd);
6553 if (menu->children != NULL)
6554 {
6555 gui_mch_tearoff(tbuf, menu, x, y);
6556 if (IsWindow(menu->tearoff_handle))
6557 (void) SetWindowPos(menu->tearoff_handle,
6558 NULL,
6559 (int)trect.left,
6560 (int)trect.top,
6561 0, 0,
6562 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6563 }
6564}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006565# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567/*
6568 * Make a menu either grey or not grey.
6569 */
6570 void
6571gui_mch_menu_grey(
6572 vimmenu_T *menu,
6573 int grey)
6574{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006575# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 /*
6577 * is this a toolbar button?
6578 */
6579 if (menu->submenu_id == (HMENU)-1)
6580 {
6581 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006582 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 }
6584 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006585# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006586 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6587 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006589# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6591 {
6592 WORD menuID;
6593 HWND menuHandle;
6594
6595 /*
6596 * A tearoff button has changed state.
6597 */
6598 if (menu->children == NULL)
6599 menuID = (WORD)(menu->id);
6600 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006601 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6603 if (menuHandle)
6604 EnableWindow(menuHandle, !grey);
6605
6606 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
Bram Moolenaar734a8672019-12-02 22:49:38 +01006610#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
6612
Bram Moolenaar734a8672019-12-02 22:49:38 +01006613// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006616#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6619/*
6620 * stuff for dialogs
6621 */
6622
6623/*
6624 * The callback routine used by all the dialogs. Very simple. First,
6625 * acknowledges the INITDIALOG message so that Windows knows to do standard
6626 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6627 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6628 * number.
6629 */
6630 static LRESULT CALLBACK
6631dialog_callback(
6632 HWND hwnd,
6633 UINT message,
6634 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006635 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636{
6637 if (message == WM_INITDIALOG)
6638 {
6639 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006640 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 (void)SetFocus(hwnd);
6642 if (dialog_default_button > IDCANCEL)
6643 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006644 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006645 // We don't have a default, set focus on another element of the
6646 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006647 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 return FALSE;
6649 }
6650
6651 if (message == WM_COMMAND)
6652 {
6653 int button = LOWORD(wParam);
6654
Bram Moolenaar734a8672019-12-02 22:49:38 +01006655 // Don't end the dialog if something was selected that was
6656 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (button >= DLG_NONBUTTON_CONTROL)
6658 return TRUE;
6659
Bram Moolenaar734a8672019-12-02 22:49:38 +01006660 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006662 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006663 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006664 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006665
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006666 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6667 p = utf16_to_enc(wp, NULL);
6668 vim_strncpy(s_textfield, p, IOSIZE);
6669 vim_free(p);
6670 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 /*
6674 * Need to check for IDOK because if the user just hits Return to
6675 * accept the default value, some reason this is what we get.
6676 */
6677 if (button == IDOK)
6678 {
6679 if (dialog_default_button > IDCANCEL)
6680 EndDialog(hwnd, dialog_default_button);
6681 }
6682 else
6683 EndDialog(hwnd, button - IDCANCEL);
6684 return TRUE;
6685 }
6686
6687 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6688 {
6689 EndDialog(hwnd, 0);
6690 return TRUE;
6691 }
6692 return FALSE;
6693}
6694
6695/*
6696 * Create a dialog dynamically from the parameter strings.
6697 * type = type of dialog (question, alert, etc.)
6698 * title = dialog title. may be NULL for default title.
6699 * message = text to display. Dialog sizes to accommodate it.
6700 * buttons = '\n' separated list of button captions, default first.
6701 * dfltbutton = number of default button.
6702 *
6703 * This routine returns 1 if the first button is pressed,
6704 * 2 for the second, etc.
6705 *
6706 * 0 indicates Esc was pressed.
6707 * -1 for unexpected error
6708 *
6709 * If stubbing out this fn, return 1.
6710 */
6711
Bram Moolenaar734a8672019-12-02 22:49:38 +01006712static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 "IDR_VIM",
6715 "IDR_VIM_ERROR",
6716 "IDR_VIM_ALERT",
6717 "IDR_VIM_INFO",
6718 "IDR_VIM_QUESTION"
6719};
6720
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 int
6722gui_mch_dialog(
6723 int type,
6724 char_u *title,
6725 char_u *message,
6726 char_u *buttons,
6727 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006728 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006729 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006732 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 int numButtons;
6734 int *buttonWidths, *buttonPositions;
6735 int buttonYpos;
6736 int nchar, i;
6737 DWORD lStyle;
6738 int dlgwidth = 0;
6739 int dlgheight;
6740 int editboxheight;
6741 int horizWidth = 0;
6742 int msgheight;
6743 char_u *pstart;
6744 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006745 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 char_u *tbuffer;
6747 RECT rect;
6748 HWND hwnd;
6749 HDC hdc;
6750 HFONT font, oldFont;
6751 TEXTMETRIC fontInfo;
6752 int fontHeight;
6753 int textWidth, minButtonWidth, messageWidth;
6754 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006755 int maxDialogHeight;
6756 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 int vertical;
6758 int dlgPaddingX;
6759 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006760# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006761 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006763# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006764 garray_T ga;
6765 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006766 int dlg_icon_width;
6767 int dlg_icon_height;
6768 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006770# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006771 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006772# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006773 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006774# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006775 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006776 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
Bram Moolenaar748bf032005-02-02 23:04:36 +00006779 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006780 {
6781 load_dpi_func();
6782 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006783 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006784 }
6785 else
6786 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 if ((type < 0) || (type > VIM_LAST_TYPE))
6789 type = 0;
6790
Bram Moolenaar734a8672019-12-02 22:49:38 +01006791 // allocate some memory for dialog template
6792 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006793 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006794 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 if (p == NULL)
6797 return -1;
6798
6799 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006800 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6802 * const.
6803 */
6804 tbuffer = vim_strsave(buttons);
6805 if (tbuffer == NULL)
6806 return -1;
6807
Bram Moolenaar734a8672019-12-02 22:49:38 +01006808 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
Bram Moolenaar734a8672019-12-02 22:49:38 +01006810 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 numButtons = 1;
6812 for (i = 0; tbuffer[i] != '\0'; i++)
6813 {
6814 if (tbuffer[i] == DLG_BUTTON_SEP)
6815 numButtons++;
6816 }
6817 if (dfltbutton >= numButtons)
6818 dfltbutton = -1;
6819
Bram Moolenaar734a8672019-12-02 22:49:38 +01006820 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006821 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (buttonWidths == NULL)
6823 return -1;
6824
Bram Moolenaar734a8672019-12-02 22:49:38 +01006825 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006826 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (buttonPositions == NULL)
6828 return -1;
6829
6830 /*
6831 * Calculate how big the dialog must be.
6832 */
6833 hwnd = GetDesktopWindow();
6834 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006835# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6837 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006838 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 use_lfSysmenu = TRUE;
6840 }
6841 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006842# endif
K.Takatad1c58992022-01-23 12:31:57 +00006843 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6844 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6845
6846 oldFont = SelectFont(hdc, font);
6847 dlgPaddingX = DLG_PADDING_X;
6848 dlgPaddingY = DLG_PADDING_Y;
6849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 GetTextMetrics(hdc, &fontInfo);
6851 fontHeight = fontInfo.tmHeight;
6852
Bram Moolenaar734a8672019-12-02 22:49:38 +01006853 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006854 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
Bram Moolenaar734a8672019-12-02 22:49:38 +01006856 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006857 if (s_hwnd == NULL)
6858 {
6859 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
Bram Moolenaar734a8672019-12-02 22:49:38 +01006861 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006862 get_work_area(&workarea_rect);
6863 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006864 if (maxDialogWidth > adjust_by_system_dpi(600))
6865 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006866 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006867 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006868 }
6869 else
6870 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006871 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006872 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006873 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006874 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6875 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6876 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6877 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006878
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006879 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006880 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6881 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6882 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6883 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6884 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006885 }
6886
Bram Moolenaar734a8672019-12-02 22:49:38 +01006887 // Set dlgwidth to width of message.
6888 // Copy the message into "ga", changing NL to CR-NL and inserting line
6889 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 pstart = message;
6891 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006892 msgheight = 0;
6893 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 do
6895 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006896 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006897
Bram Moolenaar734a8672019-12-02 22:49:38 +01006898 // Need to figure out where to break the string. The system does it
6899 // at a word boundary, which would mean we can't compute the number of
6900 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006901 textWidth = 0;
6902 last_white = NULL;
6903 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006904 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006905 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006906 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006907 && textWidth > maxDialogWidth * 3 / 4)
6908 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006909 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006910 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006911 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006912 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006913 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006914 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006915 textWidth = 0;
6916
6917 if (last_white != NULL)
6918 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006919 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006920 if (pend > last_white)
6921 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006922 pend = last_white + 1;
6923 last_white = NULL;
6924 }
6925 ga_append(&ga, '\r');
6926 ga_append(&ga, '\n');
6927 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006928 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929
6930 while (--l >= 0)
6931 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006932 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006933 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006935
6936 ga_append(&ga, '\r');
6937 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 pstart = pend + 1;
6939 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006940
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006941 if (ga.ga_data != NULL)
6942 message = ga.ga_data;
6943
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006945
K.Takatac81e9bf2022-01-16 14:15:49 +00006946 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6947 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
K.Takatac81e9bf2022-01-16 14:15:49 +00006949 // Add width of icon to dlgwidth, and some space
6950 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6951 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6952
6953 if (msgheight < dlg_icon_height)
6954 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 /*
6957 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006958 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006960 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (!vertical)
6962 {
6963 // Place buttons horizontally if they fit.
6964 horizWidth = dlgPaddingX;
6965 pstart = tbuffer;
6966 i = 0;
6967 do
6968 {
6969 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6970 if (pend == NULL)
6971 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006972 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (textWidth < minButtonWidth)
6974 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006975 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 buttonWidths[i] = textWidth;
6977 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006978 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 pstart = pend + 1;
6980 } while (*pend != NUL);
6981
6982 if (horizWidth > maxDialogWidth)
6983 vertical = TRUE; // Too wide to fit on the screen.
6984 else if (horizWidth > dlgwidth)
6985 dlgwidth = horizWidth;
6986 }
6987
6988 if (vertical)
6989 {
6990 // Stack buttons vertically.
6991 pstart = tbuffer;
6992 do
6993 {
6994 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6995 if (pend == NULL)
6996 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006997 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006998 textWidth += dlgPaddingX; // Padding within button
6999 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (textWidth > dlgwidth)
7001 dlgwidth = textWidth;
7002 pstart = pend + 1;
7003 } while (*pend != NUL);
7004 }
7005
7006 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007007 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
Bram Moolenaar734a8672019-12-02 22:49:38 +01007009 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007010 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 add_long(lStyle);
7013 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007014 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 add_word(0); // NumberOfItems(will change later)
7016 add_word(10); // x
7017 add_word(10); // y
7018 add_word(PixelToDialogX(dlgwidth)); // cx
7019
7020 // Dialog height.
7021 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007022 dlgheight = msgheight + 2 * dlgPaddingY
7023 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 else
7025 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7026
7027 // Dialog needs to be taller if contains an edit box.
7028 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7029 if (textfield != NULL)
7030 dlgheight += editboxheight;
7031
Bram Moolenaar734a8672019-12-02 22:49:38 +01007032 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007033 if (dlgheight > maxDialogHeight)
7034 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007035 msgheight = msgheight - (dlgheight - maxDialogHeight);
7036 dlgheight = maxDialogHeight;
7037 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007038 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007039 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007040 }
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 add_word(PixelToDialogY(dlgheight));
7043
7044 add_word(0); // Menu
7045 add_word(0); // Class
7046
Bram Moolenaar734a8672019-12-02 22:49:38 +01007047 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007048 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7049 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 p += nchar;
7051
K.Takatad1c58992022-01-23 12:31:57 +00007052 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007053# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007054 if (use_lfSysmenu)
7055 {
7056 // point size
7057 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7058 GetDeviceCaps(hdc, LOGPIXELSY));
7059 wcscpy(p, lfSysmenu.lfFaceName);
7060 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 }
K.Takatad1c58992022-01-23 12:31:57 +00007062 else
7063# endif
7064 {
7065 *p++ = DLG_FONT_POINT_SIZE; // point size
7066 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7067 }
7068 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
7070 buttonYpos = msgheight + 2 * dlgPaddingY;
7071
7072 if (textfield != NULL)
7073 buttonYpos += editboxheight;
7074
7075 pstart = tbuffer;
7076 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007077 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 for (i = 0; i < numButtons; i++)
7079 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007080 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 for ( pend = pstart;
7082 *pend && (*pend != DLG_BUTTON_SEP);
7083 pend++)
7084 ;
7085
7086 if (*pend)
7087 *pend = '\0';
7088
7089 /*
7090 * old NOTE:
7091 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7092 * the focus to the first tab-able button and in so doing makes that
7093 * the default!! Grrr. Workaround: Make the default button the only
7094 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7095 * he/she can use arrow keys.
7096 *
7097 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007098 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 * dialog. Also needed for when the textfield is the default control.
7100 * It appears to work now (perhaps not on Win95?).
7101 */
7102 if (vertical)
7103 {
7104 p = add_dialog_element(p,
7105 (i == dfltbutton
7106 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7107 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007108 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 + 2 * fontHeight * i),
7110 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
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 }
7114 else
7115 {
7116 p = add_dialog_element(p,
7117 (i == dfltbutton
7118 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7119 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007120 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 PixelToDialogX(buttonWidths[i]),
7122 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007123 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007125 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
7127 *pnumitems += numButtons;
7128
Bram Moolenaar734a8672019-12-02 22:49:38 +01007129 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 p = add_dialog_element(p, SS_ICON,
7131 PixelToDialogX(dlgPaddingX),
7132 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007133 PixelToDialogX(dlg_icon_width),
7134 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7136 dlg_icons[type]);
7137
Bram Moolenaar734a8672019-12-02 22:49:38 +01007138 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007139 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007140 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007141 PixelToDialogY(dlgPaddingY),
7142 (WORD)(PixelToDialogX(messageWidth) + 1),
7143 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007144 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaar734a8672019-12-02 22:49:38 +01007146 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (textfield != NULL)
7148 {
7149 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7150 PixelToDialogX(2 * dlgPaddingX),
7151 PixelToDialogY(2 * dlgPaddingY + msgheight),
7152 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7153 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007154 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 *pnumitems += 1;
7156 }
7157
7158 *pnumitems += 2;
7159
7160 SelectFont(hdc, oldFont);
7161 DeleteObject(font);
7162 ReleaseDC(hwnd, hdc);
7163
Bram Moolenaar734a8672019-12-02 22:49:38 +01007164 // Let the dialog_callback() function know which button to make default
7165 // If we have an edit box, make that the default. We also need to tell
7166 // dialog_callback() if this dialog contains an edit box or not. We do
7167 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (textfield != NULL)
7169 {
7170 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7171 s_textfield = textfield;
7172 }
7173 else
7174 {
7175 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7176 s_textfield = NULL;
7177 }
7178
Bram Moolenaar734a8672019-12-02 22:49:38 +01007179 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007181 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 (LPDLGTEMPLATE)pdlgtemplate,
7183 s_hwnd,
7184 (DLGPROC)dialog_callback);
7185
7186 LocalFree(LocalHandle(pdlgtemplate));
7187 vim_free(tbuffer);
7188 vim_free(buttonWidths);
7189 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007190 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar734a8672019-12-02 22:49:38 +01007192 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 (void)SetFocus(s_hwnd);
7194
7195 return nchar;
7196}
7197
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007199
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200/*
7201 * Put a simple element (basic class) onto a dialog template in memory.
7202 * return a pointer to where the next item should be added.
7203 *
7204 * parameters:
7205 * lStyle = additional style flags
7206 * (be careful, NT3.51 & Win32s will ignore the new ones)
7207 * x,y = x & y positions IN DIALOG UNITS
7208 * w,h = width and height IN DIALOG UNITS
7209 * Id = ID used in messages
7210 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7211 * caption = usually text or resource name
7212 *
7213 * TODO: use the length information noted here to enable the dialog creation
7214 * routines to work out more exactly how much memory they need to alloc.
7215 */
7216 static PWORD
7217add_dialog_element(
7218 PWORD p,
7219 DWORD lStyle,
7220 WORD x,
7221 WORD y,
7222 WORD w,
7223 WORD h,
7224 WORD Id,
7225 WORD clss,
7226 const char *caption)
7227{
7228 int nchar;
7229
Bram Moolenaar734a8672019-12-02 22:49:38 +01007230 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7232 *p++ = LOWORD(lStyle);
7233 *p++ = HIWORD(lStyle);
7234 *p++ = 0; // LOWORD (lExtendedStyle)
7235 *p++ = 0; // HIWORD (lExtendedStyle)
7236 *p++ = x;
7237 *p++ = y;
7238 *p++ = w;
7239 *p++ = h;
7240 *p++ = Id; //9 or 10 words in all
7241
7242 *p++ = (WORD)0xffff;
7243 *p++ = clss; //2 more here
7244
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007245 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 p += nchar;
7247
7248 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7249
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007250 return p; // total = 15 + strlen(caption) words
7251 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252}
7253
7254
7255/*
7256 * Helper routine. Take an input pointer, return closest pointer that is
7257 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7258 */
7259 static LPWORD
7260lpwAlign(
7261 LPWORD lpIn)
7262{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007263 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007265 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 ul += 3;
7267 ul >>= 2;
7268 ul <<= 2;
7269 return (LPWORD)ul;
7270}
7271
7272/*
7273 * Helper routine. Takes second parameter as Ansi string, copies it to first
7274 * parameter as wide character (16-bits / char) string, and returns integer
7275 * number of wide characters (words) in string (including the trailing wide
7276 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007277 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7278 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 static int
7280nCopyAnsiToWideChar(
7281 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007282 LPSTR lpAnsiIn,
7283 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007286 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 int i;
7288 WCHAR *wn;
7289
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007290 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007292 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007293 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (wn != NULL)
7295 {
7296 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 vim_free(wn);
7299 }
7300 }
7301 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007302 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 nChar = MultiByteToWideChar(
7304 enc_codepage > 0 ? enc_codepage : CP_ACP,
7305 MB_PRECOMPOSED,
7306 lpAnsiIn, len,
7307 lpWCStr, len);
7308 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007309 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 return nChar;
7313}
7314
7315
7316#ifdef FEAT_TEAROFF
7317/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007318 * Lookup menu handle from "menu_id".
7319 */
7320 static HMENU
7321tearoff_lookup_menuhandle(
7322 vimmenu_T *menu,
7323 WORD menu_id)
7324{
7325 for ( ; menu != NULL; menu = menu->next)
7326 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007327 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007328 continue;
7329 if (menu_is_separator(menu->dname))
7330 continue;
7331 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7332 return menu->submenu_id;
7333 }
7334 return NULL;
7335}
7336
7337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 * The callback function for all the modeless dialogs that make up the
7339 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7340 * thinking its menus have been clicked), and go away when closed.
7341 */
7342 static LRESULT CALLBACK
7343tearoff_callback(
7344 HWND hwnd,
7345 UINT message,
7346 WPARAM wParam,
7347 LPARAM lParam)
7348{
7349 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007350 {
7351 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar734a8672019-12-02 22:49:38 +01007355 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 HandleMouseHide(message, lParam);
7357
7358 if (message == WM_COMMAND)
7359 {
7360 if ((WORD)(LOWORD(wParam)) & 0x8000)
7361 {
7362 POINT mp;
7363 RECT rect;
7364
7365 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7366 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007367 vimmenu_T *menu;
7368
7369 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007371 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7373 (int)rect.right - 8,
7374 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007375 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 s_hwnd,
7377 NULL);
7378 /*
7379 * NOTE: The pop-up menu can eat the mouse up event.
7380 * We deal with this in normal.c.
7381 */
7382 }
7383 }
7384 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007385 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7387 /*
7388 * Give main window the focus back: this is so after
7389 * choosing a tearoff button you can start typing again
7390 * straight away.
7391 */
7392 (void)SetFocus(s_hwnd);
7393 return TRUE;
7394 }
7395 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7396 {
7397 DestroyWindow(hwnd);
7398 return TRUE;
7399 }
7400
Bram Moolenaar734a8672019-12-02 22:49:38 +01007401 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (message == WM_EXITSIZEMOVE)
7403 (void)SetActiveWindow(s_hwnd);
7404
7405 return FALSE;
7406}
7407#endif
7408
7409
7410/*
K.Takatad1c58992022-01-23 12:31:57 +00007411 * Computes the dialog base units based on the current dialog font.
7412 * We don't use the GetDialogBaseUnits() API, because we don't use the
7413 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 */
7415 static void
7416get_dialog_font_metrics(void)
7417{
7418 HDC hdc;
7419 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 SIZE size;
7421#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007422 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423#endif
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007426 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007427 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429#endif
7430 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007431 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
K.Takatad1c58992022-01-23 12:31:57 +00007433 hdc = GetDC(s_hwnd);
7434 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007435 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007436 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
K.Takataabe628e2022-01-23 16:25:17 +00007438 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007439 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440}
7441
7442#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7443/*
7444 * Create a pseudo-"tearoff menu" based on the child
7445 * items of a given menu pointer.
7446 */
7447 static void
7448gui_mch_tearoff(
7449 char_u *title,
7450 vimmenu_T *menu,
7451 int initX,
7452 int initY)
7453{
7454 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7455 int template_len;
7456 int nchar, textWidth, submenuWidth;
7457 DWORD lStyle;
7458 DWORD lExtendedStyle;
7459 WORD dlgwidth;
7460 WORD menuID;
7461 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007462 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 vimmenu_T *the_menu = menu;
7464 HWND hwnd;
7465 HDC hdc;
7466 HFONT font, oldFont;
7467 int col, spaceWidth, len;
7468 int columnWidths[2];
7469 char_u *label, *text;
7470 int acLen = 0;
7471 int nameLen;
7472 int padding0, padding1, padding2 = 0;
7473 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007474 int x;
7475 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007476# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007477 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481 /*
7482 * If this menu is already torn off, move it to the mouse position.
7483 */
7484 if (IsWindow(menu->tearoff_handle))
7485 {
7486 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007487 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
7489 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7490 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7491 }
7492 return;
7493 }
7494
7495 /*
7496 * Create a new tearoff.
7497 */
7498 if (*title == MNU_HIDDEN_CHAR)
7499 title++;
7500
Bram Moolenaar734a8672019-12-02 22:49:38 +01007501 // Allocate memory to store the dialog template. It's made bigger when
7502 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 template_len = DLG_ALLOC_SIZE;
7504 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7505 if (p == NULL)
7506 return;
7507
7508 hwnd = GetDesktopWindow();
7509 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007510# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7512 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007513 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 use_lfSysmenu = TRUE;
7515 }
7516 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007517# endif
K.Takatad1c58992022-01-23 12:31:57 +00007518 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7519 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7520
7521 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
Bram Moolenaar734a8672019-12-02 22:49:38 +01007523 // Calculate width of a single space. Used for padding columns to the
7524 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007525 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526
Bram Moolenaar734a8672019-12-02 22:49:38 +01007527 // Figure out max width of the text column, the accelerator column and the
7528 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 submenuWidth = 0;
7530 for (col = 0; col < 2; col++)
7531 {
7532 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007533 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007535 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 text = (col == 0) ? pmenu->dname : pmenu->actext;
7537 if (text != NULL && *text != NUL)
7538 {
7539 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7540 if (textWidth > columnWidths[col])
7541 columnWidths[col] = textWidth;
7542 }
7543 if (pmenu->children != NULL)
7544 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7545 }
7546 }
7547 if (columnWidths[1] == 0)
7548 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007549 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 if (submenuWidth != 0)
7551 columnWidths[0] += submenuWidth;
7552 else
7553 columnWidths[0] += spaceWidth;
7554 }
7555 else
7556 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007557 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7559 columnWidths[1] += submenuWidth;
7560 }
7561
7562 /*
7563 * Now find the total width of our 'menu'.
7564 */
7565 textWidth = columnWidths[0] + columnWidths[1];
7566 if (submenuWidth != 0)
7567 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007568 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7570 textWidth += submenuWidth;
7571 }
7572 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7573 if (textWidth > dlgwidth)
7574 dlgwidth = textWidth;
7575 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7576
Bram Moolenaar734a8672019-12-02 22:49:38 +01007577 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007578 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7581 *p++ = LOWORD(lStyle);
7582 *p++ = HIWORD(lStyle);
7583 *p++ = LOWORD(lExtendedStyle);
7584 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007585 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007587 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007589 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 else
7591 *p++ = PixelToDialogX(initX); // x
7592 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007593 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 else
7595 *p++ = PixelToDialogY(initY); // y
7596 *p++ = PixelToDialogX(dlgwidth); // cx
7597 ptrueheight = p;
7598 *p++ = 0; // dialog height: changed later anyway
7599 *p++ = 0; // Menu
7600 *p++ = 0; // Class
7601
Bram Moolenaar734a8672019-12-02 22:49:38 +01007602 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007604 ? (LPSTR)title
7605 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 p += nchar;
7607
K.Takatad1c58992022-01-23 12:31:57 +00007608 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007609# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007610 if (use_lfSysmenu)
7611 {
7612 // point size
7613 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7614 GetDeviceCaps(hdc, LOGPIXELSY));
7615 wcscpy(p, lfSysmenu.lfFaceName);
7616 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
K.Takatad1c58992022-01-23 12:31:57 +00007618 else
7619# endif
7620 {
7621 *p++ = DLG_FONT_POINT_SIZE; // point size
7622 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7623 }
7624 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625
7626 /*
7627 * Loop over all the items in the menu.
7628 * But skip over the tearbar.
7629 */
7630 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7631 menu = menu->children->next;
7632 else
7633 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007634 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 for ( ; menu != NULL; menu = menu->next)
7636 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007637 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 continue;
7639 if (menu_is_separator(menu->dname))
7640 {
7641 sepPadding += 3;
7642 continue;
7643 }
7644
Bram Moolenaar734a8672019-12-02 22:49:38 +01007645 // Check if there still is plenty of room in the template. Make it
7646 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7648 {
7649 WORD *newp;
7650
7651 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7652 if (newp != NULL)
7653 {
7654 template_len += 4096;
7655 mch_memmove(newp, pdlgtemplate,
7656 (char *)p - (char *)pdlgtemplate);
7657 p = newp + (p - pdlgtemplate);
7658 pnumitems = newp + (pnumitems - pdlgtemplate);
7659 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7660 LocalFree(LocalHandle(pdlgtemplate));
7661 pdlgtemplate = newp;
7662 }
7663 }
7664
Bram Moolenaar734a8672019-12-02 22:49:38 +01007665 // Figure out minimal length of this menu label. Use "name" for the
7666 // actual text, "dname" for estimating the displayed size. "name"
7667 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 len = nameLen = (int)STRLEN(menu->name);
7669 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7670 (int)STRLEN(menu->dname))) / spaceWidth;
7671 len += padding0;
7672
7673 if (menu->actext != NULL)
7674 {
7675 acLen = (int)STRLEN(menu->actext);
7676 len += acLen;
7677 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7678 }
7679 else
7680 textWidth = 0;
7681 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7682 len += padding1;
7683
7684 if (menu->children == NULL)
7685 {
7686 padding2 = submenuWidth / spaceWidth;
7687 len += padding2;
7688 menuID = (WORD)(menu->id);
7689 }
7690 else
7691 {
7692 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007693 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
7695
Bram Moolenaar734a8672019-12-02 22:49:38 +01007696 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007697 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 if (label == NULL)
7699 break;
7700
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007701 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007702 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007704 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 while (padding0-- > 0)
7706 *text++ = ' ';
7707 if (menu->actext != NULL)
7708 {
7709 STRNCPY(text, menu->actext, acLen);
7710 text += acLen;
7711 }
7712 while (padding1-- > 0)
7713 *text++ = ' ';
7714 if (menu->children != NULL)
7715 {
7716 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7717 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7718 }
7719 else
7720 {
7721 while (padding2-- > 0)
7722 *text++ = ' ';
7723 }
7724 *text = NUL;
7725
7726 /*
7727 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7728 * W95/NT4 it makes the tear-off look more like a menu.
7729 */
7730 p = add_dialog_element(p,
7731 BS_PUSHBUTTON|BS_LEFT,
7732 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7733 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7734 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7735 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007736 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 vim_free(label);
7738 (*pnumitems)++;
7739 }
7740
7741 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7742
7743
Bram Moolenaar734a8672019-12-02 22:49:38 +01007744 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007745 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007746 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 (LPDLGTEMPLATE)pdlgtemplate,
7748 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007749 (DLGPROC)tearoff_callback,
7750 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 LocalFree(LocalHandle(pdlgtemplate));
7753 SelectFont(hdc, oldFont);
7754 DeleteObject(font);
7755 ReleaseDC(hwnd, hdc);
7756
7757 /*
7758 * Reassert ourselves as the active window. This is so that after creating
7759 * a tearoff, the user doesn't have to click with the mouse just to start
7760 * typing again!
7761 */
7762 (void)SetActiveWindow(s_hwnd);
7763
Bram Moolenaar734a8672019-12-02 22:49:38 +01007764 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 force_menu_update = TRUE;
7766}
7767#endif
7768
7769#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007770# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772/*
7773 * Create the toolbar, initially unpopulated.
7774 * (just like the menu, there are no defaults, it's all
7775 * set up through menu.vim)
7776 */
7777 static void
7778initialise_toolbar(void)
7779{
7780 InitCommonControls();
7781 s_toolbarhwnd = CreateToolbarEx(
7782 s_hwnd,
7783 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7784 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007785 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007786 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 IDR_TOOLBAR1, // id of initial bitmap
7788 NULL,
7789 0, // initial number of buttons
7790 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7791 TOOLBAR_BUTTON_HEIGHT,
7792 TOOLBAR_BUTTON_WIDTH,
7793 TOOLBAR_BUTTON_HEIGHT,
7794 sizeof(TBBUTTON)
7795 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007796
7797 // Remove transparency from the toolbar to prevent the main window
7798 // background colour showing through
7799 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7800 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7801
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007802 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
7804 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007805
7806 update_toolbar_size();
7807}
7808
7809 static void
7810update_toolbar_size(void)
7811{
7812 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007813 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007814
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007815 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007816 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7817 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7818 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7819 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7820
7821 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7822 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7823 //TRACE("button size: %d, %d", w, h);
7824 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7825 gui.toolbar_height = h + 6;
7826
7827 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7828 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7829
7830 // TODO:
7831 // Currently, this function only updates the size of toolbar buttons.
7832 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833}
7834
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007835 static LRESULT CALLBACK
7836toolbar_wndproc(
7837 HWND hwnd,
7838 UINT uMsg,
7839 WPARAM wParam,
7840 LPARAM lParam)
7841{
7842 HandleMouseHide(uMsg, lParam);
7843 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7844}
7845
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 static int
7847get_toolbar_bitmap(vimmenu_T *menu)
7848{
7849 int i = -1;
7850
7851 /*
7852 * Check user bitmaps first, unless builtin is specified.
7853 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007854 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
7856 char_u fname[MAXPATHL];
7857 HANDLE hbitmap = NULL;
7858
7859 if (menu->iconfile != NULL)
7860 {
7861 gui_find_iconfile(menu->iconfile, fname, "bmp");
7862 hbitmap = LoadImage(
7863 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007864 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 IMAGE_BITMAP,
7866 TOOLBAR_BUTTON_WIDTH,
7867 TOOLBAR_BUTTON_HEIGHT,
7868 LR_LOADFROMFILE |
7869 LR_LOADMAP3DCOLORS
7870 );
7871 }
7872
7873 /*
7874 * If the LoadImage call failed, or the "icon=" file
7875 * didn't exist or wasn't specified, try the menu name
7876 */
7877 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007878 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007879# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007880 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007881# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007882 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 hbitmap = LoadImage(
7884 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007885 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 IMAGE_BITMAP,
7887 TOOLBAR_BUTTON_WIDTH,
7888 TOOLBAR_BUTTON_HEIGHT,
7889 LR_LOADFROMFILE |
7890 LR_LOADMAP3DCOLORS
7891 );
7892
7893 if (hbitmap != NULL)
7894 {
7895 TBADDBITMAP tbAddBitmap;
7896
7897 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007898 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
7900 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7901 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007902 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 }
7905 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7906 i = menu->iconidx;
7907
7908 return i;
7909}
7910#endif
7911
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007912#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7913 static void
7914initialise_tabline(void)
7915{
7916 InitCommonControls();
7917
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007918 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007919 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007920 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007921 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007922 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007923
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007924 gui.tabline_height = TABLINE_HEIGHT;
7925
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007926 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007927}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007928
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007929/*
7930 * Get tabpage_T from POINT.
7931 */
7932 static tabpage_T *
7933GetTabFromPoint(
7934 HWND hWnd,
7935 POINT pt)
7936{
7937 tabpage_T *ptp = NULL;
7938
7939 if (gui_mch_showing_tabline())
7940 {
7941 TCHITTESTINFO htinfo;
7942 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007943 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007944 if (s_tabhwnd == hWnd)
7945 {
7946 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7947 if (idx != -1)
7948 ptp = find_tabpage(idx + 1);
7949 }
7950 }
7951 return ptp;
7952}
7953
7954static POINT s_pt = {0, 0};
7955static HCURSOR s_hCursor = NULL;
7956
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007957 static LRESULT CALLBACK
7958tabline_wndproc(
7959 HWND hwnd,
7960 UINT uMsg,
7961 WPARAM wParam,
7962 LPARAM lParam)
7963{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007964 POINT pt;
7965 tabpage_T *tp;
7966 RECT rect;
7967 int nCenter;
7968 int idx0;
7969 int idx1;
7970
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007971 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007972
7973 switch (uMsg)
7974 {
7975 case WM_LBUTTONDOWN:
7976 {
7977 s_pt.x = GET_X_LPARAM(lParam);
7978 s_pt.y = GET_Y_LPARAM(lParam);
7979 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007980 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007981 break;
7982 }
7983 case WM_MOUSEMOVE:
7984 if (GetCapture() == hwnd
7985 && ((wParam & MK_LBUTTON)) != 0)
7986 {
7987 pt.x = GET_X_LPARAM(lParam);
7988 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007989 if (abs(pt.x - s_pt.x) >
7990 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007991 {
7992 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7993
7994 tp = GetTabFromPoint(hwnd, pt);
7995 if (tp != NULL)
7996 {
7997 idx0 = tabpage_index(curtab) - 1;
7998 idx1 = tabpage_index(tp) - 1;
7999
8000 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8001 nCenter = rect.left + (rect.right - rect.left) / 2;
8002
Bram Moolenaar734a8672019-12-02 22:49:38 +01008003 // Check if the mouse cursor goes over the center of
8004 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008005 if ((idx0 < idx1) && (nCenter < pt.x))
8006 {
8007 tabpage_move(idx1 + 1);
8008 update_screen(0);
8009 }
8010 else if ((idx1 < idx0) && (pt.x < nCenter))
8011 {
8012 tabpage_move(idx1);
8013 update_screen(0);
8014 }
8015 }
8016 }
8017 }
8018 break;
8019 case WM_LBUTTONUP:
8020 {
8021 if (GetCapture() == hwnd)
8022 {
8023 SetCursor(s_hCursor);
8024 ReleaseCapture();
8025 }
8026 break;
8027 }
8028 default:
8029 break;
8030 }
8031
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008032 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8033}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008034#endif
8035
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8037/*
8038 * Make the GUI window come to the foreground.
8039 */
8040 void
8041gui_mch_set_foreground(void)
8042{
8043 if (IsIconic(s_hwnd))
8044 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8045 SetForegroundWindow(s_hwnd);
8046}
8047#endif
8048
8049#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8050 static void
8051dyn_imm_load(void)
8052{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008053 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 if (hLibImm == NULL)
8055 return;
8056
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008058 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008060 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008062 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008064 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008066 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008068 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008069 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008070 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008071 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008072 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008074 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008076 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008077 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008078 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
K.Takatab0b2b732022-01-19 12:59:21 +00008080 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 || pImmGetContext == NULL
8082 || pImmAssociateContext == NULL
8083 || pImmReleaseContext == NULL
8084 || pImmGetOpenStatus == NULL
8085 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008086 || pImmGetCompositionFontW == NULL
8087 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008089 || pImmGetConversionStatus == NULL
8090 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {
8092 FreeLibrary(hLibImm);
8093 hLibImm = NULL;
8094 pImmGetContext = NULL;
8095 return;
8096 }
8097
8098 return;
8099}
8100
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101#endif
8102
8103#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8104
8105# ifdef FEAT_XPM_W32
8106# define IMAGE_XPM 100
8107# endif
8108
8109typedef struct _signicon_t
8110{
8111 HANDLE hImage;
8112 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008113# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008114 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116} signicon_t;
8117
8118 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008119gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 signicon_t *sign;
8122 int x, y, w, h;
8123
8124 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8125 return;
8126
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008127# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008128 if (IS_ENABLE_DIRECTX())
8129 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008130# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008131
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 x = TEXT_X(col);
8133 y = TEXT_Y(row);
8134 w = gui.char_width * 2;
8135 h = gui.char_height;
8136 switch (sign->uType)
8137 {
8138 case IMAGE_BITMAP:
8139 {
8140 HDC hdcMem;
8141 HBITMAP hbmpOld;
8142
8143 hdcMem = CreateCompatibleDC(s_hdc);
8144 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8145 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8146 SelectObject(hdcMem, hbmpOld);
8147 DeleteDC(hdcMem);
8148 }
8149 break;
8150 case IMAGE_ICON:
8151 case IMAGE_CURSOR:
8152 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8153 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008154# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 case IMAGE_XPM:
8156 {
8157 HDC hdcMem;
8158 HBITMAP hbmpOld;
8159
8160 hdcMem = CreateCompatibleDC(s_hdc);
8161 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008162 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8164
8165 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008166 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8168 SelectObject(hdcMem, hbmpOld);
8169 DeleteDC(hdcMem);
8170 }
8171 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 }
8174}
8175
8176 static void
8177close_signicon_image(signicon_t *sign)
8178{
8179 if (sign)
8180 switch (sign->uType)
8181 {
8182 case IMAGE_BITMAP:
8183 DeleteObject((HGDIOBJ)sign->hImage);
8184 break;
8185 case IMAGE_CURSOR:
8186 DestroyCursor((HCURSOR)sign->hImage);
8187 break;
8188 case IMAGE_ICON:
8189 DestroyIcon((HICON)sign->hImage);
8190 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008191# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 case IMAGE_XPM:
8193 DeleteObject((HBITMAP)sign->hImage);
8194 DeleteObject((HBITMAP)sign->hShape);
8195 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 }
8198}
8199
8200 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008201gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202{
8203 signicon_t sign, *psign;
8204 char_u *ext;
8205
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008207 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 if (ext > signfile)
8209 {
8210 int do_load = 1;
8211
8212 if (!STRICMP(ext, ".bmp"))
8213 sign.uType = IMAGE_BITMAP;
8214 else if (!STRICMP(ext, ".ico"))
8215 sign.uType = IMAGE_ICON;
8216 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8217 sign.uType = IMAGE_CURSOR;
8218 else
8219 do_load = 0;
8220
8221 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008222 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 gui.char_width * 2, gui.char_height,
8224 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008225# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 if (!STRICMP(ext, ".xpm"))
8227 {
8228 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008229 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8230 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234
8235 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008236 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 *psign = sign;
8238
8239 if (!psign)
8240 {
8241 if (sign.hImage)
8242 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008243 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 }
8245 return (void *)psign;
8246
8247}
8248
8249 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008250gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252 if (sign)
8253 {
8254 close_signicon_image((signicon_t *)sign);
8255 vim_free(sign);
8256 }
8257}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008260#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261
Bram Moolenaar734a8672019-12-02 22:49:38 +01008262/*
8263 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008264 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008266 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8268 * to get current mouse position).
8269 *
8270 * Trying to use as more Windows services as possible, and as less
8271 * IE version as possible :)).
8272 *
8273 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8274 * BalloonEval struct.
8275 * 2) Enable/Disable simply create/kill BalloonEval Timer
8276 * 3) When there was enough inactivity, timer procedure posts
8277 * async request to debugger
8278 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8279 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008280 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 */
8282
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008283 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008284make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008285{
K.Takata76687d22022-01-25 10:31:37 +00008286 TOOLINFOW *pti;
8287 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008288
K.Takata76687d22022-01-25 10:31:37 +00008289 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008290 if (pti == NULL)
8291 return;
8292
8293 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8294 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8295 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008296 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008297
8298 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8299 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8300
K.Takata76687d22022-01-25 10:31:37 +00008301 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008302 pti->uFlags = TTF_SUBCLASS;
8303 pti->hwnd = beval->target;
8304 pti->hinst = 0; // Don't use string resources
8305 pti->uId = ID_BEVAL_TOOLTIP;
8306
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008307 pti->lpszText = LPSTR_TEXTCALLBACKW;
8308 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8309 pti->lParam = (LPARAM)beval->tofree;
8310 // switch multiline tooltips on
8311 if (GetClientRect(s_textArea, &rect))
8312 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8313 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008314
8315 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8316 pti->rect.left = pt.x - 3;
8317 pti->rect.top = pt.y - 3;
8318 pti->rect.right = pt.x + 3;
8319 pti->rect.bottom = pt.y + 3;
8320
8321 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8322 // Make tooltip appear sooner.
8323 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8324 // I've performed some tests and it seems the longest possible life time
8325 // of tooltip is 30 seconds.
8326 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8327 /*
8328 * HACK: force tooltip to appear, because it'll not appear until
8329 * first mouse move. D*mn M$
8330 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8331 */
8332 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8333 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8334 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008335}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008336
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008338delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008340 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
8343 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008344beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008345 HWND hwnd UNUSED,
8346 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008347 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008348 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 POINT pt;
8351 RECT rect;
8352
8353 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8354 return;
8355
8356 GetCursorPos(&pt);
8357 if (WindowFromPoint(pt) != s_textArea)
8358 return;
8359
8360 ScreenToClient(s_textArea, &pt);
8361 GetClientRect(s_textArea, &rect);
8362 if (!PtInRect(&rect, pt))
8363 return;
8364
K.Takataa8ec4912022-02-03 14:32:33 +00008365 if (last_user_activity > 0
8366 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 && (cur_beval->showState != ShS_PENDING
8368 || abs(cur_beval->x - pt.x) > 3
8369 || abs(cur_beval->y - pt.y) > 3))
8370 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008371 // Pointer resting in one place long enough, it's time to show
8372 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 cur_beval->showState = ShS_PENDING;
8374 cur_beval->x = pt.x;
8375 cur_beval->y = pt.y;
8376
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 if (cur_beval->msgCB != NULL)
8378 (*cur_beval->msgCB)(cur_beval, 0);
8379 }
8380}
8381
8382 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008383gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384{
K.Takataa8ec4912022-02-03 14:32:33 +00008385 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386}
8387
8388 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008389gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 if (beval == NULL)
8392 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008393 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8394 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395}
8396
8397 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008398gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008401
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008402 vim_free(beval->msg);
8403 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8404 if (beval->msg == NULL)
8405 {
8406 delete_tooltip(beval);
8407 beval->showState = ShS_NEUTRAL;
8408 return;
8409 }
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if (beval->showState == ShS_SHOWING)
8412 return;
8413 GetCursorPos(&pt);
8414 ScreenToClient(s_textArea, &pt);
8415
8416 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008418 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 gui_mch_disable_beval_area(cur_beval);
8420 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008421 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423}
8424
8425 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008426gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008427 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008428 char_u *mesg,
8429 void (*mesgCB)(BalloonEval *, int),
8430 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008432 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 BalloonEval *beval;
8434
8435 if (mesg != NULL && mesgCB != NULL)
8436 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008437 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 return NULL;
8439 }
8440
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008441 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (beval != NULL)
8443 {
8444 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
8446 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 beval->msg = mesg;
8448 beval->msgCB = mesgCB;
8449 beval->clientData = clientData;
8450
8451 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 cur_beval = beval;
8453
8454 if (p_beval)
8455 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 }
8457 return beval;
8458}
8459
8460 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008461Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008463 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 return;
8465
8466 if (cur_beval != NULL)
8467 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008468 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008470 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008471 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008472 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 delete_tooltip(cur_beval);
8474 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
8476 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008477 break;
8478 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008479 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008480 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008481 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008482 info->lpszText = (LPSTR)info->lParam;
8483 info->uFlags |= TTF_DI_SETITEM;
8484 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008485 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008486 case TTN_GETDISPINFOW:
8487 {
8488 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008489 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008490 info->lpszText = (LPWSTR)info->lParam;
8491 info->uFlags |= TTF_DI_SETITEM;
8492 }
8493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 }
8496}
8497
8498 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008499track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8502 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008503 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504}
8505
8506 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008507gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008509# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008510 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008511# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008512 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 vim_free(beval);
8514}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008515#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516
8517#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8518/*
8519 * We have multiple signs to draw at the same location. Draw the
8520 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8521 */
8522 void
8523netbeans_draw_multisign_indicator(int row)
8524{
8525 int i;
8526 int y;
8527 int x;
8528
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008529 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008530 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 x = 0;
8533 y = TEXT_Y(row);
8534
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008535# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008536 if (IS_ENABLE_DIRECTX())
8537 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008538# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 for (i = 0; i < gui.char_height - 3; i++)
8541 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8542
8543 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8544 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8545 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8546 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8547 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8548 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8549 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8550}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008551#endif