blob: 0935ecfd12233e74e652c964868f0e27cac21da7 [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
Anton Sharonov3f026672022-07-26 21:26:18 +010032// values for "dead_key"
33#define DEAD_KEY_OFF 0 // no dead key
34#define DEAD_KEY_SET_DEFAULT 1 // dead key pressed
35#define DEAD_KEY_TRANSIENT_IN_ON_CHAR 2 // wait for next key press
36#define DEAD_KEY_SKIP_ON_CHAR 3 // skip next _OnChar()
37
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010038#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020039static DWriteContext *s_dwc = NULL;
40static int s_directx_enabled = 0;
41static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010042# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010043static int directx_enabled(void);
44static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010045#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020046
Bram Moolenaar065bbac2016-02-20 13:08:46 +010047#ifdef FEAT_MENU
48static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000049#else
50# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010051#endif
52
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
54 int
55gui_mch_set_rendering_options(char_u *s)
56{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010057# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020058 char_u *p, *q;
59
60 int dx_enable = 0;
61 int dx_flags = 0;
62 float dx_gamma = 0.0f;
63 float dx_contrast = 0.0f;
64 float dx_level = 0.0f;
65 int dx_geom = 0;
66 int dx_renmode = 0;
67 int dx_taamode = 0;
68
Bram Moolenaar734a8672019-12-02 22:49:38 +010069 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020070 for (p = s; p != NULL && *p != NUL; )
71 {
72 char_u item[256];
73 char_u name[128];
74 char_u value[128];
75
Bram Moolenaarcde88542015-08-11 19:14:00 +020076 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020077 if (p == NULL)
78 break;
79 q = &item[0];
80 copy_option_part(&q, name, sizeof(name), ":");
81 if (q == NULL)
82 return FAIL;
83 copy_option_part(&q, value, sizeof(value), ":");
84
85 if (STRCMP(name, "type") == 0)
86 {
87 if (STRCMP(value, "directx") == 0)
88 dx_enable = 1;
89 else
90 return FAIL;
91 }
92 else if (STRCMP(name, "gamma") == 0)
93 {
94 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "contrast") == 0)
98 {
99 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "level") == 0)
103 {
104 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 }
107 else if (STRCMP(name, "geom") == 0)
108 {
109 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100110 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200111 if (dx_geom < 0 || dx_geom > 2)
112 return FAIL;
113 }
114 else if (STRCMP(name, "renmode") == 0)
115 {
116 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100117 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200118 if (dx_renmode < 0 || dx_renmode > 6)
119 return FAIL;
120 }
121 else if (STRCMP(name, "taamode") == 0)
122 {
123 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100124 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200125 if (dx_taamode < 0 || dx_taamode > 3)
126 return FAIL;
127 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100128 else if (STRCMP(name, "scrlines") == 0)
129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100130 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100131 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 else
133 return FAIL;
134 }
135
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100136 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100137 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100138
Bram Moolenaar734a8672019-12-02 22:49:38 +0100139 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200140 if (dx_enable)
141 {
142 if (!directx_enabled())
143 return FAIL;
144 DWriteContext_SetRenderingParams(s_dwc, NULL);
145 if (dx_flags)
146 {
147 DWriteRenderingParams param;
148 DWriteContext_GetRenderingParams(s_dwc, &param);
149 if (dx_flags & (1 << 0))
150 param.gamma = dx_gamma;
151 if (dx_flags & (1 << 1))
152 param.enhancedContrast = dx_contrast;
153 if (dx_flags & (1 << 2))
154 param.clearTypeLevel = dx_level;
155 if (dx_flags & (1 << 3))
156 param.pixelGeometry = dx_geom;
157 if (dx_flags & (1 << 4))
158 param.renderingMode = dx_renmode;
159 if (dx_flags & (1 << 5))
160 param.textAntialiasMode = dx_taamode;
161 DWriteContext_SetRenderingParams(s_dwc, &param);
162 }
163 }
164 s_directx_enabled = dx_enable;
165
166 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100167# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200168 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100169# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200170}
171#endif
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
174 * These are new in Windows ME/XP, only defined in recent compilers.
175 */
176#ifndef HANDLE_WM_XBUTTONUP
177# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180#ifndef HANDLE_WM_XBUTTONDOWN
181# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
182 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
183#endif
184#ifndef HANDLE_WM_XBUTTONDBLCLK
185# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
186 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
187#endif
188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100189
Bram Moolenaar734a8672019-12-02 22:49:38 +0100190#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100191#ifdef DEBUG
192# include <tchar.h>
193#endif
194
Bram Moolenaar734a8672019-12-02 22:49:38 +0100195// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100196#ifndef PROTO
197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifndef __MINGW32__
199# include <shellapi.h>
200# endif
K.Takata2da11a42022-09-10 13:03:12 +0100201# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100202# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000218#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
K.Takatac81e9bf2022-01-16 14:15:49 +0000225#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100226# define WM_DPICHANGED 0x02E0
227#endif
228
Ken Takata7086b3e2023-10-02 21:26:03 +0200229#ifndef WM_GETDPISCALEDSIZE
230# define WM_GETDPISCALEDSIZE 0x02E4
231#endif
232
LemonBoy365d8f72022-05-05 19:23:07 +0100233#ifndef WM_MOUSEHWHEEL
234# define WM_MOUSEHWHEEL 0x020E
235#endif
236
237#ifndef SPI_GETWHEELSCROLLCHARS
238# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000239#endif
240
LemonBoyc27747e2022-05-07 12:25:40 +0100241#ifndef SPI_SETWHEELSCROLLCHARS
242# define SPI_SETWHEELSCROLLCHARS 0x006D
243#endif
244
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247 * Define a few things for generating prototypes. This is just to avoid
248 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100250# define APIENTRY
251# define CALLBACK
252# define CONST
253# define FAR
254# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200255# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200256# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100257# define _cdecl
258typedef int BOOL;
259typedef int BYTE;
260typedef int DWORD;
261typedef int WCHAR;
262typedef int ENUMLOGFONT;
263typedef int FINDREPLACE;
264typedef int HANDLE;
265typedef int HBITMAP;
266typedef int HBRUSH;
267typedef int HDROP;
268typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100269typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100270typedef int LPARAM;
271typedef int LPCREATESTRUCT;
272typedef int LPCSTR;
273typedef int LPCTSTR;
274typedef int LPRECT;
275typedef int LPSTR;
276typedef int LPWINDOWPOS;
277typedef int LPWORD;
278typedef int LRESULT;
279typedef int HRESULT;
280# undef MSG
281typedef int MSG;
282typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100283typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284typedef int OSVERSIONINFO;
285typedef int PWORD;
286typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100287typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288typedef int UINT;
289typedef int WORD;
290typedef int WPARAM;
291typedef int POINT;
292typedef void *HINSTANCE;
293typedef void *HMENU;
294typedef void *HWND;
295typedef void *HDC;
296typedef void VOID;
297typedef int LPNMHDR;
298typedef int LONG;
299typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200300typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200301typedef int COLORREF;
302typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303#endif
304
K.Takata45f9cfb2022-01-21 11:11:00 +0000305static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100306static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307static void clear_rect(RECT *rcp);
308
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static WORD s_dlgfntheight; // height of the dialog font
310static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100311
312#ifdef FEAT_MENU
313static HMENU s_menuBar = NULL;
314#endif
315#ifdef FEAT_TEAROFF
316static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100317static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318#endif
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320// Flag that is set while processing a message that must not be interrupted by
321// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322static int s_busy_processing = FALSE;
323
Bram Moolenaar734a8672019-12-02 22:49:38 +0100324static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325
326#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000327static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200328static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200330static int s_findrep_is_find; // TRUE for find dialog, FALSE
331 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100332#endif
333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100334HWND s_hwnd = NULL;
335static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100336static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100337
338#ifdef FEAT_TOOLBAR
339static HWND s_toolbarhwnd = NULL;
340static WNDPROC s_toolbar_wndproc = NULL;
341#endif
342
343#ifdef FEAT_GUI_TABLINE
344static HWND s_tabhwnd = NULL;
345static WNDPROC s_tabline_wndproc = NULL;
346static int showing_tabline = 0;
347#endif
348
349static WPARAM s_wParam = 0;
350static LPARAM s_lParam = 0;
351
352static HWND s_textArea = NULL;
353static UINT s_uMsg = 0;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100356
357static int s_need_activate = FALSE;
358
Bram Moolenaar734a8672019-12-02 22:49:38 +0100359// This variable is set when waiting for an event, which is the only moment
360// scrollbar dragging can be done directly. It's not allowed while commands
361// are executed, because it may move the cursor and that may cause unexpected
362// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100363static int allow_scrollbar = FALSE;
364
K.Takatac81e9bf2022-01-16 14:15:49 +0000365#ifndef _DPI_AWARENESS_CONTEXTS_
366typedef HANDLE DPI_AWARENESS_CONTEXT;
367
368typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000369 DPI_AWARENESS_INVALID = -1,
370 DPI_AWARENESS_UNAWARE = 0,
371 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000372 DPI_AWARENESS_PER_MONITOR_AWARE = 2
373} DPI_AWARENESS;
374
K.Takatab0b2b732022-01-19 12:59:21 +0000375# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
376# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000377# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
378# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
379# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
380#endif
381
382#define DEFAULT_DPI 96
383static int s_dpi = DEFAULT_DPI;
384static BOOL s_in_dpichanged = FALSE;
385static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
Ken Takata7086b3e2023-10-02 21:26:03 +0200386static RECT s_suggested_rect;
K.Takatac81e9bf2022-01-16 14:15:49 +0000387
388static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
389static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
390static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
391//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
392static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
393static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
394
K.Takatac81e9bf2022-01-16 14:15:49 +0000395 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100396stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000397{
398 return GetSystemMetrics(nIndex);
399}
400
401 static int
402adjust_fontsize_by_dpi(int size)
403{
404 return size * s_dpi / (int)pGetDpiForSystem();
405}
406
407 static int
408adjust_by_system_dpi(int size)
409{
410 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
411}
412
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100413#if defined(FEAT_DIRECTX)
414 static int
415directx_enabled(void)
416{
417 if (s_dwc != NULL)
418 return 1;
419 else if (s_directx_load_attempted)
420 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100421 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100422 DWrite_Init();
423 s_directx_load_attempted = 1;
424 s_dwc = DWriteContext_Open();
425 directx_binddc();
426 return s_dwc != NULL ? 1 : 0;
427}
428
429 static void
430directx_binddc(void)
431{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000432 if (s_textArea == NULL)
433 return;
434
435 RECT rect;
436 GetClientRect(s_textArea, &rect);
437 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100438}
439#endif
440
Bram Moolenaar734a8672019-12-02 22:49:38 +0100441extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100442
443static struct
444{
445 UINT key_sym;
446 char_u vim_code0;
447 char_u vim_code1;
448} special_keys[] =
449{
450 {VK_UP, 'k', 'u'},
451 {VK_DOWN, 'k', 'd'},
452 {VK_LEFT, 'k', 'l'},
453 {VK_RIGHT, 'k', 'r'},
454
455 {VK_F1, 'k', '1'},
456 {VK_F2, 'k', '2'},
457 {VK_F3, 'k', '3'},
458 {VK_F4, 'k', '4'},
459 {VK_F5, 'k', '5'},
460 {VK_F6, 'k', '6'},
461 {VK_F7, 'k', '7'},
462 {VK_F8, 'k', '8'},
463 {VK_F9, 'k', '9'},
464 {VK_F10, 'k', ';'},
465
466 {VK_F11, 'F', '1'},
467 {VK_F12, 'F', '2'},
468 {VK_F13, 'F', '3'},
469 {VK_F14, 'F', '4'},
470 {VK_F15, 'F', '5'},
471 {VK_F16, 'F', '6'},
472 {VK_F17, 'F', '7'},
473 {VK_F18, 'F', '8'},
474 {VK_F19, 'F', '9'},
475 {VK_F20, 'F', 'A'},
476
477 {VK_F21, 'F', 'B'},
478#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100479 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100480#endif
481 {VK_F22, 'F', 'C'},
482 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100483 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100484
485 {VK_HELP, '%', '1'},
486 {VK_BACK, 'k', 'b'},
487 {VK_INSERT, 'k', 'I'},
488 {VK_DELETE, 'k', 'D'},
489 {VK_HOME, 'k', 'h'},
490 {VK_END, '@', '7'},
491 {VK_PRIOR, 'k', 'P'},
492 {VK_NEXT, 'k', 'N'},
493 {VK_PRINT, '%', '9'},
494 {VK_ADD, 'K', '6'},
495 {VK_SUBTRACT, 'K', '7'},
496 {VK_DIVIDE, 'K', '8'},
497 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100498 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100499 {VK_DECIMAL, 'K', 'B'},
500
501 {VK_NUMPAD0, 'K', 'C'},
502 {VK_NUMPAD1, 'K', 'D'},
503 {VK_NUMPAD2, 'K', 'E'},
504 {VK_NUMPAD3, 'K', 'F'},
505 {VK_NUMPAD4, 'K', 'G'},
506 {VK_NUMPAD5, 'K', 'H'},
507 {VK_NUMPAD6, 'K', 'I'},
508 {VK_NUMPAD7, 'K', 'J'},
509 {VK_NUMPAD8, 'K', 'K'},
510 {VK_NUMPAD9, 'K', 'L'},
511
Bram Moolenaar734a8672019-12-02 22:49:38 +0100512 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100513 {VK_SPACE, ' ', NUL},
514 {VK_TAB, TAB, NUL},
515 {VK_ESCAPE, ESC, NUL},
516 {NL, NL, NUL},
517 {CAR, CAR, NUL},
518
Bram Moolenaar734a8672019-12-02 22:49:38 +0100519 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100520 {0, 0, 0}
521};
522
Bram Moolenaar734a8672019-12-02 22:49:38 +0100523// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100524static int s_button_pending = -1;
525
Bram Moolenaar734a8672019-12-02 22:49:38 +0100526// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
527// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100528static int s_getting_focus = FALSE;
529
530static int s_x_pending;
531static int s_y_pending;
532static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000533static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100534static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100535static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200536static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
537 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100538
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100539#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100540// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100541static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000542static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100543#endif
544
545/*
546 * For control IME.
547 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100548 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100549 */
K.Takata4ac893f2022-01-20 12:44:28 +0000550#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100551// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100552static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100553// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100554static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100555#endif
556
557#ifdef FEAT_MBYTE_IME
558static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
559#endif
560
561#if defined(FEAT_BROWSE)
562static char_u *convert_filter(char_u *s);
563#endif
564
565#ifdef DEBUG_PRINT_ERROR
566/*
567 * Print out the last Windows error message
568 */
569 static void
570print_windows_error(void)
571{
572 LPVOID lpMsgBuf;
573
574 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
575 NULL, GetLastError(),
576 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
577 (LPTSTR) &lpMsgBuf, 0, NULL);
578 TRACE1("Error: %s\n", lpMsgBuf);
579 LocalFree(lpMsgBuf);
580}
581#endif
582
583/*
584 * Cursor blink functions.
585 *
586 * This is a simple state machine:
587 * BLINK_NONE not blinking at all
588 * BLINK_OFF blinking, cursor is not shown
589 * BLINK_ON blinking, cursor is shown
590 */
591
592#define BLINK_NONE 0
593#define BLINK_OFF 1
594#define BLINK_ON 2
595
596static int blink_state = BLINK_NONE;
597static long_u blink_waittime = 700;
598static long_u blink_ontime = 400;
599static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000600static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100601
Bram Moolenaar703a8042016-06-04 16:24:32 +0200602 int
603gui_mch_is_blinking(void)
604{
605 return blink_state != BLINK_NONE;
606}
607
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200608 int
609gui_mch_is_blink_off(void)
610{
611 return blink_state == BLINK_OFF;
612}
613
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100614 void
615gui_mch_set_blinking(long wait, long on, long off)
616{
617 blink_waittime = wait;
618 blink_ontime = on;
619 blink_offtime = off;
620}
621
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100622 static VOID CALLBACK
623_OnBlinkTimer(
624 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100625 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000626 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100627 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100628{
629 MSG msg;
630
631 /*
632 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
633 */
634
635 KillTimer(NULL, idEvent);
636
Bram Moolenaar734a8672019-12-02 22:49:38 +0100637 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000638 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 ;
640
641 if (blink_state == BLINK_ON)
642 {
643 gui_undraw_cursor();
644 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000645 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646 }
647 else
648 {
649 gui_update_cursor(TRUE, FALSE);
650 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000651 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100652 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100653 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100654}
655
656 static void
657gui_mswin_rm_blink_timer(void)
658{
659 MSG msg;
660
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000661 if (blink_timer == 0)
662 return;
663
664 KillTimer(NULL, blink_timer);
665 // Eat spurious WM_TIMER messages
666 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
667 ;
668 blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669}
670
671/*
672 * Stop the cursor blinking. Show the cursor if it wasn't shown.
673 */
674 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100675gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100676{
677 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100678 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100679 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100680 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100681 gui_mch_flush();
682 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 blink_state = BLINK_NONE;
684}
685
686/*
687 * Start the cursor blinking. If it was already blinking, this restarts the
688 * waiting time and shows the cursor.
689 */
690 void
691gui_mch_start_blink(void)
692{
693 gui_mswin_rm_blink_timer();
694
Bram Moolenaar734a8672019-12-02 22:49:38 +0100695 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100696 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
697 {
K.Takataa8ec4912022-02-03 14:32:33 +0000698 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100699 blink_state = BLINK_ON;
700 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100701 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702 }
703}
704
705/*
706 * Call-back routines.
707 */
708
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 static VOID CALLBACK
710_OnTimer(
711 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100712 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000713 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100714 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715{
716 MSG msg;
717
718 /*
719 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
720 */
721 KillTimer(NULL, idEvent);
722 s_timed_out = TRUE;
723
Bram Moolenaar734a8672019-12-02 22:49:38 +0100724 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000725 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100726 ;
727 if (idEvent == s_wait_timer)
728 s_wait_timer = 0;
729}
730
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100731 static void
732_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100733 HWND hwnd UNUSED,
734 UINT ch UNUSED,
735 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736{
737 dead_key = 1;
738}
739
740/*
741 * Convert Unicode character "ch" to bytes in "string[slen]".
742 * When "had_alt" is TRUE the ALT key was included in "ch".
743 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200744 * Because the Windows API uses UTF-16, we have to deal with surrogate
745 * pairs; this is where we choose to deal with them: if "ch" is a high
746 * surrogate, it will be stored, and the length returned will be zero; the next
747 * char_to_string call will then include the high surrogate, decoding the pair
748 * of UTF-16 code units to a single Unicode code point, presuming it is the
749 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100750 */
751 static int
752char_to_string(int ch, char_u *string, int slen, int had_alt)
753{
754 int len;
755 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100756 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200757 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100758
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200759 if (surrogate_pending_ch != 0)
760 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100761 // We don't guarantee ch is a low surrogate to match the high surrogate
762 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200763 wstring[0] = surrogate_pending_ch;
764 wstring[1] = ch;
765 surrogate_pending_ch = 0;
766 len = 2;
767 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100768 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200769 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100770 // We don't have the entire code point yet, only the first UTF-16 code
771 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200772 surrogate_pending_ch = ch;
773 return 0;
774 }
775 else
776 {
777 wstring[0] = ch;
778 len = 1;
779 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200780
Bram Moolenaar734a8672019-12-02 22:49:38 +0100781 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
782 // "enc_codepage" is non-zero use the standard Win32 function,
783 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200784 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100785 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200786 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
787 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100788 // If we had included the ALT key into the character but now the
789 // upper bit is no longer set, that probably means the conversion
790 // failed. Convert the original character and set the upper bit
791 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200792 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200794 wstring[0] = ch & 0x7f;
795 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
796 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100797 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 }
800 }
801 else
802 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200803 ws = utf16_to_enc(wstring, &len);
804 if (ws == NULL)
805 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100806 else
807 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100808 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200809 len = slen;
810 mch_memmove(string, ws, len);
811 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812 }
813 }
814
815 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816 {
817 string[0] = ch;
818 len = 1;
819 }
820
821 for (i = 0; i < len; ++i)
822 if (string[i] == CSI && len <= slen - 2)
823 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100824 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
826 string[++i] = KS_EXTRA;
827 string[++i] = (int)KE_CSI;
828 len += 2;
829 }
830
831 return len;
832}
833
LemonBoy45684c62022-04-24 15:46:42 +0100834 static int
835get_active_modifiers(void)
836{
837 int modifiers = 0;
838
839 if (GetKeyState(VK_CONTROL) & 0x8000)
840 modifiers |= MOD_MASK_CTRL;
841 if (GetKeyState(VK_SHIFT) & 0x8000)
842 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100843 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
844 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100845 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100846 if (GetKeyState(VK_LMENU) & 0x8000)
847 modifiers |= MOD_MASK_ALT;
848 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
849 modifiers &= ~MOD_MASK_CTRL;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100850 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
851 // is pressed, Windows claims that Ctrl is hold as well. That way we can
852 // recognize Right-ALT alone and be sure that not AltGr is hold.
853 if (!(GetKeyState(VK_CONTROL) & 0x8000)
854 && (GetKeyState(VK_RMENU) & 0x8000)
855 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
856 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100857
858 return modifiers;
859}
860
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861/*
862 * Key hit, add it to the input buffer.
863 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100864 static void
865_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100866 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100867 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100868 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100869{
870 char_u string[40];
871 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100872 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100873 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100874
Anton Sharonov3f026672022-07-26 21:26:18 +0100875 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
876 return;
877
878 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
879 // process_message()
880 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
881 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100882
LemonBoy45684c62022-04-24 15:46:42 +0100883 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100884
885 ch = simplify_key(ch, &modifiers);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000886
Christopher Plewright7b0afc12022-12-30 16:54:58 +0000887 // Some keys need adjustment when the Ctrl modifier is used.
888 ++no_reduce_keys;
889 ch = may_adjust_key_for_ctrl(modifiers, ch);
890 --no_reduce_keys;
891
LemonBoy77fc0b02022-04-22 22:45:52 +0100892 // remove the SHIFT modifier for keys where it's already included, e.g.,
893 // '(' and '*'
894 modifiers = may_remove_shift_modifier(modifiers, ch);
895
896 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
897 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
898 if (ch == CSI)
899 ch = K_CSI;
900
901 if (modifiers)
902 {
903 string[0] = CSI;
904 string[1] = KS_MODIFIER;
905 string[2] = modifiers;
906 add_to_input_buf(string, 3);
907 }
908
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909 len = char_to_string(ch, string, 40, FALSE);
910 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
911 {
912 trash_input_buf();
913 got_int = TRUE;
914 }
915
916 add_to_input_buf(string, len);
917}
918
919/*
920 * Alt-Key hit, add it to the input buffer.
921 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100922 static void
923_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100924 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100925 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100926 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100927{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100928 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100929 int len;
930 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100931 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932
Anton Sharonov3f026672022-07-26 21:26:18 +0100933 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100934
Bram Moolenaar734a8672019-12-02 22:49:38 +0100935 // OK, we have a character key (given by ch) which was entered with the
936 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
937 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
938 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100939 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100940 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100941 // remove the SHIFT modifier for keys where it's already included, e.g.,
942 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200943 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200945 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
946 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100947 if (ch == CSI)
948 ch = K_CSI;
949
950 len = 0;
951 if (modifiers)
952 {
953 string[len++] = CSI;
954 string[len++] = KS_MODIFIER;
955 string[len++] = modifiers;
956 }
957
958 if (IS_SPECIAL((int)ch))
959 {
960 string[len++] = CSI;
961 string[len++] = K_SECOND((int)ch);
962 string[len++] = K_THIRD((int)ch);
963 }
964 else
965 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100966 // Although the documentation isn't clear about it, we assume "ch" is
967 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100968 len += char_to_string(ch, string + len, 40 - len, TRUE);
969 }
970
971 add_to_input_buf(string, len);
972}
973
974 static void
975_OnMouseEvent(
976 int button,
977 int x,
978 int y,
979 int repeated_click,
980 UINT keyFlags)
981{
982 int vim_modifiers = 0x0;
983
984 s_getting_focus = FALSE;
985
986 if (keyFlags & MK_SHIFT)
987 vim_modifiers |= MOUSE_SHIFT;
988 if (keyFlags & MK_CONTROL)
989 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100990 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100991 vim_modifiers |= MOUSE_ALT;
992
993 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
994}
995
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100996 static void
997_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100998 HWND hwnd UNUSED,
999 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001000 int x,
1001 int y,
1002 UINT keyFlags)
1003{
1004 static LONG s_prevTime = 0;
1005
1006 LONG currentTime = GetMessageTime();
1007 int button = -1;
1008 int repeated_click;
1009
Bram Moolenaar734a8672019-12-02 22:49:38 +01001010 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001011 (void)SetFocus(s_hwnd);
1012
1013 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1014 button = MOUSE_LEFT;
1015 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1016 button = MOUSE_MIDDLE;
1017 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1018 button = MOUSE_RIGHT;
1019 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1020 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001021 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1022 }
1023 else if (s_uMsg == WM_CAPTURECHANGED)
1024 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001025 // on W95/NT4, somehow you get in here with an odd Msg
1026 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001027 if (s_button_pending == MOUSE_LEFT)
1028 button = MOUSE_RIGHT;
1029 else
1030 button = MOUSE_LEFT;
1031 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001032
1033 if (button < 0)
1034 return;
1035
1036 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1037
1038 /*
1039 * Holding down the left and right buttons simulates pushing the middle
1040 * button.
1041 */
1042 if (repeated_click
1043 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1044 || (button == MOUSE_RIGHT
1045 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001046 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001047 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001048 * Hmm, gui.c will ignore more than one button down at a time, so
1049 * pretend we let go of it first.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001050 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001051 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1052 button = MOUSE_MIDDLE;
1053 repeated_click = FALSE;
1054 s_button_pending = -1;
1055 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001056 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001057 else if ((repeated_click)
1058 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1059 {
1060 if (s_button_pending > -1)
1061 {
1062 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1063 s_button_pending = -1;
1064 }
1065 // TRACE("Button down at x %d, y %d\n", x, y);
1066 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1067 }
1068 else
1069 {
1070 /*
1071 * If this is the first press (i.e. not a multiple click) don't
1072 * action immediately, but store and wait for:
1073 * i) button-up
1074 * ii) mouse move
1075 * iii) another button press
1076 * before using it.
1077 * This enables us to make left+right simulate middle button,
1078 * without left or right being actioned first. The side-effect is
1079 * that if you click and hold the mouse without dragging, the
1080 * cursor doesn't move until you release the button. In practice
1081 * this is hardly a problem.
1082 */
1083 s_button_pending = button;
1084 s_x_pending = x;
1085 s_y_pending = y;
1086 s_kFlags_pending = keyFlags;
1087 }
1088
1089 s_prevTime = currentTime;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001090}
1091
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092 static void
1093_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001094 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001095 int x,
1096 int y,
1097 UINT keyFlags)
1098{
1099 int button;
1100
1101 s_getting_focus = FALSE;
1102 if (s_button_pending > -1)
1103 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001104 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105 _OnMouseEvent(s_button_pending, s_x_pending,
1106 s_y_pending, FALSE, s_kFlags_pending);
1107 s_button_pending = -1;
1108 }
1109 if (s_uMsg == WM_MOUSEMOVE)
1110 {
1111 /*
1112 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1113 * down.
1114 */
1115 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1116 | MK_XBUTTON1 | MK_XBUTTON2)))
1117 {
1118 gui_mouse_moved(x, y);
1119 return;
1120 }
1121
1122 /*
1123 * While button is down, keep grabbing mouse move events when
1124 * the mouse goes outside the window
1125 */
1126 SetCapture(s_textArea);
1127 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001128 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001129 }
1130 else
1131 {
1132 ReleaseCapture();
1133 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001134 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001135 }
1136
1137 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1138}
1139
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001140 static void
1141_OnSizeTextArea(
1142 HWND hwnd UNUSED,
1143 UINT state UNUSED,
1144 int cx UNUSED,
1145 int cy UNUSED)
1146{
1147#if defined(FEAT_DIRECTX)
1148 if (IS_ENABLE_DIRECTX())
1149 directx_binddc();
1150#endif
1151}
1152
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001153#ifdef FEAT_MENU
1154/*
1155 * Find the vimmenu_T with the given id
1156 */
1157 static vimmenu_T *
1158gui_mswin_find_menu(
1159 vimmenu_T *pMenu,
1160 int id)
1161{
1162 vimmenu_T *pChildMenu;
1163
1164 while (pMenu)
1165 {
1166 if (pMenu->id == (UINT)id)
1167 break;
1168 if (pMenu->children != NULL)
1169 {
1170 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1171 if (pChildMenu)
1172 {
1173 pMenu = pChildMenu;
1174 break;
1175 }
1176 }
1177 pMenu = pMenu->next;
1178 }
1179 return pMenu;
1180}
1181
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001182 static void
1183_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001184 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001185 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001186 HWND hwndCtl UNUSED,
1187 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001188{
1189 vimmenu_T *pMenu;
1190
1191 pMenu = gui_mswin_find_menu(root_menu, id);
1192 if (pMenu)
1193 gui_menu_cb(pMenu);
1194}
1195#endif
1196
1197#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001199 * Handle a Find/Replace window message.
1200 */
1201 static void
1202_OnFindRepl(void)
1203{
1204 int flags = 0;
1205 int down;
1206
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001207 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001208 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001209 (void)SetFocus(s_hwnd);
1210
1211 if (s_findrep_struct.Flags & FR_FINDNEXT)
1212 {
1213 flags = FRD_FINDNEXT;
1214
Bram Moolenaar734a8672019-12-02 22:49:38 +01001215 // Give main window the focus back: this is so the cursor isn't
1216 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001217 (void)SetFocus(s_hwnd);
1218 }
1219 else if (s_findrep_struct.Flags & FR_REPLACE)
1220 {
1221 flags = FRD_REPLACE;
1222
Bram Moolenaar734a8672019-12-02 22:49:38 +01001223 // Give main window the focus back: this is so the cursor isn't
1224 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001225 (void)SetFocus(s_hwnd);
1226 }
1227 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1228 {
1229 flags = FRD_REPLACEALL;
1230 }
1231
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001232 if (flags == 0)
1233 return;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001234
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001235 char_u *p, *q;
1236
1237 // Call the generic GUI function to do the actual work.
1238 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1239 flags |= FRD_WHOLE_WORD;
1240 if (s_findrep_struct.Flags & FR_MATCHCASE)
1241 flags |= FRD_MATCH_CASE;
1242 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1243 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1244 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1245 if (p != NULL && q != NULL)
1246 gui_do_findrepl(flags, p, q, down);
1247 vim_free(p);
1248 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249}
1250#endif
1251
1252 static void
1253HandleMouseHide(UINT uMsg, LPARAM lParam)
1254{
1255 static LPARAM last_lParam = 0L;
1256
Bram Moolenaar734a8672019-12-02 22:49:38 +01001257 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001258 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1259 {
1260 if (lParam == last_lParam)
1261 return;
1262 last_lParam = lParam;
1263 }
1264
Bram Moolenaar734a8672019-12-02 22:49:38 +01001265 // Handle specially, to centralise coding. We need to be sure we catch all
1266 // possible events which should cause us to restore the cursor (as it is a
1267 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268 switch (uMsg)
1269 {
1270 case WM_KEYUP:
1271 case WM_CHAR:
1272 /*
1273 * blank out the pointer if necessary
1274 */
1275 if (p_mh)
1276 gui_mch_mousehide(TRUE);
1277 break;
1278
Bram Moolenaar734a8672019-12-02 22:49:38 +01001279 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001280 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001281 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282 case WM_LBUTTONDOWN:
1283 case WM_LBUTTONUP:
1284 case WM_MBUTTONDOWN:
1285 case WM_MBUTTONUP:
1286 case WM_RBUTTONDOWN:
1287 case WM_RBUTTONUP:
1288 case WM_XBUTTONDOWN:
1289 case WM_XBUTTONUP:
1290 case WM_NCMOUSEMOVE:
1291 case WM_NCLBUTTONDOWN:
1292 case WM_NCLBUTTONUP:
1293 case WM_NCMBUTTONDOWN:
1294 case WM_NCMBUTTONUP:
1295 case WM_NCRBUTTONDOWN:
1296 case WM_NCRBUTTONUP:
1297 case WM_KILLFOCUS:
1298 /*
1299 * if the pointer is currently hidden, then we should show it.
1300 */
1301 gui_mch_mousehide(FALSE);
1302 break;
1303 }
1304}
1305
1306 static LRESULT CALLBACK
1307_TextAreaWndProc(
1308 HWND hwnd,
1309 UINT uMsg,
1310 WPARAM wParam,
1311 LPARAM lParam)
1312{
1313 /*
1314 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1315 hwnd, uMsg, wParam, lParam);
1316 */
1317
1318 HandleMouseHide(uMsg, lParam);
1319
1320 s_uMsg = uMsg;
1321 s_wParam = wParam;
1322 s_lParam = lParam;
1323
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001324#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001325 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001326#endif
1327
1328 switch (uMsg)
1329 {
1330 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1331 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1332 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1333 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1334 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1335 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1336 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1337 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1338 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1339 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1340 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1341 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1342 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1343 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001344 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001345
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001346#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001347 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1348 return TRUE;
1349#endif
1350 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001351 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001352 }
1353}
1354
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001355/*
1356 * Called when the foreground or background color has been changed.
1357 */
1358 void
1359gui_mch_new_colors(void)
1360{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001361 HBRUSH prevBrush;
1362
1363 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001364 prevBrush = (HBRUSH)SetClassLongPtr(
1365 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001366 InvalidateRect(s_hwnd, NULL, TRUE);
1367 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001368}
1369
1370/*
1371 * Set the colors to their default values.
1372 */
1373 void
1374gui_mch_def_colors(void)
1375{
1376 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1377 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1378 gui.def_norm_pixel = gui.norm_pixel;
1379 gui.def_back_pixel = gui.back_pixel;
1380}
1381
1382/*
1383 * Open the GUI window which was created by a call to gui_mch_init().
1384 */
1385 int
1386gui_mch_open(void)
1387{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001388 // Actually open the window, if not already visible
1389 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001390 if (!IsWindowVisible(s_hwnd))
1391 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1392
1393#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001394 // Init replace string here, so that we keep it when re-opening the
1395 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001396 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1397#endif
1398
1399 return OK;
1400}
1401
1402/*
1403 * Get the position of the top left corner of the window.
1404 */
1405 int
1406gui_mch_get_winpos(int *x, int *y)
1407{
1408 RECT rect;
1409
1410 GetWindowRect(s_hwnd, &rect);
1411 *x = rect.left;
1412 *y = rect.top;
1413 return OK;
1414}
1415
1416/*
1417 * Set the position of the top left corner of the window to the given
1418 * coordinates.
1419 */
1420 void
1421gui_mch_set_winpos(int x, int y)
1422{
1423 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1424 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1425}
1426 void
1427gui_mch_set_text_area_pos(int x, int y, int w, int h)
1428{
1429 static int oldx = 0;
1430 static int oldy = 0;
1431
1432 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1433
1434#ifdef FEAT_TOOLBAR
1435 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1436 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001437 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001438#endif
1439#if defined(FEAT_GUI_TABLINE)
1440 if (showing_tabline)
1441 {
1442 int top = 0;
1443 RECT rect;
1444
1445# ifdef FEAT_TOOLBAR
1446 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001447 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448# endif
1449 GetClientRect(s_hwnd, &rect);
1450 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1451 }
1452#endif
1453
Bram Moolenaar734a8672019-12-02 22:49:38 +01001454 // When side scroll bar is unshown, the size of window will change.
1455 // then, the text area move left or right. thus client rect should be
1456 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001457 if (oldx != x || oldy != y)
1458 {
1459 InvalidateRect(s_hwnd, NULL, FALSE);
1460 oldx = x;
1461 oldy = y;
1462 }
1463}
1464
1465
1466/*
1467 * Scrollbar stuff:
1468 */
1469
1470 void
1471gui_mch_enable_scrollbar(
1472 scrollbar_T *sb,
1473 int flag)
1474{
1475 ShowScrollBar(sb->id, SB_CTL, flag);
1476
Bram Moolenaar734a8672019-12-02 22:49:38 +01001477 // TODO: When the window is maximized, the size of the window stays the
1478 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1479 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001480}
1481
1482 void
1483gui_mch_set_scrollbar_pos(
1484 scrollbar_T *sb,
1485 int x,
1486 int y,
1487 int w,
1488 int h)
1489{
1490 SetWindowPos(sb->id, NULL, x, y, w, h,
1491 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1492}
1493
Bram Moolenaar203ec772020-07-17 20:43:43 +02001494 int
1495gui_mch_get_scrollbar_xpadding(void)
1496{
1497 RECT rcTxt, rcWnd;
1498 int xpad;
1499
1500 GetWindowRect(s_textArea, &rcTxt);
1501 GetWindowRect(s_hwnd, &rcWnd);
1502 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001503 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1504 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001505 return (xpad < 0) ? 0 : xpad;
1506}
1507
1508 int
1509gui_mch_get_scrollbar_ypadding(void)
1510{
1511 RECT rcTxt, rcWnd;
1512 int ypad;
1513
1514 GetWindowRect(s_textArea, &rcTxt);
1515 GetWindowRect(s_hwnd, &rcWnd);
1516 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001517 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1518 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001519 return (ypad < 0) ? 0 : ypad;
1520}
1521
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001522 void
1523gui_mch_create_scrollbar(
1524 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001525 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001526{
1527 sb->id = CreateWindow(
1528 "SCROLLBAR", "Scrollbar",
1529 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001530 10, // Any value will do for now
1531 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001532 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001533 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001534}
1535
1536/*
1537 * Find the scrollbar with the given hwnd.
1538 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001539 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001540gui_mswin_find_scrollbar(HWND hwnd)
1541{
1542 win_T *wp;
1543
1544 if (gui.bottom_sbar.id == hwnd)
1545 return &gui.bottom_sbar;
1546 FOR_ALL_WINDOWS(wp)
1547 {
1548 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1549 return &wp->w_scrollbars[SBAR_LEFT];
1550 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1551 return &wp->w_scrollbars[SBAR_RIGHT];
1552 }
1553 return NULL;
1554}
1555
K.Takatac81e9bf2022-01-16 14:15:49 +00001556 static void
1557update_scrollbar_size(void)
1558{
1559 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1560 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1561}
1562
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001563/*
K.Takataabe628e2022-01-23 16:25:17 +00001564 * Get the average character size of a font.
1565 */
1566 static void
1567GetAverageFontSize(HDC hdc, SIZE *size)
1568{
1569 // GetTextMetrics() may not return the right value in tmAveCharWidth
1570 // for some fonts. Do our own average computation.
1571 GetTextExtentPoint(hdc,
1572 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1573 52, size);
1574 size->cx = (size->cx / 26 + 1) / 2;
1575}
1576
1577/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001578 * Get the character size of a font.
1579 */
1580 static void
Ken Takatada5da652023-10-05 20:20:58 +02001581GetFontSize(GuiFont font, int *char_width, int *char_height)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582{
1583 HWND hwnd = GetDesktopWindow();
1584 HDC hdc = GetWindowDC(hwnd);
1585 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001586 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587 TEXTMETRIC tm;
1588
1589 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001590 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001591
Ken Takatada5da652023-10-05 20:20:58 +02001592 if (char_width)
1593 *char_width = size.cx + tm.tmOverhang;
1594 if (char_height)
1595 *char_height = tm.tmHeight + p_linespace;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596
1597 SelectFont(hdc, hfntOld);
1598
1599 ReleaseDC(hwnd, hdc);
1600}
1601
1602/*
Ken Takatada5da652023-10-05 20:20:58 +02001603 * Update the character size in "gui" structure with the specified font.
1604 */
1605 static void
1606UpdateFontSize(GuiFont font)
1607{
1608 GetFontSize(font, &gui.char_width, &gui.char_height);
1609}
1610
1611/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001612 * Adjust gui.char_height (after 'linespace' was changed).
1613 */
1614 int
1615gui_mch_adjust_charheight(void)
1616{
Ken Takatada5da652023-10-05 20:20:58 +02001617 UpdateFontSize(gui.norm_font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001618 return OK;
1619}
1620
1621 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001622get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001623{
1624 HFONT font = NULL;
1625
Bram Moolenaar734a8672019-12-02 22:49:38 +01001626 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001627 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001628
1629 if (font == NULL)
1630 return NOFONT;
1631
1632 return (GuiFont)font;
1633}
1634
1635 static int
1636pixels_to_points(int pixels, int vertical)
1637{
1638 int points;
1639 HWND hwnd;
1640 HDC hdc;
1641
1642 hwnd = GetDesktopWindow();
1643 hdc = GetWindowDC(hwnd);
1644
1645 points = MulDiv(pixels, 72,
1646 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1647
1648 ReleaseDC(hwnd, hdc);
1649
1650 return points;
1651}
1652
1653 GuiFont
1654gui_mch_get_font(
1655 char_u *name,
1656 int giveErrorIfMissing)
1657{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001658 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001659 GuiFont font = NOFONT;
1660
1661 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001662 {
1663 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001664 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001665 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001666 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001667 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668 return font;
1669}
1670
1671#if defined(FEAT_EVAL) || defined(PROTO)
1672/*
1673 * Return the name of font "font" in allocated memory.
1674 * Don't know how to get the actual name, thus use the provided name.
1675 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001676 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001677gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678{
1679 if (name == NULL)
1680 return NULL;
1681 return vim_strsave(name);
1682}
1683#endif
1684
1685 void
1686gui_mch_free_font(GuiFont font)
1687{
1688 if (font)
1689 DeleteObject((HFONT)font);
1690}
1691
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692/*
1693 * Return the Pixel value (color) for the given color name.
1694 * Return INVALCOLOR for error.
1695 */
1696 guicolor_T
1697gui_mch_get_color(char_u *name)
1698{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001699 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700
1701 typedef struct SysColorTable
1702 {
1703 char *name;
1704 int color;
1705 } SysColorTable;
1706
1707 static SysColorTable sys_table[] =
1708 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001709 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1710 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001711#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001712 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1713#endif
1714 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1715 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1716 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1717 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1718 {"SYS_DESKTOP", COLOR_DESKTOP},
1719 {"SYS_INFOBK", COLOR_INFOBK},
1720 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1721 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001722 {"SYS_BTNFACE", COLOR_BTNFACE},
1723 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1724 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1725 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1726 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1727 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1728 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1729 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1730 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1731 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1732 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1733 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1734 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1735 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1736 {"SYS_MENU", COLOR_MENU},
1737 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1738 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1739 {"SYS_WINDOW", COLOR_WINDOW},
1740 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1741 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1742 };
1743
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001744 /*
1745 * Try to look up a system colour.
1746 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001747 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001748 if (STRICMP(name, sys_table[i].name) == 0)
1749 return GetSysColor(sys_table[i].color);
1750
Bram Moolenaarab302212016-04-26 20:59:29 +02001751 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001752}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001753
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001754 guicolor_T
1755gui_mch_get_rgb_color(int r, int g, int b)
1756{
1757 return gui_get_rgb_color_cmn(r, g, b);
1758}
1759
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001760/*
1761 * Return OK if the key with the termcap name "name" is supported.
1762 */
1763 int
1764gui_mch_haskey(char_u *name)
1765{
1766 int i;
1767
1768 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001769 if (name[0] == special_keys[i].vim_code0
1770 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001771 return OK;
1772 return FAIL;
1773}
1774
1775 void
1776gui_mch_beep(void)
1777{
LemonBoy77771d32022-04-13 11:47:25 +01001778 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001779}
1780/*
1781 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1782 */
1783 void
1784gui_mch_invert_rectangle(
1785 int r,
1786 int c,
1787 int nr,
1788 int nc)
1789{
1790 RECT rc;
1791
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001792#if defined(FEAT_DIRECTX)
1793 if (IS_ENABLE_DIRECTX())
1794 DWriteContext_Flush(s_dwc);
1795#endif
1796
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001797 /*
1798 * Note: InvertRect() excludes right and bottom of rectangle.
1799 */
1800 rc.left = FILL_X(c);
1801 rc.top = FILL_Y(r);
1802 rc.right = rc.left + nc * gui.char_width;
1803 rc.bottom = rc.top + nr * gui.char_height;
1804 InvertRect(s_hdc, &rc);
1805}
1806
1807/*
1808 * Iconify the GUI window.
1809 */
1810 void
1811gui_mch_iconify(void)
1812{
1813 ShowWindow(s_hwnd, SW_MINIMIZE);
1814}
1815
1816/*
1817 * Draw a cursor without focus.
1818 */
1819 void
1820gui_mch_draw_hollow_cursor(guicolor_T color)
1821{
1822 HBRUSH hbr;
1823 RECT rc;
1824
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001825#if defined(FEAT_DIRECTX)
1826 if (IS_ENABLE_DIRECTX())
1827 DWriteContext_Flush(s_dwc);
1828#endif
1829
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001830 /*
1831 * Note: FrameRect() excludes right and bottom of rectangle.
1832 */
1833 rc.left = FILL_X(gui.col);
1834 rc.top = FILL_Y(gui.row);
1835 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001836 if (mb_lefthalve(gui.row, gui.col))
1837 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001838 rc.bottom = rc.top + gui.char_height;
1839 hbr = CreateSolidBrush(color);
1840 FrameRect(s_hdc, &rc, hbr);
1841 DeleteBrush(hbr);
1842}
1843/*
1844 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1845 * color "color".
1846 */
1847 void
1848gui_mch_draw_part_cursor(
1849 int w,
1850 int h,
1851 guicolor_T color)
1852{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001853 RECT rc;
1854
1855 /*
1856 * Note: FillRect() excludes right and bottom of rectangle.
1857 */
1858 rc.left =
1859#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001860 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001861 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1862#endif
1863 FILL_X(gui.col);
1864 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1865 rc.right = rc.left + w;
1866 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001867
Bram Moolenaar92467d32017-12-05 13:22:16 +01001868 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869}
1870
1871
1872/*
1873 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1874 * dead key's nominal character and re-post the original message.
1875 */
1876 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001877outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001878{
1879 static MSG deadCharExpel;
1880
Anton Sharonov3f026672022-07-26 21:26:18 +01001881 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001882 return;
1883
Anton Sharonov3f026672022-07-26 21:26:18 +01001884 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885
Bram Moolenaar734a8672019-12-02 22:49:38 +01001886 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001887 deadCharExpel.message = originalMsg.message;
1888 deadCharExpel.hwnd = originalMsg.hwnd;
1889 deadCharExpel.wParam = VK_SPACE;
1890
K.Takata4ac893f2022-01-20 12:44:28 +00001891 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001892
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1895 originalMsg.lParam);
1896}
1897
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001899 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1900 */
1901 static void
1902outputDeadKey_rePost(MSG originalMsg)
1903{
1904 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1905}
1906
1907/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908 * Process a single Windows message.
1909 * If one is not available we hang until one is.
1910 */
1911 static void
1912process_message(void)
1913{
1914 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001915 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001916 char_u string[40];
1917 int i;
1918 int modifiers = 0;
1919 int key;
1920#ifdef FEAT_MENU
1921 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1922#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001923 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001924
K.Takatab7057bd2022-01-21 11:37:07 +00001925 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001926
1927#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001928 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001929 if (msg.message == WM_OLE)
1930 {
1931 char_u *str = (char_u *)msg.lParam;
1932 if (str == NULL || *str == NUL)
1933 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001934 // Message can't be ours, forward it. Fixes problem with Ultramon
1935 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001936 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001937 }
1938 else
1939 {
1940 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001941 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001942 }
1943 return;
1944 }
1945#endif
1946
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001948 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001949 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001950 {
1951 HandleMouseHide(msg.message, msg.lParam);
1952 return;
1953 }
1954#endif
1955
1956 /*
1957 * Check if it's a special key that we recognise. If not, call
1958 * TranslateMessage().
1959 */
1960 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1961 {
1962 vk = (int) msg.wParam;
1963
1964 /*
1965 * Handle dead keys in special conditions in other cases we let Windows
1966 * handle them and do not interfere.
1967 *
1968 * The dead_key flag must be reset on several occasions:
1969 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1970 * consumed at that point (This is when we let Windows combine the
1971 * dead character on its own)
1972 *
1973 * - Before doing something special such as regenerating keypresses to
1974 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001975 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001976 * immediately to _OnChar() (or _OnSysChar()).
1977 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001978
1979 /*
1980 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1981 * was handled by _WndProc, this keypress we want to process normally
1982 */
1983 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1984 dead_key = DEAD_KEY_OFF;
1985
1986 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001987 {
1988 /*
dundargocc57b5bc2022-11-02 13:30:51 +00001989 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01001990 *
1991 * After dead key was pressed with Ctrl in some cases, ESC was
1992 * artificially injected and handled by _OnChar(), now we are
1993 * dealing with completely new key press from the user. If we don't
1994 * do anything, ToUnicode() call will interpret this vk+scan_code
1995 * under influence of "dead-modifier". To prevent this we translate
1996 * this message replacing current char from user with VK_SPACE,
1997 * which will cause WM_CHAR with dead_key's character itself. Using
1998 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1999 * ignore this one WM_CHAR event completely. Afterwards (due to
2000 * usage of PostMessage), this procedure is scheduled to be called
2001 * again with user char and on next entry we will clean
2002 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2003 * outputDeadKey_rePost() since we do not wish to reset dead_key
2004 * value.
2005 */
2006 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
2007 {
2008 outputDeadKey_rePost_Ex(msg,
2009 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2010 return;
2011 }
2012
2013 if (dead_key != DEAD_KEY_SET_DEFAULT)
2014 {
2015 // should never happen - is there a way to make ASSERT here?
2016 return;
2017 }
2018
2019 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002020 * If a dead key was pressed and the user presses VK_SPACE,
2021 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2022 * with the dead char now, so do nothing special and let Windows
2023 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002024 *
2025 * Note that VK_SPACE combines with the dead_key's character and
2026 * only one WM_CHAR will be generated by TranslateMessage(), in
2027 * the two other cases two WM_CHAR will be generated: the dead
2028 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2029 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 */
2031 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2032 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002033 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002034 TranslateMessage(&msg);
2035 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002036 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002037 // In modes where we are not typing, dead keys should behave
2038 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002039 else if ((get_real_state()
2040 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 {
2042 outputDeadKey_rePost(msg);
2043 return;
2044 }
2045 }
2046
Bram Moolenaar734a8672019-12-02 22:49:38 +01002047 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048 if (vk == VK_CANCEL)
2049 {
2050 trash_input_buf();
2051 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002052 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002053 string[0] = Ctrl_C;
2054 add_to_input_buf(string, 1);
2055 }
2056
LemonBoy77fc0b02022-04-22 22:45:52 +01002057 // This is an IME event or a synthetic keystroke, let Windows handle it.
2058 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2059 {
2060 TranslateMessage(&msg);
2061 return;
2062 }
2063
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002064 for (i = 0; special_keys[i].key_sym != 0; i++)
2065 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002066 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002068 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069 {
2070 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002071 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002072 * is a key that would normally trigger the dead key nominal
2073 * character output (such as a NUMPAD printable character or
2074 * the TAB key, etc...).
2075 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002076 if (dead_key == DEAD_KEY_SET_DEFAULT
2077 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002078 || vk == VK_TAB || vk == CAR))
2079 {
2080 outputDeadKey_rePost(msg);
2081 return;
2082 }
2083
2084#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002085 // Check for <F10>: Windows selects the menu. When <F10> is
2086 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 if (vk == VK_F10
2088 && gui.menu_is_active
2089 && check_map(k10, State, FALSE, TRUE, FALSE,
2090 NULL, NULL) == NULL)
2091 break;
2092#endif
LemonBoy45684c62022-04-24 15:46:42 +01002093 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094
2095 if (special_keys[i].vim_code1 == NUL)
2096 key = special_keys[i].vim_code0;
2097 else
2098 key = TO_SPECIAL(special_keys[i].vim_code0,
2099 special_keys[i].vim_code1);
2100 key = simplify_key(key, &modifiers);
2101 if (key == CSI)
2102 key = K_CSI;
2103
2104 if (modifiers)
2105 {
2106 string[0] = CSI;
2107 string[1] = KS_MODIFIER;
2108 string[2] = modifiers;
2109 add_to_input_buf(string, 3);
2110 }
2111
2112 if (IS_SPECIAL(key))
2113 {
2114 string[0] = CSI;
2115 string[1] = K_SECOND(key);
2116 string[2] = K_THIRD(key);
2117 add_to_input_buf(string, 3);
2118 }
2119 else
2120 {
2121 int len;
2122
Bram Moolenaar734a8672019-12-02 22:49:38 +01002123 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124 len = char_to_string(key, string, 40, FALSE);
2125 add_to_input_buf(string, len);
2126 }
2127 break;
2128 }
2129 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002130
2131 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002132 if (special_keys[i].key_sym == 0)
2133 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002134 WCHAR ch[8];
2135 int len;
2136 int i;
2137 UINT scan_code;
2138
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002139 // Construct the state table with only a few modifiers, we don't
2140 // really care about the presence of Ctrl/Alt as those modifiers are
2141 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002142 memset(keyboard_state, 0, 256);
2143 if (GetKeyState(VK_SHIFT) & 0x8000)
2144 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002145 if (GetKeyState(VK_CAPITAL) & 0x0001)
2146 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002147 // Alt-Gr is synthesized as Alt + Ctrl.
2148 if ((GetKeyState(VK_RMENU) & 0x8000)
2149 && (GetKeyState(VK_CONTROL) & 0x8000))
2150 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002151 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002152 keyboard_state[VK_CONTROL] = 0x80;
2153 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002154
2155 // Translate the virtual key according to the current keyboard
2156 // layout.
2157 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2158 // Convert the scan-code into a sequence of zero or more unicode
2159 // codepoints.
2160 // If this is a dead key ToUnicode returns a negative value.
2161 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2162 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002163 if (len < 0)
2164 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002165
2166 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002167 {
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002168 int wm_char = NUL;
2169
2170 if (dead_key == DEAD_KEY_SET_DEFAULT
2171 && (GetKeyState(VK_CONTROL) & 0x8000))
2172 {
2173 if ( // AZERTY CTRL+dead_circumflex
2174 (vk == 221 && scan_code == 26)
2175 // QWERTZ CTRL+dead_circumflex
2176 || (vk == 220 && scan_code == 41))
2177 wm_char = '[';
2178 if ( // QWERTZ CTRL+dead_two-overdots
2179 (vk == 192 && scan_code == 27))
2180 wm_char = ']';
2181 }
2182 if (wm_char != NUL)
Anton Sharonov3f026672022-07-26 21:26:18 +01002183 {
2184 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002185 // still hold as ESC
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002186 PostMessageW(msg.hwnd, WM_CHAR, wm_char, msg.lParam);
Anton Sharonov3f026672022-07-26 21:26:18 +01002187 // ask _OnChar() to not touch this state, wait for next key
2188 // press and maintain knowledge that we are "poisoned" with
2189 // "dead state"
2190 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2191 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002192 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002193 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002194
2195 // Post the message as TranslateMessage would do.
2196 if (msg.message == WM_KEYDOWN)
2197 {
2198 for (i = 0; i < len; i++)
2199 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002200 }
2201 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002202 {
2203 for (i = 0; i < len; i++)
2204 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2205 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002206 }
2207 }
2208#ifdef FEAT_MBYTE_IME
2209 else if (msg.message == WM_IME_NOTIFY)
2210 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2211 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002212 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002213 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002214#endif
2215
2216#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002217 // Check for <F10>: Default effect is to select the menu. When <F10> is
2218 // mapped we need to stop it here to avoid strange effects (e.g., for the
2219 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002220 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2221 NULL, NULL) == NULL)
2222#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002223 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002224}
2225
2226/*
2227 * Catch up with any queued events. This may put keyboard input into the
2228 * input buffer, call resize call-backs, trigger timers etc. If there is
2229 * nothing in the event queue (& no timers pending), then we return
2230 * immediately.
2231 */
2232 void
2233gui_mch_update(void)
2234{
2235 MSG msg;
2236
2237 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002238 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002239 && !vim_is_input_buf_full())
2240 process_message();
2241}
2242
Bram Moolenaar4231da42016-06-02 14:30:04 +02002243 static void
2244remove_any_timer(void)
2245{
2246 MSG msg;
2247
2248 if (s_wait_timer != 0 && !s_timed_out)
2249 {
2250 KillTimer(NULL, s_wait_timer);
2251
Bram Moolenaar734a8672019-12-02 22:49:38 +01002252 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002253 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002254 ;
2255 s_wait_timer = 0;
2256 }
2257}
2258
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002259/*
2260 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2261 * from the keyboard.
2262 * wtime == -1 Wait forever.
2263 * wtime == 0 This should never happen.
2264 * wtime > 0 Wait wtime milliseconds for a character.
2265 * Returns OK if a character was found to be available within the given time,
2266 * or FAIL otherwise.
2267 */
2268 int
2269gui_mch_wait_for_chars(int wtime)
2270{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271 int focus;
2272
2273 s_timed_out = FALSE;
2274
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002275 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002276 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002277 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002278 if (s_busy_processing)
2279 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002280
2281 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002282 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2283 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002284 }
2285
2286 allow_scrollbar = TRUE;
2287
2288 focus = gui.in_focus;
2289 while (!s_timed_out)
2290 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002291 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292 if (gui.in_focus != focus)
2293 {
2294 if (gui.in_focus)
2295 gui_mch_start_blink();
2296 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002297 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002298 focus = gui.in_focus;
2299 }
2300
2301 if (s_need_activate)
2302 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002304 s_need_activate = FALSE;
2305 }
2306
Bram Moolenaar4231da42016-06-02 14:30:04 +02002307#ifdef FEAT_TIMERS
2308 did_add_timer = FALSE;
2309#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002311 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002312 for (;;)
2313 {
2314 MSG msg;
2315
2316 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002317# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002318 if (did_add_timer)
2319 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002320# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002321 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002322 {
2323 process_message();
2324 break;
2325 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002326 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002327 // TODO: The 10 msec is a compromise between laggy response
2328 // and consuming more CPU time. Better would be to handle
2329 // channel messages when they arrive.
2330 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002331 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002332 break;
2333 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002334#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002335 // Don't use gui_mch_update() because then we will spin-lock until a
2336 // char arrives, instead we use GetMessage() to hang until an
2337 // event arrives. No need to check for input_buf_full because we are
2338 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002340#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341
2342 if (input_available())
2343 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002344 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002345 allow_scrollbar = FALSE;
2346
Bram Moolenaar89c00032019-09-04 13:53:21 +02002347 // Clear pending mouse button, the release event may have been
2348 // taken by the dialog window. But don't do this when getting
2349 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002350 if (!s_getting_focus)
2351 s_button_pending = -1;
2352
2353 return OK;
2354 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002355
2356#ifdef FEAT_TIMERS
2357 if (did_add_timer)
2358 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002359 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002360 remove_any_timer();
2361 break;
2362 }
2363#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002364 }
2365 allow_scrollbar = FALSE;
2366 return FAIL;
2367}
2368
2369/*
2370 * Clear a rectangular region of the screen from text pos (row1, col1) to
2371 * (row2, col2) inclusive.
2372 */
2373 void
2374gui_mch_clear_block(
2375 int row1,
2376 int col1,
2377 int row2,
2378 int col2)
2379{
2380 RECT rc;
2381
2382 /*
2383 * Clear one extra pixel at the far right, for when bold characters have
2384 * spilled over to the window border.
2385 * Note: FillRect() excludes right and bottom of rectangle.
2386 */
2387 rc.left = FILL_X(col1);
2388 rc.top = FILL_Y(row1);
2389 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2390 rc.bottom = FILL_Y(row2 + 1);
2391 clear_rect(&rc);
2392}
2393
2394/*
2395 * Clear the whole text window.
2396 */
2397 void
2398gui_mch_clear_all(void)
2399{
2400 RECT rc;
2401
2402 rc.left = 0;
2403 rc.top = 0;
2404 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2405 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2406 clear_rect(&rc);
2407}
2408/*
2409 * Menu stuff.
2410 */
2411
2412 void
2413gui_mch_enable_menu(int flag)
2414{
2415#ifdef FEAT_MENU
2416 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2417#endif
2418}
2419
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002420 void
2421gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002422 int x UNUSED,
2423 int y UNUSED,
2424 int w UNUSED,
2425 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002427 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002428}
2429
2430#if defined(FEAT_MENU) || defined(PROTO)
2431/*
2432 * Make menu item hidden or not hidden
2433 */
2434 void
2435gui_mch_menu_hidden(
2436 vimmenu_T *menu,
2437 int hidden)
2438{
2439 /*
2440 * This doesn't do what we want. Hmm, just grey the menu items for now.
2441 */
2442 /*
2443 if (hidden)
2444 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2445 else
2446 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2447 */
2448 gui_mch_menu_grey(menu, hidden);
2449}
2450
2451/*
2452 * This is called after setting all the menus to grey/hidden or not.
2453 */
2454 void
2455gui_mch_draw_menubar(void)
2456{
2457 DrawMenuBar(s_hwnd);
2458}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002459#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002460
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002461/*
2462 * Return the RGB value of a pixel as a long.
2463 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002464 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002465gui_mch_get_rgb(guicolor_T pixel)
2466{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002467 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2468 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469}
2470
2471#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002472/*
2473 * Convert pixels in X to dialog units
2474 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475 static WORD
2476PixelToDialogX(int numPixels)
2477{
2478 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2479}
2480
Bram Moolenaar734a8672019-12-02 22:49:38 +01002481/*
2482 * Convert pixels in Y to dialog units
2483 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002484 static WORD
2485PixelToDialogY(int numPixels)
2486{
2487 return (WORD)((numPixels * 8) / s_dlgfntheight);
2488}
2489
Bram Moolenaar734a8672019-12-02 22:49:38 +01002490/*
2491 * Return the width in pixels of the given text in the given DC.
2492 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002493 static int
2494GetTextWidth(HDC hdc, char_u *str, int len)
2495{
2496 SIZE size;
2497
2498 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2499 return size.cx;
2500}
2501
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502/*
2503 * Return the width in pixels of the given text in the given DC, taking care
2504 * of 'encoding' to active codepage conversion.
2505 */
2506 static int
2507GetTextWidthEnc(HDC hdc, char_u *str, int len)
2508{
2509 SIZE size;
2510 WCHAR *wstr;
2511 int n;
2512 int wlen = len;
2513
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002514 wstr = enc_to_utf16(str, &wlen);
2515 if (wstr == NULL)
2516 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002517
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002518 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2519 vim_free(wstr);
2520 if (n)
2521 return size.cx;
2522 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002523}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002525static void get_work_area(RECT *spi_rect);
2526
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002527/*
2528 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002529 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2530 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002531 */
2532 static BOOL
2533CenterWindow(
2534 HWND hwndChild,
2535 HWND hwndParent)
2536{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002537 HMONITOR mon;
2538 MONITORINFO moninfo;
2539 RECT rChild, rParent, rScreen;
2540 int wChild, hChild, wParent, hParent;
2541 int xNew, yNew;
2542 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002543
2544 GetWindowRect(hwndChild, &rChild);
2545 wChild = rChild.right - rChild.left;
2546 hChild = rChild.bottom - rChild.top;
2547
Bram Moolenaar734a8672019-12-02 22:49:38 +01002548 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002549 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002550 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002551 else
2552 GetWindowRect(hwndParent, &rParent);
2553 wParent = rParent.right - rParent.left;
2554 hParent = rParent.bottom - rParent.top;
2555
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002556 moninfo.cbSize = sizeof(MONITORINFO);
2557 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2558 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002560 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002562 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002563 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002564 hdc = GetDC(hwndChild);
2565 rScreen.left = 0;
2566 rScreen.top = 0;
2567 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2568 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2569 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002570 }
2571
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002572 xNew = rParent.left + ((wParent - wChild) / 2);
2573 if (xNew < rScreen.left)
2574 xNew = rScreen.left;
2575 else if ((xNew + wChild) > rScreen.right)
2576 xNew = rScreen.right - wChild;
2577
2578 yNew = rParent.top + ((hParent - hChild) / 2);
2579 if (yNew < rScreen.top)
2580 yNew = rScreen.top;
2581 else if ((yNew + hChild) > rScreen.bottom)
2582 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583
2584 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2585 SWP_NOSIZE | SWP_NOZORDER);
2586}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002587#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002588
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002589#if defined(FEAT_TOOLBAR) || defined(PROTO)
2590 void
2591gui_mch_show_toolbar(int showit)
2592{
2593 if (s_toolbarhwnd == NULL)
2594 return;
2595
2596 if (showit)
2597 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002598 // Enable unicode support
2599 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2600 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002601 ShowWindow(s_toolbarhwnd, SW_SHOW);
2602 }
2603 else
2604 ShowWindow(s_toolbarhwnd, SW_HIDE);
2605}
2606
Bram Moolenaar734a8672019-12-02 22:49:38 +01002607// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002608# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609
2610#endif
2611
2612#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2613 static void
2614add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2615{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002616 WCHAR *wn;
2617 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002618
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002619 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002621 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002622
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002623 infow.cbSize = sizeof(infow);
2624 infow.fMask = MIIM_TYPE | MIIM_ID;
2625 infow.wID = item_id;
2626 infow.fType = MFT_STRING;
2627 infow.dwTypeData = wn;
2628 infow.cch = (UINT)wcslen(wn);
2629 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2630 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002631}
2632
2633 static void
2634show_tabline_popup_menu(void)
2635{
2636 HMENU tab_pmenu;
2637 long rval;
2638 POINT pt;
2639
Bram Moolenaar734a8672019-12-02 22:49:38 +01002640 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002641 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002642 return;
2643
2644 tab_pmenu = CreatePopupMenu();
2645 if (tab_pmenu == NULL)
2646 return;
2647
2648 if (first_tabpage->tp_next != NULL)
2649 add_tabline_popup_menu_entry(tab_pmenu,
2650 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2651 add_tabline_popup_menu_entry(tab_pmenu,
2652 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2653 add_tabline_popup_menu_entry(tab_pmenu,
2654 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2655
2656 GetCursorPos(&pt);
2657 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2658 NULL);
2659
2660 DestroyMenu(tab_pmenu);
2661
Bram Moolenaar734a8672019-12-02 22:49:38 +01002662 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663 if (rval > 0)
2664 {
2665 TCHITTESTINFO htinfo;
2666 int idx;
2667
2668 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2669 return;
2670
2671 htinfo.pt.x = pt.x;
2672 htinfo.pt.y = pt.y;
2673 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2674 if (idx == -1)
2675 idx = 0;
2676 else
2677 idx += 1;
2678
2679 send_tabline_menu_event(idx, (int)rval);
2680 }
2681}
2682
2683/*
2684 * Show or hide the tabline.
2685 */
2686 void
2687gui_mch_show_tabline(int showit)
2688{
2689 if (s_tabhwnd == NULL)
2690 return;
2691
2692 if (!showit != !showing_tabline)
2693 {
2694 if (showit)
2695 ShowWindow(s_tabhwnd, SW_SHOW);
2696 else
2697 ShowWindow(s_tabhwnd, SW_HIDE);
2698 showing_tabline = showit;
2699 }
2700}
2701
2702/*
2703 * Return TRUE when tabline is displayed.
2704 */
2705 int
2706gui_mch_showing_tabline(void)
2707{
2708 return s_tabhwnd != NULL && showing_tabline;
2709}
2710
2711/*
2712 * Update the labels of the tabline.
2713 */
2714 void
2715gui_mch_update_tabline(void)
2716{
2717 tabpage_T *tp;
2718 TCITEM tie;
2719 int nr = 0;
2720 int curtabidx = 0;
2721 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723
2724 if (s_tabhwnd == NULL)
2725 return;
2726
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002727 // Enable unicode support
2728 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002729
2730 tie.mask = TCIF_TEXT;
2731 tie.iImage = -1;
2732
Bram Moolenaar734a8672019-12-02 22:49:38 +01002733 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2735
Bram Moolenaar734a8672019-12-02 22:49:38 +01002736 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002737 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2738 {
2739 if (tp == curtab)
2740 curtabidx = nr;
2741
2742 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2743 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002744 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 tie.pszText = "-Empty-";
2746 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2747 tabadded = 1;
2748 }
2749
2750 get_tabline_label(tp, FALSE);
2751 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002752
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002753 wstr = enc_to_utf16(NameBuff, NULL);
2754 if (wstr != NULL)
2755 {
2756 TCITEMW tiw;
2757
2758 tiw.mask = TCIF_TEXT;
2759 tiw.iImage = -1;
2760 tiw.pszText = wstr;
2761 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2762 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002763 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002764 }
2765
Bram Moolenaar734a8672019-12-02 22:49:38 +01002766 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2768 TabCtrl_DeleteItem(s_tabhwnd, nr);
2769
2770 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2771 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2772
Bram Moolenaar734a8672019-12-02 22:49:38 +01002773 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2775 RedrawWindow(s_tabhwnd, NULL, NULL,
2776 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2777
2778 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2779 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2780}
2781
2782/*
2783 * Set the current tab to "nr". First tab is 1.
2784 */
2785 void
2786gui_mch_set_curtab(int nr)
2787{
2788 if (s_tabhwnd == NULL)
2789 return;
2790
2791 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2792 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2793}
2794
2795#endif
2796
2797/*
2798 * ":simalt" command.
2799 */
2800 void
2801ex_simalt(exarg_T *eap)
2802{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002803 char_u *keys = eap->arg;
2804 int fill_typebuf = FALSE;
2805 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002806
2807 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2808 while (*keys)
2809 {
2810 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002811 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002812 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2813 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002814 fill_typebuf = TRUE;
2815 }
2816 if (fill_typebuf)
2817 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002818 // Put a NOP in the typeahead buffer so that the message will get
2819 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002820 key_name[0] = K_SPECIAL;
2821 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002822 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002823 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002824#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002825 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002826#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002827 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828 }
2829}
2830
2831/*
2832 * Create the find & replace dialogs.
2833 * You can't have both at once: ":find" when replace is showing, destroys
2834 * the replace dialog first, and the other way around.
2835 */
2836#ifdef MSWIN_FIND_REPLACE
2837 static void
2838initialise_findrep(char_u *initial_string)
2839{
2840 int wword = FALSE;
2841 int mcase = !p_ic;
2842 char_u *entry_text;
2843
Bram Moolenaar734a8672019-12-02 22:49:38 +01002844 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002845 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2846
2847 s_findrep_struct.hwndOwner = s_hwnd;
2848 s_findrep_struct.Flags = FR_DOWN;
2849 if (mcase)
2850 s_findrep_struct.Flags |= FR_MATCHCASE;
2851 if (wword)
2852 s_findrep_struct.Flags |= FR_WHOLEWORD;
2853 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002854 {
2855 WCHAR *p = enc_to_utf16(entry_text, NULL);
2856 if (p != NULL)
2857 {
2858 int len = s_findrep_struct.wFindWhatLen - 1;
2859
2860 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2861 s_findrep_struct.lpstrFindWhat[len] = NUL;
2862 vim_free(p);
2863 }
2864 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 vim_free(entry_text);
2866}
2867#endif
2868
2869 static void
2870set_window_title(HWND hwnd, char *title)
2871{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002872 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 {
2874 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875
Bram Moolenaar734a8672019-12-02 22:49:38 +01002876 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2878 if (wbuf != NULL)
2879 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002880 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002882 }
2883 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002884 else
2885 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002886}
2887
2888 void
2889gui_mch_find_dialog(exarg_T *eap)
2890{
2891#ifdef MSWIN_FIND_REPLACE
2892 if (s_findrep_msg != 0)
2893 {
2894 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2895 DestroyWindow(s_findrep_hwnd);
2896
2897 if (!IsWindow(s_findrep_hwnd))
2898 {
2899 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002900 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 }
2902
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002903 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 (void)SetFocus(s_findrep_hwnd);
2905
2906 s_findrep_is_find = TRUE;
2907 }
2908#endif
2909}
2910
2911
2912 void
2913gui_mch_replace_dialog(exarg_T *eap)
2914{
2915#ifdef MSWIN_FIND_REPLACE
2916 if (s_findrep_msg != 0)
2917 {
2918 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2919 DestroyWindow(s_findrep_hwnd);
2920
2921 if (!IsWindow(s_findrep_hwnd))
2922 {
2923 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002924 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002925 }
2926
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002927 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928 (void)SetFocus(s_findrep_hwnd);
2929
2930 s_findrep_is_find = FALSE;
2931 }
2932#endif
2933}
2934
2935
2936/*
2937 * Set visibility of the pointer.
2938 */
2939 void
2940gui_mch_mousehide(int hide)
2941{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002942 if (hide == gui.pointer_hidden)
2943 return;
2944
2945 ShowCursor(!hide);
2946 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002947}
2948
2949#ifdef FEAT_MENU
2950 static void
2951gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2952{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002953 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954 gui_mch_mousehide(FALSE);
2955
2956 (void)TrackPopupMenu(
2957 (HMENU)menu->submenu_id,
2958 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2959 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002960 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002961 s_hwnd,
2962 NULL);
2963 /*
2964 * NOTE: The pop-up menu can eat the mouse up event.
2965 * We deal with this in normal.c.
2966 */
2967}
2968#endif
2969
2970/*
2971 * Got a message when the system will go down.
2972 */
2973 static void
2974_OnEndSession(void)
2975{
2976 getout_preserve_modified(1);
2977}
2978
2979/*
2980 * Get this message when the user clicks on the cross in the top right corner
2981 * of a Windows95 window.
2982 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002983 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002984_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985{
2986 gui_shell_closed();
2987}
2988
2989/*
2990 * Get a message when the window is being destroyed.
2991 */
2992 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002993_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002994{
2995 if (!destroying)
2996 _OnClose(hwnd);
2997}
2998
2999 static void
3000_OnPaint(
3001 HWND hwnd)
3002{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003003 if (IsMinimized(hwnd))
3004 return;
3005
3006 PAINTSTRUCT ps;
3007
3008 out_flush(); // make sure all output has been processed
3009 (void)BeginPaint(hwnd, &ps);
3010
3011 // prevent multi-byte characters from misprinting on an invalid
3012 // rectangle
3013 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003014 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003015 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003017 GetClientRect(hwnd, &rect);
3018 ps.rcPaint.left = rect.left;
3019 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003020 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003021
3022 if (!IsRectEmpty(&ps.rcPaint))
3023 {
3024 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3025 ps.rcPaint.right - ps.rcPaint.left + 1,
3026 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3027 }
3028
3029 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003030}
3031
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032 static void
3033_OnSize(
3034 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003035 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036 int cx,
3037 int cy)
3038{
K.Takatac81e9bf2022-01-16 14:15:49 +00003039 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003040 {
3041 gui_resize_shell(cx, cy);
3042
Bram Moolenaar734a8672019-12-02 22:49:38 +01003043 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003045 }
3046}
3047
3048 static void
3049_OnSetFocus(
3050 HWND hwnd,
3051 HWND hwndOldFocus)
3052{
3053 gui_focus_change(TRUE);
3054 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003055 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003056}
3057
3058 static void
3059_OnKillFocus(
3060 HWND hwnd,
3061 HWND hwndNewFocus)
3062{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003063 if (destroying)
3064 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003065 gui_focus_change(FALSE);
3066 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003067 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003068}
3069
3070/*
3071 * Get a message when the user switches back to vim
3072 */
3073 static LRESULT
3074_OnActivateApp(
3075 HWND hwnd,
3076 BOOL fActivate,
3077 DWORD dwThreadId)
3078{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003079 // we call gui_focus_change() in _OnSetFocus()
3080 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003081 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003082}
3083
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003084 void
3085gui_mch_destroy_scrollbar(scrollbar_T *sb)
3086{
3087 DestroyWindow(sb->id);
3088}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003089
3090/*
3091 * Get current mouse coordinates in text window.
3092 */
3093 void
3094gui_mch_getmouse(int *x, int *y)
3095{
3096 RECT rct;
3097 POINT mp;
3098
3099 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003100 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003101 *x = (int)(mp.x - rct.left);
3102 *y = (int)(mp.y - rct.top);
3103}
3104
3105/*
3106 * Move mouse pointer to character at (x, y).
3107 */
3108 void
3109gui_mch_setmouse(int x, int y)
3110{
3111 RECT rct;
3112
3113 (void)GetWindowRect(s_textArea, &rct);
3114 (void)SetCursorPos(x + gui.border_offset + rct.left,
3115 y + gui.border_offset + rct.top);
3116}
3117
3118 static void
3119gui_mswin_get_valid_dimensions(
3120 int w,
3121 int h,
3122 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003123 int *valid_h,
3124 int *cols,
3125 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126{
3127 int base_width, base_height;
3128
3129 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003130 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3131 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003132 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003133 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3134 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3135 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3136 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003137 *cols = (w - base_width) / gui.char_width;
3138 *rows = (h - base_height) / gui.char_height;
3139 *valid_w = base_width + *cols * gui.char_width;
3140 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003141}
3142
3143 void
3144gui_mch_flash(int msec)
3145{
3146 RECT rc;
3147
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003148#if defined(FEAT_DIRECTX)
3149 if (IS_ENABLE_DIRECTX())
3150 DWriteContext_Flush(s_dwc);
3151#endif
3152
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003153 /*
3154 * Note: InvertRect() excludes right and bottom of rectangle.
3155 */
3156 rc.left = 0;
3157 rc.top = 0;
3158 rc.right = gui.num_cols * gui.char_width;
3159 rc.bottom = gui.num_rows * gui.char_height;
3160 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003161 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003162
Bram Moolenaar734a8672019-12-02 22:49:38 +01003163 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164
3165 InvertRect(s_hdc, &rc);
3166}
3167
3168/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003169 * Check if the specified point is on-screen. (multi-monitor aware)
3170 */
3171 static BOOL
3172is_point_onscreen(int x, int y)
3173{
3174 POINT pt = {x, y};
3175
3176 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3177}
3178
3179/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003180 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003181 *
3182 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3183 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3184 * only works when the whole window is on-screen.
3185 */
3186 static BOOL
3187is_window_onscreen(HWND hwnd)
3188{
3189 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003190 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003191
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003192 GetClientRect(hwnd, &rc);
3193 p1.x = rc.left;
3194 p1.y = rc.top;
3195 p2.x = rc.right - 1;
3196 p2.y = rc.bottom - 1;
3197 ClientToScreen(hwnd, &p1);
3198 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003199
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003200 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003201 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003202 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003203 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003204 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003205 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003206 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003207 return FALSE;
3208 return TRUE;
3209}
3210
3211/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212 * Return flags used for scrolling.
3213 * The SW_INVALIDATE is required when part of the window is covered or
3214 * off-screen. Refer to MS KB Q75236.
3215 */
3216 static int
3217get_scroll_flags(void)
3218{
3219 HWND hwnd;
3220 RECT rcVim, rcOther, rcDest;
3221
Bram Moolenaar185577e2020-10-29 20:08:21 +01003222 // Check if the window is (partly) off-screen.
3223 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 return SW_INVALIDATE;
3225
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003226 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003227 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3229 if (IsWindowVisible(hwnd))
3230 {
3231 GetWindowRect(hwnd, &rcOther);
3232 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3233 return SW_INVALIDATE;
3234 }
3235 return 0;
3236}
3237
3238/*
3239 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3240 * may not be scrolled out properly.
3241 * For gVim, when _OnScroll() is repeated, the character at the
3242 * previous cursor position may be left drawn after scroll.
3243 * The problem can be avoided by calling GetPixel() to get a pixel in
3244 * the region before ScrollWindowEx().
3245 */
3246 static void
3247intel_gpu_workaround(void)
3248{
3249 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3250}
3251
3252/*
3253 * Delete the given number of lines from the given row, scrolling up any
3254 * text further down within the scroll region.
3255 */
3256 void
3257gui_mch_delete_lines(
3258 int row,
3259 int num_lines)
3260{
3261 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003262
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003263 rc.left = FILL_X(gui.scroll_region_left);
3264 rc.right = FILL_X(gui.scroll_region_right + 1);
3265 rc.top = FILL_Y(row);
3266 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3267
Bram Moolenaar92467d32017-12-05 13:22:16 +01003268#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003269 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003270 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003271 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003272 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003273 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003274#endif
3275 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003276#if defined(FEAT_DIRECTX)
3277 if (IS_ENABLE_DIRECTX())
3278 DWriteContext_Flush(s_dwc);
3279#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003280 intel_gpu_workaround();
3281 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003283 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003284 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003285
Bram Moolenaar734a8672019-12-02 22:49:38 +01003286 // This seems to be required to avoid the cursor disappearing when
3287 // scrolling such that the cursor ends up in the top-left character on
3288 // the screen... But why? (Webb)
3289 // It's probably fixed by disabling drawing the cursor while scrolling.
3290 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003291
3292 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3293 gui.scroll_region_left,
3294 gui.scroll_region_bot, gui.scroll_region_right);
3295}
3296
3297/*
3298 * Insert the given number of lines before the given row, scrolling down any
3299 * following text within the scroll region.
3300 */
3301 void
3302gui_mch_insert_lines(
3303 int row,
3304 int num_lines)
3305{
3306 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003307
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308 rc.left = FILL_X(gui.scroll_region_left);
3309 rc.right = FILL_X(gui.scroll_region_right + 1);
3310 rc.top = FILL_Y(row);
3311 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003312
3313#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003314 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003315 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003316 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003317 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003318 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003319#endif
3320 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003321#if defined(FEAT_DIRECTX)
3322 if (IS_ENABLE_DIRECTX())
3323 DWriteContext_Flush(s_dwc);
3324#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003325 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003326 // The SW_INVALIDATE is required when part of the window is covered or
3327 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003328 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003329 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003330 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003331 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003333 gui_clear_block(row, gui.scroll_region_left,
3334 row + num_lines - 1, gui.scroll_region_right);
3335}
3336
3337
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003339gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340{
3341#if defined(FEAT_DIRECTX)
3342 DWriteContext_Close(s_dwc);
3343 DWrite_Final();
3344 s_dwc = NULL;
3345#endif
3346
3347 ReleaseDC(s_textArea, s_hdc);
3348 DeleteObject(s_brush);
3349
3350#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003351 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3353#endif
3354
Bram Moolenaar734a8672019-12-02 22:49:38 +01003355 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003356 if (s_hwnd != NULL)
3357 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003358 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 DestroyWindow(s_hwnd);
3360 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361}
3362
3363 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003364logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365{
3366 char *p;
3367 char *res;
3368 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003369 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003370 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003371 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003373 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3374 if (font_name == NULL)
3375 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003377 quality_name = quality_id2name((int)lf.lfQuality);
3378
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003379 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003380 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003381 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003382 if (res != NULL)
3383 {
3384 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003385 // make a normal font string out of the lf thing:
3386 points = pixels_to_points(
3387 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3388 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3389 sprintf((char *)p, "%s:h%d", font_name, points);
3390 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003391 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392 while (*p)
3393 {
3394 if (*p == ' ')
3395 *p = '_';
3396 ++p;
3397 }
3398 if (lf.lfItalic)
3399 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003400 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 STRCAT(p, ":b");
3402 if (lf.lfUnderline)
3403 STRCAT(p, ":u");
3404 if (lf.lfStrikeOut)
3405 STRCAT(p, ":s");
3406 if (charset_name != NULL)
3407 {
3408 STRCAT(p, ":c");
3409 STRCAT(p, charset_name);
3410 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003411 if (quality_name != NULL)
3412 {
3413 STRCAT(p, ":q");
3414 STRCAT(p, quality_name);
3415 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416 }
3417
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003418 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419 return (char_u *)res;
3420}
3421
3422
3423#ifdef FEAT_MBYTE_IME
3424/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003425 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003426 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003427 */
3428 static void
3429update_im_font(void)
3430{
K.Takatac81e9bf2022-01-16 14:15:49 +00003431 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432
3433 if (p_guifontwide != NULL && *p_guifontwide != NUL
3434 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003435 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436 norm_logfont = lf_wide;
3437 else
3438 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003439
3440 lf = norm_logfont;
3441 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3442 // Work around when PerMonitorV2 is not enabled in the process level.
3443 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3444 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003445}
3446#endif
3447
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003448/*
3449 * Handler of gui.wide_font (p_guifontwide) changed notification.
3450 */
3451 void
3452gui_mch_wide_font_changed(void)
3453{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003454 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003455
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003456#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003457 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003458#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459
3460 gui_mch_free_font(gui.wide_ital_font);
3461 gui.wide_ital_font = NOFONT;
3462 gui_mch_free_font(gui.wide_bold_font);
3463 gui.wide_bold_font = NOFONT;
3464 gui_mch_free_font(gui.wide_boldital_font);
3465 gui.wide_boldital_font = NOFONT;
3466
3467 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003468 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469 {
3470 if (!lf.lfItalic)
3471 {
3472 lf.lfItalic = TRUE;
3473 gui.wide_ital_font = get_font_handle(&lf);
3474 lf.lfItalic = FALSE;
3475 }
3476 if (lf.lfWeight < FW_BOLD)
3477 {
3478 lf.lfWeight = FW_BOLD;
3479 gui.wide_bold_font = get_font_handle(&lf);
3480 if (!lf.lfItalic)
3481 {
3482 lf.lfItalic = TRUE;
3483 gui.wide_boldital_font = get_font_handle(&lf);
3484 }
3485 }
3486 }
3487}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488
3489/*
3490 * Initialise vim to use the font with the given name.
3491 * Return FAIL if the font could not be loaded, OK otherwise.
3492 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003493 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003494gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003495{
K.Takatac81e9bf2022-01-16 14:15:49 +00003496 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497 GuiFont font = NOFONT;
3498 char_u *p;
3499
Bram Moolenaar734a8672019-12-02 22:49:38 +01003500 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003502 {
3503 lfOrig = lf;
3504 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003505 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003506 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 if (font == NOFONT)
3508 return FAIL;
3509
3510 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003511 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003512#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 norm_logfont = lf;
3514 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003515 if (!s_in_dpichanged)
3516 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003517#endif
3518 gui_mch_free_font(gui.norm_font);
3519 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003520 current_font_height = lfOrig.lfHeight;
Ken Takatada5da652023-10-05 20:20:58 +02003521 UpdateFontSize(font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522
K.Takatac81e9bf2022-01-16 14:15:49 +00003523 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003524 if (p != NULL)
3525 {
3526 hl_set_font_name(p);
3527
Bram Moolenaar734a8672019-12-02 22:49:38 +01003528 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003529 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3530 {
3531 vim_free(p_guifont);
3532 p_guifont = p;
3533 }
3534 else
3535 vim_free(p);
3536 }
3537
3538 gui_mch_free_font(gui.ital_font);
3539 gui.ital_font = NOFONT;
3540 gui_mch_free_font(gui.bold_font);
3541 gui.bold_font = NOFONT;
3542 gui_mch_free_font(gui.boldital_font);
3543 gui.boldital_font = NOFONT;
3544
3545 if (!lf.lfItalic)
3546 {
3547 lf.lfItalic = TRUE;
3548 gui.ital_font = get_font_handle(&lf);
3549 lf.lfItalic = FALSE;
3550 }
3551 if (lf.lfWeight < FW_BOLD)
3552 {
3553 lf.lfWeight = FW_BOLD;
3554 gui.bold_font = get_font_handle(&lf);
3555 if (!lf.lfItalic)
3556 {
3557 lf.lfItalic = TRUE;
3558 gui.boldital_font = get_font_handle(&lf);
3559 }
3560 }
3561
3562 return OK;
3563}
3564
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003565/*
3566 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003567 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003568 */
3569 int
3570gui_mch_maximized(void)
3571{
3572 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003573 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003574
3575 wp.length = sizeof(WINDOWPLACEMENT);
3576 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003577 {
3578 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003579 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003580 && wp.flags == WPF_RESTORETOMAXIMIZED))
3581 return TRUE;
3582 if (wp.showCmd == SW_SHOWMINIMIZED)
3583 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003584
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003585 // Assume the window is snapped when the sizes from two APIs differ.
3586 GetWindowRect(s_hwnd, &rc);
3587 if ((rc.right - rc.left !=
3588 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3589 || (rc.bottom - rc.top !=
3590 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3591 return TRUE;
3592 }
3593 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594}
3595
3596/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003597 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3598 * is set. Compute the new Rows and Columns. This is like resizing the
3599 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003600 */
3601 void
3602gui_mch_newfont(void)
3603{
3604 RECT rect;
3605
3606 GetWindowRect(s_hwnd, &rect);
3607 if (win_socket_id == 0)
3608 {
3609 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003610 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3611 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003612 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003613 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3614 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3615 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3616 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617 }
3618 else
3619 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003620 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003621 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003622 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623 }
3624}
3625
3626/*
3627 * Set the window title
3628 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003629 void
3630gui_mch_settitle(
3631 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003632 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633{
3634 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3635}
3636
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003637#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003638// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3639// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003640static LPCSTR mshape_idcs[] =
3641{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003642 IDC_ARROW, // arrow
3643 MAKEINTRESOURCE(0), // blank
3644 IDC_IBEAM, // beam
3645 IDC_SIZENS, // updown
3646 IDC_SIZENS, // udsizing
3647 IDC_SIZEWE, // leftright
3648 IDC_SIZEWE, // lrsizing
3649 IDC_WAIT, // busy
3650 IDC_NO, // no
3651 IDC_ARROW, // crosshair
3652 IDC_ARROW, // hand1
3653 IDC_ARROW, // hand2
3654 IDC_ARROW, // pencil
3655 IDC_ARROW, // question
3656 IDC_ARROW, // right-arrow
3657 IDC_UPARROW, // up-arrow
3658 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659};
3660
3661 void
3662mch_set_mouse_shape(int shape)
3663{
3664 LPCSTR idc;
3665
3666 if (shape == MSHAPE_HIDE)
3667 ShowCursor(FALSE);
3668 else
3669 {
3670 if (shape >= MSHAPE_NUMBERED)
3671 idc = IDC_ARROW;
3672 else
3673 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003674 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675 if (!p_mh)
3676 {
3677 POINT mp;
3678
Bram Moolenaar734a8672019-12-02 22:49:38 +01003679 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003680 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003681 (void)SetCursorPos(mp.x, mp.y);
3682 ShowCursor(TRUE);
3683 }
3684 }
3685}
3686#endif
3687
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003688#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003690 * Wide version of convert_filter().
3691 */
3692 static WCHAR *
3693convert_filterW(char_u *s)
3694{
3695 char_u *tmp;
3696 int len;
3697 WCHAR *res;
3698
3699 tmp = convert_filter(s);
3700 if (tmp == NULL)
3701 return NULL;
3702 len = (int)STRLEN(s) + 3;
3703 res = enc_to_utf16(tmp, &len);
3704 vim_free(tmp);
3705 return res;
3706}
3707
3708/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003709 * Pop open a file browser and return the file selected, in allocated memory,
3710 * or NULL if Cancel is hit.
3711 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3712 * title - Title message for the file browser dialog.
3713 * dflt - Default name of file.
3714 * ext - Default extension to be added to files without extensions.
3715 * initdir - directory in which to open the browser (NULL = current dir)
3716 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003717 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003718 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003719gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 int saving,
3721 char_u *title,
3722 char_u *dflt,
3723 char_u *ext,
3724 char_u *initdir,
3725 char_u *filter)
3726{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727 // We always use the wide function. This means enc_to_utf16() must work,
3728 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729 OPENFILENAMEW fileStruct;
3730 WCHAR fileBuf[MAXPATHL];
3731 WCHAR *wp;
3732 int i;
3733 WCHAR *titlep = NULL;
3734 WCHAR *extp = NULL;
3735 WCHAR *initdirp = NULL;
3736 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003737 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003738 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003739
3740 if (dflt == NULL)
3741 fileBuf[0] = NUL;
3742 else
3743 {
3744 wp = enc_to_utf16(dflt, NULL);
3745 if (wp == NULL)
3746 fileBuf[0] = NUL;
3747 else
3748 {
3749 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3750 fileBuf[i] = wp[i];
3751 fileBuf[i] = NUL;
3752 vim_free(wp);
3753 }
3754 }
3755
Bram Moolenaar734a8672019-12-02 22:49:38 +01003756 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 filterp = convert_filterW(filter);
3758
Bram Moolenaara80faa82020-04-12 19:37:17 +02003759 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003760# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003761 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003762 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003763# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003765# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003766
3767 if (title != NULL)
3768 titlep = enc_to_utf16(title, NULL);
3769 fileStruct.lpstrTitle = titlep;
3770
3771 if (ext != NULL)
3772 extp = enc_to_utf16(ext, NULL);
3773 fileStruct.lpstrDefExt = extp;
3774
3775 fileStruct.lpstrFile = fileBuf;
3776 fileStruct.nMaxFile = MAXPATHL;
3777 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003778 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3779 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 if (initdir != NULL && *initdir != NUL)
3781 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003782 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 initdirp = enc_to_utf16(initdir, NULL);
3784 if (initdirp != NULL)
3785 {
3786 for (wp = initdirp; *wp != NUL; ++wp)
3787 if (*wp == '/')
3788 *wp = '\\';
3789 }
3790 fileStruct.lpstrInitialDir = initdirp;
3791 }
3792
3793 /*
3794 * TODO: Allow selection of multiple files. Needs another arg to this
3795 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3796 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3797 * files that don't exist yet, so I haven't put it in. What about
3798 * OFN_PATHMUSTEXIST?
3799 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3800 */
3801 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003802# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 if (curbuf->b_p_bin)
3804 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003805# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003806 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003807 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003809 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003810
3811 vim_free(filterp);
3812 vim_free(initdirp);
3813 vim_free(titlep);
3814 vim_free(extp);
3815
K.Takata14b8d6a2022-01-20 15:05:22 +00003816 if (!ret)
3817 return NULL;
3818
3819 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003820 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003821 if (p == NULL)
3822 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003823
Bram Moolenaar734a8672019-12-02 22:49:38 +01003824 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825 SetFocus(s_hwnd);
3826
Bram Moolenaar734a8672019-12-02 22:49:38 +01003827 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003828 q = vim_strsave(shorten_fname1(p));
3829 vim_free(p);
3830 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003831}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003832
3833
3834/*
3835 * Convert the string s to the proper format for a filter string by replacing
3836 * the \t and \n delimiters with \0.
3837 * Returns the converted string in allocated memory.
3838 *
3839 * Keep in sync with convert_filterW() above!
3840 */
3841 static char_u *
3842convert_filter(char_u *s)
3843{
3844 char_u *res;
3845 unsigned s_len = (unsigned)STRLEN(s);
3846 unsigned i;
3847
3848 res = alloc(s_len + 3);
3849 if (res != NULL)
3850 {
3851 for (i = 0; i < s_len; ++i)
3852 if (s[i] == '\t' || s[i] == '\n')
3853 res[i] = '\0';
3854 else
3855 res[i] = s[i];
3856 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003857 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 res[s_len + 1] = NUL;
3859 res[s_len + 2] = NUL;
3860 }
3861 return res;
3862}
3863
3864/*
3865 * Select a directory.
3866 */
3867 char_u *
3868gui_mch_browsedir(char_u *title, char_u *initdir)
3869{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003870 // We fake this: Use a filter that doesn't select anything and a default
3871 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3873 initdir, (char_u *)_("Directory\t*.nothing\n"));
3874}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003875#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003876
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003877 static void
3878_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003879 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003880 HDROP hDrop)
3881{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003882#define BUFPATHLEN _MAX_PATH
3883#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003884 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003885 char szFile[BUFPATHLEN];
3886 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3887 UINT i;
3888 char_u **fnames;
3889 POINT pt;
3890 int_u modifiers = 0;
3891
Bram Moolenaar734a8672019-12-02 22:49:38 +01003892 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893 DragQueryPoint(hDrop, &pt);
3894 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3895
3896 reset_VIsual();
3897
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003898 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003899
3900 if (fnames != NULL)
3901 for (i = 0; i < cFiles; ++i)
3902 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003903 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3904 fnames[i] = utf16_to_enc(wszFile, NULL);
3905 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906 {
3907 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3908 fnames[i] = vim_strsave((char_u *)szFile);
3909 }
3910 }
3911
3912 DragFinish(hDrop);
3913
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003914 if (fnames == NULL)
3915 return;
LemonBoy45684c62022-04-24 15:46:42 +01003916
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003917 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003919 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
3920 modifiers |= MOUSE_SHIFT;
3921 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
3922 modifiers |= MOUSE_CTRL;
3923 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
3924 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003925
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003926 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3927
3928 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929}
3930
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 static int
3932_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003933 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003934 HWND hwndCtl,
3935 UINT code,
3936 int pos)
3937{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003938 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003939 scrollbar_T *sb, *sb_info;
3940 long val;
3941 int dragging = FALSE;
3942 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003943 SCROLLINFO si;
3944
3945 si.cbSize = sizeof(si);
3946 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003947
3948 sb = gui_mswin_find_scrollbar(hwndCtl);
3949 if (sb == NULL)
3950 return 0;
3951
Bram Moolenaar734a8672019-12-02 22:49:38 +01003952 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003953 {
3954 /*
3955 * Careful: need to get scrollbar info out of first (left) scrollbar
3956 * for window, but keep real scrollbar too because we must pass it to
3957 * gui_drag_scrollbar().
3958 */
3959 sb_info = &sb->wp->w_scrollbars[0];
3960 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003961 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003962 sb_info = sb;
3963 val = sb_info->value;
3964
3965 switch (code)
3966 {
3967 case SB_THUMBTRACK:
3968 val = pos;
3969 dragging = TRUE;
3970 if (sb->scroll_shift > 0)
3971 val <<= sb->scroll_shift;
3972 break;
3973 case SB_LINEDOWN:
3974 val++;
3975 break;
3976 case SB_LINEUP:
3977 val--;
3978 break;
3979 case SB_PAGEDOWN:
3980 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3981 break;
3982 case SB_PAGEUP:
3983 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3984 break;
3985 case SB_TOP:
3986 val = 0;
3987 break;
3988 case SB_BOTTOM:
3989 val = sb_info->max;
3990 break;
3991 case SB_ENDSCROLL:
3992 if (prev_code == SB_THUMBTRACK)
3993 {
3994 /*
3995 * "pos" only gives us 16-bit data. In case of large file,
3996 * use GetScrollPos() which returns 32-bit. Unfortunately it
3997 * is not valid while the scrollbar is being dragged.
3998 */
3999 val = GetScrollPos(hwndCtl, SB_CTL);
4000 if (sb->scroll_shift > 0)
4001 val <<= sb->scroll_shift;
4002 }
4003 break;
4004
4005 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004006 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007 return 0;
4008 }
4009 prev_code = code;
4010
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004011 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4012 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013
4014 /*
4015 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4016 */
4017 if (sb->wp != NULL)
4018 {
4019 scrollbar_T *sba = sb->wp->w_scrollbars;
4020 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4021
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004022 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023 }
4024
Bram Moolenaar734a8672019-12-02 22:49:38 +01004025 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004026 s_busy_processing = TRUE;
4027
Bram Moolenaar734a8672019-12-02 22:49:38 +01004028 // When "allow_scrollbar" is FALSE still need to remember the new
4029 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004030 dont_scroll = !allow_scrollbar;
4031
Bram Moolenaara338adc2018-01-31 20:51:47 +01004032 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004033 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004034 mch_enable_flush();
4035 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004036
4037 s_busy_processing = FALSE;
4038 dont_scroll = dont_scroll_save;
4039
4040 return 0;
4041}
4042
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#ifdef FEAT_XPM_W32
4045# include "xpm_w32.h"
4046#endif
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaar734a8672019-12-02 22:49:38 +01004049// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#define TEAROFF_PADDING_X 2
4051#define TEAROFF_BUTTON_PAD_X 8
4052#define TEAROFF_MIN_WIDTH 200
4053#define TEAROFF_SUBMENU_LABEL ">>"
4054#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4055
4056
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004057#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058# define ID_BEVAL_TOOLTIP 200
4059# define BEVAL_TEXT_LEN MAXPATHL
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004062static UINT_PTR beval_timer_id = 0;
4063static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004064#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004065
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004066
Bram Moolenaar734a8672019-12-02 22:49:38 +01004067// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069#ifdef FEAT_MENU
4070static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073/*
4074 * Use the system font for dialogs and tear-off menus. Remove this line to
4075 * use DLG_FONT_NAME.
4076 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004077#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080#define VIM_CLASSW L"Vim"
4081
Bram Moolenaar734a8672019-12-02 22:49:38 +01004082// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4083// thus there should be room for every dialog. For tearoffs it's made bigger
4084// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#define DLG_ALLOC_SIZE 16 * 1024
4086
4087/*
4088 * stuff for dialogs, menus, tearoffs etc.
4089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090static PWORD
4091add_dialog_element(
4092 PWORD p,
4093 DWORD lStyle,
4094 WORD x,
4095 WORD y,
4096 WORD w,
4097 WORD h,
4098 WORD Id,
4099 WORD clss,
4100 const char *caption);
4101static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004102static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004103#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106static void get_dialog_font_metrics(void);
4107
4108static int dialog_default_button = -1;
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_TOOLBAR
4111static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004112static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004113static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004115#else
4116# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#endif
4118
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004119#ifdef FEAT_GUI_TABLINE
4120static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004121static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004122#endif
4123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124#ifdef FEAT_MBYTE_IME
4125static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4126static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4127#endif
4128#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4129# ifdef NOIME
4130typedef struct tagCOMPOSITIONFORM {
4131 DWORD dwStyle;
4132 POINT ptCurrentPos;
4133 RECT rcArea;
4134} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4135typedef HANDLE HIMC;
4136# endif
4137
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004138static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004139static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4140static HIMC (WINAPI *pImmGetContext)(HWND);
4141static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4142static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4143static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4144static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004145static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4146static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004147static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4148static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004149static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150static void dyn_imm_load(void);
4151#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152# define pImmGetCompositionStringW ImmGetCompositionStringW
4153# define pImmGetContext ImmGetContext
4154# define pImmAssociateContext ImmAssociateContext
4155# define pImmReleaseContext ImmReleaseContext
4156# define pImmGetOpenStatus ImmGetOpenStatus
4157# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004158# define pImmGetCompositionFontW ImmGetCompositionFontW
4159# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160# define pImmSetCompositionWindow ImmSetCompositionWindow
4161# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004162# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163#endif
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#ifdef FEAT_MENU
4166/*
4167 * Figure out how high the menu bar is at the moment.
4168 */
4169 static int
4170gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004171 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 static int old_menu_height = -1;
4174
4175 RECT rc1, rc2;
4176 int num;
4177 int menu_height;
4178
4179 if (gui.menu_is_active)
4180 num = GetMenuItemCount(s_menuBar);
4181 else
4182 num = 0;
4183
4184 if (num == 0)
4185 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004186 else if (IsMinimized(s_hwnd))
4187 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004188 // The height of the menu cannot be determined while the window is
4189 // minimized. Take the previous height if the menu is changed in that
4190 // state, to avoid that Vim's vertical window size accidentally
4191 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004192 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else
4195 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004196 /*
4197 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4198 * seem to have been set yet, so menu wraps in default window
4199 * width which is very narrow. Instead just return height of a
4200 * single menu item. Will still be wrong when the menu really
4201 * should wrap over more than one line.
4202 */
4203 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4204 if (gui.starting)
4205 menu_height = rc1.bottom - rc1.top + 1;
4206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004208 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4209 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 }
4212
4213 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004214 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004215 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 return menu_height;
4218}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004219#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221
4222/*
4223 * Setup for the Intellimouse
4224 */
LemonBoyc27747e2022-05-07 12:25:40 +01004225 static long
4226mouse_vertical_scroll_step(void)
4227{
4228 UINT val;
4229 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4230 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4231 return 3; // Safe default;
4232}
4233
4234 static long
4235mouse_horizontal_scroll_step(void)
4236{
4237 UINT val;
4238 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4239 return (long)val;
4240 return 3; // Safe default;
4241}
4242
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 static void
4244init_mouse_wheel(void)
4245{
LemonBoyc27747e2022-05-07 12:25:40 +01004246 // Get the default values for the horizontal and vertical scroll steps from
4247 // the system.
4248 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4249 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250}
4251
Bram Moolenaarae20f342019-11-05 21:09:23 +01004252/*
LemonBoya773d842022-05-10 20:54:46 +01004253 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004256_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
LemonBoy365d8f72022-05-05 19:23:07 +01004258 int button;
4259 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004260 int modifiers = 0;
4261 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004262 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4263 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
Bram Moolenaarae20f342019-11-05 21:09:23 +01004265 wp = gui_mouse_window(FIND_POPUP);
4266
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004267#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004268 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004269 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004270 cmdarg_T cap;
4271 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004272
Bram Moolenaarae20f342019-11-05 21:09:23 +01004273 // Mouse hovers over popup window, scroll it if possible.
4274 mouse_row = wp->w_winrow;
4275 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004276 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004277 if (horizontal)
4278 {
4279 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4280 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4281 }
4282 else
4283 {
4284 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4285 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4286 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004287 clear_oparg(&oa);
4288 cap.oap = &oa;
4289 nv_mousescroll(&cap);
4290 update_screen(0);
4291 setcursor();
4292 out_flush();
4293 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004294 }
4295#endif
4296
Bram Moolenaarae20f342019-11-05 21:09:23 +01004297 if (wp == NULL || !p_scf)
4298 wp = curwin;
4299
LemonBoy365d8f72022-05-05 19:23:07 +01004300 // Translate the scroll event into an event that Vim can process so that
4301 // the user has a chance to map the scrollwheel buttons.
4302 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004303 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 else
LemonBoy365d8f72022-05-05 19:23:07 +01004305 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004306
4307 kbd_modifiers = get_active_modifiers();
4308
4309 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4310 modifiers |= MOUSE_SHIFT;
4311 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4312 modifiers |= MOUSE_CTRL;
4313 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4314 modifiers |= MOUSE_ALT;
4315
LemonBoya773d842022-05-10 20:54:46 +01004316 // The cursor position is relative to the upper-left corner of the screen.
4317 pt.x = GET_X_LPARAM(lParam);
4318 pt.y = GET_Y_LPARAM(lParam);
4319 ScreenToClient(s_textArea, &pt);
4320
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004321 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322}
4323
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004324#ifdef USE_SYSMENU_FONT
4325/*
4326 * Get Menu Font.
4327 * Return OK or FAIL.
4328 */
4329 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004330gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004331{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004332 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004333
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004334 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4335 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004336 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004337 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004338 &nm,
4339 0))
4340 return FAIL;
4341 *lf = nm.lfMenuFont;
4342 return OK;
4343}
4344#endif
4345
4346
4347#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4348/*
4349 * Set the GUI tabline font to the system menu font
4350 */
4351 static void
4352set_tabline_font(void)
4353{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004354 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004355 HFONT font;
4356 HWND hwnd;
4357 HDC hdc;
4358 HFONT hfntOld;
4359 TEXTMETRIC tm;
4360
4361 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4362 return;
4363
K.Takatac81e9bf2022-01-16 14:15:49 +00004364 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004365 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004366
4367 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4368
4369 /*
4370 * Compute the height of the font used for the tab text
4371 */
4372 hwnd = GetDesktopWindow();
4373 hdc = GetWindowDC(hwnd);
4374 hfntOld = SelectFont(hdc, font);
4375
4376 GetTextMetrics(hdc, &tm);
4377
4378 SelectFont(hdc, hfntOld);
4379 ReleaseDC(hwnd, hdc);
4380
4381 /*
4382 * The space used by the tab border and the space between the tab label
4383 * and the tab border is included as 7.
4384 */
4385 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4386}
K.Takatac81e9bf2022-01-16 14:15:49 +00004387#else
4388# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004389#endif
4390
Bram Moolenaar520470a2005-06-16 21:59:56 +00004391/*
4392 * Invoked when a setting was changed.
4393 */
4394 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004395_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004396{
LemonBoy365d8f72022-05-05 19:23:07 +01004397 switch (param)
4398 {
4399 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004400 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004401 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004402 case SPI_SETWHEELSCROLLCHARS:
4403 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004404 break;
4405 case SPI_SETNONCLIENTMETRICS:
4406 set_tabline_font();
4407 break;
4408 default:
4409 break;
4410 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004411 return 0;
4412}
4413
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#ifdef FEAT_NETBEANS_INTG
4415 static void
4416_OnWindowPosChanged(
4417 HWND hwnd,
4418 const LPWINDOWPOS lpwpos)
4419{
4420 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004421 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
4423 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4424 || lpwpos->cx != cx || lpwpos->cy != cy))
4425 {
4426 x = lpwpos->x;
4427 y = lpwpos->y;
4428 cx = lpwpos->cx;
4429 cy = lpwpos->cy;
4430 netbeans_frame_moved(x, y);
4431 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004432 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004433 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434}
4435#endif
4436
K.Takata4f2417f2021-06-05 16:25:32 +02004437
4438static HWND hwndTip = NULL;
4439
4440 static void
4441show_sizing_tip(int cols, int rows)
4442{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004443 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004444 char buf[32];
4445
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004446 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004447 ti.hwnd = s_hwnd;
4448 ti.uId = (UINT_PTR)s_hwnd;
4449 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4450 ti.lpszText = buf;
4451 sprintf(buf, "%dx%d", cols, rows);
4452 if (hwndTip == NULL)
4453 {
4454 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4455 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4456 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4457 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4458 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4459 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4460 }
4461 else
4462 {
4463 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4464 }
4465 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4466}
4467
4468 static void
4469destroy_sizing_tip(void)
4470{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004471 if (hwndTip == NULL)
4472 return;
4473
4474 DestroyWindow(hwndTip);
4475 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004476}
4477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 static int
4479_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 UINT fwSide,
4481 LPRECT lprc)
4482{
4483 int w, h;
4484 int valid_w, valid_h;
4485 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004486 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 w = lprc->right - lprc->left;
4489 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004490 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 w_offset = w - valid_w;
4492 h_offset = h - valid_h;
4493
4494 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4495 || fwSide == WMSZ_BOTTOMLEFT)
4496 lprc->left += w_offset;
4497 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4498 || fwSide == WMSZ_BOTTOMRIGHT)
4499 lprc->right -= w_offset;
4500
4501 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4502 || fwSide == WMSZ_TOPRIGHT)
4503 lprc->top += h_offset;
4504 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4505 || fwSide == WMSZ_BOTTOMRIGHT)
4506 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004507
4508 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 return TRUE;
4510}
4511
K.Takata92000e22022-01-20 15:10:57 +00004512#ifdef FEAT_GUI_TABLINE
4513 static void
4514_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4515{
4516 if (gui_mch_showing_tabline())
4517 {
4518 POINT pt;
4519 RECT rect;
4520
4521 /*
4522 * If the cursor is on the tabline, display the tab menu
4523 */
4524 GetCursorPos(&pt);
4525 GetWindowRect(s_textArea, &rect);
4526 if (pt.y < rect.top)
4527 {
4528 show_tabline_popup_menu();
4529 return;
4530 }
4531 }
4532 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4533}
4534
4535 static void
4536_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4537{
4538 /*
4539 * If the user double clicked the tabline, create a new tab
4540 */
4541 if (gui_mch_showing_tabline())
4542 {
4543 POINT pt;
4544 RECT rect;
4545
4546 GetCursorPos(&pt);
4547 GetWindowRect(s_textArea, &rect);
4548 if (pt.y < rect.top)
4549 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4550 }
4551 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4552}
4553#endif
4554
4555 static UINT
4556_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4557{
4558 UINT result;
4559 int x, y;
4560
4561 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4562 if (result != HTCLIENT)
4563 return result;
4564
4565#ifdef FEAT_GUI_TABLINE
4566 if (gui_mch_showing_tabline())
4567 {
4568 RECT rct;
4569
4570 // If the cursor is on the GUI tabline, don't process this event
4571 GetWindowRect(s_textArea, &rct);
4572 if (yPos < rct.top)
4573 return result;
4574 }
4575#endif
4576 (void)gui_mch_get_winpos(&x, &y);
4577 xPos -= x;
4578
4579 if (xPos < 48) // <VN> TODO should use system metric?
4580 return HTBOTTOMLEFT;
4581 else
4582 return HTBOTTOMRIGHT;
4583}
4584
4585#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4586 static LRESULT
4587_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4588{
4589 switch (hdr->code)
4590 {
4591 case TTN_GETDISPINFOW:
4592 case TTN_GETDISPINFO:
4593 {
4594 char_u *str = NULL;
4595 static void *tt_text = NULL;
4596
4597 VIM_CLEAR(tt_text);
4598
4599# ifdef FEAT_GUI_TABLINE
4600 if (gui_mch_showing_tabline()
4601 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4602 {
4603 POINT pt;
4604 /*
4605 * Mouse is over the GUI tabline. Display the
4606 * tooltip for the tab under the cursor
4607 *
4608 * Get the cursor position within the tab control
4609 */
4610 GetCursorPos(&pt);
4611 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4612 {
4613 TCHITTESTINFO htinfo;
4614 int idx;
4615
4616 /*
4617 * Get the tab under the cursor
4618 */
4619 htinfo.pt.x = pt.x;
4620 htinfo.pt.y = pt.y;
4621 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4622 if (idx != -1)
4623 {
4624 tabpage_T *tp;
4625
4626 tp = find_tabpage(idx + 1);
4627 if (tp != NULL)
4628 {
4629 get_tabline_label(tp, TRUE);
4630 str = NameBuff;
4631 }
4632 }
4633 }
4634 }
4635# endif
4636# ifdef FEAT_TOOLBAR
4637# ifdef FEAT_GUI_TABLINE
4638 else
4639# endif
4640 {
4641 UINT idButton;
4642 vimmenu_T *pMenu;
4643
4644 idButton = (UINT) hdr->idFrom;
4645 pMenu = gui_mswin_find_menu(root_menu, idButton);
4646 if (pMenu)
4647 str = pMenu->strings[MENU_INDEX_TIP];
4648 }
4649# endif
4650 if (str == NULL)
4651 break;
4652
4653 // Set the maximum width, this also enables using \n for
4654 // line break.
4655 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4656
4657 if (hdr->code == TTN_GETDISPINFOW)
4658 {
4659 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4660
4661 tt_text = enc_to_utf16(str, NULL);
4662 lpdi->lpszText = tt_text;
4663 // can't show tooltip if failed
4664 }
4665 else
4666 {
4667 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4668
4669 if (STRLEN(str) < sizeof(lpdi->szText)
4670 || ((tt_text = vim_strsave(str)) == NULL))
4671 vim_strncpy((char_u *)lpdi->szText, str,
4672 sizeof(lpdi->szText) - 1);
4673 else
4674 lpdi->lpszText = tt_text;
4675 }
4676 }
4677 break;
4678
4679# ifdef FEAT_GUI_TABLINE
4680 case TCN_SELCHANGE:
4681 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4682 {
4683 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4684 return 0L;
4685 }
4686 break;
4687
4688 case NM_RCLICK:
4689 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4690 {
4691 show_tabline_popup_menu();
4692 return 0L;
4693 }
4694 break;
4695# endif
4696
4697 default:
4698 break;
4699 }
4700 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4701}
4702#endif
4703
4704#if defined(MENUHINTS) && defined(FEAT_MENU)
4705 static LRESULT
4706_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4707{
4708 if (((UINT) HIWORD(wParam)
4709 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4710 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004711 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004712 {
4713 UINT idButton;
4714 vimmenu_T *pMenu;
4715 static int did_menu_tip = FALSE;
4716
4717 if (did_menu_tip)
4718 {
4719 msg_clr_cmdline();
4720 setcursor();
4721 out_flush();
4722 did_menu_tip = FALSE;
4723 }
4724
4725 idButton = (UINT)LOWORD(wParam);
4726 pMenu = gui_mswin_find_menu(root_menu, idButton);
4727 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4728 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4729 {
4730 ++msg_hist_off;
4731 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4732 --msg_hist_off;
4733 setcursor();
4734 out_flush();
4735 did_menu_tip = TRUE;
4736 }
4737 return 0L;
4738 }
4739 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4740}
4741#endif
4742
Ken Takata7086b3e2023-10-02 21:26:03 +02004743 static BOOL
4744_OnGetDpiScaledSize(HWND hwnd, UINT dpi, SIZE *size)
4745{
Ken Takatada5da652023-10-05 20:20:58 +02004746 int old_width, old_height;
4747 int new_width, new_height;
4748 LOGFONTW lf;
4749 HFONT font;
4750
Ken Takata7086b3e2023-10-02 21:26:03 +02004751 //TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
4752
4753 // Calculate new approximate size.
Ken Takatada5da652023-10-05 20:20:58 +02004754 GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
4755 GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
4756 lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
4757 font = CreateFontIndirectW(&lf);
4758 if (font)
4759 {
4760 GetFontSize((GuiFont)font, &new_width, &new_height); // New size
4761 DeleteFont(font);
4762 }
4763 else
4764 {
4765 new_width = old_width;
4766 new_height = old_height;
4767 }
4768 size->cx = size->cx * new_width / old_width;
4769 size->cy = size->cy * new_height / old_height;
Ken Takata7086b3e2023-10-02 21:26:03 +02004770 //TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
4771
Ken Takatada5da652023-10-05 20:20:58 +02004772 return TRUE;
Ken Takata7086b3e2023-10-02 21:26:03 +02004773}
4774
K.Takatac81e9bf2022-01-16 14:15:49 +00004775 static LRESULT
Ken Takata7086b3e2023-10-02 21:26:03 +02004776_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
K.Takatac81e9bf2022-01-16 14:15:49 +00004777{
4778 s_dpi = ydpi;
4779 s_in_dpichanged = TRUE;
4780 //TRACE("DPI: %d", ydpi);
4781
Ken Takata7086b3e2023-10-02 21:26:03 +02004782 s_suggested_rect = *rc;
4783 //TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
4784 // rc->right - rc->left, rc->bottom - rc->top);
4785
K.Takatac81e9bf2022-01-16 14:15:49 +00004786 update_scrollbar_size();
4787 update_toolbar_size();
4788 set_tabline_font();
4789
4790 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4791 gui_get_wide_font();
4792 gui_mswin_get_menu_height(FALSE);
4793#ifdef FEAT_MBYTE_IME
4794 im_set_position(gui.row, gui.col);
4795#endif
4796 InvalidateRect(hwnd, NULL, TRUE);
4797
4798 s_in_dpichanged = FALSE;
4799 return 0L;
4800}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802
4803 static LRESULT CALLBACK
4804_WndProc(
4805 HWND hwnd,
4806 UINT uMsg,
4807 WPARAM wParam,
4808 LPARAM lParam)
4809{
LemonBoya773d842022-05-10 20:54:46 +01004810 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4811 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 HandleMouseHide(uMsg, lParam);
4814
4815 s_uMsg = uMsg;
4816 s_wParam = wParam;
4817 s_lParam = lParam;
4818
4819 switch (uMsg)
4820 {
4821 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4822 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004823 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004825 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4827 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4828 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4829 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4830#ifdef FEAT_MENU
4831 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4832#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004833 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4834 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4836 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004837 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4838 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4840 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4841 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4842#ifdef FEAT_NETBEANS_INTG
4843 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4844#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004845#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004846 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4847 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004848#endif
K.Takata92000e22022-01-20 15:10:57 +00004849 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004850
Bram Moolenaar734a8672019-12-02 22:49:38 +01004851 case WM_QUERYENDSESSION: // System wants to go down.
4852 gui_shell_closed(); // Will exit when no changed buffers.
4853 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004856 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004859 return 0L;
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 break;
4862
4863 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004864 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4865 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004866 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return 0L;
4868
4869 case WM_SYSCHAR:
4870 /*
4871 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4872 * shortcut key, handle like a typed ALT key, otherwise call Windows
4873 * ALT key handling.
4874 */
4875#ifdef FEAT_MENU
4876 if ( !gui.menu_is_active
4877 || p_wak[0] == 'n'
4878 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4879 )
4880#endif
4881 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004882 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return 0L;
4884 }
4885#ifdef FEAT_MENU
4886 else
K.Takata4ac893f2022-01-20 12:44:28 +00004887 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
4889
4890 case WM_SYSKEYUP:
4891#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004892 // This used to be done only when menu is active: ALT key is used for
4893 // that. But that caused problems when menu is disabled and using
4894 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4895 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004896 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004898 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#endif
4900
K.Takata4f2417f2021-06-05 16:25:32 +02004901 case WM_EXITSIZEMOVE:
4902 destroy_sizing_tip();
4903 break;
4904
Bram Moolenaar734a8672019-12-02 22:49:38 +01004905 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004906 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004909 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004910 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004911 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
Bram Moolenaar734a8672019-12-02 22:49:38 +01004913 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004914 case WM_SETTINGCHANGE:
4915 return _OnSettingChange((UINT)wParam);
4916
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004917#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004919 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#endif
K.Takata92000e22022-01-20 15:10:57 +00004921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922#if defined(MENUHINTS) && defined(FEAT_MENU)
4923 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004924 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927#ifdef FEAT_MBYTE_IME
4928 case WM_IME_NOTIFY:
4929 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004930 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004931 return 1L;
4932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 case WM_IME_COMPOSITION:
4934 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004935 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004936 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#endif
Ken Takata7086b3e2023-10-02 21:26:03 +02004938 case WM_GETDPISCALEDSIZE:
4939 return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
K.Takatac81e9bf2022-01-16 14:15:49 +00004940 case WM_DPICHANGED:
4941 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4942 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004946 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#endif
K.Takata92000e22022-01-20 15:10:57 +00004949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951
K.Takata4ac893f2022-01-20 12:44:28 +00004952 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953}
4954
4955/*
4956 * End of call-back routines
4957 */
4958
Bram Moolenaar734a8672019-12-02 22:49:38 +01004959// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960HWND vim_parent_hwnd = NULL;
4961
4962 static BOOL CALLBACK
4963FindWindowTitle(HWND hwnd, LPARAM lParam)
4964{
4965 char buf[2048];
4966 char *title = (char *)lParam;
4967
4968 if (GetWindowText(hwnd, buf, sizeof(buf)))
4969 {
4970 if (strstr(buf, title) != NULL)
4971 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004972 // Found it. Store the window ref. and quit searching if MDI
4973 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004975 if (vim_parent_hwnd != NULL)
4976 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004979 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980}
4981
4982/*
4983 * Invoked for '-P "title"' argument: search for parent application to open
4984 * our window in.
4985 */
4986 void
4987gui_mch_set_parent(char *title)
4988{
4989 EnumWindows(FindWindowTitle, (LPARAM)title);
4990 if (vim_parent_hwnd == NULL)
4991 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004992 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 mch_exit(2);
4994 }
4995}
4996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004997#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 static void
4999ole_error(char *arg)
5000{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005001 char buf[IOSIZE];
5002
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005003# ifdef VIMDLL
5004 gui.in_use = mch_is_gui_executable();
5005# endif
5006
Bram Moolenaar734a8672019-12-02 22:49:38 +01005007 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005008 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005009 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005010 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005014#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5015 static char *
5016gvim_error(void)
5017{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005018 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005019
5020 if (starting)
5021 {
5022 mch_errmsg(msg);
5023 mch_errmsg("\n");
5024 mch_exit(2);
5025 }
5026 return msg;
5027}
5028
5029 char *
5030gui_mch_do_spawn(char_u *arg)
5031{
5032 int len;
5033# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5034 char_u *session = NULL;
5035 LPWSTR tofree1 = NULL;
5036# endif
5037 WCHAR name[MAX_PATH];
5038 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5039 STARTUPINFOW si = {sizeof(si)};
5040 PROCESS_INFORMATION pi;
5041
5042 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5043 goto error;
5044 p = wcsrchr(name, L'\\');
5045 if (p == NULL)
5046 goto error;
5047 // Replace the executable name from vim(d).exe to gvim(d).exe.
5048# ifdef DEBUG
5049 wcscpy(p + 1, L"gvimd.exe");
5050# else
5051 wcscpy(p + 1, L"gvim.exe");
5052# endif
5053
5054# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5055 if (starting)
5056# endif
5057 {
5058 // Pass the command line to the new process.
5059 p = GetCommandLineW();
5060 // Skip 1st argument.
5061 while (*p && *p != L' ' && *p != L'\t')
5062 {
5063 if (*p == L'"')
5064 {
5065 while (*p && *p != L'"')
5066 ++p;
5067 if (*p)
5068 ++p;
5069 }
5070 else
5071 ++p;
5072 }
5073 cmd = p;
5074 }
5075# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5076 else
5077 {
5078 // Create a session file and pass it to the new process.
5079 LPWSTR wsession;
5080 char_u *savebg;
5081 int ret;
5082
5083 session = vim_tempname('s', FALSE);
5084 if (session == NULL)
5085 goto error;
5086 savebg = p_bg;
5087 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5088 ret = write_session_file(session);
5089 vim_free(p_bg);
5090 p_bg = savebg;
5091 if (!ret)
5092 goto error;
5093 wsession = enc_to_utf16(session, NULL);
5094 if (wsession == NULL)
5095 goto error;
5096 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005097 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005098 if (cmd == NULL)
5099 {
5100 vim_free(wsession);
5101 goto error;
5102 }
5103 tofree1 = cmd;
5104 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5105 wsession, wsession);
5106 vim_free(wsession);
5107 }
5108# endif
5109
5110 // Check additional arguments to the `:gui` command.
5111 if (arg != NULL)
5112 {
5113 warg = enc_to_utf16(arg, NULL);
5114 if (warg == NULL)
5115 goto error;
5116 tofree2 = warg;
5117 }
5118 else
5119 warg = L"";
5120
5121 // Set up the new command line.
5122 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005123 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005124 if (newcmd == NULL)
5125 goto error;
5126 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5127
5128 // Spawn a new GUI process.
5129 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5130 NULL, NULL, &si, &pi))
5131 goto error;
5132 CloseHandle(pi.hProcess);
5133 CloseHandle(pi.hThread);
5134 mch_exit(0);
5135
5136error:
5137# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5138 if (session)
5139 mch_remove(session);
5140 vim_free(session);
5141 vim_free(tofree1);
5142# endif
5143 vim_free(newcmd);
5144 vim_free(tofree2);
5145 return gvim_error();
5146}
5147#endif
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149/*
5150 * Parse the GUI related command-line arguments. Any arguments used are
5151 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005152 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
5154 void
5155gui_mch_prepare(int *argc, char **argv)
5156{
5157 int silent = FALSE;
5158 int idx;
5159
Bram Moolenaar734a8672019-12-02 22:49:38 +01005160 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5162 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005163 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5165 && (argv[2][0] == '-' || argv[2][0] == '/'))
5166 {
5167 silent = TRUE;
5168 idx = 2;
5169 }
5170 else
5171 idx = 1;
5172
Bram Moolenaar734a8672019-12-02 22:49:38 +01005173 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (STRICMP(argv[idx] + 1, "register") == 0)
5175 {
5176#ifdef FEAT_OLE
5177 RegisterMe(silent);
5178 mch_exit(0);
5179#else
5180 if (!silent)
5181 ole_error("register");
5182 mch_exit(2);
5183#endif
5184 }
5185
Bram Moolenaar734a8672019-12-02 22:49:38 +01005186 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5188 {
5189#ifdef FEAT_OLE
5190 UnregisterMe(!silent);
5191 mch_exit(0);
5192#else
5193 if (!silent)
5194 ole_error("unregister");
5195 mch_exit(2);
5196#endif
5197 }
5198
Bram Moolenaar734a8672019-12-02 22:49:38 +01005199 // Ignore an -embedding argument. It is only relevant if the
5200 // application wants to treat the case when it is started manually
5201 // differently from the case where it is started via automation (and
5202 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5204 {
5205#ifdef FEAT_OLE
5206 *argc = 1;
5207#else
5208 ole_error("embedding");
5209 mch_exit(2);
5210#endif
5211 }
5212 }
5213
5214#ifdef FEAT_OLE
5215 {
5216 int bDoRestart = FALSE;
5217
5218 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005219 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (bDoRestart)
5221 mch_exit(0);
5222 }
5223#endif
5224
5225#ifdef FEAT_NETBEANS_INTG
5226 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005227 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 int arg;
5229
5230 for (arg = 1; arg < *argc; arg++)
5231 if (strncmp("-nb", argv[arg], 3) == 0)
5232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 netbeansArg = argv[arg];
5234 mch_memmove(&argv[arg], &argv[arg + 1],
5235 (--*argc - arg) * sizeof(char *));
5236 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005237 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
5240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241}
5242
K.Takatac81e9bf2022-01-16 14:15:49 +00005243 static void
5244load_dpi_func(void)
5245{
5246 HMODULE hUser32;
5247
5248 hUser32 = GetModuleHandle("user32.dll");
5249 if (hUser32 == NULL)
5250 goto fail;
5251
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005252 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5253 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5254 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005255 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005256 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5257 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005258
5259 if (pSetThreadDpiAwarenessContext != NULL)
5260 {
5261 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5262 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5263 if (oldctx != NULL)
5264 {
5265 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5266 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5267#ifdef DEBUG
5268 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5269 {
5270 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5271 }
5272#endif
5273 return;
5274 }
5275 }
5276
5277fail:
5278 // Disable PerMonitorV2 APIs.
Ken Takatad8cb1dd2024-01-12 18:09:43 +01005279 pGetDpiForSystem = vimGetDpiForSystem;
K.Takatac81e9bf2022-01-16 14:15:49 +00005280 pGetDpiForWindow = NULL;
5281 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5282 pSetThreadDpiAwarenessContext = NULL;
5283 pGetAwarenessFromDpiAwarenessContext = NULL;
5284}
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286/*
5287 * Initialise the GUI. Create all the windows, set up all the call-backs
5288 * etc.
5289 */
5290 int
5291gui_mch_init(void)
5292{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005294 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar734a8672019-12-02 22:49:38 +01005297 // Return here if the window was already opened (happens when
5298 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005300 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 /*
5303 * Load the tearoff bitmap
5304 */
5305#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005306 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#endif
5308
K.Takatac81e9bf2022-01-16 14:15:49 +00005309 load_dpi_func();
5310
5311 s_dpi = pGetDpiForSystem();
5312 update_scrollbar_size();
5313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005315 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#endif
5317 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005318#ifdef FEAT_TOOLBAR
5319 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
5322 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5323
Bram Moolenaar734a8672019-12-02 22:49:38 +01005324 // First try using the wide version, so that we can use any title.
5325 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005326 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005328 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 wndclassw.lpfnWndProc = _WndProc;
5330 wndclassw.cbClsExtra = 0;
5331 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005332 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5334 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5335 wndclassw.hbrBackground = s_brush;
5336 wndclassw.lpszMenuName = NULL;
5337 wndclassw.lpszClassName = szVimWndClassW;
5338
K.Takata4ac893f2022-01-20 12:44:28 +00005339 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (vim_parent_hwnd != NULL)
5344 {
5345#ifdef HAVE_TRY_EXCEPT
5346 __try
5347 {
5348#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005349 // Open inside the specified parent window.
5350 // TODO: last argument should point to a CLIENTCREATESTRUCT
5351 // structure.
5352 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005354 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005355 WS_OVERLAPPEDWINDOW | WS_CHILD
5356 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5358 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005359 100, // Any value will do
5360 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005362 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#ifdef HAVE_TRY_EXCEPT
5364 }
5365 __except(EXCEPTION_EXECUTE_HANDLER)
5366 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005367 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369#endif
5370 if (s_hwnd == NULL)
5371 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005372 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 mch_exit(2);
5374 }
5375 }
5376 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005377 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005378 // If the provided windowid is not valid reset it to zero, so that it
5379 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005380 if (IsWindow((HWND)win_socket_id) <= 0)
5381 win_socket_id = 0;
5382
Bram Moolenaar734a8672019-12-02 22:49:38 +01005383 // Create a window. If win_socket_id is not zero without border and
5384 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005385 s_hwnd = CreateWindowW(
5386 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005387 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5388 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005389 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5390 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005391 100, // Any value will do
5392 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005393 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005394 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005395 if (s_hwnd != NULL && win_socket_id != 0)
5396 {
5397 SetParent(s_hwnd, (HWND)win_socket_id);
5398 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5399 }
5400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402 if (s_hwnd == NULL)
5403 return FAIL;
5404
K.Takatac81e9bf2022-01-16 14:15:49 +00005405 if (pGetDpiForWindow != NULL)
5406 {
5407 s_dpi = pGetDpiForWindow(s_hwnd);
5408 update_scrollbar_size();
5409 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5410 }
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5413 dyn_imm_load();
5414#endif
5415
Bram Moolenaar734a8672019-12-02 22:49:38 +01005416 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005417 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005418 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005419 wndclassw.style = CS_OWNDC;
5420 wndclassw.lpfnWndProc = _TextAreaWndProc;
5421 wndclassw.cbClsExtra = 0;
5422 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005423 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005424 wndclassw.hIcon = NULL;
5425 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5426 wndclassw.hbrBackground = NULL;
5427 wndclassw.lpszMenuName = NULL;
5428 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005429
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005430 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 return FAIL;
5432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005434 s_textArea = CreateWindowExW(
5435 0,
5436 szTextAreaClassW, L"Vim text area",
5437 WS_CHILD | WS_VISIBLE, 0, 0,
5438 100, // Any value will do for now
5439 100, // Any value will do for now
5440 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005441 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (s_textArea == NULL)
5444 return FAIL;
5445
Bram Moolenaar20321902016-02-17 12:30:17 +01005446#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005447 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005448 {
5449 HANDLE hIcon = NULL;
5450
5451 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005452 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005453 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005454#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456#ifdef FEAT_MENU
5457 s_menuBar = CreateMenu();
5458#endif
5459 s_hdc = GetDC(s_textArea);
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
Bram Moolenaar734a8672019-12-02 22:49:38 +01005463 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005464 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar734a8672019-12-02 22:49:38 +01005466 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 gui_mch_def_colors();
5468
Bram Moolenaar734a8672019-12-02 22:49:38 +01005469 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5470 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 set_normal_colors();
5472
5473 /*
5474 * Check that none of the colors are the same as the background color.
5475 * Then store the current values as the defaults.
5476 */
5477 gui_check_colors();
5478 gui.def_norm_pixel = gui.norm_pixel;
5479 gui.def_back_pixel = gui.back_pixel;
5480
Bram Moolenaar734a8672019-12-02 22:49:38 +01005481 // Get the colors for the highlight groups (gui_check_colors() might have
5482 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 highlight_gui_started();
5484
5485 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005486 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005488 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
5490 /*
5491 * Set up for Intellimouse processing
5492 */
5493 init_mouse_wheel();
5494
5495 /*
5496 * compute a couple of metrics used for the dialogs
5497 */
5498 get_dialog_font_metrics();
5499#ifdef FEAT_TOOLBAR
5500 /*
5501 * Create the toolbar
5502 */
5503 initialise_toolbar();
5504#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005505#ifdef FEAT_GUI_TABLINE
5506 /*
5507 * Create the tabline
5508 */
5509 initialise_tabline();
5510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511#ifdef MSWIN_FIND_REPLACE
5512 /*
5513 * Initialise the dialog box stuff
5514 */
5515 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5516
Bram Moolenaar734a8672019-12-02 22:49:38 +01005517 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005519 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005521 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5523 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5524 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005527#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005528 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005529 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005530#endif
5531
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005532#ifdef FEAT_RENDER_OPTIONS
5533 if (p_rop)
5534 (void)gui_mch_set_rendering_options(p_rop);
5535#endif
5536
Bram Moolenaar748bf032005-02-02 23:04:36 +00005537theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005538 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005539 display_errors();
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 return OK;
5542}
5543
5544/*
5545 * Get the size of the screen, taking position on multiple monitors into
5546 * account (if supported).
5547 */
5548 static void
5549get_work_area(RECT *spi_rect)
5550{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005551 HMONITOR mon;
5552 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
Bram Moolenaar734a8672019-12-02 22:49:38 +01005554 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005555 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005556 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005558 moninfo.cbSize = sizeof(MONITORINFO);
5559 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005561 *spi_rect = moninfo.rcWork;
5562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005565 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5567}
5568
5569/*
5570 * Set the size of the window to the given width and height in pixels.
5571 */
5572 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005573gui_mch_set_shellsize(
5574 int width,
5575 int height,
5576 int min_width UNUSED,
5577 int min_height UNUSED,
5578 int base_width UNUSED,
5579 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005580 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005583 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar734a8672019-12-02 22:49:38 +01005586 // Try to keep window completely on screen.
5587 // Get position of the screen work area. This is the part that is not
5588 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 get_work_area(&workarea_rect);
5590
Bram Moolenaar734a8672019-12-02 22:49:38 +01005591 // Resizing a maximized window looks very strange, unzoom it first.
5592 // But don't do it when still starting up, it may have been requested in
5593 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005594 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005596
Ken Takata7086b3e2023-10-02 21:26:03 +02005597 if (s_in_dpichanged)
5598 // Use the suggested position when in WM_DPICHANGED.
5599 window_rect = s_suggested_rect;
5600 else
5601 // Use current position.
5602 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
Bram Moolenaar734a8672019-12-02 22:49:38 +01005604 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005605 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5606 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5607 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5608 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5609 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5610 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
Bram Moolenaar734a8672019-12-02 22:49:38 +01005612 // The following should take care of keeping Vim on the same monitor, no
5613 // matter if the secondary monitor is left or right of the primary
5614 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005615 window_rect.right = window_rect.left + win_width;
5616 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005617
Bram Moolenaar734a8672019-12-02 22:49:38 +01005618 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005619 // Don't adjust the position when in WM_DPICHANGED.
5620 if (!s_in_dpichanged)
5621 {
5622 if ((direction & RESIZE_HOR)
5623 && window_rect.right > workarea_rect.right)
5624 OffsetRect(&window_rect,
5625 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Ken Takata7086b3e2023-10-02 21:26:03 +02005627 if ((direction & RESIZE_HOR)
5628 && window_rect.left < workarea_rect.left)
5629 OffsetRect(&window_rect,
5630 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Ken Takata7086b3e2023-10-02 21:26:03 +02005632 if ((direction & RESIZE_VERT)
5633 && window_rect.bottom > workarea_rect.bottom)
5634 OffsetRect(&window_rect,
5635 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Ken Takata7086b3e2023-10-02 21:26:03 +02005637 if ((direction & RESIZE_VERT)
5638 && window_rect.top < workarea_rect.top)
5639 OffsetRect(&window_rect,
5640 0, workarea_rect.top - window_rect.top);
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005643 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5644 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
Ken Takata7086b3e2023-10-02 21:26:03 +02005646 //TRACE("New pos: %d,%d New size: %d,%d",
5647 // window_rect.left, window_rect.top, win_width, win_height);
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 SetActiveWindow(s_hwnd);
5650 SetFocus(s_hwnd);
5651
Bram Moolenaar734a8672019-12-02 22:49:38 +01005652 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654}
5655
5656
5657 void
5658gui_mch_set_scrollbar_thumb(
5659 scrollbar_T *sb,
5660 long val,
5661 long size,
5662 long max)
5663{
5664 SCROLLINFO info;
5665
5666 sb->scroll_shift = 0;
5667 while (max > 32767)
5668 {
5669 max = (max + 1) >> 1;
5670 val >>= 1;
5671 size >>= 1;
5672 ++sb->scroll_shift;
5673 }
5674
5675 if (sb->scroll_shift > 0)
5676 ++size;
5677
5678 info.cbSize = sizeof(info);
5679 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5680 info.nPos = val;
5681 info.nMin = 0;
5682 info.nMax = max;
5683 info.nPage = size;
5684 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5685}
5686
5687
5688/*
5689 * Set the current text font.
5690 */
5691 void
5692gui_mch_set_font(GuiFont font)
5693{
5694 gui.currFont = font;
5695}
5696
5697
5698/*
5699 * Set the current text foreground color.
5700 */
5701 void
5702gui_mch_set_fg_color(guicolor_T color)
5703{
5704 gui.currFgColor = color;
5705}
5706
5707/*
5708 * Set the current text background color.
5709 */
5710 void
5711gui_mch_set_bg_color(guicolor_T color)
5712{
5713 gui.currBgColor = color;
5714}
5715
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005716/*
5717 * Set the current text special color.
5718 */
5719 void
5720gui_mch_set_sp_color(guicolor_T color)
5721{
5722 gui.currSpColor = color;
5723}
5724
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005725#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726/*
5727 * Multi-byte handling, originally by Sung-Hoon Baek.
5728 * First static functions (no prototypes generated).
5729 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005730# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005731# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005732# endif
5733# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 * handle WM_IME_NOTIFY message
5737 */
5738 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005739_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 LRESULT lResult = 0;
5742 HIMC hImc;
5743
5744 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5745 return lResult;
5746 switch (dwCommand)
5747 {
5748 case IMN_SETOPENSTATUS:
5749 if (pImmGetOpenStatus(hImc))
5750 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005751 LOGFONTW lf = norm_logfont;
5752 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5753 // Work around when PerMonitorV2 is not enabled in the process level.
5754 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5755 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 im_set_position(gui.row, gui.col);
5757
Bram Moolenaar734a8672019-12-02 22:49:38 +01005758 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005759 State &= ~MODE_LANGMAP;
5760 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005762# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005763 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5765 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005766 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int old_row = gui.row;
5768 int old_col = gui.col;
5769
5770 // This must be called here before
5771 // status_redraw_curbuf(), otherwise the mode
5772 // message may appear in the wrong position.
5773 showmode();
5774 status_redraw_curbuf();
5775 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005776 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 gui.row = old_row;
5778 gui.col = old_col;
5779 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782 }
5783 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005784 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 lResult = 0;
5786 break;
5787 }
5788 pImmReleaseContext(hWnd, hImc);
5789 return lResult;
5790}
5791
5792 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005793_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 char_u *ret;
5796 int len;
5797
Bram Moolenaar734a8672019-12-02 22:49:38 +01005798 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 return 0;
5800
5801 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5802 if (ret != NULL)
5803 {
5804 add_to_input_buf_csi(ret, len);
5805 vim_free(ret);
5806 return 1;
5807 }
5808 return 0;
5809}
5810
5811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 * void GetResultStr()
5813 *
5814 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5815 * get complete composition string
5816 */
5817 static char_u *
5818GetResultStr(HWND hwnd, int GCS, int *lenp)
5819{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005820 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005821 LONG ret;
5822 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 char_u *convbuf = NULL;
5824
5825 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5826 return NULL;
5827
K.Takatab0b2b732022-01-19 12:59:21 +00005828 // Get the length of the composition string.
5829 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5830 if (ret <= 0)
5831 return NULL;
5832
5833 // Allocate the requested buffer plus space for the NUL character.
5834 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (buf == NULL)
5836 return NULL;
5837
K.Takatab0b2b732022-01-19 12:59:21 +00005838 // Reads in the composition string.
5839 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5840 *lenp = ret / sizeof(WCHAR);
5841
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005842 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 pImmReleaseContext(hwnd, hIMC);
5844 vim_free(buf);
5845 return convbuf;
5846}
5847#endif
5848
Bram Moolenaar734a8672019-12-02 22:49:38 +01005849// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005850#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852/*
5853 * set font to IM.
5854 */
5855 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005856im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 HIMC hImc;
5859
5860 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5861 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005862 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 pImmReleaseContext(s_hwnd, hImc);
5864 }
5865}
5866
5867/*
5868 * Notify cursor position to IM.
5869 */
5870 void
5871im_set_position(int row, int col)
5872{
5873 HIMC hImc;
5874
5875 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5876 {
5877 COMPOSITIONFORM cfs;
5878
5879 cfs.dwStyle = CFS_POINT;
5880 cfs.ptCurrentPos.x = FILL_X(col);
5881 cfs.ptCurrentPos.y = FILL_Y(row);
5882 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005883 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5884 {
5885 // Work around when PerMonitorV2 is not enabled in the process level.
5886 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5887 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 pImmSetCompositionWindow(hImc, &cfs);
5890
5891 pImmReleaseContext(s_hwnd, hImc);
5892 }
5893}
5894
5895/*
5896 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5897 */
5898 void
5899im_set_active(int active)
5900{
5901 HIMC hImc;
5902 static HIMC hImcOld = (HIMC)0;
5903
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005904# ifdef VIMDLL
5905 if (!gui.in_use && !gui.starting)
5906 {
5907 mbyte_im_set_active(active);
5908 return;
5909 }
5910# endif
5911
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005912 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
5913 return;
5914
5915 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005917 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005919 hImcOld = pImmGetContext(s_hwnd);
5920 if (hImcOld)
5921 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005923 active = FALSE;
5924 }
5925 else if (hImcOld != (HIMC)0)
5926 {
5927 pImmAssociateContext(s_hwnd, hImcOld);
5928 hImcOld = (HIMC)0;
5929 }
5930
5931 hImc = pImmGetContext(s_hwnd);
5932 if (!hImc)
5933 return;
5934
5935 /*
5936 * for Korean ime
5937 */
5938 HKL hKL = GetKeyboardLayout(0);
5939
5940 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5941 {
5942 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5943 static BOOL bSaved = FALSE;
5944
5945 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005947 // if we have a saved conversion status, restore it
5948 if (bSaved)
5949 pImmSetConversionStatus(hImc, dwConversionSaved,
5950 dwSentenceSaved);
5951 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005953 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005955 // save conversion status and disable korean
5956 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5957 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00005958 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005959 bSaved = TRUE;
5960 pImmSetConversionStatus(hImc,
5961 dwConversionSaved & ~(IME_CMODE_NATIVE
5962 | IME_CMODE_FULLSHAPE),
5963 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00005964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
5966 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005967
5968 pImmSetOpenStatus(hImc, active);
5969 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970}
5971
5972/*
5973 * Get IM status. When IM is on, return not 0. Else return 0.
5974 */
5975 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005976im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 int status = 0;
5979 HIMC hImc;
5980
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005981# ifdef VIMDLL
5982 if (!gui.in_use && !gui.starting)
5983 return mbyte_im_get_status();
5984# endif
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5987 {
5988 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5989 pImmReleaseContext(s_hwnd, hImc);
5990 }
5991 return status;
5992}
5993
Bram Moolenaar734a8672019-12-02 22:49:38 +01005994#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005997/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005998 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005999 */
6000 static void
6001latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6002{
6003 int c;
6004
Bram Moolenaarca003e12006-03-17 23:19:38 +00006005 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006006 {
6007 c = *text++;
6008 switch (c)
6009 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006010 case 0xa4: c = 0x20ac; break; // euro
6011 case 0xa6: c = 0x0160; break; // S hat
6012 case 0xa8: c = 0x0161; break; // S -hat
6013 case 0xb4: c = 0x017d; break; // Z hat
6014 case 0xb8: c = 0x017e; break; // Z -hat
6015 case 0xbc: c = 0x0152; break; // OE
6016 case 0xbd: c = 0x0153; break; // oe
6017 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006018 }
6019 *unicodebuf++ = c;
6020 }
6021}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
6023#ifdef FEAT_RIGHTLEFT
6024/*
6025 * What is this for? In the case where you are using Win98 or Win2K or later,
6026 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6027 * reverses the string sent to the TextOut... family. This sucks, because we
6028 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6029 * way to tell Windblows not to do this!
6030 *
6031 * The short of it is that this 'RevOut' only gets called if you are running
6032 * one of the new, "improved" MS OSes, and only if you are running in
6033 * 'rightleft' mode. It makes display take *slightly* longer, but not
6034 * noticeably so.
6035 */
6036 static void
K.Takata135e1522022-01-29 15:27:58 +00006037RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 int col,
6039 int row,
6040 UINT foptions,
6041 CONST RECT *pcliprect,
6042 LPCTSTR text,
6043 UINT len,
6044 CONST INT *padding)
6045{
6046 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006048 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006049 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006050 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051}
6052#endif
6053
Bram Moolenaar92467d32017-12-05 13:22:16 +01006054 static void
6055draw_line(
6056 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006057 int y1,
6058 int x2,
6059 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006060 COLORREF color)
6061{
6062#if defined(FEAT_DIRECTX)
6063 if (IS_ENABLE_DIRECTX())
6064 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6065 else
6066#endif
6067 {
6068 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6069 HPEN old_pen = SelectObject(s_hdc, hpen);
6070 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006071 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006072 LineTo(s_hdc, x2, y2);
6073 DeleteObject(SelectObject(s_hdc, old_pen));
6074 }
6075}
6076
6077 static void
6078set_pixel(
6079 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006080 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006081 COLORREF color)
6082{
6083#if defined(FEAT_DIRECTX)
6084 if (IS_ENABLE_DIRECTX())
6085 DWriteContext_SetPixel(s_dwc, x, y, color);
6086 else
6087#endif
6088 SetPixel(s_hdc, x, y, color);
6089}
6090
6091 static void
6092fill_rect(
6093 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006094 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006095 COLORREF color)
6096{
6097#if defined(FEAT_DIRECTX)
6098 if (IS_ENABLE_DIRECTX())
6099 DWriteContext_FillRect(s_dwc, rcp, color);
6100 else
6101#endif
6102 {
6103 HBRUSH hbr2;
6104
6105 if (hbr == NULL)
6106 hbr2 = CreateSolidBrush(color);
6107 else
6108 hbr2 = hbr;
6109 FillRect(s_hdc, rcp, hbr2);
6110 if (hbr == NULL)
6111 DeleteBrush(hbr2);
6112 }
6113}
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 void
6116gui_mch_draw_string(
6117 int row,
6118 int col,
6119 char_u *text,
6120 int len,
6121 int flags)
6122{
6123 static int *padding = NULL;
6124 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 const RECT *pcliprect = NULL;
6126 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 static WCHAR *unicodebuf = NULL;
6128 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006129 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int y;
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 /*
6134 * Italic and bold text seems to have an extra row of pixels at the bottom
6135 * (below where the bottom of the character should be). If we draw the
6136 * characters with a solid background, the top row of pixels in the
6137 * character below will be overwritten. We can fix this by filling in the
6138 * background ourselves, to the correct character proportions, and then
6139 * writing the character in transparent mode. Still have a problem when
6140 * the character is "_", which gets written on to the character below.
6141 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6142 * pixel in their slots, which fixes the problem with the bottom row of
6143 * pixels. We still need this code because otherwise the top row of pixels
6144 * becomes a problem. - webb.
6145 */
6146 static HBRUSH hbr_cache[2] = {NULL, NULL};
6147 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6148 static int brush_lru = 0;
6149 HBRUSH hbr;
6150 RECT rc;
6151
6152 if (!(flags & DRAW_TRANSP))
6153 {
6154 /*
6155 * Clear background first.
6156 * Note: FillRect() excludes right and bottom of rectangle.
6157 */
6158 rc.left = FILL_X(col);
6159 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (has_mbyte)
6161 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006162 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006163 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
6165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 rc.right = FILL_X(col + len);
6167 rc.bottom = FILL_Y(row + 1);
6168
Bram Moolenaar734a8672019-12-02 22:49:38 +01006169 // Cache the created brush, that saves a lot of time. We need two:
6170 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 if (gui.currBgColor == brush_color[0])
6172 {
6173 hbr = hbr_cache[0];
6174 brush_lru = 1;
6175 }
6176 else if (gui.currBgColor == brush_color[1])
6177 {
6178 hbr = hbr_cache[1];
6179 brush_lru = 0;
6180 }
6181 else
6182 {
6183 if (hbr_cache[brush_lru] != NULL)
6184 DeleteBrush(hbr_cache[brush_lru]);
6185 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6186 brush_color[brush_lru] = gui.currBgColor;
6187 hbr = hbr_cache[brush_lru];
6188 brush_lru = !brush_lru;
6189 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006190
Bram Moolenaar92467d32017-12-05 13:22:16 +01006191 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
6193 SetBkMode(s_hdc, TRANSPARENT);
6194
6195 /*
6196 * When drawing block cursor, prevent inverted character spilling
6197 * over character cell (can happen with bold/italic)
6198 */
6199 if (flags & DRAW_CURSOR)
6200 {
6201 pcliprect = &rc;
6202 foptions = ETO_CLIPPED;
6203 }
6204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 SetTextColor(s_hdc, gui.currFgColor);
6206 SelectFont(s_hdc, gui.currFont);
6207
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006208#ifdef FEAT_DIRECTX
6209 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006210 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006211#endif
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6214 {
K.Takata135e1522022-01-29 15:27:58 +00006215 int i;
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 vim_free(padding);
6218 pad_size = Columns;
6219
Bram Moolenaar734a8672019-12-02 22:49:38 +01006220 // Don't give an out-of-memory message here, it would call us
6221 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006222 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (padding != NULL)
6224 for (i = 0; i < pad_size; i++)
6225 padding[i] = gui.char_width;
6226 }
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 /*
6229 * We have to provide the padding argument because italic and bold versions
6230 * of fixed-width fonts are often one pixel or so wider than their normal
6231 * versions.
6232 * No check for DRAW_BOLD, Windows will have done it already.
6233 */
6234
Bram Moolenaar734a8672019-12-02 22:49:38 +01006235 // Check if there are any UTF-8 characters. If not, use normal text
6236 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (enc_utf8)
6238 for (n = 0; n < len; ++n)
6239 if (text[n] >= 0x80)
6240 break;
6241
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006242#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006243 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6244 // required that unicode drawing routine, currently. So this forces it
6245 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006246 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006247 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006248#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006249
Bram Moolenaar734a8672019-12-02 22:49:38 +01006250 // Check if the Unicode buffer exists and is big enough. Create it
6251 // with the same length as the multi-byte string, the number of wide
6252 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006253 if ((enc_utf8
6254 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6255 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 && (unicodebuf == NULL || len > unibuflen))
6257 {
6258 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006259 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006262 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
6264 unibuflen = len;
6265 }
6266
6267 if (enc_utf8 && n < len && unicodebuf != NULL)
6268 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006269 // Output UTF-8 characters. Composing characters should be
6270 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006271 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006272 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006273 int cells; // cell width of string up to composing char
6274 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006275 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006277 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006279 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006281 c = utf_ptr2char(text + i);
6282 if (c >= 0x10000)
6283 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006284 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006285 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6286 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006287 }
6288 else
6289 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006290 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006291 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006292
6293 if (utf_iscomposing(c))
6294 cw = 0;
6295 else
6296 {
6297 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006298 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006299 cw = 1;
6300 }
6301
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (unicodepdy != NULL)
6303 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006304 // Use unicodepdy to make characters fit as we expect, even
6305 // when the font uses different widths (e.g., bold character
6306 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006307 if (c >= 0x10000)
6308 {
6309 unicodepdy[wlen - 2] = cw * gui.char_width;
6310 unicodepdy[wlen - 1] = 0;
6311 }
6312 else
6313 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
6315 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006316 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006318#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006319 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006320 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006321 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006322 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006323 TEXT_X(col), TEXT_Y(row),
6324 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006325 gui.char_width, gui.currFgColor,
6326 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006327 }
6328 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006329#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006330 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6331 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006332 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006334 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006336 // If we want to display codepage data, and the current CP is not the
6337 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (unicodebuf != NULL)
6339 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006340 if (enc_latin9)
6341 latin9_to_ucs(text, len, unicodebuf);
6342 else
6343 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 MB_PRECOMPOSED,
6345 (char *)text, len,
6346 (LPWSTR)unicodebuf, unibuflen);
6347 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006348 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006349 // Use unicodepdy to make characters fit as we expect, even
6350 // when the font uses different widths (e.g., bold character
6351 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006352 if (unicodepdy != NULL)
6353 {
6354 int i;
6355 int cw;
6356
6357 for (i = 0; i < len; ++i)
6358 {
6359 cw = utf_char2cells(unicodebuf[i]);
6360 if (cw > 2)
6361 cw = 1;
6362 unicodepdy[i] = cw * gui.char_width;
6363 }
6364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006366 foptions, pcliprect, unicodebuf, len, unicodepdy);
6367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369 }
6370 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006373 // Windows will mess up RL text, so we have to draw it character by
6374 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006375 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6377 foptions, pcliprect, (char *)text, len, padding);
6378 else
6379#endif
6380 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6381 foptions, pcliprect, (char *)text, len, padding);
6382 }
6383
Bram Moolenaar734a8672019-12-02 22:49:38 +01006384 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (flags & DRAW_UNDERL)
6386 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006387 // When p_linespace is 0, overwrite the bottom row of pixels.
6388 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (p_linespace > 1)
6391 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006392 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006394
Bram Moolenaar734a8672019-12-02 22:49:38 +01006395 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006396 if (flags & DRAW_STRIKE)
6397 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006398 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006399 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006400 }
6401
Bram Moolenaar734a8672019-12-02 22:49:38 +01006402 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006403 if (flags & DRAW_UNDERC)
6404 {
6405 int x;
6406 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006407 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006408
6409 y = FILL_Y(row + 1) - 1;
6410 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6411 {
6412 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006413 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006414 }
6415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416}
6417
6418
6419/*
6420 * Output routines.
6421 */
6422
Bram Moolenaar734a8672019-12-02 22:49:38 +01006423/*
6424 * Flush any output to the screen
6425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 void
6427gui_mch_flush(void)
6428{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006429#if defined(FEAT_DIRECTX)
6430 if (IS_ENABLE_DIRECTX())
6431 DWriteContext_Flush(s_dwc);
6432#endif
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 GdiFlush();
6435}
6436
6437 static void
6438clear_rect(RECT *rcp)
6439{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006440 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442
6443
Bram Moolenaarc716c302006-01-21 22:12:51 +00006444 void
6445gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6446{
6447 RECT workarea_rect;
6448
6449 get_work_area(&workarea_rect);
6450
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006451 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006452 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6453 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006454
Bram Moolenaar734a8672019-12-02 22:49:38 +01006455 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6456 // the menubar for MSwin, we subtract it from the screen height, so that
6457 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006458 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006459 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6460 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6461 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6462 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006463}
6464
6465
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466#if defined(FEAT_MENU) || defined(PROTO)
6467/*
6468 * Add a sub menu to the menu bar.
6469 */
6470 void
6471gui_mch_add_menu(
6472 vimmenu_T *menu,
6473 int pos)
6474{
6475 vimmenu_T *parent = menu->parent;
6476
6477 menu->submenu_id = CreatePopupMenu();
6478 menu->id = s_menu_id++;
6479
6480 if (menu_is_menubar(menu->name))
6481 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006482 WCHAR *wn;
6483 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006485 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006486 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006487 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006489 infow.cbSize = sizeof(infow);
6490 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6491 | MIIM_SUBMENU;
6492 infow.dwItemData = (long_u)menu;
6493 infow.wID = menu->id;
6494 infow.fType = MFT_STRING;
6495 infow.dwTypeData = wn;
6496 infow.cch = (UINT)wcslen(wn);
6497 infow.hSubMenu = menu->submenu_id;
6498 InsertMenuItemW((parent == NULL)
6499 ? s_menuBar : parent->submenu_id,
6500 (UINT)pos, TRUE, &infow);
6501 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 }
6503
Bram Moolenaar734a8672019-12-02 22:49:38 +01006504 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (parent == NULL)
6506 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006507# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 else if (IsWindow(parent->tearoff_handle))
6509 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511}
6512
6513 void
6514gui_mch_show_popupmenu(vimmenu_T *menu)
6515{
6516 POINT mp;
6517
K.Takata45f9cfb2022-01-21 11:11:00 +00006518 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6520}
6521
6522 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006523gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 vimmenu_T *menu = gui_find_menu(path_name);
6526
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006527 if (menu == NULL)
6528 return;
6529
6530 POINT p;
6531
6532 // Find the position of the current cursor
6533 GetDCOrgEx(s_hdc, &p);
6534 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006536 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006538 gui_mch_getmouse(&mx, &my);
6539 p.x += mx;
6540 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006542 else if (curwin != NULL)
6543 {
6544 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6545 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6546 }
6547 msg_scroll = FALSE;
6548 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549}
6550
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006551# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552/*
6553 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6554 * create it as a pseudo-"tearoff menu".
6555 */
6556 void
6557gui_make_tearoff(char_u *path_name)
6558{
6559 vimmenu_T *menu = gui_find_menu(path_name);
6560
Bram Moolenaar734a8672019-12-02 22:49:38 +01006561 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (menu != NULL)
6563 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6564}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567/*
6568 * Add a menu item to a menu
6569 */
6570 void
6571gui_mch_add_menu_item(
6572 vimmenu_T *menu,
6573 int idx)
6574{
6575 vimmenu_T *parent = menu->parent;
6576
6577 menu->id = s_menu_id++;
6578 menu->submenu_id = NULL;
6579
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006580# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6582 {
6583 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6584 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6585 }
6586 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006587# endif
6588# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (menu_is_toolbar(parent->name))
6590 {
6591 TBBUTTON newtb;
6592
Bram Moolenaara80faa82020-04-12 19:37:17 +02006593 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 if (menu_is_separator(menu->name))
6595 {
6596 newtb.iBitmap = 0;
6597 newtb.fsStyle = TBSTYLE_SEP;
6598 }
6599 else
6600 {
6601 newtb.iBitmap = get_toolbar_bitmap(menu);
6602 newtb.fsStyle = TBSTYLE_BUTTON;
6603 }
6604 newtb.idCommand = menu->id;
6605 newtb.fsState = TBSTATE_ENABLED;
6606 newtb.iString = 0;
6607 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6608 (LPARAM)&newtb);
6609 menu->submenu_id = (HMENU)-1;
6610 }
6611 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006614 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006616 wn = enc_to_utf16(menu->name, NULL);
6617 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006619 InsertMenuW(parent->submenu_id, (UINT)idx,
6620 (menu_is_separator(menu->name)
6621 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6622 (UINT)menu->id, wn);
6623 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006625# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (IsWindow(parent->tearoff_handle))
6627 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630}
6631
6632/*
6633 * Destroy the machine specific menu widget.
6634 */
6635 void
6636gui_mch_destroy_menu(vimmenu_T *menu)
6637{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006638# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 /*
6640 * is this a toolbar button?
6641 */
6642 if (menu->submenu_id == (HMENU)-1)
6643 {
6644 int iButton;
6645
6646 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6647 (WPARAM)menu->id, 0);
6648 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6649 }
6650 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
6653 if (menu->parent != NULL
6654 && menu_is_popup(menu->parent->dname)
6655 && menu->parent->submenu_id != NULL)
6656 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6657 else
6658 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6659 if (menu->submenu_id != NULL)
6660 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006661# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (IsWindow(menu->tearoff_handle))
6663 DestroyWindow(menu->tearoff_handle);
6664 if (menu->parent != NULL
6665 && menu->parent->children != NULL
6666 && IsWindow(menu->parent->tearoff_handle))
6667 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006668 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 menu->modes = 0;
6670 rebuild_tearoff(menu->parent);
6671 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 }
6674}
6675
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006676# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 static void
6678rebuild_tearoff(vimmenu_T *menu)
6679{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006680 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 char_u tbuf[128];
6682 RECT trect;
6683 RECT rct;
6684 RECT roct;
6685 int x, y;
6686
6687 HWND thwnd = menu->tearoff_handle;
6688
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006689 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if (GetWindowRect(thwnd, &trect)
6691 && GetWindowRect(s_hwnd, &rct)
6692 && GetClientRect(s_hwnd, &roct))
6693 {
6694 x = trect.left - rct.left;
6695 y = (trect.top - rct.bottom + roct.bottom);
6696 }
6697 else
6698 {
6699 x = y = 0xffffL;
6700 }
6701 DestroyWindow(thwnd);
6702 if (menu->children != NULL)
6703 {
6704 gui_mch_tearoff(tbuf, menu, x, y);
6705 if (IsWindow(menu->tearoff_handle))
6706 (void) SetWindowPos(menu->tearoff_handle,
6707 NULL,
6708 (int)trect.left,
6709 (int)trect.top,
6710 0, 0,
6711 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6712 }
6713}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006714# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716/*
6717 * Make a menu either grey or not grey.
6718 */
6719 void
6720gui_mch_menu_grey(
6721 vimmenu_T *menu,
6722 int grey)
6723{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006724# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 /*
6726 * is this a toolbar button?
6727 */
6728 if (menu->submenu_id == (HMENU)-1)
6729 {
6730 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006731 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
6733 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006734# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006735 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6736 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006738# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6740 {
6741 WORD menuID;
6742 HWND menuHandle;
6743
6744 /*
6745 * A tearoff button has changed state.
6746 */
6747 if (menu->children == NULL)
6748 menuID = (WORD)(menu->id);
6749 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006750 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6752 if (menuHandle)
6753 EnableWindow(menuHandle, !grey);
6754
6755 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757}
6758
Bram Moolenaar734a8672019-12-02 22:49:38 +01006759#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
6761
Bram Moolenaar734a8672019-12-02 22:49:38 +01006762// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006765#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6768/*
6769 * stuff for dialogs
6770 */
6771
6772/*
6773 * The callback routine used by all the dialogs. Very simple. First,
6774 * acknowledges the INITDIALOG message so that Windows knows to do standard
6775 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6776 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6777 * number.
6778 */
6779 static LRESULT CALLBACK
6780dialog_callback(
6781 HWND hwnd,
6782 UINT message,
6783 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006784 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 if (message == WM_INITDIALOG)
6787 {
6788 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006789 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 (void)SetFocus(hwnd);
6791 if (dialog_default_button > IDCANCEL)
6792 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006793 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006794 // We don't have a default, set focus on another element of the
6795 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006796 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 return FALSE;
6798 }
6799
6800 if (message == WM_COMMAND)
6801 {
6802 int button = LOWORD(wParam);
6803
Bram Moolenaar734a8672019-12-02 22:49:38 +01006804 // Don't end the dialog if something was selected that was
6805 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (button >= DLG_NONBUTTON_CONTROL)
6807 return TRUE;
6808
Bram Moolenaar734a8672019-12-02 22:49:38 +01006809 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006811 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006812 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006813 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006814
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006815 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6816 p = utf16_to_enc(wp, NULL);
6817 vim_strncpy(s_textfield, p, IOSIZE);
6818 vim_free(p);
6819 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 /*
6823 * Need to check for IDOK because if the user just hits Return to
6824 * accept the default value, some reason this is what we get.
6825 */
6826 if (button == IDOK)
6827 {
6828 if (dialog_default_button > IDCANCEL)
6829 EndDialog(hwnd, dialog_default_button);
6830 }
6831 else
6832 EndDialog(hwnd, button - IDCANCEL);
6833 return TRUE;
6834 }
6835
6836 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6837 {
6838 EndDialog(hwnd, 0);
6839 return TRUE;
6840 }
6841 return FALSE;
6842}
6843
6844/*
6845 * Create a dialog dynamically from the parameter strings.
6846 * type = type of dialog (question, alert, etc.)
6847 * title = dialog title. may be NULL for default title.
6848 * message = text to display. Dialog sizes to accommodate it.
6849 * buttons = '\n' separated list of button captions, default first.
6850 * dfltbutton = number of default button.
6851 *
6852 * This routine returns 1 if the first button is pressed,
6853 * 2 for the second, etc.
6854 *
6855 * 0 indicates Esc was pressed.
6856 * -1 for unexpected error
6857 *
6858 * If stubbing out this fn, return 1.
6859 */
6860
Bram Moolenaar734a8672019-12-02 22:49:38 +01006861static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 "IDR_VIM",
6864 "IDR_VIM_ERROR",
6865 "IDR_VIM_ALERT",
6866 "IDR_VIM_INFO",
6867 "IDR_VIM_QUESTION"
6868};
6869
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 int
6871gui_mch_dialog(
6872 int type,
6873 char_u *title,
6874 char_u *message,
6875 char_u *buttons,
6876 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006877 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006878 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006881 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 int numButtons;
6883 int *buttonWidths, *buttonPositions;
6884 int buttonYpos;
6885 int nchar, i;
6886 DWORD lStyle;
6887 int dlgwidth = 0;
6888 int dlgheight;
6889 int editboxheight;
6890 int horizWidth = 0;
6891 int msgheight;
6892 char_u *pstart;
6893 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006894 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 char_u *tbuffer;
6896 RECT rect;
6897 HWND hwnd;
6898 HDC hdc;
6899 HFONT font, oldFont;
6900 TEXTMETRIC fontInfo;
6901 int fontHeight;
6902 int textWidth, minButtonWidth, messageWidth;
6903 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006904 int maxDialogHeight;
6905 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 int vertical;
6907 int dlgPaddingX;
6908 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006909# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006910 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006912# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006913 garray_T ga;
6914 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006915 int dlg_icon_width;
6916 int dlg_icon_height;
6917 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006919# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006920 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006921# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006922 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006923# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006924 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006925 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
Bram Moolenaar748bf032005-02-02 23:04:36 +00006928 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006929 {
6930 load_dpi_func();
6931 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006932 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006933 }
6934 else
6935 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937 if ((type < 0) || (type > VIM_LAST_TYPE))
6938 type = 0;
6939
Bram Moolenaar734a8672019-12-02 22:49:38 +01006940 // allocate some memory for dialog template
6941 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006942 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006943 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945 if (p == NULL)
6946 return -1;
6947
6948 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006949 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6951 * const.
6952 */
6953 tbuffer = vim_strsave(buttons);
6954 if (tbuffer == NULL)
6955 return -1;
6956
Bram Moolenaar734a8672019-12-02 22:49:38 +01006957 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar734a8672019-12-02 22:49:38 +01006959 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 numButtons = 1;
6961 for (i = 0; tbuffer[i] != '\0'; i++)
6962 {
6963 if (tbuffer[i] == DLG_BUTTON_SEP)
6964 numButtons++;
6965 }
6966 if (dfltbutton >= numButtons)
6967 dfltbutton = -1;
6968
Bram Moolenaar734a8672019-12-02 22:49:38 +01006969 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006970 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 if (buttonWidths == NULL)
6972 return -1;
6973
Bram Moolenaar734a8672019-12-02 22:49:38 +01006974 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006975 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (buttonPositions == NULL)
6977 return -1;
6978
6979 /*
6980 * Calculate how big the dialog must be.
6981 */
6982 hwnd = GetDesktopWindow();
6983 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006984# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6986 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006987 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 use_lfSysmenu = TRUE;
6989 }
6990 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006991# endif
K.Takatad1c58992022-01-23 12:31:57 +00006992 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6993 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6994
6995 oldFont = SelectFont(hdc, font);
6996 dlgPaddingX = DLG_PADDING_X;
6997 dlgPaddingY = DLG_PADDING_Y;
6998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 GetTextMetrics(hdc, &fontInfo);
7000 fontHeight = fontInfo.tmHeight;
7001
Bram Moolenaar734a8672019-12-02 22:49:38 +01007002 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007003 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
Bram Moolenaar734a8672019-12-02 22:49:38 +01007005 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007006 if (s_hwnd == NULL)
7007 {
7008 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
Bram Moolenaar734a8672019-12-02 22:49:38 +01007010 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007011 get_work_area(&workarea_rect);
7012 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007013 if (maxDialogWidth > adjust_by_system_dpi(600))
7014 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007015 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007016 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007017 }
7018 else
7019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007020 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007021 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007022 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007023 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7024 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7025 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7026 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007027
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007028 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007029 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7030 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7031 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7032 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7033 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007034 }
7035
Bram Moolenaar734a8672019-12-02 22:49:38 +01007036 // Set dlgwidth to width of message.
7037 // Copy the message into "ga", changing NL to CR-NL and inserting line
7038 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 pstart = message;
7040 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007041 msgheight = 0;
7042 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 do
7044 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007045 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007046
Bram Moolenaar734a8672019-12-02 22:49:38 +01007047 // Need to figure out where to break the string. The system does it
7048 // at a word boundary, which would mean we can't compute the number of
7049 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007050 textWidth = 0;
7051 last_white = NULL;
7052 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007053 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007054 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007055 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007056 && textWidth > maxDialogWidth * 3 / 4)
7057 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007058 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007059 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007060 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007061 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007062 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007063 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007064 textWidth = 0;
7065
7066 if (last_white != NULL)
7067 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007068 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007069 if (pend > last_white)
7070 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007071 pend = last_white + 1;
7072 last_white = NULL;
7073 }
7074 ga_append(&ga, '\r');
7075 ga_append(&ga, '\n');
7076 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007077 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007078
7079 while (--l >= 0)
7080 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007081 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007082 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007084
7085 ga_append(&ga, '\r');
7086 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 pstart = pend + 1;
7088 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007089
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007090 if (ga.ga_data != NULL)
7091 message = ga.ga_data;
7092
Bram Moolenaar734a8672019-12-02 22:49:38 +01007093 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007094
K.Takatac81e9bf2022-01-16 14:15:49 +00007095 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7096 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
K.Takatac81e9bf2022-01-16 14:15:49 +00007098 // Add width of icon to dlgwidth, and some space
7099 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7100 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7101
7102 if (msgheight < dlg_icon_height)
7103 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 /*
7106 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007107 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007109 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (!vertical)
7111 {
7112 // Place buttons horizontally if they fit.
7113 horizWidth = dlgPaddingX;
7114 pstart = tbuffer;
7115 i = 0;
7116 do
7117 {
7118 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7119 if (pend == NULL)
7120 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007121 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 if (textWidth < minButtonWidth)
7123 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007124 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 buttonWidths[i] = textWidth;
7126 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007127 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 pstart = pend + 1;
7129 } while (*pend != NUL);
7130
7131 if (horizWidth > maxDialogWidth)
7132 vertical = TRUE; // Too wide to fit on the screen.
7133 else if (horizWidth > dlgwidth)
7134 dlgwidth = horizWidth;
7135 }
7136
7137 if (vertical)
7138 {
7139 // Stack buttons vertically.
7140 pstart = tbuffer;
7141 do
7142 {
7143 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7144 if (pend == NULL)
7145 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007146 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007147 textWidth += dlgPaddingX; // Padding within button
7148 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (textWidth > dlgwidth)
7150 dlgwidth = textWidth;
7151 pstart = pend + 1;
7152 } while (*pend != NUL);
7153 }
7154
7155 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007156 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
Bram Moolenaar734a8672019-12-02 22:49:38 +01007158 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007159 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161 add_long(lStyle);
7162 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007163 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 add_word(0); // NumberOfItems(will change later)
7165 add_word(10); // x
7166 add_word(10); // y
7167 add_word(PixelToDialogX(dlgwidth)); // cx
7168
7169 // Dialog height.
7170 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007171 dlgheight = msgheight + 2 * dlgPaddingY
7172 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 else
7174 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7175
7176 // Dialog needs to be taller if contains an edit box.
7177 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7178 if (textfield != NULL)
7179 dlgheight += editboxheight;
7180
Bram Moolenaar734a8672019-12-02 22:49:38 +01007181 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007182 if (dlgheight > maxDialogHeight)
7183 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007184 msgheight = msgheight - (dlgheight - maxDialogHeight);
7185 dlgheight = maxDialogHeight;
7186 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007188 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007189 }
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 add_word(PixelToDialogY(dlgheight));
7192
7193 add_word(0); // Menu
7194 add_word(0); // Class
7195
Bram Moolenaar734a8672019-12-02 22:49:38 +01007196 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007197 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7198 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 p += nchar;
7200
K.Takatad1c58992022-01-23 12:31:57 +00007201 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007202# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007203 if (use_lfSysmenu)
7204 {
7205 // point size
7206 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7207 GetDeviceCaps(hdc, LOGPIXELSY));
7208 wcscpy(p, lfSysmenu.lfFaceName);
7209 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
K.Takatad1c58992022-01-23 12:31:57 +00007211 else
7212# endif
7213 {
7214 *p++ = DLG_FONT_POINT_SIZE; // point size
7215 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7216 }
7217 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219 buttonYpos = msgheight + 2 * dlgPaddingY;
7220
7221 if (textfield != NULL)
7222 buttonYpos += editboxheight;
7223
7224 pstart = tbuffer;
7225 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007226 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 for (i = 0; i < numButtons; i++)
7228 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007229 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 for ( pend = pstart;
7231 *pend && (*pend != DLG_BUTTON_SEP);
7232 pend++)
7233 ;
7234
7235 if (*pend)
7236 *pend = '\0';
7237
7238 /*
7239 * old NOTE:
7240 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7241 * the focus to the first tab-able button and in so doing makes that
7242 * the default!! Grrr. Workaround: Make the default button the only
7243 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7244 * he/she can use arrow keys.
7245 *
7246 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007247 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 * dialog. Also needed for when the textfield is the default control.
7249 * It appears to work now (perhaps not on Win95?).
7250 */
7251 if (vertical)
7252 {
7253 p = add_dialog_element(p,
7254 (i == dfltbutton
7255 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7256 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007257 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 + 2 * fontHeight * i),
7259 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7260 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007261 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263 else
7264 {
7265 p = add_dialog_element(p,
7266 (i == dfltbutton
7267 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7268 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007269 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 PixelToDialogX(buttonWidths[i]),
7271 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007272 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007274 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276 *pnumitems += numButtons;
7277
Bram Moolenaar734a8672019-12-02 22:49:38 +01007278 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 p = add_dialog_element(p, SS_ICON,
7280 PixelToDialogX(dlgPaddingX),
7281 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007282 PixelToDialogX(dlg_icon_width),
7283 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7285 dlg_icons[type]);
7286
Bram Moolenaar734a8672019-12-02 22:49:38 +01007287 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007288 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007289 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007290 PixelToDialogY(dlgPaddingY),
7291 (WORD)(PixelToDialogX(messageWidth) + 1),
7292 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007293 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaar734a8672019-12-02 22:49:38 +01007295 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 if (textfield != NULL)
7297 {
7298 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7299 PixelToDialogX(2 * dlgPaddingX),
7300 PixelToDialogY(2 * dlgPaddingY + msgheight),
7301 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7302 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007303 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 *pnumitems += 1;
7305 }
7306
7307 *pnumitems += 2;
7308
7309 SelectFont(hdc, oldFont);
7310 DeleteObject(font);
7311 ReleaseDC(hwnd, hdc);
7312
Bram Moolenaar734a8672019-12-02 22:49:38 +01007313 // Let the dialog_callback() function know which button to make default
7314 // If we have an edit box, make that the default. We also need to tell
7315 // dialog_callback() if this dialog contains an edit box or not. We do
7316 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (textfield != NULL)
7318 {
7319 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7320 s_textfield = textfield;
7321 }
7322 else
7323 {
7324 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7325 s_textfield = NULL;
7326 }
7327
Bram Moolenaar734a8672019-12-02 22:49:38 +01007328 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007330 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 (LPDLGTEMPLATE)pdlgtemplate,
7332 s_hwnd,
7333 (DLGPROC)dialog_callback);
7334
7335 LocalFree(LocalHandle(pdlgtemplate));
7336 vim_free(tbuffer);
7337 vim_free(buttonWidths);
7338 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007339 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 (void)SetFocus(s_hwnd);
7343
7344 return nchar;
7345}
7346
Bram Moolenaar734a8672019-12-02 22:49:38 +01007347#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349/*
7350 * Put a simple element (basic class) onto a dialog template in memory.
7351 * return a pointer to where the next item should be added.
7352 *
7353 * parameters:
7354 * lStyle = additional style flags
7355 * (be careful, NT3.51 & Win32s will ignore the new ones)
7356 * x,y = x & y positions IN DIALOG UNITS
7357 * w,h = width and height IN DIALOG UNITS
7358 * Id = ID used in messages
7359 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7360 * caption = usually text or resource name
7361 *
7362 * TODO: use the length information noted here to enable the dialog creation
7363 * routines to work out more exactly how much memory they need to alloc.
7364 */
7365 static PWORD
7366add_dialog_element(
7367 PWORD p,
7368 DWORD lStyle,
7369 WORD x,
7370 WORD y,
7371 WORD w,
7372 WORD h,
7373 WORD Id,
7374 WORD clss,
7375 const char *caption)
7376{
7377 int nchar;
7378
Bram Moolenaar734a8672019-12-02 22:49:38 +01007379 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7381 *p++ = LOWORD(lStyle);
7382 *p++ = HIWORD(lStyle);
7383 *p++ = 0; // LOWORD (lExtendedStyle)
7384 *p++ = 0; // HIWORD (lExtendedStyle)
7385 *p++ = x;
7386 *p++ = y;
7387 *p++ = w;
7388 *p++ = h;
7389 *p++ = Id; //9 or 10 words in all
7390
7391 *p++ = (WORD)0xffff;
7392 *p++ = clss; //2 more here
7393
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007394 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 p += nchar;
7396
7397 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7398
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007399 return p; // total = 15 + strlen(caption) words
7400 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401}
7402
7403
7404/*
7405 * Helper routine. Take an input pointer, return closest pointer that is
7406 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7407 */
7408 static LPWORD
7409lpwAlign(
7410 LPWORD lpIn)
7411{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007412 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007414 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 ul += 3;
7416 ul >>= 2;
7417 ul <<= 2;
7418 return (LPWORD)ul;
7419}
7420
7421/*
7422 * Helper routine. Takes second parameter as Ansi string, copies it to first
7423 * parameter as wide character (16-bits / char) string, and returns integer
7424 * number of wide characters (words) in string (including the trailing wide
7425 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007426 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7427 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 static int
7429nCopyAnsiToWideChar(
7430 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007431 LPSTR lpAnsiIn,
7432 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
7434 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007435 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 int i;
7437 WCHAR *wn;
7438
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007439 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007441 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007442 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (wn != NULL)
7444 {
7445 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007446 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 vim_free(wn);
7448 }
7449 }
7450 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007451 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 nChar = MultiByteToWideChar(
7453 enc_codepage > 0 ? enc_codepage : CP_ACP,
7454 MB_PRECOMPOSED,
7455 lpAnsiIn, len,
7456 lpWCStr, len);
7457 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007458 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 return nChar;
7462}
7463
7464
7465#ifdef FEAT_TEAROFF
7466/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007467 * Lookup menu handle from "menu_id".
7468 */
7469 static HMENU
7470tearoff_lookup_menuhandle(
7471 vimmenu_T *menu,
7472 WORD menu_id)
7473{
7474 for ( ; menu != NULL; menu = menu->next)
7475 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007476 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007477 continue;
7478 if (menu_is_separator(menu->dname))
7479 continue;
7480 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7481 return menu->submenu_id;
7482 }
7483 return NULL;
7484}
7485
7486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 * The callback function for all the modeless dialogs that make up the
7488 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7489 * thinking its menus have been clicked), and go away when closed.
7490 */
7491 static LRESULT CALLBACK
7492tearoff_callback(
7493 HWND hwnd,
7494 UINT message,
7495 WPARAM wParam,
7496 LPARAM lParam)
7497{
7498 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007499 {
7500 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar734a8672019-12-02 22:49:38 +01007504 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 HandleMouseHide(message, lParam);
7506
7507 if (message == WM_COMMAND)
7508 {
7509 if ((WORD)(LOWORD(wParam)) & 0x8000)
7510 {
7511 POINT mp;
7512 RECT rect;
7513
7514 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7515 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007516 vimmenu_T *menu;
7517
7518 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007520 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7522 (int)rect.right - 8,
7523 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007524 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 s_hwnd,
7526 NULL);
7527 /*
7528 * NOTE: The pop-up menu can eat the mouse up event.
7529 * We deal with this in normal.c.
7530 */
7531 }
7532 }
7533 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007534 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7536 /*
7537 * Give main window the focus back: this is so after
7538 * choosing a tearoff button you can start typing again
7539 * straight away.
7540 */
7541 (void)SetFocus(s_hwnd);
7542 return TRUE;
7543 }
7544 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7545 {
7546 DestroyWindow(hwnd);
7547 return TRUE;
7548 }
7549
Bram Moolenaar734a8672019-12-02 22:49:38 +01007550 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 if (message == WM_EXITSIZEMOVE)
7552 (void)SetActiveWindow(s_hwnd);
7553
7554 return FALSE;
7555}
7556#endif
7557
7558
7559/*
K.Takatad1c58992022-01-23 12:31:57 +00007560 * Computes the dialog base units based on the current dialog font.
7561 * We don't use the GetDialogBaseUnits() API, because we don't use the
7562 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 */
7564 static void
7565get_dialog_font_metrics(void)
7566{
7567 HDC hdc;
7568 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 SIZE size;
7570#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007571 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572#endif
7573
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007575 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007576 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578#endif
7579 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007580 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
K.Takatad1c58992022-01-23 12:31:57 +00007582 hdc = GetDC(s_hwnd);
7583 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007584 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007585 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
K.Takataabe628e2022-01-23 16:25:17 +00007587 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007588 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589}
7590
7591#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7592/*
7593 * Create a pseudo-"tearoff menu" based on the child
7594 * items of a given menu pointer.
7595 */
7596 static void
7597gui_mch_tearoff(
7598 char_u *title,
7599 vimmenu_T *menu,
7600 int initX,
7601 int initY)
7602{
7603 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7604 int template_len;
7605 int nchar, textWidth, submenuWidth;
7606 DWORD lStyle;
7607 DWORD lExtendedStyle;
7608 WORD dlgwidth;
7609 WORD menuID;
7610 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007611 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 vimmenu_T *the_menu = menu;
7613 HWND hwnd;
7614 HDC hdc;
7615 HFONT font, oldFont;
7616 int col, spaceWidth, len;
7617 int columnWidths[2];
7618 char_u *label, *text;
7619 int acLen = 0;
7620 int nameLen;
7621 int padding0, padding1, padding2 = 0;
7622 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007623 int x;
7624 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007625# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007626 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
7630 /*
7631 * If this menu is already torn off, move it to the mouse position.
7632 */
7633 if (IsWindow(menu->tearoff_handle))
7634 {
7635 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007636 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {
7638 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7639 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7640 }
7641 return;
7642 }
7643
7644 /*
7645 * Create a new tearoff.
7646 */
7647 if (*title == MNU_HIDDEN_CHAR)
7648 title++;
7649
Bram Moolenaar734a8672019-12-02 22:49:38 +01007650 // Allocate memory to store the dialog template. It's made bigger when
7651 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 template_len = DLG_ALLOC_SIZE;
7653 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7654 if (p == NULL)
7655 return;
7656
7657 hwnd = GetDesktopWindow();
7658 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007659# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7661 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007662 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 use_lfSysmenu = TRUE;
7664 }
7665 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007666# endif
K.Takatad1c58992022-01-23 12:31:57 +00007667 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7668 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7669
7670 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaar734a8672019-12-02 22:49:38 +01007672 // Calculate width of a single space. Used for padding columns to the
7673 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007674 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaar734a8672019-12-02 22:49:38 +01007676 // Figure out max width of the text column, the accelerator column and the
7677 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 submenuWidth = 0;
7679 for (col = 0; col < 2; col++)
7680 {
7681 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007682 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007684 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 text = (col == 0) ? pmenu->dname : pmenu->actext;
7686 if (text != NULL && *text != NUL)
7687 {
7688 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7689 if (textWidth > columnWidths[col])
7690 columnWidths[col] = textWidth;
7691 }
7692 if (pmenu->children != NULL)
7693 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7694 }
7695 }
7696 if (columnWidths[1] == 0)
7697 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007698 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (submenuWidth != 0)
7700 columnWidths[0] += submenuWidth;
7701 else
7702 columnWidths[0] += spaceWidth;
7703 }
7704 else
7705 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007706 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7708 columnWidths[1] += submenuWidth;
7709 }
7710
7711 /*
7712 * Now find the total width of our 'menu'.
7713 */
7714 textWidth = columnWidths[0] + columnWidths[1];
7715 if (submenuWidth != 0)
7716 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007717 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7719 textWidth += submenuWidth;
7720 }
7721 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7722 if (textWidth > dlgwidth)
7723 dlgwidth = textWidth;
7724 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7725
Bram Moolenaar734a8672019-12-02 22:49:38 +01007726 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007727 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7730 *p++ = LOWORD(lStyle);
7731 *p++ = HIWORD(lStyle);
7732 *p++ = LOWORD(lExtendedStyle);
7733 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007736 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007738 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 else
7740 *p++ = PixelToDialogX(initX); // x
7741 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007742 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 else
7744 *p++ = PixelToDialogY(initY); // y
7745 *p++ = PixelToDialogX(dlgwidth); // cx
7746 ptrueheight = p;
7747 *p++ = 0; // dialog height: changed later anyway
7748 *p++ = 0; // Menu
7749 *p++ = 0; // Class
7750
Bram Moolenaar734a8672019-12-02 22:49:38 +01007751 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007753 ? (LPSTR)title
7754 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 p += nchar;
7756
K.Takatad1c58992022-01-23 12:31:57 +00007757 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007758# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007759 if (use_lfSysmenu)
7760 {
7761 // point size
7762 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7763 GetDeviceCaps(hdc, LOGPIXELSY));
7764 wcscpy(p, lfSysmenu.lfFaceName);
7765 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
K.Takatad1c58992022-01-23 12:31:57 +00007767 else
7768# endif
7769 {
7770 *p++ = DLG_FONT_POINT_SIZE; // point size
7771 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7772 }
7773 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 /*
7776 * Loop over all the items in the menu.
7777 * But skip over the tearbar.
7778 */
7779 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7780 menu = menu->children->next;
7781 else
7782 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007783 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 for ( ; menu != NULL; menu = menu->next)
7785 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007786 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 continue;
7788 if (menu_is_separator(menu->dname))
7789 {
7790 sepPadding += 3;
7791 continue;
7792 }
7793
Bram Moolenaar734a8672019-12-02 22:49:38 +01007794 // Check if there still is plenty of room in the template. Make it
7795 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7797 {
7798 WORD *newp;
7799
7800 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7801 if (newp != NULL)
7802 {
7803 template_len += 4096;
7804 mch_memmove(newp, pdlgtemplate,
7805 (char *)p - (char *)pdlgtemplate);
7806 p = newp + (p - pdlgtemplate);
7807 pnumitems = newp + (pnumitems - pdlgtemplate);
7808 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7809 LocalFree(LocalHandle(pdlgtemplate));
7810 pdlgtemplate = newp;
7811 }
7812 }
7813
Bram Moolenaar734a8672019-12-02 22:49:38 +01007814 // Figure out minimal length of this menu label. Use "name" for the
7815 // actual text, "dname" for estimating the displayed size. "name"
7816 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 len = nameLen = (int)STRLEN(menu->name);
7818 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7819 (int)STRLEN(menu->dname))) / spaceWidth;
7820 len += padding0;
7821
7822 if (menu->actext != NULL)
7823 {
7824 acLen = (int)STRLEN(menu->actext);
7825 len += acLen;
7826 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7827 }
7828 else
7829 textWidth = 0;
7830 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7831 len += padding1;
7832
7833 if (menu->children == NULL)
7834 {
7835 padding2 = submenuWidth / spaceWidth;
7836 len += padding2;
7837 menuID = (WORD)(menu->id);
7838 }
7839 else
7840 {
7841 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007842 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844
Bram Moolenaar734a8672019-12-02 22:49:38 +01007845 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007846 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 if (label == NULL)
7848 break;
7849
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007850 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007851 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007853 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 while (padding0-- > 0)
7855 *text++ = ' ';
7856 if (menu->actext != NULL)
7857 {
7858 STRNCPY(text, menu->actext, acLen);
7859 text += acLen;
7860 }
7861 while (padding1-- > 0)
7862 *text++ = ' ';
7863 if (menu->children != NULL)
7864 {
7865 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7866 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7867 }
7868 else
7869 {
7870 while (padding2-- > 0)
7871 *text++ = ' ';
7872 }
7873 *text = NUL;
7874
7875 /*
7876 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7877 * W95/NT4 it makes the tear-off look more like a menu.
7878 */
7879 p = add_dialog_element(p,
7880 BS_PUSHBUTTON|BS_LEFT,
7881 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7882 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7883 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7884 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007885 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 vim_free(label);
7887 (*pnumitems)++;
7888 }
7889
7890 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7891
7892
Bram Moolenaar734a8672019-12-02 22:49:38 +01007893 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007894 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007895 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 (LPDLGTEMPLATE)pdlgtemplate,
7897 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007898 (DLGPROC)tearoff_callback,
7899 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
7901 LocalFree(LocalHandle(pdlgtemplate));
7902 SelectFont(hdc, oldFont);
7903 DeleteObject(font);
7904 ReleaseDC(hwnd, hdc);
7905
7906 /*
7907 * Reassert ourselves as the active window. This is so that after creating
7908 * a tearoff, the user doesn't have to click with the mouse just to start
7909 * typing again!
7910 */
7911 (void)SetActiveWindow(s_hwnd);
7912
Bram Moolenaar734a8672019-12-02 22:49:38 +01007913 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 force_menu_update = TRUE;
7915}
7916#endif
7917
7918#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007919# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921/*
7922 * Create the toolbar, initially unpopulated.
7923 * (just like the menu, there are no defaults, it's all
7924 * set up through menu.vim)
7925 */
7926 static void
7927initialise_toolbar(void)
7928{
7929 InitCommonControls();
7930 s_toolbarhwnd = CreateToolbarEx(
7931 s_hwnd,
7932 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7933 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007934 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007935 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 IDR_TOOLBAR1, // id of initial bitmap
7937 NULL,
7938 0, // initial number of buttons
7939 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7940 TOOLBAR_BUTTON_HEIGHT,
7941 TOOLBAR_BUTTON_WIDTH,
7942 TOOLBAR_BUTTON_HEIGHT,
7943 sizeof(TBBUTTON)
7944 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007945
7946 // Remove transparency from the toolbar to prevent the main window
7947 // background colour showing through
7948 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7949 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7950
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007951 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
7953 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007954
7955 update_toolbar_size();
7956}
7957
7958 static void
7959update_toolbar_size(void)
7960{
7961 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007962 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007963
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007964 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007965 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7966 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7967 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7968 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7969
7970 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7971 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7972 //TRACE("button size: %d, %d", w, h);
7973 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7974 gui.toolbar_height = h + 6;
7975
7976 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7977 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7978
7979 // TODO:
7980 // Currently, this function only updates the size of toolbar buttons.
7981 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007984 static LRESULT CALLBACK
7985toolbar_wndproc(
7986 HWND hwnd,
7987 UINT uMsg,
7988 WPARAM wParam,
7989 LPARAM lParam)
7990{
7991 HandleMouseHide(uMsg, lParam);
7992 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7993}
7994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 static int
7996get_toolbar_bitmap(vimmenu_T *menu)
7997{
7998 int i = -1;
7999
8000 /*
8001 * Check user bitmaps first, unless builtin is specified.
8002 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008003 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
8005 char_u fname[MAXPATHL];
8006 HANDLE hbitmap = NULL;
8007
8008 if (menu->iconfile != NULL)
8009 {
8010 gui_find_iconfile(menu->iconfile, fname, "bmp");
8011 hbitmap = LoadImage(
8012 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008013 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 IMAGE_BITMAP,
8015 TOOLBAR_BUTTON_WIDTH,
8016 TOOLBAR_BUTTON_HEIGHT,
8017 LR_LOADFROMFILE |
8018 LR_LOADMAP3DCOLORS
8019 );
8020 }
8021
8022 /*
8023 * If the LoadImage call failed, or the "icon=" file
8024 * didn't exist or wasn't specified, try the menu name
8025 */
8026 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008027 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008028# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008029 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008030# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008031 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 hbitmap = LoadImage(
8033 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008034 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 IMAGE_BITMAP,
8036 TOOLBAR_BUTTON_WIDTH,
8037 TOOLBAR_BUTTON_HEIGHT,
8038 LR_LOADFROMFILE |
8039 LR_LOADMAP3DCOLORS
8040 );
8041
8042 if (hbitmap != NULL)
8043 {
8044 TBADDBITMAP tbAddBitmap;
8045
8046 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008047 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
8049 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8050 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008051 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 }
8054 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8055 i = menu->iconidx;
8056
8057 return i;
8058}
8059#endif
8060
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008061#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8062 static void
8063initialise_tabline(void)
8064{
8065 InitCommonControls();
8066
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008067 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008068 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008069 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008070 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008071 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008072
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008073 gui.tabline_height = TABLINE_HEIGHT;
8074
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008075 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008076}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008077
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008078/*
8079 * Get tabpage_T from POINT.
8080 */
8081 static tabpage_T *
8082GetTabFromPoint(
8083 HWND hWnd,
8084 POINT pt)
8085{
8086 tabpage_T *ptp = NULL;
8087
8088 if (gui_mch_showing_tabline())
8089 {
8090 TCHITTESTINFO htinfo;
8091 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008092 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008093 if (s_tabhwnd == hWnd)
8094 {
8095 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8096 if (idx != -1)
8097 ptp = find_tabpage(idx + 1);
8098 }
8099 }
8100 return ptp;
8101}
8102
8103static POINT s_pt = {0, 0};
8104static HCURSOR s_hCursor = NULL;
8105
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008106 static LRESULT CALLBACK
8107tabline_wndproc(
8108 HWND hwnd,
8109 UINT uMsg,
8110 WPARAM wParam,
8111 LPARAM lParam)
8112{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008113 POINT pt;
8114 tabpage_T *tp;
8115 RECT rect;
8116 int nCenter;
8117 int idx0;
8118 int idx1;
8119
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008120 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008121
8122 switch (uMsg)
8123 {
8124 case WM_LBUTTONDOWN:
8125 {
8126 s_pt.x = GET_X_LPARAM(lParam);
8127 s_pt.y = GET_Y_LPARAM(lParam);
8128 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008129 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008130 break;
8131 }
8132 case WM_MOUSEMOVE:
8133 if (GetCapture() == hwnd
8134 && ((wParam & MK_LBUTTON)) != 0)
8135 {
8136 pt.x = GET_X_LPARAM(lParam);
8137 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008138 if (abs(pt.x - s_pt.x) >
8139 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008140 {
8141 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8142
8143 tp = GetTabFromPoint(hwnd, pt);
8144 if (tp != NULL)
8145 {
8146 idx0 = tabpage_index(curtab) - 1;
8147 idx1 = tabpage_index(tp) - 1;
8148
8149 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8150 nCenter = rect.left + (rect.right - rect.left) / 2;
8151
Bram Moolenaar734a8672019-12-02 22:49:38 +01008152 // Check if the mouse cursor goes over the center of
8153 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008154 if ((idx0 < idx1) && (nCenter < pt.x))
8155 {
8156 tabpage_move(idx1 + 1);
8157 update_screen(0);
8158 }
8159 else if ((idx1 < idx0) && (pt.x < nCenter))
8160 {
8161 tabpage_move(idx1);
8162 update_screen(0);
8163 }
8164 }
8165 }
8166 }
8167 break;
8168 case WM_LBUTTONUP:
8169 {
8170 if (GetCapture() == hwnd)
8171 {
8172 SetCursor(s_hCursor);
8173 ReleaseCapture();
8174 }
8175 break;
8176 }
regomne3bdef102022-09-26 20:48:32 +01008177 case WM_MBUTTONUP:
8178 {
8179 TCHITTESTINFO htinfo;
8180
8181 htinfo.pt.x = GET_X_LPARAM(lParam);
8182 htinfo.pt.y = GET_Y_LPARAM(lParam);
8183 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8184 if (idx0 != -1)
8185 {
8186 idx0 += 1;
8187 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8188 }
8189 break;
8190 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008191 default:
8192 break;
8193 }
8194
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008195 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8196}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008197#endif
8198
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8200/*
8201 * Make the GUI window come to the foreground.
8202 */
8203 void
8204gui_mch_set_foreground(void)
8205{
8206 if (IsIconic(s_hwnd))
8207 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8208 SetForegroundWindow(s_hwnd);
8209}
8210#endif
8211
8212#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8213 static void
8214dyn_imm_load(void)
8215{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008216 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (hLibImm == NULL)
8218 return;
8219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008221 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008223 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008225 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008227 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008229 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008231 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008232 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008233 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008234 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008235 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008237 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008239 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008240 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008241 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
K.Takatab0b2b732022-01-19 12:59:21 +00008243 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 || pImmGetContext == NULL
8245 || pImmAssociateContext == NULL
8246 || pImmReleaseContext == NULL
8247 || pImmGetOpenStatus == NULL
8248 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008249 || pImmGetCompositionFontW == NULL
8250 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008252 || pImmGetConversionStatus == NULL
8253 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
8255 FreeLibrary(hLibImm);
8256 hLibImm = NULL;
8257 pImmGetContext = NULL;
8258 return;
8259 }
8260
8261 return;
8262}
8263
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264#endif
8265
8266#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8267
8268# ifdef FEAT_XPM_W32
8269# define IMAGE_XPM 100
8270# endif
8271
8272typedef struct _signicon_t
8273{
8274 HANDLE hImage;
8275 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008276# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008277 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279} signicon_t;
8280
8281 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008282gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 signicon_t *sign;
8285 int x, y, w, h;
8286
8287 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8288 return;
8289
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008290# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008291 if (IS_ENABLE_DIRECTX())
8292 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008293# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008294
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 x = TEXT_X(col);
8296 y = TEXT_Y(row);
8297 w = gui.char_width * 2;
8298 h = gui.char_height;
8299 switch (sign->uType)
8300 {
8301 case IMAGE_BITMAP:
8302 {
8303 HDC hdcMem;
8304 HBITMAP hbmpOld;
8305
8306 hdcMem = CreateCompatibleDC(s_hdc);
8307 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8308 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8309 SelectObject(hdcMem, hbmpOld);
8310 DeleteDC(hdcMem);
8311 }
8312 break;
8313 case IMAGE_ICON:
8314 case IMAGE_CURSOR:
8315 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8316 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008317# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 case IMAGE_XPM:
8319 {
8320 HDC hdcMem;
8321 HBITMAP hbmpOld;
8322
8323 hdcMem = CreateCompatibleDC(s_hdc);
8324 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008325 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8327
8328 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008329 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8331 SelectObject(hdcMem, hbmpOld);
8332 DeleteDC(hdcMem);
8333 }
8334 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
8337}
8338
8339 static void
8340close_signicon_image(signicon_t *sign)
8341{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008342 if (sign == NULL)
8343 return;
8344
8345 switch (sign->uType)
8346 {
8347 case IMAGE_BITMAP:
8348 DeleteObject((HGDIOBJ)sign->hImage);
8349 break;
8350 case IMAGE_CURSOR:
8351 DestroyCursor((HCURSOR)sign->hImage);
8352 break;
8353 case IMAGE_ICON:
8354 DestroyIcon((HICON)sign->hImage);
8355 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008356# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008357 case IMAGE_XPM:
8358 DeleteObject((HBITMAP)sign->hImage);
8359 DeleteObject((HBITMAP)sign->hShape);
8360 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008361# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
8364
8365 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008366gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 signicon_t sign, *psign;
8369 char_u *ext;
8370
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008372 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 if (ext > signfile)
8374 {
8375 int do_load = 1;
8376
8377 if (!STRICMP(ext, ".bmp"))
8378 sign.uType = IMAGE_BITMAP;
8379 else if (!STRICMP(ext, ".ico"))
8380 sign.uType = IMAGE_ICON;
8381 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8382 sign.uType = IMAGE_CURSOR;
8383 else
8384 do_load = 0;
8385
8386 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008387 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 gui.char_width * 2, gui.char_height,
8389 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008390# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 if (!STRICMP(ext, ".xpm"))
8392 {
8393 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008394 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8395 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 }
8399
8400 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008401 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 *psign = sign;
8403
8404 if (!psign)
8405 {
8406 if (sign.hImage)
8407 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008408 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 }
8410 return (void *)psign;
8411
8412}
8413
8414 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008415gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008417 if (sign == NULL)
8418 return;
8419
8420 close_signicon_image((signicon_t *)sign);
8421 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008425#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
Bram Moolenaar734a8672019-12-02 22:49:38 +01008427/*
8428 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008429 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008431 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8433 * to get current mouse position).
8434 *
8435 * Trying to use as more Windows services as possible, and as less
8436 * IE version as possible :)).
8437 *
8438 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8439 * BalloonEval struct.
8440 * 2) Enable/Disable simply create/kill BalloonEval Timer
8441 * 3) When there was enough inactivity, timer procedure posts
8442 * async request to debugger
8443 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8444 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008445 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 */
8447
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008448 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008449make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008450{
K.Takata76687d22022-01-25 10:31:37 +00008451 TOOLINFOW *pti;
8452 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008453
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008454 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008455 if (pti == NULL)
8456 return;
8457
8458 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8459 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8460 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008461 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008462
8463 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8464 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8465
K.Takata76687d22022-01-25 10:31:37 +00008466 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008467 pti->uFlags = TTF_SUBCLASS;
8468 pti->hwnd = beval->target;
8469 pti->hinst = 0; // Don't use string resources
8470 pti->uId = ID_BEVAL_TOOLTIP;
8471
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008472 pti->lpszText = LPSTR_TEXTCALLBACKW;
8473 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8474 pti->lParam = (LPARAM)beval->tofree;
8475 // switch multiline tooltips on
8476 if (GetClientRect(s_textArea, &rect))
8477 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8478 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008479
8480 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8481 pti->rect.left = pt.x - 3;
8482 pti->rect.top = pt.y - 3;
8483 pti->rect.right = pt.x + 3;
8484 pti->rect.bottom = pt.y + 3;
8485
8486 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8487 // Make tooltip appear sooner.
8488 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8489 // I've performed some tests and it seems the longest possible life time
8490 // of tooltip is 30 seconds.
8491 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8492 /*
8493 * HACK: force tooltip to appear, because it'll not appear until
8494 * first mouse move. D*mn M$
8495 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8496 */
8497 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8498 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8499 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008500}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008501
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008503delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008505 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506}
8507
8508 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008509beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008510 HWND hwnd UNUSED,
8511 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008512 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008513 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 POINT pt;
8516 RECT rect;
8517
8518 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8519 return;
8520
8521 GetCursorPos(&pt);
8522 if (WindowFromPoint(pt) != s_textArea)
8523 return;
8524
8525 ScreenToClient(s_textArea, &pt);
8526 GetClientRect(s_textArea, &rect);
8527 if (!PtInRect(&rect, pt))
8528 return;
8529
K.Takataa8ec4912022-02-03 14:32:33 +00008530 if (last_user_activity > 0
8531 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 && (cur_beval->showState != ShS_PENDING
8533 || abs(cur_beval->x - pt.x) > 3
8534 || abs(cur_beval->y - pt.y) > 3))
8535 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008536 // Pointer resting in one place long enough, it's time to show
8537 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 cur_beval->showState = ShS_PENDING;
8539 cur_beval->x = pt.x;
8540 cur_beval->y = pt.y;
8541
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 if (cur_beval->msgCB != NULL)
8543 (*cur_beval->msgCB)(cur_beval, 0);
8544 }
8545}
8546
8547 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008548gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
K.Takataa8ec4912022-02-03 14:32:33 +00008550 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008554gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 if (beval == NULL)
8557 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008558 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8559 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561
8562 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008563gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008566
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008567 vim_free(beval->msg);
8568 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8569 if (beval->msg == NULL)
8570 {
8571 delete_tooltip(beval);
8572 beval->showState = ShS_NEUTRAL;
8573 return;
8574 }
8575
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (beval->showState == ShS_SHOWING)
8577 return;
8578 GetCursorPos(&pt);
8579 ScreenToClient(s_textArea, &pt);
8580
8581 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008583 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 gui_mch_disable_beval_area(cur_beval);
8585 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008586 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588}
8589
8590 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008591gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008592 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008593 char_u *mesg,
8594 void (*mesgCB)(BalloonEval *, int),
8595 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008597 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 BalloonEval *beval;
8599
8600 if (mesg != NULL && mesgCB != NULL)
8601 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008602 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 return NULL;
8604 }
8605
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008606 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 if (beval != NULL)
8608 {
8609 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
8611 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 beval->msg = mesg;
8613 beval->msgCB = mesgCB;
8614 beval->clientData = clientData;
8615
8616 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 cur_beval = beval;
8618
8619 if (p_beval)
8620 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 }
8622 return beval;
8623}
8624
8625 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008626Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008628 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 return;
8630
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008631 if (cur_beval == NULL)
8632 return;
8633
8634 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008636 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008637 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008638 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 delete_tooltip(cur_beval);
8640 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008643 break;
8644 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008645 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008646 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008647 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008648 info->lpszText = (LPSTR)info->lParam;
8649 info->uFlags |= TTF_DI_SETITEM;
8650 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008651 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008652 case TTN_GETDISPINFOW:
8653 {
8654 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008655 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008656 info->lpszText = (LPWSTR)info->lParam;
8657 info->uFlags |= TTF_DI_SETITEM;
8658 }
8659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 }
8661}
8662
8663 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008664track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
8666 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8667 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008668 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
8671 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008672gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008674# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008675 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008676# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008677 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 vim_free(beval);
8679}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008680#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681
8682#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8683/*
8684 * We have multiple signs to draw at the same location. Draw the
8685 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8686 */
8687 void
8688netbeans_draw_multisign_indicator(int row)
8689{
8690 int i;
8691 int y;
8692 int x;
8693
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008694 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008695 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 x = 0;
8698 y = TEXT_Y(row);
8699
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008700# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008701 if (IS_ENABLE_DIRECTX())
8702 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008703# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008704
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 for (i = 0; i < gui.char_height - 3; i++)
8706 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8707
8708 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8709 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8710 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8711 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8712 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8713 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8714 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8715}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008716#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008717
8718#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008719
Christopher Plewright20b795e2022-12-20 20:01:58 +00008720// TODO: at the moment, this is just a copy of test_gui_mouse_event.
8721// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008722// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00008723 static int
8724test_gui_w32_sendevent_mouse(dict_T *args)
8725{
8726 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008727 return FALSE;
8728
Christopher Plewright20b795e2022-12-20 20:01:58 +00008729 // Note: "move" is optional, requires fewer arguments
8730 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008731
Christopher Plewright20b795e2022-12-20 20:01:58 +00008732 if (!move && (!dict_has_key(args, "button")
8733 || !dict_has_key(args, "multiclick")
8734 || !dict_has_key(args, "modifiers")))
8735 return FALSE;
8736
8737 int row = (int)dict_get_number(args, "row");
8738 int col = (int)dict_get_number(args, "col");
8739
8740 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008741 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00008742 // the "move" argument expects row and col coordnates to be in pixels,
8743 // unless "cell" is specified and is TRUE.
8744 if (dict_get_bool(args, "cell", FALSE))
8745 {
8746 // calculate the middle of the character cell
8747 // Note: Cell coordinates are 1-based from vimscript
8748 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
8749 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
8750 gui_mouse_moved(pX, pY);
8751 }
8752 else
8753 gui_mouse_moved(col, row);
8754 }
8755 else
8756 {
8757 int button = (int)dict_get_number(args, "button");
8758 int repeated_click = (int)dict_get_number(args, "multiclick");
8759 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008760
Christopher Plewright20b795e2022-12-20 20:01:58 +00008761 // Reset the scroll values to known values.
8762 // XXX: Remove this when/if the scroll step is made configurable.
8763 mouse_set_hor_scroll_step(6);
8764 mouse_set_vert_scroll_step(3);
8765
8766 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
8767 repeated_click, mods);
8768 }
8769 return TRUE;
8770}
8771
8772 static int
8773test_gui_w32_sendevent_keyboard(dict_T *args)
8774{
8775 INPUT inputs[1];
8776 INPUT modkeys[3];
8777 SecureZeroMemory(inputs, sizeof(INPUT));
8778 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
8779
8780 char_u *event = dict_get_string(args, "event", TRUE);
8781
8782 if (event && (STRICMP(event, "keydown") == 0
8783 || STRICMP(event, "keyup") == 0))
8784 {
8785 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008786 if (vkCode <= 0 || vkCode >= 0xFF)
8787 {
8788 semsg(_(e_invalid_argument_nr), (long)vkCode);
8789 return FALSE;
8790 }
8791
Christopher Plewright20b795e2022-12-20 20:01:58 +00008792 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
8793 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
8794 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
8795 || vkCode == VK_LMENU || vkCode == VK_RMENU );
8796
8797 BOOL unwrapMods = FALSE;
8798 int mods = (int)dict_get_number(args, "modifiers");
8799
8800 // If there are modifiers in the args, and it is not a keyup event and
8801 // vkCode is not a modifier key, then we generate virtual modifier key
8802 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01008803 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008804 {
8805 int n = 0;
8806 if (mods & MOD_MASK_SHIFT)
8807 {
8808 modkeys[n].type = INPUT_KEYBOARD;
8809 modkeys[n].ki.wVk = VK_LSHIFT;
8810 n++;
8811 }
8812 if (mods & MOD_MASK_CTRL)
8813 {
8814 modkeys[n].type = INPUT_KEYBOARD;
8815 modkeys[n].ki.wVk = VK_LCONTROL;
8816 n++;
8817 }
8818 if (mods & MOD_MASK_ALT)
8819 {
8820 modkeys[n].type = INPUT_KEYBOARD;
8821 modkeys[n].ki.wVk = VK_LMENU;
8822 n++;
8823 }
8824 if (n)
8825 {
8826 (void)SetForegroundWindow(s_hwnd);
8827 SendInput(n, modkeys, sizeof(INPUT));
8828 }
8829 }
8830
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008831 inputs[0].type = INPUT_KEYBOARD;
8832 inputs[0].ki.wVk = vkCode;
8833 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008834 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008835 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01008836 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008837 unwrapMods = TRUE;
8838 }
8839
K.Takatafef38d82022-09-07 19:03:42 +01008840 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008841 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00008842 vim_free(event);
8843
8844 if (unwrapMods)
8845 {
8846 modkeys[0].type = INPUT_KEYBOARD;
8847 modkeys[0].ki.wVk = VK_LSHIFT;
8848 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
8849
8850 modkeys[1].type = INPUT_KEYBOARD;
8851 modkeys[1].ki.wVk = VK_LCONTROL;
8852 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
8853
8854 modkeys[2].type = INPUT_KEYBOARD;
8855 modkeys[2].ki.wVk = VK_LMENU;
8856 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
8857
8858 (void)SetForegroundWindow(s_hwnd);
8859 SendInput(3, modkeys, sizeof(INPUT));
8860 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008861 }
8862 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00008863 {
8864 if (event == NULL)
8865 {
8866 semsg(_(e_missing_argument_str), "event");
8867 }
8868 else
8869 {
8870 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8871 vim_free(event);
8872 }
8873 return FALSE;
8874 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008875 return TRUE;
8876}
Christopher Plewright20b795e2022-12-20 20:01:58 +00008877
8878 int
8879test_gui_w32_sendevent(char_u *event, dict_T *args)
8880{
8881 if (STRICMP(event, "key") == 0)
8882 return test_gui_w32_sendevent_keyboard(args);
8883 else if (STRICMP(event, "mouse") == 0)
8884 return test_gui_w32_sendevent_mouse(args);
8885 else
8886 {
8887 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8888 return FALSE;
8889 }
8890}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008891#endif