blob: b5ddc5dace248e6f550d21e59ee38f839032774e [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{
2902 gui_focus_change(FALSE);
2903 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002904 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905}
2906
2907/*
2908 * Get a message when the user switches back to vim
2909 */
2910 static LRESULT
2911_OnActivateApp(
2912 HWND hwnd,
2913 BOOL fActivate,
2914 DWORD dwThreadId)
2915{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002916 // we call gui_focus_change() in _OnSetFocus()
2917 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002918 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919}
2920
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921 void
2922gui_mch_destroy_scrollbar(scrollbar_T *sb)
2923{
2924 DestroyWindow(sb->id);
2925}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002926
2927/*
2928 * Get current mouse coordinates in text window.
2929 */
2930 void
2931gui_mch_getmouse(int *x, int *y)
2932{
2933 RECT rct;
2934 POINT mp;
2935
2936 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002937 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 *x = (int)(mp.x - rct.left);
2939 *y = (int)(mp.y - rct.top);
2940}
2941
2942/*
2943 * Move mouse pointer to character at (x, y).
2944 */
2945 void
2946gui_mch_setmouse(int x, int y)
2947{
2948 RECT rct;
2949
2950 (void)GetWindowRect(s_textArea, &rct);
2951 (void)SetCursorPos(x + gui.border_offset + rct.left,
2952 y + gui.border_offset + rct.top);
2953}
2954
2955 static void
2956gui_mswin_get_valid_dimensions(
2957 int w,
2958 int h,
2959 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002960 int *valid_h,
2961 int *cols,
2962 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002963{
2964 int base_width, base_height;
2965
2966 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002967 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2968 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002969 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002970 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2971 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2972 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2973 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002974 *cols = (w - base_width) / gui.char_width;
2975 *rows = (h - base_height) / gui.char_height;
2976 *valid_w = base_width + *cols * gui.char_width;
2977 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002978}
2979
2980 void
2981gui_mch_flash(int msec)
2982{
2983 RECT rc;
2984
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002985#if defined(FEAT_DIRECTX)
2986 if (IS_ENABLE_DIRECTX())
2987 DWriteContext_Flush(s_dwc);
2988#endif
2989
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002990 /*
2991 * Note: InvertRect() excludes right and bottom of rectangle.
2992 */
2993 rc.left = 0;
2994 rc.top = 0;
2995 rc.right = gui.num_cols * gui.char_width;
2996 rc.bottom = gui.num_rows * gui.char_height;
2997 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002998 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002999
Bram Moolenaar734a8672019-12-02 22:49:38 +01003000 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003001
3002 InvertRect(s_hdc, &rc);
3003}
3004
3005/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003006 * Check if the specified point is on-screen. (multi-monitor aware)
3007 */
3008 static BOOL
3009is_point_onscreen(int x, int y)
3010{
3011 POINT pt = {x, y};
3012
3013 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3014}
3015
3016/*
3017 * Check if the whole area of the specified window is on-screen.
3018 *
3019 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3020 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3021 * only works when the whole window is on-screen.
3022 */
3023 static BOOL
3024is_window_onscreen(HWND hwnd)
3025{
3026 RECT rc;
3027
3028 GetWindowRect(hwnd, &rc);
3029
3030 if (!is_point_onscreen(rc.left, rc.top))
3031 return FALSE;
3032 if (!is_point_onscreen(rc.left, rc.bottom))
3033 return FALSE;
3034 if (!is_point_onscreen(rc.right, rc.top))
3035 return FALSE;
3036 if (!is_point_onscreen(rc.right, rc.bottom))
3037 return FALSE;
3038 return TRUE;
3039}
3040
3041/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003042 * Return flags used for scrolling.
3043 * The SW_INVALIDATE is required when part of the window is covered or
3044 * off-screen. Refer to MS KB Q75236.
3045 */
3046 static int
3047get_scroll_flags(void)
3048{
3049 HWND hwnd;
3050 RECT rcVim, rcOther, rcDest;
3051
Bram Moolenaar185577e2020-10-29 20:08:21 +01003052 // Check if the window is (partly) off-screen.
3053 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003054 return SW_INVALIDATE;
3055
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003056 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003057 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003058 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3059 if (IsWindowVisible(hwnd))
3060 {
3061 GetWindowRect(hwnd, &rcOther);
3062 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3063 return SW_INVALIDATE;
3064 }
3065 return 0;
3066}
3067
3068/*
3069 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3070 * may not be scrolled out properly.
3071 * For gVim, when _OnScroll() is repeated, the character at the
3072 * previous cursor position may be left drawn after scroll.
3073 * The problem can be avoided by calling GetPixel() to get a pixel in
3074 * the region before ScrollWindowEx().
3075 */
3076 static void
3077intel_gpu_workaround(void)
3078{
3079 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3080}
3081
3082/*
3083 * Delete the given number of lines from the given row, scrolling up any
3084 * text further down within the scroll region.
3085 */
3086 void
3087gui_mch_delete_lines(
3088 int row,
3089 int num_lines)
3090{
3091 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003092
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003093 rc.left = FILL_X(gui.scroll_region_left);
3094 rc.right = FILL_X(gui.scroll_region_right + 1);
3095 rc.top = FILL_Y(row);
3096 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3097
Bram Moolenaar92467d32017-12-05 13:22:16 +01003098#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003099 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003100 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003101 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003102 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003103 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003104#endif
3105 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003106#if defined(FEAT_DIRECTX)
3107 if (IS_ENABLE_DIRECTX())
3108 DWriteContext_Flush(s_dwc);
3109#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003110 intel_gpu_workaround();
3111 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003112 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003113 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003114 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003115
Bram Moolenaar734a8672019-12-02 22:49:38 +01003116 // This seems to be required to avoid the cursor disappearing when
3117 // scrolling such that the cursor ends up in the top-left character on
3118 // the screen... But why? (Webb)
3119 // It's probably fixed by disabling drawing the cursor while scrolling.
3120 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003121
3122 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3123 gui.scroll_region_left,
3124 gui.scroll_region_bot, gui.scroll_region_right);
3125}
3126
3127/*
3128 * Insert the given number of lines before the given row, scrolling down any
3129 * following text within the scroll region.
3130 */
3131 void
3132gui_mch_insert_lines(
3133 int row,
3134 int num_lines)
3135{
3136 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003137
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003138 rc.left = FILL_X(gui.scroll_region_left);
3139 rc.right = FILL_X(gui.scroll_region_right + 1);
3140 rc.top = FILL_Y(row);
3141 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003142
3143#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003144 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003145 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003146 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003147 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003148 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003149#endif
3150 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003151#if defined(FEAT_DIRECTX)
3152 if (IS_ENABLE_DIRECTX())
3153 DWriteContext_Flush(s_dwc);
3154#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003155 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003156 // The SW_INVALIDATE is required when part of the window is covered or
3157 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003158 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003159 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003160 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003161 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003162
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003163 gui_clear_block(row, gui.scroll_region_left,
3164 row + num_lines - 1, gui.scroll_region_right);
3165}
3166
3167
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003168 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003169gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170{
3171#if defined(FEAT_DIRECTX)
3172 DWriteContext_Close(s_dwc);
3173 DWrite_Final();
3174 s_dwc = NULL;
3175#endif
3176
3177 ReleaseDC(s_textArea, s_hdc);
3178 DeleteObject(s_brush);
3179
3180#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003181 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003182 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3183#endif
3184
Bram Moolenaar734a8672019-12-02 22:49:38 +01003185 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186 if (s_hwnd != NULL)
3187 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003188 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189 DestroyWindow(s_hwnd);
3190 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191}
3192
3193 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003194logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195{
3196 char *p;
3197 char *res;
3198 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003199 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003200 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003201 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003202
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3204 if (font_name == NULL)
3205 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003206 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003207 quality_name = quality_id2name((int)lf.lfQuality);
3208
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003209 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003210 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003211 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212 if (res != NULL)
3213 {
3214 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003215 // make a normal font string out of the lf thing:
3216 points = pixels_to_points(
3217 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3218 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3219 sprintf((char *)p, "%s:h%d", font_name, points);
3220 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003221 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003222 while (*p)
3223 {
3224 if (*p == ' ')
3225 *p = '_';
3226 ++p;
3227 }
3228 if (lf.lfItalic)
3229 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003230 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231 STRCAT(p, ":b");
3232 if (lf.lfUnderline)
3233 STRCAT(p, ":u");
3234 if (lf.lfStrikeOut)
3235 STRCAT(p, ":s");
3236 if (charset_name != NULL)
3237 {
3238 STRCAT(p, ":c");
3239 STRCAT(p, charset_name);
3240 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003241 if (quality_name != NULL)
3242 {
3243 STRCAT(p, ":q");
3244 STRCAT(p, quality_name);
3245 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003246 }
3247
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003248 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 return (char_u *)res;
3250}
3251
3252
3253#ifdef FEAT_MBYTE_IME
3254/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003255 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003256 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257 */
3258 static void
3259update_im_font(void)
3260{
K.Takatac81e9bf2022-01-16 14:15:49 +00003261 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
3263 if (p_guifontwide != NULL && *p_guifontwide != NUL
3264 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003265 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 norm_logfont = lf_wide;
3267 else
3268 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003269
3270 lf = norm_logfont;
3271 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3272 // Work around when PerMonitorV2 is not enabled in the process level.
3273 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3274 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275}
3276#endif
3277
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278/*
3279 * Handler of gui.wide_font (p_guifontwide) changed notification.
3280 */
3281 void
3282gui_mch_wide_font_changed(void)
3283{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003284 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003285
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003286#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003287 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003288#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003289
3290 gui_mch_free_font(gui.wide_ital_font);
3291 gui.wide_ital_font = NOFONT;
3292 gui_mch_free_font(gui.wide_bold_font);
3293 gui.wide_bold_font = NOFONT;
3294 gui_mch_free_font(gui.wide_boldital_font);
3295 gui.wide_boldital_font = NOFONT;
3296
3297 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003298 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003299 {
3300 if (!lf.lfItalic)
3301 {
3302 lf.lfItalic = TRUE;
3303 gui.wide_ital_font = get_font_handle(&lf);
3304 lf.lfItalic = FALSE;
3305 }
3306 if (lf.lfWeight < FW_BOLD)
3307 {
3308 lf.lfWeight = FW_BOLD;
3309 gui.wide_bold_font = get_font_handle(&lf);
3310 if (!lf.lfItalic)
3311 {
3312 lf.lfItalic = TRUE;
3313 gui.wide_boldital_font = get_font_handle(&lf);
3314 }
3315 }
3316 }
3317}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003318
3319/*
3320 * Initialise vim to use the font with the given name.
3321 * Return FAIL if the font could not be loaded, OK otherwise.
3322 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003323 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003324gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325{
K.Takatac81e9bf2022-01-16 14:15:49 +00003326 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327 GuiFont font = NOFONT;
3328 char_u *p;
3329
Bram Moolenaar734a8672019-12-02 22:49:38 +01003330 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003332 {
3333 lfOrig = lf;
3334 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003335 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003336 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337 if (font == NOFONT)
3338 return FAIL;
3339
3340 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003341 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003342#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343 norm_logfont = lf;
3344 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003345 if (!s_in_dpichanged)
3346 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003347#endif
3348 gui_mch_free_font(gui.norm_font);
3349 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003350 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351 GetFontSize(font);
3352
K.Takatac81e9bf2022-01-16 14:15:49 +00003353 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354 if (p != NULL)
3355 {
3356 hl_set_font_name(p);
3357
Bram Moolenaar734a8672019-12-02 22:49:38 +01003358 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3360 {
3361 vim_free(p_guifont);
3362 p_guifont = p;
3363 }
3364 else
3365 vim_free(p);
3366 }
3367
3368 gui_mch_free_font(gui.ital_font);
3369 gui.ital_font = NOFONT;
3370 gui_mch_free_font(gui.bold_font);
3371 gui.bold_font = NOFONT;
3372 gui_mch_free_font(gui.boldital_font);
3373 gui.boldital_font = NOFONT;
3374
3375 if (!lf.lfItalic)
3376 {
3377 lf.lfItalic = TRUE;
3378 gui.ital_font = get_font_handle(&lf);
3379 lf.lfItalic = FALSE;
3380 }
3381 if (lf.lfWeight < FW_BOLD)
3382 {
3383 lf.lfWeight = FW_BOLD;
3384 gui.bold_font = get_font_handle(&lf);
3385 if (!lf.lfItalic)
3386 {
3387 lf.lfItalic = TRUE;
3388 gui.boldital_font = get_font_handle(&lf);
3389 }
3390 }
3391
3392 return OK;
3393}
3394
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395/*
3396 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003397 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398 */
3399 int
3400gui_mch_maximized(void)
3401{
3402 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003403 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404
3405 wp.length = sizeof(WINDOWPLACEMENT);
3406 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003407 {
3408 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003409 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003410 && wp.flags == WPF_RESTORETOMAXIMIZED))
3411 return TRUE;
3412 if (wp.showCmd == SW_SHOWMINIMIZED)
3413 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003414
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003415 // Assume the window is snapped when the sizes from two APIs differ.
3416 GetWindowRect(s_hwnd, &rc);
3417 if ((rc.right - rc.left !=
3418 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3419 || (rc.bottom - rc.top !=
3420 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3421 return TRUE;
3422 }
3423 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003424}
3425
3426/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003427 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3428 * is set. Compute the new Rows and Columns. This is like resizing the
3429 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430 */
3431 void
3432gui_mch_newfont(void)
3433{
3434 RECT rect;
3435
3436 GetWindowRect(s_hwnd, &rect);
3437 if (win_socket_id == 0)
3438 {
3439 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003440 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3441 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003442 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003443 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3444 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3445 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3446 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003447 }
3448 else
3449 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003450 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003452 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003453 }
3454}
3455
3456/*
3457 * Set the window title
3458 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459 void
3460gui_mch_settitle(
3461 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003462 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463{
3464 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3465}
3466
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003467#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003468// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3469// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470static LPCSTR mshape_idcs[] =
3471{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003472 IDC_ARROW, // arrow
3473 MAKEINTRESOURCE(0), // blank
3474 IDC_IBEAM, // beam
3475 IDC_SIZENS, // updown
3476 IDC_SIZENS, // udsizing
3477 IDC_SIZEWE, // leftright
3478 IDC_SIZEWE, // lrsizing
3479 IDC_WAIT, // busy
3480 IDC_NO, // no
3481 IDC_ARROW, // crosshair
3482 IDC_ARROW, // hand1
3483 IDC_ARROW, // hand2
3484 IDC_ARROW, // pencil
3485 IDC_ARROW, // question
3486 IDC_ARROW, // right-arrow
3487 IDC_UPARROW, // up-arrow
3488 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003489};
3490
3491 void
3492mch_set_mouse_shape(int shape)
3493{
3494 LPCSTR idc;
3495
3496 if (shape == MSHAPE_HIDE)
3497 ShowCursor(FALSE);
3498 else
3499 {
3500 if (shape >= MSHAPE_NUMBERED)
3501 idc = IDC_ARROW;
3502 else
3503 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003504 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003505 if (!p_mh)
3506 {
3507 POINT mp;
3508
Bram Moolenaar734a8672019-12-02 22:49:38 +01003509 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003510 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511 (void)SetCursorPos(mp.x, mp.y);
3512 ShowCursor(TRUE);
3513 }
3514 }
3515}
3516#endif
3517
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003518#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520 * Wide version of convert_filter().
3521 */
3522 static WCHAR *
3523convert_filterW(char_u *s)
3524{
3525 char_u *tmp;
3526 int len;
3527 WCHAR *res;
3528
3529 tmp = convert_filter(s);
3530 if (tmp == NULL)
3531 return NULL;
3532 len = (int)STRLEN(s) + 3;
3533 res = enc_to_utf16(tmp, &len);
3534 vim_free(tmp);
3535 return res;
3536}
3537
3538/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003539 * Pop open a file browser and return the file selected, in allocated memory,
3540 * or NULL if Cancel is hit.
3541 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3542 * title - Title message for the file browser dialog.
3543 * dflt - Default name of file.
3544 * ext - Default extension to be added to files without extensions.
3545 * initdir - directory in which to open the browser (NULL = current dir)
3546 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003547 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003548 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003549gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550 int saving,
3551 char_u *title,
3552 char_u *dflt,
3553 char_u *ext,
3554 char_u *initdir,
3555 char_u *filter)
3556{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003557 // We always use the wide function. This means enc_to_utf16() must work,
3558 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003559 OPENFILENAMEW fileStruct;
3560 WCHAR fileBuf[MAXPATHL];
3561 WCHAR *wp;
3562 int i;
3563 WCHAR *titlep = NULL;
3564 WCHAR *extp = NULL;
3565 WCHAR *initdirp = NULL;
3566 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003567 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003568 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569
3570 if (dflt == NULL)
3571 fileBuf[0] = NUL;
3572 else
3573 {
3574 wp = enc_to_utf16(dflt, NULL);
3575 if (wp == NULL)
3576 fileBuf[0] = NUL;
3577 else
3578 {
3579 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3580 fileBuf[i] = wp[i];
3581 fileBuf[i] = NUL;
3582 vim_free(wp);
3583 }
3584 }
3585
Bram Moolenaar734a8672019-12-02 22:49:38 +01003586 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587 filterp = convert_filterW(filter);
3588
Bram Moolenaara80faa82020-04-12 19:37:17 +02003589 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003590# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003591 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003592 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003593# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003595# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596
3597 if (title != NULL)
3598 titlep = enc_to_utf16(title, NULL);
3599 fileStruct.lpstrTitle = titlep;
3600
3601 if (ext != NULL)
3602 extp = enc_to_utf16(ext, NULL);
3603 fileStruct.lpstrDefExt = extp;
3604
3605 fileStruct.lpstrFile = fileBuf;
3606 fileStruct.nMaxFile = MAXPATHL;
3607 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003608 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3609 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610 if (initdir != NULL && *initdir != NUL)
3611 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003612 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003613 initdirp = enc_to_utf16(initdir, NULL);
3614 if (initdirp != NULL)
3615 {
3616 for (wp = initdirp; *wp != NUL; ++wp)
3617 if (*wp == '/')
3618 *wp = '\\';
3619 }
3620 fileStruct.lpstrInitialDir = initdirp;
3621 }
3622
3623 /*
3624 * TODO: Allow selection of multiple files. Needs another arg to this
3625 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3626 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3627 * files that don't exist yet, so I haven't put it in. What about
3628 * OFN_PATHMUSTEXIST?
3629 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3630 */
3631 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003632# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633 if (curbuf->b_p_bin)
3634 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003635# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003636 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003637 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003638 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003639 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003640
3641 vim_free(filterp);
3642 vim_free(initdirp);
3643 vim_free(titlep);
3644 vim_free(extp);
3645
K.Takata14b8d6a2022-01-20 15:05:22 +00003646 if (!ret)
3647 return NULL;
3648
3649 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003651 if (p == NULL)
3652 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653
Bram Moolenaar734a8672019-12-02 22:49:38 +01003654 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 SetFocus(s_hwnd);
3656
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003658 q = vim_strsave(shorten_fname1(p));
3659 vim_free(p);
3660 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003661}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663
3664/*
3665 * Convert the string s to the proper format for a filter string by replacing
3666 * the \t and \n delimiters with \0.
3667 * Returns the converted string in allocated memory.
3668 *
3669 * Keep in sync with convert_filterW() above!
3670 */
3671 static char_u *
3672convert_filter(char_u *s)
3673{
3674 char_u *res;
3675 unsigned s_len = (unsigned)STRLEN(s);
3676 unsigned i;
3677
3678 res = alloc(s_len + 3);
3679 if (res != NULL)
3680 {
3681 for (i = 0; i < s_len; ++i)
3682 if (s[i] == '\t' || s[i] == '\n')
3683 res[i] = '\0';
3684 else
3685 res[i] = s[i];
3686 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003687 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688 res[s_len + 1] = NUL;
3689 res[s_len + 2] = NUL;
3690 }
3691 return res;
3692}
3693
3694/*
3695 * Select a directory.
3696 */
3697 char_u *
3698gui_mch_browsedir(char_u *title, char_u *initdir)
3699{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003700 // We fake this: Use a filter that doesn't select anything and a default
3701 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3703 initdir, (char_u *)_("Directory\t*.nothing\n"));
3704}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003705#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003707 static void
3708_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003709 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 HDROP hDrop)
3711{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003712#define BUFPATHLEN _MAX_PATH
3713#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715 char szFile[BUFPATHLEN];
3716 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3717 UINT i;
3718 char_u **fnames;
3719 POINT pt;
3720 int_u modifiers = 0;
3721
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725 DragQueryPoint(hDrop, &pt);
3726 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3727
3728 reset_VIsual();
3729
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003730 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003731
3732 if (fnames != NULL)
3733 for (i = 0; i < cFiles; ++i)
3734 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3736 fnames[i] = utf16_to_enc(wszFile, NULL);
3737 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738 {
3739 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3740 fnames[i] = vim_strsave((char_u *)szFile);
3741 }
3742 }
3743
3744 DragFinish(hDrop);
3745
3746 if (fnames != NULL)
3747 {
3748 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3749 modifiers |= MOUSE_SHIFT;
3750 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3751 modifiers |= MOUSE_CTRL;
3752 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3753 modifiers |= MOUSE_ALT;
3754
3755 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3756
3757 s_need_activate = TRUE;
3758 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759}
3760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761 static int
3762_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003763 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 HWND hwndCtl,
3765 UINT code,
3766 int pos)
3767{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003768 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769 scrollbar_T *sb, *sb_info;
3770 long val;
3771 int dragging = FALSE;
3772 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 SCROLLINFO si;
3774
3775 si.cbSize = sizeof(si);
3776 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777
3778 sb = gui_mswin_find_scrollbar(hwndCtl);
3779 if (sb == NULL)
3780 return 0;
3781
Bram Moolenaar734a8672019-12-02 22:49:38 +01003782 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 {
3784 /*
3785 * Careful: need to get scrollbar info out of first (left) scrollbar
3786 * for window, but keep real scrollbar too because we must pass it to
3787 * gui_drag_scrollbar().
3788 */
3789 sb_info = &sb->wp->w_scrollbars[0];
3790 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003791 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003792 sb_info = sb;
3793 val = sb_info->value;
3794
3795 switch (code)
3796 {
3797 case SB_THUMBTRACK:
3798 val = pos;
3799 dragging = TRUE;
3800 if (sb->scroll_shift > 0)
3801 val <<= sb->scroll_shift;
3802 break;
3803 case SB_LINEDOWN:
3804 val++;
3805 break;
3806 case SB_LINEUP:
3807 val--;
3808 break;
3809 case SB_PAGEDOWN:
3810 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3811 break;
3812 case SB_PAGEUP:
3813 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3814 break;
3815 case SB_TOP:
3816 val = 0;
3817 break;
3818 case SB_BOTTOM:
3819 val = sb_info->max;
3820 break;
3821 case SB_ENDSCROLL:
3822 if (prev_code == SB_THUMBTRACK)
3823 {
3824 /*
3825 * "pos" only gives us 16-bit data. In case of large file,
3826 * use GetScrollPos() which returns 32-bit. Unfortunately it
3827 * is not valid while the scrollbar is being dragged.
3828 */
3829 val = GetScrollPos(hwndCtl, SB_CTL);
3830 if (sb->scroll_shift > 0)
3831 val <<= sb->scroll_shift;
3832 }
3833 break;
3834
3835 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003836 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003837 return 0;
3838 }
3839 prev_code = code;
3840
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3842 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843
3844 /*
3845 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3846 */
3847 if (sb->wp != NULL)
3848 {
3849 scrollbar_T *sba = sb->wp->w_scrollbars;
3850 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3851
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853 }
3854
Bram Moolenaar734a8672019-12-02 22:49:38 +01003855 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003856 s_busy_processing = TRUE;
3857
Bram Moolenaar734a8672019-12-02 22:49:38 +01003858 // When "allow_scrollbar" is FALSE still need to remember the new
3859 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003860 dont_scroll = !allow_scrollbar;
3861
Bram Moolenaara338adc2018-01-31 20:51:47 +01003862 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003864 mch_enable_flush();
3865 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003866
3867 s_busy_processing = FALSE;
3868 dont_scroll = dont_scroll_save;
3869
3870 return 0;
3871}
3872
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#ifdef FEAT_XPM_W32
3875# include "xpm_w32.h"
3876#endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
Bram Moolenaar734a8672019-12-02 22:49:38 +01003879// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880#define TEAROFF_PADDING_X 2
3881#define TEAROFF_BUTTON_PAD_X 8
3882#define TEAROFF_MIN_WIDTH 200
3883#define TEAROFF_SUBMENU_LABEL ">>"
3884#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3885
3886
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003887#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888# define ID_BEVAL_TOOLTIP 200
3889# define BEVAL_TEXT_LEN MAXPATHL
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003892static UINT_PTR beval_timer_id = 0;
3893static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003894#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003895
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003896
Bram Moolenaar734a8672019-12-02 22:49:38 +01003897// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
3899#ifdef FEAT_MENU
3900static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903/*
3904 * Use the system font for dialogs and tear-off menus. Remove this line to
3905 * use DLG_FONT_NAME.
3906 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003907#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910#define VIM_CLASSW L"Vim"
3911
Bram Moolenaar734a8672019-12-02 22:49:38 +01003912// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3913// thus there should be room for every dialog. For tearoffs it's made bigger
3914// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#define DLG_ALLOC_SIZE 16 * 1024
3916
3917/*
3918 * stuff for dialogs, menus, tearoffs etc.
3919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920static PWORD
3921add_dialog_element(
3922 PWORD p,
3923 DWORD lStyle,
3924 WORD x,
3925 WORD y,
3926 WORD w,
3927 WORD h,
3928 WORD Id,
3929 WORD clss,
3930 const char *caption);
3931static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003932static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003933#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936static void get_dialog_font_metrics(void);
3937
3938static int dialog_default_button = -1;
3939
Bram Moolenaar734a8672019-12-02 22:49:38 +01003940// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943#ifdef FEAT_TOOLBAR
3944static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00003945static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003946static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00003948#else
3949# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#endif
3951
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003952#ifdef FEAT_GUI_TABLINE
3953static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003954static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003955#endif
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#ifdef FEAT_MBYTE_IME
3958static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
3959static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
3960#endif
3961#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
3962# ifdef NOIME
3963typedef struct tagCOMPOSITIONFORM {
3964 DWORD dwStyle;
3965 POINT ptCurrentPos;
3966 RECT rcArea;
3967} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
3968typedef HANDLE HIMC;
3969# endif
3970
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003971static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003972static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
3973static HIMC (WINAPI *pImmGetContext)(HWND);
3974static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
3975static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
3976static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
3977static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003978static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
3979static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003980static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
3981static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00003982static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983static void dyn_imm_load(void);
3984#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985# define pImmGetCompositionStringW ImmGetCompositionStringW
3986# define pImmGetContext ImmGetContext
3987# define pImmAssociateContext ImmAssociateContext
3988# define pImmReleaseContext ImmReleaseContext
3989# define pImmGetOpenStatus ImmGetOpenStatus
3990# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003991# define pImmGetCompositionFontW ImmGetCompositionFontW
3992# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993# define pImmSetCompositionWindow ImmSetCompositionWindow
3994# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00003995# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996#endif
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#ifdef FEAT_MENU
3999/*
4000 * Figure out how high the menu bar is at the moment.
4001 */
4002 static int
4003gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004004 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 static int old_menu_height = -1;
4007
4008 RECT rc1, rc2;
4009 int num;
4010 int menu_height;
4011
4012 if (gui.menu_is_active)
4013 num = GetMenuItemCount(s_menuBar);
4014 else
4015 num = 0;
4016
4017 if (num == 0)
4018 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004019 else if (IsMinimized(s_hwnd))
4020 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004021 // The height of the menu cannot be determined while the window is
4022 // minimized. Take the previous height if the menu is changed in that
4023 // state, to avoid that Vim's vertical window size accidentally
4024 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004025 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 else
4028 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004029 /*
4030 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4031 * seem to have been set yet, so menu wraps in default window
4032 * width which is very narrow. Instead just return height of a
4033 * single menu item. Will still be wrong when the menu really
4034 * should wrap over more than one line.
4035 */
4036 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4037 if (gui.starting)
4038 menu_height = rc1.bottom - rc1.top + 1;
4039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004041 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4042 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044 }
4045
4046 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004047 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004048 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 return menu_height;
4051}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004052#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054
4055/*
4056 * Setup for the Intellimouse
4057 */
4058 static void
4059init_mouse_wheel(void)
4060{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004061 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
Bram Moolenaar734a8672019-12-02 22:49:38 +01004063 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004064 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4065 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066}
4067
4068
Bram Moolenaarae20f342019-11-05 21:09:23 +01004069/*
4070 * Intellimouse wheel handler.
4071 * Treat a mouse wheel event as if it were a scroll request.
4072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 static void
4074_OnMouseWheel(
4075 HWND hwnd,
4076 short zDelta)
4077{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 int i;
4079 int size;
4080 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004081 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (mouse_scroll_lines == 0)
4084 init_mouse_wheel();
4085
Bram Moolenaarae20f342019-11-05 21:09:23 +01004086 wp = gui_mouse_window(FIND_POPUP);
4087
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004088#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004089 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004090 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004091 cmdarg_T cap;
4092 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004093
Bram Moolenaarae20f342019-11-05 21:09:23 +01004094 // Mouse hovers over popup window, scroll it if possible.
4095 mouse_row = wp->w_winrow;
4096 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004097 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004098 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4099 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4100 clear_oparg(&oa);
4101 cap.oap = &oa;
4102 nv_mousescroll(&cap);
4103 update_screen(0);
4104 setcursor();
4105 out_flush();
4106 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004107 }
4108#endif
4109
Bram Moolenaarae20f342019-11-05 21:09:23 +01004110 if (wp == NULL || !p_scf)
4111 wp = curwin;
4112
4113 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4114 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4115 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4116 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4117 else
4118 return;
4119 size = wp->w_height;
4120
Bram Moolenaara338adc2018-01-31 20:51:47 +01004121 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (mouse_scroll_lines > 0
4123 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4124 {
4125 for (i = mouse_scroll_lines; i > 0; --i)
4126 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4127 }
4128 else
4129 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004130 mch_enable_flush();
4131 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132}
4133
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004134#ifdef USE_SYSMENU_FONT
4135/*
4136 * Get Menu Font.
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004140gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004141{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004142 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004143
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004144 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4145 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004146 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004147 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004148 &nm,
4149 0))
4150 return FAIL;
4151 *lf = nm.lfMenuFont;
4152 return OK;
4153}
4154#endif
4155
4156
4157#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4158/*
4159 * Set the GUI tabline font to the system menu font
4160 */
4161 static void
4162set_tabline_font(void)
4163{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004164 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004165 HFONT font;
4166 HWND hwnd;
4167 HDC hdc;
4168 HFONT hfntOld;
4169 TEXTMETRIC tm;
4170
4171 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4172 return;
4173
K.Takatac81e9bf2022-01-16 14:15:49 +00004174 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004175 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004176
4177 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4178
4179 /*
4180 * Compute the height of the font used for the tab text
4181 */
4182 hwnd = GetDesktopWindow();
4183 hdc = GetWindowDC(hwnd);
4184 hfntOld = SelectFont(hdc, font);
4185
4186 GetTextMetrics(hdc, &tm);
4187
4188 SelectFont(hdc, hfntOld);
4189 ReleaseDC(hwnd, hdc);
4190
4191 /*
4192 * The space used by the tab border and the space between the tab label
4193 * and the tab border is included as 7.
4194 */
4195 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4196}
K.Takatac81e9bf2022-01-16 14:15:49 +00004197#else
4198# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004199#endif
4200
Bram Moolenaar520470a2005-06-16 21:59:56 +00004201/*
4202 * Invoked when a setting was changed.
4203 */
4204 static LRESULT CALLBACK
4205_OnSettingChange(UINT n)
4206{
4207 if (n == SPI_SETWHEELSCROLLLINES)
4208 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4209 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004210 if (n == SPI_SETNONCLIENTMETRICS)
4211 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004212 return 0;
4213}
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#ifdef FEAT_NETBEANS_INTG
4216 static void
4217_OnWindowPosChanged(
4218 HWND hwnd,
4219 const LPWINDOWPOS lpwpos)
4220{
4221 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004222 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4225 || lpwpos->cx != cx || lpwpos->cy != cy))
4226 {
4227 x = lpwpos->x;
4228 y = lpwpos->y;
4229 cx = lpwpos->cx;
4230 cy = lpwpos->cy;
4231 netbeans_frame_moved(x, y);
4232 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004233 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004234 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236#endif
4237
K.Takata4f2417f2021-06-05 16:25:32 +02004238
4239static HWND hwndTip = NULL;
4240
4241 static void
4242show_sizing_tip(int cols, int rows)
4243{
4244 TOOLINFOA ti = {sizeof(ti)};
4245 char buf[32];
4246
4247 ti.hwnd = s_hwnd;
4248 ti.uId = (UINT_PTR)s_hwnd;
4249 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4250 ti.lpszText = buf;
4251 sprintf(buf, "%dx%d", cols, rows);
4252 if (hwndTip == NULL)
4253 {
4254 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4255 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4256 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4257 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4258 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4259 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4260 }
4261 else
4262 {
4263 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4264 }
4265 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4266}
4267
4268 static void
4269destroy_sizing_tip(void)
4270{
4271 if (hwndTip != NULL)
4272 {
4273 DestroyWindow(hwndTip);
4274 hwndTip = NULL;
4275 }
4276}
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 static int
4279_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 UINT fwSide,
4281 LPRECT lprc)
4282{
4283 int w, h;
4284 int valid_w, valid_h;
4285 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004286 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 w = lprc->right - lprc->left;
4289 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004290 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 w_offset = w - valid_w;
4292 h_offset = h - valid_h;
4293
4294 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4295 || fwSide == WMSZ_BOTTOMLEFT)
4296 lprc->left += w_offset;
4297 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4298 || fwSide == WMSZ_BOTTOMRIGHT)
4299 lprc->right -= w_offset;
4300
4301 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4302 || fwSide == WMSZ_TOPRIGHT)
4303 lprc->top += h_offset;
4304 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4305 || fwSide == WMSZ_BOTTOMRIGHT)
4306 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004307
4308 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return TRUE;
4310}
4311
K.Takata92000e22022-01-20 15:10:57 +00004312#ifdef FEAT_GUI_TABLINE
4313 static void
4314_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4315{
4316 if (gui_mch_showing_tabline())
4317 {
4318 POINT pt;
4319 RECT rect;
4320
4321 /*
4322 * If the cursor is on the tabline, display the tab menu
4323 */
4324 GetCursorPos(&pt);
4325 GetWindowRect(s_textArea, &rect);
4326 if (pt.y < rect.top)
4327 {
4328 show_tabline_popup_menu();
4329 return;
4330 }
4331 }
4332 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4333}
4334
4335 static void
4336_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4337{
4338 /*
4339 * If the user double clicked the tabline, create a new tab
4340 */
4341 if (gui_mch_showing_tabline())
4342 {
4343 POINT pt;
4344 RECT rect;
4345
4346 GetCursorPos(&pt);
4347 GetWindowRect(s_textArea, &rect);
4348 if (pt.y < rect.top)
4349 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4350 }
4351 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4352}
4353#endif
4354
4355 static UINT
4356_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4357{
4358 UINT result;
4359 int x, y;
4360
4361 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4362 if (result != HTCLIENT)
4363 return result;
4364
4365#ifdef FEAT_GUI_TABLINE
4366 if (gui_mch_showing_tabline())
4367 {
4368 RECT rct;
4369
4370 // If the cursor is on the GUI tabline, don't process this event
4371 GetWindowRect(s_textArea, &rct);
4372 if (yPos < rct.top)
4373 return result;
4374 }
4375#endif
4376 (void)gui_mch_get_winpos(&x, &y);
4377 xPos -= x;
4378
4379 if (xPos < 48) // <VN> TODO should use system metric?
4380 return HTBOTTOMLEFT;
4381 else
4382 return HTBOTTOMRIGHT;
4383}
4384
4385#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4386 static LRESULT
4387_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4388{
4389 switch (hdr->code)
4390 {
4391 case TTN_GETDISPINFOW:
4392 case TTN_GETDISPINFO:
4393 {
4394 char_u *str = NULL;
4395 static void *tt_text = NULL;
4396
4397 VIM_CLEAR(tt_text);
4398
4399# ifdef FEAT_GUI_TABLINE
4400 if (gui_mch_showing_tabline()
4401 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4402 {
4403 POINT pt;
4404 /*
4405 * Mouse is over the GUI tabline. Display the
4406 * tooltip for the tab under the cursor
4407 *
4408 * Get the cursor position within the tab control
4409 */
4410 GetCursorPos(&pt);
4411 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4412 {
4413 TCHITTESTINFO htinfo;
4414 int idx;
4415
4416 /*
4417 * Get the tab under the cursor
4418 */
4419 htinfo.pt.x = pt.x;
4420 htinfo.pt.y = pt.y;
4421 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4422 if (idx != -1)
4423 {
4424 tabpage_T *tp;
4425
4426 tp = find_tabpage(idx + 1);
4427 if (tp != NULL)
4428 {
4429 get_tabline_label(tp, TRUE);
4430 str = NameBuff;
4431 }
4432 }
4433 }
4434 }
4435# endif
4436# ifdef FEAT_TOOLBAR
4437# ifdef FEAT_GUI_TABLINE
4438 else
4439# endif
4440 {
4441 UINT idButton;
4442 vimmenu_T *pMenu;
4443
4444 idButton = (UINT) hdr->idFrom;
4445 pMenu = gui_mswin_find_menu(root_menu, idButton);
4446 if (pMenu)
4447 str = pMenu->strings[MENU_INDEX_TIP];
4448 }
4449# endif
4450 if (str == NULL)
4451 break;
4452
4453 // Set the maximum width, this also enables using \n for
4454 // line break.
4455 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4456
4457 if (hdr->code == TTN_GETDISPINFOW)
4458 {
4459 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4460
4461 tt_text = enc_to_utf16(str, NULL);
4462 lpdi->lpszText = tt_text;
4463 // can't show tooltip if failed
4464 }
4465 else
4466 {
4467 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4468
4469 if (STRLEN(str) < sizeof(lpdi->szText)
4470 || ((tt_text = vim_strsave(str)) == NULL))
4471 vim_strncpy((char_u *)lpdi->szText, str,
4472 sizeof(lpdi->szText) - 1);
4473 else
4474 lpdi->lpszText = tt_text;
4475 }
4476 }
4477 break;
4478
4479# ifdef FEAT_GUI_TABLINE
4480 case TCN_SELCHANGE:
4481 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4482 {
4483 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4484 return 0L;
4485 }
4486 break;
4487
4488 case NM_RCLICK:
4489 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4490 {
4491 show_tabline_popup_menu();
4492 return 0L;
4493 }
4494 break;
4495# endif
4496
4497 default:
4498 break;
4499 }
4500 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4501}
4502#endif
4503
4504#if defined(MENUHINTS) && defined(FEAT_MENU)
4505 static LRESULT
4506_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4507{
4508 if (((UINT) HIWORD(wParam)
4509 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4510 == MF_HILITE
4511 && (State & CMDLINE) == 0)
4512 {
4513 UINT idButton;
4514 vimmenu_T *pMenu;
4515 static int did_menu_tip = FALSE;
4516
4517 if (did_menu_tip)
4518 {
4519 msg_clr_cmdline();
4520 setcursor();
4521 out_flush();
4522 did_menu_tip = FALSE;
4523 }
4524
4525 idButton = (UINT)LOWORD(wParam);
4526 pMenu = gui_mswin_find_menu(root_menu, idButton);
4527 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4528 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4529 {
4530 ++msg_hist_off;
4531 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4532 --msg_hist_off;
4533 setcursor();
4534 out_flush();
4535 did_menu_tip = TRUE;
4536 }
4537 return 0L;
4538 }
4539 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4540}
4541#endif
4542
K.Takatac81e9bf2022-01-16 14:15:49 +00004543 static LRESULT
4544_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4545{
4546 s_dpi = ydpi;
4547 s_in_dpichanged = TRUE;
4548 //TRACE("DPI: %d", ydpi);
4549
4550 update_scrollbar_size();
4551 update_toolbar_size();
4552 set_tabline_font();
4553
4554 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4555 gui_get_wide_font();
4556 gui_mswin_get_menu_height(FALSE);
4557#ifdef FEAT_MBYTE_IME
4558 im_set_position(gui.row, gui.col);
4559#endif
4560 InvalidateRect(hwnd, NULL, TRUE);
4561
4562 s_in_dpichanged = FALSE;
4563 return 0L;
4564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565
4566
4567 static LRESULT CALLBACK
4568_WndProc(
4569 HWND hwnd,
4570 UINT uMsg,
4571 WPARAM wParam,
4572 LPARAM lParam)
4573{
4574 /*
4575 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4576 hwnd, uMsg, wParam, lParam);
4577 */
4578
4579 HandleMouseHide(uMsg, lParam);
4580
4581 s_uMsg = uMsg;
4582 s_wParam = wParam;
4583 s_lParam = lParam;
4584
4585 switch (uMsg)
4586 {
4587 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4588 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004589 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004591 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4593 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4594 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4595 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4596#ifdef FEAT_MENU
4597 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4598#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004599 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4600 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4602 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004603 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4604 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4606 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4607 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4608#ifdef FEAT_NETBEANS_INTG
4609 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4610#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004611#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004612 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4613 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004614#endif
K.Takata92000e22022-01-20 15:10:57 +00004615 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004616
Bram Moolenaar734a8672019-12-02 22:49:38 +01004617 case WM_QUERYENDSESSION: // System wants to go down.
4618 gui_shell_closed(); // Will exit when no changed buffers.
4619 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004622 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004625 return 0L;
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 break;
4628
4629 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004630 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4631 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004632 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return 0L;
4634
4635 case WM_SYSCHAR:
4636 /*
4637 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4638 * shortcut key, handle like a typed ALT key, otherwise call Windows
4639 * ALT key handling.
4640 */
4641#ifdef FEAT_MENU
4642 if ( !gui.menu_is_active
4643 || p_wak[0] == 'n'
4644 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4645 )
4646#endif
4647 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004648 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return 0L;
4650 }
4651#ifdef FEAT_MENU
4652 else
K.Takata4ac893f2022-01-20 12:44:28 +00004653 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655
4656 case WM_SYSKEYUP:
4657#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004658 // This used to be done only when menu is active: ALT key is used for
4659 // that. But that caused problems when menu is disabled and using
4660 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4661 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004662 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004664 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
4666
K.Takata4f2417f2021-06-05 16:25:32 +02004667 case WM_EXITSIZEMOVE:
4668 destroy_sizing_tip();
4669 break;
4670
Bram Moolenaar734a8672019-12-02 22:49:38 +01004671 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004672 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 case WM_MOUSEWHEEL:
4675 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004676 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
Bram Moolenaar734a8672019-12-02 22:49:38 +01004678 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004679 case WM_SETTINGCHANGE:
4680 return _OnSettingChange((UINT)wParam);
4681
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004682#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004684 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
K.Takata92000e22022-01-20 15:10:57 +00004686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#if defined(MENUHINTS) && defined(FEAT_MENU)
4688 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004689 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
4692#ifdef FEAT_MBYTE_IME
4693 case WM_IME_NOTIFY:
4694 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004695 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004696 return 1L;
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 case WM_IME_COMPOSITION:
4699 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004700 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004701 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004703 case WM_DPICHANGED:
4704 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4705 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
4707 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004709 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#endif
K.Takata92000e22022-01-20 15:10:57 +00004712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714
K.Takata4ac893f2022-01-20 12:44:28 +00004715 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
4718/*
4719 * End of call-back routines
4720 */
4721
Bram Moolenaar734a8672019-12-02 22:49:38 +01004722// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723HWND vim_parent_hwnd = NULL;
4724
4725 static BOOL CALLBACK
4726FindWindowTitle(HWND hwnd, LPARAM lParam)
4727{
4728 char buf[2048];
4729 char *title = (char *)lParam;
4730
4731 if (GetWindowText(hwnd, buf, sizeof(buf)))
4732 {
4733 if (strstr(buf, title) != NULL)
4734 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004735 // Found it. Store the window ref. and quit searching if MDI
4736 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004738 if (vim_parent_hwnd != NULL)
4739 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004742 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
4745/*
4746 * Invoked for '-P "title"' argument: search for parent application to open
4747 * our window in.
4748 */
4749 void
4750gui_mch_set_parent(char *title)
4751{
4752 EnumWindows(FindWindowTitle, (LPARAM)title);
4753 if (vim_parent_hwnd == NULL)
4754 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004755 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 mch_exit(2);
4757 }
4758}
4759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004760#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 static void
4762ole_error(char *arg)
4763{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004764 char buf[IOSIZE];
4765
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004766# ifdef VIMDLL
4767 gui.in_use = mch_is_gui_executable();
4768# endif
4769
Bram Moolenaar734a8672019-12-02 22:49:38 +01004770 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004771 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004772 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004773 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004777#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4778 static char *
4779gvim_error(void)
4780{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004781 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004782
4783 if (starting)
4784 {
4785 mch_errmsg(msg);
4786 mch_errmsg("\n");
4787 mch_exit(2);
4788 }
4789 return msg;
4790}
4791
4792 char *
4793gui_mch_do_spawn(char_u *arg)
4794{
4795 int len;
4796# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4797 char_u *session = NULL;
4798 LPWSTR tofree1 = NULL;
4799# endif
4800 WCHAR name[MAX_PATH];
4801 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4802 STARTUPINFOW si = {sizeof(si)};
4803 PROCESS_INFORMATION pi;
4804
4805 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4806 goto error;
4807 p = wcsrchr(name, L'\\');
4808 if (p == NULL)
4809 goto error;
4810 // Replace the executable name from vim(d).exe to gvim(d).exe.
4811# ifdef DEBUG
4812 wcscpy(p + 1, L"gvimd.exe");
4813# else
4814 wcscpy(p + 1, L"gvim.exe");
4815# endif
4816
4817# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4818 if (starting)
4819# endif
4820 {
4821 // Pass the command line to the new process.
4822 p = GetCommandLineW();
4823 // Skip 1st argument.
4824 while (*p && *p != L' ' && *p != L'\t')
4825 {
4826 if (*p == L'"')
4827 {
4828 while (*p && *p != L'"')
4829 ++p;
4830 if (*p)
4831 ++p;
4832 }
4833 else
4834 ++p;
4835 }
4836 cmd = p;
4837 }
4838# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4839 else
4840 {
4841 // Create a session file and pass it to the new process.
4842 LPWSTR wsession;
4843 char_u *savebg;
4844 int ret;
4845
4846 session = vim_tempname('s', FALSE);
4847 if (session == NULL)
4848 goto error;
4849 savebg = p_bg;
4850 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4851 ret = write_session_file(session);
4852 vim_free(p_bg);
4853 p_bg = savebg;
4854 if (!ret)
4855 goto error;
4856 wsession = enc_to_utf16(session, NULL);
4857 if (wsession == NULL)
4858 goto error;
4859 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004860 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004861 if (cmd == NULL)
4862 {
4863 vim_free(wsession);
4864 goto error;
4865 }
4866 tofree1 = cmd;
4867 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4868 wsession, wsession);
4869 vim_free(wsession);
4870 }
4871# endif
4872
4873 // Check additional arguments to the `:gui` command.
4874 if (arg != NULL)
4875 {
4876 warg = enc_to_utf16(arg, NULL);
4877 if (warg == NULL)
4878 goto error;
4879 tofree2 = warg;
4880 }
4881 else
4882 warg = L"";
4883
4884 // Set up the new command line.
4885 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004886 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004887 if (newcmd == NULL)
4888 goto error;
4889 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4890
4891 // Spawn a new GUI process.
4892 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4893 NULL, NULL, &si, &pi))
4894 goto error;
4895 CloseHandle(pi.hProcess);
4896 CloseHandle(pi.hThread);
4897 mch_exit(0);
4898
4899error:
4900# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4901 if (session)
4902 mch_remove(session);
4903 vim_free(session);
4904 vim_free(tofree1);
4905# endif
4906 vim_free(newcmd);
4907 vim_free(tofree2);
4908 return gvim_error();
4909}
4910#endif
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
4913 * Parse the GUI related command-line arguments. Any arguments used are
4914 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02004915 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 */
4917 void
4918gui_mch_prepare(int *argc, char **argv)
4919{
4920 int silent = FALSE;
4921 int idx;
4922
Bram Moolenaar734a8672019-12-02 22:49:38 +01004923 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4925 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004926 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4928 && (argv[2][0] == '-' || argv[2][0] == '/'))
4929 {
4930 silent = TRUE;
4931 idx = 2;
4932 }
4933 else
4934 idx = 1;
4935
Bram Moolenaar734a8672019-12-02 22:49:38 +01004936 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (STRICMP(argv[idx] + 1, "register") == 0)
4938 {
4939#ifdef FEAT_OLE
4940 RegisterMe(silent);
4941 mch_exit(0);
4942#else
4943 if (!silent)
4944 ole_error("register");
4945 mch_exit(2);
4946#endif
4947 }
4948
Bram Moolenaar734a8672019-12-02 22:49:38 +01004949 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4951 {
4952#ifdef FEAT_OLE
4953 UnregisterMe(!silent);
4954 mch_exit(0);
4955#else
4956 if (!silent)
4957 ole_error("unregister");
4958 mch_exit(2);
4959#endif
4960 }
4961
Bram Moolenaar734a8672019-12-02 22:49:38 +01004962 // Ignore an -embedding argument. It is only relevant if the
4963 // application wants to treat the case when it is started manually
4964 // differently from the case where it is started via automation (and
4965 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4967 {
4968#ifdef FEAT_OLE
4969 *argc = 1;
4970#else
4971 ole_error("embedding");
4972 mch_exit(2);
4973#endif
4974 }
4975 }
4976
4977#ifdef FEAT_OLE
4978 {
4979 int bDoRestart = FALSE;
4980
4981 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004982 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (bDoRestart)
4984 mch_exit(0);
4985 }
4986#endif
4987
4988#ifdef FEAT_NETBEANS_INTG
4989 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004990 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 int arg;
4992
4993 for (arg = 1; arg < *argc; arg++)
4994 if (strncmp("-nb", argv[arg], 3) == 0)
4995 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 netbeansArg = argv[arg];
4997 mch_memmove(&argv[arg], &argv[arg + 1],
4998 (--*argc - arg) * sizeof(char *));
4999 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005000 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004}
5005
K.Takatac81e9bf2022-01-16 14:15:49 +00005006 static void
5007load_dpi_func(void)
5008{
5009 HMODULE hUser32;
5010
5011 hUser32 = GetModuleHandle("user32.dll");
5012 if (hUser32 == NULL)
5013 goto fail;
5014
5015 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5016 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5017 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5018 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5019 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5020 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5021
5022 if (pSetThreadDpiAwarenessContext != NULL)
5023 {
5024 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5025 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5026 if (oldctx != NULL)
5027 {
5028 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5029 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5030#ifdef DEBUG
5031 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5032 {
5033 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5034 }
5035#endif
5036 return;
5037 }
5038 }
5039
5040fail:
5041 // Disable PerMonitorV2 APIs.
5042 pGetDpiForSystem = stubGetDpiForSystem;
5043 pGetDpiForWindow = NULL;
5044 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5045 pSetThreadDpiAwarenessContext = NULL;
5046 pGetAwarenessFromDpiAwarenessContext = NULL;
5047}
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049/*
5050 * Initialise the GUI. Create all the windows, set up all the call-backs
5051 * etc.
5052 */
5053 int
5054gui_mch_init(void)
5055{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005057 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar734a8672019-12-02 22:49:38 +01005060 // Return here if the window was already opened (happens when
5061 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005063 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
5065 /*
5066 * Load the tearoff bitmap
5067 */
5068#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005069 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070#endif
5071
K.Takatac81e9bf2022-01-16 14:15:49 +00005072 load_dpi_func();
5073
5074 s_dpi = pGetDpiForSystem();
5075 update_scrollbar_size();
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005078 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#endif
5080 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005081#ifdef FEAT_TOOLBAR
5082 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5086
Bram Moolenaar734a8672019-12-02 22:49:38 +01005087 // First try using the wide version, so that we can use any title.
5088 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005089 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005091 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 wndclassw.lpfnWndProc = _WndProc;
5093 wndclassw.cbClsExtra = 0;
5094 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005095 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5097 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5098 wndclassw.hbrBackground = s_brush;
5099 wndclassw.lpszMenuName = NULL;
5100 wndclassw.lpszClassName = szVimWndClassW;
5101
K.Takata4ac893f2022-01-20 12:44:28 +00005102 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005103 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (vim_parent_hwnd != NULL)
5107 {
5108#ifdef HAVE_TRY_EXCEPT
5109 __try
5110 {
5111#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005112 // Open inside the specified parent window.
5113 // TODO: last argument should point to a CLIENTCREATESTRUCT
5114 // structure.
5115 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005117 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005118 WS_OVERLAPPEDWINDOW | WS_CHILD
5119 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5121 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005122 100, // Any value will do
5123 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005125 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef HAVE_TRY_EXCEPT
5127 }
5128 __except(EXCEPTION_EXECUTE_HANDLER)
5129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005130 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132#endif
5133 if (s_hwnd == NULL)
5134 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005135 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 mch_exit(2);
5137 }
5138 }
5139 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005140 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005141 // If the provided windowid is not valid reset it to zero, so that it
5142 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005143 if (IsWindow((HWND)win_socket_id) <= 0)
5144 win_socket_id = 0;
5145
Bram Moolenaar734a8672019-12-02 22:49:38 +01005146 // Create a window. If win_socket_id is not zero without border and
5147 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005148 s_hwnd = CreateWindowW(
5149 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005150 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5151 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005152 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5153 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005154 100, // Any value will do
5155 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005156 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005157 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005158 if (s_hwnd != NULL && win_socket_id != 0)
5159 {
5160 SetParent(s_hwnd, (HWND)win_socket_id);
5161 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5162 }
5163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165 if (s_hwnd == NULL)
5166 return FAIL;
5167
K.Takatac81e9bf2022-01-16 14:15:49 +00005168 if (pGetDpiForWindow != NULL)
5169 {
5170 s_dpi = pGetDpiForWindow(s_hwnd);
5171 update_scrollbar_size();
5172 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5173 }
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5176 dyn_imm_load();
5177#endif
5178
Bram Moolenaar734a8672019-12-02 22:49:38 +01005179 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005180 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005181 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005182 wndclassw.style = CS_OWNDC;
5183 wndclassw.lpfnWndProc = _TextAreaWndProc;
5184 wndclassw.cbClsExtra = 0;
5185 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005186 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005187 wndclassw.hIcon = NULL;
5188 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5189 wndclassw.hbrBackground = NULL;
5190 wndclassw.lpszMenuName = NULL;
5191 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005192
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005193 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return FAIL;
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005197 s_textArea = CreateWindowExW(
5198 0,
5199 szTextAreaClassW, L"Vim text area",
5200 WS_CHILD | WS_VISIBLE, 0, 0,
5201 100, // Any value will do for now
5202 100, // Any value will do for now
5203 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005204 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (s_textArea == NULL)
5207 return FAIL;
5208
Bram Moolenaar20321902016-02-17 12:30:17 +01005209#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005210 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005211 {
5212 HANDLE hIcon = NULL;
5213
5214 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005215 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005216 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005217#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219#ifdef FEAT_MENU
5220 s_menuBar = CreateMenu();
5221#endif
5222 s_hdc = GetDC(s_textArea);
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
Bram Moolenaar734a8672019-12-02 22:49:38 +01005226 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005227 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
Bram Moolenaar734a8672019-12-02 22:49:38 +01005229 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 gui_mch_def_colors();
5231
Bram Moolenaar734a8672019-12-02 22:49:38 +01005232 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5233 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 set_normal_colors();
5235
5236 /*
5237 * Check that none of the colors are the same as the background color.
5238 * Then store the current values as the defaults.
5239 */
5240 gui_check_colors();
5241 gui.def_norm_pixel = gui.norm_pixel;
5242 gui.def_back_pixel = gui.back_pixel;
5243
Bram Moolenaar734a8672019-12-02 22:49:38 +01005244 // Get the colors for the highlight groups (gui_check_colors() might have
5245 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 highlight_gui_started();
5247
5248 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005249 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005251 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253 /*
5254 * Set up for Intellimouse processing
5255 */
5256 init_mouse_wheel();
5257
5258 /*
5259 * compute a couple of metrics used for the dialogs
5260 */
5261 get_dialog_font_metrics();
5262#ifdef FEAT_TOOLBAR
5263 /*
5264 * Create the toolbar
5265 */
5266 initialise_toolbar();
5267#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005268#ifdef FEAT_GUI_TABLINE
5269 /*
5270 * Create the tabline
5271 */
5272 initialise_tabline();
5273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274#ifdef MSWIN_FIND_REPLACE
5275 /*
5276 * Initialise the dialog box stuff
5277 */
5278 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5279
Bram Moolenaar734a8672019-12-02 22:49:38 +01005280 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005282 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005284 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5286 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5287 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005290#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005291 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005292 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005293#endif
5294
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005295#ifdef FEAT_RENDER_OPTIONS
5296 if (p_rop)
5297 (void)gui_mch_set_rendering_options(p_rop);
5298#endif
5299
Bram Moolenaar748bf032005-02-02 23:04:36 +00005300theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005301 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005302 display_errors();
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 return OK;
5305}
5306
5307/*
5308 * Get the size of the screen, taking position on multiple monitors into
5309 * account (if supported).
5310 */
5311 static void
5312get_work_area(RECT *spi_rect)
5313{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005314 HMONITOR mon;
5315 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaar734a8672019-12-02 22:49:38 +01005317 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005318 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005319 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005321 moninfo.cbSize = sizeof(MONITORINFO);
5322 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005324 *spi_rect = moninfo.rcWork;
5325 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005328 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5330}
5331
5332/*
5333 * Set the size of the window to the given width and height in pixels.
5334 */
5335 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005336gui_mch_set_shellsize(
5337 int width,
5338 int height,
5339 int min_width UNUSED,
5340 int min_height UNUSED,
5341 int base_width UNUSED,
5342 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005343 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005346 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 // Try to keep window completely on screen.
5350 // Get position of the screen work area. This is the part that is not
5351 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 get_work_area(&workarea_rect);
5353
Bram Moolenaar734a8672019-12-02 22:49:38 +01005354 // Resizing a maximized window looks very strange, unzoom it first.
5355 // But don't do it when still starting up, it may have been requested in
5356 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005357 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005359
5360 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361
Bram Moolenaar734a8672019-12-02 22:49:38 +01005362 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005363 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5364 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5365 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5366 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5367 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5368 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
Bram Moolenaar734a8672019-12-02 22:49:38 +01005370 // The following should take care of keeping Vim on the same monitor, no
5371 // matter if the secondary monitor is left or right of the primary
5372 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005373 window_rect.right = window_rect.left + win_width;
5374 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005375
Bram Moolenaar734a8672019-12-02 22:49:38 +01005376 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005377 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5378 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005380 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5381 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005383 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5384 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005386 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5387 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005389 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5390 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392 SetActiveWindow(s_hwnd);
5393 SetFocus(s_hwnd);
5394
Bram Moolenaar734a8672019-12-02 22:49:38 +01005395 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397}
5398
5399
5400 void
5401gui_mch_set_scrollbar_thumb(
5402 scrollbar_T *sb,
5403 long val,
5404 long size,
5405 long max)
5406{
5407 SCROLLINFO info;
5408
5409 sb->scroll_shift = 0;
5410 while (max > 32767)
5411 {
5412 max = (max + 1) >> 1;
5413 val >>= 1;
5414 size >>= 1;
5415 ++sb->scroll_shift;
5416 }
5417
5418 if (sb->scroll_shift > 0)
5419 ++size;
5420
5421 info.cbSize = sizeof(info);
5422 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5423 info.nPos = val;
5424 info.nMin = 0;
5425 info.nMax = max;
5426 info.nPage = size;
5427 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5428}
5429
5430
5431/*
5432 * Set the current text font.
5433 */
5434 void
5435gui_mch_set_font(GuiFont font)
5436{
5437 gui.currFont = font;
5438}
5439
5440
5441/*
5442 * Set the current text foreground color.
5443 */
5444 void
5445gui_mch_set_fg_color(guicolor_T color)
5446{
5447 gui.currFgColor = color;
5448}
5449
5450/*
5451 * Set the current text background color.
5452 */
5453 void
5454gui_mch_set_bg_color(guicolor_T color)
5455{
5456 gui.currBgColor = color;
5457}
5458
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005459/*
5460 * Set the current text special color.
5461 */
5462 void
5463gui_mch_set_sp_color(guicolor_T color)
5464{
5465 gui.currSpColor = color;
5466}
5467
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005468#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469/*
5470 * Multi-byte handling, originally by Sung-Hoon Baek.
5471 * First static functions (no prototypes generated).
5472 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005473# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005474# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005475# endif
5476# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 * handle WM_IME_NOTIFY message
5480 */
5481 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005482_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 LRESULT lResult = 0;
5485 HIMC hImc;
5486
5487 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5488 return lResult;
5489 switch (dwCommand)
5490 {
5491 case IMN_SETOPENSTATUS:
5492 if (pImmGetOpenStatus(hImc))
5493 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005494 LOGFONTW lf = norm_logfont;
5495 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5496 // Work around when PerMonitorV2 is not enabled in the process level.
5497 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5498 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 im_set_position(gui.row, gui.col);
5500
Bram Moolenaar734a8672019-12-02 22:49:38 +01005501 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 State &= ~LANGMAP;
5503 if (State & INSERT)
5504 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005505# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005506 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5508 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005509 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 int old_row = gui.row;
5511 int old_col = gui.col;
5512
5513 // This must be called here before
5514 // status_redraw_curbuf(), otherwise the mode
5515 // message may appear in the wrong position.
5516 showmode();
5517 status_redraw_curbuf();
5518 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005519 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 gui.row = old_row;
5521 gui.col = old_col;
5522 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 }
5525 }
5526 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005527 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 lResult = 0;
5529 break;
5530 }
5531 pImmReleaseContext(hWnd, hImc);
5532 return lResult;
5533}
5534
5535 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005536_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 char_u *ret;
5539 int len;
5540
Bram Moolenaar734a8672019-12-02 22:49:38 +01005541 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 return 0;
5543
5544 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5545 if (ret != NULL)
5546 {
5547 add_to_input_buf_csi(ret, len);
5548 vim_free(ret);
5549 return 1;
5550 }
5551 return 0;
5552}
5553
5554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 * void GetResultStr()
5556 *
5557 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5558 * get complete composition string
5559 */
5560 static char_u *
5561GetResultStr(HWND hwnd, int GCS, int *lenp)
5562{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005563 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005564 LONG ret;
5565 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 char_u *convbuf = NULL;
5567
5568 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5569 return NULL;
5570
K.Takatab0b2b732022-01-19 12:59:21 +00005571 // Get the length of the composition string.
5572 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5573 if (ret <= 0)
5574 return NULL;
5575
5576 // Allocate the requested buffer plus space for the NUL character.
5577 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (buf == NULL)
5579 return NULL;
5580
K.Takatab0b2b732022-01-19 12:59:21 +00005581 // Reads in the composition string.
5582 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5583 *lenp = ret / sizeof(WCHAR);
5584
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005585 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 pImmReleaseContext(hwnd, hIMC);
5587 vim_free(buf);
5588 return convbuf;
5589}
5590#endif
5591
Bram Moolenaar734a8672019-12-02 22:49:38 +01005592// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005593#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595/*
5596 * set font to IM.
5597 */
5598 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005599im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600{
5601 HIMC hImc;
5602
5603 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5604 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005605 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 pImmReleaseContext(s_hwnd, hImc);
5607 }
5608}
5609
5610/*
5611 * Notify cursor position to IM.
5612 */
5613 void
5614im_set_position(int row, int col)
5615{
5616 HIMC hImc;
5617
5618 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5619 {
5620 COMPOSITIONFORM cfs;
5621
5622 cfs.dwStyle = CFS_POINT;
5623 cfs.ptCurrentPos.x = FILL_X(col);
5624 cfs.ptCurrentPos.y = FILL_Y(row);
5625 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005626 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5627 {
5628 // Work around when PerMonitorV2 is not enabled in the process level.
5629 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5630 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 pImmSetCompositionWindow(hImc, &cfs);
5633
5634 pImmReleaseContext(s_hwnd, hImc);
5635 }
5636}
5637
5638/*
5639 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5640 */
5641 void
5642im_set_active(int active)
5643{
5644 HIMC hImc;
5645 static HIMC hImcOld = (HIMC)0;
5646
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005647# ifdef VIMDLL
5648 if (!gui.in_use && !gui.starting)
5649 {
5650 mbyte_im_set_active(active);
5651 return;
5652 }
5653# endif
5654
Bram Moolenaar734a8672019-12-02 22:49:38 +01005655 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
5657 if (p_imdisable)
5658 {
5659 if (hImcOld == (HIMC)0)
5660 {
5661 hImcOld = pImmGetContext(s_hwnd);
5662 if (hImcOld)
5663 pImmAssociateContext(s_hwnd, (HIMC)0);
5664 }
5665 active = FALSE;
5666 }
5667 else if (hImcOld != (HIMC)0)
5668 {
5669 pImmAssociateContext(s_hwnd, hImcOld);
5670 hImcOld = (HIMC)0;
5671 }
5672
5673 hImc = pImmGetContext(s_hwnd);
5674 if (hImc)
5675 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005676 /*
5677 * for Korean ime
5678 */
5679 HKL hKL = GetKeyboardLayout(0);
5680
5681 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5682 {
5683 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5684 static BOOL bSaved = FALSE;
5685
5686 if (active)
5687 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005688 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005689 if (bSaved)
5690 pImmSetConversionStatus(hImc, dwConversionSaved,
5691 dwSentenceSaved);
5692 bSaved = FALSE;
5693 }
5694 else
5695 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005696 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005697 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5698 &dwSentenceSaved))
5699 {
5700 bSaved = TRUE;
5701 pImmSetConversionStatus(hImc,
5702 dwConversionSaved & ~(IME_CMODE_NATIVE
5703 | IME_CMODE_FULLSHAPE),
5704 dwSentenceSaved);
5705 }
5706 }
5707 }
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 pImmSetOpenStatus(hImc, active);
5710 pImmReleaseContext(s_hwnd, hImc);
5711 }
5712 }
5713}
5714
5715/*
5716 * Get IM status. When IM is on, return not 0. Else return 0.
5717 */
5718 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005719im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 int status = 0;
5722 HIMC hImc;
5723
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005724# ifdef VIMDLL
5725 if (!gui.in_use && !gui.starting)
5726 return mbyte_im_get_status();
5727# endif
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5730 {
5731 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5732 pImmReleaseContext(s_hwnd, hImc);
5733 }
5734 return status;
5735}
5736
Bram Moolenaar734a8672019-12-02 22:49:38 +01005737#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005740/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005741 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005742 */
5743 static void
5744latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5745{
5746 int c;
5747
Bram Moolenaarca003e12006-03-17 23:19:38 +00005748 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005749 {
5750 c = *text++;
5751 switch (c)
5752 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005753 case 0xa4: c = 0x20ac; break; // euro
5754 case 0xa6: c = 0x0160; break; // S hat
5755 case 0xa8: c = 0x0161; break; // S -hat
5756 case 0xb4: c = 0x017d; break; // Z hat
5757 case 0xb8: c = 0x017e; break; // Z -hat
5758 case 0xbc: c = 0x0152; break; // OE
5759 case 0xbd: c = 0x0153; break; // oe
5760 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005761 }
5762 *unicodebuf++ = c;
5763 }
5764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766#ifdef FEAT_RIGHTLEFT
5767/*
5768 * What is this for? In the case where you are using Win98 or Win2K or later,
5769 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5770 * reverses the string sent to the TextOut... family. This sucks, because we
5771 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5772 * way to tell Windblows not to do this!
5773 *
5774 * The short of it is that this 'RevOut' only gets called if you are running
5775 * one of the new, "improved" MS OSes, and only if you are running in
5776 * 'rightleft' mode. It makes display take *slightly* longer, but not
5777 * noticeably so.
5778 */
5779 static void
K.Takata135e1522022-01-29 15:27:58 +00005780RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 int col,
5782 int row,
5783 UINT foptions,
5784 CONST RECT *pcliprect,
5785 LPCTSTR text,
5786 UINT len,
5787 CONST INT *padding)
5788{
5789 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005791 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005792 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005793 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794}
5795#endif
5796
Bram Moolenaar92467d32017-12-05 13:22:16 +01005797 static void
5798draw_line(
5799 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005800 int y1,
5801 int x2,
5802 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005803 COLORREF color)
5804{
5805#if defined(FEAT_DIRECTX)
5806 if (IS_ENABLE_DIRECTX())
5807 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5808 else
5809#endif
5810 {
5811 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5812 HPEN old_pen = SelectObject(s_hdc, hpen);
5813 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005814 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005815 LineTo(s_hdc, x2, y2);
5816 DeleteObject(SelectObject(s_hdc, old_pen));
5817 }
5818}
5819
5820 static void
5821set_pixel(
5822 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005823 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005824 COLORREF color)
5825{
5826#if defined(FEAT_DIRECTX)
5827 if (IS_ENABLE_DIRECTX())
5828 DWriteContext_SetPixel(s_dwc, x, y, color);
5829 else
5830#endif
5831 SetPixel(s_hdc, x, y, color);
5832}
5833
5834 static void
5835fill_rect(
5836 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005837 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005838 COLORREF color)
5839{
5840#if defined(FEAT_DIRECTX)
5841 if (IS_ENABLE_DIRECTX())
5842 DWriteContext_FillRect(s_dwc, rcp, color);
5843 else
5844#endif
5845 {
5846 HBRUSH hbr2;
5847
5848 if (hbr == NULL)
5849 hbr2 = CreateSolidBrush(color);
5850 else
5851 hbr2 = hbr;
5852 FillRect(s_hdc, rcp, hbr2);
5853 if (hbr == NULL)
5854 DeleteBrush(hbr2);
5855 }
5856}
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 void
5859gui_mch_draw_string(
5860 int row,
5861 int col,
5862 char_u *text,
5863 int len,
5864 int flags)
5865{
5866 static int *padding = NULL;
5867 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 const RECT *pcliprect = NULL;
5869 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 static WCHAR *unicodebuf = NULL;
5871 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005872 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 int y;
5875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 /*
5877 * Italic and bold text seems to have an extra row of pixels at the bottom
5878 * (below where the bottom of the character should be). If we draw the
5879 * characters with a solid background, the top row of pixels in the
5880 * character below will be overwritten. We can fix this by filling in the
5881 * background ourselves, to the correct character proportions, and then
5882 * writing the character in transparent mode. Still have a problem when
5883 * the character is "_", which gets written on to the character below.
5884 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5885 * pixel in their slots, which fixes the problem with the bottom row of
5886 * pixels. We still need this code because otherwise the top row of pixels
5887 * becomes a problem. - webb.
5888 */
5889 static HBRUSH hbr_cache[2] = {NULL, NULL};
5890 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5891 static int brush_lru = 0;
5892 HBRUSH hbr;
5893 RECT rc;
5894
5895 if (!(flags & DRAW_TRANSP))
5896 {
5897 /*
5898 * Clear background first.
5899 * Note: FillRect() excludes right and bottom of rectangle.
5900 */
5901 rc.left = FILL_X(col);
5902 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 if (has_mbyte)
5904 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005905 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005906 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 rc.right = FILL_X(col + len);
5910 rc.bottom = FILL_Y(row + 1);
5911
Bram Moolenaar734a8672019-12-02 22:49:38 +01005912 // Cache the created brush, that saves a lot of time. We need two:
5913 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 if (gui.currBgColor == brush_color[0])
5915 {
5916 hbr = hbr_cache[0];
5917 brush_lru = 1;
5918 }
5919 else if (gui.currBgColor == brush_color[1])
5920 {
5921 hbr = hbr_cache[1];
5922 brush_lru = 0;
5923 }
5924 else
5925 {
5926 if (hbr_cache[brush_lru] != NULL)
5927 DeleteBrush(hbr_cache[brush_lru]);
5928 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5929 brush_color[brush_lru] = gui.currBgColor;
5930 hbr = hbr_cache[brush_lru];
5931 brush_lru = !brush_lru;
5932 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005933
Bram Moolenaar92467d32017-12-05 13:22:16 +01005934 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
5936 SetBkMode(s_hdc, TRANSPARENT);
5937
5938 /*
5939 * When drawing block cursor, prevent inverted character spilling
5940 * over character cell (can happen with bold/italic)
5941 */
5942 if (flags & DRAW_CURSOR)
5943 {
5944 pcliprect = &rc;
5945 foptions = ETO_CLIPPED;
5946 }
5947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 SetTextColor(s_hdc, gui.currFgColor);
5949 SelectFont(s_hdc, gui.currFont);
5950
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005951#ifdef FEAT_DIRECTX
5952 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005953 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005954#endif
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5957 {
K.Takata135e1522022-01-29 15:27:58 +00005958 int i;
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 vim_free(padding);
5961 pad_size = Columns;
5962
Bram Moolenaar734a8672019-12-02 22:49:38 +01005963 // Don't give an out-of-memory message here, it would call us
5964 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02005965 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 if (padding != NULL)
5967 for (i = 0; i < pad_size; i++)
5968 padding[i] = gui.char_width;
5969 }
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 /*
5972 * We have to provide the padding argument because italic and bold versions
5973 * of fixed-width fonts are often one pixel or so wider than their normal
5974 * versions.
5975 * No check for DRAW_BOLD, Windows will have done it already.
5976 */
5977
Bram Moolenaar734a8672019-12-02 22:49:38 +01005978 // Check if there are any UTF-8 characters. If not, use normal text
5979 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 if (enc_utf8)
5981 for (n = 0; n < len; ++n)
5982 if (text[n] >= 0x80)
5983 break;
5984
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005985#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005986 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5987 // required that unicode drawing routine, currently. So this forces it
5988 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005989 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01005990 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005991#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005992
Bram Moolenaar734a8672019-12-02 22:49:38 +01005993 // Check if the Unicode buffer exists and is big enough. Create it
5994 // with the same length as the multi-byte string, the number of wide
5995 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005996 if ((enc_utf8
5997 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
5998 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 && (unicodebuf == NULL || len > unibuflen))
6000 {
6001 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006002 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
6004 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006005 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 unibuflen = len;
6008 }
6009
6010 if (enc_utf8 && n < len && unicodebuf != NULL)
6011 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006012 // Output UTF-8 characters. Composing characters should be
6013 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006014 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006015 int wlen; // string length in words
6016 int clen; // string length in characters
6017 int cells; // cell width of string up to composing char
6018 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006019 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006021 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006022 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006024 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006026 c = utf_ptr2char(text + i);
6027 if (c >= 0x10000)
6028 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006029 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006030 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6031 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006032 }
6033 else
6034 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006035 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006036 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006037
6038 if (utf_iscomposing(c))
6039 cw = 0;
6040 else
6041 {
6042 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006043 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006044 cw = 1;
6045 }
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (unicodepdy != NULL)
6048 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006049 // Use unicodepdy to make characters fit as we expect, even
6050 // when the font uses different widths (e.g., bold character
6051 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006052 if (c >= 0x10000)
6053 {
6054 unicodepdy[wlen - 2] = cw * gui.char_width;
6055 unicodepdy[wlen - 1] = 0;
6056 }
6057 else
6058 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
6060 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006061 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006062 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006064#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006065 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006066 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006067 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006068 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006069 TEXT_X(col), TEXT_Y(row),
6070 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006071 gui.char_width, gui.currFgColor,
6072 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006073 }
6074 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006075#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006076 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6077 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006078 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006080 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006082 // If we want to display codepage data, and the current CP is not the
6083 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (unicodebuf != NULL)
6085 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006086 if (enc_latin9)
6087 latin9_to_ucs(text, len, unicodebuf);
6088 else
6089 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 MB_PRECOMPOSED,
6091 (char *)text, len,
6092 (LPWSTR)unicodebuf, unibuflen);
6093 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006094 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006095 // Use unicodepdy to make characters fit as we expect, even
6096 // when the font uses different widths (e.g., bold character
6097 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006098 if (unicodepdy != NULL)
6099 {
6100 int i;
6101 int cw;
6102
6103 for (i = 0; i < len; ++i)
6104 {
6105 cw = utf_char2cells(unicodebuf[i]);
6106 if (cw > 2)
6107 cw = 1;
6108 unicodepdy[i] = cw * gui.char_width;
6109 }
6110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006112 foptions, pcliprect, unicodebuf, len, unicodepdy);
6113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
6115 }
6116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
6118#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006119 // Windows will mess up RL text, so we have to draw it character by
6120 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006121 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6123 foptions, pcliprect, (char *)text, len, padding);
6124 else
6125#endif
6126 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6127 foptions, pcliprect, (char *)text, len, padding);
6128 }
6129
Bram Moolenaar734a8672019-12-02 22:49:38 +01006130 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 if (flags & DRAW_UNDERL)
6132 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006133 // When p_linespace is 0, overwrite the bottom row of pixels.
6134 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (p_linespace > 1)
6137 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006138 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006140
Bram Moolenaar734a8672019-12-02 22:49:38 +01006141 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006142 if (flags & DRAW_STRIKE)
6143 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006144 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006145 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006146 }
6147
Bram Moolenaar734a8672019-12-02 22:49:38 +01006148 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006149 if (flags & DRAW_UNDERC)
6150 {
6151 int x;
6152 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006153 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006154
6155 y = FILL_Y(row + 1) - 1;
6156 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6157 {
6158 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006159 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006160 }
6161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
6163
6164
6165/*
6166 * Output routines.
6167 */
6168
Bram Moolenaar734a8672019-12-02 22:49:38 +01006169/*
6170 * Flush any output to the screen
6171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 void
6173gui_mch_flush(void)
6174{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006175#if defined(FEAT_DIRECTX)
6176 if (IS_ENABLE_DIRECTX())
6177 DWriteContext_Flush(s_dwc);
6178#endif
6179
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 GdiFlush();
6181}
6182
6183 static void
6184clear_rect(RECT *rcp)
6185{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006186 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187}
6188
6189
Bram Moolenaarc716c302006-01-21 22:12:51 +00006190 void
6191gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6192{
6193 RECT workarea_rect;
6194
6195 get_work_area(&workarea_rect);
6196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006197 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006198 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6199 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006200
Bram Moolenaar734a8672019-12-02 22:49:38 +01006201 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6202 // the menubar for MSwin, we subtract it from the screen height, so that
6203 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006204 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006205 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6206 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6207 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6208 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006209}
6210
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#if defined(FEAT_MENU) || defined(PROTO)
6213/*
6214 * Add a sub menu to the menu bar.
6215 */
6216 void
6217gui_mch_add_menu(
6218 vimmenu_T *menu,
6219 int pos)
6220{
6221 vimmenu_T *parent = menu->parent;
6222
6223 menu->submenu_id = CreatePopupMenu();
6224 menu->id = s_menu_id++;
6225
6226 if (menu_is_menubar(menu->name))
6227 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006228 WCHAR *wn;
6229 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006231 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006232 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006233 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006235 infow.cbSize = sizeof(infow);
6236 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6237 | MIIM_SUBMENU;
6238 infow.dwItemData = (long_u)menu;
6239 infow.wID = menu->id;
6240 infow.fType = MFT_STRING;
6241 infow.dwTypeData = wn;
6242 infow.cch = (UINT)wcslen(wn);
6243 infow.hSubMenu = menu->submenu_id;
6244 InsertMenuItemW((parent == NULL)
6245 ? s_menuBar : parent->submenu_id,
6246 (UINT)pos, TRUE, &infow);
6247 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
6249
Bram Moolenaar734a8672019-12-02 22:49:38 +01006250 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (parent == NULL)
6252 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006253# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 else if (IsWindow(parent->tearoff_handle))
6255 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257}
6258
6259 void
6260gui_mch_show_popupmenu(vimmenu_T *menu)
6261{
6262 POINT mp;
6263
K.Takata45f9cfb2022-01-21 11:11:00 +00006264 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6266}
6267
6268 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006269gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 vimmenu_T *menu = gui_find_menu(path_name);
6272
6273 if (menu != NULL)
6274 {
6275 POINT p;
6276
Bram Moolenaar734a8672019-12-02 22:49:38 +01006277 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006279 if (mouse_pos)
6280 {
6281 int mx, my;
6282
6283 gui_mch_getmouse(&mx, &my);
6284 p.x += mx;
6285 p.y += my;
6286 }
6287 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006289 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6291 }
6292 msg_scroll = FALSE;
6293 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6294 }
6295}
6296
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006297# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298/*
6299 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6300 * create it as a pseudo-"tearoff menu".
6301 */
6302 void
6303gui_make_tearoff(char_u *path_name)
6304{
6305 vimmenu_T *menu = gui_find_menu(path_name);
6306
Bram Moolenaar734a8672019-12-02 22:49:38 +01006307 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (menu != NULL)
6309 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6310}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313/*
6314 * Add a menu item to a menu
6315 */
6316 void
6317gui_mch_add_menu_item(
6318 vimmenu_T *menu,
6319 int idx)
6320{
6321 vimmenu_T *parent = menu->parent;
6322
6323 menu->id = s_menu_id++;
6324 menu->submenu_id = NULL;
6325
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006326# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6328 {
6329 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6330 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6331 }
6332 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006333# endif
6334# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (menu_is_toolbar(parent->name))
6336 {
6337 TBBUTTON newtb;
6338
Bram Moolenaara80faa82020-04-12 19:37:17 +02006339 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 if (menu_is_separator(menu->name))
6341 {
6342 newtb.iBitmap = 0;
6343 newtb.fsStyle = TBSTYLE_SEP;
6344 }
6345 else
6346 {
6347 newtb.iBitmap = get_toolbar_bitmap(menu);
6348 newtb.fsStyle = TBSTYLE_BUTTON;
6349 }
6350 newtb.idCommand = menu->id;
6351 newtb.fsState = TBSTATE_ENABLED;
6352 newtb.iString = 0;
6353 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6354 (LPARAM)&newtb);
6355 menu->submenu_id = (HMENU)-1;
6356 }
6357 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006360 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006362 wn = enc_to_utf16(menu->name, NULL);
6363 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006365 InsertMenuW(parent->submenu_id, (UINT)idx,
6366 (menu_is_separator(menu->name)
6367 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6368 (UINT)menu->id, wn);
6369 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006371# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (IsWindow(parent->tearoff_handle))
6373 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
6376}
6377
6378/*
6379 * Destroy the machine specific menu widget.
6380 */
6381 void
6382gui_mch_destroy_menu(vimmenu_T *menu)
6383{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006384# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 /*
6386 * is this a toolbar button?
6387 */
6388 if (menu->submenu_id == (HMENU)-1)
6389 {
6390 int iButton;
6391
6392 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6393 (WPARAM)menu->id, 0);
6394 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6395 }
6396 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 {
6399 if (menu->parent != NULL
6400 && menu_is_popup(menu->parent->dname)
6401 && menu->parent->submenu_id != NULL)
6402 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6403 else
6404 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6405 if (menu->submenu_id != NULL)
6406 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006407# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (IsWindow(menu->tearoff_handle))
6409 DestroyWindow(menu->tearoff_handle);
6410 if (menu->parent != NULL
6411 && menu->parent->children != NULL
6412 && IsWindow(menu->parent->tearoff_handle))
6413 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006414 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 menu->modes = 0;
6416 rebuild_tearoff(menu->parent);
6417 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 }
6420}
6421
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006422# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 static void
6424rebuild_tearoff(vimmenu_T *menu)
6425{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006426 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 char_u tbuf[128];
6428 RECT trect;
6429 RECT rct;
6430 RECT roct;
6431 int x, y;
6432
6433 HWND thwnd = menu->tearoff_handle;
6434
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006435 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (GetWindowRect(thwnd, &trect)
6437 && GetWindowRect(s_hwnd, &rct)
6438 && GetClientRect(s_hwnd, &roct))
6439 {
6440 x = trect.left - rct.left;
6441 y = (trect.top - rct.bottom + roct.bottom);
6442 }
6443 else
6444 {
6445 x = y = 0xffffL;
6446 }
6447 DestroyWindow(thwnd);
6448 if (menu->children != NULL)
6449 {
6450 gui_mch_tearoff(tbuf, menu, x, y);
6451 if (IsWindow(menu->tearoff_handle))
6452 (void) SetWindowPos(menu->tearoff_handle,
6453 NULL,
6454 (int)trect.left,
6455 (int)trect.top,
6456 0, 0,
6457 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6458 }
6459}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006460# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462/*
6463 * Make a menu either grey or not grey.
6464 */
6465 void
6466gui_mch_menu_grey(
6467 vimmenu_T *menu,
6468 int grey)
6469{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006470# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 /*
6472 * is this a toolbar button?
6473 */
6474 if (menu->submenu_id == (HMENU)-1)
6475 {
6476 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006477 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006480# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006481 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6482 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006484# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6486 {
6487 WORD menuID;
6488 HWND menuHandle;
6489
6490 /*
6491 * A tearoff button has changed state.
6492 */
6493 if (menu->children == NULL)
6494 menuID = (WORD)(menu->id);
6495 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006496 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6498 if (menuHandle)
6499 EnableWindow(menuHandle, !grey);
6500
6501 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
Bram Moolenaar734a8672019-12-02 22:49:38 +01006505#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507
Bram Moolenaar734a8672019-12-02 22:49:38 +01006508// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006511#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512
6513#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6514/*
6515 * stuff for dialogs
6516 */
6517
6518/*
6519 * The callback routine used by all the dialogs. Very simple. First,
6520 * acknowledges the INITDIALOG message so that Windows knows to do standard
6521 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6522 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6523 * number.
6524 */
6525 static LRESULT CALLBACK
6526dialog_callback(
6527 HWND hwnd,
6528 UINT message,
6529 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006530 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 if (message == WM_INITDIALOG)
6533 {
6534 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006535 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 (void)SetFocus(hwnd);
6537 if (dialog_default_button > IDCANCEL)
6538 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006539 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006540 // We don't have a default, set focus on another element of the
6541 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006542 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 return FALSE;
6544 }
6545
6546 if (message == WM_COMMAND)
6547 {
6548 int button = LOWORD(wParam);
6549
Bram Moolenaar734a8672019-12-02 22:49:38 +01006550 // Don't end the dialog if something was selected that was
6551 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (button >= DLG_NONBUTTON_CONTROL)
6553 return TRUE;
6554
Bram Moolenaar734a8672019-12-02 22:49:38 +01006555 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006557 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006558 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006559 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006560
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006561 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6562 p = utf16_to_enc(wp, NULL);
6563 vim_strncpy(s_textfield, p, IOSIZE);
6564 vim_free(p);
6565 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 /*
6569 * Need to check for IDOK because if the user just hits Return to
6570 * accept the default value, some reason this is what we get.
6571 */
6572 if (button == IDOK)
6573 {
6574 if (dialog_default_button > IDCANCEL)
6575 EndDialog(hwnd, dialog_default_button);
6576 }
6577 else
6578 EndDialog(hwnd, button - IDCANCEL);
6579 return TRUE;
6580 }
6581
6582 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6583 {
6584 EndDialog(hwnd, 0);
6585 return TRUE;
6586 }
6587 return FALSE;
6588}
6589
6590/*
6591 * Create a dialog dynamically from the parameter strings.
6592 * type = type of dialog (question, alert, etc.)
6593 * title = dialog title. may be NULL for default title.
6594 * message = text to display. Dialog sizes to accommodate it.
6595 * buttons = '\n' separated list of button captions, default first.
6596 * dfltbutton = number of default button.
6597 *
6598 * This routine returns 1 if the first button is pressed,
6599 * 2 for the second, etc.
6600 *
6601 * 0 indicates Esc was pressed.
6602 * -1 for unexpected error
6603 *
6604 * If stubbing out this fn, return 1.
6605 */
6606
Bram Moolenaar734a8672019-12-02 22:49:38 +01006607static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 "IDR_VIM",
6610 "IDR_VIM_ERROR",
6611 "IDR_VIM_ALERT",
6612 "IDR_VIM_INFO",
6613 "IDR_VIM_QUESTION"
6614};
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 int
6617gui_mch_dialog(
6618 int type,
6619 char_u *title,
6620 char_u *message,
6621 char_u *buttons,
6622 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006623 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006624 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006627 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 int numButtons;
6629 int *buttonWidths, *buttonPositions;
6630 int buttonYpos;
6631 int nchar, i;
6632 DWORD lStyle;
6633 int dlgwidth = 0;
6634 int dlgheight;
6635 int editboxheight;
6636 int horizWidth = 0;
6637 int msgheight;
6638 char_u *pstart;
6639 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006640 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 char_u *tbuffer;
6642 RECT rect;
6643 HWND hwnd;
6644 HDC hdc;
6645 HFONT font, oldFont;
6646 TEXTMETRIC fontInfo;
6647 int fontHeight;
6648 int textWidth, minButtonWidth, messageWidth;
6649 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006650 int maxDialogHeight;
6651 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 int vertical;
6653 int dlgPaddingX;
6654 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006655# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006656 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006658# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006659 garray_T ga;
6660 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006661 int dlg_icon_width;
6662 int dlg_icon_height;
6663 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006665# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006666 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006667# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006668 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006669# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006670 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006671 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaar748bf032005-02-02 23:04:36 +00006674 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006675 {
6676 load_dpi_func();
6677 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006678 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006679 }
6680 else
6681 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 if ((type < 0) || (type > VIM_LAST_TYPE))
6684 type = 0;
6685
Bram Moolenaar734a8672019-12-02 22:49:38 +01006686 // allocate some memory for dialog template
6687 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006688 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006689 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (p == NULL)
6692 return -1;
6693
6694 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006695 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6697 * const.
6698 */
6699 tbuffer = vim_strsave(buttons);
6700 if (tbuffer == NULL)
6701 return -1;
6702
Bram Moolenaar734a8672019-12-02 22:49:38 +01006703 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
Bram Moolenaar734a8672019-12-02 22:49:38 +01006705 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 numButtons = 1;
6707 for (i = 0; tbuffer[i] != '\0'; i++)
6708 {
6709 if (tbuffer[i] == DLG_BUTTON_SEP)
6710 numButtons++;
6711 }
6712 if (dfltbutton >= numButtons)
6713 dfltbutton = -1;
6714
Bram Moolenaar734a8672019-12-02 22:49:38 +01006715 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006716 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (buttonWidths == NULL)
6718 return -1;
6719
Bram Moolenaar734a8672019-12-02 22:49:38 +01006720 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006721 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (buttonPositions == NULL)
6723 return -1;
6724
6725 /*
6726 * Calculate how big the dialog must be.
6727 */
6728 hwnd = GetDesktopWindow();
6729 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006730# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6732 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006733 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 use_lfSysmenu = TRUE;
6735 }
6736 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006737# endif
K.Takatad1c58992022-01-23 12:31:57 +00006738 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6739 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6740
6741 oldFont = SelectFont(hdc, font);
6742 dlgPaddingX = DLG_PADDING_X;
6743 dlgPaddingY = DLG_PADDING_Y;
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 GetTextMetrics(hdc, &fontInfo);
6746 fontHeight = fontInfo.tmHeight;
6747
Bram Moolenaar734a8672019-12-02 22:49:38 +01006748 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006749 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar734a8672019-12-02 22:49:38 +01006751 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006752 if (s_hwnd == NULL)
6753 {
6754 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar734a8672019-12-02 22:49:38 +01006756 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006757 get_work_area(&workarea_rect);
6758 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006759 if (maxDialogWidth > adjust_by_system_dpi(600))
6760 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006761 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006762 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006763 }
6764 else
6765 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006766 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006767 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006768 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006769 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6770 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6771 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6772 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006773
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006774 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006775 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6776 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6777 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6778 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6779 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006780 }
6781
Bram Moolenaar734a8672019-12-02 22:49:38 +01006782 // Set dlgwidth to width of message.
6783 // Copy the message into "ga", changing NL to CR-NL and inserting line
6784 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 pstart = message;
6786 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006787 msgheight = 0;
6788 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 do
6790 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006791 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006792
Bram Moolenaar734a8672019-12-02 22:49:38 +01006793 // Need to figure out where to break the string. The system does it
6794 // at a word boundary, which would mean we can't compute the number of
6795 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006796 textWidth = 0;
6797 last_white = NULL;
6798 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006799 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006800 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006801 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006802 && textWidth > maxDialogWidth * 3 / 4)
6803 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006804 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006805 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006806 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006807 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006808 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006809 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006810 textWidth = 0;
6811
6812 if (last_white != NULL)
6813 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006814 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006815 if (pend > last_white)
6816 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006817 pend = last_white + 1;
6818 last_white = NULL;
6819 }
6820 ga_append(&ga, '\r');
6821 ga_append(&ga, '\n');
6822 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006823 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006824
6825 while (--l >= 0)
6826 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006827 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006828 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006830
6831 ga_append(&ga, '\r');
6832 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 pstart = pend + 1;
6834 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006835
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006836 if (ga.ga_data != NULL)
6837 message = ga.ga_data;
6838
Bram Moolenaar734a8672019-12-02 22:49:38 +01006839 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006840
K.Takatac81e9bf2022-01-16 14:15:49 +00006841 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6842 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
K.Takatac81e9bf2022-01-16 14:15:49 +00006844 // Add width of icon to dlgwidth, and some space
6845 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6846 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6847
6848 if (msgheight < dlg_icon_height)
6849 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
6851 /*
6852 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006853 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006855 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (!vertical)
6857 {
6858 // Place buttons horizontally if they fit.
6859 horizWidth = dlgPaddingX;
6860 pstart = tbuffer;
6861 i = 0;
6862 do
6863 {
6864 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6865 if (pend == NULL)
6866 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006867 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 if (textWidth < minButtonWidth)
6869 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006870 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 buttonWidths[i] = textWidth;
6872 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006873 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 pstart = pend + 1;
6875 } while (*pend != NUL);
6876
6877 if (horizWidth > maxDialogWidth)
6878 vertical = TRUE; // Too wide to fit on the screen.
6879 else if (horizWidth > dlgwidth)
6880 dlgwidth = horizWidth;
6881 }
6882
6883 if (vertical)
6884 {
6885 // Stack buttons vertically.
6886 pstart = tbuffer;
6887 do
6888 {
6889 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6890 if (pend == NULL)
6891 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006892 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006893 textWidth += dlgPaddingX; // Padding within button
6894 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (textWidth > dlgwidth)
6896 dlgwidth = textWidth;
6897 pstart = pend + 1;
6898 } while (*pend != NUL);
6899 }
6900
6901 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006902 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
Bram Moolenaar734a8672019-12-02 22:49:38 +01006904 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006905 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
6907 add_long(lStyle);
6908 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006909 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 add_word(0); // NumberOfItems(will change later)
6911 add_word(10); // x
6912 add_word(10); // y
6913 add_word(PixelToDialogX(dlgwidth)); // cx
6914
6915 // Dialog height.
6916 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006917 dlgheight = msgheight + 2 * dlgPaddingY
6918 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 else
6920 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6921
6922 // Dialog needs to be taller if contains an edit box.
6923 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6924 if (textfield != NULL)
6925 dlgheight += editboxheight;
6926
Bram Moolenaar734a8672019-12-02 22:49:38 +01006927 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006928 if (dlgheight > maxDialogHeight)
6929 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006930 msgheight = msgheight - (dlgheight - maxDialogHeight);
6931 dlgheight = maxDialogHeight;
6932 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006933 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00006934 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006935 }
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 add_word(PixelToDialogY(dlgheight));
6938
6939 add_word(0); // Menu
6940 add_word(0); // Class
6941
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006943 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6944 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 p += nchar;
6946
K.Takatad1c58992022-01-23 12:31:57 +00006947 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006948# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00006949 if (use_lfSysmenu)
6950 {
6951 // point size
6952 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6953 GetDeviceCaps(hdc, LOGPIXELSY));
6954 wcscpy(p, lfSysmenu.lfFaceName);
6955 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 }
K.Takatad1c58992022-01-23 12:31:57 +00006957 else
6958# endif
6959 {
6960 *p++ = DLG_FONT_POINT_SIZE; // point size
6961 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
6962 }
6963 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
6965 buttonYpos = msgheight + 2 * dlgPaddingY;
6966
6967 if (textfield != NULL)
6968 buttonYpos += editboxheight;
6969
6970 pstart = tbuffer;
6971 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006972 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 for (i = 0; i < numButtons; i++)
6974 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006975 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 for ( pend = pstart;
6977 *pend && (*pend != DLG_BUTTON_SEP);
6978 pend++)
6979 ;
6980
6981 if (*pend)
6982 *pend = '\0';
6983
6984 /*
6985 * old NOTE:
6986 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6987 * the focus to the first tab-able button and in so doing makes that
6988 * the default!! Grrr. Workaround: Make the default button the only
6989 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6990 * he/she can use arrow keys.
6991 *
6992 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00006993 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 * dialog. Also needed for when the textfield is the default control.
6995 * It appears to work now (perhaps not on Win95?).
6996 */
6997 if (vertical)
6998 {
6999 p = add_dialog_element(p,
7000 (i == dfltbutton
7001 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7002 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007003 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 + 2 * fontHeight * i),
7005 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7006 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007007 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009 else
7010 {
7011 p = add_dialog_element(p,
7012 (i == dfltbutton
7013 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7014 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007015 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 PixelToDialogX(buttonWidths[i]),
7017 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007018 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007020 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
7022 *pnumitems += numButtons;
7023
Bram Moolenaar734a8672019-12-02 22:49:38 +01007024 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 p = add_dialog_element(p, SS_ICON,
7026 PixelToDialogX(dlgPaddingX),
7027 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007028 PixelToDialogX(dlg_icon_width),
7029 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7031 dlg_icons[type]);
7032
Bram Moolenaar734a8672019-12-02 22:49:38 +01007033 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007034 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007035 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007036 PixelToDialogY(dlgPaddingY),
7037 (WORD)(PixelToDialogX(messageWidth) + 1),
7038 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007039 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar734a8672019-12-02 22:49:38 +01007041 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (textfield != NULL)
7043 {
7044 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7045 PixelToDialogX(2 * dlgPaddingX),
7046 PixelToDialogY(2 * dlgPaddingY + msgheight),
7047 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7048 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007049 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 *pnumitems += 1;
7051 }
7052
7053 *pnumitems += 2;
7054
7055 SelectFont(hdc, oldFont);
7056 DeleteObject(font);
7057 ReleaseDC(hwnd, hdc);
7058
Bram Moolenaar734a8672019-12-02 22:49:38 +01007059 // Let the dialog_callback() function know which button to make default
7060 // If we have an edit box, make that the default. We also need to tell
7061 // dialog_callback() if this dialog contains an edit box or not. We do
7062 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (textfield != NULL)
7064 {
7065 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7066 s_textfield = textfield;
7067 }
7068 else
7069 {
7070 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7071 s_textfield = NULL;
7072 }
7073
Bram Moolenaar734a8672019-12-02 22:49:38 +01007074 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007076 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 (LPDLGTEMPLATE)pdlgtemplate,
7078 s_hwnd,
7079 (DLGPROC)dialog_callback);
7080
7081 LocalFree(LocalHandle(pdlgtemplate));
7082 vim_free(tbuffer);
7083 vim_free(buttonWidths);
7084 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007085 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
Bram Moolenaar734a8672019-12-02 22:49:38 +01007087 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 (void)SetFocus(s_hwnd);
7089
7090 return nchar;
7091}
7092
Bram Moolenaar734a8672019-12-02 22:49:38 +01007093#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007094
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095/*
7096 * Put a simple element (basic class) onto a dialog template in memory.
7097 * return a pointer to where the next item should be added.
7098 *
7099 * parameters:
7100 * lStyle = additional style flags
7101 * (be careful, NT3.51 & Win32s will ignore the new ones)
7102 * x,y = x & y positions IN DIALOG UNITS
7103 * w,h = width and height IN DIALOG UNITS
7104 * Id = ID used in messages
7105 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7106 * caption = usually text or resource name
7107 *
7108 * TODO: use the length information noted here to enable the dialog creation
7109 * routines to work out more exactly how much memory they need to alloc.
7110 */
7111 static PWORD
7112add_dialog_element(
7113 PWORD p,
7114 DWORD lStyle,
7115 WORD x,
7116 WORD y,
7117 WORD w,
7118 WORD h,
7119 WORD Id,
7120 WORD clss,
7121 const char *caption)
7122{
7123 int nchar;
7124
Bram Moolenaar734a8672019-12-02 22:49:38 +01007125 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7127 *p++ = LOWORD(lStyle);
7128 *p++ = HIWORD(lStyle);
7129 *p++ = 0; // LOWORD (lExtendedStyle)
7130 *p++ = 0; // HIWORD (lExtendedStyle)
7131 *p++ = x;
7132 *p++ = y;
7133 *p++ = w;
7134 *p++ = h;
7135 *p++ = Id; //9 or 10 words in all
7136
7137 *p++ = (WORD)0xffff;
7138 *p++ = clss; //2 more here
7139
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007140 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 p += nchar;
7142
7143 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7144
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007145 return p; // total = 15 + strlen(caption) words
7146 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147}
7148
7149
7150/*
7151 * Helper routine. Take an input pointer, return closest pointer that is
7152 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7153 */
7154 static LPWORD
7155lpwAlign(
7156 LPWORD lpIn)
7157{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007158 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007160 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 ul += 3;
7162 ul >>= 2;
7163 ul <<= 2;
7164 return (LPWORD)ul;
7165}
7166
7167/*
7168 * Helper routine. Takes second parameter as Ansi string, copies it to first
7169 * parameter as wide character (16-bits / char) string, and returns integer
7170 * number of wide characters (words) in string (including the trailing wide
7171 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007172 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7173 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 static int
7175nCopyAnsiToWideChar(
7176 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007177 LPSTR lpAnsiIn,
7178 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007181 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 int i;
7183 WCHAR *wn;
7184
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007185 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007188 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (wn != NULL)
7190 {
7191 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007192 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 vim_free(wn);
7194 }
7195 }
7196 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007197 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 nChar = MultiByteToWideChar(
7199 enc_codepage > 0 ? enc_codepage : CP_ACP,
7200 MB_PRECOMPOSED,
7201 lpAnsiIn, len,
7202 lpWCStr, len);
7203 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007204 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207 return nChar;
7208}
7209
7210
7211#ifdef FEAT_TEAROFF
7212/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007213 * Lookup menu handle from "menu_id".
7214 */
7215 static HMENU
7216tearoff_lookup_menuhandle(
7217 vimmenu_T *menu,
7218 WORD menu_id)
7219{
7220 for ( ; menu != NULL; menu = menu->next)
7221 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007222 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007223 continue;
7224 if (menu_is_separator(menu->dname))
7225 continue;
7226 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7227 return menu->submenu_id;
7228 }
7229 return NULL;
7230}
7231
7232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 * The callback function for all the modeless dialogs that make up the
7234 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7235 * thinking its menus have been clicked), and go away when closed.
7236 */
7237 static LRESULT CALLBACK
7238tearoff_callback(
7239 HWND hwnd,
7240 UINT message,
7241 WPARAM wParam,
7242 LPARAM lParam)
7243{
7244 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007245 {
7246 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
Bram Moolenaar734a8672019-12-02 22:49:38 +01007250 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 HandleMouseHide(message, lParam);
7252
7253 if (message == WM_COMMAND)
7254 {
7255 if ((WORD)(LOWORD(wParam)) & 0x8000)
7256 {
7257 POINT mp;
7258 RECT rect;
7259
7260 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7261 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007262 vimmenu_T *menu;
7263
7264 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007266 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7268 (int)rect.right - 8,
7269 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007270 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 s_hwnd,
7272 NULL);
7273 /*
7274 * NOTE: The pop-up menu can eat the mouse up event.
7275 * We deal with this in normal.c.
7276 */
7277 }
7278 }
7279 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007280 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7282 /*
7283 * Give main window the focus back: this is so after
7284 * choosing a tearoff button you can start typing again
7285 * straight away.
7286 */
7287 (void)SetFocus(s_hwnd);
7288 return TRUE;
7289 }
7290 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7291 {
7292 DestroyWindow(hwnd);
7293 return TRUE;
7294 }
7295
Bram Moolenaar734a8672019-12-02 22:49:38 +01007296 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (message == WM_EXITSIZEMOVE)
7298 (void)SetActiveWindow(s_hwnd);
7299
7300 return FALSE;
7301}
7302#endif
7303
7304
7305/*
K.Takatad1c58992022-01-23 12:31:57 +00007306 * Computes the dialog base units based on the current dialog font.
7307 * We don't use the GetDialogBaseUnits() API, because we don't use the
7308 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 */
7310 static void
7311get_dialog_font_metrics(void)
7312{
7313 HDC hdc;
7314 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 SIZE size;
7316#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007317 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318#endif
7319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007321 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007322 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324#endif
7325 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007326 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327
K.Takatad1c58992022-01-23 12:31:57 +00007328 hdc = GetDC(s_hwnd);
7329 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007330 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007331 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
K.Takataabe628e2022-01-23 16:25:17 +00007333 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007334 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335}
7336
7337#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7338/*
7339 * Create a pseudo-"tearoff menu" based on the child
7340 * items of a given menu pointer.
7341 */
7342 static void
7343gui_mch_tearoff(
7344 char_u *title,
7345 vimmenu_T *menu,
7346 int initX,
7347 int initY)
7348{
7349 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7350 int template_len;
7351 int nchar, textWidth, submenuWidth;
7352 DWORD lStyle;
7353 DWORD lExtendedStyle;
7354 WORD dlgwidth;
7355 WORD menuID;
7356 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007357 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 vimmenu_T *the_menu = menu;
7359 HWND hwnd;
7360 HDC hdc;
7361 HFONT font, oldFont;
7362 int col, spaceWidth, len;
7363 int columnWidths[2];
7364 char_u *label, *text;
7365 int acLen = 0;
7366 int nameLen;
7367 int padding0, padding1, padding2 = 0;
7368 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007369 int x;
7370 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007371# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007372 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 /*
7377 * If this menu is already torn off, move it to the mouse position.
7378 */
7379 if (IsWindow(menu->tearoff_handle))
7380 {
7381 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007382 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
7384 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7385 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7386 }
7387 return;
7388 }
7389
7390 /*
7391 * Create a new tearoff.
7392 */
7393 if (*title == MNU_HIDDEN_CHAR)
7394 title++;
7395
Bram Moolenaar734a8672019-12-02 22:49:38 +01007396 // Allocate memory to store the dialog template. It's made bigger when
7397 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 template_len = DLG_ALLOC_SIZE;
7399 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7400 if (p == NULL)
7401 return;
7402
7403 hwnd = GetDesktopWindow();
7404 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007405# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7407 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007408 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 use_lfSysmenu = TRUE;
7410 }
7411 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007412# endif
K.Takatad1c58992022-01-23 12:31:57 +00007413 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7414 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7415
7416 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
Bram Moolenaar734a8672019-12-02 22:49:38 +01007418 // Calculate width of a single space. Used for padding columns to the
7419 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007420 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
Bram Moolenaar734a8672019-12-02 22:49:38 +01007422 // Figure out max width of the text column, the accelerator column and the
7423 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 submenuWidth = 0;
7425 for (col = 0; col < 2; col++)
7426 {
7427 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007428 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007430 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 text = (col == 0) ? pmenu->dname : pmenu->actext;
7432 if (text != NULL && *text != NUL)
7433 {
7434 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7435 if (textWidth > columnWidths[col])
7436 columnWidths[col] = textWidth;
7437 }
7438 if (pmenu->children != NULL)
7439 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7440 }
7441 }
7442 if (columnWidths[1] == 0)
7443 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007444 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 if (submenuWidth != 0)
7446 columnWidths[0] += submenuWidth;
7447 else
7448 columnWidths[0] += spaceWidth;
7449 }
7450 else
7451 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007452 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7454 columnWidths[1] += submenuWidth;
7455 }
7456
7457 /*
7458 * Now find the total width of our 'menu'.
7459 */
7460 textWidth = columnWidths[0] + columnWidths[1];
7461 if (submenuWidth != 0)
7462 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007463 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7465 textWidth += submenuWidth;
7466 }
7467 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7468 if (textWidth > dlgwidth)
7469 dlgwidth = textWidth;
7470 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7471
Bram Moolenaar734a8672019-12-02 22:49:38 +01007472 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007473 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7476 *p++ = LOWORD(lStyle);
7477 *p++ = HIWORD(lStyle);
7478 *p++ = LOWORD(lExtendedStyle);
7479 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007480 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007482 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007484 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 else
7486 *p++ = PixelToDialogX(initX); // x
7487 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007488 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 else
7490 *p++ = PixelToDialogY(initY); // y
7491 *p++ = PixelToDialogX(dlgwidth); // cx
7492 ptrueheight = p;
7493 *p++ = 0; // dialog height: changed later anyway
7494 *p++ = 0; // Menu
7495 *p++ = 0; // Class
7496
Bram Moolenaar734a8672019-12-02 22:49:38 +01007497 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007499 ? (LPSTR)title
7500 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 p += nchar;
7502
K.Takatad1c58992022-01-23 12:31:57 +00007503 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007504# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007505 if (use_lfSysmenu)
7506 {
7507 // point size
7508 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7509 GetDeviceCaps(hdc, LOGPIXELSY));
7510 wcscpy(p, lfSysmenu.lfFaceName);
7511 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
K.Takatad1c58992022-01-23 12:31:57 +00007513 else
7514# endif
7515 {
7516 *p++ = DLG_FONT_POINT_SIZE; // point size
7517 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7518 }
7519 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 /*
7522 * Loop over all the items in the menu.
7523 * But skip over the tearbar.
7524 */
7525 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7526 menu = menu->children->next;
7527 else
7528 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007529 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 for ( ; menu != NULL; menu = menu->next)
7531 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007532 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 continue;
7534 if (menu_is_separator(menu->dname))
7535 {
7536 sepPadding += 3;
7537 continue;
7538 }
7539
Bram Moolenaar734a8672019-12-02 22:49:38 +01007540 // Check if there still is plenty of room in the template. Make it
7541 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7543 {
7544 WORD *newp;
7545
7546 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7547 if (newp != NULL)
7548 {
7549 template_len += 4096;
7550 mch_memmove(newp, pdlgtemplate,
7551 (char *)p - (char *)pdlgtemplate);
7552 p = newp + (p - pdlgtemplate);
7553 pnumitems = newp + (pnumitems - pdlgtemplate);
7554 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7555 LocalFree(LocalHandle(pdlgtemplate));
7556 pdlgtemplate = newp;
7557 }
7558 }
7559
Bram Moolenaar734a8672019-12-02 22:49:38 +01007560 // Figure out minimal length of this menu label. Use "name" for the
7561 // actual text, "dname" for estimating the displayed size. "name"
7562 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 len = nameLen = (int)STRLEN(menu->name);
7564 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7565 (int)STRLEN(menu->dname))) / spaceWidth;
7566 len += padding0;
7567
7568 if (menu->actext != NULL)
7569 {
7570 acLen = (int)STRLEN(menu->actext);
7571 len += acLen;
7572 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7573 }
7574 else
7575 textWidth = 0;
7576 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7577 len += padding1;
7578
7579 if (menu->children == NULL)
7580 {
7581 padding2 = submenuWidth / spaceWidth;
7582 len += padding2;
7583 menuID = (WORD)(menu->id);
7584 }
7585 else
7586 {
7587 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007588 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590
Bram Moolenaar734a8672019-12-02 22:49:38 +01007591 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007592 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (label == NULL)
7594 break;
7595
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007596 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007597 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007599 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 while (padding0-- > 0)
7601 *text++ = ' ';
7602 if (menu->actext != NULL)
7603 {
7604 STRNCPY(text, menu->actext, acLen);
7605 text += acLen;
7606 }
7607 while (padding1-- > 0)
7608 *text++ = ' ';
7609 if (menu->children != NULL)
7610 {
7611 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7612 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7613 }
7614 else
7615 {
7616 while (padding2-- > 0)
7617 *text++ = ' ';
7618 }
7619 *text = NUL;
7620
7621 /*
7622 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7623 * W95/NT4 it makes the tear-off look more like a menu.
7624 */
7625 p = add_dialog_element(p,
7626 BS_PUSHBUTTON|BS_LEFT,
7627 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7628 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7629 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7630 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007631 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 vim_free(label);
7633 (*pnumitems)++;
7634 }
7635
7636 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7637
7638
Bram Moolenaar734a8672019-12-02 22:49:38 +01007639 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007640 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007641 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 (LPDLGTEMPLATE)pdlgtemplate,
7643 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007644 (DLGPROC)tearoff_callback,
7645 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
7647 LocalFree(LocalHandle(pdlgtemplate));
7648 SelectFont(hdc, oldFont);
7649 DeleteObject(font);
7650 ReleaseDC(hwnd, hdc);
7651
7652 /*
7653 * Reassert ourselves as the active window. This is so that after creating
7654 * a tearoff, the user doesn't have to click with the mouse just to start
7655 * typing again!
7656 */
7657 (void)SetActiveWindow(s_hwnd);
7658
Bram Moolenaar734a8672019-12-02 22:49:38 +01007659 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 force_menu_update = TRUE;
7661}
7662#endif
7663
7664#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007665# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667/*
7668 * Create the toolbar, initially unpopulated.
7669 * (just like the menu, there are no defaults, it's all
7670 * set up through menu.vim)
7671 */
7672 static void
7673initialise_toolbar(void)
7674{
7675 InitCommonControls();
7676 s_toolbarhwnd = CreateToolbarEx(
7677 s_hwnd,
7678 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7679 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007680 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007681 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 IDR_TOOLBAR1, // id of initial bitmap
7683 NULL,
7684 0, // initial number of buttons
7685 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7686 TOOLBAR_BUTTON_HEIGHT,
7687 TOOLBAR_BUTTON_WIDTH,
7688 TOOLBAR_BUTTON_HEIGHT,
7689 sizeof(TBBUTTON)
7690 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007691
7692 // Remove transparency from the toolbar to prevent the main window
7693 // background colour showing through
7694 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7695 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7696
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007697 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
7699 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007700
7701 update_toolbar_size();
7702}
7703
7704 static void
7705update_toolbar_size(void)
7706{
7707 int w, h;
7708 TBMETRICS tbm = {sizeof(TBMETRICS)};
7709
7710 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7711 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7712 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7713 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7714
7715 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7716 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7717 //TRACE("button size: %d, %d", w, h);
7718 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7719 gui.toolbar_height = h + 6;
7720
7721 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7722 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7723
7724 // TODO:
7725 // Currently, this function only updates the size of toolbar buttons.
7726 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727}
7728
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007729 static LRESULT CALLBACK
7730toolbar_wndproc(
7731 HWND hwnd,
7732 UINT uMsg,
7733 WPARAM wParam,
7734 LPARAM lParam)
7735{
7736 HandleMouseHide(uMsg, lParam);
7737 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7738}
7739
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 static int
7741get_toolbar_bitmap(vimmenu_T *menu)
7742{
7743 int i = -1;
7744
7745 /*
7746 * Check user bitmaps first, unless builtin is specified.
7747 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007748 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 char_u fname[MAXPATHL];
7751 HANDLE hbitmap = NULL;
7752
7753 if (menu->iconfile != NULL)
7754 {
7755 gui_find_iconfile(menu->iconfile, fname, "bmp");
7756 hbitmap = LoadImage(
7757 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007758 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 IMAGE_BITMAP,
7760 TOOLBAR_BUTTON_WIDTH,
7761 TOOLBAR_BUTTON_HEIGHT,
7762 LR_LOADFROMFILE |
7763 LR_LOADMAP3DCOLORS
7764 );
7765 }
7766
7767 /*
7768 * If the LoadImage call failed, or the "icon=" file
7769 * didn't exist or wasn't specified, try the menu name
7770 */
7771 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007772 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007773# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007774 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007775# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007776 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 hbitmap = LoadImage(
7778 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007779 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 IMAGE_BITMAP,
7781 TOOLBAR_BUTTON_WIDTH,
7782 TOOLBAR_BUTTON_HEIGHT,
7783 LR_LOADFROMFILE |
7784 LR_LOADMAP3DCOLORS
7785 );
7786
7787 if (hbitmap != NULL)
7788 {
7789 TBADDBITMAP tbAddBitmap;
7790
7791 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007792 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7795 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007796 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 }
7798 }
7799 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7800 i = menu->iconidx;
7801
7802 return i;
7803}
7804#endif
7805
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007806#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7807 static void
7808initialise_tabline(void)
7809{
7810 InitCommonControls();
7811
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007812 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007813 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007814 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007815 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007816 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007817
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007818 gui.tabline_height = TABLINE_HEIGHT;
7819
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007820 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007821}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007822
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007823/*
7824 * Get tabpage_T from POINT.
7825 */
7826 static tabpage_T *
7827GetTabFromPoint(
7828 HWND hWnd,
7829 POINT pt)
7830{
7831 tabpage_T *ptp = NULL;
7832
7833 if (gui_mch_showing_tabline())
7834 {
7835 TCHITTESTINFO htinfo;
7836 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007837 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007838 if (s_tabhwnd == hWnd)
7839 {
7840 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7841 if (idx != -1)
7842 ptp = find_tabpage(idx + 1);
7843 }
7844 }
7845 return ptp;
7846}
7847
7848static POINT s_pt = {0, 0};
7849static HCURSOR s_hCursor = NULL;
7850
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007851 static LRESULT CALLBACK
7852tabline_wndproc(
7853 HWND hwnd,
7854 UINT uMsg,
7855 WPARAM wParam,
7856 LPARAM lParam)
7857{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007858 POINT pt;
7859 tabpage_T *tp;
7860 RECT rect;
7861 int nCenter;
7862 int idx0;
7863 int idx1;
7864
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007865 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007866
7867 switch (uMsg)
7868 {
7869 case WM_LBUTTONDOWN:
7870 {
7871 s_pt.x = GET_X_LPARAM(lParam);
7872 s_pt.y = GET_Y_LPARAM(lParam);
7873 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007874 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007875 break;
7876 }
7877 case WM_MOUSEMOVE:
7878 if (GetCapture() == hwnd
7879 && ((wParam & MK_LBUTTON)) != 0)
7880 {
7881 pt.x = GET_X_LPARAM(lParam);
7882 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007883 if (abs(pt.x - s_pt.x) >
7884 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007885 {
7886 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7887
7888 tp = GetTabFromPoint(hwnd, pt);
7889 if (tp != NULL)
7890 {
7891 idx0 = tabpage_index(curtab) - 1;
7892 idx1 = tabpage_index(tp) - 1;
7893
7894 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7895 nCenter = rect.left + (rect.right - rect.left) / 2;
7896
Bram Moolenaar734a8672019-12-02 22:49:38 +01007897 // Check if the mouse cursor goes over the center of
7898 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007899 if ((idx0 < idx1) && (nCenter < pt.x))
7900 {
7901 tabpage_move(idx1 + 1);
7902 update_screen(0);
7903 }
7904 else if ((idx1 < idx0) && (pt.x < nCenter))
7905 {
7906 tabpage_move(idx1);
7907 update_screen(0);
7908 }
7909 }
7910 }
7911 }
7912 break;
7913 case WM_LBUTTONUP:
7914 {
7915 if (GetCapture() == hwnd)
7916 {
7917 SetCursor(s_hCursor);
7918 ReleaseCapture();
7919 }
7920 break;
7921 }
7922 default:
7923 break;
7924 }
7925
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007926 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7927}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007928#endif
7929
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7931/*
7932 * Make the GUI window come to the foreground.
7933 */
7934 void
7935gui_mch_set_foreground(void)
7936{
7937 if (IsIconic(s_hwnd))
7938 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7939 SetForegroundWindow(s_hwnd);
7940}
7941#endif
7942
7943#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7944 static void
7945dyn_imm_load(void)
7946{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007947 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 if (hLibImm == NULL)
7949 return;
7950
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 pImmGetCompositionStringW
7952 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7953 pImmGetContext
7954 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7955 pImmAssociateContext
7956 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7957 pImmReleaseContext
7958 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7959 pImmGetOpenStatus
7960 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7961 pImmSetOpenStatus
7962 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007963 pImmGetCompositionFontW
7964 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7965 pImmSetCompositionFontW
7966 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 pImmSetCompositionWindow
7968 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7969 pImmGetConversionStatus
7970 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007971 pImmSetConversionStatus
7972 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
K.Takatab0b2b732022-01-19 12:59:21 +00007974 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 || pImmGetContext == NULL
7976 || pImmAssociateContext == NULL
7977 || pImmReleaseContext == NULL
7978 || pImmGetOpenStatus == NULL
7979 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007980 || pImmGetCompositionFontW == NULL
7981 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007983 || pImmGetConversionStatus == NULL
7984 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {
7986 FreeLibrary(hLibImm);
7987 hLibImm = NULL;
7988 pImmGetContext = NULL;
7989 return;
7990 }
7991
7992 return;
7993}
7994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995#endif
7996
7997#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7998
7999# ifdef FEAT_XPM_W32
8000# define IMAGE_XPM 100
8001# endif
8002
8003typedef struct _signicon_t
8004{
8005 HANDLE hImage;
8006 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008007# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008008 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010} signicon_t;
8011
8012 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008013gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014{
8015 signicon_t *sign;
8016 int x, y, w, h;
8017
8018 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8019 return;
8020
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008021# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008022 if (IS_ENABLE_DIRECTX())
8023 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008024# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 x = TEXT_X(col);
8027 y = TEXT_Y(row);
8028 w = gui.char_width * 2;
8029 h = gui.char_height;
8030 switch (sign->uType)
8031 {
8032 case IMAGE_BITMAP:
8033 {
8034 HDC hdcMem;
8035 HBITMAP hbmpOld;
8036
8037 hdcMem = CreateCompatibleDC(s_hdc);
8038 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8039 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8040 SelectObject(hdcMem, hbmpOld);
8041 DeleteDC(hdcMem);
8042 }
8043 break;
8044 case IMAGE_ICON:
8045 case IMAGE_CURSOR:
8046 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8047 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008048# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 case IMAGE_XPM:
8050 {
8051 HDC hdcMem;
8052 HBITMAP hbmpOld;
8053
8054 hdcMem = CreateCompatibleDC(s_hdc);
8055 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008056 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8058
8059 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008060 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8062 SelectObject(hdcMem, hbmpOld);
8063 DeleteDC(hdcMem);
8064 }
8065 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 }
8068}
8069
8070 static void
8071close_signicon_image(signicon_t *sign)
8072{
8073 if (sign)
8074 switch (sign->uType)
8075 {
8076 case IMAGE_BITMAP:
8077 DeleteObject((HGDIOBJ)sign->hImage);
8078 break;
8079 case IMAGE_CURSOR:
8080 DestroyCursor((HCURSOR)sign->hImage);
8081 break;
8082 case IMAGE_ICON:
8083 DestroyIcon((HICON)sign->hImage);
8084 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008085# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 case IMAGE_XPM:
8087 DeleteObject((HBITMAP)sign->hImage);
8088 DeleteObject((HBITMAP)sign->hShape);
8089 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092}
8093
8094 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008095gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 signicon_t sign, *psign;
8098 char_u *ext;
8099
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008101 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (ext > signfile)
8103 {
8104 int do_load = 1;
8105
8106 if (!STRICMP(ext, ".bmp"))
8107 sign.uType = IMAGE_BITMAP;
8108 else if (!STRICMP(ext, ".ico"))
8109 sign.uType = IMAGE_ICON;
8110 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8111 sign.uType = IMAGE_CURSOR;
8112 else
8113 do_load = 0;
8114
8115 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008116 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 gui.char_width * 2, gui.char_height,
8118 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008119# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (!STRICMP(ext, ".xpm"))
8121 {
8122 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008123 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8124 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 }
8128
8129 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008130 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 *psign = sign;
8132
8133 if (!psign)
8134 {
8135 if (sign.hImage)
8136 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008137 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 }
8139 return (void *)psign;
8140
8141}
8142
8143 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008144gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145{
8146 if (sign)
8147 {
8148 close_signicon_image((signicon_t *)sign);
8149 vim_free(sign);
8150 }
8151}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008154#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaar734a8672019-12-02 22:49:38 +01008156/*
8157 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008158 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008160 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8162 * to get current mouse position).
8163 *
8164 * Trying to use as more Windows services as possible, and as less
8165 * IE version as possible :)).
8166 *
8167 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8168 * BalloonEval struct.
8169 * 2) Enable/Disable simply create/kill BalloonEval Timer
8170 * 3) When there was enough inactivity, timer procedure posts
8171 * async request to debugger
8172 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8173 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008174 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 */
8176
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008177 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008178make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008179{
K.Takata76687d22022-01-25 10:31:37 +00008180 TOOLINFOW *pti;
8181 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008182
K.Takata76687d22022-01-25 10:31:37 +00008183 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008184 if (pti == NULL)
8185 return;
8186
8187 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8188 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8189 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008190 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008191
8192 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8193 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8194
K.Takata76687d22022-01-25 10:31:37 +00008195 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008196 pti->uFlags = TTF_SUBCLASS;
8197 pti->hwnd = beval->target;
8198 pti->hinst = 0; // Don't use string resources
8199 pti->uId = ID_BEVAL_TOOLTIP;
8200
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008201 pti->lpszText = LPSTR_TEXTCALLBACKW;
8202 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8203 pti->lParam = (LPARAM)beval->tofree;
8204 // switch multiline tooltips on
8205 if (GetClientRect(s_textArea, &rect))
8206 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8207 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008208
8209 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8210 pti->rect.left = pt.x - 3;
8211 pti->rect.top = pt.y - 3;
8212 pti->rect.right = pt.x + 3;
8213 pti->rect.bottom = pt.y + 3;
8214
8215 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8216 // Make tooltip appear sooner.
8217 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8218 // I've performed some tests and it seems the longest possible life time
8219 // of tooltip is 30 seconds.
8220 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8221 /*
8222 * HACK: force tooltip to appear, because it'll not appear until
8223 * first mouse move. D*mn M$
8224 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8225 */
8226 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8227 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8228 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008229}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008232delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008234 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235}
8236
8237 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008238beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008239 HWND hwnd UNUSED,
8240 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008241 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008242 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 POINT pt;
8245 RECT rect;
8246
8247 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8248 return;
8249
8250 GetCursorPos(&pt);
8251 if (WindowFromPoint(pt) != s_textArea)
8252 return;
8253
8254 ScreenToClient(s_textArea, &pt);
8255 GetClientRect(s_textArea, &rect);
8256 if (!PtInRect(&rect, pt))
8257 return;
8258
K.Takataa8ec4912022-02-03 14:32:33 +00008259 if (last_user_activity > 0
8260 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 && (cur_beval->showState != ShS_PENDING
8262 || abs(cur_beval->x - pt.x) > 3
8263 || abs(cur_beval->y - pt.y) > 3))
8264 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008265 // Pointer resting in one place long enough, it's time to show
8266 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 cur_beval->showState = ShS_PENDING;
8268 cur_beval->x = pt.x;
8269 cur_beval->y = pt.y;
8270
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (cur_beval->msgCB != NULL)
8272 (*cur_beval->msgCB)(cur_beval, 0);
8273 }
8274}
8275
8276 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008277gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278{
K.Takataa8ec4912022-02-03 14:32:33 +00008279 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280}
8281
8282 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008283gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (beval == NULL)
8286 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008287 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8288 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289}
8290
8291 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008292gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
8294 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008295
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008296 vim_free(beval->msg);
8297 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8298 if (beval->msg == NULL)
8299 {
8300 delete_tooltip(beval);
8301 beval->showState = ShS_NEUTRAL;
8302 return;
8303 }
8304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (beval->showState == ShS_SHOWING)
8306 return;
8307 GetCursorPos(&pt);
8308 ScreenToClient(s_textArea, &pt);
8309
8310 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008312 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 gui_mch_disable_beval_area(cur_beval);
8314 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008315 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317}
8318
8319 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008320gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008321 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008322 char_u *mesg,
8323 void (*mesgCB)(BalloonEval *, int),
8324 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008326 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 BalloonEval *beval;
8328
8329 if (mesg != NULL && mesgCB != NULL)
8330 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008331 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 return NULL;
8333 }
8334
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008335 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 if (beval != NULL)
8337 {
8338 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
8340 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 beval->msg = mesg;
8342 beval->msgCB = mesgCB;
8343 beval->clientData = clientData;
8344
8345 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 cur_beval = beval;
8347
8348 if (p_beval)
8349 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 }
8351 return beval;
8352}
8353
8354 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008355Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008357 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 return;
8359
8360 if (cur_beval != NULL)
8361 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008362 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008364 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008365 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008366 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 delete_tooltip(cur_beval);
8368 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
8370 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008371 break;
8372 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008373 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008374 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008375 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008376 info->lpszText = (LPSTR)info->lParam;
8377 info->uFlags |= TTF_DI_SETITEM;
8378 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008379 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008380 case TTN_GETDISPINFOW:
8381 {
8382 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008383 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008384 info->lpszText = (LPWSTR)info->lParam;
8385 info->uFlags |= TTF_DI_SETITEM;
8386 }
8387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
8389 }
8390}
8391
8392 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008393track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
8395 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8396 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008397 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398}
8399
8400 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008401gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008403# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008404 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008405# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008406 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 vim_free(beval);
8408}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008409#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410
8411#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8412/*
8413 * We have multiple signs to draw at the same location. Draw the
8414 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8415 */
8416 void
8417netbeans_draw_multisign_indicator(int row)
8418{
8419 int i;
8420 int y;
8421 int x;
8422
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008423 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008424 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008425
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 x = 0;
8427 y = TEXT_Y(row);
8428
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008429# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008430 if (IS_ENABLE_DIRECTX())
8431 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008432# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008433
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 for (i = 0; i < gui.char_height - 3; i++)
8435 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8436
8437 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8438 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8439 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8440 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8441 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8442 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8443 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8444}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008445#endif