blob: cbbae9edd6984ba709cd1382eb3ed056321ff9cf [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
209#define DLG_OLD_STYLE_PADDING_X 5
210#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100211#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100212#define DLG_VERT_PADDING_Y 4
213#define DLG_ICON_WIDTH 34
214#define DLG_ICON_HEIGHT 34
215#define DLG_MIN_WIDTH 150
216#define DLG_FONT_NAME "MS Sans Serif"
217#define DLG_FONT_POINT_SIZE 8
218#define DLG_MIN_MAX_WIDTH 400
219#define DLG_MIN_MAX_HEIGHT 400
220
Bram Moolenaar734a8672019-12-02 22:49:38 +0100221#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224# define WM_XBUTTONDOWN 0x020B
225# define WM_XBUTTONUP 0x020C
226# define WM_XBUTTONDBLCLK 0x020D
227# define MK_XBUTTON1 0x0020
228# define MK_XBUTTON2 0x0040
229#endif
230
K.Takatac81e9bf2022-01-16 14:15:49 +0000231#ifndef WM_DPICHANGED
232# define WM_DPICHANGED 0x02E0
233#endif
234
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237 * Define a few things for generating prototypes. This is just to avoid
238 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100240# define APIENTRY
241# define CALLBACK
242# define CONST
243# define FAR
244# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200245# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200246# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247# define _cdecl
248typedef int BOOL;
249typedef int BYTE;
250typedef int DWORD;
251typedef int WCHAR;
252typedef int ENUMLOGFONT;
253typedef int FINDREPLACE;
254typedef int HANDLE;
255typedef int HBITMAP;
256typedef int HBRUSH;
257typedef int HDROP;
258typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100259typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100260typedef int LPARAM;
261typedef int LPCREATESTRUCT;
262typedef int LPCSTR;
263typedef int LPCTSTR;
264typedef int LPRECT;
265typedef int LPSTR;
266typedef int LPWINDOWPOS;
267typedef int LPWORD;
268typedef int LRESULT;
269typedef int HRESULT;
270# undef MSG
271typedef int MSG;
272typedef int NEWTEXTMETRIC;
273typedef int OSVERSIONINFO;
274typedef int PWORD;
275typedef int RECT;
276typedef int UINT;
277typedef int WORD;
278typedef int WPARAM;
279typedef int POINT;
280typedef void *HINSTANCE;
281typedef void *HMENU;
282typedef void *HWND;
283typedef void *HDC;
284typedef void VOID;
285typedef int LPNMHDR;
286typedef int LONG;
287typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200288typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200289typedef int COLORREF;
290typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100291#endif
292
293#ifndef GET_X_LPARAM
294# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
295#endif
296
297static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100298static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299static void clear_rect(RECT *rcp);
300
Bram Moolenaar734a8672019-12-02 22:49:38 +0100301static WORD s_dlgfntheight; // height of the dialog font
302static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef FEAT_MENU
305static HMENU s_menuBar = NULL;
306#endif
307#ifdef FEAT_TEAROFF
308static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaar734a8672019-12-02 22:49:38 +0100312// Flag that is set while processing a message that must not be interrupted by
313// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314static int s_busy_processing = FALSE;
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100317
318#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000319static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200322static int s_findrep_is_find; // TRUE for find dialog, FALSE
323 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324#endif
325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
Bram Moolenaar734a8672019-12-02 22:49:38 +0100347static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100348
349static int s_need_activate = FALSE;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351// This variable is set when waiting for an event, which is the only moment
352// scrollbar dragging can be done directly. It's not allowed while commands
353// are executed, because it may move the cursor and that may cause unexpected
354// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100355static int allow_scrollbar = FALSE;
356
K.Takatac81e9bf2022-01-16 14:15:49 +0000357#ifndef _DPI_AWARENESS_CONTEXTS_
358typedef HANDLE DPI_AWARENESS_CONTEXT;
359
360typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000361 DPI_AWARENESS_INVALID = -1,
362 DPI_AWARENESS_UNAWARE = 0,
363 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000364 DPI_AWARENESS_PER_MONITOR_AWARE = 2
365} DPI_AWARENESS;
366
K.Takatab0b2b732022-01-19 12:59:21 +0000367# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
368# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000369# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
370# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
371# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
372#endif
373
374#define DEFAULT_DPI 96
375static int s_dpi = DEFAULT_DPI;
376static BOOL s_in_dpichanged = FALSE;
377static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
378
379static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
380static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
381static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
382//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
383static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
384static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
385
386 static UINT WINAPI
387stubGetDpiForSystem(void)
388{
389 HWND hwnd = GetDesktopWindow();
390 HDC hdc = GetWindowDC(hwnd);
391 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
392 ReleaseDC(hwnd, hdc);
393 return dpi;
394}
395
396 static int WINAPI
397stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
398{
399 return GetSystemMetrics(nIndex);
400}
401
402 static int
403adjust_fontsize_by_dpi(int size)
404{
405 return size * s_dpi / (int)pGetDpiForSystem();
406}
407
408 static int
409adjust_by_system_dpi(int size)
410{
411 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
412}
413
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100414#if defined(FEAT_DIRECTX)
415 static int
416directx_enabled(void)
417{
418 if (s_dwc != NULL)
419 return 1;
420 else if (s_directx_load_attempted)
421 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100422 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100423 DWrite_Init();
424 s_directx_load_attempted = 1;
425 s_dwc = DWriteContext_Open();
426 directx_binddc();
427 return s_dwc != NULL ? 1 : 0;
428}
429
430 static void
431directx_binddc(void)
432{
433 if (s_textArea != NULL)
434 {
435 RECT rect;
436 GetClientRect(s_textArea, &rect);
437 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
438 }
439}
440#endif
441
Bram Moolenaar734a8672019-12-02 22:49:38 +0100442extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100443
444static struct
445{
446 UINT key_sym;
447 char_u vim_code0;
448 char_u vim_code1;
449} special_keys[] =
450{
451 {VK_UP, 'k', 'u'},
452 {VK_DOWN, 'k', 'd'},
453 {VK_LEFT, 'k', 'l'},
454 {VK_RIGHT, 'k', 'r'},
455
456 {VK_F1, 'k', '1'},
457 {VK_F2, 'k', '2'},
458 {VK_F3, 'k', '3'},
459 {VK_F4, 'k', '4'},
460 {VK_F5, 'k', '5'},
461 {VK_F6, 'k', '6'},
462 {VK_F7, 'k', '7'},
463 {VK_F8, 'k', '8'},
464 {VK_F9, 'k', '9'},
465 {VK_F10, 'k', ';'},
466
467 {VK_F11, 'F', '1'},
468 {VK_F12, 'F', '2'},
469 {VK_F13, 'F', '3'},
470 {VK_F14, 'F', '4'},
471 {VK_F15, 'F', '5'},
472 {VK_F16, 'F', '6'},
473 {VK_F17, 'F', '7'},
474 {VK_F18, 'F', '8'},
475 {VK_F19, 'F', '9'},
476 {VK_F20, 'F', 'A'},
477
478 {VK_F21, 'F', 'B'},
479#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100480 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100481#endif
482 {VK_F22, 'F', 'C'},
483 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485
486 {VK_HELP, '%', '1'},
487 {VK_BACK, 'k', 'b'},
488 {VK_INSERT, 'k', 'I'},
489 {VK_DELETE, 'k', 'D'},
490 {VK_HOME, 'k', 'h'},
491 {VK_END, '@', '7'},
492 {VK_PRIOR, 'k', 'P'},
493 {VK_NEXT, 'k', 'N'},
494 {VK_PRINT, '%', '9'},
495 {VK_ADD, 'K', '6'},
496 {VK_SUBTRACT, 'K', '7'},
497 {VK_DIVIDE, 'K', '8'},
498 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_DECIMAL, 'K', 'B'},
501
502 {VK_NUMPAD0, 'K', 'C'},
503 {VK_NUMPAD1, 'K', 'D'},
504 {VK_NUMPAD2, 'K', 'E'},
505 {VK_NUMPAD3, 'K', 'F'},
506 {VK_NUMPAD4, 'K', 'G'},
507 {VK_NUMPAD5, 'K', 'H'},
508 {VK_NUMPAD6, 'K', 'I'},
509 {VK_NUMPAD7, 'K', 'J'},
510 {VK_NUMPAD8, 'K', 'K'},
511 {VK_NUMPAD9, 'K', 'L'},
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100514 {VK_SPACE, ' ', NUL},
515 {VK_TAB, TAB, NUL},
516 {VK_ESCAPE, ESC, NUL},
517 {NL, NL, NUL},
518 {CAR, CAR, NUL},
519
Bram Moolenaar734a8672019-12-02 22:49:38 +0100520 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521 {0, 0, 0}
522};
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525static int s_button_pending = -1;
526
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
528// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_getting_focus = FALSE;
530
531static int s_x_pending;
532static int s_y_pending;
533static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200534static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100535static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200536static int dead_key = 0; // 0: no dead key, 1: dead key pressed
537static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
538 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100540#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100541// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
543static void TrackUserActivity(UINT uMsg);
544#endif
545
546/*
547 * For control IME.
548 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100549 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100550 */
K.Takata4ac893f2022-01-20 12:44:28 +0000551#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100554// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100555static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100556#endif
557
558#ifdef FEAT_MBYTE_IME
559static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
560#endif
561
562#if defined(FEAT_BROWSE)
563static char_u *convert_filter(char_u *s);
564#endif
565
566#ifdef DEBUG_PRINT_ERROR
567/*
568 * Print out the last Windows error message
569 */
570 static void
571print_windows_error(void)
572{
573 LPVOID lpMsgBuf;
574
575 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
576 NULL, GetLastError(),
577 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
578 (LPTSTR) &lpMsgBuf, 0, NULL);
579 TRACE1("Error: %s\n", lpMsgBuf);
580 LocalFree(lpMsgBuf);
581}
582#endif
583
584/*
585 * Cursor blink functions.
586 *
587 * This is a simple state machine:
588 * BLINK_NONE not blinking at all
589 * BLINK_OFF blinking, cursor is not shown
590 * BLINK_ON blinking, cursor is shown
591 */
592
593#define BLINK_NONE 0
594#define BLINK_OFF 1
595#define BLINK_ON 2
596
597static int blink_state = BLINK_NONE;
598static long_u blink_waittime = 700;
599static long_u blink_ontime = 400;
600static long_u blink_offtime = 250;
601static UINT blink_timer = 0;
602
Bram Moolenaar703a8042016-06-04 16:24:32 +0200603 int
604gui_mch_is_blinking(void)
605{
606 return blink_state != BLINK_NONE;
607}
608
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200609 int
610gui_mch_is_blink_off(void)
611{
612 return blink_state == BLINK_OFF;
613}
614
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615 void
616gui_mch_set_blinking(long wait, long on, long off)
617{
618 blink_waittime = wait;
619 blink_ontime = on;
620 blink_offtime = off;
621}
622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100623 static VOID CALLBACK
624_OnBlinkTimer(
625 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100628 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100629{
630 MSG msg;
631
632 /*
633 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
634 */
635
636 KillTimer(NULL, idEvent);
637
Bram Moolenaar734a8672019-12-02 22:49:38 +0100638 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
640 ;
641
642 if (blink_state == BLINK_ON)
643 {
644 gui_undraw_cursor();
645 blink_state = BLINK_OFF;
646 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
647 (TIMERPROC)_OnBlinkTimer);
648 }
649 else
650 {
651 gui_update_cursor(TRUE, FALSE);
652 blink_state = BLINK_ON;
653 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100654 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100655 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100656 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100657}
658
659 static void
660gui_mswin_rm_blink_timer(void)
661{
662 MSG msg;
663
664 if (blink_timer != 0)
665 {
666 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100667 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
669 ;
670 blink_timer = 0;
671 }
672}
673
674/*
675 * Stop the cursor blinking. Show the cursor if it wasn't shown.
676 */
677 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100678gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679{
680 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100681 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100682 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100684 gui_mch_flush();
685 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 blink_state = BLINK_NONE;
687}
688
689/*
690 * Start the cursor blinking. If it was already blinking, this restarts the
691 * waiting time and shows the cursor.
692 */
693 void
694gui_mch_start_blink(void)
695{
696 gui_mswin_rm_blink_timer();
697
Bram Moolenaar734a8672019-12-02 22:49:38 +0100698 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100699 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
700 {
701 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
702 (TIMERPROC)_OnBlinkTimer);
703 blink_state = BLINK_ON;
704 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100705 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 }
707}
708
709/*
710 * Call-back routines.
711 */
712
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100713 static VOID CALLBACK
714_OnTimer(
715 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100716 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100718 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719{
720 MSG msg;
721
722 /*
723 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
724 */
725 KillTimer(NULL, idEvent);
726 s_timed_out = TRUE;
727
Bram Moolenaar734a8672019-12-02 22:49:38 +0100728 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100729 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
730 ;
731 if (idEvent == s_wait_timer)
732 s_wait_timer = 0;
733}
734
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100735 static void
736_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100737 HWND hwnd UNUSED,
738 UINT ch UNUSED,
739 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100740{
741 dead_key = 1;
742}
743
744/*
745 * Convert Unicode character "ch" to bytes in "string[slen]".
746 * When "had_alt" is TRUE the ALT key was included in "ch".
747 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200748 * Because the Windows API uses UTF-16, we have to deal with surrogate
749 * pairs; this is where we choose to deal with them: if "ch" is a high
750 * surrogate, it will be stored, and the length returned will be zero; the next
751 * char_to_string call will then include the high surrogate, decoding the pair
752 * of UTF-16 code units to a single Unicode code point, presuming it is the
753 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100754 */
755 static int
756char_to_string(int ch, char_u *string, int slen, int had_alt)
757{
758 int len;
759 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200761 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200763 if (surrogate_pending_ch != 0)
764 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100765 // We don't guarantee ch is a low surrogate to match the high surrogate
766 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200767 wstring[0] = surrogate_pending_ch;
768 wstring[1] = ch;
769 surrogate_pending_ch = 0;
770 len = 2;
771 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100772 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200773 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100774 // We don't have the entire code point yet, only the first UTF-16 code
775 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200776 surrogate_pending_ch = ch;
777 return 0;
778 }
779 else
780 {
781 wstring[0] = ch;
782 len = 1;
783 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200784
Bram Moolenaar734a8672019-12-02 22:49:38 +0100785 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
786 // "enc_codepage" is non-zero use the standard Win32 function,
787 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200788 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100789 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
791 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100792 // If we had included the ALT key into the character but now the
793 // upper bit is no longer set, that probably means the conversion
794 // failed. Convert the original character and set the upper bit
795 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100797 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798 wstring[0] = ch & 0x7f;
799 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
800 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100801 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 }
804 }
805 else
806 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200807 ws = utf16_to_enc(wstring, &len);
808 if (ws == NULL)
809 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100810 else
811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100812 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 len = slen;
814 mch_memmove(string, ws, len);
815 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816 }
817 }
818
819 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 {
821 string[0] = ch;
822 len = 1;
823 }
824
825 for (i = 0; i < len; ++i)
826 if (string[i] == CSI && len <= slen - 2)
827 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100828 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
830 string[++i] = KS_EXTRA;
831 string[++i] = (int)KE_CSI;
832 len += 2;
833 }
834
835 return len;
836}
837
838/*
839 * Key hit, add it to the input buffer.
840 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100841 static void
842_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100843 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100845 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100846{
847 char_u string[40];
848 int len = 0;
849
850 dead_key = 0;
851
852 len = char_to_string(ch, string, 40, FALSE);
853 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
854 {
855 trash_input_buf();
856 got_int = TRUE;
857 }
858
859 add_to_input_buf(string, len);
860}
861
862/*
863 * Alt-Key hit, add it to the input buffer.
864 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100865 static void
866_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100867 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100868 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100869 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100871 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872 int len;
873 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100874 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100875
876 dead_key = 0;
877
Bram Moolenaar734a8672019-12-02 22:49:38 +0100878 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879
Bram Moolenaar734a8672019-12-02 22:49:38 +0100880 // OK, we have a character key (given by ch) which was entered with the
881 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
882 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
883 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100884 modifiers = MOD_MASK_ALT;
885 if (GetKeyState(VK_SHIFT) & 0x8000)
886 modifiers |= MOD_MASK_SHIFT;
887 if (GetKeyState(VK_CONTROL) & 0x8000)
888 modifiers |= MOD_MASK_CTRL;
889
890 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100891 // remove the SHIFT modifier for keys where it's already included, e.g.,
892 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200893 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100894
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200895 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
896 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100897 if (ch == CSI)
898 ch = K_CSI;
899
900 len = 0;
901 if (modifiers)
902 {
903 string[len++] = CSI;
904 string[len++] = KS_MODIFIER;
905 string[len++] = modifiers;
906 }
907
908 if (IS_SPECIAL((int)ch))
909 {
910 string[len++] = CSI;
911 string[len++] = K_SECOND((int)ch);
912 string[len++] = K_THIRD((int)ch);
913 }
914 else
915 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100916 // Although the documentation isn't clear about it, we assume "ch" is
917 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100918 len += char_to_string(ch, string + len, 40 - len, TRUE);
919 }
920
921 add_to_input_buf(string, len);
922}
923
924 static void
925_OnMouseEvent(
926 int button,
927 int x,
928 int y,
929 int repeated_click,
930 UINT keyFlags)
931{
932 int vim_modifiers = 0x0;
933
934 s_getting_focus = FALSE;
935
936 if (keyFlags & MK_SHIFT)
937 vim_modifiers |= MOUSE_SHIFT;
938 if (keyFlags & MK_CONTROL)
939 vim_modifiers |= MOUSE_CTRL;
940 if (GetKeyState(VK_MENU) & 0x8000)
941 vim_modifiers |= MOUSE_ALT;
942
943 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
944}
945
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100946 static void
947_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100948 HWND hwnd UNUSED,
949 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950 int x,
951 int y,
952 UINT keyFlags)
953{
954 static LONG s_prevTime = 0;
955
956 LONG currentTime = GetMessageTime();
957 int button = -1;
958 int repeated_click;
959
Bram Moolenaar734a8672019-12-02 22:49:38 +0100960 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100961 (void)SetFocus(s_hwnd);
962
963 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
964 button = MOUSE_LEFT;
965 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
966 button = MOUSE_MIDDLE;
967 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
968 button = MOUSE_RIGHT;
969 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
970 {
971#ifndef GET_XBUTTON_WPARAM
972# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
973#endif
974 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
975 }
976 else if (s_uMsg == WM_CAPTURECHANGED)
977 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100978 // on W95/NT4, somehow you get in here with an odd Msg
979 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100980 if (s_button_pending == MOUSE_LEFT)
981 button = MOUSE_RIGHT;
982 else
983 button = MOUSE_LEFT;
984 }
985 if (button >= 0)
986 {
987 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
988
989 /*
990 * Holding down the left and right buttons simulates pushing the middle
991 * button.
992 */
993 if (repeated_click
994 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
995 || (button == MOUSE_RIGHT
996 && s_button_pending == MOUSE_LEFT)))
997 {
998 /*
999 * Hmm, gui.c will ignore more than one button down at a time, so
1000 * pretend we let go of it first.
1001 */
1002 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1003 button = MOUSE_MIDDLE;
1004 repeated_click = FALSE;
1005 s_button_pending = -1;
1006 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1007 }
1008 else if ((repeated_click)
1009 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1010 {
1011 if (s_button_pending > -1)
1012 {
1013 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1014 s_button_pending = -1;
1015 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001016 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001017 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1018 }
1019 else
1020 {
1021 /*
1022 * If this is the first press (i.e. not a multiple click) don't
1023 * action immediately, but store and wait for:
1024 * i) button-up
1025 * ii) mouse move
1026 * iii) another button press
1027 * before using it.
1028 * This enables us to make left+right simulate middle button,
1029 * without left or right being actioned first. The side-effect is
1030 * that if you click and hold the mouse without dragging, the
1031 * cursor doesn't move until you release the button. In practice
1032 * this is hardly a problem.
1033 */
1034 s_button_pending = button;
1035 s_x_pending = x;
1036 s_y_pending = y;
1037 s_kFlags_pending = keyFlags;
1038 }
1039
1040 s_prevTime = currentTime;
1041 }
1042}
1043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001044 static void
1045_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001046 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001047 int x,
1048 int y,
1049 UINT keyFlags)
1050{
1051 int button;
1052
1053 s_getting_focus = FALSE;
1054 if (s_button_pending > -1)
1055 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001056 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057 _OnMouseEvent(s_button_pending, s_x_pending,
1058 s_y_pending, FALSE, s_kFlags_pending);
1059 s_button_pending = -1;
1060 }
1061 if (s_uMsg == WM_MOUSEMOVE)
1062 {
1063 /*
1064 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1065 * down.
1066 */
1067 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1068 | MK_XBUTTON1 | MK_XBUTTON2)))
1069 {
1070 gui_mouse_moved(x, y);
1071 return;
1072 }
1073
1074 /*
1075 * While button is down, keep grabbing mouse move events when
1076 * the mouse goes outside the window
1077 */
1078 SetCapture(s_textArea);
1079 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001080 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001081 }
1082 else
1083 {
1084 ReleaseCapture();
1085 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001086 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001087 }
1088
1089 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1090}
1091
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001092 static void
1093_OnSizeTextArea(
1094 HWND hwnd UNUSED,
1095 UINT state UNUSED,
1096 int cx UNUSED,
1097 int cy UNUSED)
1098{
1099#if defined(FEAT_DIRECTX)
1100 if (IS_ENABLE_DIRECTX())
1101 directx_binddc();
1102#endif
1103}
1104
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105#ifdef FEAT_MENU
1106/*
1107 * Find the vimmenu_T with the given id
1108 */
1109 static vimmenu_T *
1110gui_mswin_find_menu(
1111 vimmenu_T *pMenu,
1112 int id)
1113{
1114 vimmenu_T *pChildMenu;
1115
1116 while (pMenu)
1117 {
1118 if (pMenu->id == (UINT)id)
1119 break;
1120 if (pMenu->children != NULL)
1121 {
1122 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1123 if (pChildMenu)
1124 {
1125 pMenu = pChildMenu;
1126 break;
1127 }
1128 }
1129 pMenu = pMenu->next;
1130 }
1131 return pMenu;
1132}
1133
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001134 static void
1135_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001136 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001137 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001138 HWND hwndCtl UNUSED,
1139 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001140{
1141 vimmenu_T *pMenu;
1142
1143 pMenu = gui_mswin_find_menu(root_menu, id);
1144 if (pMenu)
1145 gui_menu_cb(pMenu);
1146}
1147#endif
1148
1149#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001150/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001151 * Handle a Find/Replace window message.
1152 */
1153 static void
1154_OnFindRepl(void)
1155{
1156 int flags = 0;
1157 int down;
1158
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001159 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001160 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001161 (void)SetFocus(s_hwnd);
1162
1163 if (s_findrep_struct.Flags & FR_FINDNEXT)
1164 {
1165 flags = FRD_FINDNEXT;
1166
Bram Moolenaar734a8672019-12-02 22:49:38 +01001167 // Give main window the focus back: this is so the cursor isn't
1168 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001169 (void)SetFocus(s_hwnd);
1170 }
1171 else if (s_findrep_struct.Flags & FR_REPLACE)
1172 {
1173 flags = FRD_REPLACE;
1174
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Give main window the focus back: this is so the cursor isn't
1176 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001177 (void)SetFocus(s_hwnd);
1178 }
1179 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1180 {
1181 flags = FRD_REPLACEALL;
1182 }
1183
1184 if (flags != 0)
1185 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001186 char_u *p, *q;
1187
Bram Moolenaar734a8672019-12-02 22:49:38 +01001188 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1190 flags |= FRD_WHOLE_WORD;
1191 if (s_findrep_struct.Flags & FR_MATCHCASE)
1192 flags |= FRD_MATCH_CASE;
1193 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001194 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1195 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1196 if (p != NULL && q != NULL)
1197 gui_do_findrepl(flags, p, q, down);
1198 vim_free(p);
1199 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 }
1201}
1202#endif
1203
1204 static void
1205HandleMouseHide(UINT uMsg, LPARAM lParam)
1206{
1207 static LPARAM last_lParam = 0L;
1208
Bram Moolenaar734a8672019-12-02 22:49:38 +01001209 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001210 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1211 {
1212 if (lParam == last_lParam)
1213 return;
1214 last_lParam = lParam;
1215 }
1216
Bram Moolenaar734a8672019-12-02 22:49:38 +01001217 // Handle specially, to centralise coding. We need to be sure we catch all
1218 // possible events which should cause us to restore the cursor (as it is a
1219 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001220 switch (uMsg)
1221 {
1222 case WM_KEYUP:
1223 case WM_CHAR:
1224 /*
1225 * blank out the pointer if necessary
1226 */
1227 if (p_mh)
1228 gui_mch_mousehide(TRUE);
1229 break;
1230
Bram Moolenaar734a8672019-12-02 22:49:38 +01001231 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001232 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001233 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001234 case WM_LBUTTONDOWN:
1235 case WM_LBUTTONUP:
1236 case WM_MBUTTONDOWN:
1237 case WM_MBUTTONUP:
1238 case WM_RBUTTONDOWN:
1239 case WM_RBUTTONUP:
1240 case WM_XBUTTONDOWN:
1241 case WM_XBUTTONUP:
1242 case WM_NCMOUSEMOVE:
1243 case WM_NCLBUTTONDOWN:
1244 case WM_NCLBUTTONUP:
1245 case WM_NCMBUTTONDOWN:
1246 case WM_NCMBUTTONUP:
1247 case WM_NCRBUTTONDOWN:
1248 case WM_NCRBUTTONUP:
1249 case WM_KILLFOCUS:
1250 /*
1251 * if the pointer is currently hidden, then we should show it.
1252 */
1253 gui_mch_mousehide(FALSE);
1254 break;
1255 }
1256}
1257
1258 static LRESULT CALLBACK
1259_TextAreaWndProc(
1260 HWND hwnd,
1261 UINT uMsg,
1262 WPARAM wParam,
1263 LPARAM lParam)
1264{
1265 /*
1266 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1267 hwnd, uMsg, wParam, lParam);
1268 */
1269
1270 HandleMouseHide(uMsg, lParam);
1271
1272 s_uMsg = uMsg;
1273 s_wParam = wParam;
1274 s_lParam = lParam;
1275
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001276#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001277 TrackUserActivity(uMsg);
1278#endif
1279
1280 switch (uMsg)
1281 {
1282 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1283 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1284 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1285 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1286 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1287 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1288 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1289 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1290 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1291 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1292 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1293 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1294 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1295 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001296 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001297
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001298#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001299 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1300 return TRUE;
1301#endif
1302 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001303 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304 }
1305}
1306
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001307/*
1308 * Called when the foreground or background color has been changed.
1309 */
1310 void
1311gui_mch_new_colors(void)
1312{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001313 HBRUSH prevBrush;
1314
1315 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001316 prevBrush = (HBRUSH)SetClassLongPtr(
1317 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001318 InvalidateRect(s_hwnd, NULL, TRUE);
1319 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001320}
1321
1322/*
1323 * Set the colors to their default values.
1324 */
1325 void
1326gui_mch_def_colors(void)
1327{
1328 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1329 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1330 gui.def_norm_pixel = gui.norm_pixel;
1331 gui.def_back_pixel = gui.back_pixel;
1332}
1333
1334/*
1335 * Open the GUI window which was created by a call to gui_mch_init().
1336 */
1337 int
1338gui_mch_open(void)
1339{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001340 // Actually open the window, if not already visible
1341 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001342 if (!IsWindowVisible(s_hwnd))
1343 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1344
1345#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001346 // Init replace string here, so that we keep it when re-opening the
1347 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001348 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1349#endif
1350
1351 return OK;
1352}
1353
1354/*
1355 * Get the position of the top left corner of the window.
1356 */
1357 int
1358gui_mch_get_winpos(int *x, int *y)
1359{
1360 RECT rect;
1361
1362 GetWindowRect(s_hwnd, &rect);
1363 *x = rect.left;
1364 *y = rect.top;
1365 return OK;
1366}
1367
1368/*
1369 * Set the position of the top left corner of the window to the given
1370 * coordinates.
1371 */
1372 void
1373gui_mch_set_winpos(int x, int y)
1374{
1375 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1376 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1377}
1378 void
1379gui_mch_set_text_area_pos(int x, int y, int w, int h)
1380{
1381 static int oldx = 0;
1382 static int oldy = 0;
1383
1384 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1385
1386#ifdef FEAT_TOOLBAR
1387 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1388 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001389 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001390#endif
1391#if defined(FEAT_GUI_TABLINE)
1392 if (showing_tabline)
1393 {
1394 int top = 0;
1395 RECT rect;
1396
1397# ifdef FEAT_TOOLBAR
1398 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001399 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001400# endif
1401 GetClientRect(s_hwnd, &rect);
1402 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1403 }
1404#endif
1405
Bram Moolenaar734a8672019-12-02 22:49:38 +01001406 // When side scroll bar is unshown, the size of window will change.
1407 // then, the text area move left or right. thus client rect should be
1408 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001409 if (oldx != x || oldy != y)
1410 {
1411 InvalidateRect(s_hwnd, NULL, FALSE);
1412 oldx = x;
1413 oldy = y;
1414 }
1415}
1416
1417
1418/*
1419 * Scrollbar stuff:
1420 */
1421
1422 void
1423gui_mch_enable_scrollbar(
1424 scrollbar_T *sb,
1425 int flag)
1426{
1427 ShowScrollBar(sb->id, SB_CTL, flag);
1428
Bram Moolenaar734a8672019-12-02 22:49:38 +01001429 // TODO: When the window is maximized, the size of the window stays the
1430 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1431 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001432}
1433
1434 void
1435gui_mch_set_scrollbar_pos(
1436 scrollbar_T *sb,
1437 int x,
1438 int y,
1439 int w,
1440 int h)
1441{
1442 SetWindowPos(sb->id, NULL, x, y, w, h,
1443 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1444}
1445
Bram Moolenaar203ec772020-07-17 20:43:43 +02001446 int
1447gui_mch_get_scrollbar_xpadding(void)
1448{
1449 RECT rcTxt, rcWnd;
1450 int xpad;
1451
1452 GetWindowRect(s_textArea, &rcTxt);
1453 GetWindowRect(s_hwnd, &rcWnd);
1454 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001455 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1456 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001457 return (xpad < 0) ? 0 : xpad;
1458}
1459
1460 int
1461gui_mch_get_scrollbar_ypadding(void)
1462{
1463 RECT rcTxt, rcWnd;
1464 int ypad;
1465
1466 GetWindowRect(s_textArea, &rcTxt);
1467 GetWindowRect(s_hwnd, &rcWnd);
1468 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001469 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1470 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001471 return (ypad < 0) ? 0 : ypad;
1472}
1473
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001474 void
1475gui_mch_create_scrollbar(
1476 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001477 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001478{
1479 sb->id = CreateWindow(
1480 "SCROLLBAR", "Scrollbar",
1481 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001482 10, // Any value will do for now
1483 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001484 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001485 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001486}
1487
1488/*
1489 * Find the scrollbar with the given hwnd.
1490 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001491 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001492gui_mswin_find_scrollbar(HWND hwnd)
1493{
1494 win_T *wp;
1495
1496 if (gui.bottom_sbar.id == hwnd)
1497 return &gui.bottom_sbar;
1498 FOR_ALL_WINDOWS(wp)
1499 {
1500 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1501 return &wp->w_scrollbars[SBAR_LEFT];
1502 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1503 return &wp->w_scrollbars[SBAR_RIGHT];
1504 }
1505 return NULL;
1506}
1507
K.Takatac81e9bf2022-01-16 14:15:49 +00001508 static void
1509update_scrollbar_size(void)
1510{
1511 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1512 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1513}
1514
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515/*
1516 * Get the character size of a font.
1517 */
1518 static void
1519GetFontSize(GuiFont font)
1520{
1521 HWND hwnd = GetDesktopWindow();
1522 HDC hdc = GetWindowDC(hwnd);
1523 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001524 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 TEXTMETRIC tm;
1526
1527 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001528 // GetTextMetrics() may not return the right value in tmAveCharWidth
1529 // for some fonts. Do our own average computation.
1530 GetTextExtentPoint(hdc,
1531 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1532 52, &size);
1533 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001534
1535 gui.char_height = tm.tmHeight + p_linespace;
1536
1537 SelectFont(hdc, hfntOld);
1538
1539 ReleaseDC(hwnd, hdc);
1540}
1541
1542/*
1543 * Adjust gui.char_height (after 'linespace' was changed).
1544 */
1545 int
1546gui_mch_adjust_charheight(void)
1547{
1548 GetFontSize(gui.norm_font);
1549 return OK;
1550}
1551
1552 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001553get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554{
1555 HFONT font = NULL;
1556
Bram Moolenaar734a8672019-12-02 22:49:38 +01001557 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001558 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559
1560 if (font == NULL)
1561 return NOFONT;
1562
1563 return (GuiFont)font;
1564}
1565
1566 static int
1567pixels_to_points(int pixels, int vertical)
1568{
1569 int points;
1570 HWND hwnd;
1571 HDC hdc;
1572
1573 hwnd = GetDesktopWindow();
1574 hdc = GetWindowDC(hwnd);
1575
1576 points = MulDiv(pixels, 72,
1577 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1578
1579 ReleaseDC(hwnd, hdc);
1580
1581 return points;
1582}
1583
1584 GuiFont
1585gui_mch_get_font(
1586 char_u *name,
1587 int giveErrorIfMissing)
1588{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001589 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 GuiFont font = NOFONT;
1591
1592 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001593 {
1594 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001596 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001598 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599 return font;
1600}
1601
1602#if defined(FEAT_EVAL) || defined(PROTO)
1603/*
1604 * Return the name of font "font" in allocated memory.
1605 * Don't know how to get the actual name, thus use the provided name.
1606 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001607 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001608gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001609{
1610 if (name == NULL)
1611 return NULL;
1612 return vim_strsave(name);
1613}
1614#endif
1615
1616 void
1617gui_mch_free_font(GuiFont font)
1618{
1619 if (font)
1620 DeleteObject((HFONT)font);
1621}
1622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001623/*
1624 * Return the Pixel value (color) for the given color name.
1625 * Return INVALCOLOR for error.
1626 */
1627 guicolor_T
1628gui_mch_get_color(char_u *name)
1629{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001630 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001631
1632 typedef struct SysColorTable
1633 {
1634 char *name;
1635 int color;
1636 } SysColorTable;
1637
1638 static SysColorTable sys_table[] =
1639 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1641 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001642#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1644#endif
1645 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1646 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1647 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1648 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1649 {"SYS_DESKTOP", COLOR_DESKTOP},
1650 {"SYS_INFOBK", COLOR_INFOBK},
1651 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1652 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 {"SYS_BTNFACE", COLOR_BTNFACE},
1654 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1655 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1656 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1657 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1658 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1659 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1660 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1661 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1662 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1663 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1664 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1665 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1666 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1667 {"SYS_MENU", COLOR_MENU},
1668 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1669 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1670 {"SYS_WINDOW", COLOR_WINDOW},
1671 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1672 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1673 };
1674
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001675 /*
1676 * Try to look up a system colour.
1677 */
K.Takataeeec2542021-06-02 13:28:16 +02001678 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001679 if (STRICMP(name, sys_table[i].name) == 0)
1680 return GetSysColor(sys_table[i].color);
1681
Bram Moolenaarab302212016-04-26 20:59:29 +02001682 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001683}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001684
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001685 guicolor_T
1686gui_mch_get_rgb_color(int r, int g, int b)
1687{
1688 return gui_get_rgb_color_cmn(r, g, b);
1689}
1690
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691/*
1692 * Return OK if the key with the termcap name "name" is supported.
1693 */
1694 int
1695gui_mch_haskey(char_u *name)
1696{
1697 int i;
1698
1699 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1700 if (name[0] == special_keys[i].vim_code0 &&
1701 name[1] == special_keys[i].vim_code1)
1702 return OK;
1703 return FAIL;
1704}
1705
1706 void
1707gui_mch_beep(void)
1708{
1709 MessageBeep(MB_OK);
1710}
1711/*
1712 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1713 */
1714 void
1715gui_mch_invert_rectangle(
1716 int r,
1717 int c,
1718 int nr,
1719 int nc)
1720{
1721 RECT rc;
1722
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001723#if defined(FEAT_DIRECTX)
1724 if (IS_ENABLE_DIRECTX())
1725 DWriteContext_Flush(s_dwc);
1726#endif
1727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 /*
1729 * Note: InvertRect() excludes right and bottom of rectangle.
1730 */
1731 rc.left = FILL_X(c);
1732 rc.top = FILL_Y(r);
1733 rc.right = rc.left + nc * gui.char_width;
1734 rc.bottom = rc.top + nr * gui.char_height;
1735 InvertRect(s_hdc, &rc);
1736}
1737
1738/*
1739 * Iconify the GUI window.
1740 */
1741 void
1742gui_mch_iconify(void)
1743{
1744 ShowWindow(s_hwnd, SW_MINIMIZE);
1745}
1746
1747/*
1748 * Draw a cursor without focus.
1749 */
1750 void
1751gui_mch_draw_hollow_cursor(guicolor_T color)
1752{
1753 HBRUSH hbr;
1754 RECT rc;
1755
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001756#if defined(FEAT_DIRECTX)
1757 if (IS_ENABLE_DIRECTX())
1758 DWriteContext_Flush(s_dwc);
1759#endif
1760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761 /*
1762 * Note: FrameRect() excludes right and bottom of rectangle.
1763 */
1764 rc.left = FILL_X(gui.col);
1765 rc.top = FILL_Y(gui.row);
1766 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001767 if (mb_lefthalve(gui.row, gui.col))
1768 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001769 rc.bottom = rc.top + gui.char_height;
1770 hbr = CreateSolidBrush(color);
1771 FrameRect(s_hdc, &rc, hbr);
1772 DeleteBrush(hbr);
1773}
1774/*
1775 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1776 * color "color".
1777 */
1778 void
1779gui_mch_draw_part_cursor(
1780 int w,
1781 int h,
1782 guicolor_T color)
1783{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001784 RECT rc;
1785
1786 /*
1787 * Note: FillRect() excludes right and bottom of rectangle.
1788 */
1789 rc.left =
1790#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001791 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1793#endif
1794 FILL_X(gui.col);
1795 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1796 rc.right = rc.left + w;
1797 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001798
Bram Moolenaar92467d32017-12-05 13:22:16 +01001799 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800}
1801
1802
1803/*
1804 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1805 * dead key's nominal character and re-post the original message.
1806 */
1807 static void
1808outputDeadKey_rePost(MSG originalMsg)
1809{
1810 static MSG deadCharExpel;
1811
1812 if (!dead_key)
1813 return;
1814
1815 dead_key = 0;
1816
Bram Moolenaar734a8672019-12-02 22:49:38 +01001817 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 deadCharExpel.message = originalMsg.message;
1819 deadCharExpel.hwnd = originalMsg.hwnd;
1820 deadCharExpel.wParam = VK_SPACE;
1821
K.Takata4ac893f2022-01-20 12:44:28 +00001822 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823
Bram Moolenaar734a8672019-12-02 22:49:38 +01001824 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1826 originalMsg.lParam);
1827}
1828
1829
1830/*
1831 * Process a single Windows message.
1832 * If one is not available we hang until one is.
1833 */
1834 static void
1835process_message(void)
1836{
1837 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001838 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001839 char_u string[40];
1840 int i;
1841 int modifiers = 0;
1842 int key;
1843#ifdef FEAT_MENU
1844 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1845#endif
1846
1847 pGetMessage(&msg, NULL, 0, 0);
1848
1849#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001850 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851 if (msg.message == WM_OLE)
1852 {
1853 char_u *str = (char_u *)msg.lParam;
1854 if (str == NULL || *str == NUL)
1855 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001856 // Message can't be ours, forward it. Fixes problem with Ultramon
1857 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858 pDispatchMessage(&msg);
1859 }
1860 else
1861 {
1862 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 }
1865 return;
1866 }
1867#endif
1868
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001870 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1872 {
1873 HandleMouseHide(msg.message, msg.lParam);
1874 return;
1875 }
1876#endif
1877
1878 /*
1879 * Check if it's a special key that we recognise. If not, call
1880 * TranslateMessage().
1881 */
1882 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1883 {
1884 vk = (int) msg.wParam;
1885
1886 /*
1887 * Handle dead keys in special conditions in other cases we let Windows
1888 * handle them and do not interfere.
1889 *
1890 * The dead_key flag must be reset on several occasions:
1891 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1892 * consumed at that point (This is when we let Windows combine the
1893 * dead character on its own)
1894 *
1895 * - Before doing something special such as regenerating keypresses to
1896 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001897 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898 * immediately to _OnChar() (or _OnSysChar()).
1899 */
1900 if (dead_key)
1901 {
1902 /*
1903 * If a dead key was pressed and the user presses VK_SPACE,
1904 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1905 * with the dead char now, so do nothing special and let Windows
1906 * handle it.
1907 *
1908 * Note that VK_SPACE combines with the dead_key's character and
1909 * only one WM_CHAR will be generated by TranslateMessage(), in
1910 * the two other cases two WM_CHAR will be generated: the dead
1911 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1912 * user expects.
1913 */
1914 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1915 {
1916 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001917 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001918 return;
1919 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001920 // In modes where we are not typing, dead keys should behave
1921 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001922 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1923 {
1924 outputDeadKey_rePost(msg);
1925 return;
1926 }
1927 }
1928
Bram Moolenaar734a8672019-12-02 22:49:38 +01001929 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 if (vk == VK_CANCEL)
1931 {
1932 trash_input_buf();
1933 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001934 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 string[0] = Ctrl_C;
1936 add_to_input_buf(string, 1);
1937 }
1938
1939 for (i = 0; special_keys[i].key_sym != 0; i++)
1940 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001941 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001942 if (special_keys[i].key_sym == vk
1943 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1944 {
1945 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001946 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 * is a key that would normally trigger the dead key nominal
1948 * character output (such as a NUMPAD printable character or
1949 * the TAB key, etc...).
1950 */
1951 if (dead_key && (special_keys[i].vim_code0 == 'K'
1952 || vk == VK_TAB || vk == CAR))
1953 {
1954 outputDeadKey_rePost(msg);
1955 return;
1956 }
1957
1958#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001959 // Check for <F10>: Windows selects the menu. When <F10> is
1960 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001961 if (vk == VK_F10
1962 && gui.menu_is_active
1963 && check_map(k10, State, FALSE, TRUE, FALSE,
1964 NULL, NULL) == NULL)
1965 break;
1966#endif
1967 if (GetKeyState(VK_SHIFT) & 0x8000)
1968 modifiers |= MOD_MASK_SHIFT;
1969 /*
1970 * Don't use caps-lock as shift, because these are special keys
1971 * being considered here, and we only want letters to get
1972 * shifted -- webb
1973 */
1974 /*
1975 if (GetKeyState(VK_CAPITAL) & 0x0001)
1976 modifiers ^= MOD_MASK_SHIFT;
1977 */
1978 if (GetKeyState(VK_CONTROL) & 0x8000)
1979 modifiers |= MOD_MASK_CTRL;
1980 if (GetKeyState(VK_MENU) & 0x8000)
1981 modifiers |= MOD_MASK_ALT;
1982
1983 if (special_keys[i].vim_code1 == NUL)
1984 key = special_keys[i].vim_code0;
1985 else
1986 key = TO_SPECIAL(special_keys[i].vim_code0,
1987 special_keys[i].vim_code1);
1988 key = simplify_key(key, &modifiers);
1989 if (key == CSI)
1990 key = K_CSI;
1991
1992 if (modifiers)
1993 {
1994 string[0] = CSI;
1995 string[1] = KS_MODIFIER;
1996 string[2] = modifiers;
1997 add_to_input_buf(string, 3);
1998 }
1999
2000 if (IS_SPECIAL(key))
2001 {
2002 string[0] = CSI;
2003 string[1] = K_SECOND(key);
2004 string[2] = K_THIRD(key);
2005 add_to_input_buf(string, 3);
2006 }
2007 else
2008 {
2009 int len;
2010
Bram Moolenaar734a8672019-12-02 22:49:38 +01002011 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002012 len = char_to_string(key, string, 40, FALSE);
2013 add_to_input_buf(string, len);
2014 }
2015 break;
2016 }
2017 }
2018 if (special_keys[i].key_sym == 0)
2019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002020 // Some keys need C-S- where they should only need C-.
2021 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2022 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002023 if (vk != 0xff
2024 && (GetKeyState(VK_CONTROL) & 0x8000)
2025 && !(GetKeyState(VK_SHIFT) & 0x8000)
2026 && !(GetKeyState(VK_MENU) & 0x8000))
2027 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002028 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002029 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2030 {
2031 string[0] = Ctrl_HAT;
2032 add_to_input_buf(string, 1);
2033 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002034 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2035 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002036 {
2037 string[0] = Ctrl__;
2038 add_to_input_buf(string, 1);
2039 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002040 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2042 {
2043 string[0] = Ctrl_AT;
2044 add_to_input_buf(string, 1);
2045 }
2046 else
K.Takata4ac893f2022-01-20 12:44:28 +00002047 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048 }
2049 else
K.Takata4ac893f2022-01-20 12:44:28 +00002050 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 }
2052 }
2053#ifdef FEAT_MBYTE_IME
2054 else if (msg.message == WM_IME_NOTIFY)
2055 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2056 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002057 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002058 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059#endif
2060
2061#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002062 // Check for <F10>: Default effect is to select the menu. When <F10> is
2063 // mapped we need to stop it here to avoid strange effects (e.g., for the
2064 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002065 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2066 NULL, NULL) == NULL)
2067#endif
2068 pDispatchMessage(&msg);
2069}
2070
2071/*
2072 * Catch up with any queued events. This may put keyboard input into the
2073 * input buffer, call resize call-backs, trigger timers etc. If there is
2074 * nothing in the event queue (& no timers pending), then we return
2075 * immediately.
2076 */
2077 void
2078gui_mch_update(void)
2079{
2080 MSG msg;
2081
2082 if (!s_busy_processing)
2083 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2084 && !vim_is_input_buf_full())
2085 process_message();
2086}
2087
Bram Moolenaar4231da42016-06-02 14:30:04 +02002088 static void
2089remove_any_timer(void)
2090{
2091 MSG msg;
2092
2093 if (s_wait_timer != 0 && !s_timed_out)
2094 {
2095 KillTimer(NULL, s_wait_timer);
2096
Bram Moolenaar734a8672019-12-02 22:49:38 +01002097 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002098 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2099 ;
2100 s_wait_timer = 0;
2101 }
2102}
2103
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104/*
2105 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2106 * from the keyboard.
2107 * wtime == -1 Wait forever.
2108 * wtime == 0 This should never happen.
2109 * wtime > 0 Wait wtime milliseconds for a character.
2110 * Returns OK if a character was found to be available within the given time,
2111 * or FAIL otherwise.
2112 */
2113 int
2114gui_mch_wait_for_chars(int wtime)
2115{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116 int focus;
2117
2118 s_timed_out = FALSE;
2119
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002120 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002122 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123 if (s_busy_processing)
2124 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002125
2126 // When called with "wtime" zero, just want one msec.
2127 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002128 (TIMERPROC)_OnTimer);
2129 }
2130
2131 allow_scrollbar = TRUE;
2132
2133 focus = gui.in_focus;
2134 while (!s_timed_out)
2135 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002137 if (gui.in_focus != focus)
2138 {
2139 if (gui.in_focus)
2140 gui_mch_start_blink();
2141 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002142 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002143 focus = gui.in_focus;
2144 }
2145
2146 if (s_need_activate)
2147 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 s_need_activate = FALSE;
2150 }
2151
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152#ifdef FEAT_TIMERS
2153 did_add_timer = FALSE;
2154#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002156 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002157 for (;;)
2158 {
2159 MSG msg;
2160
2161 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002162# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002163 if (did_add_timer)
2164 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002165# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002166 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2167 {
2168 process_message();
2169 break;
2170 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002171 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002172 // TODO: The 10 msec is a compromise between laggy response
2173 // and consuming more CPU time. Better would be to handle
2174 // channel messages when they arrive.
2175 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002176 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002177 break;
2178 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002179#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002180 // Don't use gui_mch_update() because then we will spin-lock until a
2181 // char arrives, instead we use GetMessage() to hang until an
2182 // event arrives. No need to check for input_buf_full because we are
2183 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002184 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002185#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186
2187 if (input_available())
2188 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002189 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002190 allow_scrollbar = FALSE;
2191
Bram Moolenaar89c00032019-09-04 13:53:21 +02002192 // Clear pending mouse button, the release event may have been
2193 // taken by the dialog window. But don't do this when getting
2194 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195 if (!s_getting_focus)
2196 s_button_pending = -1;
2197
2198 return OK;
2199 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002200
2201#ifdef FEAT_TIMERS
2202 if (did_add_timer)
2203 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002204 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002205 remove_any_timer();
2206 break;
2207 }
2208#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002209 }
2210 allow_scrollbar = FALSE;
2211 return FAIL;
2212}
2213
2214/*
2215 * Clear a rectangular region of the screen from text pos (row1, col1) to
2216 * (row2, col2) inclusive.
2217 */
2218 void
2219gui_mch_clear_block(
2220 int row1,
2221 int col1,
2222 int row2,
2223 int col2)
2224{
2225 RECT rc;
2226
2227 /*
2228 * Clear one extra pixel at the far right, for when bold characters have
2229 * spilled over to the window border.
2230 * Note: FillRect() excludes right and bottom of rectangle.
2231 */
2232 rc.left = FILL_X(col1);
2233 rc.top = FILL_Y(row1);
2234 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2235 rc.bottom = FILL_Y(row2 + 1);
2236 clear_rect(&rc);
2237}
2238
2239/*
2240 * Clear the whole text window.
2241 */
2242 void
2243gui_mch_clear_all(void)
2244{
2245 RECT rc;
2246
2247 rc.left = 0;
2248 rc.top = 0;
2249 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2250 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2251 clear_rect(&rc);
2252}
2253/*
2254 * Menu stuff.
2255 */
2256
2257 void
2258gui_mch_enable_menu(int flag)
2259{
2260#ifdef FEAT_MENU
2261 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2262#endif
2263}
2264
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002265 void
2266gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002267 int x UNUSED,
2268 int y UNUSED,
2269 int w UNUSED,
2270 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002272 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273}
2274
2275#if defined(FEAT_MENU) || defined(PROTO)
2276/*
2277 * Make menu item hidden or not hidden
2278 */
2279 void
2280gui_mch_menu_hidden(
2281 vimmenu_T *menu,
2282 int hidden)
2283{
2284 /*
2285 * This doesn't do what we want. Hmm, just grey the menu items for now.
2286 */
2287 /*
2288 if (hidden)
2289 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2290 else
2291 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2292 */
2293 gui_mch_menu_grey(menu, hidden);
2294}
2295
2296/*
2297 * This is called after setting all the menus to grey/hidden or not.
2298 */
2299 void
2300gui_mch_draw_menubar(void)
2301{
2302 DrawMenuBar(s_hwnd);
2303}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002304#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002306/*
2307 * Return the RGB value of a pixel as a long.
2308 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002309 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310gui_mch_get_rgb(guicolor_T pixel)
2311{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002312 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2313 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314}
2315
2316#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002317/*
2318 * Convert pixels in X to dialog units
2319 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320 static WORD
2321PixelToDialogX(int numPixels)
2322{
2323 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2324}
2325
Bram Moolenaar734a8672019-12-02 22:49:38 +01002326/*
2327 * Convert pixels in Y to dialog units
2328 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002329 static WORD
2330PixelToDialogY(int numPixels)
2331{
2332 return (WORD)((numPixels * 8) / s_dlgfntheight);
2333}
2334
Bram Moolenaar734a8672019-12-02 22:49:38 +01002335/*
2336 * Return the width in pixels of the given text in the given DC.
2337 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 static int
2339GetTextWidth(HDC hdc, char_u *str, int len)
2340{
2341 SIZE size;
2342
2343 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2344 return size.cx;
2345}
2346
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002347/*
2348 * Return the width in pixels of the given text in the given DC, taking care
2349 * of 'encoding' to active codepage conversion.
2350 */
2351 static int
2352GetTextWidthEnc(HDC hdc, char_u *str, int len)
2353{
2354 SIZE size;
2355 WCHAR *wstr;
2356 int n;
2357 int wlen = len;
2358
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002359 wstr = enc_to_utf16(str, &wlen);
2360 if (wstr == NULL)
2361 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002363 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2364 vim_free(wstr);
2365 if (n)
2366 return size.cx;
2367 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002368}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370static void get_work_area(RECT *spi_rect);
2371
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002372/*
2373 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2375 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 */
2377 static BOOL
2378CenterWindow(
2379 HWND hwndChild,
2380 HWND hwndParent)
2381{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002382 HMONITOR mon;
2383 MONITORINFO moninfo;
2384 RECT rChild, rParent, rScreen;
2385 int wChild, hChild, wParent, hParent;
2386 int xNew, yNew;
2387 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002388
2389 GetWindowRect(hwndChild, &rChild);
2390 wChild = rChild.right - rChild.left;
2391 hChild = rChild.bottom - rChild.top;
2392
Bram Moolenaar734a8672019-12-02 22:49:38 +01002393 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002395 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396 else
2397 GetWindowRect(hwndParent, &rParent);
2398 wParent = rParent.right - rParent.left;
2399 hParent = rParent.bottom - rParent.top;
2400
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002401 moninfo.cbSize = sizeof(MONITORINFO);
2402 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2403 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002405 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002407 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002409 hdc = GetDC(hwndChild);
2410 rScreen.left = 0;
2411 rScreen.top = 0;
2412 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2413 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2414 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 }
2416
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002417 xNew = rParent.left + ((wParent - wChild) / 2);
2418 if (xNew < rScreen.left)
2419 xNew = rScreen.left;
2420 else if ((xNew + wChild) > rScreen.right)
2421 xNew = rScreen.right - wChild;
2422
2423 yNew = rParent.top + ((hParent - hChild) / 2);
2424 if (yNew < rScreen.top)
2425 yNew = rScreen.top;
2426 else if ((yNew + hChild) > rScreen.bottom)
2427 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002428
2429 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2430 SWP_NOSIZE | SWP_NOZORDER);
2431}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002432#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434#if defined(FEAT_TOOLBAR) || defined(PROTO)
2435 void
2436gui_mch_show_toolbar(int showit)
2437{
2438 if (s_toolbarhwnd == NULL)
2439 return;
2440
2441 if (showit)
2442 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002443# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002444 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002445# define TB_SETUNICODEFORMAT 0x2005
2446# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002447 // Enable unicode support
2448 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2449 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 ShowWindow(s_toolbarhwnd, SW_SHOW);
2451 }
2452 else
2453 ShowWindow(s_toolbarhwnd, SW_HIDE);
2454}
2455
Bram Moolenaar734a8672019-12-02 22:49:38 +01002456// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002457# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458
2459#endif
2460
2461#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2462 static void
2463add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2464{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002465 WCHAR *wn;
2466 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002467
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002468 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002470 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002472 infow.cbSize = sizeof(infow);
2473 infow.fMask = MIIM_TYPE | MIIM_ID;
2474 infow.wID = item_id;
2475 infow.fType = MFT_STRING;
2476 infow.dwTypeData = wn;
2477 infow.cch = (UINT)wcslen(wn);
2478 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2479 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480}
2481
2482 static void
2483show_tabline_popup_menu(void)
2484{
2485 HMENU tab_pmenu;
2486 long rval;
2487 POINT pt;
2488
Bram Moolenaar734a8672019-12-02 22:49:38 +01002489 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002490 if (hold_gui_events
2491# ifdef FEAT_CMDWIN
2492 || cmdwin_type != 0
2493# endif
2494 )
2495 return;
2496
2497 tab_pmenu = CreatePopupMenu();
2498 if (tab_pmenu == NULL)
2499 return;
2500
2501 if (first_tabpage->tp_next != NULL)
2502 add_tabline_popup_menu_entry(tab_pmenu,
2503 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2504 add_tabline_popup_menu_entry(tab_pmenu,
2505 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2506 add_tabline_popup_menu_entry(tab_pmenu,
2507 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2508
2509 GetCursorPos(&pt);
2510 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2511 NULL);
2512
2513 DestroyMenu(tab_pmenu);
2514
Bram Moolenaar734a8672019-12-02 22:49:38 +01002515 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516 if (rval > 0)
2517 {
2518 TCHITTESTINFO htinfo;
2519 int idx;
2520
2521 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2522 return;
2523
2524 htinfo.pt.x = pt.x;
2525 htinfo.pt.y = pt.y;
2526 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2527 if (idx == -1)
2528 idx = 0;
2529 else
2530 idx += 1;
2531
2532 send_tabline_menu_event(idx, (int)rval);
2533 }
2534}
2535
2536/*
2537 * Show or hide the tabline.
2538 */
2539 void
2540gui_mch_show_tabline(int showit)
2541{
2542 if (s_tabhwnd == NULL)
2543 return;
2544
2545 if (!showit != !showing_tabline)
2546 {
2547 if (showit)
2548 ShowWindow(s_tabhwnd, SW_SHOW);
2549 else
2550 ShowWindow(s_tabhwnd, SW_HIDE);
2551 showing_tabline = showit;
2552 }
2553}
2554
2555/*
2556 * Return TRUE when tabline is displayed.
2557 */
2558 int
2559gui_mch_showing_tabline(void)
2560{
2561 return s_tabhwnd != NULL && showing_tabline;
2562}
2563
2564/*
2565 * Update the labels of the tabline.
2566 */
2567 void
2568gui_mch_update_tabline(void)
2569{
2570 tabpage_T *tp;
2571 TCITEM tie;
2572 int nr = 0;
2573 int curtabidx = 0;
2574 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002575 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576
2577 if (s_tabhwnd == NULL)
2578 return;
2579
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002580# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002581 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002582# define CCM_SETUNICODEFORMAT 0x2005
2583# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002584 // Enable unicode support
2585 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586
2587 tie.mask = TCIF_TEXT;
2588 tie.iImage = -1;
2589
Bram Moolenaar734a8672019-12-02 22:49:38 +01002590 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2592
Bram Moolenaar734a8672019-12-02 22:49:38 +01002593 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2595 {
2596 if (tp == curtab)
2597 curtabidx = nr;
2598
2599 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2600 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002601 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 tie.pszText = "-Empty-";
2603 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2604 tabadded = 1;
2605 }
2606
2607 get_tabline_label(tp, FALSE);
2608 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002610 wstr = enc_to_utf16(NameBuff, NULL);
2611 if (wstr != NULL)
2612 {
2613 TCITEMW tiw;
2614
2615 tiw.mask = TCIF_TEXT;
2616 tiw.iImage = -1;
2617 tiw.pszText = wstr;
2618 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2619 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002621 }
2622
Bram Moolenaar734a8672019-12-02 22:49:38 +01002623 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2625 TabCtrl_DeleteItem(s_tabhwnd, nr);
2626
2627 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2628 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2629
Bram Moolenaar734a8672019-12-02 22:49:38 +01002630 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002631 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2632 RedrawWindow(s_tabhwnd, NULL, NULL,
2633 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2634
2635 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2636 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2637}
2638
2639/*
2640 * Set the current tab to "nr". First tab is 1.
2641 */
2642 void
2643gui_mch_set_curtab(int nr)
2644{
2645 if (s_tabhwnd == NULL)
2646 return;
2647
2648 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2649 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2650}
2651
2652#endif
2653
2654/*
2655 * ":simalt" command.
2656 */
2657 void
2658ex_simalt(exarg_T *eap)
2659{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002660 char_u *keys = eap->arg;
2661 int fill_typebuf = FALSE;
2662 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663
2664 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2665 while (*keys)
2666 {
2667 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2670 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002671 fill_typebuf = TRUE;
2672 }
2673 if (fill_typebuf)
2674 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002675 // Put a NOP in the typeahead buffer so that the message will get
2676 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002677 key_name[0] = K_SPECIAL;
2678 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002679 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002680 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002681#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002682 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002683#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002684 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002685 }
2686}
2687
2688/*
2689 * Create the find & replace dialogs.
2690 * You can't have both at once: ":find" when replace is showing, destroys
2691 * the replace dialog first, and the other way around.
2692 */
2693#ifdef MSWIN_FIND_REPLACE
2694 static void
2695initialise_findrep(char_u *initial_string)
2696{
2697 int wword = FALSE;
2698 int mcase = !p_ic;
2699 char_u *entry_text;
2700
Bram Moolenaar734a8672019-12-02 22:49:38 +01002701 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2703
2704 s_findrep_struct.hwndOwner = s_hwnd;
2705 s_findrep_struct.Flags = FR_DOWN;
2706 if (mcase)
2707 s_findrep_struct.Flags |= FR_MATCHCASE;
2708 if (wword)
2709 s_findrep_struct.Flags |= FR_WHOLEWORD;
2710 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002711 {
2712 WCHAR *p = enc_to_utf16(entry_text, NULL);
2713 if (p != NULL)
2714 {
2715 int len = s_findrep_struct.wFindWhatLen - 1;
2716
2717 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2718 s_findrep_struct.lpstrFindWhat[len] = NUL;
2719 vim_free(p);
2720 }
2721 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722 vim_free(entry_text);
2723}
2724#endif
2725
2726 static void
2727set_window_title(HWND hwnd, char *title)
2728{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002729 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 {
2731 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732
Bram Moolenaar734a8672019-12-02 22:49:38 +01002733 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2735 if (wbuf != NULL)
2736 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002737 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739 }
2740 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002741 else
2742 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743}
2744
2745 void
2746gui_mch_find_dialog(exarg_T *eap)
2747{
2748#ifdef MSWIN_FIND_REPLACE
2749 if (s_findrep_msg != 0)
2750 {
2751 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2752 DestroyWindow(s_findrep_hwnd);
2753
2754 if (!IsWindow(s_findrep_hwnd))
2755 {
2756 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002757 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758 }
2759
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002760 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 (void)SetFocus(s_findrep_hwnd);
2762
2763 s_findrep_is_find = TRUE;
2764 }
2765#endif
2766}
2767
2768
2769 void
2770gui_mch_replace_dialog(exarg_T *eap)
2771{
2772#ifdef MSWIN_FIND_REPLACE
2773 if (s_findrep_msg != 0)
2774 {
2775 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2776 DestroyWindow(s_findrep_hwnd);
2777
2778 if (!IsWindow(s_findrep_hwnd))
2779 {
2780 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002781 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782 }
2783
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002784 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002785 (void)SetFocus(s_findrep_hwnd);
2786
2787 s_findrep_is_find = FALSE;
2788 }
2789#endif
2790}
2791
2792
2793/*
2794 * Set visibility of the pointer.
2795 */
2796 void
2797gui_mch_mousehide(int hide)
2798{
2799 if (hide != gui.pointer_hidden)
2800 {
2801 ShowCursor(!hide);
2802 gui.pointer_hidden = hide;
2803 }
2804}
2805
2806#ifdef FEAT_MENU
2807 static void
2808gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2809{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002810 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811 gui_mch_mousehide(FALSE);
2812
2813 (void)TrackPopupMenu(
2814 (HMENU)menu->submenu_id,
2815 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2816 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002817 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002818 s_hwnd,
2819 NULL);
2820 /*
2821 * NOTE: The pop-up menu can eat the mouse up event.
2822 * We deal with this in normal.c.
2823 */
2824}
2825#endif
2826
2827/*
2828 * Got a message when the system will go down.
2829 */
2830 static void
2831_OnEndSession(void)
2832{
2833 getout_preserve_modified(1);
2834}
2835
2836/*
2837 * Get this message when the user clicks on the cross in the top right corner
2838 * of a Windows95 window.
2839 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002841_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842{
2843 gui_shell_closed();
2844}
2845
2846/*
2847 * Get a message when the window is being destroyed.
2848 */
2849 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002850_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851{
2852 if (!destroying)
2853 _OnClose(hwnd);
2854}
2855
2856 static void
2857_OnPaint(
2858 HWND hwnd)
2859{
2860 if (!IsMinimized(hwnd))
2861 {
2862 PAINTSTRUCT ps;
2863
Bram Moolenaar734a8672019-12-02 22:49:38 +01002864 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002866
Bram Moolenaar734a8672019-12-02 22:49:38 +01002867 // prevent multi-byte characters from misprinting on an invalid
2868 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002869 if (has_mbyte)
2870 {
2871 RECT rect;
2872
2873 GetClientRect(hwnd, &rect);
2874 ps.rcPaint.left = rect.left;
2875 ps.rcPaint.right = rect.right;
2876 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877
2878 if (!IsRectEmpty(&ps.rcPaint))
2879 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2881 ps.rcPaint.right - ps.rcPaint.left + 1,
2882 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2883 }
2884
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885 EndPaint(hwnd, &ps);
2886 }
2887}
2888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889 static void
2890_OnSize(
2891 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002892 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002893 int cx,
2894 int cy)
2895{
K.Takatac81e9bf2022-01-16 14:15:49 +00002896 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897 {
2898 gui_resize_shell(cx, cy);
2899
Bram Moolenaar734a8672019-12-02 22:49:38 +01002900 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902 }
2903}
2904
2905 static void
2906_OnSetFocus(
2907 HWND hwnd,
2908 HWND hwndOldFocus)
2909{
2910 gui_focus_change(TRUE);
2911 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002912 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913}
2914
2915 static void
2916_OnKillFocus(
2917 HWND hwnd,
2918 HWND hwndNewFocus)
2919{
2920 gui_focus_change(FALSE);
2921 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002922 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923}
2924
2925/*
2926 * Get a message when the user switches back to vim
2927 */
2928 static LRESULT
2929_OnActivateApp(
2930 HWND hwnd,
2931 BOOL fActivate,
2932 DWORD dwThreadId)
2933{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002934 // we call gui_focus_change() in _OnSetFocus()
2935 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002936 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002937}
2938
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 void
2940gui_mch_destroy_scrollbar(scrollbar_T *sb)
2941{
2942 DestroyWindow(sb->id);
2943}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002944
2945/*
2946 * Get current mouse coordinates in text window.
2947 */
2948 void
2949gui_mch_getmouse(int *x, int *y)
2950{
2951 RECT rct;
2952 POINT mp;
2953
2954 (void)GetWindowRect(s_textArea, &rct);
2955 (void)GetCursorPos((LPPOINT)&mp);
2956 *x = (int)(mp.x - rct.left);
2957 *y = (int)(mp.y - rct.top);
2958}
2959
2960/*
2961 * Move mouse pointer to character at (x, y).
2962 */
2963 void
2964gui_mch_setmouse(int x, int y)
2965{
2966 RECT rct;
2967
2968 (void)GetWindowRect(s_textArea, &rct);
2969 (void)SetCursorPos(x + gui.border_offset + rct.left,
2970 y + gui.border_offset + rct.top);
2971}
2972
2973 static void
2974gui_mswin_get_valid_dimensions(
2975 int w,
2976 int h,
2977 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002978 int *valid_h,
2979 int *cols,
2980 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002981{
2982 int base_width, base_height;
2983
2984 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002985 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2986 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002988 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2989 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2990 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2991 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002992 *cols = (w - base_width) / gui.char_width;
2993 *rows = (h - base_height) / gui.char_height;
2994 *valid_w = base_width + *cols * gui.char_width;
2995 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002996}
2997
2998 void
2999gui_mch_flash(int msec)
3000{
3001 RECT rc;
3002
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003003#if defined(FEAT_DIRECTX)
3004 if (IS_ENABLE_DIRECTX())
3005 DWriteContext_Flush(s_dwc);
3006#endif
3007
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003008 /*
3009 * Note: InvertRect() excludes right and bottom of rectangle.
3010 */
3011 rc.left = 0;
3012 rc.top = 0;
3013 rc.right = gui.num_cols * gui.char_width;
3014 rc.bottom = gui.num_rows * gui.char_height;
3015 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003016 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003017
Bram Moolenaar734a8672019-12-02 22:49:38 +01003018 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019
3020 InvertRect(s_hdc, &rc);
3021}
3022
3023/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003024 * Check if the specified point is on-screen. (multi-monitor aware)
3025 */
3026 static BOOL
3027is_point_onscreen(int x, int y)
3028{
3029 POINT pt = {x, y};
3030
3031 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3032}
3033
3034/*
3035 * Check if the whole area of the specified window is on-screen.
3036 *
3037 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3038 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3039 * only works when the whole window is on-screen.
3040 */
3041 static BOOL
3042is_window_onscreen(HWND hwnd)
3043{
3044 RECT rc;
3045
3046 GetWindowRect(hwnd, &rc);
3047
3048 if (!is_point_onscreen(rc.left, rc.top))
3049 return FALSE;
3050 if (!is_point_onscreen(rc.left, rc.bottom))
3051 return FALSE;
3052 if (!is_point_onscreen(rc.right, rc.top))
3053 return FALSE;
3054 if (!is_point_onscreen(rc.right, rc.bottom))
3055 return FALSE;
3056 return TRUE;
3057}
3058
3059/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 * Return flags used for scrolling.
3061 * The SW_INVALIDATE is required when part of the window is covered or
3062 * off-screen. Refer to MS KB Q75236.
3063 */
3064 static int
3065get_scroll_flags(void)
3066{
3067 HWND hwnd;
3068 RECT rcVim, rcOther, rcDest;
3069
Bram Moolenaar185577e2020-10-29 20:08:21 +01003070 // Check if the window is (partly) off-screen.
3071 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003072 return SW_INVALIDATE;
3073
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003074 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003075 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3077 if (IsWindowVisible(hwnd))
3078 {
3079 GetWindowRect(hwnd, &rcOther);
3080 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3081 return SW_INVALIDATE;
3082 }
3083 return 0;
3084}
3085
3086/*
3087 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3088 * may not be scrolled out properly.
3089 * For gVim, when _OnScroll() is repeated, the character at the
3090 * previous cursor position may be left drawn after scroll.
3091 * The problem can be avoided by calling GetPixel() to get a pixel in
3092 * the region before ScrollWindowEx().
3093 */
3094 static void
3095intel_gpu_workaround(void)
3096{
3097 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3098}
3099
3100/*
3101 * Delete the given number of lines from the given row, scrolling up any
3102 * text further down within the scroll region.
3103 */
3104 void
3105gui_mch_delete_lines(
3106 int row,
3107 int num_lines)
3108{
3109 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003110
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003111 rc.left = FILL_X(gui.scroll_region_left);
3112 rc.right = FILL_X(gui.scroll_region_right + 1);
3113 rc.top = FILL_Y(row);
3114 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3115
Bram Moolenaar92467d32017-12-05 13:22:16 +01003116#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003117 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003118 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003119 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003120 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003121 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003122#endif
3123 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003124#if defined(FEAT_DIRECTX)
3125 if (IS_ENABLE_DIRECTX())
3126 DWriteContext_Flush(s_dwc);
3127#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003128 intel_gpu_workaround();
3129 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003131 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003132 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133
Bram Moolenaar734a8672019-12-02 22:49:38 +01003134 // This seems to be required to avoid the cursor disappearing when
3135 // scrolling such that the cursor ends up in the top-left character on
3136 // the screen... But why? (Webb)
3137 // It's probably fixed by disabling drawing the cursor while scrolling.
3138 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003139
3140 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3141 gui.scroll_region_left,
3142 gui.scroll_region_bot, gui.scroll_region_right);
3143}
3144
3145/*
3146 * Insert the given number of lines before the given row, scrolling down any
3147 * following text within the scroll region.
3148 */
3149 void
3150gui_mch_insert_lines(
3151 int row,
3152 int num_lines)
3153{
3154 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003155
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156 rc.left = FILL_X(gui.scroll_region_left);
3157 rc.right = FILL_X(gui.scroll_region_right + 1);
3158 rc.top = FILL_Y(row);
3159 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160
3161#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003164 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003165 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003166 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003167#endif
3168 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003169#if defined(FEAT_DIRECTX)
3170 if (IS_ENABLE_DIRECTX())
3171 DWriteContext_Flush(s_dwc);
3172#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003173 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003174 // The SW_INVALIDATE is required when part of the window is covered or
3175 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003178 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003179 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003181 gui_clear_block(row, gui.scroll_region_left,
3182 row + num_lines - 1, gui.scroll_region_right);
3183}
3184
3185
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003187gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188{
3189#if defined(FEAT_DIRECTX)
3190 DWriteContext_Close(s_dwc);
3191 DWrite_Final();
3192 s_dwc = NULL;
3193#endif
3194
3195 ReleaseDC(s_textArea, s_hdc);
3196 DeleteObject(s_brush);
3197
3198#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003199 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3201#endif
3202
Bram Moolenaar734a8672019-12-02 22:49:38 +01003203 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 if (s_hwnd != NULL)
3205 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003206 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003207 DestroyWindow(s_hwnd);
3208 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209}
3210
3211 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003212logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213{
3214 char *p;
3215 char *res;
3216 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003217 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003218 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003219 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003221 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3222 if (font_name == NULL)
3223 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003225 quality_name = quality_id2name((int)lf.lfQuality);
3226
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003227 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003228 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003229 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003230 if (res != NULL)
3231 {
3232 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003233 // make a normal font string out of the lf thing:
3234 points = pixels_to_points(
3235 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3236 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3237 sprintf((char *)p, "%s:h%d", font_name, points);
3238 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003239 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 while (*p)
3241 {
3242 if (*p == ' ')
3243 *p = '_';
3244 ++p;
3245 }
3246 if (lf.lfItalic)
3247 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003248 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 STRCAT(p, ":b");
3250 if (lf.lfUnderline)
3251 STRCAT(p, ":u");
3252 if (lf.lfStrikeOut)
3253 STRCAT(p, ":s");
3254 if (charset_name != NULL)
3255 {
3256 STRCAT(p, ":c");
3257 STRCAT(p, charset_name);
3258 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003259 if (quality_name != NULL)
3260 {
3261 STRCAT(p, ":q");
3262 STRCAT(p, quality_name);
3263 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003264 }
3265
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003266 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 return (char_u *)res;
3268}
3269
3270
3271#ifdef FEAT_MBYTE_IME
3272/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003273 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003274 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 */
3276 static void
3277update_im_font(void)
3278{
K.Takatac81e9bf2022-01-16 14:15:49 +00003279 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003280
3281 if (p_guifontwide != NULL && *p_guifontwide != NUL
3282 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003283 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284 norm_logfont = lf_wide;
3285 else
3286 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003287
3288 lf = norm_logfont;
3289 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3290 // Work around when PerMonitorV2 is not enabled in the process level.
3291 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3292 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003293}
3294#endif
3295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296/*
3297 * Handler of gui.wide_font (p_guifontwide) changed notification.
3298 */
3299 void
3300gui_mch_wide_font_changed(void)
3301{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003302 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003304#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003305 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003306#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307
3308 gui_mch_free_font(gui.wide_ital_font);
3309 gui.wide_ital_font = NOFONT;
3310 gui_mch_free_font(gui.wide_bold_font);
3311 gui.wide_bold_font = NOFONT;
3312 gui_mch_free_font(gui.wide_boldital_font);
3313 gui.wide_boldital_font = NOFONT;
3314
3315 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003316 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317 {
3318 if (!lf.lfItalic)
3319 {
3320 lf.lfItalic = TRUE;
3321 gui.wide_ital_font = get_font_handle(&lf);
3322 lf.lfItalic = FALSE;
3323 }
3324 if (lf.lfWeight < FW_BOLD)
3325 {
3326 lf.lfWeight = FW_BOLD;
3327 gui.wide_bold_font = get_font_handle(&lf);
3328 if (!lf.lfItalic)
3329 {
3330 lf.lfItalic = TRUE;
3331 gui.wide_boldital_font = get_font_handle(&lf);
3332 }
3333 }
3334 }
3335}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336
3337/*
3338 * Initialise vim to use the font with the given name.
3339 * Return FAIL if the font could not be loaded, OK otherwise.
3340 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003342gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343{
K.Takatac81e9bf2022-01-16 14:15:49 +00003344 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345 GuiFont font = NOFONT;
3346 char_u *p;
3347
Bram Moolenaar734a8672019-12-02 22:49:38 +01003348 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003350 {
3351 lfOrig = lf;
3352 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003354 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355 if (font == NOFONT)
3356 return FAIL;
3357
3358 if (font_name == NULL)
3359 font_name = (char_u *)lf.lfFaceName;
K.Takata4ac893f2022-01-20 12:44:28 +00003360#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361 norm_logfont = lf;
3362 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003363 if (!s_in_dpichanged)
3364 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365#endif
3366 gui_mch_free_font(gui.norm_font);
3367 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003368 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 GetFontSize(font);
3370
K.Takatac81e9bf2022-01-16 14:15:49 +00003371 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372 if (p != NULL)
3373 {
3374 hl_set_font_name(p);
3375
Bram Moolenaar734a8672019-12-02 22:49:38 +01003376 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3378 {
3379 vim_free(p_guifont);
3380 p_guifont = p;
3381 }
3382 else
3383 vim_free(p);
3384 }
3385
3386 gui_mch_free_font(gui.ital_font);
3387 gui.ital_font = NOFONT;
3388 gui_mch_free_font(gui.bold_font);
3389 gui.bold_font = NOFONT;
3390 gui_mch_free_font(gui.boldital_font);
3391 gui.boldital_font = NOFONT;
3392
3393 if (!lf.lfItalic)
3394 {
3395 lf.lfItalic = TRUE;
3396 gui.ital_font = get_font_handle(&lf);
3397 lf.lfItalic = FALSE;
3398 }
3399 if (lf.lfWeight < FW_BOLD)
3400 {
3401 lf.lfWeight = FW_BOLD;
3402 gui.bold_font = get_font_handle(&lf);
3403 if (!lf.lfItalic)
3404 {
3405 lf.lfItalic = TRUE;
3406 gui.boldital_font = get_font_handle(&lf);
3407 }
3408 }
3409
3410 return OK;
3411}
3412
3413#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003414# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415#endif
3416
3417/*
3418 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003419 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 */
3421 int
3422gui_mch_maximized(void)
3423{
3424 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003425 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426
3427 wp.length = sizeof(WINDOWPLACEMENT);
3428 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003429 {
3430 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003432 && wp.flags == WPF_RESTORETOMAXIMIZED))
3433 return TRUE;
3434 if (wp.showCmd == SW_SHOWMINIMIZED)
3435 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003437 // Assume the window is snapped when the sizes from two APIs differ.
3438 GetWindowRect(s_hwnd, &rc);
3439 if ((rc.right - rc.left !=
3440 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3441 || (rc.bottom - rc.top !=
3442 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3443 return TRUE;
3444 }
3445 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446}
3447
3448/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003449 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3450 * is set. Compute the new Rows and Columns. This is like resizing the
3451 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452 */
3453 void
3454gui_mch_newfont(void)
3455{
3456 RECT rect;
3457
3458 GetWindowRect(s_hwnd, &rect);
3459 if (win_socket_id == 0)
3460 {
3461 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003462 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3463 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003465 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3466 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3467 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3468 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469 }
3470 else
3471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003472 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003474 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 }
3476}
3477
3478/*
3479 * Set the window title
3480 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481 void
3482gui_mch_settitle(
3483 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003484 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485{
3486 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3487}
3488
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003489#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003490// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3491// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492static LPCSTR mshape_idcs[] =
3493{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003494 IDC_ARROW, // arrow
3495 MAKEINTRESOURCE(0), // blank
3496 IDC_IBEAM, // beam
3497 IDC_SIZENS, // updown
3498 IDC_SIZENS, // udsizing
3499 IDC_SIZEWE, // leftright
3500 IDC_SIZEWE, // lrsizing
3501 IDC_WAIT, // busy
3502 IDC_NO, // no
3503 IDC_ARROW, // crosshair
3504 IDC_ARROW, // hand1
3505 IDC_ARROW, // hand2
3506 IDC_ARROW, // pencil
3507 IDC_ARROW, // question
3508 IDC_ARROW, // right-arrow
3509 IDC_UPARROW, // up-arrow
3510 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511};
3512
3513 void
3514mch_set_mouse_shape(int shape)
3515{
3516 LPCSTR idc;
3517
3518 if (shape == MSHAPE_HIDE)
3519 ShowCursor(FALSE);
3520 else
3521 {
3522 if (shape >= MSHAPE_NUMBERED)
3523 idc = IDC_ARROW;
3524 else
3525 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003526 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 if (!p_mh)
3528 {
3529 POINT mp;
3530
Bram Moolenaar734a8672019-12-02 22:49:38 +01003531 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003532 (void)GetCursorPos((LPPOINT)&mp);
3533 (void)SetCursorPos(mp.x, mp.y);
3534 ShowCursor(TRUE);
3535 }
3536 }
3537}
3538#endif
3539
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003540#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542 * Wide version of convert_filter().
3543 */
3544 static WCHAR *
3545convert_filterW(char_u *s)
3546{
3547 char_u *tmp;
3548 int len;
3549 WCHAR *res;
3550
3551 tmp = convert_filter(s);
3552 if (tmp == NULL)
3553 return NULL;
3554 len = (int)STRLEN(s) + 3;
3555 res = enc_to_utf16(tmp, &len);
3556 vim_free(tmp);
3557 return res;
3558}
3559
3560/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003561 * Pop open a file browser and return the file selected, in allocated memory,
3562 * or NULL if Cancel is hit.
3563 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3564 * title - Title message for the file browser dialog.
3565 * dflt - Default name of file.
3566 * ext - Default extension to be added to files without extensions.
3567 * initdir - directory in which to open the browser (NULL = current dir)
3568 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003570 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003571gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572 int saving,
3573 char_u *title,
3574 char_u *dflt,
3575 char_u *ext,
3576 char_u *initdir,
3577 char_u *filter)
3578{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003579 // We always use the wide function. This means enc_to_utf16() must work,
3580 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581 OPENFILENAMEW fileStruct;
3582 WCHAR fileBuf[MAXPATHL];
3583 WCHAR *wp;
3584 int i;
3585 WCHAR *titlep = NULL;
3586 WCHAR *extp = NULL;
3587 WCHAR *initdirp = NULL;
3588 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003589 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003590
3591 if (dflt == NULL)
3592 fileBuf[0] = NUL;
3593 else
3594 {
3595 wp = enc_to_utf16(dflt, NULL);
3596 if (wp == NULL)
3597 fileBuf[0] = NUL;
3598 else
3599 {
3600 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3601 fileBuf[i] = wp[i];
3602 fileBuf[i] = NUL;
3603 vim_free(wp);
3604 }
3605 }
3606
Bram Moolenaar734a8672019-12-02 22:49:38 +01003607 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003608 filterp = convert_filterW(filter);
3609
Bram Moolenaara80faa82020-04-12 19:37:17 +02003610 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003611# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003612 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003613 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003614# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003616# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617
3618 if (title != NULL)
3619 titlep = enc_to_utf16(title, NULL);
3620 fileStruct.lpstrTitle = titlep;
3621
3622 if (ext != NULL)
3623 extp = enc_to_utf16(ext, NULL);
3624 fileStruct.lpstrDefExt = extp;
3625
3626 fileStruct.lpstrFile = fileBuf;
3627 fileStruct.nMaxFile = MAXPATHL;
3628 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003629 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3630 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631 if (initdir != NULL && *initdir != NUL)
3632 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003633 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003634 initdirp = enc_to_utf16(initdir, NULL);
3635 if (initdirp != NULL)
3636 {
3637 for (wp = initdirp; *wp != NUL; ++wp)
3638 if (*wp == '/')
3639 *wp = '\\';
3640 }
3641 fileStruct.lpstrInitialDir = initdirp;
3642 }
3643
3644 /*
3645 * TODO: Allow selection of multiple files. Needs another arg to this
3646 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3647 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3648 * files that don't exist yet, so I haven't put it in. What about
3649 * OFN_PATHMUSTEXIST?
3650 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3651 */
3652 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003653# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003654 if (curbuf->b_p_bin)
3655 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003656# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657 if (saving)
3658 {
3659 if (!GetSaveFileNameW(&fileStruct))
3660 return NULL;
3661 }
3662 else
3663 {
3664 if (!GetOpenFileNameW(&fileStruct))
3665 return NULL;
3666 }
3667
3668 vim_free(filterp);
3669 vim_free(initdirp);
3670 vim_free(titlep);
3671 vim_free(extp);
3672
Bram Moolenaar734a8672019-12-02 22:49:38 +01003673 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003675 if (p == NULL)
3676 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677
Bram Moolenaar734a8672019-12-02 22:49:38 +01003678 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679 SetFocus(s_hwnd);
3680
Bram Moolenaar734a8672019-12-02 22:49:38 +01003681 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003682 q = vim_strsave(shorten_fname1(p));
3683 vim_free(p);
3684 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003685}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686
3687
3688/*
3689 * Convert the string s to the proper format for a filter string by replacing
3690 * the \t and \n delimiters with \0.
3691 * Returns the converted string in allocated memory.
3692 *
3693 * Keep in sync with convert_filterW() above!
3694 */
3695 static char_u *
3696convert_filter(char_u *s)
3697{
3698 char_u *res;
3699 unsigned s_len = (unsigned)STRLEN(s);
3700 unsigned i;
3701
3702 res = alloc(s_len + 3);
3703 if (res != NULL)
3704 {
3705 for (i = 0; i < s_len; ++i)
3706 if (s[i] == '\t' || s[i] == '\n')
3707 res[i] = '\0';
3708 else
3709 res[i] = s[i];
3710 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003711 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712 res[s_len + 1] = NUL;
3713 res[s_len + 2] = NUL;
3714 }
3715 return res;
3716}
3717
3718/*
3719 * Select a directory.
3720 */
3721 char_u *
3722gui_mch_browsedir(char_u *title, char_u *initdir)
3723{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 // We fake this: Use a filter that doesn't select anything and a default
3725 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3727 initdir, (char_u *)_("Directory\t*.nothing\n"));
3728}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003729#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003730
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003731 static void
3732_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003733 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003734 HDROP hDrop)
3735{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003736#define BUFPATHLEN _MAX_PATH
3737#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003739 char szFile[BUFPATHLEN];
3740 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3741 UINT i;
3742 char_u **fnames;
3743 POINT pt;
3744 int_u modifiers = 0;
3745
Bram Moolenaar734a8672019-12-02 22:49:38 +01003746 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003747
Bram Moolenaar734a8672019-12-02 22:49:38 +01003748 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003749 DragQueryPoint(hDrop, &pt);
3750 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3751
3752 reset_VIsual();
3753
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003754 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755
3756 if (fnames != NULL)
3757 for (i = 0; i < cFiles; ++i)
3758 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3760 fnames[i] = utf16_to_enc(wszFile, NULL);
3761 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762 {
3763 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3764 fnames[i] = vim_strsave((char_u *)szFile);
3765 }
3766 }
3767
3768 DragFinish(hDrop);
3769
3770 if (fnames != NULL)
3771 {
3772 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3773 modifiers |= MOUSE_SHIFT;
3774 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3775 modifiers |= MOUSE_CTRL;
3776 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3777 modifiers |= MOUSE_ALT;
3778
3779 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3780
3781 s_need_activate = TRUE;
3782 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783}
3784
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785 static int
3786_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003787 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003788 HWND hwndCtl,
3789 UINT code,
3790 int pos)
3791{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003792 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793 scrollbar_T *sb, *sb_info;
3794 long val;
3795 int dragging = FALSE;
3796 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797 SCROLLINFO si;
3798
3799 si.cbSize = sizeof(si);
3800 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801
3802 sb = gui_mswin_find_scrollbar(hwndCtl);
3803 if (sb == NULL)
3804 return 0;
3805
Bram Moolenaar734a8672019-12-02 22:49:38 +01003806 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807 {
3808 /*
3809 * Careful: need to get scrollbar info out of first (left) scrollbar
3810 * for window, but keep real scrollbar too because we must pass it to
3811 * gui_drag_scrollbar().
3812 */
3813 sb_info = &sb->wp->w_scrollbars[0];
3814 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003815 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003816 sb_info = sb;
3817 val = sb_info->value;
3818
3819 switch (code)
3820 {
3821 case SB_THUMBTRACK:
3822 val = pos;
3823 dragging = TRUE;
3824 if (sb->scroll_shift > 0)
3825 val <<= sb->scroll_shift;
3826 break;
3827 case SB_LINEDOWN:
3828 val++;
3829 break;
3830 case SB_LINEUP:
3831 val--;
3832 break;
3833 case SB_PAGEDOWN:
3834 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3835 break;
3836 case SB_PAGEUP:
3837 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3838 break;
3839 case SB_TOP:
3840 val = 0;
3841 break;
3842 case SB_BOTTOM:
3843 val = sb_info->max;
3844 break;
3845 case SB_ENDSCROLL:
3846 if (prev_code == SB_THUMBTRACK)
3847 {
3848 /*
3849 * "pos" only gives us 16-bit data. In case of large file,
3850 * use GetScrollPos() which returns 32-bit. Unfortunately it
3851 * is not valid while the scrollbar is being dragged.
3852 */
3853 val = GetScrollPos(hwndCtl, SB_CTL);
3854 if (sb->scroll_shift > 0)
3855 val <<= sb->scroll_shift;
3856 }
3857 break;
3858
3859 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003860 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 return 0;
3862 }
3863 prev_code = code;
3864
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003865 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3866 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003867
3868 /*
3869 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3870 */
3871 if (sb->wp != NULL)
3872 {
3873 scrollbar_T *sba = sb->wp->w_scrollbars;
3874 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3875
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003876 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003877 }
3878
Bram Moolenaar734a8672019-12-02 22:49:38 +01003879 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003880 s_busy_processing = TRUE;
3881
Bram Moolenaar734a8672019-12-02 22:49:38 +01003882 // When "allow_scrollbar" is FALSE still need to remember the new
3883 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003884 dont_scroll = !allow_scrollbar;
3885
Bram Moolenaara338adc2018-01-31 20:51:47 +01003886 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003887 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003888 mch_enable_flush();
3889 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890
3891 s_busy_processing = FALSE;
3892 dont_scroll = dont_scroll_save;
3893
3894 return 0;
3895}
3896
3897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898#ifdef FEAT_XPM_W32
3899# include "xpm_w32.h"
3900#endif
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#ifdef __MINGW32__
3903/*
3904 * Add a lot of missing defines.
3905 * They are not always missing, we need the #ifndef's.
3906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907# ifndef IsMinimized
3908# define IsMinimized(hwnd) IsIconic(hwnd)
3909# endif
3910# ifndef IsMaximized
3911# define IsMaximized(hwnd) IsZoomed(hwnd)
3912# endif
3913# ifndef SelectFont
3914# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3915# endif
3916# ifndef GetStockBrush
3917# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3918# endif
3919# ifndef DeleteBrush
3920# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3921# endif
3922
3923# ifndef HANDLE_WM_RBUTTONDBLCLK
3924# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3925 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3926# endif
3927# ifndef HANDLE_WM_MBUTTONUP
3928# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3929 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3930# endif
3931# ifndef HANDLE_WM_MBUTTONDBLCLK
3932# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3933 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3934# endif
3935# ifndef HANDLE_WM_LBUTTONDBLCLK
3936# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3937 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3938# endif
3939# ifndef HANDLE_WM_RBUTTONDOWN
3940# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3941 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3942# endif
3943# ifndef HANDLE_WM_MOUSEMOVE
3944# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3945 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3946# endif
3947# ifndef HANDLE_WM_RBUTTONUP
3948# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3949 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3950# endif
3951# ifndef HANDLE_WM_MBUTTONDOWN
3952# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3953 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3954# endif
3955# ifndef HANDLE_WM_LBUTTONUP
3956# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3957 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3958# endif
3959# ifndef HANDLE_WM_LBUTTONDOWN
3960# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3961 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3962# endif
3963# ifndef HANDLE_WM_SYSCHAR
3964# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3965 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3966# endif
3967# ifndef HANDLE_WM_ACTIVATEAPP
3968# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3969 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3970# endif
3971# ifndef HANDLE_WM_WINDOWPOSCHANGING
3972# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3973 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3974# endif
3975# ifndef HANDLE_WM_VSCROLL
3976# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3977 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3978# endif
3979# ifndef HANDLE_WM_SETFOCUS
3980# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3981 ((fn)((hwnd), (HWND)(wParam)), 0L)
3982# endif
3983# ifndef HANDLE_WM_KILLFOCUS
3984# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3985 ((fn)((hwnd), (HWND)(wParam)), 0L)
3986# endif
3987# ifndef HANDLE_WM_HSCROLL
3988# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3989 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3990# endif
3991# ifndef HANDLE_WM_DROPFILES
3992# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3993 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3994# endif
3995# ifndef HANDLE_WM_CHAR
3996# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3997 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3998# endif
3999# ifndef HANDLE_WM_SYSDEADCHAR
4000# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4001 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4002# endif
4003# ifndef HANDLE_WM_DEADCHAR
4004# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4005 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4006# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004007#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009
Bram Moolenaar734a8672019-12-02 22:49:38 +01004010// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#define TEAROFF_PADDING_X 2
4012#define TEAROFF_BUTTON_PAD_X 8
4013#define TEAROFF_MIN_WIDTH 200
4014#define TEAROFF_SUBMENU_LABEL ">>"
4015#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4016
4017
Bram Moolenaar734a8672019-12-02 22:49:38 +01004018// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004020# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#endif
4022
4023
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004024#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025# define ID_BEVAL_TOOLTIP 200
4026# define BEVAL_TEXT_LEN MAXPATHL
4027
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004028# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004029// Work around old versions of basetsd.h which wrongly declares
4030// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004031# undef UINT_PTR
4032# define UINT_PTR UINT
4033# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004036static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004038
Bram Moolenaar82881492012-11-20 16:53:39 +01004039
Bram Moolenaar734a8672019-12-02 22:49:38 +01004040// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004041# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004042
Bram Moolenaar45360022005-07-21 21:08:21 +00004043/*
4044 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004045 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004046 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004047# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004048
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004049# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004050
4051typedef struct _DllVersionInfo
4052{
4053 DWORD cbSize;
4054 DWORD dwMajorVersion;
4055 DWORD dwMinorVersion;
4056 DWORD dwBuildNumber;
4057 DWORD dwPlatformID;
4058} DLLVERSIONINFO;
4059
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004060# ifndef PROTO
4061# include <poppack.h>
4062# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004063
Bram Moolenaar45360022005-07-21 21:08:21 +00004064typedef struct tagTOOLINFOA_NEW
4065{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004066 UINT cbSize;
4067 UINT uFlags;
4068 HWND hwnd;
4069 UINT_PTR uId;
4070 RECT rect;
4071 HINSTANCE hinst;
4072 LPSTR lpszText;
4073 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004074} TOOLINFO_NEW;
4075
4076typedef struct tagNMTTDISPINFO_NEW
4077{
4078 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004079 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004080 char szText[80];
4081 HINSTANCE hinst;
4082 UINT uFlags;
4083 LPARAM lParam;
4084} NMTTDISPINFO_NEW;
4085
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004086typedef struct tagTOOLINFOW_NEW
4087{
4088 UINT cbSize;
4089 UINT uFlags;
4090 HWND hwnd;
4091 UINT_PTR uId;
4092 RECT rect;
4093 HINSTANCE hinst;
4094 LPWSTR lpszText;
4095 LPARAM lParam;
4096 void *lpReserved;
4097} TOOLINFOW_NEW;
4098
4099typedef struct tagNMTTDISPINFOW_NEW
4100{
4101 NMHDR hdr;
4102 LPWSTR lpszText;
4103 WCHAR szText[80];
4104 HINSTANCE hinst;
4105 UINT uFlags;
4106 LPARAM lParam;
4107} NMTTDISPINFOW_NEW;
4108
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004109
Bram Moolenaar45360022005-07-21 21:08:21 +00004110typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004111# ifndef TTM_SETMAXTIPWIDTH
4112# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004115# ifndef TTF_DI_SETITEM
4116# define TTF_DI_SETITEM 0x8000
4117# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004118
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004119# ifndef TTN_GETDISPINFO
4120# define TTN_GETDISPINFO (TTN_FIRST - 0)
4121# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004122
Bram Moolenaar734a8672019-12-02 22:49:38 +01004123#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004124
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004125#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004126// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4127// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004128// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004129// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004130# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4131typedef struct tagNMTTDISPINFOA {
4132 NMHDR hdr;
4133 LPSTR lpszText;
4134 char szText[80];
4135 HINSTANCE hinst;
4136 UINT uFlags;
4137 LPARAM lParam;
4138} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4139# define LPNMTTDISPINFO LPNMTTDISPINFOA
4140
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004141typedef struct tagNMTTDISPINFOW {
4142 NMHDR hdr;
4143 LPWSTR lpszText;
4144 WCHAR szText[80];
4145 HINSTANCE hinst;
4146 UINT uFlags;
4147 LPARAM lParam;
4148} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004149# endif
4150#endif
4151
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004152#ifndef TTN_GETDISPINFOW
4153# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4154#endif
4155
Bram Moolenaar734a8672019-12-02 22:49:38 +01004156// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158#ifdef FEAT_MENU
4159static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162/*
4163 * Use the system font for dialogs and tear-off menus. Remove this line to
4164 * use DLG_FONT_NAME.
4165 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004166#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169#define VIM_CLASSW L"Vim"
4170
Bram Moolenaar734a8672019-12-02 22:49:38 +01004171// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4172// thus there should be room for every dialog. For tearoffs it's made bigger
4173// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#define DLG_ALLOC_SIZE 16 * 1024
4175
4176/*
4177 * stuff for dialogs, menus, tearoffs etc.
4178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179static PWORD
4180add_dialog_element(
4181 PWORD p,
4182 DWORD lStyle,
4183 WORD x,
4184 WORD y,
4185 WORD w,
4186 WORD h,
4187 WORD Id,
4188 WORD clss,
4189 const char *caption);
4190static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004191static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004192#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195static void get_dialog_font_metrics(void);
4196
4197static int dialog_default_button = -1;
4198
Bram Moolenaar734a8672019-12-02 22:49:38 +01004199// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaar734a8672019-12-02 22:49:38 +01004202static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203#ifdef FEAT_TOOLBAR
4204static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004205static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004206static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004208#else
4209# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210#endif
4211
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004212#ifdef FEAT_GUI_TABLINE
4213static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004214static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004215#endif
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#ifdef FEAT_MBYTE_IME
4218static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4219static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4220#endif
4221#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4222# ifdef NOIME
4223typedef struct tagCOMPOSITIONFORM {
4224 DWORD dwStyle;
4225 POINT ptCurrentPos;
4226 RECT rcArea;
4227} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4228typedef HANDLE HIMC;
4229# endif
4230
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004231static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004232static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4233static HIMC (WINAPI *pImmGetContext)(HWND);
4234static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4235static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4236static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4237static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004238static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4239static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004240static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4241static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004242static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243static void dyn_imm_load(void);
4244#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245# define pImmGetCompositionStringW ImmGetCompositionStringW
4246# define pImmGetContext ImmGetContext
4247# define pImmAssociateContext ImmAssociateContext
4248# define pImmReleaseContext ImmReleaseContext
4249# define pImmGetOpenStatus ImmGetOpenStatus
4250# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004251# define pImmGetCompositionFontW ImmGetCompositionFontW
4252# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253# define pImmSetCompositionWindow ImmSetCompositionWindow
4254# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004255# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#ifdef FEAT_MENU
4259/*
4260 * Figure out how high the menu bar is at the moment.
4261 */
4262 static int
4263gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004264 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265{
4266 static int old_menu_height = -1;
4267
4268 RECT rc1, rc2;
4269 int num;
4270 int menu_height;
4271
4272 if (gui.menu_is_active)
4273 num = GetMenuItemCount(s_menuBar);
4274 else
4275 num = 0;
4276
4277 if (num == 0)
4278 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004279 else if (IsMinimized(s_hwnd))
4280 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004281 // The height of the menu cannot be determined while the window is
4282 // minimized. Take the previous height if the menu is changed in that
4283 // state, to avoid that Vim's vertical window size accidentally
4284 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004285 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 else
4288 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004289 /*
4290 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4291 * seem to have been set yet, so menu wraps in default window
4292 * width which is very narrow. Instead just return height of a
4293 * single menu item. Will still be wrong when the menu really
4294 * should wrap over more than one line.
4295 */
4296 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4297 if (gui.starting)
4298 menu_height = rc1.bottom - rc1.top + 1;
4299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004301 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4302 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 }
4305
4306 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004307 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004308 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 return menu_height;
4311}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004312#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314
4315/*
4316 * Setup for the Intellimouse
4317 */
4318 static void
4319init_mouse_wheel(void)
4320{
4321
4322#ifndef SPI_GETWHEELSCROLLLINES
4323# define SPI_GETWHEELSCROLLLINES 104
4324#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004325#ifndef SPI_SETWHEELSCROLLLINES
4326# define SPI_SETWHEELSCROLLLINES 105
4327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
Bram Moolenaar734a8672019-12-02 22:49:38 +01004329#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4330#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4332#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4333
Bram Moolenaar734a8672019-12-02 22:49:38 +01004334 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
Bram Moolenaar734a8672019-12-02 22:49:38 +01004336 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004337 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4338 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339}
4340
4341
Bram Moolenaarae20f342019-11-05 21:09:23 +01004342/*
4343 * Intellimouse wheel handler.
4344 * Treat a mouse wheel event as if it were a scroll request.
4345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 static void
4347_OnMouseWheel(
4348 HWND hwnd,
4349 short zDelta)
4350{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 int i;
4352 int size;
4353 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004354 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (mouse_scroll_lines == 0)
4357 init_mouse_wheel();
4358
Bram Moolenaarae20f342019-11-05 21:09:23 +01004359 wp = gui_mouse_window(FIND_POPUP);
4360
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004361#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004362 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004363 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004364 cmdarg_T cap;
4365 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004366
Bram Moolenaarae20f342019-11-05 21:09:23 +01004367 // Mouse hovers over popup window, scroll it if possible.
4368 mouse_row = wp->w_winrow;
4369 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004370 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004371 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4372 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4373 clear_oparg(&oa);
4374 cap.oap = &oa;
4375 nv_mousescroll(&cap);
4376 update_screen(0);
4377 setcursor();
4378 out_flush();
4379 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004380 }
4381#endif
4382
Bram Moolenaarae20f342019-11-05 21:09:23 +01004383 if (wp == NULL || !p_scf)
4384 wp = curwin;
4385
4386 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4387 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4388 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4389 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4390 else
4391 return;
4392 size = wp->w_height;
4393
Bram Moolenaara338adc2018-01-31 20:51:47 +01004394 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (mouse_scroll_lines > 0
4396 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4397 {
4398 for (i = mouse_scroll_lines; i > 0; --i)
4399 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4400 }
4401 else
4402 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004403 mch_enable_flush();
4404 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405}
4406
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004407#ifdef USE_SYSMENU_FONT
4408/*
4409 * Get Menu Font.
4410 * Return OK or FAIL.
4411 */
4412 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004413gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004414{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004415 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004416
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004417 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4418 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004419 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004420 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004421 &nm,
4422 0))
4423 return FAIL;
4424 *lf = nm.lfMenuFont;
4425 return OK;
4426}
4427#endif
4428
4429
4430#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4431/*
4432 * Set the GUI tabline font to the system menu font
4433 */
4434 static void
4435set_tabline_font(void)
4436{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004437 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004438 HFONT font;
4439 HWND hwnd;
4440 HDC hdc;
4441 HFONT hfntOld;
4442 TEXTMETRIC tm;
4443
4444 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4445 return;
4446
K.Takatac81e9bf2022-01-16 14:15:49 +00004447 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004448 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004449
4450 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4451
4452 /*
4453 * Compute the height of the font used for the tab text
4454 */
4455 hwnd = GetDesktopWindow();
4456 hdc = GetWindowDC(hwnd);
4457 hfntOld = SelectFont(hdc, font);
4458
4459 GetTextMetrics(hdc, &tm);
4460
4461 SelectFont(hdc, hfntOld);
4462 ReleaseDC(hwnd, hdc);
4463
4464 /*
4465 * The space used by the tab border and the space between the tab label
4466 * and the tab border is included as 7.
4467 */
4468 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4469}
K.Takatac81e9bf2022-01-16 14:15:49 +00004470#else
4471# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004472#endif
4473
Bram Moolenaar520470a2005-06-16 21:59:56 +00004474/*
4475 * Invoked when a setting was changed.
4476 */
4477 static LRESULT CALLBACK
4478_OnSettingChange(UINT n)
4479{
4480 if (n == SPI_SETWHEELSCROLLLINES)
4481 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4482 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004483 if (n == SPI_SETNONCLIENTMETRICS)
4484 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004485 return 0;
4486}
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488#ifdef FEAT_NETBEANS_INTG
4489 static void
4490_OnWindowPosChanged(
4491 HWND hwnd,
4492 const LPWINDOWPOS lpwpos)
4493{
4494 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004495 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4498 || lpwpos->cx != cx || lpwpos->cy != cy))
4499 {
4500 x = lpwpos->x;
4501 y = lpwpos->y;
4502 cx = lpwpos->cx;
4503 cy = lpwpos->cy;
4504 netbeans_frame_moved(x, y);
4505 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004506 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004507 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508}
4509#endif
4510
K.Takata4f2417f2021-06-05 16:25:32 +02004511
4512static HWND hwndTip = NULL;
4513
4514 static void
4515show_sizing_tip(int cols, int rows)
4516{
4517 TOOLINFOA ti = {sizeof(ti)};
4518 char buf[32];
4519
4520 ti.hwnd = s_hwnd;
4521 ti.uId = (UINT_PTR)s_hwnd;
4522 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4523 ti.lpszText = buf;
4524 sprintf(buf, "%dx%d", cols, rows);
4525 if (hwndTip == NULL)
4526 {
4527 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4528 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4529 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4530 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4531 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4532 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4533 }
4534 else
4535 {
4536 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4537 }
4538 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4539}
4540
4541 static void
4542destroy_sizing_tip(void)
4543{
4544 if (hwndTip != NULL)
4545 {
4546 DestroyWindow(hwndTip);
4547 hwndTip = NULL;
4548 }
4549}
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 static int
4552_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 UINT fwSide,
4554 LPRECT lprc)
4555{
4556 int w, h;
4557 int valid_w, valid_h;
4558 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004559 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 w = lprc->right - lprc->left;
4562 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004563 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 w_offset = w - valid_w;
4565 h_offset = h - valid_h;
4566
4567 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4568 || fwSide == WMSZ_BOTTOMLEFT)
4569 lprc->left += w_offset;
4570 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4571 || fwSide == WMSZ_BOTTOMRIGHT)
4572 lprc->right -= w_offset;
4573
4574 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4575 || fwSide == WMSZ_TOPRIGHT)
4576 lprc->top += h_offset;
4577 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4578 || fwSide == WMSZ_BOTTOMRIGHT)
4579 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004580
4581 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 return TRUE;
4583}
4584
K.Takatac81e9bf2022-01-16 14:15:49 +00004585 static LRESULT
4586_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4587{
4588 s_dpi = ydpi;
4589 s_in_dpichanged = TRUE;
4590 //TRACE("DPI: %d", ydpi);
4591
4592 update_scrollbar_size();
4593 update_toolbar_size();
4594 set_tabline_font();
4595
4596 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4597 gui_get_wide_font();
4598 gui_mswin_get_menu_height(FALSE);
4599#ifdef FEAT_MBYTE_IME
4600 im_set_position(gui.row, gui.col);
4601#endif
4602 InvalidateRect(hwnd, NULL, TRUE);
4603
4604 s_in_dpichanged = FALSE;
4605 return 0L;
4606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608
4609 static LRESULT CALLBACK
4610_WndProc(
4611 HWND hwnd,
4612 UINT uMsg,
4613 WPARAM wParam,
4614 LPARAM lParam)
4615{
4616 /*
4617 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4618 hwnd, uMsg, wParam, lParam);
4619 */
4620
4621 HandleMouseHide(uMsg, lParam);
4622
4623 s_uMsg = uMsg;
4624 s_wParam = wParam;
4625 s_lParam = lParam;
4626
4627 switch (uMsg)
4628 {
4629 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4630 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004631 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004633 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4635 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4636 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4637 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4638#ifdef FEAT_MENU
4639 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4640#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004641 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4642 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4644 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004645 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4646 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4648 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4649 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4650#ifdef FEAT_NETBEANS_INTG
4651 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4652#endif
4653
Bram Moolenaarafa24992006-03-27 20:58:26 +00004654#ifdef FEAT_GUI_TABLINE
4655 case WM_RBUTTONUP:
4656 {
4657 if (gui_mch_showing_tabline())
4658 {
4659 POINT pt;
4660 RECT rect;
4661
4662 /*
4663 * If the cursor is on the tabline, display the tab menu
4664 */
4665 GetCursorPos((LPPOINT)&pt);
4666 GetWindowRect(s_textArea, &rect);
4667 if (pt.y < rect.top)
4668 {
4669 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004670 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004671 }
4672 }
K.Takata4ac893f2022-01-20 12:44:28 +00004673 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004674 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004675 case WM_LBUTTONDBLCLK:
4676 {
4677 /*
4678 * If the user double clicked the tabline, create a new tab
4679 */
4680 if (gui_mch_showing_tabline())
4681 {
4682 POINT pt;
4683 RECT rect;
4684
4685 GetCursorPos((LPPOINT)&pt);
4686 GetWindowRect(s_textArea, &rect);
4687 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004688 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004689 }
K.Takata4ac893f2022-01-20 12:44:28 +00004690 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004691 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004692#endif
4693
Bram Moolenaar734a8672019-12-02 22:49:38 +01004694 case WM_QUERYENDSESSION: // System wants to go down.
4695 gui_shell_closed(); // Will exit when no changed buffers.
4696 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004699 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004702 return 0L;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 break;
4705
4706 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004707 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4708 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004709 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 return 0L;
4711
4712 case WM_SYSCHAR:
4713 /*
4714 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4715 * shortcut key, handle like a typed ALT key, otherwise call Windows
4716 * ALT key handling.
4717 */
4718#ifdef FEAT_MENU
4719 if ( !gui.menu_is_active
4720 || p_wak[0] == 'n'
4721 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4722 )
4723#endif
4724 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004725 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return 0L;
4727 }
4728#ifdef FEAT_MENU
4729 else
K.Takata4ac893f2022-01-20 12:44:28 +00004730 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731#endif
4732
4733 case WM_SYSKEYUP:
4734#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004735 // This used to be done only when menu is active: ALT key is used for
4736 // that. But that caused problems when menu is disabled and using
4737 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4738 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004739 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004741 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742#endif
4743
K.Takata4f2417f2021-06-05 16:25:32 +02004744 case WM_EXITSIZEMOVE:
4745 destroy_sizing_tip();
4746 break;
4747
Bram Moolenaar734a8672019-12-02 22:49:38 +01004748 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 case WM_MOUSEWHEEL:
4752 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004753 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar734a8672019-12-02 22:49:38 +01004755 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004756 case WM_SETTINGCHANGE:
4757 return _OnSettingChange((UINT)wParam);
4758
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004759#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 case WM_NOTIFY:
4761 switch (((LPNMHDR) lParam)->code)
4762 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004763 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004764 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004766 LPNMHDR hdr = (LPNMHDR)lParam;
4767 char_u *str = NULL;
4768 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004769
Bram Moolenaard23a8232018-02-10 18:45:26 +01004770 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004771
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004772# ifdef FEAT_GUI_TABLINE
4773 if (gui_mch_showing_tabline()
4774 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004775 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004776 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004777 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004778 * Mouse is over the GUI tabline. Display the
4779 * tooltip for the tab under the cursor
4780 *
4781 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004782 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004783 GetCursorPos(&pt);
4784 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004785 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004786 TCHITTESTINFO htinfo;
4787 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004788
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004789 /*
4790 * Get the tab under the cursor
4791 */
4792 htinfo.pt.x = pt.x;
4793 htinfo.pt.y = pt.y;
4794 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4795 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004796 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004797 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004798
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004799 tp = find_tabpage(idx + 1);
4800 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004801 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004802 get_tabline_label(tp, TRUE);
4803 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004804 }
4805 }
4806 }
4807 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004808# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004809# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004810# ifdef FEAT_GUI_TABLINE
4811 else
4812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004814 UINT idButton;
4815 vimmenu_T *pMenu;
4816
4817 idButton = (UINT) hdr->idFrom;
4818 pMenu = gui_mswin_find_menu(root_menu, idButton);
4819 if (pMenu)
4820 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004822# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004823 if (str != NULL)
4824 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004825 if (hdr->code == TTN_GETDISPINFOW)
4826 {
4827 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4828
Bram Moolenaar734a8672019-12-02 22:49:38 +01004829 // Set the maximum width, this also enables using
4830 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004831 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4832 0, 500);
4833
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004834 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004835 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004836 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004837 }
4838 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004839 {
4840 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4841
Bram Moolenaar734a8672019-12-02 22:49:38 +01004842 // Set the maximum width, this also enables using
4843 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004844 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4845 0, 500);
4846
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004847 if (STRLEN(str) < sizeof(lpdi->szText)
4848 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004849 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004850 sizeof(lpdi->szText) - 1);
4851 else
4852 lpdi->lpszText = tt_text;
4853 }
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004857# ifdef FEAT_GUI_TABLINE
4858 case TCN_SELCHANGE:
4859 if (gui_mch_showing_tabline()
4860 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004861 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004862 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004863 return 0L;
4864 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004865 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004866
4867 case NM_RCLICK:
4868 if (gui_mch_showing_tabline()
4869 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004870 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004871 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004872 return 0L;
4873 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004874 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004877# ifdef FEAT_GUI_TABLINE
4878 if (gui_mch_showing_tabline()
4879 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
K.Takata4ac893f2022-01-20 12:44:28 +00004880 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 break;
4883 }
4884 break;
4885#endif
4886#if defined(MENUHINTS) && defined(FEAT_MENU)
4887 case WM_MENUSELECT:
4888 if (((UINT) HIWORD(wParam)
4889 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4890 == MF_HILITE
4891 && (State & CMDLINE) == 0)
4892 {
4893 UINT idButton;
4894 vimmenu_T *pMenu;
4895 static int did_menu_tip = FALSE;
4896
4897 if (did_menu_tip)
4898 {
4899 msg_clr_cmdline();
4900 setcursor();
4901 out_flush();
4902 did_menu_tip = FALSE;
4903 }
4904
4905 idButton = (UINT)LOWORD(wParam);
4906 pMenu = gui_mswin_find_menu(root_menu, idButton);
4907 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4908 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4909 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004910 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004911 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004912 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 setcursor();
4914 out_flush();
4915 did_menu_tip = TRUE;
4916 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004917 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 break;
4920#endif
4921 case WM_NCHITTEST:
4922 {
4923 LRESULT result;
4924 int x, y;
4925 int xPos = GET_X_LPARAM(lParam);
4926
K.Takata4ac893f2022-01-20 12:44:28 +00004927 result = DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (result == HTCLIENT)
4929 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004930#ifdef FEAT_GUI_TABLINE
4931 if (gui_mch_showing_tabline())
4932 {
4933 int yPos = GET_Y_LPARAM(lParam);
4934 RECT rct;
4935
Bram Moolenaar734a8672019-12-02 22:49:38 +01004936 // If the cursor is on the GUI tabline, don't process this
4937 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004938 GetWindowRect(s_textArea, &rct);
4939 if (yPos < rct.top)
4940 return result;
4941 }
4942#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004943 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 xPos -= x;
4945
Bram Moolenaar734a8672019-12-02 22:49:38 +01004946 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 return HTBOTTOMLEFT;
4948 else
4949 return HTBOTTOMRIGHT;
4950 }
4951 else
4952 return result;
4953 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004954 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956#ifdef FEAT_MBYTE_IME
4957 case WM_IME_NOTIFY:
4958 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004959 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004960 return 1L;
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 case WM_IME_COMPOSITION:
4963 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004964 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004965 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004967 case WM_DPICHANGED:
4968 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4969 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004973 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975#endif
K.Takata4ac893f2022-01-20 12:44:28 +00004976 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978
K.Takata4ac893f2022-01-20 12:44:28 +00004979 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980}
4981
4982/*
4983 * End of call-back routines
4984 */
4985
Bram Moolenaar734a8672019-12-02 22:49:38 +01004986// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987HWND vim_parent_hwnd = NULL;
4988
4989 static BOOL CALLBACK
4990FindWindowTitle(HWND hwnd, LPARAM lParam)
4991{
4992 char buf[2048];
4993 char *title = (char *)lParam;
4994
4995 if (GetWindowText(hwnd, buf, sizeof(buf)))
4996 {
4997 if (strstr(buf, title) != NULL)
4998 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004999 // Found it. Store the window ref. and quit searching if MDI
5000 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005002 if (vim_parent_hwnd != NULL)
5003 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005006 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007}
5008
5009/*
5010 * Invoked for '-P "title"' argument: search for parent application to open
5011 * our window in.
5012 */
5013 void
5014gui_mch_set_parent(char *title)
5015{
5016 EnumWindows(FindWindowTitle, (LPARAM)title);
5017 if (vim_parent_hwnd == NULL)
5018 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005019 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 mch_exit(2);
5021 }
5022}
5023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005024#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 static void
5026ole_error(char *arg)
5027{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005028 char buf[IOSIZE];
5029
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005030# ifdef VIMDLL
5031 gui.in_use = mch_is_gui_executable();
5032# endif
5033
Bram Moolenaar734a8672019-12-02 22:49:38 +01005034 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005035 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005036 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005037 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005041#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5042 static char *
5043gvim_error(void)
5044{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005045 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005046
5047 if (starting)
5048 {
5049 mch_errmsg(msg);
5050 mch_errmsg("\n");
5051 mch_exit(2);
5052 }
5053 return msg;
5054}
5055
5056 char *
5057gui_mch_do_spawn(char_u *arg)
5058{
5059 int len;
5060# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5061 char_u *session = NULL;
5062 LPWSTR tofree1 = NULL;
5063# endif
5064 WCHAR name[MAX_PATH];
5065 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5066 STARTUPINFOW si = {sizeof(si)};
5067 PROCESS_INFORMATION pi;
5068
5069 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5070 goto error;
5071 p = wcsrchr(name, L'\\');
5072 if (p == NULL)
5073 goto error;
5074 // Replace the executable name from vim(d).exe to gvim(d).exe.
5075# ifdef DEBUG
5076 wcscpy(p + 1, L"gvimd.exe");
5077# else
5078 wcscpy(p + 1, L"gvim.exe");
5079# endif
5080
5081# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5082 if (starting)
5083# endif
5084 {
5085 // Pass the command line to the new process.
5086 p = GetCommandLineW();
5087 // Skip 1st argument.
5088 while (*p && *p != L' ' && *p != L'\t')
5089 {
5090 if (*p == L'"')
5091 {
5092 while (*p && *p != L'"')
5093 ++p;
5094 if (*p)
5095 ++p;
5096 }
5097 else
5098 ++p;
5099 }
5100 cmd = p;
5101 }
5102# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5103 else
5104 {
5105 // Create a session file and pass it to the new process.
5106 LPWSTR wsession;
5107 char_u *savebg;
5108 int ret;
5109
5110 session = vim_tempname('s', FALSE);
5111 if (session == NULL)
5112 goto error;
5113 savebg = p_bg;
5114 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5115 ret = write_session_file(session);
5116 vim_free(p_bg);
5117 p_bg = savebg;
5118 if (!ret)
5119 goto error;
5120 wsession = enc_to_utf16(session, NULL);
5121 if (wsession == NULL)
5122 goto error;
5123 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005124 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005125 if (cmd == NULL)
5126 {
5127 vim_free(wsession);
5128 goto error;
5129 }
5130 tofree1 = cmd;
5131 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5132 wsession, wsession);
5133 vim_free(wsession);
5134 }
5135# endif
5136
5137 // Check additional arguments to the `:gui` command.
5138 if (arg != NULL)
5139 {
5140 warg = enc_to_utf16(arg, NULL);
5141 if (warg == NULL)
5142 goto error;
5143 tofree2 = warg;
5144 }
5145 else
5146 warg = L"";
5147
5148 // Set up the new command line.
5149 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005150 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005151 if (newcmd == NULL)
5152 goto error;
5153 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5154
5155 // Spawn a new GUI process.
5156 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5157 NULL, NULL, &si, &pi))
5158 goto error;
5159 CloseHandle(pi.hProcess);
5160 CloseHandle(pi.hThread);
5161 mch_exit(0);
5162
5163error:
5164# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5165 if (session)
5166 mch_remove(session);
5167 vim_free(session);
5168 vim_free(tofree1);
5169# endif
5170 vim_free(newcmd);
5171 vim_free(tofree2);
5172 return gvim_error();
5173}
5174#endif
5175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176/*
5177 * Parse the GUI related command-line arguments. Any arguments used are
5178 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005179 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 */
5181 void
5182gui_mch_prepare(int *argc, char **argv)
5183{
5184 int silent = FALSE;
5185 int idx;
5186
Bram Moolenaar734a8672019-12-02 22:49:38 +01005187 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5189 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005190 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5192 && (argv[2][0] == '-' || argv[2][0] == '/'))
5193 {
5194 silent = TRUE;
5195 idx = 2;
5196 }
5197 else
5198 idx = 1;
5199
Bram Moolenaar734a8672019-12-02 22:49:38 +01005200 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (STRICMP(argv[idx] + 1, "register") == 0)
5202 {
5203#ifdef FEAT_OLE
5204 RegisterMe(silent);
5205 mch_exit(0);
5206#else
5207 if (!silent)
5208 ole_error("register");
5209 mch_exit(2);
5210#endif
5211 }
5212
Bram Moolenaar734a8672019-12-02 22:49:38 +01005213 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5215 {
5216#ifdef FEAT_OLE
5217 UnregisterMe(!silent);
5218 mch_exit(0);
5219#else
5220 if (!silent)
5221 ole_error("unregister");
5222 mch_exit(2);
5223#endif
5224 }
5225
Bram Moolenaar734a8672019-12-02 22:49:38 +01005226 // Ignore an -embedding argument. It is only relevant if the
5227 // application wants to treat the case when it is started manually
5228 // differently from the case where it is started via automation (and
5229 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5231 {
5232#ifdef FEAT_OLE
5233 *argc = 1;
5234#else
5235 ole_error("embedding");
5236 mch_exit(2);
5237#endif
5238 }
5239 }
5240
5241#ifdef FEAT_OLE
5242 {
5243 int bDoRestart = FALSE;
5244
5245 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005246 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (bDoRestart)
5248 mch_exit(0);
5249 }
5250#endif
5251
5252#ifdef FEAT_NETBEANS_INTG
5253 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005254 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 int arg;
5256
5257 for (arg = 1; arg < *argc; arg++)
5258 if (strncmp("-nb", argv[arg], 3) == 0)
5259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 netbeansArg = argv[arg];
5261 mch_memmove(&argv[arg], &argv[arg + 1],
5262 (--*argc - arg) * sizeof(char *));
5263 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005264 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268}
5269
K.Takatac81e9bf2022-01-16 14:15:49 +00005270 static void
5271load_dpi_func(void)
5272{
5273 HMODULE hUser32;
5274
5275 hUser32 = GetModuleHandle("user32.dll");
5276 if (hUser32 == NULL)
5277 goto fail;
5278
5279 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5280 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5281 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5282 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5283 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5284 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5285
5286 if (pSetThreadDpiAwarenessContext != NULL)
5287 {
5288 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5289 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5290 if (oldctx != NULL)
5291 {
5292 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5293 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5294#ifdef DEBUG
5295 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5296 {
5297 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5298 }
5299#endif
5300 return;
5301 }
5302 }
5303
5304fail:
5305 // Disable PerMonitorV2 APIs.
5306 pGetDpiForSystem = stubGetDpiForSystem;
5307 pGetDpiForWindow = NULL;
5308 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5309 pSetThreadDpiAwarenessContext = NULL;
5310 pGetAwarenessFromDpiAwarenessContext = NULL;
5311}
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313/*
5314 * Initialise the GUI. Create all the windows, set up all the call-backs
5315 * etc.
5316 */
5317 int
5318gui_mch_init(void)
5319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005321 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
Bram Moolenaar734a8672019-12-02 22:49:38 +01005324 // Return here if the window was already opened (happens when
5325 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005327 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Load the tearoff bitmap
5331 */
5332#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005333 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334#endif
5335
K.Takatac81e9bf2022-01-16 14:15:49 +00005336 load_dpi_func();
5337
5338 s_dpi = pGetDpiForSystem();
5339 update_scrollbar_size();
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005342 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#endif
5344 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005345#ifdef FEAT_TOOLBAR
5346 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
5349 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5350
Bram Moolenaar734a8672019-12-02 22:49:38 +01005351 // First try using the wide version, so that we can use any title.
5352 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005353 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005355 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 wndclassw.lpfnWndProc = _WndProc;
5357 wndclassw.cbClsExtra = 0;
5358 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005359 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5361 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5362 wndclassw.hbrBackground = s_brush;
5363 wndclassw.lpszMenuName = NULL;
5364 wndclassw.lpszClassName = szVimWndClassW;
5365
K.Takata4ac893f2022-01-20 12:44:28 +00005366 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (vim_parent_hwnd != NULL)
5371 {
5372#ifdef HAVE_TRY_EXCEPT
5373 __try
5374 {
5375#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005376 // Open inside the specified parent window.
5377 // TODO: last argument should point to a CLIENTCREATESTRUCT
5378 // structure.
5379 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005381 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005382 WS_OVERLAPPEDWINDOW | WS_CHILD
5383 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5385 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005386 100, // Any value will do
5387 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005389 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#ifdef HAVE_TRY_EXCEPT
5391 }
5392 __except(EXCEPTION_EXECUTE_HANDLER)
5393 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005394 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396#endif
5397 if (s_hwnd == NULL)
5398 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005399 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 mch_exit(2);
5401 }
5402 }
5403 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005404 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005405 // If the provided windowid is not valid reset it to zero, so that it
5406 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005407 if (IsWindow((HWND)win_socket_id) <= 0)
5408 win_socket_id = 0;
5409
Bram Moolenaar734a8672019-12-02 22:49:38 +01005410 // Create a window. If win_socket_id is not zero without border and
5411 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005412 s_hwnd = CreateWindowW(
5413 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005414 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5415 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005416 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5417 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005418 100, // Any value will do
5419 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005420 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005421 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005422 if (s_hwnd != NULL && win_socket_id != 0)
5423 {
5424 SetParent(s_hwnd, (HWND)win_socket_id);
5425 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5426 }
5427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429 if (s_hwnd == NULL)
5430 return FAIL;
5431
K.Takatac81e9bf2022-01-16 14:15:49 +00005432 if (pGetDpiForWindow != NULL)
5433 {
5434 s_dpi = pGetDpiForWindow(s_hwnd);
5435 update_scrollbar_size();
5436 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5437 }
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5440 dyn_imm_load();
5441#endif
5442
Bram Moolenaar734a8672019-12-02 22:49:38 +01005443 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005444 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005445 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005446 wndclassw.style = CS_OWNDC;
5447 wndclassw.lpfnWndProc = _TextAreaWndProc;
5448 wndclassw.cbClsExtra = 0;
5449 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005450 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005451 wndclassw.hIcon = NULL;
5452 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5453 wndclassw.hbrBackground = NULL;
5454 wndclassw.lpszMenuName = NULL;
5455 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005456
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005457 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 return FAIL;
5459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005461 s_textArea = CreateWindowExW(
5462 0,
5463 szTextAreaClassW, L"Vim text area",
5464 WS_CHILD | WS_VISIBLE, 0, 0,
5465 100, // Any value will do for now
5466 100, // Any value will do for now
5467 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005468 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 if (s_textArea == NULL)
5471 return FAIL;
5472
Bram Moolenaar20321902016-02-17 12:30:17 +01005473#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005474 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005475 {
5476 HANDLE hIcon = NULL;
5477
5478 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005479 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005480 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005481#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483#ifdef FEAT_MENU
5484 s_menuBar = CreateMenu();
5485#endif
5486 s_hdc = GetDC(s_textArea);
5487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
Bram Moolenaar734a8672019-12-02 22:49:38 +01005490 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005491 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaar734a8672019-12-02 22:49:38 +01005493 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 gui_mch_def_colors();
5495
Bram Moolenaar734a8672019-12-02 22:49:38 +01005496 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5497 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 set_normal_colors();
5499
5500 /*
5501 * Check that none of the colors are the same as the background color.
5502 * Then store the current values as the defaults.
5503 */
5504 gui_check_colors();
5505 gui.def_norm_pixel = gui.norm_pixel;
5506 gui.def_back_pixel = gui.back_pixel;
5507
Bram Moolenaar734a8672019-12-02 22:49:38 +01005508 // Get the colors for the highlight groups (gui_check_colors() might have
5509 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 highlight_gui_started();
5511
5512 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005513 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005515 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 /*
5518 * Set up for Intellimouse processing
5519 */
5520 init_mouse_wheel();
5521
5522 /*
5523 * compute a couple of metrics used for the dialogs
5524 */
5525 get_dialog_font_metrics();
5526#ifdef FEAT_TOOLBAR
5527 /*
5528 * Create the toolbar
5529 */
5530 initialise_toolbar();
5531#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005532#ifdef FEAT_GUI_TABLINE
5533 /*
5534 * Create the tabline
5535 */
5536 initialise_tabline();
5537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#ifdef MSWIN_FIND_REPLACE
5539 /*
5540 * Initialise the dialog box stuff
5541 */
5542 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5543
Bram Moolenaar734a8672019-12-02 22:49:38 +01005544 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005546 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005548 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5550 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5551 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005554#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005555# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005556// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005557# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005558# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005559# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005560# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005561 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005562 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005563#endif
5564
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005565#ifdef FEAT_RENDER_OPTIONS
5566 if (p_rop)
5567 (void)gui_mch_set_rendering_options(p_rop);
5568#endif
5569
Bram Moolenaar748bf032005-02-02 23:04:36 +00005570theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005571 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005572 display_errors();
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 return OK;
5575}
5576
5577/*
5578 * Get the size of the screen, taking position on multiple monitors into
5579 * account (if supported).
5580 */
5581 static void
5582get_work_area(RECT *spi_rect)
5583{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005584 HMONITOR mon;
5585 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaar734a8672019-12-02 22:49:38 +01005587 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005588 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005589 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005591 moninfo.cbSize = sizeof(MONITORINFO);
5592 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005594 *spi_rect = moninfo.rcWork;
5595 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005598 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5600}
5601
5602/*
5603 * Set the size of the window to the given width and height in pixels.
5604 */
5605 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005606gui_mch_set_shellsize(
5607 int width,
5608 int height,
5609 int min_width UNUSED,
5610 int min_height UNUSED,
5611 int base_width UNUSED,
5612 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005613 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005616 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar734a8672019-12-02 22:49:38 +01005619 // Try to keep window completely on screen.
5620 // Get position of the screen work area. This is the part that is not
5621 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 get_work_area(&workarea_rect);
5623
Bram Moolenaar734a8672019-12-02 22:49:38 +01005624 // Resizing a maximized window looks very strange, unzoom it first.
5625 // But don't do it when still starting up, it may have been requested in
5626 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005627 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005629
5630 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar734a8672019-12-02 22:49:38 +01005632 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005633 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5634 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5635 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5636 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5637 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5638 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar734a8672019-12-02 22:49:38 +01005640 // The following should take care of keeping Vim on the same monitor, no
5641 // matter if the secondary monitor is left or right of the primary
5642 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005643 window_rect.right = window_rect.left + win_width;
5644 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005645
Bram Moolenaar734a8672019-12-02 22:49:38 +01005646 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005647 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5648 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005650 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5651 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005653 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5654 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005656 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5657 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005659 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5660 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 SetActiveWindow(s_hwnd);
5663 SetFocus(s_hwnd);
5664
Bram Moolenaar734a8672019-12-02 22:49:38 +01005665 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
5668
5669
5670 void
5671gui_mch_set_scrollbar_thumb(
5672 scrollbar_T *sb,
5673 long val,
5674 long size,
5675 long max)
5676{
5677 SCROLLINFO info;
5678
5679 sb->scroll_shift = 0;
5680 while (max > 32767)
5681 {
5682 max = (max + 1) >> 1;
5683 val >>= 1;
5684 size >>= 1;
5685 ++sb->scroll_shift;
5686 }
5687
5688 if (sb->scroll_shift > 0)
5689 ++size;
5690
5691 info.cbSize = sizeof(info);
5692 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5693 info.nPos = val;
5694 info.nMin = 0;
5695 info.nMax = max;
5696 info.nPage = size;
5697 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5698}
5699
5700
5701/*
5702 * Set the current text font.
5703 */
5704 void
5705gui_mch_set_font(GuiFont font)
5706{
5707 gui.currFont = font;
5708}
5709
5710
5711/*
5712 * Set the current text foreground color.
5713 */
5714 void
5715gui_mch_set_fg_color(guicolor_T color)
5716{
5717 gui.currFgColor = color;
5718}
5719
5720/*
5721 * Set the current text background color.
5722 */
5723 void
5724gui_mch_set_bg_color(guicolor_T color)
5725{
5726 gui.currBgColor = color;
5727}
5728
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005729/*
5730 * Set the current text special color.
5731 */
5732 void
5733gui_mch_set_sp_color(guicolor_T color)
5734{
5735 gui.currSpColor = color;
5736}
5737
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005738#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739/*
5740 * Multi-byte handling, originally by Sung-Hoon Baek.
5741 * First static functions (no prototypes generated).
5742 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005743# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005744# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005745# endif
5746# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 * handle WM_IME_NOTIFY message
5750 */
5751 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005752_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
5754 LRESULT lResult = 0;
5755 HIMC hImc;
5756
5757 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5758 return lResult;
5759 switch (dwCommand)
5760 {
5761 case IMN_SETOPENSTATUS:
5762 if (pImmGetOpenStatus(hImc))
5763 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005764 LOGFONTW lf = norm_logfont;
5765 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5766 // Work around when PerMonitorV2 is not enabled in the process level.
5767 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5768 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 im_set_position(gui.row, gui.col);
5770
Bram Moolenaar734a8672019-12-02 22:49:38 +01005771 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 State &= ~LANGMAP;
5773 if (State & INSERT)
5774 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005775# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005776 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5778 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005779 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 int old_row = gui.row;
5781 int old_col = gui.col;
5782
5783 // This must be called here before
5784 // status_redraw_curbuf(), otherwise the mode
5785 // message may appear in the wrong position.
5786 showmode();
5787 status_redraw_curbuf();
5788 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005789 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 gui.row = old_row;
5791 gui.col = old_col;
5792 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
5795 }
5796 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005797 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 lResult = 0;
5799 break;
5800 }
5801 pImmReleaseContext(hWnd, hImc);
5802 return lResult;
5803}
5804
5805 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005806_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 char_u *ret;
5809 int len;
5810
Bram Moolenaar734a8672019-12-02 22:49:38 +01005811 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 return 0;
5813
5814 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5815 if (ret != NULL)
5816 {
5817 add_to_input_buf_csi(ret, len);
5818 vim_free(ret);
5819 return 1;
5820 }
5821 return 0;
5822}
5823
5824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 * void GetResultStr()
5826 *
5827 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5828 * get complete composition string
5829 */
5830 static char_u *
5831GetResultStr(HWND hwnd, int GCS, int *lenp)
5832{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005833 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005834 LONG ret;
5835 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 char_u *convbuf = NULL;
5837
5838 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5839 return NULL;
5840
K.Takatab0b2b732022-01-19 12:59:21 +00005841 // Get the length of the composition string.
5842 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5843 if (ret <= 0)
5844 return NULL;
5845
5846 // Allocate the requested buffer plus space for the NUL character.
5847 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (buf == NULL)
5849 return NULL;
5850
K.Takatab0b2b732022-01-19 12:59:21 +00005851 // Reads in the composition string.
5852 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5853 *lenp = ret / sizeof(WCHAR);
5854
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005855 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 pImmReleaseContext(hwnd, hIMC);
5857 vim_free(buf);
5858 return convbuf;
5859}
5860#endif
5861
Bram Moolenaar734a8672019-12-02 22:49:38 +01005862// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005863#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865/*
5866 * set font to IM.
5867 */
5868 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005869im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 HIMC hImc;
5872
5873 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5874 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005875 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 pImmReleaseContext(s_hwnd, hImc);
5877 }
5878}
5879
5880/*
5881 * Notify cursor position to IM.
5882 */
5883 void
5884im_set_position(int row, int col)
5885{
5886 HIMC hImc;
5887
5888 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5889 {
5890 COMPOSITIONFORM cfs;
5891
5892 cfs.dwStyle = CFS_POINT;
5893 cfs.ptCurrentPos.x = FILL_X(col);
5894 cfs.ptCurrentPos.y = FILL_Y(row);
5895 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005896 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5897 {
5898 // Work around when PerMonitorV2 is not enabled in the process level.
5899 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5900 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 pImmSetCompositionWindow(hImc, &cfs);
5903
5904 pImmReleaseContext(s_hwnd, hImc);
5905 }
5906}
5907
5908/*
5909 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5910 */
5911 void
5912im_set_active(int active)
5913{
5914 HIMC hImc;
5915 static HIMC hImcOld = (HIMC)0;
5916
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005917# ifdef VIMDLL
5918 if (!gui.in_use && !gui.starting)
5919 {
5920 mbyte_im_set_active(active);
5921 return;
5922 }
5923# endif
5924
Bram Moolenaar734a8672019-12-02 22:49:38 +01005925 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 if (p_imdisable)
5928 {
5929 if (hImcOld == (HIMC)0)
5930 {
5931 hImcOld = pImmGetContext(s_hwnd);
5932 if (hImcOld)
5933 pImmAssociateContext(s_hwnd, (HIMC)0);
5934 }
5935 active = FALSE;
5936 }
5937 else if (hImcOld != (HIMC)0)
5938 {
5939 pImmAssociateContext(s_hwnd, hImcOld);
5940 hImcOld = (HIMC)0;
5941 }
5942
5943 hImc = pImmGetContext(s_hwnd);
5944 if (hImc)
5945 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005946 /*
5947 * for Korean ime
5948 */
5949 HKL hKL = GetKeyboardLayout(0);
5950
5951 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5952 {
5953 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5954 static BOOL bSaved = FALSE;
5955
5956 if (active)
5957 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005958 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005959 if (bSaved)
5960 pImmSetConversionStatus(hImc, dwConversionSaved,
5961 dwSentenceSaved);
5962 bSaved = FALSE;
5963 }
5964 else
5965 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005966 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005967 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5968 &dwSentenceSaved))
5969 {
5970 bSaved = TRUE;
5971 pImmSetConversionStatus(hImc,
5972 dwConversionSaved & ~(IME_CMODE_NATIVE
5973 | IME_CMODE_FULLSHAPE),
5974 dwSentenceSaved);
5975 }
5976 }
5977 }
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 pImmSetOpenStatus(hImc, active);
5980 pImmReleaseContext(s_hwnd, hImc);
5981 }
5982 }
5983}
5984
5985/*
5986 * Get IM status. When IM is on, return not 0. Else return 0.
5987 */
5988 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005989im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 int status = 0;
5992 HIMC hImc;
5993
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005994# ifdef VIMDLL
5995 if (!gui.in_use && !gui.starting)
5996 return mbyte_im_get_status();
5997# endif
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6000 {
6001 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6002 pImmReleaseContext(s_hwnd, hImc);
6003 }
6004 return status;
6005}
6006
Bram Moolenaar734a8672019-12-02 22:49:38 +01006007#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006010/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006011 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006012 */
6013 static void
6014latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6015{
6016 int c;
6017
Bram Moolenaarca003e12006-03-17 23:19:38 +00006018 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006019 {
6020 c = *text++;
6021 switch (c)
6022 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006023 case 0xa4: c = 0x20ac; break; // euro
6024 case 0xa6: c = 0x0160; break; // S hat
6025 case 0xa8: c = 0x0161; break; // S -hat
6026 case 0xb4: c = 0x017d; break; // Z hat
6027 case 0xb8: c = 0x017e; break; // Z -hat
6028 case 0xbc: c = 0x0152; break; // OE
6029 case 0xbd: c = 0x0153; break; // oe
6030 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006031 }
6032 *unicodebuf++ = c;
6033 }
6034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036#ifdef FEAT_RIGHTLEFT
6037/*
6038 * What is this for? In the case where you are using Win98 or Win2K or later,
6039 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6040 * reverses the string sent to the TextOut... family. This sucks, because we
6041 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6042 * way to tell Windblows not to do this!
6043 *
6044 * The short of it is that this 'RevOut' only gets called if you are running
6045 * one of the new, "improved" MS OSes, and only if you are running in
6046 * 'rightleft' mode. It makes display take *slightly* longer, but not
6047 * noticeably so.
6048 */
6049 static void
6050RevOut( HDC s_hdc,
6051 int col,
6052 int row,
6053 UINT foptions,
6054 CONST RECT *pcliprect,
6055 LPCTSTR text,
6056 UINT len,
6057 CONST INT *padding)
6058{
6059 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006061 for (ix = 0; ix < (int)len; ++ix)
6062 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6063 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064}
6065#endif
6066
Bram Moolenaar92467d32017-12-05 13:22:16 +01006067 static void
6068draw_line(
6069 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006070 int y1,
6071 int x2,
6072 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006073 COLORREF color)
6074{
6075#if defined(FEAT_DIRECTX)
6076 if (IS_ENABLE_DIRECTX())
6077 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6078 else
6079#endif
6080 {
6081 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6082 HPEN old_pen = SelectObject(s_hdc, hpen);
6083 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006084 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006085 LineTo(s_hdc, x2, y2);
6086 DeleteObject(SelectObject(s_hdc, old_pen));
6087 }
6088}
6089
6090 static void
6091set_pixel(
6092 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006093 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006094 COLORREF color)
6095{
6096#if defined(FEAT_DIRECTX)
6097 if (IS_ENABLE_DIRECTX())
6098 DWriteContext_SetPixel(s_dwc, x, y, color);
6099 else
6100#endif
6101 SetPixel(s_hdc, x, y, color);
6102}
6103
6104 static void
6105fill_rect(
6106 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006107 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006108 COLORREF color)
6109{
6110#if defined(FEAT_DIRECTX)
6111 if (IS_ENABLE_DIRECTX())
6112 DWriteContext_FillRect(s_dwc, rcp, color);
6113 else
6114#endif
6115 {
6116 HBRUSH hbr2;
6117
6118 if (hbr == NULL)
6119 hbr2 = CreateSolidBrush(color);
6120 else
6121 hbr2 = hbr;
6122 FillRect(s_hdc, rcp, hbr2);
6123 if (hbr == NULL)
6124 DeleteBrush(hbr2);
6125 }
6126}
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 void
6129gui_mch_draw_string(
6130 int row,
6131 int col,
6132 char_u *text,
6133 int len,
6134 int flags)
6135{
6136 static int *padding = NULL;
6137 static int pad_size = 0;
6138 int i;
6139 const RECT *pcliprect = NULL;
6140 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 static WCHAR *unicodebuf = NULL;
6142 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006143 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 int y;
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 /*
6148 * Italic and bold text seems to have an extra row of pixels at the bottom
6149 * (below where the bottom of the character should be). If we draw the
6150 * characters with a solid background, the top row of pixels in the
6151 * character below will be overwritten. We can fix this by filling in the
6152 * background ourselves, to the correct character proportions, and then
6153 * writing the character in transparent mode. Still have a problem when
6154 * the character is "_", which gets written on to the character below.
6155 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6156 * pixel in their slots, which fixes the problem with the bottom row of
6157 * pixels. We still need this code because otherwise the top row of pixels
6158 * becomes a problem. - webb.
6159 */
6160 static HBRUSH hbr_cache[2] = {NULL, NULL};
6161 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6162 static int brush_lru = 0;
6163 HBRUSH hbr;
6164 RECT rc;
6165
6166 if (!(flags & DRAW_TRANSP))
6167 {
6168 /*
6169 * Clear background first.
6170 * Note: FillRect() excludes right and bottom of rectangle.
6171 */
6172 rc.left = FILL_X(col);
6173 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (has_mbyte)
6175 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006176 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006177 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 }
6179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 rc.right = FILL_X(col + len);
6181 rc.bottom = FILL_Y(row + 1);
6182
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 // Cache the created brush, that saves a lot of time. We need two:
6184 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (gui.currBgColor == brush_color[0])
6186 {
6187 hbr = hbr_cache[0];
6188 brush_lru = 1;
6189 }
6190 else if (gui.currBgColor == brush_color[1])
6191 {
6192 hbr = hbr_cache[1];
6193 brush_lru = 0;
6194 }
6195 else
6196 {
6197 if (hbr_cache[brush_lru] != NULL)
6198 DeleteBrush(hbr_cache[brush_lru]);
6199 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6200 brush_color[brush_lru] = gui.currBgColor;
6201 hbr = hbr_cache[brush_lru];
6202 brush_lru = !brush_lru;
6203 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006204
Bram Moolenaar92467d32017-12-05 13:22:16 +01006205 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 SetBkMode(s_hdc, TRANSPARENT);
6208
6209 /*
6210 * When drawing block cursor, prevent inverted character spilling
6211 * over character cell (can happen with bold/italic)
6212 */
6213 if (flags & DRAW_CURSOR)
6214 {
6215 pcliprect = &rc;
6216 foptions = ETO_CLIPPED;
6217 }
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 SetTextColor(s_hdc, gui.currFgColor);
6220 SelectFont(s_hdc, gui.currFont);
6221
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006222#ifdef FEAT_DIRECTX
6223 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006224 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006225#endif
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6228 {
6229 vim_free(padding);
6230 pad_size = Columns;
6231
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // Don't give an out-of-memory message here, it would call us
6233 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006234 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (padding != NULL)
6236 for (i = 0; i < pad_size; i++)
6237 padding[i] = gui.char_width;
6238 }
6239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 /*
6241 * We have to provide the padding argument because italic and bold versions
6242 * of fixed-width fonts are often one pixel or so wider than their normal
6243 * versions.
6244 * No check for DRAW_BOLD, Windows will have done it already.
6245 */
6246
Bram Moolenaar734a8672019-12-02 22:49:38 +01006247 // Check if there are any UTF-8 characters. If not, use normal text
6248 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (enc_utf8)
6250 for (n = 0; n < len; ++n)
6251 if (text[n] >= 0x80)
6252 break;
6253
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006254#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006255 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6256 // required that unicode drawing routine, currently. So this forces it
6257 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006258 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006259 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006260#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006261
Bram Moolenaar734a8672019-12-02 22:49:38 +01006262 // Check if the Unicode buffer exists and is big enough. Create it
6263 // with the same length as the multi-byte string, the number of wide
6264 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006265 if ((enc_utf8
6266 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6267 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 && (unicodebuf == NULL || len > unibuflen))
6269 {
6270 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006271 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006274 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 unibuflen = len;
6277 }
6278
6279 if (enc_utf8 && n < len && unicodebuf != NULL)
6280 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006281 // Output UTF-8 characters. Composing characters should be
6282 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006283 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006284 int wlen; // string length in words
6285 int clen; // string length in characters
6286 int cells; // cell width of string up to composing char
6287 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006288 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006290 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006291 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006293 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006295 c = utf_ptr2char(text + i);
6296 if (c >= 0x10000)
6297 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006298 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006299 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6300 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006301 }
6302 else
6303 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006304 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006305 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006306
6307 if (utf_iscomposing(c))
6308 cw = 0;
6309 else
6310 {
6311 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006312 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006313 cw = 1;
6314 }
6315
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 if (unicodepdy != NULL)
6317 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006318 // Use unicodepdy to make characters fit as we expect, even
6319 // when the font uses different widths (e.g., bold character
6320 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006321 if (c >= 0x10000)
6322 {
6323 unicodepdy[wlen - 2] = cw * gui.char_width;
6324 unicodepdy[wlen - 1] = 0;
6325 }
6326 else
6327 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 }
6329 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006330 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006331 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006333#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006334 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006335 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006336 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006337 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006338 TEXT_X(col), TEXT_Y(row),
6339 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006340 gui.char_width, gui.currFgColor,
6341 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006342 }
6343 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006344#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006345 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6346 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006347 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006349 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006351 // If we want to display codepage data, and the current CP is not the
6352 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (unicodebuf != NULL)
6354 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006355 if (enc_latin9)
6356 latin9_to_ucs(text, len, unicodebuf);
6357 else
6358 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 MB_PRECOMPOSED,
6360 (char *)text, len,
6361 (LPWSTR)unicodebuf, unibuflen);
6362 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006363 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006364 // Use unicodepdy to make characters fit as we expect, even
6365 // when the font uses different widths (e.g., bold character
6366 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006367 if (unicodepdy != NULL)
6368 {
6369 int i;
6370 int cw;
6371
6372 for (i = 0; i < len; ++i)
6373 {
6374 cw = utf_char2cells(unicodebuf[i]);
6375 if (cw > 2)
6376 cw = 1;
6377 unicodepdy[i] = cw * gui.char_width;
6378 }
6379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006381 foptions, pcliprect, unicodebuf, len, unicodepdy);
6382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
6384 }
6385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 {
6387#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006388 // Windows will mess up RL text, so we have to draw it character by
6389 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006390 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6392 foptions, pcliprect, (char *)text, len, padding);
6393 else
6394#endif
6395 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6396 foptions, pcliprect, (char *)text, len, padding);
6397 }
6398
Bram Moolenaar734a8672019-12-02 22:49:38 +01006399 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (flags & DRAW_UNDERL)
6401 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006402 // When p_linespace is 0, overwrite the bottom row of pixels.
6403 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (p_linespace > 1)
6406 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006407 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006409
Bram Moolenaar734a8672019-12-02 22:49:38 +01006410 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006411 if (flags & DRAW_STRIKE)
6412 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006413 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006414 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006415 }
6416
Bram Moolenaar734a8672019-12-02 22:49:38 +01006417 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006418 if (flags & DRAW_UNDERC)
6419 {
6420 int x;
6421 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006422 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006423
6424 y = FILL_Y(row + 1) - 1;
6425 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6426 {
6427 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006428 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006429 }
6430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
6433
6434/*
6435 * Output routines.
6436 */
6437
Bram Moolenaar734a8672019-12-02 22:49:38 +01006438/*
6439 * Flush any output to the screen
6440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 void
6442gui_mch_flush(void)
6443{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006444#if defined(FEAT_DIRECTX)
6445 if (IS_ENABLE_DIRECTX())
6446 DWriteContext_Flush(s_dwc);
6447#endif
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 GdiFlush();
6450}
6451
6452 static void
6453clear_rect(RECT *rcp)
6454{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006455 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456}
6457
6458
Bram Moolenaarc716c302006-01-21 22:12:51 +00006459 void
6460gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6461{
6462 RECT workarea_rect;
6463
6464 get_work_area(&workarea_rect);
6465
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006467 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6468 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006469
Bram Moolenaar734a8672019-12-02 22:49:38 +01006470 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6471 // the menubar for MSwin, we subtract it from the screen height, so that
6472 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006473 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006474 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6475 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6476 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6477 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006478}
6479
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481#if defined(FEAT_MENU) || defined(PROTO)
6482/*
6483 * Add a sub menu to the menu bar.
6484 */
6485 void
6486gui_mch_add_menu(
6487 vimmenu_T *menu,
6488 int pos)
6489{
6490 vimmenu_T *parent = menu->parent;
6491
6492 menu->submenu_id = CreatePopupMenu();
6493 menu->id = s_menu_id++;
6494
6495 if (menu_is_menubar(menu->name))
6496 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006497 WCHAR *wn;
6498 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006500 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006501 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006502 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006504 infow.cbSize = sizeof(infow);
6505 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6506 | MIIM_SUBMENU;
6507 infow.dwItemData = (long_u)menu;
6508 infow.wID = menu->id;
6509 infow.fType = MFT_STRING;
6510 infow.dwTypeData = wn;
6511 infow.cch = (UINT)wcslen(wn);
6512 infow.hSubMenu = menu->submenu_id;
6513 InsertMenuItemW((parent == NULL)
6514 ? s_menuBar : parent->submenu_id,
6515 (UINT)pos, TRUE, &infow);
6516 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518
Bram Moolenaar734a8672019-12-02 22:49:38 +01006519 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (parent == NULL)
6521 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006522# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 else if (IsWindow(parent->tearoff_handle))
6524 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
6528 void
6529gui_mch_show_popupmenu(vimmenu_T *menu)
6530{
6531 POINT mp;
6532
6533 (void)GetCursorPos((LPPOINT)&mp);
6534 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6535}
6536
6537 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006538gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 vimmenu_T *menu = gui_find_menu(path_name);
6541
6542 if (menu != NULL)
6543 {
6544 POINT p;
6545
Bram Moolenaar734a8672019-12-02 22:49:38 +01006546 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006548 if (mouse_pos)
6549 {
6550 int mx, my;
6551
6552 gui_mch_getmouse(&mx, &my);
6553 p.x += mx;
6554 p.y += my;
6555 }
6556 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006558 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6560 }
6561 msg_scroll = FALSE;
6562 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6563 }
6564}
6565
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006566# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
6568 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6569 * create it as a pseudo-"tearoff menu".
6570 */
6571 void
6572gui_make_tearoff(char_u *path_name)
6573{
6574 vimmenu_T *menu = gui_find_menu(path_name);
6575
Bram Moolenaar734a8672019-12-02 22:49:38 +01006576 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 if (menu != NULL)
6578 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6579}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582/*
6583 * Add a menu item to a menu
6584 */
6585 void
6586gui_mch_add_menu_item(
6587 vimmenu_T *menu,
6588 int idx)
6589{
6590 vimmenu_T *parent = menu->parent;
6591
6592 menu->id = s_menu_id++;
6593 menu->submenu_id = NULL;
6594
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006595# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6597 {
6598 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6599 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6600 }
6601 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006602# endif
6603# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 if (menu_is_toolbar(parent->name))
6605 {
6606 TBBUTTON newtb;
6607
Bram Moolenaara80faa82020-04-12 19:37:17 +02006608 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 if (menu_is_separator(menu->name))
6610 {
6611 newtb.iBitmap = 0;
6612 newtb.fsStyle = TBSTYLE_SEP;
6613 }
6614 else
6615 {
6616 newtb.iBitmap = get_toolbar_bitmap(menu);
6617 newtb.fsStyle = TBSTYLE_BUTTON;
6618 }
6619 newtb.idCommand = menu->id;
6620 newtb.fsState = TBSTATE_ENABLED;
6621 newtb.iString = 0;
6622 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6623 (LPARAM)&newtb);
6624 menu->submenu_id = (HMENU)-1;
6625 }
6626 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006629 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006631 wn = enc_to_utf16(menu->name, NULL);
6632 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006634 InsertMenuW(parent->submenu_id, (UINT)idx,
6635 (menu_is_separator(menu->name)
6636 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6637 (UINT)menu->id, wn);
6638 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006640# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (IsWindow(parent->tearoff_handle))
6642 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645}
6646
6647/*
6648 * Destroy the machine specific menu widget.
6649 */
6650 void
6651gui_mch_destroy_menu(vimmenu_T *menu)
6652{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006653# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 /*
6655 * is this a toolbar button?
6656 */
6657 if (menu->submenu_id == (HMENU)-1)
6658 {
6659 int iButton;
6660
6661 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6662 (WPARAM)menu->id, 0);
6663 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6664 }
6665 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 if (menu->parent != NULL
6669 && menu_is_popup(menu->parent->dname)
6670 && menu->parent->submenu_id != NULL)
6671 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6672 else
6673 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6674 if (menu->submenu_id != NULL)
6675 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006676# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (IsWindow(menu->tearoff_handle))
6678 DestroyWindow(menu->tearoff_handle);
6679 if (menu->parent != NULL
6680 && menu->parent->children != NULL
6681 && IsWindow(menu->parent->tearoff_handle))
6682 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006683 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 menu->modes = 0;
6685 rebuild_tearoff(menu->parent);
6686 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 }
6689}
6690
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006691# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 static void
6693rebuild_tearoff(vimmenu_T *menu)
6694{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006695 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 char_u tbuf[128];
6697 RECT trect;
6698 RECT rct;
6699 RECT roct;
6700 int x, y;
6701
6702 HWND thwnd = menu->tearoff_handle;
6703
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006704 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (GetWindowRect(thwnd, &trect)
6706 && GetWindowRect(s_hwnd, &rct)
6707 && GetClientRect(s_hwnd, &roct))
6708 {
6709 x = trect.left - rct.left;
6710 y = (trect.top - rct.bottom + roct.bottom);
6711 }
6712 else
6713 {
6714 x = y = 0xffffL;
6715 }
6716 DestroyWindow(thwnd);
6717 if (menu->children != NULL)
6718 {
6719 gui_mch_tearoff(tbuf, menu, x, y);
6720 if (IsWindow(menu->tearoff_handle))
6721 (void) SetWindowPos(menu->tearoff_handle,
6722 NULL,
6723 (int)trect.left,
6724 (int)trect.top,
6725 0, 0,
6726 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6727 }
6728}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006729# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731/*
6732 * Make a menu either grey or not grey.
6733 */
6734 void
6735gui_mch_menu_grey(
6736 vimmenu_T *menu,
6737 int grey)
6738{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006739# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 /*
6741 * is this a toolbar button?
6742 */
6743 if (menu->submenu_id == (HMENU)-1)
6744 {
6745 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6746 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6747 }
6748 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006749# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006750 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6751 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006753# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6755 {
6756 WORD menuID;
6757 HWND menuHandle;
6758
6759 /*
6760 * A tearoff button has changed state.
6761 */
6762 if (menu->children == NULL)
6763 menuID = (WORD)(menu->id);
6764 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006765 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6767 if (menuHandle)
6768 EnableWindow(menuHandle, !grey);
6769
6770 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772}
6773
Bram Moolenaar734a8672019-12-02 22:49:38 +01006774#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776
Bram Moolenaar734a8672019-12-02 22:49:38 +01006777// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6780#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006781#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6784/*
6785 * stuff for dialogs
6786 */
6787
6788/*
6789 * The callback routine used by all the dialogs. Very simple. First,
6790 * acknowledges the INITDIALOG message so that Windows knows to do standard
6791 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6792 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6793 * number.
6794 */
6795 static LRESULT CALLBACK
6796dialog_callback(
6797 HWND hwnd,
6798 UINT message,
6799 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006800 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 if (message == WM_INITDIALOG)
6803 {
6804 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006805 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 (void)SetFocus(hwnd);
6807 if (dialog_default_button > IDCANCEL)
6808 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006809 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006810 // We don't have a default, set focus on another element of the
6811 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006812 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 return FALSE;
6814 }
6815
6816 if (message == WM_COMMAND)
6817 {
6818 int button = LOWORD(wParam);
6819
Bram Moolenaar734a8672019-12-02 22:49:38 +01006820 // Don't end the dialog if something was selected that was
6821 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (button >= DLG_NONBUTTON_CONTROL)
6823 return TRUE;
6824
Bram Moolenaar734a8672019-12-02 22:49:38 +01006825 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006827 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006828 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006829 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006830
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006831 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6832 p = utf16_to_enc(wp, NULL);
6833 vim_strncpy(s_textfield, p, IOSIZE);
6834 vim_free(p);
6835 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
6838 /*
6839 * Need to check for IDOK because if the user just hits Return to
6840 * accept the default value, some reason this is what we get.
6841 */
6842 if (button == IDOK)
6843 {
6844 if (dialog_default_button > IDCANCEL)
6845 EndDialog(hwnd, dialog_default_button);
6846 }
6847 else
6848 EndDialog(hwnd, button - IDCANCEL);
6849 return TRUE;
6850 }
6851
6852 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6853 {
6854 EndDialog(hwnd, 0);
6855 return TRUE;
6856 }
6857 return FALSE;
6858}
6859
6860/*
6861 * Create a dialog dynamically from the parameter strings.
6862 * type = type of dialog (question, alert, etc.)
6863 * title = dialog title. may be NULL for default title.
6864 * message = text to display. Dialog sizes to accommodate it.
6865 * buttons = '\n' separated list of button captions, default first.
6866 * dfltbutton = number of default button.
6867 *
6868 * This routine returns 1 if the first button is pressed,
6869 * 2 for the second, etc.
6870 *
6871 * 0 indicates Esc was pressed.
6872 * -1 for unexpected error
6873 *
6874 * If stubbing out this fn, return 1.
6875 */
6876
Bram Moolenaar734a8672019-12-02 22:49:38 +01006877static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 "IDR_VIM",
6880 "IDR_VIM_ERROR",
6881 "IDR_VIM_ALERT",
6882 "IDR_VIM_INFO",
6883 "IDR_VIM_QUESTION"
6884};
6885
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 int
6887gui_mch_dialog(
6888 int type,
6889 char_u *title,
6890 char_u *message,
6891 char_u *buttons,
6892 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006893 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006894 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006897 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 int numButtons;
6899 int *buttonWidths, *buttonPositions;
6900 int buttonYpos;
6901 int nchar, i;
6902 DWORD lStyle;
6903 int dlgwidth = 0;
6904 int dlgheight;
6905 int editboxheight;
6906 int horizWidth = 0;
6907 int msgheight;
6908 char_u *pstart;
6909 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006910 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 char_u *tbuffer;
6912 RECT rect;
6913 HWND hwnd;
6914 HDC hdc;
6915 HFONT font, oldFont;
6916 TEXTMETRIC fontInfo;
6917 int fontHeight;
6918 int textWidth, minButtonWidth, messageWidth;
6919 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006920 int maxDialogHeight;
6921 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 int vertical;
6923 int dlgPaddingX;
6924 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006925# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006926 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006928# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929 garray_T ga;
6930 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006931 int dlg_icon_width;
6932 int dlg_icon_height;
6933 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006935# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006936 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006937# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006938 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006939# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006940 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006941 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar748bf032005-02-02 23:04:36 +00006944 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006945 {
6946 load_dpi_func();
6947 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006948 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006949 }
6950 else
6951 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953 if ((type < 0) || (type > VIM_LAST_TYPE))
6954 type = 0;
6955
Bram Moolenaar734a8672019-12-02 22:49:38 +01006956 // allocate some memory for dialog template
6957 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006958 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006959 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 if (p == NULL)
6962 return -1;
6963
6964 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006965 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6967 * const.
6968 */
6969 tbuffer = vim_strsave(buttons);
6970 if (tbuffer == NULL)
6971 return -1;
6972
Bram Moolenaar734a8672019-12-02 22:49:38 +01006973 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar734a8672019-12-02 22:49:38 +01006975 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 numButtons = 1;
6977 for (i = 0; tbuffer[i] != '\0'; i++)
6978 {
6979 if (tbuffer[i] == DLG_BUTTON_SEP)
6980 numButtons++;
6981 }
6982 if (dfltbutton >= numButtons)
6983 dfltbutton = -1;
6984
Bram Moolenaar734a8672019-12-02 22:49:38 +01006985 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006986 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (buttonWidths == NULL)
6988 return -1;
6989
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006991 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (buttonPositions == NULL)
6993 return -1;
6994
6995 /*
6996 * Calculate how big the dialog must be.
6997 */
6998 hwnd = GetDesktopWindow();
6999 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007000# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7002 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007003 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 use_lfSysmenu = TRUE;
7005 }
7006 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007009 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (s_usenewlook)
7011 {
7012 oldFont = SelectFont(hdc, font);
7013 dlgPaddingX = DLG_PADDING_X;
7014 dlgPaddingY = DLG_PADDING_Y;
7015 }
7016 else
7017 {
7018 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7019 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7020 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7021 }
7022 GetTextMetrics(hdc, &fontInfo);
7023 fontHeight = fontInfo.tmHeight;
7024
Bram Moolenaar734a8672019-12-02 22:49:38 +01007025 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007026 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007029 if (s_hwnd == NULL)
7030 {
7031 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
Bram Moolenaar734a8672019-12-02 22:49:38 +01007033 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007034 get_work_area(&workarea_rect);
7035 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007036 if (maxDialogWidth > adjust_by_system_dpi(600))
7037 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007038 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007039 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007040 }
7041 else
7042 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007043 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007044 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007045 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007046 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7047 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7048 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7049 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007050
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007051 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007052 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7053 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7054 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7055 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7056 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007057 }
7058
Bram Moolenaar734a8672019-12-02 22:49:38 +01007059 // Set dlgwidth to width of message.
7060 // Copy the message into "ga", changing NL to CR-NL and inserting line
7061 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 pstart = message;
7063 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007064 msgheight = 0;
7065 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 do
7067 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007068 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007069
Bram Moolenaar734a8672019-12-02 22:49:38 +01007070 // Need to figure out where to break the string. The system does it
7071 // at a word boundary, which would mean we can't compute the number of
7072 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007073 textWidth = 0;
7074 last_white = NULL;
7075 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007076 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007077 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007078 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007079 && textWidth > maxDialogWidth * 3 / 4)
7080 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007081 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007082 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007083 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007084 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007085 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007086 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007087 textWidth = 0;
7088
7089 if (last_white != NULL)
7090 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007091 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007092 if (pend > last_white)
7093 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007094 pend = last_white + 1;
7095 last_white = NULL;
7096 }
7097 ga_append(&ga, '\r');
7098 ga_append(&ga, '\n');
7099 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007100 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007101
7102 while (--l >= 0)
7103 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007104 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007105 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007107
7108 ga_append(&ga, '\r');
7109 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 pstart = pend + 1;
7111 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007112
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007113 if (ga.ga_data != NULL)
7114 message = ga.ga_data;
7115
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007117
K.Takatac81e9bf2022-01-16 14:15:49 +00007118 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7119 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
K.Takatac81e9bf2022-01-16 14:15:49 +00007121 // Add width of icon to dlgwidth, and some space
7122 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7123 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7124
7125 if (msgheight < dlg_icon_height)
7126 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128 /*
7129 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007130 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007132 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (!vertical)
7134 {
7135 // Place buttons horizontally if they fit.
7136 horizWidth = dlgPaddingX;
7137 pstart = tbuffer;
7138 i = 0;
7139 do
7140 {
7141 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7142 if (pend == NULL)
7143 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007144 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (textWidth < minButtonWidth)
7146 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007147 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 buttonWidths[i] = textWidth;
7149 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007150 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 pstart = pend + 1;
7152 } while (*pend != NUL);
7153
7154 if (horizWidth > maxDialogWidth)
7155 vertical = TRUE; // Too wide to fit on the screen.
7156 else if (horizWidth > dlgwidth)
7157 dlgwidth = horizWidth;
7158 }
7159
7160 if (vertical)
7161 {
7162 // Stack buttons vertically.
7163 pstart = tbuffer;
7164 do
7165 {
7166 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7167 if (pend == NULL)
7168 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007169 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007170 textWidth += dlgPaddingX; // Padding within button
7171 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (textWidth > dlgwidth)
7173 dlgwidth = textWidth;
7174 pstart = pend + 1;
7175 } while (*pend != NUL);
7176 }
7177
7178 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007179 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar734a8672019-12-02 22:49:38 +01007181 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (s_usenewlook)
7183 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7184 else
7185 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7186
7187 add_long(lStyle);
7188 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007189 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 add_word(0); // NumberOfItems(will change later)
7191 add_word(10); // x
7192 add_word(10); // y
7193 add_word(PixelToDialogX(dlgwidth)); // cx
7194
7195 // Dialog height.
7196 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007197 dlgheight = msgheight + 2 * dlgPaddingY
7198 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 else
7200 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7201
7202 // Dialog needs to be taller if contains an edit box.
7203 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7204 if (textfield != NULL)
7205 dlgheight += editboxheight;
7206
Bram Moolenaar734a8672019-12-02 22:49:38 +01007207 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007208 if (dlgheight > maxDialogHeight)
7209 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007210 msgheight = msgheight - (dlgheight - maxDialogHeight);
7211 dlgheight = maxDialogHeight;
7212 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007213 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007214 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007215 }
7216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 add_word(PixelToDialogY(dlgheight));
7218
7219 add_word(0); // Menu
7220 add_word(0); // Class
7221
Bram Moolenaar734a8672019-12-02 22:49:38 +01007222 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007223 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7224 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 p += nchar;
7226
7227 if (s_usenewlook)
7228 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007229 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007230# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 if (use_lfSysmenu)
7232 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007233 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7235 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007236 wcscpy(p, lfSysmenu.lfFaceName);
7237 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
7242 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007243 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 p += nchar;
7246 }
7247
7248 buttonYpos = msgheight + 2 * dlgPaddingY;
7249
7250 if (textfield != NULL)
7251 buttonYpos += editboxheight;
7252
7253 pstart = tbuffer;
7254 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007255 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 for (i = 0; i < numButtons; i++)
7257 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007258 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 for ( pend = pstart;
7260 *pend && (*pend != DLG_BUTTON_SEP);
7261 pend++)
7262 ;
7263
7264 if (*pend)
7265 *pend = '\0';
7266
7267 /*
7268 * old NOTE:
7269 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7270 * the focus to the first tab-able button and in so doing makes that
7271 * the default!! Grrr. Workaround: Make the default button the only
7272 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7273 * he/she can use arrow keys.
7274 *
7275 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007276 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 * dialog. Also needed for when the textfield is the default control.
7278 * It appears to work now (perhaps not on Win95?).
7279 */
7280 if (vertical)
7281 {
7282 p = add_dialog_element(p,
7283 (i == dfltbutton
7284 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7285 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007286 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 + 2 * fontHeight * i),
7288 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7289 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007290 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292 else
7293 {
7294 p = add_dialog_element(p,
7295 (i == dfltbutton
7296 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7297 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007298 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 PixelToDialogX(buttonWidths[i]),
7300 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007301 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007303 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
7305 *pnumitems += numButtons;
7306
Bram Moolenaar734a8672019-12-02 22:49:38 +01007307 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 p = add_dialog_element(p, SS_ICON,
7309 PixelToDialogX(dlgPaddingX),
7310 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007311 PixelToDialogX(dlg_icon_width),
7312 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7314 dlg_icons[type]);
7315
Bram Moolenaar734a8672019-12-02 22:49:38 +01007316 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007317 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007318 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007319 PixelToDialogY(dlgPaddingY),
7320 (WORD)(PixelToDialogX(messageWidth) + 1),
7321 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007322 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
Bram Moolenaar734a8672019-12-02 22:49:38 +01007324 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 if (textfield != NULL)
7326 {
7327 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7328 PixelToDialogX(2 * dlgPaddingX),
7329 PixelToDialogY(2 * dlgPaddingY + msgheight),
7330 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7331 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007332 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 *pnumitems += 1;
7334 }
7335
7336 *pnumitems += 2;
7337
7338 SelectFont(hdc, oldFont);
7339 DeleteObject(font);
7340 ReleaseDC(hwnd, hdc);
7341
Bram Moolenaar734a8672019-12-02 22:49:38 +01007342 // Let the dialog_callback() function know which button to make default
7343 // If we have an edit box, make that the default. We also need to tell
7344 // dialog_callback() if this dialog contains an edit box or not. We do
7345 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (textfield != NULL)
7347 {
7348 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7349 s_textfield = textfield;
7350 }
7351 else
7352 {
7353 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7354 s_textfield = NULL;
7355 }
7356
Bram Moolenaar734a8672019-12-02 22:49:38 +01007357 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007359 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 (LPDLGTEMPLATE)pdlgtemplate,
7361 s_hwnd,
7362 (DLGPROC)dialog_callback);
7363
7364 LocalFree(LocalHandle(pdlgtemplate));
7365 vim_free(tbuffer);
7366 vim_free(buttonWidths);
7367 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007368 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
Bram Moolenaar734a8672019-12-02 22:49:38 +01007370 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 (void)SetFocus(s_hwnd);
7372
7373 return nchar;
7374}
7375
Bram Moolenaar734a8672019-12-02 22:49:38 +01007376#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378/*
7379 * Put a simple element (basic class) onto a dialog template in memory.
7380 * return a pointer to where the next item should be added.
7381 *
7382 * parameters:
7383 * lStyle = additional style flags
7384 * (be careful, NT3.51 & Win32s will ignore the new ones)
7385 * x,y = x & y positions IN DIALOG UNITS
7386 * w,h = width and height IN DIALOG UNITS
7387 * Id = ID used in messages
7388 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7389 * caption = usually text or resource name
7390 *
7391 * TODO: use the length information noted here to enable the dialog creation
7392 * routines to work out more exactly how much memory they need to alloc.
7393 */
7394 static PWORD
7395add_dialog_element(
7396 PWORD p,
7397 DWORD lStyle,
7398 WORD x,
7399 WORD y,
7400 WORD w,
7401 WORD h,
7402 WORD Id,
7403 WORD clss,
7404 const char *caption)
7405{
7406 int nchar;
7407
Bram Moolenaar734a8672019-12-02 22:49:38 +01007408 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7410 *p++ = LOWORD(lStyle);
7411 *p++ = HIWORD(lStyle);
7412 *p++ = 0; // LOWORD (lExtendedStyle)
7413 *p++ = 0; // HIWORD (lExtendedStyle)
7414 *p++ = x;
7415 *p++ = y;
7416 *p++ = w;
7417 *p++ = h;
7418 *p++ = Id; //9 or 10 words in all
7419
7420 *p++ = (WORD)0xffff;
7421 *p++ = clss; //2 more here
7422
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007423 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 p += nchar;
7425
7426 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7427
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007428 return p; // total = 15 + strlen(caption) words
7429 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431
7432
7433/*
7434 * Helper routine. Take an input pointer, return closest pointer that is
7435 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7436 */
7437 static LPWORD
7438lpwAlign(
7439 LPWORD lpIn)
7440{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007441 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007443 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 ul += 3;
7445 ul >>= 2;
7446 ul <<= 2;
7447 return (LPWORD)ul;
7448}
7449
7450/*
7451 * Helper routine. Takes second parameter as Ansi string, copies it to first
7452 * parameter as wide character (16-bits / char) string, and returns integer
7453 * number of wide characters (words) in string (including the trailing wide
7454 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007455 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7456 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 static int
7458nCopyAnsiToWideChar(
7459 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007460 LPSTR lpAnsiIn,
7461 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
7463 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007464 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 int i;
7466 WCHAR *wn;
7467
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007468 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007471 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 if (wn != NULL)
7473 {
7474 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007475 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 vim_free(wn);
7477 }
7478 }
7479 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007480 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 nChar = MultiByteToWideChar(
7482 enc_codepage > 0 ? enc_codepage : CP_ACP,
7483 MB_PRECOMPOSED,
7484 lpAnsiIn, len,
7485 lpWCStr, len);
7486 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007487 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
7490 return nChar;
7491}
7492
7493
7494#ifdef FEAT_TEAROFF
7495/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007496 * Lookup menu handle from "menu_id".
7497 */
7498 static HMENU
7499tearoff_lookup_menuhandle(
7500 vimmenu_T *menu,
7501 WORD menu_id)
7502{
7503 for ( ; menu != NULL; menu = menu->next)
7504 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007505 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007506 continue;
7507 if (menu_is_separator(menu->dname))
7508 continue;
7509 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7510 return menu->submenu_id;
7511 }
7512 return NULL;
7513}
7514
7515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 * The callback function for all the modeless dialogs that make up the
7517 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7518 * thinking its menus have been clicked), and go away when closed.
7519 */
7520 static LRESULT CALLBACK
7521tearoff_callback(
7522 HWND hwnd,
7523 UINT message,
7524 WPARAM wParam,
7525 LPARAM lParam)
7526{
7527 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007528 {
7529 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
Bram Moolenaar734a8672019-12-02 22:49:38 +01007533 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 HandleMouseHide(message, lParam);
7535
7536 if (message == WM_COMMAND)
7537 {
7538 if ((WORD)(LOWORD(wParam)) & 0x8000)
7539 {
7540 POINT mp;
7541 RECT rect;
7542
7543 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7544 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007545 vimmenu_T *menu;
7546
7547 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007549 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7551 (int)rect.right - 8,
7552 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007553 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 s_hwnd,
7555 NULL);
7556 /*
7557 * NOTE: The pop-up menu can eat the mouse up event.
7558 * We deal with this in normal.c.
7559 */
7560 }
7561 }
7562 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007563 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7565 /*
7566 * Give main window the focus back: this is so after
7567 * choosing a tearoff button you can start typing again
7568 * straight away.
7569 */
7570 (void)SetFocus(s_hwnd);
7571 return TRUE;
7572 }
7573 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7574 {
7575 DestroyWindow(hwnd);
7576 return TRUE;
7577 }
7578
Bram Moolenaar734a8672019-12-02 22:49:38 +01007579 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (message == WM_EXITSIZEMOVE)
7581 (void)SetActiveWindow(s_hwnd);
7582
7583 return FALSE;
7584}
7585#endif
7586
7587
7588/*
7589 * Decide whether to use the "new look" (small, non-bold font) or the "old
7590 * look" (big, clanky font) for dialogs, and work out a few values for use
7591 * later accordingly.
7592 */
7593 static void
7594get_dialog_font_metrics(void)
7595{
7596 HDC hdc;
7597 HFONT hfontTools = 0;
7598 DWORD dlgFontSize;
7599 SIZE size;
7600#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007601 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602#endif
7603
7604 s_usenewlook = FALSE;
7605
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007607 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007608 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610#endif
7611 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007612 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007614 if (hfontTools)
7615 {
7616 hdc = GetDC(s_hwnd);
7617 SelectObject(hdc, hfontTools);
7618 /*
7619 * GetTextMetrics() doesn't return the right value in
7620 * tmAveCharWidth, so we have to figure out the dialog base units
7621 * ourselves.
7622 */
7623 GetTextExtentPoint(hdc,
7624 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7625 52, &size);
7626 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007628 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7629 s_dlgfntheight = (WORD)size.cy;
7630 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632
7633 if (!s_usenewlook)
7634 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007635 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 s_dlgfntwidth = LOWORD(dlgFontSize);
7637 s_dlgfntheight = HIWORD(dlgFontSize);
7638 }
7639}
7640
7641#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7642/*
7643 * Create a pseudo-"tearoff menu" based on the child
7644 * items of a given menu pointer.
7645 */
7646 static void
7647gui_mch_tearoff(
7648 char_u *title,
7649 vimmenu_T *menu,
7650 int initX,
7651 int initY)
7652{
7653 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7654 int template_len;
7655 int nchar, textWidth, submenuWidth;
7656 DWORD lStyle;
7657 DWORD lExtendedStyle;
7658 WORD dlgwidth;
7659 WORD menuID;
7660 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007661 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 vimmenu_T *the_menu = menu;
7663 HWND hwnd;
7664 HDC hdc;
7665 HFONT font, oldFont;
7666 int col, spaceWidth, len;
7667 int columnWidths[2];
7668 char_u *label, *text;
7669 int acLen = 0;
7670 int nameLen;
7671 int padding0, padding1, padding2 = 0;
7672 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007673 int x;
7674 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007675# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007676 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
7680 /*
7681 * If this menu is already torn off, move it to the mouse position.
7682 */
7683 if (IsWindow(menu->tearoff_handle))
7684 {
7685 POINT mp;
7686 if (GetCursorPos((LPPOINT)&mp))
7687 {
7688 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7689 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7690 }
7691 return;
7692 }
7693
7694 /*
7695 * Create a new tearoff.
7696 */
7697 if (*title == MNU_HIDDEN_CHAR)
7698 title++;
7699
Bram Moolenaar734a8672019-12-02 22:49:38 +01007700 // Allocate memory to store the dialog template. It's made bigger when
7701 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 template_len = DLG_ALLOC_SIZE;
7703 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7704 if (p == NULL)
7705 return;
7706
7707 hwnd = GetDesktopWindow();
7708 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007709# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7711 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007712 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 use_lfSysmenu = TRUE;
7714 }
7715 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007718 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (s_usenewlook)
7720 oldFont = SelectFont(hdc, font);
7721 else
7722 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7723
Bram Moolenaar734a8672019-12-02 22:49:38 +01007724 // Calculate width of a single space. Used for padding columns to the
7725 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007726 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
Bram Moolenaar734a8672019-12-02 22:49:38 +01007728 // Figure out max width of the text column, the accelerator column and the
7729 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 submenuWidth = 0;
7731 for (col = 0; col < 2; col++)
7732 {
7733 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007734 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007736 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 text = (col == 0) ? pmenu->dname : pmenu->actext;
7738 if (text != NULL && *text != NUL)
7739 {
7740 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7741 if (textWidth > columnWidths[col])
7742 columnWidths[col] = textWidth;
7743 }
7744 if (pmenu->children != NULL)
7745 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7746 }
7747 }
7748 if (columnWidths[1] == 0)
7749 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007750 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if (submenuWidth != 0)
7752 columnWidths[0] += submenuWidth;
7753 else
7754 columnWidths[0] += spaceWidth;
7755 }
7756 else
7757 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007758 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7760 columnWidths[1] += submenuWidth;
7761 }
7762
7763 /*
7764 * Now find the total width of our 'menu'.
7765 */
7766 textWidth = columnWidths[0] + columnWidths[1];
7767 if (submenuWidth != 0)
7768 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007769 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7771 textWidth += submenuWidth;
7772 }
7773 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7774 if (textWidth > dlgwidth)
7775 dlgwidth = textWidth;
7776 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7777
Bram Moolenaar734a8672019-12-02 22:49:38 +01007778 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 if (s_usenewlook)
7780 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7781 else
7782 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7783
7784 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7785 *p++ = LOWORD(lStyle);
7786 *p++ = HIWORD(lStyle);
7787 *p++ = LOWORD(lExtendedStyle);
7788 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007789 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007791 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007793 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 else
7795 *p++ = PixelToDialogX(initX); // x
7796 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007797 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 else
7799 *p++ = PixelToDialogY(initY); // y
7800 *p++ = PixelToDialogX(dlgwidth); // cx
7801 ptrueheight = p;
7802 *p++ = 0; // dialog height: changed later anyway
7803 *p++ = 0; // Menu
7804 *p++ = 0; // Class
7805
Bram Moolenaar734a8672019-12-02 22:49:38 +01007806 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007808 ? (LPSTR)title
7809 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 p += nchar;
7811
7812 if (s_usenewlook)
7813 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007814 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007815# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (use_lfSysmenu)
7817 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007818 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7820 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007821 wcscpy(p, lfSysmenu.lfFaceName);
7822 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
7824 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
7827 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007828 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 p += nchar;
7831 }
7832
7833 /*
7834 * Loop over all the items in the menu.
7835 * But skip over the tearbar.
7836 */
7837 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7838 menu = menu->children->next;
7839 else
7840 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007841 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 for ( ; menu != NULL; menu = menu->next)
7843 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007844 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 continue;
7846 if (menu_is_separator(menu->dname))
7847 {
7848 sepPadding += 3;
7849 continue;
7850 }
7851
Bram Moolenaar734a8672019-12-02 22:49:38 +01007852 // Check if there still is plenty of room in the template. Make it
7853 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7855 {
7856 WORD *newp;
7857
7858 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7859 if (newp != NULL)
7860 {
7861 template_len += 4096;
7862 mch_memmove(newp, pdlgtemplate,
7863 (char *)p - (char *)pdlgtemplate);
7864 p = newp + (p - pdlgtemplate);
7865 pnumitems = newp + (pnumitems - pdlgtemplate);
7866 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7867 LocalFree(LocalHandle(pdlgtemplate));
7868 pdlgtemplate = newp;
7869 }
7870 }
7871
Bram Moolenaar734a8672019-12-02 22:49:38 +01007872 // Figure out minimal length of this menu label. Use "name" for the
7873 // actual text, "dname" for estimating the displayed size. "name"
7874 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 len = nameLen = (int)STRLEN(menu->name);
7876 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7877 (int)STRLEN(menu->dname))) / spaceWidth;
7878 len += padding0;
7879
7880 if (menu->actext != NULL)
7881 {
7882 acLen = (int)STRLEN(menu->actext);
7883 len += acLen;
7884 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7885 }
7886 else
7887 textWidth = 0;
7888 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7889 len += padding1;
7890
7891 if (menu->children == NULL)
7892 {
7893 padding2 = submenuWidth / spaceWidth;
7894 len += padding2;
7895 menuID = (WORD)(menu->id);
7896 }
7897 else
7898 {
7899 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007900 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902
Bram Moolenaar734a8672019-12-02 22:49:38 +01007903 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007904 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 if (label == NULL)
7906 break;
7907
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007908 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007909 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007911 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 while (padding0-- > 0)
7913 *text++ = ' ';
7914 if (menu->actext != NULL)
7915 {
7916 STRNCPY(text, menu->actext, acLen);
7917 text += acLen;
7918 }
7919 while (padding1-- > 0)
7920 *text++ = ' ';
7921 if (menu->children != NULL)
7922 {
7923 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7924 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7925 }
7926 else
7927 {
7928 while (padding2-- > 0)
7929 *text++ = ' ';
7930 }
7931 *text = NUL;
7932
7933 /*
7934 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7935 * W95/NT4 it makes the tear-off look more like a menu.
7936 */
7937 p = add_dialog_element(p,
7938 BS_PUSHBUTTON|BS_LEFT,
7939 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7940 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7941 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7942 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007943 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 vim_free(label);
7945 (*pnumitems)++;
7946 }
7947
7948 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7949
7950
Bram Moolenaar734a8672019-12-02 22:49:38 +01007951 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007952 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007953 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 (LPDLGTEMPLATE)pdlgtemplate,
7955 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007956 (DLGPROC)tearoff_callback,
7957 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
7959 LocalFree(LocalHandle(pdlgtemplate));
7960 SelectFont(hdc, oldFont);
7961 DeleteObject(font);
7962 ReleaseDC(hwnd, hdc);
7963
7964 /*
7965 * Reassert ourselves as the active window. This is so that after creating
7966 * a tearoff, the user doesn't have to click with the mouse just to start
7967 * typing again!
7968 */
7969 (void)SetActiveWindow(s_hwnd);
7970
Bram Moolenaar734a8672019-12-02 22:49:38 +01007971 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 force_menu_update = TRUE;
7973}
7974#endif
7975
7976#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007977# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
Bram Moolenaar734a8672019-12-02 22:49:38 +01007979// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980# ifndef TBSTYLE_FLAT
7981# define TBSTYLE_FLAT 0x0800
7982# endif
7983
7984/*
7985 * Create the toolbar, initially unpopulated.
7986 * (just like the menu, there are no defaults, it's all
7987 * set up through menu.vim)
7988 */
7989 static void
7990initialise_toolbar(void)
7991{
7992 InitCommonControls();
7993 s_toolbarhwnd = CreateToolbarEx(
7994 s_hwnd,
7995 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7996 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007997 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007998 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 IDR_TOOLBAR1, // id of initial bitmap
8000 NULL,
8001 0, // initial number of buttons
8002 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8003 TOOLBAR_BUTTON_HEIGHT,
8004 TOOLBAR_BUTTON_WIDTH,
8005 TOOLBAR_BUTTON_HEIGHT,
8006 sizeof(TBBUTTON)
8007 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008008
8009 // Remove transparency from the toolbar to prevent the main window
8010 // background colour showing through
8011 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8012 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8013
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008014 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015
8016 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008017
8018 update_toolbar_size();
8019}
8020
8021 static void
8022update_toolbar_size(void)
8023{
8024 int w, h;
8025 TBMETRICS tbm = {sizeof(TBMETRICS)};
8026
8027 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8028 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8029 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8030 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8031
8032 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8033 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8034 //TRACE("button size: %d, %d", w, h);
8035 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8036 gui.toolbar_height = h + 6;
8037
8038 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8039 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8040
8041 // TODO:
8042 // Currently, this function only updates the size of toolbar buttons.
8043 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044}
8045
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008046 static LRESULT CALLBACK
8047toolbar_wndproc(
8048 HWND hwnd,
8049 UINT uMsg,
8050 WPARAM wParam,
8051 LPARAM lParam)
8052{
8053 HandleMouseHide(uMsg, lParam);
8054 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8055}
8056
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 static int
8058get_toolbar_bitmap(vimmenu_T *menu)
8059{
8060 int i = -1;
8061
8062 /*
8063 * Check user bitmaps first, unless builtin is specified.
8064 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008065 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {
8067 char_u fname[MAXPATHL];
8068 HANDLE hbitmap = NULL;
8069
8070 if (menu->iconfile != NULL)
8071 {
8072 gui_find_iconfile(menu->iconfile, fname, "bmp");
8073 hbitmap = LoadImage(
8074 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008075 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 IMAGE_BITMAP,
8077 TOOLBAR_BUTTON_WIDTH,
8078 TOOLBAR_BUTTON_HEIGHT,
8079 LR_LOADFROMFILE |
8080 LR_LOADMAP3DCOLORS
8081 );
8082 }
8083
8084 /*
8085 * If the LoadImage call failed, or the "icon=" file
8086 * didn't exist or wasn't specified, try the menu name
8087 */
8088 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008089 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008090# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008091 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008092# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008093 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 hbitmap = LoadImage(
8095 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008096 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 IMAGE_BITMAP,
8098 TOOLBAR_BUTTON_WIDTH,
8099 TOOLBAR_BUTTON_HEIGHT,
8100 LR_LOADFROMFILE |
8101 LR_LOADMAP3DCOLORS
8102 );
8103
8104 if (hbitmap != NULL)
8105 {
8106 TBADDBITMAP tbAddBitmap;
8107
8108 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008109 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
8111 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8112 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008113 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 }
8115 }
8116 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8117 i = menu->iconidx;
8118
8119 return i;
8120}
8121#endif
8122
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008123#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8124 static void
8125initialise_tabline(void)
8126{
8127 InitCommonControls();
8128
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008129 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008130 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008131 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008132 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008133 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008134
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008135 gui.tabline_height = TABLINE_HEIGHT;
8136
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008137 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008138}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008139
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008140/*
8141 * Get tabpage_T from POINT.
8142 */
8143 static tabpage_T *
8144GetTabFromPoint(
8145 HWND hWnd,
8146 POINT pt)
8147{
8148 tabpage_T *ptp = NULL;
8149
8150 if (gui_mch_showing_tabline())
8151 {
8152 TCHITTESTINFO htinfo;
8153 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008154 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008155 if (s_tabhwnd == hWnd)
8156 {
8157 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8158 if (idx != -1)
8159 ptp = find_tabpage(idx + 1);
8160 }
8161 }
8162 return ptp;
8163}
8164
8165static POINT s_pt = {0, 0};
8166static HCURSOR s_hCursor = NULL;
8167
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008168 static LRESULT CALLBACK
8169tabline_wndproc(
8170 HWND hwnd,
8171 UINT uMsg,
8172 WPARAM wParam,
8173 LPARAM lParam)
8174{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008175 POINT pt;
8176 tabpage_T *tp;
8177 RECT rect;
8178 int nCenter;
8179 int idx0;
8180 int idx1;
8181
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008182 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008183
8184 switch (uMsg)
8185 {
8186 case WM_LBUTTONDOWN:
8187 {
8188 s_pt.x = GET_X_LPARAM(lParam);
8189 s_pt.y = GET_Y_LPARAM(lParam);
8190 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008191 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008192 break;
8193 }
8194 case WM_MOUSEMOVE:
8195 if (GetCapture() == hwnd
8196 && ((wParam & MK_LBUTTON)) != 0)
8197 {
8198 pt.x = GET_X_LPARAM(lParam);
8199 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008200 if (abs(pt.x - s_pt.x) >
8201 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008202 {
8203 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8204
8205 tp = GetTabFromPoint(hwnd, pt);
8206 if (tp != NULL)
8207 {
8208 idx0 = tabpage_index(curtab) - 1;
8209 idx1 = tabpage_index(tp) - 1;
8210
8211 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8212 nCenter = rect.left + (rect.right - rect.left) / 2;
8213
Bram Moolenaar734a8672019-12-02 22:49:38 +01008214 // Check if the mouse cursor goes over the center of
8215 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008216 if ((idx0 < idx1) && (nCenter < pt.x))
8217 {
8218 tabpage_move(idx1 + 1);
8219 update_screen(0);
8220 }
8221 else if ((idx1 < idx0) && (pt.x < nCenter))
8222 {
8223 tabpage_move(idx1);
8224 update_screen(0);
8225 }
8226 }
8227 }
8228 }
8229 break;
8230 case WM_LBUTTONUP:
8231 {
8232 if (GetCapture() == hwnd)
8233 {
8234 SetCursor(s_hCursor);
8235 ReleaseCapture();
8236 }
8237 break;
8238 }
8239 default:
8240 break;
8241 }
8242
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008243 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8244}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008245#endif
8246
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8248/*
8249 * Make the GUI window come to the foreground.
8250 */
8251 void
8252gui_mch_set_foreground(void)
8253{
8254 if (IsIconic(s_hwnd))
8255 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8256 SetForegroundWindow(s_hwnd);
8257}
8258#endif
8259
8260#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8261 static void
8262dyn_imm_load(void)
8263{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008264 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (hLibImm == NULL)
8266 return;
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 pImmGetCompositionStringW
8269 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8270 pImmGetContext
8271 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8272 pImmAssociateContext
8273 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8274 pImmReleaseContext
8275 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8276 pImmGetOpenStatus
8277 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8278 pImmSetOpenStatus
8279 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008280 pImmGetCompositionFontW
8281 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8282 pImmSetCompositionFontW
8283 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 pImmSetCompositionWindow
8285 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8286 pImmGetConversionStatus
8287 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008288 pImmSetConversionStatus
8289 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290
K.Takatab0b2b732022-01-19 12:59:21 +00008291 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 || pImmGetContext == NULL
8293 || pImmAssociateContext == NULL
8294 || pImmReleaseContext == NULL
8295 || pImmGetOpenStatus == NULL
8296 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008297 || pImmGetCompositionFontW == NULL
8298 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008300 || pImmGetConversionStatus == NULL
8301 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 FreeLibrary(hLibImm);
8304 hLibImm = NULL;
8305 pImmGetContext = NULL;
8306 return;
8307 }
8308
8309 return;
8310}
8311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312#endif
8313
8314#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8315
8316# ifdef FEAT_XPM_W32
8317# define IMAGE_XPM 100
8318# endif
8319
8320typedef struct _signicon_t
8321{
8322 HANDLE hImage;
8323 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008324# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008325 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327} signicon_t;
8328
8329 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008330gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 signicon_t *sign;
8333 int x, y, w, h;
8334
8335 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8336 return;
8337
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008338# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008339 if (IS_ENABLE_DIRECTX())
8340 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008341# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008342
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 x = TEXT_X(col);
8344 y = TEXT_Y(row);
8345 w = gui.char_width * 2;
8346 h = gui.char_height;
8347 switch (sign->uType)
8348 {
8349 case IMAGE_BITMAP:
8350 {
8351 HDC hdcMem;
8352 HBITMAP hbmpOld;
8353
8354 hdcMem = CreateCompatibleDC(s_hdc);
8355 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8356 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8357 SelectObject(hdcMem, hbmpOld);
8358 DeleteDC(hdcMem);
8359 }
8360 break;
8361 case IMAGE_ICON:
8362 case IMAGE_CURSOR:
8363 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8364 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008365# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 case IMAGE_XPM:
8367 {
8368 HDC hdcMem;
8369 HBITMAP hbmpOld;
8370
8371 hdcMem = CreateCompatibleDC(s_hdc);
8372 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008373 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8375
8376 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008377 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8379 SelectObject(hdcMem, hbmpOld);
8380 DeleteDC(hdcMem);
8381 }
8382 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385}
8386
8387 static void
8388close_signicon_image(signicon_t *sign)
8389{
8390 if (sign)
8391 switch (sign->uType)
8392 {
8393 case IMAGE_BITMAP:
8394 DeleteObject((HGDIOBJ)sign->hImage);
8395 break;
8396 case IMAGE_CURSOR:
8397 DestroyCursor((HCURSOR)sign->hImage);
8398 break;
8399 case IMAGE_ICON:
8400 DestroyIcon((HICON)sign->hImage);
8401 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008402# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 case IMAGE_XPM:
8404 DeleteObject((HBITMAP)sign->hImage);
8405 DeleteObject((HBITMAP)sign->hShape);
8406 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 }
8409}
8410
8411 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008412gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
8414 signicon_t sign, *psign;
8415 char_u *ext;
8416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008418 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if (ext > signfile)
8420 {
8421 int do_load = 1;
8422
8423 if (!STRICMP(ext, ".bmp"))
8424 sign.uType = IMAGE_BITMAP;
8425 else if (!STRICMP(ext, ".ico"))
8426 sign.uType = IMAGE_ICON;
8427 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8428 sign.uType = IMAGE_CURSOR;
8429 else
8430 do_load = 0;
8431
8432 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008433 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 gui.char_width * 2, gui.char_height,
8435 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008436# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 if (!STRICMP(ext, ".xpm"))
8438 {
8439 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008440 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8441 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 }
8445
8446 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008447 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 *psign = sign;
8449
8450 if (!psign)
8451 {
8452 if (sign.hImage)
8453 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008454 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 return (void *)psign;
8457
8458}
8459
8460 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008461gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
8463 if (sign)
8464 {
8465 close_signicon_image((signicon_t *)sign);
8466 vim_free(sign);
8467 }
8468}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008471#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
Bram Moolenaar734a8672019-12-02 22:49:38 +01008473/*
8474 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008475 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008477 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8479 * to get current mouse position).
8480 *
8481 * Trying to use as more Windows services as possible, and as less
8482 * IE version as possible :)).
8483 *
8484 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8485 * BalloonEval struct.
8486 * 2) Enable/Disable simply create/kill BalloonEval Timer
8487 * 3) When there was enough inactivity, timer procedure posts
8488 * async request to debugger
8489 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8490 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008491 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 */
8493
Bram Moolenaar45360022005-07-21 21:08:21 +00008494/*
8495 * determine whether installed Common Controls support multiline tooltips
8496 * (i.e. their version is >= 4.70
8497 */
8498 int
8499multiline_balloon_available(void)
8500{
8501 HINSTANCE hDll;
8502 static char comctl_dll[] = "comctl32.dll";
8503 static int multiline_tip = MAYBE;
8504
8505 if (multiline_tip != MAYBE)
8506 return multiline_tip;
8507
8508 hDll = GetModuleHandle(comctl_dll);
8509 if (hDll != NULL)
8510 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008511 DLLGETVERSIONPROC pGetVer;
8512 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008513
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008514 if (pGetVer != NULL)
8515 {
8516 DLLVERSIONINFO dvi;
8517 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008518
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008519 ZeroMemory(&dvi, sizeof(dvi));
8520 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008521
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008522 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008523
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008524 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008525 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008526 || (dvi.dwMajorVersion == 4
8527 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008528 {
8529 multiline_tip = TRUE;
8530 return multiline_tip;
8531 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008532 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008533 else
8534 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008535 // there is chance we have ancient CommCtl 4.70
8536 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008537 DWORD dwHandle = 0;
8538 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8539 if (len > 0)
8540 {
8541 VS_FIXEDFILEINFO *ver;
8542 UINT vlen = 0;
8543 void *data = alloc(len);
8544
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008545 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008546 && GetFileVersionInfo(comctl_dll, 0, len, data)
8547 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8548 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008549 && HIWORD(ver->dwFileVersionMS) > 4)
8550 || ((HIWORD(ver->dwFileVersionMS) == 4
8551 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008552 {
8553 vim_free(data);
8554 multiline_tip = TRUE;
8555 return multiline_tip;
8556 }
8557 vim_free(data);
8558 }
8559 }
8560 }
8561 multiline_tip = FALSE;
8562 return multiline_tip;
8563}
8564
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008565 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008566make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008567{
8568 TOOLINFOW *pti;
8569 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008570
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008571 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008572 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8573 else
8574 ToolInfoSize = sizeof(TOOLINFOW);
8575
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008576 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008577 if (pti == NULL)
8578 return;
8579
8580 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8581 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8582 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008583 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008584
8585 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8586 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8587
8588 pti->cbSize = ToolInfoSize;
8589 pti->uFlags = TTF_SUBCLASS;
8590 pti->hwnd = beval->target;
8591 pti->hinst = 0; // Don't use string resources
8592 pti->uId = ID_BEVAL_TOOLTIP;
8593
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008594 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008595 {
8596 RECT rect;
8597 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8598 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008599 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8600 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008601 // switch multiline tooltips on
8602 if (GetClientRect(s_textArea, &rect))
8603 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8604 (LPARAM)rect.right);
8605 }
8606 else
8607 {
8608 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008609 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8610 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008611 }
8612
8613 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8614 pti->rect.left = pt.x - 3;
8615 pti->rect.top = pt.y - 3;
8616 pti->rect.right = pt.x + 3;
8617 pti->rect.bottom = pt.y + 3;
8618
8619 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8620 // Make tooltip appear sooner.
8621 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8622 // I've performed some tests and it seems the longest possible life time
8623 // of tooltip is 30 seconds.
8624 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8625 /*
8626 * HACK: force tooltip to appear, because it'll not appear until
8627 * first mouse move. D*mn M$
8628 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8629 */
8630 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8631 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8632 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008633}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008634
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008636delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008638 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008642BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008643 HWND hwnd UNUSED,
8644 UINT uMsg UNUSED,
8645 UINT_PTR idEvent UNUSED,
8646 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 POINT pt;
8649 RECT rect;
8650
8651 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8652 return;
8653
8654 GetCursorPos(&pt);
8655 if (WindowFromPoint(pt) != s_textArea)
8656 return;
8657
8658 ScreenToClient(s_textArea, &pt);
8659 GetClientRect(s_textArea, &rect);
8660 if (!PtInRect(&rect, pt))
8661 return;
8662
8663 if (LastActivity > 0
8664 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8665 && (cur_beval->showState != ShS_PENDING
8666 || abs(cur_beval->x - pt.x) > 3
8667 || abs(cur_beval->y - pt.y) > 3))
8668 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008669 // Pointer resting in one place long enough, it's time to show
8670 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 cur_beval->showState = ShS_PENDING;
8672 cur_beval->x = pt.x;
8673 cur_beval->y = pt.y;
8674
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008675 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676
8677 if (cur_beval->msgCB != NULL)
8678 (*cur_beval->msgCB)(cur_beval, 0);
8679 }
8680}
8681
8682 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008683gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008685 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008687 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688}
8689
8690 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008691gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008693 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (beval == NULL)
8695 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008696 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008697 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008698 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699}
8700
8701 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008702gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
8704 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008705
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008706 vim_free(beval->msg);
8707 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8708 if (beval->msg == NULL)
8709 {
8710 delete_tooltip(beval);
8711 beval->showState = ShS_NEUTRAL;
8712 return;
8713 }
8714
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008715 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 if (beval->showState == ShS_SHOWING)
8717 return;
8718 GetCursorPos(&pt);
8719 ScreenToClient(s_textArea, &pt);
8720
8721 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008723 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 gui_mch_disable_beval_area(cur_beval);
8725 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008726 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008728 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729}
8730
8731 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008732gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008733 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008734 char_u *mesg,
8735 void (*mesgCB)(BalloonEval *, int),
8736 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008738 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 BalloonEval *beval;
8740
8741 if (mesg != NULL && mesgCB != NULL)
8742 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008743 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 return NULL;
8745 }
8746
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008747 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (beval != NULL)
8749 {
8750 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 beval->msg = mesg;
8754 beval->msgCB = mesgCB;
8755 beval->clientData = clientData;
8756
8757 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 cur_beval = beval;
8759
8760 if (p_beval)
8761 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 }
8763 return beval;
8764}
8765
8766 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008767Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008769 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 return;
8771
8772 if (cur_beval != NULL)
8773 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008774 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008776 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008777 // TRACE0("TTN_SHOW {{{");
8778 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008779 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008780 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008781 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 delete_tooltip(cur_beval);
8783 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008784 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008787 break;
8788 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008789 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008790 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008791 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8792 info->lpszText = (LPSTR)info->lParam;
8793 info->uFlags |= TTF_DI_SETITEM;
8794 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008795 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008796 case TTN_GETDISPINFOW:
8797 {
8798 // if we get here then we have new common controls
8799 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8800 info->lpszText = (LPWSTR)info->lParam;
8801 info->uFlags |= TTF_DI_SETITEM;
8802 }
8803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 }
8805 }
8806}
8807
8808 static void
8809TrackUserActivity(UINT uMsg)
8810{
8811 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8812 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8813 LastActivity = GetTickCount();
8814}
8815
8816 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008817gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008819# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008820 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008821# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008822 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 vim_free(beval);
8824}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008825#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8828/*
8829 * We have multiple signs to draw at the same location. Draw the
8830 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8831 */
8832 void
8833netbeans_draw_multisign_indicator(int row)
8834{
8835 int i;
8836 int y;
8837 int x;
8838
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008839 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008840 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008841
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 x = 0;
8843 y = TEXT_Y(row);
8844
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008845# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008846 if (IS_ENABLE_DIRECTX())
8847 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008848# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 for (i = 0; i < gui.char_height - 3; i++)
8851 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8852
8853 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8854 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8855 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8856 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8857 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8858 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8859 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8860}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008861#endif