blob: 5f895c0b21fdf02acfb9309dc2a76fee66028960 [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 Moolenaar912bc4a2019-12-01 18:58:11 +0100200# ifdef GLOBAL_IME
201# include "glbl_ime.h"
202# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
213#define DLG_OLD_STYLE_PADDING_X 5
214#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100215#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100216#define DLG_VERT_PADDING_Y 4
217#define DLG_ICON_WIDTH 34
218#define DLG_ICON_HEIGHT 34
219#define DLG_MIN_WIDTH 150
220#define DLG_FONT_NAME "MS Sans Serif"
221#define DLG_FONT_POINT_SIZE 8
222#define DLG_MIN_MAX_WIDTH 400
223#define DLG_MIN_MAX_HEIGHT 400
224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226
Bram Moolenaar734a8672019-12-02 22:49:38 +0100227#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100228# define WM_XBUTTONDOWN 0x020B
229# define WM_XBUTTONUP 0x020C
230# define WM_XBUTTONDBLCLK 0x020D
231# define MK_XBUTTON1 0x0020
232# define MK_XBUTTON2 0x0040
233#endif
234
K.Takatac81e9bf2022-01-16 14:15:49 +0000235#ifndef WM_DPICHANGED
236# define WM_DPICHANGED 0x02E0
237#endif
238
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100239#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241 * Define a few things for generating prototypes. This is just to avoid
242 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100244# define APIENTRY
245# define CALLBACK
246# define CONST
247# define FAR
248# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200249# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200250# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100251# define _cdecl
252typedef int BOOL;
253typedef int BYTE;
254typedef int DWORD;
255typedef int WCHAR;
256typedef int ENUMLOGFONT;
257typedef int FINDREPLACE;
258typedef int HANDLE;
259typedef int HBITMAP;
260typedef int HBRUSH;
261typedef int HDROP;
262typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100263typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100264typedef int LPARAM;
265typedef int LPCREATESTRUCT;
266typedef int LPCSTR;
267typedef int LPCTSTR;
268typedef int LPRECT;
269typedef int LPSTR;
270typedef int LPWINDOWPOS;
271typedef int LPWORD;
272typedef int LRESULT;
273typedef int HRESULT;
274# undef MSG
275typedef int MSG;
276typedef int NEWTEXTMETRIC;
277typedef int OSVERSIONINFO;
278typedef int PWORD;
279typedef int RECT;
280typedef int UINT;
281typedef int WORD;
282typedef int WPARAM;
283typedef int POINT;
284typedef void *HINSTANCE;
285typedef void *HMENU;
286typedef void *HWND;
287typedef void *HDC;
288typedef void VOID;
289typedef int LPNMHDR;
290typedef int LONG;
291typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200292typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200293typedef int COLORREF;
294typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100295#endif
296
297#ifndef GET_X_LPARAM
298# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
299#endif
300
301static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100302static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303static void clear_rect(RECT *rcp);
304
Bram Moolenaar734a8672019-12-02 22:49:38 +0100305static WORD s_dlgfntheight; // height of the dialog font
306static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307
308#ifdef FEAT_MENU
309static HMENU s_menuBar = NULL;
310#endif
311#ifdef FEAT_TEAROFF
312static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100313static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314#endif
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316// Flag that is set while processing a message that must not be interrupted by
317// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static int s_busy_processing = FALSE;
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321
322#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000323static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200324static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200326static int s_findrep_is_find; // TRUE for find dialog, FALSE
327 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100328#endif
329
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330HWND s_hwnd = NULL;
331static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100332static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100333
334#ifdef FEAT_TOOLBAR
335static HWND s_toolbarhwnd = NULL;
336static WNDPROC s_toolbar_wndproc = NULL;
337#endif
338
339#ifdef FEAT_GUI_TABLINE
340static HWND s_tabhwnd = NULL;
341static WNDPROC s_tabline_wndproc = NULL;
342static int showing_tabline = 0;
343#endif
344
345static WPARAM s_wParam = 0;
346static LPARAM s_lParam = 0;
347
348static HWND s_textArea = NULL;
349static UINT s_uMsg = 0;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100352
353static int s_need_activate = FALSE;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355// This variable is set when waiting for an event, which is the only moment
356// scrollbar dragging can be done directly. It's not allowed while commands
357// are executed, because it may move the cursor and that may cause unexpected
358// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100359static int allow_scrollbar = FALSE;
360
361#ifdef GLOBAL_IME
362# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
363#else
364# define MyTranslateMessage(x) TranslateMessage(x)
365#endif
366
K.Takatac81e9bf2022-01-16 14:15:49 +0000367#ifndef _DPI_AWARENESS_CONTEXTS_
368typedef HANDLE DPI_AWARENESS_CONTEXT;
369
370typedef enum DPI_AWARENESS {
371 DPI_AWARENESS_INVALID = -1,
372 DPI_AWARENESS_UNAWARE = 0,
373 DPI_AWARENESS_SYSTEM_AWARE = 1,
374 DPI_AWARENESS_PER_MONITOR_AWARE = 2
375} DPI_AWARENESS;
376
377# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
378# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
379# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
380# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
381# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
382#endif
383
384#define DEFAULT_DPI 96
385static int s_dpi = DEFAULT_DPI;
386static BOOL s_in_dpichanged = FALSE;
387static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
388
389static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
390static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
391static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
392//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
393static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
394static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
395
396 static UINT WINAPI
397stubGetDpiForSystem(void)
398{
399 HWND hwnd = GetDesktopWindow();
400 HDC hdc = GetWindowDC(hwnd);
401 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
402 ReleaseDC(hwnd, hdc);
403 return dpi;
404}
405
406 static int WINAPI
407stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
408{
409 return GetSystemMetrics(nIndex);
410}
411
412 static int
413adjust_fontsize_by_dpi(int size)
414{
415 return size * s_dpi / (int)pGetDpiForSystem();
416}
417
418 static int
419adjust_by_system_dpi(int size)
420{
421 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
422}
423
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100424#if defined(FEAT_DIRECTX)
425 static int
426directx_enabled(void)
427{
428 if (s_dwc != NULL)
429 return 1;
430 else if (s_directx_load_attempted)
431 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100432 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100433 DWrite_Init();
434 s_directx_load_attempted = 1;
435 s_dwc = DWriteContext_Open();
436 directx_binddc();
437 return s_dwc != NULL ? 1 : 0;
438}
439
440 static void
441directx_binddc(void)
442{
443 if (s_textArea != NULL)
444 {
445 RECT rect;
446 GetClientRect(s_textArea, &rect);
447 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
448 }
449}
450#endif
451
Bram Moolenaar734a8672019-12-02 22:49:38 +0100452// use of WindowProc depends on Global IME
Bram Moolenaar945c8572020-07-17 22:17:03 +0200453static LRESULT WINAPI MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100454
Bram Moolenaar734a8672019-12-02 22:49:38 +0100455extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100456
457static struct
458{
459 UINT key_sym;
460 char_u vim_code0;
461 char_u vim_code1;
462} special_keys[] =
463{
464 {VK_UP, 'k', 'u'},
465 {VK_DOWN, 'k', 'd'},
466 {VK_LEFT, 'k', 'l'},
467 {VK_RIGHT, 'k', 'r'},
468
469 {VK_F1, 'k', '1'},
470 {VK_F2, 'k', '2'},
471 {VK_F3, 'k', '3'},
472 {VK_F4, 'k', '4'},
473 {VK_F5, 'k', '5'},
474 {VK_F6, 'k', '6'},
475 {VK_F7, 'k', '7'},
476 {VK_F8, 'k', '8'},
477 {VK_F9, 'k', '9'},
478 {VK_F10, 'k', ';'},
479
480 {VK_F11, 'F', '1'},
481 {VK_F12, 'F', '2'},
482 {VK_F13, 'F', '3'},
483 {VK_F14, 'F', '4'},
484 {VK_F15, 'F', '5'},
485 {VK_F16, 'F', '6'},
486 {VK_F17, 'F', '7'},
487 {VK_F18, 'F', '8'},
488 {VK_F19, 'F', '9'},
489 {VK_F20, 'F', 'A'},
490
491 {VK_F21, 'F', 'B'},
492#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100493 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494#endif
495 {VK_F22, 'F', 'C'},
496 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100497 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100498
499 {VK_HELP, '%', '1'},
500 {VK_BACK, 'k', 'b'},
501 {VK_INSERT, 'k', 'I'},
502 {VK_DELETE, 'k', 'D'},
503 {VK_HOME, 'k', 'h'},
504 {VK_END, '@', '7'},
505 {VK_PRIOR, 'k', 'P'},
506 {VK_NEXT, 'k', 'N'},
507 {VK_PRINT, '%', '9'},
508 {VK_ADD, 'K', '6'},
509 {VK_SUBTRACT, 'K', '7'},
510 {VK_DIVIDE, 'K', '8'},
511 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100512 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100513 {VK_DECIMAL, 'K', 'B'},
514
515 {VK_NUMPAD0, 'K', 'C'},
516 {VK_NUMPAD1, 'K', 'D'},
517 {VK_NUMPAD2, 'K', 'E'},
518 {VK_NUMPAD3, 'K', 'F'},
519 {VK_NUMPAD4, 'K', 'G'},
520 {VK_NUMPAD5, 'K', 'H'},
521 {VK_NUMPAD6, 'K', 'I'},
522 {VK_NUMPAD7, 'K', 'J'},
523 {VK_NUMPAD8, 'K', 'K'},
524 {VK_NUMPAD9, 'K', 'L'},
525
Bram Moolenaar734a8672019-12-02 22:49:38 +0100526 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100527 {VK_SPACE, ' ', NUL},
528 {VK_TAB, TAB, NUL},
529 {VK_ESCAPE, ESC, NUL},
530 {NL, NL, NUL},
531 {CAR, CAR, NUL},
532
Bram Moolenaar734a8672019-12-02 22:49:38 +0100533 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100534 {0, 0, 0}
535};
536
Bram Moolenaar734a8672019-12-02 22:49:38 +0100537// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100538static int s_button_pending = -1;
539
Bram Moolenaar734a8672019-12-02 22:49:38 +0100540// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
541// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542static int s_getting_focus = FALSE;
543
544static int s_x_pending;
545static int s_y_pending;
546static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200547static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200549static int dead_key = 0; // 0: no dead key, 1: dead key pressed
550static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
551 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100552
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100553#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100554// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100555static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
556static void TrackUserActivity(UINT uMsg);
557#endif
558
559/*
560 * For control IME.
561 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100562 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100563 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100564#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100565// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100566static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100567#endif
568#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100569// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100570static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100571#endif
572
573#ifdef FEAT_MBYTE_IME
574static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
575#endif
576
577#if defined(FEAT_BROWSE)
578static char_u *convert_filter(char_u *s);
579#endif
580
581#ifdef DEBUG_PRINT_ERROR
582/*
583 * Print out the last Windows error message
584 */
585 static void
586print_windows_error(void)
587{
588 LPVOID lpMsgBuf;
589
590 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
591 NULL, GetLastError(),
592 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
593 (LPTSTR) &lpMsgBuf, 0, NULL);
594 TRACE1("Error: %s\n", lpMsgBuf);
595 LocalFree(lpMsgBuf);
596}
597#endif
598
599/*
600 * Cursor blink functions.
601 *
602 * This is a simple state machine:
603 * BLINK_NONE not blinking at all
604 * BLINK_OFF blinking, cursor is not shown
605 * BLINK_ON blinking, cursor is shown
606 */
607
608#define BLINK_NONE 0
609#define BLINK_OFF 1
610#define BLINK_ON 2
611
612static int blink_state = BLINK_NONE;
613static long_u blink_waittime = 700;
614static long_u blink_ontime = 400;
615static long_u blink_offtime = 250;
616static UINT blink_timer = 0;
617
Bram Moolenaar703a8042016-06-04 16:24:32 +0200618 int
619gui_mch_is_blinking(void)
620{
621 return blink_state != BLINK_NONE;
622}
623
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200624 int
625gui_mch_is_blink_off(void)
626{
627 return blink_state == BLINK_OFF;
628}
629
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100630 void
631gui_mch_set_blinking(long wait, long on, long off)
632{
633 blink_waittime = wait;
634 blink_ontime = on;
635 blink_offtime = off;
636}
637
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 static VOID CALLBACK
639_OnBlinkTimer(
640 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100641 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100642 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100643 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100644{
645 MSG msg;
646
647 /*
648 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
649 */
650
651 KillTimer(NULL, idEvent);
652
Bram Moolenaar734a8672019-12-02 22:49:38 +0100653 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100654 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
655 ;
656
657 if (blink_state == BLINK_ON)
658 {
659 gui_undraw_cursor();
660 blink_state = BLINK_OFF;
661 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
662 (TIMERPROC)_OnBlinkTimer);
663 }
664 else
665 {
666 gui_update_cursor(TRUE, FALSE);
667 blink_state = BLINK_ON;
668 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100669 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100671 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672}
673
674 static void
675gui_mswin_rm_blink_timer(void)
676{
677 MSG msg;
678
679 if (blink_timer != 0)
680 {
681 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
684 ;
685 blink_timer = 0;
686 }
687}
688
689/*
690 * Stop the cursor blinking. Show the cursor if it wasn't shown.
691 */
692 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100693gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100694{
695 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100696 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100697 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100699 gui_mch_flush();
700 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 blink_state = BLINK_NONE;
702}
703
704/*
705 * Start the cursor blinking. If it was already blinking, this restarts the
706 * waiting time and shows the cursor.
707 */
708 void
709gui_mch_start_blink(void)
710{
711 gui_mswin_rm_blink_timer();
712
Bram Moolenaar734a8672019-12-02 22:49:38 +0100713 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
715 {
716 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
717 (TIMERPROC)_OnBlinkTimer);
718 blink_state = BLINK_ON;
719 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100720 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100721 }
722}
723
724/*
725 * Call-back routines.
726 */
727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100728 static VOID CALLBACK
729_OnTimer(
730 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100731 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100732 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100733 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100734{
735 MSG msg;
736
737 /*
738 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
739 */
740 KillTimer(NULL, idEvent);
741 s_timed_out = TRUE;
742
Bram Moolenaar734a8672019-12-02 22:49:38 +0100743 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
745 ;
746 if (idEvent == s_wait_timer)
747 s_wait_timer = 0;
748}
749
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100750 static void
751_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100752 HWND hwnd UNUSED,
753 UINT ch UNUSED,
754 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755{
756 dead_key = 1;
757}
758
759/*
760 * Convert Unicode character "ch" to bytes in "string[slen]".
761 * When "had_alt" is TRUE the ALT key was included in "ch".
762 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200763 * Because the Windows API uses UTF-16, we have to deal with surrogate
764 * pairs; this is where we choose to deal with them: if "ch" is a high
765 * surrogate, it will be stored, and the length returned will be zero; the next
766 * char_to_string call will then include the high surrogate, decoding the pair
767 * of UTF-16 code units to a single Unicode code point, presuming it is the
768 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100769 */
770 static int
771char_to_string(int ch, char_u *string, int slen, int had_alt)
772{
773 int len;
774 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200776 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100777
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200778 if (surrogate_pending_ch != 0)
779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // We don't guarantee ch is a low surrogate to match the high surrogate
781 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200782 wstring[0] = surrogate_pending_ch;
783 wstring[1] = ch;
784 surrogate_pending_ch = 0;
785 len = 2;
786 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100787 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200788 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100789 // We don't have the entire code point yet, only the first UTF-16 code
790 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200791 surrogate_pending_ch = ch;
792 return 0;
793 }
794 else
795 {
796 wstring[0] = ch;
797 len = 1;
798 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799
Bram Moolenaar734a8672019-12-02 22:49:38 +0100800 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
801 // "enc_codepage" is non-zero use the standard Win32 function,
802 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200803 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100804 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200805 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
806 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100807 // If we had included the ALT key into the character but now the
808 // upper bit is no longer set, that probably means the conversion
809 // failed. Convert the original character and set the upper bit
810 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200811 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 wstring[0] = ch & 0x7f;
814 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
815 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100816 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200817 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100818 }
819 }
820 else
821 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200822 ws = utf16_to_enc(wstring, &len);
823 if (ws == NULL)
824 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825 else
826 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100827 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200828 len = slen;
829 mch_memmove(string, ws, len);
830 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100831 }
832 }
833
834 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100835 {
836 string[0] = ch;
837 len = 1;
838 }
839
840 for (i = 0; i < len; ++i)
841 if (string[i] == CSI && len <= slen - 2)
842 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100843 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
845 string[++i] = KS_EXTRA;
846 string[++i] = (int)KE_CSI;
847 len += 2;
848 }
849
850 return len;
851}
852
853/*
854 * Key hit, add it to the input buffer.
855 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100856 static void
857_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100858 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100859 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100860 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861{
862 char_u string[40];
863 int len = 0;
864
865 dead_key = 0;
866
867 len = char_to_string(ch, string, 40, FALSE);
868 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
869 {
870 trash_input_buf();
871 got_int = TRUE;
872 }
873
874 add_to_input_buf(string, len);
875}
876
877/*
878 * Alt-Key hit, add it to the input buffer.
879 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880 static void
881_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100882 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100883 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100884 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100885{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100886 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100887 int len;
888 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100889 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100890
891 dead_key = 0;
892
Bram Moolenaar734a8672019-12-02 22:49:38 +0100893 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100894
Bram Moolenaar734a8672019-12-02 22:49:38 +0100895 // OK, we have a character key (given by ch) which was entered with the
896 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
897 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
898 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100899 modifiers = MOD_MASK_ALT;
900 if (GetKeyState(VK_SHIFT) & 0x8000)
901 modifiers |= MOD_MASK_SHIFT;
902 if (GetKeyState(VK_CONTROL) & 0x8000)
903 modifiers |= MOD_MASK_CTRL;
904
905 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100906 // remove the SHIFT modifier for keys where it's already included, e.g.,
907 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200908 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200910 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
911 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100912 if (ch == CSI)
913 ch = K_CSI;
914
915 len = 0;
916 if (modifiers)
917 {
918 string[len++] = CSI;
919 string[len++] = KS_MODIFIER;
920 string[len++] = modifiers;
921 }
922
923 if (IS_SPECIAL((int)ch))
924 {
925 string[len++] = CSI;
926 string[len++] = K_SECOND((int)ch);
927 string[len++] = K_THIRD((int)ch);
928 }
929 else
930 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100931 // Although the documentation isn't clear about it, we assume "ch" is
932 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933 len += char_to_string(ch, string + len, 40 - len, TRUE);
934 }
935
936 add_to_input_buf(string, len);
937}
938
939 static void
940_OnMouseEvent(
941 int button,
942 int x,
943 int y,
944 int repeated_click,
945 UINT keyFlags)
946{
947 int vim_modifiers = 0x0;
948
949 s_getting_focus = FALSE;
950
951 if (keyFlags & MK_SHIFT)
952 vim_modifiers |= MOUSE_SHIFT;
953 if (keyFlags & MK_CONTROL)
954 vim_modifiers |= MOUSE_CTRL;
955 if (GetKeyState(VK_MENU) & 0x8000)
956 vim_modifiers |= MOUSE_ALT;
957
958 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
959}
960
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100961 static void
962_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100963 HWND hwnd UNUSED,
964 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100965 int x,
966 int y,
967 UINT keyFlags)
968{
969 static LONG s_prevTime = 0;
970
971 LONG currentTime = GetMessageTime();
972 int button = -1;
973 int repeated_click;
974
Bram Moolenaar734a8672019-12-02 22:49:38 +0100975 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100976 (void)SetFocus(s_hwnd);
977
978 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
979 button = MOUSE_LEFT;
980 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
981 button = MOUSE_MIDDLE;
982 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
983 button = MOUSE_RIGHT;
984 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
985 {
986#ifndef GET_XBUTTON_WPARAM
987# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
988#endif
989 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
990 }
991 else if (s_uMsg == WM_CAPTURECHANGED)
992 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100993 // on W95/NT4, somehow you get in here with an odd Msg
994 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100995 if (s_button_pending == MOUSE_LEFT)
996 button = MOUSE_RIGHT;
997 else
998 button = MOUSE_LEFT;
999 }
1000 if (button >= 0)
1001 {
1002 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1003
1004 /*
1005 * Holding down the left and right buttons simulates pushing the middle
1006 * button.
1007 */
1008 if (repeated_click
1009 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1010 || (button == MOUSE_RIGHT
1011 && s_button_pending == MOUSE_LEFT)))
1012 {
1013 /*
1014 * Hmm, gui.c will ignore more than one button down at a time, so
1015 * pretend we let go of it first.
1016 */
1017 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1018 button = MOUSE_MIDDLE;
1019 repeated_click = FALSE;
1020 s_button_pending = -1;
1021 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1022 }
1023 else if ((repeated_click)
1024 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1025 {
1026 if (s_button_pending > -1)
1027 {
1028 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1029 s_button_pending = -1;
1030 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001031 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001032 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1033 }
1034 else
1035 {
1036 /*
1037 * If this is the first press (i.e. not a multiple click) don't
1038 * action immediately, but store and wait for:
1039 * i) button-up
1040 * ii) mouse move
1041 * iii) another button press
1042 * before using it.
1043 * This enables us to make left+right simulate middle button,
1044 * without left or right being actioned first. The side-effect is
1045 * that if you click and hold the mouse without dragging, the
1046 * cursor doesn't move until you release the button. In practice
1047 * this is hardly a problem.
1048 */
1049 s_button_pending = button;
1050 s_x_pending = x;
1051 s_y_pending = y;
1052 s_kFlags_pending = keyFlags;
1053 }
1054
1055 s_prevTime = currentTime;
1056 }
1057}
1058
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001059 static void
1060_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001061 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062 int x,
1063 int y,
1064 UINT keyFlags)
1065{
1066 int button;
1067
1068 s_getting_focus = FALSE;
1069 if (s_button_pending > -1)
1070 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001071 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001072 _OnMouseEvent(s_button_pending, s_x_pending,
1073 s_y_pending, FALSE, s_kFlags_pending);
1074 s_button_pending = -1;
1075 }
1076 if (s_uMsg == WM_MOUSEMOVE)
1077 {
1078 /*
1079 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1080 * down.
1081 */
1082 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1083 | MK_XBUTTON1 | MK_XBUTTON2)))
1084 {
1085 gui_mouse_moved(x, y);
1086 return;
1087 }
1088
1089 /*
1090 * While button is down, keep grabbing mouse move events when
1091 * the mouse goes outside the window
1092 */
1093 SetCapture(s_textArea);
1094 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001095 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 }
1097 else
1098 {
1099 ReleaseCapture();
1100 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001101 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102 }
1103
1104 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1105}
1106
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001107 static void
1108_OnSizeTextArea(
1109 HWND hwnd UNUSED,
1110 UINT state UNUSED,
1111 int cx UNUSED,
1112 int cy UNUSED)
1113{
1114#if defined(FEAT_DIRECTX)
1115 if (IS_ENABLE_DIRECTX())
1116 directx_binddc();
1117#endif
1118}
1119
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120#ifdef FEAT_MENU
1121/*
1122 * Find the vimmenu_T with the given id
1123 */
1124 static vimmenu_T *
1125gui_mswin_find_menu(
1126 vimmenu_T *pMenu,
1127 int id)
1128{
1129 vimmenu_T *pChildMenu;
1130
1131 while (pMenu)
1132 {
1133 if (pMenu->id == (UINT)id)
1134 break;
1135 if (pMenu->children != NULL)
1136 {
1137 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1138 if (pChildMenu)
1139 {
1140 pMenu = pChildMenu;
1141 break;
1142 }
1143 }
1144 pMenu = pMenu->next;
1145 }
1146 return pMenu;
1147}
1148
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001149 static void
1150_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001151 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001153 HWND hwndCtl UNUSED,
1154 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001155{
1156 vimmenu_T *pMenu;
1157
1158 pMenu = gui_mswin_find_menu(root_menu, id);
1159 if (pMenu)
1160 gui_menu_cb(pMenu);
1161}
1162#endif
1163
1164#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001165/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001166 * Handle a Find/Replace window message.
1167 */
1168 static void
1169_OnFindRepl(void)
1170{
1171 int flags = 0;
1172 int down;
1173
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001174 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001176 (void)SetFocus(s_hwnd);
1177
1178 if (s_findrep_struct.Flags & FR_FINDNEXT)
1179 {
1180 flags = FRD_FINDNEXT;
1181
Bram Moolenaar734a8672019-12-02 22:49:38 +01001182 // Give main window the focus back: this is so the cursor isn't
1183 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 (void)SetFocus(s_hwnd);
1185 }
1186 else if (s_findrep_struct.Flags & FR_REPLACE)
1187 {
1188 flags = FRD_REPLACE;
1189
Bram Moolenaar734a8672019-12-02 22:49:38 +01001190 // Give main window the focus back: this is so the cursor isn't
1191 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 (void)SetFocus(s_hwnd);
1193 }
1194 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1195 {
1196 flags = FRD_REPLACEALL;
1197 }
1198
1199 if (flags != 0)
1200 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001201 char_u *p, *q;
1202
Bram Moolenaar734a8672019-12-02 22:49:38 +01001203 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001204 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1205 flags |= FRD_WHOLE_WORD;
1206 if (s_findrep_struct.Flags & FR_MATCHCASE)
1207 flags |= FRD_MATCH_CASE;
1208 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001209 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1210 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1211 if (p != NULL && q != NULL)
1212 gui_do_findrepl(flags, p, q, down);
1213 vim_free(p);
1214 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001215 }
1216}
1217#endif
1218
1219 static void
1220HandleMouseHide(UINT uMsg, LPARAM lParam)
1221{
1222 static LPARAM last_lParam = 0L;
1223
Bram Moolenaar734a8672019-12-02 22:49:38 +01001224 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001225 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1226 {
1227 if (lParam == last_lParam)
1228 return;
1229 last_lParam = lParam;
1230 }
1231
Bram Moolenaar734a8672019-12-02 22:49:38 +01001232 // Handle specially, to centralise coding. We need to be sure we catch all
1233 // possible events which should cause us to restore the cursor (as it is a
1234 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 switch (uMsg)
1236 {
1237 case WM_KEYUP:
1238 case WM_CHAR:
1239 /*
1240 * blank out the pointer if necessary
1241 */
1242 if (p_mh)
1243 gui_mch_mousehide(TRUE);
1244 break;
1245
Bram Moolenaar734a8672019-12-02 22:49:38 +01001246 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001247 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001248 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249 case WM_LBUTTONDOWN:
1250 case WM_LBUTTONUP:
1251 case WM_MBUTTONDOWN:
1252 case WM_MBUTTONUP:
1253 case WM_RBUTTONDOWN:
1254 case WM_RBUTTONUP:
1255 case WM_XBUTTONDOWN:
1256 case WM_XBUTTONUP:
1257 case WM_NCMOUSEMOVE:
1258 case WM_NCLBUTTONDOWN:
1259 case WM_NCLBUTTONUP:
1260 case WM_NCMBUTTONDOWN:
1261 case WM_NCMBUTTONUP:
1262 case WM_NCRBUTTONDOWN:
1263 case WM_NCRBUTTONUP:
1264 case WM_KILLFOCUS:
1265 /*
1266 * if the pointer is currently hidden, then we should show it.
1267 */
1268 gui_mch_mousehide(FALSE);
1269 break;
1270 }
1271}
1272
1273 static LRESULT CALLBACK
1274_TextAreaWndProc(
1275 HWND hwnd,
1276 UINT uMsg,
1277 WPARAM wParam,
1278 LPARAM lParam)
1279{
1280 /*
1281 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1282 hwnd, uMsg, wParam, lParam);
1283 */
1284
1285 HandleMouseHide(uMsg, lParam);
1286
1287 s_uMsg = uMsg;
1288 s_wParam = wParam;
1289 s_lParam = lParam;
1290
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001291#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001292 TrackUserActivity(uMsg);
1293#endif
1294
1295 switch (uMsg)
1296 {
1297 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1298 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1299 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1300 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1301 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1302 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1303 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1304 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1305 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1306 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1307 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1308 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1309 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1310 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001311 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001312
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001313#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001314 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1315 return TRUE;
1316#endif
1317 default:
1318 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1319 }
1320}
1321
Bram Moolenaar945c8572020-07-17 22:17:03 +02001322 static LRESULT WINAPI
1323MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001324{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001325#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001326 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001327#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001328 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001329#endif
1330}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331
1332/*
1333 * Called when the foreground or background color has been changed.
1334 */
1335 void
1336gui_mch_new_colors(void)
1337{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001338 HBRUSH prevBrush;
1339
1340 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001341 prevBrush = (HBRUSH)SetClassLongPtr(
1342 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001343 InvalidateRect(s_hwnd, NULL, TRUE);
1344 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001345}
1346
1347/*
1348 * Set the colors to their default values.
1349 */
1350 void
1351gui_mch_def_colors(void)
1352{
1353 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1354 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1355 gui.def_norm_pixel = gui.norm_pixel;
1356 gui.def_back_pixel = gui.back_pixel;
1357}
1358
1359/*
1360 * Open the GUI window which was created by a call to gui_mch_init().
1361 */
1362 int
1363gui_mch_open(void)
1364{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001365 // Actually open the window, if not already visible
1366 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001367 if (!IsWindowVisible(s_hwnd))
1368 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1369
1370#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001371 // Init replace string here, so that we keep it when re-opening the
1372 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001373 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1374#endif
1375
1376 return OK;
1377}
1378
1379/*
1380 * Get the position of the top left corner of the window.
1381 */
1382 int
1383gui_mch_get_winpos(int *x, int *y)
1384{
1385 RECT rect;
1386
1387 GetWindowRect(s_hwnd, &rect);
1388 *x = rect.left;
1389 *y = rect.top;
1390 return OK;
1391}
1392
1393/*
1394 * Set the position of the top left corner of the window to the given
1395 * coordinates.
1396 */
1397 void
1398gui_mch_set_winpos(int x, int y)
1399{
1400 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1401 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1402}
1403 void
1404gui_mch_set_text_area_pos(int x, int y, int w, int h)
1405{
1406 static int oldx = 0;
1407 static int oldy = 0;
1408
1409 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1410
1411#ifdef FEAT_TOOLBAR
1412 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1413 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001414 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001415#endif
1416#if defined(FEAT_GUI_TABLINE)
1417 if (showing_tabline)
1418 {
1419 int top = 0;
1420 RECT rect;
1421
1422# ifdef FEAT_TOOLBAR
1423 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001424 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001425# endif
1426 GetClientRect(s_hwnd, &rect);
1427 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1428 }
1429#endif
1430
Bram Moolenaar734a8672019-12-02 22:49:38 +01001431 // When side scroll bar is unshown, the size of window will change.
1432 // then, the text area move left or right. thus client rect should be
1433 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001434 if (oldx != x || oldy != y)
1435 {
1436 InvalidateRect(s_hwnd, NULL, FALSE);
1437 oldx = x;
1438 oldy = y;
1439 }
1440}
1441
1442
1443/*
1444 * Scrollbar stuff:
1445 */
1446
1447 void
1448gui_mch_enable_scrollbar(
1449 scrollbar_T *sb,
1450 int flag)
1451{
1452 ShowScrollBar(sb->id, SB_CTL, flag);
1453
Bram Moolenaar734a8672019-12-02 22:49:38 +01001454 // TODO: When the window is maximized, the size of the window stays the
1455 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1456 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001457}
1458
1459 void
1460gui_mch_set_scrollbar_pos(
1461 scrollbar_T *sb,
1462 int x,
1463 int y,
1464 int w,
1465 int h)
1466{
1467 SetWindowPos(sb->id, NULL, x, y, w, h,
1468 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1469}
1470
Bram Moolenaar203ec772020-07-17 20:43:43 +02001471 int
1472gui_mch_get_scrollbar_xpadding(void)
1473{
1474 RECT rcTxt, rcWnd;
1475 int xpad;
1476
1477 GetWindowRect(s_textArea, &rcTxt);
1478 GetWindowRect(s_hwnd, &rcWnd);
1479 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001480 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1481 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001482 return (xpad < 0) ? 0 : xpad;
1483}
1484
1485 int
1486gui_mch_get_scrollbar_ypadding(void)
1487{
1488 RECT rcTxt, rcWnd;
1489 int ypad;
1490
1491 GetWindowRect(s_textArea, &rcTxt);
1492 GetWindowRect(s_hwnd, &rcWnd);
1493 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001494 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1495 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001496 return (ypad < 0) ? 0 : ypad;
1497}
1498
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001499 void
1500gui_mch_create_scrollbar(
1501 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001502 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001503{
1504 sb->id = CreateWindow(
1505 "SCROLLBAR", "Scrollbar",
1506 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001507 10, // Any value will do for now
1508 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001509 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001510 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001511}
1512
1513/*
1514 * Find the scrollbar with the given hwnd.
1515 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001516 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001517gui_mswin_find_scrollbar(HWND hwnd)
1518{
1519 win_T *wp;
1520
1521 if (gui.bottom_sbar.id == hwnd)
1522 return &gui.bottom_sbar;
1523 FOR_ALL_WINDOWS(wp)
1524 {
1525 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1526 return &wp->w_scrollbars[SBAR_LEFT];
1527 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1528 return &wp->w_scrollbars[SBAR_RIGHT];
1529 }
1530 return NULL;
1531}
1532
K.Takatac81e9bf2022-01-16 14:15:49 +00001533 static void
1534update_scrollbar_size(void)
1535{
1536 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1537 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1538}
1539
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001540/*
1541 * Get the character size of a font.
1542 */
1543 static void
1544GetFontSize(GuiFont font)
1545{
1546 HWND hwnd = GetDesktopWindow();
1547 HDC hdc = GetWindowDC(hwnd);
1548 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001549 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001550 TEXTMETRIC tm;
1551
1552 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001553 // GetTextMetrics() may not return the right value in tmAveCharWidth
1554 // for some fonts. Do our own average computation.
1555 GetTextExtentPoint(hdc,
1556 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1557 52, &size);
1558 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559
1560 gui.char_height = tm.tmHeight + p_linespace;
1561
1562 SelectFont(hdc, hfntOld);
1563
1564 ReleaseDC(hwnd, hdc);
1565}
1566
1567/*
1568 * Adjust gui.char_height (after 'linespace' was changed).
1569 */
1570 int
1571gui_mch_adjust_charheight(void)
1572{
1573 GetFontSize(gui.norm_font);
1574 return OK;
1575}
1576
1577 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001578get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001579{
1580 HFONT font = NULL;
1581
Bram Moolenaar734a8672019-12-02 22:49:38 +01001582 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001583 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
1585 if (font == NULL)
1586 return NOFONT;
1587
1588 return (GuiFont)font;
1589}
1590
1591 static int
1592pixels_to_points(int pixels, int vertical)
1593{
1594 int points;
1595 HWND hwnd;
1596 HDC hdc;
1597
1598 hwnd = GetDesktopWindow();
1599 hdc = GetWindowDC(hwnd);
1600
1601 points = MulDiv(pixels, 72,
1602 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1603
1604 ReleaseDC(hwnd, hdc);
1605
1606 return points;
1607}
1608
1609 GuiFont
1610gui_mch_get_font(
1611 char_u *name,
1612 int giveErrorIfMissing)
1613{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001614 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001615 GuiFont font = NOFONT;
1616
1617 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001618 {
1619 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001620 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001621 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001623 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001624 return font;
1625}
1626
1627#if defined(FEAT_EVAL) || defined(PROTO)
1628/*
1629 * Return the name of font "font" in allocated memory.
1630 * Don't know how to get the actual name, thus use the provided name.
1631 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001632 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001633gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634{
1635 if (name == NULL)
1636 return NULL;
1637 return vim_strsave(name);
1638}
1639#endif
1640
1641 void
1642gui_mch_free_font(GuiFont font)
1643{
1644 if (font)
1645 DeleteObject((HFONT)font);
1646}
1647
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648/*
1649 * Return the Pixel value (color) for the given color name.
1650 * Return INVALCOLOR for error.
1651 */
1652 guicolor_T
1653gui_mch_get_color(char_u *name)
1654{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001655 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656
1657 typedef struct SysColorTable
1658 {
1659 char *name;
1660 int color;
1661 } SysColorTable;
1662
1663 static SysColorTable sys_table[] =
1664 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1666 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001667#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1669#endif
1670 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1671 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1672 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1673 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1674 {"SYS_DESKTOP", COLOR_DESKTOP},
1675 {"SYS_INFOBK", COLOR_INFOBK},
1676 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1677 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678 {"SYS_BTNFACE", COLOR_BTNFACE},
1679 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1680 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1681 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1682 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1683 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1684 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1685 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1686 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1687 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1688 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1689 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1690 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1691 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1692 {"SYS_MENU", COLOR_MENU},
1693 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1694 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1695 {"SYS_WINDOW", COLOR_WINDOW},
1696 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1697 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1698 };
1699
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700 /*
1701 * Try to look up a system colour.
1702 */
K.Takataeeec2542021-06-02 13:28:16 +02001703 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001704 if (STRICMP(name, sys_table[i].name) == 0)
1705 return GetSysColor(sys_table[i].color);
1706
Bram Moolenaarab302212016-04-26 20:59:29 +02001707 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001709
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001710 guicolor_T
1711gui_mch_get_rgb_color(int r, int g, int b)
1712{
1713 return gui_get_rgb_color_cmn(r, g, b);
1714}
1715
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716/*
1717 * Return OK if the key with the termcap name "name" is supported.
1718 */
1719 int
1720gui_mch_haskey(char_u *name)
1721{
1722 int i;
1723
1724 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1725 if (name[0] == special_keys[i].vim_code0 &&
1726 name[1] == special_keys[i].vim_code1)
1727 return OK;
1728 return FAIL;
1729}
1730
1731 void
1732gui_mch_beep(void)
1733{
1734 MessageBeep(MB_OK);
1735}
1736/*
1737 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1738 */
1739 void
1740gui_mch_invert_rectangle(
1741 int r,
1742 int c,
1743 int nr,
1744 int nc)
1745{
1746 RECT rc;
1747
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001748#if defined(FEAT_DIRECTX)
1749 if (IS_ENABLE_DIRECTX())
1750 DWriteContext_Flush(s_dwc);
1751#endif
1752
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753 /*
1754 * Note: InvertRect() excludes right and bottom of rectangle.
1755 */
1756 rc.left = FILL_X(c);
1757 rc.top = FILL_Y(r);
1758 rc.right = rc.left + nc * gui.char_width;
1759 rc.bottom = rc.top + nr * gui.char_height;
1760 InvertRect(s_hdc, &rc);
1761}
1762
1763/*
1764 * Iconify the GUI window.
1765 */
1766 void
1767gui_mch_iconify(void)
1768{
1769 ShowWindow(s_hwnd, SW_MINIMIZE);
1770}
1771
1772/*
1773 * Draw a cursor without focus.
1774 */
1775 void
1776gui_mch_draw_hollow_cursor(guicolor_T color)
1777{
1778 HBRUSH hbr;
1779 RECT rc;
1780
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001781#if defined(FEAT_DIRECTX)
1782 if (IS_ENABLE_DIRECTX())
1783 DWriteContext_Flush(s_dwc);
1784#endif
1785
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001786 /*
1787 * Note: FrameRect() excludes right and bottom of rectangle.
1788 */
1789 rc.left = FILL_X(gui.col);
1790 rc.top = FILL_Y(gui.row);
1791 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 if (mb_lefthalve(gui.row, gui.col))
1793 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001794 rc.bottom = rc.top + gui.char_height;
1795 hbr = CreateSolidBrush(color);
1796 FrameRect(s_hdc, &rc, hbr);
1797 DeleteBrush(hbr);
1798}
1799/*
1800 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1801 * color "color".
1802 */
1803 void
1804gui_mch_draw_part_cursor(
1805 int w,
1806 int h,
1807 guicolor_T color)
1808{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001809 RECT rc;
1810
1811 /*
1812 * Note: FillRect() excludes right and bottom of rectangle.
1813 */
1814 rc.left =
1815#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001816 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1818#endif
1819 FILL_X(gui.col);
1820 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1821 rc.right = rc.left + w;
1822 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001823
Bram Moolenaar92467d32017-12-05 13:22:16 +01001824 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825}
1826
1827
1828/*
1829 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1830 * dead key's nominal character and re-post the original message.
1831 */
1832 static void
1833outputDeadKey_rePost(MSG originalMsg)
1834{
1835 static MSG deadCharExpel;
1836
1837 if (!dead_key)
1838 return;
1839
1840 dead_key = 0;
1841
Bram Moolenaar734a8672019-12-02 22:49:38 +01001842 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001843 deadCharExpel.message = originalMsg.message;
1844 deadCharExpel.hwnd = originalMsg.hwnd;
1845 deadCharExpel.wParam = VK_SPACE;
1846
1847 MyTranslateMessage(&deadCharExpel);
1848
Bram Moolenaar734a8672019-12-02 22:49:38 +01001849 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001850 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1851 originalMsg.lParam);
1852}
1853
1854
1855/*
1856 * Process a single Windows message.
1857 * If one is not available we hang until one is.
1858 */
1859 static void
1860process_message(void)
1861{
1862 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 char_u string[40];
1865 int i;
1866 int modifiers = 0;
1867 int key;
1868#ifdef FEAT_MENU
1869 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1870#endif
1871
1872 pGetMessage(&msg, NULL, 0, 0);
1873
1874#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001875 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001876 if (msg.message == WM_OLE)
1877 {
1878 char_u *str = (char_u *)msg.lParam;
1879 if (str == NULL || *str == NUL)
1880 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001881 // Message can't be ours, forward it. Fixes problem with Ultramon
1882 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001883 pDispatchMessage(&msg);
1884 }
1885 else
1886 {
1887 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001888 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001889 }
1890 return;
1891 }
1892#endif
1893
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001895 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1897 {
1898 HandleMouseHide(msg.message, msg.lParam);
1899 return;
1900 }
1901#endif
1902
1903 /*
1904 * Check if it's a special key that we recognise. If not, call
1905 * TranslateMessage().
1906 */
1907 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1908 {
1909 vk = (int) msg.wParam;
1910
1911 /*
1912 * Handle dead keys in special conditions in other cases we let Windows
1913 * handle them and do not interfere.
1914 *
1915 * The dead_key flag must be reset on several occasions:
1916 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1917 * consumed at that point (This is when we let Windows combine the
1918 * dead character on its own)
1919 *
1920 * - Before doing something special such as regenerating keypresses to
1921 * expel the dead character as this could trigger an infinite loop if
1922 * for some reason MyTranslateMessage() do not trigger a call
1923 * immediately to _OnChar() (or _OnSysChar()).
1924 */
1925 if (dead_key)
1926 {
1927 /*
1928 * If a dead key was pressed and the user presses VK_SPACE,
1929 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1930 * with the dead char now, so do nothing special and let Windows
1931 * handle it.
1932 *
1933 * Note that VK_SPACE combines with the dead_key's character and
1934 * only one WM_CHAR will be generated by TranslateMessage(), in
1935 * the two other cases two WM_CHAR will be generated: the dead
1936 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1937 * user expects.
1938 */
1939 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1940 {
1941 dead_key = 0;
1942 MyTranslateMessage(&msg);
1943 return;
1944 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001945 // In modes where we are not typing, dead keys should behave
1946 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1948 {
1949 outputDeadKey_rePost(msg);
1950 return;
1951 }
1952 }
1953
Bram Moolenaar734a8672019-12-02 22:49:38 +01001954 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001955 if (vk == VK_CANCEL)
1956 {
1957 trash_input_buf();
1958 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001959 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001960 string[0] = Ctrl_C;
1961 add_to_input_buf(string, 1);
1962 }
1963
1964 for (i = 0; special_keys[i].key_sym != 0; i++)
1965 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001966 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001967 if (special_keys[i].key_sym == vk
1968 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1969 {
1970 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001971 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001972 * is a key that would normally trigger the dead key nominal
1973 * character output (such as a NUMPAD printable character or
1974 * the TAB key, etc...).
1975 */
1976 if (dead_key && (special_keys[i].vim_code0 == 'K'
1977 || vk == VK_TAB || vk == CAR))
1978 {
1979 outputDeadKey_rePost(msg);
1980 return;
1981 }
1982
1983#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001984 // Check for <F10>: Windows selects the menu. When <F10> is
1985 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001986 if (vk == VK_F10
1987 && gui.menu_is_active
1988 && check_map(k10, State, FALSE, TRUE, FALSE,
1989 NULL, NULL) == NULL)
1990 break;
1991#endif
1992 if (GetKeyState(VK_SHIFT) & 0x8000)
1993 modifiers |= MOD_MASK_SHIFT;
1994 /*
1995 * Don't use caps-lock as shift, because these are special keys
1996 * being considered here, and we only want letters to get
1997 * shifted -- webb
1998 */
1999 /*
2000 if (GetKeyState(VK_CAPITAL) & 0x0001)
2001 modifiers ^= MOD_MASK_SHIFT;
2002 */
2003 if (GetKeyState(VK_CONTROL) & 0x8000)
2004 modifiers |= MOD_MASK_CTRL;
2005 if (GetKeyState(VK_MENU) & 0x8000)
2006 modifiers |= MOD_MASK_ALT;
2007
2008 if (special_keys[i].vim_code1 == NUL)
2009 key = special_keys[i].vim_code0;
2010 else
2011 key = TO_SPECIAL(special_keys[i].vim_code0,
2012 special_keys[i].vim_code1);
2013 key = simplify_key(key, &modifiers);
2014 if (key == CSI)
2015 key = K_CSI;
2016
2017 if (modifiers)
2018 {
2019 string[0] = CSI;
2020 string[1] = KS_MODIFIER;
2021 string[2] = modifiers;
2022 add_to_input_buf(string, 3);
2023 }
2024
2025 if (IS_SPECIAL(key))
2026 {
2027 string[0] = CSI;
2028 string[1] = K_SECOND(key);
2029 string[2] = K_THIRD(key);
2030 add_to_input_buf(string, 3);
2031 }
2032 else
2033 {
2034 int len;
2035
Bram Moolenaar734a8672019-12-02 22:49:38 +01002036 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002037 len = char_to_string(key, string, 40, FALSE);
2038 add_to_input_buf(string, len);
2039 }
2040 break;
2041 }
2042 }
2043 if (special_keys[i].key_sym == 0)
2044 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002045 // Some keys need C-S- where they should only need C-.
2046 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2047 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048 if (vk != 0xff
2049 && (GetKeyState(VK_CONTROL) & 0x8000)
2050 && !(GetKeyState(VK_SHIFT) & 0x8000)
2051 && !(GetKeyState(VK_MENU) & 0x8000))
2052 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002053 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002054 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2055 {
2056 string[0] = Ctrl_HAT;
2057 add_to_input_buf(string, 1);
2058 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002059 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2060 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002061 {
2062 string[0] = Ctrl__;
2063 add_to_input_buf(string, 1);
2064 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002066 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2067 {
2068 string[0] = Ctrl_AT;
2069 add_to_input_buf(string, 1);
2070 }
2071 else
2072 MyTranslateMessage(&msg);
2073 }
2074 else
2075 MyTranslateMessage(&msg);
2076 }
2077 }
2078#ifdef FEAT_MBYTE_IME
2079 else if (msg.message == WM_IME_NOTIFY)
2080 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2081 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002082 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002083 MyTranslateMessage(&msg);
2084#endif
2085#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002086// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 else if (msg.message == WM_IME_STARTCOMPOSITION)
2088 {
2089 POINT point;
2090
2091 global_ime_set_font(&norm_logfont);
2092 point.x = FILL_X(gui.col);
2093 point.y = FILL_Y(gui.row);
2094 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2095 global_ime_set_position(&point);
2096 }
2097#endif
2098
2099#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002100 // Check for <F10>: Default effect is to select the menu. When <F10> is
2101 // mapped we need to stop it here to avoid strange effects (e.g., for the
2102 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002103 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2104 NULL, NULL) == NULL)
2105#endif
2106 pDispatchMessage(&msg);
2107}
2108
2109/*
2110 * Catch up with any queued events. This may put keyboard input into the
2111 * input buffer, call resize call-backs, trigger timers etc. If there is
2112 * nothing in the event queue (& no timers pending), then we return
2113 * immediately.
2114 */
2115 void
2116gui_mch_update(void)
2117{
2118 MSG msg;
2119
2120 if (!s_busy_processing)
2121 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2122 && !vim_is_input_buf_full())
2123 process_message();
2124}
2125
Bram Moolenaar4231da42016-06-02 14:30:04 +02002126 static void
2127remove_any_timer(void)
2128{
2129 MSG msg;
2130
2131 if (s_wait_timer != 0 && !s_timed_out)
2132 {
2133 KillTimer(NULL, s_wait_timer);
2134
Bram Moolenaar734a8672019-12-02 22:49:38 +01002135 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002136 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2137 ;
2138 s_wait_timer = 0;
2139 }
2140}
2141
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002142/*
2143 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2144 * from the keyboard.
2145 * wtime == -1 Wait forever.
2146 * wtime == 0 This should never happen.
2147 * wtime > 0 Wait wtime milliseconds for a character.
2148 * Returns OK if a character was found to be available within the given time,
2149 * or FAIL otherwise.
2150 */
2151 int
2152gui_mch_wait_for_chars(int wtime)
2153{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002154 int focus;
2155
2156 s_timed_out = FALSE;
2157
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002158 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002159 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002160 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002161 if (s_busy_processing)
2162 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002163
2164 // When called with "wtime" zero, just want one msec.
2165 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002166 (TIMERPROC)_OnTimer);
2167 }
2168
2169 allow_scrollbar = TRUE;
2170
2171 focus = gui.in_focus;
2172 while (!s_timed_out)
2173 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002174 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002175 if (gui.in_focus != focus)
2176 {
2177 if (gui.in_focus)
2178 gui_mch_start_blink();
2179 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002180 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002181 focus = gui.in_focus;
2182 }
2183
2184 if (s_need_activate)
2185 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002187 s_need_activate = FALSE;
2188 }
2189
Bram Moolenaar4231da42016-06-02 14:30:04 +02002190#ifdef FEAT_TIMERS
2191 did_add_timer = FALSE;
2192#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002193#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002194 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002195 for (;;)
2196 {
2197 MSG msg;
2198
2199 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002200# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002201 if (did_add_timer)
2202 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002203# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002204 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2205 {
2206 process_message();
2207 break;
2208 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002209 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002210 // TODO: The 10 msec is a compromise between laggy response
2211 // and consuming more CPU time. Better would be to handle
2212 // channel messages when they arrive.
2213 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002214 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002215 break;
2216 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002217#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002218 // Don't use gui_mch_update() because then we will spin-lock until a
2219 // char arrives, instead we use GetMessage() to hang until an
2220 // event arrives. No need to check for input_buf_full because we are
2221 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002222 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002223#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002224
2225 if (input_available())
2226 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002227 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002228 allow_scrollbar = FALSE;
2229
Bram Moolenaar89c00032019-09-04 13:53:21 +02002230 // Clear pending mouse button, the release event may have been
2231 // taken by the dialog window. But don't do this when getting
2232 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233 if (!s_getting_focus)
2234 s_button_pending = -1;
2235
2236 return OK;
2237 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002238
2239#ifdef FEAT_TIMERS
2240 if (did_add_timer)
2241 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002242 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002243 remove_any_timer();
2244 break;
2245 }
2246#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002247 }
2248 allow_scrollbar = FALSE;
2249 return FAIL;
2250}
2251
2252/*
2253 * Clear a rectangular region of the screen from text pos (row1, col1) to
2254 * (row2, col2) inclusive.
2255 */
2256 void
2257gui_mch_clear_block(
2258 int row1,
2259 int col1,
2260 int row2,
2261 int col2)
2262{
2263 RECT rc;
2264
2265 /*
2266 * Clear one extra pixel at the far right, for when bold characters have
2267 * spilled over to the window border.
2268 * Note: FillRect() excludes right and bottom of rectangle.
2269 */
2270 rc.left = FILL_X(col1);
2271 rc.top = FILL_Y(row1);
2272 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2273 rc.bottom = FILL_Y(row2 + 1);
2274 clear_rect(&rc);
2275}
2276
2277/*
2278 * Clear the whole text window.
2279 */
2280 void
2281gui_mch_clear_all(void)
2282{
2283 RECT rc;
2284
2285 rc.left = 0;
2286 rc.top = 0;
2287 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2288 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2289 clear_rect(&rc);
2290}
2291/*
2292 * Menu stuff.
2293 */
2294
2295 void
2296gui_mch_enable_menu(int flag)
2297{
2298#ifdef FEAT_MENU
2299 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2300#endif
2301}
2302
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 void
2304gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002305 int x UNUSED,
2306 int y UNUSED,
2307 int w UNUSED,
2308 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002310 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002311}
2312
2313#if defined(FEAT_MENU) || defined(PROTO)
2314/*
2315 * Make menu item hidden or not hidden
2316 */
2317 void
2318gui_mch_menu_hidden(
2319 vimmenu_T *menu,
2320 int hidden)
2321{
2322 /*
2323 * This doesn't do what we want. Hmm, just grey the menu items for now.
2324 */
2325 /*
2326 if (hidden)
2327 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2328 else
2329 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2330 */
2331 gui_mch_menu_grey(menu, hidden);
2332}
2333
2334/*
2335 * This is called after setting all the menus to grey/hidden or not.
2336 */
2337 void
2338gui_mch_draw_menubar(void)
2339{
2340 DrawMenuBar(s_hwnd);
2341}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002342#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002343
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002344/*
2345 * Return the RGB value of a pixel as a long.
2346 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002347 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002348gui_mch_get_rgb(guicolor_T pixel)
2349{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002350 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2351 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352}
2353
2354#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002355/*
2356 * Convert pixels in X to dialog units
2357 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 static WORD
2359PixelToDialogX(int numPixels)
2360{
2361 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2362}
2363
Bram Moolenaar734a8672019-12-02 22:49:38 +01002364/*
2365 * Convert pixels in Y to dialog units
2366 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 static WORD
2368PixelToDialogY(int numPixels)
2369{
2370 return (WORD)((numPixels * 8) / s_dlgfntheight);
2371}
2372
Bram Moolenaar734a8672019-12-02 22:49:38 +01002373/*
2374 * Return the width in pixels of the given text in the given DC.
2375 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 static int
2377GetTextWidth(HDC hdc, char_u *str, int len)
2378{
2379 SIZE size;
2380
2381 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2382 return size.cx;
2383}
2384
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002385/*
2386 * Return the width in pixels of the given text in the given DC, taking care
2387 * of 'encoding' to active codepage conversion.
2388 */
2389 static int
2390GetTextWidthEnc(HDC hdc, char_u *str, int len)
2391{
2392 SIZE size;
2393 WCHAR *wstr;
2394 int n;
2395 int wlen = len;
2396
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002397 wstr = enc_to_utf16(str, &wlen);
2398 if (wstr == NULL)
2399 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002401 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2402 vim_free(wstr);
2403 if (n)
2404 return size.cx;
2405 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002408static void get_work_area(RECT *spi_rect);
2409
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002410/*
2411 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002412 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2413 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414 */
2415 static BOOL
2416CenterWindow(
2417 HWND hwndChild,
2418 HWND hwndParent)
2419{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002420 HMONITOR mon;
2421 MONITORINFO moninfo;
2422 RECT rChild, rParent, rScreen;
2423 int wChild, hChild, wParent, hParent;
2424 int xNew, yNew;
2425 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426
2427 GetWindowRect(hwndChild, &rChild);
2428 wChild = rChild.right - rChild.left;
2429 hChild = rChild.bottom - rChild.top;
2430
Bram Moolenaar734a8672019-12-02 22:49:38 +01002431 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002433 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434 else
2435 GetWindowRect(hwndParent, &rParent);
2436 wParent = rParent.right - rParent.left;
2437 hParent = rParent.bottom - rParent.top;
2438
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002439 moninfo.cbSize = sizeof(MONITORINFO);
2440 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2441 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002443 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002444 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002445 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002446 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002447 hdc = GetDC(hwndChild);
2448 rScreen.left = 0;
2449 rScreen.top = 0;
2450 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2451 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2452 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 }
2454
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002455 xNew = rParent.left + ((wParent - wChild) / 2);
2456 if (xNew < rScreen.left)
2457 xNew = rScreen.left;
2458 else if ((xNew + wChild) > rScreen.right)
2459 xNew = rScreen.right - wChild;
2460
2461 yNew = rParent.top + ((hParent - hChild) / 2);
2462 if (yNew < rScreen.top)
2463 yNew = rScreen.top;
2464 else if ((yNew + hChild) > rScreen.bottom)
2465 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466
2467 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2468 SWP_NOSIZE | SWP_NOZORDER);
2469}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002470#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002472#if defined(FEAT_TOOLBAR) || defined(PROTO)
2473 void
2474gui_mch_show_toolbar(int showit)
2475{
2476 if (s_toolbarhwnd == NULL)
2477 return;
2478
2479 if (showit)
2480 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002481# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002482 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002483# define TB_SETUNICODEFORMAT 0x2005
2484# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002485 // Enable unicode support
2486 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2487 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488 ShowWindow(s_toolbarhwnd, SW_SHOW);
2489 }
2490 else
2491 ShowWindow(s_toolbarhwnd, SW_HIDE);
2492}
2493
Bram Moolenaar734a8672019-12-02 22:49:38 +01002494// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002495# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496
2497#endif
2498
2499#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2500 static void
2501add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2502{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002503 WCHAR *wn;
2504 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002506 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002507 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002508 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002510 infow.cbSize = sizeof(infow);
2511 infow.fMask = MIIM_TYPE | MIIM_ID;
2512 infow.wID = item_id;
2513 infow.fType = MFT_STRING;
2514 infow.dwTypeData = wn;
2515 infow.cch = (UINT)wcslen(wn);
2516 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2517 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518}
2519
2520 static void
2521show_tabline_popup_menu(void)
2522{
2523 HMENU tab_pmenu;
2524 long rval;
2525 POINT pt;
2526
Bram Moolenaar734a8672019-12-02 22:49:38 +01002527 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002528 if (hold_gui_events
2529# ifdef FEAT_CMDWIN
2530 || cmdwin_type != 0
2531# endif
2532 )
2533 return;
2534
2535 tab_pmenu = CreatePopupMenu();
2536 if (tab_pmenu == NULL)
2537 return;
2538
2539 if (first_tabpage->tp_next != NULL)
2540 add_tabline_popup_menu_entry(tab_pmenu,
2541 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2542 add_tabline_popup_menu_entry(tab_pmenu,
2543 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2544 add_tabline_popup_menu_entry(tab_pmenu,
2545 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2546
2547 GetCursorPos(&pt);
2548 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2549 NULL);
2550
2551 DestroyMenu(tab_pmenu);
2552
Bram Moolenaar734a8672019-12-02 22:49:38 +01002553 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 if (rval > 0)
2555 {
2556 TCHITTESTINFO htinfo;
2557 int idx;
2558
2559 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2560 return;
2561
2562 htinfo.pt.x = pt.x;
2563 htinfo.pt.y = pt.y;
2564 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2565 if (idx == -1)
2566 idx = 0;
2567 else
2568 idx += 1;
2569
2570 send_tabline_menu_event(idx, (int)rval);
2571 }
2572}
2573
2574/*
2575 * Show or hide the tabline.
2576 */
2577 void
2578gui_mch_show_tabline(int showit)
2579{
2580 if (s_tabhwnd == NULL)
2581 return;
2582
2583 if (!showit != !showing_tabline)
2584 {
2585 if (showit)
2586 ShowWindow(s_tabhwnd, SW_SHOW);
2587 else
2588 ShowWindow(s_tabhwnd, SW_HIDE);
2589 showing_tabline = showit;
2590 }
2591}
2592
2593/*
2594 * Return TRUE when tabline is displayed.
2595 */
2596 int
2597gui_mch_showing_tabline(void)
2598{
2599 return s_tabhwnd != NULL && showing_tabline;
2600}
2601
2602/*
2603 * Update the labels of the tabline.
2604 */
2605 void
2606gui_mch_update_tabline(void)
2607{
2608 tabpage_T *tp;
2609 TCITEM tie;
2610 int nr = 0;
2611 int curtabidx = 0;
2612 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614
2615 if (s_tabhwnd == NULL)
2616 return;
2617
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002618# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002619 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002620# define CCM_SETUNICODEFORMAT 0x2005
2621# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002622 // Enable unicode support
2623 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624
2625 tie.mask = TCIF_TEXT;
2626 tie.iImage = -1;
2627
Bram Moolenaar734a8672019-12-02 22:49:38 +01002628 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002629 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2630
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2633 {
2634 if (tp == curtab)
2635 curtabidx = nr;
2636
2637 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2638 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002639 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002640 tie.pszText = "-Empty-";
2641 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2642 tabadded = 1;
2643 }
2644
2645 get_tabline_label(tp, FALSE);
2646 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002648 wstr = enc_to_utf16(NameBuff, NULL);
2649 if (wstr != NULL)
2650 {
2651 TCITEMW tiw;
2652
2653 tiw.mask = TCIF_TEXT;
2654 tiw.iImage = -1;
2655 tiw.pszText = wstr;
2656 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2657 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002658 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002659 }
2660
Bram Moolenaar734a8672019-12-02 22:49:38 +01002661 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2663 TabCtrl_DeleteItem(s_tabhwnd, nr);
2664
2665 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2666 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2667
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2670 RedrawWindow(s_tabhwnd, NULL, NULL,
2671 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2672
2673 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2674 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2675}
2676
2677/*
2678 * Set the current tab to "nr". First tab is 1.
2679 */
2680 void
2681gui_mch_set_curtab(int nr)
2682{
2683 if (s_tabhwnd == NULL)
2684 return;
2685
2686 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2687 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2688}
2689
2690#endif
2691
2692/*
2693 * ":simalt" command.
2694 */
2695 void
2696ex_simalt(exarg_T *eap)
2697{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002698 char_u *keys = eap->arg;
2699 int fill_typebuf = FALSE;
2700 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701
2702 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2703 while (*keys)
2704 {
2705 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002706 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2708 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002709 fill_typebuf = TRUE;
2710 }
2711 if (fill_typebuf)
2712 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002713 // Put a NOP in the typeahead buffer so that the message will get
2714 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002715 key_name[0] = K_SPECIAL;
2716 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002717 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002718 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002719#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002720 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002721#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002722 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 }
2724}
2725
2726/*
2727 * Create the find & replace dialogs.
2728 * You can't have both at once: ":find" when replace is showing, destroys
2729 * the replace dialog first, and the other way around.
2730 */
2731#ifdef MSWIN_FIND_REPLACE
2732 static void
2733initialise_findrep(char_u *initial_string)
2734{
2735 int wword = FALSE;
2736 int mcase = !p_ic;
2737 char_u *entry_text;
2738
Bram Moolenaar734a8672019-12-02 22:49:38 +01002739 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2741
2742 s_findrep_struct.hwndOwner = s_hwnd;
2743 s_findrep_struct.Flags = FR_DOWN;
2744 if (mcase)
2745 s_findrep_struct.Flags |= FR_MATCHCASE;
2746 if (wword)
2747 s_findrep_struct.Flags |= FR_WHOLEWORD;
2748 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002749 {
2750 WCHAR *p = enc_to_utf16(entry_text, NULL);
2751 if (p != NULL)
2752 {
2753 int len = s_findrep_struct.wFindWhatLen - 1;
2754
2755 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2756 s_findrep_struct.lpstrFindWhat[len] = NUL;
2757 vim_free(p);
2758 }
2759 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 vim_free(entry_text);
2761}
2762#endif
2763
2764 static void
2765set_window_title(HWND hwnd, char *title)
2766{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002767 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002768 {
2769 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002770
Bram Moolenaar734a8672019-12-02 22:49:38 +01002771 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002772 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2773 if (wbuf != NULL)
2774 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002775 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002776 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777 }
2778 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002779 else
2780 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781}
2782
2783 void
2784gui_mch_find_dialog(exarg_T *eap)
2785{
2786#ifdef MSWIN_FIND_REPLACE
2787 if (s_findrep_msg != 0)
2788 {
2789 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2790 DestroyWindow(s_findrep_hwnd);
2791
2792 if (!IsWindow(s_findrep_hwnd))
2793 {
2794 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002795 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002796 }
2797
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002798 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 (void)SetFocus(s_findrep_hwnd);
2800
2801 s_findrep_is_find = TRUE;
2802 }
2803#endif
2804}
2805
2806
2807 void
2808gui_mch_replace_dialog(exarg_T *eap)
2809{
2810#ifdef MSWIN_FIND_REPLACE
2811 if (s_findrep_msg != 0)
2812 {
2813 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2814 DestroyWindow(s_findrep_hwnd);
2815
2816 if (!IsWindow(s_findrep_hwnd))
2817 {
2818 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002819 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820 }
2821
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002822 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 (void)SetFocus(s_findrep_hwnd);
2824
2825 s_findrep_is_find = FALSE;
2826 }
2827#endif
2828}
2829
2830
2831/*
2832 * Set visibility of the pointer.
2833 */
2834 void
2835gui_mch_mousehide(int hide)
2836{
2837 if (hide != gui.pointer_hidden)
2838 {
2839 ShowCursor(!hide);
2840 gui.pointer_hidden = hide;
2841 }
2842}
2843
2844#ifdef FEAT_MENU
2845 static void
2846gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2847{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002848 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002849 gui_mch_mousehide(FALSE);
2850
2851 (void)TrackPopupMenu(
2852 (HMENU)menu->submenu_id,
2853 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2854 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002855 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002856 s_hwnd,
2857 NULL);
2858 /*
2859 * NOTE: The pop-up menu can eat the mouse up event.
2860 * We deal with this in normal.c.
2861 */
2862}
2863#endif
2864
2865/*
2866 * Got a message when the system will go down.
2867 */
2868 static void
2869_OnEndSession(void)
2870{
2871 getout_preserve_modified(1);
2872}
2873
2874/*
2875 * Get this message when the user clicks on the cross in the top right corner
2876 * of a Windows95 window.
2877 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002878 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002879_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880{
2881 gui_shell_closed();
2882}
2883
2884/*
2885 * Get a message when the window is being destroyed.
2886 */
2887 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002888_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889{
2890 if (!destroying)
2891 _OnClose(hwnd);
2892}
2893
2894 static void
2895_OnPaint(
2896 HWND hwnd)
2897{
2898 if (!IsMinimized(hwnd))
2899 {
2900 PAINTSTRUCT ps;
2901
Bram Moolenaar734a8672019-12-02 22:49:38 +01002902 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904
Bram Moolenaar734a8672019-12-02 22:49:38 +01002905 // prevent multi-byte characters from misprinting on an invalid
2906 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907 if (has_mbyte)
2908 {
2909 RECT rect;
2910
2911 GetClientRect(hwnd, &rect);
2912 ps.rcPaint.left = rect.left;
2913 ps.rcPaint.right = rect.right;
2914 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915
2916 if (!IsRectEmpty(&ps.rcPaint))
2917 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2919 ps.rcPaint.right - ps.rcPaint.left + 1,
2920 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2921 }
2922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 EndPaint(hwnd, &ps);
2924 }
2925}
2926
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002927 static void
2928_OnSize(
2929 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002930 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931 int cx,
2932 int cy)
2933{
K.Takatac81e9bf2022-01-16 14:15:49 +00002934 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002935 {
2936 gui_resize_shell(cx, cy);
2937
Bram Moolenaar734a8672019-12-02 22:49:38 +01002938 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 }
2941}
2942
2943 static void
2944_OnSetFocus(
2945 HWND hwnd,
2946 HWND hwndOldFocus)
2947{
2948 gui_focus_change(TRUE);
2949 s_getting_focus = TRUE;
2950 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2951}
2952
2953 static void
2954_OnKillFocus(
2955 HWND hwnd,
2956 HWND hwndNewFocus)
2957{
2958 gui_focus_change(FALSE);
2959 s_getting_focus = FALSE;
2960 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2961}
2962
2963/*
2964 * Get a message when the user switches back to vim
2965 */
2966 static LRESULT
2967_OnActivateApp(
2968 HWND hwnd,
2969 BOOL fActivate,
2970 DWORD dwThreadId)
2971{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002972 // we call gui_focus_change() in _OnSetFocus()
2973 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002974 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2975}
2976
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002977 void
2978gui_mch_destroy_scrollbar(scrollbar_T *sb)
2979{
2980 DestroyWindow(sb->id);
2981}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002982
2983/*
2984 * Get current mouse coordinates in text window.
2985 */
2986 void
2987gui_mch_getmouse(int *x, int *y)
2988{
2989 RECT rct;
2990 POINT mp;
2991
2992 (void)GetWindowRect(s_textArea, &rct);
2993 (void)GetCursorPos((LPPOINT)&mp);
2994 *x = (int)(mp.x - rct.left);
2995 *y = (int)(mp.y - rct.top);
2996}
2997
2998/*
2999 * Move mouse pointer to character at (x, y).
3000 */
3001 void
3002gui_mch_setmouse(int x, int y)
3003{
3004 RECT rct;
3005
3006 (void)GetWindowRect(s_textArea, &rct);
3007 (void)SetCursorPos(x + gui.border_offset + rct.left,
3008 y + gui.border_offset + rct.top);
3009}
3010
3011 static void
3012gui_mswin_get_valid_dimensions(
3013 int w,
3014 int h,
3015 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003016 int *valid_h,
3017 int *cols,
3018 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019{
3020 int base_width, base_height;
3021
3022 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003023 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3024 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003025 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003026 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3027 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3028 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3029 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003030 *cols = (w - base_width) / gui.char_width;
3031 *rows = (h - base_height) / gui.char_height;
3032 *valid_w = base_width + *cols * gui.char_width;
3033 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003034}
3035
3036 void
3037gui_mch_flash(int msec)
3038{
3039 RECT rc;
3040
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003041#if defined(FEAT_DIRECTX)
3042 if (IS_ENABLE_DIRECTX())
3043 DWriteContext_Flush(s_dwc);
3044#endif
3045
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003046 /*
3047 * Note: InvertRect() excludes right and bottom of rectangle.
3048 */
3049 rc.left = 0;
3050 rc.top = 0;
3051 rc.right = gui.num_cols * gui.char_width;
3052 rc.bottom = gui.num_rows * gui.char_height;
3053 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003054 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055
Bram Moolenaar734a8672019-12-02 22:49:38 +01003056 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057
3058 InvertRect(s_hdc, &rc);
3059}
3060
3061/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003062 * Check if the specified point is on-screen. (multi-monitor aware)
3063 */
3064 static BOOL
3065is_point_onscreen(int x, int y)
3066{
3067 POINT pt = {x, y};
3068
3069 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3070}
3071
3072/*
3073 * Check if the whole area of the specified window is on-screen.
3074 *
3075 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3076 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3077 * only works when the whole window is on-screen.
3078 */
3079 static BOOL
3080is_window_onscreen(HWND hwnd)
3081{
3082 RECT rc;
3083
3084 GetWindowRect(hwnd, &rc);
3085
3086 if (!is_point_onscreen(rc.left, rc.top))
3087 return FALSE;
3088 if (!is_point_onscreen(rc.left, rc.bottom))
3089 return FALSE;
3090 if (!is_point_onscreen(rc.right, rc.top))
3091 return FALSE;
3092 if (!is_point_onscreen(rc.right, rc.bottom))
3093 return FALSE;
3094 return TRUE;
3095}
3096
3097/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003098 * Return flags used for scrolling.
3099 * The SW_INVALIDATE is required when part of the window is covered or
3100 * off-screen. Refer to MS KB Q75236.
3101 */
3102 static int
3103get_scroll_flags(void)
3104{
3105 HWND hwnd;
3106 RECT rcVim, rcOther, rcDest;
3107
Bram Moolenaar185577e2020-10-29 20:08:21 +01003108 // Check if the window is (partly) off-screen.
3109 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003110 return SW_INVALIDATE;
3111
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003112 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003113 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003114 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3115 if (IsWindowVisible(hwnd))
3116 {
3117 GetWindowRect(hwnd, &rcOther);
3118 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3119 return SW_INVALIDATE;
3120 }
3121 return 0;
3122}
3123
3124/*
3125 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3126 * may not be scrolled out properly.
3127 * For gVim, when _OnScroll() is repeated, the character at the
3128 * previous cursor position may be left drawn after scroll.
3129 * The problem can be avoided by calling GetPixel() to get a pixel in
3130 * the region before ScrollWindowEx().
3131 */
3132 static void
3133intel_gpu_workaround(void)
3134{
3135 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3136}
3137
3138/*
3139 * Delete the given number of lines from the given row, scrolling up any
3140 * text further down within the scroll region.
3141 */
3142 void
3143gui_mch_delete_lines(
3144 int row,
3145 int num_lines)
3146{
3147 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003148
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149 rc.left = FILL_X(gui.scroll_region_left);
3150 rc.right = FILL_X(gui.scroll_region_right + 1);
3151 rc.top = FILL_Y(row);
3152 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3153
Bram Moolenaar92467d32017-12-05 13:22:16 +01003154#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003155 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003156 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003157 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003158 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003159 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160#endif
3161 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162#if defined(FEAT_DIRECTX)
3163 if (IS_ENABLE_DIRECTX())
3164 DWriteContext_Flush(s_dwc);
3165#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166 intel_gpu_workaround();
3167 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003168 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003169 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171
Bram Moolenaar734a8672019-12-02 22:49:38 +01003172 // This seems to be required to avoid the cursor disappearing when
3173 // scrolling such that the cursor ends up in the top-left character on
3174 // the screen... But why? (Webb)
3175 // It's probably fixed by disabling drawing the cursor while scrolling.
3176 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177
3178 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3179 gui.scroll_region_left,
3180 gui.scroll_region_bot, gui.scroll_region_right);
3181}
3182
3183/*
3184 * Insert the given number of lines before the given row, scrolling down any
3185 * following text within the scroll region.
3186 */
3187 void
3188gui_mch_insert_lines(
3189 int row,
3190 int num_lines)
3191{
3192 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003193
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 rc.left = FILL_X(gui.scroll_region_left);
3195 rc.right = FILL_X(gui.scroll_region_right + 1);
3196 rc.top = FILL_Y(row);
3197 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003198
3199#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003200 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003201 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003202 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003203 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003204 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003205#endif
3206 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003207#if defined(FEAT_DIRECTX)
3208 if (IS_ENABLE_DIRECTX())
3209 DWriteContext_Flush(s_dwc);
3210#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003211 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003212 // The SW_INVALIDATE is required when part of the window is covered or
3213 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003214 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003216 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003217 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003219 gui_clear_block(row, gui.scroll_region_left,
3220 row + num_lines - 1, gui.scroll_region_right);
3221}
3222
3223
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003225gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003226{
3227#if defined(FEAT_DIRECTX)
3228 DWriteContext_Close(s_dwc);
3229 DWrite_Final();
3230 s_dwc = NULL;
3231#endif
3232
3233 ReleaseDC(s_textArea, s_hdc);
3234 DeleteObject(s_brush);
3235
3236#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003237 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3239#endif
3240
Bram Moolenaar734a8672019-12-02 22:49:38 +01003241 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242 if (s_hwnd != NULL)
3243 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003244 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003245 DestroyWindow(s_hwnd);
3246 }
3247
3248#ifdef GLOBAL_IME
3249 global_ime_end();
3250#endif
3251}
3252
3253 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003254logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255{
3256 char *p;
3257 char *res;
3258 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003259 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003260 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003261 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003263 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3264 if (font_name == NULL)
3265 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003267 quality_name = quality_id2name((int)lf.lfQuality);
3268
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003269 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003270 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003271 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 if (res != NULL)
3273 {
3274 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003275 // make a normal font string out of the lf thing:
3276 points = pixels_to_points(
3277 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3278 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3279 sprintf((char *)p, "%s:h%d", font_name, points);
3280 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003281 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 while (*p)
3283 {
3284 if (*p == ' ')
3285 *p = '_';
3286 ++p;
3287 }
3288 if (lf.lfItalic)
3289 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003290 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003291 STRCAT(p, ":b");
3292 if (lf.lfUnderline)
3293 STRCAT(p, ":u");
3294 if (lf.lfStrikeOut)
3295 STRCAT(p, ":s");
3296 if (charset_name != NULL)
3297 {
3298 STRCAT(p, ":c");
3299 STRCAT(p, charset_name);
3300 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003301 if (quality_name != NULL)
3302 {
3303 STRCAT(p, ":q");
3304 STRCAT(p, quality_name);
3305 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003306 }
3307
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003308 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309 return (char_u *)res;
3310}
3311
3312
3313#ifdef FEAT_MBYTE_IME
3314/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003315 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003316 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317 */
3318 static void
3319update_im_font(void)
3320{
K.Takatac81e9bf2022-01-16 14:15:49 +00003321 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003322
3323 if (p_guifontwide != NULL && *p_guifontwide != NUL
3324 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003325 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326 norm_logfont = lf_wide;
3327 else
3328 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003329
3330 lf = norm_logfont;
3331 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3332 // Work around when PerMonitorV2 is not enabled in the process level.
3333 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3334 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003335}
3336#endif
3337
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338/*
3339 * Handler of gui.wide_font (p_guifontwide) changed notification.
3340 */
3341 void
3342gui_mch_wide_font_changed(void)
3343{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003344 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003346#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003347 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003348#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349
3350 gui_mch_free_font(gui.wide_ital_font);
3351 gui.wide_ital_font = NOFONT;
3352 gui_mch_free_font(gui.wide_bold_font);
3353 gui.wide_bold_font = NOFONT;
3354 gui_mch_free_font(gui.wide_boldital_font);
3355 gui.wide_boldital_font = NOFONT;
3356
3357 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003358 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 {
3360 if (!lf.lfItalic)
3361 {
3362 lf.lfItalic = TRUE;
3363 gui.wide_ital_font = get_font_handle(&lf);
3364 lf.lfItalic = FALSE;
3365 }
3366 if (lf.lfWeight < FW_BOLD)
3367 {
3368 lf.lfWeight = FW_BOLD;
3369 gui.wide_bold_font = get_font_handle(&lf);
3370 if (!lf.lfItalic)
3371 {
3372 lf.lfItalic = TRUE;
3373 gui.wide_boldital_font = get_font_handle(&lf);
3374 }
3375 }
3376 }
3377}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378
3379/*
3380 * Initialise vim to use the font with the given name.
3381 * Return FAIL if the font could not be loaded, OK otherwise.
3382 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003383 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003384gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385{
K.Takatac81e9bf2022-01-16 14:15:49 +00003386 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 GuiFont font = NOFONT;
3388 char_u *p;
3389
Bram Moolenaar734a8672019-12-02 22:49:38 +01003390 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003392 {
3393 lfOrig = lf;
3394 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003396 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397 if (font == NOFONT)
3398 return FAIL;
3399
3400 if (font_name == NULL)
3401 font_name = (char_u *)lf.lfFaceName;
3402#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3403 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003404#endif
3405#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406 sub_logfont = lf;
3407#endif
3408#ifdef FEAT_MBYTE_IME
K.Takatac81e9bf2022-01-16 14:15:49 +00003409 if (!s_in_dpichanged)
3410 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411#endif
3412 gui_mch_free_font(gui.norm_font);
3413 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003414 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 GetFontSize(font);
3416
K.Takatac81e9bf2022-01-16 14:15:49 +00003417 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003418 if (p != NULL)
3419 {
3420 hl_set_font_name(p);
3421
Bram Moolenaar734a8672019-12-02 22:49:38 +01003422 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3424 {
3425 vim_free(p_guifont);
3426 p_guifont = p;
3427 }
3428 else
3429 vim_free(p);
3430 }
3431
3432 gui_mch_free_font(gui.ital_font);
3433 gui.ital_font = NOFONT;
3434 gui_mch_free_font(gui.bold_font);
3435 gui.bold_font = NOFONT;
3436 gui_mch_free_font(gui.boldital_font);
3437 gui.boldital_font = NOFONT;
3438
3439 if (!lf.lfItalic)
3440 {
3441 lf.lfItalic = TRUE;
3442 gui.ital_font = get_font_handle(&lf);
3443 lf.lfItalic = FALSE;
3444 }
3445 if (lf.lfWeight < FW_BOLD)
3446 {
3447 lf.lfWeight = FW_BOLD;
3448 gui.bold_font = get_font_handle(&lf);
3449 if (!lf.lfItalic)
3450 {
3451 lf.lfItalic = TRUE;
3452 gui.boldital_font = get_font_handle(&lf);
3453 }
3454 }
3455
3456 return OK;
3457}
3458
3459#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003460# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461#endif
3462
3463/*
3464 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003465 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466 */
3467 int
3468gui_mch_maximized(void)
3469{
3470 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003471 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472
3473 wp.length = sizeof(WINDOWPLACEMENT);
3474 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003475 {
3476 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003477 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003478 && wp.flags == WPF_RESTORETOMAXIMIZED))
3479 return TRUE;
3480 if (wp.showCmd == SW_SHOWMINIMIZED)
3481 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003483 // Assume the window is snapped when the sizes from two APIs differ.
3484 GetWindowRect(s_hwnd, &rc);
3485 if ((rc.right - rc.left !=
3486 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3487 || (rc.bottom - rc.top !=
3488 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3489 return TRUE;
3490 }
3491 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492}
3493
3494/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003495 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3496 * is set. Compute the new Rows and Columns. This is like resizing the
3497 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498 */
3499 void
3500gui_mch_newfont(void)
3501{
3502 RECT rect;
3503
3504 GetWindowRect(s_hwnd, &rect);
3505 if (win_socket_id == 0)
3506 {
3507 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003508 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3509 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003511 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3512 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3513 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3514 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515 }
3516 else
3517 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003518 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003520 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003521 }
3522}
3523
3524/*
3525 * Set the window title
3526 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 void
3528gui_mch_settitle(
3529 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003530 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531{
3532 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3533}
3534
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003535#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003536// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3537// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003538static LPCSTR mshape_idcs[] =
3539{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003540 IDC_ARROW, // arrow
3541 MAKEINTRESOURCE(0), // blank
3542 IDC_IBEAM, // beam
3543 IDC_SIZENS, // updown
3544 IDC_SIZENS, // udsizing
3545 IDC_SIZEWE, // leftright
3546 IDC_SIZEWE, // lrsizing
3547 IDC_WAIT, // busy
3548 IDC_NO, // no
3549 IDC_ARROW, // crosshair
3550 IDC_ARROW, // hand1
3551 IDC_ARROW, // hand2
3552 IDC_ARROW, // pencil
3553 IDC_ARROW, // question
3554 IDC_ARROW, // right-arrow
3555 IDC_UPARROW, // up-arrow
3556 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557};
3558
3559 void
3560mch_set_mouse_shape(int shape)
3561{
3562 LPCSTR idc;
3563
3564 if (shape == MSHAPE_HIDE)
3565 ShowCursor(FALSE);
3566 else
3567 {
3568 if (shape >= MSHAPE_NUMBERED)
3569 idc = IDC_ARROW;
3570 else
3571 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003572 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 if (!p_mh)
3574 {
3575 POINT mp;
3576
Bram Moolenaar734a8672019-12-02 22:49:38 +01003577 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578 (void)GetCursorPos((LPPOINT)&mp);
3579 (void)SetCursorPos(mp.x, mp.y);
3580 ShowCursor(TRUE);
3581 }
3582 }
3583}
3584#endif
3585
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003586#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588 * Wide version of convert_filter().
3589 */
3590 static WCHAR *
3591convert_filterW(char_u *s)
3592{
3593 char_u *tmp;
3594 int len;
3595 WCHAR *res;
3596
3597 tmp = convert_filter(s);
3598 if (tmp == NULL)
3599 return NULL;
3600 len = (int)STRLEN(s) + 3;
3601 res = enc_to_utf16(tmp, &len);
3602 vim_free(tmp);
3603 return res;
3604}
3605
3606/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003607 * Pop open a file browser and return the file selected, in allocated memory,
3608 * or NULL if Cancel is hit.
3609 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3610 * title - Title message for the file browser dialog.
3611 * dflt - Default name of file.
3612 * ext - Default extension to be added to files without extensions.
3613 * initdir - directory in which to open the browser (NULL = current dir)
3614 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003616 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003617gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618 int saving,
3619 char_u *title,
3620 char_u *dflt,
3621 char_u *ext,
3622 char_u *initdir,
3623 char_u *filter)
3624{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003625 // We always use the wide function. This means enc_to_utf16() must work,
3626 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 OPENFILENAMEW fileStruct;
3628 WCHAR fileBuf[MAXPATHL];
3629 WCHAR *wp;
3630 int i;
3631 WCHAR *titlep = NULL;
3632 WCHAR *extp = NULL;
3633 WCHAR *initdirp = NULL;
3634 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003635 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003636
3637 if (dflt == NULL)
3638 fileBuf[0] = NUL;
3639 else
3640 {
3641 wp = enc_to_utf16(dflt, NULL);
3642 if (wp == NULL)
3643 fileBuf[0] = NUL;
3644 else
3645 {
3646 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3647 fileBuf[i] = wp[i];
3648 fileBuf[i] = NUL;
3649 vim_free(wp);
3650 }
3651 }
3652
Bram Moolenaar734a8672019-12-02 22:49:38 +01003653 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003654 filterp = convert_filterW(filter);
3655
Bram Moolenaara80faa82020-04-12 19:37:17 +02003656 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003657# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003658 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003659 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003660# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003661 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003662# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663
3664 if (title != NULL)
3665 titlep = enc_to_utf16(title, NULL);
3666 fileStruct.lpstrTitle = titlep;
3667
3668 if (ext != NULL)
3669 extp = enc_to_utf16(ext, NULL);
3670 fileStruct.lpstrDefExt = extp;
3671
3672 fileStruct.lpstrFile = fileBuf;
3673 fileStruct.nMaxFile = MAXPATHL;
3674 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003675 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3676 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 if (initdir != NULL && *initdir != NUL)
3678 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003679 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680 initdirp = enc_to_utf16(initdir, NULL);
3681 if (initdirp != NULL)
3682 {
3683 for (wp = initdirp; *wp != NUL; ++wp)
3684 if (*wp == '/')
3685 *wp = '\\';
3686 }
3687 fileStruct.lpstrInitialDir = initdirp;
3688 }
3689
3690 /*
3691 * TODO: Allow selection of multiple files. Needs another arg to this
3692 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3693 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3694 * files that don't exist yet, so I haven't put it in. What about
3695 * OFN_PATHMUSTEXIST?
3696 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3697 */
3698 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003699# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 if (curbuf->b_p_bin)
3701 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003702# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003703 if (saving)
3704 {
3705 if (!GetSaveFileNameW(&fileStruct))
3706 return NULL;
3707 }
3708 else
3709 {
3710 if (!GetOpenFileNameW(&fileStruct))
3711 return NULL;
3712 }
3713
3714 vim_free(filterp);
3715 vim_free(initdirp);
3716 vim_free(titlep);
3717 vim_free(extp);
3718
Bram Moolenaar734a8672019-12-02 22:49:38 +01003719 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003721 if (p == NULL)
3722 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725 SetFocus(s_hwnd);
3726
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003728 q = vim_strsave(shorten_fname1(p));
3729 vim_free(p);
3730 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003731}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732
3733
3734/*
3735 * Convert the string s to the proper format for a filter string by replacing
3736 * the \t and \n delimiters with \0.
3737 * Returns the converted string in allocated memory.
3738 *
3739 * Keep in sync with convert_filterW() above!
3740 */
3741 static char_u *
3742convert_filter(char_u *s)
3743{
3744 char_u *res;
3745 unsigned s_len = (unsigned)STRLEN(s);
3746 unsigned i;
3747
3748 res = alloc(s_len + 3);
3749 if (res != NULL)
3750 {
3751 for (i = 0; i < s_len; ++i)
3752 if (s[i] == '\t' || s[i] == '\n')
3753 res[i] = '\0';
3754 else
3755 res[i] = s[i];
3756 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003757 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 res[s_len + 1] = NUL;
3759 res[s_len + 2] = NUL;
3760 }
3761 return res;
3762}
3763
3764/*
3765 * Select a directory.
3766 */
3767 char_u *
3768gui_mch_browsedir(char_u *title, char_u *initdir)
3769{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003770 // We fake this: Use a filter that doesn't select anything and a default
3771 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3773 initdir, (char_u *)_("Directory\t*.nothing\n"));
3774}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003775#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777 static void
3778_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003779 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 HDROP hDrop)
3781{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003782#define BUFPATHLEN _MAX_PATH
3783#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785 char szFile[BUFPATHLEN];
3786 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3787 UINT i;
3788 char_u **fnames;
3789 POINT pt;
3790 int_u modifiers = 0;
3791
Bram Moolenaar734a8672019-12-02 22:49:38 +01003792 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793
Bram Moolenaar734a8672019-12-02 22:49:38 +01003794 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 DragQueryPoint(hDrop, &pt);
3796 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3797
3798 reset_VIsual();
3799
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003800 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801
3802 if (fnames != NULL)
3803 for (i = 0; i < cFiles; ++i)
3804 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3806 fnames[i] = utf16_to_enc(wszFile, NULL);
3807 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808 {
3809 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3810 fnames[i] = vim_strsave((char_u *)szFile);
3811 }
3812 }
3813
3814 DragFinish(hDrop);
3815
3816 if (fnames != NULL)
3817 {
3818 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3819 modifiers |= MOUSE_SHIFT;
3820 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3821 modifiers |= MOUSE_CTRL;
3822 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3823 modifiers |= MOUSE_ALT;
3824
3825 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3826
3827 s_need_activate = TRUE;
3828 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829}
3830
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003831 static int
3832_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003833 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834 HWND hwndCtl,
3835 UINT code,
3836 int pos)
3837{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003838 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 scrollbar_T *sb, *sb_info;
3840 long val;
3841 int dragging = FALSE;
3842 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843 SCROLLINFO si;
3844
3845 si.cbSize = sizeof(si);
3846 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847
3848 sb = gui_mswin_find_scrollbar(hwndCtl);
3849 if (sb == NULL)
3850 return 0;
3851
Bram Moolenaar734a8672019-12-02 22:49:38 +01003852 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853 {
3854 /*
3855 * Careful: need to get scrollbar info out of first (left) scrollbar
3856 * for window, but keep real scrollbar too because we must pass it to
3857 * gui_drag_scrollbar().
3858 */
3859 sb_info = &sb->wp->w_scrollbars[0];
3860 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003861 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 sb_info = sb;
3863 val = sb_info->value;
3864
3865 switch (code)
3866 {
3867 case SB_THUMBTRACK:
3868 val = pos;
3869 dragging = TRUE;
3870 if (sb->scroll_shift > 0)
3871 val <<= sb->scroll_shift;
3872 break;
3873 case SB_LINEDOWN:
3874 val++;
3875 break;
3876 case SB_LINEUP:
3877 val--;
3878 break;
3879 case SB_PAGEDOWN:
3880 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3881 break;
3882 case SB_PAGEUP:
3883 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3884 break;
3885 case SB_TOP:
3886 val = 0;
3887 break;
3888 case SB_BOTTOM:
3889 val = sb_info->max;
3890 break;
3891 case SB_ENDSCROLL:
3892 if (prev_code == SB_THUMBTRACK)
3893 {
3894 /*
3895 * "pos" only gives us 16-bit data. In case of large file,
3896 * use GetScrollPos() which returns 32-bit. Unfortunately it
3897 * is not valid while the scrollbar is being dragged.
3898 */
3899 val = GetScrollPos(hwndCtl, SB_CTL);
3900 if (sb->scroll_shift > 0)
3901 val <<= sb->scroll_shift;
3902 }
3903 break;
3904
3905 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003906 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907 return 0;
3908 }
3909 prev_code = code;
3910
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003911 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3912 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913
3914 /*
3915 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3916 */
3917 if (sb->wp != NULL)
3918 {
3919 scrollbar_T *sba = sb->wp->w_scrollbars;
3920 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3921
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003923 }
3924
Bram Moolenaar734a8672019-12-02 22:49:38 +01003925 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003926 s_busy_processing = TRUE;
3927
Bram Moolenaar734a8672019-12-02 22:49:38 +01003928 // When "allow_scrollbar" is FALSE still need to remember the new
3929 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003930 dont_scroll = !allow_scrollbar;
3931
Bram Moolenaara338adc2018-01-31 20:51:47 +01003932 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003934 mch_enable_flush();
3935 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003936
3937 s_busy_processing = FALSE;
3938 dont_scroll = dont_scroll_save;
3939
3940 return 0;
3941}
3942
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#ifdef FEAT_XPM_W32
3945# include "xpm_w32.h"
3946#endif
3947
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#ifdef __MINGW32__
3949/*
3950 * Add a lot of missing defines.
3951 * They are not always missing, we need the #ifndef's.
3952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953# ifndef IsMinimized
3954# define IsMinimized(hwnd) IsIconic(hwnd)
3955# endif
3956# ifndef IsMaximized
3957# define IsMaximized(hwnd) IsZoomed(hwnd)
3958# endif
3959# ifndef SelectFont
3960# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3961# endif
3962# ifndef GetStockBrush
3963# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3964# endif
3965# ifndef DeleteBrush
3966# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3967# endif
3968
3969# ifndef HANDLE_WM_RBUTTONDBLCLK
3970# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3971 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3972# endif
3973# ifndef HANDLE_WM_MBUTTONUP
3974# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3975 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3976# endif
3977# ifndef HANDLE_WM_MBUTTONDBLCLK
3978# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3979 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3980# endif
3981# ifndef HANDLE_WM_LBUTTONDBLCLK
3982# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3983 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3984# endif
3985# ifndef HANDLE_WM_RBUTTONDOWN
3986# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3987 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3988# endif
3989# ifndef HANDLE_WM_MOUSEMOVE
3990# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3991 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3992# endif
3993# ifndef HANDLE_WM_RBUTTONUP
3994# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3995 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3996# endif
3997# ifndef HANDLE_WM_MBUTTONDOWN
3998# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3999 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4000# endif
4001# ifndef HANDLE_WM_LBUTTONUP
4002# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4003 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4004# endif
4005# ifndef HANDLE_WM_LBUTTONDOWN
4006# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4007 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4008# endif
4009# ifndef HANDLE_WM_SYSCHAR
4010# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4011 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4012# endif
4013# ifndef HANDLE_WM_ACTIVATEAPP
4014# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4015 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4016# endif
4017# ifndef HANDLE_WM_WINDOWPOSCHANGING
4018# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4019 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4020# endif
4021# ifndef HANDLE_WM_VSCROLL
4022# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4023 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4024# endif
4025# ifndef HANDLE_WM_SETFOCUS
4026# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4027 ((fn)((hwnd), (HWND)(wParam)), 0L)
4028# endif
4029# ifndef HANDLE_WM_KILLFOCUS
4030# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4031 ((fn)((hwnd), (HWND)(wParam)), 0L)
4032# endif
4033# ifndef HANDLE_WM_HSCROLL
4034# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4035 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4036# endif
4037# ifndef HANDLE_WM_DROPFILES
4038# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4039 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4040# endif
4041# ifndef HANDLE_WM_CHAR
4042# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4043 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4044# endif
4045# ifndef HANDLE_WM_SYSDEADCHAR
4046# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4047 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4048# endif
4049# ifndef HANDLE_WM_DEADCHAR
4050# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4051 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4052# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004053#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055
Bram Moolenaar734a8672019-12-02 22:49:38 +01004056// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#define TEAROFF_PADDING_X 2
4058#define TEAROFF_BUTTON_PAD_X 8
4059#define TEAROFF_MIN_WIDTH 200
4060#define TEAROFF_SUBMENU_LABEL ">>"
4061#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4062
4063
Bram Moolenaar734a8672019-12-02 22:49:38 +01004064// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004066# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#endif
4068
4069
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004070#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071# define ID_BEVAL_TOOLTIP 200
4072# define BEVAL_TEXT_LEN MAXPATHL
4073
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004074# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004075// Work around old versions of basetsd.h which wrongly declares
4076// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004077# undef UINT_PTR
4078# define UINT_PTR UINT
4079# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004082static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004084
Bram Moolenaar82881492012-11-20 16:53:39 +01004085
Bram Moolenaar734a8672019-12-02 22:49:38 +01004086// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004087# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004088
Bram Moolenaar45360022005-07-21 21:08:21 +00004089/*
4090 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004091 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004092 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004093# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004094
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004095# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004096
4097typedef struct _DllVersionInfo
4098{
4099 DWORD cbSize;
4100 DWORD dwMajorVersion;
4101 DWORD dwMinorVersion;
4102 DWORD dwBuildNumber;
4103 DWORD dwPlatformID;
4104} DLLVERSIONINFO;
4105
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004106# ifndef PROTO
4107# include <poppack.h>
4108# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004109
Bram Moolenaar45360022005-07-21 21:08:21 +00004110typedef struct tagTOOLINFOA_NEW
4111{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004112 UINT cbSize;
4113 UINT uFlags;
4114 HWND hwnd;
4115 UINT_PTR uId;
4116 RECT rect;
4117 HINSTANCE hinst;
4118 LPSTR lpszText;
4119 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004120} TOOLINFO_NEW;
4121
4122typedef struct tagNMTTDISPINFO_NEW
4123{
4124 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004125 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004126 char szText[80];
4127 HINSTANCE hinst;
4128 UINT uFlags;
4129 LPARAM lParam;
4130} NMTTDISPINFO_NEW;
4131
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004132typedef struct tagTOOLINFOW_NEW
4133{
4134 UINT cbSize;
4135 UINT uFlags;
4136 HWND hwnd;
4137 UINT_PTR uId;
4138 RECT rect;
4139 HINSTANCE hinst;
4140 LPWSTR lpszText;
4141 LPARAM lParam;
4142 void *lpReserved;
4143} TOOLINFOW_NEW;
4144
4145typedef struct tagNMTTDISPINFOW_NEW
4146{
4147 NMHDR hdr;
4148 LPWSTR lpszText;
4149 WCHAR szText[80];
4150 HINSTANCE hinst;
4151 UINT uFlags;
4152 LPARAM lParam;
4153} NMTTDISPINFOW_NEW;
4154
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004155
Bram Moolenaar45360022005-07-21 21:08:21 +00004156typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004157# ifndef TTM_SETMAXTIPWIDTH
4158# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004161# ifndef TTF_DI_SETITEM
4162# define TTF_DI_SETITEM 0x8000
4163# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004164
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004165# ifndef TTN_GETDISPINFO
4166# define TTN_GETDISPINFO (TTN_FIRST - 0)
4167# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004168
Bram Moolenaar734a8672019-12-02 22:49:38 +01004169#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004170
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004171#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004172// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4173// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004174// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004175// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004176# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4177typedef struct tagNMTTDISPINFOA {
4178 NMHDR hdr;
4179 LPSTR lpszText;
4180 char szText[80];
4181 HINSTANCE hinst;
4182 UINT uFlags;
4183 LPARAM lParam;
4184} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4185# define LPNMTTDISPINFO LPNMTTDISPINFOA
4186
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004187typedef struct tagNMTTDISPINFOW {
4188 NMHDR hdr;
4189 LPWSTR lpszText;
4190 WCHAR szText[80];
4191 HINSTANCE hinst;
4192 UINT uFlags;
4193 LPARAM lParam;
4194} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004195# endif
4196#endif
4197
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004198#ifndef TTN_GETDISPINFOW
4199# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4200#endif
4201
Bram Moolenaar734a8672019-12-02 22:49:38 +01004202// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204#ifdef FEAT_MENU
4205static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208/*
4209 * Use the system font for dialogs and tear-off menus. Remove this line to
4210 * use DLG_FONT_NAME.
4211 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004212#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#define VIM_CLASSW L"Vim"
4216
Bram Moolenaar734a8672019-12-02 22:49:38 +01004217// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4218// thus there should be room for every dialog. For tearoffs it's made bigger
4219// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#define DLG_ALLOC_SIZE 16 * 1024
4221
4222/*
4223 * stuff for dialogs, menus, tearoffs etc.
4224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225static PWORD
4226add_dialog_element(
4227 PWORD p,
4228 DWORD lStyle,
4229 WORD x,
4230 WORD y,
4231 WORD w,
4232 WORD h,
4233 WORD Id,
4234 WORD clss,
4235 const char *caption);
4236static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004237static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004238#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241static void get_dialog_font_metrics(void);
4242
4243static int dialog_default_button = -1;
4244
Bram Moolenaar734a8672019-12-02 22:49:38 +01004245// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar734a8672019-12-02 22:49:38 +01004248static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#ifdef FEAT_TOOLBAR
4250static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004251static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004252static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004254#else
4255# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif
4257
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004258#ifdef FEAT_GUI_TABLINE
4259static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004260static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004261#endif
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef FEAT_MBYTE_IME
4264static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4265static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4266#endif
4267#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4268# ifdef NOIME
4269typedef struct tagCOMPOSITIONFORM {
4270 DWORD dwStyle;
4271 POINT ptCurrentPos;
4272 RECT rcArea;
4273} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4274typedef HANDLE HIMC;
4275# endif
4276
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004277static HINSTANCE hLibImm = NULL;
4278static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4279static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4280static HIMC (WINAPI *pImmGetContext)(HWND);
4281static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4282static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4283static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4284static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004285static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4286static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004287static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4288static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004289static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290static void dyn_imm_load(void);
4291#else
4292# define pImmGetCompositionStringA ImmGetCompositionStringA
4293# define pImmGetCompositionStringW ImmGetCompositionStringW
4294# define pImmGetContext ImmGetContext
4295# define pImmAssociateContext ImmAssociateContext
4296# define pImmReleaseContext ImmReleaseContext
4297# define pImmGetOpenStatus ImmGetOpenStatus
4298# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004299# define pImmGetCompositionFontW ImmGetCompositionFontW
4300# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301# define pImmSetCompositionWindow ImmSetCompositionWindow
4302# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004303# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304#endif
4305
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306#ifdef FEAT_MENU
4307/*
4308 * Figure out how high the menu bar is at the moment.
4309 */
4310 static int
4311gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004312 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
4314 static int old_menu_height = -1;
4315
4316 RECT rc1, rc2;
4317 int num;
4318 int menu_height;
4319
4320 if (gui.menu_is_active)
4321 num = GetMenuItemCount(s_menuBar);
4322 else
4323 num = 0;
4324
4325 if (num == 0)
4326 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004327 else if (IsMinimized(s_hwnd))
4328 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004329 // The height of the menu cannot be determined while the window is
4330 // minimized. Take the previous height if the menu is changed in that
4331 // state, to avoid that Vim's vertical window size accidentally
4332 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004333 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 else
4336 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004337 /*
4338 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4339 * seem to have been set yet, so menu wraps in default window
4340 * width which is very narrow. Instead just return height of a
4341 * single menu item. Will still be wrong when the menu really
4342 * should wrap over more than one line.
4343 */
4344 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4345 if (gui.starting)
4346 menu_height = rc1.bottom - rc1.top + 1;
4347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004349 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4350 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004355 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004356 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
4358 return menu_height;
4359}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004360#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362
4363/*
4364 * Setup for the Intellimouse
4365 */
4366 static void
4367init_mouse_wheel(void)
4368{
4369
4370#ifndef SPI_GETWHEELSCROLLLINES
4371# define SPI_GETWHEELSCROLLLINES 104
4372#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004373#ifndef SPI_SETWHEELSCROLLLINES
4374# define SPI_SETWHEELSCROLLLINES 105
4375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
Bram Moolenaar734a8672019-12-02 22:49:38 +01004377#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4378#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4380#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4381
Bram Moolenaar734a8672019-12-02 22:49:38 +01004382 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaar734a8672019-12-02 22:49:38 +01004384 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004385 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4386 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387}
4388
4389
Bram Moolenaarae20f342019-11-05 21:09:23 +01004390/*
4391 * Intellimouse wheel handler.
4392 * Treat a mouse wheel event as if it were a scroll request.
4393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 static void
4395_OnMouseWheel(
4396 HWND hwnd,
4397 short zDelta)
4398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int i;
4400 int size;
4401 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004402 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (mouse_scroll_lines == 0)
4405 init_mouse_wheel();
4406
Bram Moolenaarae20f342019-11-05 21:09:23 +01004407 wp = gui_mouse_window(FIND_POPUP);
4408
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004409#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004410 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004411 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004412 cmdarg_T cap;
4413 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004414
Bram Moolenaarae20f342019-11-05 21:09:23 +01004415 // Mouse hovers over popup window, scroll it if possible.
4416 mouse_row = wp->w_winrow;
4417 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004418 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004419 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4420 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4421 clear_oparg(&oa);
4422 cap.oap = &oa;
4423 nv_mousescroll(&cap);
4424 update_screen(0);
4425 setcursor();
4426 out_flush();
4427 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004428 }
4429#endif
4430
Bram Moolenaarae20f342019-11-05 21:09:23 +01004431 if (wp == NULL || !p_scf)
4432 wp = curwin;
4433
4434 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4435 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4436 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4437 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4438 else
4439 return;
4440 size = wp->w_height;
4441
Bram Moolenaara338adc2018-01-31 20:51:47 +01004442 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 if (mouse_scroll_lines > 0
4444 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4445 {
4446 for (i = mouse_scroll_lines; i > 0; --i)
4447 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4448 }
4449 else
4450 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004451 mch_enable_flush();
4452 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453}
4454
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004455#ifdef USE_SYSMENU_FONT
4456/*
4457 * Get Menu Font.
4458 * Return OK or FAIL.
4459 */
4460 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004461gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004462{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004463 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004464
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004465 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4466 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004467 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004468 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004469 &nm,
4470 0))
4471 return FAIL;
4472 *lf = nm.lfMenuFont;
4473 return OK;
4474}
4475#endif
4476
4477
4478#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4479/*
4480 * Set the GUI tabline font to the system menu font
4481 */
4482 static void
4483set_tabline_font(void)
4484{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004485 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004486 HFONT font;
4487 HWND hwnd;
4488 HDC hdc;
4489 HFONT hfntOld;
4490 TEXTMETRIC tm;
4491
4492 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4493 return;
4494
K.Takatac81e9bf2022-01-16 14:15:49 +00004495 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004496 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004497
4498 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4499
4500 /*
4501 * Compute the height of the font used for the tab text
4502 */
4503 hwnd = GetDesktopWindow();
4504 hdc = GetWindowDC(hwnd);
4505 hfntOld = SelectFont(hdc, font);
4506
4507 GetTextMetrics(hdc, &tm);
4508
4509 SelectFont(hdc, hfntOld);
4510 ReleaseDC(hwnd, hdc);
4511
4512 /*
4513 * The space used by the tab border and the space between the tab label
4514 * and the tab border is included as 7.
4515 */
4516 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4517}
K.Takatac81e9bf2022-01-16 14:15:49 +00004518#else
4519# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004520#endif
4521
Bram Moolenaar520470a2005-06-16 21:59:56 +00004522/*
4523 * Invoked when a setting was changed.
4524 */
4525 static LRESULT CALLBACK
4526_OnSettingChange(UINT n)
4527{
4528 if (n == SPI_SETWHEELSCROLLLINES)
4529 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4530 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004531 if (n == SPI_SETNONCLIENTMETRICS)
4532 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004533 return 0;
4534}
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#ifdef FEAT_NETBEANS_INTG
4537 static void
4538_OnWindowPosChanged(
4539 HWND hwnd,
4540 const LPWINDOWPOS lpwpos)
4541{
4542 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004543 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4546 || lpwpos->cx != cx || lpwpos->cy != cy))
4547 {
4548 x = lpwpos->x;
4549 y = lpwpos->y;
4550 cx = lpwpos->cx;
4551 cy = lpwpos->cy;
4552 netbeans_frame_moved(x, y);
4553 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004554 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4556}
4557#endif
4558
K.Takata4f2417f2021-06-05 16:25:32 +02004559
4560static HWND hwndTip = NULL;
4561
4562 static void
4563show_sizing_tip(int cols, int rows)
4564{
4565 TOOLINFOA ti = {sizeof(ti)};
4566 char buf[32];
4567
4568 ti.hwnd = s_hwnd;
4569 ti.uId = (UINT_PTR)s_hwnd;
4570 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4571 ti.lpszText = buf;
4572 sprintf(buf, "%dx%d", cols, rows);
4573 if (hwndTip == NULL)
4574 {
4575 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4576 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4577 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4578 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4579 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4580 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4581 }
4582 else
4583 {
4584 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4585 }
4586 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4587}
4588
4589 static void
4590destroy_sizing_tip(void)
4591{
4592 if (hwndTip != NULL)
4593 {
4594 DestroyWindow(hwndTip);
4595 hwndTip = NULL;
4596 }
4597}
4598
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 static int
4600_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 UINT fwSide,
4602 LPRECT lprc)
4603{
4604 int w, h;
4605 int valid_w, valid_h;
4606 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004607 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609 w = lprc->right - lprc->left;
4610 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004611 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 w_offset = w - valid_w;
4613 h_offset = h - valid_h;
4614
4615 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4616 || fwSide == WMSZ_BOTTOMLEFT)
4617 lprc->left += w_offset;
4618 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4619 || fwSide == WMSZ_BOTTOMRIGHT)
4620 lprc->right -= w_offset;
4621
4622 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4623 || fwSide == WMSZ_TOPRIGHT)
4624 lprc->top += h_offset;
4625 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4626 || fwSide == WMSZ_BOTTOMRIGHT)
4627 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004628
4629 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 return TRUE;
4631}
4632
K.Takatac81e9bf2022-01-16 14:15:49 +00004633 static LRESULT
4634_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4635{
4636 s_dpi = ydpi;
4637 s_in_dpichanged = TRUE;
4638 //TRACE("DPI: %d", ydpi);
4639
4640 update_scrollbar_size();
4641 update_toolbar_size();
4642 set_tabline_font();
4643
4644 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4645 gui_get_wide_font();
4646 gui_mswin_get_menu_height(FALSE);
4647#ifdef FEAT_MBYTE_IME
4648 im_set_position(gui.row, gui.col);
4649#endif
4650 InvalidateRect(hwnd, NULL, TRUE);
4651
4652 s_in_dpichanged = FALSE;
4653 return 0L;
4654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656
4657 static LRESULT CALLBACK
4658_WndProc(
4659 HWND hwnd,
4660 UINT uMsg,
4661 WPARAM wParam,
4662 LPARAM lParam)
4663{
4664 /*
4665 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4666 hwnd, uMsg, wParam, lParam);
4667 */
4668
4669 HandleMouseHide(uMsg, lParam);
4670
4671 s_uMsg = uMsg;
4672 s_wParam = wParam;
4673 s_lParam = lParam;
4674
4675 switch (uMsg)
4676 {
4677 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4678 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004679 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004681 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4683 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4684 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4685 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4686#ifdef FEAT_MENU
4687 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4688#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004689 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4690 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4692 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004693 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4694 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4696 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4697 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4698#ifdef FEAT_NETBEANS_INTG
4699 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4700#endif
4701
Bram Moolenaarafa24992006-03-27 20:58:26 +00004702#ifdef FEAT_GUI_TABLINE
4703 case WM_RBUTTONUP:
4704 {
4705 if (gui_mch_showing_tabline())
4706 {
4707 POINT pt;
4708 RECT rect;
4709
4710 /*
4711 * If the cursor is on the tabline, display the tab menu
4712 */
4713 GetCursorPos((LPPOINT)&pt);
4714 GetWindowRect(s_textArea, &rect);
4715 if (pt.y < rect.top)
4716 {
4717 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004718 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004719 }
4720 }
4721 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4722 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004723 case WM_LBUTTONDBLCLK:
4724 {
4725 /*
4726 * If the user double clicked the tabline, create a new tab
4727 */
4728 if (gui_mch_showing_tabline())
4729 {
4730 POINT pt;
4731 RECT rect;
4732
4733 GetCursorPos((LPPOINT)&pt);
4734 GetWindowRect(s_textArea, &rect);
4735 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004736 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004737 }
4738 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4739 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004740#endif
4741
Bram Moolenaar734a8672019-12-02 22:49:38 +01004742 case WM_QUERYENDSESSION: // System wants to go down.
4743 gui_shell_closed(); // Will exit when no changed buffers.
4744 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004747 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004750 return 0L;
4751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 break;
4753
4754 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004755 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4756 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004757 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 return 0L;
4759
4760 case WM_SYSCHAR:
4761 /*
4762 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4763 * shortcut key, handle like a typed ALT key, otherwise call Windows
4764 * ALT key handling.
4765 */
4766#ifdef FEAT_MENU
4767 if ( !gui.menu_is_active
4768 || p_wak[0] == 'n'
4769 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4770 )
4771#endif
4772 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004773 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 return 0L;
4775 }
4776#ifdef FEAT_MENU
4777 else
4778 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4779#endif
4780
4781 case WM_SYSKEYUP:
4782#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004783 // This used to be done only when menu is active: ALT key is used for
4784 // that. But that caused problems when menu is disabled and using
4785 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4786 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4788#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004789 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#endif
4791
K.Takata4f2417f2021-06-05 16:25:32 +02004792 case WM_EXITSIZEMOVE:
4793 destroy_sizing_tip();
4794 break;
4795
Bram Moolenaar734a8672019-12-02 22:49:38 +01004796 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 case WM_MOUSEWHEEL:
4800 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004801 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaar734a8672019-12-02 22:49:38 +01004803 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004804 case WM_SETTINGCHANGE:
4805 return _OnSettingChange((UINT)wParam);
4806
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004807#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 case WM_NOTIFY:
4809 switch (((LPNMHDR) lParam)->code)
4810 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004811 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004812 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004814 LPNMHDR hdr = (LPNMHDR)lParam;
4815 char_u *str = NULL;
4816 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004817
Bram Moolenaard23a8232018-02-10 18:45:26 +01004818 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004819
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004820# ifdef FEAT_GUI_TABLINE
4821 if (gui_mch_showing_tabline()
4822 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004823 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004824 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004825 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004826 * Mouse is over the GUI tabline. Display the
4827 * tooltip for the tab under the cursor
4828 *
4829 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004830 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004831 GetCursorPos(&pt);
4832 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004833 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004834 TCHITTESTINFO htinfo;
4835 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004836
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004837 /*
4838 * Get the tab under the cursor
4839 */
4840 htinfo.pt.x = pt.x;
4841 htinfo.pt.y = pt.y;
4842 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4843 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004844 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004845 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004846
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004847 tp = find_tabpage(idx + 1);
4848 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004849 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004850 get_tabline_label(tp, TRUE);
4851 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004852 }
4853 }
4854 }
4855 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004856# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004857# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004858# ifdef FEAT_GUI_TABLINE
4859 else
4860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004862 UINT idButton;
4863 vimmenu_T *pMenu;
4864
4865 idButton = (UINT) hdr->idFrom;
4866 pMenu = gui_mswin_find_menu(root_menu, idButton);
4867 if (pMenu)
4868 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004870# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004871 if (str != NULL)
4872 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004873 if (hdr->code == TTN_GETDISPINFOW)
4874 {
4875 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4876
Bram Moolenaar734a8672019-12-02 22:49:38 +01004877 // Set the maximum width, this also enables using
4878 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004879 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4880 0, 500);
4881
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004882 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004883 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004884 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004885 }
4886 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004887 {
4888 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4889
Bram Moolenaar734a8672019-12-02 22:49:38 +01004890 // Set the maximum width, this also enables using
4891 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004892 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4893 0, 500);
4894
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004895 if (STRLEN(str) < sizeof(lpdi->szText)
4896 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004897 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004898 sizeof(lpdi->szText) - 1);
4899 else
4900 lpdi->lpszText = tt_text;
4901 }
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004905# ifdef FEAT_GUI_TABLINE
4906 case TCN_SELCHANGE:
4907 if (gui_mch_showing_tabline()
4908 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004909 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004910 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004911 return 0L;
4912 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004913 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004914
4915 case NM_RCLICK:
4916 if (gui_mch_showing_tabline()
4917 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004918 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004919 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004920 return 0L;
4921 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004922 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004925# ifdef FEAT_GUI_TABLINE
4926 if (gui_mch_showing_tabline()
4927 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4928 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 break;
4931 }
4932 break;
4933#endif
4934#if defined(MENUHINTS) && defined(FEAT_MENU)
4935 case WM_MENUSELECT:
4936 if (((UINT) HIWORD(wParam)
4937 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4938 == MF_HILITE
4939 && (State & CMDLINE) == 0)
4940 {
4941 UINT idButton;
4942 vimmenu_T *pMenu;
4943 static int did_menu_tip = FALSE;
4944
4945 if (did_menu_tip)
4946 {
4947 msg_clr_cmdline();
4948 setcursor();
4949 out_flush();
4950 did_menu_tip = FALSE;
4951 }
4952
4953 idButton = (UINT)LOWORD(wParam);
4954 pMenu = gui_mswin_find_menu(root_menu, idButton);
4955 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4956 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4957 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004958 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004959 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004960 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 setcursor();
4962 out_flush();
4963 did_menu_tip = TRUE;
4964 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004965 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967 break;
4968#endif
4969 case WM_NCHITTEST:
4970 {
4971 LRESULT result;
4972 int x, y;
4973 int xPos = GET_X_LPARAM(lParam);
4974
4975 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4976 if (result == HTCLIENT)
4977 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004978#ifdef FEAT_GUI_TABLINE
4979 if (gui_mch_showing_tabline())
4980 {
4981 int yPos = GET_Y_LPARAM(lParam);
4982 RECT rct;
4983
Bram Moolenaar734a8672019-12-02 22:49:38 +01004984 // If the cursor is on the GUI tabline, don't process this
4985 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004986 GetWindowRect(s_textArea, &rct);
4987 if (yPos < rct.top)
4988 return result;
4989 }
4990#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004991 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 xPos -= x;
4993
Bram Moolenaar734a8672019-12-02 22:49:38 +01004994 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 return HTBOTTOMLEFT;
4996 else
4997 return HTBOTTOMRIGHT;
4998 }
4999 else
5000 return result;
5001 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005002 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004#ifdef FEAT_MBYTE_IME
5005 case WM_IME_NOTIFY:
5006 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5007 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005008 return 1L;
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 case WM_IME_COMPOSITION:
5011 if (!_OnImeComposition(hwnd, wParam, lParam))
5012 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005013 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00005015 case WM_DPICHANGED:
5016 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
5017 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005021 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#endif
5024 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5025 }
5026
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005027 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028}
5029
5030/*
5031 * End of call-back routines
5032 */
5033
Bram Moolenaar734a8672019-12-02 22:49:38 +01005034// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035HWND vim_parent_hwnd = NULL;
5036
5037 static BOOL CALLBACK
5038FindWindowTitle(HWND hwnd, LPARAM lParam)
5039{
5040 char buf[2048];
5041 char *title = (char *)lParam;
5042
5043 if (GetWindowText(hwnd, buf, sizeof(buf)))
5044 {
5045 if (strstr(buf, title) != NULL)
5046 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005047 // Found it. Store the window ref. and quit searching if MDI
5048 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005050 if (vim_parent_hwnd != NULL)
5051 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005054 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
5057/*
5058 * Invoked for '-P "title"' argument: search for parent application to open
5059 * our window in.
5060 */
5061 void
5062gui_mch_set_parent(char *title)
5063{
5064 EnumWindows(FindWindowTitle, (LPARAM)title);
5065 if (vim_parent_hwnd == NULL)
5066 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005067 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 mch_exit(2);
5069 }
5070}
5071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 static void
5074ole_error(char *arg)
5075{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005076 char buf[IOSIZE];
5077
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005078# ifdef VIMDLL
5079 gui.in_use = mch_is_gui_executable();
5080# endif
5081
Bram Moolenaar734a8672019-12-02 22:49:38 +01005082 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005083 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005084 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005085 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005089#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5090 static char *
5091gvim_error(void)
5092{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005093 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005094
5095 if (starting)
5096 {
5097 mch_errmsg(msg);
5098 mch_errmsg("\n");
5099 mch_exit(2);
5100 }
5101 return msg;
5102}
5103
5104 char *
5105gui_mch_do_spawn(char_u *arg)
5106{
5107 int len;
5108# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5109 char_u *session = NULL;
5110 LPWSTR tofree1 = NULL;
5111# endif
5112 WCHAR name[MAX_PATH];
5113 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5114 STARTUPINFOW si = {sizeof(si)};
5115 PROCESS_INFORMATION pi;
5116
5117 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5118 goto error;
5119 p = wcsrchr(name, L'\\');
5120 if (p == NULL)
5121 goto error;
5122 // Replace the executable name from vim(d).exe to gvim(d).exe.
5123# ifdef DEBUG
5124 wcscpy(p + 1, L"gvimd.exe");
5125# else
5126 wcscpy(p + 1, L"gvim.exe");
5127# endif
5128
5129# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5130 if (starting)
5131# endif
5132 {
5133 // Pass the command line to the new process.
5134 p = GetCommandLineW();
5135 // Skip 1st argument.
5136 while (*p && *p != L' ' && *p != L'\t')
5137 {
5138 if (*p == L'"')
5139 {
5140 while (*p && *p != L'"')
5141 ++p;
5142 if (*p)
5143 ++p;
5144 }
5145 else
5146 ++p;
5147 }
5148 cmd = p;
5149 }
5150# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5151 else
5152 {
5153 // Create a session file and pass it to the new process.
5154 LPWSTR wsession;
5155 char_u *savebg;
5156 int ret;
5157
5158 session = vim_tempname('s', FALSE);
5159 if (session == NULL)
5160 goto error;
5161 savebg = p_bg;
5162 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5163 ret = write_session_file(session);
5164 vim_free(p_bg);
5165 p_bg = savebg;
5166 if (!ret)
5167 goto error;
5168 wsession = enc_to_utf16(session, NULL);
5169 if (wsession == NULL)
5170 goto error;
5171 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005172 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005173 if (cmd == NULL)
5174 {
5175 vim_free(wsession);
5176 goto error;
5177 }
5178 tofree1 = cmd;
5179 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5180 wsession, wsession);
5181 vim_free(wsession);
5182 }
5183# endif
5184
5185 // Check additional arguments to the `:gui` command.
5186 if (arg != NULL)
5187 {
5188 warg = enc_to_utf16(arg, NULL);
5189 if (warg == NULL)
5190 goto error;
5191 tofree2 = warg;
5192 }
5193 else
5194 warg = L"";
5195
5196 // Set up the new command line.
5197 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005198 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005199 if (newcmd == NULL)
5200 goto error;
5201 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5202
5203 // Spawn a new GUI process.
5204 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5205 NULL, NULL, &si, &pi))
5206 goto error;
5207 CloseHandle(pi.hProcess);
5208 CloseHandle(pi.hThread);
5209 mch_exit(0);
5210
5211error:
5212# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5213 if (session)
5214 mch_remove(session);
5215 vim_free(session);
5216 vim_free(tofree1);
5217# endif
5218 vim_free(newcmd);
5219 vim_free(tofree2);
5220 return gvim_error();
5221}
5222#endif
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224/*
5225 * Parse the GUI related command-line arguments. Any arguments used are
5226 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005227 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 */
5229 void
5230gui_mch_prepare(int *argc, char **argv)
5231{
5232 int silent = FALSE;
5233 int idx;
5234
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5237 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005238 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5240 && (argv[2][0] == '-' || argv[2][0] == '/'))
5241 {
5242 silent = TRUE;
5243 idx = 2;
5244 }
5245 else
5246 idx = 1;
5247
Bram Moolenaar734a8672019-12-02 22:49:38 +01005248 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (STRICMP(argv[idx] + 1, "register") == 0)
5250 {
5251#ifdef FEAT_OLE
5252 RegisterMe(silent);
5253 mch_exit(0);
5254#else
5255 if (!silent)
5256 ole_error("register");
5257 mch_exit(2);
5258#endif
5259 }
5260
Bram Moolenaar734a8672019-12-02 22:49:38 +01005261 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5263 {
5264#ifdef FEAT_OLE
5265 UnregisterMe(!silent);
5266 mch_exit(0);
5267#else
5268 if (!silent)
5269 ole_error("unregister");
5270 mch_exit(2);
5271#endif
5272 }
5273
Bram Moolenaar734a8672019-12-02 22:49:38 +01005274 // Ignore an -embedding argument. It is only relevant if the
5275 // application wants to treat the case when it is started manually
5276 // differently from the case where it is started via automation (and
5277 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5279 {
5280#ifdef FEAT_OLE
5281 *argc = 1;
5282#else
5283 ole_error("embedding");
5284 mch_exit(2);
5285#endif
5286 }
5287 }
5288
5289#ifdef FEAT_OLE
5290 {
5291 int bDoRestart = FALSE;
5292
5293 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005294 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (bDoRestart)
5296 mch_exit(0);
5297 }
5298#endif
5299
5300#ifdef FEAT_NETBEANS_INTG
5301 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005302 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 int arg;
5304
5305 for (arg = 1; arg < *argc; arg++)
5306 if (strncmp("-nb", argv[arg], 3) == 0)
5307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 netbeansArg = argv[arg];
5309 mch_memmove(&argv[arg], &argv[arg + 1],
5310 (--*argc - arg) * sizeof(char *));
5311 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005312 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316}
5317
K.Takatac81e9bf2022-01-16 14:15:49 +00005318 static void
5319load_dpi_func(void)
5320{
5321 HMODULE hUser32;
5322
5323 hUser32 = GetModuleHandle("user32.dll");
5324 if (hUser32 == NULL)
5325 goto fail;
5326
5327 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5328 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5329 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5330 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5331 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5332 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5333
5334 if (pSetThreadDpiAwarenessContext != NULL)
5335 {
5336 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5337 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5338 if (oldctx != NULL)
5339 {
5340 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5341 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5342#ifdef DEBUG
5343 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5344 {
5345 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5346 }
5347#endif
5348 return;
5349 }
5350 }
5351
5352fail:
5353 // Disable PerMonitorV2 APIs.
5354 pGetDpiForSystem = stubGetDpiForSystem;
5355 pGetDpiForWindow = NULL;
5356 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5357 pSetThreadDpiAwarenessContext = NULL;
5358 pGetAwarenessFromDpiAwarenessContext = NULL;
5359}
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361/*
5362 * Initialise the GUI. Create all the windows, set up all the call-backs
5363 * etc.
5364 */
5365 int
5366gui_mch_init(void)
5367{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005369 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef GLOBAL_IME
5372 ATOM atom;
5373#endif
5374
Bram Moolenaar734a8672019-12-02 22:49:38 +01005375 // Return here if the window was already opened (happens when
5376 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005378 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380 /*
5381 * Load the tearoff bitmap
5382 */
5383#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005384 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#endif
5386
K.Takatac81e9bf2022-01-16 14:15:49 +00005387 load_dpi_func();
5388
5389 s_dpi = pGetDpiForSystem();
5390 update_scrollbar_size();
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005393 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#endif
5395 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005396#ifdef FEAT_TOOLBAR
5397 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
5400 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5401
Bram Moolenaar734a8672019-12-02 22:49:38 +01005402 // First try using the wide version, so that we can use any title.
5403 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005404 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005406 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 wndclassw.lpfnWndProc = _WndProc;
5408 wndclassw.cbClsExtra = 0;
5409 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005410 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5412 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5413 wndclassw.hbrBackground = s_brush;
5414 wndclassw.lpszMenuName = NULL;
5415 wndclassw.lpszClassName = szVimWndClassW;
5416
5417 if ((
5418#ifdef GLOBAL_IME
5419 atom =
5420#endif
5421 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (vim_parent_hwnd != NULL)
5426 {
5427#ifdef HAVE_TRY_EXCEPT
5428 __try
5429 {
5430#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005431 // Open inside the specified parent window.
5432 // TODO: last argument should point to a CLIENTCREATESTRUCT
5433 // structure.
5434 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005436 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005437 WS_OVERLAPPEDWINDOW | WS_CHILD
5438 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5440 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005441 100, // Any value will do
5442 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005444 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445#ifdef HAVE_TRY_EXCEPT
5446 }
5447 __except(EXCEPTION_EXECUTE_HANDLER)
5448 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005449 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451#endif
5452 if (s_hwnd == NULL)
5453 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005454 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 mch_exit(2);
5456 }
5457 }
5458 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005459 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005460 // If the provided windowid is not valid reset it to zero, so that it
5461 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005462 if (IsWindow((HWND)win_socket_id) <= 0)
5463 win_socket_id = 0;
5464
Bram Moolenaar734a8672019-12-02 22:49:38 +01005465 // Create a window. If win_socket_id is not zero without border and
5466 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005467 s_hwnd = CreateWindowW(
5468 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005469 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5470 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005471 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5472 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005473 100, // Any value will do
5474 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005475 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005476 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005477 if (s_hwnd != NULL && win_socket_id != 0)
5478 {
5479 SetParent(s_hwnd, (HWND)win_socket_id);
5480 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5481 }
5482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
5484 if (s_hwnd == NULL)
5485 return FAIL;
5486
K.Takatac81e9bf2022-01-16 14:15:49 +00005487 if (pGetDpiForWindow != NULL)
5488 {
5489 s_dpi = pGetDpiForWindow(s_hwnd);
5490 update_scrollbar_size();
5491 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5492 }
5493
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494#ifdef GLOBAL_IME
5495 global_ime_init(atom, s_hwnd);
5496#endif
5497#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5498 dyn_imm_load();
5499#endif
5500
Bram Moolenaar734a8672019-12-02 22:49:38 +01005501 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005502 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005503 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005504 wndclassw.style = CS_OWNDC;
5505 wndclassw.lpfnWndProc = _TextAreaWndProc;
5506 wndclassw.cbClsExtra = 0;
5507 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005508 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005509 wndclassw.hIcon = NULL;
5510 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5511 wndclassw.hbrBackground = NULL;
5512 wndclassw.lpszMenuName = NULL;
5513 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005514
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005515 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 return FAIL;
5517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005519 s_textArea = CreateWindowExW(
5520 0,
5521 szTextAreaClassW, L"Vim text area",
5522 WS_CHILD | WS_VISIBLE, 0, 0,
5523 100, // Any value will do for now
5524 100, // Any value will do for now
5525 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005526 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (s_textArea == NULL)
5529 return FAIL;
5530
Bram Moolenaar20321902016-02-17 12:30:17 +01005531#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005532 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005533 {
5534 HANDLE hIcon = NULL;
5535
5536 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005537 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005538 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005539#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_MENU
5542 s_menuBar = CreateMenu();
5543#endif
5544 s_hdc = GetDC(s_textArea);
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
Bram Moolenaar734a8672019-12-02 22:49:38 +01005548 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005549 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
Bram Moolenaar734a8672019-12-02 22:49:38 +01005551 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 gui_mch_def_colors();
5553
Bram Moolenaar734a8672019-12-02 22:49:38 +01005554 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5555 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 set_normal_colors();
5557
5558 /*
5559 * Check that none of the colors are the same as the background color.
5560 * Then store the current values as the defaults.
5561 */
5562 gui_check_colors();
5563 gui.def_norm_pixel = gui.norm_pixel;
5564 gui.def_back_pixel = gui.back_pixel;
5565
Bram Moolenaar734a8672019-12-02 22:49:38 +01005566 // Get the colors for the highlight groups (gui_check_colors() might have
5567 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 highlight_gui_started();
5569
5570 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005571 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005573 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 /*
5576 * Set up for Intellimouse processing
5577 */
5578 init_mouse_wheel();
5579
5580 /*
5581 * compute a couple of metrics used for the dialogs
5582 */
5583 get_dialog_font_metrics();
5584#ifdef FEAT_TOOLBAR
5585 /*
5586 * Create the toolbar
5587 */
5588 initialise_toolbar();
5589#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005590#ifdef FEAT_GUI_TABLINE
5591 /*
5592 * Create the tabline
5593 */
5594 initialise_tabline();
5595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#ifdef MSWIN_FIND_REPLACE
5597 /*
5598 * Initialise the dialog box stuff
5599 */
5600 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5601
Bram Moolenaar734a8672019-12-02 22:49:38 +01005602 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005604 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005606 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5608 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5609 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005612#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005613# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005614// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005615# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005616# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005617# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005618# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005619 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005620 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005621#endif
5622
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005623#ifdef FEAT_RENDER_OPTIONS
5624 if (p_rop)
5625 (void)gui_mch_set_rendering_options(p_rop);
5626#endif
5627
Bram Moolenaar748bf032005-02-02 23:04:36 +00005628theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005629 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005630 display_errors();
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 return OK;
5633}
5634
5635/*
5636 * Get the size of the screen, taking position on multiple monitors into
5637 * account (if supported).
5638 */
5639 static void
5640get_work_area(RECT *spi_rect)
5641{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005642 HMONITOR mon;
5643 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
Bram Moolenaar734a8672019-12-02 22:49:38 +01005645 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005646 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005647 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005649 moninfo.cbSize = sizeof(MONITORINFO);
5650 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005652 *spi_rect = moninfo.rcWork;
5653 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005656 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5658}
5659
5660/*
5661 * Set the size of the window to the given width and height in pixels.
5662 */
5663 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005664gui_mch_set_shellsize(
5665 int width,
5666 int height,
5667 int min_width UNUSED,
5668 int min_height UNUSED,
5669 int base_width UNUSED,
5670 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005671 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005674 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
Bram Moolenaar734a8672019-12-02 22:49:38 +01005677 // Try to keep window completely on screen.
5678 // Get position of the screen work area. This is the part that is not
5679 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 get_work_area(&workarea_rect);
5681
Bram Moolenaar734a8672019-12-02 22:49:38 +01005682 // Resizing a maximized window looks very strange, unzoom it first.
5683 // But don't do it when still starting up, it may have been requested in
5684 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005685 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005687
5688 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
Bram Moolenaar734a8672019-12-02 22:49:38 +01005690 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005691 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5692 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5693 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5694 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5695 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5696 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
Bram Moolenaar734a8672019-12-02 22:49:38 +01005698 // The following should take care of keeping Vim on the same monitor, no
5699 // matter if the secondary monitor is left or right of the primary
5700 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005701 window_rect.right = window_rect.left + win_width;
5702 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005703
Bram Moolenaar734a8672019-12-02 22:49:38 +01005704 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005705 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5706 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005708 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5709 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005711 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5712 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005714 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5715 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005717 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5718 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 SetActiveWindow(s_hwnd);
5721 SetFocus(s_hwnd);
5722
Bram Moolenaar734a8672019-12-02 22:49:38 +01005723 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725}
5726
5727
5728 void
5729gui_mch_set_scrollbar_thumb(
5730 scrollbar_T *sb,
5731 long val,
5732 long size,
5733 long max)
5734{
5735 SCROLLINFO info;
5736
5737 sb->scroll_shift = 0;
5738 while (max > 32767)
5739 {
5740 max = (max + 1) >> 1;
5741 val >>= 1;
5742 size >>= 1;
5743 ++sb->scroll_shift;
5744 }
5745
5746 if (sb->scroll_shift > 0)
5747 ++size;
5748
5749 info.cbSize = sizeof(info);
5750 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5751 info.nPos = val;
5752 info.nMin = 0;
5753 info.nMax = max;
5754 info.nPage = size;
5755 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5756}
5757
5758
5759/*
5760 * Set the current text font.
5761 */
5762 void
5763gui_mch_set_font(GuiFont font)
5764{
5765 gui.currFont = font;
5766}
5767
5768
5769/*
5770 * Set the current text foreground color.
5771 */
5772 void
5773gui_mch_set_fg_color(guicolor_T color)
5774{
5775 gui.currFgColor = color;
5776}
5777
5778/*
5779 * Set the current text background color.
5780 */
5781 void
5782gui_mch_set_bg_color(guicolor_T color)
5783{
5784 gui.currBgColor = color;
5785}
5786
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005787/*
5788 * Set the current text special color.
5789 */
5790 void
5791gui_mch_set_sp_color(guicolor_T color)
5792{
5793 gui.currSpColor = color;
5794}
5795
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005796#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797/*
5798 * Multi-byte handling, originally by Sung-Hoon Baek.
5799 * First static functions (no prototypes generated).
5800 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005801# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005802# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005803# endif
5804# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 * handle WM_IME_NOTIFY message
5808 */
5809 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005810_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 LRESULT lResult = 0;
5813 HIMC hImc;
5814
5815 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5816 return lResult;
5817 switch (dwCommand)
5818 {
5819 case IMN_SETOPENSTATUS:
5820 if (pImmGetOpenStatus(hImc))
5821 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005822 LOGFONTW lf = norm_logfont;
5823 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5824 // Work around when PerMonitorV2 is not enabled in the process level.
5825 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5826 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 im_set_position(gui.row, gui.col);
5828
Bram Moolenaar734a8672019-12-02 22:49:38 +01005829 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 State &= ~LANGMAP;
5831 if (State & INSERT)
5832 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005833# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005834 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5836 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005837 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 int old_row = gui.row;
5839 int old_col = gui.col;
5840
5841 // This must be called here before
5842 // status_redraw_curbuf(), otherwise the mode
5843 // message may appear in the wrong position.
5844 showmode();
5845 status_redraw_curbuf();
5846 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005847 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 gui.row = old_row;
5849 gui.col = old_col;
5850 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853 }
5854 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005855 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 lResult = 0;
5857 break;
5858 }
5859 pImmReleaseContext(hWnd, hImc);
5860 return lResult;
5861}
5862
5863 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005864_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 char_u *ret;
5867 int len;
5868
Bram Moolenaar734a8672019-12-02 22:49:38 +01005869 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 return 0;
5871
5872 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5873 if (ret != NULL)
5874 {
5875 add_to_input_buf_csi(ret, len);
5876 vim_free(ret);
5877 return 1;
5878 }
5879 return 0;
5880}
5881
5882/*
5883 * get the current composition string, in UCS-2; *lenp is the number of
5884 * *lenp is the number of Unicode characters.
5885 */
5886 static short_u *
5887GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5888{
5889 LONG ret;
5890 LPWSTR wbuf = NULL;
5891 char_u *buf;
5892
5893 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005894 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005896 // Try Unicode; this will always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5898 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005899 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 if (ret > 0)
5902 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005903 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005904 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (wbuf != NULL)
5906 {
5907 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5908 *lenp = ret / sizeof(WCHAR);
5909 }
5910 return (short_u *)wbuf;
5911 }
5912
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005913 // ret < 0; we got an error, so try the ANSI version. This will work
Bram Moolenaar734a8672019-12-02 22:49:38 +01005914 // on 9x/ME, but only if the codepage happens to be set to whatever
5915 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5917 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005918 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 buf = alloc(ret);
5921 if (buf == NULL)
5922 return NULL;
5923 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5924
Bram Moolenaar734a8672019-12-02 22:49:38 +01005925 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005926 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 vim_free(buf);
5928
5929 return (short_u *)wbuf;
5930}
5931
5932/*
5933 * void GetResultStr()
5934 *
5935 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5936 * get complete composition string
5937 */
5938 static char_u *
5939GetResultStr(HWND hwnd, int GCS, int *lenp)
5940{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005941 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 short_u *buf = NULL;
5943 char_u *convbuf = NULL;
5944
5945 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5946 return NULL;
5947
Bram Moolenaar734a8672019-12-02 22:49:38 +01005948 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5950 if (buf == NULL)
5951 return NULL;
5952
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005953 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 pImmReleaseContext(hwnd, hIMC);
5955 vim_free(buf);
5956 return convbuf;
5957}
5958#endif
5959
Bram Moolenaar734a8672019-12-02 22:49:38 +01005960// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005961#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962
5963/*
5964 * set font to IM.
5965 */
5966 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005967im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 HIMC hImc;
5970
5971 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5972 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005973 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 pImmReleaseContext(s_hwnd, hImc);
5975 }
5976}
5977
5978/*
5979 * Notify cursor position to IM.
5980 */
5981 void
5982im_set_position(int row, int col)
5983{
5984 HIMC hImc;
5985
5986 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5987 {
5988 COMPOSITIONFORM cfs;
5989
5990 cfs.dwStyle = CFS_POINT;
5991 cfs.ptCurrentPos.x = FILL_X(col);
5992 cfs.ptCurrentPos.y = FILL_Y(row);
5993 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005994 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5995 {
5996 // Work around when PerMonitorV2 is not enabled in the process level.
5997 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5998 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 pImmSetCompositionWindow(hImc, &cfs);
6001
6002 pImmReleaseContext(s_hwnd, hImc);
6003 }
6004}
6005
6006/*
6007 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6008 */
6009 void
6010im_set_active(int active)
6011{
6012 HIMC hImc;
6013 static HIMC hImcOld = (HIMC)0;
6014
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006015# ifdef VIMDLL
6016 if (!gui.in_use && !gui.starting)
6017 {
6018 mbyte_im_set_active(active);
6019 return;
6020 }
6021# endif
6022
Bram Moolenaar734a8672019-12-02 22:49:38 +01006023 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
6025 if (p_imdisable)
6026 {
6027 if (hImcOld == (HIMC)0)
6028 {
6029 hImcOld = pImmGetContext(s_hwnd);
6030 if (hImcOld)
6031 pImmAssociateContext(s_hwnd, (HIMC)0);
6032 }
6033 active = FALSE;
6034 }
6035 else if (hImcOld != (HIMC)0)
6036 {
6037 pImmAssociateContext(s_hwnd, hImcOld);
6038 hImcOld = (HIMC)0;
6039 }
6040
6041 hImc = pImmGetContext(s_hwnd);
6042 if (hImc)
6043 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006044 /*
6045 * for Korean ime
6046 */
6047 HKL hKL = GetKeyboardLayout(0);
6048
6049 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6050 {
6051 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6052 static BOOL bSaved = FALSE;
6053
6054 if (active)
6055 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006056 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00006057 if (bSaved)
6058 pImmSetConversionStatus(hImc, dwConversionSaved,
6059 dwSentenceSaved);
6060 bSaved = FALSE;
6061 }
6062 else
6063 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006064 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00006065 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6066 &dwSentenceSaved))
6067 {
6068 bSaved = TRUE;
6069 pImmSetConversionStatus(hImc,
6070 dwConversionSaved & ~(IME_CMODE_NATIVE
6071 | IME_CMODE_FULLSHAPE),
6072 dwSentenceSaved);
6073 }
6074 }
6075 }
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 pImmSetOpenStatus(hImc, active);
6078 pImmReleaseContext(s_hwnd, hImc);
6079 }
6080 }
6081}
6082
6083/*
6084 * Get IM status. When IM is on, return not 0. Else return 0.
6085 */
6086 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006087im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 int status = 0;
6090 HIMC hImc;
6091
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006092# ifdef VIMDLL
6093 if (!gui.in_use && !gui.starting)
6094 return mbyte_im_get_status();
6095# endif
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6098 {
6099 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6100 pImmReleaseContext(s_hwnd, hImc);
6101 }
6102 return status;
6103}
6104
Bram Moolenaar734a8672019-12-02 22:49:38 +01006105#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006107#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006108// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110/*
6111 * Notify cursor position to IM.
6112 */
6113 void
6114im_set_position(int row, int col)
6115{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006116 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 POINT p;
6118
6119 p.x = FILL_X(col);
6120 p.y = FILL_Y(row);
6121 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6122 global_ime_set_position(&p);
6123}
6124
6125/*
6126 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6127 */
6128 void
6129im_set_active(int active)
6130{
6131 global_ime_set_status(active);
6132}
6133
6134/*
6135 * Get IM status. When IM is on, return not 0. Else return 0.
6136 */
6137 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006138im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 return global_ime_get_status();
6141}
6142#endif
6143
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006144/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006145 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006146 */
6147 static void
6148latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6149{
6150 int c;
6151
Bram Moolenaarca003e12006-03-17 23:19:38 +00006152 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006153 {
6154 c = *text++;
6155 switch (c)
6156 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006157 case 0xa4: c = 0x20ac; break; // euro
6158 case 0xa6: c = 0x0160; break; // S hat
6159 case 0xa8: c = 0x0161; break; // S -hat
6160 case 0xb4: c = 0x017d; break; // Z hat
6161 case 0xb8: c = 0x017e; break; // Z -hat
6162 case 0xbc: c = 0x0152; break; // OE
6163 case 0xbd: c = 0x0153; break; // oe
6164 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006165 }
6166 *unicodebuf++ = c;
6167 }
6168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
6170#ifdef FEAT_RIGHTLEFT
6171/*
6172 * What is this for? In the case where you are using Win98 or Win2K or later,
6173 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6174 * reverses the string sent to the TextOut... family. This sucks, because we
6175 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6176 * way to tell Windblows not to do this!
6177 *
6178 * The short of it is that this 'RevOut' only gets called if you are running
6179 * one of the new, "improved" MS OSes, and only if you are running in
6180 * 'rightleft' mode. It makes display take *slightly* longer, but not
6181 * noticeably so.
6182 */
6183 static void
6184RevOut( HDC s_hdc,
6185 int col,
6186 int row,
6187 UINT foptions,
6188 CONST RECT *pcliprect,
6189 LPCTSTR text,
6190 UINT len,
6191 CONST INT *padding)
6192{
6193 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006195 for (ix = 0; ix < (int)len; ++ix)
6196 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6197 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198}
6199#endif
6200
Bram Moolenaar92467d32017-12-05 13:22:16 +01006201 static void
6202draw_line(
6203 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006204 int y1,
6205 int x2,
6206 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006207 COLORREF color)
6208{
6209#if defined(FEAT_DIRECTX)
6210 if (IS_ENABLE_DIRECTX())
6211 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6212 else
6213#endif
6214 {
6215 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6216 HPEN old_pen = SelectObject(s_hdc, hpen);
6217 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006218 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006219 LineTo(s_hdc, x2, y2);
6220 DeleteObject(SelectObject(s_hdc, old_pen));
6221 }
6222}
6223
6224 static void
6225set_pixel(
6226 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006227 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006228 COLORREF color)
6229{
6230#if defined(FEAT_DIRECTX)
6231 if (IS_ENABLE_DIRECTX())
6232 DWriteContext_SetPixel(s_dwc, x, y, color);
6233 else
6234#endif
6235 SetPixel(s_hdc, x, y, color);
6236}
6237
6238 static void
6239fill_rect(
6240 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006241 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006242 COLORREF color)
6243{
6244#if defined(FEAT_DIRECTX)
6245 if (IS_ENABLE_DIRECTX())
6246 DWriteContext_FillRect(s_dwc, rcp, color);
6247 else
6248#endif
6249 {
6250 HBRUSH hbr2;
6251
6252 if (hbr == NULL)
6253 hbr2 = CreateSolidBrush(color);
6254 else
6255 hbr2 = hbr;
6256 FillRect(s_hdc, rcp, hbr2);
6257 if (hbr == NULL)
6258 DeleteBrush(hbr2);
6259 }
6260}
6261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 void
6263gui_mch_draw_string(
6264 int row,
6265 int col,
6266 char_u *text,
6267 int len,
6268 int flags)
6269{
6270 static int *padding = NULL;
6271 static int pad_size = 0;
6272 int i;
6273 const RECT *pcliprect = NULL;
6274 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 static WCHAR *unicodebuf = NULL;
6276 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006277 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 int y;
6280
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 /*
6282 * Italic and bold text seems to have an extra row of pixels at the bottom
6283 * (below where the bottom of the character should be). If we draw the
6284 * characters with a solid background, the top row of pixels in the
6285 * character below will be overwritten. We can fix this by filling in the
6286 * background ourselves, to the correct character proportions, and then
6287 * writing the character in transparent mode. Still have a problem when
6288 * the character is "_", which gets written on to the character below.
6289 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6290 * pixel in their slots, which fixes the problem with the bottom row of
6291 * pixels. We still need this code because otherwise the top row of pixels
6292 * becomes a problem. - webb.
6293 */
6294 static HBRUSH hbr_cache[2] = {NULL, NULL};
6295 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6296 static int brush_lru = 0;
6297 HBRUSH hbr;
6298 RECT rc;
6299
6300 if (!(flags & DRAW_TRANSP))
6301 {
6302 /*
6303 * Clear background first.
6304 * Note: FillRect() excludes right and bottom of rectangle.
6305 */
6306 rc.left = FILL_X(col);
6307 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (has_mbyte)
6309 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006310 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006311 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 }
6313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 rc.right = FILL_X(col + len);
6315 rc.bottom = FILL_Y(row + 1);
6316
Bram Moolenaar734a8672019-12-02 22:49:38 +01006317 // Cache the created brush, that saves a lot of time. We need two:
6318 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (gui.currBgColor == brush_color[0])
6320 {
6321 hbr = hbr_cache[0];
6322 brush_lru = 1;
6323 }
6324 else if (gui.currBgColor == brush_color[1])
6325 {
6326 hbr = hbr_cache[1];
6327 brush_lru = 0;
6328 }
6329 else
6330 {
6331 if (hbr_cache[brush_lru] != NULL)
6332 DeleteBrush(hbr_cache[brush_lru]);
6333 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6334 brush_color[brush_lru] = gui.currBgColor;
6335 hbr = hbr_cache[brush_lru];
6336 brush_lru = !brush_lru;
6337 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006338
Bram Moolenaar92467d32017-12-05 13:22:16 +01006339 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340
6341 SetBkMode(s_hdc, TRANSPARENT);
6342
6343 /*
6344 * When drawing block cursor, prevent inverted character spilling
6345 * over character cell (can happen with bold/italic)
6346 */
6347 if (flags & DRAW_CURSOR)
6348 {
6349 pcliprect = &rc;
6350 foptions = ETO_CLIPPED;
6351 }
6352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 SetTextColor(s_hdc, gui.currFgColor);
6354 SelectFont(s_hdc, gui.currFont);
6355
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006356#ifdef FEAT_DIRECTX
6357 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006358 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006359#endif
6360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6362 {
6363 vim_free(padding);
6364 pad_size = Columns;
6365
Bram Moolenaar734a8672019-12-02 22:49:38 +01006366 // Don't give an out-of-memory message here, it would call us
6367 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006368 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (padding != NULL)
6370 for (i = 0; i < pad_size; i++)
6371 padding[i] = gui.char_width;
6372 }
6373
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 /*
6375 * We have to provide the padding argument because italic and bold versions
6376 * of fixed-width fonts are often one pixel or so wider than their normal
6377 * versions.
6378 * No check for DRAW_BOLD, Windows will have done it already.
6379 */
6380
Bram Moolenaar734a8672019-12-02 22:49:38 +01006381 // Check if there are any UTF-8 characters. If not, use normal text
6382 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (enc_utf8)
6384 for (n = 0; n < len; ++n)
6385 if (text[n] >= 0x80)
6386 break;
6387
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006388#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006389 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6390 // required that unicode drawing routine, currently. So this forces it
6391 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006392 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006393 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006394#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006395
Bram Moolenaar734a8672019-12-02 22:49:38 +01006396 // Check if the Unicode buffer exists and is big enough. Create it
6397 // with the same length as the multi-byte string, the number of wide
6398 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006399 if ((enc_utf8
6400 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6401 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 && (unicodebuf == NULL || len > unibuflen))
6403 {
6404 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006405 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006408 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410 unibuflen = len;
6411 }
6412
6413 if (enc_utf8 && n < len && unicodebuf != NULL)
6414 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006415 // Output UTF-8 characters. Composing characters should be
6416 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006417 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006418 int wlen; // string length in words
6419 int clen; // string length in characters
6420 int cells; // cell width of string up to composing char
6421 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006422 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006424 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006425 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006427 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006429 c = utf_ptr2char(text + i);
6430 if (c >= 0x10000)
6431 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006432 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006433 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6434 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006435 }
6436 else
6437 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006438 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006439 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006440
6441 if (utf_iscomposing(c))
6442 cw = 0;
6443 else
6444 {
6445 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006446 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006447 cw = 1;
6448 }
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (unicodepdy != NULL)
6451 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006452 // Use unicodepdy to make characters fit as we expect, even
6453 // when the font uses different widths (e.g., bold character
6454 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006455 if (c >= 0x10000)
6456 {
6457 unicodepdy[wlen - 2] = cw * gui.char_width;
6458 unicodepdy[wlen - 1] = 0;
6459 }
6460 else
6461 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 }
6463 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006464 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006465 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006467#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006468 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006469 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006470 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006471 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006472 TEXT_X(col), TEXT_Y(row),
6473 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006474 gui.char_width, gui.currFgColor,
6475 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006476 }
6477 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006478#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006479 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6480 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006481 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006483 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006485 // If we want to display codepage data, and the current CP is not the
6486 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (unicodebuf != NULL)
6488 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006489 if (enc_latin9)
6490 latin9_to_ucs(text, len, unicodebuf);
6491 else
6492 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 MB_PRECOMPOSED,
6494 (char *)text, len,
6495 (LPWSTR)unicodebuf, unibuflen);
6496 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006497 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006498 // Use unicodepdy to make characters fit as we expect, even
6499 // when the font uses different widths (e.g., bold character
6500 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006501 if (unicodepdy != NULL)
6502 {
6503 int i;
6504 int cw;
6505
6506 for (i = 0; i < len; ++i)
6507 {
6508 cw = utf_char2cells(unicodebuf[i]);
6509 if (cw > 2)
6510 cw = 1;
6511 unicodepdy[i] = cw * gui.char_width;
6512 }
6513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006515 foptions, pcliprect, unicodebuf, len, unicodepdy);
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518 }
6519 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
6521#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006522 // Windows will mess up RL text, so we have to draw it character by
6523 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006524 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6526 foptions, pcliprect, (char *)text, len, padding);
6527 else
6528#endif
6529 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6530 foptions, pcliprect, (char *)text, len, padding);
6531 }
6532
Bram Moolenaar734a8672019-12-02 22:49:38 +01006533 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (flags & DRAW_UNDERL)
6535 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006536 // When p_linespace is 0, overwrite the bottom row of pixels.
6537 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (p_linespace > 1)
6540 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006541 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006543
Bram Moolenaar734a8672019-12-02 22:49:38 +01006544 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006545 if (flags & DRAW_STRIKE)
6546 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006547 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006548 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006549 }
6550
Bram Moolenaar734a8672019-12-02 22:49:38 +01006551 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006552 if (flags & DRAW_UNDERC)
6553 {
6554 int x;
6555 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006556 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006557
6558 y = FILL_Y(row + 1) - 1;
6559 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6560 {
6561 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006562 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006563 }
6564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565}
6566
6567
6568/*
6569 * Output routines.
6570 */
6571
Bram Moolenaar734a8672019-12-02 22:49:38 +01006572/*
6573 * Flush any output to the screen
6574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 void
6576gui_mch_flush(void)
6577{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006578#if defined(FEAT_DIRECTX)
6579 if (IS_ENABLE_DIRECTX())
6580 DWriteContext_Flush(s_dwc);
6581#endif
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 GdiFlush();
6584}
6585
6586 static void
6587clear_rect(RECT *rcp)
6588{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006589 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590}
6591
6592
Bram Moolenaarc716c302006-01-21 22:12:51 +00006593 void
6594gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6595{
6596 RECT workarea_rect;
6597
6598 get_work_area(&workarea_rect);
6599
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006600 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006601 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6602 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006603
Bram Moolenaar734a8672019-12-02 22:49:38 +01006604 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6605 // the menubar for MSwin, we subtract it from the screen height, so that
6606 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006607 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006608 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6609 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6610 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6611 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006612}
6613
6614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615#if defined(FEAT_MENU) || defined(PROTO)
6616/*
6617 * Add a sub menu to the menu bar.
6618 */
6619 void
6620gui_mch_add_menu(
6621 vimmenu_T *menu,
6622 int pos)
6623{
6624 vimmenu_T *parent = menu->parent;
6625
6626 menu->submenu_id = CreatePopupMenu();
6627 menu->id = s_menu_id++;
6628
6629 if (menu_is_menubar(menu->name))
6630 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006631 WCHAR *wn;
6632 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006634 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006635 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006638 infow.cbSize = sizeof(infow);
6639 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6640 | MIIM_SUBMENU;
6641 infow.dwItemData = (long_u)menu;
6642 infow.wID = menu->id;
6643 infow.fType = MFT_STRING;
6644 infow.dwTypeData = wn;
6645 infow.cch = (UINT)wcslen(wn);
6646 infow.hSubMenu = menu->submenu_id;
6647 InsertMenuItemW((parent == NULL)
6648 ? s_menuBar : parent->submenu_id,
6649 (UINT)pos, TRUE, &infow);
6650 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 }
6652
Bram Moolenaar734a8672019-12-02 22:49:38 +01006653 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (parent == NULL)
6655 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006656# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 else if (IsWindow(parent->tearoff_handle))
6658 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
6662 void
6663gui_mch_show_popupmenu(vimmenu_T *menu)
6664{
6665 POINT mp;
6666
6667 (void)GetCursorPos((LPPOINT)&mp);
6668 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6669}
6670
6671 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006672gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 vimmenu_T *menu = gui_find_menu(path_name);
6675
6676 if (menu != NULL)
6677 {
6678 POINT p;
6679
Bram Moolenaar734a8672019-12-02 22:49:38 +01006680 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006682 if (mouse_pos)
6683 {
6684 int mx, my;
6685
6686 gui_mch_getmouse(&mx, &my);
6687 p.x += mx;
6688 p.y += my;
6689 }
6690 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006692 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6694 }
6695 msg_scroll = FALSE;
6696 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6697 }
6698}
6699
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006700# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701/*
6702 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6703 * create it as a pseudo-"tearoff menu".
6704 */
6705 void
6706gui_make_tearoff(char_u *path_name)
6707{
6708 vimmenu_T *menu = gui_find_menu(path_name);
6709
Bram Moolenaar734a8672019-12-02 22:49:38 +01006710 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 if (menu != NULL)
6712 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6713}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716/*
6717 * Add a menu item to a menu
6718 */
6719 void
6720gui_mch_add_menu_item(
6721 vimmenu_T *menu,
6722 int idx)
6723{
6724 vimmenu_T *parent = menu->parent;
6725
6726 menu->id = s_menu_id++;
6727 menu->submenu_id = NULL;
6728
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006729# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6731 {
6732 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6733 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6734 }
6735 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006736# endif
6737# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (menu_is_toolbar(parent->name))
6739 {
6740 TBBUTTON newtb;
6741
Bram Moolenaara80faa82020-04-12 19:37:17 +02006742 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (menu_is_separator(menu->name))
6744 {
6745 newtb.iBitmap = 0;
6746 newtb.fsStyle = TBSTYLE_SEP;
6747 }
6748 else
6749 {
6750 newtb.iBitmap = get_toolbar_bitmap(menu);
6751 newtb.fsStyle = TBSTYLE_BUTTON;
6752 }
6753 newtb.idCommand = menu->id;
6754 newtb.fsState = TBSTATE_ENABLED;
6755 newtb.iString = 0;
6756 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6757 (LPARAM)&newtb);
6758 menu->submenu_id = (HMENU)-1;
6759 }
6760 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006763 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006765 wn = enc_to_utf16(menu->name, NULL);
6766 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006768 InsertMenuW(parent->submenu_id, (UINT)idx,
6769 (menu_is_separator(menu->name)
6770 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6771 (UINT)menu->id, wn);
6772 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006774# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (IsWindow(parent->tearoff_handle))
6776 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 }
6779}
6780
6781/*
6782 * Destroy the machine specific menu widget.
6783 */
6784 void
6785gui_mch_destroy_menu(vimmenu_T *menu)
6786{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006787# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 /*
6789 * is this a toolbar button?
6790 */
6791 if (menu->submenu_id == (HMENU)-1)
6792 {
6793 int iButton;
6794
6795 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6796 (WPARAM)menu->id, 0);
6797 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6798 }
6799 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 if (menu->parent != NULL
6803 && menu_is_popup(menu->parent->dname)
6804 && menu->parent->submenu_id != NULL)
6805 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6806 else
6807 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6808 if (menu->submenu_id != NULL)
6809 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006810# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (IsWindow(menu->tearoff_handle))
6812 DestroyWindow(menu->tearoff_handle);
6813 if (menu->parent != NULL
6814 && menu->parent->children != NULL
6815 && IsWindow(menu->parent->tearoff_handle))
6816 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006817 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 menu->modes = 0;
6819 rebuild_tearoff(menu->parent);
6820 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823}
6824
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006825# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 static void
6827rebuild_tearoff(vimmenu_T *menu)
6828{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006829 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 char_u tbuf[128];
6831 RECT trect;
6832 RECT rct;
6833 RECT roct;
6834 int x, y;
6835
6836 HWND thwnd = menu->tearoff_handle;
6837
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006838 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (GetWindowRect(thwnd, &trect)
6840 && GetWindowRect(s_hwnd, &rct)
6841 && GetClientRect(s_hwnd, &roct))
6842 {
6843 x = trect.left - rct.left;
6844 y = (trect.top - rct.bottom + roct.bottom);
6845 }
6846 else
6847 {
6848 x = y = 0xffffL;
6849 }
6850 DestroyWindow(thwnd);
6851 if (menu->children != NULL)
6852 {
6853 gui_mch_tearoff(tbuf, menu, x, y);
6854 if (IsWindow(menu->tearoff_handle))
6855 (void) SetWindowPos(menu->tearoff_handle,
6856 NULL,
6857 (int)trect.left,
6858 (int)trect.top,
6859 0, 0,
6860 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6861 }
6862}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006863# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865/*
6866 * Make a menu either grey or not grey.
6867 */
6868 void
6869gui_mch_menu_grey(
6870 vimmenu_T *menu,
6871 int grey)
6872{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006873# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 /*
6875 * is this a toolbar button?
6876 */
6877 if (menu->submenu_id == (HMENU)-1)
6878 {
6879 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6880 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6881 }
6882 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006883# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006884 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6885 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006887# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6889 {
6890 WORD menuID;
6891 HWND menuHandle;
6892
6893 /*
6894 * A tearoff button has changed state.
6895 */
6896 if (menu->children == NULL)
6897 menuID = (WORD)(menu->id);
6898 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006899 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6901 if (menuHandle)
6902 EnableWindow(menuHandle, !grey);
6903
6904 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906}
6907
Bram Moolenaar734a8672019-12-02 22:49:38 +01006908#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
6910
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6914#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006915#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
6917#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6918/*
6919 * stuff for dialogs
6920 */
6921
6922/*
6923 * The callback routine used by all the dialogs. Very simple. First,
6924 * acknowledges the INITDIALOG message so that Windows knows to do standard
6925 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6926 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6927 * number.
6928 */
6929 static LRESULT CALLBACK
6930dialog_callback(
6931 HWND hwnd,
6932 UINT message,
6933 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006934 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 if (message == WM_INITDIALOG)
6937 {
6938 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006939 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 (void)SetFocus(hwnd);
6941 if (dialog_default_button > IDCANCEL)
6942 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006943 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 // We don't have a default, set focus on another element of the
6945 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006946 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 return FALSE;
6948 }
6949
6950 if (message == WM_COMMAND)
6951 {
6952 int button = LOWORD(wParam);
6953
Bram Moolenaar734a8672019-12-02 22:49:38 +01006954 // Don't end the dialog if something was selected that was
6955 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (button >= DLG_NONBUTTON_CONTROL)
6957 return TRUE;
6958
Bram Moolenaar734a8672019-12-02 22:49:38 +01006959 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006961 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006962 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006963 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006964
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006965 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6966 p = utf16_to_enc(wp, NULL);
6967 vim_strncpy(s_textfield, p, IOSIZE);
6968 vim_free(p);
6969 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972 /*
6973 * Need to check for IDOK because if the user just hits Return to
6974 * accept the default value, some reason this is what we get.
6975 */
6976 if (button == IDOK)
6977 {
6978 if (dialog_default_button > IDCANCEL)
6979 EndDialog(hwnd, dialog_default_button);
6980 }
6981 else
6982 EndDialog(hwnd, button - IDCANCEL);
6983 return TRUE;
6984 }
6985
6986 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6987 {
6988 EndDialog(hwnd, 0);
6989 return TRUE;
6990 }
6991 return FALSE;
6992}
6993
6994/*
6995 * Create a dialog dynamically from the parameter strings.
6996 * type = type of dialog (question, alert, etc.)
6997 * title = dialog title. may be NULL for default title.
6998 * message = text to display. Dialog sizes to accommodate it.
6999 * buttons = '\n' separated list of button captions, default first.
7000 * dfltbutton = number of default button.
7001 *
7002 * This routine returns 1 if the first button is pressed,
7003 * 2 for the second, etc.
7004 *
7005 * 0 indicates Esc was pressed.
7006 * -1 for unexpected error
7007 *
7008 * If stubbing out this fn, return 1.
7009 */
7010
Bram Moolenaar734a8672019-12-02 22:49:38 +01007011static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 "IDR_VIM",
7014 "IDR_VIM_ERROR",
7015 "IDR_VIM_ALERT",
7016 "IDR_VIM_INFO",
7017 "IDR_VIM_QUESTION"
7018};
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 int
7021gui_mch_dialog(
7022 int type,
7023 char_u *title,
7024 char_u *message,
7025 char_u *buttons,
7026 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007027 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007028 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007031 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 int numButtons;
7033 int *buttonWidths, *buttonPositions;
7034 int buttonYpos;
7035 int nchar, i;
7036 DWORD lStyle;
7037 int dlgwidth = 0;
7038 int dlgheight;
7039 int editboxheight;
7040 int horizWidth = 0;
7041 int msgheight;
7042 char_u *pstart;
7043 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007044 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 char_u *tbuffer;
7046 RECT rect;
7047 HWND hwnd;
7048 HDC hdc;
7049 HFONT font, oldFont;
7050 TEXTMETRIC fontInfo;
7051 int fontHeight;
7052 int textWidth, minButtonWidth, messageWidth;
7053 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007054 int maxDialogHeight;
7055 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 int vertical;
7057 int dlgPaddingX;
7058 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007059# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007060 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007062# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007063 garray_T ga;
7064 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007065 int dlg_icon_width;
7066 int dlg_icon_height;
7067 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007069# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007070 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007071# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007072 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007073# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007074 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007075 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar748bf032005-02-02 23:04:36 +00007078 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007079 {
7080 load_dpi_func();
7081 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007082 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007083 }
7084 else
7085 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
7087 if ((type < 0) || (type > VIM_LAST_TYPE))
7088 type = 0;
7089
Bram Moolenaar734a8672019-12-02 22:49:38 +01007090 // allocate some memory for dialog template
7091 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007092 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007093 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
7095 if (p == NULL)
7096 return -1;
7097
7098 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007099 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7101 * const.
7102 */
7103 tbuffer = vim_strsave(buttons);
7104 if (tbuffer == NULL)
7105 return -1;
7106
Bram Moolenaar734a8672019-12-02 22:49:38 +01007107 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
Bram Moolenaar734a8672019-12-02 22:49:38 +01007109 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 numButtons = 1;
7111 for (i = 0; tbuffer[i] != '\0'; i++)
7112 {
7113 if (tbuffer[i] == DLG_BUTTON_SEP)
7114 numButtons++;
7115 }
7116 if (dfltbutton >= numButtons)
7117 dfltbutton = -1;
7118
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007120 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 if (buttonWidths == NULL)
7122 return -1;
7123
Bram Moolenaar734a8672019-12-02 22:49:38 +01007124 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007125 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 if (buttonPositions == NULL)
7127 return -1;
7128
7129 /*
7130 * Calculate how big the dialog must be.
7131 */
7132 hwnd = GetDesktopWindow();
7133 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007134# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7136 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007137 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 use_lfSysmenu = TRUE;
7139 }
7140 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007143 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (s_usenewlook)
7145 {
7146 oldFont = SelectFont(hdc, font);
7147 dlgPaddingX = DLG_PADDING_X;
7148 dlgPaddingY = DLG_PADDING_Y;
7149 }
7150 else
7151 {
7152 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7153 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7154 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7155 }
7156 GetTextMetrics(hdc, &fontInfo);
7157 fontHeight = fontInfo.tmHeight;
7158
Bram Moolenaar734a8672019-12-02 22:49:38 +01007159 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007160 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaar734a8672019-12-02 22:49:38 +01007162 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007163 if (s_hwnd == NULL)
7164 {
7165 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaar734a8672019-12-02 22:49:38 +01007167 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007168 get_work_area(&workarea_rect);
7169 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007170 if (maxDialogWidth > adjust_by_system_dpi(600))
7171 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007172 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007173 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007174 }
7175 else
7176 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007178 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007179 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007180 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7181 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7182 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7183 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007184
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007185 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007186 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7187 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7188 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7189 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7190 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007191 }
7192
Bram Moolenaar734a8672019-12-02 22:49:38 +01007193 // Set dlgwidth to width of message.
7194 // Copy the message into "ga", changing NL to CR-NL and inserting line
7195 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 pstart = message;
7197 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007198 msgheight = 0;
7199 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 do
7201 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007202 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007203
Bram Moolenaar734a8672019-12-02 22:49:38 +01007204 // Need to figure out where to break the string. The system does it
7205 // at a word boundary, which would mean we can't compute the number of
7206 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007207 textWidth = 0;
7208 last_white = NULL;
7209 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007210 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007211 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007212 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007213 && textWidth > maxDialogWidth * 3 / 4)
7214 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007215 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007216 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007217 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007218 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007219 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007220 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007221 textWidth = 0;
7222
7223 if (last_white != NULL)
7224 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007225 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007226 if (pend > last_white)
7227 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007228 pend = last_white + 1;
7229 last_white = NULL;
7230 }
7231 ga_append(&ga, '\r');
7232 ga_append(&ga, '\n');
7233 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007234 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007235
7236 while (--l >= 0)
7237 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007238 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007239 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007241
7242 ga_append(&ga, '\r');
7243 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 pstart = pend + 1;
7245 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007246
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007247 if (ga.ga_data != NULL)
7248 message = ga.ga_data;
7249
Bram Moolenaar734a8672019-12-02 22:49:38 +01007250 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007251
K.Takatac81e9bf2022-01-16 14:15:49 +00007252 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7253 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
K.Takatac81e9bf2022-01-16 14:15:49 +00007255 // Add width of icon to dlgwidth, and some space
7256 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7257 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7258
7259 if (msgheight < dlg_icon_height)
7260 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262 /*
7263 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007264 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007266 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 if (!vertical)
7268 {
7269 // Place buttons horizontally if they fit.
7270 horizWidth = dlgPaddingX;
7271 pstart = tbuffer;
7272 i = 0;
7273 do
7274 {
7275 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7276 if (pend == NULL)
7277 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007278 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 if (textWidth < minButtonWidth)
7280 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007281 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 buttonWidths[i] = textWidth;
7283 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007284 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 pstart = pend + 1;
7286 } while (*pend != NUL);
7287
7288 if (horizWidth > maxDialogWidth)
7289 vertical = TRUE; // Too wide to fit on the screen.
7290 else if (horizWidth > dlgwidth)
7291 dlgwidth = horizWidth;
7292 }
7293
7294 if (vertical)
7295 {
7296 // Stack buttons vertically.
7297 pstart = tbuffer;
7298 do
7299 {
7300 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7301 if (pend == NULL)
7302 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007303 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007304 textWidth += dlgPaddingX; // Padding within button
7305 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (textWidth > dlgwidth)
7307 dlgwidth = textWidth;
7308 pstart = pend + 1;
7309 } while (*pend != NUL);
7310 }
7311
7312 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007313 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaar734a8672019-12-02 22:49:38 +01007315 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 if (s_usenewlook)
7317 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7318 else
7319 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7320
7321 add_long(lStyle);
7322 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007323 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 add_word(0); // NumberOfItems(will change later)
7325 add_word(10); // x
7326 add_word(10); // y
7327 add_word(PixelToDialogX(dlgwidth)); // cx
7328
7329 // Dialog height.
7330 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007331 dlgheight = msgheight + 2 * dlgPaddingY
7332 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 else
7334 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7335
7336 // Dialog needs to be taller if contains an edit box.
7337 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7338 if (textfield != NULL)
7339 dlgheight += editboxheight;
7340
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007342 if (dlgheight > maxDialogHeight)
7343 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007344 msgheight = msgheight - (dlgheight - maxDialogHeight);
7345 dlgheight = maxDialogHeight;
7346 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007347 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007348 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007349 }
7350
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 add_word(PixelToDialogY(dlgheight));
7352
7353 add_word(0); // Menu
7354 add_word(0); // Class
7355
Bram Moolenaar734a8672019-12-02 22:49:38 +01007356 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007357 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7358 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 p += nchar;
7360
7361 if (s_usenewlook)
7362 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007363 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007364# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (use_lfSysmenu)
7366 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007367 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7369 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007370 wcscpy(p, lfSysmenu.lfFaceName);
7371 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
7376 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007377 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379 p += nchar;
7380 }
7381
7382 buttonYpos = msgheight + 2 * dlgPaddingY;
7383
7384 if (textfield != NULL)
7385 buttonYpos += editboxheight;
7386
7387 pstart = tbuffer;
7388 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007389 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 for (i = 0; i < numButtons; i++)
7391 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007392 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 for ( pend = pstart;
7394 *pend && (*pend != DLG_BUTTON_SEP);
7395 pend++)
7396 ;
7397
7398 if (*pend)
7399 *pend = '\0';
7400
7401 /*
7402 * old NOTE:
7403 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7404 * the focus to the first tab-able button and in so doing makes that
7405 * the default!! Grrr. Workaround: Make the default button the only
7406 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7407 * he/she can use arrow keys.
7408 *
7409 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007410 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 * dialog. Also needed for when the textfield is the default control.
7412 * It appears to work now (perhaps not on Win95?).
7413 */
7414 if (vertical)
7415 {
7416 p = add_dialog_element(p,
7417 (i == dfltbutton
7418 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7419 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007420 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 + 2 * fontHeight * i),
7422 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7423 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007424 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426 else
7427 {
7428 p = add_dialog_element(p,
7429 (i == dfltbutton
7430 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7431 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007432 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 PixelToDialogX(buttonWidths[i]),
7434 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007435 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007437 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 }
7439 *pnumitems += numButtons;
7440
Bram Moolenaar734a8672019-12-02 22:49:38 +01007441 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 p = add_dialog_element(p, SS_ICON,
7443 PixelToDialogX(dlgPaddingX),
7444 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007445 PixelToDialogX(dlg_icon_width),
7446 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7448 dlg_icons[type]);
7449
Bram Moolenaar734a8672019-12-02 22:49:38 +01007450 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007451 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007452 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007453 PixelToDialogY(dlgPaddingY),
7454 (WORD)(PixelToDialogX(messageWidth) + 1),
7455 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007456 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
Bram Moolenaar734a8672019-12-02 22:49:38 +01007458 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (textfield != NULL)
7460 {
7461 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7462 PixelToDialogX(2 * dlgPaddingX),
7463 PixelToDialogY(2 * dlgPaddingY + msgheight),
7464 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7465 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007466 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 *pnumitems += 1;
7468 }
7469
7470 *pnumitems += 2;
7471
7472 SelectFont(hdc, oldFont);
7473 DeleteObject(font);
7474 ReleaseDC(hwnd, hdc);
7475
Bram Moolenaar734a8672019-12-02 22:49:38 +01007476 // Let the dialog_callback() function know which button to make default
7477 // If we have an edit box, make that the default. We also need to tell
7478 // dialog_callback() if this dialog contains an edit box or not. We do
7479 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (textfield != NULL)
7481 {
7482 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7483 s_textfield = textfield;
7484 }
7485 else
7486 {
7487 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7488 s_textfield = NULL;
7489 }
7490
Bram Moolenaar734a8672019-12-02 22:49:38 +01007491 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007493 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 (LPDLGTEMPLATE)pdlgtemplate,
7495 s_hwnd,
7496 (DLGPROC)dialog_callback);
7497
7498 LocalFree(LocalHandle(pdlgtemplate));
7499 vim_free(tbuffer);
7500 vim_free(buttonWidths);
7501 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007502 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar734a8672019-12-02 22:49:38 +01007504 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 (void)SetFocus(s_hwnd);
7506
7507 return nchar;
7508}
7509
Bram Moolenaar734a8672019-12-02 22:49:38 +01007510#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512/*
7513 * Put a simple element (basic class) onto a dialog template in memory.
7514 * return a pointer to where the next item should be added.
7515 *
7516 * parameters:
7517 * lStyle = additional style flags
7518 * (be careful, NT3.51 & Win32s will ignore the new ones)
7519 * x,y = x & y positions IN DIALOG UNITS
7520 * w,h = width and height IN DIALOG UNITS
7521 * Id = ID used in messages
7522 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7523 * caption = usually text or resource name
7524 *
7525 * TODO: use the length information noted here to enable the dialog creation
7526 * routines to work out more exactly how much memory they need to alloc.
7527 */
7528 static PWORD
7529add_dialog_element(
7530 PWORD p,
7531 DWORD lStyle,
7532 WORD x,
7533 WORD y,
7534 WORD w,
7535 WORD h,
7536 WORD Id,
7537 WORD clss,
7538 const char *caption)
7539{
7540 int nchar;
7541
Bram Moolenaar734a8672019-12-02 22:49:38 +01007542 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7544 *p++ = LOWORD(lStyle);
7545 *p++ = HIWORD(lStyle);
7546 *p++ = 0; // LOWORD (lExtendedStyle)
7547 *p++ = 0; // HIWORD (lExtendedStyle)
7548 *p++ = x;
7549 *p++ = y;
7550 *p++ = w;
7551 *p++ = h;
7552 *p++ = Id; //9 or 10 words in all
7553
7554 *p++ = (WORD)0xffff;
7555 *p++ = clss; //2 more here
7556
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007557 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 p += nchar;
7559
7560 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7561
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007562 return p; // total = 15 + strlen(caption) words
7563 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564}
7565
7566
7567/*
7568 * Helper routine. Take an input pointer, return closest pointer that is
7569 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7570 */
7571 static LPWORD
7572lpwAlign(
7573 LPWORD lpIn)
7574{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007575 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007577 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 ul += 3;
7579 ul >>= 2;
7580 ul <<= 2;
7581 return (LPWORD)ul;
7582}
7583
7584/*
7585 * Helper routine. Takes second parameter as Ansi string, copies it to first
7586 * parameter as wide character (16-bits / char) string, and returns integer
7587 * number of wide characters (words) in string (including the trailing wide
7588 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007589 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7590 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 static int
7592nCopyAnsiToWideChar(
7593 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007594 LPSTR lpAnsiIn,
7595 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007598 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 int i;
7600 WCHAR *wn;
7601
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007602 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007604 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007605 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (wn != NULL)
7607 {
7608 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007609 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 vim_free(wn);
7611 }
7612 }
7613 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007614 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 nChar = MultiByteToWideChar(
7616 enc_codepage > 0 ? enc_codepage : CP_ACP,
7617 MB_PRECOMPOSED,
7618 lpAnsiIn, len,
7619 lpWCStr, len);
7620 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007621 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
7624 return nChar;
7625}
7626
7627
7628#ifdef FEAT_TEAROFF
7629/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007630 * Lookup menu handle from "menu_id".
7631 */
7632 static HMENU
7633tearoff_lookup_menuhandle(
7634 vimmenu_T *menu,
7635 WORD menu_id)
7636{
7637 for ( ; menu != NULL; menu = menu->next)
7638 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007639 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007640 continue;
7641 if (menu_is_separator(menu->dname))
7642 continue;
7643 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7644 return menu->submenu_id;
7645 }
7646 return NULL;
7647}
7648
7649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 * The callback function for all the modeless dialogs that make up the
7651 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7652 * thinking its menus have been clicked), and go away when closed.
7653 */
7654 static LRESULT CALLBACK
7655tearoff_callback(
7656 HWND hwnd,
7657 UINT message,
7658 WPARAM wParam,
7659 LPARAM lParam)
7660{
7661 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007662 {
7663 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666
Bram Moolenaar734a8672019-12-02 22:49:38 +01007667 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 HandleMouseHide(message, lParam);
7669
7670 if (message == WM_COMMAND)
7671 {
7672 if ((WORD)(LOWORD(wParam)) & 0x8000)
7673 {
7674 POINT mp;
7675 RECT rect;
7676
7677 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7678 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007679 vimmenu_T *menu;
7680
7681 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007683 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7685 (int)rect.right - 8,
7686 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007687 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 s_hwnd,
7689 NULL);
7690 /*
7691 * NOTE: The pop-up menu can eat the mouse up event.
7692 * We deal with this in normal.c.
7693 */
7694 }
7695 }
7696 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007697 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7699 /*
7700 * Give main window the focus back: this is so after
7701 * choosing a tearoff button you can start typing again
7702 * straight away.
7703 */
7704 (void)SetFocus(s_hwnd);
7705 return TRUE;
7706 }
7707 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7708 {
7709 DestroyWindow(hwnd);
7710 return TRUE;
7711 }
7712
Bram Moolenaar734a8672019-12-02 22:49:38 +01007713 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 if (message == WM_EXITSIZEMOVE)
7715 (void)SetActiveWindow(s_hwnd);
7716
7717 return FALSE;
7718}
7719#endif
7720
7721
7722/*
7723 * Decide whether to use the "new look" (small, non-bold font) or the "old
7724 * look" (big, clanky font) for dialogs, and work out a few values for use
7725 * later accordingly.
7726 */
7727 static void
7728get_dialog_font_metrics(void)
7729{
7730 HDC hdc;
7731 HFONT hfontTools = 0;
7732 DWORD dlgFontSize;
7733 SIZE size;
7734#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007735 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736#endif
7737
7738 s_usenewlook = FALSE;
7739
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007741 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007742 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744#endif
7745 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007746 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007748 if (hfontTools)
7749 {
7750 hdc = GetDC(s_hwnd);
7751 SelectObject(hdc, hfontTools);
7752 /*
7753 * GetTextMetrics() doesn't return the right value in
7754 * tmAveCharWidth, so we have to figure out the dialog base units
7755 * ourselves.
7756 */
7757 GetTextExtentPoint(hdc,
7758 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7759 52, &size);
7760 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007762 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7763 s_dlgfntheight = (WORD)size.cy;
7764 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766
7767 if (!s_usenewlook)
7768 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007769 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 s_dlgfntwidth = LOWORD(dlgFontSize);
7771 s_dlgfntheight = HIWORD(dlgFontSize);
7772 }
7773}
7774
7775#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7776/*
7777 * Create a pseudo-"tearoff menu" based on the child
7778 * items of a given menu pointer.
7779 */
7780 static void
7781gui_mch_tearoff(
7782 char_u *title,
7783 vimmenu_T *menu,
7784 int initX,
7785 int initY)
7786{
7787 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7788 int template_len;
7789 int nchar, textWidth, submenuWidth;
7790 DWORD lStyle;
7791 DWORD lExtendedStyle;
7792 WORD dlgwidth;
7793 WORD menuID;
7794 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007795 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 vimmenu_T *the_menu = menu;
7797 HWND hwnd;
7798 HDC hdc;
7799 HFONT font, oldFont;
7800 int col, spaceWidth, len;
7801 int columnWidths[2];
7802 char_u *label, *text;
7803 int acLen = 0;
7804 int nameLen;
7805 int padding0, padding1, padding2 = 0;
7806 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007807 int x;
7808 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007809# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007810 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
7814 /*
7815 * If this menu is already torn off, move it to the mouse position.
7816 */
7817 if (IsWindow(menu->tearoff_handle))
7818 {
7819 POINT mp;
7820 if (GetCursorPos((LPPOINT)&mp))
7821 {
7822 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7823 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7824 }
7825 return;
7826 }
7827
7828 /*
7829 * Create a new tearoff.
7830 */
7831 if (*title == MNU_HIDDEN_CHAR)
7832 title++;
7833
Bram Moolenaar734a8672019-12-02 22:49:38 +01007834 // Allocate memory to store the dialog template. It's made bigger when
7835 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 template_len = DLG_ALLOC_SIZE;
7837 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7838 if (p == NULL)
7839 return;
7840
7841 hwnd = GetDesktopWindow();
7842 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007843# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7845 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007846 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 use_lfSysmenu = TRUE;
7848 }
7849 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007852 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (s_usenewlook)
7854 oldFont = SelectFont(hdc, font);
7855 else
7856 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7857
Bram Moolenaar734a8672019-12-02 22:49:38 +01007858 // Calculate width of a single space. Used for padding columns to the
7859 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007860 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
Bram Moolenaar734a8672019-12-02 22:49:38 +01007862 // Figure out max width of the text column, the accelerator column and the
7863 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 submenuWidth = 0;
7865 for (col = 0; col < 2; col++)
7866 {
7867 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007868 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007870 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 text = (col == 0) ? pmenu->dname : pmenu->actext;
7872 if (text != NULL && *text != NUL)
7873 {
7874 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7875 if (textWidth > columnWidths[col])
7876 columnWidths[col] = textWidth;
7877 }
7878 if (pmenu->children != NULL)
7879 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7880 }
7881 }
7882 if (columnWidths[1] == 0)
7883 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007884 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (submenuWidth != 0)
7886 columnWidths[0] += submenuWidth;
7887 else
7888 columnWidths[0] += spaceWidth;
7889 }
7890 else
7891 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007892 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7894 columnWidths[1] += submenuWidth;
7895 }
7896
7897 /*
7898 * Now find the total width of our 'menu'.
7899 */
7900 textWidth = columnWidths[0] + columnWidths[1];
7901 if (submenuWidth != 0)
7902 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007903 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7905 textWidth += submenuWidth;
7906 }
7907 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7908 if (textWidth > dlgwidth)
7909 dlgwidth = textWidth;
7910 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7911
Bram Moolenaar734a8672019-12-02 22:49:38 +01007912 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 if (s_usenewlook)
7914 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7915 else
7916 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7917
7918 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7919 *p++ = LOWORD(lStyle);
7920 *p++ = HIWORD(lStyle);
7921 *p++ = LOWORD(lExtendedStyle);
7922 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007923 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007925 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007927 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 else
7929 *p++ = PixelToDialogX(initX); // x
7930 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007931 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 else
7933 *p++ = PixelToDialogY(initY); // y
7934 *p++ = PixelToDialogX(dlgwidth); // cx
7935 ptrueheight = p;
7936 *p++ = 0; // dialog height: changed later anyway
7937 *p++ = 0; // Menu
7938 *p++ = 0; // Class
7939
Bram Moolenaar734a8672019-12-02 22:49:38 +01007940 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007942 ? (LPSTR)title
7943 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 p += nchar;
7945
7946 if (s_usenewlook)
7947 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007948 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007949# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (use_lfSysmenu)
7951 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007952 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7954 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007955 wcscpy(p, lfSysmenu.lfFaceName);
7956 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 }
7958 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {
7961 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007962 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964 p += nchar;
7965 }
7966
7967 /*
7968 * Loop over all the items in the menu.
7969 * But skip over the tearbar.
7970 */
7971 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7972 menu = menu->children->next;
7973 else
7974 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007975 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 for ( ; menu != NULL; menu = menu->next)
7977 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007978 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 continue;
7980 if (menu_is_separator(menu->dname))
7981 {
7982 sepPadding += 3;
7983 continue;
7984 }
7985
Bram Moolenaar734a8672019-12-02 22:49:38 +01007986 // Check if there still is plenty of room in the template. Make it
7987 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7989 {
7990 WORD *newp;
7991
7992 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7993 if (newp != NULL)
7994 {
7995 template_len += 4096;
7996 mch_memmove(newp, pdlgtemplate,
7997 (char *)p - (char *)pdlgtemplate);
7998 p = newp + (p - pdlgtemplate);
7999 pnumitems = newp + (pnumitems - pdlgtemplate);
8000 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8001 LocalFree(LocalHandle(pdlgtemplate));
8002 pdlgtemplate = newp;
8003 }
8004 }
8005
Bram Moolenaar734a8672019-12-02 22:49:38 +01008006 // Figure out minimal length of this menu label. Use "name" for the
8007 // actual text, "dname" for estimating the displayed size. "name"
8008 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 len = nameLen = (int)STRLEN(menu->name);
8010 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8011 (int)STRLEN(menu->dname))) / spaceWidth;
8012 len += padding0;
8013
8014 if (menu->actext != NULL)
8015 {
8016 acLen = (int)STRLEN(menu->actext);
8017 len += acLen;
8018 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8019 }
8020 else
8021 textWidth = 0;
8022 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8023 len += padding1;
8024
8025 if (menu->children == NULL)
8026 {
8027 padding2 = submenuWidth / spaceWidth;
8028 len += padding2;
8029 menuID = (WORD)(menu->id);
8030 }
8031 else
8032 {
8033 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008034 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036
Bram Moolenaar734a8672019-12-02 22:49:38 +01008037 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02008038 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (label == NULL)
8040 break;
8041
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008042 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008043 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008045 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 while (padding0-- > 0)
8047 *text++ = ' ';
8048 if (menu->actext != NULL)
8049 {
8050 STRNCPY(text, menu->actext, acLen);
8051 text += acLen;
8052 }
8053 while (padding1-- > 0)
8054 *text++ = ' ';
8055 if (menu->children != NULL)
8056 {
8057 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8058 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8059 }
8060 else
8061 {
8062 while (padding2-- > 0)
8063 *text++ = ' ';
8064 }
8065 *text = NUL;
8066
8067 /*
8068 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8069 * W95/NT4 it makes the tear-off look more like a menu.
8070 */
8071 p = add_dialog_element(p,
8072 BS_PUSHBUTTON|BS_LEFT,
8073 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8074 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8075 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8076 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008077 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 vim_free(label);
8079 (*pnumitems)++;
8080 }
8081
8082 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8083
8084
Bram Moolenaar734a8672019-12-02 22:49:38 +01008085 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008086 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008087 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 (LPDLGTEMPLATE)pdlgtemplate,
8089 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008090 (DLGPROC)tearoff_callback,
8091 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
8093 LocalFree(LocalHandle(pdlgtemplate));
8094 SelectFont(hdc, oldFont);
8095 DeleteObject(font);
8096 ReleaseDC(hwnd, hdc);
8097
8098 /*
8099 * Reassert ourselves as the active window. This is so that after creating
8100 * a tearoff, the user doesn't have to click with the mouse just to start
8101 * typing again!
8102 */
8103 (void)SetActiveWindow(s_hwnd);
8104
Bram Moolenaar734a8672019-12-02 22:49:38 +01008105 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 force_menu_update = TRUE;
8107}
8108#endif
8109
8110#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008111# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
Bram Moolenaar734a8672019-12-02 22:49:38 +01008113// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114# ifndef TBSTYLE_FLAT
8115# define TBSTYLE_FLAT 0x0800
8116# endif
8117
8118/*
8119 * Create the toolbar, initially unpopulated.
8120 * (just like the menu, there are no defaults, it's all
8121 * set up through menu.vim)
8122 */
8123 static void
8124initialise_toolbar(void)
8125{
8126 InitCommonControls();
8127 s_toolbarhwnd = CreateToolbarEx(
8128 s_hwnd,
8129 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8130 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008131 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008132 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 IDR_TOOLBAR1, // id of initial bitmap
8134 NULL,
8135 0, // initial number of buttons
8136 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8137 TOOLBAR_BUTTON_HEIGHT,
8138 TOOLBAR_BUTTON_WIDTH,
8139 TOOLBAR_BUTTON_HEIGHT,
8140 sizeof(TBBUTTON)
8141 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008142
8143 // Remove transparency from the toolbar to prevent the main window
8144 // background colour showing through
8145 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8146 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8147
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008148 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149
8150 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008151
8152 update_toolbar_size();
8153}
8154
8155 static void
8156update_toolbar_size(void)
8157{
8158 int w, h;
8159 TBMETRICS tbm = {sizeof(TBMETRICS)};
8160
8161 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8162 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8163 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8164 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8165
8166 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8167 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8168 //TRACE("button size: %d, %d", w, h);
8169 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8170 gui.toolbar_height = h + 6;
8171
8172 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8173 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8174
8175 // TODO:
8176 // Currently, this function only updates the size of toolbar buttons.
8177 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178}
8179
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008180 static LRESULT CALLBACK
8181toolbar_wndproc(
8182 HWND hwnd,
8183 UINT uMsg,
8184 WPARAM wParam,
8185 LPARAM lParam)
8186{
8187 HandleMouseHide(uMsg, lParam);
8188 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8189}
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 static int
8192get_toolbar_bitmap(vimmenu_T *menu)
8193{
8194 int i = -1;
8195
8196 /*
8197 * Check user bitmaps first, unless builtin is specified.
8198 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008199 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {
8201 char_u fname[MAXPATHL];
8202 HANDLE hbitmap = NULL;
8203
8204 if (menu->iconfile != NULL)
8205 {
8206 gui_find_iconfile(menu->iconfile, fname, "bmp");
8207 hbitmap = LoadImage(
8208 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008209 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 IMAGE_BITMAP,
8211 TOOLBAR_BUTTON_WIDTH,
8212 TOOLBAR_BUTTON_HEIGHT,
8213 LR_LOADFROMFILE |
8214 LR_LOADMAP3DCOLORS
8215 );
8216 }
8217
8218 /*
8219 * If the LoadImage call failed, or the "icon=" file
8220 * didn't exist or wasn't specified, try the menu name
8221 */
8222 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008223 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008224# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008225 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008226# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008227 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 hbitmap = LoadImage(
8229 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008230 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 IMAGE_BITMAP,
8232 TOOLBAR_BUTTON_WIDTH,
8233 TOOLBAR_BUTTON_HEIGHT,
8234 LR_LOADFROMFILE |
8235 LR_LOADMAP3DCOLORS
8236 );
8237
8238 if (hbitmap != NULL)
8239 {
8240 TBADDBITMAP tbAddBitmap;
8241
8242 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008243 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244
8245 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8246 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008247 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
8249 }
8250 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8251 i = menu->iconidx;
8252
8253 return i;
8254}
8255#endif
8256
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008257#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8258 static void
8259initialise_tabline(void)
8260{
8261 InitCommonControls();
8262
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008263 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008264 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008265 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008266 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008267 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008268
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008269 gui.tabline_height = TABLINE_HEIGHT;
8270
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008271 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008272}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008273
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008274/*
8275 * Get tabpage_T from POINT.
8276 */
8277 static tabpage_T *
8278GetTabFromPoint(
8279 HWND hWnd,
8280 POINT pt)
8281{
8282 tabpage_T *ptp = NULL;
8283
8284 if (gui_mch_showing_tabline())
8285 {
8286 TCHITTESTINFO htinfo;
8287 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008288 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008289 if (s_tabhwnd == hWnd)
8290 {
8291 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8292 if (idx != -1)
8293 ptp = find_tabpage(idx + 1);
8294 }
8295 }
8296 return ptp;
8297}
8298
8299static POINT s_pt = {0, 0};
8300static HCURSOR s_hCursor = NULL;
8301
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008302 static LRESULT CALLBACK
8303tabline_wndproc(
8304 HWND hwnd,
8305 UINT uMsg,
8306 WPARAM wParam,
8307 LPARAM lParam)
8308{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008309 POINT pt;
8310 tabpage_T *tp;
8311 RECT rect;
8312 int nCenter;
8313 int idx0;
8314 int idx1;
8315
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008316 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008317
8318 switch (uMsg)
8319 {
8320 case WM_LBUTTONDOWN:
8321 {
8322 s_pt.x = GET_X_LPARAM(lParam);
8323 s_pt.y = GET_Y_LPARAM(lParam);
8324 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008325 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008326 break;
8327 }
8328 case WM_MOUSEMOVE:
8329 if (GetCapture() == hwnd
8330 && ((wParam & MK_LBUTTON)) != 0)
8331 {
8332 pt.x = GET_X_LPARAM(lParam);
8333 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008334 if (abs(pt.x - s_pt.x) >
8335 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008336 {
8337 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8338
8339 tp = GetTabFromPoint(hwnd, pt);
8340 if (tp != NULL)
8341 {
8342 idx0 = tabpage_index(curtab) - 1;
8343 idx1 = tabpage_index(tp) - 1;
8344
8345 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8346 nCenter = rect.left + (rect.right - rect.left) / 2;
8347
Bram Moolenaar734a8672019-12-02 22:49:38 +01008348 // Check if the mouse cursor goes over the center of
8349 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008350 if ((idx0 < idx1) && (nCenter < pt.x))
8351 {
8352 tabpage_move(idx1 + 1);
8353 update_screen(0);
8354 }
8355 else if ((idx1 < idx0) && (pt.x < nCenter))
8356 {
8357 tabpage_move(idx1);
8358 update_screen(0);
8359 }
8360 }
8361 }
8362 }
8363 break;
8364 case WM_LBUTTONUP:
8365 {
8366 if (GetCapture() == hwnd)
8367 {
8368 SetCursor(s_hCursor);
8369 ReleaseCapture();
8370 }
8371 break;
8372 }
8373 default:
8374 break;
8375 }
8376
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008377 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8378}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008379#endif
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8382/*
8383 * Make the GUI window come to the foreground.
8384 */
8385 void
8386gui_mch_set_foreground(void)
8387{
8388 if (IsIconic(s_hwnd))
8389 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8390 SetForegroundWindow(s_hwnd);
8391}
8392#endif
8393
8394#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8395 static void
8396dyn_imm_load(void)
8397{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008398 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (hLibImm == NULL)
8400 return;
8401
8402 pImmGetCompositionStringA
8403 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8404 pImmGetCompositionStringW
8405 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8406 pImmGetContext
8407 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8408 pImmAssociateContext
8409 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8410 pImmReleaseContext
8411 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8412 pImmGetOpenStatus
8413 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8414 pImmSetOpenStatus
8415 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008416 pImmGetCompositionFontW
8417 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8418 pImmSetCompositionFontW
8419 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 pImmSetCompositionWindow
8421 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8422 pImmGetConversionStatus
8423 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008424 pImmSetConversionStatus
8425 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
8427 if ( pImmGetCompositionStringA == NULL
8428 || pImmGetCompositionStringW == NULL
8429 || pImmGetContext == NULL
8430 || pImmAssociateContext == NULL
8431 || pImmReleaseContext == NULL
8432 || pImmGetOpenStatus == NULL
8433 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008434 || pImmGetCompositionFontW == NULL
8435 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008437 || pImmGetConversionStatus == NULL
8438 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
8440 FreeLibrary(hLibImm);
8441 hLibImm = NULL;
8442 pImmGetContext = NULL;
8443 return;
8444 }
8445
8446 return;
8447}
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#endif
8450
8451#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8452
8453# ifdef FEAT_XPM_W32
8454# define IMAGE_XPM 100
8455# endif
8456
8457typedef struct _signicon_t
8458{
8459 HANDLE hImage;
8460 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008461# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008462 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464} signicon_t;
8465
8466 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008467gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 signicon_t *sign;
8470 int x, y, w, h;
8471
8472 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8473 return;
8474
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008475# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008476 if (IS_ENABLE_DIRECTX())
8477 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008478# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008479
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 x = TEXT_X(col);
8481 y = TEXT_Y(row);
8482 w = gui.char_width * 2;
8483 h = gui.char_height;
8484 switch (sign->uType)
8485 {
8486 case IMAGE_BITMAP:
8487 {
8488 HDC hdcMem;
8489 HBITMAP hbmpOld;
8490
8491 hdcMem = CreateCompatibleDC(s_hdc);
8492 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8493 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8494 SelectObject(hdcMem, hbmpOld);
8495 DeleteDC(hdcMem);
8496 }
8497 break;
8498 case IMAGE_ICON:
8499 case IMAGE_CURSOR:
8500 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8501 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008502# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 case IMAGE_XPM:
8504 {
8505 HDC hdcMem;
8506 HBITMAP hbmpOld;
8507
8508 hdcMem = CreateCompatibleDC(s_hdc);
8509 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008510 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8512
8513 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008514 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8516 SelectObject(hdcMem, hbmpOld);
8517 DeleteDC(hdcMem);
8518 }
8519 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 }
8522}
8523
8524 static void
8525close_signicon_image(signicon_t *sign)
8526{
8527 if (sign)
8528 switch (sign->uType)
8529 {
8530 case IMAGE_BITMAP:
8531 DeleteObject((HGDIOBJ)sign->hImage);
8532 break;
8533 case IMAGE_CURSOR:
8534 DestroyCursor((HCURSOR)sign->hImage);
8535 break;
8536 case IMAGE_ICON:
8537 DestroyIcon((HICON)sign->hImage);
8538 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008539# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 case IMAGE_XPM:
8541 DeleteObject((HBITMAP)sign->hImage);
8542 DeleteObject((HBITMAP)sign->hShape);
8543 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 }
8546}
8547
8548 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008549gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 signicon_t sign, *psign;
8552 char_u *ext;
8553
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008555 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 if (ext > signfile)
8557 {
8558 int do_load = 1;
8559
8560 if (!STRICMP(ext, ".bmp"))
8561 sign.uType = IMAGE_BITMAP;
8562 else if (!STRICMP(ext, ".ico"))
8563 sign.uType = IMAGE_ICON;
8564 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8565 sign.uType = IMAGE_CURSOR;
8566 else
8567 do_load = 0;
8568
8569 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008570 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 gui.char_width * 2, gui.char_height,
8572 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008573# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (!STRICMP(ext, ".xpm"))
8575 {
8576 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008577 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8578 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
8582
8583 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008584 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 *psign = sign;
8586
8587 if (!psign)
8588 {
8589 if (sign.hImage)
8590 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008591 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593 return (void *)psign;
8594
8595}
8596
8597 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008598gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 if (sign)
8601 {
8602 close_signicon_image((signicon_t *)sign);
8603 vim_free(sign);
8604 }
8605}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008608#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaar734a8672019-12-02 22:49:38 +01008610/*
8611 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008612 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008614 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8616 * to get current mouse position).
8617 *
8618 * Trying to use as more Windows services as possible, and as less
8619 * IE version as possible :)).
8620 *
8621 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8622 * BalloonEval struct.
8623 * 2) Enable/Disable simply create/kill BalloonEval Timer
8624 * 3) When there was enough inactivity, timer procedure posts
8625 * async request to debugger
8626 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8627 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008628 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 */
8630
Bram Moolenaar45360022005-07-21 21:08:21 +00008631/*
8632 * determine whether installed Common Controls support multiline tooltips
8633 * (i.e. their version is >= 4.70
8634 */
8635 int
8636multiline_balloon_available(void)
8637{
8638 HINSTANCE hDll;
8639 static char comctl_dll[] = "comctl32.dll";
8640 static int multiline_tip = MAYBE;
8641
8642 if (multiline_tip != MAYBE)
8643 return multiline_tip;
8644
8645 hDll = GetModuleHandle(comctl_dll);
8646 if (hDll != NULL)
8647 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008648 DLLGETVERSIONPROC pGetVer;
8649 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008650
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008651 if (pGetVer != NULL)
8652 {
8653 DLLVERSIONINFO dvi;
8654 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008655
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008656 ZeroMemory(&dvi, sizeof(dvi));
8657 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008658
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008659 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008660
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008661 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008662 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008663 || (dvi.dwMajorVersion == 4
8664 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008665 {
8666 multiline_tip = TRUE;
8667 return multiline_tip;
8668 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008669 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008670 else
8671 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008672 // there is chance we have ancient CommCtl 4.70
8673 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008674 DWORD dwHandle = 0;
8675 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8676 if (len > 0)
8677 {
8678 VS_FIXEDFILEINFO *ver;
8679 UINT vlen = 0;
8680 void *data = alloc(len);
8681
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008682 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008683 && GetFileVersionInfo(comctl_dll, 0, len, data)
8684 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8685 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008686 && HIWORD(ver->dwFileVersionMS) > 4)
8687 || ((HIWORD(ver->dwFileVersionMS) == 4
8688 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008689 {
8690 vim_free(data);
8691 multiline_tip = TRUE;
8692 return multiline_tip;
8693 }
8694 vim_free(data);
8695 }
8696 }
8697 }
8698 multiline_tip = FALSE;
8699 return multiline_tip;
8700}
8701
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008702 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008703make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008704{
8705 TOOLINFOW *pti;
8706 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008707
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008708 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008709 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8710 else
8711 ToolInfoSize = sizeof(TOOLINFOW);
8712
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008713 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008714 if (pti == NULL)
8715 return;
8716
8717 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8718 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8719 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008720 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008721
8722 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8723 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8724
8725 pti->cbSize = ToolInfoSize;
8726 pti->uFlags = TTF_SUBCLASS;
8727 pti->hwnd = beval->target;
8728 pti->hinst = 0; // Don't use string resources
8729 pti->uId = ID_BEVAL_TOOLTIP;
8730
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008731 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008732 {
8733 RECT rect;
8734 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8735 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008736 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8737 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008738 // switch multiline tooltips on
8739 if (GetClientRect(s_textArea, &rect))
8740 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8741 (LPARAM)rect.right);
8742 }
8743 else
8744 {
8745 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008746 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8747 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008748 }
8749
8750 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8751 pti->rect.left = pt.x - 3;
8752 pti->rect.top = pt.y - 3;
8753 pti->rect.right = pt.x + 3;
8754 pti->rect.bottom = pt.y + 3;
8755
8756 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8757 // Make tooltip appear sooner.
8758 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8759 // I've performed some tests and it seems the longest possible life time
8760 // of tooltip is 30 seconds.
8761 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8762 /*
8763 * HACK: force tooltip to appear, because it'll not appear until
8764 * first mouse move. D*mn M$
8765 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8766 */
8767 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8768 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8769 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008770}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008771
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008773delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008775 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776}
8777
8778 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008779BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008780 HWND hwnd UNUSED,
8781 UINT uMsg UNUSED,
8782 UINT_PTR idEvent UNUSED,
8783 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 POINT pt;
8786 RECT rect;
8787
8788 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8789 return;
8790
8791 GetCursorPos(&pt);
8792 if (WindowFromPoint(pt) != s_textArea)
8793 return;
8794
8795 ScreenToClient(s_textArea, &pt);
8796 GetClientRect(s_textArea, &rect);
8797 if (!PtInRect(&rect, pt))
8798 return;
8799
8800 if (LastActivity > 0
8801 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8802 && (cur_beval->showState != ShS_PENDING
8803 || abs(cur_beval->x - pt.x) > 3
8804 || abs(cur_beval->y - pt.y) > 3))
8805 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008806 // Pointer resting in one place long enough, it's time to show
8807 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 cur_beval->showState = ShS_PENDING;
8809 cur_beval->x = pt.x;
8810 cur_beval->y = pt.y;
8811
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008812 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
8814 if (cur_beval->msgCB != NULL)
8815 (*cur_beval->msgCB)(cur_beval, 0);
8816 }
8817}
8818
8819 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008820gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008822 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008824 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
8827 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008828gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008830 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (beval == NULL)
8832 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008833 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008834 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008835 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
8838 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008839gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008842
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008843 vim_free(beval->msg);
8844 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8845 if (beval->msg == NULL)
8846 {
8847 delete_tooltip(beval);
8848 beval->showState = ShS_NEUTRAL;
8849 return;
8850 }
8851
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008852 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 if (beval->showState == ShS_SHOWING)
8854 return;
8855 GetCursorPos(&pt);
8856 ScreenToClient(s_textArea, &pt);
8857
8858 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008860 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 gui_mch_disable_beval_area(cur_beval);
8862 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008863 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008865 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866}
8867
8868 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008869gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008870 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008871 char_u *mesg,
8872 void (*mesgCB)(BalloonEval *, int),
8873 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008875 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 BalloonEval *beval;
8877
8878 if (mesg != NULL && mesgCB != NULL)
8879 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008880 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 return NULL;
8882 }
8883
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008884 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 if (beval != NULL)
8886 {
8887 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888
8889 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 beval->msg = mesg;
8891 beval->msgCB = mesgCB;
8892 beval->clientData = clientData;
8893
8894 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 cur_beval = beval;
8896
8897 if (p_beval)
8898 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900 return beval;
8901}
8902
8903 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008904Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008906 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 return;
8908
8909 if (cur_beval != NULL)
8910 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008911 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008913 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008914 // TRACE0("TTN_SHOW {{{");
8915 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008916 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008917 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008918 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 delete_tooltip(cur_beval);
8920 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008921 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
8923 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008924 break;
8925 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008926 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008927 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008928 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8929 info->lpszText = (LPSTR)info->lParam;
8930 info->uFlags |= TTF_DI_SETITEM;
8931 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008932 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008933 case TTN_GETDISPINFOW:
8934 {
8935 // if we get here then we have new common controls
8936 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8937 info->lpszText = (LPWSTR)info->lParam;
8938 info->uFlags |= TTF_DI_SETITEM;
8939 }
8940 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 }
8942 }
8943}
8944
8945 static void
8946TrackUserActivity(UINT uMsg)
8947{
8948 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8949 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8950 LastActivity = GetTickCount();
8951}
8952
8953 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008954gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008956# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008957 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008958# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008959 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 vim_free(beval);
8961}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008962#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
8964#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8965/*
8966 * We have multiple signs to draw at the same location. Draw the
8967 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8968 */
8969 void
8970netbeans_draw_multisign_indicator(int row)
8971{
8972 int i;
8973 int y;
8974 int x;
8975
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008976 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008977 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008978
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 x = 0;
8980 y = TEXT_Y(row);
8981
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008982# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008983 if (IS_ENABLE_DIRECTX())
8984 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008985# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008986
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 for (i = 0; i < gui.char_height - 3; i++)
8988 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8989
8990 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8991 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8992 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8993 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8994 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8995 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8996 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8997}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008998#endif