blob: 7bdbf418da29f5fd3253be3861f05d922100b7f1 [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
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002058 // Construct the state table with only a few modifiers, we don't
2059 // really care about the presence of Ctrl/Alt as those modifiers are
2060 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002061 memset(keyboard_state, 0, 256);
2062 if (GetKeyState(VK_SHIFT) & 0x8000)
2063 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002064 if (GetKeyState(VK_CAPITAL) & 0x0001)
2065 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002066 // Alt-Gr is synthesized as Alt + Ctrl.
2067 if ((GetKeyState(VK_RMENU) & 0x8000)
2068 && (GetKeyState(VK_CONTROL) & 0x8000))
2069 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002070 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002071 keyboard_state[VK_CONTROL] = 0x80;
2072 }
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);
2082 dead_key = len < 0;
2083
2084 if (len <= 0)
2085 return;
2086
2087 // Post the message as TranslateMessage would do.
2088 if (msg.message == WM_KEYDOWN)
2089 {
2090 for (i = 0; i < len; i++)
2091 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002092 }
2093 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002094 {
2095 for (i = 0; i < len; i++)
2096 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2097 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002098 }
2099 }
2100#ifdef FEAT_MBYTE_IME
2101 else if (msg.message == WM_IME_NOTIFY)
2102 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2103 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002104 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002105 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106#endif
2107
2108#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002109 // Check for <F10>: Default effect is to select the menu. When <F10> is
2110 // mapped we need to stop it here to avoid strange effects (e.g., for the
2111 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2113 NULL, NULL) == NULL)
2114#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002115 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116}
2117
2118/*
2119 * Catch up with any queued events. This may put keyboard input into the
2120 * input buffer, call resize call-backs, trigger timers etc. If there is
2121 * nothing in the event queue (& no timers pending), then we return
2122 * immediately.
2123 */
2124 void
2125gui_mch_update(void)
2126{
2127 MSG msg;
2128
2129 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002130 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002131 && !vim_is_input_buf_full())
2132 process_message();
2133}
2134
Bram Moolenaar4231da42016-06-02 14:30:04 +02002135 static void
2136remove_any_timer(void)
2137{
2138 MSG msg;
2139
2140 if (s_wait_timer != 0 && !s_timed_out)
2141 {
2142 KillTimer(NULL, s_wait_timer);
2143
Bram Moolenaar734a8672019-12-02 22:49:38 +01002144 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002145 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002146 ;
2147 s_wait_timer = 0;
2148 }
2149}
2150
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002151/*
2152 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2153 * from the keyboard.
2154 * wtime == -1 Wait forever.
2155 * wtime == 0 This should never happen.
2156 * wtime > 0 Wait wtime milliseconds for a character.
2157 * Returns OK if a character was found to be available within the given time,
2158 * or FAIL otherwise.
2159 */
2160 int
2161gui_mch_wait_for_chars(int wtime)
2162{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002163 int focus;
2164
2165 s_timed_out = FALSE;
2166
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002167 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002168 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002169 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002170 if (s_busy_processing)
2171 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002172
2173 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002174 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2175 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002176 }
2177
2178 allow_scrollbar = TRUE;
2179
2180 focus = gui.in_focus;
2181 while (!s_timed_out)
2182 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002183 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002184 if (gui.in_focus != focus)
2185 {
2186 if (gui.in_focus)
2187 gui_mch_start_blink();
2188 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002189 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002190 focus = gui.in_focus;
2191 }
2192
2193 if (s_need_activate)
2194 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002196 s_need_activate = FALSE;
2197 }
2198
Bram Moolenaar4231da42016-06-02 14:30:04 +02002199#ifdef FEAT_TIMERS
2200 did_add_timer = FALSE;
2201#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002202#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002203 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002204 for (;;)
2205 {
2206 MSG msg;
2207
2208 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002209# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002210 if (did_add_timer)
2211 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002212# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002213 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002214 {
2215 process_message();
2216 break;
2217 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002218 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002219 // TODO: The 10 msec is a compromise between laggy response
2220 // and consuming more CPU time. Better would be to handle
2221 // channel messages when they arrive.
2222 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002223 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002224 break;
2225 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002226#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002227 // Don't use gui_mch_update() because then we will spin-lock until a
2228 // char arrives, instead we use GetMessage() to hang until an
2229 // event arrives. No need to check for input_buf_full because we are
2230 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002231 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002232#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233
2234 if (input_available())
2235 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002236 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002237 allow_scrollbar = FALSE;
2238
Bram Moolenaar89c00032019-09-04 13:53:21 +02002239 // Clear pending mouse button, the release event may have been
2240 // taken by the dialog window. But don't do this when getting
2241 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002242 if (!s_getting_focus)
2243 s_button_pending = -1;
2244
2245 return OK;
2246 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002247
2248#ifdef FEAT_TIMERS
2249 if (did_add_timer)
2250 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002251 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002252 remove_any_timer();
2253 break;
2254 }
2255#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002256 }
2257 allow_scrollbar = FALSE;
2258 return FAIL;
2259}
2260
2261/*
2262 * Clear a rectangular region of the screen from text pos (row1, col1) to
2263 * (row2, col2) inclusive.
2264 */
2265 void
2266gui_mch_clear_block(
2267 int row1,
2268 int col1,
2269 int row2,
2270 int col2)
2271{
2272 RECT rc;
2273
2274 /*
2275 * Clear one extra pixel at the far right, for when bold characters have
2276 * spilled over to the window border.
2277 * Note: FillRect() excludes right and bottom of rectangle.
2278 */
2279 rc.left = FILL_X(col1);
2280 rc.top = FILL_Y(row1);
2281 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2282 rc.bottom = FILL_Y(row2 + 1);
2283 clear_rect(&rc);
2284}
2285
2286/*
2287 * Clear the whole text window.
2288 */
2289 void
2290gui_mch_clear_all(void)
2291{
2292 RECT rc;
2293
2294 rc.left = 0;
2295 rc.top = 0;
2296 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2297 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2298 clear_rect(&rc);
2299}
2300/*
2301 * Menu stuff.
2302 */
2303
2304 void
2305gui_mch_enable_menu(int flag)
2306{
2307#ifdef FEAT_MENU
2308 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2309#endif
2310}
2311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312 void
2313gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002314 int x UNUSED,
2315 int y UNUSED,
2316 int w UNUSED,
2317 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002318{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002319 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320}
2321
2322#if defined(FEAT_MENU) || defined(PROTO)
2323/*
2324 * Make menu item hidden or not hidden
2325 */
2326 void
2327gui_mch_menu_hidden(
2328 vimmenu_T *menu,
2329 int hidden)
2330{
2331 /*
2332 * This doesn't do what we want. Hmm, just grey the menu items for now.
2333 */
2334 /*
2335 if (hidden)
2336 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2337 else
2338 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2339 */
2340 gui_mch_menu_grey(menu, hidden);
2341}
2342
2343/*
2344 * This is called after setting all the menus to grey/hidden or not.
2345 */
2346 void
2347gui_mch_draw_menubar(void)
2348{
2349 DrawMenuBar(s_hwnd);
2350}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002351#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002353/*
2354 * Return the RGB value of a pixel as a long.
2355 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002356 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357gui_mch_get_rgb(guicolor_T pixel)
2358{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002359 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2360 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361}
2362
2363#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002364/*
2365 * Convert pixels in X to dialog units
2366 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 static WORD
2368PixelToDialogX(int numPixels)
2369{
2370 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2371}
2372
Bram Moolenaar734a8672019-12-02 22:49:38 +01002373/*
2374 * Convert pixels in Y to dialog units
2375 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 static WORD
2377PixelToDialogY(int numPixels)
2378{
2379 return (WORD)((numPixels * 8) / s_dlgfntheight);
2380}
2381
Bram Moolenaar734a8672019-12-02 22:49:38 +01002382/*
2383 * Return the width in pixels of the given text in the given DC.
2384 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002385 static int
2386GetTextWidth(HDC hdc, char_u *str, int len)
2387{
2388 SIZE size;
2389
2390 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2391 return size.cx;
2392}
2393
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394/*
2395 * Return the width in pixels of the given text in the given DC, taking care
2396 * of 'encoding' to active codepage conversion.
2397 */
2398 static int
2399GetTextWidthEnc(HDC hdc, char_u *str, int len)
2400{
2401 SIZE size;
2402 WCHAR *wstr;
2403 int n;
2404 int wlen = len;
2405
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002406 wstr = enc_to_utf16(str, &wlen);
2407 if (wstr == NULL)
2408 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002409
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002410 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2411 vim_free(wstr);
2412 if (n)
2413 return size.cx;
2414 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002416
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002417static void get_work_area(RECT *spi_rect);
2418
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419/*
2420 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002421 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2422 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423 */
2424 static BOOL
2425CenterWindow(
2426 HWND hwndChild,
2427 HWND hwndParent)
2428{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002429 HMONITOR mon;
2430 MONITORINFO moninfo;
2431 RECT rChild, rParent, rScreen;
2432 int wChild, hChild, wParent, hParent;
2433 int xNew, yNew;
2434 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002435
2436 GetWindowRect(hwndChild, &rChild);
2437 wChild = rChild.right - rChild.left;
2438 hChild = rChild.bottom - rChild.top;
2439
Bram Moolenaar734a8672019-12-02 22:49:38 +01002440 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002441 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002442 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443 else
2444 GetWindowRect(hwndParent, &rParent);
2445 wParent = rParent.right - rParent.left;
2446 hParent = rParent.bottom - rParent.top;
2447
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002448 moninfo.cbSize = sizeof(MONITORINFO);
2449 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2450 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002451 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002452 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002454 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002456 hdc = GetDC(hwndChild);
2457 rScreen.left = 0;
2458 rScreen.top = 0;
2459 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2460 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2461 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002462 }
2463
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002464 xNew = rParent.left + ((wParent - wChild) / 2);
2465 if (xNew < rScreen.left)
2466 xNew = rScreen.left;
2467 else if ((xNew + wChild) > rScreen.right)
2468 xNew = rScreen.right - wChild;
2469
2470 yNew = rParent.top + ((hParent - hChild) / 2);
2471 if (yNew < rScreen.top)
2472 yNew = rScreen.top;
2473 else if ((yNew + hChild) > rScreen.bottom)
2474 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475
2476 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2477 SWP_NOSIZE | SWP_NOZORDER);
2478}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002479#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002481#if defined(FEAT_TOOLBAR) || defined(PROTO)
2482 void
2483gui_mch_show_toolbar(int showit)
2484{
2485 if (s_toolbarhwnd == NULL)
2486 return;
2487
2488 if (showit)
2489 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002490 // Enable unicode support
2491 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2492 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002493 ShowWindow(s_toolbarhwnd, SW_SHOW);
2494 }
2495 else
2496 ShowWindow(s_toolbarhwnd, SW_HIDE);
2497}
2498
Bram Moolenaar734a8672019-12-02 22:49:38 +01002499// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002500# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002501
2502#endif
2503
2504#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2505 static void
2506add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2507{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002508 WCHAR *wn;
2509 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002510
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002511 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002512 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002513 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002515 infow.cbSize = sizeof(infow);
2516 infow.fMask = MIIM_TYPE | MIIM_ID;
2517 infow.wID = item_id;
2518 infow.fType = MFT_STRING;
2519 infow.dwTypeData = wn;
2520 infow.cch = (UINT)wcslen(wn);
2521 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2522 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002523}
2524
2525 static void
2526show_tabline_popup_menu(void)
2527{
2528 HMENU tab_pmenu;
2529 long rval;
2530 POINT pt;
2531
Bram Moolenaar734a8672019-12-02 22:49:38 +01002532 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002533 if (hold_gui_events
2534# ifdef FEAT_CMDWIN
2535 || cmdwin_type != 0
2536# endif
2537 )
2538 return;
2539
2540 tab_pmenu = CreatePopupMenu();
2541 if (tab_pmenu == NULL)
2542 return;
2543
2544 if (first_tabpage->tp_next != NULL)
2545 add_tabline_popup_menu_entry(tab_pmenu,
2546 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2547 add_tabline_popup_menu_entry(tab_pmenu,
2548 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2549 add_tabline_popup_menu_entry(tab_pmenu,
2550 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2551
2552 GetCursorPos(&pt);
2553 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2554 NULL);
2555
2556 DestroyMenu(tab_pmenu);
2557
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 if (rval > 0)
2560 {
2561 TCHITTESTINFO htinfo;
2562 int idx;
2563
2564 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2565 return;
2566
2567 htinfo.pt.x = pt.x;
2568 htinfo.pt.y = pt.y;
2569 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2570 if (idx == -1)
2571 idx = 0;
2572 else
2573 idx += 1;
2574
2575 send_tabline_menu_event(idx, (int)rval);
2576 }
2577}
2578
2579/*
2580 * Show or hide the tabline.
2581 */
2582 void
2583gui_mch_show_tabline(int showit)
2584{
2585 if (s_tabhwnd == NULL)
2586 return;
2587
2588 if (!showit != !showing_tabline)
2589 {
2590 if (showit)
2591 ShowWindow(s_tabhwnd, SW_SHOW);
2592 else
2593 ShowWindow(s_tabhwnd, SW_HIDE);
2594 showing_tabline = showit;
2595 }
2596}
2597
2598/*
2599 * Return TRUE when tabline is displayed.
2600 */
2601 int
2602gui_mch_showing_tabline(void)
2603{
2604 return s_tabhwnd != NULL && showing_tabline;
2605}
2606
2607/*
2608 * Update the labels of the tabline.
2609 */
2610 void
2611gui_mch_update_tabline(void)
2612{
2613 tabpage_T *tp;
2614 TCITEM tie;
2615 int nr = 0;
2616 int curtabidx = 0;
2617 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002618 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002619
2620 if (s_tabhwnd == NULL)
2621 return;
2622
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002623 // Enable unicode support
2624 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625
2626 tie.mask = TCIF_TEXT;
2627 tie.iImage = -1;
2628
Bram Moolenaar734a8672019-12-02 22:49:38 +01002629 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002630 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2631
Bram Moolenaar734a8672019-12-02 22:49:38 +01002632 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002633 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2634 {
2635 if (tp == curtab)
2636 curtabidx = nr;
2637
2638 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2639 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002640 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002641 tie.pszText = "-Empty-";
2642 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2643 tabadded = 1;
2644 }
2645
2646 get_tabline_label(tp, FALSE);
2647 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002648
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002649 wstr = enc_to_utf16(NameBuff, NULL);
2650 if (wstr != NULL)
2651 {
2652 TCITEMW tiw;
2653
2654 tiw.mask = TCIF_TEXT;
2655 tiw.iImage = -1;
2656 tiw.pszText = wstr;
2657 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2658 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002659 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002660 }
2661
Bram Moolenaar734a8672019-12-02 22:49:38 +01002662 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2664 TabCtrl_DeleteItem(s_tabhwnd, nr);
2665
2666 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2667 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2668
Bram Moolenaar734a8672019-12-02 22:49:38 +01002669 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2671 RedrawWindow(s_tabhwnd, NULL, NULL,
2672 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2673
2674 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2675 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2676}
2677
2678/*
2679 * Set the current tab to "nr". First tab is 1.
2680 */
2681 void
2682gui_mch_set_curtab(int nr)
2683{
2684 if (s_tabhwnd == NULL)
2685 return;
2686
2687 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2688 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2689}
2690
2691#endif
2692
2693/*
2694 * ":simalt" command.
2695 */
2696 void
2697ex_simalt(exarg_T *eap)
2698{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002699 char_u *keys = eap->arg;
2700 int fill_typebuf = FALSE;
2701 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702
2703 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2704 while (*keys)
2705 {
2706 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002707 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2709 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002710 fill_typebuf = TRUE;
2711 }
2712 if (fill_typebuf)
2713 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002714 // Put a NOP in the typeahead buffer so that the message will get
2715 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002716 key_name[0] = K_SPECIAL;
2717 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002718 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002719 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002720#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002721 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002722#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002723 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724 }
2725}
2726
2727/*
2728 * Create the find & replace dialogs.
2729 * You can't have both at once: ":find" when replace is showing, destroys
2730 * the replace dialog first, and the other way around.
2731 */
2732#ifdef MSWIN_FIND_REPLACE
2733 static void
2734initialise_findrep(char_u *initial_string)
2735{
2736 int wword = FALSE;
2737 int mcase = !p_ic;
2738 char_u *entry_text;
2739
Bram Moolenaar734a8672019-12-02 22:49:38 +01002740 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002741 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2742
2743 s_findrep_struct.hwndOwner = s_hwnd;
2744 s_findrep_struct.Flags = FR_DOWN;
2745 if (mcase)
2746 s_findrep_struct.Flags |= FR_MATCHCASE;
2747 if (wword)
2748 s_findrep_struct.Flags |= FR_WHOLEWORD;
2749 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002750 {
2751 WCHAR *p = enc_to_utf16(entry_text, NULL);
2752 if (p != NULL)
2753 {
2754 int len = s_findrep_struct.wFindWhatLen - 1;
2755
2756 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2757 s_findrep_struct.lpstrFindWhat[len] = NUL;
2758 vim_free(p);
2759 }
2760 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 vim_free(entry_text);
2762}
2763#endif
2764
2765 static void
2766set_window_title(HWND hwnd, char *title)
2767{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002768 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002769 {
2770 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002771
Bram Moolenaar734a8672019-12-02 22:49:38 +01002772 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2774 if (wbuf != NULL)
2775 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002776 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002778 }
2779 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002780 else
2781 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782}
2783
2784 void
2785gui_mch_find_dialog(exarg_T *eap)
2786{
2787#ifdef MSWIN_FIND_REPLACE
2788 if (s_findrep_msg != 0)
2789 {
2790 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2791 DestroyWindow(s_findrep_hwnd);
2792
2793 if (!IsWindow(s_findrep_hwnd))
2794 {
2795 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002796 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002797 }
2798
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002799 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002800 (void)SetFocus(s_findrep_hwnd);
2801
2802 s_findrep_is_find = TRUE;
2803 }
2804#endif
2805}
2806
2807
2808 void
2809gui_mch_replace_dialog(exarg_T *eap)
2810{
2811#ifdef MSWIN_FIND_REPLACE
2812 if (s_findrep_msg != 0)
2813 {
2814 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2815 DestroyWindow(s_findrep_hwnd);
2816
2817 if (!IsWindow(s_findrep_hwnd))
2818 {
2819 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002820 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002821 }
2822
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002823 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 (void)SetFocus(s_findrep_hwnd);
2825
2826 s_findrep_is_find = FALSE;
2827 }
2828#endif
2829}
2830
2831
2832/*
2833 * Set visibility of the pointer.
2834 */
2835 void
2836gui_mch_mousehide(int hide)
2837{
2838 if (hide != gui.pointer_hidden)
2839 {
2840 ShowCursor(!hide);
2841 gui.pointer_hidden = hide;
2842 }
2843}
2844
2845#ifdef FEAT_MENU
2846 static void
2847gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2848{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002849 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850 gui_mch_mousehide(FALSE);
2851
2852 (void)TrackPopupMenu(
2853 (HMENU)menu->submenu_id,
2854 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2855 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002856 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002857 s_hwnd,
2858 NULL);
2859 /*
2860 * NOTE: The pop-up menu can eat the mouse up event.
2861 * We deal with this in normal.c.
2862 */
2863}
2864#endif
2865
2866/*
2867 * Got a message when the system will go down.
2868 */
2869 static void
2870_OnEndSession(void)
2871{
2872 getout_preserve_modified(1);
2873}
2874
2875/*
2876 * Get this message when the user clicks on the cross in the top right corner
2877 * of a Windows95 window.
2878 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002880_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881{
2882 gui_shell_closed();
2883}
2884
2885/*
2886 * Get a message when the window is being destroyed.
2887 */
2888 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002889_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002890{
2891 if (!destroying)
2892 _OnClose(hwnd);
2893}
2894
2895 static void
2896_OnPaint(
2897 HWND hwnd)
2898{
2899 if (!IsMinimized(hwnd))
2900 {
2901 PAINTSTRUCT ps;
2902
Bram Moolenaar734a8672019-12-02 22:49:38 +01002903 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905
Bram Moolenaar734a8672019-12-02 22:49:38 +01002906 // prevent multi-byte characters from misprinting on an invalid
2907 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002908 if (has_mbyte)
2909 {
2910 RECT rect;
2911
2912 GetClientRect(hwnd, &rect);
2913 ps.rcPaint.left = rect.left;
2914 ps.rcPaint.right = rect.right;
2915 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002916
2917 if (!IsRectEmpty(&ps.rcPaint))
2918 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2920 ps.rcPaint.right - ps.rcPaint.left + 1,
2921 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2922 }
2923
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924 EndPaint(hwnd, &ps);
2925 }
2926}
2927
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928 static void
2929_OnSize(
2930 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002931 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932 int cx,
2933 int cy)
2934{
K.Takatac81e9bf2022-01-16 14:15:49 +00002935 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936 {
2937 gui_resize_shell(cx, cy);
2938
Bram Moolenaar734a8672019-12-02 22:49:38 +01002939 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941 }
2942}
2943
2944 static void
2945_OnSetFocus(
2946 HWND hwnd,
2947 HWND hwndOldFocus)
2948{
2949 gui_focus_change(TRUE);
2950 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002951 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002952}
2953
2954 static void
2955_OnKillFocus(
2956 HWND hwnd,
2957 HWND hwndNewFocus)
2958{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002959 if (destroying)
2960 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002961 gui_focus_change(FALSE);
2962 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002963 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964}
2965
2966/*
2967 * Get a message when the user switches back to vim
2968 */
2969 static LRESULT
2970_OnActivateApp(
2971 HWND hwnd,
2972 BOOL fActivate,
2973 DWORD dwThreadId)
2974{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002975 // we call gui_focus_change() in _OnSetFocus()
2976 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002977 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002978}
2979
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980 void
2981gui_mch_destroy_scrollbar(scrollbar_T *sb)
2982{
2983 DestroyWindow(sb->id);
2984}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985
2986/*
2987 * Get current mouse coordinates in text window.
2988 */
2989 void
2990gui_mch_getmouse(int *x, int *y)
2991{
2992 RECT rct;
2993 POINT mp;
2994
2995 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002996 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002997 *x = (int)(mp.x - rct.left);
2998 *y = (int)(mp.y - rct.top);
2999}
3000
3001/*
3002 * Move mouse pointer to character at (x, y).
3003 */
3004 void
3005gui_mch_setmouse(int x, int y)
3006{
3007 RECT rct;
3008
3009 (void)GetWindowRect(s_textArea, &rct);
3010 (void)SetCursorPos(x + gui.border_offset + rct.left,
3011 y + gui.border_offset + rct.top);
3012}
3013
3014 static void
3015gui_mswin_get_valid_dimensions(
3016 int w,
3017 int h,
3018 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003019 int *valid_h,
3020 int *cols,
3021 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003022{
3023 int base_width, base_height;
3024
3025 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003026 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3027 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003028 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003029 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3030 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3031 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3032 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003033 *cols = (w - base_width) / gui.char_width;
3034 *rows = (h - base_height) / gui.char_height;
3035 *valid_w = base_width + *cols * gui.char_width;
3036 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003037}
3038
3039 void
3040gui_mch_flash(int msec)
3041{
3042 RECT rc;
3043
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003044#if defined(FEAT_DIRECTX)
3045 if (IS_ENABLE_DIRECTX())
3046 DWriteContext_Flush(s_dwc);
3047#endif
3048
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003049 /*
3050 * Note: InvertRect() excludes right and bottom of rectangle.
3051 */
3052 rc.left = 0;
3053 rc.top = 0;
3054 rc.right = gui.num_cols * gui.char_width;
3055 rc.bottom = gui.num_rows * gui.char_height;
3056 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003057 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003058
Bram Moolenaar734a8672019-12-02 22:49:38 +01003059 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060
3061 InvertRect(s_hdc, &rc);
3062}
3063
3064/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003065 * Check if the specified point is on-screen. (multi-monitor aware)
3066 */
3067 static BOOL
3068is_point_onscreen(int x, int y)
3069{
3070 POINT pt = {x, y};
3071
3072 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3073}
3074
3075/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003076 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003077 *
3078 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3079 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3080 * only works when the whole window is on-screen.
3081 */
3082 static BOOL
3083is_window_onscreen(HWND hwnd)
3084{
3085 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003086 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003087
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003088 GetClientRect(hwnd, &rc);
3089 p1.x = rc.left;
3090 p1.y = rc.top;
3091 p2.x = rc.right - 1;
3092 p2.y = rc.bottom - 1;
3093 ClientToScreen(hwnd, &p1);
3094 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003095
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003096 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003097 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003098 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003099 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003100 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003101 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003102 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003103 return FALSE;
3104 return TRUE;
3105}
3106
3107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003108 * Return flags used for scrolling.
3109 * The SW_INVALIDATE is required when part of the window is covered or
3110 * off-screen. Refer to MS KB Q75236.
3111 */
3112 static int
3113get_scroll_flags(void)
3114{
3115 HWND hwnd;
3116 RECT rcVim, rcOther, rcDest;
3117
Bram Moolenaar185577e2020-10-29 20:08:21 +01003118 // Check if the window is (partly) off-screen.
3119 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003120 return SW_INVALIDATE;
3121
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003122 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003123 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3125 if (IsWindowVisible(hwnd))
3126 {
3127 GetWindowRect(hwnd, &rcOther);
3128 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3129 return SW_INVALIDATE;
3130 }
3131 return 0;
3132}
3133
3134/*
3135 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3136 * may not be scrolled out properly.
3137 * For gVim, when _OnScroll() is repeated, the character at the
3138 * previous cursor position may be left drawn after scroll.
3139 * The problem can be avoided by calling GetPixel() to get a pixel in
3140 * the region before ScrollWindowEx().
3141 */
3142 static void
3143intel_gpu_workaround(void)
3144{
3145 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3146}
3147
3148/*
3149 * Delete the given number of lines from the given row, scrolling up any
3150 * text further down within the scroll region.
3151 */
3152 void
3153gui_mch_delete_lines(
3154 int row,
3155 int num_lines)
3156{
3157 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003158
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003159 rc.left = FILL_X(gui.scroll_region_left);
3160 rc.right = FILL_X(gui.scroll_region_right + 1);
3161 rc.top = FILL_Y(row);
3162 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3163
Bram Moolenaar92467d32017-12-05 13:22:16 +01003164#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003165 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003167 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003169 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170#endif
3171 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003172#if defined(FEAT_DIRECTX)
3173 if (IS_ENABLE_DIRECTX())
3174 DWriteContext_Flush(s_dwc);
3175#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 intel_gpu_workaround();
3177 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003179 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003180 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003181
Bram Moolenaar734a8672019-12-02 22:49:38 +01003182 // This seems to be required to avoid the cursor disappearing when
3183 // scrolling such that the cursor ends up in the top-left character on
3184 // the screen... But why? (Webb)
3185 // It's probably fixed by disabling drawing the cursor while scrolling.
3186 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187
3188 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3189 gui.scroll_region_left,
3190 gui.scroll_region_bot, gui.scroll_region_right);
3191}
3192
3193/*
3194 * Insert the given number of lines before the given row, scrolling down any
3195 * following text within the scroll region.
3196 */
3197 void
3198gui_mch_insert_lines(
3199 int row,
3200 int num_lines)
3201{
3202 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003203
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 rc.left = FILL_X(gui.scroll_region_left);
3205 rc.right = FILL_X(gui.scroll_region_right + 1);
3206 rc.top = FILL_Y(row);
3207 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208
3209#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003210 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003211 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003212 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003213 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003214 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003215#endif
3216 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003217#if defined(FEAT_DIRECTX)
3218 if (IS_ENABLE_DIRECTX())
3219 DWriteContext_Flush(s_dwc);
3220#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003221 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003222 // The SW_INVALIDATE is required when part of the window is covered or
3223 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003224 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003226 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003227 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229 gui_clear_block(row, gui.scroll_region_left,
3230 row + num_lines - 1, gui.scroll_region_right);
3231}
3232
3233
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003235gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236{
3237#if defined(FEAT_DIRECTX)
3238 DWriteContext_Close(s_dwc);
3239 DWrite_Final();
3240 s_dwc = NULL;
3241#endif
3242
3243 ReleaseDC(s_textArea, s_hdc);
3244 DeleteObject(s_brush);
3245
3246#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003247 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3249#endif
3250
Bram Moolenaar734a8672019-12-02 22:49:38 +01003251 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252 if (s_hwnd != NULL)
3253 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003254 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255 DestroyWindow(s_hwnd);
3256 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257}
3258
3259 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003260logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261{
3262 char *p;
3263 char *res;
3264 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003265 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003266 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003267 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003269 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3270 if (font_name == NULL)
3271 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003273 quality_name = quality_id2name((int)lf.lfQuality);
3274
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003275 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003276 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003277 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278 if (res != NULL)
3279 {
3280 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003281 // make a normal font string out of the lf thing:
3282 points = pixels_to_points(
3283 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3284 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3285 sprintf((char *)p, "%s:h%d", font_name, points);
3286 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003287 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003288 while (*p)
3289 {
3290 if (*p == ' ')
3291 *p = '_';
3292 ++p;
3293 }
3294 if (lf.lfItalic)
3295 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003296 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003297 STRCAT(p, ":b");
3298 if (lf.lfUnderline)
3299 STRCAT(p, ":u");
3300 if (lf.lfStrikeOut)
3301 STRCAT(p, ":s");
3302 if (charset_name != NULL)
3303 {
3304 STRCAT(p, ":c");
3305 STRCAT(p, charset_name);
3306 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003307 if (quality_name != NULL)
3308 {
3309 STRCAT(p, ":q");
3310 STRCAT(p, quality_name);
3311 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003312 }
3313
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003314 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003315 return (char_u *)res;
3316}
3317
3318
3319#ifdef FEAT_MBYTE_IME
3320/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003321 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003322 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003323 */
3324 static void
3325update_im_font(void)
3326{
K.Takatac81e9bf2022-01-16 14:15:49 +00003327 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328
3329 if (p_guifontwide != NULL && *p_guifontwide != NUL
3330 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003331 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 norm_logfont = lf_wide;
3333 else
3334 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003335
3336 lf = norm_logfont;
3337 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3338 // Work around when PerMonitorV2 is not enabled in the process level.
3339 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3340 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341}
3342#endif
3343
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344/*
3345 * Handler of gui.wide_font (p_guifontwide) changed notification.
3346 */
3347 void
3348gui_mch_wide_font_changed(void)
3349{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003350 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003352#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003354#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355
3356 gui_mch_free_font(gui.wide_ital_font);
3357 gui.wide_ital_font = NOFONT;
3358 gui_mch_free_font(gui.wide_bold_font);
3359 gui.wide_bold_font = NOFONT;
3360 gui_mch_free_font(gui.wide_boldital_font);
3361 gui.wide_boldital_font = NOFONT;
3362
3363 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003364 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365 {
3366 if (!lf.lfItalic)
3367 {
3368 lf.lfItalic = TRUE;
3369 gui.wide_ital_font = get_font_handle(&lf);
3370 lf.lfItalic = FALSE;
3371 }
3372 if (lf.lfWeight < FW_BOLD)
3373 {
3374 lf.lfWeight = FW_BOLD;
3375 gui.wide_bold_font = get_font_handle(&lf);
3376 if (!lf.lfItalic)
3377 {
3378 lf.lfItalic = TRUE;
3379 gui.wide_boldital_font = get_font_handle(&lf);
3380 }
3381 }
3382 }
3383}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384
3385/*
3386 * Initialise vim to use the font with the given name.
3387 * Return FAIL if the font could not be loaded, OK otherwise.
3388 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003390gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391{
K.Takatac81e9bf2022-01-16 14:15:49 +00003392 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393 GuiFont font = NOFONT;
3394 char_u *p;
3395
Bram Moolenaar734a8672019-12-02 22:49:38 +01003396 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003398 {
3399 lfOrig = lf;
3400 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003402 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 if (font == NOFONT)
3404 return FAIL;
3405
3406 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003407 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003408#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003409 norm_logfont = lf;
3410 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003411 if (!s_in_dpichanged)
3412 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413#endif
3414 gui_mch_free_font(gui.norm_font);
3415 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003416 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417 GetFontSize(font);
3418
K.Takatac81e9bf2022-01-16 14:15:49 +00003419 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 if (p != NULL)
3421 {
3422 hl_set_font_name(p);
3423
Bram Moolenaar734a8672019-12-02 22:49:38 +01003424 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003425 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3426 {
3427 vim_free(p_guifont);
3428 p_guifont = p;
3429 }
3430 else
3431 vim_free(p);
3432 }
3433
3434 gui_mch_free_font(gui.ital_font);
3435 gui.ital_font = NOFONT;
3436 gui_mch_free_font(gui.bold_font);
3437 gui.bold_font = NOFONT;
3438 gui_mch_free_font(gui.boldital_font);
3439 gui.boldital_font = NOFONT;
3440
3441 if (!lf.lfItalic)
3442 {
3443 lf.lfItalic = TRUE;
3444 gui.ital_font = get_font_handle(&lf);
3445 lf.lfItalic = FALSE;
3446 }
3447 if (lf.lfWeight < FW_BOLD)
3448 {
3449 lf.lfWeight = FW_BOLD;
3450 gui.bold_font = get_font_handle(&lf);
3451 if (!lf.lfItalic)
3452 {
3453 lf.lfItalic = TRUE;
3454 gui.boldital_font = get_font_handle(&lf);
3455 }
3456 }
3457
3458 return OK;
3459}
3460
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461/*
3462 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003463 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 */
3465 int
3466gui_mch_maximized(void)
3467{
3468 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003469 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470
3471 wp.length = sizeof(WINDOWPLACEMENT);
3472 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003473 {
3474 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003476 && wp.flags == WPF_RESTORETOMAXIMIZED))
3477 return TRUE;
3478 if (wp.showCmd == SW_SHOWMINIMIZED)
3479 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003481 // Assume the window is snapped when the sizes from two APIs differ.
3482 GetWindowRect(s_hwnd, &rc);
3483 if ((rc.right - rc.left !=
3484 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3485 || (rc.bottom - rc.top !=
3486 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3487 return TRUE;
3488 }
3489 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003490}
3491
3492/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003493 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3494 * is set. Compute the new Rows and Columns. This is like resizing the
3495 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003496 */
3497 void
3498gui_mch_newfont(void)
3499{
3500 RECT rect;
3501
3502 GetWindowRect(s_hwnd, &rect);
3503 if (win_socket_id == 0)
3504 {
3505 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003506 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3507 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003509 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3510 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3511 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3512 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 }
3514 else
3515 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003516 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003517 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003518 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519 }
3520}
3521
3522/*
3523 * Set the window title
3524 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003525 void
3526gui_mch_settitle(
3527 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003528 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003529{
3530 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3531}
3532
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003533#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003534// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3535// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003536static LPCSTR mshape_idcs[] =
3537{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003538 IDC_ARROW, // arrow
3539 MAKEINTRESOURCE(0), // blank
3540 IDC_IBEAM, // beam
3541 IDC_SIZENS, // updown
3542 IDC_SIZENS, // udsizing
3543 IDC_SIZEWE, // leftright
3544 IDC_SIZEWE, // lrsizing
3545 IDC_WAIT, // busy
3546 IDC_NO, // no
3547 IDC_ARROW, // crosshair
3548 IDC_ARROW, // hand1
3549 IDC_ARROW, // hand2
3550 IDC_ARROW, // pencil
3551 IDC_ARROW, // question
3552 IDC_ARROW, // right-arrow
3553 IDC_UPARROW, // up-arrow
3554 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555};
3556
3557 void
3558mch_set_mouse_shape(int shape)
3559{
3560 LPCSTR idc;
3561
3562 if (shape == MSHAPE_HIDE)
3563 ShowCursor(FALSE);
3564 else
3565 {
3566 if (shape >= MSHAPE_NUMBERED)
3567 idc = IDC_ARROW;
3568 else
3569 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003570 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571 if (!p_mh)
3572 {
3573 POINT mp;
3574
Bram Moolenaar734a8672019-12-02 22:49:38 +01003575 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003576 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577 (void)SetCursorPos(mp.x, mp.y);
3578 ShowCursor(TRUE);
3579 }
3580 }
3581}
3582#endif
3583
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003584#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003585/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003586 * Wide version of convert_filter().
3587 */
3588 static WCHAR *
3589convert_filterW(char_u *s)
3590{
3591 char_u *tmp;
3592 int len;
3593 WCHAR *res;
3594
3595 tmp = convert_filter(s);
3596 if (tmp == NULL)
3597 return NULL;
3598 len = (int)STRLEN(s) + 3;
3599 res = enc_to_utf16(tmp, &len);
3600 vim_free(tmp);
3601 return res;
3602}
3603
3604/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003605 * Pop open a file browser and return the file selected, in allocated memory,
3606 * or NULL if Cancel is hit.
3607 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3608 * title - Title message for the file browser dialog.
3609 * dflt - Default name of file.
3610 * ext - Default extension to be added to files without extensions.
3611 * initdir - directory in which to open the browser (NULL = current dir)
3612 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003613 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003614 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003615gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 int saving,
3617 char_u *title,
3618 char_u *dflt,
3619 char_u *ext,
3620 char_u *initdir,
3621 char_u *filter)
3622{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003623 // We always use the wide function. This means enc_to_utf16() must work,
3624 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003625 OPENFILENAMEW fileStruct;
3626 WCHAR fileBuf[MAXPATHL];
3627 WCHAR *wp;
3628 int i;
3629 WCHAR *titlep = NULL;
3630 WCHAR *extp = NULL;
3631 WCHAR *initdirp = NULL;
3632 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003633 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003634 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635
3636 if (dflt == NULL)
3637 fileBuf[0] = NUL;
3638 else
3639 {
3640 wp = enc_to_utf16(dflt, NULL);
3641 if (wp == NULL)
3642 fileBuf[0] = NUL;
3643 else
3644 {
3645 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3646 fileBuf[i] = wp[i];
3647 fileBuf[i] = NUL;
3648 vim_free(wp);
3649 }
3650 }
3651
Bram Moolenaar734a8672019-12-02 22:49:38 +01003652 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 filterp = convert_filterW(filter);
3654
Bram Moolenaara80faa82020-04-12 19:37:17 +02003655 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003656# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003658 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003659# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003661# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663 if (title != NULL)
3664 titlep = enc_to_utf16(title, NULL);
3665 fileStruct.lpstrTitle = titlep;
3666
3667 if (ext != NULL)
3668 extp = enc_to_utf16(ext, NULL);
3669 fileStruct.lpstrDefExt = extp;
3670
3671 fileStruct.lpstrFile = fileBuf;
3672 fileStruct.nMaxFile = MAXPATHL;
3673 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003674 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3675 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003676 if (initdir != NULL && *initdir != NUL)
3677 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003678 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679 initdirp = enc_to_utf16(initdir, NULL);
3680 if (initdirp != NULL)
3681 {
3682 for (wp = initdirp; *wp != NUL; ++wp)
3683 if (*wp == '/')
3684 *wp = '\\';
3685 }
3686 fileStruct.lpstrInitialDir = initdirp;
3687 }
3688
3689 /*
3690 * TODO: Allow selection of multiple files. Needs another arg to this
3691 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3692 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3693 * files that don't exist yet, so I haven't put it in. What about
3694 * OFN_PATHMUSTEXIST?
3695 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3696 */
3697 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003698# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003699 if (curbuf->b_p_bin)
3700 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003701# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003703 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003705 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706
3707 vim_free(filterp);
3708 vim_free(initdirp);
3709 vim_free(titlep);
3710 vim_free(extp);
3711
K.Takata14b8d6a2022-01-20 15:05:22 +00003712 if (!ret)
3713 return NULL;
3714
3715 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003717 if (p == NULL)
3718 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003719
Bram Moolenaar734a8672019-12-02 22:49:38 +01003720 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003721 SetFocus(s_hwnd);
3722
Bram Moolenaar734a8672019-12-02 22:49:38 +01003723 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003724 q = vim_strsave(shorten_fname1(p));
3725 vim_free(p);
3726 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003727}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728
3729
3730/*
3731 * Convert the string s to the proper format for a filter string by replacing
3732 * the \t and \n delimiters with \0.
3733 * Returns the converted string in allocated memory.
3734 *
3735 * Keep in sync with convert_filterW() above!
3736 */
3737 static char_u *
3738convert_filter(char_u *s)
3739{
3740 char_u *res;
3741 unsigned s_len = (unsigned)STRLEN(s);
3742 unsigned i;
3743
3744 res = alloc(s_len + 3);
3745 if (res != NULL)
3746 {
3747 for (i = 0; i < s_len; ++i)
3748 if (s[i] == '\t' || s[i] == '\n')
3749 res[i] = '\0';
3750 else
3751 res[i] = s[i];
3752 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003753 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003754 res[s_len + 1] = NUL;
3755 res[s_len + 2] = NUL;
3756 }
3757 return res;
3758}
3759
3760/*
3761 * Select a directory.
3762 */
3763 char_u *
3764gui_mch_browsedir(char_u *title, char_u *initdir)
3765{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003766 // We fake this: Use a filter that doesn't select anything and a default
3767 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3769 initdir, (char_u *)_("Directory\t*.nothing\n"));
3770}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003771#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 static void
3774_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003775 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 HDROP hDrop)
3777{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003778#define BUFPATHLEN _MAX_PATH
3779#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 char szFile[BUFPATHLEN];
3782 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3783 UINT i;
3784 char_u **fnames;
3785 POINT pt;
3786 int_u modifiers = 0;
3787
Bram Moolenaar734a8672019-12-02 22:49:38 +01003788 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789 DragQueryPoint(hDrop, &pt);
3790 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3791
3792 reset_VIsual();
3793
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003794 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795
3796 if (fnames != NULL)
3797 for (i = 0; i < cFiles; ++i)
3798 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3800 fnames[i] = utf16_to_enc(wszFile, NULL);
3801 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 {
3803 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3804 fnames[i] = vim_strsave((char_u *)szFile);
3805 }
3806 }
3807
3808 DragFinish(hDrop);
3809
3810 if (fnames != NULL)
3811 {
LemonBoy45684c62022-04-24 15:46:42 +01003812 int kbd_modifiers = get_active_modifiers();
3813
3814 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003816 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003818 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 modifiers |= MOUSE_ALT;
3820
3821 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3822
3823 s_need_activate = TRUE;
3824 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825}
3826
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003827 static int
3828_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003829 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830 HWND hwndCtl,
3831 UINT code,
3832 int pos)
3833{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003834 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 scrollbar_T *sb, *sb_info;
3836 long val;
3837 int dragging = FALSE;
3838 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 SCROLLINFO si;
3840
3841 si.cbSize = sizeof(si);
3842 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843
3844 sb = gui_mswin_find_scrollbar(hwndCtl);
3845 if (sb == NULL)
3846 return 0;
3847
Bram Moolenaar734a8672019-12-02 22:49:38 +01003848 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003849 {
3850 /*
3851 * Careful: need to get scrollbar info out of first (left) scrollbar
3852 * for window, but keep real scrollbar too because we must pass it to
3853 * gui_drag_scrollbar().
3854 */
3855 sb_info = &sb->wp->w_scrollbars[0];
3856 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003857 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 sb_info = sb;
3859 val = sb_info->value;
3860
3861 switch (code)
3862 {
3863 case SB_THUMBTRACK:
3864 val = pos;
3865 dragging = TRUE;
3866 if (sb->scroll_shift > 0)
3867 val <<= sb->scroll_shift;
3868 break;
3869 case SB_LINEDOWN:
3870 val++;
3871 break;
3872 case SB_LINEUP:
3873 val--;
3874 break;
3875 case SB_PAGEDOWN:
3876 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3877 break;
3878 case SB_PAGEUP:
3879 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3880 break;
3881 case SB_TOP:
3882 val = 0;
3883 break;
3884 case SB_BOTTOM:
3885 val = sb_info->max;
3886 break;
3887 case SB_ENDSCROLL:
3888 if (prev_code == SB_THUMBTRACK)
3889 {
3890 /*
3891 * "pos" only gives us 16-bit data. In case of large file,
3892 * use GetScrollPos() which returns 32-bit. Unfortunately it
3893 * is not valid while the scrollbar is being dragged.
3894 */
3895 val = GetScrollPos(hwndCtl, SB_CTL);
3896 if (sb->scroll_shift > 0)
3897 val <<= sb->scroll_shift;
3898 }
3899 break;
3900
3901 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003902 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003903 return 0;
3904 }
3905 prev_code = code;
3906
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3908 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909
3910 /*
3911 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3912 */
3913 if (sb->wp != NULL)
3914 {
3915 scrollbar_T *sba = sb->wp->w_scrollbars;
3916 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3917
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919 }
3920
Bram Moolenaar734a8672019-12-02 22:49:38 +01003921 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 s_busy_processing = TRUE;
3923
Bram Moolenaar734a8672019-12-02 22:49:38 +01003924 // When "allow_scrollbar" is FALSE still need to remember the new
3925 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003926 dont_scroll = !allow_scrollbar;
3927
Bram Moolenaara338adc2018-01-31 20:51:47 +01003928 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003930 mch_enable_flush();
3931 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003932
3933 s_busy_processing = FALSE;
3934 dont_scroll = dont_scroll_save;
3935
3936 return 0;
3937}
3938
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#ifdef FEAT_XPM_W32
3941# include "xpm_w32.h"
3942#endif
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar734a8672019-12-02 22:49:38 +01003945// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#define TEAROFF_PADDING_X 2
3947#define TEAROFF_BUTTON_PAD_X 8
3948#define TEAROFF_MIN_WIDTH 200
3949#define TEAROFF_SUBMENU_LABEL ">>"
3950#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3951
3952
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003953#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954# define ID_BEVAL_TOOLTIP 200
3955# define BEVAL_TEXT_LEN MAXPATHL
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003958static UINT_PTR beval_timer_id = 0;
3959static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003960#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003961
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003962
Bram Moolenaar734a8672019-12-02 22:49:38 +01003963// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
3965#ifdef FEAT_MENU
3966static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969/*
3970 * Use the system font for dialogs and tear-off menus. Remove this line to
3971 * use DLG_FONT_NAME.
3972 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003973#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#define VIM_CLASSW L"Vim"
3977
Bram Moolenaar734a8672019-12-02 22:49:38 +01003978// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3979// thus there should be room for every dialog. For tearoffs it's made bigger
3980// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#define DLG_ALLOC_SIZE 16 * 1024
3982
3983/*
3984 * stuff for dialogs, menus, tearoffs etc.
3985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986static PWORD
3987add_dialog_element(
3988 PWORD p,
3989 DWORD lStyle,
3990 WORD x,
3991 WORD y,
3992 WORD w,
3993 WORD h,
3994 WORD Id,
3995 WORD clss,
3996 const char *caption);
3997static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003998static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003999#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002static void get_dialog_font_metrics(void);
4003
4004static int dialog_default_button = -1;
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#ifdef FEAT_TOOLBAR
4007static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004008static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004009static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004011#else
4012# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#endif
4014
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004015#ifdef FEAT_GUI_TABLINE
4016static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004017static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004018#endif
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_MBYTE_IME
4021static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4022static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4023#endif
4024#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4025# ifdef NOIME
4026typedef struct tagCOMPOSITIONFORM {
4027 DWORD dwStyle;
4028 POINT ptCurrentPos;
4029 RECT rcArea;
4030} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4031typedef HANDLE HIMC;
4032# endif
4033
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004034static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004035static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4036static HIMC (WINAPI *pImmGetContext)(HWND);
4037static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4038static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4039static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4040static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004041static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4042static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004043static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4044static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004045static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046static void dyn_imm_load(void);
4047#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048# define pImmGetCompositionStringW ImmGetCompositionStringW
4049# define pImmGetContext ImmGetContext
4050# define pImmAssociateContext ImmAssociateContext
4051# define pImmReleaseContext ImmReleaseContext
4052# define pImmGetOpenStatus ImmGetOpenStatus
4053# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004054# define pImmGetCompositionFontW ImmGetCompositionFontW
4055# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056# define pImmSetCompositionWindow ImmSetCompositionWindow
4057# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004058# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#endif
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#ifdef FEAT_MENU
4062/*
4063 * Figure out how high the menu bar is at the moment.
4064 */
4065 static int
4066gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004067 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 static int old_menu_height = -1;
4070
4071 RECT rc1, rc2;
4072 int num;
4073 int menu_height;
4074
4075 if (gui.menu_is_active)
4076 num = GetMenuItemCount(s_menuBar);
4077 else
4078 num = 0;
4079
4080 if (num == 0)
4081 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004082 else if (IsMinimized(s_hwnd))
4083 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004084 // The height of the menu cannot be determined while the window is
4085 // minimized. Take the previous height if the menu is changed in that
4086 // state, to avoid that Vim's vertical window size accidentally
4087 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004088 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004092 /*
4093 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4094 * seem to have been set yet, so menu wraps in default window
4095 * width which is very narrow. Instead just return height of a
4096 * single menu item. Will still be wrong when the menu really
4097 * should wrap over more than one line.
4098 */
4099 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4100 if (gui.starting)
4101 menu_height = rc1.bottom - rc1.top + 1;
4102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004104 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4105 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 }
4108
4109 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004110 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004111 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 return menu_height;
4114}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004115#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
4117
4118/*
4119 * Setup for the Intellimouse
4120 */
LemonBoyc27747e2022-05-07 12:25:40 +01004121 static long
4122mouse_vertical_scroll_step(void)
4123{
4124 UINT val;
4125 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4126 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4127 return 3; // Safe default;
4128}
4129
4130 static long
4131mouse_horizontal_scroll_step(void)
4132{
4133 UINT val;
4134 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4135 return (long)val;
4136 return 3; // Safe default;
4137}
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 static void
4140init_mouse_wheel(void)
4141{
LemonBoyc27747e2022-05-07 12:25:40 +01004142 // Get the default values for the horizontal and vertical scroll steps from
4143 // the system.
4144 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4145 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146}
4147
Bram Moolenaarae20f342019-11-05 21:09:23 +01004148/*
LemonBoya773d842022-05-10 20:54:46 +01004149 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004152_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
LemonBoy365d8f72022-05-05 19:23:07 +01004154 int button;
4155 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004156 int modifiers = 0;
4157 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004158 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4159 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaarae20f342019-11-05 21:09:23 +01004161 wp = gui_mouse_window(FIND_POPUP);
4162
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004163#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004164 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004165 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004166 cmdarg_T cap;
4167 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004168
Bram Moolenaarae20f342019-11-05 21:09:23 +01004169 // Mouse hovers over popup window, scroll it if possible.
4170 mouse_row = wp->w_winrow;
4171 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004172 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004173 if (horizontal)
4174 {
4175 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4176 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4177 }
4178 else
4179 {
4180 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4181 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4182 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004183 clear_oparg(&oa);
4184 cap.oap = &oa;
4185 nv_mousescroll(&cap);
4186 update_screen(0);
4187 setcursor();
4188 out_flush();
4189 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004190 }
4191#endif
4192
Bram Moolenaarae20f342019-11-05 21:09:23 +01004193 if (wp == NULL || !p_scf)
4194 wp = curwin;
4195
LemonBoy365d8f72022-05-05 19:23:07 +01004196 // Translate the scroll event into an event that Vim can process so that
4197 // the user has a chance to map the scrollwheel buttons.
4198 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004199 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 else
LemonBoy365d8f72022-05-05 19:23:07 +01004201 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004202
4203 kbd_modifiers = get_active_modifiers();
4204
4205 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4206 modifiers |= MOUSE_SHIFT;
4207 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4208 modifiers |= MOUSE_CTRL;
4209 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4210 modifiers |= MOUSE_ALT;
4211
LemonBoya773d842022-05-10 20:54:46 +01004212 // The cursor position is relative to the upper-left corner of the screen.
4213 pt.x = GET_X_LPARAM(lParam);
4214 pt.y = GET_Y_LPARAM(lParam);
4215 ScreenToClient(s_textArea, &pt);
4216
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004217 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218}
4219
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004220#ifdef USE_SYSMENU_FONT
4221/*
4222 * Get Menu Font.
4223 * Return OK or FAIL.
4224 */
4225 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004226gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004227{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004228 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004229
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004230 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4231 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004232 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004233 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004234 &nm,
4235 0))
4236 return FAIL;
4237 *lf = nm.lfMenuFont;
4238 return OK;
4239}
4240#endif
4241
4242
4243#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4244/*
4245 * Set the GUI tabline font to the system menu font
4246 */
4247 static void
4248set_tabline_font(void)
4249{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004250 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004251 HFONT font;
4252 HWND hwnd;
4253 HDC hdc;
4254 HFONT hfntOld;
4255 TEXTMETRIC tm;
4256
4257 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4258 return;
4259
K.Takatac81e9bf2022-01-16 14:15:49 +00004260 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004261 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004262
4263 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4264
4265 /*
4266 * Compute the height of the font used for the tab text
4267 */
4268 hwnd = GetDesktopWindow();
4269 hdc = GetWindowDC(hwnd);
4270 hfntOld = SelectFont(hdc, font);
4271
4272 GetTextMetrics(hdc, &tm);
4273
4274 SelectFont(hdc, hfntOld);
4275 ReleaseDC(hwnd, hdc);
4276
4277 /*
4278 * The space used by the tab border and the space between the tab label
4279 * and the tab border is included as 7.
4280 */
4281 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4282}
K.Takatac81e9bf2022-01-16 14:15:49 +00004283#else
4284# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004285#endif
4286
Bram Moolenaar520470a2005-06-16 21:59:56 +00004287/*
4288 * Invoked when a setting was changed.
4289 */
4290 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004291_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004292{
LemonBoy365d8f72022-05-05 19:23:07 +01004293 switch (param)
4294 {
4295 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004296 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004297 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004298 case SPI_SETWHEELSCROLLCHARS:
4299 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004300 break;
4301 case SPI_SETNONCLIENTMETRICS:
4302 set_tabline_font();
4303 break;
4304 default:
4305 break;
4306 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004307 return 0;
4308}
4309
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_NETBEANS_INTG
4311 static void
4312_OnWindowPosChanged(
4313 HWND hwnd,
4314 const LPWINDOWPOS lpwpos)
4315{
4316 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004317 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4320 || lpwpos->cx != cx || lpwpos->cy != cy))
4321 {
4322 x = lpwpos->x;
4323 y = lpwpos->y;
4324 cx = lpwpos->cx;
4325 cy = lpwpos->cy;
4326 netbeans_frame_moved(x, y);
4327 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004328 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004329 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330}
4331#endif
4332
K.Takata4f2417f2021-06-05 16:25:32 +02004333
4334static HWND hwndTip = NULL;
4335
4336 static void
4337show_sizing_tip(int cols, int rows)
4338{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004339 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004340 char buf[32];
4341
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004342 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004343 ti.hwnd = s_hwnd;
4344 ti.uId = (UINT_PTR)s_hwnd;
4345 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4346 ti.lpszText = buf;
4347 sprintf(buf, "%dx%d", cols, rows);
4348 if (hwndTip == NULL)
4349 {
4350 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4351 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4352 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4353 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4354 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4355 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4356 }
4357 else
4358 {
4359 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4360 }
4361 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4362}
4363
4364 static void
4365destroy_sizing_tip(void)
4366{
4367 if (hwndTip != NULL)
4368 {
4369 DestroyWindow(hwndTip);
4370 hwndTip = NULL;
4371 }
4372}
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 static int
4375_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 UINT fwSide,
4377 LPRECT lprc)
4378{
4379 int w, h;
4380 int valid_w, valid_h;
4381 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004382 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 w = lprc->right - lprc->left;
4385 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004386 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 w_offset = w - valid_w;
4388 h_offset = h - valid_h;
4389
4390 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4391 || fwSide == WMSZ_BOTTOMLEFT)
4392 lprc->left += w_offset;
4393 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4394 || fwSide == WMSZ_BOTTOMRIGHT)
4395 lprc->right -= w_offset;
4396
4397 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4398 || fwSide == WMSZ_TOPRIGHT)
4399 lprc->top += h_offset;
4400 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4401 || fwSide == WMSZ_BOTTOMRIGHT)
4402 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004403
4404 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return TRUE;
4406}
4407
K.Takata92000e22022-01-20 15:10:57 +00004408#ifdef FEAT_GUI_TABLINE
4409 static void
4410_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4411{
4412 if (gui_mch_showing_tabline())
4413 {
4414 POINT pt;
4415 RECT rect;
4416
4417 /*
4418 * If the cursor is on the tabline, display the tab menu
4419 */
4420 GetCursorPos(&pt);
4421 GetWindowRect(s_textArea, &rect);
4422 if (pt.y < rect.top)
4423 {
4424 show_tabline_popup_menu();
4425 return;
4426 }
4427 }
4428 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4429}
4430
4431 static void
4432_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4433{
4434 /*
4435 * If the user double clicked the tabline, create a new tab
4436 */
4437 if (gui_mch_showing_tabline())
4438 {
4439 POINT pt;
4440 RECT rect;
4441
4442 GetCursorPos(&pt);
4443 GetWindowRect(s_textArea, &rect);
4444 if (pt.y < rect.top)
4445 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4446 }
4447 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4448}
4449#endif
4450
4451 static UINT
4452_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4453{
4454 UINT result;
4455 int x, y;
4456
4457 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4458 if (result != HTCLIENT)
4459 return result;
4460
4461#ifdef FEAT_GUI_TABLINE
4462 if (gui_mch_showing_tabline())
4463 {
4464 RECT rct;
4465
4466 // If the cursor is on the GUI tabline, don't process this event
4467 GetWindowRect(s_textArea, &rct);
4468 if (yPos < rct.top)
4469 return result;
4470 }
4471#endif
4472 (void)gui_mch_get_winpos(&x, &y);
4473 xPos -= x;
4474
4475 if (xPos < 48) // <VN> TODO should use system metric?
4476 return HTBOTTOMLEFT;
4477 else
4478 return HTBOTTOMRIGHT;
4479}
4480
4481#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4482 static LRESULT
4483_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4484{
4485 switch (hdr->code)
4486 {
4487 case TTN_GETDISPINFOW:
4488 case TTN_GETDISPINFO:
4489 {
4490 char_u *str = NULL;
4491 static void *tt_text = NULL;
4492
4493 VIM_CLEAR(tt_text);
4494
4495# ifdef FEAT_GUI_TABLINE
4496 if (gui_mch_showing_tabline()
4497 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4498 {
4499 POINT pt;
4500 /*
4501 * Mouse is over the GUI tabline. Display the
4502 * tooltip for the tab under the cursor
4503 *
4504 * Get the cursor position within the tab control
4505 */
4506 GetCursorPos(&pt);
4507 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4508 {
4509 TCHITTESTINFO htinfo;
4510 int idx;
4511
4512 /*
4513 * Get the tab under the cursor
4514 */
4515 htinfo.pt.x = pt.x;
4516 htinfo.pt.y = pt.y;
4517 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4518 if (idx != -1)
4519 {
4520 tabpage_T *tp;
4521
4522 tp = find_tabpage(idx + 1);
4523 if (tp != NULL)
4524 {
4525 get_tabline_label(tp, TRUE);
4526 str = NameBuff;
4527 }
4528 }
4529 }
4530 }
4531# endif
4532# ifdef FEAT_TOOLBAR
4533# ifdef FEAT_GUI_TABLINE
4534 else
4535# endif
4536 {
4537 UINT idButton;
4538 vimmenu_T *pMenu;
4539
4540 idButton = (UINT) hdr->idFrom;
4541 pMenu = gui_mswin_find_menu(root_menu, idButton);
4542 if (pMenu)
4543 str = pMenu->strings[MENU_INDEX_TIP];
4544 }
4545# endif
4546 if (str == NULL)
4547 break;
4548
4549 // Set the maximum width, this also enables using \n for
4550 // line break.
4551 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4552
4553 if (hdr->code == TTN_GETDISPINFOW)
4554 {
4555 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4556
4557 tt_text = enc_to_utf16(str, NULL);
4558 lpdi->lpszText = tt_text;
4559 // can't show tooltip if failed
4560 }
4561 else
4562 {
4563 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4564
4565 if (STRLEN(str) < sizeof(lpdi->szText)
4566 || ((tt_text = vim_strsave(str)) == NULL))
4567 vim_strncpy((char_u *)lpdi->szText, str,
4568 sizeof(lpdi->szText) - 1);
4569 else
4570 lpdi->lpszText = tt_text;
4571 }
4572 }
4573 break;
4574
4575# ifdef FEAT_GUI_TABLINE
4576 case TCN_SELCHANGE:
4577 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4578 {
4579 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4580 return 0L;
4581 }
4582 break;
4583
4584 case NM_RCLICK:
4585 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4586 {
4587 show_tabline_popup_menu();
4588 return 0L;
4589 }
4590 break;
4591# endif
4592
4593 default:
4594 break;
4595 }
4596 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4597}
4598#endif
4599
4600#if defined(MENUHINTS) && defined(FEAT_MENU)
4601 static LRESULT
4602_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4603{
4604 if (((UINT) HIWORD(wParam)
4605 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4606 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004607 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004608 {
4609 UINT idButton;
4610 vimmenu_T *pMenu;
4611 static int did_menu_tip = FALSE;
4612
4613 if (did_menu_tip)
4614 {
4615 msg_clr_cmdline();
4616 setcursor();
4617 out_flush();
4618 did_menu_tip = FALSE;
4619 }
4620
4621 idButton = (UINT)LOWORD(wParam);
4622 pMenu = gui_mswin_find_menu(root_menu, idButton);
4623 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4624 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4625 {
4626 ++msg_hist_off;
4627 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4628 --msg_hist_off;
4629 setcursor();
4630 out_flush();
4631 did_menu_tip = TRUE;
4632 }
4633 return 0L;
4634 }
4635 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4636}
4637#endif
4638
K.Takatac81e9bf2022-01-16 14:15:49 +00004639 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004640_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004641{
4642 s_dpi = ydpi;
4643 s_in_dpichanged = TRUE;
4644 //TRACE("DPI: %d", ydpi);
4645
4646 update_scrollbar_size();
4647 update_toolbar_size();
4648 set_tabline_font();
4649
4650 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4651 gui_get_wide_font();
4652 gui_mswin_get_menu_height(FALSE);
4653#ifdef FEAT_MBYTE_IME
4654 im_set_position(gui.row, gui.col);
4655#endif
4656 InvalidateRect(hwnd, NULL, TRUE);
4657
4658 s_in_dpichanged = FALSE;
4659 return 0L;
4660}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662
4663 static LRESULT CALLBACK
4664_WndProc(
4665 HWND hwnd,
4666 UINT uMsg,
4667 WPARAM wParam,
4668 LPARAM lParam)
4669{
LemonBoya773d842022-05-10 20:54:46 +01004670 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4671 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
4673 HandleMouseHide(uMsg, lParam);
4674
4675 s_uMsg = uMsg;
4676 s_wParam = wParam;
4677 s_lParam = lParam;
4678
4679 switch (uMsg)
4680 {
4681 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4682 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004683 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004685 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4687 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4688 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4689 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4690#ifdef FEAT_MENU
4691 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4692#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004693 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4694 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4696 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004697 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4698 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4700 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4701 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4702#ifdef FEAT_NETBEANS_INTG
4703 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4704#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004705#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004706 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4707 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004708#endif
K.Takata92000e22022-01-20 15:10:57 +00004709 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004710
Bram Moolenaar734a8672019-12-02 22:49:38 +01004711 case WM_QUERYENDSESSION: // System wants to go down.
4712 gui_shell_closed(); // Will exit when no changed buffers.
4713 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
4715 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004716 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004719 return 0L;
4720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 break;
4722
4723 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004724 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4725 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004726 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 return 0L;
4728
4729 case WM_SYSCHAR:
4730 /*
4731 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4732 * shortcut key, handle like a typed ALT key, otherwise call Windows
4733 * ALT key handling.
4734 */
4735#ifdef FEAT_MENU
4736 if ( !gui.menu_is_active
4737 || p_wak[0] == 'n'
4738 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4739 )
4740#endif
4741 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004742 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return 0L;
4744 }
4745#ifdef FEAT_MENU
4746 else
K.Takata4ac893f2022-01-20 12:44:28 +00004747 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748#endif
4749
4750 case WM_SYSKEYUP:
4751#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004752 // This used to be done only when menu is active: ALT key is used for
4753 // that. But that caused problems when menu is disabled and using
4754 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4755 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004756 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004758 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
4760
K.Takata4f2417f2021-06-05 16:25:32 +02004761 case WM_EXITSIZEMOVE:
4762 destroy_sizing_tip();
4763 break;
4764
Bram Moolenaar734a8672019-12-02 22:49:38 +01004765 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004766 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004769 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004770 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004771 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaar734a8672019-12-02 22:49:38 +01004773 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004774 case WM_SETTINGCHANGE:
4775 return _OnSettingChange((UINT)wParam);
4776
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004777#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004779 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#endif
K.Takata92000e22022-01-20 15:10:57 +00004781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782#if defined(MENUHINTS) && defined(FEAT_MENU)
4783 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004784 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787#ifdef FEAT_MBYTE_IME
4788 case WM_IME_NOTIFY:
4789 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004790 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004791 return 1L;
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 case WM_IME_COMPOSITION:
4794 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004795 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004796 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004798 case WM_DPICHANGED:
4799 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4800 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004804 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806#endif
K.Takata92000e22022-01-20 15:10:57 +00004807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809
K.Takata4ac893f2022-01-20 12:44:28 +00004810 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811}
4812
4813/*
4814 * End of call-back routines
4815 */
4816
Bram Moolenaar734a8672019-12-02 22:49:38 +01004817// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818HWND vim_parent_hwnd = NULL;
4819
4820 static BOOL CALLBACK
4821FindWindowTitle(HWND hwnd, LPARAM lParam)
4822{
4823 char buf[2048];
4824 char *title = (char *)lParam;
4825
4826 if (GetWindowText(hwnd, buf, sizeof(buf)))
4827 {
4828 if (strstr(buf, title) != NULL)
4829 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004830 // Found it. Store the window ref. and quit searching if MDI
4831 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004833 if (vim_parent_hwnd != NULL)
4834 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004837 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838}
4839
4840/*
4841 * Invoked for '-P "title"' argument: search for parent application to open
4842 * our window in.
4843 */
4844 void
4845gui_mch_set_parent(char *title)
4846{
4847 EnumWindows(FindWindowTitle, (LPARAM)title);
4848 if (vim_parent_hwnd == NULL)
4849 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004850 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 mch_exit(2);
4852 }
4853}
4854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 static void
4857ole_error(char *arg)
4858{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004859 char buf[IOSIZE];
4860
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004861# ifdef VIMDLL
4862 gui.in_use = mch_is_gui_executable();
4863# endif
4864
Bram Moolenaar734a8672019-12-02 22:49:38 +01004865 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004866 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004867 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004868 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004872#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4873 static char *
4874gvim_error(void)
4875{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004876 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004877
4878 if (starting)
4879 {
4880 mch_errmsg(msg);
4881 mch_errmsg("\n");
4882 mch_exit(2);
4883 }
4884 return msg;
4885}
4886
4887 char *
4888gui_mch_do_spawn(char_u *arg)
4889{
4890 int len;
4891# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4892 char_u *session = NULL;
4893 LPWSTR tofree1 = NULL;
4894# endif
4895 WCHAR name[MAX_PATH];
4896 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4897 STARTUPINFOW si = {sizeof(si)};
4898 PROCESS_INFORMATION pi;
4899
4900 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4901 goto error;
4902 p = wcsrchr(name, L'\\');
4903 if (p == NULL)
4904 goto error;
4905 // Replace the executable name from vim(d).exe to gvim(d).exe.
4906# ifdef DEBUG
4907 wcscpy(p + 1, L"gvimd.exe");
4908# else
4909 wcscpy(p + 1, L"gvim.exe");
4910# endif
4911
4912# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4913 if (starting)
4914# endif
4915 {
4916 // Pass the command line to the new process.
4917 p = GetCommandLineW();
4918 // Skip 1st argument.
4919 while (*p && *p != L' ' && *p != L'\t')
4920 {
4921 if (*p == L'"')
4922 {
4923 while (*p && *p != L'"')
4924 ++p;
4925 if (*p)
4926 ++p;
4927 }
4928 else
4929 ++p;
4930 }
4931 cmd = p;
4932 }
4933# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4934 else
4935 {
4936 // Create a session file and pass it to the new process.
4937 LPWSTR wsession;
4938 char_u *savebg;
4939 int ret;
4940
4941 session = vim_tempname('s', FALSE);
4942 if (session == NULL)
4943 goto error;
4944 savebg = p_bg;
4945 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4946 ret = write_session_file(session);
4947 vim_free(p_bg);
4948 p_bg = savebg;
4949 if (!ret)
4950 goto error;
4951 wsession = enc_to_utf16(session, NULL);
4952 if (wsession == NULL)
4953 goto error;
4954 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004955 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004956 if (cmd == NULL)
4957 {
4958 vim_free(wsession);
4959 goto error;
4960 }
4961 tofree1 = cmd;
4962 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4963 wsession, wsession);
4964 vim_free(wsession);
4965 }
4966# endif
4967
4968 // Check additional arguments to the `:gui` command.
4969 if (arg != NULL)
4970 {
4971 warg = enc_to_utf16(arg, NULL);
4972 if (warg == NULL)
4973 goto error;
4974 tofree2 = warg;
4975 }
4976 else
4977 warg = L"";
4978
4979 // Set up the new command line.
4980 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004981 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004982 if (newcmd == NULL)
4983 goto error;
4984 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4985
4986 // Spawn a new GUI process.
4987 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4988 NULL, NULL, &si, &pi))
4989 goto error;
4990 CloseHandle(pi.hProcess);
4991 CloseHandle(pi.hThread);
4992 mch_exit(0);
4993
4994error:
4995# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4996 if (session)
4997 mch_remove(session);
4998 vim_free(session);
4999 vim_free(tofree1);
5000# endif
5001 vim_free(newcmd);
5002 vim_free(tofree2);
5003 return gvim_error();
5004}
5005#endif
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007/*
5008 * Parse the GUI related command-line arguments. Any arguments used are
5009 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005010 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 */
5012 void
5013gui_mch_prepare(int *argc, char **argv)
5014{
5015 int silent = FALSE;
5016 int idx;
5017
Bram Moolenaar734a8672019-12-02 22:49:38 +01005018 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5020 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005021 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5023 && (argv[2][0] == '-' || argv[2][0] == '/'))
5024 {
5025 silent = TRUE;
5026 idx = 2;
5027 }
5028 else
5029 idx = 1;
5030
Bram Moolenaar734a8672019-12-02 22:49:38 +01005031 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (STRICMP(argv[idx] + 1, "register") == 0)
5033 {
5034#ifdef FEAT_OLE
5035 RegisterMe(silent);
5036 mch_exit(0);
5037#else
5038 if (!silent)
5039 ole_error("register");
5040 mch_exit(2);
5041#endif
5042 }
5043
Bram Moolenaar734a8672019-12-02 22:49:38 +01005044 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5046 {
5047#ifdef FEAT_OLE
5048 UnregisterMe(!silent);
5049 mch_exit(0);
5050#else
5051 if (!silent)
5052 ole_error("unregister");
5053 mch_exit(2);
5054#endif
5055 }
5056
Bram Moolenaar734a8672019-12-02 22:49:38 +01005057 // Ignore an -embedding argument. It is only relevant if the
5058 // application wants to treat the case when it is started manually
5059 // differently from the case where it is started via automation (and
5060 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5062 {
5063#ifdef FEAT_OLE
5064 *argc = 1;
5065#else
5066 ole_error("embedding");
5067 mch_exit(2);
5068#endif
5069 }
5070 }
5071
5072#ifdef FEAT_OLE
5073 {
5074 int bDoRestart = FALSE;
5075
5076 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005077 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (bDoRestart)
5079 mch_exit(0);
5080 }
5081#endif
5082
5083#ifdef FEAT_NETBEANS_INTG
5084 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005085 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 int arg;
5087
5088 for (arg = 1; arg < *argc; arg++)
5089 if (strncmp("-nb", argv[arg], 3) == 0)
5090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 netbeansArg = argv[arg];
5092 mch_memmove(&argv[arg], &argv[arg + 1],
5093 (--*argc - arg) * sizeof(char *));
5094 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005095 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099}
5100
K.Takatac81e9bf2022-01-16 14:15:49 +00005101 static void
5102load_dpi_func(void)
5103{
5104 HMODULE hUser32;
5105
5106 hUser32 = GetModuleHandle("user32.dll");
5107 if (hUser32 == NULL)
5108 goto fail;
5109
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005110 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5111 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5112 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005113 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005114 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5115 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005116
5117 if (pSetThreadDpiAwarenessContext != NULL)
5118 {
5119 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5120 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5121 if (oldctx != NULL)
5122 {
5123 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5124 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5125#ifdef DEBUG
5126 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5127 {
5128 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5129 }
5130#endif
5131 return;
5132 }
5133 }
5134
5135fail:
5136 // Disable PerMonitorV2 APIs.
5137 pGetDpiForSystem = stubGetDpiForSystem;
5138 pGetDpiForWindow = NULL;
5139 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5140 pSetThreadDpiAwarenessContext = NULL;
5141 pGetAwarenessFromDpiAwarenessContext = NULL;
5142}
5143
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144/*
5145 * Initialise the GUI. Create all the windows, set up all the call-backs
5146 * etc.
5147 */
5148 int
5149gui_mch_init(void)
5150{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005152 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 // Return here if the window was already opened (happens when
5156 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005158 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 /*
5161 * Load the tearoff bitmap
5162 */
5163#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005164 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165#endif
5166
K.Takatac81e9bf2022-01-16 14:15:49 +00005167 load_dpi_func();
5168
5169 s_dpi = pGetDpiForSystem();
5170 update_scrollbar_size();
5171
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005173 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#endif
5175 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005176#ifdef FEAT_TOOLBAR
5177 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5181
Bram Moolenaar734a8672019-12-02 22:49:38 +01005182 // First try using the wide version, so that we can use any title.
5183 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005184 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005186 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 wndclassw.lpfnWndProc = _WndProc;
5188 wndclassw.cbClsExtra = 0;
5189 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005190 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5192 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5193 wndclassw.hbrBackground = s_brush;
5194 wndclassw.lpszMenuName = NULL;
5195 wndclassw.lpszClassName = szVimWndClassW;
5196
K.Takata4ac893f2022-01-20 12:44:28 +00005197 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (vim_parent_hwnd != NULL)
5202 {
5203#ifdef HAVE_TRY_EXCEPT
5204 __try
5205 {
5206#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005207 // Open inside the specified parent window.
5208 // TODO: last argument should point to a CLIENTCREATESTRUCT
5209 // structure.
5210 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005212 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005213 WS_OVERLAPPEDWINDOW | WS_CHILD
5214 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5216 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005217 100, // Any value will do
5218 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005220 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#ifdef HAVE_TRY_EXCEPT
5222 }
5223 __except(EXCEPTION_EXECUTE_HANDLER)
5224 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005225 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227#endif
5228 if (s_hwnd == NULL)
5229 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005230 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 mch_exit(2);
5232 }
5233 }
5234 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005235 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005236 // If the provided windowid is not valid reset it to zero, so that it
5237 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005238 if (IsWindow((HWND)win_socket_id) <= 0)
5239 win_socket_id = 0;
5240
Bram Moolenaar734a8672019-12-02 22:49:38 +01005241 // Create a window. If win_socket_id is not zero without border and
5242 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005243 s_hwnd = CreateWindowW(
5244 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005245 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5246 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005247 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5248 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005249 100, // Any value will do
5250 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005251 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005252 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005253 if (s_hwnd != NULL && win_socket_id != 0)
5254 {
5255 SetParent(s_hwnd, (HWND)win_socket_id);
5256 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5257 }
5258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 if (s_hwnd == NULL)
5261 return FAIL;
5262
K.Takatac81e9bf2022-01-16 14:15:49 +00005263 if (pGetDpiForWindow != NULL)
5264 {
5265 s_dpi = pGetDpiForWindow(s_hwnd);
5266 update_scrollbar_size();
5267 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5271 dyn_imm_load();
5272#endif
5273
Bram Moolenaar734a8672019-12-02 22:49:38 +01005274 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005275 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005276 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005277 wndclassw.style = CS_OWNDC;
5278 wndclassw.lpfnWndProc = _TextAreaWndProc;
5279 wndclassw.cbClsExtra = 0;
5280 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005281 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005282 wndclassw.hIcon = NULL;
5283 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5284 wndclassw.hbrBackground = NULL;
5285 wndclassw.lpszMenuName = NULL;
5286 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005287
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005288 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return FAIL;
5290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 s_textArea = CreateWindowExW(
5293 0,
5294 szTextAreaClassW, L"Vim text area",
5295 WS_CHILD | WS_VISIBLE, 0, 0,
5296 100, // Any value will do for now
5297 100, // Any value will do for now
5298 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005299 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (s_textArea == NULL)
5302 return FAIL;
5303
Bram Moolenaar20321902016-02-17 12:30:17 +01005304#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005305 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005306 {
5307 HANDLE hIcon = NULL;
5308
5309 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005310 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005311 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005312#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#ifdef FEAT_MENU
5315 s_menuBar = CreateMenu();
5316#endif
5317 s_hdc = GetDC(s_textArea);
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
Bram Moolenaar734a8672019-12-02 22:49:38 +01005321 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005322 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
Bram Moolenaar734a8672019-12-02 22:49:38 +01005324 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 gui_mch_def_colors();
5326
Bram Moolenaar734a8672019-12-02 22:49:38 +01005327 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5328 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 set_normal_colors();
5330
5331 /*
5332 * Check that none of the colors are the same as the background color.
5333 * Then store the current values as the defaults.
5334 */
5335 gui_check_colors();
5336 gui.def_norm_pixel = gui.norm_pixel;
5337 gui.def_back_pixel = gui.back_pixel;
5338
Bram Moolenaar734a8672019-12-02 22:49:38 +01005339 // Get the colors for the highlight groups (gui_check_colors() might have
5340 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 highlight_gui_started();
5342
5343 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005344 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005346 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348 /*
5349 * Set up for Intellimouse processing
5350 */
5351 init_mouse_wheel();
5352
5353 /*
5354 * compute a couple of metrics used for the dialogs
5355 */
5356 get_dialog_font_metrics();
5357#ifdef FEAT_TOOLBAR
5358 /*
5359 * Create the toolbar
5360 */
5361 initialise_toolbar();
5362#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005363#ifdef FEAT_GUI_TABLINE
5364 /*
5365 * Create the tabline
5366 */
5367 initialise_tabline();
5368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#ifdef MSWIN_FIND_REPLACE
5370 /*
5371 * Initialise the dialog box stuff
5372 */
5373 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5374
Bram Moolenaar734a8672019-12-02 22:49:38 +01005375 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005377 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005379 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5381 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5382 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005385#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005386 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005387 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005388#endif
5389
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005390#ifdef FEAT_RENDER_OPTIONS
5391 if (p_rop)
5392 (void)gui_mch_set_rendering_options(p_rop);
5393#endif
5394
Bram Moolenaar748bf032005-02-02 23:04:36 +00005395theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005396 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005397 display_errors();
5398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 return OK;
5400}
5401
5402/*
5403 * Get the size of the screen, taking position on multiple monitors into
5404 * account (if supported).
5405 */
5406 static void
5407get_work_area(RECT *spi_rect)
5408{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005409 HMONITOR mon;
5410 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar734a8672019-12-02 22:49:38 +01005412 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005413 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005414 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005416 moninfo.cbSize = sizeof(MONITORINFO);
5417 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005419 *spi_rect = moninfo.rcWork;
5420 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005423 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5425}
5426
5427/*
5428 * Set the size of the window to the given width and height in pixels.
5429 */
5430 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005431gui_mch_set_shellsize(
5432 int width,
5433 int height,
5434 int min_width UNUSED,
5435 int min_height UNUSED,
5436 int base_width UNUSED,
5437 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005438 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005441 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar734a8672019-12-02 22:49:38 +01005444 // Try to keep window completely on screen.
5445 // Get position of the screen work area. This is the part that is not
5446 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 get_work_area(&workarea_rect);
5448
Bram Moolenaar734a8672019-12-02 22:49:38 +01005449 // Resizing a maximized window looks very strange, unzoom it first.
5450 // But don't do it when still starting up, it may have been requested in
5451 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005452 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005454
5455 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
Bram Moolenaar734a8672019-12-02 22:49:38 +01005457 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005458 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5459 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5460 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5461 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5462 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5463 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar734a8672019-12-02 22:49:38 +01005465 // The following should take care of keeping Vim on the same monitor, no
5466 // matter if the secondary monitor is left or right of the primary
5467 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005468 window_rect.right = window_rect.left + win_width;
5469 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005470
Bram Moolenaar734a8672019-12-02 22:49:38 +01005471 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005472 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5473 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005475 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5476 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005478 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5479 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005481 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5482 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005484 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5485 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487 SetActiveWindow(s_hwnd);
5488 SetFocus(s_hwnd);
5489
Bram Moolenaar734a8672019-12-02 22:49:38 +01005490 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492}
5493
5494
5495 void
5496gui_mch_set_scrollbar_thumb(
5497 scrollbar_T *sb,
5498 long val,
5499 long size,
5500 long max)
5501{
5502 SCROLLINFO info;
5503
5504 sb->scroll_shift = 0;
5505 while (max > 32767)
5506 {
5507 max = (max + 1) >> 1;
5508 val >>= 1;
5509 size >>= 1;
5510 ++sb->scroll_shift;
5511 }
5512
5513 if (sb->scroll_shift > 0)
5514 ++size;
5515
5516 info.cbSize = sizeof(info);
5517 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5518 info.nPos = val;
5519 info.nMin = 0;
5520 info.nMax = max;
5521 info.nPage = size;
5522 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5523}
5524
5525
5526/*
5527 * Set the current text font.
5528 */
5529 void
5530gui_mch_set_font(GuiFont font)
5531{
5532 gui.currFont = font;
5533}
5534
5535
5536/*
5537 * Set the current text foreground color.
5538 */
5539 void
5540gui_mch_set_fg_color(guicolor_T color)
5541{
5542 gui.currFgColor = color;
5543}
5544
5545/*
5546 * Set the current text background color.
5547 */
5548 void
5549gui_mch_set_bg_color(guicolor_T color)
5550{
5551 gui.currBgColor = color;
5552}
5553
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005554/*
5555 * Set the current text special color.
5556 */
5557 void
5558gui_mch_set_sp_color(guicolor_T color)
5559{
5560 gui.currSpColor = color;
5561}
5562
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005563#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564/*
5565 * Multi-byte handling, originally by Sung-Hoon Baek.
5566 * First static functions (no prototypes generated).
5567 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005568# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005569# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005570# endif
5571# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 * handle WM_IME_NOTIFY message
5575 */
5576 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005577_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 LRESULT lResult = 0;
5580 HIMC hImc;
5581
5582 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5583 return lResult;
5584 switch (dwCommand)
5585 {
5586 case IMN_SETOPENSTATUS:
5587 if (pImmGetOpenStatus(hImc))
5588 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005589 LOGFONTW lf = norm_logfont;
5590 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5591 // Work around when PerMonitorV2 is not enabled in the process level.
5592 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5593 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 im_set_position(gui.row, gui.col);
5595
Bram Moolenaar734a8672019-12-02 22:49:38 +01005596 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005597 State &= ~MODE_LANGMAP;
5598 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005600# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005601 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005604 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 int old_row = gui.row;
5606 int old_col = gui.col;
5607
5608 // This must be called here before
5609 // status_redraw_curbuf(), otherwise the mode
5610 // message may appear in the wrong position.
5611 showmode();
5612 status_redraw_curbuf();
5613 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005614 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 gui.row = old_row;
5616 gui.col = old_col;
5617 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620 }
5621 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005622 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 lResult = 0;
5624 break;
5625 }
5626 pImmReleaseContext(hWnd, hImc);
5627 return lResult;
5628}
5629
5630 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005631_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 char_u *ret;
5634 int len;
5635
Bram Moolenaar734a8672019-12-02 22:49:38 +01005636 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 return 0;
5638
5639 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5640 if (ret != NULL)
5641 {
5642 add_to_input_buf_csi(ret, len);
5643 vim_free(ret);
5644 return 1;
5645 }
5646 return 0;
5647}
5648
5649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 * void GetResultStr()
5651 *
5652 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5653 * get complete composition string
5654 */
5655 static char_u *
5656GetResultStr(HWND hwnd, int GCS, int *lenp)
5657{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005658 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005659 LONG ret;
5660 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 char_u *convbuf = NULL;
5662
5663 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5664 return NULL;
5665
K.Takatab0b2b732022-01-19 12:59:21 +00005666 // Get the length of the composition string.
5667 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5668 if (ret <= 0)
5669 return NULL;
5670
5671 // Allocate the requested buffer plus space for the NUL character.
5672 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (buf == NULL)
5674 return NULL;
5675
K.Takatab0b2b732022-01-19 12:59:21 +00005676 // Reads in the composition string.
5677 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5678 *lenp = ret / sizeof(WCHAR);
5679
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005680 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 pImmReleaseContext(hwnd, hIMC);
5682 vim_free(buf);
5683 return convbuf;
5684}
5685#endif
5686
Bram Moolenaar734a8672019-12-02 22:49:38 +01005687// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005688#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690/*
5691 * set font to IM.
5692 */
5693 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005694im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
5696 HIMC hImc;
5697
5698 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5699 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005700 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 pImmReleaseContext(s_hwnd, hImc);
5702 }
5703}
5704
5705/*
5706 * Notify cursor position to IM.
5707 */
5708 void
5709im_set_position(int row, int col)
5710{
5711 HIMC hImc;
5712
5713 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5714 {
5715 COMPOSITIONFORM cfs;
5716
5717 cfs.dwStyle = CFS_POINT;
5718 cfs.ptCurrentPos.x = FILL_X(col);
5719 cfs.ptCurrentPos.y = FILL_Y(row);
5720 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005721 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5722 {
5723 // Work around when PerMonitorV2 is not enabled in the process level.
5724 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5725 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 pImmSetCompositionWindow(hImc, &cfs);
5728
5729 pImmReleaseContext(s_hwnd, hImc);
5730 }
5731}
5732
5733/*
5734 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5735 */
5736 void
5737im_set_active(int active)
5738{
5739 HIMC hImc;
5740 static HIMC hImcOld = (HIMC)0;
5741
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005742# ifdef VIMDLL
5743 if (!gui.in_use && !gui.starting)
5744 {
5745 mbyte_im_set_active(active);
5746 return;
5747 }
5748# endif
5749
Bram Moolenaar734a8672019-12-02 22:49:38 +01005750 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (p_imdisable)
5753 {
5754 if (hImcOld == (HIMC)0)
5755 {
5756 hImcOld = pImmGetContext(s_hwnd);
5757 if (hImcOld)
5758 pImmAssociateContext(s_hwnd, (HIMC)0);
5759 }
5760 active = FALSE;
5761 }
5762 else if (hImcOld != (HIMC)0)
5763 {
5764 pImmAssociateContext(s_hwnd, hImcOld);
5765 hImcOld = (HIMC)0;
5766 }
5767
5768 hImc = pImmGetContext(s_hwnd);
5769 if (hImc)
5770 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005771 /*
5772 * for Korean ime
5773 */
5774 HKL hKL = GetKeyboardLayout(0);
5775
5776 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5777 {
5778 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5779 static BOOL bSaved = FALSE;
5780
5781 if (active)
5782 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005783 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005784 if (bSaved)
5785 pImmSetConversionStatus(hImc, dwConversionSaved,
5786 dwSentenceSaved);
5787 bSaved = FALSE;
5788 }
5789 else
5790 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005791 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005792 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5793 &dwSentenceSaved))
5794 {
5795 bSaved = TRUE;
5796 pImmSetConversionStatus(hImc,
5797 dwConversionSaved & ~(IME_CMODE_NATIVE
5798 | IME_CMODE_FULLSHAPE),
5799 dwSentenceSaved);
5800 }
5801 }
5802 }
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 pImmSetOpenStatus(hImc, active);
5805 pImmReleaseContext(s_hwnd, hImc);
5806 }
5807 }
5808}
5809
5810/*
5811 * Get IM status. When IM is on, return not 0. Else return 0.
5812 */
5813 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005814im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 int status = 0;
5817 HIMC hImc;
5818
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005819# ifdef VIMDLL
5820 if (!gui.in_use && !gui.starting)
5821 return mbyte_im_get_status();
5822# endif
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5825 {
5826 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5827 pImmReleaseContext(s_hwnd, hImc);
5828 }
5829 return status;
5830}
5831
Bram Moolenaar734a8672019-12-02 22:49:38 +01005832#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005835/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005836 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005837 */
5838 static void
5839latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5840{
5841 int c;
5842
Bram Moolenaarca003e12006-03-17 23:19:38 +00005843 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005844 {
5845 c = *text++;
5846 switch (c)
5847 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005848 case 0xa4: c = 0x20ac; break; // euro
5849 case 0xa6: c = 0x0160; break; // S hat
5850 case 0xa8: c = 0x0161; break; // S -hat
5851 case 0xb4: c = 0x017d; break; // Z hat
5852 case 0xb8: c = 0x017e; break; // Z -hat
5853 case 0xbc: c = 0x0152; break; // OE
5854 case 0xbd: c = 0x0153; break; // oe
5855 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005856 }
5857 *unicodebuf++ = c;
5858 }
5859}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861#ifdef FEAT_RIGHTLEFT
5862/*
5863 * What is this for? In the case where you are using Win98 or Win2K or later,
5864 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5865 * reverses the string sent to the TextOut... family. This sucks, because we
5866 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5867 * way to tell Windblows not to do this!
5868 *
5869 * The short of it is that this 'RevOut' only gets called if you are running
5870 * one of the new, "improved" MS OSes, and only if you are running in
5871 * 'rightleft' mode. It makes display take *slightly* longer, but not
5872 * noticeably so.
5873 */
5874 static void
K.Takata135e1522022-01-29 15:27:58 +00005875RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 int col,
5877 int row,
5878 UINT foptions,
5879 CONST RECT *pcliprect,
5880 LPCTSTR text,
5881 UINT len,
5882 CONST INT *padding)
5883{
5884 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005886 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005887 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005888 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889}
5890#endif
5891
Bram Moolenaar92467d32017-12-05 13:22:16 +01005892 static void
5893draw_line(
5894 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005895 int y1,
5896 int x2,
5897 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005898 COLORREF color)
5899{
5900#if defined(FEAT_DIRECTX)
5901 if (IS_ENABLE_DIRECTX())
5902 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5903 else
5904#endif
5905 {
5906 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5907 HPEN old_pen = SelectObject(s_hdc, hpen);
5908 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005909 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005910 LineTo(s_hdc, x2, y2);
5911 DeleteObject(SelectObject(s_hdc, old_pen));
5912 }
5913}
5914
5915 static void
5916set_pixel(
5917 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005918 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005919 COLORREF color)
5920{
5921#if defined(FEAT_DIRECTX)
5922 if (IS_ENABLE_DIRECTX())
5923 DWriteContext_SetPixel(s_dwc, x, y, color);
5924 else
5925#endif
5926 SetPixel(s_hdc, x, y, color);
5927}
5928
5929 static void
5930fill_rect(
5931 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005932 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005933 COLORREF color)
5934{
5935#if defined(FEAT_DIRECTX)
5936 if (IS_ENABLE_DIRECTX())
5937 DWriteContext_FillRect(s_dwc, rcp, color);
5938 else
5939#endif
5940 {
5941 HBRUSH hbr2;
5942
5943 if (hbr == NULL)
5944 hbr2 = CreateSolidBrush(color);
5945 else
5946 hbr2 = hbr;
5947 FillRect(s_hdc, rcp, hbr2);
5948 if (hbr == NULL)
5949 DeleteBrush(hbr2);
5950 }
5951}
5952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 void
5954gui_mch_draw_string(
5955 int row,
5956 int col,
5957 char_u *text,
5958 int len,
5959 int flags)
5960{
5961 static int *padding = NULL;
5962 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 const RECT *pcliprect = NULL;
5964 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 static WCHAR *unicodebuf = NULL;
5966 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005967 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 int y;
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 /*
5972 * Italic and bold text seems to have an extra row of pixels at the bottom
5973 * (below where the bottom of the character should be). If we draw the
5974 * characters with a solid background, the top row of pixels in the
5975 * character below will be overwritten. We can fix this by filling in the
5976 * background ourselves, to the correct character proportions, and then
5977 * writing the character in transparent mode. Still have a problem when
5978 * the character is "_", which gets written on to the character below.
5979 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5980 * pixel in their slots, which fixes the problem with the bottom row of
5981 * pixels. We still need this code because otherwise the top row of pixels
5982 * becomes a problem. - webb.
5983 */
5984 static HBRUSH hbr_cache[2] = {NULL, NULL};
5985 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5986 static int brush_lru = 0;
5987 HBRUSH hbr;
5988 RECT rc;
5989
5990 if (!(flags & DRAW_TRANSP))
5991 {
5992 /*
5993 * Clear background first.
5994 * Note: FillRect() excludes right and bottom of rectangle.
5995 */
5996 rc.left = FILL_X(col);
5997 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (has_mbyte)
5999 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006000 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006001 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 rc.right = FILL_X(col + len);
6005 rc.bottom = FILL_Y(row + 1);
6006
Bram Moolenaar734a8672019-12-02 22:49:38 +01006007 // Cache the created brush, that saves a lot of time. We need two:
6008 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (gui.currBgColor == brush_color[0])
6010 {
6011 hbr = hbr_cache[0];
6012 brush_lru = 1;
6013 }
6014 else if (gui.currBgColor == brush_color[1])
6015 {
6016 hbr = hbr_cache[1];
6017 brush_lru = 0;
6018 }
6019 else
6020 {
6021 if (hbr_cache[brush_lru] != NULL)
6022 DeleteBrush(hbr_cache[brush_lru]);
6023 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6024 brush_color[brush_lru] = gui.currBgColor;
6025 hbr = hbr_cache[brush_lru];
6026 brush_lru = !brush_lru;
6027 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006028
Bram Moolenaar92467d32017-12-05 13:22:16 +01006029 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 SetBkMode(s_hdc, TRANSPARENT);
6032
6033 /*
6034 * When drawing block cursor, prevent inverted character spilling
6035 * over character cell (can happen with bold/italic)
6036 */
6037 if (flags & DRAW_CURSOR)
6038 {
6039 pcliprect = &rc;
6040 foptions = ETO_CLIPPED;
6041 }
6042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 SetTextColor(s_hdc, gui.currFgColor);
6044 SelectFont(s_hdc, gui.currFont);
6045
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006046#ifdef FEAT_DIRECTX
6047 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006048 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006049#endif
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6052 {
K.Takata135e1522022-01-29 15:27:58 +00006053 int i;
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 vim_free(padding);
6056 pad_size = Columns;
6057
Bram Moolenaar734a8672019-12-02 22:49:38 +01006058 // Don't give an out-of-memory message here, it would call us
6059 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006060 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (padding != NULL)
6062 for (i = 0; i < pad_size; i++)
6063 padding[i] = gui.char_width;
6064 }
6065
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 /*
6067 * We have to provide the padding argument because italic and bold versions
6068 * of fixed-width fonts are often one pixel or so wider than their normal
6069 * versions.
6070 * No check for DRAW_BOLD, Windows will have done it already.
6071 */
6072
Bram Moolenaar734a8672019-12-02 22:49:38 +01006073 // Check if there are any UTF-8 characters. If not, use normal text
6074 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (enc_utf8)
6076 for (n = 0; n < len; ++n)
6077 if (text[n] >= 0x80)
6078 break;
6079
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006080#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006081 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6082 // required that unicode drawing routine, currently. So this forces it
6083 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006084 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006085 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006086#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006087
Bram Moolenaar734a8672019-12-02 22:49:38 +01006088 // Check if the Unicode buffer exists and is big enough. Create it
6089 // with the same length as the multi-byte string, the number of wide
6090 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006091 if ((enc_utf8
6092 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6093 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 && (unicodebuf == NULL || len > unibuflen))
6095 {
6096 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006097 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006100 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 unibuflen = len;
6103 }
6104
6105 if (enc_utf8 && n < len && unicodebuf != NULL)
6106 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006107 // Output UTF-8 characters. Composing characters should be
6108 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006109 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006110 int wlen; // string length in words
6111 int clen; // string length in characters
6112 int cells; // cell width of string up to composing char
6113 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006114 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006116 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006117 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006119 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006121 c = utf_ptr2char(text + i);
6122 if (c >= 0x10000)
6123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006124 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006125 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6126 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006127 }
6128 else
6129 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006130 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006131 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006132
6133 if (utf_iscomposing(c))
6134 cw = 0;
6135 else
6136 {
6137 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006138 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006139 cw = 1;
6140 }
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (unicodepdy != NULL)
6143 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006144 // Use unicodepdy to make characters fit as we expect, even
6145 // when the font uses different widths (e.g., bold character
6146 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006147 if (c >= 0x10000)
6148 {
6149 unicodepdy[wlen - 2] = cw * gui.char_width;
6150 unicodepdy[wlen - 1] = 0;
6151 }
6152 else
6153 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
6155 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006156 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006157 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006159#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006160 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006161 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006162 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006163 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006164 TEXT_X(col), TEXT_Y(row),
6165 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006166 gui.char_width, gui.currFgColor,
6167 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006168 }
6169 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006170#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006171 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6172 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006173 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006175 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006177 // If we want to display codepage data, and the current CP is not the
6178 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (unicodebuf != NULL)
6180 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006181 if (enc_latin9)
6182 latin9_to_ucs(text, len, unicodebuf);
6183 else
6184 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 MB_PRECOMPOSED,
6186 (char *)text, len,
6187 (LPWSTR)unicodebuf, unibuflen);
6188 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006189 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006190 // Use unicodepdy to make characters fit as we expect, even
6191 // when the font uses different widths (e.g., bold character
6192 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006193 if (unicodepdy != NULL)
6194 {
6195 int i;
6196 int cw;
6197
6198 for (i = 0; i < len; ++i)
6199 {
6200 cw = utf_char2cells(unicodebuf[i]);
6201 if (cw > 2)
6202 cw = 1;
6203 unicodepdy[i] = cw * gui.char_width;
6204 }
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006207 foptions, pcliprect, unicodebuf, len, unicodepdy);
6208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210 }
6211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
6213#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006214 // Windows will mess up RL text, so we have to draw it character by
6215 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006216 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6218 foptions, pcliprect, (char *)text, len, padding);
6219 else
6220#endif
6221 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6222 foptions, pcliprect, (char *)text, len, padding);
6223 }
6224
Bram Moolenaar734a8672019-12-02 22:49:38 +01006225 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 if (flags & DRAW_UNDERL)
6227 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006228 // When p_linespace is 0, overwrite the bottom row of pixels.
6229 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 if (p_linespace > 1)
6232 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006233 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006235
Bram Moolenaar734a8672019-12-02 22:49:38 +01006236 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006237 if (flags & DRAW_STRIKE)
6238 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006239 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006240 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006241 }
6242
Bram Moolenaar734a8672019-12-02 22:49:38 +01006243 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006244 if (flags & DRAW_UNDERC)
6245 {
6246 int x;
6247 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006248 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006249
6250 y = FILL_Y(row + 1) - 1;
6251 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6252 {
6253 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006254 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006255 }
6256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257}
6258
6259
6260/*
6261 * Output routines.
6262 */
6263
Bram Moolenaar734a8672019-12-02 22:49:38 +01006264/*
6265 * Flush any output to the screen
6266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 void
6268gui_mch_flush(void)
6269{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006270#if defined(FEAT_DIRECTX)
6271 if (IS_ENABLE_DIRECTX())
6272 DWriteContext_Flush(s_dwc);
6273#endif
6274
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 GdiFlush();
6276}
6277
6278 static void
6279clear_rect(RECT *rcp)
6280{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006281 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282}
6283
6284
Bram Moolenaarc716c302006-01-21 22:12:51 +00006285 void
6286gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6287{
6288 RECT workarea_rect;
6289
6290 get_work_area(&workarea_rect);
6291
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006292 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006293 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6294 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006295
Bram Moolenaar734a8672019-12-02 22:49:38 +01006296 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6297 // the menubar for MSwin, we subtract it from the screen height, so that
6298 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006299 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006300 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6301 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6302 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6303 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006304}
6305
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307#if defined(FEAT_MENU) || defined(PROTO)
6308/*
6309 * Add a sub menu to the menu bar.
6310 */
6311 void
6312gui_mch_add_menu(
6313 vimmenu_T *menu,
6314 int pos)
6315{
6316 vimmenu_T *parent = menu->parent;
6317
6318 menu->submenu_id = CreatePopupMenu();
6319 menu->id = s_menu_id++;
6320
6321 if (menu_is_menubar(menu->name))
6322 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006323 WCHAR *wn;
6324 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006326 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006327 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006328 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006330 infow.cbSize = sizeof(infow);
6331 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6332 | MIIM_SUBMENU;
6333 infow.dwItemData = (long_u)menu;
6334 infow.wID = menu->id;
6335 infow.fType = MFT_STRING;
6336 infow.dwTypeData = wn;
6337 infow.cch = (UINT)wcslen(wn);
6338 infow.hSubMenu = menu->submenu_id;
6339 InsertMenuItemW((parent == NULL)
6340 ? s_menuBar : parent->submenu_id,
6341 (UINT)pos, TRUE, &infow);
6342 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 }
6344
Bram Moolenaar734a8672019-12-02 22:49:38 +01006345 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (parent == NULL)
6347 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006348# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 else if (IsWindow(parent->tearoff_handle))
6350 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352}
6353
6354 void
6355gui_mch_show_popupmenu(vimmenu_T *menu)
6356{
6357 POINT mp;
6358
K.Takata45f9cfb2022-01-21 11:11:00 +00006359 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6361}
6362
6363 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006364gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
6366 vimmenu_T *menu = gui_find_menu(path_name);
6367
6368 if (menu != NULL)
6369 {
6370 POINT p;
6371
Bram Moolenaar734a8672019-12-02 22:49:38 +01006372 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006374 if (mouse_pos)
6375 {
6376 int mx, my;
6377
6378 gui_mch_getmouse(&mx, &my);
6379 p.x += mx;
6380 p.y += my;
6381 }
6382 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006384 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6386 }
6387 msg_scroll = FALSE;
6388 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6389 }
6390}
6391
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006392# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393/*
6394 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6395 * create it as a pseudo-"tearoff menu".
6396 */
6397 void
6398gui_make_tearoff(char_u *path_name)
6399{
6400 vimmenu_T *menu = gui_find_menu(path_name);
6401
Bram Moolenaar734a8672019-12-02 22:49:38 +01006402 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (menu != NULL)
6404 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6405}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408/*
6409 * Add a menu item to a menu
6410 */
6411 void
6412gui_mch_add_menu_item(
6413 vimmenu_T *menu,
6414 int idx)
6415{
6416 vimmenu_T *parent = menu->parent;
6417
6418 menu->id = s_menu_id++;
6419 menu->submenu_id = NULL;
6420
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006421# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6423 {
6424 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6425 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6426 }
6427 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006428# endif
6429# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (menu_is_toolbar(parent->name))
6431 {
6432 TBBUTTON newtb;
6433
Bram Moolenaara80faa82020-04-12 19:37:17 +02006434 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (menu_is_separator(menu->name))
6436 {
6437 newtb.iBitmap = 0;
6438 newtb.fsStyle = TBSTYLE_SEP;
6439 }
6440 else
6441 {
6442 newtb.iBitmap = get_toolbar_bitmap(menu);
6443 newtb.fsStyle = TBSTYLE_BUTTON;
6444 }
6445 newtb.idCommand = menu->id;
6446 newtb.fsState = TBSTATE_ENABLED;
6447 newtb.iString = 0;
6448 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6449 (LPARAM)&newtb);
6450 menu->submenu_id = (HMENU)-1;
6451 }
6452 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006455 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006457 wn = enc_to_utf16(menu->name, NULL);
6458 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006460 InsertMenuW(parent->submenu_id, (UINT)idx,
6461 (menu_is_separator(menu->name)
6462 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6463 (UINT)menu->id, wn);
6464 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006466# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (IsWindow(parent->tearoff_handle))
6468 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
6471}
6472
6473/*
6474 * Destroy the machine specific menu widget.
6475 */
6476 void
6477gui_mch_destroy_menu(vimmenu_T *menu)
6478{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006479# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 /*
6481 * is this a toolbar button?
6482 */
6483 if (menu->submenu_id == (HMENU)-1)
6484 {
6485 int iButton;
6486
6487 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6488 (WPARAM)menu->id, 0);
6489 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6490 }
6491 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
6494 if (menu->parent != NULL
6495 && menu_is_popup(menu->parent->dname)
6496 && menu->parent->submenu_id != NULL)
6497 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6498 else
6499 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6500 if (menu->submenu_id != NULL)
6501 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006502# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (IsWindow(menu->tearoff_handle))
6504 DestroyWindow(menu->tearoff_handle);
6505 if (menu->parent != NULL
6506 && menu->parent->children != NULL
6507 && IsWindow(menu->parent->tearoff_handle))
6508 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006509 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 menu->modes = 0;
6511 rebuild_tearoff(menu->parent);
6512 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515}
6516
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006517# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 static void
6519rebuild_tearoff(vimmenu_T *menu)
6520{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006521 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 char_u tbuf[128];
6523 RECT trect;
6524 RECT rct;
6525 RECT roct;
6526 int x, y;
6527
6528 HWND thwnd = menu->tearoff_handle;
6529
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006530 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 if (GetWindowRect(thwnd, &trect)
6532 && GetWindowRect(s_hwnd, &rct)
6533 && GetClientRect(s_hwnd, &roct))
6534 {
6535 x = trect.left - rct.left;
6536 y = (trect.top - rct.bottom + roct.bottom);
6537 }
6538 else
6539 {
6540 x = y = 0xffffL;
6541 }
6542 DestroyWindow(thwnd);
6543 if (menu->children != NULL)
6544 {
6545 gui_mch_tearoff(tbuf, menu, x, y);
6546 if (IsWindow(menu->tearoff_handle))
6547 (void) SetWindowPos(menu->tearoff_handle,
6548 NULL,
6549 (int)trect.left,
6550 (int)trect.top,
6551 0, 0,
6552 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6553 }
6554}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006555# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557/*
6558 * Make a menu either grey or not grey.
6559 */
6560 void
6561gui_mch_menu_grey(
6562 vimmenu_T *menu,
6563 int grey)
6564{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006565# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 /*
6567 * is this a toolbar button?
6568 */
6569 if (menu->submenu_id == (HMENU)-1)
6570 {
6571 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006572 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006575# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006576 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6577 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006579# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6581 {
6582 WORD menuID;
6583 HWND menuHandle;
6584
6585 /*
6586 * A tearoff button has changed state.
6587 */
6588 if (menu->children == NULL)
6589 menuID = (WORD)(menu->id);
6590 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006591 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6593 if (menuHandle)
6594 EnableWindow(menuHandle, !grey);
6595
6596 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598}
6599
Bram Moolenaar734a8672019-12-02 22:49:38 +01006600#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602
Bram Moolenaar734a8672019-12-02 22:49:38 +01006603// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006606#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6609/*
6610 * stuff for dialogs
6611 */
6612
6613/*
6614 * The callback routine used by all the dialogs. Very simple. First,
6615 * acknowledges the INITDIALOG message so that Windows knows to do standard
6616 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6617 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6618 * number.
6619 */
6620 static LRESULT CALLBACK
6621dialog_callback(
6622 HWND hwnd,
6623 UINT message,
6624 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006625 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 if (message == WM_INITDIALOG)
6628 {
6629 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006630 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 (void)SetFocus(hwnd);
6632 if (dialog_default_button > IDCANCEL)
6633 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006634 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006635 // We don't have a default, set focus on another element of the
6636 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006637 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 return FALSE;
6639 }
6640
6641 if (message == WM_COMMAND)
6642 {
6643 int button = LOWORD(wParam);
6644
Bram Moolenaar734a8672019-12-02 22:49:38 +01006645 // Don't end the dialog if something was selected that was
6646 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (button >= DLG_NONBUTTON_CONTROL)
6648 return TRUE;
6649
Bram Moolenaar734a8672019-12-02 22:49:38 +01006650 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006652 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006653 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006654 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006655
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006656 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6657 p = utf16_to_enc(wp, NULL);
6658 vim_strncpy(s_textfield, p, IOSIZE);
6659 vim_free(p);
6660 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 /*
6664 * Need to check for IDOK because if the user just hits Return to
6665 * accept the default value, some reason this is what we get.
6666 */
6667 if (button == IDOK)
6668 {
6669 if (dialog_default_button > IDCANCEL)
6670 EndDialog(hwnd, dialog_default_button);
6671 }
6672 else
6673 EndDialog(hwnd, button - IDCANCEL);
6674 return TRUE;
6675 }
6676
6677 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6678 {
6679 EndDialog(hwnd, 0);
6680 return TRUE;
6681 }
6682 return FALSE;
6683}
6684
6685/*
6686 * Create a dialog dynamically from the parameter strings.
6687 * type = type of dialog (question, alert, etc.)
6688 * title = dialog title. may be NULL for default title.
6689 * message = text to display. Dialog sizes to accommodate it.
6690 * buttons = '\n' separated list of button captions, default first.
6691 * dfltbutton = number of default button.
6692 *
6693 * This routine returns 1 if the first button is pressed,
6694 * 2 for the second, etc.
6695 *
6696 * 0 indicates Esc was pressed.
6697 * -1 for unexpected error
6698 *
6699 * If stubbing out this fn, return 1.
6700 */
6701
Bram Moolenaar734a8672019-12-02 22:49:38 +01006702static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 "IDR_VIM",
6705 "IDR_VIM_ERROR",
6706 "IDR_VIM_ALERT",
6707 "IDR_VIM_INFO",
6708 "IDR_VIM_QUESTION"
6709};
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 int
6712gui_mch_dialog(
6713 int type,
6714 char_u *title,
6715 char_u *message,
6716 char_u *buttons,
6717 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006718 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006719 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006722 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 int numButtons;
6724 int *buttonWidths, *buttonPositions;
6725 int buttonYpos;
6726 int nchar, i;
6727 DWORD lStyle;
6728 int dlgwidth = 0;
6729 int dlgheight;
6730 int editboxheight;
6731 int horizWidth = 0;
6732 int msgheight;
6733 char_u *pstart;
6734 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006735 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 char_u *tbuffer;
6737 RECT rect;
6738 HWND hwnd;
6739 HDC hdc;
6740 HFONT font, oldFont;
6741 TEXTMETRIC fontInfo;
6742 int fontHeight;
6743 int textWidth, minButtonWidth, messageWidth;
6744 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006745 int maxDialogHeight;
6746 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 int vertical;
6748 int dlgPaddingX;
6749 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006750# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006751 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006753# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006754 garray_T ga;
6755 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006756 int dlg_icon_width;
6757 int dlg_icon_height;
6758 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006760# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006761 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006762# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006763 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006764# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006765 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006766 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006767# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
Bram Moolenaar748bf032005-02-02 23:04:36 +00006769 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006770 {
6771 load_dpi_func();
6772 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006773 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006774 }
6775 else
6776 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
6778 if ((type < 0) || (type > VIM_LAST_TYPE))
6779 type = 0;
6780
Bram Moolenaar734a8672019-12-02 22:49:38 +01006781 // allocate some memory for dialog template
6782 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006783 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006784 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 if (p == NULL)
6787 return -1;
6788
6789 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006790 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6792 * const.
6793 */
6794 tbuffer = vim_strsave(buttons);
6795 if (tbuffer == NULL)
6796 return -1;
6797
Bram Moolenaar734a8672019-12-02 22:49:38 +01006798 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
Bram Moolenaar734a8672019-12-02 22:49:38 +01006800 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 numButtons = 1;
6802 for (i = 0; tbuffer[i] != '\0'; i++)
6803 {
6804 if (tbuffer[i] == DLG_BUTTON_SEP)
6805 numButtons++;
6806 }
6807 if (dfltbutton >= numButtons)
6808 dfltbutton = -1;
6809
Bram Moolenaar734a8672019-12-02 22:49:38 +01006810 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006811 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (buttonWidths == NULL)
6813 return -1;
6814
Bram Moolenaar734a8672019-12-02 22:49:38 +01006815 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006816 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (buttonPositions == NULL)
6818 return -1;
6819
6820 /*
6821 * Calculate how big the dialog must be.
6822 */
6823 hwnd = GetDesktopWindow();
6824 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006825# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6827 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006828 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 use_lfSysmenu = TRUE;
6830 }
6831 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006832# endif
K.Takatad1c58992022-01-23 12:31:57 +00006833 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6834 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6835
6836 oldFont = SelectFont(hdc, font);
6837 dlgPaddingX = DLG_PADDING_X;
6838 dlgPaddingY = DLG_PADDING_Y;
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 GetTextMetrics(hdc, &fontInfo);
6841 fontHeight = fontInfo.tmHeight;
6842
Bram Moolenaar734a8672019-12-02 22:49:38 +01006843 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006844 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
Bram Moolenaar734a8672019-12-02 22:49:38 +01006846 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006847 if (s_hwnd == NULL)
6848 {
6849 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
Bram Moolenaar734a8672019-12-02 22:49:38 +01006851 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006852 get_work_area(&workarea_rect);
6853 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006854 if (maxDialogWidth > adjust_by_system_dpi(600))
6855 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006856 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006857 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006858 }
6859 else
6860 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006861 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006862 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006863 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006864 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6865 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6866 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6867 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006868
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006869 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006870 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6871 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6872 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6873 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6874 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006875 }
6876
Bram Moolenaar734a8672019-12-02 22:49:38 +01006877 // Set dlgwidth to width of message.
6878 // Copy the message into "ga", changing NL to CR-NL and inserting line
6879 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 pstart = message;
6881 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006882 msgheight = 0;
6883 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 do
6885 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006886 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006887
Bram Moolenaar734a8672019-12-02 22:49:38 +01006888 // Need to figure out where to break the string. The system does it
6889 // at a word boundary, which would mean we can't compute the number of
6890 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006891 textWidth = 0;
6892 last_white = NULL;
6893 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006894 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006895 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006896 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006897 && textWidth > maxDialogWidth * 3 / 4)
6898 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006899 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006900 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006901 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006902 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006903 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006904 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006905 textWidth = 0;
6906
6907 if (last_white != NULL)
6908 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006909 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006910 if (pend > last_white)
6911 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006912 pend = last_white + 1;
6913 last_white = NULL;
6914 }
6915 ga_append(&ga, '\r');
6916 ga_append(&ga, '\n');
6917 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006918 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006919
6920 while (--l >= 0)
6921 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006922 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006923 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925
6926 ga_append(&ga, '\r');
6927 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 pstart = pend + 1;
6929 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006930
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006931 if (ga.ga_data != NULL)
6932 message = ga.ga_data;
6933
Bram Moolenaar734a8672019-12-02 22:49:38 +01006934 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006935
K.Takatac81e9bf2022-01-16 14:15:49 +00006936 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6937 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
K.Takatac81e9bf2022-01-16 14:15:49 +00006939 // Add width of icon to dlgwidth, and some space
6940 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6941 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6942
6943 if (msgheight < dlg_icon_height)
6944 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 /*
6947 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006948 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006950 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (!vertical)
6952 {
6953 // Place buttons horizontally if they fit.
6954 horizWidth = dlgPaddingX;
6955 pstart = tbuffer;
6956 i = 0;
6957 do
6958 {
6959 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6960 if (pend == NULL)
6961 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006962 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (textWidth < minButtonWidth)
6964 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006965 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 buttonWidths[i] = textWidth;
6967 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006968 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 pstart = pend + 1;
6970 } while (*pend != NUL);
6971
6972 if (horizWidth > maxDialogWidth)
6973 vertical = TRUE; // Too wide to fit on the screen.
6974 else if (horizWidth > dlgwidth)
6975 dlgwidth = horizWidth;
6976 }
6977
6978 if (vertical)
6979 {
6980 // Stack buttons vertically.
6981 pstart = tbuffer;
6982 do
6983 {
6984 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6985 if (pend == NULL)
6986 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006987 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006988 textWidth += dlgPaddingX; // Padding within button
6989 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (textWidth > dlgwidth)
6991 dlgwidth = textWidth;
6992 pstart = pend + 1;
6993 } while (*pend != NUL);
6994 }
6995
6996 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
Bram Moolenaar734a8672019-12-02 22:49:38 +01006999 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007000 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
7002 add_long(lStyle);
7003 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007004 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 add_word(0); // NumberOfItems(will change later)
7006 add_word(10); // x
7007 add_word(10); // y
7008 add_word(PixelToDialogX(dlgwidth)); // cx
7009
7010 // Dialog height.
7011 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007012 dlgheight = msgheight + 2 * dlgPaddingY
7013 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 else
7015 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7016
7017 // Dialog needs to be taller if contains an edit box.
7018 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7019 if (textfield != NULL)
7020 dlgheight += editboxheight;
7021
Bram Moolenaar734a8672019-12-02 22:49:38 +01007022 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007023 if (dlgheight > maxDialogHeight)
7024 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007025 msgheight = msgheight - (dlgheight - maxDialogHeight);
7026 dlgheight = maxDialogHeight;
7027 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007029 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007030 }
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 add_word(PixelToDialogY(dlgheight));
7033
7034 add_word(0); // Menu
7035 add_word(0); // Class
7036
Bram Moolenaar734a8672019-12-02 22:49:38 +01007037 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007038 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7039 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 p += nchar;
7041
K.Takatad1c58992022-01-23 12:31:57 +00007042 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007043# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007044 if (use_lfSysmenu)
7045 {
7046 // point size
7047 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7048 GetDeviceCaps(hdc, LOGPIXELSY));
7049 wcscpy(p, lfSysmenu.lfFaceName);
7050 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 }
K.Takatad1c58992022-01-23 12:31:57 +00007052 else
7053# endif
7054 {
7055 *p++ = DLG_FONT_POINT_SIZE; // point size
7056 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7057 }
7058 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
7060 buttonYpos = msgheight + 2 * dlgPaddingY;
7061
7062 if (textfield != NULL)
7063 buttonYpos += editboxheight;
7064
7065 pstart = tbuffer;
7066 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007067 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 for (i = 0; i < numButtons; i++)
7069 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007070 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 for ( pend = pstart;
7072 *pend && (*pend != DLG_BUTTON_SEP);
7073 pend++)
7074 ;
7075
7076 if (*pend)
7077 *pend = '\0';
7078
7079 /*
7080 * old NOTE:
7081 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7082 * the focus to the first tab-able button and in so doing makes that
7083 * the default!! Grrr. Workaround: Make the default button the only
7084 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7085 * he/she can use arrow keys.
7086 *
7087 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007088 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 * dialog. Also needed for when the textfield is the default control.
7090 * It appears to work now (perhaps not on Win95?).
7091 */
7092 if (vertical)
7093 {
7094 p = add_dialog_element(p,
7095 (i == dfltbutton
7096 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7097 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007098 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 + 2 * fontHeight * i),
7100 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7101 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007102 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 }
7104 else
7105 {
7106 p = add_dialog_element(p,
7107 (i == dfltbutton
7108 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7109 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007110 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 PixelToDialogX(buttonWidths[i]),
7112 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007113 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007115 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
7117 *pnumitems += numButtons;
7118
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 p = add_dialog_element(p, SS_ICON,
7121 PixelToDialogX(dlgPaddingX),
7122 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007123 PixelToDialogX(dlg_icon_width),
7124 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7126 dlg_icons[type]);
7127
Bram Moolenaar734a8672019-12-02 22:49:38 +01007128 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007129 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007130 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007131 PixelToDialogY(dlgPaddingY),
7132 (WORD)(PixelToDialogX(messageWidth) + 1),
7133 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007134 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaar734a8672019-12-02 22:49:38 +01007136 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (textfield != NULL)
7138 {
7139 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7140 PixelToDialogX(2 * dlgPaddingX),
7141 PixelToDialogY(2 * dlgPaddingY + msgheight),
7142 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7143 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007144 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 *pnumitems += 1;
7146 }
7147
7148 *pnumitems += 2;
7149
7150 SelectFont(hdc, oldFont);
7151 DeleteObject(font);
7152 ReleaseDC(hwnd, hdc);
7153
Bram Moolenaar734a8672019-12-02 22:49:38 +01007154 // Let the dialog_callback() function know which button to make default
7155 // If we have an edit box, make that the default. We also need to tell
7156 // dialog_callback() if this dialog contains an edit box or not. We do
7157 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (textfield != NULL)
7159 {
7160 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7161 s_textfield = textfield;
7162 }
7163 else
7164 {
7165 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7166 s_textfield = NULL;
7167 }
7168
Bram Moolenaar734a8672019-12-02 22:49:38 +01007169 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007171 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 (LPDLGTEMPLATE)pdlgtemplate,
7173 s_hwnd,
7174 (DLGPROC)dialog_callback);
7175
7176 LocalFree(LocalHandle(pdlgtemplate));
7177 vim_free(tbuffer);
7178 vim_free(buttonWidths);
7179 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007180 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar734a8672019-12-02 22:49:38 +01007182 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 (void)SetFocus(s_hwnd);
7184
7185 return nchar;
7186}
7187
Bram Moolenaar734a8672019-12-02 22:49:38 +01007188#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190/*
7191 * Put a simple element (basic class) onto a dialog template in memory.
7192 * return a pointer to where the next item should be added.
7193 *
7194 * parameters:
7195 * lStyle = additional style flags
7196 * (be careful, NT3.51 & Win32s will ignore the new ones)
7197 * x,y = x & y positions IN DIALOG UNITS
7198 * w,h = width and height IN DIALOG UNITS
7199 * Id = ID used in messages
7200 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7201 * caption = usually text or resource name
7202 *
7203 * TODO: use the length information noted here to enable the dialog creation
7204 * routines to work out more exactly how much memory they need to alloc.
7205 */
7206 static PWORD
7207add_dialog_element(
7208 PWORD p,
7209 DWORD lStyle,
7210 WORD x,
7211 WORD y,
7212 WORD w,
7213 WORD h,
7214 WORD Id,
7215 WORD clss,
7216 const char *caption)
7217{
7218 int nchar;
7219
Bram Moolenaar734a8672019-12-02 22:49:38 +01007220 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7222 *p++ = LOWORD(lStyle);
7223 *p++ = HIWORD(lStyle);
7224 *p++ = 0; // LOWORD (lExtendedStyle)
7225 *p++ = 0; // HIWORD (lExtendedStyle)
7226 *p++ = x;
7227 *p++ = y;
7228 *p++ = w;
7229 *p++ = h;
7230 *p++ = Id; //9 or 10 words in all
7231
7232 *p++ = (WORD)0xffff;
7233 *p++ = clss; //2 more here
7234
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007235 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 p += nchar;
7237
7238 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7239
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007240 return p; // total = 15 + strlen(caption) words
7241 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242}
7243
7244
7245/*
7246 * Helper routine. Take an input pointer, return closest pointer that is
7247 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7248 */
7249 static LPWORD
7250lpwAlign(
7251 LPWORD lpIn)
7252{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007253 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007255 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 ul += 3;
7257 ul >>= 2;
7258 ul <<= 2;
7259 return (LPWORD)ul;
7260}
7261
7262/*
7263 * Helper routine. Takes second parameter as Ansi string, copies it to first
7264 * parameter as wide character (16-bits / char) string, and returns integer
7265 * number of wide characters (words) in string (including the trailing wide
7266 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007267 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7268 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 static int
7270nCopyAnsiToWideChar(
7271 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007272 LPSTR lpAnsiIn,
7273 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007276 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 int i;
7278 WCHAR *wn;
7279
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007280 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007282 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007283 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 if (wn != NULL)
7285 {
7286 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 vim_free(wn);
7289 }
7290 }
7291 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007292 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 nChar = MultiByteToWideChar(
7294 enc_codepage > 0 ? enc_codepage : CP_ACP,
7295 MB_PRECOMPOSED,
7296 lpAnsiIn, len,
7297 lpWCStr, len);
7298 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007299 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
7302 return nChar;
7303}
7304
7305
7306#ifdef FEAT_TEAROFF
7307/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007308 * Lookup menu handle from "menu_id".
7309 */
7310 static HMENU
7311tearoff_lookup_menuhandle(
7312 vimmenu_T *menu,
7313 WORD menu_id)
7314{
7315 for ( ; menu != NULL; menu = menu->next)
7316 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007317 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007318 continue;
7319 if (menu_is_separator(menu->dname))
7320 continue;
7321 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7322 return menu->submenu_id;
7323 }
7324 return NULL;
7325}
7326
7327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 * The callback function for all the modeless dialogs that make up the
7329 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7330 * thinking its menus have been clicked), and go away when closed.
7331 */
7332 static LRESULT CALLBACK
7333tearoff_callback(
7334 HWND hwnd,
7335 UINT message,
7336 WPARAM wParam,
7337 LPARAM lParam)
7338{
7339 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007340 {
7341 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
Bram Moolenaar734a8672019-12-02 22:49:38 +01007345 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 HandleMouseHide(message, lParam);
7347
7348 if (message == WM_COMMAND)
7349 {
7350 if ((WORD)(LOWORD(wParam)) & 0x8000)
7351 {
7352 POINT mp;
7353 RECT rect;
7354
7355 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7356 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007357 vimmenu_T *menu;
7358
7359 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007361 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7363 (int)rect.right - 8,
7364 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007365 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 s_hwnd,
7367 NULL);
7368 /*
7369 * NOTE: The pop-up menu can eat the mouse up event.
7370 * We deal with this in normal.c.
7371 */
7372 }
7373 }
7374 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007375 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7377 /*
7378 * Give main window the focus back: this is so after
7379 * choosing a tearoff button you can start typing again
7380 * straight away.
7381 */
7382 (void)SetFocus(s_hwnd);
7383 return TRUE;
7384 }
7385 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7386 {
7387 DestroyWindow(hwnd);
7388 return TRUE;
7389 }
7390
Bram Moolenaar734a8672019-12-02 22:49:38 +01007391 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 if (message == WM_EXITSIZEMOVE)
7393 (void)SetActiveWindow(s_hwnd);
7394
7395 return FALSE;
7396}
7397#endif
7398
7399
7400/*
K.Takatad1c58992022-01-23 12:31:57 +00007401 * Computes the dialog base units based on the current dialog font.
7402 * We don't use the GetDialogBaseUnits() API, because we don't use the
7403 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 */
7405 static void
7406get_dialog_font_metrics(void)
7407{
7408 HDC hdc;
7409 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 SIZE size;
7411#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007412 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#endif
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007416 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007417 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007418 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#endif
7420 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007421 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
K.Takatad1c58992022-01-23 12:31:57 +00007423 hdc = GetDC(s_hwnd);
7424 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007425 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007426 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
K.Takataabe628e2022-01-23 16:25:17 +00007428 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007429 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431
7432#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7433/*
7434 * Create a pseudo-"tearoff menu" based on the child
7435 * items of a given menu pointer.
7436 */
7437 static void
7438gui_mch_tearoff(
7439 char_u *title,
7440 vimmenu_T *menu,
7441 int initX,
7442 int initY)
7443{
7444 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7445 int template_len;
7446 int nchar, textWidth, submenuWidth;
7447 DWORD lStyle;
7448 DWORD lExtendedStyle;
7449 WORD dlgwidth;
7450 WORD menuID;
7451 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007452 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 vimmenu_T *the_menu = menu;
7454 HWND hwnd;
7455 HDC hdc;
7456 HFONT font, oldFont;
7457 int col, spaceWidth, len;
7458 int columnWidths[2];
7459 char_u *label, *text;
7460 int acLen = 0;
7461 int nameLen;
7462 int padding0, padding1, padding2 = 0;
7463 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007464 int x;
7465 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007466# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007467 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471 /*
7472 * If this menu is already torn off, move it to the mouse position.
7473 */
7474 if (IsWindow(menu->tearoff_handle))
7475 {
7476 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007477 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 {
7479 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7480 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7481 }
7482 return;
7483 }
7484
7485 /*
7486 * Create a new tearoff.
7487 */
7488 if (*title == MNU_HIDDEN_CHAR)
7489 title++;
7490
Bram Moolenaar734a8672019-12-02 22:49:38 +01007491 // Allocate memory to store the dialog template. It's made bigger when
7492 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 template_len = DLG_ALLOC_SIZE;
7494 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7495 if (p == NULL)
7496 return;
7497
7498 hwnd = GetDesktopWindow();
7499 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007500# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7502 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007503 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 use_lfSysmenu = TRUE;
7505 }
7506 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007507# endif
K.Takatad1c58992022-01-23 12:31:57 +00007508 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7509 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7510
7511 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
Bram Moolenaar734a8672019-12-02 22:49:38 +01007513 // Calculate width of a single space. Used for padding columns to the
7514 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007515 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Bram Moolenaar734a8672019-12-02 22:49:38 +01007517 // Figure out max width of the text column, the accelerator column and the
7518 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 submenuWidth = 0;
7520 for (col = 0; col < 2; col++)
7521 {
7522 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007523 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007525 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 text = (col == 0) ? pmenu->dname : pmenu->actext;
7527 if (text != NULL && *text != NUL)
7528 {
7529 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7530 if (textWidth > columnWidths[col])
7531 columnWidths[col] = textWidth;
7532 }
7533 if (pmenu->children != NULL)
7534 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7535 }
7536 }
7537 if (columnWidths[1] == 0)
7538 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007539 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 if (submenuWidth != 0)
7541 columnWidths[0] += submenuWidth;
7542 else
7543 columnWidths[0] += spaceWidth;
7544 }
7545 else
7546 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007547 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7549 columnWidths[1] += submenuWidth;
7550 }
7551
7552 /*
7553 * Now find the total width of our 'menu'.
7554 */
7555 textWidth = columnWidths[0] + columnWidths[1];
7556 if (submenuWidth != 0)
7557 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007558 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7560 textWidth += submenuWidth;
7561 }
7562 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7563 if (textWidth > dlgwidth)
7564 dlgwidth = textWidth;
7565 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7566
Bram Moolenaar734a8672019-12-02 22:49:38 +01007567 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007568 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
7570 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7571 *p++ = LOWORD(lStyle);
7572 *p++ = HIWORD(lStyle);
7573 *p++ = LOWORD(lExtendedStyle);
7574 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007575 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007577 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007579 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 else
7581 *p++ = PixelToDialogX(initX); // x
7582 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007583 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 else
7585 *p++ = PixelToDialogY(initY); // y
7586 *p++ = PixelToDialogX(dlgwidth); // cx
7587 ptrueheight = p;
7588 *p++ = 0; // dialog height: changed later anyway
7589 *p++ = 0; // Menu
7590 *p++ = 0; // Class
7591
Bram Moolenaar734a8672019-12-02 22:49:38 +01007592 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007594 ? (LPSTR)title
7595 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 p += nchar;
7597
K.Takatad1c58992022-01-23 12:31:57 +00007598 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007599# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007600 if (use_lfSysmenu)
7601 {
7602 // point size
7603 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7604 GetDeviceCaps(hdc, LOGPIXELSY));
7605 wcscpy(p, lfSysmenu.lfFaceName);
7606 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
K.Takatad1c58992022-01-23 12:31:57 +00007608 else
7609# endif
7610 {
7611 *p++ = DLG_FONT_POINT_SIZE; // point size
7612 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7613 }
7614 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615
7616 /*
7617 * Loop over all the items in the menu.
7618 * But skip over the tearbar.
7619 */
7620 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7621 menu = menu->children->next;
7622 else
7623 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007624 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 for ( ; menu != NULL; menu = menu->next)
7626 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007627 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 continue;
7629 if (menu_is_separator(menu->dname))
7630 {
7631 sepPadding += 3;
7632 continue;
7633 }
7634
Bram Moolenaar734a8672019-12-02 22:49:38 +01007635 // Check if there still is plenty of room in the template. Make it
7636 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7638 {
7639 WORD *newp;
7640
7641 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7642 if (newp != NULL)
7643 {
7644 template_len += 4096;
7645 mch_memmove(newp, pdlgtemplate,
7646 (char *)p - (char *)pdlgtemplate);
7647 p = newp + (p - pdlgtemplate);
7648 pnumitems = newp + (pnumitems - pdlgtemplate);
7649 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7650 LocalFree(LocalHandle(pdlgtemplate));
7651 pdlgtemplate = newp;
7652 }
7653 }
7654
Bram Moolenaar734a8672019-12-02 22:49:38 +01007655 // Figure out minimal length of this menu label. Use "name" for the
7656 // actual text, "dname" for estimating the displayed size. "name"
7657 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 len = nameLen = (int)STRLEN(menu->name);
7659 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7660 (int)STRLEN(menu->dname))) / spaceWidth;
7661 len += padding0;
7662
7663 if (menu->actext != NULL)
7664 {
7665 acLen = (int)STRLEN(menu->actext);
7666 len += acLen;
7667 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7668 }
7669 else
7670 textWidth = 0;
7671 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7672 len += padding1;
7673
7674 if (menu->children == NULL)
7675 {
7676 padding2 = submenuWidth / spaceWidth;
7677 len += padding2;
7678 menuID = (WORD)(menu->id);
7679 }
7680 else
7681 {
7682 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007683 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685
Bram Moolenaar734a8672019-12-02 22:49:38 +01007686 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007687 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (label == NULL)
7689 break;
7690
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007691 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007692 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007694 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 while (padding0-- > 0)
7696 *text++ = ' ';
7697 if (menu->actext != NULL)
7698 {
7699 STRNCPY(text, menu->actext, acLen);
7700 text += acLen;
7701 }
7702 while (padding1-- > 0)
7703 *text++ = ' ';
7704 if (menu->children != NULL)
7705 {
7706 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7707 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7708 }
7709 else
7710 {
7711 while (padding2-- > 0)
7712 *text++ = ' ';
7713 }
7714 *text = NUL;
7715
7716 /*
7717 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7718 * W95/NT4 it makes the tear-off look more like a menu.
7719 */
7720 p = add_dialog_element(p,
7721 BS_PUSHBUTTON|BS_LEFT,
7722 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7723 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7724 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7725 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007726 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 vim_free(label);
7728 (*pnumitems)++;
7729 }
7730
7731 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7732
7733
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007735 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007736 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 (LPDLGTEMPLATE)pdlgtemplate,
7738 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007739 (DLGPROC)tearoff_callback,
7740 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741
7742 LocalFree(LocalHandle(pdlgtemplate));
7743 SelectFont(hdc, oldFont);
7744 DeleteObject(font);
7745 ReleaseDC(hwnd, hdc);
7746
7747 /*
7748 * Reassert ourselves as the active window. This is so that after creating
7749 * a tearoff, the user doesn't have to click with the mouse just to start
7750 * typing again!
7751 */
7752 (void)SetActiveWindow(s_hwnd);
7753
Bram Moolenaar734a8672019-12-02 22:49:38 +01007754 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 force_menu_update = TRUE;
7756}
7757#endif
7758
7759#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007760# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762/*
7763 * Create the toolbar, initially unpopulated.
7764 * (just like the menu, there are no defaults, it's all
7765 * set up through menu.vim)
7766 */
7767 static void
7768initialise_toolbar(void)
7769{
7770 InitCommonControls();
7771 s_toolbarhwnd = CreateToolbarEx(
7772 s_hwnd,
7773 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7774 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007775 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007776 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 IDR_TOOLBAR1, // id of initial bitmap
7778 NULL,
7779 0, // initial number of buttons
7780 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7781 TOOLBAR_BUTTON_HEIGHT,
7782 TOOLBAR_BUTTON_WIDTH,
7783 TOOLBAR_BUTTON_HEIGHT,
7784 sizeof(TBBUTTON)
7785 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007786
7787 // Remove transparency from the toolbar to prevent the main window
7788 // background colour showing through
7789 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7790 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7791
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007792 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007795
7796 update_toolbar_size();
7797}
7798
7799 static void
7800update_toolbar_size(void)
7801{
7802 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007803 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007804
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007805 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007806 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7807 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7808 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7809 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7810
7811 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7812 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7813 //TRACE("button size: %d, %d", w, h);
7814 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7815 gui.toolbar_height = h + 6;
7816
7817 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7818 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7819
7820 // TODO:
7821 // Currently, this function only updates the size of toolbar buttons.
7822 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823}
7824
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007825 static LRESULT CALLBACK
7826toolbar_wndproc(
7827 HWND hwnd,
7828 UINT uMsg,
7829 WPARAM wParam,
7830 LPARAM lParam)
7831{
7832 HandleMouseHide(uMsg, lParam);
7833 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7834}
7835
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 static int
7837get_toolbar_bitmap(vimmenu_T *menu)
7838{
7839 int i = -1;
7840
7841 /*
7842 * Check user bitmaps first, unless builtin is specified.
7843 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007844 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {
7846 char_u fname[MAXPATHL];
7847 HANDLE hbitmap = NULL;
7848
7849 if (menu->iconfile != NULL)
7850 {
7851 gui_find_iconfile(menu->iconfile, fname, "bmp");
7852 hbitmap = LoadImage(
7853 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007854 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 IMAGE_BITMAP,
7856 TOOLBAR_BUTTON_WIDTH,
7857 TOOLBAR_BUTTON_HEIGHT,
7858 LR_LOADFROMFILE |
7859 LR_LOADMAP3DCOLORS
7860 );
7861 }
7862
7863 /*
7864 * If the LoadImage call failed, or the "icon=" file
7865 * didn't exist or wasn't specified, try the menu name
7866 */
7867 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007868 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007869# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007870 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007871# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007872 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 hbitmap = LoadImage(
7874 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007875 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 IMAGE_BITMAP,
7877 TOOLBAR_BUTTON_WIDTH,
7878 TOOLBAR_BUTTON_HEIGHT,
7879 LR_LOADFROMFILE |
7880 LR_LOADMAP3DCOLORS
7881 );
7882
7883 if (hbitmap != NULL)
7884 {
7885 TBADDBITMAP tbAddBitmap;
7886
7887 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007888 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
7890 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7891 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007892 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894 }
7895 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7896 i = menu->iconidx;
7897
7898 return i;
7899}
7900#endif
7901
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007902#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7903 static void
7904initialise_tabline(void)
7905{
7906 InitCommonControls();
7907
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007908 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007909 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007910 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007911 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007912 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007913
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007914 gui.tabline_height = TABLINE_HEIGHT;
7915
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007916 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007917}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007918
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007919/*
7920 * Get tabpage_T from POINT.
7921 */
7922 static tabpage_T *
7923GetTabFromPoint(
7924 HWND hWnd,
7925 POINT pt)
7926{
7927 tabpage_T *ptp = NULL;
7928
7929 if (gui_mch_showing_tabline())
7930 {
7931 TCHITTESTINFO htinfo;
7932 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007933 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007934 if (s_tabhwnd == hWnd)
7935 {
7936 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7937 if (idx != -1)
7938 ptp = find_tabpage(idx + 1);
7939 }
7940 }
7941 return ptp;
7942}
7943
7944static POINT s_pt = {0, 0};
7945static HCURSOR s_hCursor = NULL;
7946
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007947 static LRESULT CALLBACK
7948tabline_wndproc(
7949 HWND hwnd,
7950 UINT uMsg,
7951 WPARAM wParam,
7952 LPARAM lParam)
7953{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007954 POINT pt;
7955 tabpage_T *tp;
7956 RECT rect;
7957 int nCenter;
7958 int idx0;
7959 int idx1;
7960
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007961 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007962
7963 switch (uMsg)
7964 {
7965 case WM_LBUTTONDOWN:
7966 {
7967 s_pt.x = GET_X_LPARAM(lParam);
7968 s_pt.y = GET_Y_LPARAM(lParam);
7969 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007970 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007971 break;
7972 }
7973 case WM_MOUSEMOVE:
7974 if (GetCapture() == hwnd
7975 && ((wParam & MK_LBUTTON)) != 0)
7976 {
7977 pt.x = GET_X_LPARAM(lParam);
7978 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007979 if (abs(pt.x - s_pt.x) >
7980 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007981 {
7982 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7983
7984 tp = GetTabFromPoint(hwnd, pt);
7985 if (tp != NULL)
7986 {
7987 idx0 = tabpage_index(curtab) - 1;
7988 idx1 = tabpage_index(tp) - 1;
7989
7990 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7991 nCenter = rect.left + (rect.right - rect.left) / 2;
7992
Bram Moolenaar734a8672019-12-02 22:49:38 +01007993 // Check if the mouse cursor goes over the center of
7994 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007995 if ((idx0 < idx1) && (nCenter < pt.x))
7996 {
7997 tabpage_move(idx1 + 1);
7998 update_screen(0);
7999 }
8000 else if ((idx1 < idx0) && (pt.x < nCenter))
8001 {
8002 tabpage_move(idx1);
8003 update_screen(0);
8004 }
8005 }
8006 }
8007 }
8008 break;
8009 case WM_LBUTTONUP:
8010 {
8011 if (GetCapture() == hwnd)
8012 {
8013 SetCursor(s_hCursor);
8014 ReleaseCapture();
8015 }
8016 break;
8017 }
8018 default:
8019 break;
8020 }
8021
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008022 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8023}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008024#endif
8025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8027/*
8028 * Make the GUI window come to the foreground.
8029 */
8030 void
8031gui_mch_set_foreground(void)
8032{
8033 if (IsIconic(s_hwnd))
8034 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8035 SetForegroundWindow(s_hwnd);
8036}
8037#endif
8038
8039#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8040 static void
8041dyn_imm_load(void)
8042{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008043 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (hLibImm == NULL)
8045 return;
8046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008048 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008050 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008052 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008054 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008056 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008058 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008059 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008060 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008061 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008062 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008064 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008066 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008067 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008068 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
K.Takatab0b2b732022-01-19 12:59:21 +00008070 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 || pImmGetContext == NULL
8072 || pImmAssociateContext == NULL
8073 || pImmReleaseContext == NULL
8074 || pImmGetOpenStatus == NULL
8075 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008076 || pImmGetCompositionFontW == NULL
8077 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008079 || pImmGetConversionStatus == NULL
8080 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
8082 FreeLibrary(hLibImm);
8083 hLibImm = NULL;
8084 pImmGetContext = NULL;
8085 return;
8086 }
8087
8088 return;
8089}
8090
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#endif
8092
8093#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8094
8095# ifdef FEAT_XPM_W32
8096# define IMAGE_XPM 100
8097# endif
8098
8099typedef struct _signicon_t
8100{
8101 HANDLE hImage;
8102 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008103# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008104 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106} signicon_t;
8107
8108 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008109gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111 signicon_t *sign;
8112 int x, y, w, h;
8113
8114 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8115 return;
8116
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008117# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008118 if (IS_ENABLE_DIRECTX())
8119 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008120# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008121
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 x = TEXT_X(col);
8123 y = TEXT_Y(row);
8124 w = gui.char_width * 2;
8125 h = gui.char_height;
8126 switch (sign->uType)
8127 {
8128 case IMAGE_BITMAP:
8129 {
8130 HDC hdcMem;
8131 HBITMAP hbmpOld;
8132
8133 hdcMem = CreateCompatibleDC(s_hdc);
8134 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8135 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8136 SelectObject(hdcMem, hbmpOld);
8137 DeleteDC(hdcMem);
8138 }
8139 break;
8140 case IMAGE_ICON:
8141 case IMAGE_CURSOR:
8142 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8143 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008144# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 case IMAGE_XPM:
8146 {
8147 HDC hdcMem;
8148 HBITMAP hbmpOld;
8149
8150 hdcMem = CreateCompatibleDC(s_hdc);
8151 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008152 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8154
8155 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008156 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8158 SelectObject(hdcMem, hbmpOld);
8159 DeleteDC(hdcMem);
8160 }
8161 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 }
8164}
8165
8166 static void
8167close_signicon_image(signicon_t *sign)
8168{
8169 if (sign)
8170 switch (sign->uType)
8171 {
8172 case IMAGE_BITMAP:
8173 DeleteObject((HGDIOBJ)sign->hImage);
8174 break;
8175 case IMAGE_CURSOR:
8176 DestroyCursor((HCURSOR)sign->hImage);
8177 break;
8178 case IMAGE_ICON:
8179 DestroyIcon((HICON)sign->hImage);
8180 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008181# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 case IMAGE_XPM:
8183 DeleteObject((HBITMAP)sign->hImage);
8184 DeleteObject((HBITMAP)sign->hShape);
8185 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 }
8188}
8189
8190 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008191gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 signicon_t sign, *psign;
8194 char_u *ext;
8195
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008197 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (ext > signfile)
8199 {
8200 int do_load = 1;
8201
8202 if (!STRICMP(ext, ".bmp"))
8203 sign.uType = IMAGE_BITMAP;
8204 else if (!STRICMP(ext, ".ico"))
8205 sign.uType = IMAGE_ICON;
8206 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8207 sign.uType = IMAGE_CURSOR;
8208 else
8209 do_load = 0;
8210
8211 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008212 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 gui.char_width * 2, gui.char_height,
8214 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008215# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (!STRICMP(ext, ".xpm"))
8217 {
8218 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008219 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8220 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
8224
8225 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008226 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 *psign = sign;
8228
8229 if (!psign)
8230 {
8231 if (sign.hImage)
8232 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008233 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235 return (void *)psign;
8236
8237}
8238
8239 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008240gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241{
8242 if (sign)
8243 {
8244 close_signicon_image((signicon_t *)sign);
8245 vim_free(sign);
8246 }
8247}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008250#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
Bram Moolenaar734a8672019-12-02 22:49:38 +01008252/*
8253 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008254 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008256 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8258 * to get current mouse position).
8259 *
8260 * Trying to use as more Windows services as possible, and as less
8261 * IE version as possible :)).
8262 *
8263 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8264 * BalloonEval struct.
8265 * 2) Enable/Disable simply create/kill BalloonEval Timer
8266 * 3) When there was enough inactivity, timer procedure posts
8267 * async request to debugger
8268 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8269 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008270 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 */
8272
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008273 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008274make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008275{
K.Takata76687d22022-01-25 10:31:37 +00008276 TOOLINFOW *pti;
8277 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008278
K.Takata76687d22022-01-25 10:31:37 +00008279 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008280 if (pti == NULL)
8281 return;
8282
8283 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8284 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8285 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008286 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008287
8288 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8289 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8290
K.Takata76687d22022-01-25 10:31:37 +00008291 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008292 pti->uFlags = TTF_SUBCLASS;
8293 pti->hwnd = beval->target;
8294 pti->hinst = 0; // Don't use string resources
8295 pti->uId = ID_BEVAL_TOOLTIP;
8296
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008297 pti->lpszText = LPSTR_TEXTCALLBACKW;
8298 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8299 pti->lParam = (LPARAM)beval->tofree;
8300 // switch multiline tooltips on
8301 if (GetClientRect(s_textArea, &rect))
8302 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8303 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008304
8305 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8306 pti->rect.left = pt.x - 3;
8307 pti->rect.top = pt.y - 3;
8308 pti->rect.right = pt.x + 3;
8309 pti->rect.bottom = pt.y + 3;
8310
8311 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8312 // Make tooltip appear sooner.
8313 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8314 // I've performed some tests and it seems the longest possible life time
8315 // of tooltip is 30 seconds.
8316 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8317 /*
8318 * HACK: force tooltip to appear, because it'll not appear until
8319 * first mouse move. D*mn M$
8320 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8321 */
8322 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8323 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8324 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008325}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008326
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008328delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008330 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008334beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008335 HWND hwnd UNUSED,
8336 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008337 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008338 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 POINT pt;
8341 RECT rect;
8342
8343 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8344 return;
8345
8346 GetCursorPos(&pt);
8347 if (WindowFromPoint(pt) != s_textArea)
8348 return;
8349
8350 ScreenToClient(s_textArea, &pt);
8351 GetClientRect(s_textArea, &rect);
8352 if (!PtInRect(&rect, pt))
8353 return;
8354
K.Takataa8ec4912022-02-03 14:32:33 +00008355 if (last_user_activity > 0
8356 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 && (cur_beval->showState != ShS_PENDING
8358 || abs(cur_beval->x - pt.x) > 3
8359 || abs(cur_beval->y - pt.y) > 3))
8360 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008361 // Pointer resting in one place long enough, it's time to show
8362 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 cur_beval->showState = ShS_PENDING;
8364 cur_beval->x = pt.x;
8365 cur_beval->y = pt.y;
8366
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (cur_beval->msgCB != NULL)
8368 (*cur_beval->msgCB)(cur_beval, 0);
8369 }
8370}
8371
8372 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008373gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
K.Takataa8ec4912022-02-03 14:32:33 +00008375 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376}
8377
8378 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008379gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 if (beval == NULL)
8382 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008383 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8384 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385}
8386
8387 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008388gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389{
8390 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008391
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008392 vim_free(beval->msg);
8393 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8394 if (beval->msg == NULL)
8395 {
8396 delete_tooltip(beval);
8397 beval->showState = ShS_NEUTRAL;
8398 return;
8399 }
8400
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 if (beval->showState == ShS_SHOWING)
8402 return;
8403 GetCursorPos(&pt);
8404 ScreenToClient(s_textArea, &pt);
8405
8406 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008408 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 gui_mch_disable_beval_area(cur_beval);
8410 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008411 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413}
8414
8415 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008416gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008417 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008418 char_u *mesg,
8419 void (*mesgCB)(BalloonEval *, int),
8420 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008422 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 BalloonEval *beval;
8424
8425 if (mesg != NULL && mesgCB != NULL)
8426 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008427 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 return NULL;
8429 }
8430
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008431 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (beval != NULL)
8433 {
8434 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 beval->msg = mesg;
8438 beval->msgCB = mesgCB;
8439 beval->clientData = clientData;
8440
8441 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 cur_beval = beval;
8443
8444 if (p_beval)
8445 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 }
8447 return beval;
8448}
8449
8450 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008451Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008453 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 return;
8455
8456 if (cur_beval != NULL)
8457 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008458 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008460 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008461 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008462 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 delete_tooltip(cur_beval);
8464 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
8466 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008467 break;
8468 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008469 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008470 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008471 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008472 info->lpszText = (LPSTR)info->lParam;
8473 info->uFlags |= TTF_DI_SETITEM;
8474 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008475 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008476 case TTN_GETDISPINFOW:
8477 {
8478 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008479 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008480 info->lpszText = (LPWSTR)info->lParam;
8481 info->uFlags |= TTF_DI_SETITEM;
8482 }
8483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 }
8485 }
8486}
8487
8488 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008489track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8492 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008493 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494}
8495
8496 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008497gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008499# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008500 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008501# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008502 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 vim_free(beval);
8504}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008505#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
8507#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8508/*
8509 * We have multiple signs to draw at the same location. Draw the
8510 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8511 */
8512 void
8513netbeans_draw_multisign_indicator(int row)
8514{
8515 int i;
8516 int y;
8517 int x;
8518
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008519 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008520 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008521
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 x = 0;
8523 y = TEXT_Y(row);
8524
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008525# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008526 if (IS_ENABLE_DIRECTX())
8527 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008528# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 for (i = 0; i < gui.char_height - 3; i++)
8531 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8532
8533 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8534 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8535 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8536 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8537 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8538 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8539 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8540}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008541#endif