blob: 27e36085a9c05d1385320f59b909a3299fa46b23 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000043#else
44# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010045#endif
46
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020047#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
48 int
49gui_mch_set_rendering_options(char_u *s)
50{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010051# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020052 char_u *p, *q;
53
54 int dx_enable = 0;
55 int dx_flags = 0;
56 float dx_gamma = 0.0f;
57 float dx_contrast = 0.0f;
58 float dx_level = 0.0f;
59 int dx_geom = 0;
60 int dx_renmode = 0;
61 int dx_taamode = 0;
62
Bram Moolenaar734a8672019-12-02 22:49:38 +010063 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100124 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100132
Bram Moolenaar734a8672019-12-02 22:49:38 +0100133 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100163# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183
Bram Moolenaar734a8672019-12-02 22:49:38 +0100184#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100185#ifdef DEBUG
186# include <tchar.h>
187#endif
188
Bram Moolenaar734a8672019-12-02 22:49:38 +0100189// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100190#ifndef PROTO
191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100192# ifndef __MINGW32__
193# include <shellapi.h>
194# endif
195# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
196# include <commctrl.h>
197# endif
198# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100199
Bram Moolenaar734a8672019-12-02 22:49:38 +0100200#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
202#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100203# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100204#endif
205
Bram Moolenaar734a8672019-12-02 22:49:38 +0100206// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100207#define DLG_PADDING_X 10
208#define DLG_PADDING_Y 10
209#define DLG_OLD_STYLE_PADDING_X 5
210#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100211#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100212#define DLG_VERT_PADDING_Y 4
213#define DLG_ICON_WIDTH 34
214#define DLG_ICON_HEIGHT 34
215#define DLG_MIN_WIDTH 150
216#define DLG_FONT_NAME "MS Sans Serif"
217#define DLG_FONT_POINT_SIZE 8
218#define DLG_MIN_MAX_WIDTH 400
219#define DLG_MIN_MAX_HEIGHT 400
220
Bram Moolenaar734a8672019-12-02 22:49:38 +0100221#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224# define WM_XBUTTONDOWN 0x020B
225# define WM_XBUTTONUP 0x020C
226# define WM_XBUTTONDBLCLK 0x020D
227# define MK_XBUTTON1 0x0020
228# define MK_XBUTTON2 0x0040
229#endif
230
K.Takatac81e9bf2022-01-16 14:15:49 +0000231#ifndef WM_DPICHANGED
232# define WM_DPICHANGED 0x02E0
233#endif
234
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237 * Define a few things for generating prototypes. This is just to avoid
238 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100240# define APIENTRY
241# define CALLBACK
242# define CONST
243# define FAR
244# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200245# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200246# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247# define _cdecl
248typedef int BOOL;
249typedef int BYTE;
250typedef int DWORD;
251typedef int WCHAR;
252typedef int ENUMLOGFONT;
253typedef int FINDREPLACE;
254typedef int HANDLE;
255typedef int HBITMAP;
256typedef int HBRUSH;
257typedef int HDROP;
258typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100259typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100260typedef int LPARAM;
261typedef int LPCREATESTRUCT;
262typedef int LPCSTR;
263typedef int LPCTSTR;
264typedef int LPRECT;
265typedef int LPSTR;
266typedef int LPWINDOWPOS;
267typedef int LPWORD;
268typedef int LRESULT;
269typedef int HRESULT;
270# undef MSG
271typedef int MSG;
272typedef int NEWTEXTMETRIC;
273typedef int OSVERSIONINFO;
274typedef int PWORD;
275typedef int RECT;
276typedef int UINT;
277typedef int WORD;
278typedef int WPARAM;
279typedef int POINT;
280typedef void *HINSTANCE;
281typedef void *HMENU;
282typedef void *HWND;
283typedef void *HDC;
284typedef void VOID;
285typedef int LPNMHDR;
286typedef int LONG;
287typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200288typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200289typedef int COLORREF;
290typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100291#endif
292
293#ifndef GET_X_LPARAM
294# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
295#endif
296
297static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100298static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299static void clear_rect(RECT *rcp);
300
Bram Moolenaar734a8672019-12-02 22:49:38 +0100301static WORD s_dlgfntheight; // height of the dialog font
302static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef FEAT_MENU
305static HMENU s_menuBar = NULL;
306#endif
307#ifdef FEAT_TEAROFF
308static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaar734a8672019-12-02 22:49:38 +0100312// Flag that is set while processing a message that must not be interrupted by
313// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314static int s_busy_processing = FALSE;
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100317
318#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000319static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200322static int s_findrep_is_find; // TRUE for find dialog, FALSE
323 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324#endif
325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
Bram Moolenaar734a8672019-12-02 22:49:38 +0100347static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100348
349static int s_need_activate = FALSE;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351// This variable is set when waiting for an event, which is the only moment
352// scrollbar dragging can be done directly. It's not allowed while commands
353// are executed, because it may move the cursor and that may cause unexpected
354// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100355static int allow_scrollbar = FALSE;
356
K.Takatac81e9bf2022-01-16 14:15:49 +0000357#ifndef _DPI_AWARENESS_CONTEXTS_
358typedef HANDLE DPI_AWARENESS_CONTEXT;
359
360typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000361 DPI_AWARENESS_INVALID = -1,
362 DPI_AWARENESS_UNAWARE = 0,
363 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000364 DPI_AWARENESS_PER_MONITOR_AWARE = 2
365} DPI_AWARENESS;
366
K.Takatab0b2b732022-01-19 12:59:21 +0000367# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
368# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000369# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
370# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
371# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
372#endif
373
374#define DEFAULT_DPI 96
375static int s_dpi = DEFAULT_DPI;
376static BOOL s_in_dpichanged = FALSE;
377static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
378
379static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
380static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
381static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
382//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
383static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
384static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
385
386 static UINT WINAPI
387stubGetDpiForSystem(void)
388{
389 HWND hwnd = GetDesktopWindow();
390 HDC hdc = GetWindowDC(hwnd);
391 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
392 ReleaseDC(hwnd, hdc);
393 return dpi;
394}
395
396 static int WINAPI
397stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
398{
399 return GetSystemMetrics(nIndex);
400}
401
402 static int
403adjust_fontsize_by_dpi(int size)
404{
405 return size * s_dpi / (int)pGetDpiForSystem();
406}
407
408 static int
409adjust_by_system_dpi(int size)
410{
411 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
412}
413
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100414#if defined(FEAT_DIRECTX)
415 static int
416directx_enabled(void)
417{
418 if (s_dwc != NULL)
419 return 1;
420 else if (s_directx_load_attempted)
421 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100422 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100423 DWrite_Init();
424 s_directx_load_attempted = 1;
425 s_dwc = DWriteContext_Open();
426 directx_binddc();
427 return s_dwc != NULL ? 1 : 0;
428}
429
430 static void
431directx_binddc(void)
432{
433 if (s_textArea != NULL)
434 {
435 RECT rect;
436 GetClientRect(s_textArea, &rect);
437 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
438 }
439}
440#endif
441
Bram Moolenaar734a8672019-12-02 22:49:38 +0100442extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100443
444static struct
445{
446 UINT key_sym;
447 char_u vim_code0;
448 char_u vim_code1;
449} special_keys[] =
450{
451 {VK_UP, 'k', 'u'},
452 {VK_DOWN, 'k', 'd'},
453 {VK_LEFT, 'k', 'l'},
454 {VK_RIGHT, 'k', 'r'},
455
456 {VK_F1, 'k', '1'},
457 {VK_F2, 'k', '2'},
458 {VK_F3, 'k', '3'},
459 {VK_F4, 'k', '4'},
460 {VK_F5, 'k', '5'},
461 {VK_F6, 'k', '6'},
462 {VK_F7, 'k', '7'},
463 {VK_F8, 'k', '8'},
464 {VK_F9, 'k', '9'},
465 {VK_F10, 'k', ';'},
466
467 {VK_F11, 'F', '1'},
468 {VK_F12, 'F', '2'},
469 {VK_F13, 'F', '3'},
470 {VK_F14, 'F', '4'},
471 {VK_F15, 'F', '5'},
472 {VK_F16, 'F', '6'},
473 {VK_F17, 'F', '7'},
474 {VK_F18, 'F', '8'},
475 {VK_F19, 'F', '9'},
476 {VK_F20, 'F', 'A'},
477
478 {VK_F21, 'F', 'B'},
479#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100480 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100481#endif
482 {VK_F22, 'F', 'C'},
483 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485
486 {VK_HELP, '%', '1'},
487 {VK_BACK, 'k', 'b'},
488 {VK_INSERT, 'k', 'I'},
489 {VK_DELETE, 'k', 'D'},
490 {VK_HOME, 'k', 'h'},
491 {VK_END, '@', '7'},
492 {VK_PRIOR, 'k', 'P'},
493 {VK_NEXT, 'k', 'N'},
494 {VK_PRINT, '%', '9'},
495 {VK_ADD, 'K', '6'},
496 {VK_SUBTRACT, 'K', '7'},
497 {VK_DIVIDE, 'K', '8'},
498 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_DECIMAL, 'K', 'B'},
501
502 {VK_NUMPAD0, 'K', 'C'},
503 {VK_NUMPAD1, 'K', 'D'},
504 {VK_NUMPAD2, 'K', 'E'},
505 {VK_NUMPAD3, 'K', 'F'},
506 {VK_NUMPAD4, 'K', 'G'},
507 {VK_NUMPAD5, 'K', 'H'},
508 {VK_NUMPAD6, 'K', 'I'},
509 {VK_NUMPAD7, 'K', 'J'},
510 {VK_NUMPAD8, 'K', 'K'},
511 {VK_NUMPAD9, 'K', 'L'},
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100514 {VK_SPACE, ' ', NUL},
515 {VK_TAB, TAB, NUL},
516 {VK_ESCAPE, ESC, NUL},
517 {NL, NL, NUL},
518 {CAR, CAR, NUL},
519
Bram Moolenaar734a8672019-12-02 22:49:38 +0100520 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521 {0, 0, 0}
522};
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525static int s_button_pending = -1;
526
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
528// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_getting_focus = FALSE;
530
531static int s_x_pending;
532static int s_y_pending;
533static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200534static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100535static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200536static int dead_key = 0; // 0: no dead key, 1: dead key pressed
537static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
538 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100540#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100541// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
543static void TrackUserActivity(UINT uMsg);
544#endif
545
546/*
547 * For control IME.
548 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100549 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100550 */
K.Takata4ac893f2022-01-20 12:44:28 +0000551#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100554// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100555static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100556#endif
557
558#ifdef FEAT_MBYTE_IME
559static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
560#endif
561
562#if defined(FEAT_BROWSE)
563static char_u *convert_filter(char_u *s);
564#endif
565
566#ifdef DEBUG_PRINT_ERROR
567/*
568 * Print out the last Windows error message
569 */
570 static void
571print_windows_error(void)
572{
573 LPVOID lpMsgBuf;
574
575 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
576 NULL, GetLastError(),
577 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
578 (LPTSTR) &lpMsgBuf, 0, NULL);
579 TRACE1("Error: %s\n", lpMsgBuf);
580 LocalFree(lpMsgBuf);
581}
582#endif
583
584/*
585 * Cursor blink functions.
586 *
587 * This is a simple state machine:
588 * BLINK_NONE not blinking at all
589 * BLINK_OFF blinking, cursor is not shown
590 * BLINK_ON blinking, cursor is shown
591 */
592
593#define BLINK_NONE 0
594#define BLINK_OFF 1
595#define BLINK_ON 2
596
597static int blink_state = BLINK_NONE;
598static long_u blink_waittime = 700;
599static long_u blink_ontime = 400;
600static long_u blink_offtime = 250;
601static UINT blink_timer = 0;
602
Bram Moolenaar703a8042016-06-04 16:24:32 +0200603 int
604gui_mch_is_blinking(void)
605{
606 return blink_state != BLINK_NONE;
607}
608
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200609 int
610gui_mch_is_blink_off(void)
611{
612 return blink_state == BLINK_OFF;
613}
614
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615 void
616gui_mch_set_blinking(long wait, long on, long off)
617{
618 blink_waittime = wait;
619 blink_ontime = on;
620 blink_offtime = off;
621}
622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100623 static VOID CALLBACK
624_OnBlinkTimer(
625 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100628 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100629{
630 MSG msg;
631
632 /*
633 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
634 */
635
636 KillTimer(NULL, idEvent);
637
Bram Moolenaar734a8672019-12-02 22:49:38 +0100638 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
640 ;
641
642 if (blink_state == BLINK_ON)
643 {
644 gui_undraw_cursor();
645 blink_state = BLINK_OFF;
646 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
647 (TIMERPROC)_OnBlinkTimer);
648 }
649 else
650 {
651 gui_update_cursor(TRUE, FALSE);
652 blink_state = BLINK_ON;
653 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100654 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100655 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100656 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100657}
658
659 static void
660gui_mswin_rm_blink_timer(void)
661{
662 MSG msg;
663
664 if (blink_timer != 0)
665 {
666 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100667 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
669 ;
670 blink_timer = 0;
671 }
672}
673
674/*
675 * Stop the cursor blinking. Show the cursor if it wasn't shown.
676 */
677 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100678gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679{
680 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100681 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100682 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100684 gui_mch_flush();
685 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 blink_state = BLINK_NONE;
687}
688
689/*
690 * Start the cursor blinking. If it was already blinking, this restarts the
691 * waiting time and shows the cursor.
692 */
693 void
694gui_mch_start_blink(void)
695{
696 gui_mswin_rm_blink_timer();
697
Bram Moolenaar734a8672019-12-02 22:49:38 +0100698 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100699 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
700 {
701 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
702 (TIMERPROC)_OnBlinkTimer);
703 blink_state = BLINK_ON;
704 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100705 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 }
707}
708
709/*
710 * Call-back routines.
711 */
712
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100713 static VOID CALLBACK
714_OnTimer(
715 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100716 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100718 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719{
720 MSG msg;
721
722 /*
723 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
724 */
725 KillTimer(NULL, idEvent);
726 s_timed_out = TRUE;
727
Bram Moolenaar734a8672019-12-02 22:49:38 +0100728 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100729 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
730 ;
731 if (idEvent == s_wait_timer)
732 s_wait_timer = 0;
733}
734
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100735 static void
736_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100737 HWND hwnd UNUSED,
738 UINT ch UNUSED,
739 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100740{
741 dead_key = 1;
742}
743
744/*
745 * Convert Unicode character "ch" to bytes in "string[slen]".
746 * When "had_alt" is TRUE the ALT key was included in "ch".
747 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200748 * Because the Windows API uses UTF-16, we have to deal with surrogate
749 * pairs; this is where we choose to deal with them: if "ch" is a high
750 * surrogate, it will be stored, and the length returned will be zero; the next
751 * char_to_string call will then include the high surrogate, decoding the pair
752 * of UTF-16 code units to a single Unicode code point, presuming it is the
753 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100754 */
755 static int
756char_to_string(int ch, char_u *string, int slen, int had_alt)
757{
758 int len;
759 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200761 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200763 if (surrogate_pending_ch != 0)
764 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100765 // We don't guarantee ch is a low surrogate to match the high surrogate
766 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200767 wstring[0] = surrogate_pending_ch;
768 wstring[1] = ch;
769 surrogate_pending_ch = 0;
770 len = 2;
771 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100772 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200773 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100774 // We don't have the entire code point yet, only the first UTF-16 code
775 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200776 surrogate_pending_ch = ch;
777 return 0;
778 }
779 else
780 {
781 wstring[0] = ch;
782 len = 1;
783 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200784
Bram Moolenaar734a8672019-12-02 22:49:38 +0100785 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
786 // "enc_codepage" is non-zero use the standard Win32 function,
787 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200788 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100789 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
791 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100792 // If we had included the ALT key into the character but now the
793 // upper bit is no longer set, that probably means the conversion
794 // failed. Convert the original character and set the upper bit
795 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100797 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200798 wstring[0] = ch & 0x7f;
799 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
800 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100801 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 }
804 }
805 else
806 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200807 ws = utf16_to_enc(wstring, &len);
808 if (ws == NULL)
809 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100810 else
811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100812 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 len = slen;
814 mch_memmove(string, ws, len);
815 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816 }
817 }
818
819 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 {
821 string[0] = ch;
822 len = 1;
823 }
824
825 for (i = 0; i < len; ++i)
826 if (string[i] == CSI && len <= slen - 2)
827 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100828 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
830 string[++i] = KS_EXTRA;
831 string[++i] = (int)KE_CSI;
832 len += 2;
833 }
834
835 return len;
836}
837
838/*
839 * Key hit, add it to the input buffer.
840 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100841 static void
842_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100843 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100845 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100846{
847 char_u string[40];
848 int len = 0;
849
850 dead_key = 0;
851
852 len = char_to_string(ch, string, 40, FALSE);
853 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
854 {
855 trash_input_buf();
856 got_int = TRUE;
857 }
858
859 add_to_input_buf(string, len);
860}
861
862/*
863 * Alt-Key hit, add it to the input buffer.
864 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100865 static void
866_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100867 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100868 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100869 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100871 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872 int len;
873 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100874 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100875
876 dead_key = 0;
877
Bram Moolenaar734a8672019-12-02 22:49:38 +0100878 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879
Bram Moolenaar734a8672019-12-02 22:49:38 +0100880 // OK, we have a character key (given by ch) which was entered with the
881 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
882 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
883 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100884 modifiers = MOD_MASK_ALT;
885 if (GetKeyState(VK_SHIFT) & 0x8000)
886 modifiers |= MOD_MASK_SHIFT;
887 if (GetKeyState(VK_CONTROL) & 0x8000)
888 modifiers |= MOD_MASK_CTRL;
889
890 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100891 // remove the SHIFT modifier for keys where it's already included, e.g.,
892 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200893 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100894
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200895 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
896 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100897 if (ch == CSI)
898 ch = K_CSI;
899
900 len = 0;
901 if (modifiers)
902 {
903 string[len++] = CSI;
904 string[len++] = KS_MODIFIER;
905 string[len++] = modifiers;
906 }
907
908 if (IS_SPECIAL((int)ch))
909 {
910 string[len++] = CSI;
911 string[len++] = K_SECOND((int)ch);
912 string[len++] = K_THIRD((int)ch);
913 }
914 else
915 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100916 // Although the documentation isn't clear about it, we assume "ch" is
917 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100918 len += char_to_string(ch, string + len, 40 - len, TRUE);
919 }
920
921 add_to_input_buf(string, len);
922}
923
924 static void
925_OnMouseEvent(
926 int button,
927 int x,
928 int y,
929 int repeated_click,
930 UINT keyFlags)
931{
932 int vim_modifiers = 0x0;
933
934 s_getting_focus = FALSE;
935
936 if (keyFlags & MK_SHIFT)
937 vim_modifiers |= MOUSE_SHIFT;
938 if (keyFlags & MK_CONTROL)
939 vim_modifiers |= MOUSE_CTRL;
940 if (GetKeyState(VK_MENU) & 0x8000)
941 vim_modifiers |= MOUSE_ALT;
942
943 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
944}
945
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100946 static void
947_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100948 HWND hwnd UNUSED,
949 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950 int x,
951 int y,
952 UINT keyFlags)
953{
954 static LONG s_prevTime = 0;
955
956 LONG currentTime = GetMessageTime();
957 int button = -1;
958 int repeated_click;
959
Bram Moolenaar734a8672019-12-02 22:49:38 +0100960 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100961 (void)SetFocus(s_hwnd);
962
963 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
964 button = MOUSE_LEFT;
965 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
966 button = MOUSE_MIDDLE;
967 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
968 button = MOUSE_RIGHT;
969 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
970 {
971#ifndef GET_XBUTTON_WPARAM
972# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
973#endif
974 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
975 }
976 else if (s_uMsg == WM_CAPTURECHANGED)
977 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100978 // on W95/NT4, somehow you get in here with an odd Msg
979 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100980 if (s_button_pending == MOUSE_LEFT)
981 button = MOUSE_RIGHT;
982 else
983 button = MOUSE_LEFT;
984 }
985 if (button >= 0)
986 {
987 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
988
989 /*
990 * Holding down the left and right buttons simulates pushing the middle
991 * button.
992 */
993 if (repeated_click
994 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
995 || (button == MOUSE_RIGHT
996 && s_button_pending == MOUSE_LEFT)))
997 {
998 /*
999 * Hmm, gui.c will ignore more than one button down at a time, so
1000 * pretend we let go of it first.
1001 */
1002 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1003 button = MOUSE_MIDDLE;
1004 repeated_click = FALSE;
1005 s_button_pending = -1;
1006 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1007 }
1008 else if ((repeated_click)
1009 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1010 {
1011 if (s_button_pending > -1)
1012 {
1013 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1014 s_button_pending = -1;
1015 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001016 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001017 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1018 }
1019 else
1020 {
1021 /*
1022 * If this is the first press (i.e. not a multiple click) don't
1023 * action immediately, but store and wait for:
1024 * i) button-up
1025 * ii) mouse move
1026 * iii) another button press
1027 * before using it.
1028 * This enables us to make left+right simulate middle button,
1029 * without left or right being actioned first. The side-effect is
1030 * that if you click and hold the mouse without dragging, the
1031 * cursor doesn't move until you release the button. In practice
1032 * this is hardly a problem.
1033 */
1034 s_button_pending = button;
1035 s_x_pending = x;
1036 s_y_pending = y;
1037 s_kFlags_pending = keyFlags;
1038 }
1039
1040 s_prevTime = currentTime;
1041 }
1042}
1043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001044 static void
1045_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001046 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001047 int x,
1048 int y,
1049 UINT keyFlags)
1050{
1051 int button;
1052
1053 s_getting_focus = FALSE;
1054 if (s_button_pending > -1)
1055 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001056 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057 _OnMouseEvent(s_button_pending, s_x_pending,
1058 s_y_pending, FALSE, s_kFlags_pending);
1059 s_button_pending = -1;
1060 }
1061 if (s_uMsg == WM_MOUSEMOVE)
1062 {
1063 /*
1064 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1065 * down.
1066 */
1067 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1068 | MK_XBUTTON1 | MK_XBUTTON2)))
1069 {
1070 gui_mouse_moved(x, y);
1071 return;
1072 }
1073
1074 /*
1075 * While button is down, keep grabbing mouse move events when
1076 * the mouse goes outside the window
1077 */
1078 SetCapture(s_textArea);
1079 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001080 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001081 }
1082 else
1083 {
1084 ReleaseCapture();
1085 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001086 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001087 }
1088
1089 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1090}
1091
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001092 static void
1093_OnSizeTextArea(
1094 HWND hwnd UNUSED,
1095 UINT state UNUSED,
1096 int cx UNUSED,
1097 int cy UNUSED)
1098{
1099#if defined(FEAT_DIRECTX)
1100 if (IS_ENABLE_DIRECTX())
1101 directx_binddc();
1102#endif
1103}
1104
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105#ifdef FEAT_MENU
1106/*
1107 * Find the vimmenu_T with the given id
1108 */
1109 static vimmenu_T *
1110gui_mswin_find_menu(
1111 vimmenu_T *pMenu,
1112 int id)
1113{
1114 vimmenu_T *pChildMenu;
1115
1116 while (pMenu)
1117 {
1118 if (pMenu->id == (UINT)id)
1119 break;
1120 if (pMenu->children != NULL)
1121 {
1122 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1123 if (pChildMenu)
1124 {
1125 pMenu = pChildMenu;
1126 break;
1127 }
1128 }
1129 pMenu = pMenu->next;
1130 }
1131 return pMenu;
1132}
1133
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001134 static void
1135_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001136 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001137 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001138 HWND hwndCtl UNUSED,
1139 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001140{
1141 vimmenu_T *pMenu;
1142
1143 pMenu = gui_mswin_find_menu(root_menu, id);
1144 if (pMenu)
1145 gui_menu_cb(pMenu);
1146}
1147#endif
1148
1149#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001150/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001151 * Handle a Find/Replace window message.
1152 */
1153 static void
1154_OnFindRepl(void)
1155{
1156 int flags = 0;
1157 int down;
1158
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001159 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001160 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001161 (void)SetFocus(s_hwnd);
1162
1163 if (s_findrep_struct.Flags & FR_FINDNEXT)
1164 {
1165 flags = FRD_FINDNEXT;
1166
Bram Moolenaar734a8672019-12-02 22:49:38 +01001167 // Give main window the focus back: this is so the cursor isn't
1168 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001169 (void)SetFocus(s_hwnd);
1170 }
1171 else if (s_findrep_struct.Flags & FR_REPLACE)
1172 {
1173 flags = FRD_REPLACE;
1174
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Give main window the focus back: this is so the cursor isn't
1176 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001177 (void)SetFocus(s_hwnd);
1178 }
1179 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1180 {
1181 flags = FRD_REPLACEALL;
1182 }
1183
1184 if (flags != 0)
1185 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001186 char_u *p, *q;
1187
Bram Moolenaar734a8672019-12-02 22:49:38 +01001188 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1190 flags |= FRD_WHOLE_WORD;
1191 if (s_findrep_struct.Flags & FR_MATCHCASE)
1192 flags |= FRD_MATCH_CASE;
1193 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001194 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1195 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1196 if (p != NULL && q != NULL)
1197 gui_do_findrepl(flags, p, q, down);
1198 vim_free(p);
1199 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 }
1201}
1202#endif
1203
1204 static void
1205HandleMouseHide(UINT uMsg, LPARAM lParam)
1206{
1207 static LPARAM last_lParam = 0L;
1208
Bram Moolenaar734a8672019-12-02 22:49:38 +01001209 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001210 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1211 {
1212 if (lParam == last_lParam)
1213 return;
1214 last_lParam = lParam;
1215 }
1216
Bram Moolenaar734a8672019-12-02 22:49:38 +01001217 // Handle specially, to centralise coding. We need to be sure we catch all
1218 // possible events which should cause us to restore the cursor (as it is a
1219 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001220 switch (uMsg)
1221 {
1222 case WM_KEYUP:
1223 case WM_CHAR:
1224 /*
1225 * blank out the pointer if necessary
1226 */
1227 if (p_mh)
1228 gui_mch_mousehide(TRUE);
1229 break;
1230
Bram Moolenaar734a8672019-12-02 22:49:38 +01001231 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001232 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001233 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001234 case WM_LBUTTONDOWN:
1235 case WM_LBUTTONUP:
1236 case WM_MBUTTONDOWN:
1237 case WM_MBUTTONUP:
1238 case WM_RBUTTONDOWN:
1239 case WM_RBUTTONUP:
1240 case WM_XBUTTONDOWN:
1241 case WM_XBUTTONUP:
1242 case WM_NCMOUSEMOVE:
1243 case WM_NCLBUTTONDOWN:
1244 case WM_NCLBUTTONUP:
1245 case WM_NCMBUTTONDOWN:
1246 case WM_NCMBUTTONUP:
1247 case WM_NCRBUTTONDOWN:
1248 case WM_NCRBUTTONUP:
1249 case WM_KILLFOCUS:
1250 /*
1251 * if the pointer is currently hidden, then we should show it.
1252 */
1253 gui_mch_mousehide(FALSE);
1254 break;
1255 }
1256}
1257
1258 static LRESULT CALLBACK
1259_TextAreaWndProc(
1260 HWND hwnd,
1261 UINT uMsg,
1262 WPARAM wParam,
1263 LPARAM lParam)
1264{
1265 /*
1266 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1267 hwnd, uMsg, wParam, lParam);
1268 */
1269
1270 HandleMouseHide(uMsg, lParam);
1271
1272 s_uMsg = uMsg;
1273 s_wParam = wParam;
1274 s_lParam = lParam;
1275
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001276#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001277 TrackUserActivity(uMsg);
1278#endif
1279
1280 switch (uMsg)
1281 {
1282 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1283 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1284 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1285 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1286 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1287 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1288 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1289 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1290 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1291 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1292 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1293 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1294 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1295 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001296 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001297
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001298#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001299 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1300 return TRUE;
1301#endif
1302 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001303 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304 }
1305}
1306
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001307/*
1308 * Called when the foreground or background color has been changed.
1309 */
1310 void
1311gui_mch_new_colors(void)
1312{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001313 HBRUSH prevBrush;
1314
1315 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001316 prevBrush = (HBRUSH)SetClassLongPtr(
1317 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001318 InvalidateRect(s_hwnd, NULL, TRUE);
1319 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001320}
1321
1322/*
1323 * Set the colors to their default values.
1324 */
1325 void
1326gui_mch_def_colors(void)
1327{
1328 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1329 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1330 gui.def_norm_pixel = gui.norm_pixel;
1331 gui.def_back_pixel = gui.back_pixel;
1332}
1333
1334/*
1335 * Open the GUI window which was created by a call to gui_mch_init().
1336 */
1337 int
1338gui_mch_open(void)
1339{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001340 // Actually open the window, if not already visible
1341 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001342 if (!IsWindowVisible(s_hwnd))
1343 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1344
1345#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001346 // Init replace string here, so that we keep it when re-opening the
1347 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001348 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1349#endif
1350
1351 return OK;
1352}
1353
1354/*
1355 * Get the position of the top left corner of the window.
1356 */
1357 int
1358gui_mch_get_winpos(int *x, int *y)
1359{
1360 RECT rect;
1361
1362 GetWindowRect(s_hwnd, &rect);
1363 *x = rect.left;
1364 *y = rect.top;
1365 return OK;
1366}
1367
1368/*
1369 * Set the position of the top left corner of the window to the given
1370 * coordinates.
1371 */
1372 void
1373gui_mch_set_winpos(int x, int y)
1374{
1375 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1376 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1377}
1378 void
1379gui_mch_set_text_area_pos(int x, int y, int w, int h)
1380{
1381 static int oldx = 0;
1382 static int oldy = 0;
1383
1384 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1385
1386#ifdef FEAT_TOOLBAR
1387 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1388 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001389 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001390#endif
1391#if defined(FEAT_GUI_TABLINE)
1392 if (showing_tabline)
1393 {
1394 int top = 0;
1395 RECT rect;
1396
1397# ifdef FEAT_TOOLBAR
1398 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001399 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001400# endif
1401 GetClientRect(s_hwnd, &rect);
1402 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1403 }
1404#endif
1405
Bram Moolenaar734a8672019-12-02 22:49:38 +01001406 // When side scroll bar is unshown, the size of window will change.
1407 // then, the text area move left or right. thus client rect should be
1408 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001409 if (oldx != x || oldy != y)
1410 {
1411 InvalidateRect(s_hwnd, NULL, FALSE);
1412 oldx = x;
1413 oldy = y;
1414 }
1415}
1416
1417
1418/*
1419 * Scrollbar stuff:
1420 */
1421
1422 void
1423gui_mch_enable_scrollbar(
1424 scrollbar_T *sb,
1425 int flag)
1426{
1427 ShowScrollBar(sb->id, SB_CTL, flag);
1428
Bram Moolenaar734a8672019-12-02 22:49:38 +01001429 // TODO: When the window is maximized, the size of the window stays the
1430 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1431 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001432}
1433
1434 void
1435gui_mch_set_scrollbar_pos(
1436 scrollbar_T *sb,
1437 int x,
1438 int y,
1439 int w,
1440 int h)
1441{
1442 SetWindowPos(sb->id, NULL, x, y, w, h,
1443 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1444}
1445
Bram Moolenaar203ec772020-07-17 20:43:43 +02001446 int
1447gui_mch_get_scrollbar_xpadding(void)
1448{
1449 RECT rcTxt, rcWnd;
1450 int xpad;
1451
1452 GetWindowRect(s_textArea, &rcTxt);
1453 GetWindowRect(s_hwnd, &rcWnd);
1454 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001455 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1456 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001457 return (xpad < 0) ? 0 : xpad;
1458}
1459
1460 int
1461gui_mch_get_scrollbar_ypadding(void)
1462{
1463 RECT rcTxt, rcWnd;
1464 int ypad;
1465
1466 GetWindowRect(s_textArea, &rcTxt);
1467 GetWindowRect(s_hwnd, &rcWnd);
1468 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001469 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1470 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001471 return (ypad < 0) ? 0 : ypad;
1472}
1473
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001474 void
1475gui_mch_create_scrollbar(
1476 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001477 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001478{
1479 sb->id = CreateWindow(
1480 "SCROLLBAR", "Scrollbar",
1481 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001482 10, // Any value will do for now
1483 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001484 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001485 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001486}
1487
1488/*
1489 * Find the scrollbar with the given hwnd.
1490 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001491 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001492gui_mswin_find_scrollbar(HWND hwnd)
1493{
1494 win_T *wp;
1495
1496 if (gui.bottom_sbar.id == hwnd)
1497 return &gui.bottom_sbar;
1498 FOR_ALL_WINDOWS(wp)
1499 {
1500 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1501 return &wp->w_scrollbars[SBAR_LEFT];
1502 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1503 return &wp->w_scrollbars[SBAR_RIGHT];
1504 }
1505 return NULL;
1506}
1507
K.Takatac81e9bf2022-01-16 14:15:49 +00001508 static void
1509update_scrollbar_size(void)
1510{
1511 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1512 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1513}
1514
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515/*
1516 * Get the character size of a font.
1517 */
1518 static void
1519GetFontSize(GuiFont font)
1520{
1521 HWND hwnd = GetDesktopWindow();
1522 HDC hdc = GetWindowDC(hwnd);
1523 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001524 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 TEXTMETRIC tm;
1526
1527 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001528 // GetTextMetrics() may not return the right value in tmAveCharWidth
1529 // for some fonts. Do our own average computation.
1530 GetTextExtentPoint(hdc,
1531 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1532 52, &size);
1533 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001534
1535 gui.char_height = tm.tmHeight + p_linespace;
1536
1537 SelectFont(hdc, hfntOld);
1538
1539 ReleaseDC(hwnd, hdc);
1540}
1541
1542/*
1543 * Adjust gui.char_height (after 'linespace' was changed).
1544 */
1545 int
1546gui_mch_adjust_charheight(void)
1547{
1548 GetFontSize(gui.norm_font);
1549 return OK;
1550}
1551
1552 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001553get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554{
1555 HFONT font = NULL;
1556
Bram Moolenaar734a8672019-12-02 22:49:38 +01001557 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001558 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559
1560 if (font == NULL)
1561 return NOFONT;
1562
1563 return (GuiFont)font;
1564}
1565
1566 static int
1567pixels_to_points(int pixels, int vertical)
1568{
1569 int points;
1570 HWND hwnd;
1571 HDC hdc;
1572
1573 hwnd = GetDesktopWindow();
1574 hdc = GetWindowDC(hwnd);
1575
1576 points = MulDiv(pixels, 72,
1577 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1578
1579 ReleaseDC(hwnd, hdc);
1580
1581 return points;
1582}
1583
1584 GuiFont
1585gui_mch_get_font(
1586 char_u *name,
1587 int giveErrorIfMissing)
1588{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001589 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 GuiFont font = NOFONT;
1591
1592 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001593 {
1594 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001596 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001598 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599 return font;
1600}
1601
1602#if defined(FEAT_EVAL) || defined(PROTO)
1603/*
1604 * Return the name of font "font" in allocated memory.
1605 * Don't know how to get the actual name, thus use the provided name.
1606 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001607 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001608gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001609{
1610 if (name == NULL)
1611 return NULL;
1612 return vim_strsave(name);
1613}
1614#endif
1615
1616 void
1617gui_mch_free_font(GuiFont font)
1618{
1619 if (font)
1620 DeleteObject((HFONT)font);
1621}
1622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001623/*
1624 * Return the Pixel value (color) for the given color name.
1625 * Return INVALCOLOR for error.
1626 */
1627 guicolor_T
1628gui_mch_get_color(char_u *name)
1629{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001630 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001631
1632 typedef struct SysColorTable
1633 {
1634 char *name;
1635 int color;
1636 } SysColorTable;
1637
1638 static SysColorTable sys_table[] =
1639 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1641 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001642#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1644#endif
1645 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1646 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1647 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1648 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1649 {"SYS_DESKTOP", COLOR_DESKTOP},
1650 {"SYS_INFOBK", COLOR_INFOBK},
1651 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1652 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 {"SYS_BTNFACE", COLOR_BTNFACE},
1654 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1655 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1656 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1657 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1658 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1659 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1660 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1661 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1662 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1663 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1664 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1665 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1666 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1667 {"SYS_MENU", COLOR_MENU},
1668 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1669 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1670 {"SYS_WINDOW", COLOR_WINDOW},
1671 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1672 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1673 };
1674
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001675 /*
1676 * Try to look up a system colour.
1677 */
K.Takataeeec2542021-06-02 13:28:16 +02001678 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001679 if (STRICMP(name, sys_table[i].name) == 0)
1680 return GetSysColor(sys_table[i].color);
1681
Bram Moolenaarab302212016-04-26 20:59:29 +02001682 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001683}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001684
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001685 guicolor_T
1686gui_mch_get_rgb_color(int r, int g, int b)
1687{
1688 return gui_get_rgb_color_cmn(r, g, b);
1689}
1690
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691/*
1692 * Return OK if the key with the termcap name "name" is supported.
1693 */
1694 int
1695gui_mch_haskey(char_u *name)
1696{
1697 int i;
1698
1699 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1700 if (name[0] == special_keys[i].vim_code0 &&
1701 name[1] == special_keys[i].vim_code1)
1702 return OK;
1703 return FAIL;
1704}
1705
1706 void
1707gui_mch_beep(void)
1708{
1709 MessageBeep(MB_OK);
1710}
1711/*
1712 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1713 */
1714 void
1715gui_mch_invert_rectangle(
1716 int r,
1717 int c,
1718 int nr,
1719 int nc)
1720{
1721 RECT rc;
1722
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001723#if defined(FEAT_DIRECTX)
1724 if (IS_ENABLE_DIRECTX())
1725 DWriteContext_Flush(s_dwc);
1726#endif
1727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 /*
1729 * Note: InvertRect() excludes right and bottom of rectangle.
1730 */
1731 rc.left = FILL_X(c);
1732 rc.top = FILL_Y(r);
1733 rc.right = rc.left + nc * gui.char_width;
1734 rc.bottom = rc.top + nr * gui.char_height;
1735 InvertRect(s_hdc, &rc);
1736}
1737
1738/*
1739 * Iconify the GUI window.
1740 */
1741 void
1742gui_mch_iconify(void)
1743{
1744 ShowWindow(s_hwnd, SW_MINIMIZE);
1745}
1746
1747/*
1748 * Draw a cursor without focus.
1749 */
1750 void
1751gui_mch_draw_hollow_cursor(guicolor_T color)
1752{
1753 HBRUSH hbr;
1754 RECT rc;
1755
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001756#if defined(FEAT_DIRECTX)
1757 if (IS_ENABLE_DIRECTX())
1758 DWriteContext_Flush(s_dwc);
1759#endif
1760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761 /*
1762 * Note: FrameRect() excludes right and bottom of rectangle.
1763 */
1764 rc.left = FILL_X(gui.col);
1765 rc.top = FILL_Y(gui.row);
1766 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001767 if (mb_lefthalve(gui.row, gui.col))
1768 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001769 rc.bottom = rc.top + gui.char_height;
1770 hbr = CreateSolidBrush(color);
1771 FrameRect(s_hdc, &rc, hbr);
1772 DeleteBrush(hbr);
1773}
1774/*
1775 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1776 * color "color".
1777 */
1778 void
1779gui_mch_draw_part_cursor(
1780 int w,
1781 int h,
1782 guicolor_T color)
1783{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001784 RECT rc;
1785
1786 /*
1787 * Note: FillRect() excludes right and bottom of rectangle.
1788 */
1789 rc.left =
1790#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001791 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1793#endif
1794 FILL_X(gui.col);
1795 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1796 rc.right = rc.left + w;
1797 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001798
Bram Moolenaar92467d32017-12-05 13:22:16 +01001799 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800}
1801
1802
1803/*
1804 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1805 * dead key's nominal character and re-post the original message.
1806 */
1807 static void
1808outputDeadKey_rePost(MSG originalMsg)
1809{
1810 static MSG deadCharExpel;
1811
1812 if (!dead_key)
1813 return;
1814
1815 dead_key = 0;
1816
Bram Moolenaar734a8672019-12-02 22:49:38 +01001817 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 deadCharExpel.message = originalMsg.message;
1819 deadCharExpel.hwnd = originalMsg.hwnd;
1820 deadCharExpel.wParam = VK_SPACE;
1821
K.Takata4ac893f2022-01-20 12:44:28 +00001822 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823
Bram Moolenaar734a8672019-12-02 22:49:38 +01001824 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1826 originalMsg.lParam);
1827}
1828
1829
1830/*
1831 * Process a single Windows message.
1832 * If one is not available we hang until one is.
1833 */
1834 static void
1835process_message(void)
1836{
1837 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001838 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001839 char_u string[40];
1840 int i;
1841 int modifiers = 0;
1842 int key;
1843#ifdef FEAT_MENU
1844 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1845#endif
1846
1847 pGetMessage(&msg, NULL, 0, 0);
1848
1849#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001850 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851 if (msg.message == WM_OLE)
1852 {
1853 char_u *str = (char_u *)msg.lParam;
1854 if (str == NULL || *str == NUL)
1855 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001856 // Message can't be ours, forward it. Fixes problem with Ultramon
1857 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858 pDispatchMessage(&msg);
1859 }
1860 else
1861 {
1862 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 }
1865 return;
1866 }
1867#endif
1868
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001870 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1872 {
1873 HandleMouseHide(msg.message, msg.lParam);
1874 return;
1875 }
1876#endif
1877
1878 /*
1879 * Check if it's a special key that we recognise. If not, call
1880 * TranslateMessage().
1881 */
1882 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1883 {
1884 vk = (int) msg.wParam;
1885
1886 /*
1887 * Handle dead keys in special conditions in other cases we let Windows
1888 * handle them and do not interfere.
1889 *
1890 * The dead_key flag must be reset on several occasions:
1891 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1892 * consumed at that point (This is when we let Windows combine the
1893 * dead character on its own)
1894 *
1895 * - Before doing something special such as regenerating keypresses to
1896 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001897 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898 * immediately to _OnChar() (or _OnSysChar()).
1899 */
1900 if (dead_key)
1901 {
1902 /*
1903 * If a dead key was pressed and the user presses VK_SPACE,
1904 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1905 * with the dead char now, so do nothing special and let Windows
1906 * handle it.
1907 *
1908 * Note that VK_SPACE combines with the dead_key's character and
1909 * only one WM_CHAR will be generated by TranslateMessage(), in
1910 * the two other cases two WM_CHAR will be generated: the dead
1911 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1912 * user expects.
1913 */
1914 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1915 {
1916 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001917 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001918 return;
1919 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001920 // In modes where we are not typing, dead keys should behave
1921 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001922 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1923 {
1924 outputDeadKey_rePost(msg);
1925 return;
1926 }
1927 }
1928
Bram Moolenaar734a8672019-12-02 22:49:38 +01001929 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 if (vk == VK_CANCEL)
1931 {
1932 trash_input_buf();
1933 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001934 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 string[0] = Ctrl_C;
1936 add_to_input_buf(string, 1);
1937 }
1938
1939 for (i = 0; special_keys[i].key_sym != 0; i++)
1940 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001941 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001942 if (special_keys[i].key_sym == vk
1943 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1944 {
1945 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001946 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 * is a key that would normally trigger the dead key nominal
1948 * character output (such as a NUMPAD printable character or
1949 * the TAB key, etc...).
1950 */
1951 if (dead_key && (special_keys[i].vim_code0 == 'K'
1952 || vk == VK_TAB || vk == CAR))
1953 {
1954 outputDeadKey_rePost(msg);
1955 return;
1956 }
1957
1958#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001959 // Check for <F10>: Windows selects the menu. When <F10> is
1960 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001961 if (vk == VK_F10
1962 && gui.menu_is_active
1963 && check_map(k10, State, FALSE, TRUE, FALSE,
1964 NULL, NULL) == NULL)
1965 break;
1966#endif
1967 if (GetKeyState(VK_SHIFT) & 0x8000)
1968 modifiers |= MOD_MASK_SHIFT;
1969 /*
1970 * Don't use caps-lock as shift, because these are special keys
1971 * being considered here, and we only want letters to get
1972 * shifted -- webb
1973 */
1974 /*
1975 if (GetKeyState(VK_CAPITAL) & 0x0001)
1976 modifiers ^= MOD_MASK_SHIFT;
1977 */
1978 if (GetKeyState(VK_CONTROL) & 0x8000)
1979 modifiers |= MOD_MASK_CTRL;
1980 if (GetKeyState(VK_MENU) & 0x8000)
1981 modifiers |= MOD_MASK_ALT;
1982
1983 if (special_keys[i].vim_code1 == NUL)
1984 key = special_keys[i].vim_code0;
1985 else
1986 key = TO_SPECIAL(special_keys[i].vim_code0,
1987 special_keys[i].vim_code1);
1988 key = simplify_key(key, &modifiers);
1989 if (key == CSI)
1990 key = K_CSI;
1991
1992 if (modifiers)
1993 {
1994 string[0] = CSI;
1995 string[1] = KS_MODIFIER;
1996 string[2] = modifiers;
1997 add_to_input_buf(string, 3);
1998 }
1999
2000 if (IS_SPECIAL(key))
2001 {
2002 string[0] = CSI;
2003 string[1] = K_SECOND(key);
2004 string[2] = K_THIRD(key);
2005 add_to_input_buf(string, 3);
2006 }
2007 else
2008 {
2009 int len;
2010
Bram Moolenaar734a8672019-12-02 22:49:38 +01002011 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002012 len = char_to_string(key, string, 40, FALSE);
2013 add_to_input_buf(string, len);
2014 }
2015 break;
2016 }
2017 }
2018 if (special_keys[i].key_sym == 0)
2019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002020 // Some keys need C-S- where they should only need C-.
2021 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2022 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002023 if (vk != 0xff
2024 && (GetKeyState(VK_CONTROL) & 0x8000)
2025 && !(GetKeyState(VK_SHIFT) & 0x8000)
2026 && !(GetKeyState(VK_MENU) & 0x8000))
2027 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002028 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002029 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2030 {
2031 string[0] = Ctrl_HAT;
2032 add_to_input_buf(string, 1);
2033 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002034 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2035 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002036 {
2037 string[0] = Ctrl__;
2038 add_to_input_buf(string, 1);
2039 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002040 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2042 {
2043 string[0] = Ctrl_AT;
2044 add_to_input_buf(string, 1);
2045 }
2046 else
K.Takata4ac893f2022-01-20 12:44:28 +00002047 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048 }
2049 else
K.Takata4ac893f2022-01-20 12:44:28 +00002050 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 }
2052 }
2053#ifdef FEAT_MBYTE_IME
2054 else if (msg.message == WM_IME_NOTIFY)
2055 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2056 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002057 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002058 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059#endif
2060
2061#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002062 // Check for <F10>: Default effect is to select the menu. When <F10> is
2063 // mapped we need to stop it here to avoid strange effects (e.g., for the
2064 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002065 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2066 NULL, NULL) == NULL)
2067#endif
2068 pDispatchMessage(&msg);
2069}
2070
2071/*
2072 * Catch up with any queued events. This may put keyboard input into the
2073 * input buffer, call resize call-backs, trigger timers etc. If there is
2074 * nothing in the event queue (& no timers pending), then we return
2075 * immediately.
2076 */
2077 void
2078gui_mch_update(void)
2079{
2080 MSG msg;
2081
2082 if (!s_busy_processing)
2083 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2084 && !vim_is_input_buf_full())
2085 process_message();
2086}
2087
Bram Moolenaar4231da42016-06-02 14:30:04 +02002088 static void
2089remove_any_timer(void)
2090{
2091 MSG msg;
2092
2093 if (s_wait_timer != 0 && !s_timed_out)
2094 {
2095 KillTimer(NULL, s_wait_timer);
2096
Bram Moolenaar734a8672019-12-02 22:49:38 +01002097 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002098 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2099 ;
2100 s_wait_timer = 0;
2101 }
2102}
2103
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104/*
2105 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2106 * from the keyboard.
2107 * wtime == -1 Wait forever.
2108 * wtime == 0 This should never happen.
2109 * wtime > 0 Wait wtime milliseconds for a character.
2110 * Returns OK if a character was found to be available within the given time,
2111 * or FAIL otherwise.
2112 */
2113 int
2114gui_mch_wait_for_chars(int wtime)
2115{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116 int focus;
2117
2118 s_timed_out = FALSE;
2119
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002120 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002122 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123 if (s_busy_processing)
2124 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002125
2126 // When called with "wtime" zero, just want one msec.
2127 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002128 (TIMERPROC)_OnTimer);
2129 }
2130
2131 allow_scrollbar = TRUE;
2132
2133 focus = gui.in_focus;
2134 while (!s_timed_out)
2135 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002137 if (gui.in_focus != focus)
2138 {
2139 if (gui.in_focus)
2140 gui_mch_start_blink();
2141 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002142 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002143 focus = gui.in_focus;
2144 }
2145
2146 if (s_need_activate)
2147 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 s_need_activate = FALSE;
2150 }
2151
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152#ifdef FEAT_TIMERS
2153 did_add_timer = FALSE;
2154#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002156 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002157 for (;;)
2158 {
2159 MSG msg;
2160
2161 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002162# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002163 if (did_add_timer)
2164 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002165# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002166 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2167 {
2168 process_message();
2169 break;
2170 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002171 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002172 // TODO: The 10 msec is a compromise between laggy response
2173 // and consuming more CPU time. Better would be to handle
2174 // channel messages when they arrive.
2175 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002176 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002177 break;
2178 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002179#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002180 // Don't use gui_mch_update() because then we will spin-lock until a
2181 // char arrives, instead we use GetMessage() to hang until an
2182 // event arrives. No need to check for input_buf_full because we are
2183 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002184 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002185#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186
2187 if (input_available())
2188 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002189 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002190 allow_scrollbar = FALSE;
2191
Bram Moolenaar89c00032019-09-04 13:53:21 +02002192 // Clear pending mouse button, the release event may have been
2193 // taken by the dialog window. But don't do this when getting
2194 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195 if (!s_getting_focus)
2196 s_button_pending = -1;
2197
2198 return OK;
2199 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002200
2201#ifdef FEAT_TIMERS
2202 if (did_add_timer)
2203 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002204 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002205 remove_any_timer();
2206 break;
2207 }
2208#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002209 }
2210 allow_scrollbar = FALSE;
2211 return FAIL;
2212}
2213
2214/*
2215 * Clear a rectangular region of the screen from text pos (row1, col1) to
2216 * (row2, col2) inclusive.
2217 */
2218 void
2219gui_mch_clear_block(
2220 int row1,
2221 int col1,
2222 int row2,
2223 int col2)
2224{
2225 RECT rc;
2226
2227 /*
2228 * Clear one extra pixel at the far right, for when bold characters have
2229 * spilled over to the window border.
2230 * Note: FillRect() excludes right and bottom of rectangle.
2231 */
2232 rc.left = FILL_X(col1);
2233 rc.top = FILL_Y(row1);
2234 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2235 rc.bottom = FILL_Y(row2 + 1);
2236 clear_rect(&rc);
2237}
2238
2239/*
2240 * Clear the whole text window.
2241 */
2242 void
2243gui_mch_clear_all(void)
2244{
2245 RECT rc;
2246
2247 rc.left = 0;
2248 rc.top = 0;
2249 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2250 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2251 clear_rect(&rc);
2252}
2253/*
2254 * Menu stuff.
2255 */
2256
2257 void
2258gui_mch_enable_menu(int flag)
2259{
2260#ifdef FEAT_MENU
2261 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2262#endif
2263}
2264
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002265 void
2266gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002267 int x UNUSED,
2268 int y UNUSED,
2269 int w UNUSED,
2270 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002272 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273}
2274
2275#if defined(FEAT_MENU) || defined(PROTO)
2276/*
2277 * Make menu item hidden or not hidden
2278 */
2279 void
2280gui_mch_menu_hidden(
2281 vimmenu_T *menu,
2282 int hidden)
2283{
2284 /*
2285 * This doesn't do what we want. Hmm, just grey the menu items for now.
2286 */
2287 /*
2288 if (hidden)
2289 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2290 else
2291 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2292 */
2293 gui_mch_menu_grey(menu, hidden);
2294}
2295
2296/*
2297 * This is called after setting all the menus to grey/hidden or not.
2298 */
2299 void
2300gui_mch_draw_menubar(void)
2301{
2302 DrawMenuBar(s_hwnd);
2303}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002304#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002306/*
2307 * Return the RGB value of a pixel as a long.
2308 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002309 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310gui_mch_get_rgb(guicolor_T pixel)
2311{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002312 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2313 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314}
2315
2316#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002317/*
2318 * Convert pixels in X to dialog units
2319 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320 static WORD
2321PixelToDialogX(int numPixels)
2322{
2323 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2324}
2325
Bram Moolenaar734a8672019-12-02 22:49:38 +01002326/*
2327 * Convert pixels in Y to dialog units
2328 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002329 static WORD
2330PixelToDialogY(int numPixels)
2331{
2332 return (WORD)((numPixels * 8) / s_dlgfntheight);
2333}
2334
Bram Moolenaar734a8672019-12-02 22:49:38 +01002335/*
2336 * Return the width in pixels of the given text in the given DC.
2337 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 static int
2339GetTextWidth(HDC hdc, char_u *str, int len)
2340{
2341 SIZE size;
2342
2343 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2344 return size.cx;
2345}
2346
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002347/*
2348 * Return the width in pixels of the given text in the given DC, taking care
2349 * of 'encoding' to active codepage conversion.
2350 */
2351 static int
2352GetTextWidthEnc(HDC hdc, char_u *str, int len)
2353{
2354 SIZE size;
2355 WCHAR *wstr;
2356 int n;
2357 int wlen = len;
2358
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002359 wstr = enc_to_utf16(str, &wlen);
2360 if (wstr == NULL)
2361 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002363 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2364 vim_free(wstr);
2365 if (n)
2366 return size.cx;
2367 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002368}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370static void get_work_area(RECT *spi_rect);
2371
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002372/*
2373 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2375 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 */
2377 static BOOL
2378CenterWindow(
2379 HWND hwndChild,
2380 HWND hwndParent)
2381{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002382 HMONITOR mon;
2383 MONITORINFO moninfo;
2384 RECT rChild, rParent, rScreen;
2385 int wChild, hChild, wParent, hParent;
2386 int xNew, yNew;
2387 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002388
2389 GetWindowRect(hwndChild, &rChild);
2390 wChild = rChild.right - rChild.left;
2391 hChild = rChild.bottom - rChild.top;
2392
Bram Moolenaar734a8672019-12-02 22:49:38 +01002393 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002395 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396 else
2397 GetWindowRect(hwndParent, &rParent);
2398 wParent = rParent.right - rParent.left;
2399 hParent = rParent.bottom - rParent.top;
2400
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002401 moninfo.cbSize = sizeof(MONITORINFO);
2402 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2403 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002405 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002407 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002409 hdc = GetDC(hwndChild);
2410 rScreen.left = 0;
2411 rScreen.top = 0;
2412 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2413 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2414 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 }
2416
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002417 xNew = rParent.left + ((wParent - wChild) / 2);
2418 if (xNew < rScreen.left)
2419 xNew = rScreen.left;
2420 else if ((xNew + wChild) > rScreen.right)
2421 xNew = rScreen.right - wChild;
2422
2423 yNew = rParent.top + ((hParent - hChild) / 2);
2424 if (yNew < rScreen.top)
2425 yNew = rScreen.top;
2426 else if ((yNew + hChild) > rScreen.bottom)
2427 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002428
2429 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2430 SWP_NOSIZE | SWP_NOZORDER);
2431}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002432#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434#if defined(FEAT_TOOLBAR) || defined(PROTO)
2435 void
2436gui_mch_show_toolbar(int showit)
2437{
2438 if (s_toolbarhwnd == NULL)
2439 return;
2440
2441 if (showit)
2442 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002443# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002444 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002445# define TB_SETUNICODEFORMAT 0x2005
2446# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002447 // Enable unicode support
2448 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2449 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 ShowWindow(s_toolbarhwnd, SW_SHOW);
2451 }
2452 else
2453 ShowWindow(s_toolbarhwnd, SW_HIDE);
2454}
2455
Bram Moolenaar734a8672019-12-02 22:49:38 +01002456// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002457# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458
2459#endif
2460
2461#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2462 static void
2463add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2464{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002465 WCHAR *wn;
2466 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002467
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002468 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002470 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002472 infow.cbSize = sizeof(infow);
2473 infow.fMask = MIIM_TYPE | MIIM_ID;
2474 infow.wID = item_id;
2475 infow.fType = MFT_STRING;
2476 infow.dwTypeData = wn;
2477 infow.cch = (UINT)wcslen(wn);
2478 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2479 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480}
2481
2482 static void
2483show_tabline_popup_menu(void)
2484{
2485 HMENU tab_pmenu;
2486 long rval;
2487 POINT pt;
2488
Bram Moolenaar734a8672019-12-02 22:49:38 +01002489 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002490 if (hold_gui_events
2491# ifdef FEAT_CMDWIN
2492 || cmdwin_type != 0
2493# endif
2494 )
2495 return;
2496
2497 tab_pmenu = CreatePopupMenu();
2498 if (tab_pmenu == NULL)
2499 return;
2500
2501 if (first_tabpage->tp_next != NULL)
2502 add_tabline_popup_menu_entry(tab_pmenu,
2503 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2504 add_tabline_popup_menu_entry(tab_pmenu,
2505 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2506 add_tabline_popup_menu_entry(tab_pmenu,
2507 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2508
2509 GetCursorPos(&pt);
2510 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2511 NULL);
2512
2513 DestroyMenu(tab_pmenu);
2514
Bram Moolenaar734a8672019-12-02 22:49:38 +01002515 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516 if (rval > 0)
2517 {
2518 TCHITTESTINFO htinfo;
2519 int idx;
2520
2521 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2522 return;
2523
2524 htinfo.pt.x = pt.x;
2525 htinfo.pt.y = pt.y;
2526 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2527 if (idx == -1)
2528 idx = 0;
2529 else
2530 idx += 1;
2531
2532 send_tabline_menu_event(idx, (int)rval);
2533 }
2534}
2535
2536/*
2537 * Show or hide the tabline.
2538 */
2539 void
2540gui_mch_show_tabline(int showit)
2541{
2542 if (s_tabhwnd == NULL)
2543 return;
2544
2545 if (!showit != !showing_tabline)
2546 {
2547 if (showit)
2548 ShowWindow(s_tabhwnd, SW_SHOW);
2549 else
2550 ShowWindow(s_tabhwnd, SW_HIDE);
2551 showing_tabline = showit;
2552 }
2553}
2554
2555/*
2556 * Return TRUE when tabline is displayed.
2557 */
2558 int
2559gui_mch_showing_tabline(void)
2560{
2561 return s_tabhwnd != NULL && showing_tabline;
2562}
2563
2564/*
2565 * Update the labels of the tabline.
2566 */
2567 void
2568gui_mch_update_tabline(void)
2569{
2570 tabpage_T *tp;
2571 TCITEM tie;
2572 int nr = 0;
2573 int curtabidx = 0;
2574 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002575 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576
2577 if (s_tabhwnd == NULL)
2578 return;
2579
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002580# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002581 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002582# define CCM_SETUNICODEFORMAT 0x2005
2583# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002584 // Enable unicode support
2585 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586
2587 tie.mask = TCIF_TEXT;
2588 tie.iImage = -1;
2589
Bram Moolenaar734a8672019-12-02 22:49:38 +01002590 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2592
Bram Moolenaar734a8672019-12-02 22:49:38 +01002593 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2595 {
2596 if (tp == curtab)
2597 curtabidx = nr;
2598
2599 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2600 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002601 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 tie.pszText = "-Empty-";
2603 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2604 tabadded = 1;
2605 }
2606
2607 get_tabline_label(tp, FALSE);
2608 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002610 wstr = enc_to_utf16(NameBuff, NULL);
2611 if (wstr != NULL)
2612 {
2613 TCITEMW tiw;
2614
2615 tiw.mask = TCIF_TEXT;
2616 tiw.iImage = -1;
2617 tiw.pszText = wstr;
2618 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2619 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002621 }
2622
Bram Moolenaar734a8672019-12-02 22:49:38 +01002623 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2625 TabCtrl_DeleteItem(s_tabhwnd, nr);
2626
2627 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2628 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2629
Bram Moolenaar734a8672019-12-02 22:49:38 +01002630 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002631 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2632 RedrawWindow(s_tabhwnd, NULL, NULL,
2633 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2634
2635 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2636 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2637}
2638
2639/*
2640 * Set the current tab to "nr". First tab is 1.
2641 */
2642 void
2643gui_mch_set_curtab(int nr)
2644{
2645 if (s_tabhwnd == NULL)
2646 return;
2647
2648 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2649 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2650}
2651
2652#endif
2653
2654/*
2655 * ":simalt" command.
2656 */
2657 void
2658ex_simalt(exarg_T *eap)
2659{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002660 char_u *keys = eap->arg;
2661 int fill_typebuf = FALSE;
2662 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663
2664 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2665 while (*keys)
2666 {
2667 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2670 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002671 fill_typebuf = TRUE;
2672 }
2673 if (fill_typebuf)
2674 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002675 // Put a NOP in the typeahead buffer so that the message will get
2676 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002677 key_name[0] = K_SPECIAL;
2678 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002679 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002680 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002681#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002682 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002683#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002684 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002685 }
2686}
2687
2688/*
2689 * Create the find & replace dialogs.
2690 * You can't have both at once: ":find" when replace is showing, destroys
2691 * the replace dialog first, and the other way around.
2692 */
2693#ifdef MSWIN_FIND_REPLACE
2694 static void
2695initialise_findrep(char_u *initial_string)
2696{
2697 int wword = FALSE;
2698 int mcase = !p_ic;
2699 char_u *entry_text;
2700
Bram Moolenaar734a8672019-12-02 22:49:38 +01002701 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2703
2704 s_findrep_struct.hwndOwner = s_hwnd;
2705 s_findrep_struct.Flags = FR_DOWN;
2706 if (mcase)
2707 s_findrep_struct.Flags |= FR_MATCHCASE;
2708 if (wword)
2709 s_findrep_struct.Flags |= FR_WHOLEWORD;
2710 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002711 {
2712 WCHAR *p = enc_to_utf16(entry_text, NULL);
2713 if (p != NULL)
2714 {
2715 int len = s_findrep_struct.wFindWhatLen - 1;
2716
2717 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2718 s_findrep_struct.lpstrFindWhat[len] = NUL;
2719 vim_free(p);
2720 }
2721 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722 vim_free(entry_text);
2723}
2724#endif
2725
2726 static void
2727set_window_title(HWND hwnd, char *title)
2728{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002729 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 {
2731 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732
Bram Moolenaar734a8672019-12-02 22:49:38 +01002733 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2735 if (wbuf != NULL)
2736 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002737 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739 }
2740 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002741 else
2742 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743}
2744
2745 void
2746gui_mch_find_dialog(exarg_T *eap)
2747{
2748#ifdef MSWIN_FIND_REPLACE
2749 if (s_findrep_msg != 0)
2750 {
2751 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2752 DestroyWindow(s_findrep_hwnd);
2753
2754 if (!IsWindow(s_findrep_hwnd))
2755 {
2756 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002757 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758 }
2759
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002760 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 (void)SetFocus(s_findrep_hwnd);
2762
2763 s_findrep_is_find = TRUE;
2764 }
2765#endif
2766}
2767
2768
2769 void
2770gui_mch_replace_dialog(exarg_T *eap)
2771{
2772#ifdef MSWIN_FIND_REPLACE
2773 if (s_findrep_msg != 0)
2774 {
2775 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2776 DestroyWindow(s_findrep_hwnd);
2777
2778 if (!IsWindow(s_findrep_hwnd))
2779 {
2780 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002781 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782 }
2783
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002784 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002785 (void)SetFocus(s_findrep_hwnd);
2786
2787 s_findrep_is_find = FALSE;
2788 }
2789#endif
2790}
2791
2792
2793/*
2794 * Set visibility of the pointer.
2795 */
2796 void
2797gui_mch_mousehide(int hide)
2798{
2799 if (hide != gui.pointer_hidden)
2800 {
2801 ShowCursor(!hide);
2802 gui.pointer_hidden = hide;
2803 }
2804}
2805
2806#ifdef FEAT_MENU
2807 static void
2808gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2809{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002810 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811 gui_mch_mousehide(FALSE);
2812
2813 (void)TrackPopupMenu(
2814 (HMENU)menu->submenu_id,
2815 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2816 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002817 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002818 s_hwnd,
2819 NULL);
2820 /*
2821 * NOTE: The pop-up menu can eat the mouse up event.
2822 * We deal with this in normal.c.
2823 */
2824}
2825#endif
2826
2827/*
2828 * Got a message when the system will go down.
2829 */
2830 static void
2831_OnEndSession(void)
2832{
2833 getout_preserve_modified(1);
2834}
2835
2836/*
2837 * Get this message when the user clicks on the cross in the top right corner
2838 * of a Windows95 window.
2839 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002841_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842{
2843 gui_shell_closed();
2844}
2845
2846/*
2847 * Get a message when the window is being destroyed.
2848 */
2849 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002850_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851{
2852 if (!destroying)
2853 _OnClose(hwnd);
2854}
2855
2856 static void
2857_OnPaint(
2858 HWND hwnd)
2859{
2860 if (!IsMinimized(hwnd))
2861 {
2862 PAINTSTRUCT ps;
2863
Bram Moolenaar734a8672019-12-02 22:49:38 +01002864 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002866
Bram Moolenaar734a8672019-12-02 22:49:38 +01002867 // prevent multi-byte characters from misprinting on an invalid
2868 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002869 if (has_mbyte)
2870 {
2871 RECT rect;
2872
2873 GetClientRect(hwnd, &rect);
2874 ps.rcPaint.left = rect.left;
2875 ps.rcPaint.right = rect.right;
2876 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877
2878 if (!IsRectEmpty(&ps.rcPaint))
2879 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2881 ps.rcPaint.right - ps.rcPaint.left + 1,
2882 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2883 }
2884
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885 EndPaint(hwnd, &ps);
2886 }
2887}
2888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889 static void
2890_OnSize(
2891 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002892 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002893 int cx,
2894 int cy)
2895{
K.Takatac81e9bf2022-01-16 14:15:49 +00002896 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897 {
2898 gui_resize_shell(cx, cy);
2899
Bram Moolenaar734a8672019-12-02 22:49:38 +01002900 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902 }
2903}
2904
2905 static void
2906_OnSetFocus(
2907 HWND hwnd,
2908 HWND hwndOldFocus)
2909{
2910 gui_focus_change(TRUE);
2911 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002912 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913}
2914
2915 static void
2916_OnKillFocus(
2917 HWND hwnd,
2918 HWND hwndNewFocus)
2919{
2920 gui_focus_change(FALSE);
2921 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002922 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923}
2924
2925/*
2926 * Get a message when the user switches back to vim
2927 */
2928 static LRESULT
2929_OnActivateApp(
2930 HWND hwnd,
2931 BOOL fActivate,
2932 DWORD dwThreadId)
2933{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002934 // we call gui_focus_change() in _OnSetFocus()
2935 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002936 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002937}
2938
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 void
2940gui_mch_destroy_scrollbar(scrollbar_T *sb)
2941{
2942 DestroyWindow(sb->id);
2943}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002944
2945/*
2946 * Get current mouse coordinates in text window.
2947 */
2948 void
2949gui_mch_getmouse(int *x, int *y)
2950{
2951 RECT rct;
2952 POINT mp;
2953
2954 (void)GetWindowRect(s_textArea, &rct);
2955 (void)GetCursorPos((LPPOINT)&mp);
2956 *x = (int)(mp.x - rct.left);
2957 *y = (int)(mp.y - rct.top);
2958}
2959
2960/*
2961 * Move mouse pointer to character at (x, y).
2962 */
2963 void
2964gui_mch_setmouse(int x, int y)
2965{
2966 RECT rct;
2967
2968 (void)GetWindowRect(s_textArea, &rct);
2969 (void)SetCursorPos(x + gui.border_offset + rct.left,
2970 y + gui.border_offset + rct.top);
2971}
2972
2973 static void
2974gui_mswin_get_valid_dimensions(
2975 int w,
2976 int h,
2977 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002978 int *valid_h,
2979 int *cols,
2980 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002981{
2982 int base_width, base_height;
2983
2984 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002985 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2986 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002988 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2989 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2990 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2991 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002992 *cols = (w - base_width) / gui.char_width;
2993 *rows = (h - base_height) / gui.char_height;
2994 *valid_w = base_width + *cols * gui.char_width;
2995 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002996}
2997
2998 void
2999gui_mch_flash(int msec)
3000{
3001 RECT rc;
3002
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003003#if defined(FEAT_DIRECTX)
3004 if (IS_ENABLE_DIRECTX())
3005 DWriteContext_Flush(s_dwc);
3006#endif
3007
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003008 /*
3009 * Note: InvertRect() excludes right and bottom of rectangle.
3010 */
3011 rc.left = 0;
3012 rc.top = 0;
3013 rc.right = gui.num_cols * gui.char_width;
3014 rc.bottom = gui.num_rows * gui.char_height;
3015 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003016 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003017
Bram Moolenaar734a8672019-12-02 22:49:38 +01003018 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019
3020 InvertRect(s_hdc, &rc);
3021}
3022
3023/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003024 * Check if the specified point is on-screen. (multi-monitor aware)
3025 */
3026 static BOOL
3027is_point_onscreen(int x, int y)
3028{
3029 POINT pt = {x, y};
3030
3031 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3032}
3033
3034/*
3035 * Check if the whole area of the specified window is on-screen.
3036 *
3037 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3038 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3039 * only works when the whole window is on-screen.
3040 */
3041 static BOOL
3042is_window_onscreen(HWND hwnd)
3043{
3044 RECT rc;
3045
3046 GetWindowRect(hwnd, &rc);
3047
3048 if (!is_point_onscreen(rc.left, rc.top))
3049 return FALSE;
3050 if (!is_point_onscreen(rc.left, rc.bottom))
3051 return FALSE;
3052 if (!is_point_onscreen(rc.right, rc.top))
3053 return FALSE;
3054 if (!is_point_onscreen(rc.right, rc.bottom))
3055 return FALSE;
3056 return TRUE;
3057}
3058
3059/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 * Return flags used for scrolling.
3061 * The SW_INVALIDATE is required when part of the window is covered or
3062 * off-screen. Refer to MS KB Q75236.
3063 */
3064 static int
3065get_scroll_flags(void)
3066{
3067 HWND hwnd;
3068 RECT rcVim, rcOther, rcDest;
3069
Bram Moolenaar185577e2020-10-29 20:08:21 +01003070 // Check if the window is (partly) off-screen.
3071 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003072 return SW_INVALIDATE;
3073
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003074 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003075 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3077 if (IsWindowVisible(hwnd))
3078 {
3079 GetWindowRect(hwnd, &rcOther);
3080 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3081 return SW_INVALIDATE;
3082 }
3083 return 0;
3084}
3085
3086/*
3087 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3088 * may not be scrolled out properly.
3089 * For gVim, when _OnScroll() is repeated, the character at the
3090 * previous cursor position may be left drawn after scroll.
3091 * The problem can be avoided by calling GetPixel() to get a pixel in
3092 * the region before ScrollWindowEx().
3093 */
3094 static void
3095intel_gpu_workaround(void)
3096{
3097 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3098}
3099
3100/*
3101 * Delete the given number of lines from the given row, scrolling up any
3102 * text further down within the scroll region.
3103 */
3104 void
3105gui_mch_delete_lines(
3106 int row,
3107 int num_lines)
3108{
3109 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003110
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003111 rc.left = FILL_X(gui.scroll_region_left);
3112 rc.right = FILL_X(gui.scroll_region_right + 1);
3113 rc.top = FILL_Y(row);
3114 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3115
Bram Moolenaar92467d32017-12-05 13:22:16 +01003116#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003117 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003118 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003119 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003120 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003121 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003122#endif
3123 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003124#if defined(FEAT_DIRECTX)
3125 if (IS_ENABLE_DIRECTX())
3126 DWriteContext_Flush(s_dwc);
3127#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003128 intel_gpu_workaround();
3129 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003131 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003132 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133
Bram Moolenaar734a8672019-12-02 22:49:38 +01003134 // This seems to be required to avoid the cursor disappearing when
3135 // scrolling such that the cursor ends up in the top-left character on
3136 // the screen... But why? (Webb)
3137 // It's probably fixed by disabling drawing the cursor while scrolling.
3138 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003139
3140 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3141 gui.scroll_region_left,
3142 gui.scroll_region_bot, gui.scroll_region_right);
3143}
3144
3145/*
3146 * Insert the given number of lines before the given row, scrolling down any
3147 * following text within the scroll region.
3148 */
3149 void
3150gui_mch_insert_lines(
3151 int row,
3152 int num_lines)
3153{
3154 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003155
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156 rc.left = FILL_X(gui.scroll_region_left);
3157 rc.right = FILL_X(gui.scroll_region_right + 1);
3158 rc.top = FILL_Y(row);
3159 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160
3161#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003164 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003165 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003166 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003167#endif
3168 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003169#if defined(FEAT_DIRECTX)
3170 if (IS_ENABLE_DIRECTX())
3171 DWriteContext_Flush(s_dwc);
3172#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003173 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003174 // The SW_INVALIDATE is required when part of the window is covered or
3175 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003178 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003179 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003181 gui_clear_block(row, gui.scroll_region_left,
3182 row + num_lines - 1, gui.scroll_region_right);
3183}
3184
3185
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003187gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188{
3189#if defined(FEAT_DIRECTX)
3190 DWriteContext_Close(s_dwc);
3191 DWrite_Final();
3192 s_dwc = NULL;
3193#endif
3194
3195 ReleaseDC(s_textArea, s_hdc);
3196 DeleteObject(s_brush);
3197
3198#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003199 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3201#endif
3202
Bram Moolenaar734a8672019-12-02 22:49:38 +01003203 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 if (s_hwnd != NULL)
3205 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003206 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003207 DestroyWindow(s_hwnd);
3208 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209}
3210
3211 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003212logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213{
3214 char *p;
3215 char *res;
3216 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003217 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003218 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003219 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003221 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3222 if (font_name == NULL)
3223 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003225 quality_name = quality_id2name((int)lf.lfQuality);
3226
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003227 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003228 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003229 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003230 if (res != NULL)
3231 {
3232 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003233 // make a normal font string out of the lf thing:
3234 points = pixels_to_points(
3235 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3236 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3237 sprintf((char *)p, "%s:h%d", font_name, points);
3238 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003239 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 while (*p)
3241 {
3242 if (*p == ' ')
3243 *p = '_';
3244 ++p;
3245 }
3246 if (lf.lfItalic)
3247 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003248 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 STRCAT(p, ":b");
3250 if (lf.lfUnderline)
3251 STRCAT(p, ":u");
3252 if (lf.lfStrikeOut)
3253 STRCAT(p, ":s");
3254 if (charset_name != NULL)
3255 {
3256 STRCAT(p, ":c");
3257 STRCAT(p, charset_name);
3258 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003259 if (quality_name != NULL)
3260 {
3261 STRCAT(p, ":q");
3262 STRCAT(p, quality_name);
3263 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003264 }
3265
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003266 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 return (char_u *)res;
3268}
3269
3270
3271#ifdef FEAT_MBYTE_IME
3272/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003273 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003274 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 */
3276 static void
3277update_im_font(void)
3278{
K.Takatac81e9bf2022-01-16 14:15:49 +00003279 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003280
3281 if (p_guifontwide != NULL && *p_guifontwide != NUL
3282 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003283 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284 norm_logfont = lf_wide;
3285 else
3286 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003287
3288 lf = norm_logfont;
3289 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3290 // Work around when PerMonitorV2 is not enabled in the process level.
3291 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3292 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003293}
3294#endif
3295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296/*
3297 * Handler of gui.wide_font (p_guifontwide) changed notification.
3298 */
3299 void
3300gui_mch_wide_font_changed(void)
3301{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003302 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003304#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003305 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003306#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307
3308 gui_mch_free_font(gui.wide_ital_font);
3309 gui.wide_ital_font = NOFONT;
3310 gui_mch_free_font(gui.wide_bold_font);
3311 gui.wide_bold_font = NOFONT;
3312 gui_mch_free_font(gui.wide_boldital_font);
3313 gui.wide_boldital_font = NOFONT;
3314
3315 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003316 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317 {
3318 if (!lf.lfItalic)
3319 {
3320 lf.lfItalic = TRUE;
3321 gui.wide_ital_font = get_font_handle(&lf);
3322 lf.lfItalic = FALSE;
3323 }
3324 if (lf.lfWeight < FW_BOLD)
3325 {
3326 lf.lfWeight = FW_BOLD;
3327 gui.wide_bold_font = get_font_handle(&lf);
3328 if (!lf.lfItalic)
3329 {
3330 lf.lfItalic = TRUE;
3331 gui.wide_boldital_font = get_font_handle(&lf);
3332 }
3333 }
3334 }
3335}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336
3337/*
3338 * Initialise vim to use the font with the given name.
3339 * Return FAIL if the font could not be loaded, OK otherwise.
3340 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003342gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343{
K.Takatac81e9bf2022-01-16 14:15:49 +00003344 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345 GuiFont font = NOFONT;
3346 char_u *p;
3347
Bram Moolenaar734a8672019-12-02 22:49:38 +01003348 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003350 {
3351 lfOrig = lf;
3352 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003354 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355 if (font == NOFONT)
3356 return FAIL;
3357
3358 if (font_name == NULL)
3359 font_name = (char_u *)lf.lfFaceName;
K.Takata4ac893f2022-01-20 12:44:28 +00003360#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361 norm_logfont = lf;
3362 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003363 if (!s_in_dpichanged)
3364 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365#endif
3366 gui_mch_free_font(gui.norm_font);
3367 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003368 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 GetFontSize(font);
3370
K.Takatac81e9bf2022-01-16 14:15:49 +00003371 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372 if (p != NULL)
3373 {
3374 hl_set_font_name(p);
3375
Bram Moolenaar734a8672019-12-02 22:49:38 +01003376 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3378 {
3379 vim_free(p_guifont);
3380 p_guifont = p;
3381 }
3382 else
3383 vim_free(p);
3384 }
3385
3386 gui_mch_free_font(gui.ital_font);
3387 gui.ital_font = NOFONT;
3388 gui_mch_free_font(gui.bold_font);
3389 gui.bold_font = NOFONT;
3390 gui_mch_free_font(gui.boldital_font);
3391 gui.boldital_font = NOFONT;
3392
3393 if (!lf.lfItalic)
3394 {
3395 lf.lfItalic = TRUE;
3396 gui.ital_font = get_font_handle(&lf);
3397 lf.lfItalic = FALSE;
3398 }
3399 if (lf.lfWeight < FW_BOLD)
3400 {
3401 lf.lfWeight = FW_BOLD;
3402 gui.bold_font = get_font_handle(&lf);
3403 if (!lf.lfItalic)
3404 {
3405 lf.lfItalic = TRUE;
3406 gui.boldital_font = get_font_handle(&lf);
3407 }
3408 }
3409
3410 return OK;
3411}
3412
3413#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003414# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415#endif
3416
3417/*
3418 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003419 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 */
3421 int
3422gui_mch_maximized(void)
3423{
3424 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003425 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426
3427 wp.length = sizeof(WINDOWPLACEMENT);
3428 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003429 {
3430 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003432 && wp.flags == WPF_RESTORETOMAXIMIZED))
3433 return TRUE;
3434 if (wp.showCmd == SW_SHOWMINIMIZED)
3435 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003437 // Assume the window is snapped when the sizes from two APIs differ.
3438 GetWindowRect(s_hwnd, &rc);
3439 if ((rc.right - rc.left !=
3440 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3441 || (rc.bottom - rc.top !=
3442 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3443 return TRUE;
3444 }
3445 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446}
3447
3448/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003449 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3450 * is set. Compute the new Rows and Columns. This is like resizing the
3451 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452 */
3453 void
3454gui_mch_newfont(void)
3455{
3456 RECT rect;
3457
3458 GetWindowRect(s_hwnd, &rect);
3459 if (win_socket_id == 0)
3460 {
3461 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003462 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3463 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003465 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3466 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3467 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3468 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003469 }
3470 else
3471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003472 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003474 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 }
3476}
3477
3478/*
3479 * Set the window title
3480 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481 void
3482gui_mch_settitle(
3483 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003484 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485{
3486 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3487}
3488
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003489#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003490// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3491// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492static LPCSTR mshape_idcs[] =
3493{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003494 IDC_ARROW, // arrow
3495 MAKEINTRESOURCE(0), // blank
3496 IDC_IBEAM, // beam
3497 IDC_SIZENS, // updown
3498 IDC_SIZENS, // udsizing
3499 IDC_SIZEWE, // leftright
3500 IDC_SIZEWE, // lrsizing
3501 IDC_WAIT, // busy
3502 IDC_NO, // no
3503 IDC_ARROW, // crosshair
3504 IDC_ARROW, // hand1
3505 IDC_ARROW, // hand2
3506 IDC_ARROW, // pencil
3507 IDC_ARROW, // question
3508 IDC_ARROW, // right-arrow
3509 IDC_UPARROW, // up-arrow
3510 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511};
3512
3513 void
3514mch_set_mouse_shape(int shape)
3515{
3516 LPCSTR idc;
3517
3518 if (shape == MSHAPE_HIDE)
3519 ShowCursor(FALSE);
3520 else
3521 {
3522 if (shape >= MSHAPE_NUMBERED)
3523 idc = IDC_ARROW;
3524 else
3525 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003526 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 if (!p_mh)
3528 {
3529 POINT mp;
3530
Bram Moolenaar734a8672019-12-02 22:49:38 +01003531 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003532 (void)GetCursorPos((LPPOINT)&mp);
3533 (void)SetCursorPos(mp.x, mp.y);
3534 ShowCursor(TRUE);
3535 }
3536 }
3537}
3538#endif
3539
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003540#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542 * Wide version of convert_filter().
3543 */
3544 static WCHAR *
3545convert_filterW(char_u *s)
3546{
3547 char_u *tmp;
3548 int len;
3549 WCHAR *res;
3550
3551 tmp = convert_filter(s);
3552 if (tmp == NULL)
3553 return NULL;
3554 len = (int)STRLEN(s) + 3;
3555 res = enc_to_utf16(tmp, &len);
3556 vim_free(tmp);
3557 return res;
3558}
3559
3560/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003561 * Pop open a file browser and return the file selected, in allocated memory,
3562 * or NULL if Cancel is hit.
3563 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3564 * title - Title message for the file browser dialog.
3565 * dflt - Default name of file.
3566 * ext - Default extension to be added to files without extensions.
3567 * initdir - directory in which to open the browser (NULL = current dir)
3568 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003570 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003571gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572 int saving,
3573 char_u *title,
3574 char_u *dflt,
3575 char_u *ext,
3576 char_u *initdir,
3577 char_u *filter)
3578{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003579 // We always use the wide function. This means enc_to_utf16() must work,
3580 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581 OPENFILENAMEW fileStruct;
3582 WCHAR fileBuf[MAXPATHL];
3583 WCHAR *wp;
3584 int i;
3585 WCHAR *titlep = NULL;
3586 WCHAR *extp = NULL;
3587 WCHAR *initdirp = NULL;
3588 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003589 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003590 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591
3592 if (dflt == NULL)
3593 fileBuf[0] = NUL;
3594 else
3595 {
3596 wp = enc_to_utf16(dflt, NULL);
3597 if (wp == NULL)
3598 fileBuf[0] = NUL;
3599 else
3600 {
3601 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3602 fileBuf[i] = wp[i];
3603 fileBuf[i] = NUL;
3604 vim_free(wp);
3605 }
3606 }
3607
Bram Moolenaar734a8672019-12-02 22:49:38 +01003608 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003609 filterp = convert_filterW(filter);
3610
Bram Moolenaara80faa82020-04-12 19:37:17 +02003611 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003612# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003613 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003614 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003615# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003617# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618
3619 if (title != NULL)
3620 titlep = enc_to_utf16(title, NULL);
3621 fileStruct.lpstrTitle = titlep;
3622
3623 if (ext != NULL)
3624 extp = enc_to_utf16(ext, NULL);
3625 fileStruct.lpstrDefExt = extp;
3626
3627 fileStruct.lpstrFile = fileBuf;
3628 fileStruct.nMaxFile = MAXPATHL;
3629 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003630 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3631 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632 if (initdir != NULL && *initdir != NUL)
3633 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003634 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635 initdirp = enc_to_utf16(initdir, NULL);
3636 if (initdirp != NULL)
3637 {
3638 for (wp = initdirp; *wp != NUL; ++wp)
3639 if (*wp == '/')
3640 *wp = '\\';
3641 }
3642 fileStruct.lpstrInitialDir = initdirp;
3643 }
3644
3645 /*
3646 * TODO: Allow selection of multiple files. Needs another arg to this
3647 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3648 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3649 * files that don't exist yet, so I haven't put it in. What about
3650 * OFN_PATHMUSTEXIST?
3651 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3652 */
3653 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003654# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 if (curbuf->b_p_bin)
3656 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003657# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003659 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003661 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663 vim_free(filterp);
3664 vim_free(initdirp);
3665 vim_free(titlep);
3666 vim_free(extp);
3667
K.Takata14b8d6a2022-01-20 15:05:22 +00003668 if (!ret)
3669 return NULL;
3670
3671 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003673 if (p == NULL)
3674 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675
Bram Moolenaar734a8672019-12-02 22:49:38 +01003676 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 SetFocus(s_hwnd);
3678
Bram Moolenaar734a8672019-12-02 22:49:38 +01003679 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003680 q = vim_strsave(shorten_fname1(p));
3681 vim_free(p);
3682 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003684
3685
3686/*
3687 * Convert the string s to the proper format for a filter string by replacing
3688 * the \t and \n delimiters with \0.
3689 * Returns the converted string in allocated memory.
3690 *
3691 * Keep in sync with convert_filterW() above!
3692 */
3693 static char_u *
3694convert_filter(char_u *s)
3695{
3696 char_u *res;
3697 unsigned s_len = (unsigned)STRLEN(s);
3698 unsigned i;
3699
3700 res = alloc(s_len + 3);
3701 if (res != NULL)
3702 {
3703 for (i = 0; i < s_len; ++i)
3704 if (s[i] == '\t' || s[i] == '\n')
3705 res[i] = '\0';
3706 else
3707 res[i] = s[i];
3708 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003709 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 res[s_len + 1] = NUL;
3711 res[s_len + 2] = NUL;
3712 }
3713 return res;
3714}
3715
3716/*
3717 * Select a directory.
3718 */
3719 char_u *
3720gui_mch_browsedir(char_u *title, char_u *initdir)
3721{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // We fake this: Use a filter that doesn't select anything and a default
3723 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003724 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3725 initdir, (char_u *)_("Directory\t*.nothing\n"));
3726}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729 static void
3730_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003731 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 HDROP hDrop)
3733{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003734#define BUFPATHLEN _MAX_PATH
3735#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003736 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737 char szFile[BUFPATHLEN];
3738 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3739 UINT i;
3740 char_u **fnames;
3741 POINT pt;
3742 int_u modifiers = 0;
3743
Bram Moolenaar734a8672019-12-02 22:49:38 +01003744 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003745
Bram Moolenaar734a8672019-12-02 22:49:38 +01003746 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003747 DragQueryPoint(hDrop, &pt);
3748 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3749
3750 reset_VIsual();
3751
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003752 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003753
3754 if (fnames != NULL)
3755 for (i = 0; i < cFiles; ++i)
3756 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3758 fnames[i] = utf16_to_enc(wszFile, NULL);
3759 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 {
3761 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3762 fnames[i] = vim_strsave((char_u *)szFile);
3763 }
3764 }
3765
3766 DragFinish(hDrop);
3767
3768 if (fnames != NULL)
3769 {
3770 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3771 modifiers |= MOUSE_SHIFT;
3772 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3773 modifiers |= MOUSE_CTRL;
3774 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3775 modifiers |= MOUSE_ALT;
3776
3777 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3778
3779 s_need_activate = TRUE;
3780 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781}
3782
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 static int
3784_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003785 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 HWND hwndCtl,
3787 UINT code,
3788 int pos)
3789{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003790 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 scrollbar_T *sb, *sb_info;
3792 long val;
3793 int dragging = FALSE;
3794 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 SCROLLINFO si;
3796
3797 si.cbSize = sizeof(si);
3798 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799
3800 sb = gui_mswin_find_scrollbar(hwndCtl);
3801 if (sb == NULL)
3802 return 0;
3803
Bram Moolenaar734a8672019-12-02 22:49:38 +01003804 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805 {
3806 /*
3807 * Careful: need to get scrollbar info out of first (left) scrollbar
3808 * for window, but keep real scrollbar too because we must pass it to
3809 * gui_drag_scrollbar().
3810 */
3811 sb_info = &sb->wp->w_scrollbars[0];
3812 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003813 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 sb_info = sb;
3815 val = sb_info->value;
3816
3817 switch (code)
3818 {
3819 case SB_THUMBTRACK:
3820 val = pos;
3821 dragging = TRUE;
3822 if (sb->scroll_shift > 0)
3823 val <<= sb->scroll_shift;
3824 break;
3825 case SB_LINEDOWN:
3826 val++;
3827 break;
3828 case SB_LINEUP:
3829 val--;
3830 break;
3831 case SB_PAGEDOWN:
3832 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3833 break;
3834 case SB_PAGEUP:
3835 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3836 break;
3837 case SB_TOP:
3838 val = 0;
3839 break;
3840 case SB_BOTTOM:
3841 val = sb_info->max;
3842 break;
3843 case SB_ENDSCROLL:
3844 if (prev_code == SB_THUMBTRACK)
3845 {
3846 /*
3847 * "pos" only gives us 16-bit data. In case of large file,
3848 * use GetScrollPos() which returns 32-bit. Unfortunately it
3849 * is not valid while the scrollbar is being dragged.
3850 */
3851 val = GetScrollPos(hwndCtl, SB_CTL);
3852 if (sb->scroll_shift > 0)
3853 val <<= sb->scroll_shift;
3854 }
3855 break;
3856
3857 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003858 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003859 return 0;
3860 }
3861 prev_code = code;
3862
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3864 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003865
3866 /*
3867 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3868 */
3869 if (sb->wp != NULL)
3870 {
3871 scrollbar_T *sba = sb->wp->w_scrollbars;
3872 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3873
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003874 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875 }
3876
Bram Moolenaar734a8672019-12-02 22:49:38 +01003877 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 s_busy_processing = TRUE;
3879
Bram Moolenaar734a8672019-12-02 22:49:38 +01003880 // When "allow_scrollbar" is FALSE still need to remember the new
3881 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 dont_scroll = !allow_scrollbar;
3883
Bram Moolenaara338adc2018-01-31 20:51:47 +01003884 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003885 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003886 mch_enable_flush();
3887 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003888
3889 s_busy_processing = FALSE;
3890 dont_scroll = dont_scroll_save;
3891
3892 return 0;
3893}
3894
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896#ifdef FEAT_XPM_W32
3897# include "xpm_w32.h"
3898#endif
3899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900#ifdef __MINGW32__
3901/*
3902 * Add a lot of missing defines.
3903 * They are not always missing, we need the #ifndef's.
3904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905# ifndef IsMinimized
3906# define IsMinimized(hwnd) IsIconic(hwnd)
3907# endif
3908# ifndef IsMaximized
3909# define IsMaximized(hwnd) IsZoomed(hwnd)
3910# endif
3911# ifndef SelectFont
3912# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3913# endif
3914# ifndef GetStockBrush
3915# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3916# endif
3917# ifndef DeleteBrush
3918# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3919# endif
3920
3921# ifndef HANDLE_WM_RBUTTONDBLCLK
3922# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3923 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3924# endif
3925# ifndef HANDLE_WM_MBUTTONUP
3926# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3927 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3928# endif
3929# ifndef HANDLE_WM_MBUTTONDBLCLK
3930# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3931 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3932# endif
3933# ifndef HANDLE_WM_LBUTTONDBLCLK
3934# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3935 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3936# endif
3937# ifndef HANDLE_WM_RBUTTONDOWN
3938# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3939 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3940# endif
3941# ifndef HANDLE_WM_MOUSEMOVE
3942# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3943 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3944# endif
3945# ifndef HANDLE_WM_RBUTTONUP
3946# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3947 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3948# endif
3949# ifndef HANDLE_WM_MBUTTONDOWN
3950# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3951 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3952# endif
3953# ifndef HANDLE_WM_LBUTTONUP
3954# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3955 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3956# endif
3957# ifndef HANDLE_WM_LBUTTONDOWN
3958# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3959 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3960# endif
3961# ifndef HANDLE_WM_SYSCHAR
3962# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3963 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3964# endif
3965# ifndef HANDLE_WM_ACTIVATEAPP
3966# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3967 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3968# endif
3969# ifndef HANDLE_WM_WINDOWPOSCHANGING
3970# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3971 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3972# endif
3973# ifndef HANDLE_WM_VSCROLL
3974# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3975 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3976# endif
3977# ifndef HANDLE_WM_SETFOCUS
3978# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3979 ((fn)((hwnd), (HWND)(wParam)), 0L)
3980# endif
3981# ifndef HANDLE_WM_KILLFOCUS
3982# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3983 ((fn)((hwnd), (HWND)(wParam)), 0L)
3984# endif
3985# ifndef HANDLE_WM_HSCROLL
3986# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3987 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3988# endif
3989# ifndef HANDLE_WM_DROPFILES
3990# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3991 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3992# endif
3993# ifndef HANDLE_WM_CHAR
3994# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3995 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3996# endif
3997# ifndef HANDLE_WM_SYSDEADCHAR
3998# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3999 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4000# endif
4001# ifndef HANDLE_WM_DEADCHAR
4002# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4003 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4004# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004005#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007
Bram Moolenaar734a8672019-12-02 22:49:38 +01004008// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#define TEAROFF_PADDING_X 2
4010#define TEAROFF_BUTTON_PAD_X 8
4011#define TEAROFF_MIN_WIDTH 200
4012#define TEAROFF_SUBMENU_LABEL ">>"
4013#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4014
4015
Bram Moolenaar734a8672019-12-02 22:49:38 +01004016// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004018# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#endif
4020
4021
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004022#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023# define ID_BEVAL_TOOLTIP 200
4024# define BEVAL_TEXT_LEN MAXPATHL
4025
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004026# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004027// Work around old versions of basetsd.h which wrongly declares
4028// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004029# undef UINT_PTR
4030# define UINT_PTR UINT
4031# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004036
Bram Moolenaar82881492012-11-20 16:53:39 +01004037
Bram Moolenaar734a8672019-12-02 22:49:38 +01004038// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004039# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004040
Bram Moolenaar45360022005-07-21 21:08:21 +00004041/*
4042 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004043 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004044 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004045# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004046
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004047# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004048
4049typedef struct _DllVersionInfo
4050{
4051 DWORD cbSize;
4052 DWORD dwMajorVersion;
4053 DWORD dwMinorVersion;
4054 DWORD dwBuildNumber;
4055 DWORD dwPlatformID;
4056} DLLVERSIONINFO;
4057
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004058# ifndef PROTO
4059# include <poppack.h>
4060# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004061
Bram Moolenaar45360022005-07-21 21:08:21 +00004062typedef struct tagTOOLINFOA_NEW
4063{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004064 UINT cbSize;
4065 UINT uFlags;
4066 HWND hwnd;
4067 UINT_PTR uId;
4068 RECT rect;
4069 HINSTANCE hinst;
4070 LPSTR lpszText;
4071 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004072} TOOLINFO_NEW;
4073
4074typedef struct tagNMTTDISPINFO_NEW
4075{
4076 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004077 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004078 char szText[80];
4079 HINSTANCE hinst;
4080 UINT uFlags;
4081 LPARAM lParam;
4082} NMTTDISPINFO_NEW;
4083
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004084typedef struct tagTOOLINFOW_NEW
4085{
4086 UINT cbSize;
4087 UINT uFlags;
4088 HWND hwnd;
4089 UINT_PTR uId;
4090 RECT rect;
4091 HINSTANCE hinst;
4092 LPWSTR lpszText;
4093 LPARAM lParam;
4094 void *lpReserved;
4095} TOOLINFOW_NEW;
4096
4097typedef struct tagNMTTDISPINFOW_NEW
4098{
4099 NMHDR hdr;
4100 LPWSTR lpszText;
4101 WCHAR szText[80];
4102 HINSTANCE hinst;
4103 UINT uFlags;
4104 LPARAM lParam;
4105} NMTTDISPINFOW_NEW;
4106
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004107
Bram Moolenaar45360022005-07-21 21:08:21 +00004108typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004109# ifndef TTM_SETMAXTIPWIDTH
4110# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004113# ifndef TTF_DI_SETITEM
4114# define TTF_DI_SETITEM 0x8000
4115# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004116
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004117# ifndef TTN_GETDISPINFO
4118# define TTN_GETDISPINFO (TTN_FIRST - 0)
4119# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004120
Bram Moolenaar734a8672019-12-02 22:49:38 +01004121#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004122
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004123#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004124// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4125// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004126// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004127// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004128# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4129typedef struct tagNMTTDISPINFOA {
4130 NMHDR hdr;
4131 LPSTR lpszText;
4132 char szText[80];
4133 HINSTANCE hinst;
4134 UINT uFlags;
4135 LPARAM lParam;
4136} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4137# define LPNMTTDISPINFO LPNMTTDISPINFOA
4138
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004139typedef struct tagNMTTDISPINFOW {
4140 NMHDR hdr;
4141 LPWSTR lpszText;
4142 WCHAR szText[80];
4143 HINSTANCE hinst;
4144 UINT uFlags;
4145 LPARAM lParam;
4146} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004147# endif
4148#endif
4149
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004150#ifndef TTN_GETDISPINFOW
4151# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4152#endif
4153
Bram Moolenaar734a8672019-12-02 22:49:38 +01004154// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156#ifdef FEAT_MENU
4157static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160/*
4161 * Use the system font for dialogs and tear-off menus. Remove this line to
4162 * use DLG_FONT_NAME.
4163 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004164#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#define VIM_CLASSW L"Vim"
4168
Bram Moolenaar734a8672019-12-02 22:49:38 +01004169// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4170// thus there should be room for every dialog. For tearoffs it's made bigger
4171// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#define DLG_ALLOC_SIZE 16 * 1024
4173
4174/*
4175 * stuff for dialogs, menus, tearoffs etc.
4176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177static PWORD
4178add_dialog_element(
4179 PWORD p,
4180 DWORD lStyle,
4181 WORD x,
4182 WORD y,
4183 WORD w,
4184 WORD h,
4185 WORD Id,
4186 WORD clss,
4187 const char *caption);
4188static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004189static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004190#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193static void get_dialog_font_metrics(void);
4194
4195static int dialog_default_button = -1;
4196
Bram Moolenaar734a8672019-12-02 22:49:38 +01004197// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaar734a8672019-12-02 22:49:38 +01004200static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#ifdef FEAT_TOOLBAR
4202static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004203static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004204static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004206#else
4207# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208#endif
4209
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004210#ifdef FEAT_GUI_TABLINE
4211static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004212static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004213#endif
4214
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#ifdef FEAT_MBYTE_IME
4216static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4217static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4218#endif
4219#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4220# ifdef NOIME
4221typedef struct tagCOMPOSITIONFORM {
4222 DWORD dwStyle;
4223 POINT ptCurrentPos;
4224 RECT rcArea;
4225} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4226typedef HANDLE HIMC;
4227# endif
4228
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004229static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004230static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4231static HIMC (WINAPI *pImmGetContext)(HWND);
4232static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4233static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4234static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4235static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004236static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4237static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004238static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4239static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004240static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241static void dyn_imm_load(void);
4242#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243# define pImmGetCompositionStringW ImmGetCompositionStringW
4244# define pImmGetContext ImmGetContext
4245# define pImmAssociateContext ImmAssociateContext
4246# define pImmReleaseContext ImmReleaseContext
4247# define pImmGetOpenStatus ImmGetOpenStatus
4248# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004249# define pImmGetCompositionFontW ImmGetCompositionFontW
4250# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251# define pImmSetCompositionWindow ImmSetCompositionWindow
4252# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004253# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#endif
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#ifdef FEAT_MENU
4257/*
4258 * Figure out how high the menu bar is at the moment.
4259 */
4260 static int
4261gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004262 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 static int old_menu_height = -1;
4265
4266 RECT rc1, rc2;
4267 int num;
4268 int menu_height;
4269
4270 if (gui.menu_is_active)
4271 num = GetMenuItemCount(s_menuBar);
4272 else
4273 num = 0;
4274
4275 if (num == 0)
4276 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004277 else if (IsMinimized(s_hwnd))
4278 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004279 // The height of the menu cannot be determined while the window is
4280 // minimized. Take the previous height if the menu is changed in that
4281 // state, to avoid that Vim's vertical window size accidentally
4282 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004283 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 else
4286 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004287 /*
4288 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4289 * seem to have been set yet, so menu wraps in default window
4290 * width which is very narrow. Instead just return height of a
4291 * single menu item. Will still be wrong when the menu really
4292 * should wrap over more than one line.
4293 */
4294 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4295 if (gui.starting)
4296 menu_height = rc1.bottom - rc1.top + 1;
4297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004299 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4300 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 }
4303
4304 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004305 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004306 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 return menu_height;
4309}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004310#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312
4313/*
4314 * Setup for the Intellimouse
4315 */
4316 static void
4317init_mouse_wheel(void)
4318{
4319
4320#ifndef SPI_GETWHEELSCROLLLINES
4321# define SPI_GETWHEELSCROLLLINES 104
4322#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004323#ifndef SPI_SETWHEELSCROLLLINES
4324# define SPI_SETWHEELSCROLLLINES 105
4325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaar734a8672019-12-02 22:49:38 +01004327#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4328#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4330#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4331
Bram Moolenaar734a8672019-12-02 22:49:38 +01004332 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaar734a8672019-12-02 22:49:38 +01004334 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004335 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4336 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337}
4338
4339
Bram Moolenaarae20f342019-11-05 21:09:23 +01004340/*
4341 * Intellimouse wheel handler.
4342 * Treat a mouse wheel event as if it were a scroll request.
4343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 static void
4345_OnMouseWheel(
4346 HWND hwnd,
4347 short zDelta)
4348{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int i;
4350 int size;
4351 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004352 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (mouse_scroll_lines == 0)
4355 init_mouse_wheel();
4356
Bram Moolenaarae20f342019-11-05 21:09:23 +01004357 wp = gui_mouse_window(FIND_POPUP);
4358
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004359#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004360 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004361 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004362 cmdarg_T cap;
4363 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004364
Bram Moolenaarae20f342019-11-05 21:09:23 +01004365 // Mouse hovers over popup window, scroll it if possible.
4366 mouse_row = wp->w_winrow;
4367 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004368 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004369 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4370 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4371 clear_oparg(&oa);
4372 cap.oap = &oa;
4373 nv_mousescroll(&cap);
4374 update_screen(0);
4375 setcursor();
4376 out_flush();
4377 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004378 }
4379#endif
4380
Bram Moolenaarae20f342019-11-05 21:09:23 +01004381 if (wp == NULL || !p_scf)
4382 wp = curwin;
4383
4384 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4385 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4386 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4387 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4388 else
4389 return;
4390 size = wp->w_height;
4391
Bram Moolenaara338adc2018-01-31 20:51:47 +01004392 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (mouse_scroll_lines > 0
4394 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4395 {
4396 for (i = mouse_scroll_lines; i > 0; --i)
4397 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4398 }
4399 else
4400 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004401 mch_enable_flush();
4402 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403}
4404
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004405#ifdef USE_SYSMENU_FONT
4406/*
4407 * Get Menu Font.
4408 * Return OK or FAIL.
4409 */
4410 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004411gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004412{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004413 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004414
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004415 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4416 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004417 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004418 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004419 &nm,
4420 0))
4421 return FAIL;
4422 *lf = nm.lfMenuFont;
4423 return OK;
4424}
4425#endif
4426
4427
4428#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4429/*
4430 * Set the GUI tabline font to the system menu font
4431 */
4432 static void
4433set_tabline_font(void)
4434{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004435 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004436 HFONT font;
4437 HWND hwnd;
4438 HDC hdc;
4439 HFONT hfntOld;
4440 TEXTMETRIC tm;
4441
4442 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4443 return;
4444
K.Takatac81e9bf2022-01-16 14:15:49 +00004445 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004446 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004447
4448 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4449
4450 /*
4451 * Compute the height of the font used for the tab text
4452 */
4453 hwnd = GetDesktopWindow();
4454 hdc = GetWindowDC(hwnd);
4455 hfntOld = SelectFont(hdc, font);
4456
4457 GetTextMetrics(hdc, &tm);
4458
4459 SelectFont(hdc, hfntOld);
4460 ReleaseDC(hwnd, hdc);
4461
4462 /*
4463 * The space used by the tab border and the space between the tab label
4464 * and the tab border is included as 7.
4465 */
4466 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4467}
K.Takatac81e9bf2022-01-16 14:15:49 +00004468#else
4469# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004470#endif
4471
Bram Moolenaar520470a2005-06-16 21:59:56 +00004472/*
4473 * Invoked when a setting was changed.
4474 */
4475 static LRESULT CALLBACK
4476_OnSettingChange(UINT n)
4477{
4478 if (n == SPI_SETWHEELSCROLLLINES)
4479 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4480 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004481 if (n == SPI_SETNONCLIENTMETRICS)
4482 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004483 return 0;
4484}
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#ifdef FEAT_NETBEANS_INTG
4487 static void
4488_OnWindowPosChanged(
4489 HWND hwnd,
4490 const LPWINDOWPOS lpwpos)
4491{
4492 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004493 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
4495 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4496 || lpwpos->cx != cx || lpwpos->cy != cy))
4497 {
4498 x = lpwpos->x;
4499 y = lpwpos->y;
4500 cx = lpwpos->cx;
4501 cy = lpwpos->cy;
4502 netbeans_frame_moved(x, y);
4503 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004504 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004505 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506}
4507#endif
4508
K.Takata4f2417f2021-06-05 16:25:32 +02004509
4510static HWND hwndTip = NULL;
4511
4512 static void
4513show_sizing_tip(int cols, int rows)
4514{
4515 TOOLINFOA ti = {sizeof(ti)};
4516 char buf[32];
4517
4518 ti.hwnd = s_hwnd;
4519 ti.uId = (UINT_PTR)s_hwnd;
4520 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4521 ti.lpszText = buf;
4522 sprintf(buf, "%dx%d", cols, rows);
4523 if (hwndTip == NULL)
4524 {
4525 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4526 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4527 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4528 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4529 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4530 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4531 }
4532 else
4533 {
4534 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4535 }
4536 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4537}
4538
4539 static void
4540destroy_sizing_tip(void)
4541{
4542 if (hwndTip != NULL)
4543 {
4544 DestroyWindow(hwndTip);
4545 hwndTip = NULL;
4546 }
4547}
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 static int
4550_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 UINT fwSide,
4552 LPRECT lprc)
4553{
4554 int w, h;
4555 int valid_w, valid_h;
4556 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004557 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 w = lprc->right - lprc->left;
4560 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004561 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 w_offset = w - valid_w;
4563 h_offset = h - valid_h;
4564
4565 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4566 || fwSide == WMSZ_BOTTOMLEFT)
4567 lprc->left += w_offset;
4568 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4569 || fwSide == WMSZ_BOTTOMRIGHT)
4570 lprc->right -= w_offset;
4571
4572 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4573 || fwSide == WMSZ_TOPRIGHT)
4574 lprc->top += h_offset;
4575 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4576 || fwSide == WMSZ_BOTTOMRIGHT)
4577 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004578
4579 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return TRUE;
4581}
4582
K.Takatac81e9bf2022-01-16 14:15:49 +00004583 static LRESULT
4584_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4585{
4586 s_dpi = ydpi;
4587 s_in_dpichanged = TRUE;
4588 //TRACE("DPI: %d", ydpi);
4589
4590 update_scrollbar_size();
4591 update_toolbar_size();
4592 set_tabline_font();
4593
4594 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4595 gui_get_wide_font();
4596 gui_mswin_get_menu_height(FALSE);
4597#ifdef FEAT_MBYTE_IME
4598 im_set_position(gui.row, gui.col);
4599#endif
4600 InvalidateRect(hwnd, NULL, TRUE);
4601
4602 s_in_dpichanged = FALSE;
4603 return 0L;
4604}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606
4607 static LRESULT CALLBACK
4608_WndProc(
4609 HWND hwnd,
4610 UINT uMsg,
4611 WPARAM wParam,
4612 LPARAM lParam)
4613{
4614 /*
4615 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4616 hwnd, uMsg, wParam, lParam);
4617 */
4618
4619 HandleMouseHide(uMsg, lParam);
4620
4621 s_uMsg = uMsg;
4622 s_wParam = wParam;
4623 s_lParam = lParam;
4624
4625 switch (uMsg)
4626 {
4627 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4628 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004629 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004631 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4633 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4634 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4635 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4636#ifdef FEAT_MENU
4637 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4638#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004639 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4640 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4642 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004643 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4644 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4646 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4647 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4648#ifdef FEAT_NETBEANS_INTG
4649 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4650#endif
4651
Bram Moolenaarafa24992006-03-27 20:58:26 +00004652#ifdef FEAT_GUI_TABLINE
4653 case WM_RBUTTONUP:
4654 {
4655 if (gui_mch_showing_tabline())
4656 {
4657 POINT pt;
4658 RECT rect;
4659
4660 /*
4661 * If the cursor is on the tabline, display the tab menu
4662 */
4663 GetCursorPos((LPPOINT)&pt);
4664 GetWindowRect(s_textArea, &rect);
4665 if (pt.y < rect.top)
4666 {
4667 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004668 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004669 }
4670 }
K.Takata4ac893f2022-01-20 12:44:28 +00004671 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004672 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004673 case WM_LBUTTONDBLCLK:
4674 {
4675 /*
4676 * If the user double clicked the tabline, create a new tab
4677 */
4678 if (gui_mch_showing_tabline())
4679 {
4680 POINT pt;
4681 RECT rect;
4682
4683 GetCursorPos((LPPOINT)&pt);
4684 GetWindowRect(s_textArea, &rect);
4685 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004686 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004687 }
K.Takata4ac893f2022-01-20 12:44:28 +00004688 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004689 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004690#endif
4691
Bram Moolenaar734a8672019-12-02 22:49:38 +01004692 case WM_QUERYENDSESSION: // System wants to go down.
4693 gui_shell_closed(); // Will exit when no changed buffers.
4694 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004697 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004700 return 0L;
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 break;
4703
4704 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004705 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4706 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004707 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return 0L;
4709
4710 case WM_SYSCHAR:
4711 /*
4712 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4713 * shortcut key, handle like a typed ALT key, otherwise call Windows
4714 * ALT key handling.
4715 */
4716#ifdef FEAT_MENU
4717 if ( !gui.menu_is_active
4718 || p_wak[0] == 'n'
4719 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4720 )
4721#endif
4722 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004723 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return 0L;
4725 }
4726#ifdef FEAT_MENU
4727 else
K.Takata4ac893f2022-01-20 12:44:28 +00004728 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729#endif
4730
4731 case WM_SYSKEYUP:
4732#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004733 // This used to be done only when menu is active: ALT key is used for
4734 // that. But that caused problems when menu is disabled and using
4735 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4736 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004737 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004739 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#endif
4741
K.Takata4f2417f2021-06-05 16:25:32 +02004742 case WM_EXITSIZEMOVE:
4743 destroy_sizing_tip();
4744 break;
4745
Bram Moolenaar734a8672019-12-02 22:49:38 +01004746 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 case WM_MOUSEWHEEL:
4750 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004751 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaar734a8672019-12-02 22:49:38 +01004753 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004754 case WM_SETTINGCHANGE:
4755 return _OnSettingChange((UINT)wParam);
4756
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004757#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 case WM_NOTIFY:
4759 switch (((LPNMHDR) lParam)->code)
4760 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004761 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004762 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004764 LPNMHDR hdr = (LPNMHDR)lParam;
4765 char_u *str = NULL;
4766 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004767
Bram Moolenaard23a8232018-02-10 18:45:26 +01004768 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004769
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004770# ifdef FEAT_GUI_TABLINE
4771 if (gui_mch_showing_tabline()
4772 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004773 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004774 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004775 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004776 * Mouse is over the GUI tabline. Display the
4777 * tooltip for the tab under the cursor
4778 *
4779 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004780 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004781 GetCursorPos(&pt);
4782 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004783 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004784 TCHITTESTINFO htinfo;
4785 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004786
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004787 /*
4788 * Get the tab under the cursor
4789 */
4790 htinfo.pt.x = pt.x;
4791 htinfo.pt.y = pt.y;
4792 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4793 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004794 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004795 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004796
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004797 tp = find_tabpage(idx + 1);
4798 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004799 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004800 get_tabline_label(tp, TRUE);
4801 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004802 }
4803 }
4804 }
4805 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004806# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004807# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004808# ifdef FEAT_GUI_TABLINE
4809 else
4810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004812 UINT idButton;
4813 vimmenu_T *pMenu;
4814
4815 idButton = (UINT) hdr->idFrom;
4816 pMenu = gui_mswin_find_menu(root_menu, idButton);
4817 if (pMenu)
4818 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004820# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004821 if (str != NULL)
4822 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004823 if (hdr->code == TTN_GETDISPINFOW)
4824 {
4825 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4826
Bram Moolenaar734a8672019-12-02 22:49:38 +01004827 // Set the maximum width, this also enables using
4828 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004829 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4830 0, 500);
4831
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004832 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004833 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004834 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004835 }
4836 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004837 {
4838 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4839
Bram Moolenaar734a8672019-12-02 22:49:38 +01004840 // Set the maximum width, this also enables using
4841 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004842 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4843 0, 500);
4844
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004845 if (STRLEN(str) < sizeof(lpdi->szText)
4846 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004847 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004848 sizeof(lpdi->szText) - 1);
4849 else
4850 lpdi->lpszText = tt_text;
4851 }
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004855# ifdef FEAT_GUI_TABLINE
4856 case TCN_SELCHANGE:
4857 if (gui_mch_showing_tabline()
4858 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004859 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004860 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004861 return 0L;
4862 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004863 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004864
4865 case NM_RCLICK:
4866 if (gui_mch_showing_tabline()
4867 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004868 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004869 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004870 return 0L;
4871 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004872 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004875# ifdef FEAT_GUI_TABLINE
4876 if (gui_mch_showing_tabline()
4877 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
K.Takata4ac893f2022-01-20 12:44:28 +00004878 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 break;
4881 }
4882 break;
4883#endif
4884#if defined(MENUHINTS) && defined(FEAT_MENU)
4885 case WM_MENUSELECT:
4886 if (((UINT) HIWORD(wParam)
4887 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4888 == MF_HILITE
4889 && (State & CMDLINE) == 0)
4890 {
4891 UINT idButton;
4892 vimmenu_T *pMenu;
4893 static int did_menu_tip = FALSE;
4894
4895 if (did_menu_tip)
4896 {
4897 msg_clr_cmdline();
4898 setcursor();
4899 out_flush();
4900 did_menu_tip = FALSE;
4901 }
4902
4903 idButton = (UINT)LOWORD(wParam);
4904 pMenu = gui_mswin_find_menu(root_menu, idButton);
4905 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4906 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4907 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004908 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004909 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004910 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 setcursor();
4912 out_flush();
4913 did_menu_tip = TRUE;
4914 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004915 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 break;
4918#endif
4919 case WM_NCHITTEST:
4920 {
4921 LRESULT result;
4922 int x, y;
4923 int xPos = GET_X_LPARAM(lParam);
4924
K.Takata4ac893f2022-01-20 12:44:28 +00004925 result = DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (result == HTCLIENT)
4927 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004928#ifdef FEAT_GUI_TABLINE
4929 if (gui_mch_showing_tabline())
4930 {
4931 int yPos = GET_Y_LPARAM(lParam);
4932 RECT rct;
4933
Bram Moolenaar734a8672019-12-02 22:49:38 +01004934 // If the cursor is on the GUI tabline, don't process this
4935 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004936 GetWindowRect(s_textArea, &rct);
4937 if (yPos < rct.top)
4938 return result;
4939 }
4940#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004941 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 xPos -= x;
4943
Bram Moolenaar734a8672019-12-02 22:49:38 +01004944 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 return HTBOTTOMLEFT;
4946 else
4947 return HTBOTTOMRIGHT;
4948 }
4949 else
4950 return result;
4951 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004952 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954#ifdef FEAT_MBYTE_IME
4955 case WM_IME_NOTIFY:
4956 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004957 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004958 return 1L;
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 case WM_IME_COMPOSITION:
4961 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004962 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004963 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004965 case WM_DPICHANGED:
4966 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4967 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004971 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973#endif
K.Takata4ac893f2022-01-20 12:44:28 +00004974 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976
K.Takata4ac893f2022-01-20 12:44:28 +00004977 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978}
4979
4980/*
4981 * End of call-back routines
4982 */
4983
Bram Moolenaar734a8672019-12-02 22:49:38 +01004984// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985HWND vim_parent_hwnd = NULL;
4986
4987 static BOOL CALLBACK
4988FindWindowTitle(HWND hwnd, LPARAM lParam)
4989{
4990 char buf[2048];
4991 char *title = (char *)lParam;
4992
4993 if (GetWindowText(hwnd, buf, sizeof(buf)))
4994 {
4995 if (strstr(buf, title) != NULL)
4996 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004997 // Found it. Store the window ref. and quit searching if MDI
4998 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005000 if (vim_parent_hwnd != NULL)
5001 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005004 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005}
5006
5007/*
5008 * Invoked for '-P "title"' argument: search for parent application to open
5009 * our window in.
5010 */
5011 void
5012gui_mch_set_parent(char *title)
5013{
5014 EnumWindows(FindWindowTitle, (LPARAM)title);
5015 if (vim_parent_hwnd == NULL)
5016 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005017 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 mch_exit(2);
5019 }
5020}
5021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005022#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 static void
5024ole_error(char *arg)
5025{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005026 char buf[IOSIZE];
5027
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005028# ifdef VIMDLL
5029 gui.in_use = mch_is_gui_executable();
5030# endif
5031
Bram Moolenaar734a8672019-12-02 22:49:38 +01005032 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005033 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005034 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005035 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005039#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5040 static char *
5041gvim_error(void)
5042{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005043 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005044
5045 if (starting)
5046 {
5047 mch_errmsg(msg);
5048 mch_errmsg("\n");
5049 mch_exit(2);
5050 }
5051 return msg;
5052}
5053
5054 char *
5055gui_mch_do_spawn(char_u *arg)
5056{
5057 int len;
5058# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5059 char_u *session = NULL;
5060 LPWSTR tofree1 = NULL;
5061# endif
5062 WCHAR name[MAX_PATH];
5063 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5064 STARTUPINFOW si = {sizeof(si)};
5065 PROCESS_INFORMATION pi;
5066
5067 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5068 goto error;
5069 p = wcsrchr(name, L'\\');
5070 if (p == NULL)
5071 goto error;
5072 // Replace the executable name from vim(d).exe to gvim(d).exe.
5073# ifdef DEBUG
5074 wcscpy(p + 1, L"gvimd.exe");
5075# else
5076 wcscpy(p + 1, L"gvim.exe");
5077# endif
5078
5079# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5080 if (starting)
5081# endif
5082 {
5083 // Pass the command line to the new process.
5084 p = GetCommandLineW();
5085 // Skip 1st argument.
5086 while (*p && *p != L' ' && *p != L'\t')
5087 {
5088 if (*p == L'"')
5089 {
5090 while (*p && *p != L'"')
5091 ++p;
5092 if (*p)
5093 ++p;
5094 }
5095 else
5096 ++p;
5097 }
5098 cmd = p;
5099 }
5100# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5101 else
5102 {
5103 // Create a session file and pass it to the new process.
5104 LPWSTR wsession;
5105 char_u *savebg;
5106 int ret;
5107
5108 session = vim_tempname('s', FALSE);
5109 if (session == NULL)
5110 goto error;
5111 savebg = p_bg;
5112 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5113 ret = write_session_file(session);
5114 vim_free(p_bg);
5115 p_bg = savebg;
5116 if (!ret)
5117 goto error;
5118 wsession = enc_to_utf16(session, NULL);
5119 if (wsession == NULL)
5120 goto error;
5121 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005122 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005123 if (cmd == NULL)
5124 {
5125 vim_free(wsession);
5126 goto error;
5127 }
5128 tofree1 = cmd;
5129 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5130 wsession, wsession);
5131 vim_free(wsession);
5132 }
5133# endif
5134
5135 // Check additional arguments to the `:gui` command.
5136 if (arg != NULL)
5137 {
5138 warg = enc_to_utf16(arg, NULL);
5139 if (warg == NULL)
5140 goto error;
5141 tofree2 = warg;
5142 }
5143 else
5144 warg = L"";
5145
5146 // Set up the new command line.
5147 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005148 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005149 if (newcmd == NULL)
5150 goto error;
5151 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5152
5153 // Spawn a new GUI process.
5154 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5155 NULL, NULL, &si, &pi))
5156 goto error;
5157 CloseHandle(pi.hProcess);
5158 CloseHandle(pi.hThread);
5159 mch_exit(0);
5160
5161error:
5162# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5163 if (session)
5164 mch_remove(session);
5165 vim_free(session);
5166 vim_free(tofree1);
5167# endif
5168 vim_free(newcmd);
5169 vim_free(tofree2);
5170 return gvim_error();
5171}
5172#endif
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Parse the GUI related command-line arguments. Any arguments used are
5176 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005177 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
5179 void
5180gui_mch_prepare(int *argc, char **argv)
5181{
5182 int silent = FALSE;
5183 int idx;
5184
Bram Moolenaar734a8672019-12-02 22:49:38 +01005185 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5187 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005188 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5190 && (argv[2][0] == '-' || argv[2][0] == '/'))
5191 {
5192 silent = TRUE;
5193 idx = 2;
5194 }
5195 else
5196 idx = 1;
5197
Bram Moolenaar734a8672019-12-02 22:49:38 +01005198 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (STRICMP(argv[idx] + 1, "register") == 0)
5200 {
5201#ifdef FEAT_OLE
5202 RegisterMe(silent);
5203 mch_exit(0);
5204#else
5205 if (!silent)
5206 ole_error("register");
5207 mch_exit(2);
5208#endif
5209 }
5210
Bram Moolenaar734a8672019-12-02 22:49:38 +01005211 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5213 {
5214#ifdef FEAT_OLE
5215 UnregisterMe(!silent);
5216 mch_exit(0);
5217#else
5218 if (!silent)
5219 ole_error("unregister");
5220 mch_exit(2);
5221#endif
5222 }
5223
Bram Moolenaar734a8672019-12-02 22:49:38 +01005224 // Ignore an -embedding argument. It is only relevant if the
5225 // application wants to treat the case when it is started manually
5226 // differently from the case where it is started via automation (and
5227 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5229 {
5230#ifdef FEAT_OLE
5231 *argc = 1;
5232#else
5233 ole_error("embedding");
5234 mch_exit(2);
5235#endif
5236 }
5237 }
5238
5239#ifdef FEAT_OLE
5240 {
5241 int bDoRestart = FALSE;
5242
5243 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005244 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (bDoRestart)
5246 mch_exit(0);
5247 }
5248#endif
5249
5250#ifdef FEAT_NETBEANS_INTG
5251 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005252 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 int arg;
5254
5255 for (arg = 1; arg < *argc; arg++)
5256 if (strncmp("-nb", argv[arg], 3) == 0)
5257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 netbeansArg = argv[arg];
5259 mch_memmove(&argv[arg], &argv[arg + 1],
5260 (--*argc - arg) * sizeof(char *));
5261 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005262 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266}
5267
K.Takatac81e9bf2022-01-16 14:15:49 +00005268 static void
5269load_dpi_func(void)
5270{
5271 HMODULE hUser32;
5272
5273 hUser32 = GetModuleHandle("user32.dll");
5274 if (hUser32 == NULL)
5275 goto fail;
5276
5277 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5278 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5279 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5280 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5281 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5282 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5283
5284 if (pSetThreadDpiAwarenessContext != NULL)
5285 {
5286 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5287 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5288 if (oldctx != NULL)
5289 {
5290 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5291 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5292#ifdef DEBUG
5293 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5294 {
5295 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5296 }
5297#endif
5298 return;
5299 }
5300 }
5301
5302fail:
5303 // Disable PerMonitorV2 APIs.
5304 pGetDpiForSystem = stubGetDpiForSystem;
5305 pGetDpiForWindow = NULL;
5306 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5307 pSetThreadDpiAwarenessContext = NULL;
5308 pGetAwarenessFromDpiAwarenessContext = NULL;
5309}
5310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311/*
5312 * Initialise the GUI. Create all the windows, set up all the call-backs
5313 * etc.
5314 */
5315 int
5316gui_mch_init(void)
5317{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005319 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaar734a8672019-12-02 22:49:38 +01005322 // Return here if the window was already opened (happens when
5323 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005325 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 /*
5328 * Load the tearoff bitmap
5329 */
5330#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005331 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#endif
5333
K.Takatac81e9bf2022-01-16 14:15:49 +00005334 load_dpi_func();
5335
5336 s_dpi = pGetDpiForSystem();
5337 update_scrollbar_size();
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005340 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#endif
5342 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005343#ifdef FEAT_TOOLBAR
5344 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5348
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 // First try using the wide version, so that we can use any title.
5350 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005351 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005353 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 wndclassw.lpfnWndProc = _WndProc;
5355 wndclassw.cbClsExtra = 0;
5356 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005357 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5359 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5360 wndclassw.hbrBackground = s_brush;
5361 wndclassw.lpszMenuName = NULL;
5362 wndclassw.lpszClassName = szVimWndClassW;
5363
K.Takata4ac893f2022-01-20 12:44:28 +00005364 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005365 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (vim_parent_hwnd != NULL)
5369 {
5370#ifdef HAVE_TRY_EXCEPT
5371 __try
5372 {
5373#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005374 // Open inside the specified parent window.
5375 // TODO: last argument should point to a CLIENTCREATESTRUCT
5376 // structure.
5377 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005379 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005380 WS_OVERLAPPEDWINDOW | WS_CHILD
5381 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5383 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005384 100, // Any value will do
5385 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005387 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388#ifdef HAVE_TRY_EXCEPT
5389 }
5390 __except(EXCEPTION_EXECUTE_HANDLER)
5391 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005392 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394#endif
5395 if (s_hwnd == NULL)
5396 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005397 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 mch_exit(2);
5399 }
5400 }
5401 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005402 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005403 // If the provided windowid is not valid reset it to zero, so that it
5404 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005405 if (IsWindow((HWND)win_socket_id) <= 0)
5406 win_socket_id = 0;
5407
Bram Moolenaar734a8672019-12-02 22:49:38 +01005408 // Create a window. If win_socket_id is not zero without border and
5409 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005410 s_hwnd = CreateWindowW(
5411 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005412 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5413 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005414 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5415 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005416 100, // Any value will do
5417 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005418 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005419 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005420 if (s_hwnd != NULL && win_socket_id != 0)
5421 {
5422 SetParent(s_hwnd, (HWND)win_socket_id);
5423 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5424 }
5425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 if (s_hwnd == NULL)
5428 return FAIL;
5429
K.Takatac81e9bf2022-01-16 14:15:49 +00005430 if (pGetDpiForWindow != NULL)
5431 {
5432 s_dpi = pGetDpiForWindow(s_hwnd);
5433 update_scrollbar_size();
5434 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5435 }
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5438 dyn_imm_load();
5439#endif
5440
Bram Moolenaar734a8672019-12-02 22:49:38 +01005441 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005442 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005443 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005444 wndclassw.style = CS_OWNDC;
5445 wndclassw.lpfnWndProc = _TextAreaWndProc;
5446 wndclassw.cbClsExtra = 0;
5447 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005448 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005449 wndclassw.hIcon = NULL;
5450 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5451 wndclassw.hbrBackground = NULL;
5452 wndclassw.lpszMenuName = NULL;
5453 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005454
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005455 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return FAIL;
5457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005459 s_textArea = CreateWindowExW(
5460 0,
5461 szTextAreaClassW, L"Vim text area",
5462 WS_CHILD | WS_VISIBLE, 0, 0,
5463 100, // Any value will do for now
5464 100, // Any value will do for now
5465 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005466 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 if (s_textArea == NULL)
5469 return FAIL;
5470
Bram Moolenaar20321902016-02-17 12:30:17 +01005471#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005472 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005473 {
5474 HANDLE hIcon = NULL;
5475
5476 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005477 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005478 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005479#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481#ifdef FEAT_MENU
5482 s_menuBar = CreateMenu();
5483#endif
5484 s_hdc = GetDC(s_textArea);
5485
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar734a8672019-12-02 22:49:38 +01005488 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005489 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar734a8672019-12-02 22:49:38 +01005491 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 gui_mch_def_colors();
5493
Bram Moolenaar734a8672019-12-02 22:49:38 +01005494 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5495 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 set_normal_colors();
5497
5498 /*
5499 * Check that none of the colors are the same as the background color.
5500 * Then store the current values as the defaults.
5501 */
5502 gui_check_colors();
5503 gui.def_norm_pixel = gui.norm_pixel;
5504 gui.def_back_pixel = gui.back_pixel;
5505
Bram Moolenaar734a8672019-12-02 22:49:38 +01005506 // Get the colors for the highlight groups (gui_check_colors() might have
5507 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 highlight_gui_started();
5509
5510 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005511 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005513 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 /*
5516 * Set up for Intellimouse processing
5517 */
5518 init_mouse_wheel();
5519
5520 /*
5521 * compute a couple of metrics used for the dialogs
5522 */
5523 get_dialog_font_metrics();
5524#ifdef FEAT_TOOLBAR
5525 /*
5526 * Create the toolbar
5527 */
5528 initialise_toolbar();
5529#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005530#ifdef FEAT_GUI_TABLINE
5531 /*
5532 * Create the tabline
5533 */
5534 initialise_tabline();
5535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#ifdef MSWIN_FIND_REPLACE
5537 /*
5538 * Initialise the dialog box stuff
5539 */
5540 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5541
Bram Moolenaar734a8672019-12-02 22:49:38 +01005542 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005544 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005546 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5548 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5549 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005552#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005553# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005554// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005555# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005556# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005557# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005558# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005559 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005560 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005561#endif
5562
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005563#ifdef FEAT_RENDER_OPTIONS
5564 if (p_rop)
5565 (void)gui_mch_set_rendering_options(p_rop);
5566#endif
5567
Bram Moolenaar748bf032005-02-02 23:04:36 +00005568theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005569 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005570 display_errors();
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 return OK;
5573}
5574
5575/*
5576 * Get the size of the screen, taking position on multiple monitors into
5577 * account (if supported).
5578 */
5579 static void
5580get_work_area(RECT *spi_rect)
5581{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005582 HMONITOR mon;
5583 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaar734a8672019-12-02 22:49:38 +01005585 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005586 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005587 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005589 moninfo.cbSize = sizeof(MONITORINFO);
5590 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005592 *spi_rect = moninfo.rcWork;
5593 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005596 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5598}
5599
5600/*
5601 * Set the size of the window to the given width and height in pixels.
5602 */
5603 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005604gui_mch_set_shellsize(
5605 int width,
5606 int height,
5607 int min_width UNUSED,
5608 int min_height UNUSED,
5609 int base_width UNUSED,
5610 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005611 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005614 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
Bram Moolenaar734a8672019-12-02 22:49:38 +01005617 // Try to keep window completely on screen.
5618 // Get position of the screen work area. This is the part that is not
5619 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 get_work_area(&workarea_rect);
5621
Bram Moolenaar734a8672019-12-02 22:49:38 +01005622 // Resizing a maximized window looks very strange, unzoom it first.
5623 // But don't do it when still starting up, it may have been requested in
5624 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005625 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005627
5628 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
Bram Moolenaar734a8672019-12-02 22:49:38 +01005630 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005631 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5632 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5633 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5634 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5635 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5636 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar734a8672019-12-02 22:49:38 +01005638 // The following should take care of keeping Vim on the same monitor, no
5639 // matter if the secondary monitor is left or right of the primary
5640 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005641 window_rect.right = window_rect.left + win_width;
5642 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005643
Bram Moolenaar734a8672019-12-02 22:49:38 +01005644 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005645 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5646 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005648 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5649 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005651 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5652 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005654 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5655 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005657 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5658 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 SetActiveWindow(s_hwnd);
5661 SetFocus(s_hwnd);
5662
Bram Moolenaar734a8672019-12-02 22:49:38 +01005663 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665}
5666
5667
5668 void
5669gui_mch_set_scrollbar_thumb(
5670 scrollbar_T *sb,
5671 long val,
5672 long size,
5673 long max)
5674{
5675 SCROLLINFO info;
5676
5677 sb->scroll_shift = 0;
5678 while (max > 32767)
5679 {
5680 max = (max + 1) >> 1;
5681 val >>= 1;
5682 size >>= 1;
5683 ++sb->scroll_shift;
5684 }
5685
5686 if (sb->scroll_shift > 0)
5687 ++size;
5688
5689 info.cbSize = sizeof(info);
5690 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5691 info.nPos = val;
5692 info.nMin = 0;
5693 info.nMax = max;
5694 info.nPage = size;
5695 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5696}
5697
5698
5699/*
5700 * Set the current text font.
5701 */
5702 void
5703gui_mch_set_font(GuiFont font)
5704{
5705 gui.currFont = font;
5706}
5707
5708
5709/*
5710 * Set the current text foreground color.
5711 */
5712 void
5713gui_mch_set_fg_color(guicolor_T color)
5714{
5715 gui.currFgColor = color;
5716}
5717
5718/*
5719 * Set the current text background color.
5720 */
5721 void
5722gui_mch_set_bg_color(guicolor_T color)
5723{
5724 gui.currBgColor = color;
5725}
5726
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005727/*
5728 * Set the current text special color.
5729 */
5730 void
5731gui_mch_set_sp_color(guicolor_T color)
5732{
5733 gui.currSpColor = color;
5734}
5735
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005736#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737/*
5738 * Multi-byte handling, originally by Sung-Hoon Baek.
5739 * First static functions (no prototypes generated).
5740 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005741# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005742# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005743# endif
5744# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 * handle WM_IME_NOTIFY message
5748 */
5749 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005750_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751{
5752 LRESULT lResult = 0;
5753 HIMC hImc;
5754
5755 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5756 return lResult;
5757 switch (dwCommand)
5758 {
5759 case IMN_SETOPENSTATUS:
5760 if (pImmGetOpenStatus(hImc))
5761 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005762 LOGFONTW lf = norm_logfont;
5763 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5764 // Work around when PerMonitorV2 is not enabled in the process level.
5765 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5766 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 im_set_position(gui.row, gui.col);
5768
Bram Moolenaar734a8672019-12-02 22:49:38 +01005769 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 State &= ~LANGMAP;
5771 if (State & INSERT)
5772 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005773# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005774 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5776 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005777 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 int old_row = gui.row;
5779 int old_col = gui.col;
5780
5781 // This must be called here before
5782 // status_redraw_curbuf(), otherwise the mode
5783 // message may appear in the wrong position.
5784 showmode();
5785 status_redraw_curbuf();
5786 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005787 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 gui.row = old_row;
5789 gui.col = old_col;
5790 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
5793 }
5794 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005795 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 lResult = 0;
5797 break;
5798 }
5799 pImmReleaseContext(hWnd, hImc);
5800 return lResult;
5801}
5802
5803 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005804_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 char_u *ret;
5807 int len;
5808
Bram Moolenaar734a8672019-12-02 22:49:38 +01005809 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 return 0;
5811
5812 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5813 if (ret != NULL)
5814 {
5815 add_to_input_buf_csi(ret, len);
5816 vim_free(ret);
5817 return 1;
5818 }
5819 return 0;
5820}
5821
5822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 * void GetResultStr()
5824 *
5825 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5826 * get complete composition string
5827 */
5828 static char_u *
5829GetResultStr(HWND hwnd, int GCS, int *lenp)
5830{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005831 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005832 LONG ret;
5833 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 char_u *convbuf = NULL;
5835
5836 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5837 return NULL;
5838
K.Takatab0b2b732022-01-19 12:59:21 +00005839 // Get the length of the composition string.
5840 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5841 if (ret <= 0)
5842 return NULL;
5843
5844 // Allocate the requested buffer plus space for the NUL character.
5845 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 if (buf == NULL)
5847 return NULL;
5848
K.Takatab0b2b732022-01-19 12:59:21 +00005849 // Reads in the composition string.
5850 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5851 *lenp = ret / sizeof(WCHAR);
5852
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005853 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 pImmReleaseContext(hwnd, hIMC);
5855 vim_free(buf);
5856 return convbuf;
5857}
5858#endif
5859
Bram Moolenaar734a8672019-12-02 22:49:38 +01005860// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005861#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863/*
5864 * set font to IM.
5865 */
5866 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005867im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 HIMC hImc;
5870
5871 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5872 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005873 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 pImmReleaseContext(s_hwnd, hImc);
5875 }
5876}
5877
5878/*
5879 * Notify cursor position to IM.
5880 */
5881 void
5882im_set_position(int row, int col)
5883{
5884 HIMC hImc;
5885
5886 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5887 {
5888 COMPOSITIONFORM cfs;
5889
5890 cfs.dwStyle = CFS_POINT;
5891 cfs.ptCurrentPos.x = FILL_X(col);
5892 cfs.ptCurrentPos.y = FILL_Y(row);
5893 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005894 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5895 {
5896 // Work around when PerMonitorV2 is not enabled in the process level.
5897 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5898 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 pImmSetCompositionWindow(hImc, &cfs);
5901
5902 pImmReleaseContext(s_hwnd, hImc);
5903 }
5904}
5905
5906/*
5907 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5908 */
5909 void
5910im_set_active(int active)
5911{
5912 HIMC hImc;
5913 static HIMC hImcOld = (HIMC)0;
5914
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005915# ifdef VIMDLL
5916 if (!gui.in_use && !gui.starting)
5917 {
5918 mbyte_im_set_active(active);
5919 return;
5920 }
5921# endif
5922
Bram Moolenaar734a8672019-12-02 22:49:38 +01005923 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
5925 if (p_imdisable)
5926 {
5927 if (hImcOld == (HIMC)0)
5928 {
5929 hImcOld = pImmGetContext(s_hwnd);
5930 if (hImcOld)
5931 pImmAssociateContext(s_hwnd, (HIMC)0);
5932 }
5933 active = FALSE;
5934 }
5935 else if (hImcOld != (HIMC)0)
5936 {
5937 pImmAssociateContext(s_hwnd, hImcOld);
5938 hImcOld = (HIMC)0;
5939 }
5940
5941 hImc = pImmGetContext(s_hwnd);
5942 if (hImc)
5943 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005944 /*
5945 * for Korean ime
5946 */
5947 HKL hKL = GetKeyboardLayout(0);
5948
5949 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5950 {
5951 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5952 static BOOL bSaved = FALSE;
5953
5954 if (active)
5955 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005956 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005957 if (bSaved)
5958 pImmSetConversionStatus(hImc, dwConversionSaved,
5959 dwSentenceSaved);
5960 bSaved = FALSE;
5961 }
5962 else
5963 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005964 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005965 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5966 &dwSentenceSaved))
5967 {
5968 bSaved = TRUE;
5969 pImmSetConversionStatus(hImc,
5970 dwConversionSaved & ~(IME_CMODE_NATIVE
5971 | IME_CMODE_FULLSHAPE),
5972 dwSentenceSaved);
5973 }
5974 }
5975 }
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 pImmSetOpenStatus(hImc, active);
5978 pImmReleaseContext(s_hwnd, hImc);
5979 }
5980 }
5981}
5982
5983/*
5984 * Get IM status. When IM is on, return not 0. Else return 0.
5985 */
5986 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005987im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int status = 0;
5990 HIMC hImc;
5991
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005992# ifdef VIMDLL
5993 if (!gui.in_use && !gui.starting)
5994 return mbyte_im_get_status();
5995# endif
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5998 {
5999 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6000 pImmReleaseContext(s_hwnd, hImc);
6001 }
6002 return status;
6003}
6004
Bram Moolenaar734a8672019-12-02 22:49:38 +01006005#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006008/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006009 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006010 */
6011 static void
6012latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6013{
6014 int c;
6015
Bram Moolenaarca003e12006-03-17 23:19:38 +00006016 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006017 {
6018 c = *text++;
6019 switch (c)
6020 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006021 case 0xa4: c = 0x20ac; break; // euro
6022 case 0xa6: c = 0x0160; break; // S hat
6023 case 0xa8: c = 0x0161; break; // S -hat
6024 case 0xb4: c = 0x017d; break; // Z hat
6025 case 0xb8: c = 0x017e; break; // Z -hat
6026 case 0xbc: c = 0x0152; break; // OE
6027 case 0xbd: c = 0x0153; break; // oe
6028 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006029 }
6030 *unicodebuf++ = c;
6031 }
6032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033
6034#ifdef FEAT_RIGHTLEFT
6035/*
6036 * What is this for? In the case where you are using Win98 or Win2K or later,
6037 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6038 * reverses the string sent to the TextOut... family. This sucks, because we
6039 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6040 * way to tell Windblows not to do this!
6041 *
6042 * The short of it is that this 'RevOut' only gets called if you are running
6043 * one of the new, "improved" MS OSes, and only if you are running in
6044 * 'rightleft' mode. It makes display take *slightly* longer, but not
6045 * noticeably so.
6046 */
6047 static void
6048RevOut( HDC s_hdc,
6049 int col,
6050 int row,
6051 UINT foptions,
6052 CONST RECT *pcliprect,
6053 LPCTSTR text,
6054 UINT len,
6055 CONST INT *padding)
6056{
6057 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006059 for (ix = 0; ix < (int)len; ++ix)
6060 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6061 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062}
6063#endif
6064
Bram Moolenaar92467d32017-12-05 13:22:16 +01006065 static void
6066draw_line(
6067 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006068 int y1,
6069 int x2,
6070 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006071 COLORREF color)
6072{
6073#if defined(FEAT_DIRECTX)
6074 if (IS_ENABLE_DIRECTX())
6075 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6076 else
6077#endif
6078 {
6079 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6080 HPEN old_pen = SelectObject(s_hdc, hpen);
6081 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006082 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006083 LineTo(s_hdc, x2, y2);
6084 DeleteObject(SelectObject(s_hdc, old_pen));
6085 }
6086}
6087
6088 static void
6089set_pixel(
6090 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006091 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006092 COLORREF color)
6093{
6094#if defined(FEAT_DIRECTX)
6095 if (IS_ENABLE_DIRECTX())
6096 DWriteContext_SetPixel(s_dwc, x, y, color);
6097 else
6098#endif
6099 SetPixel(s_hdc, x, y, color);
6100}
6101
6102 static void
6103fill_rect(
6104 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006105 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006106 COLORREF color)
6107{
6108#if defined(FEAT_DIRECTX)
6109 if (IS_ENABLE_DIRECTX())
6110 DWriteContext_FillRect(s_dwc, rcp, color);
6111 else
6112#endif
6113 {
6114 HBRUSH hbr2;
6115
6116 if (hbr == NULL)
6117 hbr2 = CreateSolidBrush(color);
6118 else
6119 hbr2 = hbr;
6120 FillRect(s_hdc, rcp, hbr2);
6121 if (hbr == NULL)
6122 DeleteBrush(hbr2);
6123 }
6124}
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 void
6127gui_mch_draw_string(
6128 int row,
6129 int col,
6130 char_u *text,
6131 int len,
6132 int flags)
6133{
6134 static int *padding = NULL;
6135 static int pad_size = 0;
6136 int i;
6137 const RECT *pcliprect = NULL;
6138 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 static WCHAR *unicodebuf = NULL;
6140 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006141 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 int y;
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 /*
6146 * Italic and bold text seems to have an extra row of pixels at the bottom
6147 * (below where the bottom of the character should be). If we draw the
6148 * characters with a solid background, the top row of pixels in the
6149 * character below will be overwritten. We can fix this by filling in the
6150 * background ourselves, to the correct character proportions, and then
6151 * writing the character in transparent mode. Still have a problem when
6152 * the character is "_", which gets written on to the character below.
6153 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6154 * pixel in their slots, which fixes the problem with the bottom row of
6155 * pixels. We still need this code because otherwise the top row of pixels
6156 * becomes a problem. - webb.
6157 */
6158 static HBRUSH hbr_cache[2] = {NULL, NULL};
6159 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6160 static int brush_lru = 0;
6161 HBRUSH hbr;
6162 RECT rc;
6163
6164 if (!(flags & DRAW_TRANSP))
6165 {
6166 /*
6167 * Clear background first.
6168 * Note: FillRect() excludes right and bottom of rectangle.
6169 */
6170 rc.left = FILL_X(col);
6171 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (has_mbyte)
6173 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006174 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006175 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 }
6177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 rc.right = FILL_X(col + len);
6179 rc.bottom = FILL_Y(row + 1);
6180
Bram Moolenaar734a8672019-12-02 22:49:38 +01006181 // Cache the created brush, that saves a lot of time. We need two:
6182 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (gui.currBgColor == brush_color[0])
6184 {
6185 hbr = hbr_cache[0];
6186 brush_lru = 1;
6187 }
6188 else if (gui.currBgColor == brush_color[1])
6189 {
6190 hbr = hbr_cache[1];
6191 brush_lru = 0;
6192 }
6193 else
6194 {
6195 if (hbr_cache[brush_lru] != NULL)
6196 DeleteBrush(hbr_cache[brush_lru]);
6197 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6198 brush_color[brush_lru] = gui.currBgColor;
6199 hbr = hbr_cache[brush_lru];
6200 brush_lru = !brush_lru;
6201 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006202
Bram Moolenaar92467d32017-12-05 13:22:16 +01006203 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204
6205 SetBkMode(s_hdc, TRANSPARENT);
6206
6207 /*
6208 * When drawing block cursor, prevent inverted character spilling
6209 * over character cell (can happen with bold/italic)
6210 */
6211 if (flags & DRAW_CURSOR)
6212 {
6213 pcliprect = &rc;
6214 foptions = ETO_CLIPPED;
6215 }
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 SetTextColor(s_hdc, gui.currFgColor);
6218 SelectFont(s_hdc, gui.currFont);
6219
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006220#ifdef FEAT_DIRECTX
6221 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006222 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006223#endif
6224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6226 {
6227 vim_free(padding);
6228 pad_size = Columns;
6229
Bram Moolenaar734a8672019-12-02 22:49:38 +01006230 // Don't give an out-of-memory message here, it would call us
6231 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006232 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (padding != NULL)
6234 for (i = 0; i < pad_size; i++)
6235 padding[i] = gui.char_width;
6236 }
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 /*
6239 * We have to provide the padding argument because italic and bold versions
6240 * of fixed-width fonts are often one pixel or so wider than their normal
6241 * versions.
6242 * No check for DRAW_BOLD, Windows will have done it already.
6243 */
6244
Bram Moolenaar734a8672019-12-02 22:49:38 +01006245 // Check if there are any UTF-8 characters. If not, use normal text
6246 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 if (enc_utf8)
6248 for (n = 0; n < len; ++n)
6249 if (text[n] >= 0x80)
6250 break;
6251
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006252#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006253 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6254 // required that unicode drawing routine, currently. So this forces it
6255 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006256 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006257 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006258#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006259
Bram Moolenaar734a8672019-12-02 22:49:38 +01006260 // Check if the Unicode buffer exists and is big enough. Create it
6261 // with the same length as the multi-byte string, the number of wide
6262 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006263 if ((enc_utf8
6264 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6265 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 && (unicodebuf == NULL || len > unibuflen))
6267 {
6268 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006269 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006272 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 unibuflen = len;
6275 }
6276
6277 if (enc_utf8 && n < len && unicodebuf != NULL)
6278 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006279 // Output UTF-8 characters. Composing characters should be
6280 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006281 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006282 int wlen; // string length in words
6283 int clen; // string length in characters
6284 int cells; // cell width of string up to composing char
6285 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006286 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006288 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006289 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006291 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006293 c = utf_ptr2char(text + i);
6294 if (c >= 0x10000)
6295 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006296 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006297 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6298 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006299 }
6300 else
6301 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006302 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006303 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006304
6305 if (utf_iscomposing(c))
6306 cw = 0;
6307 else
6308 {
6309 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006310 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006311 cw = 1;
6312 }
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (unicodepdy != NULL)
6315 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006316 // Use unicodepdy to make characters fit as we expect, even
6317 // when the font uses different widths (e.g., bold character
6318 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006319 if (c >= 0x10000)
6320 {
6321 unicodepdy[wlen - 2] = cw * gui.char_width;
6322 unicodepdy[wlen - 1] = 0;
6323 }
6324 else
6325 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006328 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006329 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006331#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006332 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006333 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006334 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006335 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006336 TEXT_X(col), TEXT_Y(row),
6337 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006338 gui.char_width, gui.currFgColor,
6339 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006340 }
6341 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006342#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006343 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6344 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006345 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006347 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006349 // If we want to display codepage data, and the current CP is not the
6350 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (unicodebuf != NULL)
6352 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006353 if (enc_latin9)
6354 latin9_to_ucs(text, len, unicodebuf);
6355 else
6356 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 MB_PRECOMPOSED,
6358 (char *)text, len,
6359 (LPWSTR)unicodebuf, unibuflen);
6360 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006361 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006362 // Use unicodepdy to make characters fit as we expect, even
6363 // when the font uses different widths (e.g., bold character
6364 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006365 if (unicodepdy != NULL)
6366 {
6367 int i;
6368 int cw;
6369
6370 for (i = 0; i < len; ++i)
6371 {
6372 cw = utf_char2cells(unicodebuf[i]);
6373 if (cw > 2)
6374 cw = 1;
6375 unicodepdy[i] = cw * gui.char_width;
6376 }
6377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006379 foptions, pcliprect, unicodebuf, len, unicodepdy);
6380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382 }
6383 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
6385#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006386 // Windows will mess up RL text, so we have to draw it character by
6387 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006388 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6390 foptions, pcliprect, (char *)text, len, padding);
6391 else
6392#endif
6393 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6394 foptions, pcliprect, (char *)text, len, padding);
6395 }
6396
Bram Moolenaar734a8672019-12-02 22:49:38 +01006397 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (flags & DRAW_UNDERL)
6399 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006400 // When p_linespace is 0, overwrite the bottom row of pixels.
6401 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (p_linespace > 1)
6404 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006405 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006407
Bram Moolenaar734a8672019-12-02 22:49:38 +01006408 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006409 if (flags & DRAW_STRIKE)
6410 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006411 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006412 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006413 }
6414
Bram Moolenaar734a8672019-12-02 22:49:38 +01006415 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006416 if (flags & DRAW_UNDERC)
6417 {
6418 int x;
6419 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006420 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006421
6422 y = FILL_Y(row + 1) - 1;
6423 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6424 {
6425 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006426 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006427 }
6428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429}
6430
6431
6432/*
6433 * Output routines.
6434 */
6435
Bram Moolenaar734a8672019-12-02 22:49:38 +01006436/*
6437 * Flush any output to the screen
6438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 void
6440gui_mch_flush(void)
6441{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006442#if defined(FEAT_DIRECTX)
6443 if (IS_ENABLE_DIRECTX())
6444 DWriteContext_Flush(s_dwc);
6445#endif
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 GdiFlush();
6448}
6449
6450 static void
6451clear_rect(RECT *rcp)
6452{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006453 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454}
6455
6456
Bram Moolenaarc716c302006-01-21 22:12:51 +00006457 void
6458gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6459{
6460 RECT workarea_rect;
6461
6462 get_work_area(&workarea_rect);
6463
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006464 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006465 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6466 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006467
Bram Moolenaar734a8672019-12-02 22:49:38 +01006468 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6469 // the menubar for MSwin, we subtract it from the screen height, so that
6470 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006471 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006472 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6473 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6474 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6475 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006476}
6477
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479#if defined(FEAT_MENU) || defined(PROTO)
6480/*
6481 * Add a sub menu to the menu bar.
6482 */
6483 void
6484gui_mch_add_menu(
6485 vimmenu_T *menu,
6486 int pos)
6487{
6488 vimmenu_T *parent = menu->parent;
6489
6490 menu->submenu_id = CreatePopupMenu();
6491 menu->id = s_menu_id++;
6492
6493 if (menu_is_menubar(menu->name))
6494 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006495 WCHAR *wn;
6496 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006498 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006499 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006500 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006502 infow.cbSize = sizeof(infow);
6503 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6504 | MIIM_SUBMENU;
6505 infow.dwItemData = (long_u)menu;
6506 infow.wID = menu->id;
6507 infow.fType = MFT_STRING;
6508 infow.dwTypeData = wn;
6509 infow.cch = (UINT)wcslen(wn);
6510 infow.hSubMenu = menu->submenu_id;
6511 InsertMenuItemW((parent == NULL)
6512 ? s_menuBar : parent->submenu_id,
6513 (UINT)pos, TRUE, &infow);
6514 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
6516
Bram Moolenaar734a8672019-12-02 22:49:38 +01006517 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (parent == NULL)
6519 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006520# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 else if (IsWindow(parent->tearoff_handle))
6522 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
6526 void
6527gui_mch_show_popupmenu(vimmenu_T *menu)
6528{
6529 POINT mp;
6530
6531 (void)GetCursorPos((LPPOINT)&mp);
6532 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6533}
6534
6535 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006536gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 vimmenu_T *menu = gui_find_menu(path_name);
6539
6540 if (menu != NULL)
6541 {
6542 POINT p;
6543
Bram Moolenaar734a8672019-12-02 22:49:38 +01006544 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006546 if (mouse_pos)
6547 {
6548 int mx, my;
6549
6550 gui_mch_getmouse(&mx, &my);
6551 p.x += mx;
6552 p.y += my;
6553 }
6554 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006556 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6558 }
6559 msg_scroll = FALSE;
6560 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6561 }
6562}
6563
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006564# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565/*
6566 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6567 * create it as a pseudo-"tearoff menu".
6568 */
6569 void
6570gui_make_tearoff(char_u *path_name)
6571{
6572 vimmenu_T *menu = gui_find_menu(path_name);
6573
Bram Moolenaar734a8672019-12-02 22:49:38 +01006574 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (menu != NULL)
6576 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6577}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
6580/*
6581 * Add a menu item to a menu
6582 */
6583 void
6584gui_mch_add_menu_item(
6585 vimmenu_T *menu,
6586 int idx)
6587{
6588 vimmenu_T *parent = menu->parent;
6589
6590 menu->id = s_menu_id++;
6591 menu->submenu_id = NULL;
6592
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006593# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6595 {
6596 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6597 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6598 }
6599 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006600# endif
6601# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 if (menu_is_toolbar(parent->name))
6603 {
6604 TBBUTTON newtb;
6605
Bram Moolenaara80faa82020-04-12 19:37:17 +02006606 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (menu_is_separator(menu->name))
6608 {
6609 newtb.iBitmap = 0;
6610 newtb.fsStyle = TBSTYLE_SEP;
6611 }
6612 else
6613 {
6614 newtb.iBitmap = get_toolbar_bitmap(menu);
6615 newtb.fsStyle = TBSTYLE_BUTTON;
6616 }
6617 newtb.idCommand = menu->id;
6618 newtb.fsState = TBSTATE_ENABLED;
6619 newtb.iString = 0;
6620 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6621 (LPARAM)&newtb);
6622 menu->submenu_id = (HMENU)-1;
6623 }
6624 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006627 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006629 wn = enc_to_utf16(menu->name, NULL);
6630 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006632 InsertMenuW(parent->submenu_id, (UINT)idx,
6633 (menu_is_separator(menu->name)
6634 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6635 (UINT)menu->id, wn);
6636 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006638# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 if (IsWindow(parent->tearoff_handle))
6640 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006641# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643}
6644
6645/*
6646 * Destroy the machine specific menu widget.
6647 */
6648 void
6649gui_mch_destroy_menu(vimmenu_T *menu)
6650{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006651# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 /*
6653 * is this a toolbar button?
6654 */
6655 if (menu->submenu_id == (HMENU)-1)
6656 {
6657 int iButton;
6658
6659 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6660 (WPARAM)menu->id, 0);
6661 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6662 }
6663 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 if (menu->parent != NULL
6667 && menu_is_popup(menu->parent->dname)
6668 && menu->parent->submenu_id != NULL)
6669 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6670 else
6671 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6672 if (menu->submenu_id != NULL)
6673 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006674# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (IsWindow(menu->tearoff_handle))
6676 DestroyWindow(menu->tearoff_handle);
6677 if (menu->parent != NULL
6678 && menu->parent->children != NULL
6679 && IsWindow(menu->parent->tearoff_handle))
6680 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006681 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 menu->modes = 0;
6683 rebuild_tearoff(menu->parent);
6684 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 }
6687}
6688
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006689# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 static void
6691rebuild_tearoff(vimmenu_T *menu)
6692{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006693 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 char_u tbuf[128];
6695 RECT trect;
6696 RECT rct;
6697 RECT roct;
6698 int x, y;
6699
6700 HWND thwnd = menu->tearoff_handle;
6701
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006702 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (GetWindowRect(thwnd, &trect)
6704 && GetWindowRect(s_hwnd, &rct)
6705 && GetClientRect(s_hwnd, &roct))
6706 {
6707 x = trect.left - rct.left;
6708 y = (trect.top - rct.bottom + roct.bottom);
6709 }
6710 else
6711 {
6712 x = y = 0xffffL;
6713 }
6714 DestroyWindow(thwnd);
6715 if (menu->children != NULL)
6716 {
6717 gui_mch_tearoff(tbuf, menu, x, y);
6718 if (IsWindow(menu->tearoff_handle))
6719 (void) SetWindowPos(menu->tearoff_handle,
6720 NULL,
6721 (int)trect.left,
6722 (int)trect.top,
6723 0, 0,
6724 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6725 }
6726}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006727# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
6729/*
6730 * Make a menu either grey or not grey.
6731 */
6732 void
6733gui_mch_menu_grey(
6734 vimmenu_T *menu,
6735 int grey)
6736{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006737# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 /*
6739 * is this a toolbar button?
6740 */
6741 if (menu->submenu_id == (HMENU)-1)
6742 {
6743 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6744 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6745 }
6746 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006747# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006748 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6749 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006751# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6753 {
6754 WORD menuID;
6755 HWND menuHandle;
6756
6757 /*
6758 * A tearoff button has changed state.
6759 */
6760 if (menu->children == NULL)
6761 menuID = (WORD)(menu->id);
6762 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006763 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6765 if (menuHandle)
6766 EnableWindow(menuHandle, !grey);
6767
6768 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770}
6771
Bram Moolenaar734a8672019-12-02 22:49:38 +01006772#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774
Bram Moolenaar734a8672019-12-02 22:49:38 +01006775// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6778#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006779#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
6781#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6782/*
6783 * stuff for dialogs
6784 */
6785
6786/*
6787 * The callback routine used by all the dialogs. Very simple. First,
6788 * acknowledges the INITDIALOG message so that Windows knows to do standard
6789 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6790 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6791 * number.
6792 */
6793 static LRESULT CALLBACK
6794dialog_callback(
6795 HWND hwnd,
6796 UINT message,
6797 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006798 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 if (message == WM_INITDIALOG)
6801 {
6802 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006803 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 (void)SetFocus(hwnd);
6805 if (dialog_default_button > IDCANCEL)
6806 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006807 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006808 // We don't have a default, set focus on another element of the
6809 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006810 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 return FALSE;
6812 }
6813
6814 if (message == WM_COMMAND)
6815 {
6816 int button = LOWORD(wParam);
6817
Bram Moolenaar734a8672019-12-02 22:49:38 +01006818 // Don't end the dialog if something was selected that was
6819 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (button >= DLG_NONBUTTON_CONTROL)
6821 return TRUE;
6822
Bram Moolenaar734a8672019-12-02 22:49:38 +01006823 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006825 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006826 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006827 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006828
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006829 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6830 p = utf16_to_enc(wp, NULL);
6831 vim_strncpy(s_textfield, p, IOSIZE);
6832 vim_free(p);
6833 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 /*
6837 * Need to check for IDOK because if the user just hits Return to
6838 * accept the default value, some reason this is what we get.
6839 */
6840 if (button == IDOK)
6841 {
6842 if (dialog_default_button > IDCANCEL)
6843 EndDialog(hwnd, dialog_default_button);
6844 }
6845 else
6846 EndDialog(hwnd, button - IDCANCEL);
6847 return TRUE;
6848 }
6849
6850 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6851 {
6852 EndDialog(hwnd, 0);
6853 return TRUE;
6854 }
6855 return FALSE;
6856}
6857
6858/*
6859 * Create a dialog dynamically from the parameter strings.
6860 * type = type of dialog (question, alert, etc.)
6861 * title = dialog title. may be NULL for default title.
6862 * message = text to display. Dialog sizes to accommodate it.
6863 * buttons = '\n' separated list of button captions, default first.
6864 * dfltbutton = number of default button.
6865 *
6866 * This routine returns 1 if the first button is pressed,
6867 * 2 for the second, etc.
6868 *
6869 * 0 indicates Esc was pressed.
6870 * -1 for unexpected error
6871 *
6872 * If stubbing out this fn, return 1.
6873 */
6874
Bram Moolenaar734a8672019-12-02 22:49:38 +01006875static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876{
6877 "IDR_VIM",
6878 "IDR_VIM_ERROR",
6879 "IDR_VIM_ALERT",
6880 "IDR_VIM_INFO",
6881 "IDR_VIM_QUESTION"
6882};
6883
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 int
6885gui_mch_dialog(
6886 int type,
6887 char_u *title,
6888 char_u *message,
6889 char_u *buttons,
6890 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006891 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006892 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006895 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 int numButtons;
6897 int *buttonWidths, *buttonPositions;
6898 int buttonYpos;
6899 int nchar, i;
6900 DWORD lStyle;
6901 int dlgwidth = 0;
6902 int dlgheight;
6903 int editboxheight;
6904 int horizWidth = 0;
6905 int msgheight;
6906 char_u *pstart;
6907 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006908 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 char_u *tbuffer;
6910 RECT rect;
6911 HWND hwnd;
6912 HDC hdc;
6913 HFONT font, oldFont;
6914 TEXTMETRIC fontInfo;
6915 int fontHeight;
6916 int textWidth, minButtonWidth, messageWidth;
6917 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006918 int maxDialogHeight;
6919 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 int vertical;
6921 int dlgPaddingX;
6922 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006923# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006924 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006926# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 garray_T ga;
6928 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006929 int dlg_icon_width;
6930 int dlg_icon_height;
6931 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006933# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006934 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006935# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006936 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006937# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006938 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006939 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
Bram Moolenaar748bf032005-02-02 23:04:36 +00006942 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006943 {
6944 load_dpi_func();
6945 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006946 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006947 }
6948 else
6949 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
6951 if ((type < 0) || (type > VIM_LAST_TYPE))
6952 type = 0;
6953
Bram Moolenaar734a8672019-12-02 22:49:38 +01006954 // allocate some memory for dialog template
6955 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006956 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006957 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959 if (p == NULL)
6960 return -1;
6961
6962 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006963 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6965 * const.
6966 */
6967 tbuffer = vim_strsave(buttons);
6968 if (tbuffer == NULL)
6969 return -1;
6970
Bram Moolenaar734a8672019-12-02 22:49:38 +01006971 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
Bram Moolenaar734a8672019-12-02 22:49:38 +01006973 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 numButtons = 1;
6975 for (i = 0; tbuffer[i] != '\0'; i++)
6976 {
6977 if (tbuffer[i] == DLG_BUTTON_SEP)
6978 numButtons++;
6979 }
6980 if (dfltbutton >= numButtons)
6981 dfltbutton = -1;
6982
Bram Moolenaar734a8672019-12-02 22:49:38 +01006983 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006984 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (buttonWidths == NULL)
6986 return -1;
6987
Bram Moolenaar734a8672019-12-02 22:49:38 +01006988 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006989 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (buttonPositions == NULL)
6991 return -1;
6992
6993 /*
6994 * Calculate how big the dialog must be.
6995 */
6996 hwnd = GetDesktopWindow();
6997 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006998# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7000 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007001 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 use_lfSysmenu = TRUE;
7003 }
7004 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007007 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (s_usenewlook)
7009 {
7010 oldFont = SelectFont(hdc, font);
7011 dlgPaddingX = DLG_PADDING_X;
7012 dlgPaddingY = DLG_PADDING_Y;
7013 }
7014 else
7015 {
7016 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7017 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7018 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7019 }
7020 GetTextMetrics(hdc, &fontInfo);
7021 fontHeight = fontInfo.tmHeight;
7022
Bram Moolenaar734a8672019-12-02 22:49:38 +01007023 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007024 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
Bram Moolenaar734a8672019-12-02 22:49:38 +01007026 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007027 if (s_hwnd == NULL)
7028 {
7029 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaar734a8672019-12-02 22:49:38 +01007031 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007032 get_work_area(&workarea_rect);
7033 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007034 if (maxDialogWidth > adjust_by_system_dpi(600))
7035 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007036 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007037 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007038 }
7039 else
7040 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007041 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007042 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007043 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007044 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7045 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7046 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7047 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007048
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007049 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007050 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7051 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7052 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7053 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7054 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007055 }
7056
Bram Moolenaar734a8672019-12-02 22:49:38 +01007057 // Set dlgwidth to width of message.
7058 // Copy the message into "ga", changing NL to CR-NL and inserting line
7059 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 pstart = message;
7061 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007062 msgheight = 0;
7063 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 do
7065 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007066 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007067
Bram Moolenaar734a8672019-12-02 22:49:38 +01007068 // Need to figure out where to break the string. The system does it
7069 // at a word boundary, which would mean we can't compute the number of
7070 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007071 textWidth = 0;
7072 last_white = NULL;
7073 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007074 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007075 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007076 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007077 && textWidth > maxDialogWidth * 3 / 4)
7078 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007079 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007080 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007081 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007082 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007083 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007084 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007085 textWidth = 0;
7086
7087 if (last_white != NULL)
7088 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007089 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007090 if (pend > last_white)
7091 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007092 pend = last_white + 1;
7093 last_white = NULL;
7094 }
7095 ga_append(&ga, '\r');
7096 ga_append(&ga, '\n');
7097 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007098 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007099
7100 while (--l >= 0)
7101 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007102 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007103 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007105
7106 ga_append(&ga, '\r');
7107 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 pstart = pend + 1;
7109 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007110
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007111 if (ga.ga_data != NULL)
7112 message = ga.ga_data;
7113
Bram Moolenaar734a8672019-12-02 22:49:38 +01007114 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007115
K.Takatac81e9bf2022-01-16 14:15:49 +00007116 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7117 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
K.Takatac81e9bf2022-01-16 14:15:49 +00007119 // Add width of icon to dlgwidth, and some space
7120 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7121 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7122
7123 if (msgheight < dlg_icon_height)
7124 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 /*
7127 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007128 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007130 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 if (!vertical)
7132 {
7133 // Place buttons horizontally if they fit.
7134 horizWidth = dlgPaddingX;
7135 pstart = tbuffer;
7136 i = 0;
7137 do
7138 {
7139 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7140 if (pend == NULL)
7141 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007142 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (textWidth < minButtonWidth)
7144 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007145 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 buttonWidths[i] = textWidth;
7147 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007148 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 pstart = pend + 1;
7150 } while (*pend != NUL);
7151
7152 if (horizWidth > maxDialogWidth)
7153 vertical = TRUE; // Too wide to fit on the screen.
7154 else if (horizWidth > dlgwidth)
7155 dlgwidth = horizWidth;
7156 }
7157
7158 if (vertical)
7159 {
7160 // Stack buttons vertically.
7161 pstart = tbuffer;
7162 do
7163 {
7164 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7165 if (pend == NULL)
7166 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007167 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007168 textWidth += dlgPaddingX; // Padding within button
7169 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 if (textWidth > dlgwidth)
7171 dlgwidth = textWidth;
7172 pstart = pend + 1;
7173 } while (*pend != NUL);
7174 }
7175
7176 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaar734a8672019-12-02 22:49:38 +01007179 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (s_usenewlook)
7181 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7182 else
7183 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7184
7185 add_long(lStyle);
7186 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 add_word(0); // NumberOfItems(will change later)
7189 add_word(10); // x
7190 add_word(10); // y
7191 add_word(PixelToDialogX(dlgwidth)); // cx
7192
7193 // Dialog height.
7194 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007195 dlgheight = msgheight + 2 * dlgPaddingY
7196 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 else
7198 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7199
7200 // Dialog needs to be taller if contains an edit box.
7201 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7202 if (textfield != NULL)
7203 dlgheight += editboxheight;
7204
Bram Moolenaar734a8672019-12-02 22:49:38 +01007205 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007206 if (dlgheight > maxDialogHeight)
7207 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007208 msgheight = msgheight - (dlgheight - maxDialogHeight);
7209 dlgheight = maxDialogHeight;
7210 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007211 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007212 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007213 }
7214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 add_word(PixelToDialogY(dlgheight));
7216
7217 add_word(0); // Menu
7218 add_word(0); // Class
7219
Bram Moolenaar734a8672019-12-02 22:49:38 +01007220 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007221 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7222 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 p += nchar;
7224
7225 if (s_usenewlook)
7226 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007227 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007228# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (use_lfSysmenu)
7230 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007231 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7233 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007234 wcscpy(p, lfSysmenu.lfFaceName);
7235 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
7240 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007241 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 }
7243 p += nchar;
7244 }
7245
7246 buttonYpos = msgheight + 2 * dlgPaddingY;
7247
7248 if (textfield != NULL)
7249 buttonYpos += editboxheight;
7250
7251 pstart = tbuffer;
7252 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007253 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 for (i = 0; i < numButtons; i++)
7255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007256 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 for ( pend = pstart;
7258 *pend && (*pend != DLG_BUTTON_SEP);
7259 pend++)
7260 ;
7261
7262 if (*pend)
7263 *pend = '\0';
7264
7265 /*
7266 * old NOTE:
7267 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7268 * the focus to the first tab-able button and in so doing makes that
7269 * the default!! Grrr. Workaround: Make the default button the only
7270 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7271 * he/she can use arrow keys.
7272 *
7273 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007274 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 * dialog. Also needed for when the textfield is the default control.
7276 * It appears to work now (perhaps not on Win95?).
7277 */
7278 if (vertical)
7279 {
7280 p = add_dialog_element(p,
7281 (i == dfltbutton
7282 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7283 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007284 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 + 2 * fontHeight * i),
7286 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7287 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007288 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290 else
7291 {
7292 p = add_dialog_element(p,
7293 (i == dfltbutton
7294 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7295 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007296 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 PixelToDialogX(buttonWidths[i]),
7298 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007299 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007301 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303 *pnumitems += numButtons;
7304
Bram Moolenaar734a8672019-12-02 22:49:38 +01007305 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 p = add_dialog_element(p, SS_ICON,
7307 PixelToDialogX(dlgPaddingX),
7308 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007309 PixelToDialogX(dlg_icon_width),
7310 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7312 dlg_icons[type]);
7313
Bram Moolenaar734a8672019-12-02 22:49:38 +01007314 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007315 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007316 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007317 PixelToDialogY(dlgPaddingY),
7318 (WORD)(PixelToDialogX(messageWidth) + 1),
7319 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007320 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaar734a8672019-12-02 22:49:38 +01007322 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (textfield != NULL)
7324 {
7325 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7326 PixelToDialogX(2 * dlgPaddingX),
7327 PixelToDialogY(2 * dlgPaddingY + msgheight),
7328 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7329 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007330 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 *pnumitems += 1;
7332 }
7333
7334 *pnumitems += 2;
7335
7336 SelectFont(hdc, oldFont);
7337 DeleteObject(font);
7338 ReleaseDC(hwnd, hdc);
7339
Bram Moolenaar734a8672019-12-02 22:49:38 +01007340 // Let the dialog_callback() function know which button to make default
7341 // If we have an edit box, make that the default. We also need to tell
7342 // dialog_callback() if this dialog contains an edit box or not. We do
7343 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 if (textfield != NULL)
7345 {
7346 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7347 s_textfield = textfield;
7348 }
7349 else
7350 {
7351 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7352 s_textfield = NULL;
7353 }
7354
Bram Moolenaar734a8672019-12-02 22:49:38 +01007355 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007357 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 (LPDLGTEMPLATE)pdlgtemplate,
7359 s_hwnd,
7360 (DLGPROC)dialog_callback);
7361
7362 LocalFree(LocalHandle(pdlgtemplate));
7363 vim_free(tbuffer);
7364 vim_free(buttonWidths);
7365 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007366 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaar734a8672019-12-02 22:49:38 +01007368 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 (void)SetFocus(s_hwnd);
7370
7371 return nchar;
7372}
7373
Bram Moolenaar734a8672019-12-02 22:49:38 +01007374#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007375
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376/*
7377 * Put a simple element (basic class) onto a dialog template in memory.
7378 * return a pointer to where the next item should be added.
7379 *
7380 * parameters:
7381 * lStyle = additional style flags
7382 * (be careful, NT3.51 & Win32s will ignore the new ones)
7383 * x,y = x & y positions IN DIALOG UNITS
7384 * w,h = width and height IN DIALOG UNITS
7385 * Id = ID used in messages
7386 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7387 * caption = usually text or resource name
7388 *
7389 * TODO: use the length information noted here to enable the dialog creation
7390 * routines to work out more exactly how much memory they need to alloc.
7391 */
7392 static PWORD
7393add_dialog_element(
7394 PWORD p,
7395 DWORD lStyle,
7396 WORD x,
7397 WORD y,
7398 WORD w,
7399 WORD h,
7400 WORD Id,
7401 WORD clss,
7402 const char *caption)
7403{
7404 int nchar;
7405
Bram Moolenaar734a8672019-12-02 22:49:38 +01007406 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7408 *p++ = LOWORD(lStyle);
7409 *p++ = HIWORD(lStyle);
7410 *p++ = 0; // LOWORD (lExtendedStyle)
7411 *p++ = 0; // HIWORD (lExtendedStyle)
7412 *p++ = x;
7413 *p++ = y;
7414 *p++ = w;
7415 *p++ = h;
7416 *p++ = Id; //9 or 10 words in all
7417
7418 *p++ = (WORD)0xffff;
7419 *p++ = clss; //2 more here
7420
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007421 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 p += nchar;
7423
7424 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7425
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007426 return p; // total = 15 + strlen(caption) words
7427 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
7430
7431/*
7432 * Helper routine. Take an input pointer, return closest pointer that is
7433 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7434 */
7435 static LPWORD
7436lpwAlign(
7437 LPWORD lpIn)
7438{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007439 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007441 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 ul += 3;
7443 ul >>= 2;
7444 ul <<= 2;
7445 return (LPWORD)ul;
7446}
7447
7448/*
7449 * Helper routine. Takes second parameter as Ansi string, copies it to first
7450 * parameter as wide character (16-bits / char) string, and returns integer
7451 * number of wide characters (words) in string (including the trailing wide
7452 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007453 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7454 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 static int
7456nCopyAnsiToWideChar(
7457 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007458 LPSTR lpAnsiIn,
7459 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007462 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 int i;
7464 WCHAR *wn;
7465
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007466 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007468 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007469 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 if (wn != NULL)
7471 {
7472 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007473 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 vim_free(wn);
7475 }
7476 }
7477 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007478 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 nChar = MultiByteToWideChar(
7480 enc_codepage > 0 ? enc_codepage : CP_ACP,
7481 MB_PRECOMPOSED,
7482 lpAnsiIn, len,
7483 lpWCStr, len);
7484 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007485 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488 return nChar;
7489}
7490
7491
7492#ifdef FEAT_TEAROFF
7493/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007494 * Lookup menu handle from "menu_id".
7495 */
7496 static HMENU
7497tearoff_lookup_menuhandle(
7498 vimmenu_T *menu,
7499 WORD menu_id)
7500{
7501 for ( ; menu != NULL; menu = menu->next)
7502 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007503 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007504 continue;
7505 if (menu_is_separator(menu->dname))
7506 continue;
7507 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7508 return menu->submenu_id;
7509 }
7510 return NULL;
7511}
7512
7513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 * The callback function for all the modeless dialogs that make up the
7515 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7516 * thinking its menus have been clicked), and go away when closed.
7517 */
7518 static LRESULT CALLBACK
7519tearoff_callback(
7520 HWND hwnd,
7521 UINT message,
7522 WPARAM wParam,
7523 LPARAM lParam)
7524{
7525 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007526 {
7527 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530
Bram Moolenaar734a8672019-12-02 22:49:38 +01007531 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 HandleMouseHide(message, lParam);
7533
7534 if (message == WM_COMMAND)
7535 {
7536 if ((WORD)(LOWORD(wParam)) & 0x8000)
7537 {
7538 POINT mp;
7539 RECT rect;
7540
7541 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7542 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007543 vimmenu_T *menu;
7544
7545 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007547 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7549 (int)rect.right - 8,
7550 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007551 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 s_hwnd,
7553 NULL);
7554 /*
7555 * NOTE: The pop-up menu can eat the mouse up event.
7556 * We deal with this in normal.c.
7557 */
7558 }
7559 }
7560 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007561 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7563 /*
7564 * Give main window the focus back: this is so after
7565 * choosing a tearoff button you can start typing again
7566 * straight away.
7567 */
7568 (void)SetFocus(s_hwnd);
7569 return TRUE;
7570 }
7571 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7572 {
7573 DestroyWindow(hwnd);
7574 return TRUE;
7575 }
7576
Bram Moolenaar734a8672019-12-02 22:49:38 +01007577 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (message == WM_EXITSIZEMOVE)
7579 (void)SetActiveWindow(s_hwnd);
7580
7581 return FALSE;
7582}
7583#endif
7584
7585
7586/*
7587 * Decide whether to use the "new look" (small, non-bold font) or the "old
7588 * look" (big, clanky font) for dialogs, and work out a few values for use
7589 * later accordingly.
7590 */
7591 static void
7592get_dialog_font_metrics(void)
7593{
7594 HDC hdc;
7595 HFONT hfontTools = 0;
7596 DWORD dlgFontSize;
7597 SIZE size;
7598#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007599 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600#endif
7601
7602 s_usenewlook = FALSE;
7603
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007605 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007606 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608#endif
7609 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007610 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007612 if (hfontTools)
7613 {
7614 hdc = GetDC(s_hwnd);
7615 SelectObject(hdc, hfontTools);
7616 /*
7617 * GetTextMetrics() doesn't return the right value in
7618 * tmAveCharWidth, so we have to figure out the dialog base units
7619 * ourselves.
7620 */
7621 GetTextExtentPoint(hdc,
7622 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7623 52, &size);
7624 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007626 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7627 s_dlgfntheight = (WORD)size.cy;
7628 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630
7631 if (!s_usenewlook)
7632 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007633 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 s_dlgfntwidth = LOWORD(dlgFontSize);
7635 s_dlgfntheight = HIWORD(dlgFontSize);
7636 }
7637}
7638
7639#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7640/*
7641 * Create a pseudo-"tearoff menu" based on the child
7642 * items of a given menu pointer.
7643 */
7644 static void
7645gui_mch_tearoff(
7646 char_u *title,
7647 vimmenu_T *menu,
7648 int initX,
7649 int initY)
7650{
7651 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7652 int template_len;
7653 int nchar, textWidth, submenuWidth;
7654 DWORD lStyle;
7655 DWORD lExtendedStyle;
7656 WORD dlgwidth;
7657 WORD menuID;
7658 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007659 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 vimmenu_T *the_menu = menu;
7661 HWND hwnd;
7662 HDC hdc;
7663 HFONT font, oldFont;
7664 int col, spaceWidth, len;
7665 int columnWidths[2];
7666 char_u *label, *text;
7667 int acLen = 0;
7668 int nameLen;
7669 int padding0, padding1, padding2 = 0;
7670 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007671 int x;
7672 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007673# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007674 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 /*
7679 * If this menu is already torn off, move it to the mouse position.
7680 */
7681 if (IsWindow(menu->tearoff_handle))
7682 {
7683 POINT mp;
7684 if (GetCursorPos((LPPOINT)&mp))
7685 {
7686 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7687 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7688 }
7689 return;
7690 }
7691
7692 /*
7693 * Create a new tearoff.
7694 */
7695 if (*title == MNU_HIDDEN_CHAR)
7696 title++;
7697
Bram Moolenaar734a8672019-12-02 22:49:38 +01007698 // Allocate memory to store the dialog template. It's made bigger when
7699 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 template_len = DLG_ALLOC_SIZE;
7701 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7702 if (p == NULL)
7703 return;
7704
7705 hwnd = GetDesktopWindow();
7706 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007707# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7709 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007710 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 use_lfSysmenu = TRUE;
7712 }
7713 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007716 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (s_usenewlook)
7718 oldFont = SelectFont(hdc, font);
7719 else
7720 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7721
Bram Moolenaar734a8672019-12-02 22:49:38 +01007722 // Calculate width of a single space. Used for padding columns to the
7723 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007724 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
Bram Moolenaar734a8672019-12-02 22:49:38 +01007726 // Figure out max width of the text column, the accelerator column and the
7727 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 submenuWidth = 0;
7729 for (col = 0; col < 2; col++)
7730 {
7731 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007732 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 text = (col == 0) ? pmenu->dname : pmenu->actext;
7736 if (text != NULL && *text != NUL)
7737 {
7738 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7739 if (textWidth > columnWidths[col])
7740 columnWidths[col] = textWidth;
7741 }
7742 if (pmenu->children != NULL)
7743 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7744 }
7745 }
7746 if (columnWidths[1] == 0)
7747 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007748 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 if (submenuWidth != 0)
7750 columnWidths[0] += submenuWidth;
7751 else
7752 columnWidths[0] += spaceWidth;
7753 }
7754 else
7755 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007756 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7758 columnWidths[1] += submenuWidth;
7759 }
7760
7761 /*
7762 * Now find the total width of our 'menu'.
7763 */
7764 textWidth = columnWidths[0] + columnWidths[1];
7765 if (submenuWidth != 0)
7766 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007767 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7769 textWidth += submenuWidth;
7770 }
7771 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7772 if (textWidth > dlgwidth)
7773 dlgwidth = textWidth;
7774 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7775
Bram Moolenaar734a8672019-12-02 22:49:38 +01007776 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 if (s_usenewlook)
7778 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7779 else
7780 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7781
7782 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7783 *p++ = LOWORD(lStyle);
7784 *p++ = HIWORD(lStyle);
7785 *p++ = LOWORD(lExtendedStyle);
7786 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007787 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007789 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007791 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 else
7793 *p++ = PixelToDialogX(initX); // x
7794 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007795 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 else
7797 *p++ = PixelToDialogY(initY); // y
7798 *p++ = PixelToDialogX(dlgwidth); // cx
7799 ptrueheight = p;
7800 *p++ = 0; // dialog height: changed later anyway
7801 *p++ = 0; // Menu
7802 *p++ = 0; // Class
7803
Bram Moolenaar734a8672019-12-02 22:49:38 +01007804 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007806 ? (LPSTR)title
7807 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 p += nchar;
7809
7810 if (s_usenewlook)
7811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007812 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007813# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (use_lfSysmenu)
7815 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007816 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7818 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007819 wcscpy(p, lfSysmenu.lfFaceName);
7820 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 }
7822 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
7825 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007826 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 }
7828 p += nchar;
7829 }
7830
7831 /*
7832 * Loop over all the items in the menu.
7833 * But skip over the tearbar.
7834 */
7835 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7836 menu = menu->children->next;
7837 else
7838 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007839 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 for ( ; menu != NULL; menu = menu->next)
7841 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007842 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 continue;
7844 if (menu_is_separator(menu->dname))
7845 {
7846 sepPadding += 3;
7847 continue;
7848 }
7849
Bram Moolenaar734a8672019-12-02 22:49:38 +01007850 // Check if there still is plenty of room in the template. Make it
7851 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7853 {
7854 WORD *newp;
7855
7856 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7857 if (newp != NULL)
7858 {
7859 template_len += 4096;
7860 mch_memmove(newp, pdlgtemplate,
7861 (char *)p - (char *)pdlgtemplate);
7862 p = newp + (p - pdlgtemplate);
7863 pnumitems = newp + (pnumitems - pdlgtemplate);
7864 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7865 LocalFree(LocalHandle(pdlgtemplate));
7866 pdlgtemplate = newp;
7867 }
7868 }
7869
Bram Moolenaar734a8672019-12-02 22:49:38 +01007870 // Figure out minimal length of this menu label. Use "name" for the
7871 // actual text, "dname" for estimating the displayed size. "name"
7872 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 len = nameLen = (int)STRLEN(menu->name);
7874 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7875 (int)STRLEN(menu->dname))) / spaceWidth;
7876 len += padding0;
7877
7878 if (menu->actext != NULL)
7879 {
7880 acLen = (int)STRLEN(menu->actext);
7881 len += acLen;
7882 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7883 }
7884 else
7885 textWidth = 0;
7886 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7887 len += padding1;
7888
7889 if (menu->children == NULL)
7890 {
7891 padding2 = submenuWidth / spaceWidth;
7892 len += padding2;
7893 menuID = (WORD)(menu->id);
7894 }
7895 else
7896 {
7897 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007898 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900
Bram Moolenaar734a8672019-12-02 22:49:38 +01007901 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007902 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if (label == NULL)
7904 break;
7905
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007906 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007907 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007909 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 while (padding0-- > 0)
7911 *text++ = ' ';
7912 if (menu->actext != NULL)
7913 {
7914 STRNCPY(text, menu->actext, acLen);
7915 text += acLen;
7916 }
7917 while (padding1-- > 0)
7918 *text++ = ' ';
7919 if (menu->children != NULL)
7920 {
7921 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7922 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7923 }
7924 else
7925 {
7926 while (padding2-- > 0)
7927 *text++ = ' ';
7928 }
7929 *text = NUL;
7930
7931 /*
7932 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7933 * W95/NT4 it makes the tear-off look more like a menu.
7934 */
7935 p = add_dialog_element(p,
7936 BS_PUSHBUTTON|BS_LEFT,
7937 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7938 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7939 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7940 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007941 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 vim_free(label);
7943 (*pnumitems)++;
7944 }
7945
7946 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7947
7948
Bram Moolenaar734a8672019-12-02 22:49:38 +01007949 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007950 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007951 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 (LPDLGTEMPLATE)pdlgtemplate,
7953 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007954 (DLGPROC)tearoff_callback,
7955 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956
7957 LocalFree(LocalHandle(pdlgtemplate));
7958 SelectFont(hdc, oldFont);
7959 DeleteObject(font);
7960 ReleaseDC(hwnd, hdc);
7961
7962 /*
7963 * Reassert ourselves as the active window. This is so that after creating
7964 * a tearoff, the user doesn't have to click with the mouse just to start
7965 * typing again!
7966 */
7967 (void)SetActiveWindow(s_hwnd);
7968
Bram Moolenaar734a8672019-12-02 22:49:38 +01007969 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 force_menu_update = TRUE;
7971}
7972#endif
7973
7974#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007975# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
Bram Moolenaar734a8672019-12-02 22:49:38 +01007977// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978# ifndef TBSTYLE_FLAT
7979# define TBSTYLE_FLAT 0x0800
7980# endif
7981
7982/*
7983 * Create the toolbar, initially unpopulated.
7984 * (just like the menu, there are no defaults, it's all
7985 * set up through menu.vim)
7986 */
7987 static void
7988initialise_toolbar(void)
7989{
7990 InitCommonControls();
7991 s_toolbarhwnd = CreateToolbarEx(
7992 s_hwnd,
7993 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7994 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007995 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007996 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 IDR_TOOLBAR1, // id of initial bitmap
7998 NULL,
7999 0, // initial number of buttons
8000 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8001 TOOLBAR_BUTTON_HEIGHT,
8002 TOOLBAR_BUTTON_WIDTH,
8003 TOOLBAR_BUTTON_HEIGHT,
8004 sizeof(TBBUTTON)
8005 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008006
8007 // Remove transparency from the toolbar to prevent the main window
8008 // background colour showing through
8009 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8010 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8011
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008012 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
8014 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008015
8016 update_toolbar_size();
8017}
8018
8019 static void
8020update_toolbar_size(void)
8021{
8022 int w, h;
8023 TBMETRICS tbm = {sizeof(TBMETRICS)};
8024
8025 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8026 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8027 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8028 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8029
8030 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8031 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8032 //TRACE("button size: %d, %d", w, h);
8033 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8034 gui.toolbar_height = h + 6;
8035
8036 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8037 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8038
8039 // TODO:
8040 // Currently, this function only updates the size of toolbar buttons.
8041 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042}
8043
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008044 static LRESULT CALLBACK
8045toolbar_wndproc(
8046 HWND hwnd,
8047 UINT uMsg,
8048 WPARAM wParam,
8049 LPARAM lParam)
8050{
8051 HandleMouseHide(uMsg, lParam);
8052 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8053}
8054
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 static int
8056get_toolbar_bitmap(vimmenu_T *menu)
8057{
8058 int i = -1;
8059
8060 /*
8061 * Check user bitmaps first, unless builtin is specified.
8062 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008063 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
8065 char_u fname[MAXPATHL];
8066 HANDLE hbitmap = NULL;
8067
8068 if (menu->iconfile != NULL)
8069 {
8070 gui_find_iconfile(menu->iconfile, fname, "bmp");
8071 hbitmap = LoadImage(
8072 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008073 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 IMAGE_BITMAP,
8075 TOOLBAR_BUTTON_WIDTH,
8076 TOOLBAR_BUTTON_HEIGHT,
8077 LR_LOADFROMFILE |
8078 LR_LOADMAP3DCOLORS
8079 );
8080 }
8081
8082 /*
8083 * If the LoadImage call failed, or the "icon=" file
8084 * didn't exist or wasn't specified, try the menu name
8085 */
8086 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008087 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008088# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008089 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008090# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008091 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 hbitmap = LoadImage(
8093 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008094 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 IMAGE_BITMAP,
8096 TOOLBAR_BUTTON_WIDTH,
8097 TOOLBAR_BUTTON_HEIGHT,
8098 LR_LOADFROMFILE |
8099 LR_LOADMAP3DCOLORS
8100 );
8101
8102 if (hbitmap != NULL)
8103 {
8104 TBADDBITMAP tbAddBitmap;
8105
8106 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008107 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
8109 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8110 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008111 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
8113 }
8114 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8115 i = menu->iconidx;
8116
8117 return i;
8118}
8119#endif
8120
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008121#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8122 static void
8123initialise_tabline(void)
8124{
8125 InitCommonControls();
8126
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008127 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008128 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008129 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008130 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008131 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008132
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008133 gui.tabline_height = TABLINE_HEIGHT;
8134
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008135 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008136}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008137
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008138/*
8139 * Get tabpage_T from POINT.
8140 */
8141 static tabpage_T *
8142GetTabFromPoint(
8143 HWND hWnd,
8144 POINT pt)
8145{
8146 tabpage_T *ptp = NULL;
8147
8148 if (gui_mch_showing_tabline())
8149 {
8150 TCHITTESTINFO htinfo;
8151 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008152 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008153 if (s_tabhwnd == hWnd)
8154 {
8155 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8156 if (idx != -1)
8157 ptp = find_tabpage(idx + 1);
8158 }
8159 }
8160 return ptp;
8161}
8162
8163static POINT s_pt = {0, 0};
8164static HCURSOR s_hCursor = NULL;
8165
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008166 static LRESULT CALLBACK
8167tabline_wndproc(
8168 HWND hwnd,
8169 UINT uMsg,
8170 WPARAM wParam,
8171 LPARAM lParam)
8172{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008173 POINT pt;
8174 tabpage_T *tp;
8175 RECT rect;
8176 int nCenter;
8177 int idx0;
8178 int idx1;
8179
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008180 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008181
8182 switch (uMsg)
8183 {
8184 case WM_LBUTTONDOWN:
8185 {
8186 s_pt.x = GET_X_LPARAM(lParam);
8187 s_pt.y = GET_Y_LPARAM(lParam);
8188 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008189 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008190 break;
8191 }
8192 case WM_MOUSEMOVE:
8193 if (GetCapture() == hwnd
8194 && ((wParam & MK_LBUTTON)) != 0)
8195 {
8196 pt.x = GET_X_LPARAM(lParam);
8197 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008198 if (abs(pt.x - s_pt.x) >
8199 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008200 {
8201 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8202
8203 tp = GetTabFromPoint(hwnd, pt);
8204 if (tp != NULL)
8205 {
8206 idx0 = tabpage_index(curtab) - 1;
8207 idx1 = tabpage_index(tp) - 1;
8208
8209 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8210 nCenter = rect.left + (rect.right - rect.left) / 2;
8211
Bram Moolenaar734a8672019-12-02 22:49:38 +01008212 // Check if the mouse cursor goes over the center of
8213 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008214 if ((idx0 < idx1) && (nCenter < pt.x))
8215 {
8216 tabpage_move(idx1 + 1);
8217 update_screen(0);
8218 }
8219 else if ((idx1 < idx0) && (pt.x < nCenter))
8220 {
8221 tabpage_move(idx1);
8222 update_screen(0);
8223 }
8224 }
8225 }
8226 }
8227 break;
8228 case WM_LBUTTONUP:
8229 {
8230 if (GetCapture() == hwnd)
8231 {
8232 SetCursor(s_hCursor);
8233 ReleaseCapture();
8234 }
8235 break;
8236 }
8237 default:
8238 break;
8239 }
8240
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008241 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8242}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008243#endif
8244
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8246/*
8247 * Make the GUI window come to the foreground.
8248 */
8249 void
8250gui_mch_set_foreground(void)
8251{
8252 if (IsIconic(s_hwnd))
8253 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8254 SetForegroundWindow(s_hwnd);
8255}
8256#endif
8257
8258#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8259 static void
8260dyn_imm_load(void)
8261{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008262 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 if (hLibImm == NULL)
8264 return;
8265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 pImmGetCompositionStringW
8267 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8268 pImmGetContext
8269 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8270 pImmAssociateContext
8271 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8272 pImmReleaseContext
8273 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8274 pImmGetOpenStatus
8275 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8276 pImmSetOpenStatus
8277 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008278 pImmGetCompositionFontW
8279 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8280 pImmSetCompositionFontW
8281 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 pImmSetCompositionWindow
8283 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8284 pImmGetConversionStatus
8285 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008286 pImmSetConversionStatus
8287 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
K.Takatab0b2b732022-01-19 12:59:21 +00008289 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 || pImmGetContext == NULL
8291 || pImmAssociateContext == NULL
8292 || pImmReleaseContext == NULL
8293 || pImmGetOpenStatus == NULL
8294 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008295 || pImmGetCompositionFontW == NULL
8296 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008298 || pImmGetConversionStatus == NULL
8299 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {
8301 FreeLibrary(hLibImm);
8302 hLibImm = NULL;
8303 pImmGetContext = NULL;
8304 return;
8305 }
8306
8307 return;
8308}
8309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310#endif
8311
8312#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8313
8314# ifdef FEAT_XPM_W32
8315# define IMAGE_XPM 100
8316# endif
8317
8318typedef struct _signicon_t
8319{
8320 HANDLE hImage;
8321 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008322# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008323 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325} signicon_t;
8326
8327 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008328gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330 signicon_t *sign;
8331 int x, y, w, h;
8332
8333 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8334 return;
8335
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008336# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008337 if (IS_ENABLE_DIRECTX())
8338 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008339# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008340
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 x = TEXT_X(col);
8342 y = TEXT_Y(row);
8343 w = gui.char_width * 2;
8344 h = gui.char_height;
8345 switch (sign->uType)
8346 {
8347 case IMAGE_BITMAP:
8348 {
8349 HDC hdcMem;
8350 HBITMAP hbmpOld;
8351
8352 hdcMem = CreateCompatibleDC(s_hdc);
8353 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8354 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8355 SelectObject(hdcMem, hbmpOld);
8356 DeleteDC(hdcMem);
8357 }
8358 break;
8359 case IMAGE_ICON:
8360 case IMAGE_CURSOR:
8361 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8362 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008363# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 case IMAGE_XPM:
8365 {
8366 HDC hdcMem;
8367 HBITMAP hbmpOld;
8368
8369 hdcMem = CreateCompatibleDC(s_hdc);
8370 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008371 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8373
8374 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008375 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8377 SelectObject(hdcMem, hbmpOld);
8378 DeleteDC(hdcMem);
8379 }
8380 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383}
8384
8385 static void
8386close_signicon_image(signicon_t *sign)
8387{
8388 if (sign)
8389 switch (sign->uType)
8390 {
8391 case IMAGE_BITMAP:
8392 DeleteObject((HGDIOBJ)sign->hImage);
8393 break;
8394 case IMAGE_CURSOR:
8395 DestroyCursor((HCURSOR)sign->hImage);
8396 break;
8397 case IMAGE_ICON:
8398 DestroyIcon((HICON)sign->hImage);
8399 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008400# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 case IMAGE_XPM:
8402 DeleteObject((HBITMAP)sign->hImage);
8403 DeleteObject((HBITMAP)sign->hShape);
8404 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407}
8408
8409 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008410gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 signicon_t sign, *psign;
8413 char_u *ext;
8414
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008416 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (ext > signfile)
8418 {
8419 int do_load = 1;
8420
8421 if (!STRICMP(ext, ".bmp"))
8422 sign.uType = IMAGE_BITMAP;
8423 else if (!STRICMP(ext, ".ico"))
8424 sign.uType = IMAGE_ICON;
8425 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8426 sign.uType = IMAGE_CURSOR;
8427 else
8428 do_load = 0;
8429
8430 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008431 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 gui.char_width * 2, gui.char_height,
8433 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008434# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (!STRICMP(ext, ".xpm"))
8436 {
8437 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008438 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8439 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443
8444 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008445 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 *psign = sign;
8447
8448 if (!psign)
8449 {
8450 if (sign.hImage)
8451 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008452 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 }
8454 return (void *)psign;
8455
8456}
8457
8458 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008459gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 if (sign)
8462 {
8463 close_signicon_image((signicon_t *)sign);
8464 vim_free(sign);
8465 }
8466}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008469#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
Bram Moolenaar734a8672019-12-02 22:49:38 +01008471/*
8472 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008473 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008475 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8477 * to get current mouse position).
8478 *
8479 * Trying to use as more Windows services as possible, and as less
8480 * IE version as possible :)).
8481 *
8482 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8483 * BalloonEval struct.
8484 * 2) Enable/Disable simply create/kill BalloonEval Timer
8485 * 3) When there was enough inactivity, timer procedure posts
8486 * async request to debugger
8487 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8488 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008489 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 */
8491
Bram Moolenaar45360022005-07-21 21:08:21 +00008492/*
8493 * determine whether installed Common Controls support multiline tooltips
8494 * (i.e. their version is >= 4.70
8495 */
8496 int
8497multiline_balloon_available(void)
8498{
8499 HINSTANCE hDll;
8500 static char comctl_dll[] = "comctl32.dll";
8501 static int multiline_tip = MAYBE;
8502
8503 if (multiline_tip != MAYBE)
8504 return multiline_tip;
8505
8506 hDll = GetModuleHandle(comctl_dll);
8507 if (hDll != NULL)
8508 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008509 DLLGETVERSIONPROC pGetVer;
8510 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008511
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008512 if (pGetVer != NULL)
8513 {
8514 DLLVERSIONINFO dvi;
8515 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008516
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008517 ZeroMemory(&dvi, sizeof(dvi));
8518 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008519
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008520 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008521
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008522 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008523 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008524 || (dvi.dwMajorVersion == 4
8525 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008526 {
8527 multiline_tip = TRUE;
8528 return multiline_tip;
8529 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008530 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008531 else
8532 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008533 // there is chance we have ancient CommCtl 4.70
8534 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008535 DWORD dwHandle = 0;
8536 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8537 if (len > 0)
8538 {
8539 VS_FIXEDFILEINFO *ver;
8540 UINT vlen = 0;
8541 void *data = alloc(len);
8542
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008543 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008544 && GetFileVersionInfo(comctl_dll, 0, len, data)
8545 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8546 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008547 && HIWORD(ver->dwFileVersionMS) > 4)
8548 || ((HIWORD(ver->dwFileVersionMS) == 4
8549 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008550 {
8551 vim_free(data);
8552 multiline_tip = TRUE;
8553 return multiline_tip;
8554 }
8555 vim_free(data);
8556 }
8557 }
8558 }
8559 multiline_tip = FALSE;
8560 return multiline_tip;
8561}
8562
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008563 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008564make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008565{
8566 TOOLINFOW *pti;
8567 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008568
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008569 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008570 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8571 else
8572 ToolInfoSize = sizeof(TOOLINFOW);
8573
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008574 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008575 if (pti == NULL)
8576 return;
8577
8578 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8579 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8580 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008581 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008582
8583 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8584 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8585
8586 pti->cbSize = ToolInfoSize;
8587 pti->uFlags = TTF_SUBCLASS;
8588 pti->hwnd = beval->target;
8589 pti->hinst = 0; // Don't use string resources
8590 pti->uId = ID_BEVAL_TOOLTIP;
8591
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008592 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008593 {
8594 RECT rect;
8595 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8596 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008597 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8598 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008599 // switch multiline tooltips on
8600 if (GetClientRect(s_textArea, &rect))
8601 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8602 (LPARAM)rect.right);
8603 }
8604 else
8605 {
8606 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008607 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8608 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008609 }
8610
8611 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8612 pti->rect.left = pt.x - 3;
8613 pti->rect.top = pt.y - 3;
8614 pti->rect.right = pt.x + 3;
8615 pti->rect.bottom = pt.y + 3;
8616
8617 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8618 // Make tooltip appear sooner.
8619 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8620 // I've performed some tests and it seems the longest possible life time
8621 // of tooltip is 30 seconds.
8622 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8623 /*
8624 * HACK: force tooltip to appear, because it'll not appear until
8625 * first mouse move. D*mn M$
8626 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8627 */
8628 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8629 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8630 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008631}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008634delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008636 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637}
8638
8639 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008640BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008641 HWND hwnd UNUSED,
8642 UINT uMsg UNUSED,
8643 UINT_PTR idEvent UNUSED,
8644 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 POINT pt;
8647 RECT rect;
8648
8649 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8650 return;
8651
8652 GetCursorPos(&pt);
8653 if (WindowFromPoint(pt) != s_textArea)
8654 return;
8655
8656 ScreenToClient(s_textArea, &pt);
8657 GetClientRect(s_textArea, &rect);
8658 if (!PtInRect(&rect, pt))
8659 return;
8660
8661 if (LastActivity > 0
8662 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8663 && (cur_beval->showState != ShS_PENDING
8664 || abs(cur_beval->x - pt.x) > 3
8665 || abs(cur_beval->y - pt.y) > 3))
8666 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008667 // Pointer resting in one place long enough, it's time to show
8668 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 cur_beval->showState = ShS_PENDING;
8670 cur_beval->x = pt.x;
8671 cur_beval->y = pt.y;
8672
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008673 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
8675 if (cur_beval->msgCB != NULL)
8676 (*cur_beval->msgCB)(cur_beval, 0);
8677 }
8678}
8679
8680 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008681gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008683 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008685 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686}
8687
8688 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008689gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008691 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 if (beval == NULL)
8693 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008694 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008695 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008696 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
8699 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008700gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008703
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008704 vim_free(beval->msg);
8705 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8706 if (beval->msg == NULL)
8707 {
8708 delete_tooltip(beval);
8709 beval->showState = ShS_NEUTRAL;
8710 return;
8711 }
8712
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008713 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 if (beval->showState == ShS_SHOWING)
8715 return;
8716 GetCursorPos(&pt);
8717 ScreenToClient(s_textArea, &pt);
8718
8719 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008721 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 gui_mch_disable_beval_area(cur_beval);
8723 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008724 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008726 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727}
8728
8729 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008730gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008731 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008732 char_u *mesg,
8733 void (*mesgCB)(BalloonEval *, int),
8734 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008736 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 BalloonEval *beval;
8738
8739 if (mesg != NULL && mesgCB != NULL)
8740 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008741 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 return NULL;
8743 }
8744
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008745 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 if (beval != NULL)
8747 {
8748 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749
8750 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 beval->msg = mesg;
8752 beval->msgCB = mesgCB;
8753 beval->clientData = clientData;
8754
8755 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 cur_beval = beval;
8757
8758 if (p_beval)
8759 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 }
8761 return beval;
8762}
8763
8764 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008765Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008767 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 return;
8769
8770 if (cur_beval != NULL)
8771 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008772 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008774 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008775 // TRACE0("TTN_SHOW {{{");
8776 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008777 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008778 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008779 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 delete_tooltip(cur_beval);
8781 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008782 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008785 break;
8786 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008787 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008788 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008789 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8790 info->lpszText = (LPSTR)info->lParam;
8791 info->uFlags |= TTF_DI_SETITEM;
8792 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008793 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008794 case TTN_GETDISPINFOW:
8795 {
8796 // if we get here then we have new common controls
8797 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8798 info->lpszText = (LPWSTR)info->lParam;
8799 info->uFlags |= TTF_DI_SETITEM;
8800 }
8801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
8803 }
8804}
8805
8806 static void
8807TrackUserActivity(UINT uMsg)
8808{
8809 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8810 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8811 LastActivity = GetTickCount();
8812}
8813
8814 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008815gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008817# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008818 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008819# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008820 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 vim_free(beval);
8822}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008823#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
8825#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8826/*
8827 * We have multiple signs to draw at the same location. Draw the
8828 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8829 */
8830 void
8831netbeans_draw_multisign_indicator(int row)
8832{
8833 int i;
8834 int y;
8835 int x;
8836
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008837 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008838 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 x = 0;
8841 y = TEXT_Y(row);
8842
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008843# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008844 if (IS_ENABLE_DIRECTX())
8845 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008846# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008847
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 for (i = 0; i < gui.char_height - 3; i++)
8849 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8850
8851 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8852 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8853 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8854 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8855 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8856 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8857 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8858}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008859#endif