blob: ec6267178aecfc80ee2ceb1dd88c7e08d1a82e72 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000043#else
44# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010045#endif
46
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020047#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
48 int
49gui_mch_set_rendering_options(char_u *s)
50{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010051# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020052 char_u *p, *q;
53
54 int dx_enable = 0;
55 int dx_flags = 0;
56 float dx_gamma = 0.0f;
57 float dx_contrast = 0.0f;
58 float dx_level = 0.0f;
59 int dx_geom = 0;
60 int dx_renmode = 0;
61 int dx_taamode = 0;
62
Bram Moolenaar734a8672019-12-02 22:49:38 +010063 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100124 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100132
Bram Moolenaar734a8672019-12-02 22:49:38 +0100133 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100163# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183
Bram Moolenaar734a8672019-12-02 22:49:38 +0100184#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100185#ifdef DEBUG
186# include <tchar.h>
187#endif
188
Bram Moolenaar734a8672019-12-02 22:49:38 +0100189// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100190#ifndef PROTO
191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100192# ifndef __MINGW32__
193# include <shellapi.h>
194# endif
195# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
196# include <commctrl.h>
197# endif
198# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100199
Bram Moolenaar734a8672019-12-02 22:49:38 +0100200#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
202#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100203# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100204#endif
205
Bram Moolenaar734a8672019-12-02 22:49:38 +0100206// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100207#define DLG_PADDING_X 10
208#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100209#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100210#define DLG_VERT_PADDING_Y 4
211#define DLG_ICON_WIDTH 34
212#define DLG_ICON_HEIGHT 34
213#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000214#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100215#define DLG_FONT_POINT_SIZE 8
216#define DLG_MIN_MAX_WIDTH 400
217#define DLG_MIN_MAX_HEIGHT 400
218
Bram Moolenaar734a8672019-12-02 22:49:38 +0100219#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100220
K.Takatac81e9bf2022-01-16 14:15:49 +0000221#ifndef WM_DPICHANGED
222# define WM_DPICHANGED 0x02E0
223#endif
224
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100225#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100227 * Define a few things for generating prototypes. This is just to avoid
228 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100230# define APIENTRY
231# define CALLBACK
232# define CONST
233# define FAR
234# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200235# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200236# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237# define _cdecl
238typedef int BOOL;
239typedef int BYTE;
240typedef int DWORD;
241typedef int WCHAR;
242typedef int ENUMLOGFONT;
243typedef int FINDREPLACE;
244typedef int HANDLE;
245typedef int HBITMAP;
246typedef int HBRUSH;
247typedef int HDROP;
248typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100249typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100250typedef int LPARAM;
251typedef int LPCREATESTRUCT;
252typedef int LPCSTR;
253typedef int LPCTSTR;
254typedef int LPRECT;
255typedef int LPSTR;
256typedef int LPWINDOWPOS;
257typedef int LPWORD;
258typedef int LRESULT;
259typedef int HRESULT;
260# undef MSG
261typedef int MSG;
262typedef int NEWTEXTMETRIC;
263typedef int OSVERSIONINFO;
264typedef int PWORD;
265typedef int RECT;
266typedef int UINT;
267typedef int WORD;
268typedef int WPARAM;
269typedef int POINT;
270typedef void *HINSTANCE;
271typedef void *HMENU;
272typedef void *HWND;
273typedef void *HDC;
274typedef void VOID;
275typedef int LPNMHDR;
276typedef int LONG;
277typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200278typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200279typedef int COLORREF;
280typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100281#endif
282
K.Takata45f9cfb2022-01-21 11:11:00 +0000283static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100284static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100285static void clear_rect(RECT *rcp);
286
Bram Moolenaar734a8672019-12-02 22:49:38 +0100287static WORD s_dlgfntheight; // height of the dialog font
288static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289
290#ifdef FEAT_MENU
291static HMENU s_menuBar = NULL;
292#endif
293#ifdef FEAT_TEAROFF
294static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100295static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100296#endif
297
Bram Moolenaar734a8672019-12-02 22:49:38 +0100298// Flag that is set while processing a message that must not be interrupted by
299// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100300static int s_busy_processing = FALSE;
301
Bram Moolenaar734a8672019-12-02 22:49:38 +0100302static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000305static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200306static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200308static int s_findrep_is_find; // TRUE for find dialog, FALSE
309 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312HWND s_hwnd = NULL;
313static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100314static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef FEAT_TOOLBAR
317static HWND s_toolbarhwnd = NULL;
318static WNDPROC s_toolbar_wndproc = NULL;
319#endif
320
321#ifdef FEAT_GUI_TABLINE
322static HWND s_tabhwnd = NULL;
323static WNDPROC s_tabline_wndproc = NULL;
324static int showing_tabline = 0;
325#endif
326
327static WPARAM s_wParam = 0;
328static LPARAM s_lParam = 0;
329
330static HWND s_textArea = NULL;
331static UINT s_uMsg = 0;
332
Bram Moolenaar734a8672019-12-02 22:49:38 +0100333static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100334
335static int s_need_activate = FALSE;
336
Bram Moolenaar734a8672019-12-02 22:49:38 +0100337// This variable is set when waiting for an event, which is the only moment
338// scrollbar dragging can be done directly. It's not allowed while commands
339// are executed, because it may move the cursor and that may cause unexpected
340// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100341static int allow_scrollbar = FALSE;
342
K.Takatac81e9bf2022-01-16 14:15:49 +0000343#ifndef _DPI_AWARENESS_CONTEXTS_
344typedef HANDLE DPI_AWARENESS_CONTEXT;
345
346typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000347 DPI_AWARENESS_INVALID = -1,
348 DPI_AWARENESS_UNAWARE = 0,
349 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000350 DPI_AWARENESS_PER_MONITOR_AWARE = 2
351} DPI_AWARENESS;
352
K.Takatab0b2b732022-01-19 12:59:21 +0000353# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
354# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000355# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
356# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
357# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
358#endif
359
360#define DEFAULT_DPI 96
361static int s_dpi = DEFAULT_DPI;
362static BOOL s_in_dpichanged = FALSE;
363static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
364
365static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
366static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
367static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
368//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
369static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
370static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
371
372 static UINT WINAPI
373stubGetDpiForSystem(void)
374{
375 HWND hwnd = GetDesktopWindow();
376 HDC hdc = GetWindowDC(hwnd);
377 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
378 ReleaseDC(hwnd, hdc);
379 return dpi;
380}
381
382 static int WINAPI
383stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
384{
385 return GetSystemMetrics(nIndex);
386}
387
388 static int
389adjust_fontsize_by_dpi(int size)
390{
391 return size * s_dpi / (int)pGetDpiForSystem();
392}
393
394 static int
395adjust_by_system_dpi(int size)
396{
397 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
398}
399
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100400#if defined(FEAT_DIRECTX)
401 static int
402directx_enabled(void)
403{
404 if (s_dwc != NULL)
405 return 1;
406 else if (s_directx_load_attempted)
407 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100408 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100409 DWrite_Init();
410 s_directx_load_attempted = 1;
411 s_dwc = DWriteContext_Open();
412 directx_binddc();
413 return s_dwc != NULL ? 1 : 0;
414}
415
416 static void
417directx_binddc(void)
418{
419 if (s_textArea != NULL)
420 {
421 RECT rect;
422 GetClientRect(s_textArea, &rect);
423 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
424 }
425}
426#endif
427
Bram Moolenaar734a8672019-12-02 22:49:38 +0100428extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100429
430static struct
431{
432 UINT key_sym;
433 char_u vim_code0;
434 char_u vim_code1;
435} special_keys[] =
436{
437 {VK_UP, 'k', 'u'},
438 {VK_DOWN, 'k', 'd'},
439 {VK_LEFT, 'k', 'l'},
440 {VK_RIGHT, 'k', 'r'},
441
442 {VK_F1, 'k', '1'},
443 {VK_F2, 'k', '2'},
444 {VK_F3, 'k', '3'},
445 {VK_F4, 'k', '4'},
446 {VK_F5, 'k', '5'},
447 {VK_F6, 'k', '6'},
448 {VK_F7, 'k', '7'},
449 {VK_F8, 'k', '8'},
450 {VK_F9, 'k', '9'},
451 {VK_F10, 'k', ';'},
452
453 {VK_F11, 'F', '1'},
454 {VK_F12, 'F', '2'},
455 {VK_F13, 'F', '3'},
456 {VK_F14, 'F', '4'},
457 {VK_F15, 'F', '5'},
458 {VK_F16, 'F', '6'},
459 {VK_F17, 'F', '7'},
460 {VK_F18, 'F', '8'},
461 {VK_F19, 'F', '9'},
462 {VK_F20, 'F', 'A'},
463
464 {VK_F21, 'F', 'B'},
465#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100466 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100467#endif
468 {VK_F22, 'F', 'C'},
469 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471
472 {VK_HELP, '%', '1'},
473 {VK_BACK, 'k', 'b'},
474 {VK_INSERT, 'k', 'I'},
475 {VK_DELETE, 'k', 'D'},
476 {VK_HOME, 'k', 'h'},
477 {VK_END, '@', '7'},
478 {VK_PRIOR, 'k', 'P'},
479 {VK_NEXT, 'k', 'N'},
480 {VK_PRINT, '%', '9'},
481 {VK_ADD, 'K', '6'},
482 {VK_SUBTRACT, 'K', '7'},
483 {VK_DIVIDE, 'K', '8'},
484 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100485 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100486 {VK_DECIMAL, 'K', 'B'},
487
488 {VK_NUMPAD0, 'K', 'C'},
489 {VK_NUMPAD1, 'K', 'D'},
490 {VK_NUMPAD2, 'K', 'E'},
491 {VK_NUMPAD3, 'K', 'F'},
492 {VK_NUMPAD4, 'K', 'G'},
493 {VK_NUMPAD5, 'K', 'H'},
494 {VK_NUMPAD6, 'K', 'I'},
495 {VK_NUMPAD7, 'K', 'J'},
496 {VK_NUMPAD8, 'K', 'K'},
497 {VK_NUMPAD9, 'K', 'L'},
498
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_SPACE, ' ', NUL},
501 {VK_TAB, TAB, NUL},
502 {VK_ESCAPE, ESC, NUL},
503 {NL, NL, NUL},
504 {CAR, CAR, NUL},
505
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100507 {0, 0, 0}
508};
509
Bram Moolenaar734a8672019-12-02 22:49:38 +0100510// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511static int s_button_pending = -1;
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
514// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100515static int s_getting_focus = FALSE;
516
517static int s_x_pending;
518static int s_y_pending;
519static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000520static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200522static int dead_key = 0; // 0: no dead key, 1: dead key pressed
523static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
524 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100526#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100528static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000529static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100530#endif
531
532/*
533 * For control IME.
534 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100535 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100536 */
K.Takata4ac893f2022-01-20 12:44:28 +0000537#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100538// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100539static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100540// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100541static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542#endif
543
544#ifdef FEAT_MBYTE_IME
545static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
546#endif
547
548#if defined(FEAT_BROWSE)
549static char_u *convert_filter(char_u *s);
550#endif
551
552#ifdef DEBUG_PRINT_ERROR
553/*
554 * Print out the last Windows error message
555 */
556 static void
557print_windows_error(void)
558{
559 LPVOID lpMsgBuf;
560
561 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
562 NULL, GetLastError(),
563 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
564 (LPTSTR) &lpMsgBuf, 0, NULL);
565 TRACE1("Error: %s\n", lpMsgBuf);
566 LocalFree(lpMsgBuf);
567}
568#endif
569
570/*
571 * Cursor blink functions.
572 *
573 * This is a simple state machine:
574 * BLINK_NONE not blinking at all
575 * BLINK_OFF blinking, cursor is not shown
576 * BLINK_ON blinking, cursor is shown
577 */
578
579#define BLINK_NONE 0
580#define BLINK_OFF 1
581#define BLINK_ON 2
582
583static int blink_state = BLINK_NONE;
584static long_u blink_waittime = 700;
585static long_u blink_ontime = 400;
586static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000587static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100588
Bram Moolenaar703a8042016-06-04 16:24:32 +0200589 int
590gui_mch_is_blinking(void)
591{
592 return blink_state != BLINK_NONE;
593}
594
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200595 int
596gui_mch_is_blink_off(void)
597{
598 return blink_state == BLINK_OFF;
599}
600
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100601 void
602gui_mch_set_blinking(long wait, long on, long off)
603{
604 blink_waittime = wait;
605 blink_ontime = on;
606 blink_offtime = off;
607}
608
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609 static VOID CALLBACK
610_OnBlinkTimer(
611 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100612 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000613 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100614 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615{
616 MSG msg;
617
618 /*
619 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
620 */
621
622 KillTimer(NULL, idEvent);
623
Bram Moolenaar734a8672019-12-02 22:49:38 +0100624 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000625 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100626 ;
627
628 if (blink_state == BLINK_ON)
629 {
630 gui_undraw_cursor();
631 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000632 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633 }
634 else
635 {
636 gui_update_cursor(TRUE, FALSE);
637 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000638 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100640 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641}
642
643 static void
644gui_mswin_rm_blink_timer(void)
645{
646 MSG msg;
647
648 if (blink_timer != 0)
649 {
650 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100651 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000652 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 ;
654 blink_timer = 0;
655 }
656}
657
658/*
659 * Stop the cursor blinking. Show the cursor if it wasn't shown.
660 */
661 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100662gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100663{
664 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100665 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100666 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100667 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100668 gui_mch_flush();
669 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670 blink_state = BLINK_NONE;
671}
672
673/*
674 * Start the cursor blinking. If it was already blinking, this restarts the
675 * waiting time and shows the cursor.
676 */
677 void
678gui_mch_start_blink(void)
679{
680 gui_mswin_rm_blink_timer();
681
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
684 {
K.Takataa8ec4912022-02-03 14:32:33 +0000685 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 blink_state = BLINK_ON;
687 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100688 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100689 }
690}
691
692/*
693 * Call-back routines.
694 */
695
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100696 static VOID CALLBACK
697_OnTimer(
698 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100699 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000700 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100701 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702{
703 MSG msg;
704
705 /*
706 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
707 */
708 KillTimer(NULL, idEvent);
709 s_timed_out = TRUE;
710
Bram Moolenaar734a8672019-12-02 22:49:38 +0100711 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000712 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100713 ;
714 if (idEvent == s_wait_timer)
715 s_wait_timer = 0;
716}
717
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100718 static void
719_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100720 HWND hwnd UNUSED,
721 UINT ch UNUSED,
722 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100723{
724 dead_key = 1;
725}
726
727/*
728 * Convert Unicode character "ch" to bytes in "string[slen]".
729 * When "had_alt" is TRUE the ALT key was included in "ch".
730 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200731 * Because the Windows API uses UTF-16, we have to deal with surrogate
732 * pairs; this is where we choose to deal with them: if "ch" is a high
733 * surrogate, it will be stored, and the length returned will be zero; the next
734 * char_to_string call will then include the high surrogate, decoding the pair
735 * of UTF-16 code units to a single Unicode code point, presuming it is the
736 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100737 */
738 static int
739char_to_string(int ch, char_u *string, int slen, int had_alt)
740{
741 int len;
742 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100743 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200744 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100745
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200746 if (surrogate_pending_ch != 0)
747 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100748 // We don't guarantee ch is a low surrogate to match the high surrogate
749 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200750 wstring[0] = surrogate_pending_ch;
751 wstring[1] = ch;
752 surrogate_pending_ch = 0;
753 len = 2;
754 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100755 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200756 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100757 // We don't have the entire code point yet, only the first UTF-16 code
758 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200759 surrogate_pending_ch = ch;
760 return 0;
761 }
762 else
763 {
764 wstring[0] = ch;
765 len = 1;
766 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767
Bram Moolenaar734a8672019-12-02 22:49:38 +0100768 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
769 // "enc_codepage" is non-zero use the standard Win32 function,
770 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200771 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200773 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
774 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // If we had included the ALT key into the character but now the
776 // upper bit is no longer set, that probably means the conversion
777 // failed. Convert the original character and set the upper bit
778 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200779 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100780 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200781 wstring[0] = ch & 0x7f;
782 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
783 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100784 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100786 }
787 }
788 else
789 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790 ws = utf16_to_enc(wstring, &len);
791 if (ws == NULL)
792 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 else
794 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100795 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 len = slen;
797 mch_memmove(string, ws, len);
798 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 }
800 }
801
802 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 {
804 string[0] = ch;
805 len = 1;
806 }
807
808 for (i = 0; i < len; ++i)
809 if (string[i] == CSI && len <= slen - 2)
810 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100811 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
813 string[++i] = KS_EXTRA;
814 string[++i] = (int)KE_CSI;
815 len += 2;
816 }
817
818 return len;
819}
820
821/*
822 * Key hit, add it to the input buffer.
823 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 static void
825_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100826 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100828 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829{
830 char_u string[40];
831 int len = 0;
832
833 dead_key = 0;
834
835 len = char_to_string(ch, string, 40, FALSE);
836 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
837 {
838 trash_input_buf();
839 got_int = TRUE;
840 }
841
842 add_to_input_buf(string, len);
843}
844
845/*
846 * Alt-Key hit, add it to the input buffer.
847 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100848 static void
849_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100850 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100851 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100852 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100853{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100854 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100855 int len;
856 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100857 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100858
859 dead_key = 0;
860
Bram Moolenaar734a8672019-12-02 22:49:38 +0100861 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100862
Bram Moolenaar734a8672019-12-02 22:49:38 +0100863 // OK, we have a character key (given by ch) which was entered with the
864 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
865 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
866 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100867 modifiers = MOD_MASK_ALT;
868 if (GetKeyState(VK_SHIFT) & 0x8000)
869 modifiers |= MOD_MASK_SHIFT;
870 if (GetKeyState(VK_CONTROL) & 0x8000)
871 modifiers |= MOD_MASK_CTRL;
872
873 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100874 // remove the SHIFT modifier for keys where it's already included, e.g.,
875 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200876 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100877
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200878 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
879 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880 if (ch == CSI)
881 ch = K_CSI;
882
883 len = 0;
884 if (modifiers)
885 {
886 string[len++] = CSI;
887 string[len++] = KS_MODIFIER;
888 string[len++] = modifiers;
889 }
890
891 if (IS_SPECIAL((int)ch))
892 {
893 string[len++] = CSI;
894 string[len++] = K_SECOND((int)ch);
895 string[len++] = K_THIRD((int)ch);
896 }
897 else
898 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100899 // Although the documentation isn't clear about it, we assume "ch" is
900 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 len += char_to_string(ch, string + len, 40 - len, TRUE);
902 }
903
904 add_to_input_buf(string, len);
905}
906
907 static void
908_OnMouseEvent(
909 int button,
910 int x,
911 int y,
912 int repeated_click,
913 UINT keyFlags)
914{
915 int vim_modifiers = 0x0;
916
917 s_getting_focus = FALSE;
918
919 if (keyFlags & MK_SHIFT)
920 vim_modifiers |= MOUSE_SHIFT;
921 if (keyFlags & MK_CONTROL)
922 vim_modifiers |= MOUSE_CTRL;
923 if (GetKeyState(VK_MENU) & 0x8000)
924 vim_modifiers |= MOUSE_ALT;
925
926 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
927}
928
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100929 static void
930_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100931 HWND hwnd UNUSED,
932 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933 int x,
934 int y,
935 UINT keyFlags)
936{
937 static LONG s_prevTime = 0;
938
939 LONG currentTime = GetMessageTime();
940 int button = -1;
941 int repeated_click;
942
Bram Moolenaar734a8672019-12-02 22:49:38 +0100943 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944 (void)SetFocus(s_hwnd);
945
946 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
947 button = MOUSE_LEFT;
948 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
949 button = MOUSE_MIDDLE;
950 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
951 button = MOUSE_RIGHT;
952 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
953 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100954 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
955 }
956 else if (s_uMsg == WM_CAPTURECHANGED)
957 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100958 // on W95/NT4, somehow you get in here with an odd Msg
959 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100960 if (s_button_pending == MOUSE_LEFT)
961 button = MOUSE_RIGHT;
962 else
963 button = MOUSE_LEFT;
964 }
965 if (button >= 0)
966 {
967 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
968
969 /*
970 * Holding down the left and right buttons simulates pushing the middle
971 * button.
972 */
973 if (repeated_click
974 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
975 || (button == MOUSE_RIGHT
976 && s_button_pending == MOUSE_LEFT)))
977 {
978 /*
979 * Hmm, gui.c will ignore more than one button down at a time, so
980 * pretend we let go of it first.
981 */
982 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
983 button = MOUSE_MIDDLE;
984 repeated_click = FALSE;
985 s_button_pending = -1;
986 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
987 }
988 else if ((repeated_click)
989 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
990 {
991 if (s_button_pending > -1)
992 {
K.Takata45f9cfb2022-01-21 11:11:00 +0000993 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
994 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100995 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100996 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100997 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
998 }
999 else
1000 {
1001 /*
1002 * If this is the first press (i.e. not a multiple click) don't
1003 * action immediately, but store and wait for:
1004 * i) button-up
1005 * ii) mouse move
1006 * iii) another button press
1007 * before using it.
1008 * This enables us to make left+right simulate middle button,
1009 * without left or right being actioned first. The side-effect is
1010 * that if you click and hold the mouse without dragging, the
1011 * cursor doesn't move until you release the button. In practice
1012 * this is hardly a problem.
1013 */
1014 s_button_pending = button;
1015 s_x_pending = x;
1016 s_y_pending = y;
1017 s_kFlags_pending = keyFlags;
1018 }
1019
1020 s_prevTime = currentTime;
1021 }
1022}
1023
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001024 static void
1025_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001026 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001027 int x,
1028 int y,
1029 UINT keyFlags)
1030{
1031 int button;
1032
1033 s_getting_focus = FALSE;
1034 if (s_button_pending > -1)
1035 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001036 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001037 _OnMouseEvent(s_button_pending, s_x_pending,
1038 s_y_pending, FALSE, s_kFlags_pending);
1039 s_button_pending = -1;
1040 }
1041 if (s_uMsg == WM_MOUSEMOVE)
1042 {
1043 /*
1044 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1045 * down.
1046 */
1047 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1048 | MK_XBUTTON1 | MK_XBUTTON2)))
1049 {
1050 gui_mouse_moved(x, y);
1051 return;
1052 }
1053
1054 /*
1055 * While button is down, keep grabbing mouse move events when
1056 * the mouse goes outside the window
1057 */
1058 SetCapture(s_textArea);
1059 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001060 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001061 }
1062 else
1063 {
1064 ReleaseCapture();
1065 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001066 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001067 }
1068
1069 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1070}
1071
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001072 static void
1073_OnSizeTextArea(
1074 HWND hwnd UNUSED,
1075 UINT state UNUSED,
1076 int cx UNUSED,
1077 int cy UNUSED)
1078{
1079#if defined(FEAT_DIRECTX)
1080 if (IS_ENABLE_DIRECTX())
1081 directx_binddc();
1082#endif
1083}
1084
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001085#ifdef FEAT_MENU
1086/*
1087 * Find the vimmenu_T with the given id
1088 */
1089 static vimmenu_T *
1090gui_mswin_find_menu(
1091 vimmenu_T *pMenu,
1092 int id)
1093{
1094 vimmenu_T *pChildMenu;
1095
1096 while (pMenu)
1097 {
1098 if (pMenu->id == (UINT)id)
1099 break;
1100 if (pMenu->children != NULL)
1101 {
1102 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1103 if (pChildMenu)
1104 {
1105 pMenu = pChildMenu;
1106 break;
1107 }
1108 }
1109 pMenu = pMenu->next;
1110 }
1111 return pMenu;
1112}
1113
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 static void
1115_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001116 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001117 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001118 HWND hwndCtl UNUSED,
1119 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120{
1121 vimmenu_T *pMenu;
1122
1123 pMenu = gui_mswin_find_menu(root_menu, id);
1124 if (pMenu)
1125 gui_menu_cb(pMenu);
1126}
1127#endif
1128
1129#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001130/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001131 * Handle a Find/Replace window message.
1132 */
1133 static void
1134_OnFindRepl(void)
1135{
1136 int flags = 0;
1137 int down;
1138
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001139 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001140 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001141 (void)SetFocus(s_hwnd);
1142
1143 if (s_findrep_struct.Flags & FR_FINDNEXT)
1144 {
1145 flags = FRD_FINDNEXT;
1146
Bram Moolenaar734a8672019-12-02 22:49:38 +01001147 // Give main window the focus back: this is so the cursor isn't
1148 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001149 (void)SetFocus(s_hwnd);
1150 }
1151 else if (s_findrep_struct.Flags & FR_REPLACE)
1152 {
1153 flags = FRD_REPLACE;
1154
Bram Moolenaar734a8672019-12-02 22:49:38 +01001155 // Give main window the focus back: this is so the cursor isn't
1156 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 (void)SetFocus(s_hwnd);
1158 }
1159 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1160 {
1161 flags = FRD_REPLACEALL;
1162 }
1163
1164 if (flags != 0)
1165 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001166 char_u *p, *q;
1167
Bram Moolenaar734a8672019-12-02 22:49:38 +01001168 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001169 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1170 flags |= FRD_WHOLE_WORD;
1171 if (s_findrep_struct.Flags & FR_MATCHCASE)
1172 flags |= FRD_MATCH_CASE;
1173 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001174 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1175 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1176 if (p != NULL && q != NULL)
1177 gui_do_findrepl(flags, p, q, down);
1178 vim_free(p);
1179 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001180 }
1181}
1182#endif
1183
1184 static void
1185HandleMouseHide(UINT uMsg, LPARAM lParam)
1186{
1187 static LPARAM last_lParam = 0L;
1188
Bram Moolenaar734a8672019-12-02 22:49:38 +01001189 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1191 {
1192 if (lParam == last_lParam)
1193 return;
1194 last_lParam = lParam;
1195 }
1196
Bram Moolenaar734a8672019-12-02 22:49:38 +01001197 // Handle specially, to centralise coding. We need to be sure we catch all
1198 // possible events which should cause us to restore the cursor (as it is a
1199 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 switch (uMsg)
1201 {
1202 case WM_KEYUP:
1203 case WM_CHAR:
1204 /*
1205 * blank out the pointer if necessary
1206 */
1207 if (p_mh)
1208 gui_mch_mousehide(TRUE);
1209 break;
1210
Bram Moolenaar734a8672019-12-02 22:49:38 +01001211 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001212 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001213 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001214 case WM_LBUTTONDOWN:
1215 case WM_LBUTTONUP:
1216 case WM_MBUTTONDOWN:
1217 case WM_MBUTTONUP:
1218 case WM_RBUTTONDOWN:
1219 case WM_RBUTTONUP:
1220 case WM_XBUTTONDOWN:
1221 case WM_XBUTTONUP:
1222 case WM_NCMOUSEMOVE:
1223 case WM_NCLBUTTONDOWN:
1224 case WM_NCLBUTTONUP:
1225 case WM_NCMBUTTONDOWN:
1226 case WM_NCMBUTTONUP:
1227 case WM_NCRBUTTONDOWN:
1228 case WM_NCRBUTTONUP:
1229 case WM_KILLFOCUS:
1230 /*
1231 * if the pointer is currently hidden, then we should show it.
1232 */
1233 gui_mch_mousehide(FALSE);
1234 break;
1235 }
1236}
1237
1238 static LRESULT CALLBACK
1239_TextAreaWndProc(
1240 HWND hwnd,
1241 UINT uMsg,
1242 WPARAM wParam,
1243 LPARAM lParam)
1244{
1245 /*
1246 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1247 hwnd, uMsg, wParam, lParam);
1248 */
1249
1250 HandleMouseHide(uMsg, lParam);
1251
1252 s_uMsg = uMsg;
1253 s_wParam = wParam;
1254 s_lParam = lParam;
1255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001257 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001258#endif
1259
1260 switch (uMsg)
1261 {
1262 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1263 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1264 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1265 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1266 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1267 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1268 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1269 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1270 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1271 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1272 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1273 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1274 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1275 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001276 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001277
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001278#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001279 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1280 return TRUE;
1281#endif
1282 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001283 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001284 }
1285}
1286
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001287/*
1288 * Called when the foreground or background color has been changed.
1289 */
1290 void
1291gui_mch_new_colors(void)
1292{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001293 HBRUSH prevBrush;
1294
1295 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001296 prevBrush = (HBRUSH)SetClassLongPtr(
1297 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001298 InvalidateRect(s_hwnd, NULL, TRUE);
1299 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001300}
1301
1302/*
1303 * Set the colors to their default values.
1304 */
1305 void
1306gui_mch_def_colors(void)
1307{
1308 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1309 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1310 gui.def_norm_pixel = gui.norm_pixel;
1311 gui.def_back_pixel = gui.back_pixel;
1312}
1313
1314/*
1315 * Open the GUI window which was created by a call to gui_mch_init().
1316 */
1317 int
1318gui_mch_open(void)
1319{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001320 // Actually open the window, if not already visible
1321 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001322 if (!IsWindowVisible(s_hwnd))
1323 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1324
1325#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001326 // Init replace string here, so that we keep it when re-opening the
1327 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001328 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1329#endif
1330
1331 return OK;
1332}
1333
1334/*
1335 * Get the position of the top left corner of the window.
1336 */
1337 int
1338gui_mch_get_winpos(int *x, int *y)
1339{
1340 RECT rect;
1341
1342 GetWindowRect(s_hwnd, &rect);
1343 *x = rect.left;
1344 *y = rect.top;
1345 return OK;
1346}
1347
1348/*
1349 * Set the position of the top left corner of the window to the given
1350 * coordinates.
1351 */
1352 void
1353gui_mch_set_winpos(int x, int y)
1354{
1355 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1356 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1357}
1358 void
1359gui_mch_set_text_area_pos(int x, int y, int w, int h)
1360{
1361 static int oldx = 0;
1362 static int oldy = 0;
1363
1364 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1365
1366#ifdef FEAT_TOOLBAR
1367 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1368 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001369 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001370#endif
1371#if defined(FEAT_GUI_TABLINE)
1372 if (showing_tabline)
1373 {
1374 int top = 0;
1375 RECT rect;
1376
1377# ifdef FEAT_TOOLBAR
1378 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001379 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001380# endif
1381 GetClientRect(s_hwnd, &rect);
1382 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1383 }
1384#endif
1385
Bram Moolenaar734a8672019-12-02 22:49:38 +01001386 // When side scroll bar is unshown, the size of window will change.
1387 // then, the text area move left or right. thus client rect should be
1388 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001389 if (oldx != x || oldy != y)
1390 {
1391 InvalidateRect(s_hwnd, NULL, FALSE);
1392 oldx = x;
1393 oldy = y;
1394 }
1395}
1396
1397
1398/*
1399 * Scrollbar stuff:
1400 */
1401
1402 void
1403gui_mch_enable_scrollbar(
1404 scrollbar_T *sb,
1405 int flag)
1406{
1407 ShowScrollBar(sb->id, SB_CTL, flag);
1408
Bram Moolenaar734a8672019-12-02 22:49:38 +01001409 // TODO: When the window is maximized, the size of the window stays the
1410 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1411 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001412}
1413
1414 void
1415gui_mch_set_scrollbar_pos(
1416 scrollbar_T *sb,
1417 int x,
1418 int y,
1419 int w,
1420 int h)
1421{
1422 SetWindowPos(sb->id, NULL, x, y, w, h,
1423 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1424}
1425
Bram Moolenaar203ec772020-07-17 20:43:43 +02001426 int
1427gui_mch_get_scrollbar_xpadding(void)
1428{
1429 RECT rcTxt, rcWnd;
1430 int xpad;
1431
1432 GetWindowRect(s_textArea, &rcTxt);
1433 GetWindowRect(s_hwnd, &rcWnd);
1434 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001435 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1436 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001437 return (xpad < 0) ? 0 : xpad;
1438}
1439
1440 int
1441gui_mch_get_scrollbar_ypadding(void)
1442{
1443 RECT rcTxt, rcWnd;
1444 int ypad;
1445
1446 GetWindowRect(s_textArea, &rcTxt);
1447 GetWindowRect(s_hwnd, &rcWnd);
1448 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001449 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1450 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001451 return (ypad < 0) ? 0 : ypad;
1452}
1453
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001454 void
1455gui_mch_create_scrollbar(
1456 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001457 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001458{
1459 sb->id = CreateWindow(
1460 "SCROLLBAR", "Scrollbar",
1461 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001462 10, // Any value will do for now
1463 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001464 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001465 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001466}
1467
1468/*
1469 * Find the scrollbar with the given hwnd.
1470 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001471 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001472gui_mswin_find_scrollbar(HWND hwnd)
1473{
1474 win_T *wp;
1475
1476 if (gui.bottom_sbar.id == hwnd)
1477 return &gui.bottom_sbar;
1478 FOR_ALL_WINDOWS(wp)
1479 {
1480 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1481 return &wp->w_scrollbars[SBAR_LEFT];
1482 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1483 return &wp->w_scrollbars[SBAR_RIGHT];
1484 }
1485 return NULL;
1486}
1487
K.Takatac81e9bf2022-01-16 14:15:49 +00001488 static void
1489update_scrollbar_size(void)
1490{
1491 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1492 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1493}
1494
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001495/*
K.Takataabe628e2022-01-23 16:25:17 +00001496 * Get the average character size of a font.
1497 */
1498 static void
1499GetAverageFontSize(HDC hdc, SIZE *size)
1500{
1501 // GetTextMetrics() may not return the right value in tmAveCharWidth
1502 // for some fonts. Do our own average computation.
1503 GetTextExtentPoint(hdc,
1504 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1505 52, size);
1506 size->cx = (size->cx / 26 + 1) / 2;
1507}
1508
1509/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001510 * Get the character size of a font.
1511 */
1512 static void
1513GetFontSize(GuiFont font)
1514{
1515 HWND hwnd = GetDesktopWindow();
1516 HDC hdc = GetWindowDC(hwnd);
1517 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001518 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519 TEXTMETRIC tm;
1520
1521 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001522 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001523
K.Takataabe628e2022-01-23 16:25:17 +00001524 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 gui.char_height = tm.tmHeight + p_linespace;
1526
1527 SelectFont(hdc, hfntOld);
1528
1529 ReleaseDC(hwnd, hdc);
1530}
1531
1532/*
1533 * Adjust gui.char_height (after 'linespace' was changed).
1534 */
1535 int
1536gui_mch_adjust_charheight(void)
1537{
1538 GetFontSize(gui.norm_font);
1539 return OK;
1540}
1541
1542 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001543get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001544{
1545 HFONT font = NULL;
1546
Bram Moolenaar734a8672019-12-02 22:49:38 +01001547 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001548 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001549
1550 if (font == NULL)
1551 return NOFONT;
1552
1553 return (GuiFont)font;
1554}
1555
1556 static int
1557pixels_to_points(int pixels, int vertical)
1558{
1559 int points;
1560 HWND hwnd;
1561 HDC hdc;
1562
1563 hwnd = GetDesktopWindow();
1564 hdc = GetWindowDC(hwnd);
1565
1566 points = MulDiv(pixels, 72,
1567 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1568
1569 ReleaseDC(hwnd, hdc);
1570
1571 return points;
1572}
1573
1574 GuiFont
1575gui_mch_get_font(
1576 char_u *name,
1577 int giveErrorIfMissing)
1578{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001579 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001580 GuiFont font = NOFONT;
1581
1582 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001583 {
1584 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001585 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001586 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001588 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001589 return font;
1590}
1591
1592#if defined(FEAT_EVAL) || defined(PROTO)
1593/*
1594 * Return the name of font "font" in allocated memory.
1595 * Don't know how to get the actual name, thus use the provided name.
1596 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001598gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599{
1600 if (name == NULL)
1601 return NULL;
1602 return vim_strsave(name);
1603}
1604#endif
1605
1606 void
1607gui_mch_free_font(GuiFont font)
1608{
1609 if (font)
1610 DeleteObject((HFONT)font);
1611}
1612
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001613/*
1614 * Return the Pixel value (color) for the given color name.
1615 * Return INVALCOLOR for error.
1616 */
1617 guicolor_T
1618gui_mch_get_color(char_u *name)
1619{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001620 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001621
1622 typedef struct SysColorTable
1623 {
1624 char *name;
1625 int color;
1626 } SysColorTable;
1627
1628 static SysColorTable sys_table[] =
1629 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1631 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001632#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001633 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1634#endif
1635 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1636 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1637 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1638 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1639 {"SYS_DESKTOP", COLOR_DESKTOP},
1640 {"SYS_INFOBK", COLOR_INFOBK},
1641 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1642 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643 {"SYS_BTNFACE", COLOR_BTNFACE},
1644 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1645 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1646 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1647 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1648 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1649 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1650 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1651 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1652 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1653 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1654 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1655 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1656 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1657 {"SYS_MENU", COLOR_MENU},
1658 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1659 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1660 {"SYS_WINDOW", COLOR_WINDOW},
1661 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1662 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1663 };
1664
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665 /*
1666 * Try to look up a system colour.
1667 */
K.Takataeeec2542021-06-02 13:28:16 +02001668 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001669 if (STRICMP(name, sys_table[i].name) == 0)
1670 return GetSysColor(sys_table[i].color);
1671
Bram Moolenaarab302212016-04-26 20:59:29 +02001672 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001673}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001674
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001675 guicolor_T
1676gui_mch_get_rgb_color(int r, int g, int b)
1677{
1678 return gui_get_rgb_color_cmn(r, g, b);
1679}
1680
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001681/*
1682 * Return OK if the key with the termcap name "name" is supported.
1683 */
1684 int
1685gui_mch_haskey(char_u *name)
1686{
1687 int i;
1688
1689 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1690 if (name[0] == special_keys[i].vim_code0 &&
1691 name[1] == special_keys[i].vim_code1)
1692 return OK;
1693 return FAIL;
1694}
1695
1696 void
1697gui_mch_beep(void)
1698{
1699 MessageBeep(MB_OK);
1700}
1701/*
1702 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1703 */
1704 void
1705gui_mch_invert_rectangle(
1706 int r,
1707 int c,
1708 int nr,
1709 int nc)
1710{
1711 RECT rc;
1712
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001713#if defined(FEAT_DIRECTX)
1714 if (IS_ENABLE_DIRECTX())
1715 DWriteContext_Flush(s_dwc);
1716#endif
1717
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001718 /*
1719 * Note: InvertRect() excludes right and bottom of rectangle.
1720 */
1721 rc.left = FILL_X(c);
1722 rc.top = FILL_Y(r);
1723 rc.right = rc.left + nc * gui.char_width;
1724 rc.bottom = rc.top + nr * gui.char_height;
1725 InvertRect(s_hdc, &rc);
1726}
1727
1728/*
1729 * Iconify the GUI window.
1730 */
1731 void
1732gui_mch_iconify(void)
1733{
1734 ShowWindow(s_hwnd, SW_MINIMIZE);
1735}
1736
1737/*
1738 * Draw a cursor without focus.
1739 */
1740 void
1741gui_mch_draw_hollow_cursor(guicolor_T color)
1742{
1743 HBRUSH hbr;
1744 RECT rc;
1745
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001746#if defined(FEAT_DIRECTX)
1747 if (IS_ENABLE_DIRECTX())
1748 DWriteContext_Flush(s_dwc);
1749#endif
1750
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751 /*
1752 * Note: FrameRect() excludes right and bottom of rectangle.
1753 */
1754 rc.left = FILL_X(gui.col);
1755 rc.top = FILL_Y(gui.row);
1756 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001757 if (mb_lefthalve(gui.row, gui.col))
1758 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001759 rc.bottom = rc.top + gui.char_height;
1760 hbr = CreateSolidBrush(color);
1761 FrameRect(s_hdc, &rc, hbr);
1762 DeleteBrush(hbr);
1763}
1764/*
1765 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1766 * color "color".
1767 */
1768 void
1769gui_mch_draw_part_cursor(
1770 int w,
1771 int h,
1772 guicolor_T color)
1773{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001774 RECT rc;
1775
1776 /*
1777 * Note: FillRect() excludes right and bottom of rectangle.
1778 */
1779 rc.left =
1780#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001781 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001782 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1783#endif
1784 FILL_X(gui.col);
1785 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1786 rc.right = rc.left + w;
1787 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001788
Bram Moolenaar92467d32017-12-05 13:22:16 +01001789 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001790}
1791
1792
1793/*
1794 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1795 * dead key's nominal character and re-post the original message.
1796 */
1797 static void
1798outputDeadKey_rePost(MSG originalMsg)
1799{
1800 static MSG deadCharExpel;
1801
1802 if (!dead_key)
1803 return;
1804
1805 dead_key = 0;
1806
Bram Moolenaar734a8672019-12-02 22:49:38 +01001807 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001808 deadCharExpel.message = originalMsg.message;
1809 deadCharExpel.hwnd = originalMsg.hwnd;
1810 deadCharExpel.wParam = VK_SPACE;
1811
K.Takata4ac893f2022-01-20 12:44:28 +00001812 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001813
Bram Moolenaar734a8672019-12-02 22:49:38 +01001814 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001815 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1816 originalMsg.lParam);
1817}
1818
1819
1820/*
1821 * Process a single Windows message.
1822 * If one is not available we hang until one is.
1823 */
1824 static void
1825process_message(void)
1826{
1827 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001828 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001829 char_u string[40];
1830 int i;
1831 int modifiers = 0;
1832 int key;
1833#ifdef FEAT_MENU
1834 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1835#endif
1836
K.Takatab7057bd2022-01-21 11:37:07 +00001837 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001838
1839#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001840 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001841 if (msg.message == WM_OLE)
1842 {
1843 char_u *str = (char_u *)msg.lParam;
1844 if (str == NULL || *str == NUL)
1845 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001846 // Message can't be ours, forward it. Fixes problem with Ultramon
1847 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001848 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001849 }
1850 else
1851 {
1852 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001853 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001854 }
1855 return;
1856 }
1857#endif
1858
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001859#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001860 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001861 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862 {
1863 HandleMouseHide(msg.message, msg.lParam);
1864 return;
1865 }
1866#endif
1867
1868 /*
1869 * Check if it's a special key that we recognise. If not, call
1870 * TranslateMessage().
1871 */
1872 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1873 {
1874 vk = (int) msg.wParam;
1875
1876 /*
1877 * Handle dead keys in special conditions in other cases we let Windows
1878 * handle them and do not interfere.
1879 *
1880 * The dead_key flag must be reset on several occasions:
1881 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1882 * consumed at that point (This is when we let Windows combine the
1883 * dead character on its own)
1884 *
1885 * - Before doing something special such as regenerating keypresses to
1886 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001887 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 * immediately to _OnChar() (or _OnSysChar()).
1889 */
1890 if (dead_key)
1891 {
1892 /*
1893 * If a dead key was pressed and the user presses VK_SPACE,
1894 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1895 * with the dead char now, so do nothing special and let Windows
1896 * handle it.
1897 *
1898 * Note that VK_SPACE combines with the dead_key's character and
1899 * only one WM_CHAR will be generated by TranslateMessage(), in
1900 * the two other cases two WM_CHAR will be generated: the dead
1901 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1902 * user expects.
1903 */
1904 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1905 {
1906 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001907 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908 return;
1909 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001910 // In modes where we are not typing, dead keys should behave
1911 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001912 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1913 {
1914 outputDeadKey_rePost(msg);
1915 return;
1916 }
1917 }
1918
Bram Moolenaar734a8672019-12-02 22:49:38 +01001919 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001920 if (vk == VK_CANCEL)
1921 {
1922 trash_input_buf();
1923 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001924 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001925 string[0] = Ctrl_C;
1926 add_to_input_buf(string, 1);
1927 }
1928
1929 for (i = 0; special_keys[i].key_sym != 0; i++)
1930 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001931 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001932 if (special_keys[i].key_sym == vk
1933 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1934 {
1935 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001936 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001937 * is a key that would normally trigger the dead key nominal
1938 * character output (such as a NUMPAD printable character or
1939 * the TAB key, etc...).
1940 */
1941 if (dead_key && (special_keys[i].vim_code0 == 'K'
1942 || vk == VK_TAB || vk == CAR))
1943 {
1944 outputDeadKey_rePost(msg);
1945 return;
1946 }
1947
1948#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001949 // Check for <F10>: Windows selects the menu. When <F10> is
1950 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001951 if (vk == VK_F10
1952 && gui.menu_is_active
1953 && check_map(k10, State, FALSE, TRUE, FALSE,
1954 NULL, NULL) == NULL)
1955 break;
1956#endif
1957 if (GetKeyState(VK_SHIFT) & 0x8000)
1958 modifiers |= MOD_MASK_SHIFT;
1959 /*
1960 * Don't use caps-lock as shift, because these are special keys
1961 * being considered here, and we only want letters to get
1962 * shifted -- webb
1963 */
1964 /*
1965 if (GetKeyState(VK_CAPITAL) & 0x0001)
1966 modifiers ^= MOD_MASK_SHIFT;
1967 */
1968 if (GetKeyState(VK_CONTROL) & 0x8000)
1969 modifiers |= MOD_MASK_CTRL;
1970 if (GetKeyState(VK_MENU) & 0x8000)
1971 modifiers |= MOD_MASK_ALT;
1972
1973 if (special_keys[i].vim_code1 == NUL)
1974 key = special_keys[i].vim_code0;
1975 else
1976 key = TO_SPECIAL(special_keys[i].vim_code0,
1977 special_keys[i].vim_code1);
1978 key = simplify_key(key, &modifiers);
1979 if (key == CSI)
1980 key = K_CSI;
1981
1982 if (modifiers)
1983 {
1984 string[0] = CSI;
1985 string[1] = KS_MODIFIER;
1986 string[2] = modifiers;
1987 add_to_input_buf(string, 3);
1988 }
1989
1990 if (IS_SPECIAL(key))
1991 {
1992 string[0] = CSI;
1993 string[1] = K_SECOND(key);
1994 string[2] = K_THIRD(key);
1995 add_to_input_buf(string, 3);
1996 }
1997 else
1998 {
1999 int len;
2000
Bram Moolenaar734a8672019-12-02 22:49:38 +01002001 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002002 len = char_to_string(key, string, 40, FALSE);
2003 add_to_input_buf(string, len);
2004 }
2005 break;
2006 }
2007 }
2008 if (special_keys[i].key_sym == 0)
2009 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002010 // Some keys need C-S- where they should only need C-.
2011 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2012 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002013 if (vk != 0xff
2014 && (GetKeyState(VK_CONTROL) & 0x8000)
2015 && !(GetKeyState(VK_SHIFT) & 0x8000)
2016 && !(GetKeyState(VK_MENU) & 0x8000))
2017 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002018 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002019 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2020 {
2021 string[0] = Ctrl_HAT;
2022 add_to_input_buf(string, 1);
2023 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002024 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2025 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002026 {
2027 string[0] = Ctrl__;
2028 add_to_input_buf(string, 1);
2029 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002030 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002031 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2032 {
2033 string[0] = Ctrl_AT;
2034 add_to_input_buf(string, 1);
2035 }
2036 else
K.Takata4ac893f2022-01-20 12:44:28 +00002037 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002038 }
2039 else
K.Takata4ac893f2022-01-20 12:44:28 +00002040 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 }
2042 }
2043#ifdef FEAT_MBYTE_IME
2044 else if (msg.message == WM_IME_NOTIFY)
2045 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2046 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002047 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002048 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002049#endif
2050
2051#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002052 // Check for <F10>: Default effect is to select the menu. When <F10> is
2053 // mapped we need to stop it here to avoid strange effects (e.g., for the
2054 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002055 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2056 NULL, NULL) == NULL)
2057#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002058 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059}
2060
2061/*
2062 * Catch up with any queued events. This may put keyboard input into the
2063 * input buffer, call resize call-backs, trigger timers etc. If there is
2064 * nothing in the event queue (& no timers pending), then we return
2065 * immediately.
2066 */
2067 void
2068gui_mch_update(void)
2069{
2070 MSG msg;
2071
2072 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002073 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074 && !vim_is_input_buf_full())
2075 process_message();
2076}
2077
Bram Moolenaar4231da42016-06-02 14:30:04 +02002078 static void
2079remove_any_timer(void)
2080{
2081 MSG msg;
2082
2083 if (s_wait_timer != 0 && !s_timed_out)
2084 {
2085 KillTimer(NULL, s_wait_timer);
2086
Bram Moolenaar734a8672019-12-02 22:49:38 +01002087 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002088 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002089 ;
2090 s_wait_timer = 0;
2091 }
2092}
2093
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094/*
2095 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2096 * from the keyboard.
2097 * wtime == -1 Wait forever.
2098 * wtime == 0 This should never happen.
2099 * wtime > 0 Wait wtime milliseconds for a character.
2100 * Returns OK if a character was found to be available within the given time,
2101 * or FAIL otherwise.
2102 */
2103 int
2104gui_mch_wait_for_chars(int wtime)
2105{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106 int focus;
2107
2108 s_timed_out = FALSE;
2109
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002110 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002111 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002112 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 if (s_busy_processing)
2114 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002115
2116 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002117 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2118 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002119 }
2120
2121 allow_scrollbar = TRUE;
2122
2123 focus = gui.in_focus;
2124 while (!s_timed_out)
2125 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002126 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002127 if (gui.in_focus != focus)
2128 {
2129 if (gui.in_focus)
2130 gui_mch_start_blink();
2131 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002132 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133 focus = gui.in_focus;
2134 }
2135
2136 if (s_need_activate)
2137 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002138 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002139 s_need_activate = FALSE;
2140 }
2141
Bram Moolenaar4231da42016-06-02 14:30:04 +02002142#ifdef FEAT_TIMERS
2143 did_add_timer = FALSE;
2144#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002145#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002146 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002147 for (;;)
2148 {
2149 MSG msg;
2150
2151 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002152# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002153 if (did_add_timer)
2154 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002155# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002156 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002157 {
2158 process_message();
2159 break;
2160 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002161 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002162 // TODO: The 10 msec is a compromise between laggy response
2163 // and consuming more CPU time. Better would be to handle
2164 // channel messages when they arrive.
2165 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002166 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002167 break;
2168 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002169#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002170 // Don't use gui_mch_update() because then we will spin-lock until a
2171 // char arrives, instead we use GetMessage() to hang until an
2172 // event arrives. No need to check for input_buf_full because we are
2173 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002174 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002175#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002176
2177 if (input_available())
2178 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002179 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002180 allow_scrollbar = FALSE;
2181
Bram Moolenaar89c00032019-09-04 13:53:21 +02002182 // Clear pending mouse button, the release event may have been
2183 // taken by the dialog window. But don't do this when getting
2184 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185 if (!s_getting_focus)
2186 s_button_pending = -1;
2187
2188 return OK;
2189 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002190
2191#ifdef FEAT_TIMERS
2192 if (did_add_timer)
2193 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002194 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002195 remove_any_timer();
2196 break;
2197 }
2198#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002199 }
2200 allow_scrollbar = FALSE;
2201 return FAIL;
2202}
2203
2204/*
2205 * Clear a rectangular region of the screen from text pos (row1, col1) to
2206 * (row2, col2) inclusive.
2207 */
2208 void
2209gui_mch_clear_block(
2210 int row1,
2211 int col1,
2212 int row2,
2213 int col2)
2214{
2215 RECT rc;
2216
2217 /*
2218 * Clear one extra pixel at the far right, for when bold characters have
2219 * spilled over to the window border.
2220 * Note: FillRect() excludes right and bottom of rectangle.
2221 */
2222 rc.left = FILL_X(col1);
2223 rc.top = FILL_Y(row1);
2224 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2225 rc.bottom = FILL_Y(row2 + 1);
2226 clear_rect(&rc);
2227}
2228
2229/*
2230 * Clear the whole text window.
2231 */
2232 void
2233gui_mch_clear_all(void)
2234{
2235 RECT rc;
2236
2237 rc.left = 0;
2238 rc.top = 0;
2239 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2240 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2241 clear_rect(&rc);
2242}
2243/*
2244 * Menu stuff.
2245 */
2246
2247 void
2248gui_mch_enable_menu(int flag)
2249{
2250#ifdef FEAT_MENU
2251 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2252#endif
2253}
2254
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002255 void
2256gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002257 int x UNUSED,
2258 int y UNUSED,
2259 int w UNUSED,
2260 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002261{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002262 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263}
2264
2265#if defined(FEAT_MENU) || defined(PROTO)
2266/*
2267 * Make menu item hidden or not hidden
2268 */
2269 void
2270gui_mch_menu_hidden(
2271 vimmenu_T *menu,
2272 int hidden)
2273{
2274 /*
2275 * This doesn't do what we want. Hmm, just grey the menu items for now.
2276 */
2277 /*
2278 if (hidden)
2279 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2280 else
2281 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2282 */
2283 gui_mch_menu_grey(menu, hidden);
2284}
2285
2286/*
2287 * This is called after setting all the menus to grey/hidden or not.
2288 */
2289 void
2290gui_mch_draw_menubar(void)
2291{
2292 DrawMenuBar(s_hwnd);
2293}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002294#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296/*
2297 * Return the RGB value of a pixel as a long.
2298 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002299 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002300gui_mch_get_rgb(guicolor_T pixel)
2301{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002302 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2303 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002304}
2305
2306#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002307/*
2308 * Convert pixels in X to dialog units
2309 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310 static WORD
2311PixelToDialogX(int numPixels)
2312{
2313 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2314}
2315
Bram Moolenaar734a8672019-12-02 22:49:38 +01002316/*
2317 * Convert pixels in Y to dialog units
2318 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319 static WORD
2320PixelToDialogY(int numPixels)
2321{
2322 return (WORD)((numPixels * 8) / s_dlgfntheight);
2323}
2324
Bram Moolenaar734a8672019-12-02 22:49:38 +01002325/*
2326 * Return the width in pixels of the given text in the given DC.
2327 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328 static int
2329GetTextWidth(HDC hdc, char_u *str, int len)
2330{
2331 SIZE size;
2332
2333 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2334 return size.cx;
2335}
2336
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337/*
2338 * Return the width in pixels of the given text in the given DC, taking care
2339 * of 'encoding' to active codepage conversion.
2340 */
2341 static int
2342GetTextWidthEnc(HDC hdc, char_u *str, int len)
2343{
2344 SIZE size;
2345 WCHAR *wstr;
2346 int n;
2347 int wlen = len;
2348
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002349 wstr = enc_to_utf16(str, &wlen);
2350 if (wstr == NULL)
2351 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002353 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2354 vim_free(wstr);
2355 if (n)
2356 return size.cx;
2357 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002360static void get_work_area(RECT *spi_rect);
2361
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362/*
2363 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002364 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2365 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366 */
2367 static BOOL
2368CenterWindow(
2369 HWND hwndChild,
2370 HWND hwndParent)
2371{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002372 HMONITOR mon;
2373 MONITORINFO moninfo;
2374 RECT rChild, rParent, rScreen;
2375 int wChild, hChild, wParent, hParent;
2376 int xNew, yNew;
2377 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378
2379 GetWindowRect(hwndChild, &rChild);
2380 wChild = rChild.right - rChild.left;
2381 hChild = rChild.bottom - rChild.top;
2382
Bram Moolenaar734a8672019-12-02 22:49:38 +01002383 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002385 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002386 else
2387 GetWindowRect(hwndParent, &rParent);
2388 wParent = rParent.right - rParent.left;
2389 hParent = rParent.bottom - rParent.top;
2390
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002391 moninfo.cbSize = sizeof(MONITORINFO);
2392 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2393 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002395 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002397 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002399 hdc = GetDC(hwndChild);
2400 rScreen.left = 0;
2401 rScreen.top = 0;
2402 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2403 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2404 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002405 }
2406
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002407 xNew = rParent.left + ((wParent - wChild) / 2);
2408 if (xNew < rScreen.left)
2409 xNew = rScreen.left;
2410 else if ((xNew + wChild) > rScreen.right)
2411 xNew = rScreen.right - wChild;
2412
2413 yNew = rParent.top + ((hParent - hChild) / 2);
2414 if (yNew < rScreen.top)
2415 yNew = rScreen.top;
2416 else if ((yNew + hChild) > rScreen.bottom)
2417 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002418
2419 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2420 SWP_NOSIZE | SWP_NOZORDER);
2421}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002422#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002424#if defined(FEAT_TOOLBAR) || defined(PROTO)
2425 void
2426gui_mch_show_toolbar(int showit)
2427{
2428 if (s_toolbarhwnd == NULL)
2429 return;
2430
2431 if (showit)
2432 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002433 // Enable unicode support
2434 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2435 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436 ShowWindow(s_toolbarhwnd, SW_SHOW);
2437 }
2438 else
2439 ShowWindow(s_toolbarhwnd, SW_HIDE);
2440}
2441
Bram Moolenaar734a8672019-12-02 22:49:38 +01002442// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002443# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002444
2445#endif
2446
2447#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2448 static void
2449add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2450{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002451 WCHAR *wn;
2452 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002454 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002456 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002457
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002458 infow.cbSize = sizeof(infow);
2459 infow.fMask = MIIM_TYPE | MIIM_ID;
2460 infow.wID = item_id;
2461 infow.fType = MFT_STRING;
2462 infow.dwTypeData = wn;
2463 infow.cch = (UINT)wcslen(wn);
2464 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2465 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466}
2467
2468 static void
2469show_tabline_popup_menu(void)
2470{
2471 HMENU tab_pmenu;
2472 long rval;
2473 POINT pt;
2474
Bram Moolenaar734a8672019-12-02 22:49:38 +01002475 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002476 if (hold_gui_events
2477# ifdef FEAT_CMDWIN
2478 || cmdwin_type != 0
2479# endif
2480 )
2481 return;
2482
2483 tab_pmenu = CreatePopupMenu();
2484 if (tab_pmenu == NULL)
2485 return;
2486
2487 if (first_tabpage->tp_next != NULL)
2488 add_tabline_popup_menu_entry(tab_pmenu,
2489 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2490 add_tabline_popup_menu_entry(tab_pmenu,
2491 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2492 add_tabline_popup_menu_entry(tab_pmenu,
2493 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2494
2495 GetCursorPos(&pt);
2496 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2497 NULL);
2498
2499 DestroyMenu(tab_pmenu);
2500
Bram Moolenaar734a8672019-12-02 22:49:38 +01002501 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502 if (rval > 0)
2503 {
2504 TCHITTESTINFO htinfo;
2505 int idx;
2506
2507 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2508 return;
2509
2510 htinfo.pt.x = pt.x;
2511 htinfo.pt.y = pt.y;
2512 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2513 if (idx == -1)
2514 idx = 0;
2515 else
2516 idx += 1;
2517
2518 send_tabline_menu_event(idx, (int)rval);
2519 }
2520}
2521
2522/*
2523 * Show or hide the tabline.
2524 */
2525 void
2526gui_mch_show_tabline(int showit)
2527{
2528 if (s_tabhwnd == NULL)
2529 return;
2530
2531 if (!showit != !showing_tabline)
2532 {
2533 if (showit)
2534 ShowWindow(s_tabhwnd, SW_SHOW);
2535 else
2536 ShowWindow(s_tabhwnd, SW_HIDE);
2537 showing_tabline = showit;
2538 }
2539}
2540
2541/*
2542 * Return TRUE when tabline is displayed.
2543 */
2544 int
2545gui_mch_showing_tabline(void)
2546{
2547 return s_tabhwnd != NULL && showing_tabline;
2548}
2549
2550/*
2551 * Update the labels of the tabline.
2552 */
2553 void
2554gui_mch_update_tabline(void)
2555{
2556 tabpage_T *tp;
2557 TCITEM tie;
2558 int nr = 0;
2559 int curtabidx = 0;
2560 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002562
2563 if (s_tabhwnd == NULL)
2564 return;
2565
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002566 // Enable unicode support
2567 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002568
2569 tie.mask = TCIF_TEXT;
2570 tie.iImage = -1;
2571
Bram Moolenaar734a8672019-12-02 22:49:38 +01002572 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002573 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2574
Bram Moolenaar734a8672019-12-02 22:49:38 +01002575 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2577 {
2578 if (tp == curtab)
2579 curtabidx = nr;
2580
2581 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2582 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002583 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002584 tie.pszText = "-Empty-";
2585 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2586 tabadded = 1;
2587 }
2588
2589 get_tabline_label(tp, FALSE);
2590 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 wstr = enc_to_utf16(NameBuff, NULL);
2593 if (wstr != NULL)
2594 {
2595 TCITEMW tiw;
2596
2597 tiw.mask = TCIF_TEXT;
2598 tiw.iImage = -1;
2599 tiw.pszText = wstr;
2600 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2601 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002603 }
2604
Bram Moolenaar734a8672019-12-02 22:49:38 +01002605 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002606 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2607 TabCtrl_DeleteItem(s_tabhwnd, nr);
2608
2609 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2610 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2611
Bram Moolenaar734a8672019-12-02 22:49:38 +01002612 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2614 RedrawWindow(s_tabhwnd, NULL, NULL,
2615 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2616
2617 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2618 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2619}
2620
2621/*
2622 * Set the current tab to "nr". First tab is 1.
2623 */
2624 void
2625gui_mch_set_curtab(int nr)
2626{
2627 if (s_tabhwnd == NULL)
2628 return;
2629
2630 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2631 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2632}
2633
2634#endif
2635
2636/*
2637 * ":simalt" command.
2638 */
2639 void
2640ex_simalt(exarg_T *eap)
2641{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002642 char_u *keys = eap->arg;
2643 int fill_typebuf = FALSE;
2644 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002645
2646 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2647 while (*keys)
2648 {
2649 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002650 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002651 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2652 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002653 fill_typebuf = TRUE;
2654 }
2655 if (fill_typebuf)
2656 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002657 // Put a NOP in the typeahead buffer so that the message will get
2658 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002659 key_name[0] = K_SPECIAL;
2660 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002661 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002662 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002663#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002664 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002665#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002666 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002667 }
2668}
2669
2670/*
2671 * Create the find & replace dialogs.
2672 * You can't have both at once: ":find" when replace is showing, destroys
2673 * the replace dialog first, and the other way around.
2674 */
2675#ifdef MSWIN_FIND_REPLACE
2676 static void
2677initialise_findrep(char_u *initial_string)
2678{
2679 int wword = FALSE;
2680 int mcase = !p_ic;
2681 char_u *entry_text;
2682
Bram Moolenaar734a8672019-12-02 22:49:38 +01002683 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002684 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2685
2686 s_findrep_struct.hwndOwner = s_hwnd;
2687 s_findrep_struct.Flags = FR_DOWN;
2688 if (mcase)
2689 s_findrep_struct.Flags |= FR_MATCHCASE;
2690 if (wword)
2691 s_findrep_struct.Flags |= FR_WHOLEWORD;
2692 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002693 {
2694 WCHAR *p = enc_to_utf16(entry_text, NULL);
2695 if (p != NULL)
2696 {
2697 int len = s_findrep_struct.wFindWhatLen - 1;
2698
2699 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2700 s_findrep_struct.lpstrFindWhat[len] = NUL;
2701 vim_free(p);
2702 }
2703 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704 vim_free(entry_text);
2705}
2706#endif
2707
2708 static void
2709set_window_title(HWND hwnd, char *title)
2710{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002711 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712 {
2713 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002714
Bram Moolenaar734a8672019-12-02 22:49:38 +01002715 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2717 if (wbuf != NULL)
2718 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002719 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 }
2722 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002723 else
2724 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002725}
2726
2727 void
2728gui_mch_find_dialog(exarg_T *eap)
2729{
2730#ifdef MSWIN_FIND_REPLACE
2731 if (s_findrep_msg != 0)
2732 {
2733 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2734 DestroyWindow(s_findrep_hwnd);
2735
2736 if (!IsWindow(s_findrep_hwnd))
2737 {
2738 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002739 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 }
2741
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002742 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743 (void)SetFocus(s_findrep_hwnd);
2744
2745 s_findrep_is_find = TRUE;
2746 }
2747#endif
2748}
2749
2750
2751 void
2752gui_mch_replace_dialog(exarg_T *eap)
2753{
2754#ifdef MSWIN_FIND_REPLACE
2755 if (s_findrep_msg != 0)
2756 {
2757 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2758 DestroyWindow(s_findrep_hwnd);
2759
2760 if (!IsWindow(s_findrep_hwnd))
2761 {
2762 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002763 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002764 }
2765
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002766 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767 (void)SetFocus(s_findrep_hwnd);
2768
2769 s_findrep_is_find = FALSE;
2770 }
2771#endif
2772}
2773
2774
2775/*
2776 * Set visibility of the pointer.
2777 */
2778 void
2779gui_mch_mousehide(int hide)
2780{
2781 if (hide != gui.pointer_hidden)
2782 {
2783 ShowCursor(!hide);
2784 gui.pointer_hidden = hide;
2785 }
2786}
2787
2788#ifdef FEAT_MENU
2789 static void
2790gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2791{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002792 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002793 gui_mch_mousehide(FALSE);
2794
2795 (void)TrackPopupMenu(
2796 (HMENU)menu->submenu_id,
2797 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2798 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002799 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002800 s_hwnd,
2801 NULL);
2802 /*
2803 * NOTE: The pop-up menu can eat the mouse up event.
2804 * We deal with this in normal.c.
2805 */
2806}
2807#endif
2808
2809/*
2810 * Got a message when the system will go down.
2811 */
2812 static void
2813_OnEndSession(void)
2814{
2815 getout_preserve_modified(1);
2816}
2817
2818/*
2819 * Get this message when the user clicks on the cross in the top right corner
2820 * of a Windows95 window.
2821 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002822 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002823_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824{
2825 gui_shell_closed();
2826}
2827
2828/*
2829 * Get a message when the window is being destroyed.
2830 */
2831 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002832_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002833{
2834 if (!destroying)
2835 _OnClose(hwnd);
2836}
2837
2838 static void
2839_OnPaint(
2840 HWND hwnd)
2841{
2842 if (!IsMinimized(hwnd))
2843 {
2844 PAINTSTRUCT ps;
2845
Bram Moolenaar734a8672019-12-02 22:49:38 +01002846 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002847 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848
Bram Moolenaar734a8672019-12-02 22:49:38 +01002849 // prevent multi-byte characters from misprinting on an invalid
2850 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851 if (has_mbyte)
2852 {
2853 RECT rect;
2854
2855 GetClientRect(hwnd, &rect);
2856 ps.rcPaint.left = rect.left;
2857 ps.rcPaint.right = rect.right;
2858 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859
2860 if (!IsRectEmpty(&ps.rcPaint))
2861 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2863 ps.rcPaint.right - ps.rcPaint.left + 1,
2864 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2865 }
2866
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867 EndPaint(hwnd, &ps);
2868 }
2869}
2870
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871 static void
2872_OnSize(
2873 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002874 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 int cx,
2876 int cy)
2877{
K.Takatac81e9bf2022-01-16 14:15:49 +00002878 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 {
2880 gui_resize_shell(cx, cy);
2881
Bram Moolenaar734a8672019-12-02 22:49:38 +01002882 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002883 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002884 }
2885}
2886
2887 static void
2888_OnSetFocus(
2889 HWND hwnd,
2890 HWND hwndOldFocus)
2891{
2892 gui_focus_change(TRUE);
2893 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002894 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002895}
2896
2897 static void
2898_OnKillFocus(
2899 HWND hwnd,
2900 HWND hwndNewFocus)
2901{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002902 if (destroying)
2903 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 gui_focus_change(FALSE);
2905 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002906 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907}
2908
2909/*
2910 * Get a message when the user switches back to vim
2911 */
2912 static LRESULT
2913_OnActivateApp(
2914 HWND hwnd,
2915 BOOL fActivate,
2916 DWORD dwThreadId)
2917{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002918 // we call gui_focus_change() in _OnSetFocus()
2919 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002920 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921}
2922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 void
2924gui_mch_destroy_scrollbar(scrollbar_T *sb)
2925{
2926 DestroyWindow(sb->id);
2927}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928
2929/*
2930 * Get current mouse coordinates in text window.
2931 */
2932 void
2933gui_mch_getmouse(int *x, int *y)
2934{
2935 RECT rct;
2936 POINT mp;
2937
2938 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002939 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 *x = (int)(mp.x - rct.left);
2941 *y = (int)(mp.y - rct.top);
2942}
2943
2944/*
2945 * Move mouse pointer to character at (x, y).
2946 */
2947 void
2948gui_mch_setmouse(int x, int y)
2949{
2950 RECT rct;
2951
2952 (void)GetWindowRect(s_textArea, &rct);
2953 (void)SetCursorPos(x + gui.border_offset + rct.left,
2954 y + gui.border_offset + rct.top);
2955}
2956
2957 static void
2958gui_mswin_get_valid_dimensions(
2959 int w,
2960 int h,
2961 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002962 int *valid_h,
2963 int *cols,
2964 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002965{
2966 int base_width, base_height;
2967
2968 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002969 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2970 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002971 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002972 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2973 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2974 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2975 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002976 *cols = (w - base_width) / gui.char_width;
2977 *rows = (h - base_height) / gui.char_height;
2978 *valid_w = base_width + *cols * gui.char_width;
2979 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980}
2981
2982 void
2983gui_mch_flash(int msec)
2984{
2985 RECT rc;
2986
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002987#if defined(FEAT_DIRECTX)
2988 if (IS_ENABLE_DIRECTX())
2989 DWriteContext_Flush(s_dwc);
2990#endif
2991
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002992 /*
2993 * Note: InvertRect() excludes right and bottom of rectangle.
2994 */
2995 rc.left = 0;
2996 rc.top = 0;
2997 rc.right = gui.num_cols * gui.char_width;
2998 rc.bottom = gui.num_rows * gui.char_height;
2999 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003000 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003001
Bram Moolenaar734a8672019-12-02 22:49:38 +01003002 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003003
3004 InvertRect(s_hdc, &rc);
3005}
3006
3007/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003008 * Check if the specified point is on-screen. (multi-monitor aware)
3009 */
3010 static BOOL
3011is_point_onscreen(int x, int y)
3012{
3013 POINT pt = {x, y};
3014
3015 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3016}
3017
3018/*
3019 * Check if the whole area of the specified window is on-screen.
3020 *
3021 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3022 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3023 * only works when the whole window is on-screen.
3024 */
3025 static BOOL
3026is_window_onscreen(HWND hwnd)
3027{
3028 RECT rc;
3029
3030 GetWindowRect(hwnd, &rc);
3031
3032 if (!is_point_onscreen(rc.left, rc.top))
3033 return FALSE;
3034 if (!is_point_onscreen(rc.left, rc.bottom))
3035 return FALSE;
3036 if (!is_point_onscreen(rc.right, rc.top))
3037 return FALSE;
3038 if (!is_point_onscreen(rc.right, rc.bottom))
3039 return FALSE;
3040 return TRUE;
3041}
3042
3043/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 * Return flags used for scrolling.
3045 * The SW_INVALIDATE is required when part of the window is covered or
3046 * off-screen. Refer to MS KB Q75236.
3047 */
3048 static int
3049get_scroll_flags(void)
3050{
3051 HWND hwnd;
3052 RECT rcVim, rcOther, rcDest;
3053
Bram Moolenaar185577e2020-10-29 20:08:21 +01003054 // Check if the window is (partly) off-screen.
3055 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003056 return SW_INVALIDATE;
3057
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003058 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003059 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3061 if (IsWindowVisible(hwnd))
3062 {
3063 GetWindowRect(hwnd, &rcOther);
3064 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3065 return SW_INVALIDATE;
3066 }
3067 return 0;
3068}
3069
3070/*
3071 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3072 * may not be scrolled out properly.
3073 * For gVim, when _OnScroll() is repeated, the character at the
3074 * previous cursor position may be left drawn after scroll.
3075 * The problem can be avoided by calling GetPixel() to get a pixel in
3076 * the region before ScrollWindowEx().
3077 */
3078 static void
3079intel_gpu_workaround(void)
3080{
3081 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3082}
3083
3084/*
3085 * Delete the given number of lines from the given row, scrolling up any
3086 * text further down within the scroll region.
3087 */
3088 void
3089gui_mch_delete_lines(
3090 int row,
3091 int num_lines)
3092{
3093 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003094
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003095 rc.left = FILL_X(gui.scroll_region_left);
3096 rc.right = FILL_X(gui.scroll_region_right + 1);
3097 rc.top = FILL_Y(row);
3098 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3099
Bram Moolenaar92467d32017-12-05 13:22:16 +01003100#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003101 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003102 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003103 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003104 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003105 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003106#endif
3107 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003108#if defined(FEAT_DIRECTX)
3109 if (IS_ENABLE_DIRECTX())
3110 DWriteContext_Flush(s_dwc);
3111#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003112 intel_gpu_workaround();
3113 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003114 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003115 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003116 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003117
Bram Moolenaar734a8672019-12-02 22:49:38 +01003118 // This seems to be required to avoid the cursor disappearing when
3119 // scrolling such that the cursor ends up in the top-left character on
3120 // the screen... But why? (Webb)
3121 // It's probably fixed by disabling drawing the cursor while scrolling.
3122 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003123
3124 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3125 gui.scroll_region_left,
3126 gui.scroll_region_bot, gui.scroll_region_right);
3127}
3128
3129/*
3130 * Insert the given number of lines before the given row, scrolling down any
3131 * following text within the scroll region.
3132 */
3133 void
3134gui_mch_insert_lines(
3135 int row,
3136 int num_lines)
3137{
3138 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003139
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140 rc.left = FILL_X(gui.scroll_region_left);
3141 rc.right = FILL_X(gui.scroll_region_right + 1);
3142 rc.top = FILL_Y(row);
3143 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003144
3145#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003146 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003147 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003148 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003149 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003150 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003151#endif
3152 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003153#if defined(FEAT_DIRECTX)
3154 if (IS_ENABLE_DIRECTX())
3155 DWriteContext_Flush(s_dwc);
3156#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003157 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003158 // The SW_INVALIDATE is required when part of the window is covered or
3159 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003162 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003165 gui_clear_block(row, gui.scroll_region_left,
3166 row + num_lines - 1, gui.scroll_region_right);
3167}
3168
3169
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003171gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172{
3173#if defined(FEAT_DIRECTX)
3174 DWriteContext_Close(s_dwc);
3175 DWrite_Final();
3176 s_dwc = NULL;
3177#endif
3178
3179 ReleaseDC(s_textArea, s_hdc);
3180 DeleteObject(s_brush);
3181
3182#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003183 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003184 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3185#endif
3186
Bram Moolenaar734a8672019-12-02 22:49:38 +01003187 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188 if (s_hwnd != NULL)
3189 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003190 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191 DestroyWindow(s_hwnd);
3192 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193}
3194
3195 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003196logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003197{
3198 char *p;
3199 char *res;
3200 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003201 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003202 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003203 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003205 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3206 if (font_name == NULL)
3207 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003208 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003209 quality_name = quality_id2name((int)lf.lfQuality);
3210
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003211 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003212 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003213 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 if (res != NULL)
3215 {
3216 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003217 // make a normal font string out of the lf thing:
3218 points = pixels_to_points(
3219 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3220 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3221 sprintf((char *)p, "%s:h%d", font_name, points);
3222 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003223 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 while (*p)
3225 {
3226 if (*p == ' ')
3227 *p = '_';
3228 ++p;
3229 }
3230 if (lf.lfItalic)
3231 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003232 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233 STRCAT(p, ":b");
3234 if (lf.lfUnderline)
3235 STRCAT(p, ":u");
3236 if (lf.lfStrikeOut)
3237 STRCAT(p, ":s");
3238 if (charset_name != NULL)
3239 {
3240 STRCAT(p, ":c");
3241 STRCAT(p, charset_name);
3242 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003243 if (quality_name != NULL)
3244 {
3245 STRCAT(p, ":q");
3246 STRCAT(p, quality_name);
3247 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248 }
3249
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003250 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003251 return (char_u *)res;
3252}
3253
3254
3255#ifdef FEAT_MBYTE_IME
3256/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003257 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003258 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003259 */
3260 static void
3261update_im_font(void)
3262{
K.Takatac81e9bf2022-01-16 14:15:49 +00003263 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003264
3265 if (p_guifontwide != NULL && *p_guifontwide != NUL
3266 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003267 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268 norm_logfont = lf_wide;
3269 else
3270 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003271
3272 lf = norm_logfont;
3273 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3274 // Work around when PerMonitorV2 is not enabled in the process level.
3275 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3276 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003277}
3278#endif
3279
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003280/*
3281 * Handler of gui.wide_font (p_guifontwide) changed notification.
3282 */
3283 void
3284gui_mch_wide_font_changed(void)
3285{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003286 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003287
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003288#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003289 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003290#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003291
3292 gui_mch_free_font(gui.wide_ital_font);
3293 gui.wide_ital_font = NOFONT;
3294 gui_mch_free_font(gui.wide_bold_font);
3295 gui.wide_bold_font = NOFONT;
3296 gui_mch_free_font(gui.wide_boldital_font);
3297 gui.wide_boldital_font = NOFONT;
3298
3299 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003300 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003301 {
3302 if (!lf.lfItalic)
3303 {
3304 lf.lfItalic = TRUE;
3305 gui.wide_ital_font = get_font_handle(&lf);
3306 lf.lfItalic = FALSE;
3307 }
3308 if (lf.lfWeight < FW_BOLD)
3309 {
3310 lf.lfWeight = FW_BOLD;
3311 gui.wide_bold_font = get_font_handle(&lf);
3312 if (!lf.lfItalic)
3313 {
3314 lf.lfItalic = TRUE;
3315 gui.wide_boldital_font = get_font_handle(&lf);
3316 }
3317 }
3318 }
3319}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003320
3321/*
3322 * Initialise vim to use the font with the given name.
3323 * Return FAIL if the font could not be loaded, OK otherwise.
3324 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003326gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327{
K.Takatac81e9bf2022-01-16 14:15:49 +00003328 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003329 GuiFont font = NOFONT;
3330 char_u *p;
3331
Bram Moolenaar734a8672019-12-02 22:49:38 +01003332 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003333 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003334 {
3335 lfOrig = lf;
3336 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003338 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339 if (font == NOFONT)
3340 return FAIL;
3341
3342 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003343 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003344#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345 norm_logfont = lf;
3346 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003347 if (!s_in_dpichanged)
3348 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349#endif
3350 gui_mch_free_font(gui.norm_font);
3351 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003352 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 GetFontSize(font);
3354
K.Takatac81e9bf2022-01-16 14:15:49 +00003355 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003356 if (p != NULL)
3357 {
3358 hl_set_font_name(p);
3359
Bram Moolenaar734a8672019-12-02 22:49:38 +01003360 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3362 {
3363 vim_free(p_guifont);
3364 p_guifont = p;
3365 }
3366 else
3367 vim_free(p);
3368 }
3369
3370 gui_mch_free_font(gui.ital_font);
3371 gui.ital_font = NOFONT;
3372 gui_mch_free_font(gui.bold_font);
3373 gui.bold_font = NOFONT;
3374 gui_mch_free_font(gui.boldital_font);
3375 gui.boldital_font = NOFONT;
3376
3377 if (!lf.lfItalic)
3378 {
3379 lf.lfItalic = TRUE;
3380 gui.ital_font = get_font_handle(&lf);
3381 lf.lfItalic = FALSE;
3382 }
3383 if (lf.lfWeight < FW_BOLD)
3384 {
3385 lf.lfWeight = FW_BOLD;
3386 gui.bold_font = get_font_handle(&lf);
3387 if (!lf.lfItalic)
3388 {
3389 lf.lfItalic = TRUE;
3390 gui.boldital_font = get_font_handle(&lf);
3391 }
3392 }
3393
3394 return OK;
3395}
3396
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397/*
3398 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003399 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400 */
3401 int
3402gui_mch_maximized(void)
3403{
3404 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003405 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406
3407 wp.length = sizeof(WINDOWPLACEMENT);
3408 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003409 {
3410 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003412 && wp.flags == WPF_RESTORETOMAXIMIZED))
3413 return TRUE;
3414 if (wp.showCmd == SW_SHOWMINIMIZED)
3415 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003417 // Assume the window is snapped when the sizes from two APIs differ.
3418 GetWindowRect(s_hwnd, &rc);
3419 if ((rc.right - rc.left !=
3420 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3421 || (rc.bottom - rc.top !=
3422 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3423 return TRUE;
3424 }
3425 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426}
3427
3428/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003429 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3430 * is set. Compute the new Rows and Columns. This is like resizing the
3431 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432 */
3433 void
3434gui_mch_newfont(void)
3435{
3436 RECT rect;
3437
3438 GetWindowRect(s_hwnd, &rect);
3439 if (win_socket_id == 0)
3440 {
3441 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003442 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3443 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003445 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3446 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3447 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3448 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003449 }
3450 else
3451 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003452 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003453 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003454 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003455 }
3456}
3457
3458/*
3459 * Set the window title
3460 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461 void
3462gui_mch_settitle(
3463 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003464 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465{
3466 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3467}
3468
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003469#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003470// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3471// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472static LPCSTR mshape_idcs[] =
3473{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003474 IDC_ARROW, // arrow
3475 MAKEINTRESOURCE(0), // blank
3476 IDC_IBEAM, // beam
3477 IDC_SIZENS, // updown
3478 IDC_SIZENS, // udsizing
3479 IDC_SIZEWE, // leftright
3480 IDC_SIZEWE, // lrsizing
3481 IDC_WAIT, // busy
3482 IDC_NO, // no
3483 IDC_ARROW, // crosshair
3484 IDC_ARROW, // hand1
3485 IDC_ARROW, // hand2
3486 IDC_ARROW, // pencil
3487 IDC_ARROW, // question
3488 IDC_ARROW, // right-arrow
3489 IDC_UPARROW, // up-arrow
3490 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003491};
3492
3493 void
3494mch_set_mouse_shape(int shape)
3495{
3496 LPCSTR idc;
3497
3498 if (shape == MSHAPE_HIDE)
3499 ShowCursor(FALSE);
3500 else
3501 {
3502 if (shape >= MSHAPE_NUMBERED)
3503 idc = IDC_ARROW;
3504 else
3505 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003506 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 if (!p_mh)
3508 {
3509 POINT mp;
3510
Bram Moolenaar734a8672019-12-02 22:49:38 +01003511 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003512 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 (void)SetCursorPos(mp.x, mp.y);
3514 ShowCursor(TRUE);
3515 }
3516 }
3517}
3518#endif
3519
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003520#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003521/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522 * Wide version of convert_filter().
3523 */
3524 static WCHAR *
3525convert_filterW(char_u *s)
3526{
3527 char_u *tmp;
3528 int len;
3529 WCHAR *res;
3530
3531 tmp = convert_filter(s);
3532 if (tmp == NULL)
3533 return NULL;
3534 len = (int)STRLEN(s) + 3;
3535 res = enc_to_utf16(tmp, &len);
3536 vim_free(tmp);
3537 return res;
3538}
3539
3540/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003541 * Pop open a file browser and return the file selected, in allocated memory,
3542 * or NULL if Cancel is hit.
3543 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3544 * title - Title message for the file browser dialog.
3545 * dflt - Default name of file.
3546 * ext - Default extension to be added to files without extensions.
3547 * initdir - directory in which to open the browser (NULL = current dir)
3548 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003549 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003550 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003551gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003552 int saving,
3553 char_u *title,
3554 char_u *dflt,
3555 char_u *ext,
3556 char_u *initdir,
3557 char_u *filter)
3558{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003559 // We always use the wide function. This means enc_to_utf16() must work,
3560 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561 OPENFILENAMEW fileStruct;
3562 WCHAR fileBuf[MAXPATHL];
3563 WCHAR *wp;
3564 int i;
3565 WCHAR *titlep = NULL;
3566 WCHAR *extp = NULL;
3567 WCHAR *initdirp = NULL;
3568 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003569 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003570 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571
3572 if (dflt == NULL)
3573 fileBuf[0] = NUL;
3574 else
3575 {
3576 wp = enc_to_utf16(dflt, NULL);
3577 if (wp == NULL)
3578 fileBuf[0] = NUL;
3579 else
3580 {
3581 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3582 fileBuf[i] = wp[i];
3583 fileBuf[i] = NUL;
3584 vim_free(wp);
3585 }
3586 }
3587
Bram Moolenaar734a8672019-12-02 22:49:38 +01003588 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 filterp = convert_filterW(filter);
3590
Bram Moolenaara80faa82020-04-12 19:37:17 +02003591 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003592# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003593 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003594 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003595# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003597# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003598
3599 if (title != NULL)
3600 titlep = enc_to_utf16(title, NULL);
3601 fileStruct.lpstrTitle = titlep;
3602
3603 if (ext != NULL)
3604 extp = enc_to_utf16(ext, NULL);
3605 fileStruct.lpstrDefExt = extp;
3606
3607 fileStruct.lpstrFile = fileBuf;
3608 fileStruct.nMaxFile = MAXPATHL;
3609 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003610 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3611 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003612 if (initdir != NULL && *initdir != NUL)
3613 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003614 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 initdirp = enc_to_utf16(initdir, NULL);
3616 if (initdirp != NULL)
3617 {
3618 for (wp = initdirp; *wp != NUL; ++wp)
3619 if (*wp == '/')
3620 *wp = '\\';
3621 }
3622 fileStruct.lpstrInitialDir = initdirp;
3623 }
3624
3625 /*
3626 * TODO: Allow selection of multiple files. Needs another arg to this
3627 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3628 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3629 * files that don't exist yet, so I haven't put it in. What about
3630 * OFN_PATHMUSTEXIST?
3631 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3632 */
3633 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003634# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635 if (curbuf->b_p_bin)
3636 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003637# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003638 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003639 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003640 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003641 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003642
3643 vim_free(filterp);
3644 vim_free(initdirp);
3645 vim_free(titlep);
3646 vim_free(extp);
3647
K.Takata14b8d6a2022-01-20 15:05:22 +00003648 if (!ret)
3649 return NULL;
3650
3651 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003653 if (p == NULL)
3654 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655
Bram Moolenaar734a8672019-12-02 22:49:38 +01003656 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657 SetFocus(s_hwnd);
3658
Bram Moolenaar734a8672019-12-02 22:49:38 +01003659 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003660 q = vim_strsave(shorten_fname1(p));
3661 vim_free(p);
3662 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664
3665
3666/*
3667 * Convert the string s to the proper format for a filter string by replacing
3668 * the \t and \n delimiters with \0.
3669 * Returns the converted string in allocated memory.
3670 *
3671 * Keep in sync with convert_filterW() above!
3672 */
3673 static char_u *
3674convert_filter(char_u *s)
3675{
3676 char_u *res;
3677 unsigned s_len = (unsigned)STRLEN(s);
3678 unsigned i;
3679
3680 res = alloc(s_len + 3);
3681 if (res != NULL)
3682 {
3683 for (i = 0; i < s_len; ++i)
3684 if (s[i] == '\t' || s[i] == '\n')
3685 res[i] = '\0';
3686 else
3687 res[i] = s[i];
3688 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003689 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003690 res[s_len + 1] = NUL;
3691 res[s_len + 2] = NUL;
3692 }
3693 return res;
3694}
3695
3696/*
3697 * Select a directory.
3698 */
3699 char_u *
3700gui_mch_browsedir(char_u *title, char_u *initdir)
3701{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003702 // We fake this: Use a filter that doesn't select anything and a default
3703 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3705 initdir, (char_u *)_("Directory\t*.nothing\n"));
3706}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003707#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003708
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003709 static void
3710_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003711 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712 HDROP hDrop)
3713{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003714#define BUFPATHLEN _MAX_PATH
3715#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003717 char szFile[BUFPATHLEN];
3718 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3719 UINT i;
3720 char_u **fnames;
3721 POINT pt;
3722 int_u modifiers = 0;
3723
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725
Bram Moolenaar734a8672019-12-02 22:49:38 +01003726 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003727 DragQueryPoint(hDrop, &pt);
3728 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3729
3730 reset_VIsual();
3731
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003732 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733
3734 if (fnames != NULL)
3735 for (i = 0; i < cFiles; ++i)
3736 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3738 fnames[i] = utf16_to_enc(wszFile, NULL);
3739 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003740 {
3741 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3742 fnames[i] = vim_strsave((char_u *)szFile);
3743 }
3744 }
3745
3746 DragFinish(hDrop);
3747
3748 if (fnames != NULL)
3749 {
3750 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3751 modifiers |= MOUSE_SHIFT;
3752 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3753 modifiers |= MOUSE_CTRL;
3754 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3755 modifiers |= MOUSE_ALT;
3756
3757 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3758
3759 s_need_activate = TRUE;
3760 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761}
3762
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003763 static int
3764_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003765 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003766 HWND hwndCtl,
3767 UINT code,
3768 int pos)
3769{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003770 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 scrollbar_T *sb, *sb_info;
3772 long val;
3773 int dragging = FALSE;
3774 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775 SCROLLINFO si;
3776
3777 si.cbSize = sizeof(si);
3778 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779
3780 sb = gui_mswin_find_scrollbar(hwndCtl);
3781 if (sb == NULL)
3782 return 0;
3783
Bram Moolenaar734a8672019-12-02 22:49:38 +01003784 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785 {
3786 /*
3787 * Careful: need to get scrollbar info out of first (left) scrollbar
3788 * for window, but keep real scrollbar too because we must pass it to
3789 * gui_drag_scrollbar().
3790 */
3791 sb_info = &sb->wp->w_scrollbars[0];
3792 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003793 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794 sb_info = sb;
3795 val = sb_info->value;
3796
3797 switch (code)
3798 {
3799 case SB_THUMBTRACK:
3800 val = pos;
3801 dragging = TRUE;
3802 if (sb->scroll_shift > 0)
3803 val <<= sb->scroll_shift;
3804 break;
3805 case SB_LINEDOWN:
3806 val++;
3807 break;
3808 case SB_LINEUP:
3809 val--;
3810 break;
3811 case SB_PAGEDOWN:
3812 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3813 break;
3814 case SB_PAGEUP:
3815 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3816 break;
3817 case SB_TOP:
3818 val = 0;
3819 break;
3820 case SB_BOTTOM:
3821 val = sb_info->max;
3822 break;
3823 case SB_ENDSCROLL:
3824 if (prev_code == SB_THUMBTRACK)
3825 {
3826 /*
3827 * "pos" only gives us 16-bit data. In case of large file,
3828 * use GetScrollPos() which returns 32-bit. Unfortunately it
3829 * is not valid while the scrollbar is being dragged.
3830 */
3831 val = GetScrollPos(hwndCtl, SB_CTL);
3832 if (sb->scroll_shift > 0)
3833 val <<= sb->scroll_shift;
3834 }
3835 break;
3836
3837 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003838 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 return 0;
3840 }
3841 prev_code = code;
3842
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3844 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845
3846 /*
3847 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3848 */
3849 if (sb->wp != NULL)
3850 {
3851 scrollbar_T *sba = sb->wp->w_scrollbars;
3852 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3853
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003854 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 }
3856
Bram Moolenaar734a8672019-12-02 22:49:38 +01003857 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 s_busy_processing = TRUE;
3859
Bram Moolenaar734a8672019-12-02 22:49:38 +01003860 // When "allow_scrollbar" is FALSE still need to remember the new
3861 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 dont_scroll = !allow_scrollbar;
3863
Bram Moolenaara338adc2018-01-31 20:51:47 +01003864 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003865 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003866 mch_enable_flush();
3867 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003868
3869 s_busy_processing = FALSE;
3870 dont_scroll = dont_scroll_save;
3871
3872 return 0;
3873}
3874
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876#ifdef FEAT_XPM_W32
3877# include "xpm_w32.h"
3878#endif
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
Bram Moolenaar734a8672019-12-02 22:49:38 +01003881// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882#define TEAROFF_PADDING_X 2
3883#define TEAROFF_BUTTON_PAD_X 8
3884#define TEAROFF_MIN_WIDTH 200
3885#define TEAROFF_SUBMENU_LABEL ">>"
3886#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3887
3888
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003889#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890# define ID_BEVAL_TOOLTIP 200
3891# define BEVAL_TEXT_LEN MAXPATHL
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003894static UINT_PTR beval_timer_id = 0;
3895static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003896#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003897
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003898
Bram Moolenaar734a8672019-12-02 22:49:38 +01003899// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901#ifdef FEAT_MENU
3902static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905/*
3906 * Use the system font for dialogs and tear-off menus. Remove this line to
3907 * use DLG_FONT_NAME.
3908 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003909#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912#define VIM_CLASSW L"Vim"
3913
Bram Moolenaar734a8672019-12-02 22:49:38 +01003914// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3915// thus there should be room for every dialog. For tearoffs it's made bigger
3916// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#define DLG_ALLOC_SIZE 16 * 1024
3918
3919/*
3920 * stuff for dialogs, menus, tearoffs etc.
3921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922static PWORD
3923add_dialog_element(
3924 PWORD p,
3925 DWORD lStyle,
3926 WORD x,
3927 WORD y,
3928 WORD w,
3929 WORD h,
3930 WORD Id,
3931 WORD clss,
3932 const char *caption);
3933static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003934static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003935#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938static void get_dialog_font_metrics(void);
3939
3940static int dialog_default_button = -1;
3941
Bram Moolenaar734a8672019-12-02 22:49:38 +01003942// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945#ifdef FEAT_TOOLBAR
3946static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00003947static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003948static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00003950#else
3951# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#endif
3953
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003954#ifdef FEAT_GUI_TABLINE
3955static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003956static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003957#endif
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#ifdef FEAT_MBYTE_IME
3960static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
3961static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
3962#endif
3963#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
3964# ifdef NOIME
3965typedef struct tagCOMPOSITIONFORM {
3966 DWORD dwStyle;
3967 POINT ptCurrentPos;
3968 RECT rcArea;
3969} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
3970typedef HANDLE HIMC;
3971# endif
3972
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003973static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003974static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
3975static HIMC (WINAPI *pImmGetContext)(HWND);
3976static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
3977static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
3978static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
3979static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003980static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
3981static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003982static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
3983static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00003984static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985static void dyn_imm_load(void);
3986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987# define pImmGetCompositionStringW ImmGetCompositionStringW
3988# define pImmGetContext ImmGetContext
3989# define pImmAssociateContext ImmAssociateContext
3990# define pImmReleaseContext ImmReleaseContext
3991# define pImmGetOpenStatus ImmGetOpenStatus
3992# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003993# define pImmGetCompositionFontW ImmGetCompositionFontW
3994# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995# define pImmSetCompositionWindow ImmSetCompositionWindow
3996# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00003997# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#endif
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#ifdef FEAT_MENU
4001/*
4002 * Figure out how high the menu bar is at the moment.
4003 */
4004 static int
4005gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004006 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
4008 static int old_menu_height = -1;
4009
4010 RECT rc1, rc2;
4011 int num;
4012 int menu_height;
4013
4014 if (gui.menu_is_active)
4015 num = GetMenuItemCount(s_menuBar);
4016 else
4017 num = 0;
4018
4019 if (num == 0)
4020 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004021 else if (IsMinimized(s_hwnd))
4022 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004023 // The height of the menu cannot be determined while the window is
4024 // minimized. Take the previous height if the menu is changed in that
4025 // state, to avoid that Vim's vertical window size accidentally
4026 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004027 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 else
4030 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004031 /*
4032 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4033 * seem to have been set yet, so menu wraps in default window
4034 * width which is very narrow. Instead just return height of a
4035 * single menu item. Will still be wrong when the menu really
4036 * should wrap over more than one line.
4037 */
4038 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4039 if (gui.starting)
4040 menu_height = rc1.bottom - rc1.top + 1;
4041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004043 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4044 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 }
4047
4048 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004049 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004050 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 return menu_height;
4053}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004054#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056
4057/*
4058 * Setup for the Intellimouse
4059 */
4060 static void
4061init_mouse_wheel(void)
4062{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004063 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
Bram Moolenaar734a8672019-12-02 22:49:38 +01004065 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004066 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4067 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068}
4069
4070
Bram Moolenaarae20f342019-11-05 21:09:23 +01004071/*
4072 * Intellimouse wheel handler.
4073 * Treat a mouse wheel event as if it were a scroll request.
4074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 static void
4076_OnMouseWheel(
4077 HWND hwnd,
4078 short zDelta)
4079{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 int i;
4081 int size;
4082 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004083 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (mouse_scroll_lines == 0)
4086 init_mouse_wheel();
4087
Bram Moolenaarae20f342019-11-05 21:09:23 +01004088 wp = gui_mouse_window(FIND_POPUP);
4089
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004090#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004091 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004092 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004093 cmdarg_T cap;
4094 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004095
Bram Moolenaarae20f342019-11-05 21:09:23 +01004096 // Mouse hovers over popup window, scroll it if possible.
4097 mouse_row = wp->w_winrow;
4098 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004099 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004100 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4101 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4102 clear_oparg(&oa);
4103 cap.oap = &oa;
4104 nv_mousescroll(&cap);
4105 update_screen(0);
4106 setcursor();
4107 out_flush();
4108 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004109 }
4110#endif
4111
Bram Moolenaarae20f342019-11-05 21:09:23 +01004112 if (wp == NULL || !p_scf)
4113 wp = curwin;
4114
4115 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4116 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4117 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4118 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4119 else
4120 return;
4121 size = wp->w_height;
4122
Bram Moolenaara338adc2018-01-31 20:51:47 +01004123 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (mouse_scroll_lines > 0
4125 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4126 {
4127 for (i = mouse_scroll_lines; i > 0; --i)
4128 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4129 }
4130 else
4131 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004132 mch_enable_flush();
4133 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134}
4135
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004136#ifdef USE_SYSMENU_FONT
4137/*
4138 * Get Menu Font.
4139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004142gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004143{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004144 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004145
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004146 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4147 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004148 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004149 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004150 &nm,
4151 0))
4152 return FAIL;
4153 *lf = nm.lfMenuFont;
4154 return OK;
4155}
4156#endif
4157
4158
4159#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4160/*
4161 * Set the GUI tabline font to the system menu font
4162 */
4163 static void
4164set_tabline_font(void)
4165{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004166 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004167 HFONT font;
4168 HWND hwnd;
4169 HDC hdc;
4170 HFONT hfntOld;
4171 TEXTMETRIC tm;
4172
4173 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4174 return;
4175
K.Takatac81e9bf2022-01-16 14:15:49 +00004176 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004177 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004178
4179 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4180
4181 /*
4182 * Compute the height of the font used for the tab text
4183 */
4184 hwnd = GetDesktopWindow();
4185 hdc = GetWindowDC(hwnd);
4186 hfntOld = SelectFont(hdc, font);
4187
4188 GetTextMetrics(hdc, &tm);
4189
4190 SelectFont(hdc, hfntOld);
4191 ReleaseDC(hwnd, hdc);
4192
4193 /*
4194 * The space used by the tab border and the space between the tab label
4195 * and the tab border is included as 7.
4196 */
4197 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4198}
K.Takatac81e9bf2022-01-16 14:15:49 +00004199#else
4200# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004201#endif
4202
Bram Moolenaar520470a2005-06-16 21:59:56 +00004203/*
4204 * Invoked when a setting was changed.
4205 */
4206 static LRESULT CALLBACK
4207_OnSettingChange(UINT n)
4208{
4209 if (n == SPI_SETWHEELSCROLLLINES)
4210 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4211 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004212 if (n == SPI_SETNONCLIENTMETRICS)
4213 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004214 return 0;
4215}
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#ifdef FEAT_NETBEANS_INTG
4218 static void
4219_OnWindowPosChanged(
4220 HWND hwnd,
4221 const LPWINDOWPOS lpwpos)
4222{
4223 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004224 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4227 || lpwpos->cx != cx || lpwpos->cy != cy))
4228 {
4229 x = lpwpos->x;
4230 y = lpwpos->y;
4231 cx = lpwpos->cx;
4232 cy = lpwpos->cy;
4233 netbeans_frame_moved(x, y);
4234 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004235 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004236 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237}
4238#endif
4239
K.Takata4f2417f2021-06-05 16:25:32 +02004240
4241static HWND hwndTip = NULL;
4242
4243 static void
4244show_sizing_tip(int cols, int rows)
4245{
4246 TOOLINFOA ti = {sizeof(ti)};
4247 char buf[32];
4248
4249 ti.hwnd = s_hwnd;
4250 ti.uId = (UINT_PTR)s_hwnd;
4251 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4252 ti.lpszText = buf;
4253 sprintf(buf, "%dx%d", cols, rows);
4254 if (hwndTip == NULL)
4255 {
4256 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4257 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4258 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4259 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4260 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4261 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4262 }
4263 else
4264 {
4265 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4266 }
4267 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4268}
4269
4270 static void
4271destroy_sizing_tip(void)
4272{
4273 if (hwndTip != NULL)
4274 {
4275 DestroyWindow(hwndTip);
4276 hwndTip = NULL;
4277 }
4278}
4279
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 static int
4281_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 UINT fwSide,
4283 LPRECT lprc)
4284{
4285 int w, h;
4286 int valid_w, valid_h;
4287 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004288 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 w = lprc->right - lprc->left;
4291 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004292 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 w_offset = w - valid_w;
4294 h_offset = h - valid_h;
4295
4296 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4297 || fwSide == WMSZ_BOTTOMLEFT)
4298 lprc->left += w_offset;
4299 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4300 || fwSide == WMSZ_BOTTOMRIGHT)
4301 lprc->right -= w_offset;
4302
4303 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4304 || fwSide == WMSZ_TOPRIGHT)
4305 lprc->top += h_offset;
4306 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4307 || fwSide == WMSZ_BOTTOMRIGHT)
4308 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004309
4310 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 return TRUE;
4312}
4313
K.Takata92000e22022-01-20 15:10:57 +00004314#ifdef FEAT_GUI_TABLINE
4315 static void
4316_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4317{
4318 if (gui_mch_showing_tabline())
4319 {
4320 POINT pt;
4321 RECT rect;
4322
4323 /*
4324 * If the cursor is on the tabline, display the tab menu
4325 */
4326 GetCursorPos(&pt);
4327 GetWindowRect(s_textArea, &rect);
4328 if (pt.y < rect.top)
4329 {
4330 show_tabline_popup_menu();
4331 return;
4332 }
4333 }
4334 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4335}
4336
4337 static void
4338_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4339{
4340 /*
4341 * If the user double clicked the tabline, create a new tab
4342 */
4343 if (gui_mch_showing_tabline())
4344 {
4345 POINT pt;
4346 RECT rect;
4347
4348 GetCursorPos(&pt);
4349 GetWindowRect(s_textArea, &rect);
4350 if (pt.y < rect.top)
4351 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4352 }
4353 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4354}
4355#endif
4356
4357 static UINT
4358_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4359{
4360 UINT result;
4361 int x, y;
4362
4363 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4364 if (result != HTCLIENT)
4365 return result;
4366
4367#ifdef FEAT_GUI_TABLINE
4368 if (gui_mch_showing_tabline())
4369 {
4370 RECT rct;
4371
4372 // If the cursor is on the GUI tabline, don't process this event
4373 GetWindowRect(s_textArea, &rct);
4374 if (yPos < rct.top)
4375 return result;
4376 }
4377#endif
4378 (void)gui_mch_get_winpos(&x, &y);
4379 xPos -= x;
4380
4381 if (xPos < 48) // <VN> TODO should use system metric?
4382 return HTBOTTOMLEFT;
4383 else
4384 return HTBOTTOMRIGHT;
4385}
4386
4387#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4388 static LRESULT
4389_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4390{
4391 switch (hdr->code)
4392 {
4393 case TTN_GETDISPINFOW:
4394 case TTN_GETDISPINFO:
4395 {
4396 char_u *str = NULL;
4397 static void *tt_text = NULL;
4398
4399 VIM_CLEAR(tt_text);
4400
4401# ifdef FEAT_GUI_TABLINE
4402 if (gui_mch_showing_tabline()
4403 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4404 {
4405 POINT pt;
4406 /*
4407 * Mouse is over the GUI tabline. Display the
4408 * tooltip for the tab under the cursor
4409 *
4410 * Get the cursor position within the tab control
4411 */
4412 GetCursorPos(&pt);
4413 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4414 {
4415 TCHITTESTINFO htinfo;
4416 int idx;
4417
4418 /*
4419 * Get the tab under the cursor
4420 */
4421 htinfo.pt.x = pt.x;
4422 htinfo.pt.y = pt.y;
4423 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4424 if (idx != -1)
4425 {
4426 tabpage_T *tp;
4427
4428 tp = find_tabpage(idx + 1);
4429 if (tp != NULL)
4430 {
4431 get_tabline_label(tp, TRUE);
4432 str = NameBuff;
4433 }
4434 }
4435 }
4436 }
4437# endif
4438# ifdef FEAT_TOOLBAR
4439# ifdef FEAT_GUI_TABLINE
4440 else
4441# endif
4442 {
4443 UINT idButton;
4444 vimmenu_T *pMenu;
4445
4446 idButton = (UINT) hdr->idFrom;
4447 pMenu = gui_mswin_find_menu(root_menu, idButton);
4448 if (pMenu)
4449 str = pMenu->strings[MENU_INDEX_TIP];
4450 }
4451# endif
4452 if (str == NULL)
4453 break;
4454
4455 // Set the maximum width, this also enables using \n for
4456 // line break.
4457 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4458
4459 if (hdr->code == TTN_GETDISPINFOW)
4460 {
4461 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4462
4463 tt_text = enc_to_utf16(str, NULL);
4464 lpdi->lpszText = tt_text;
4465 // can't show tooltip if failed
4466 }
4467 else
4468 {
4469 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4470
4471 if (STRLEN(str) < sizeof(lpdi->szText)
4472 || ((tt_text = vim_strsave(str)) == NULL))
4473 vim_strncpy((char_u *)lpdi->szText, str,
4474 sizeof(lpdi->szText) - 1);
4475 else
4476 lpdi->lpszText = tt_text;
4477 }
4478 }
4479 break;
4480
4481# ifdef FEAT_GUI_TABLINE
4482 case TCN_SELCHANGE:
4483 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4484 {
4485 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4486 return 0L;
4487 }
4488 break;
4489
4490 case NM_RCLICK:
4491 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4492 {
4493 show_tabline_popup_menu();
4494 return 0L;
4495 }
4496 break;
4497# endif
4498
4499 default:
4500 break;
4501 }
4502 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4503}
4504#endif
4505
4506#if defined(MENUHINTS) && defined(FEAT_MENU)
4507 static LRESULT
4508_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4509{
4510 if (((UINT) HIWORD(wParam)
4511 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4512 == MF_HILITE
4513 && (State & CMDLINE) == 0)
4514 {
4515 UINT idButton;
4516 vimmenu_T *pMenu;
4517 static int did_menu_tip = FALSE;
4518
4519 if (did_menu_tip)
4520 {
4521 msg_clr_cmdline();
4522 setcursor();
4523 out_flush();
4524 did_menu_tip = FALSE;
4525 }
4526
4527 idButton = (UINT)LOWORD(wParam);
4528 pMenu = gui_mswin_find_menu(root_menu, idButton);
4529 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4530 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4531 {
4532 ++msg_hist_off;
4533 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4534 --msg_hist_off;
4535 setcursor();
4536 out_flush();
4537 did_menu_tip = TRUE;
4538 }
4539 return 0L;
4540 }
4541 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4542}
4543#endif
4544
K.Takatac81e9bf2022-01-16 14:15:49 +00004545 static LRESULT
4546_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4547{
4548 s_dpi = ydpi;
4549 s_in_dpichanged = TRUE;
4550 //TRACE("DPI: %d", ydpi);
4551
4552 update_scrollbar_size();
4553 update_toolbar_size();
4554 set_tabline_font();
4555
4556 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4557 gui_get_wide_font();
4558 gui_mswin_get_menu_height(FALSE);
4559#ifdef FEAT_MBYTE_IME
4560 im_set_position(gui.row, gui.col);
4561#endif
4562 InvalidateRect(hwnd, NULL, TRUE);
4563
4564 s_in_dpichanged = FALSE;
4565 return 0L;
4566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568
4569 static LRESULT CALLBACK
4570_WndProc(
4571 HWND hwnd,
4572 UINT uMsg,
4573 WPARAM wParam,
4574 LPARAM lParam)
4575{
4576 /*
4577 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4578 hwnd, uMsg, wParam, lParam);
4579 */
4580
4581 HandleMouseHide(uMsg, lParam);
4582
4583 s_uMsg = uMsg;
4584 s_wParam = wParam;
4585 s_lParam = lParam;
4586
4587 switch (uMsg)
4588 {
4589 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4590 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004591 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004593 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4595 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4596 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4597 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4598#ifdef FEAT_MENU
4599 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4600#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004601 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4602 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4604 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004605 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4606 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4608 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4609 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4610#ifdef FEAT_NETBEANS_INTG
4611 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4612#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004613#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004614 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4615 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004616#endif
K.Takata92000e22022-01-20 15:10:57 +00004617 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004618
Bram Moolenaar734a8672019-12-02 22:49:38 +01004619 case WM_QUERYENDSESSION: // System wants to go down.
4620 gui_shell_closed(); // Will exit when no changed buffers.
4621 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004624 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004627 return 0L;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630
4631 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004632 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4633 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004634 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 return 0L;
4636
4637 case WM_SYSCHAR:
4638 /*
4639 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4640 * shortcut key, handle like a typed ALT key, otherwise call Windows
4641 * ALT key handling.
4642 */
4643#ifdef FEAT_MENU
4644 if ( !gui.menu_is_active
4645 || p_wak[0] == 'n'
4646 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4647 )
4648#endif
4649 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004650 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 return 0L;
4652 }
4653#ifdef FEAT_MENU
4654 else
K.Takata4ac893f2022-01-20 12:44:28 +00004655 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#endif
4657
4658 case WM_SYSKEYUP:
4659#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004660 // This used to be done only when menu is active: ALT key is used for
4661 // that. But that caused problems when menu is disabled and using
4662 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4663 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004664 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004666 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
4668
K.Takata4f2417f2021-06-05 16:25:32 +02004669 case WM_EXITSIZEMOVE:
4670 destroy_sizing_tip();
4671 break;
4672
Bram Moolenaar734a8672019-12-02 22:49:38 +01004673 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
4676 case WM_MOUSEWHEEL:
4677 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004678 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar734a8672019-12-02 22:49:38 +01004680 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004681 case WM_SETTINGCHANGE:
4682 return _OnSettingChange((UINT)wParam);
4683
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004684#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004686 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#endif
K.Takata92000e22022-01-20 15:10:57 +00004688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689#if defined(MENUHINTS) && defined(FEAT_MENU)
4690 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004691 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
4694#ifdef FEAT_MBYTE_IME
4695 case WM_IME_NOTIFY:
4696 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004697 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004698 return 1L;
4699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 case WM_IME_COMPOSITION:
4701 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004702 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004703 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004705 case WM_DPICHANGED:
4706 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4707 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004711 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#endif
K.Takata92000e22022-01-20 15:10:57 +00004714 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716
K.Takata4ac893f2022-01-20 12:44:28 +00004717 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719
4720/*
4721 * End of call-back routines
4722 */
4723
Bram Moolenaar734a8672019-12-02 22:49:38 +01004724// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725HWND vim_parent_hwnd = NULL;
4726
4727 static BOOL CALLBACK
4728FindWindowTitle(HWND hwnd, LPARAM lParam)
4729{
4730 char buf[2048];
4731 char *title = (char *)lParam;
4732
4733 if (GetWindowText(hwnd, buf, sizeof(buf)))
4734 {
4735 if (strstr(buf, title) != NULL)
4736 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004737 // Found it. Store the window ref. and quit searching if MDI
4738 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004740 if (vim_parent_hwnd != NULL)
4741 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004744 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745}
4746
4747/*
4748 * Invoked for '-P "title"' argument: search for parent application to open
4749 * our window in.
4750 */
4751 void
4752gui_mch_set_parent(char *title)
4753{
4754 EnumWindows(FindWindowTitle, (LPARAM)title);
4755 if (vim_parent_hwnd == NULL)
4756 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004757 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 mch_exit(2);
4759 }
4760}
4761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 static void
4764ole_error(char *arg)
4765{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004766 char buf[IOSIZE];
4767
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004768# ifdef VIMDLL
4769 gui.in_use = mch_is_gui_executable();
4770# endif
4771
Bram Moolenaar734a8672019-12-02 22:49:38 +01004772 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004773 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004774 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004775 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004779#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4780 static char *
4781gvim_error(void)
4782{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004783 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004784
4785 if (starting)
4786 {
4787 mch_errmsg(msg);
4788 mch_errmsg("\n");
4789 mch_exit(2);
4790 }
4791 return msg;
4792}
4793
4794 char *
4795gui_mch_do_spawn(char_u *arg)
4796{
4797 int len;
4798# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4799 char_u *session = NULL;
4800 LPWSTR tofree1 = NULL;
4801# endif
4802 WCHAR name[MAX_PATH];
4803 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4804 STARTUPINFOW si = {sizeof(si)};
4805 PROCESS_INFORMATION pi;
4806
4807 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4808 goto error;
4809 p = wcsrchr(name, L'\\');
4810 if (p == NULL)
4811 goto error;
4812 // Replace the executable name from vim(d).exe to gvim(d).exe.
4813# ifdef DEBUG
4814 wcscpy(p + 1, L"gvimd.exe");
4815# else
4816 wcscpy(p + 1, L"gvim.exe");
4817# endif
4818
4819# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4820 if (starting)
4821# endif
4822 {
4823 // Pass the command line to the new process.
4824 p = GetCommandLineW();
4825 // Skip 1st argument.
4826 while (*p && *p != L' ' && *p != L'\t')
4827 {
4828 if (*p == L'"')
4829 {
4830 while (*p && *p != L'"')
4831 ++p;
4832 if (*p)
4833 ++p;
4834 }
4835 else
4836 ++p;
4837 }
4838 cmd = p;
4839 }
4840# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4841 else
4842 {
4843 // Create a session file and pass it to the new process.
4844 LPWSTR wsession;
4845 char_u *savebg;
4846 int ret;
4847
4848 session = vim_tempname('s', FALSE);
4849 if (session == NULL)
4850 goto error;
4851 savebg = p_bg;
4852 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4853 ret = write_session_file(session);
4854 vim_free(p_bg);
4855 p_bg = savebg;
4856 if (!ret)
4857 goto error;
4858 wsession = enc_to_utf16(session, NULL);
4859 if (wsession == NULL)
4860 goto error;
4861 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004862 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004863 if (cmd == NULL)
4864 {
4865 vim_free(wsession);
4866 goto error;
4867 }
4868 tofree1 = cmd;
4869 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4870 wsession, wsession);
4871 vim_free(wsession);
4872 }
4873# endif
4874
4875 // Check additional arguments to the `:gui` command.
4876 if (arg != NULL)
4877 {
4878 warg = enc_to_utf16(arg, NULL);
4879 if (warg == NULL)
4880 goto error;
4881 tofree2 = warg;
4882 }
4883 else
4884 warg = L"";
4885
4886 // Set up the new command line.
4887 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004888 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004889 if (newcmd == NULL)
4890 goto error;
4891 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4892
4893 // Spawn a new GUI process.
4894 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4895 NULL, NULL, &si, &pi))
4896 goto error;
4897 CloseHandle(pi.hProcess);
4898 CloseHandle(pi.hThread);
4899 mch_exit(0);
4900
4901error:
4902# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4903 if (session)
4904 mch_remove(session);
4905 vim_free(session);
4906 vim_free(tofree1);
4907# endif
4908 vim_free(newcmd);
4909 vim_free(tofree2);
4910 return gvim_error();
4911}
4912#endif
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914/*
4915 * Parse the GUI related command-line arguments. Any arguments used are
4916 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02004917 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 */
4919 void
4920gui_mch_prepare(int *argc, char **argv)
4921{
4922 int silent = FALSE;
4923 int idx;
4924
Bram Moolenaar734a8672019-12-02 22:49:38 +01004925 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4927 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004928 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4930 && (argv[2][0] == '-' || argv[2][0] == '/'))
4931 {
4932 silent = TRUE;
4933 idx = 2;
4934 }
4935 else
4936 idx = 1;
4937
Bram Moolenaar734a8672019-12-02 22:49:38 +01004938 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (STRICMP(argv[idx] + 1, "register") == 0)
4940 {
4941#ifdef FEAT_OLE
4942 RegisterMe(silent);
4943 mch_exit(0);
4944#else
4945 if (!silent)
4946 ole_error("register");
4947 mch_exit(2);
4948#endif
4949 }
4950
Bram Moolenaar734a8672019-12-02 22:49:38 +01004951 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4953 {
4954#ifdef FEAT_OLE
4955 UnregisterMe(!silent);
4956 mch_exit(0);
4957#else
4958 if (!silent)
4959 ole_error("unregister");
4960 mch_exit(2);
4961#endif
4962 }
4963
Bram Moolenaar734a8672019-12-02 22:49:38 +01004964 // Ignore an -embedding argument. It is only relevant if the
4965 // application wants to treat the case when it is started manually
4966 // differently from the case where it is started via automation (and
4967 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4969 {
4970#ifdef FEAT_OLE
4971 *argc = 1;
4972#else
4973 ole_error("embedding");
4974 mch_exit(2);
4975#endif
4976 }
4977 }
4978
4979#ifdef FEAT_OLE
4980 {
4981 int bDoRestart = FALSE;
4982
4983 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004984 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (bDoRestart)
4986 mch_exit(0);
4987 }
4988#endif
4989
4990#ifdef FEAT_NETBEANS_INTG
4991 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004992 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int arg;
4994
4995 for (arg = 1; arg < *argc; arg++)
4996 if (strncmp("-nb", argv[arg], 3) == 0)
4997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 netbeansArg = argv[arg];
4999 mch_memmove(&argv[arg], &argv[arg + 1],
5000 (--*argc - arg) * sizeof(char *));
5001 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005002 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006}
5007
K.Takatac81e9bf2022-01-16 14:15:49 +00005008 static void
5009load_dpi_func(void)
5010{
5011 HMODULE hUser32;
5012
5013 hUser32 = GetModuleHandle("user32.dll");
5014 if (hUser32 == NULL)
5015 goto fail;
5016
5017 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5018 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5019 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5020 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5021 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5022 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5023
5024 if (pSetThreadDpiAwarenessContext != NULL)
5025 {
5026 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5027 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5028 if (oldctx != NULL)
5029 {
5030 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5031 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5032#ifdef DEBUG
5033 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5034 {
5035 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5036 }
5037#endif
5038 return;
5039 }
5040 }
5041
5042fail:
5043 // Disable PerMonitorV2 APIs.
5044 pGetDpiForSystem = stubGetDpiForSystem;
5045 pGetDpiForWindow = NULL;
5046 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5047 pSetThreadDpiAwarenessContext = NULL;
5048 pGetAwarenessFromDpiAwarenessContext = NULL;
5049}
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051/*
5052 * Initialise the GUI. Create all the windows, set up all the call-backs
5053 * etc.
5054 */
5055 int
5056gui_mch_init(void)
5057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005059 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar734a8672019-12-02 22:49:38 +01005062 // Return here if the window was already opened (happens when
5063 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005065 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 /*
5068 * Load the tearoff bitmap
5069 */
5070#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005071 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072#endif
5073
K.Takatac81e9bf2022-01-16 14:15:49 +00005074 load_dpi_func();
5075
5076 s_dpi = pGetDpiForSystem();
5077 update_scrollbar_size();
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005080 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#endif
5082 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005083#ifdef FEAT_TOOLBAR
5084 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5088
Bram Moolenaar734a8672019-12-02 22:49:38 +01005089 // First try using the wide version, so that we can use any title.
5090 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005091 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005093 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 wndclassw.lpfnWndProc = _WndProc;
5095 wndclassw.cbClsExtra = 0;
5096 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005097 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5099 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5100 wndclassw.hbrBackground = s_brush;
5101 wndclassw.lpszMenuName = NULL;
5102 wndclassw.lpszClassName = szVimWndClassW;
5103
K.Takata4ac893f2022-01-20 12:44:28 +00005104 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005105 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (vim_parent_hwnd != NULL)
5109 {
5110#ifdef HAVE_TRY_EXCEPT
5111 __try
5112 {
5113#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005114 // Open inside the specified parent window.
5115 // TODO: last argument should point to a CLIENTCREATESTRUCT
5116 // structure.
5117 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005119 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005120 WS_OVERLAPPEDWINDOW | WS_CHILD
5121 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5123 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005124 100, // Any value will do
5125 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005127 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#ifdef HAVE_TRY_EXCEPT
5129 }
5130 __except(EXCEPTION_EXECUTE_HANDLER)
5131 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005132 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134#endif
5135 if (s_hwnd == NULL)
5136 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005137 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 mch_exit(2);
5139 }
5140 }
5141 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005142 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005143 // If the provided windowid is not valid reset it to zero, so that it
5144 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005145 if (IsWindow((HWND)win_socket_id) <= 0)
5146 win_socket_id = 0;
5147
Bram Moolenaar734a8672019-12-02 22:49:38 +01005148 // Create a window. If win_socket_id is not zero without border and
5149 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005150 s_hwnd = CreateWindowW(
5151 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005152 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5153 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005154 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5155 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005156 100, // Any value will do
5157 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005158 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005159 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005160 if (s_hwnd != NULL && win_socket_id != 0)
5161 {
5162 SetParent(s_hwnd, (HWND)win_socket_id);
5163 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5164 }
5165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167 if (s_hwnd == NULL)
5168 return FAIL;
5169
K.Takatac81e9bf2022-01-16 14:15:49 +00005170 if (pGetDpiForWindow != NULL)
5171 {
5172 s_dpi = pGetDpiForWindow(s_hwnd);
5173 update_scrollbar_size();
5174 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5175 }
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5178 dyn_imm_load();
5179#endif
5180
Bram Moolenaar734a8672019-12-02 22:49:38 +01005181 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005182 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005183 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005184 wndclassw.style = CS_OWNDC;
5185 wndclassw.lpfnWndProc = _TextAreaWndProc;
5186 wndclassw.cbClsExtra = 0;
5187 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005188 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005189 wndclassw.hIcon = NULL;
5190 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5191 wndclassw.hbrBackground = NULL;
5192 wndclassw.lpszMenuName = NULL;
5193 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005194
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005195 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 return FAIL;
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005199 s_textArea = CreateWindowExW(
5200 0,
5201 szTextAreaClassW, L"Vim text area",
5202 WS_CHILD | WS_VISIBLE, 0, 0,
5203 100, // Any value will do for now
5204 100, // Any value will do for now
5205 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005206 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (s_textArea == NULL)
5209 return FAIL;
5210
Bram Moolenaar20321902016-02-17 12:30:17 +01005211#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005212 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005213 {
5214 HANDLE hIcon = NULL;
5215
5216 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005217 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005218 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005219#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#ifdef FEAT_MENU
5222 s_menuBar = CreateMenu();
5223#endif
5224 s_hdc = GetDC(s_textArea);
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
Bram Moolenaar734a8672019-12-02 22:49:38 +01005228 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005229 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
Bram Moolenaar734a8672019-12-02 22:49:38 +01005231 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 gui_mch_def_colors();
5233
Bram Moolenaar734a8672019-12-02 22:49:38 +01005234 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5235 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 set_normal_colors();
5237
5238 /*
5239 * Check that none of the colors are the same as the background color.
5240 * Then store the current values as the defaults.
5241 */
5242 gui_check_colors();
5243 gui.def_norm_pixel = gui.norm_pixel;
5244 gui.def_back_pixel = gui.back_pixel;
5245
Bram Moolenaar734a8672019-12-02 22:49:38 +01005246 // Get the colors for the highlight groups (gui_check_colors() might have
5247 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 highlight_gui_started();
5249
5250 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005251 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005253 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
5255 /*
5256 * Set up for Intellimouse processing
5257 */
5258 init_mouse_wheel();
5259
5260 /*
5261 * compute a couple of metrics used for the dialogs
5262 */
5263 get_dialog_font_metrics();
5264#ifdef FEAT_TOOLBAR
5265 /*
5266 * Create the toolbar
5267 */
5268 initialise_toolbar();
5269#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005270#ifdef FEAT_GUI_TABLINE
5271 /*
5272 * Create the tabline
5273 */
5274 initialise_tabline();
5275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#ifdef MSWIN_FIND_REPLACE
5277 /*
5278 * Initialise the dialog box stuff
5279 */
5280 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5281
Bram Moolenaar734a8672019-12-02 22:49:38 +01005282 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005284 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005286 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5288 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5289 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005292#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005293 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005294 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005295#endif
5296
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005297#ifdef FEAT_RENDER_OPTIONS
5298 if (p_rop)
5299 (void)gui_mch_set_rendering_options(p_rop);
5300#endif
5301
Bram Moolenaar748bf032005-02-02 23:04:36 +00005302theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005303 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005304 display_errors();
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 return OK;
5307}
5308
5309/*
5310 * Get the size of the screen, taking position on multiple monitors into
5311 * account (if supported).
5312 */
5313 static void
5314get_work_area(RECT *spi_rect)
5315{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005316 HMONITOR mon;
5317 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar734a8672019-12-02 22:49:38 +01005319 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005320 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005321 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005323 moninfo.cbSize = sizeof(MONITORINFO);
5324 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005326 *spi_rect = moninfo.rcWork;
5327 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
5329 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005330 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5332}
5333
5334/*
5335 * Set the size of the window to the given width and height in pixels.
5336 */
5337 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005338gui_mch_set_shellsize(
5339 int width,
5340 int height,
5341 int min_width UNUSED,
5342 int min_height UNUSED,
5343 int base_width UNUSED,
5344 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005345 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005348 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar734a8672019-12-02 22:49:38 +01005351 // Try to keep window completely on screen.
5352 // Get position of the screen work area. This is the part that is not
5353 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 get_work_area(&workarea_rect);
5355
Bram Moolenaar734a8672019-12-02 22:49:38 +01005356 // Resizing a maximized window looks very strange, unzoom it first.
5357 // But don't do it when still starting up, it may have been requested in
5358 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005359 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005361
5362 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
Bram Moolenaar734a8672019-12-02 22:49:38 +01005364 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005365 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5366 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5367 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5368 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5369 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5370 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
Bram Moolenaar734a8672019-12-02 22:49:38 +01005372 // The following should take care of keeping Vim on the same monitor, no
5373 // matter if the secondary monitor is left or right of the primary
5374 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005375 window_rect.right = window_rect.left + win_width;
5376 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005377
Bram Moolenaar734a8672019-12-02 22:49:38 +01005378 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005379 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5380 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005382 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5383 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005385 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5386 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005388 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5389 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005391 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5392 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
5394 SetActiveWindow(s_hwnd);
5395 SetFocus(s_hwnd);
5396
Bram Moolenaar734a8672019-12-02 22:49:38 +01005397 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
5400
5401
5402 void
5403gui_mch_set_scrollbar_thumb(
5404 scrollbar_T *sb,
5405 long val,
5406 long size,
5407 long max)
5408{
5409 SCROLLINFO info;
5410
5411 sb->scroll_shift = 0;
5412 while (max > 32767)
5413 {
5414 max = (max + 1) >> 1;
5415 val >>= 1;
5416 size >>= 1;
5417 ++sb->scroll_shift;
5418 }
5419
5420 if (sb->scroll_shift > 0)
5421 ++size;
5422
5423 info.cbSize = sizeof(info);
5424 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5425 info.nPos = val;
5426 info.nMin = 0;
5427 info.nMax = max;
5428 info.nPage = size;
5429 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5430}
5431
5432
5433/*
5434 * Set the current text font.
5435 */
5436 void
5437gui_mch_set_font(GuiFont font)
5438{
5439 gui.currFont = font;
5440}
5441
5442
5443/*
5444 * Set the current text foreground color.
5445 */
5446 void
5447gui_mch_set_fg_color(guicolor_T color)
5448{
5449 gui.currFgColor = color;
5450}
5451
5452/*
5453 * Set the current text background color.
5454 */
5455 void
5456gui_mch_set_bg_color(guicolor_T color)
5457{
5458 gui.currBgColor = color;
5459}
5460
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005461/*
5462 * Set the current text special color.
5463 */
5464 void
5465gui_mch_set_sp_color(guicolor_T color)
5466{
5467 gui.currSpColor = color;
5468}
5469
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005470#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471/*
5472 * Multi-byte handling, originally by Sung-Hoon Baek.
5473 * First static functions (no prototypes generated).
5474 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005475# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005476# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005477# endif
5478# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
5480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 * handle WM_IME_NOTIFY message
5482 */
5483 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005484_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 LRESULT lResult = 0;
5487 HIMC hImc;
5488
5489 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5490 return lResult;
5491 switch (dwCommand)
5492 {
5493 case IMN_SETOPENSTATUS:
5494 if (pImmGetOpenStatus(hImc))
5495 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005496 LOGFONTW lf = norm_logfont;
5497 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5498 // Work around when PerMonitorV2 is not enabled in the process level.
5499 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5500 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 im_set_position(gui.row, gui.col);
5502
Bram Moolenaar734a8672019-12-02 22:49:38 +01005503 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 State &= ~LANGMAP;
5505 if (State & INSERT)
5506 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005507# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005508 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5510 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005511 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 int old_row = gui.row;
5513 int old_col = gui.col;
5514
5515 // This must be called here before
5516 // status_redraw_curbuf(), otherwise the mode
5517 // message may appear in the wrong position.
5518 showmode();
5519 status_redraw_curbuf();
5520 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005521 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 gui.row = old_row;
5523 gui.col = old_col;
5524 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527 }
5528 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005529 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 lResult = 0;
5531 break;
5532 }
5533 pImmReleaseContext(hWnd, hImc);
5534 return lResult;
5535}
5536
5537 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005538_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 char_u *ret;
5541 int len;
5542
Bram Moolenaar734a8672019-12-02 22:49:38 +01005543 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 return 0;
5545
5546 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5547 if (ret != NULL)
5548 {
5549 add_to_input_buf_csi(ret, len);
5550 vim_free(ret);
5551 return 1;
5552 }
5553 return 0;
5554}
5555
5556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * void GetResultStr()
5558 *
5559 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5560 * get complete composition string
5561 */
5562 static char_u *
5563GetResultStr(HWND hwnd, int GCS, int *lenp)
5564{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005565 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005566 LONG ret;
5567 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *convbuf = NULL;
5569
5570 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5571 return NULL;
5572
K.Takatab0b2b732022-01-19 12:59:21 +00005573 // Get the length of the composition string.
5574 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5575 if (ret <= 0)
5576 return NULL;
5577
5578 // Allocate the requested buffer plus space for the NUL character.
5579 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 if (buf == NULL)
5581 return NULL;
5582
K.Takatab0b2b732022-01-19 12:59:21 +00005583 // Reads in the composition string.
5584 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5585 *lenp = ret / sizeof(WCHAR);
5586
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005587 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 pImmReleaseContext(hwnd, hIMC);
5589 vim_free(buf);
5590 return convbuf;
5591}
5592#endif
5593
Bram Moolenaar734a8672019-12-02 22:49:38 +01005594// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005595#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
5597/*
5598 * set font to IM.
5599 */
5600 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005601im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 HIMC hImc;
5604
5605 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5606 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005607 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 pImmReleaseContext(s_hwnd, hImc);
5609 }
5610}
5611
5612/*
5613 * Notify cursor position to IM.
5614 */
5615 void
5616im_set_position(int row, int col)
5617{
5618 HIMC hImc;
5619
5620 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5621 {
5622 COMPOSITIONFORM cfs;
5623
5624 cfs.dwStyle = CFS_POINT;
5625 cfs.ptCurrentPos.x = FILL_X(col);
5626 cfs.ptCurrentPos.y = FILL_Y(row);
5627 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005628 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5629 {
5630 // Work around when PerMonitorV2 is not enabled in the process level.
5631 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5632 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 pImmSetCompositionWindow(hImc, &cfs);
5635
5636 pImmReleaseContext(s_hwnd, hImc);
5637 }
5638}
5639
5640/*
5641 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5642 */
5643 void
5644im_set_active(int active)
5645{
5646 HIMC hImc;
5647 static HIMC hImcOld = (HIMC)0;
5648
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005649# ifdef VIMDLL
5650 if (!gui.in_use && !gui.starting)
5651 {
5652 mbyte_im_set_active(active);
5653 return;
5654 }
5655# endif
5656
Bram Moolenaar734a8672019-12-02 22:49:38 +01005657 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 if (p_imdisable)
5660 {
5661 if (hImcOld == (HIMC)0)
5662 {
5663 hImcOld = pImmGetContext(s_hwnd);
5664 if (hImcOld)
5665 pImmAssociateContext(s_hwnd, (HIMC)0);
5666 }
5667 active = FALSE;
5668 }
5669 else if (hImcOld != (HIMC)0)
5670 {
5671 pImmAssociateContext(s_hwnd, hImcOld);
5672 hImcOld = (HIMC)0;
5673 }
5674
5675 hImc = pImmGetContext(s_hwnd);
5676 if (hImc)
5677 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005678 /*
5679 * for Korean ime
5680 */
5681 HKL hKL = GetKeyboardLayout(0);
5682
5683 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5684 {
5685 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5686 static BOOL bSaved = FALSE;
5687
5688 if (active)
5689 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005690 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005691 if (bSaved)
5692 pImmSetConversionStatus(hImc, dwConversionSaved,
5693 dwSentenceSaved);
5694 bSaved = FALSE;
5695 }
5696 else
5697 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005698 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005699 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5700 &dwSentenceSaved))
5701 {
5702 bSaved = TRUE;
5703 pImmSetConversionStatus(hImc,
5704 dwConversionSaved & ~(IME_CMODE_NATIVE
5705 | IME_CMODE_FULLSHAPE),
5706 dwSentenceSaved);
5707 }
5708 }
5709 }
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 pImmSetOpenStatus(hImc, active);
5712 pImmReleaseContext(s_hwnd, hImc);
5713 }
5714 }
5715}
5716
5717/*
5718 * Get IM status. When IM is on, return not 0. Else return 0.
5719 */
5720 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005721im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 int status = 0;
5724 HIMC hImc;
5725
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005726# ifdef VIMDLL
5727 if (!gui.in_use && !gui.starting)
5728 return mbyte_im_get_status();
5729# endif
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5732 {
5733 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5734 pImmReleaseContext(s_hwnd, hImc);
5735 }
5736 return status;
5737}
5738
Bram Moolenaar734a8672019-12-02 22:49:38 +01005739#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005742/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005743 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005744 */
5745 static void
5746latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5747{
5748 int c;
5749
Bram Moolenaarca003e12006-03-17 23:19:38 +00005750 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005751 {
5752 c = *text++;
5753 switch (c)
5754 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005755 case 0xa4: c = 0x20ac; break; // euro
5756 case 0xa6: c = 0x0160; break; // S hat
5757 case 0xa8: c = 0x0161; break; // S -hat
5758 case 0xb4: c = 0x017d; break; // Z hat
5759 case 0xb8: c = 0x017e; break; // Z -hat
5760 case 0xbc: c = 0x0152; break; // OE
5761 case 0xbd: c = 0x0153; break; // oe
5762 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005763 }
5764 *unicodebuf++ = c;
5765 }
5766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768#ifdef FEAT_RIGHTLEFT
5769/*
5770 * What is this for? In the case where you are using Win98 or Win2K or later,
5771 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5772 * reverses the string sent to the TextOut... family. This sucks, because we
5773 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5774 * way to tell Windblows not to do this!
5775 *
5776 * The short of it is that this 'RevOut' only gets called if you are running
5777 * one of the new, "improved" MS OSes, and only if you are running in
5778 * 'rightleft' mode. It makes display take *slightly* longer, but not
5779 * noticeably so.
5780 */
5781 static void
K.Takata135e1522022-01-29 15:27:58 +00005782RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 int col,
5784 int row,
5785 UINT foptions,
5786 CONST RECT *pcliprect,
5787 LPCTSTR text,
5788 UINT len,
5789 CONST INT *padding)
5790{
5791 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005793 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005794 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005795 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796}
5797#endif
5798
Bram Moolenaar92467d32017-12-05 13:22:16 +01005799 static void
5800draw_line(
5801 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005802 int y1,
5803 int x2,
5804 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005805 COLORREF color)
5806{
5807#if defined(FEAT_DIRECTX)
5808 if (IS_ENABLE_DIRECTX())
5809 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5810 else
5811#endif
5812 {
5813 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5814 HPEN old_pen = SelectObject(s_hdc, hpen);
5815 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005816 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005817 LineTo(s_hdc, x2, y2);
5818 DeleteObject(SelectObject(s_hdc, old_pen));
5819 }
5820}
5821
5822 static void
5823set_pixel(
5824 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005825 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005826 COLORREF color)
5827{
5828#if defined(FEAT_DIRECTX)
5829 if (IS_ENABLE_DIRECTX())
5830 DWriteContext_SetPixel(s_dwc, x, y, color);
5831 else
5832#endif
5833 SetPixel(s_hdc, x, y, color);
5834}
5835
5836 static void
5837fill_rect(
5838 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005839 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005840 COLORREF color)
5841{
5842#if defined(FEAT_DIRECTX)
5843 if (IS_ENABLE_DIRECTX())
5844 DWriteContext_FillRect(s_dwc, rcp, color);
5845 else
5846#endif
5847 {
5848 HBRUSH hbr2;
5849
5850 if (hbr == NULL)
5851 hbr2 = CreateSolidBrush(color);
5852 else
5853 hbr2 = hbr;
5854 FillRect(s_hdc, rcp, hbr2);
5855 if (hbr == NULL)
5856 DeleteBrush(hbr2);
5857 }
5858}
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 void
5861gui_mch_draw_string(
5862 int row,
5863 int col,
5864 char_u *text,
5865 int len,
5866 int flags)
5867{
5868 static int *padding = NULL;
5869 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 const RECT *pcliprect = NULL;
5871 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 static WCHAR *unicodebuf = NULL;
5873 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005874 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 int y;
5877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 /*
5879 * Italic and bold text seems to have an extra row of pixels at the bottom
5880 * (below where the bottom of the character should be). If we draw the
5881 * characters with a solid background, the top row of pixels in the
5882 * character below will be overwritten. We can fix this by filling in the
5883 * background ourselves, to the correct character proportions, and then
5884 * writing the character in transparent mode. Still have a problem when
5885 * the character is "_", which gets written on to the character below.
5886 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5887 * pixel in their slots, which fixes the problem with the bottom row of
5888 * pixels. We still need this code because otherwise the top row of pixels
5889 * becomes a problem. - webb.
5890 */
5891 static HBRUSH hbr_cache[2] = {NULL, NULL};
5892 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5893 static int brush_lru = 0;
5894 HBRUSH hbr;
5895 RECT rc;
5896
5897 if (!(flags & DRAW_TRANSP))
5898 {
5899 /*
5900 * Clear background first.
5901 * Note: FillRect() excludes right and bottom of rectangle.
5902 */
5903 rc.left = FILL_X(col);
5904 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (has_mbyte)
5906 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005907 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005908 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 rc.right = FILL_X(col + len);
5912 rc.bottom = FILL_Y(row + 1);
5913
Bram Moolenaar734a8672019-12-02 22:49:38 +01005914 // Cache the created brush, that saves a lot of time. We need two:
5915 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (gui.currBgColor == brush_color[0])
5917 {
5918 hbr = hbr_cache[0];
5919 brush_lru = 1;
5920 }
5921 else if (gui.currBgColor == brush_color[1])
5922 {
5923 hbr = hbr_cache[1];
5924 brush_lru = 0;
5925 }
5926 else
5927 {
5928 if (hbr_cache[brush_lru] != NULL)
5929 DeleteBrush(hbr_cache[brush_lru]);
5930 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5931 brush_color[brush_lru] = gui.currBgColor;
5932 hbr = hbr_cache[brush_lru];
5933 brush_lru = !brush_lru;
5934 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005935
Bram Moolenaar92467d32017-12-05 13:22:16 +01005936 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
5938 SetBkMode(s_hdc, TRANSPARENT);
5939
5940 /*
5941 * When drawing block cursor, prevent inverted character spilling
5942 * over character cell (can happen with bold/italic)
5943 */
5944 if (flags & DRAW_CURSOR)
5945 {
5946 pcliprect = &rc;
5947 foptions = ETO_CLIPPED;
5948 }
5949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 SetTextColor(s_hdc, gui.currFgColor);
5951 SelectFont(s_hdc, gui.currFont);
5952
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005953#ifdef FEAT_DIRECTX
5954 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005955 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005956#endif
5957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5959 {
K.Takata135e1522022-01-29 15:27:58 +00005960 int i;
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 vim_free(padding);
5963 pad_size = Columns;
5964
Bram Moolenaar734a8672019-12-02 22:49:38 +01005965 // Don't give an out-of-memory message here, it would call us
5966 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02005967 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (padding != NULL)
5969 for (i = 0; i < pad_size; i++)
5970 padding[i] = gui.char_width;
5971 }
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 /*
5974 * We have to provide the padding argument because italic and bold versions
5975 * of fixed-width fonts are often one pixel or so wider than their normal
5976 * versions.
5977 * No check for DRAW_BOLD, Windows will have done it already.
5978 */
5979
Bram Moolenaar734a8672019-12-02 22:49:38 +01005980 // Check if there are any UTF-8 characters. If not, use normal text
5981 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 if (enc_utf8)
5983 for (n = 0; n < len; ++n)
5984 if (text[n] >= 0x80)
5985 break;
5986
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005987#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005988 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5989 // required that unicode drawing routine, currently. So this forces it
5990 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005991 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01005992 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005993#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005994
Bram Moolenaar734a8672019-12-02 22:49:38 +01005995 // Check if the Unicode buffer exists and is big enough. Create it
5996 // with the same length as the multi-byte string, the number of wide
5997 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005998 if ((enc_utf8
5999 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6000 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 && (unicodebuf == NULL || len > unibuflen))
6002 {
6003 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006004 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006007 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
6009 unibuflen = len;
6010 }
6011
6012 if (enc_utf8 && n < len && unicodebuf != NULL)
6013 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006014 // Output UTF-8 characters. Composing characters should be
6015 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006016 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006017 int wlen; // string length in words
6018 int clen; // string length in characters
6019 int cells; // cell width of string up to composing char
6020 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006021 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006023 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006024 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006026 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006028 c = utf_ptr2char(text + i);
6029 if (c >= 0x10000)
6030 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006031 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006032 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6033 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034 }
6035 else
6036 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006037 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006039
6040 if (utf_iscomposing(c))
6041 cw = 0;
6042 else
6043 {
6044 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006045 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006046 cw = 1;
6047 }
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 if (unicodepdy != NULL)
6050 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006051 // Use unicodepdy to make characters fit as we expect, even
6052 // when the font uses different widths (e.g., bold character
6053 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006054 if (c >= 0x10000)
6055 {
6056 unicodepdy[wlen - 2] = cw * gui.char_width;
6057 unicodepdy[wlen - 1] = 0;
6058 }
6059 else
6060 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 }
6062 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006063 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006064 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006066#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006067 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006068 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006069 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006070 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006071 TEXT_X(col), TEXT_Y(row),
6072 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006073 gui.char_width, gui.currFgColor,
6074 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006075 }
6076 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006077#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006078 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6079 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006080 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006082 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006084 // If we want to display codepage data, and the current CP is not the
6085 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (unicodebuf != NULL)
6087 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006088 if (enc_latin9)
6089 latin9_to_ucs(text, len, unicodebuf);
6090 else
6091 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 MB_PRECOMPOSED,
6093 (char *)text, len,
6094 (LPWSTR)unicodebuf, unibuflen);
6095 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006096 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006097 // Use unicodepdy to make characters fit as we expect, even
6098 // when the font uses different widths (e.g., bold character
6099 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006100 if (unicodepdy != NULL)
6101 {
6102 int i;
6103 int cw;
6104
6105 for (i = 0; i < len; ++i)
6106 {
6107 cw = utf_char2cells(unicodebuf[i]);
6108 if (cw > 2)
6109 cw = 1;
6110 unicodepdy[i] = cw * gui.char_width;
6111 }
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006114 foptions, pcliprect, unicodebuf, len, unicodepdy);
6115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117 }
6118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006121 // Windows will mess up RL text, so we have to draw it character by
6122 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006123 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6125 foptions, pcliprect, (char *)text, len, padding);
6126 else
6127#endif
6128 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6129 foptions, pcliprect, (char *)text, len, padding);
6130 }
6131
Bram Moolenaar734a8672019-12-02 22:49:38 +01006132 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (flags & DRAW_UNDERL)
6134 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006135 // When p_linespace is 0, overwrite the bottom row of pixels.
6136 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (p_linespace > 1)
6139 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006140 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006142
Bram Moolenaar734a8672019-12-02 22:49:38 +01006143 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006144 if (flags & DRAW_STRIKE)
6145 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006146 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006147 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006148 }
6149
Bram Moolenaar734a8672019-12-02 22:49:38 +01006150 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006151 if (flags & DRAW_UNDERC)
6152 {
6153 int x;
6154 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006155 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006156
6157 y = FILL_Y(row + 1) - 1;
6158 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6159 {
6160 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006161 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006162 }
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164}
6165
6166
6167/*
6168 * Output routines.
6169 */
6170
Bram Moolenaar734a8672019-12-02 22:49:38 +01006171/*
6172 * Flush any output to the screen
6173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 void
6175gui_mch_flush(void)
6176{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006177#if defined(FEAT_DIRECTX)
6178 if (IS_ENABLE_DIRECTX())
6179 DWriteContext_Flush(s_dwc);
6180#endif
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 GdiFlush();
6183}
6184
6185 static void
6186clear_rect(RECT *rcp)
6187{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006188 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189}
6190
6191
Bram Moolenaarc716c302006-01-21 22:12:51 +00006192 void
6193gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6194{
6195 RECT workarea_rect;
6196
6197 get_work_area(&workarea_rect);
6198
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006199 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006200 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6201 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006202
Bram Moolenaar734a8672019-12-02 22:49:38 +01006203 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6204 // the menubar for MSwin, we subtract it from the screen height, so that
6205 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006206 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006207 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6208 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6209 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6210 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006211}
6212
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214#if defined(FEAT_MENU) || defined(PROTO)
6215/*
6216 * Add a sub menu to the menu bar.
6217 */
6218 void
6219gui_mch_add_menu(
6220 vimmenu_T *menu,
6221 int pos)
6222{
6223 vimmenu_T *parent = menu->parent;
6224
6225 menu->submenu_id = CreatePopupMenu();
6226 menu->id = s_menu_id++;
6227
6228 if (menu_is_menubar(menu->name))
6229 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006230 WCHAR *wn;
6231 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006233 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006234 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006235 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006237 infow.cbSize = sizeof(infow);
6238 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6239 | MIIM_SUBMENU;
6240 infow.dwItemData = (long_u)menu;
6241 infow.wID = menu->id;
6242 infow.fType = MFT_STRING;
6243 infow.dwTypeData = wn;
6244 infow.cch = (UINT)wcslen(wn);
6245 infow.hSubMenu = menu->submenu_id;
6246 InsertMenuItemW((parent == NULL)
6247 ? s_menuBar : parent->submenu_id,
6248 (UINT)pos, TRUE, &infow);
6249 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251
Bram Moolenaar734a8672019-12-02 22:49:38 +01006252 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (parent == NULL)
6254 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006255# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 else if (IsWindow(parent->tearoff_handle))
6257 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259}
6260
6261 void
6262gui_mch_show_popupmenu(vimmenu_T *menu)
6263{
6264 POINT mp;
6265
K.Takata45f9cfb2022-01-21 11:11:00 +00006266 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6268}
6269
6270 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006271gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 vimmenu_T *menu = gui_find_menu(path_name);
6274
6275 if (menu != NULL)
6276 {
6277 POINT p;
6278
Bram Moolenaar734a8672019-12-02 22:49:38 +01006279 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006281 if (mouse_pos)
6282 {
6283 int mx, my;
6284
6285 gui_mch_getmouse(&mx, &my);
6286 p.x += mx;
6287 p.y += my;
6288 }
6289 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006291 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6293 }
6294 msg_scroll = FALSE;
6295 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6296 }
6297}
6298
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006299# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300/*
6301 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6302 * create it as a pseudo-"tearoff menu".
6303 */
6304 void
6305gui_make_tearoff(char_u *path_name)
6306{
6307 vimmenu_T *menu = gui_find_menu(path_name);
6308
Bram Moolenaar734a8672019-12-02 22:49:38 +01006309 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (menu != NULL)
6311 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6312}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315/*
6316 * Add a menu item to a menu
6317 */
6318 void
6319gui_mch_add_menu_item(
6320 vimmenu_T *menu,
6321 int idx)
6322{
6323 vimmenu_T *parent = menu->parent;
6324
6325 menu->id = s_menu_id++;
6326 menu->submenu_id = NULL;
6327
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006328# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6330 {
6331 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6332 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6333 }
6334 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006335# endif
6336# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (menu_is_toolbar(parent->name))
6338 {
6339 TBBUTTON newtb;
6340
Bram Moolenaara80faa82020-04-12 19:37:17 +02006341 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (menu_is_separator(menu->name))
6343 {
6344 newtb.iBitmap = 0;
6345 newtb.fsStyle = TBSTYLE_SEP;
6346 }
6347 else
6348 {
6349 newtb.iBitmap = get_toolbar_bitmap(menu);
6350 newtb.fsStyle = TBSTYLE_BUTTON;
6351 }
6352 newtb.idCommand = menu->id;
6353 newtb.fsState = TBSTATE_ENABLED;
6354 newtb.iString = 0;
6355 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6356 (LPARAM)&newtb);
6357 menu->submenu_id = (HMENU)-1;
6358 }
6359 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006362 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006364 wn = enc_to_utf16(menu->name, NULL);
6365 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006367 InsertMenuW(parent->submenu_id, (UINT)idx,
6368 (menu_is_separator(menu->name)
6369 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6370 (UINT)menu->id, wn);
6371 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006373# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (IsWindow(parent->tearoff_handle))
6375 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378}
6379
6380/*
6381 * Destroy the machine specific menu widget.
6382 */
6383 void
6384gui_mch_destroy_menu(vimmenu_T *menu)
6385{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006386# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 /*
6388 * is this a toolbar button?
6389 */
6390 if (menu->submenu_id == (HMENU)-1)
6391 {
6392 int iButton;
6393
6394 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6395 (WPARAM)menu->id, 0);
6396 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6397 }
6398 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 if (menu->parent != NULL
6402 && menu_is_popup(menu->parent->dname)
6403 && menu->parent->submenu_id != NULL)
6404 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6405 else
6406 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6407 if (menu->submenu_id != NULL)
6408 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006409# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (IsWindow(menu->tearoff_handle))
6411 DestroyWindow(menu->tearoff_handle);
6412 if (menu->parent != NULL
6413 && menu->parent->children != NULL
6414 && IsWindow(menu->parent->tearoff_handle))
6415 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006416 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 menu->modes = 0;
6418 rebuild_tearoff(menu->parent);
6419 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422}
6423
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006424# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 static void
6426rebuild_tearoff(vimmenu_T *menu)
6427{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006428 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 char_u tbuf[128];
6430 RECT trect;
6431 RECT rct;
6432 RECT roct;
6433 int x, y;
6434
6435 HWND thwnd = menu->tearoff_handle;
6436
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006437 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (GetWindowRect(thwnd, &trect)
6439 && GetWindowRect(s_hwnd, &rct)
6440 && GetClientRect(s_hwnd, &roct))
6441 {
6442 x = trect.left - rct.left;
6443 y = (trect.top - rct.bottom + roct.bottom);
6444 }
6445 else
6446 {
6447 x = y = 0xffffL;
6448 }
6449 DestroyWindow(thwnd);
6450 if (menu->children != NULL)
6451 {
6452 gui_mch_tearoff(tbuf, menu, x, y);
6453 if (IsWindow(menu->tearoff_handle))
6454 (void) SetWindowPos(menu->tearoff_handle,
6455 NULL,
6456 (int)trect.left,
6457 (int)trect.top,
6458 0, 0,
6459 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6460 }
6461}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006462# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464/*
6465 * Make a menu either grey or not grey.
6466 */
6467 void
6468gui_mch_menu_grey(
6469 vimmenu_T *menu,
6470 int grey)
6471{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006472# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /*
6474 * is this a toolbar button?
6475 */
6476 if (menu->submenu_id == (HMENU)-1)
6477 {
6478 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006479 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006482# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006483 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6484 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006486# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6488 {
6489 WORD menuID;
6490 HWND menuHandle;
6491
6492 /*
6493 * A tearoff button has changed state.
6494 */
6495 if (menu->children == NULL)
6496 menuID = (WORD)(menu->id);
6497 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006498 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6500 if (menuHandle)
6501 EnableWindow(menuHandle, !grey);
6502
6503 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505}
6506
Bram Moolenaar734a8672019-12-02 22:49:38 +01006507#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509
Bram Moolenaar734a8672019-12-02 22:49:38 +01006510// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006513#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6516/*
6517 * stuff for dialogs
6518 */
6519
6520/*
6521 * The callback routine used by all the dialogs. Very simple. First,
6522 * acknowledges the INITDIALOG message so that Windows knows to do standard
6523 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6524 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6525 * number.
6526 */
6527 static LRESULT CALLBACK
6528dialog_callback(
6529 HWND hwnd,
6530 UINT message,
6531 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006532 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 if (message == WM_INITDIALOG)
6535 {
6536 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006537 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 (void)SetFocus(hwnd);
6539 if (dialog_default_button > IDCANCEL)
6540 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006541 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006542 // We don't have a default, set focus on another element of the
6543 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006544 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 return FALSE;
6546 }
6547
6548 if (message == WM_COMMAND)
6549 {
6550 int button = LOWORD(wParam);
6551
Bram Moolenaar734a8672019-12-02 22:49:38 +01006552 // Don't end the dialog if something was selected that was
6553 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (button >= DLG_NONBUTTON_CONTROL)
6555 return TRUE;
6556
Bram Moolenaar734a8672019-12-02 22:49:38 +01006557 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006559 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006560 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006561 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006562
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006563 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6564 p = utf16_to_enc(wp, NULL);
6565 vim_strncpy(s_textfield, p, IOSIZE);
6566 vim_free(p);
6567 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 /*
6571 * Need to check for IDOK because if the user just hits Return to
6572 * accept the default value, some reason this is what we get.
6573 */
6574 if (button == IDOK)
6575 {
6576 if (dialog_default_button > IDCANCEL)
6577 EndDialog(hwnd, dialog_default_button);
6578 }
6579 else
6580 EndDialog(hwnd, button - IDCANCEL);
6581 return TRUE;
6582 }
6583
6584 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6585 {
6586 EndDialog(hwnd, 0);
6587 return TRUE;
6588 }
6589 return FALSE;
6590}
6591
6592/*
6593 * Create a dialog dynamically from the parameter strings.
6594 * type = type of dialog (question, alert, etc.)
6595 * title = dialog title. may be NULL for default title.
6596 * message = text to display. Dialog sizes to accommodate it.
6597 * buttons = '\n' separated list of button captions, default first.
6598 * dfltbutton = number of default button.
6599 *
6600 * This routine returns 1 if the first button is pressed,
6601 * 2 for the second, etc.
6602 *
6603 * 0 indicates Esc was pressed.
6604 * -1 for unexpected error
6605 *
6606 * If stubbing out this fn, return 1.
6607 */
6608
Bram Moolenaar734a8672019-12-02 22:49:38 +01006609static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 "IDR_VIM",
6612 "IDR_VIM_ERROR",
6613 "IDR_VIM_ALERT",
6614 "IDR_VIM_INFO",
6615 "IDR_VIM_QUESTION"
6616};
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 int
6619gui_mch_dialog(
6620 int type,
6621 char_u *title,
6622 char_u *message,
6623 char_u *buttons,
6624 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006625 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006626 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006629 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int numButtons;
6631 int *buttonWidths, *buttonPositions;
6632 int buttonYpos;
6633 int nchar, i;
6634 DWORD lStyle;
6635 int dlgwidth = 0;
6636 int dlgheight;
6637 int editboxheight;
6638 int horizWidth = 0;
6639 int msgheight;
6640 char_u *pstart;
6641 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006642 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 char_u *tbuffer;
6644 RECT rect;
6645 HWND hwnd;
6646 HDC hdc;
6647 HFONT font, oldFont;
6648 TEXTMETRIC fontInfo;
6649 int fontHeight;
6650 int textWidth, minButtonWidth, messageWidth;
6651 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006652 int maxDialogHeight;
6653 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 int vertical;
6655 int dlgPaddingX;
6656 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006657# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006658 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006660# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006661 garray_T ga;
6662 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006663 int dlg_icon_width;
6664 int dlg_icon_height;
6665 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006667# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006668 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006669# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006670 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006671# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006672 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006673 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
Bram Moolenaar748bf032005-02-02 23:04:36 +00006676 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006677 {
6678 load_dpi_func();
6679 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006680 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006681 }
6682 else
6683 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685 if ((type < 0) || (type > VIM_LAST_TYPE))
6686 type = 0;
6687
Bram Moolenaar734a8672019-12-02 22:49:38 +01006688 // allocate some memory for dialog template
6689 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006690 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006691 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 if (p == NULL)
6694 return -1;
6695
6696 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006697 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6699 * const.
6700 */
6701 tbuffer = vim_strsave(buttons);
6702 if (tbuffer == NULL)
6703 return -1;
6704
Bram Moolenaar734a8672019-12-02 22:49:38 +01006705 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
Bram Moolenaar734a8672019-12-02 22:49:38 +01006707 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 numButtons = 1;
6709 for (i = 0; tbuffer[i] != '\0'; i++)
6710 {
6711 if (tbuffer[i] == DLG_BUTTON_SEP)
6712 numButtons++;
6713 }
6714 if (dfltbutton >= numButtons)
6715 dfltbutton = -1;
6716
Bram Moolenaar734a8672019-12-02 22:49:38 +01006717 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006718 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (buttonWidths == NULL)
6720 return -1;
6721
Bram Moolenaar734a8672019-12-02 22:49:38 +01006722 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006723 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (buttonPositions == NULL)
6725 return -1;
6726
6727 /*
6728 * Calculate how big the dialog must be.
6729 */
6730 hwnd = GetDesktopWindow();
6731 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006732# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6734 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006735 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 use_lfSysmenu = TRUE;
6737 }
6738 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006739# endif
K.Takatad1c58992022-01-23 12:31:57 +00006740 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6741 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6742
6743 oldFont = SelectFont(hdc, font);
6744 dlgPaddingX = DLG_PADDING_X;
6745 dlgPaddingY = DLG_PADDING_Y;
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 GetTextMetrics(hdc, &fontInfo);
6748 fontHeight = fontInfo.tmHeight;
6749
Bram Moolenaar734a8672019-12-02 22:49:38 +01006750 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006751 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar734a8672019-12-02 22:49:38 +01006753 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006754 if (s_hwnd == NULL)
6755 {
6756 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
Bram Moolenaar734a8672019-12-02 22:49:38 +01006758 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006759 get_work_area(&workarea_rect);
6760 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006761 if (maxDialogWidth > adjust_by_system_dpi(600))
6762 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006763 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006764 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006765 }
6766 else
6767 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006768 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006769 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006770 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006771 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6772 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6773 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6774 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006775
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006776 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006777 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6778 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6779 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6780 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6781 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006782 }
6783
Bram Moolenaar734a8672019-12-02 22:49:38 +01006784 // Set dlgwidth to width of message.
6785 // Copy the message into "ga", changing NL to CR-NL and inserting line
6786 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 pstart = message;
6788 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006789 msgheight = 0;
6790 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 do
6792 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006793 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006794
Bram Moolenaar734a8672019-12-02 22:49:38 +01006795 // Need to figure out where to break the string. The system does it
6796 // at a word boundary, which would mean we can't compute the number of
6797 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006798 textWidth = 0;
6799 last_white = NULL;
6800 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006801 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006802 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006803 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006804 && textWidth > maxDialogWidth * 3 / 4)
6805 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006806 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006807 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006809 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006810 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006811 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006812 textWidth = 0;
6813
6814 if (last_white != NULL)
6815 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006816 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006817 if (pend > last_white)
6818 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006819 pend = last_white + 1;
6820 last_white = NULL;
6821 }
6822 ga_append(&ga, '\r');
6823 ga_append(&ga, '\n');
6824 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006825 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006826
6827 while (--l >= 0)
6828 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006829 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006830 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006832
6833 ga_append(&ga, '\r');
6834 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 pstart = pend + 1;
6836 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006837
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006838 if (ga.ga_data != NULL)
6839 message = ga.ga_data;
6840
Bram Moolenaar734a8672019-12-02 22:49:38 +01006841 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006842
K.Takatac81e9bf2022-01-16 14:15:49 +00006843 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6844 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
K.Takatac81e9bf2022-01-16 14:15:49 +00006846 // Add width of icon to dlgwidth, and some space
6847 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6848 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6849
6850 if (msgheight < dlg_icon_height)
6851 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853 /*
6854 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006855 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006857 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if (!vertical)
6859 {
6860 // Place buttons horizontally if they fit.
6861 horizWidth = dlgPaddingX;
6862 pstart = tbuffer;
6863 i = 0;
6864 do
6865 {
6866 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6867 if (pend == NULL)
6868 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006869 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (textWidth < minButtonWidth)
6871 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006872 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 buttonWidths[i] = textWidth;
6874 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006875 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 pstart = pend + 1;
6877 } while (*pend != NUL);
6878
6879 if (horizWidth > maxDialogWidth)
6880 vertical = TRUE; // Too wide to fit on the screen.
6881 else if (horizWidth > dlgwidth)
6882 dlgwidth = horizWidth;
6883 }
6884
6885 if (vertical)
6886 {
6887 // Stack buttons vertically.
6888 pstart = tbuffer;
6889 do
6890 {
6891 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6892 if (pend == NULL)
6893 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006894 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006895 textWidth += dlgPaddingX; // Padding within button
6896 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (textWidth > dlgwidth)
6898 dlgwidth = textWidth;
6899 pstart = pend + 1;
6900 } while (*pend != NUL);
6901 }
6902
6903 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006904 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar734a8672019-12-02 22:49:38 +01006906 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006907 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 add_long(lStyle);
6910 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 add_word(0); // NumberOfItems(will change later)
6913 add_word(10); // x
6914 add_word(10); // y
6915 add_word(PixelToDialogX(dlgwidth)); // cx
6916
6917 // Dialog height.
6918 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006919 dlgheight = msgheight + 2 * dlgPaddingY
6920 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 else
6922 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6923
6924 // Dialog needs to be taller if contains an edit box.
6925 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6926 if (textfield != NULL)
6927 dlgheight += editboxheight;
6928
Bram Moolenaar734a8672019-12-02 22:49:38 +01006929 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006930 if (dlgheight > maxDialogHeight)
6931 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006932 msgheight = msgheight - (dlgheight - maxDialogHeight);
6933 dlgheight = maxDialogHeight;
6934 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006935 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00006936 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006937 }
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 add_word(PixelToDialogY(dlgheight));
6940
6941 add_word(0); // Menu
6942 add_word(0); // Class
6943
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006945 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6946 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 p += nchar;
6948
K.Takatad1c58992022-01-23 12:31:57 +00006949 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006950# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00006951 if (use_lfSysmenu)
6952 {
6953 // point size
6954 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6955 GetDeviceCaps(hdc, LOGPIXELSY));
6956 wcscpy(p, lfSysmenu.lfFaceName);
6957 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
K.Takatad1c58992022-01-23 12:31:57 +00006959 else
6960# endif
6961 {
6962 *p++ = DLG_FONT_POINT_SIZE; // point size
6963 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
6964 }
6965 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 buttonYpos = msgheight + 2 * dlgPaddingY;
6968
6969 if (textfield != NULL)
6970 buttonYpos += editboxheight;
6971
6972 pstart = tbuffer;
6973 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006974 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 for (i = 0; i < numButtons; i++)
6976 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006977 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 for ( pend = pstart;
6979 *pend && (*pend != DLG_BUTTON_SEP);
6980 pend++)
6981 ;
6982
6983 if (*pend)
6984 *pend = '\0';
6985
6986 /*
6987 * old NOTE:
6988 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6989 * the focus to the first tab-able button and in so doing makes that
6990 * the default!! Grrr. Workaround: Make the default button the only
6991 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6992 * he/she can use arrow keys.
6993 *
6994 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00006995 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 * dialog. Also needed for when the textfield is the default control.
6997 * It appears to work now (perhaps not on Win95?).
6998 */
6999 if (vertical)
7000 {
7001 p = add_dialog_element(p,
7002 (i == dfltbutton
7003 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7004 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007005 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 + 2 * fontHeight * i),
7007 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7008 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007009 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011 else
7012 {
7013 p = add_dialog_element(p,
7014 (i == dfltbutton
7015 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7016 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007017 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 PixelToDialogX(buttonWidths[i]),
7019 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007020 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007022 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024 *pnumitems += numButtons;
7025
Bram Moolenaar734a8672019-12-02 22:49:38 +01007026 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 p = add_dialog_element(p, SS_ICON,
7028 PixelToDialogX(dlgPaddingX),
7029 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007030 PixelToDialogX(dlg_icon_width),
7031 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7033 dlg_icons[type]);
7034
Bram Moolenaar734a8672019-12-02 22:49:38 +01007035 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007036 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007037 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007038 PixelToDialogY(dlgPaddingY),
7039 (WORD)(PixelToDialogX(messageWidth) + 1),
7040 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007041 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar734a8672019-12-02 22:49:38 +01007043 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (textfield != NULL)
7045 {
7046 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7047 PixelToDialogX(2 * dlgPaddingX),
7048 PixelToDialogY(2 * dlgPaddingY + msgheight),
7049 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7050 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007051 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 *pnumitems += 1;
7053 }
7054
7055 *pnumitems += 2;
7056
7057 SelectFont(hdc, oldFont);
7058 DeleteObject(font);
7059 ReleaseDC(hwnd, hdc);
7060
Bram Moolenaar734a8672019-12-02 22:49:38 +01007061 // Let the dialog_callback() function know which button to make default
7062 // If we have an edit box, make that the default. We also need to tell
7063 // dialog_callback() if this dialog contains an edit box or not. We do
7064 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (textfield != NULL)
7066 {
7067 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7068 s_textfield = textfield;
7069 }
7070 else
7071 {
7072 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7073 s_textfield = NULL;
7074 }
7075
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007078 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 (LPDLGTEMPLATE)pdlgtemplate,
7080 s_hwnd,
7081 (DLGPROC)dialog_callback);
7082
7083 LocalFree(LocalHandle(pdlgtemplate));
7084 vim_free(tbuffer);
7085 vim_free(buttonWidths);
7086 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007087 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
Bram Moolenaar734a8672019-12-02 22:49:38 +01007089 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 (void)SetFocus(s_hwnd);
7091
7092 return nchar;
7093}
7094
Bram Moolenaar734a8672019-12-02 22:49:38 +01007095#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097/*
7098 * Put a simple element (basic class) onto a dialog template in memory.
7099 * return a pointer to where the next item should be added.
7100 *
7101 * parameters:
7102 * lStyle = additional style flags
7103 * (be careful, NT3.51 & Win32s will ignore the new ones)
7104 * x,y = x & y positions IN DIALOG UNITS
7105 * w,h = width and height IN DIALOG UNITS
7106 * Id = ID used in messages
7107 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7108 * caption = usually text or resource name
7109 *
7110 * TODO: use the length information noted here to enable the dialog creation
7111 * routines to work out more exactly how much memory they need to alloc.
7112 */
7113 static PWORD
7114add_dialog_element(
7115 PWORD p,
7116 DWORD lStyle,
7117 WORD x,
7118 WORD y,
7119 WORD w,
7120 WORD h,
7121 WORD Id,
7122 WORD clss,
7123 const char *caption)
7124{
7125 int nchar;
7126
Bram Moolenaar734a8672019-12-02 22:49:38 +01007127 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7129 *p++ = LOWORD(lStyle);
7130 *p++ = HIWORD(lStyle);
7131 *p++ = 0; // LOWORD (lExtendedStyle)
7132 *p++ = 0; // HIWORD (lExtendedStyle)
7133 *p++ = x;
7134 *p++ = y;
7135 *p++ = w;
7136 *p++ = h;
7137 *p++ = Id; //9 or 10 words in all
7138
7139 *p++ = (WORD)0xffff;
7140 *p++ = clss; //2 more here
7141
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007142 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 p += nchar;
7144
7145 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7146
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007147 return p; // total = 15 + strlen(caption) words
7148 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
7151
7152/*
7153 * Helper routine. Take an input pointer, return closest pointer that is
7154 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7155 */
7156 static LPWORD
7157lpwAlign(
7158 LPWORD lpIn)
7159{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007160 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007162 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 ul += 3;
7164 ul >>= 2;
7165 ul <<= 2;
7166 return (LPWORD)ul;
7167}
7168
7169/*
7170 * Helper routine. Takes second parameter as Ansi string, copies it to first
7171 * parameter as wide character (16-bits / char) string, and returns integer
7172 * number of wide characters (words) in string (including the trailing wide
7173 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007174 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7175 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 static int
7177nCopyAnsiToWideChar(
7178 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007179 LPSTR lpAnsiIn,
7180 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007183 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 int i;
7185 WCHAR *wn;
7186
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007187 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007189 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007190 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (wn != NULL)
7192 {
7193 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007194 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 vim_free(wn);
7196 }
7197 }
7198 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007199 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 nChar = MultiByteToWideChar(
7201 enc_codepage > 0 ? enc_codepage : CP_ACP,
7202 MB_PRECOMPOSED,
7203 lpAnsiIn, len,
7204 lpWCStr, len);
7205 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007206 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
7209 return nChar;
7210}
7211
7212
7213#ifdef FEAT_TEAROFF
7214/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007215 * Lookup menu handle from "menu_id".
7216 */
7217 static HMENU
7218tearoff_lookup_menuhandle(
7219 vimmenu_T *menu,
7220 WORD menu_id)
7221{
7222 for ( ; menu != NULL; menu = menu->next)
7223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007224 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007225 continue;
7226 if (menu_is_separator(menu->dname))
7227 continue;
7228 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7229 return menu->submenu_id;
7230 }
7231 return NULL;
7232}
7233
7234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 * The callback function for all the modeless dialogs that make up the
7236 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7237 * thinking its menus have been clicked), and go away when closed.
7238 */
7239 static LRESULT CALLBACK
7240tearoff_callback(
7241 HWND hwnd,
7242 UINT message,
7243 WPARAM wParam,
7244 LPARAM lParam)
7245{
7246 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007247 {
7248 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar734a8672019-12-02 22:49:38 +01007252 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 HandleMouseHide(message, lParam);
7254
7255 if (message == WM_COMMAND)
7256 {
7257 if ((WORD)(LOWORD(wParam)) & 0x8000)
7258 {
7259 POINT mp;
7260 RECT rect;
7261
7262 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7263 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007264 vimmenu_T *menu;
7265
7266 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007268 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7270 (int)rect.right - 8,
7271 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007272 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 s_hwnd,
7274 NULL);
7275 /*
7276 * NOTE: The pop-up menu can eat the mouse up event.
7277 * We deal with this in normal.c.
7278 */
7279 }
7280 }
7281 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007282 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7284 /*
7285 * Give main window the focus back: this is so after
7286 * choosing a tearoff button you can start typing again
7287 * straight away.
7288 */
7289 (void)SetFocus(s_hwnd);
7290 return TRUE;
7291 }
7292 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7293 {
7294 DestroyWindow(hwnd);
7295 return TRUE;
7296 }
7297
Bram Moolenaar734a8672019-12-02 22:49:38 +01007298 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (message == WM_EXITSIZEMOVE)
7300 (void)SetActiveWindow(s_hwnd);
7301
7302 return FALSE;
7303}
7304#endif
7305
7306
7307/*
K.Takatad1c58992022-01-23 12:31:57 +00007308 * Computes the dialog base units based on the current dialog font.
7309 * We don't use the GetDialogBaseUnits() API, because we don't use the
7310 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 */
7312 static void
7313get_dialog_font_metrics(void)
7314{
7315 HDC hdc;
7316 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 SIZE size;
7318#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007319 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#endif
7321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007323 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007324 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326#endif
7327 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007328 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
K.Takatad1c58992022-01-23 12:31:57 +00007330 hdc = GetDC(s_hwnd);
7331 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007332 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007333 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
K.Takataabe628e2022-01-23 16:25:17 +00007335 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007336 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
7339#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7340/*
7341 * Create a pseudo-"tearoff menu" based on the child
7342 * items of a given menu pointer.
7343 */
7344 static void
7345gui_mch_tearoff(
7346 char_u *title,
7347 vimmenu_T *menu,
7348 int initX,
7349 int initY)
7350{
7351 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7352 int template_len;
7353 int nchar, textWidth, submenuWidth;
7354 DWORD lStyle;
7355 DWORD lExtendedStyle;
7356 WORD dlgwidth;
7357 WORD menuID;
7358 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007359 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 vimmenu_T *the_menu = menu;
7361 HWND hwnd;
7362 HDC hdc;
7363 HFONT font, oldFont;
7364 int col, spaceWidth, len;
7365 int columnWidths[2];
7366 char_u *label, *text;
7367 int acLen = 0;
7368 int nameLen;
7369 int padding0, padding1, padding2 = 0;
7370 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007371 int x;
7372 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007373# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007374 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378 /*
7379 * If this menu is already torn off, move it to the mouse position.
7380 */
7381 if (IsWindow(menu->tearoff_handle))
7382 {
7383 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007384 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7387 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7388 }
7389 return;
7390 }
7391
7392 /*
7393 * Create a new tearoff.
7394 */
7395 if (*title == MNU_HIDDEN_CHAR)
7396 title++;
7397
Bram Moolenaar734a8672019-12-02 22:49:38 +01007398 // Allocate memory to store the dialog template. It's made bigger when
7399 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 template_len = DLG_ALLOC_SIZE;
7401 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7402 if (p == NULL)
7403 return;
7404
7405 hwnd = GetDesktopWindow();
7406 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007407# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7409 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007410 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 use_lfSysmenu = TRUE;
7412 }
7413 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007414# endif
K.Takatad1c58992022-01-23 12:31:57 +00007415 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7416 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7417
7418 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar734a8672019-12-02 22:49:38 +01007420 // Calculate width of a single space. Used for padding columns to the
7421 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007422 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 // Figure out max width of the text column, the accelerator column and the
7425 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 submenuWidth = 0;
7427 for (col = 0; col < 2; col++)
7428 {
7429 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007430 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007432 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 text = (col == 0) ? pmenu->dname : pmenu->actext;
7434 if (text != NULL && *text != NUL)
7435 {
7436 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7437 if (textWidth > columnWidths[col])
7438 columnWidths[col] = textWidth;
7439 }
7440 if (pmenu->children != NULL)
7441 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7442 }
7443 }
7444 if (columnWidths[1] == 0)
7445 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007446 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (submenuWidth != 0)
7448 columnWidths[0] += submenuWidth;
7449 else
7450 columnWidths[0] += spaceWidth;
7451 }
7452 else
7453 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007454 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7456 columnWidths[1] += submenuWidth;
7457 }
7458
7459 /*
7460 * Now find the total width of our 'menu'.
7461 */
7462 textWidth = columnWidths[0] + columnWidths[1];
7463 if (submenuWidth != 0)
7464 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007465 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7467 textWidth += submenuWidth;
7468 }
7469 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7470 if (textWidth > dlgwidth)
7471 dlgwidth = textWidth;
7472 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7473
Bram Moolenaar734a8672019-12-02 22:49:38 +01007474 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007475 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
7477 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7478 *p++ = LOWORD(lStyle);
7479 *p++ = HIWORD(lStyle);
7480 *p++ = LOWORD(lExtendedStyle);
7481 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007482 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007484 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007486 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
7488 *p++ = PixelToDialogX(initX); // x
7489 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007490 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
7492 *p++ = PixelToDialogY(initY); // y
7493 *p++ = PixelToDialogX(dlgwidth); // cx
7494 ptrueheight = p;
7495 *p++ = 0; // dialog height: changed later anyway
7496 *p++ = 0; // Menu
7497 *p++ = 0; // Class
7498
Bram Moolenaar734a8672019-12-02 22:49:38 +01007499 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007501 ? (LPSTR)title
7502 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 p += nchar;
7504
K.Takatad1c58992022-01-23 12:31:57 +00007505 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007506# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007507 if (use_lfSysmenu)
7508 {
7509 // point size
7510 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7511 GetDeviceCaps(hdc, LOGPIXELSY));
7512 wcscpy(p, lfSysmenu.lfFaceName);
7513 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
K.Takatad1c58992022-01-23 12:31:57 +00007515 else
7516# endif
7517 {
7518 *p++ = DLG_FONT_POINT_SIZE; // point size
7519 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7520 }
7521 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
7523 /*
7524 * Loop over all the items in the menu.
7525 * But skip over the tearbar.
7526 */
7527 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7528 menu = menu->children->next;
7529 else
7530 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007531 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 for ( ; menu != NULL; menu = menu->next)
7533 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007534 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 continue;
7536 if (menu_is_separator(menu->dname))
7537 {
7538 sepPadding += 3;
7539 continue;
7540 }
7541
Bram Moolenaar734a8672019-12-02 22:49:38 +01007542 // Check if there still is plenty of room in the template. Make it
7543 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7545 {
7546 WORD *newp;
7547
7548 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7549 if (newp != NULL)
7550 {
7551 template_len += 4096;
7552 mch_memmove(newp, pdlgtemplate,
7553 (char *)p - (char *)pdlgtemplate);
7554 p = newp + (p - pdlgtemplate);
7555 pnumitems = newp + (pnumitems - pdlgtemplate);
7556 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7557 LocalFree(LocalHandle(pdlgtemplate));
7558 pdlgtemplate = newp;
7559 }
7560 }
7561
Bram Moolenaar734a8672019-12-02 22:49:38 +01007562 // Figure out minimal length of this menu label. Use "name" for the
7563 // actual text, "dname" for estimating the displayed size. "name"
7564 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 len = nameLen = (int)STRLEN(menu->name);
7566 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7567 (int)STRLEN(menu->dname))) / spaceWidth;
7568 len += padding0;
7569
7570 if (menu->actext != NULL)
7571 {
7572 acLen = (int)STRLEN(menu->actext);
7573 len += acLen;
7574 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7575 }
7576 else
7577 textWidth = 0;
7578 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7579 len += padding1;
7580
7581 if (menu->children == NULL)
7582 {
7583 padding2 = submenuWidth / spaceWidth;
7584 len += padding2;
7585 menuID = (WORD)(menu->id);
7586 }
7587 else
7588 {
7589 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007590 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592
Bram Moolenaar734a8672019-12-02 22:49:38 +01007593 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007594 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (label == NULL)
7596 break;
7597
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007598 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007599 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007601 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 while (padding0-- > 0)
7603 *text++ = ' ';
7604 if (menu->actext != NULL)
7605 {
7606 STRNCPY(text, menu->actext, acLen);
7607 text += acLen;
7608 }
7609 while (padding1-- > 0)
7610 *text++ = ' ';
7611 if (menu->children != NULL)
7612 {
7613 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7614 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7615 }
7616 else
7617 {
7618 while (padding2-- > 0)
7619 *text++ = ' ';
7620 }
7621 *text = NUL;
7622
7623 /*
7624 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7625 * W95/NT4 it makes the tear-off look more like a menu.
7626 */
7627 p = add_dialog_element(p,
7628 BS_PUSHBUTTON|BS_LEFT,
7629 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7630 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7631 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7632 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007633 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 vim_free(label);
7635 (*pnumitems)++;
7636 }
7637
7638 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7639
7640
Bram Moolenaar734a8672019-12-02 22:49:38 +01007641 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007642 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007643 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 (LPDLGTEMPLATE)pdlgtemplate,
7645 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007646 (DLGPROC)tearoff_callback,
7647 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649 LocalFree(LocalHandle(pdlgtemplate));
7650 SelectFont(hdc, oldFont);
7651 DeleteObject(font);
7652 ReleaseDC(hwnd, hdc);
7653
7654 /*
7655 * Reassert ourselves as the active window. This is so that after creating
7656 * a tearoff, the user doesn't have to click with the mouse just to start
7657 * typing again!
7658 */
7659 (void)SetActiveWindow(s_hwnd);
7660
Bram Moolenaar734a8672019-12-02 22:49:38 +01007661 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 force_menu_update = TRUE;
7663}
7664#endif
7665
7666#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007667# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669/*
7670 * Create the toolbar, initially unpopulated.
7671 * (just like the menu, there are no defaults, it's all
7672 * set up through menu.vim)
7673 */
7674 static void
7675initialise_toolbar(void)
7676{
7677 InitCommonControls();
7678 s_toolbarhwnd = CreateToolbarEx(
7679 s_hwnd,
7680 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7681 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007682 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007683 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 IDR_TOOLBAR1, // id of initial bitmap
7685 NULL,
7686 0, // initial number of buttons
7687 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7688 TOOLBAR_BUTTON_HEIGHT,
7689 TOOLBAR_BUTTON_WIDTH,
7690 TOOLBAR_BUTTON_HEIGHT,
7691 sizeof(TBBUTTON)
7692 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007693
7694 // Remove transparency from the toolbar to prevent the main window
7695 // background colour showing through
7696 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7697 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7698
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007699 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700
7701 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007702
7703 update_toolbar_size();
7704}
7705
7706 static void
7707update_toolbar_size(void)
7708{
7709 int w, h;
7710 TBMETRICS tbm = {sizeof(TBMETRICS)};
7711
7712 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7713 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7714 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7715 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7716
7717 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7718 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7719 //TRACE("button size: %d, %d", w, h);
7720 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7721 gui.toolbar_height = h + 6;
7722
7723 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7724 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7725
7726 // TODO:
7727 // Currently, this function only updates the size of toolbar buttons.
7728 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007731 static LRESULT CALLBACK
7732toolbar_wndproc(
7733 HWND hwnd,
7734 UINT uMsg,
7735 WPARAM wParam,
7736 LPARAM lParam)
7737{
7738 HandleMouseHide(uMsg, lParam);
7739 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7740}
7741
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 static int
7743get_toolbar_bitmap(vimmenu_T *menu)
7744{
7745 int i = -1;
7746
7747 /*
7748 * Check user bitmaps first, unless builtin is specified.
7749 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007750 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 char_u fname[MAXPATHL];
7753 HANDLE hbitmap = NULL;
7754
7755 if (menu->iconfile != NULL)
7756 {
7757 gui_find_iconfile(menu->iconfile, fname, "bmp");
7758 hbitmap = LoadImage(
7759 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007760 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 IMAGE_BITMAP,
7762 TOOLBAR_BUTTON_WIDTH,
7763 TOOLBAR_BUTTON_HEIGHT,
7764 LR_LOADFROMFILE |
7765 LR_LOADMAP3DCOLORS
7766 );
7767 }
7768
7769 /*
7770 * If the LoadImage call failed, or the "icon=" file
7771 * didn't exist or wasn't specified, try the menu name
7772 */
7773 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007774 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007775# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007776 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007777# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007778 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 hbitmap = LoadImage(
7780 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007781 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 IMAGE_BITMAP,
7783 TOOLBAR_BUTTON_WIDTH,
7784 TOOLBAR_BUTTON_HEIGHT,
7785 LR_LOADFROMFILE |
7786 LR_LOADMAP3DCOLORS
7787 );
7788
7789 if (hbitmap != NULL)
7790 {
7791 TBADDBITMAP tbAddBitmap;
7792
7793 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007794 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
7796 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7797 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007798 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800 }
7801 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7802 i = menu->iconidx;
7803
7804 return i;
7805}
7806#endif
7807
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007808#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7809 static void
7810initialise_tabline(void)
7811{
7812 InitCommonControls();
7813
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007814 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007815 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007816 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007817 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007818 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007819
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007820 gui.tabline_height = TABLINE_HEIGHT;
7821
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007822 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007823}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007824
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007825/*
7826 * Get tabpage_T from POINT.
7827 */
7828 static tabpage_T *
7829GetTabFromPoint(
7830 HWND hWnd,
7831 POINT pt)
7832{
7833 tabpage_T *ptp = NULL;
7834
7835 if (gui_mch_showing_tabline())
7836 {
7837 TCHITTESTINFO htinfo;
7838 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007839 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007840 if (s_tabhwnd == hWnd)
7841 {
7842 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7843 if (idx != -1)
7844 ptp = find_tabpage(idx + 1);
7845 }
7846 }
7847 return ptp;
7848}
7849
7850static POINT s_pt = {0, 0};
7851static HCURSOR s_hCursor = NULL;
7852
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007853 static LRESULT CALLBACK
7854tabline_wndproc(
7855 HWND hwnd,
7856 UINT uMsg,
7857 WPARAM wParam,
7858 LPARAM lParam)
7859{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007860 POINT pt;
7861 tabpage_T *tp;
7862 RECT rect;
7863 int nCenter;
7864 int idx0;
7865 int idx1;
7866
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007867 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007868
7869 switch (uMsg)
7870 {
7871 case WM_LBUTTONDOWN:
7872 {
7873 s_pt.x = GET_X_LPARAM(lParam);
7874 s_pt.y = GET_Y_LPARAM(lParam);
7875 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007876 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007877 break;
7878 }
7879 case WM_MOUSEMOVE:
7880 if (GetCapture() == hwnd
7881 && ((wParam & MK_LBUTTON)) != 0)
7882 {
7883 pt.x = GET_X_LPARAM(lParam);
7884 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007885 if (abs(pt.x - s_pt.x) >
7886 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007887 {
7888 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7889
7890 tp = GetTabFromPoint(hwnd, pt);
7891 if (tp != NULL)
7892 {
7893 idx0 = tabpage_index(curtab) - 1;
7894 idx1 = tabpage_index(tp) - 1;
7895
7896 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7897 nCenter = rect.left + (rect.right - rect.left) / 2;
7898
Bram Moolenaar734a8672019-12-02 22:49:38 +01007899 // Check if the mouse cursor goes over the center of
7900 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007901 if ((idx0 < idx1) && (nCenter < pt.x))
7902 {
7903 tabpage_move(idx1 + 1);
7904 update_screen(0);
7905 }
7906 else if ((idx1 < idx0) && (pt.x < nCenter))
7907 {
7908 tabpage_move(idx1);
7909 update_screen(0);
7910 }
7911 }
7912 }
7913 }
7914 break;
7915 case WM_LBUTTONUP:
7916 {
7917 if (GetCapture() == hwnd)
7918 {
7919 SetCursor(s_hCursor);
7920 ReleaseCapture();
7921 }
7922 break;
7923 }
7924 default:
7925 break;
7926 }
7927
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007928 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7929}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007930#endif
7931
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7933/*
7934 * Make the GUI window come to the foreground.
7935 */
7936 void
7937gui_mch_set_foreground(void)
7938{
7939 if (IsIconic(s_hwnd))
7940 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7941 SetForegroundWindow(s_hwnd);
7942}
7943#endif
7944
7945#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7946 static void
7947dyn_imm_load(void)
7948{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007949 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (hLibImm == NULL)
7951 return;
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 pImmGetCompositionStringW
7954 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7955 pImmGetContext
7956 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7957 pImmAssociateContext
7958 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7959 pImmReleaseContext
7960 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7961 pImmGetOpenStatus
7962 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7963 pImmSetOpenStatus
7964 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007965 pImmGetCompositionFontW
7966 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7967 pImmSetCompositionFontW
7968 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 pImmSetCompositionWindow
7970 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7971 pImmGetConversionStatus
7972 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007973 pImmSetConversionStatus
7974 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
K.Takatab0b2b732022-01-19 12:59:21 +00007976 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 || pImmGetContext == NULL
7978 || pImmAssociateContext == NULL
7979 || pImmReleaseContext == NULL
7980 || pImmGetOpenStatus == NULL
7981 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007982 || pImmGetCompositionFontW == NULL
7983 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007985 || pImmGetConversionStatus == NULL
7986 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
7988 FreeLibrary(hLibImm);
7989 hLibImm = NULL;
7990 pImmGetContext = NULL;
7991 return;
7992 }
7993
7994 return;
7995}
7996
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997#endif
7998
7999#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8000
8001# ifdef FEAT_XPM_W32
8002# define IMAGE_XPM 100
8003# endif
8004
8005typedef struct _signicon_t
8006{
8007 HANDLE hImage;
8008 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008009# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008010 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012} signicon_t;
8013
8014 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008015gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 signicon_t *sign;
8018 int x, y, w, h;
8019
8020 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8021 return;
8022
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008023# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008024 if (IS_ENABLE_DIRECTX())
8025 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008026# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 x = TEXT_X(col);
8029 y = TEXT_Y(row);
8030 w = gui.char_width * 2;
8031 h = gui.char_height;
8032 switch (sign->uType)
8033 {
8034 case IMAGE_BITMAP:
8035 {
8036 HDC hdcMem;
8037 HBITMAP hbmpOld;
8038
8039 hdcMem = CreateCompatibleDC(s_hdc);
8040 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8041 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8042 SelectObject(hdcMem, hbmpOld);
8043 DeleteDC(hdcMem);
8044 }
8045 break;
8046 case IMAGE_ICON:
8047 case IMAGE_CURSOR:
8048 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8049 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008050# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 case IMAGE_XPM:
8052 {
8053 HDC hdcMem;
8054 HBITMAP hbmpOld;
8055
8056 hdcMem = CreateCompatibleDC(s_hdc);
8057 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008058 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8060
8061 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008062 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8064 SelectObject(hdcMem, hbmpOld);
8065 DeleteDC(hdcMem);
8066 }
8067 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 }
8070}
8071
8072 static void
8073close_signicon_image(signicon_t *sign)
8074{
8075 if (sign)
8076 switch (sign->uType)
8077 {
8078 case IMAGE_BITMAP:
8079 DeleteObject((HGDIOBJ)sign->hImage);
8080 break;
8081 case IMAGE_CURSOR:
8082 DestroyCursor((HCURSOR)sign->hImage);
8083 break;
8084 case IMAGE_ICON:
8085 DestroyIcon((HICON)sign->hImage);
8086 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008087# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 case IMAGE_XPM:
8089 DeleteObject((HBITMAP)sign->hImage);
8090 DeleteObject((HBITMAP)sign->hShape);
8091 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094}
8095
8096 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008097gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 signicon_t sign, *psign;
8100 char_u *ext;
8101
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008103 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 if (ext > signfile)
8105 {
8106 int do_load = 1;
8107
8108 if (!STRICMP(ext, ".bmp"))
8109 sign.uType = IMAGE_BITMAP;
8110 else if (!STRICMP(ext, ".ico"))
8111 sign.uType = IMAGE_ICON;
8112 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8113 sign.uType = IMAGE_CURSOR;
8114 else
8115 do_load = 0;
8116
8117 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008118 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 gui.char_width * 2, gui.char_height,
8120 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008121# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (!STRICMP(ext, ".xpm"))
8123 {
8124 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008125 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8126 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
8130
8131 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008132 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 *psign = sign;
8134
8135 if (!psign)
8136 {
8137 if (sign.hImage)
8138 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008139 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 return (void *)psign;
8142
8143}
8144
8145 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008146gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147{
8148 if (sign)
8149 {
8150 close_signicon_image((signicon_t *)sign);
8151 vim_free(sign);
8152 }
8153}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008156#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157
Bram Moolenaar734a8672019-12-02 22:49:38 +01008158/*
8159 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008160 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008162 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8164 * to get current mouse position).
8165 *
8166 * Trying to use as more Windows services as possible, and as less
8167 * IE version as possible :)).
8168 *
8169 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8170 * BalloonEval struct.
8171 * 2) Enable/Disable simply create/kill BalloonEval Timer
8172 * 3) When there was enough inactivity, timer procedure posts
8173 * async request to debugger
8174 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8175 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008176 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 */
8178
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008179 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008180make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008181{
K.Takata76687d22022-01-25 10:31:37 +00008182 TOOLINFOW *pti;
8183 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008184
K.Takata76687d22022-01-25 10:31:37 +00008185 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008186 if (pti == NULL)
8187 return;
8188
8189 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8190 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8191 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008192 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008193
8194 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8195 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8196
K.Takata76687d22022-01-25 10:31:37 +00008197 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008198 pti->uFlags = TTF_SUBCLASS;
8199 pti->hwnd = beval->target;
8200 pti->hinst = 0; // Don't use string resources
8201 pti->uId = ID_BEVAL_TOOLTIP;
8202
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008203 pti->lpszText = LPSTR_TEXTCALLBACKW;
8204 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8205 pti->lParam = (LPARAM)beval->tofree;
8206 // switch multiline tooltips on
8207 if (GetClientRect(s_textArea, &rect))
8208 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8209 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008210
8211 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8212 pti->rect.left = pt.x - 3;
8213 pti->rect.top = pt.y - 3;
8214 pti->rect.right = pt.x + 3;
8215 pti->rect.bottom = pt.y + 3;
8216
8217 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8218 // Make tooltip appear sooner.
8219 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8220 // I've performed some tests and it seems the longest possible life time
8221 // of tooltip is 30 seconds.
8222 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8223 /*
8224 * HACK: force tooltip to appear, because it'll not appear until
8225 * first mouse move. D*mn M$
8226 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8227 */
8228 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8229 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8230 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008231}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008234delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008236 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237}
8238
8239 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008240beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008241 HWND hwnd UNUSED,
8242 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008243 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008244 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246 POINT pt;
8247 RECT rect;
8248
8249 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8250 return;
8251
8252 GetCursorPos(&pt);
8253 if (WindowFromPoint(pt) != s_textArea)
8254 return;
8255
8256 ScreenToClient(s_textArea, &pt);
8257 GetClientRect(s_textArea, &rect);
8258 if (!PtInRect(&rect, pt))
8259 return;
8260
K.Takataa8ec4912022-02-03 14:32:33 +00008261 if (last_user_activity > 0
8262 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 && (cur_beval->showState != ShS_PENDING
8264 || abs(cur_beval->x - pt.x) > 3
8265 || abs(cur_beval->y - pt.y) > 3))
8266 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008267 // Pointer resting in one place long enough, it's time to show
8268 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 cur_beval->showState = ShS_PENDING;
8270 cur_beval->x = pt.x;
8271 cur_beval->y = pt.y;
8272
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 if (cur_beval->msgCB != NULL)
8274 (*cur_beval->msgCB)(cur_beval, 0);
8275 }
8276}
8277
8278 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008279gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
K.Takataa8ec4912022-02-03 14:32:33 +00008281 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282}
8283
8284 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008285gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (beval == NULL)
8288 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008289 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8290 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291}
8292
8293 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008294gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008297
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008298 vim_free(beval->msg);
8299 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8300 if (beval->msg == NULL)
8301 {
8302 delete_tooltip(beval);
8303 beval->showState = ShS_NEUTRAL;
8304 return;
8305 }
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 if (beval->showState == ShS_SHOWING)
8308 return;
8309 GetCursorPos(&pt);
8310 ScreenToClient(s_textArea, &pt);
8311
8312 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008314 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 gui_mch_disable_beval_area(cur_beval);
8316 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008317 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319}
8320
8321 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008322gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008323 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008324 char_u *mesg,
8325 void (*mesgCB)(BalloonEval *, int),
8326 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008328 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 BalloonEval *beval;
8330
8331 if (mesg != NULL && mesgCB != NULL)
8332 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008333 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 return NULL;
8335 }
8336
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008337 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (beval != NULL)
8339 {
8340 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 beval->msg = mesg;
8344 beval->msgCB = mesgCB;
8345 beval->clientData = clientData;
8346
8347 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 cur_beval = beval;
8349
8350 if (p_beval)
8351 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 }
8353 return beval;
8354}
8355
8356 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008357Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008359 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 return;
8361
8362 if (cur_beval != NULL)
8363 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008364 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008366 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008367 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008368 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 delete_tooltip(cur_beval);
8370 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
8372 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008373 break;
8374 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008375 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008376 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008377 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008378 info->lpszText = (LPSTR)info->lParam;
8379 info->uFlags |= TTF_DI_SETITEM;
8380 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008381 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008382 case TTN_GETDISPINFOW:
8383 {
8384 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008385 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008386 info->lpszText = (LPWSTR)info->lParam;
8387 info->uFlags |= TTF_DI_SETITEM;
8388 }
8389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 }
8391 }
8392}
8393
8394 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008395track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8398 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008399 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401
8402 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008403gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008405# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008406 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008407# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008408 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 vim_free(beval);
8410}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008411#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
8413#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8414/*
8415 * We have multiple signs to draw at the same location. Draw the
8416 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8417 */
8418 void
8419netbeans_draw_multisign_indicator(int row)
8420{
8421 int i;
8422 int y;
8423 int x;
8424
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008425 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008426 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 x = 0;
8429 y = TEXT_Y(row);
8430
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008431# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008432 if (IS_ENABLE_DIRECTX())
8433 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008434# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 for (i = 0; i < gui.char_height - 3; i++)
8437 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8438
8439 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8440 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8441 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8442 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8443 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8444 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8445 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8446}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008447#endif