blob: 4f047f15a5bf848ed092afc0716bd10d30de7cd7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000043#else
44# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010045#endif
46
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020047#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
48 int
49gui_mch_set_rendering_options(char_u *s)
50{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010051# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020052 char_u *p, *q;
53
54 int dx_enable = 0;
55 int dx_flags = 0;
56 float dx_gamma = 0.0f;
57 float dx_contrast = 0.0f;
58 float dx_level = 0.0f;
59 int dx_geom = 0;
60 int dx_renmode = 0;
61 int dx_taamode = 0;
62
Bram Moolenaar734a8672019-12-02 22:49:38 +010063 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100124 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100132
Bram Moolenaar734a8672019-12-02 22:49:38 +0100133 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100163# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183
Bram Moolenaar734a8672019-12-02 22:49:38 +0100184#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100185#ifdef DEBUG
186# include <tchar.h>
187#endif
188
Bram Moolenaar734a8672019-12-02 22:49:38 +0100189// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100190#ifndef PROTO
191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100192# ifndef __MINGW32__
193# include <shellapi.h>
194# endif
195# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
196# include <commctrl.h>
197# endif
198# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100199
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100200# ifdef GLOBAL_IME
201# include "glbl_ime.h"
202# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
213#define DLG_OLD_STYLE_PADDING_X 5
214#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100215#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100216#define DLG_VERT_PADDING_Y 4
217#define DLG_ICON_WIDTH 34
218#define DLG_ICON_HEIGHT 34
219#define DLG_MIN_WIDTH 150
220#define DLG_FONT_NAME "MS Sans Serif"
221#define DLG_FONT_POINT_SIZE 8
222#define DLG_MIN_MAX_WIDTH 400
223#define DLG_MIN_MAX_HEIGHT 400
224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226
Bram Moolenaar734a8672019-12-02 22:49:38 +0100227#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100228# define WM_XBUTTONDOWN 0x020B
229# define WM_XBUTTONUP 0x020C
230# define WM_XBUTTONDBLCLK 0x020D
231# define MK_XBUTTON1 0x0020
232# define MK_XBUTTON2 0x0040
233#endif
234
K.Takatac81e9bf2022-01-16 14:15:49 +0000235#ifndef WM_DPICHANGED
236# define WM_DPICHANGED 0x02E0
237#endif
238
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100239#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241 * Define a few things for generating prototypes. This is just to avoid
242 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100244# define APIENTRY
245# define CALLBACK
246# define CONST
247# define FAR
248# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200249# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200250# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100251# define _cdecl
252typedef int BOOL;
253typedef int BYTE;
254typedef int DWORD;
255typedef int WCHAR;
256typedef int ENUMLOGFONT;
257typedef int FINDREPLACE;
258typedef int HANDLE;
259typedef int HBITMAP;
260typedef int HBRUSH;
261typedef int HDROP;
262typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100263typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100264typedef int LPARAM;
265typedef int LPCREATESTRUCT;
266typedef int LPCSTR;
267typedef int LPCTSTR;
268typedef int LPRECT;
269typedef int LPSTR;
270typedef int LPWINDOWPOS;
271typedef int LPWORD;
272typedef int LRESULT;
273typedef int HRESULT;
274# undef MSG
275typedef int MSG;
276typedef int NEWTEXTMETRIC;
277typedef int OSVERSIONINFO;
278typedef int PWORD;
279typedef int RECT;
280typedef int UINT;
281typedef int WORD;
282typedef int WPARAM;
283typedef int POINT;
284typedef void *HINSTANCE;
285typedef void *HMENU;
286typedef void *HWND;
287typedef void *HDC;
288typedef void VOID;
289typedef int LPNMHDR;
290typedef int LONG;
291typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200292typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200293typedef int COLORREF;
294typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100295#endif
296
297#ifndef GET_X_LPARAM
298# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
299#endif
300
301static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100302static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303static void clear_rect(RECT *rcp);
304
Bram Moolenaar734a8672019-12-02 22:49:38 +0100305static WORD s_dlgfntheight; // height of the dialog font
306static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307
308#ifdef FEAT_MENU
309static HMENU s_menuBar = NULL;
310#endif
311#ifdef FEAT_TEAROFF
312static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100313static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314#endif
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316// Flag that is set while processing a message that must not be interrupted by
317// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static int s_busy_processing = FALSE;
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321
322#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000323static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200324static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200326static int s_findrep_is_find; // TRUE for find dialog, FALSE
327 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100328#endif
329
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330HWND s_hwnd = NULL;
331static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100332static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100333
334#ifdef FEAT_TOOLBAR
335static HWND s_toolbarhwnd = NULL;
336static WNDPROC s_toolbar_wndproc = NULL;
337#endif
338
339#ifdef FEAT_GUI_TABLINE
340static HWND s_tabhwnd = NULL;
341static WNDPROC s_tabline_wndproc = NULL;
342static int showing_tabline = 0;
343#endif
344
345static WPARAM s_wParam = 0;
346static LPARAM s_lParam = 0;
347
348static HWND s_textArea = NULL;
349static UINT s_uMsg = 0;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100352
353static int s_need_activate = FALSE;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355// This variable is set when waiting for an event, which is the only moment
356// scrollbar dragging can be done directly. It's not allowed while commands
357// are executed, because it may move the cursor and that may cause unexpected
358// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100359static int allow_scrollbar = FALSE;
360
361#ifdef GLOBAL_IME
362# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
363#else
364# define MyTranslateMessage(x) TranslateMessage(x)
365#endif
366
K.Takatac81e9bf2022-01-16 14:15:49 +0000367#ifndef _DPI_AWARENESS_CONTEXTS_
368typedef HANDLE DPI_AWARENESS_CONTEXT;
369
370typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000371 DPI_AWARENESS_INVALID = -1,
372 DPI_AWARENESS_UNAWARE = 0,
373 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000374 DPI_AWARENESS_PER_MONITOR_AWARE = 2
375} DPI_AWARENESS;
376
K.Takatab0b2b732022-01-19 12:59:21 +0000377# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
378# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000379# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
380# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
381# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
382#endif
383
384#define DEFAULT_DPI 96
385static int s_dpi = DEFAULT_DPI;
386static BOOL s_in_dpichanged = FALSE;
387static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
388
389static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
390static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
391static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
392//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
393static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
394static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
395
396 static UINT WINAPI
397stubGetDpiForSystem(void)
398{
399 HWND hwnd = GetDesktopWindow();
400 HDC hdc = GetWindowDC(hwnd);
401 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
402 ReleaseDC(hwnd, hdc);
403 return dpi;
404}
405
406 static int WINAPI
407stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
408{
409 return GetSystemMetrics(nIndex);
410}
411
412 static int
413adjust_fontsize_by_dpi(int size)
414{
415 return size * s_dpi / (int)pGetDpiForSystem();
416}
417
418 static int
419adjust_by_system_dpi(int size)
420{
421 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
422}
423
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100424#if defined(FEAT_DIRECTX)
425 static int
426directx_enabled(void)
427{
428 if (s_dwc != NULL)
429 return 1;
430 else if (s_directx_load_attempted)
431 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100432 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100433 DWrite_Init();
434 s_directx_load_attempted = 1;
435 s_dwc = DWriteContext_Open();
436 directx_binddc();
437 return s_dwc != NULL ? 1 : 0;
438}
439
440 static void
441directx_binddc(void)
442{
443 if (s_textArea != NULL)
444 {
445 RECT rect;
446 GetClientRect(s_textArea, &rect);
447 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
448 }
449}
450#endif
451
Bram Moolenaar734a8672019-12-02 22:49:38 +0100452// use of WindowProc depends on Global IME
Bram Moolenaar945c8572020-07-17 22:17:03 +0200453static LRESULT WINAPI MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100454
Bram Moolenaar734a8672019-12-02 22:49:38 +0100455extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100456
457static struct
458{
459 UINT key_sym;
460 char_u vim_code0;
461 char_u vim_code1;
462} special_keys[] =
463{
464 {VK_UP, 'k', 'u'},
465 {VK_DOWN, 'k', 'd'},
466 {VK_LEFT, 'k', 'l'},
467 {VK_RIGHT, 'k', 'r'},
468
469 {VK_F1, 'k', '1'},
470 {VK_F2, 'k', '2'},
471 {VK_F3, 'k', '3'},
472 {VK_F4, 'k', '4'},
473 {VK_F5, 'k', '5'},
474 {VK_F6, 'k', '6'},
475 {VK_F7, 'k', '7'},
476 {VK_F8, 'k', '8'},
477 {VK_F9, 'k', '9'},
478 {VK_F10, 'k', ';'},
479
480 {VK_F11, 'F', '1'},
481 {VK_F12, 'F', '2'},
482 {VK_F13, 'F', '3'},
483 {VK_F14, 'F', '4'},
484 {VK_F15, 'F', '5'},
485 {VK_F16, 'F', '6'},
486 {VK_F17, 'F', '7'},
487 {VK_F18, 'F', '8'},
488 {VK_F19, 'F', '9'},
489 {VK_F20, 'F', 'A'},
490
491 {VK_F21, 'F', 'B'},
492#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100493 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494#endif
495 {VK_F22, 'F', 'C'},
496 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100497 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100498
499 {VK_HELP, '%', '1'},
500 {VK_BACK, 'k', 'b'},
501 {VK_INSERT, 'k', 'I'},
502 {VK_DELETE, 'k', 'D'},
503 {VK_HOME, 'k', 'h'},
504 {VK_END, '@', '7'},
505 {VK_PRIOR, 'k', 'P'},
506 {VK_NEXT, 'k', 'N'},
507 {VK_PRINT, '%', '9'},
508 {VK_ADD, 'K', '6'},
509 {VK_SUBTRACT, 'K', '7'},
510 {VK_DIVIDE, 'K', '8'},
511 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100512 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100513 {VK_DECIMAL, 'K', 'B'},
514
515 {VK_NUMPAD0, 'K', 'C'},
516 {VK_NUMPAD1, 'K', 'D'},
517 {VK_NUMPAD2, 'K', 'E'},
518 {VK_NUMPAD3, 'K', 'F'},
519 {VK_NUMPAD4, 'K', 'G'},
520 {VK_NUMPAD5, 'K', 'H'},
521 {VK_NUMPAD6, 'K', 'I'},
522 {VK_NUMPAD7, 'K', 'J'},
523 {VK_NUMPAD8, 'K', 'K'},
524 {VK_NUMPAD9, 'K', 'L'},
525
Bram Moolenaar734a8672019-12-02 22:49:38 +0100526 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100527 {VK_SPACE, ' ', NUL},
528 {VK_TAB, TAB, NUL},
529 {VK_ESCAPE, ESC, NUL},
530 {NL, NL, NUL},
531 {CAR, CAR, NUL},
532
Bram Moolenaar734a8672019-12-02 22:49:38 +0100533 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100534 {0, 0, 0}
535};
536
Bram Moolenaar734a8672019-12-02 22:49:38 +0100537// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100538static int s_button_pending = -1;
539
Bram Moolenaar734a8672019-12-02 22:49:38 +0100540// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
541// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542static int s_getting_focus = FALSE;
543
544static int s_x_pending;
545static int s_y_pending;
546static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200547static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200549static int dead_key = 0; // 0: no dead key, 1: dead key pressed
550static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
551 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100552
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100553#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100554// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100555static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
556static void TrackUserActivity(UINT uMsg);
557#endif
558
559/*
560 * For control IME.
561 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100562 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100563 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100564#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100565// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100566static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100567#endif
568#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100569// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100570static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100571#endif
572
573#ifdef FEAT_MBYTE_IME
574static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
575#endif
576
577#if defined(FEAT_BROWSE)
578static char_u *convert_filter(char_u *s);
579#endif
580
581#ifdef DEBUG_PRINT_ERROR
582/*
583 * Print out the last Windows error message
584 */
585 static void
586print_windows_error(void)
587{
588 LPVOID lpMsgBuf;
589
590 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
591 NULL, GetLastError(),
592 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
593 (LPTSTR) &lpMsgBuf, 0, NULL);
594 TRACE1("Error: %s\n", lpMsgBuf);
595 LocalFree(lpMsgBuf);
596}
597#endif
598
599/*
600 * Cursor blink functions.
601 *
602 * This is a simple state machine:
603 * BLINK_NONE not blinking at all
604 * BLINK_OFF blinking, cursor is not shown
605 * BLINK_ON blinking, cursor is shown
606 */
607
608#define BLINK_NONE 0
609#define BLINK_OFF 1
610#define BLINK_ON 2
611
612static int blink_state = BLINK_NONE;
613static long_u blink_waittime = 700;
614static long_u blink_ontime = 400;
615static long_u blink_offtime = 250;
616static UINT blink_timer = 0;
617
Bram Moolenaar703a8042016-06-04 16:24:32 +0200618 int
619gui_mch_is_blinking(void)
620{
621 return blink_state != BLINK_NONE;
622}
623
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200624 int
625gui_mch_is_blink_off(void)
626{
627 return blink_state == BLINK_OFF;
628}
629
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100630 void
631gui_mch_set_blinking(long wait, long on, long off)
632{
633 blink_waittime = wait;
634 blink_ontime = on;
635 blink_offtime = off;
636}
637
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 static VOID CALLBACK
639_OnBlinkTimer(
640 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100641 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100642 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100643 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100644{
645 MSG msg;
646
647 /*
648 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
649 */
650
651 KillTimer(NULL, idEvent);
652
Bram Moolenaar734a8672019-12-02 22:49:38 +0100653 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100654 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
655 ;
656
657 if (blink_state == BLINK_ON)
658 {
659 gui_undraw_cursor();
660 blink_state = BLINK_OFF;
661 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
662 (TIMERPROC)_OnBlinkTimer);
663 }
664 else
665 {
666 gui_update_cursor(TRUE, FALSE);
667 blink_state = BLINK_ON;
668 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100669 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100671 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672}
673
674 static void
675gui_mswin_rm_blink_timer(void)
676{
677 MSG msg;
678
679 if (blink_timer != 0)
680 {
681 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
684 ;
685 blink_timer = 0;
686 }
687}
688
689/*
690 * Stop the cursor blinking. Show the cursor if it wasn't shown.
691 */
692 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100693gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100694{
695 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100696 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100697 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100699 gui_mch_flush();
700 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 blink_state = BLINK_NONE;
702}
703
704/*
705 * Start the cursor blinking. If it was already blinking, this restarts the
706 * waiting time and shows the cursor.
707 */
708 void
709gui_mch_start_blink(void)
710{
711 gui_mswin_rm_blink_timer();
712
Bram Moolenaar734a8672019-12-02 22:49:38 +0100713 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
715 {
716 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
717 (TIMERPROC)_OnBlinkTimer);
718 blink_state = BLINK_ON;
719 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100720 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100721 }
722}
723
724/*
725 * Call-back routines.
726 */
727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100728 static VOID CALLBACK
729_OnTimer(
730 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100731 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100732 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100733 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100734{
735 MSG msg;
736
737 /*
738 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
739 */
740 KillTimer(NULL, idEvent);
741 s_timed_out = TRUE;
742
Bram Moolenaar734a8672019-12-02 22:49:38 +0100743 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
745 ;
746 if (idEvent == s_wait_timer)
747 s_wait_timer = 0;
748}
749
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100750 static void
751_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100752 HWND hwnd UNUSED,
753 UINT ch UNUSED,
754 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755{
756 dead_key = 1;
757}
758
759/*
760 * Convert Unicode character "ch" to bytes in "string[slen]".
761 * When "had_alt" is TRUE the ALT key was included in "ch".
762 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200763 * Because the Windows API uses UTF-16, we have to deal with surrogate
764 * pairs; this is where we choose to deal with them: if "ch" is a high
765 * surrogate, it will be stored, and the length returned will be zero; the next
766 * char_to_string call will then include the high surrogate, decoding the pair
767 * of UTF-16 code units to a single Unicode code point, presuming it is the
768 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100769 */
770 static int
771char_to_string(int ch, char_u *string, int slen, int had_alt)
772{
773 int len;
774 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200776 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100777
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200778 if (surrogate_pending_ch != 0)
779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // We don't guarantee ch is a low surrogate to match the high surrogate
781 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200782 wstring[0] = surrogate_pending_ch;
783 wstring[1] = ch;
784 surrogate_pending_ch = 0;
785 len = 2;
786 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100787 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200788 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100789 // We don't have the entire code point yet, only the first UTF-16 code
790 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200791 surrogate_pending_ch = ch;
792 return 0;
793 }
794 else
795 {
796 wstring[0] = ch;
797 len = 1;
798 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799
Bram Moolenaar734a8672019-12-02 22:49:38 +0100800 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
801 // "enc_codepage" is non-zero use the standard Win32 function,
802 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200803 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100804 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200805 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
806 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100807 // If we had included the ALT key into the character but now the
808 // upper bit is no longer set, that probably means the conversion
809 // failed. Convert the original character and set the upper bit
810 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200811 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 wstring[0] = ch & 0x7f;
814 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
815 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100816 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200817 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100818 }
819 }
820 else
821 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200822 ws = utf16_to_enc(wstring, &len);
823 if (ws == NULL)
824 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825 else
826 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100827 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200828 len = slen;
829 mch_memmove(string, ws, len);
830 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100831 }
832 }
833
834 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100835 {
836 string[0] = ch;
837 len = 1;
838 }
839
840 for (i = 0; i < len; ++i)
841 if (string[i] == CSI && len <= slen - 2)
842 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100843 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
845 string[++i] = KS_EXTRA;
846 string[++i] = (int)KE_CSI;
847 len += 2;
848 }
849
850 return len;
851}
852
853/*
854 * Key hit, add it to the input buffer.
855 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100856 static void
857_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100858 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100859 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100860 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861{
862 char_u string[40];
863 int len = 0;
864
865 dead_key = 0;
866
867 len = char_to_string(ch, string, 40, FALSE);
868 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
869 {
870 trash_input_buf();
871 got_int = TRUE;
872 }
873
874 add_to_input_buf(string, len);
875}
876
877/*
878 * Alt-Key hit, add it to the input buffer.
879 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880 static void
881_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100882 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100883 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100884 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100885{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100886 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100887 int len;
888 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100889 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100890
891 dead_key = 0;
892
Bram Moolenaar734a8672019-12-02 22:49:38 +0100893 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100894
Bram Moolenaar734a8672019-12-02 22:49:38 +0100895 // OK, we have a character key (given by ch) which was entered with the
896 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
897 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
898 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100899 modifiers = MOD_MASK_ALT;
900 if (GetKeyState(VK_SHIFT) & 0x8000)
901 modifiers |= MOD_MASK_SHIFT;
902 if (GetKeyState(VK_CONTROL) & 0x8000)
903 modifiers |= MOD_MASK_CTRL;
904
905 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100906 // remove the SHIFT modifier for keys where it's already included, e.g.,
907 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200908 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200910 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
911 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100912 if (ch == CSI)
913 ch = K_CSI;
914
915 len = 0;
916 if (modifiers)
917 {
918 string[len++] = CSI;
919 string[len++] = KS_MODIFIER;
920 string[len++] = modifiers;
921 }
922
923 if (IS_SPECIAL((int)ch))
924 {
925 string[len++] = CSI;
926 string[len++] = K_SECOND((int)ch);
927 string[len++] = K_THIRD((int)ch);
928 }
929 else
930 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100931 // Although the documentation isn't clear about it, we assume "ch" is
932 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933 len += char_to_string(ch, string + len, 40 - len, TRUE);
934 }
935
936 add_to_input_buf(string, len);
937}
938
939 static void
940_OnMouseEvent(
941 int button,
942 int x,
943 int y,
944 int repeated_click,
945 UINT keyFlags)
946{
947 int vim_modifiers = 0x0;
948
949 s_getting_focus = FALSE;
950
951 if (keyFlags & MK_SHIFT)
952 vim_modifiers |= MOUSE_SHIFT;
953 if (keyFlags & MK_CONTROL)
954 vim_modifiers |= MOUSE_CTRL;
955 if (GetKeyState(VK_MENU) & 0x8000)
956 vim_modifiers |= MOUSE_ALT;
957
958 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
959}
960
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100961 static void
962_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100963 HWND hwnd UNUSED,
964 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100965 int x,
966 int y,
967 UINT keyFlags)
968{
969 static LONG s_prevTime = 0;
970
971 LONG currentTime = GetMessageTime();
972 int button = -1;
973 int repeated_click;
974
Bram Moolenaar734a8672019-12-02 22:49:38 +0100975 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100976 (void)SetFocus(s_hwnd);
977
978 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
979 button = MOUSE_LEFT;
980 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
981 button = MOUSE_MIDDLE;
982 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
983 button = MOUSE_RIGHT;
984 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
985 {
986#ifndef GET_XBUTTON_WPARAM
987# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
988#endif
989 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
990 }
991 else if (s_uMsg == WM_CAPTURECHANGED)
992 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100993 // on W95/NT4, somehow you get in here with an odd Msg
994 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100995 if (s_button_pending == MOUSE_LEFT)
996 button = MOUSE_RIGHT;
997 else
998 button = MOUSE_LEFT;
999 }
1000 if (button >= 0)
1001 {
1002 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1003
1004 /*
1005 * Holding down the left and right buttons simulates pushing the middle
1006 * button.
1007 */
1008 if (repeated_click
1009 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1010 || (button == MOUSE_RIGHT
1011 && s_button_pending == MOUSE_LEFT)))
1012 {
1013 /*
1014 * Hmm, gui.c will ignore more than one button down at a time, so
1015 * pretend we let go of it first.
1016 */
1017 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1018 button = MOUSE_MIDDLE;
1019 repeated_click = FALSE;
1020 s_button_pending = -1;
1021 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1022 }
1023 else if ((repeated_click)
1024 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1025 {
1026 if (s_button_pending > -1)
1027 {
1028 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1029 s_button_pending = -1;
1030 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001031 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001032 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1033 }
1034 else
1035 {
1036 /*
1037 * If this is the first press (i.e. not a multiple click) don't
1038 * action immediately, but store and wait for:
1039 * i) button-up
1040 * ii) mouse move
1041 * iii) another button press
1042 * before using it.
1043 * This enables us to make left+right simulate middle button,
1044 * without left or right being actioned first. The side-effect is
1045 * that if you click and hold the mouse without dragging, the
1046 * cursor doesn't move until you release the button. In practice
1047 * this is hardly a problem.
1048 */
1049 s_button_pending = button;
1050 s_x_pending = x;
1051 s_y_pending = y;
1052 s_kFlags_pending = keyFlags;
1053 }
1054
1055 s_prevTime = currentTime;
1056 }
1057}
1058
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001059 static void
1060_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001061 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062 int x,
1063 int y,
1064 UINT keyFlags)
1065{
1066 int button;
1067
1068 s_getting_focus = FALSE;
1069 if (s_button_pending > -1)
1070 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001071 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001072 _OnMouseEvent(s_button_pending, s_x_pending,
1073 s_y_pending, FALSE, s_kFlags_pending);
1074 s_button_pending = -1;
1075 }
1076 if (s_uMsg == WM_MOUSEMOVE)
1077 {
1078 /*
1079 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1080 * down.
1081 */
1082 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1083 | MK_XBUTTON1 | MK_XBUTTON2)))
1084 {
1085 gui_mouse_moved(x, y);
1086 return;
1087 }
1088
1089 /*
1090 * While button is down, keep grabbing mouse move events when
1091 * the mouse goes outside the window
1092 */
1093 SetCapture(s_textArea);
1094 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001095 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 }
1097 else
1098 {
1099 ReleaseCapture();
1100 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001101 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102 }
1103
1104 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1105}
1106
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001107 static void
1108_OnSizeTextArea(
1109 HWND hwnd UNUSED,
1110 UINT state UNUSED,
1111 int cx UNUSED,
1112 int cy UNUSED)
1113{
1114#if defined(FEAT_DIRECTX)
1115 if (IS_ENABLE_DIRECTX())
1116 directx_binddc();
1117#endif
1118}
1119
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120#ifdef FEAT_MENU
1121/*
1122 * Find the vimmenu_T with the given id
1123 */
1124 static vimmenu_T *
1125gui_mswin_find_menu(
1126 vimmenu_T *pMenu,
1127 int id)
1128{
1129 vimmenu_T *pChildMenu;
1130
1131 while (pMenu)
1132 {
1133 if (pMenu->id == (UINT)id)
1134 break;
1135 if (pMenu->children != NULL)
1136 {
1137 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1138 if (pChildMenu)
1139 {
1140 pMenu = pChildMenu;
1141 break;
1142 }
1143 }
1144 pMenu = pMenu->next;
1145 }
1146 return pMenu;
1147}
1148
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001149 static void
1150_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001151 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001153 HWND hwndCtl UNUSED,
1154 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001155{
1156 vimmenu_T *pMenu;
1157
1158 pMenu = gui_mswin_find_menu(root_menu, id);
1159 if (pMenu)
1160 gui_menu_cb(pMenu);
1161}
1162#endif
1163
1164#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001165/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001166 * Handle a Find/Replace window message.
1167 */
1168 static void
1169_OnFindRepl(void)
1170{
1171 int flags = 0;
1172 int down;
1173
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001174 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001176 (void)SetFocus(s_hwnd);
1177
1178 if (s_findrep_struct.Flags & FR_FINDNEXT)
1179 {
1180 flags = FRD_FINDNEXT;
1181
Bram Moolenaar734a8672019-12-02 22:49:38 +01001182 // Give main window the focus back: this is so the cursor isn't
1183 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 (void)SetFocus(s_hwnd);
1185 }
1186 else if (s_findrep_struct.Flags & FR_REPLACE)
1187 {
1188 flags = FRD_REPLACE;
1189
Bram Moolenaar734a8672019-12-02 22:49:38 +01001190 // Give main window the focus back: this is so the cursor isn't
1191 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 (void)SetFocus(s_hwnd);
1193 }
1194 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1195 {
1196 flags = FRD_REPLACEALL;
1197 }
1198
1199 if (flags != 0)
1200 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001201 char_u *p, *q;
1202
Bram Moolenaar734a8672019-12-02 22:49:38 +01001203 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001204 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1205 flags |= FRD_WHOLE_WORD;
1206 if (s_findrep_struct.Flags & FR_MATCHCASE)
1207 flags |= FRD_MATCH_CASE;
1208 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001209 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1210 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1211 if (p != NULL && q != NULL)
1212 gui_do_findrepl(flags, p, q, down);
1213 vim_free(p);
1214 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001215 }
1216}
1217#endif
1218
1219 static void
1220HandleMouseHide(UINT uMsg, LPARAM lParam)
1221{
1222 static LPARAM last_lParam = 0L;
1223
Bram Moolenaar734a8672019-12-02 22:49:38 +01001224 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001225 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1226 {
1227 if (lParam == last_lParam)
1228 return;
1229 last_lParam = lParam;
1230 }
1231
Bram Moolenaar734a8672019-12-02 22:49:38 +01001232 // Handle specially, to centralise coding. We need to be sure we catch all
1233 // possible events which should cause us to restore the cursor (as it is a
1234 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 switch (uMsg)
1236 {
1237 case WM_KEYUP:
1238 case WM_CHAR:
1239 /*
1240 * blank out the pointer if necessary
1241 */
1242 if (p_mh)
1243 gui_mch_mousehide(TRUE);
1244 break;
1245
Bram Moolenaar734a8672019-12-02 22:49:38 +01001246 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001247 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001248 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249 case WM_LBUTTONDOWN:
1250 case WM_LBUTTONUP:
1251 case WM_MBUTTONDOWN:
1252 case WM_MBUTTONUP:
1253 case WM_RBUTTONDOWN:
1254 case WM_RBUTTONUP:
1255 case WM_XBUTTONDOWN:
1256 case WM_XBUTTONUP:
1257 case WM_NCMOUSEMOVE:
1258 case WM_NCLBUTTONDOWN:
1259 case WM_NCLBUTTONUP:
1260 case WM_NCMBUTTONDOWN:
1261 case WM_NCMBUTTONUP:
1262 case WM_NCRBUTTONDOWN:
1263 case WM_NCRBUTTONUP:
1264 case WM_KILLFOCUS:
1265 /*
1266 * if the pointer is currently hidden, then we should show it.
1267 */
1268 gui_mch_mousehide(FALSE);
1269 break;
1270 }
1271}
1272
1273 static LRESULT CALLBACK
1274_TextAreaWndProc(
1275 HWND hwnd,
1276 UINT uMsg,
1277 WPARAM wParam,
1278 LPARAM lParam)
1279{
1280 /*
1281 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1282 hwnd, uMsg, wParam, lParam);
1283 */
1284
1285 HandleMouseHide(uMsg, lParam);
1286
1287 s_uMsg = uMsg;
1288 s_wParam = wParam;
1289 s_lParam = lParam;
1290
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001291#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001292 TrackUserActivity(uMsg);
1293#endif
1294
1295 switch (uMsg)
1296 {
1297 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1298 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1299 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1300 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1301 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1302 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1303 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1304 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1305 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1306 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1307 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1308 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1309 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1310 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001311 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001312
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001313#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001314 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1315 return TRUE;
1316#endif
1317 default:
1318 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1319 }
1320}
1321
Bram Moolenaar945c8572020-07-17 22:17:03 +02001322 static LRESULT WINAPI
1323MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001324{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001325#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001326 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001327#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001328 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001329#endif
1330}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331
1332/*
1333 * Called when the foreground or background color has been changed.
1334 */
1335 void
1336gui_mch_new_colors(void)
1337{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001338 HBRUSH prevBrush;
1339
1340 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001341 prevBrush = (HBRUSH)SetClassLongPtr(
1342 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001343 InvalidateRect(s_hwnd, NULL, TRUE);
1344 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001345}
1346
1347/*
1348 * Set the colors to their default values.
1349 */
1350 void
1351gui_mch_def_colors(void)
1352{
1353 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1354 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1355 gui.def_norm_pixel = gui.norm_pixel;
1356 gui.def_back_pixel = gui.back_pixel;
1357}
1358
1359/*
1360 * Open the GUI window which was created by a call to gui_mch_init().
1361 */
1362 int
1363gui_mch_open(void)
1364{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001365 // Actually open the window, if not already visible
1366 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001367 if (!IsWindowVisible(s_hwnd))
1368 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1369
1370#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001371 // Init replace string here, so that we keep it when re-opening the
1372 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001373 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1374#endif
1375
1376 return OK;
1377}
1378
1379/*
1380 * Get the position of the top left corner of the window.
1381 */
1382 int
1383gui_mch_get_winpos(int *x, int *y)
1384{
1385 RECT rect;
1386
1387 GetWindowRect(s_hwnd, &rect);
1388 *x = rect.left;
1389 *y = rect.top;
1390 return OK;
1391}
1392
1393/*
1394 * Set the position of the top left corner of the window to the given
1395 * coordinates.
1396 */
1397 void
1398gui_mch_set_winpos(int x, int y)
1399{
1400 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1401 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1402}
1403 void
1404gui_mch_set_text_area_pos(int x, int y, int w, int h)
1405{
1406 static int oldx = 0;
1407 static int oldy = 0;
1408
1409 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1410
1411#ifdef FEAT_TOOLBAR
1412 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1413 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001414 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001415#endif
1416#if defined(FEAT_GUI_TABLINE)
1417 if (showing_tabline)
1418 {
1419 int top = 0;
1420 RECT rect;
1421
1422# ifdef FEAT_TOOLBAR
1423 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001424 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001425# endif
1426 GetClientRect(s_hwnd, &rect);
1427 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1428 }
1429#endif
1430
Bram Moolenaar734a8672019-12-02 22:49:38 +01001431 // When side scroll bar is unshown, the size of window will change.
1432 // then, the text area move left or right. thus client rect should be
1433 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001434 if (oldx != x || oldy != y)
1435 {
1436 InvalidateRect(s_hwnd, NULL, FALSE);
1437 oldx = x;
1438 oldy = y;
1439 }
1440}
1441
1442
1443/*
1444 * Scrollbar stuff:
1445 */
1446
1447 void
1448gui_mch_enable_scrollbar(
1449 scrollbar_T *sb,
1450 int flag)
1451{
1452 ShowScrollBar(sb->id, SB_CTL, flag);
1453
Bram Moolenaar734a8672019-12-02 22:49:38 +01001454 // TODO: When the window is maximized, the size of the window stays the
1455 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1456 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001457}
1458
1459 void
1460gui_mch_set_scrollbar_pos(
1461 scrollbar_T *sb,
1462 int x,
1463 int y,
1464 int w,
1465 int h)
1466{
1467 SetWindowPos(sb->id, NULL, x, y, w, h,
1468 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1469}
1470
Bram Moolenaar203ec772020-07-17 20:43:43 +02001471 int
1472gui_mch_get_scrollbar_xpadding(void)
1473{
1474 RECT rcTxt, rcWnd;
1475 int xpad;
1476
1477 GetWindowRect(s_textArea, &rcTxt);
1478 GetWindowRect(s_hwnd, &rcWnd);
1479 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001480 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1481 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001482 return (xpad < 0) ? 0 : xpad;
1483}
1484
1485 int
1486gui_mch_get_scrollbar_ypadding(void)
1487{
1488 RECT rcTxt, rcWnd;
1489 int ypad;
1490
1491 GetWindowRect(s_textArea, &rcTxt);
1492 GetWindowRect(s_hwnd, &rcWnd);
1493 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001494 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1495 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001496 return (ypad < 0) ? 0 : ypad;
1497}
1498
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001499 void
1500gui_mch_create_scrollbar(
1501 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001502 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001503{
1504 sb->id = CreateWindow(
1505 "SCROLLBAR", "Scrollbar",
1506 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001507 10, // Any value will do for now
1508 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001509 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001510 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001511}
1512
1513/*
1514 * Find the scrollbar with the given hwnd.
1515 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001516 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001517gui_mswin_find_scrollbar(HWND hwnd)
1518{
1519 win_T *wp;
1520
1521 if (gui.bottom_sbar.id == hwnd)
1522 return &gui.bottom_sbar;
1523 FOR_ALL_WINDOWS(wp)
1524 {
1525 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1526 return &wp->w_scrollbars[SBAR_LEFT];
1527 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1528 return &wp->w_scrollbars[SBAR_RIGHT];
1529 }
1530 return NULL;
1531}
1532
K.Takatac81e9bf2022-01-16 14:15:49 +00001533 static void
1534update_scrollbar_size(void)
1535{
1536 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1537 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1538}
1539
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001540/*
1541 * Get the character size of a font.
1542 */
1543 static void
1544GetFontSize(GuiFont font)
1545{
1546 HWND hwnd = GetDesktopWindow();
1547 HDC hdc = GetWindowDC(hwnd);
1548 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001549 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001550 TEXTMETRIC tm;
1551
1552 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001553 // GetTextMetrics() may not return the right value in tmAveCharWidth
1554 // for some fonts. Do our own average computation.
1555 GetTextExtentPoint(hdc,
1556 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1557 52, &size);
1558 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559
1560 gui.char_height = tm.tmHeight + p_linespace;
1561
1562 SelectFont(hdc, hfntOld);
1563
1564 ReleaseDC(hwnd, hdc);
1565}
1566
1567/*
1568 * Adjust gui.char_height (after 'linespace' was changed).
1569 */
1570 int
1571gui_mch_adjust_charheight(void)
1572{
1573 GetFontSize(gui.norm_font);
1574 return OK;
1575}
1576
1577 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001578get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001579{
1580 HFONT font = NULL;
1581
Bram Moolenaar734a8672019-12-02 22:49:38 +01001582 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001583 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
1585 if (font == NULL)
1586 return NOFONT;
1587
1588 return (GuiFont)font;
1589}
1590
1591 static int
1592pixels_to_points(int pixels, int vertical)
1593{
1594 int points;
1595 HWND hwnd;
1596 HDC hdc;
1597
1598 hwnd = GetDesktopWindow();
1599 hdc = GetWindowDC(hwnd);
1600
1601 points = MulDiv(pixels, 72,
1602 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1603
1604 ReleaseDC(hwnd, hdc);
1605
1606 return points;
1607}
1608
1609 GuiFont
1610gui_mch_get_font(
1611 char_u *name,
1612 int giveErrorIfMissing)
1613{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001614 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001615 GuiFont font = NOFONT;
1616
1617 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001618 {
1619 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001620 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001621 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001623 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001624 return font;
1625}
1626
1627#if defined(FEAT_EVAL) || defined(PROTO)
1628/*
1629 * Return the name of font "font" in allocated memory.
1630 * Don't know how to get the actual name, thus use the provided name.
1631 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001632 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001633gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634{
1635 if (name == NULL)
1636 return NULL;
1637 return vim_strsave(name);
1638}
1639#endif
1640
1641 void
1642gui_mch_free_font(GuiFont font)
1643{
1644 if (font)
1645 DeleteObject((HFONT)font);
1646}
1647
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648/*
1649 * Return the Pixel value (color) for the given color name.
1650 * Return INVALCOLOR for error.
1651 */
1652 guicolor_T
1653gui_mch_get_color(char_u *name)
1654{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001655 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656
1657 typedef struct SysColorTable
1658 {
1659 char *name;
1660 int color;
1661 } SysColorTable;
1662
1663 static SysColorTable sys_table[] =
1664 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1666 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001667#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1669#endif
1670 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1671 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1672 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1673 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1674 {"SYS_DESKTOP", COLOR_DESKTOP},
1675 {"SYS_INFOBK", COLOR_INFOBK},
1676 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1677 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678 {"SYS_BTNFACE", COLOR_BTNFACE},
1679 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1680 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1681 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1682 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1683 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1684 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1685 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1686 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1687 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1688 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1689 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1690 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1691 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1692 {"SYS_MENU", COLOR_MENU},
1693 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1694 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1695 {"SYS_WINDOW", COLOR_WINDOW},
1696 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1697 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1698 };
1699
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700 /*
1701 * Try to look up a system colour.
1702 */
K.Takataeeec2542021-06-02 13:28:16 +02001703 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001704 if (STRICMP(name, sys_table[i].name) == 0)
1705 return GetSysColor(sys_table[i].color);
1706
Bram Moolenaarab302212016-04-26 20:59:29 +02001707 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001709
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001710 guicolor_T
1711gui_mch_get_rgb_color(int r, int g, int b)
1712{
1713 return gui_get_rgb_color_cmn(r, g, b);
1714}
1715
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716/*
1717 * Return OK if the key with the termcap name "name" is supported.
1718 */
1719 int
1720gui_mch_haskey(char_u *name)
1721{
1722 int i;
1723
1724 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1725 if (name[0] == special_keys[i].vim_code0 &&
1726 name[1] == special_keys[i].vim_code1)
1727 return OK;
1728 return FAIL;
1729}
1730
1731 void
1732gui_mch_beep(void)
1733{
1734 MessageBeep(MB_OK);
1735}
1736/*
1737 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1738 */
1739 void
1740gui_mch_invert_rectangle(
1741 int r,
1742 int c,
1743 int nr,
1744 int nc)
1745{
1746 RECT rc;
1747
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001748#if defined(FEAT_DIRECTX)
1749 if (IS_ENABLE_DIRECTX())
1750 DWriteContext_Flush(s_dwc);
1751#endif
1752
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753 /*
1754 * Note: InvertRect() excludes right and bottom of rectangle.
1755 */
1756 rc.left = FILL_X(c);
1757 rc.top = FILL_Y(r);
1758 rc.right = rc.left + nc * gui.char_width;
1759 rc.bottom = rc.top + nr * gui.char_height;
1760 InvertRect(s_hdc, &rc);
1761}
1762
1763/*
1764 * Iconify the GUI window.
1765 */
1766 void
1767gui_mch_iconify(void)
1768{
1769 ShowWindow(s_hwnd, SW_MINIMIZE);
1770}
1771
1772/*
1773 * Draw a cursor without focus.
1774 */
1775 void
1776gui_mch_draw_hollow_cursor(guicolor_T color)
1777{
1778 HBRUSH hbr;
1779 RECT rc;
1780
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001781#if defined(FEAT_DIRECTX)
1782 if (IS_ENABLE_DIRECTX())
1783 DWriteContext_Flush(s_dwc);
1784#endif
1785
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001786 /*
1787 * Note: FrameRect() excludes right and bottom of rectangle.
1788 */
1789 rc.left = FILL_X(gui.col);
1790 rc.top = FILL_Y(gui.row);
1791 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 if (mb_lefthalve(gui.row, gui.col))
1793 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001794 rc.bottom = rc.top + gui.char_height;
1795 hbr = CreateSolidBrush(color);
1796 FrameRect(s_hdc, &rc, hbr);
1797 DeleteBrush(hbr);
1798}
1799/*
1800 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1801 * color "color".
1802 */
1803 void
1804gui_mch_draw_part_cursor(
1805 int w,
1806 int h,
1807 guicolor_T color)
1808{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001809 RECT rc;
1810
1811 /*
1812 * Note: FillRect() excludes right and bottom of rectangle.
1813 */
1814 rc.left =
1815#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001816 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1818#endif
1819 FILL_X(gui.col);
1820 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1821 rc.right = rc.left + w;
1822 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001823
Bram Moolenaar92467d32017-12-05 13:22:16 +01001824 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825}
1826
1827
1828/*
1829 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1830 * dead key's nominal character and re-post the original message.
1831 */
1832 static void
1833outputDeadKey_rePost(MSG originalMsg)
1834{
1835 static MSG deadCharExpel;
1836
1837 if (!dead_key)
1838 return;
1839
1840 dead_key = 0;
1841
Bram Moolenaar734a8672019-12-02 22:49:38 +01001842 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001843 deadCharExpel.message = originalMsg.message;
1844 deadCharExpel.hwnd = originalMsg.hwnd;
1845 deadCharExpel.wParam = VK_SPACE;
1846
1847 MyTranslateMessage(&deadCharExpel);
1848
Bram Moolenaar734a8672019-12-02 22:49:38 +01001849 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001850 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1851 originalMsg.lParam);
1852}
1853
1854
1855/*
1856 * Process a single Windows message.
1857 * If one is not available we hang until one is.
1858 */
1859 static void
1860process_message(void)
1861{
1862 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 char_u string[40];
1865 int i;
1866 int modifiers = 0;
1867 int key;
1868#ifdef FEAT_MENU
1869 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1870#endif
1871
1872 pGetMessage(&msg, NULL, 0, 0);
1873
1874#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001875 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001876 if (msg.message == WM_OLE)
1877 {
1878 char_u *str = (char_u *)msg.lParam;
1879 if (str == NULL || *str == NUL)
1880 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001881 // Message can't be ours, forward it. Fixes problem with Ultramon
1882 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001883 pDispatchMessage(&msg);
1884 }
1885 else
1886 {
1887 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001888 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001889 }
1890 return;
1891 }
1892#endif
1893
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001895 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1897 {
1898 HandleMouseHide(msg.message, msg.lParam);
1899 return;
1900 }
1901#endif
1902
1903 /*
1904 * Check if it's a special key that we recognise. If not, call
1905 * TranslateMessage().
1906 */
1907 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1908 {
1909 vk = (int) msg.wParam;
1910
1911 /*
1912 * Handle dead keys in special conditions in other cases we let Windows
1913 * handle them and do not interfere.
1914 *
1915 * The dead_key flag must be reset on several occasions:
1916 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1917 * consumed at that point (This is when we let Windows combine the
1918 * dead character on its own)
1919 *
1920 * - Before doing something special such as regenerating keypresses to
1921 * expel the dead character as this could trigger an infinite loop if
1922 * for some reason MyTranslateMessage() do not trigger a call
1923 * immediately to _OnChar() (or _OnSysChar()).
1924 */
1925 if (dead_key)
1926 {
1927 /*
1928 * If a dead key was pressed and the user presses VK_SPACE,
1929 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1930 * with the dead char now, so do nothing special and let Windows
1931 * handle it.
1932 *
1933 * Note that VK_SPACE combines with the dead_key's character and
1934 * only one WM_CHAR will be generated by TranslateMessage(), in
1935 * the two other cases two WM_CHAR will be generated: the dead
1936 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1937 * user expects.
1938 */
1939 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1940 {
1941 dead_key = 0;
1942 MyTranslateMessage(&msg);
1943 return;
1944 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001945 // In modes where we are not typing, dead keys should behave
1946 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1948 {
1949 outputDeadKey_rePost(msg);
1950 return;
1951 }
1952 }
1953
Bram Moolenaar734a8672019-12-02 22:49:38 +01001954 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001955 if (vk == VK_CANCEL)
1956 {
1957 trash_input_buf();
1958 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001959 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001960 string[0] = Ctrl_C;
1961 add_to_input_buf(string, 1);
1962 }
1963
1964 for (i = 0; special_keys[i].key_sym != 0; i++)
1965 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001966 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001967 if (special_keys[i].key_sym == vk
1968 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1969 {
1970 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001971 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001972 * is a key that would normally trigger the dead key nominal
1973 * character output (such as a NUMPAD printable character or
1974 * the TAB key, etc...).
1975 */
1976 if (dead_key && (special_keys[i].vim_code0 == 'K'
1977 || vk == VK_TAB || vk == CAR))
1978 {
1979 outputDeadKey_rePost(msg);
1980 return;
1981 }
1982
1983#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001984 // Check for <F10>: Windows selects the menu. When <F10> is
1985 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001986 if (vk == VK_F10
1987 && gui.menu_is_active
1988 && check_map(k10, State, FALSE, TRUE, FALSE,
1989 NULL, NULL) == NULL)
1990 break;
1991#endif
1992 if (GetKeyState(VK_SHIFT) & 0x8000)
1993 modifiers |= MOD_MASK_SHIFT;
1994 /*
1995 * Don't use caps-lock as shift, because these are special keys
1996 * being considered here, and we only want letters to get
1997 * shifted -- webb
1998 */
1999 /*
2000 if (GetKeyState(VK_CAPITAL) & 0x0001)
2001 modifiers ^= MOD_MASK_SHIFT;
2002 */
2003 if (GetKeyState(VK_CONTROL) & 0x8000)
2004 modifiers |= MOD_MASK_CTRL;
2005 if (GetKeyState(VK_MENU) & 0x8000)
2006 modifiers |= MOD_MASK_ALT;
2007
2008 if (special_keys[i].vim_code1 == NUL)
2009 key = special_keys[i].vim_code0;
2010 else
2011 key = TO_SPECIAL(special_keys[i].vim_code0,
2012 special_keys[i].vim_code1);
2013 key = simplify_key(key, &modifiers);
2014 if (key == CSI)
2015 key = K_CSI;
2016
2017 if (modifiers)
2018 {
2019 string[0] = CSI;
2020 string[1] = KS_MODIFIER;
2021 string[2] = modifiers;
2022 add_to_input_buf(string, 3);
2023 }
2024
2025 if (IS_SPECIAL(key))
2026 {
2027 string[0] = CSI;
2028 string[1] = K_SECOND(key);
2029 string[2] = K_THIRD(key);
2030 add_to_input_buf(string, 3);
2031 }
2032 else
2033 {
2034 int len;
2035
Bram Moolenaar734a8672019-12-02 22:49:38 +01002036 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002037 len = char_to_string(key, string, 40, FALSE);
2038 add_to_input_buf(string, len);
2039 }
2040 break;
2041 }
2042 }
2043 if (special_keys[i].key_sym == 0)
2044 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002045 // Some keys need C-S- where they should only need C-.
2046 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2047 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048 if (vk != 0xff
2049 && (GetKeyState(VK_CONTROL) & 0x8000)
2050 && !(GetKeyState(VK_SHIFT) & 0x8000)
2051 && !(GetKeyState(VK_MENU) & 0x8000))
2052 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002053 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002054 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2055 {
2056 string[0] = Ctrl_HAT;
2057 add_to_input_buf(string, 1);
2058 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002059 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2060 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002061 {
2062 string[0] = Ctrl__;
2063 add_to_input_buf(string, 1);
2064 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002066 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2067 {
2068 string[0] = Ctrl_AT;
2069 add_to_input_buf(string, 1);
2070 }
2071 else
2072 MyTranslateMessage(&msg);
2073 }
2074 else
2075 MyTranslateMessage(&msg);
2076 }
2077 }
2078#ifdef FEAT_MBYTE_IME
2079 else if (msg.message == WM_IME_NOTIFY)
2080 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2081 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002082 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002083 MyTranslateMessage(&msg);
2084#endif
2085#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002086// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 else if (msg.message == WM_IME_STARTCOMPOSITION)
2088 {
2089 POINT point;
2090
2091 global_ime_set_font(&norm_logfont);
2092 point.x = FILL_X(gui.col);
2093 point.y = FILL_Y(gui.row);
2094 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2095 global_ime_set_position(&point);
2096 }
2097#endif
2098
2099#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002100 // Check for <F10>: Default effect is to select the menu. When <F10> is
2101 // mapped we need to stop it here to avoid strange effects (e.g., for the
2102 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002103 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2104 NULL, NULL) == NULL)
2105#endif
2106 pDispatchMessage(&msg);
2107}
2108
2109/*
2110 * Catch up with any queued events. This may put keyboard input into the
2111 * input buffer, call resize call-backs, trigger timers etc. If there is
2112 * nothing in the event queue (& no timers pending), then we return
2113 * immediately.
2114 */
2115 void
2116gui_mch_update(void)
2117{
2118 MSG msg;
2119
2120 if (!s_busy_processing)
2121 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2122 && !vim_is_input_buf_full())
2123 process_message();
2124}
2125
Bram Moolenaar4231da42016-06-02 14:30:04 +02002126 static void
2127remove_any_timer(void)
2128{
2129 MSG msg;
2130
2131 if (s_wait_timer != 0 && !s_timed_out)
2132 {
2133 KillTimer(NULL, s_wait_timer);
2134
Bram Moolenaar734a8672019-12-02 22:49:38 +01002135 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002136 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2137 ;
2138 s_wait_timer = 0;
2139 }
2140}
2141
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002142/*
2143 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2144 * from the keyboard.
2145 * wtime == -1 Wait forever.
2146 * wtime == 0 This should never happen.
2147 * wtime > 0 Wait wtime milliseconds for a character.
2148 * Returns OK if a character was found to be available within the given time,
2149 * or FAIL otherwise.
2150 */
2151 int
2152gui_mch_wait_for_chars(int wtime)
2153{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002154 int focus;
2155
2156 s_timed_out = FALSE;
2157
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002158 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002159 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002160 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002161 if (s_busy_processing)
2162 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002163
2164 // When called with "wtime" zero, just want one msec.
2165 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002166 (TIMERPROC)_OnTimer);
2167 }
2168
2169 allow_scrollbar = TRUE;
2170
2171 focus = gui.in_focus;
2172 while (!s_timed_out)
2173 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002174 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002175 if (gui.in_focus != focus)
2176 {
2177 if (gui.in_focus)
2178 gui_mch_start_blink();
2179 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002180 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002181 focus = gui.in_focus;
2182 }
2183
2184 if (s_need_activate)
2185 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002187 s_need_activate = FALSE;
2188 }
2189
Bram Moolenaar4231da42016-06-02 14:30:04 +02002190#ifdef FEAT_TIMERS
2191 did_add_timer = FALSE;
2192#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002193#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002194 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002195 for (;;)
2196 {
2197 MSG msg;
2198
2199 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002200# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002201 if (did_add_timer)
2202 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002203# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002204 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2205 {
2206 process_message();
2207 break;
2208 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002209 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002210 // TODO: The 10 msec is a compromise between laggy response
2211 // and consuming more CPU time. Better would be to handle
2212 // channel messages when they arrive.
2213 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002214 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002215 break;
2216 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002217#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002218 // Don't use gui_mch_update() because then we will spin-lock until a
2219 // char arrives, instead we use GetMessage() to hang until an
2220 // event arrives. No need to check for input_buf_full because we are
2221 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002222 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002223#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002224
2225 if (input_available())
2226 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002227 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002228 allow_scrollbar = FALSE;
2229
Bram Moolenaar89c00032019-09-04 13:53:21 +02002230 // Clear pending mouse button, the release event may have been
2231 // taken by the dialog window. But don't do this when getting
2232 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233 if (!s_getting_focus)
2234 s_button_pending = -1;
2235
2236 return OK;
2237 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002238
2239#ifdef FEAT_TIMERS
2240 if (did_add_timer)
2241 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002242 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002243 remove_any_timer();
2244 break;
2245 }
2246#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002247 }
2248 allow_scrollbar = FALSE;
2249 return FAIL;
2250}
2251
2252/*
2253 * Clear a rectangular region of the screen from text pos (row1, col1) to
2254 * (row2, col2) inclusive.
2255 */
2256 void
2257gui_mch_clear_block(
2258 int row1,
2259 int col1,
2260 int row2,
2261 int col2)
2262{
2263 RECT rc;
2264
2265 /*
2266 * Clear one extra pixel at the far right, for when bold characters have
2267 * spilled over to the window border.
2268 * Note: FillRect() excludes right and bottom of rectangle.
2269 */
2270 rc.left = FILL_X(col1);
2271 rc.top = FILL_Y(row1);
2272 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2273 rc.bottom = FILL_Y(row2 + 1);
2274 clear_rect(&rc);
2275}
2276
2277/*
2278 * Clear the whole text window.
2279 */
2280 void
2281gui_mch_clear_all(void)
2282{
2283 RECT rc;
2284
2285 rc.left = 0;
2286 rc.top = 0;
2287 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2288 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2289 clear_rect(&rc);
2290}
2291/*
2292 * Menu stuff.
2293 */
2294
2295 void
2296gui_mch_enable_menu(int flag)
2297{
2298#ifdef FEAT_MENU
2299 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2300#endif
2301}
2302
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 void
2304gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002305 int x UNUSED,
2306 int y UNUSED,
2307 int w UNUSED,
2308 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002310 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002311}
2312
2313#if defined(FEAT_MENU) || defined(PROTO)
2314/*
2315 * Make menu item hidden or not hidden
2316 */
2317 void
2318gui_mch_menu_hidden(
2319 vimmenu_T *menu,
2320 int hidden)
2321{
2322 /*
2323 * This doesn't do what we want. Hmm, just grey the menu items for now.
2324 */
2325 /*
2326 if (hidden)
2327 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2328 else
2329 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2330 */
2331 gui_mch_menu_grey(menu, hidden);
2332}
2333
2334/*
2335 * This is called after setting all the menus to grey/hidden or not.
2336 */
2337 void
2338gui_mch_draw_menubar(void)
2339{
2340 DrawMenuBar(s_hwnd);
2341}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002342#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002343
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002344/*
2345 * Return the RGB value of a pixel as a long.
2346 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002347 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002348gui_mch_get_rgb(guicolor_T pixel)
2349{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002350 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2351 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352}
2353
2354#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002355/*
2356 * Convert pixels in X to dialog units
2357 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 static WORD
2359PixelToDialogX(int numPixels)
2360{
2361 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2362}
2363
Bram Moolenaar734a8672019-12-02 22:49:38 +01002364/*
2365 * Convert pixels in Y to dialog units
2366 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 static WORD
2368PixelToDialogY(int numPixels)
2369{
2370 return (WORD)((numPixels * 8) / s_dlgfntheight);
2371}
2372
Bram Moolenaar734a8672019-12-02 22:49:38 +01002373/*
2374 * Return the width in pixels of the given text in the given DC.
2375 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 static int
2377GetTextWidth(HDC hdc, char_u *str, int len)
2378{
2379 SIZE size;
2380
2381 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2382 return size.cx;
2383}
2384
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002385/*
2386 * Return the width in pixels of the given text in the given DC, taking care
2387 * of 'encoding' to active codepage conversion.
2388 */
2389 static int
2390GetTextWidthEnc(HDC hdc, char_u *str, int len)
2391{
2392 SIZE size;
2393 WCHAR *wstr;
2394 int n;
2395 int wlen = len;
2396
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002397 wstr = enc_to_utf16(str, &wlen);
2398 if (wstr == NULL)
2399 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002401 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2402 vim_free(wstr);
2403 if (n)
2404 return size.cx;
2405 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002408static void get_work_area(RECT *spi_rect);
2409
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002410/*
2411 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002412 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2413 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414 */
2415 static BOOL
2416CenterWindow(
2417 HWND hwndChild,
2418 HWND hwndParent)
2419{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002420 HMONITOR mon;
2421 MONITORINFO moninfo;
2422 RECT rChild, rParent, rScreen;
2423 int wChild, hChild, wParent, hParent;
2424 int xNew, yNew;
2425 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426
2427 GetWindowRect(hwndChild, &rChild);
2428 wChild = rChild.right - rChild.left;
2429 hChild = rChild.bottom - rChild.top;
2430
Bram Moolenaar734a8672019-12-02 22:49:38 +01002431 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002433 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434 else
2435 GetWindowRect(hwndParent, &rParent);
2436 wParent = rParent.right - rParent.left;
2437 hParent = rParent.bottom - rParent.top;
2438
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002439 moninfo.cbSize = sizeof(MONITORINFO);
2440 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2441 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002443 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002444 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002445 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002446 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002447 hdc = GetDC(hwndChild);
2448 rScreen.left = 0;
2449 rScreen.top = 0;
2450 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2451 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2452 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 }
2454
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002455 xNew = rParent.left + ((wParent - wChild) / 2);
2456 if (xNew < rScreen.left)
2457 xNew = rScreen.left;
2458 else if ((xNew + wChild) > rScreen.right)
2459 xNew = rScreen.right - wChild;
2460
2461 yNew = rParent.top + ((hParent - hChild) / 2);
2462 if (yNew < rScreen.top)
2463 yNew = rScreen.top;
2464 else if ((yNew + hChild) > rScreen.bottom)
2465 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466
2467 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2468 SWP_NOSIZE | SWP_NOZORDER);
2469}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002470#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002472#if defined(FEAT_TOOLBAR) || defined(PROTO)
2473 void
2474gui_mch_show_toolbar(int showit)
2475{
2476 if (s_toolbarhwnd == NULL)
2477 return;
2478
2479 if (showit)
2480 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002481# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002482 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002483# define TB_SETUNICODEFORMAT 0x2005
2484# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002485 // Enable unicode support
2486 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2487 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488 ShowWindow(s_toolbarhwnd, SW_SHOW);
2489 }
2490 else
2491 ShowWindow(s_toolbarhwnd, SW_HIDE);
2492}
2493
Bram Moolenaar734a8672019-12-02 22:49:38 +01002494// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002495# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496
2497#endif
2498
2499#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2500 static void
2501add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2502{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002503 WCHAR *wn;
2504 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002506 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002507 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002508 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002510 infow.cbSize = sizeof(infow);
2511 infow.fMask = MIIM_TYPE | MIIM_ID;
2512 infow.wID = item_id;
2513 infow.fType = MFT_STRING;
2514 infow.dwTypeData = wn;
2515 infow.cch = (UINT)wcslen(wn);
2516 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2517 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518}
2519
2520 static void
2521show_tabline_popup_menu(void)
2522{
2523 HMENU tab_pmenu;
2524 long rval;
2525 POINT pt;
2526
Bram Moolenaar734a8672019-12-02 22:49:38 +01002527 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002528 if (hold_gui_events
2529# ifdef FEAT_CMDWIN
2530 || cmdwin_type != 0
2531# endif
2532 )
2533 return;
2534
2535 tab_pmenu = CreatePopupMenu();
2536 if (tab_pmenu == NULL)
2537 return;
2538
2539 if (first_tabpage->tp_next != NULL)
2540 add_tabline_popup_menu_entry(tab_pmenu,
2541 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2542 add_tabline_popup_menu_entry(tab_pmenu,
2543 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2544 add_tabline_popup_menu_entry(tab_pmenu,
2545 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2546
2547 GetCursorPos(&pt);
2548 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2549 NULL);
2550
2551 DestroyMenu(tab_pmenu);
2552
Bram Moolenaar734a8672019-12-02 22:49:38 +01002553 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 if (rval > 0)
2555 {
2556 TCHITTESTINFO htinfo;
2557 int idx;
2558
2559 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2560 return;
2561
2562 htinfo.pt.x = pt.x;
2563 htinfo.pt.y = pt.y;
2564 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2565 if (idx == -1)
2566 idx = 0;
2567 else
2568 idx += 1;
2569
2570 send_tabline_menu_event(idx, (int)rval);
2571 }
2572}
2573
2574/*
2575 * Show or hide the tabline.
2576 */
2577 void
2578gui_mch_show_tabline(int showit)
2579{
2580 if (s_tabhwnd == NULL)
2581 return;
2582
2583 if (!showit != !showing_tabline)
2584 {
2585 if (showit)
2586 ShowWindow(s_tabhwnd, SW_SHOW);
2587 else
2588 ShowWindow(s_tabhwnd, SW_HIDE);
2589 showing_tabline = showit;
2590 }
2591}
2592
2593/*
2594 * Return TRUE when tabline is displayed.
2595 */
2596 int
2597gui_mch_showing_tabline(void)
2598{
2599 return s_tabhwnd != NULL && showing_tabline;
2600}
2601
2602/*
2603 * Update the labels of the tabline.
2604 */
2605 void
2606gui_mch_update_tabline(void)
2607{
2608 tabpage_T *tp;
2609 TCITEM tie;
2610 int nr = 0;
2611 int curtabidx = 0;
2612 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614
2615 if (s_tabhwnd == NULL)
2616 return;
2617
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002618# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002619 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002620# define CCM_SETUNICODEFORMAT 0x2005
2621# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002622 // Enable unicode support
2623 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624
2625 tie.mask = TCIF_TEXT;
2626 tie.iImage = -1;
2627
Bram Moolenaar734a8672019-12-02 22:49:38 +01002628 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002629 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2630
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2633 {
2634 if (tp == curtab)
2635 curtabidx = nr;
2636
2637 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2638 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002639 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002640 tie.pszText = "-Empty-";
2641 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2642 tabadded = 1;
2643 }
2644
2645 get_tabline_label(tp, FALSE);
2646 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002648 wstr = enc_to_utf16(NameBuff, NULL);
2649 if (wstr != NULL)
2650 {
2651 TCITEMW tiw;
2652
2653 tiw.mask = TCIF_TEXT;
2654 tiw.iImage = -1;
2655 tiw.pszText = wstr;
2656 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2657 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002658 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002659 }
2660
Bram Moolenaar734a8672019-12-02 22:49:38 +01002661 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2663 TabCtrl_DeleteItem(s_tabhwnd, nr);
2664
2665 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2666 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2667
Bram Moolenaar734a8672019-12-02 22:49:38 +01002668 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2670 RedrawWindow(s_tabhwnd, NULL, NULL,
2671 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2672
2673 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2674 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2675}
2676
2677/*
2678 * Set the current tab to "nr". First tab is 1.
2679 */
2680 void
2681gui_mch_set_curtab(int nr)
2682{
2683 if (s_tabhwnd == NULL)
2684 return;
2685
2686 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2687 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2688}
2689
2690#endif
2691
2692/*
2693 * ":simalt" command.
2694 */
2695 void
2696ex_simalt(exarg_T *eap)
2697{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002698 char_u *keys = eap->arg;
2699 int fill_typebuf = FALSE;
2700 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701
2702 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2703 while (*keys)
2704 {
2705 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002706 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2708 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002709 fill_typebuf = TRUE;
2710 }
2711 if (fill_typebuf)
2712 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002713 // Put a NOP in the typeahead buffer so that the message will get
2714 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002715 key_name[0] = K_SPECIAL;
2716 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002717 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002718 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002719#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002720 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002721#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002722 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 }
2724}
2725
2726/*
2727 * Create the find & replace dialogs.
2728 * You can't have both at once: ":find" when replace is showing, destroys
2729 * the replace dialog first, and the other way around.
2730 */
2731#ifdef MSWIN_FIND_REPLACE
2732 static void
2733initialise_findrep(char_u *initial_string)
2734{
2735 int wword = FALSE;
2736 int mcase = !p_ic;
2737 char_u *entry_text;
2738
Bram Moolenaar734a8672019-12-02 22:49:38 +01002739 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2741
2742 s_findrep_struct.hwndOwner = s_hwnd;
2743 s_findrep_struct.Flags = FR_DOWN;
2744 if (mcase)
2745 s_findrep_struct.Flags |= FR_MATCHCASE;
2746 if (wword)
2747 s_findrep_struct.Flags |= FR_WHOLEWORD;
2748 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002749 {
2750 WCHAR *p = enc_to_utf16(entry_text, NULL);
2751 if (p != NULL)
2752 {
2753 int len = s_findrep_struct.wFindWhatLen - 1;
2754
2755 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2756 s_findrep_struct.lpstrFindWhat[len] = NUL;
2757 vim_free(p);
2758 }
2759 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 vim_free(entry_text);
2761}
2762#endif
2763
2764 static void
2765set_window_title(HWND hwnd, char *title)
2766{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002767 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002768 {
2769 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002770
Bram Moolenaar734a8672019-12-02 22:49:38 +01002771 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002772 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2773 if (wbuf != NULL)
2774 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002775 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002776 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777 }
2778 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002779 else
2780 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781}
2782
2783 void
2784gui_mch_find_dialog(exarg_T *eap)
2785{
2786#ifdef MSWIN_FIND_REPLACE
2787 if (s_findrep_msg != 0)
2788 {
2789 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2790 DestroyWindow(s_findrep_hwnd);
2791
2792 if (!IsWindow(s_findrep_hwnd))
2793 {
2794 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002795 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002796 }
2797
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002798 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 (void)SetFocus(s_findrep_hwnd);
2800
2801 s_findrep_is_find = TRUE;
2802 }
2803#endif
2804}
2805
2806
2807 void
2808gui_mch_replace_dialog(exarg_T *eap)
2809{
2810#ifdef MSWIN_FIND_REPLACE
2811 if (s_findrep_msg != 0)
2812 {
2813 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2814 DestroyWindow(s_findrep_hwnd);
2815
2816 if (!IsWindow(s_findrep_hwnd))
2817 {
2818 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002819 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820 }
2821
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002822 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 (void)SetFocus(s_findrep_hwnd);
2824
2825 s_findrep_is_find = FALSE;
2826 }
2827#endif
2828}
2829
2830
2831/*
2832 * Set visibility of the pointer.
2833 */
2834 void
2835gui_mch_mousehide(int hide)
2836{
2837 if (hide != gui.pointer_hidden)
2838 {
2839 ShowCursor(!hide);
2840 gui.pointer_hidden = hide;
2841 }
2842}
2843
2844#ifdef FEAT_MENU
2845 static void
2846gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2847{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002848 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002849 gui_mch_mousehide(FALSE);
2850
2851 (void)TrackPopupMenu(
2852 (HMENU)menu->submenu_id,
2853 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2854 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002855 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002856 s_hwnd,
2857 NULL);
2858 /*
2859 * NOTE: The pop-up menu can eat the mouse up event.
2860 * We deal with this in normal.c.
2861 */
2862}
2863#endif
2864
2865/*
2866 * Got a message when the system will go down.
2867 */
2868 static void
2869_OnEndSession(void)
2870{
2871 getout_preserve_modified(1);
2872}
2873
2874/*
2875 * Get this message when the user clicks on the cross in the top right corner
2876 * of a Windows95 window.
2877 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002878 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002879_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880{
2881 gui_shell_closed();
2882}
2883
2884/*
2885 * Get a message when the window is being destroyed.
2886 */
2887 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002888_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889{
2890 if (!destroying)
2891 _OnClose(hwnd);
2892}
2893
2894 static void
2895_OnPaint(
2896 HWND hwnd)
2897{
2898 if (!IsMinimized(hwnd))
2899 {
2900 PAINTSTRUCT ps;
2901
Bram Moolenaar734a8672019-12-02 22:49:38 +01002902 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904
Bram Moolenaar734a8672019-12-02 22:49:38 +01002905 // prevent multi-byte characters from misprinting on an invalid
2906 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907 if (has_mbyte)
2908 {
2909 RECT rect;
2910
2911 GetClientRect(hwnd, &rect);
2912 ps.rcPaint.left = rect.left;
2913 ps.rcPaint.right = rect.right;
2914 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915
2916 if (!IsRectEmpty(&ps.rcPaint))
2917 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2919 ps.rcPaint.right - ps.rcPaint.left + 1,
2920 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2921 }
2922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 EndPaint(hwnd, &ps);
2924 }
2925}
2926
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002927 static void
2928_OnSize(
2929 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002930 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931 int cx,
2932 int cy)
2933{
K.Takatac81e9bf2022-01-16 14:15:49 +00002934 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002935 {
2936 gui_resize_shell(cx, cy);
2937
Bram Moolenaar734a8672019-12-02 22:49:38 +01002938 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 }
2941}
2942
2943 static void
2944_OnSetFocus(
2945 HWND hwnd,
2946 HWND hwndOldFocus)
2947{
2948 gui_focus_change(TRUE);
2949 s_getting_focus = TRUE;
2950 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2951}
2952
2953 static void
2954_OnKillFocus(
2955 HWND hwnd,
2956 HWND hwndNewFocus)
2957{
2958 gui_focus_change(FALSE);
2959 s_getting_focus = FALSE;
2960 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2961}
2962
2963/*
2964 * Get a message when the user switches back to vim
2965 */
2966 static LRESULT
2967_OnActivateApp(
2968 HWND hwnd,
2969 BOOL fActivate,
2970 DWORD dwThreadId)
2971{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002972 // we call gui_focus_change() in _OnSetFocus()
2973 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002974 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2975}
2976
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002977 void
2978gui_mch_destroy_scrollbar(scrollbar_T *sb)
2979{
2980 DestroyWindow(sb->id);
2981}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002982
2983/*
2984 * Get current mouse coordinates in text window.
2985 */
2986 void
2987gui_mch_getmouse(int *x, int *y)
2988{
2989 RECT rct;
2990 POINT mp;
2991
2992 (void)GetWindowRect(s_textArea, &rct);
2993 (void)GetCursorPos((LPPOINT)&mp);
2994 *x = (int)(mp.x - rct.left);
2995 *y = (int)(mp.y - rct.top);
2996}
2997
2998/*
2999 * Move mouse pointer to character at (x, y).
3000 */
3001 void
3002gui_mch_setmouse(int x, int y)
3003{
3004 RECT rct;
3005
3006 (void)GetWindowRect(s_textArea, &rct);
3007 (void)SetCursorPos(x + gui.border_offset + rct.left,
3008 y + gui.border_offset + rct.top);
3009}
3010
3011 static void
3012gui_mswin_get_valid_dimensions(
3013 int w,
3014 int h,
3015 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003016 int *valid_h,
3017 int *cols,
3018 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019{
3020 int base_width, base_height;
3021
3022 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003023 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3024 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003025 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003026 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3027 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3028 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3029 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003030 *cols = (w - base_width) / gui.char_width;
3031 *rows = (h - base_height) / gui.char_height;
3032 *valid_w = base_width + *cols * gui.char_width;
3033 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003034}
3035
3036 void
3037gui_mch_flash(int msec)
3038{
3039 RECT rc;
3040
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003041#if defined(FEAT_DIRECTX)
3042 if (IS_ENABLE_DIRECTX())
3043 DWriteContext_Flush(s_dwc);
3044#endif
3045
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003046 /*
3047 * Note: InvertRect() excludes right and bottom of rectangle.
3048 */
3049 rc.left = 0;
3050 rc.top = 0;
3051 rc.right = gui.num_cols * gui.char_width;
3052 rc.bottom = gui.num_rows * gui.char_height;
3053 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003054 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055
Bram Moolenaar734a8672019-12-02 22:49:38 +01003056 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057
3058 InvertRect(s_hdc, &rc);
3059}
3060
3061/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003062 * Check if the specified point is on-screen. (multi-monitor aware)
3063 */
3064 static BOOL
3065is_point_onscreen(int x, int y)
3066{
3067 POINT pt = {x, y};
3068
3069 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3070}
3071
3072/*
3073 * Check if the whole area of the specified window is on-screen.
3074 *
3075 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3076 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3077 * only works when the whole window is on-screen.
3078 */
3079 static BOOL
3080is_window_onscreen(HWND hwnd)
3081{
3082 RECT rc;
3083
3084 GetWindowRect(hwnd, &rc);
3085
3086 if (!is_point_onscreen(rc.left, rc.top))
3087 return FALSE;
3088 if (!is_point_onscreen(rc.left, rc.bottom))
3089 return FALSE;
3090 if (!is_point_onscreen(rc.right, rc.top))
3091 return FALSE;
3092 if (!is_point_onscreen(rc.right, rc.bottom))
3093 return FALSE;
3094 return TRUE;
3095}
3096
3097/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003098 * Return flags used for scrolling.
3099 * The SW_INVALIDATE is required when part of the window is covered or
3100 * off-screen. Refer to MS KB Q75236.
3101 */
3102 static int
3103get_scroll_flags(void)
3104{
3105 HWND hwnd;
3106 RECT rcVim, rcOther, rcDest;
3107
Bram Moolenaar185577e2020-10-29 20:08:21 +01003108 // Check if the window is (partly) off-screen.
3109 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003110 return SW_INVALIDATE;
3111
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003112 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003113 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003114 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3115 if (IsWindowVisible(hwnd))
3116 {
3117 GetWindowRect(hwnd, &rcOther);
3118 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3119 return SW_INVALIDATE;
3120 }
3121 return 0;
3122}
3123
3124/*
3125 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3126 * may not be scrolled out properly.
3127 * For gVim, when _OnScroll() is repeated, the character at the
3128 * previous cursor position may be left drawn after scroll.
3129 * The problem can be avoided by calling GetPixel() to get a pixel in
3130 * the region before ScrollWindowEx().
3131 */
3132 static void
3133intel_gpu_workaround(void)
3134{
3135 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3136}
3137
3138/*
3139 * Delete the given number of lines from the given row, scrolling up any
3140 * text further down within the scroll region.
3141 */
3142 void
3143gui_mch_delete_lines(
3144 int row,
3145 int num_lines)
3146{
3147 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003148
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149 rc.left = FILL_X(gui.scroll_region_left);
3150 rc.right = FILL_X(gui.scroll_region_right + 1);
3151 rc.top = FILL_Y(row);
3152 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3153
Bram Moolenaar92467d32017-12-05 13:22:16 +01003154#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003155 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003156 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003157 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003158 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003159 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160#endif
3161 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162#if defined(FEAT_DIRECTX)
3163 if (IS_ENABLE_DIRECTX())
3164 DWriteContext_Flush(s_dwc);
3165#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166 intel_gpu_workaround();
3167 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003168 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003169 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171
Bram Moolenaar734a8672019-12-02 22:49:38 +01003172 // This seems to be required to avoid the cursor disappearing when
3173 // scrolling such that the cursor ends up in the top-left character on
3174 // the screen... But why? (Webb)
3175 // It's probably fixed by disabling drawing the cursor while scrolling.
3176 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177
3178 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3179 gui.scroll_region_left,
3180 gui.scroll_region_bot, gui.scroll_region_right);
3181}
3182
3183/*
3184 * Insert the given number of lines before the given row, scrolling down any
3185 * following text within the scroll region.
3186 */
3187 void
3188gui_mch_insert_lines(
3189 int row,
3190 int num_lines)
3191{
3192 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003193
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 rc.left = FILL_X(gui.scroll_region_left);
3195 rc.right = FILL_X(gui.scroll_region_right + 1);
3196 rc.top = FILL_Y(row);
3197 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003198
3199#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003200 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003201 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003202 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003203 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003204 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003205#endif
3206 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003207#if defined(FEAT_DIRECTX)
3208 if (IS_ENABLE_DIRECTX())
3209 DWriteContext_Flush(s_dwc);
3210#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003211 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003212 // The SW_INVALIDATE is required when part of the window is covered or
3213 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003214 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003216 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003217 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003219 gui_clear_block(row, gui.scroll_region_left,
3220 row + num_lines - 1, gui.scroll_region_right);
3221}
3222
3223
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003225gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003226{
3227#if defined(FEAT_DIRECTX)
3228 DWriteContext_Close(s_dwc);
3229 DWrite_Final();
3230 s_dwc = NULL;
3231#endif
3232
3233 ReleaseDC(s_textArea, s_hdc);
3234 DeleteObject(s_brush);
3235
3236#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003237 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3239#endif
3240
Bram Moolenaar734a8672019-12-02 22:49:38 +01003241 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242 if (s_hwnd != NULL)
3243 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003244 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003245 DestroyWindow(s_hwnd);
3246 }
3247
3248#ifdef GLOBAL_IME
3249 global_ime_end();
3250#endif
3251}
3252
3253 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003254logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255{
3256 char *p;
3257 char *res;
3258 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003259 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003260 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003261 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003263 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3264 if (font_name == NULL)
3265 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003267 quality_name = quality_id2name((int)lf.lfQuality);
3268
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003269 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003270 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003271 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 if (res != NULL)
3273 {
3274 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003275 // make a normal font string out of the lf thing:
3276 points = pixels_to_points(
3277 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3278 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3279 sprintf((char *)p, "%s:h%d", font_name, points);
3280 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003281 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 while (*p)
3283 {
3284 if (*p == ' ')
3285 *p = '_';
3286 ++p;
3287 }
3288 if (lf.lfItalic)
3289 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003290 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003291 STRCAT(p, ":b");
3292 if (lf.lfUnderline)
3293 STRCAT(p, ":u");
3294 if (lf.lfStrikeOut)
3295 STRCAT(p, ":s");
3296 if (charset_name != NULL)
3297 {
3298 STRCAT(p, ":c");
3299 STRCAT(p, charset_name);
3300 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003301 if (quality_name != NULL)
3302 {
3303 STRCAT(p, ":q");
3304 STRCAT(p, quality_name);
3305 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003306 }
3307
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003308 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309 return (char_u *)res;
3310}
3311
3312
3313#ifdef FEAT_MBYTE_IME
3314/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003315 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003316 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317 */
3318 static void
3319update_im_font(void)
3320{
K.Takatac81e9bf2022-01-16 14:15:49 +00003321 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003322
3323 if (p_guifontwide != NULL && *p_guifontwide != NUL
3324 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003325 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326 norm_logfont = lf_wide;
3327 else
3328 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003329
3330 lf = norm_logfont;
3331 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3332 // Work around when PerMonitorV2 is not enabled in the process level.
3333 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3334 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003335}
3336#endif
3337
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338/*
3339 * Handler of gui.wide_font (p_guifontwide) changed notification.
3340 */
3341 void
3342gui_mch_wide_font_changed(void)
3343{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003344 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003346#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003347 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003348#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349
3350 gui_mch_free_font(gui.wide_ital_font);
3351 gui.wide_ital_font = NOFONT;
3352 gui_mch_free_font(gui.wide_bold_font);
3353 gui.wide_bold_font = NOFONT;
3354 gui_mch_free_font(gui.wide_boldital_font);
3355 gui.wide_boldital_font = NOFONT;
3356
3357 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003358 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 {
3360 if (!lf.lfItalic)
3361 {
3362 lf.lfItalic = TRUE;
3363 gui.wide_ital_font = get_font_handle(&lf);
3364 lf.lfItalic = FALSE;
3365 }
3366 if (lf.lfWeight < FW_BOLD)
3367 {
3368 lf.lfWeight = FW_BOLD;
3369 gui.wide_bold_font = get_font_handle(&lf);
3370 if (!lf.lfItalic)
3371 {
3372 lf.lfItalic = TRUE;
3373 gui.wide_boldital_font = get_font_handle(&lf);
3374 }
3375 }
3376 }
3377}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378
3379/*
3380 * Initialise vim to use the font with the given name.
3381 * Return FAIL if the font could not be loaded, OK otherwise.
3382 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003383 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003384gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385{
K.Takatac81e9bf2022-01-16 14:15:49 +00003386 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 GuiFont font = NOFONT;
3388 char_u *p;
3389
Bram Moolenaar734a8672019-12-02 22:49:38 +01003390 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003392 {
3393 lfOrig = lf;
3394 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003396 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397 if (font == NOFONT)
3398 return FAIL;
3399
3400 if (font_name == NULL)
3401 font_name = (char_u *)lf.lfFaceName;
3402#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3403 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003404#endif
3405#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406 sub_logfont = lf;
3407#endif
3408#ifdef FEAT_MBYTE_IME
K.Takatac81e9bf2022-01-16 14:15:49 +00003409 if (!s_in_dpichanged)
3410 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411#endif
3412 gui_mch_free_font(gui.norm_font);
3413 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003414 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 GetFontSize(font);
3416
K.Takatac81e9bf2022-01-16 14:15:49 +00003417 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003418 if (p != NULL)
3419 {
3420 hl_set_font_name(p);
3421
Bram Moolenaar734a8672019-12-02 22:49:38 +01003422 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3424 {
3425 vim_free(p_guifont);
3426 p_guifont = p;
3427 }
3428 else
3429 vim_free(p);
3430 }
3431
3432 gui_mch_free_font(gui.ital_font);
3433 gui.ital_font = NOFONT;
3434 gui_mch_free_font(gui.bold_font);
3435 gui.bold_font = NOFONT;
3436 gui_mch_free_font(gui.boldital_font);
3437 gui.boldital_font = NOFONT;
3438
3439 if (!lf.lfItalic)
3440 {
3441 lf.lfItalic = TRUE;
3442 gui.ital_font = get_font_handle(&lf);
3443 lf.lfItalic = FALSE;
3444 }
3445 if (lf.lfWeight < FW_BOLD)
3446 {
3447 lf.lfWeight = FW_BOLD;
3448 gui.bold_font = get_font_handle(&lf);
3449 if (!lf.lfItalic)
3450 {
3451 lf.lfItalic = TRUE;
3452 gui.boldital_font = get_font_handle(&lf);
3453 }
3454 }
3455
3456 return OK;
3457}
3458
3459#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003460# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461#endif
3462
3463/*
3464 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003465 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466 */
3467 int
3468gui_mch_maximized(void)
3469{
3470 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003471 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472
3473 wp.length = sizeof(WINDOWPLACEMENT);
3474 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003475 {
3476 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003477 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003478 && wp.flags == WPF_RESTORETOMAXIMIZED))
3479 return TRUE;
3480 if (wp.showCmd == SW_SHOWMINIMIZED)
3481 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003483 // Assume the window is snapped when the sizes from two APIs differ.
3484 GetWindowRect(s_hwnd, &rc);
3485 if ((rc.right - rc.left !=
3486 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3487 || (rc.bottom - rc.top !=
3488 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3489 return TRUE;
3490 }
3491 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492}
3493
3494/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003495 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3496 * is set. Compute the new Rows and Columns. This is like resizing the
3497 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498 */
3499 void
3500gui_mch_newfont(void)
3501{
3502 RECT rect;
3503
3504 GetWindowRect(s_hwnd, &rect);
3505 if (win_socket_id == 0)
3506 {
3507 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003508 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3509 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003511 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3512 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3513 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3514 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515 }
3516 else
3517 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003518 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003520 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003521 }
3522}
3523
3524/*
3525 * Set the window title
3526 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 void
3528gui_mch_settitle(
3529 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003530 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531{
3532 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3533}
3534
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003535#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003536// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3537// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003538static LPCSTR mshape_idcs[] =
3539{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003540 IDC_ARROW, // arrow
3541 MAKEINTRESOURCE(0), // blank
3542 IDC_IBEAM, // beam
3543 IDC_SIZENS, // updown
3544 IDC_SIZENS, // udsizing
3545 IDC_SIZEWE, // leftright
3546 IDC_SIZEWE, // lrsizing
3547 IDC_WAIT, // busy
3548 IDC_NO, // no
3549 IDC_ARROW, // crosshair
3550 IDC_ARROW, // hand1
3551 IDC_ARROW, // hand2
3552 IDC_ARROW, // pencil
3553 IDC_ARROW, // question
3554 IDC_ARROW, // right-arrow
3555 IDC_UPARROW, // up-arrow
3556 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557};
3558
3559 void
3560mch_set_mouse_shape(int shape)
3561{
3562 LPCSTR idc;
3563
3564 if (shape == MSHAPE_HIDE)
3565 ShowCursor(FALSE);
3566 else
3567 {
3568 if (shape >= MSHAPE_NUMBERED)
3569 idc = IDC_ARROW;
3570 else
3571 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003572 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 if (!p_mh)
3574 {
3575 POINT mp;
3576
Bram Moolenaar734a8672019-12-02 22:49:38 +01003577 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578 (void)GetCursorPos((LPPOINT)&mp);
3579 (void)SetCursorPos(mp.x, mp.y);
3580 ShowCursor(TRUE);
3581 }
3582 }
3583}
3584#endif
3585
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003586#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588 * Wide version of convert_filter().
3589 */
3590 static WCHAR *
3591convert_filterW(char_u *s)
3592{
3593 char_u *tmp;
3594 int len;
3595 WCHAR *res;
3596
3597 tmp = convert_filter(s);
3598 if (tmp == NULL)
3599 return NULL;
3600 len = (int)STRLEN(s) + 3;
3601 res = enc_to_utf16(tmp, &len);
3602 vim_free(tmp);
3603 return res;
3604}
3605
3606/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003607 * Pop open a file browser and return the file selected, in allocated memory,
3608 * or NULL if Cancel is hit.
3609 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3610 * title - Title message for the file browser dialog.
3611 * dflt - Default name of file.
3612 * ext - Default extension to be added to files without extensions.
3613 * initdir - directory in which to open the browser (NULL = current dir)
3614 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003616 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003617gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618 int saving,
3619 char_u *title,
3620 char_u *dflt,
3621 char_u *ext,
3622 char_u *initdir,
3623 char_u *filter)
3624{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003625 // We always use the wide function. This means enc_to_utf16() must work,
3626 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 OPENFILENAMEW fileStruct;
3628 WCHAR fileBuf[MAXPATHL];
3629 WCHAR *wp;
3630 int i;
3631 WCHAR *titlep = NULL;
3632 WCHAR *extp = NULL;
3633 WCHAR *initdirp = NULL;
3634 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003635 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003636
3637 if (dflt == NULL)
3638 fileBuf[0] = NUL;
3639 else
3640 {
3641 wp = enc_to_utf16(dflt, NULL);
3642 if (wp == NULL)
3643 fileBuf[0] = NUL;
3644 else
3645 {
3646 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3647 fileBuf[i] = wp[i];
3648 fileBuf[i] = NUL;
3649 vim_free(wp);
3650 }
3651 }
3652
Bram Moolenaar734a8672019-12-02 22:49:38 +01003653 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003654 filterp = convert_filterW(filter);
3655
Bram Moolenaara80faa82020-04-12 19:37:17 +02003656 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003657# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003658 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003659 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003660# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003661 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003662# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663
3664 if (title != NULL)
3665 titlep = enc_to_utf16(title, NULL);
3666 fileStruct.lpstrTitle = titlep;
3667
3668 if (ext != NULL)
3669 extp = enc_to_utf16(ext, NULL);
3670 fileStruct.lpstrDefExt = extp;
3671
3672 fileStruct.lpstrFile = fileBuf;
3673 fileStruct.nMaxFile = MAXPATHL;
3674 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003675 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3676 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 if (initdir != NULL && *initdir != NUL)
3678 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003679 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680 initdirp = enc_to_utf16(initdir, NULL);
3681 if (initdirp != NULL)
3682 {
3683 for (wp = initdirp; *wp != NUL; ++wp)
3684 if (*wp == '/')
3685 *wp = '\\';
3686 }
3687 fileStruct.lpstrInitialDir = initdirp;
3688 }
3689
3690 /*
3691 * TODO: Allow selection of multiple files. Needs another arg to this
3692 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3693 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3694 * files that don't exist yet, so I haven't put it in. What about
3695 * OFN_PATHMUSTEXIST?
3696 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3697 */
3698 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003699# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 if (curbuf->b_p_bin)
3701 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003702# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003703 if (saving)
3704 {
3705 if (!GetSaveFileNameW(&fileStruct))
3706 return NULL;
3707 }
3708 else
3709 {
3710 if (!GetOpenFileNameW(&fileStruct))
3711 return NULL;
3712 }
3713
3714 vim_free(filterp);
3715 vim_free(initdirp);
3716 vim_free(titlep);
3717 vim_free(extp);
3718
Bram Moolenaar734a8672019-12-02 22:49:38 +01003719 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003721 if (p == NULL)
3722 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725 SetFocus(s_hwnd);
3726
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003728 q = vim_strsave(shorten_fname1(p));
3729 vim_free(p);
3730 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003731}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732
3733
3734/*
3735 * Convert the string s to the proper format for a filter string by replacing
3736 * the \t and \n delimiters with \0.
3737 * Returns the converted string in allocated memory.
3738 *
3739 * Keep in sync with convert_filterW() above!
3740 */
3741 static char_u *
3742convert_filter(char_u *s)
3743{
3744 char_u *res;
3745 unsigned s_len = (unsigned)STRLEN(s);
3746 unsigned i;
3747
3748 res = alloc(s_len + 3);
3749 if (res != NULL)
3750 {
3751 for (i = 0; i < s_len; ++i)
3752 if (s[i] == '\t' || s[i] == '\n')
3753 res[i] = '\0';
3754 else
3755 res[i] = s[i];
3756 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003757 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 res[s_len + 1] = NUL;
3759 res[s_len + 2] = NUL;
3760 }
3761 return res;
3762}
3763
3764/*
3765 * Select a directory.
3766 */
3767 char_u *
3768gui_mch_browsedir(char_u *title, char_u *initdir)
3769{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003770 // We fake this: Use a filter that doesn't select anything and a default
3771 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3773 initdir, (char_u *)_("Directory\t*.nothing\n"));
3774}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003775#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777 static void
3778_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003779 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 HDROP hDrop)
3781{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003782#define BUFPATHLEN _MAX_PATH
3783#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785 char szFile[BUFPATHLEN];
3786 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3787 UINT i;
3788 char_u **fnames;
3789 POINT pt;
3790 int_u modifiers = 0;
3791
Bram Moolenaar734a8672019-12-02 22:49:38 +01003792 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793
Bram Moolenaar734a8672019-12-02 22:49:38 +01003794 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 DragQueryPoint(hDrop, &pt);
3796 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3797
3798 reset_VIsual();
3799
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003800 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801
3802 if (fnames != NULL)
3803 for (i = 0; i < cFiles; ++i)
3804 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3806 fnames[i] = utf16_to_enc(wszFile, NULL);
3807 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808 {
3809 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3810 fnames[i] = vim_strsave((char_u *)szFile);
3811 }
3812 }
3813
3814 DragFinish(hDrop);
3815
3816 if (fnames != NULL)
3817 {
3818 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3819 modifiers |= MOUSE_SHIFT;
3820 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3821 modifiers |= MOUSE_CTRL;
3822 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3823 modifiers |= MOUSE_ALT;
3824
3825 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3826
3827 s_need_activate = TRUE;
3828 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829}
3830
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003831 static int
3832_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003833 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834 HWND hwndCtl,
3835 UINT code,
3836 int pos)
3837{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003838 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 scrollbar_T *sb, *sb_info;
3840 long val;
3841 int dragging = FALSE;
3842 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843 SCROLLINFO si;
3844
3845 si.cbSize = sizeof(si);
3846 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847
3848 sb = gui_mswin_find_scrollbar(hwndCtl);
3849 if (sb == NULL)
3850 return 0;
3851
Bram Moolenaar734a8672019-12-02 22:49:38 +01003852 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853 {
3854 /*
3855 * Careful: need to get scrollbar info out of first (left) scrollbar
3856 * for window, but keep real scrollbar too because we must pass it to
3857 * gui_drag_scrollbar().
3858 */
3859 sb_info = &sb->wp->w_scrollbars[0];
3860 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003861 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 sb_info = sb;
3863 val = sb_info->value;
3864
3865 switch (code)
3866 {
3867 case SB_THUMBTRACK:
3868 val = pos;
3869 dragging = TRUE;
3870 if (sb->scroll_shift > 0)
3871 val <<= sb->scroll_shift;
3872 break;
3873 case SB_LINEDOWN:
3874 val++;
3875 break;
3876 case SB_LINEUP:
3877 val--;
3878 break;
3879 case SB_PAGEDOWN:
3880 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3881 break;
3882 case SB_PAGEUP:
3883 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3884 break;
3885 case SB_TOP:
3886 val = 0;
3887 break;
3888 case SB_BOTTOM:
3889 val = sb_info->max;
3890 break;
3891 case SB_ENDSCROLL:
3892 if (prev_code == SB_THUMBTRACK)
3893 {
3894 /*
3895 * "pos" only gives us 16-bit data. In case of large file,
3896 * use GetScrollPos() which returns 32-bit. Unfortunately it
3897 * is not valid while the scrollbar is being dragged.
3898 */
3899 val = GetScrollPos(hwndCtl, SB_CTL);
3900 if (sb->scroll_shift > 0)
3901 val <<= sb->scroll_shift;
3902 }
3903 break;
3904
3905 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003906 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907 return 0;
3908 }
3909 prev_code = code;
3910
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003911 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3912 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913
3914 /*
3915 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3916 */
3917 if (sb->wp != NULL)
3918 {
3919 scrollbar_T *sba = sb->wp->w_scrollbars;
3920 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3921
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003923 }
3924
Bram Moolenaar734a8672019-12-02 22:49:38 +01003925 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003926 s_busy_processing = TRUE;
3927
Bram Moolenaar734a8672019-12-02 22:49:38 +01003928 // When "allow_scrollbar" is FALSE still need to remember the new
3929 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003930 dont_scroll = !allow_scrollbar;
3931
Bram Moolenaara338adc2018-01-31 20:51:47 +01003932 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003934 mch_enable_flush();
3935 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003936
3937 s_busy_processing = FALSE;
3938 dont_scroll = dont_scroll_save;
3939
3940 return 0;
3941}
3942
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#ifdef FEAT_XPM_W32
3945# include "xpm_w32.h"
3946#endif
3947
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#ifdef __MINGW32__
3949/*
3950 * Add a lot of missing defines.
3951 * They are not always missing, we need the #ifndef's.
3952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953# ifndef IsMinimized
3954# define IsMinimized(hwnd) IsIconic(hwnd)
3955# endif
3956# ifndef IsMaximized
3957# define IsMaximized(hwnd) IsZoomed(hwnd)
3958# endif
3959# ifndef SelectFont
3960# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3961# endif
3962# ifndef GetStockBrush
3963# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3964# endif
3965# ifndef DeleteBrush
3966# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3967# endif
3968
3969# ifndef HANDLE_WM_RBUTTONDBLCLK
3970# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3971 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3972# endif
3973# ifndef HANDLE_WM_MBUTTONUP
3974# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3975 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3976# endif
3977# ifndef HANDLE_WM_MBUTTONDBLCLK
3978# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3979 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3980# endif
3981# ifndef HANDLE_WM_LBUTTONDBLCLK
3982# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3983 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3984# endif
3985# ifndef HANDLE_WM_RBUTTONDOWN
3986# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3987 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3988# endif
3989# ifndef HANDLE_WM_MOUSEMOVE
3990# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3991 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3992# endif
3993# ifndef HANDLE_WM_RBUTTONUP
3994# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3995 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3996# endif
3997# ifndef HANDLE_WM_MBUTTONDOWN
3998# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3999 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4000# endif
4001# ifndef HANDLE_WM_LBUTTONUP
4002# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4003 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4004# endif
4005# ifndef HANDLE_WM_LBUTTONDOWN
4006# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4007 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4008# endif
4009# ifndef HANDLE_WM_SYSCHAR
4010# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4011 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4012# endif
4013# ifndef HANDLE_WM_ACTIVATEAPP
4014# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4015 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4016# endif
4017# ifndef HANDLE_WM_WINDOWPOSCHANGING
4018# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4019 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4020# endif
4021# ifndef HANDLE_WM_VSCROLL
4022# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4023 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4024# endif
4025# ifndef HANDLE_WM_SETFOCUS
4026# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4027 ((fn)((hwnd), (HWND)(wParam)), 0L)
4028# endif
4029# ifndef HANDLE_WM_KILLFOCUS
4030# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4031 ((fn)((hwnd), (HWND)(wParam)), 0L)
4032# endif
4033# ifndef HANDLE_WM_HSCROLL
4034# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4035 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4036# endif
4037# ifndef HANDLE_WM_DROPFILES
4038# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4039 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4040# endif
4041# ifndef HANDLE_WM_CHAR
4042# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4043 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4044# endif
4045# ifndef HANDLE_WM_SYSDEADCHAR
4046# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4047 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4048# endif
4049# ifndef HANDLE_WM_DEADCHAR
4050# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4051 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4052# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004053#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055
Bram Moolenaar734a8672019-12-02 22:49:38 +01004056// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#define TEAROFF_PADDING_X 2
4058#define TEAROFF_BUTTON_PAD_X 8
4059#define TEAROFF_MIN_WIDTH 200
4060#define TEAROFF_SUBMENU_LABEL ">>"
4061#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4062
4063
Bram Moolenaar734a8672019-12-02 22:49:38 +01004064// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004066# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#endif
4068
4069
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004070#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071# define ID_BEVAL_TOOLTIP 200
4072# define BEVAL_TEXT_LEN MAXPATHL
4073
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004074# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004075// Work around old versions of basetsd.h which wrongly declares
4076// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004077# undef UINT_PTR
4078# define UINT_PTR UINT
4079# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004082static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004084
Bram Moolenaar82881492012-11-20 16:53:39 +01004085
Bram Moolenaar734a8672019-12-02 22:49:38 +01004086// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004087# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004088
Bram Moolenaar45360022005-07-21 21:08:21 +00004089/*
4090 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004091 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004092 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004093# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004094
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004095# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004096
4097typedef struct _DllVersionInfo
4098{
4099 DWORD cbSize;
4100 DWORD dwMajorVersion;
4101 DWORD dwMinorVersion;
4102 DWORD dwBuildNumber;
4103 DWORD dwPlatformID;
4104} DLLVERSIONINFO;
4105
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004106# ifndef PROTO
4107# include <poppack.h>
4108# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004109
Bram Moolenaar45360022005-07-21 21:08:21 +00004110typedef struct tagTOOLINFOA_NEW
4111{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004112 UINT cbSize;
4113 UINT uFlags;
4114 HWND hwnd;
4115 UINT_PTR uId;
4116 RECT rect;
4117 HINSTANCE hinst;
4118 LPSTR lpszText;
4119 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004120} TOOLINFO_NEW;
4121
4122typedef struct tagNMTTDISPINFO_NEW
4123{
4124 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004125 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004126 char szText[80];
4127 HINSTANCE hinst;
4128 UINT uFlags;
4129 LPARAM lParam;
4130} NMTTDISPINFO_NEW;
4131
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004132typedef struct tagTOOLINFOW_NEW
4133{
4134 UINT cbSize;
4135 UINT uFlags;
4136 HWND hwnd;
4137 UINT_PTR uId;
4138 RECT rect;
4139 HINSTANCE hinst;
4140 LPWSTR lpszText;
4141 LPARAM lParam;
4142 void *lpReserved;
4143} TOOLINFOW_NEW;
4144
4145typedef struct tagNMTTDISPINFOW_NEW
4146{
4147 NMHDR hdr;
4148 LPWSTR lpszText;
4149 WCHAR szText[80];
4150 HINSTANCE hinst;
4151 UINT uFlags;
4152 LPARAM lParam;
4153} NMTTDISPINFOW_NEW;
4154
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004155
Bram Moolenaar45360022005-07-21 21:08:21 +00004156typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004157# ifndef TTM_SETMAXTIPWIDTH
4158# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004161# ifndef TTF_DI_SETITEM
4162# define TTF_DI_SETITEM 0x8000
4163# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004164
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004165# ifndef TTN_GETDISPINFO
4166# define TTN_GETDISPINFO (TTN_FIRST - 0)
4167# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004168
Bram Moolenaar734a8672019-12-02 22:49:38 +01004169#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004170
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004171#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004172// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4173// it here if LPNMTTDISPINFO isn't defined.
K.Takatac81e9bf2022-01-16 14:15:49 +00004174// MinGW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
Bram Moolenaar734a8672019-12-02 22:49:38 +01004175// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004176# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4177typedef struct tagNMTTDISPINFOA {
4178 NMHDR hdr;
4179 LPSTR lpszText;
4180 char szText[80];
4181 HINSTANCE hinst;
4182 UINT uFlags;
4183 LPARAM lParam;
4184} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4185# define LPNMTTDISPINFO LPNMTTDISPINFOA
4186
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004187typedef struct tagNMTTDISPINFOW {
4188 NMHDR hdr;
4189 LPWSTR lpszText;
4190 WCHAR szText[80];
4191 HINSTANCE hinst;
4192 UINT uFlags;
4193 LPARAM lParam;
4194} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004195# endif
4196#endif
4197
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004198#ifndef TTN_GETDISPINFOW
4199# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4200#endif
4201
Bram Moolenaar734a8672019-12-02 22:49:38 +01004202// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204#ifdef FEAT_MENU
4205static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208/*
4209 * Use the system font for dialogs and tear-off menus. Remove this line to
4210 * use DLG_FONT_NAME.
4211 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004212#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#define VIM_CLASSW L"Vim"
4216
Bram Moolenaar734a8672019-12-02 22:49:38 +01004217// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4218// thus there should be room for every dialog. For tearoffs it's made bigger
4219// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#define DLG_ALLOC_SIZE 16 * 1024
4221
4222/*
4223 * stuff for dialogs, menus, tearoffs etc.
4224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225static PWORD
4226add_dialog_element(
4227 PWORD p,
4228 DWORD lStyle,
4229 WORD x,
4230 WORD y,
4231 WORD w,
4232 WORD h,
4233 WORD Id,
4234 WORD clss,
4235 const char *caption);
4236static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004237static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004238#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241static void get_dialog_font_metrics(void);
4242
4243static int dialog_default_button = -1;
4244
Bram Moolenaar734a8672019-12-02 22:49:38 +01004245// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
Bram Moolenaar734a8672019-12-02 22:49:38 +01004248static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#ifdef FEAT_TOOLBAR
4250static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004251static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004252static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004254#else
4255# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#endif
4257
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004258#ifdef FEAT_GUI_TABLINE
4259static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004260static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004261#endif
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef FEAT_MBYTE_IME
4264static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4265static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4266#endif
4267#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4268# ifdef NOIME
4269typedef struct tagCOMPOSITIONFORM {
4270 DWORD dwStyle;
4271 POINT ptCurrentPos;
4272 RECT rcArea;
4273} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4274typedef HANDLE HIMC;
4275# endif
4276
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004277static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004278static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4279static HIMC (WINAPI *pImmGetContext)(HWND);
4280static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4281static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4282static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4283static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004284static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4285static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004286static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4287static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004288static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289static void dyn_imm_load(void);
4290#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291# define pImmGetCompositionStringW ImmGetCompositionStringW
4292# define pImmGetContext ImmGetContext
4293# define pImmAssociateContext ImmAssociateContext
4294# define pImmReleaseContext ImmReleaseContext
4295# define pImmGetOpenStatus ImmGetOpenStatus
4296# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004297# define pImmGetCompositionFontW ImmGetCompositionFontW
4298# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299# define pImmSetCompositionWindow ImmSetCompositionWindow
4300# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004301# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif
4303
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304#ifdef FEAT_MENU
4305/*
4306 * Figure out how high the menu bar is at the moment.
4307 */
4308 static int
4309gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004310 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 static int old_menu_height = -1;
4313
4314 RECT rc1, rc2;
4315 int num;
4316 int menu_height;
4317
4318 if (gui.menu_is_active)
4319 num = GetMenuItemCount(s_menuBar);
4320 else
4321 num = 0;
4322
4323 if (num == 0)
4324 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004325 else if (IsMinimized(s_hwnd))
4326 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004327 // The height of the menu cannot be determined while the window is
4328 // minimized. Take the previous height if the menu is changed in that
4329 // state, to avoid that Vim's vertical window size accidentally
4330 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004331 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 else
4334 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004335 /*
4336 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4337 * seem to have been set yet, so menu wraps in default window
4338 * width which is very narrow. Instead just return height of a
4339 * single menu item. Will still be wrong when the menu really
4340 * should wrap over more than one line.
4341 */
4342 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4343 if (gui.starting)
4344 menu_height = rc1.bottom - rc1.top + 1;
4345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004347 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4348 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 }
4351
4352 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004353 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004354 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 return menu_height;
4357}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004358#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360
4361/*
4362 * Setup for the Intellimouse
4363 */
4364 static void
4365init_mouse_wheel(void)
4366{
4367
4368#ifndef SPI_GETWHEELSCROLLLINES
4369# define SPI_GETWHEELSCROLLLINES 104
4370#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004371#ifndef SPI_SETWHEELSCROLLLINES
4372# define SPI_SETWHEELSCROLLLINES 105
4373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
Bram Moolenaar734a8672019-12-02 22:49:38 +01004375#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4376#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4378#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4379
Bram Moolenaar734a8672019-12-02 22:49:38 +01004380 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaar734a8672019-12-02 22:49:38 +01004382 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004383 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4384 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385}
4386
4387
Bram Moolenaarae20f342019-11-05 21:09:23 +01004388/*
4389 * Intellimouse wheel handler.
4390 * Treat a mouse wheel event as if it were a scroll request.
4391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 static void
4393_OnMouseWheel(
4394 HWND hwnd,
4395 short zDelta)
4396{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 int i;
4398 int size;
4399 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004400 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (mouse_scroll_lines == 0)
4403 init_mouse_wheel();
4404
Bram Moolenaarae20f342019-11-05 21:09:23 +01004405 wp = gui_mouse_window(FIND_POPUP);
4406
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004407#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004408 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004409 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004410 cmdarg_T cap;
4411 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004412
Bram Moolenaarae20f342019-11-05 21:09:23 +01004413 // Mouse hovers over popup window, scroll it if possible.
4414 mouse_row = wp->w_winrow;
4415 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004416 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004417 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4418 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4419 clear_oparg(&oa);
4420 cap.oap = &oa;
4421 nv_mousescroll(&cap);
4422 update_screen(0);
4423 setcursor();
4424 out_flush();
4425 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004426 }
4427#endif
4428
Bram Moolenaarae20f342019-11-05 21:09:23 +01004429 if (wp == NULL || !p_scf)
4430 wp = curwin;
4431
4432 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4433 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4434 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4435 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4436 else
4437 return;
4438 size = wp->w_height;
4439
Bram Moolenaara338adc2018-01-31 20:51:47 +01004440 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (mouse_scroll_lines > 0
4442 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4443 {
4444 for (i = mouse_scroll_lines; i > 0; --i)
4445 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4446 }
4447 else
4448 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004449 mch_enable_flush();
4450 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451}
4452
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004453#ifdef USE_SYSMENU_FONT
4454/*
4455 * Get Menu Font.
4456 * Return OK or FAIL.
4457 */
4458 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004459gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004460{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004461 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004462
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004463 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4464 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004465 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004466 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004467 &nm,
4468 0))
4469 return FAIL;
4470 *lf = nm.lfMenuFont;
4471 return OK;
4472}
4473#endif
4474
4475
4476#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4477/*
4478 * Set the GUI tabline font to the system menu font
4479 */
4480 static void
4481set_tabline_font(void)
4482{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004483 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004484 HFONT font;
4485 HWND hwnd;
4486 HDC hdc;
4487 HFONT hfntOld;
4488 TEXTMETRIC tm;
4489
4490 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4491 return;
4492
K.Takatac81e9bf2022-01-16 14:15:49 +00004493 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004494 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004495
4496 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4497
4498 /*
4499 * Compute the height of the font used for the tab text
4500 */
4501 hwnd = GetDesktopWindow();
4502 hdc = GetWindowDC(hwnd);
4503 hfntOld = SelectFont(hdc, font);
4504
4505 GetTextMetrics(hdc, &tm);
4506
4507 SelectFont(hdc, hfntOld);
4508 ReleaseDC(hwnd, hdc);
4509
4510 /*
4511 * The space used by the tab border and the space between the tab label
4512 * and the tab border is included as 7.
4513 */
4514 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4515}
K.Takatac81e9bf2022-01-16 14:15:49 +00004516#else
4517# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004518#endif
4519
Bram Moolenaar520470a2005-06-16 21:59:56 +00004520/*
4521 * Invoked when a setting was changed.
4522 */
4523 static LRESULT CALLBACK
4524_OnSettingChange(UINT n)
4525{
4526 if (n == SPI_SETWHEELSCROLLLINES)
4527 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4528 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004529 if (n == SPI_SETNONCLIENTMETRICS)
4530 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004531 return 0;
4532}
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534#ifdef FEAT_NETBEANS_INTG
4535 static void
4536_OnWindowPosChanged(
4537 HWND hwnd,
4538 const LPWINDOWPOS lpwpos)
4539{
4540 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004541 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
4543 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4544 || lpwpos->cx != cx || lpwpos->cy != cy))
4545 {
4546 x = lpwpos->x;
4547 y = lpwpos->y;
4548 cx = lpwpos->cx;
4549 cy = lpwpos->cy;
4550 netbeans_frame_moved(x, y);
4551 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004552 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4554}
4555#endif
4556
K.Takata4f2417f2021-06-05 16:25:32 +02004557
4558static HWND hwndTip = NULL;
4559
4560 static void
4561show_sizing_tip(int cols, int rows)
4562{
4563 TOOLINFOA ti = {sizeof(ti)};
4564 char buf[32];
4565
4566 ti.hwnd = s_hwnd;
4567 ti.uId = (UINT_PTR)s_hwnd;
4568 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4569 ti.lpszText = buf;
4570 sprintf(buf, "%dx%d", cols, rows);
4571 if (hwndTip == NULL)
4572 {
4573 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4574 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4575 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4576 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4577 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4578 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4579 }
4580 else
4581 {
4582 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4583 }
4584 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4585}
4586
4587 static void
4588destroy_sizing_tip(void)
4589{
4590 if (hwndTip != NULL)
4591 {
4592 DestroyWindow(hwndTip);
4593 hwndTip = NULL;
4594 }
4595}
4596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 static int
4598_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 UINT fwSide,
4600 LPRECT lprc)
4601{
4602 int w, h;
4603 int valid_w, valid_h;
4604 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004605 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 w = lprc->right - lprc->left;
4608 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004609 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 w_offset = w - valid_w;
4611 h_offset = h - valid_h;
4612
4613 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4614 || fwSide == WMSZ_BOTTOMLEFT)
4615 lprc->left += w_offset;
4616 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4617 || fwSide == WMSZ_BOTTOMRIGHT)
4618 lprc->right -= w_offset;
4619
4620 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4621 || fwSide == WMSZ_TOPRIGHT)
4622 lprc->top += h_offset;
4623 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4624 || fwSide == WMSZ_BOTTOMRIGHT)
4625 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004626
4627 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 return TRUE;
4629}
4630
K.Takatac81e9bf2022-01-16 14:15:49 +00004631 static LRESULT
4632_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4633{
4634 s_dpi = ydpi;
4635 s_in_dpichanged = TRUE;
4636 //TRACE("DPI: %d", ydpi);
4637
4638 update_scrollbar_size();
4639 update_toolbar_size();
4640 set_tabline_font();
4641
4642 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4643 gui_get_wide_font();
4644 gui_mswin_get_menu_height(FALSE);
4645#ifdef FEAT_MBYTE_IME
4646 im_set_position(gui.row, gui.col);
4647#endif
4648 InvalidateRect(hwnd, NULL, TRUE);
4649
4650 s_in_dpichanged = FALSE;
4651 return 0L;
4652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654
4655 static LRESULT CALLBACK
4656_WndProc(
4657 HWND hwnd,
4658 UINT uMsg,
4659 WPARAM wParam,
4660 LPARAM lParam)
4661{
4662 /*
4663 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4664 hwnd, uMsg, wParam, lParam);
4665 */
4666
4667 HandleMouseHide(uMsg, lParam);
4668
4669 s_uMsg = uMsg;
4670 s_wParam = wParam;
4671 s_lParam = lParam;
4672
4673 switch (uMsg)
4674 {
4675 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4676 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004677 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004679 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4681 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4682 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4683 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4684#ifdef FEAT_MENU
4685 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4686#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004687 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4688 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4690 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004691 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4692 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4694 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4695 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4696#ifdef FEAT_NETBEANS_INTG
4697 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4698#endif
4699
Bram Moolenaarafa24992006-03-27 20:58:26 +00004700#ifdef FEAT_GUI_TABLINE
4701 case WM_RBUTTONUP:
4702 {
4703 if (gui_mch_showing_tabline())
4704 {
4705 POINT pt;
4706 RECT rect;
4707
4708 /*
4709 * If the cursor is on the tabline, display the tab menu
4710 */
4711 GetCursorPos((LPPOINT)&pt);
4712 GetWindowRect(s_textArea, &rect);
4713 if (pt.y < rect.top)
4714 {
4715 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004716 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004717 }
4718 }
4719 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4720 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004721 case WM_LBUTTONDBLCLK:
4722 {
4723 /*
4724 * If the user double clicked the tabline, create a new tab
4725 */
4726 if (gui_mch_showing_tabline())
4727 {
4728 POINT pt;
4729 RECT rect;
4730
4731 GetCursorPos((LPPOINT)&pt);
4732 GetWindowRect(s_textArea, &rect);
4733 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004734 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004735 }
4736 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4737 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004738#endif
4739
Bram Moolenaar734a8672019-12-02 22:49:38 +01004740 case WM_QUERYENDSESSION: // System wants to go down.
4741 gui_shell_closed(); // Will exit when no changed buffers.
4742 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004745 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004748 return 0L;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 break;
4751
4752 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004753 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4754 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004755 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 return 0L;
4757
4758 case WM_SYSCHAR:
4759 /*
4760 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4761 * shortcut key, handle like a typed ALT key, otherwise call Windows
4762 * ALT key handling.
4763 */
4764#ifdef FEAT_MENU
4765 if ( !gui.menu_is_active
4766 || p_wak[0] == 'n'
4767 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4768 )
4769#endif
4770 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004771 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 return 0L;
4773 }
4774#ifdef FEAT_MENU
4775 else
4776 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4777#endif
4778
4779 case WM_SYSKEYUP:
4780#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004781 // This used to be done only when menu is active: ALT key is used for
4782 // that. But that caused problems when menu is disabled and using
4783 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4784 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4786#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004787 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788#endif
4789
K.Takata4f2417f2021-06-05 16:25:32 +02004790 case WM_EXITSIZEMOVE:
4791 destroy_sizing_tip();
4792 break;
4793
Bram Moolenaar734a8672019-12-02 22:49:38 +01004794 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 case WM_MOUSEWHEEL:
4798 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004799 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar734a8672019-12-02 22:49:38 +01004801 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004802 case WM_SETTINGCHANGE:
4803 return _OnSettingChange((UINT)wParam);
4804
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004805#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 case WM_NOTIFY:
4807 switch (((LPNMHDR) lParam)->code)
4808 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004809 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004810 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004812 LPNMHDR hdr = (LPNMHDR)lParam;
4813 char_u *str = NULL;
4814 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004815
Bram Moolenaard23a8232018-02-10 18:45:26 +01004816 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004817
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004818# ifdef FEAT_GUI_TABLINE
4819 if (gui_mch_showing_tabline()
4820 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004821 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004822 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004823 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004824 * Mouse is over the GUI tabline. Display the
4825 * tooltip for the tab under the cursor
4826 *
4827 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004828 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004829 GetCursorPos(&pt);
4830 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004831 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004832 TCHITTESTINFO htinfo;
4833 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004834
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004835 /*
4836 * Get the tab under the cursor
4837 */
4838 htinfo.pt.x = pt.x;
4839 htinfo.pt.y = pt.y;
4840 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4841 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004842 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004843 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004844
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004845 tp = find_tabpage(idx + 1);
4846 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004847 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004848 get_tabline_label(tp, TRUE);
4849 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004850 }
4851 }
4852 }
4853 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004854# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004855# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004856# ifdef FEAT_GUI_TABLINE
4857 else
4858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004860 UINT idButton;
4861 vimmenu_T *pMenu;
4862
4863 idButton = (UINT) hdr->idFrom;
4864 pMenu = gui_mswin_find_menu(root_menu, idButton);
4865 if (pMenu)
4866 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004868# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004869 if (str != NULL)
4870 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004871 if (hdr->code == TTN_GETDISPINFOW)
4872 {
4873 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4874
Bram Moolenaar734a8672019-12-02 22:49:38 +01004875 // Set the maximum width, this also enables using
4876 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004877 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4878 0, 500);
4879
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004880 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004881 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004882 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004883 }
4884 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004885 {
4886 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4887
Bram Moolenaar734a8672019-12-02 22:49:38 +01004888 // Set the maximum width, this also enables using
4889 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004890 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4891 0, 500);
4892
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004893 if (STRLEN(str) < sizeof(lpdi->szText)
4894 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004895 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004896 sizeof(lpdi->szText) - 1);
4897 else
4898 lpdi->lpszText = tt_text;
4899 }
4900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004903# ifdef FEAT_GUI_TABLINE
4904 case TCN_SELCHANGE:
4905 if (gui_mch_showing_tabline()
4906 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004907 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004908 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004909 return 0L;
4910 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004911 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004912
4913 case NM_RCLICK:
4914 if (gui_mch_showing_tabline()
4915 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004916 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004917 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004918 return 0L;
4919 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004920 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004923# ifdef FEAT_GUI_TABLINE
4924 if (gui_mch_showing_tabline()
4925 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4926 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 break;
4929 }
4930 break;
4931#endif
4932#if defined(MENUHINTS) && defined(FEAT_MENU)
4933 case WM_MENUSELECT:
4934 if (((UINT) HIWORD(wParam)
4935 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4936 == MF_HILITE
4937 && (State & CMDLINE) == 0)
4938 {
4939 UINT idButton;
4940 vimmenu_T *pMenu;
4941 static int did_menu_tip = FALSE;
4942
4943 if (did_menu_tip)
4944 {
4945 msg_clr_cmdline();
4946 setcursor();
4947 out_flush();
4948 did_menu_tip = FALSE;
4949 }
4950
4951 idButton = (UINT)LOWORD(wParam);
4952 pMenu = gui_mswin_find_menu(root_menu, idButton);
4953 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4954 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4955 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004956 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004957 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004958 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 setcursor();
4960 out_flush();
4961 did_menu_tip = TRUE;
4962 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004963 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 break;
4966#endif
4967 case WM_NCHITTEST:
4968 {
4969 LRESULT result;
4970 int x, y;
4971 int xPos = GET_X_LPARAM(lParam);
4972
4973 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4974 if (result == HTCLIENT)
4975 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004976#ifdef FEAT_GUI_TABLINE
4977 if (gui_mch_showing_tabline())
4978 {
4979 int yPos = GET_Y_LPARAM(lParam);
4980 RECT rct;
4981
Bram Moolenaar734a8672019-12-02 22:49:38 +01004982 // If the cursor is on the GUI tabline, don't process this
4983 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004984 GetWindowRect(s_textArea, &rct);
4985 if (yPos < rct.top)
4986 return result;
4987 }
4988#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004989 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 xPos -= x;
4991
Bram Moolenaar734a8672019-12-02 22:49:38 +01004992 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 return HTBOTTOMLEFT;
4994 else
4995 return HTBOTTOMRIGHT;
4996 }
4997 else
4998 return result;
4999 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005000 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002#ifdef FEAT_MBYTE_IME
5003 case WM_IME_NOTIFY:
5004 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5005 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005006 return 1L;
5007
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 case WM_IME_COMPOSITION:
5009 if (!_OnImeComposition(hwnd, wParam, lParam))
5010 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005011 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00005013 case WM_DPICHANGED:
5014 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
5015 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005019 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021#endif
5022 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5023 }
5024
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005025 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026}
5027
5028/*
5029 * End of call-back routines
5030 */
5031
Bram Moolenaar734a8672019-12-02 22:49:38 +01005032// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033HWND vim_parent_hwnd = NULL;
5034
5035 static BOOL CALLBACK
5036FindWindowTitle(HWND hwnd, LPARAM lParam)
5037{
5038 char buf[2048];
5039 char *title = (char *)lParam;
5040
5041 if (GetWindowText(hwnd, buf, sizeof(buf)))
5042 {
5043 if (strstr(buf, title) != NULL)
5044 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005045 // Found it. Store the window ref. and quit searching if MDI
5046 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005048 if (vim_parent_hwnd != NULL)
5049 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005052 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054
5055/*
5056 * Invoked for '-P "title"' argument: search for parent application to open
5057 * our window in.
5058 */
5059 void
5060gui_mch_set_parent(char *title)
5061{
5062 EnumWindows(FindWindowTitle, (LPARAM)title);
5063 if (vim_parent_hwnd == NULL)
5064 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005065 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 mch_exit(2);
5067 }
5068}
5069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005070#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 static void
5072ole_error(char *arg)
5073{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005074 char buf[IOSIZE];
5075
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005076# ifdef VIMDLL
5077 gui.in_use = mch_is_gui_executable();
5078# endif
5079
Bram Moolenaar734a8672019-12-02 22:49:38 +01005080 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005081 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005082 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005083 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005087#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5088 static char *
5089gvim_error(void)
5090{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005091 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005092
5093 if (starting)
5094 {
5095 mch_errmsg(msg);
5096 mch_errmsg("\n");
5097 mch_exit(2);
5098 }
5099 return msg;
5100}
5101
5102 char *
5103gui_mch_do_spawn(char_u *arg)
5104{
5105 int len;
5106# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5107 char_u *session = NULL;
5108 LPWSTR tofree1 = NULL;
5109# endif
5110 WCHAR name[MAX_PATH];
5111 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5112 STARTUPINFOW si = {sizeof(si)};
5113 PROCESS_INFORMATION pi;
5114
5115 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5116 goto error;
5117 p = wcsrchr(name, L'\\');
5118 if (p == NULL)
5119 goto error;
5120 // Replace the executable name from vim(d).exe to gvim(d).exe.
5121# ifdef DEBUG
5122 wcscpy(p + 1, L"gvimd.exe");
5123# else
5124 wcscpy(p + 1, L"gvim.exe");
5125# endif
5126
5127# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5128 if (starting)
5129# endif
5130 {
5131 // Pass the command line to the new process.
5132 p = GetCommandLineW();
5133 // Skip 1st argument.
5134 while (*p && *p != L' ' && *p != L'\t')
5135 {
5136 if (*p == L'"')
5137 {
5138 while (*p && *p != L'"')
5139 ++p;
5140 if (*p)
5141 ++p;
5142 }
5143 else
5144 ++p;
5145 }
5146 cmd = p;
5147 }
5148# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5149 else
5150 {
5151 // Create a session file and pass it to the new process.
5152 LPWSTR wsession;
5153 char_u *savebg;
5154 int ret;
5155
5156 session = vim_tempname('s', FALSE);
5157 if (session == NULL)
5158 goto error;
5159 savebg = p_bg;
5160 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5161 ret = write_session_file(session);
5162 vim_free(p_bg);
5163 p_bg = savebg;
5164 if (!ret)
5165 goto error;
5166 wsession = enc_to_utf16(session, NULL);
5167 if (wsession == NULL)
5168 goto error;
5169 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005170 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005171 if (cmd == NULL)
5172 {
5173 vim_free(wsession);
5174 goto error;
5175 }
5176 tofree1 = cmd;
5177 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5178 wsession, wsession);
5179 vim_free(wsession);
5180 }
5181# endif
5182
5183 // Check additional arguments to the `:gui` command.
5184 if (arg != NULL)
5185 {
5186 warg = enc_to_utf16(arg, NULL);
5187 if (warg == NULL)
5188 goto error;
5189 tofree2 = warg;
5190 }
5191 else
5192 warg = L"";
5193
5194 // Set up the new command line.
5195 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005196 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005197 if (newcmd == NULL)
5198 goto error;
5199 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5200
5201 // Spawn a new GUI process.
5202 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5203 NULL, NULL, &si, &pi))
5204 goto error;
5205 CloseHandle(pi.hProcess);
5206 CloseHandle(pi.hThread);
5207 mch_exit(0);
5208
5209error:
5210# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5211 if (session)
5212 mch_remove(session);
5213 vim_free(session);
5214 vim_free(tofree1);
5215# endif
5216 vim_free(newcmd);
5217 vim_free(tofree2);
5218 return gvim_error();
5219}
5220#endif
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222/*
5223 * Parse the GUI related command-line arguments. Any arguments used are
5224 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005225 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
5227 void
5228gui_mch_prepare(int *argc, char **argv)
5229{
5230 int silent = FALSE;
5231 int idx;
5232
Bram Moolenaar734a8672019-12-02 22:49:38 +01005233 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5235 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005236 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5238 && (argv[2][0] == '-' || argv[2][0] == '/'))
5239 {
5240 silent = TRUE;
5241 idx = 2;
5242 }
5243 else
5244 idx = 1;
5245
Bram Moolenaar734a8672019-12-02 22:49:38 +01005246 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (STRICMP(argv[idx] + 1, "register") == 0)
5248 {
5249#ifdef FEAT_OLE
5250 RegisterMe(silent);
5251 mch_exit(0);
5252#else
5253 if (!silent)
5254 ole_error("register");
5255 mch_exit(2);
5256#endif
5257 }
5258
Bram Moolenaar734a8672019-12-02 22:49:38 +01005259 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5261 {
5262#ifdef FEAT_OLE
5263 UnregisterMe(!silent);
5264 mch_exit(0);
5265#else
5266 if (!silent)
5267 ole_error("unregister");
5268 mch_exit(2);
5269#endif
5270 }
5271
Bram Moolenaar734a8672019-12-02 22:49:38 +01005272 // Ignore an -embedding argument. It is only relevant if the
5273 // application wants to treat the case when it is started manually
5274 // differently from the case where it is started via automation (and
5275 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5277 {
5278#ifdef FEAT_OLE
5279 *argc = 1;
5280#else
5281 ole_error("embedding");
5282 mch_exit(2);
5283#endif
5284 }
5285 }
5286
5287#ifdef FEAT_OLE
5288 {
5289 int bDoRestart = FALSE;
5290
5291 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005292 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 if (bDoRestart)
5294 mch_exit(0);
5295 }
5296#endif
5297
5298#ifdef FEAT_NETBEANS_INTG
5299 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005300 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 int arg;
5302
5303 for (arg = 1; arg < *argc; arg++)
5304 if (strncmp("-nb", argv[arg], 3) == 0)
5305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 netbeansArg = argv[arg];
5307 mch_memmove(&argv[arg], &argv[arg + 1],
5308 (--*argc - arg) * sizeof(char *));
5309 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005310 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314}
5315
K.Takatac81e9bf2022-01-16 14:15:49 +00005316 static void
5317load_dpi_func(void)
5318{
5319 HMODULE hUser32;
5320
5321 hUser32 = GetModuleHandle("user32.dll");
5322 if (hUser32 == NULL)
5323 goto fail;
5324
5325 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5326 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5327 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5328 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5329 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5330 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5331
5332 if (pSetThreadDpiAwarenessContext != NULL)
5333 {
5334 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5335 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5336 if (oldctx != NULL)
5337 {
5338 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5339 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5340#ifdef DEBUG
5341 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5342 {
5343 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5344 }
5345#endif
5346 return;
5347 }
5348 }
5349
5350fail:
5351 // Disable PerMonitorV2 APIs.
5352 pGetDpiForSystem = stubGetDpiForSystem;
5353 pGetDpiForWindow = NULL;
5354 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5355 pSetThreadDpiAwarenessContext = NULL;
5356 pGetAwarenessFromDpiAwarenessContext = NULL;
5357}
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359/*
5360 * Initialise the GUI. Create all the windows, set up all the call-backs
5361 * etc.
5362 */
5363 int
5364gui_mch_init(void)
5365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005367 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#ifdef GLOBAL_IME
5370 ATOM atom;
5371#endif
5372
Bram Moolenaar734a8672019-12-02 22:49:38 +01005373 // Return here if the window was already opened (happens when
5374 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005376 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
5378 /*
5379 * Load the tearoff bitmap
5380 */
5381#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005382 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#endif
5384
K.Takatac81e9bf2022-01-16 14:15:49 +00005385 load_dpi_func();
5386
5387 s_dpi = pGetDpiForSystem();
5388 update_scrollbar_size();
5389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005391 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#endif
5393 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005394#ifdef FEAT_TOOLBAR
5395 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5399
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // First try using the wide version, so that we can use any title.
5401 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005402 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005404 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 wndclassw.lpfnWndProc = _WndProc;
5406 wndclassw.cbClsExtra = 0;
5407 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005408 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5410 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5411 wndclassw.hbrBackground = s_brush;
5412 wndclassw.lpszMenuName = NULL;
5413 wndclassw.lpszClassName = szVimWndClassW;
5414
5415 if ((
5416#ifdef GLOBAL_IME
5417 atom =
5418#endif
5419 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 if (vim_parent_hwnd != NULL)
5424 {
5425#ifdef HAVE_TRY_EXCEPT
5426 __try
5427 {
5428#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005429 // Open inside the specified parent window.
5430 // TODO: last argument should point to a CLIENTCREATESTRUCT
5431 // structure.
5432 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005434 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005435 WS_OVERLAPPEDWINDOW | WS_CHILD
5436 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5438 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005439 100, // Any value will do
5440 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005442 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443#ifdef HAVE_TRY_EXCEPT
5444 }
5445 __except(EXCEPTION_EXECUTE_HANDLER)
5446 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005447 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449#endif
5450 if (s_hwnd == NULL)
5451 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005452 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 mch_exit(2);
5454 }
5455 }
5456 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005457 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005458 // If the provided windowid is not valid reset it to zero, so that it
5459 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005460 if (IsWindow((HWND)win_socket_id) <= 0)
5461 win_socket_id = 0;
5462
Bram Moolenaar734a8672019-12-02 22:49:38 +01005463 // Create a window. If win_socket_id is not zero without border and
5464 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005465 s_hwnd = CreateWindowW(
5466 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005467 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5468 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005469 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5470 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005471 100, // Any value will do
5472 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005473 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005474 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005475 if (s_hwnd != NULL && win_socket_id != 0)
5476 {
5477 SetParent(s_hwnd, (HWND)win_socket_id);
5478 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5479 }
5480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 if (s_hwnd == NULL)
5483 return FAIL;
5484
K.Takatac81e9bf2022-01-16 14:15:49 +00005485 if (pGetDpiForWindow != NULL)
5486 {
5487 s_dpi = pGetDpiForWindow(s_hwnd);
5488 update_scrollbar_size();
5489 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5490 }
5491
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#ifdef GLOBAL_IME
5493 global_ime_init(atom, s_hwnd);
5494#endif
5495#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5496 dyn_imm_load();
5497#endif
5498
Bram Moolenaar734a8672019-12-02 22:49:38 +01005499 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005500 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005501 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005502 wndclassw.style = CS_OWNDC;
5503 wndclassw.lpfnWndProc = _TextAreaWndProc;
5504 wndclassw.cbClsExtra = 0;
5505 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005506 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005507 wndclassw.hIcon = NULL;
5508 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5509 wndclassw.hbrBackground = NULL;
5510 wndclassw.lpszMenuName = NULL;
5511 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005512
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005513 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 return FAIL;
5515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005517 s_textArea = CreateWindowExW(
5518 0,
5519 szTextAreaClassW, L"Vim text area",
5520 WS_CHILD | WS_VISIBLE, 0, 0,
5521 100, // Any value will do for now
5522 100, // Any value will do for now
5523 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005524 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (s_textArea == NULL)
5527 return FAIL;
5528
Bram Moolenaar20321902016-02-17 12:30:17 +01005529#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005530 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005531 {
5532 HANDLE hIcon = NULL;
5533
5534 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005535 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005536 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005537#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539#ifdef FEAT_MENU
5540 s_menuBar = CreateMenu();
5541#endif
5542 s_hdc = GetDC(s_textArea);
5543
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaar734a8672019-12-02 22:49:38 +01005546 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005547 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
Bram Moolenaar734a8672019-12-02 22:49:38 +01005549 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 gui_mch_def_colors();
5551
Bram Moolenaar734a8672019-12-02 22:49:38 +01005552 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5553 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 set_normal_colors();
5555
5556 /*
5557 * Check that none of the colors are the same as the background color.
5558 * Then store the current values as the defaults.
5559 */
5560 gui_check_colors();
5561 gui.def_norm_pixel = gui.norm_pixel;
5562 gui.def_back_pixel = gui.back_pixel;
5563
Bram Moolenaar734a8672019-12-02 22:49:38 +01005564 // Get the colors for the highlight groups (gui_check_colors() might have
5565 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 highlight_gui_started();
5567
5568 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005569 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005571 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 /*
5574 * Set up for Intellimouse processing
5575 */
5576 init_mouse_wheel();
5577
5578 /*
5579 * compute a couple of metrics used for the dialogs
5580 */
5581 get_dialog_font_metrics();
5582#ifdef FEAT_TOOLBAR
5583 /*
5584 * Create the toolbar
5585 */
5586 initialise_toolbar();
5587#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005588#ifdef FEAT_GUI_TABLINE
5589 /*
5590 * Create the tabline
5591 */
5592 initialise_tabline();
5593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594#ifdef MSWIN_FIND_REPLACE
5595 /*
5596 * Initialise the dialog box stuff
5597 */
5598 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5599
Bram Moolenaar734a8672019-12-02 22:49:38 +01005600 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005602 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005604 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5606 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5607 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005610#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005611# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005612// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005613# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005614# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005615# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005616# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005617 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005618 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005619#endif
5620
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005621#ifdef FEAT_RENDER_OPTIONS
5622 if (p_rop)
5623 (void)gui_mch_set_rendering_options(p_rop);
5624#endif
5625
Bram Moolenaar748bf032005-02-02 23:04:36 +00005626theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005627 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005628 display_errors();
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 return OK;
5631}
5632
5633/*
5634 * Get the size of the screen, taking position on multiple monitors into
5635 * account (if supported).
5636 */
5637 static void
5638get_work_area(RECT *spi_rect)
5639{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005640 HMONITOR mon;
5641 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar734a8672019-12-02 22:49:38 +01005643 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005644 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005645 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005647 moninfo.cbSize = sizeof(MONITORINFO);
5648 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005650 *spi_rect = moninfo.rcWork;
5651 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
5653 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005654 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5656}
5657
5658/*
5659 * Set the size of the window to the given width and height in pixels.
5660 */
5661 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005662gui_mch_set_shellsize(
5663 int width,
5664 int height,
5665 int min_width UNUSED,
5666 int min_height UNUSED,
5667 int base_width UNUSED,
5668 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005669 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005672 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaar734a8672019-12-02 22:49:38 +01005675 // Try to keep window completely on screen.
5676 // Get position of the screen work area. This is the part that is not
5677 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 get_work_area(&workarea_rect);
5679
Bram Moolenaar734a8672019-12-02 22:49:38 +01005680 // Resizing a maximized window looks very strange, unzoom it first.
5681 // But don't do it when still starting up, it may have been requested in
5682 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005683 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005685
5686 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
Bram Moolenaar734a8672019-12-02 22:49:38 +01005688 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005689 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5690 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5691 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5692 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5693 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5694 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
Bram Moolenaar734a8672019-12-02 22:49:38 +01005696 // The following should take care of keeping Vim on the same monitor, no
5697 // matter if the secondary monitor is left or right of the primary
5698 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005699 window_rect.right = window_rect.left + win_width;
5700 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005701
Bram Moolenaar734a8672019-12-02 22:49:38 +01005702 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005703 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5704 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005706 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5707 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005709 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5710 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005712 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5713 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005715 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5716 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 SetActiveWindow(s_hwnd);
5719 SetFocus(s_hwnd);
5720
Bram Moolenaar734a8672019-12-02 22:49:38 +01005721 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723}
5724
5725
5726 void
5727gui_mch_set_scrollbar_thumb(
5728 scrollbar_T *sb,
5729 long val,
5730 long size,
5731 long max)
5732{
5733 SCROLLINFO info;
5734
5735 sb->scroll_shift = 0;
5736 while (max > 32767)
5737 {
5738 max = (max + 1) >> 1;
5739 val >>= 1;
5740 size >>= 1;
5741 ++sb->scroll_shift;
5742 }
5743
5744 if (sb->scroll_shift > 0)
5745 ++size;
5746
5747 info.cbSize = sizeof(info);
5748 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5749 info.nPos = val;
5750 info.nMin = 0;
5751 info.nMax = max;
5752 info.nPage = size;
5753 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5754}
5755
5756
5757/*
5758 * Set the current text font.
5759 */
5760 void
5761gui_mch_set_font(GuiFont font)
5762{
5763 gui.currFont = font;
5764}
5765
5766
5767/*
5768 * Set the current text foreground color.
5769 */
5770 void
5771gui_mch_set_fg_color(guicolor_T color)
5772{
5773 gui.currFgColor = color;
5774}
5775
5776/*
5777 * Set the current text background color.
5778 */
5779 void
5780gui_mch_set_bg_color(guicolor_T color)
5781{
5782 gui.currBgColor = color;
5783}
5784
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005785/*
5786 * Set the current text special color.
5787 */
5788 void
5789gui_mch_set_sp_color(guicolor_T color)
5790{
5791 gui.currSpColor = color;
5792}
5793
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005794#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795/*
5796 * Multi-byte handling, originally by Sung-Hoon Baek.
5797 * First static functions (no prototypes generated).
5798 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005799# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005800# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005801# endif
5802# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
5804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * handle WM_IME_NOTIFY message
5806 */
5807 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005808_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 LRESULT lResult = 0;
5811 HIMC hImc;
5812
5813 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5814 return lResult;
5815 switch (dwCommand)
5816 {
5817 case IMN_SETOPENSTATUS:
5818 if (pImmGetOpenStatus(hImc))
5819 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005820 LOGFONTW lf = norm_logfont;
5821 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5822 // Work around when PerMonitorV2 is not enabled in the process level.
5823 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5824 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 im_set_position(gui.row, gui.col);
5826
Bram Moolenaar734a8672019-12-02 22:49:38 +01005827 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 State &= ~LANGMAP;
5829 if (State & INSERT)
5830 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005831# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005832 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5834 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005835 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 int old_row = gui.row;
5837 int old_col = gui.col;
5838
5839 // This must be called here before
5840 // status_redraw_curbuf(), otherwise the mode
5841 // message may appear in the wrong position.
5842 showmode();
5843 status_redraw_curbuf();
5844 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005845 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 gui.row = old_row;
5847 gui.col = old_col;
5848 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851 }
5852 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005853 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 lResult = 0;
5855 break;
5856 }
5857 pImmReleaseContext(hWnd, hImc);
5858 return lResult;
5859}
5860
5861 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005862_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 char_u *ret;
5865 int len;
5866
Bram Moolenaar734a8672019-12-02 22:49:38 +01005867 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 return 0;
5869
5870 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5871 if (ret != NULL)
5872 {
5873 add_to_input_buf_csi(ret, len);
5874 vim_free(ret);
5875 return 1;
5876 }
5877 return 0;
5878}
5879
5880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 * void GetResultStr()
5882 *
5883 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5884 * get complete composition string
5885 */
5886 static char_u *
5887GetResultStr(HWND hwnd, int GCS, int *lenp)
5888{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005889 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005890 LONG ret;
5891 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 char_u *convbuf = NULL;
5893
5894 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5895 return NULL;
5896
K.Takatab0b2b732022-01-19 12:59:21 +00005897 // Get the length of the composition string.
5898 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5899 if (ret <= 0)
5900 return NULL;
5901
5902 // Allocate the requested buffer plus space for the NUL character.
5903 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 if (buf == NULL)
5905 return NULL;
5906
K.Takatab0b2b732022-01-19 12:59:21 +00005907 // Reads in the composition string.
5908 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5909 *lenp = ret / sizeof(WCHAR);
5910
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005911 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 pImmReleaseContext(hwnd, hIMC);
5913 vim_free(buf);
5914 return convbuf;
5915}
5916#endif
5917
Bram Moolenaar734a8672019-12-02 22:49:38 +01005918// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005919#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921/*
5922 * set font to IM.
5923 */
5924 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005925im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 HIMC hImc;
5928
5929 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5930 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005931 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 pImmReleaseContext(s_hwnd, hImc);
5933 }
5934}
5935
5936/*
5937 * Notify cursor position to IM.
5938 */
5939 void
5940im_set_position(int row, int col)
5941{
5942 HIMC hImc;
5943
5944 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5945 {
5946 COMPOSITIONFORM cfs;
5947
5948 cfs.dwStyle = CFS_POINT;
5949 cfs.ptCurrentPos.x = FILL_X(col);
5950 cfs.ptCurrentPos.y = FILL_Y(row);
5951 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005952 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5953 {
5954 // Work around when PerMonitorV2 is not enabled in the process level.
5955 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5956 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 pImmSetCompositionWindow(hImc, &cfs);
5959
5960 pImmReleaseContext(s_hwnd, hImc);
5961 }
5962}
5963
5964/*
5965 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5966 */
5967 void
5968im_set_active(int active)
5969{
5970 HIMC hImc;
5971 static HIMC hImcOld = (HIMC)0;
5972
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005973# ifdef VIMDLL
5974 if (!gui.in_use && !gui.starting)
5975 {
5976 mbyte_im_set_active(active);
5977 return;
5978 }
5979# endif
5980
Bram Moolenaar734a8672019-12-02 22:49:38 +01005981 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 if (p_imdisable)
5984 {
5985 if (hImcOld == (HIMC)0)
5986 {
5987 hImcOld = pImmGetContext(s_hwnd);
5988 if (hImcOld)
5989 pImmAssociateContext(s_hwnd, (HIMC)0);
5990 }
5991 active = FALSE;
5992 }
5993 else if (hImcOld != (HIMC)0)
5994 {
5995 pImmAssociateContext(s_hwnd, hImcOld);
5996 hImcOld = (HIMC)0;
5997 }
5998
5999 hImc = pImmGetContext(s_hwnd);
6000 if (hImc)
6001 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006002 /*
6003 * for Korean ime
6004 */
6005 HKL hKL = GetKeyboardLayout(0);
6006
6007 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6008 {
6009 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6010 static BOOL bSaved = FALSE;
6011
6012 if (active)
6013 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006014 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00006015 if (bSaved)
6016 pImmSetConversionStatus(hImc, dwConversionSaved,
6017 dwSentenceSaved);
6018 bSaved = FALSE;
6019 }
6020 else
6021 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006022 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00006023 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6024 &dwSentenceSaved))
6025 {
6026 bSaved = TRUE;
6027 pImmSetConversionStatus(hImc,
6028 dwConversionSaved & ~(IME_CMODE_NATIVE
6029 | IME_CMODE_FULLSHAPE),
6030 dwSentenceSaved);
6031 }
6032 }
6033 }
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 pImmSetOpenStatus(hImc, active);
6036 pImmReleaseContext(s_hwnd, hImc);
6037 }
6038 }
6039}
6040
6041/*
6042 * Get IM status. When IM is on, return not 0. Else return 0.
6043 */
6044 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006045im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 int status = 0;
6048 HIMC hImc;
6049
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006050# ifdef VIMDLL
6051 if (!gui.in_use && !gui.starting)
6052 return mbyte_im_get_status();
6053# endif
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6056 {
6057 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6058 pImmReleaseContext(s_hwnd, hImc);
6059 }
6060 return status;
6061}
6062
Bram Moolenaar734a8672019-12-02 22:49:38 +01006063#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006065#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006066// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068/*
6069 * Notify cursor position to IM.
6070 */
6071 void
6072im_set_position(int row, int col)
6073{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006074 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 POINT p;
6076
6077 p.x = FILL_X(col);
6078 p.y = FILL_Y(row);
6079 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6080 global_ime_set_position(&p);
6081}
6082
6083/*
6084 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6085 */
6086 void
6087im_set_active(int active)
6088{
6089 global_ime_set_status(active);
6090}
6091
6092/*
6093 * Get IM status. When IM is on, return not 0. Else return 0.
6094 */
6095 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006096im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 return global_ime_get_status();
6099}
6100#endif
6101
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006102/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006103 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006104 */
6105 static void
6106latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6107{
6108 int c;
6109
Bram Moolenaarca003e12006-03-17 23:19:38 +00006110 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006111 {
6112 c = *text++;
6113 switch (c)
6114 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006115 case 0xa4: c = 0x20ac; break; // euro
6116 case 0xa6: c = 0x0160; break; // S hat
6117 case 0xa8: c = 0x0161; break; // S -hat
6118 case 0xb4: c = 0x017d; break; // Z hat
6119 case 0xb8: c = 0x017e; break; // Z -hat
6120 case 0xbc: c = 0x0152; break; // OE
6121 case 0xbd: c = 0x0153; break; // oe
6122 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006123 }
6124 *unicodebuf++ = c;
6125 }
6126}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128#ifdef FEAT_RIGHTLEFT
6129/*
6130 * What is this for? In the case where you are using Win98 or Win2K or later,
6131 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6132 * reverses the string sent to the TextOut... family. This sucks, because we
6133 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6134 * way to tell Windblows not to do this!
6135 *
6136 * The short of it is that this 'RevOut' only gets called if you are running
6137 * one of the new, "improved" MS OSes, and only if you are running in
6138 * 'rightleft' mode. It makes display take *slightly* longer, but not
6139 * noticeably so.
6140 */
6141 static void
6142RevOut( HDC s_hdc,
6143 int col,
6144 int row,
6145 UINT foptions,
6146 CONST RECT *pcliprect,
6147 LPCTSTR text,
6148 UINT len,
6149 CONST INT *padding)
6150{
6151 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006153 for (ix = 0; ix < (int)len; ++ix)
6154 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6155 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156}
6157#endif
6158
Bram Moolenaar92467d32017-12-05 13:22:16 +01006159 static void
6160draw_line(
6161 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006162 int y1,
6163 int x2,
6164 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006165 COLORREF color)
6166{
6167#if defined(FEAT_DIRECTX)
6168 if (IS_ENABLE_DIRECTX())
6169 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6170 else
6171#endif
6172 {
6173 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6174 HPEN old_pen = SelectObject(s_hdc, hpen);
6175 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006176 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006177 LineTo(s_hdc, x2, y2);
6178 DeleteObject(SelectObject(s_hdc, old_pen));
6179 }
6180}
6181
6182 static void
6183set_pixel(
6184 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006185 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006186 COLORREF color)
6187{
6188#if defined(FEAT_DIRECTX)
6189 if (IS_ENABLE_DIRECTX())
6190 DWriteContext_SetPixel(s_dwc, x, y, color);
6191 else
6192#endif
6193 SetPixel(s_hdc, x, y, color);
6194}
6195
6196 static void
6197fill_rect(
6198 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006199 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006200 COLORREF color)
6201{
6202#if defined(FEAT_DIRECTX)
6203 if (IS_ENABLE_DIRECTX())
6204 DWriteContext_FillRect(s_dwc, rcp, color);
6205 else
6206#endif
6207 {
6208 HBRUSH hbr2;
6209
6210 if (hbr == NULL)
6211 hbr2 = CreateSolidBrush(color);
6212 else
6213 hbr2 = hbr;
6214 FillRect(s_hdc, rcp, hbr2);
6215 if (hbr == NULL)
6216 DeleteBrush(hbr2);
6217 }
6218}
6219
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 void
6221gui_mch_draw_string(
6222 int row,
6223 int col,
6224 char_u *text,
6225 int len,
6226 int flags)
6227{
6228 static int *padding = NULL;
6229 static int pad_size = 0;
6230 int i;
6231 const RECT *pcliprect = NULL;
6232 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 static WCHAR *unicodebuf = NULL;
6234 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006235 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 int y;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 /*
6240 * Italic and bold text seems to have an extra row of pixels at the bottom
6241 * (below where the bottom of the character should be). If we draw the
6242 * characters with a solid background, the top row of pixels in the
6243 * character below will be overwritten. We can fix this by filling in the
6244 * background ourselves, to the correct character proportions, and then
6245 * writing the character in transparent mode. Still have a problem when
6246 * the character is "_", which gets written on to the character below.
6247 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6248 * pixel in their slots, which fixes the problem with the bottom row of
6249 * pixels. We still need this code because otherwise the top row of pixels
6250 * becomes a problem. - webb.
6251 */
6252 static HBRUSH hbr_cache[2] = {NULL, NULL};
6253 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6254 static int brush_lru = 0;
6255 HBRUSH hbr;
6256 RECT rc;
6257
6258 if (!(flags & DRAW_TRANSP))
6259 {
6260 /*
6261 * Clear background first.
6262 * Note: FillRect() excludes right and bottom of rectangle.
6263 */
6264 rc.left = FILL_X(col);
6265 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 if (has_mbyte)
6267 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006268 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006269 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 rc.right = FILL_X(col + len);
6273 rc.bottom = FILL_Y(row + 1);
6274
Bram Moolenaar734a8672019-12-02 22:49:38 +01006275 // Cache the created brush, that saves a lot of time. We need two:
6276 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (gui.currBgColor == brush_color[0])
6278 {
6279 hbr = hbr_cache[0];
6280 brush_lru = 1;
6281 }
6282 else if (gui.currBgColor == brush_color[1])
6283 {
6284 hbr = hbr_cache[1];
6285 brush_lru = 0;
6286 }
6287 else
6288 {
6289 if (hbr_cache[brush_lru] != NULL)
6290 DeleteBrush(hbr_cache[brush_lru]);
6291 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6292 brush_color[brush_lru] = gui.currBgColor;
6293 hbr = hbr_cache[brush_lru];
6294 brush_lru = !brush_lru;
6295 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006296
Bram Moolenaar92467d32017-12-05 13:22:16 +01006297 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 SetBkMode(s_hdc, TRANSPARENT);
6300
6301 /*
6302 * When drawing block cursor, prevent inverted character spilling
6303 * over character cell (can happen with bold/italic)
6304 */
6305 if (flags & DRAW_CURSOR)
6306 {
6307 pcliprect = &rc;
6308 foptions = ETO_CLIPPED;
6309 }
6310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 SetTextColor(s_hdc, gui.currFgColor);
6312 SelectFont(s_hdc, gui.currFont);
6313
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006314#ifdef FEAT_DIRECTX
6315 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006316 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006317#endif
6318
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6320 {
6321 vim_free(padding);
6322 pad_size = Columns;
6323
Bram Moolenaar734a8672019-12-02 22:49:38 +01006324 // Don't give an out-of-memory message here, it would call us
6325 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006326 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (padding != NULL)
6328 for (i = 0; i < pad_size; i++)
6329 padding[i] = gui.char_width;
6330 }
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 /*
6333 * We have to provide the padding argument because italic and bold versions
6334 * of fixed-width fonts are often one pixel or so wider than their normal
6335 * versions.
6336 * No check for DRAW_BOLD, Windows will have done it already.
6337 */
6338
Bram Moolenaar734a8672019-12-02 22:49:38 +01006339 // Check if there are any UTF-8 characters. If not, use normal text
6340 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (enc_utf8)
6342 for (n = 0; n < len; ++n)
6343 if (text[n] >= 0x80)
6344 break;
6345
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006346#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006347 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6348 // required that unicode drawing routine, currently. So this forces it
6349 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006350 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006351 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006352#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006353
Bram Moolenaar734a8672019-12-02 22:49:38 +01006354 // Check if the Unicode buffer exists and is big enough. Create it
6355 // with the same length as the multi-byte string, the number of wide
6356 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006357 if ((enc_utf8
6358 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6359 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 && (unicodebuf == NULL || len > unibuflen))
6361 {
6362 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006363 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006366 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368 unibuflen = len;
6369 }
6370
6371 if (enc_utf8 && n < len && unicodebuf != NULL)
6372 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006373 // Output UTF-8 characters. Composing characters should be
6374 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006375 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006376 int wlen; // string length in words
6377 int clen; // string length in characters
6378 int cells; // cell width of string up to composing char
6379 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006380 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006382 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006383 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006385 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006387 c = utf_ptr2char(text + i);
6388 if (c >= 0x10000)
6389 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006390 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006391 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6392 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006393 }
6394 else
6395 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006396 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006397 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006398
6399 if (utf_iscomposing(c))
6400 cw = 0;
6401 else
6402 {
6403 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006404 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006405 cw = 1;
6406 }
6407
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (unicodepdy != NULL)
6409 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006410 // Use unicodepdy to make characters fit as we expect, even
6411 // when the font uses different widths (e.g., bold character
6412 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006413 if (c >= 0x10000)
6414 {
6415 unicodepdy[wlen - 2] = cw * gui.char_width;
6416 unicodepdy[wlen - 1] = 0;
6417 }
6418 else
6419 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 }
6421 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006422 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006423 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006425#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006426 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006427 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006428 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006429 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006430 TEXT_X(col), TEXT_Y(row),
6431 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006432 gui.char_width, gui.currFgColor,
6433 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006434 }
6435 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006436#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006437 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6438 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006439 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006441 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006443 // If we want to display codepage data, and the current CP is not the
6444 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (unicodebuf != NULL)
6446 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006447 if (enc_latin9)
6448 latin9_to_ucs(text, len, unicodebuf);
6449 else
6450 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 MB_PRECOMPOSED,
6452 (char *)text, len,
6453 (LPWSTR)unicodebuf, unibuflen);
6454 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006455 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006456 // Use unicodepdy to make characters fit as we expect, even
6457 // when the font uses different widths (e.g., bold character
6458 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006459 if (unicodepdy != NULL)
6460 {
6461 int i;
6462 int cw;
6463
6464 for (i = 0; i < len; ++i)
6465 {
6466 cw = utf_char2cells(unicodebuf[i]);
6467 if (cw > 2)
6468 cw = 1;
6469 unicodepdy[i] = cw * gui.char_width;
6470 }
6471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006473 foptions, pcliprect, unicodebuf, len, unicodepdy);
6474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476 }
6477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
6479#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006480 // Windows will mess up RL text, so we have to draw it character by
6481 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006482 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6484 foptions, pcliprect, (char *)text, len, padding);
6485 else
6486#endif
6487 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6488 foptions, pcliprect, (char *)text, len, padding);
6489 }
6490
Bram Moolenaar734a8672019-12-02 22:49:38 +01006491 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (flags & DRAW_UNDERL)
6493 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006494 // When p_linespace is 0, overwrite the bottom row of pixels.
6495 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (p_linespace > 1)
6498 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006499 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006501
Bram Moolenaar734a8672019-12-02 22:49:38 +01006502 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006503 if (flags & DRAW_STRIKE)
6504 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006505 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006506 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006507 }
6508
Bram Moolenaar734a8672019-12-02 22:49:38 +01006509 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006510 if (flags & DRAW_UNDERC)
6511 {
6512 int x;
6513 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006514 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006515
6516 y = FILL_Y(row + 1) - 1;
6517 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6518 {
6519 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006520 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006521 }
6522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523}
6524
6525
6526/*
6527 * Output routines.
6528 */
6529
Bram Moolenaar734a8672019-12-02 22:49:38 +01006530/*
6531 * Flush any output to the screen
6532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 void
6534gui_mch_flush(void)
6535{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006536#if defined(FEAT_DIRECTX)
6537 if (IS_ENABLE_DIRECTX())
6538 DWriteContext_Flush(s_dwc);
6539#endif
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 GdiFlush();
6542}
6543
6544 static void
6545clear_rect(RECT *rcp)
6546{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006547 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548}
6549
6550
Bram Moolenaarc716c302006-01-21 22:12:51 +00006551 void
6552gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6553{
6554 RECT workarea_rect;
6555
6556 get_work_area(&workarea_rect);
6557
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006558 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006559 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6560 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006561
Bram Moolenaar734a8672019-12-02 22:49:38 +01006562 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6563 // the menubar for MSwin, we subtract it from the screen height, so that
6564 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006565 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006566 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6567 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6568 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6569 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006570}
6571
6572
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573#if defined(FEAT_MENU) || defined(PROTO)
6574/*
6575 * Add a sub menu to the menu bar.
6576 */
6577 void
6578gui_mch_add_menu(
6579 vimmenu_T *menu,
6580 int pos)
6581{
6582 vimmenu_T *parent = menu->parent;
6583
6584 menu->submenu_id = CreatePopupMenu();
6585 menu->id = s_menu_id++;
6586
6587 if (menu_is_menubar(menu->name))
6588 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006589 WCHAR *wn;
6590 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006592 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006593 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006596 infow.cbSize = sizeof(infow);
6597 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6598 | MIIM_SUBMENU;
6599 infow.dwItemData = (long_u)menu;
6600 infow.wID = menu->id;
6601 infow.fType = MFT_STRING;
6602 infow.dwTypeData = wn;
6603 infow.cch = (UINT)wcslen(wn);
6604 infow.hSubMenu = menu->submenu_id;
6605 InsertMenuItemW((parent == NULL)
6606 ? s_menuBar : parent->submenu_id,
6607 (UINT)pos, TRUE, &infow);
6608 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
6610
Bram Moolenaar734a8672019-12-02 22:49:38 +01006611 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (parent == NULL)
6613 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006614# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 else if (IsWindow(parent->tearoff_handle))
6616 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618}
6619
6620 void
6621gui_mch_show_popupmenu(vimmenu_T *menu)
6622{
6623 POINT mp;
6624
6625 (void)GetCursorPos((LPPOINT)&mp);
6626 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6627}
6628
6629 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006630gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 vimmenu_T *menu = gui_find_menu(path_name);
6633
6634 if (menu != NULL)
6635 {
6636 POINT p;
6637
Bram Moolenaar734a8672019-12-02 22:49:38 +01006638 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006640 if (mouse_pos)
6641 {
6642 int mx, my;
6643
6644 gui_mch_getmouse(&mx, &my);
6645 p.x += mx;
6646 p.y += my;
6647 }
6648 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006650 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6652 }
6653 msg_scroll = FALSE;
6654 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6655 }
6656}
6657
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006658# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6661 * create it as a pseudo-"tearoff menu".
6662 */
6663 void
6664gui_make_tearoff(char_u *path_name)
6665{
6666 vimmenu_T *menu = gui_find_menu(path_name);
6667
Bram Moolenaar734a8672019-12-02 22:49:38 +01006668 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 if (menu != NULL)
6670 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6671}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674/*
6675 * Add a menu item to a menu
6676 */
6677 void
6678gui_mch_add_menu_item(
6679 vimmenu_T *menu,
6680 int idx)
6681{
6682 vimmenu_T *parent = menu->parent;
6683
6684 menu->id = s_menu_id++;
6685 menu->submenu_id = NULL;
6686
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006687# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6689 {
6690 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6691 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6692 }
6693 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006694# endif
6695# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (menu_is_toolbar(parent->name))
6697 {
6698 TBBUTTON newtb;
6699
Bram Moolenaara80faa82020-04-12 19:37:17 +02006700 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (menu_is_separator(menu->name))
6702 {
6703 newtb.iBitmap = 0;
6704 newtb.fsStyle = TBSTYLE_SEP;
6705 }
6706 else
6707 {
6708 newtb.iBitmap = get_toolbar_bitmap(menu);
6709 newtb.fsStyle = TBSTYLE_BUTTON;
6710 }
6711 newtb.idCommand = menu->id;
6712 newtb.fsState = TBSTATE_ENABLED;
6713 newtb.iString = 0;
6714 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6715 (LPARAM)&newtb);
6716 menu->submenu_id = (HMENU)-1;
6717 }
6718 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006721 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006723 wn = enc_to_utf16(menu->name, NULL);
6724 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006726 InsertMenuW(parent->submenu_id, (UINT)idx,
6727 (menu_is_separator(menu->name)
6728 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6729 (UINT)menu->id, wn);
6730 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006732# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (IsWindow(parent->tearoff_handle))
6734 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737}
6738
6739/*
6740 * Destroy the machine specific menu widget.
6741 */
6742 void
6743gui_mch_destroy_menu(vimmenu_T *menu)
6744{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006745# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 /*
6747 * is this a toolbar button?
6748 */
6749 if (menu->submenu_id == (HMENU)-1)
6750 {
6751 int iButton;
6752
6753 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6754 (WPARAM)menu->id, 0);
6755 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6756 }
6757 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {
6760 if (menu->parent != NULL
6761 && menu_is_popup(menu->parent->dname)
6762 && menu->parent->submenu_id != NULL)
6763 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6764 else
6765 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6766 if (menu->submenu_id != NULL)
6767 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006768# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 if (IsWindow(menu->tearoff_handle))
6770 DestroyWindow(menu->tearoff_handle);
6771 if (menu->parent != NULL
6772 && menu->parent->children != NULL
6773 && IsWindow(menu->parent->tearoff_handle))
6774 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006775 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 menu->modes = 0;
6777 rebuild_tearoff(menu->parent);
6778 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 }
6781}
6782
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006783# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 static void
6785rebuild_tearoff(vimmenu_T *menu)
6786{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006787 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 char_u tbuf[128];
6789 RECT trect;
6790 RECT rct;
6791 RECT roct;
6792 int x, y;
6793
6794 HWND thwnd = menu->tearoff_handle;
6795
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006796 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (GetWindowRect(thwnd, &trect)
6798 && GetWindowRect(s_hwnd, &rct)
6799 && GetClientRect(s_hwnd, &roct))
6800 {
6801 x = trect.left - rct.left;
6802 y = (trect.top - rct.bottom + roct.bottom);
6803 }
6804 else
6805 {
6806 x = y = 0xffffL;
6807 }
6808 DestroyWindow(thwnd);
6809 if (menu->children != NULL)
6810 {
6811 gui_mch_tearoff(tbuf, menu, x, y);
6812 if (IsWindow(menu->tearoff_handle))
6813 (void) SetWindowPos(menu->tearoff_handle,
6814 NULL,
6815 (int)trect.left,
6816 (int)trect.top,
6817 0, 0,
6818 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6819 }
6820}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006821# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823/*
6824 * Make a menu either grey or not grey.
6825 */
6826 void
6827gui_mch_menu_grey(
6828 vimmenu_T *menu,
6829 int grey)
6830{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006831# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 /*
6833 * is this a toolbar button?
6834 */
6835 if (menu->submenu_id == (HMENU)-1)
6836 {
6837 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6838 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6839 }
6840 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006841# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006842 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6843 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006845# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6847 {
6848 WORD menuID;
6849 HWND menuHandle;
6850
6851 /*
6852 * A tearoff button has changed state.
6853 */
6854 if (menu->children == NULL)
6855 menuID = (WORD)(menu->id);
6856 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006857 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6859 if (menuHandle)
6860 EnableWindow(menuHandle, !grey);
6861
6862 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864}
6865
Bram Moolenaar734a8672019-12-02 22:49:38 +01006866#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868
Bram Moolenaar734a8672019-12-02 22:49:38 +01006869// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870
6871#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6872#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006873#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
6875#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6876/*
6877 * stuff for dialogs
6878 */
6879
6880/*
6881 * The callback routine used by all the dialogs. Very simple. First,
6882 * acknowledges the INITDIALOG message so that Windows knows to do standard
6883 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6884 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6885 * number.
6886 */
6887 static LRESULT CALLBACK
6888dialog_callback(
6889 HWND hwnd,
6890 UINT message,
6891 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006892 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 if (message == WM_INITDIALOG)
6895 {
6896 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006897 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 (void)SetFocus(hwnd);
6899 if (dialog_default_button > IDCANCEL)
6900 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006901 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006902 // We don't have a default, set focus on another element of the
6903 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006904 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 return FALSE;
6906 }
6907
6908 if (message == WM_COMMAND)
6909 {
6910 int button = LOWORD(wParam);
6911
Bram Moolenaar734a8672019-12-02 22:49:38 +01006912 // Don't end the dialog if something was selected that was
6913 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (button >= DLG_NONBUTTON_CONTROL)
6915 return TRUE;
6916
Bram Moolenaar734a8672019-12-02 22:49:38 +01006917 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006919 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006920 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006921 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006922
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006923 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6924 p = utf16_to_enc(wp, NULL);
6925 vim_strncpy(s_textfield, p, IOSIZE);
6926 vim_free(p);
6927 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 /*
6931 * Need to check for IDOK because if the user just hits Return to
6932 * accept the default value, some reason this is what we get.
6933 */
6934 if (button == IDOK)
6935 {
6936 if (dialog_default_button > IDCANCEL)
6937 EndDialog(hwnd, dialog_default_button);
6938 }
6939 else
6940 EndDialog(hwnd, button - IDCANCEL);
6941 return TRUE;
6942 }
6943
6944 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6945 {
6946 EndDialog(hwnd, 0);
6947 return TRUE;
6948 }
6949 return FALSE;
6950}
6951
6952/*
6953 * Create a dialog dynamically from the parameter strings.
6954 * type = type of dialog (question, alert, etc.)
6955 * title = dialog title. may be NULL for default title.
6956 * message = text to display. Dialog sizes to accommodate it.
6957 * buttons = '\n' separated list of button captions, default first.
6958 * dfltbutton = number of default button.
6959 *
6960 * This routine returns 1 if the first button is pressed,
6961 * 2 for the second, etc.
6962 *
6963 * 0 indicates Esc was pressed.
6964 * -1 for unexpected error
6965 *
6966 * If stubbing out this fn, return 1.
6967 */
6968
Bram Moolenaar734a8672019-12-02 22:49:38 +01006969static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
6971 "IDR_VIM",
6972 "IDR_VIM_ERROR",
6973 "IDR_VIM_ALERT",
6974 "IDR_VIM_INFO",
6975 "IDR_VIM_QUESTION"
6976};
6977
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 int
6979gui_mch_dialog(
6980 int type,
6981 char_u *title,
6982 char_u *message,
6983 char_u *buttons,
6984 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006985 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006986 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006989 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 int numButtons;
6991 int *buttonWidths, *buttonPositions;
6992 int buttonYpos;
6993 int nchar, i;
6994 DWORD lStyle;
6995 int dlgwidth = 0;
6996 int dlgheight;
6997 int editboxheight;
6998 int horizWidth = 0;
6999 int msgheight;
7000 char_u *pstart;
7001 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007002 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 char_u *tbuffer;
7004 RECT rect;
7005 HWND hwnd;
7006 HDC hdc;
7007 HFONT font, oldFont;
7008 TEXTMETRIC fontInfo;
7009 int fontHeight;
7010 int textWidth, minButtonWidth, messageWidth;
7011 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007012 int maxDialogHeight;
7013 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 int vertical;
7015 int dlgPaddingX;
7016 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007017# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007018 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007020# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007021 garray_T ga;
7022 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007023 int dlg_icon_width;
7024 int dlg_icon_height;
7025 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007027# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007029# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007030 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007031# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007032 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007033 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
Bram Moolenaar748bf032005-02-02 23:04:36 +00007036 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007037 {
7038 load_dpi_func();
7039 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007040 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007041 }
7042 else
7043 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045 if ((type < 0) || (type > VIM_LAST_TYPE))
7046 type = 0;
7047
Bram Moolenaar734a8672019-12-02 22:49:38 +01007048 // allocate some memory for dialog template
7049 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007050 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007051 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
7053 if (p == NULL)
7054 return -1;
7055
7056 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007057 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7059 * const.
7060 */
7061 tbuffer = vim_strsave(buttons);
7062 if (tbuffer == NULL)
7063 return -1;
7064
Bram Moolenaar734a8672019-12-02 22:49:38 +01007065 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
Bram Moolenaar734a8672019-12-02 22:49:38 +01007067 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 numButtons = 1;
7069 for (i = 0; tbuffer[i] != '\0'; i++)
7070 {
7071 if (tbuffer[i] == DLG_BUTTON_SEP)
7072 numButtons++;
7073 }
7074 if (dfltbutton >= numButtons)
7075 dfltbutton = -1;
7076
Bram Moolenaar734a8672019-12-02 22:49:38 +01007077 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007078 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 if (buttonWidths == NULL)
7080 return -1;
7081
Bram Moolenaar734a8672019-12-02 22:49:38 +01007082 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007083 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 if (buttonPositions == NULL)
7085 return -1;
7086
7087 /*
7088 * Calculate how big the dialog must be.
7089 */
7090 hwnd = GetDesktopWindow();
7091 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007092# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7094 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007095 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 use_lfSysmenu = TRUE;
7097 }
7098 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007101 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (s_usenewlook)
7103 {
7104 oldFont = SelectFont(hdc, font);
7105 dlgPaddingX = DLG_PADDING_X;
7106 dlgPaddingY = DLG_PADDING_Y;
7107 }
7108 else
7109 {
7110 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7111 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7112 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7113 }
7114 GetTextMetrics(hdc, &fontInfo);
7115 fontHeight = fontInfo.tmHeight;
7116
Bram Moolenaar734a8672019-12-02 22:49:38 +01007117 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007118 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119
Bram Moolenaar734a8672019-12-02 22:49:38 +01007120 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007121 if (s_hwnd == NULL)
7122 {
7123 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Bram Moolenaar734a8672019-12-02 22:49:38 +01007125 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007126 get_work_area(&workarea_rect);
7127 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007128 if (maxDialogWidth > adjust_by_system_dpi(600))
7129 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007130 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007131 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007132 }
7133 else
7134 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007135 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007136 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007137 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007138 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7139 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7140 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7141 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007142
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007143 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007144 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7145 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7146 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7147 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7148 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007149 }
7150
Bram Moolenaar734a8672019-12-02 22:49:38 +01007151 // Set dlgwidth to width of message.
7152 // Copy the message into "ga", changing NL to CR-NL and inserting line
7153 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 pstart = message;
7155 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007156 msgheight = 0;
7157 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 do
7159 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007160 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007161
Bram Moolenaar734a8672019-12-02 22:49:38 +01007162 // Need to figure out where to break the string. The system does it
7163 // at a word boundary, which would mean we can't compute the number of
7164 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007165 textWidth = 0;
7166 last_white = NULL;
7167 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007168 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007169 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007170 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007171 && textWidth > maxDialogWidth * 3 / 4)
7172 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007173 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007174 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007175 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007176 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007177 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007178 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007179 textWidth = 0;
7180
7181 if (last_white != NULL)
7182 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007183 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007184 if (pend > last_white)
7185 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007186 pend = last_white + 1;
7187 last_white = NULL;
7188 }
7189 ga_append(&ga, '\r');
7190 ga_append(&ga, '\n');
7191 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007192 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007193
7194 while (--l >= 0)
7195 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007196 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007197 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007199
7200 ga_append(&ga, '\r');
7201 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 pstart = pend + 1;
7203 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007204
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007205 if (ga.ga_data != NULL)
7206 message = ga.ga_data;
7207
Bram Moolenaar734a8672019-12-02 22:49:38 +01007208 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007209
K.Takatac81e9bf2022-01-16 14:15:49 +00007210 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7211 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
K.Takatac81e9bf2022-01-16 14:15:49 +00007213 // Add width of icon to dlgwidth, and some space
7214 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7215 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7216
7217 if (msgheight < dlg_icon_height)
7218 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220 /*
7221 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007222 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007224 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (!vertical)
7226 {
7227 // Place buttons horizontally if they fit.
7228 horizWidth = dlgPaddingX;
7229 pstart = tbuffer;
7230 i = 0;
7231 do
7232 {
7233 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7234 if (pend == NULL)
7235 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007236 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (textWidth < minButtonWidth)
7238 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007239 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 buttonWidths[i] = textWidth;
7241 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007242 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 pstart = pend + 1;
7244 } while (*pend != NUL);
7245
7246 if (horizWidth > maxDialogWidth)
7247 vertical = TRUE; // Too wide to fit on the screen.
7248 else if (horizWidth > dlgwidth)
7249 dlgwidth = horizWidth;
7250 }
7251
7252 if (vertical)
7253 {
7254 // Stack buttons vertically.
7255 pstart = tbuffer;
7256 do
7257 {
7258 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7259 if (pend == NULL)
7260 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007261 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007262 textWidth += dlgPaddingX; // Padding within button
7263 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (textWidth > dlgwidth)
7265 dlgwidth = textWidth;
7266 pstart = pend + 1;
7267 } while (*pend != NUL);
7268 }
7269
7270 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007271 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar734a8672019-12-02 22:49:38 +01007273 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 if (s_usenewlook)
7275 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7276 else
7277 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7278
7279 add_long(lStyle);
7280 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007281 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 add_word(0); // NumberOfItems(will change later)
7283 add_word(10); // x
7284 add_word(10); // y
7285 add_word(PixelToDialogX(dlgwidth)); // cx
7286
7287 // Dialog height.
7288 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007289 dlgheight = msgheight + 2 * dlgPaddingY
7290 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 else
7292 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7293
7294 // Dialog needs to be taller if contains an edit box.
7295 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7296 if (textfield != NULL)
7297 dlgheight += editboxheight;
7298
Bram Moolenaar734a8672019-12-02 22:49:38 +01007299 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007300 if (dlgheight > maxDialogHeight)
7301 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007302 msgheight = msgheight - (dlgheight - maxDialogHeight);
7303 dlgheight = maxDialogHeight;
7304 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007305 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007306 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007307 }
7308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 add_word(PixelToDialogY(dlgheight));
7310
7311 add_word(0); // Menu
7312 add_word(0); // Class
7313
Bram Moolenaar734a8672019-12-02 22:49:38 +01007314 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007315 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7316 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 p += nchar;
7318
7319 if (s_usenewlook)
7320 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007321 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007322# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (use_lfSysmenu)
7324 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007325 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7327 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007328 wcscpy(p, lfSysmenu.lfFaceName);
7329 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
7331 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 {
7334 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007335 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337 p += nchar;
7338 }
7339
7340 buttonYpos = msgheight + 2 * dlgPaddingY;
7341
7342 if (textfield != NULL)
7343 buttonYpos += editboxheight;
7344
7345 pstart = tbuffer;
7346 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007347 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 for (i = 0; i < numButtons; i++)
7349 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007350 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 for ( pend = pstart;
7352 *pend && (*pend != DLG_BUTTON_SEP);
7353 pend++)
7354 ;
7355
7356 if (*pend)
7357 *pend = '\0';
7358
7359 /*
7360 * old NOTE:
7361 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7362 * the focus to the first tab-able button and in so doing makes that
7363 * the default!! Grrr. Workaround: Make the default button the only
7364 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7365 * he/she can use arrow keys.
7366 *
7367 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007368 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 * dialog. Also needed for when the textfield is the default control.
7370 * It appears to work now (perhaps not on Win95?).
7371 */
7372 if (vertical)
7373 {
7374 p = add_dialog_element(p,
7375 (i == dfltbutton
7376 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7377 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007378 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 + 2 * fontHeight * i),
7380 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7381 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007382 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
7384 else
7385 {
7386 p = add_dialog_element(p,
7387 (i == dfltbutton
7388 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7389 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007390 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 PixelToDialogX(buttonWidths[i]),
7392 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007393 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007395 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397 *pnumitems += numButtons;
7398
Bram Moolenaar734a8672019-12-02 22:49:38 +01007399 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 p = add_dialog_element(p, SS_ICON,
7401 PixelToDialogX(dlgPaddingX),
7402 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007403 PixelToDialogX(dlg_icon_width),
7404 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7406 dlg_icons[type]);
7407
Bram Moolenaar734a8672019-12-02 22:49:38 +01007408 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007409 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007410 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007411 PixelToDialogY(dlgPaddingY),
7412 (WORD)(PixelToDialogX(messageWidth) + 1),
7413 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007414 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
Bram Moolenaar734a8672019-12-02 22:49:38 +01007416 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (textfield != NULL)
7418 {
7419 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7420 PixelToDialogX(2 * dlgPaddingX),
7421 PixelToDialogY(2 * dlgPaddingY + msgheight),
7422 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7423 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007424 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 *pnumitems += 1;
7426 }
7427
7428 *pnumitems += 2;
7429
7430 SelectFont(hdc, oldFont);
7431 DeleteObject(font);
7432 ReleaseDC(hwnd, hdc);
7433
Bram Moolenaar734a8672019-12-02 22:49:38 +01007434 // Let the dialog_callback() function know which button to make default
7435 // If we have an edit box, make that the default. We also need to tell
7436 // dialog_callback() if this dialog contains an edit box or not. We do
7437 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (textfield != NULL)
7439 {
7440 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7441 s_textfield = textfield;
7442 }
7443 else
7444 {
7445 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7446 s_textfield = NULL;
7447 }
7448
Bram Moolenaar734a8672019-12-02 22:49:38 +01007449 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007451 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 (LPDLGTEMPLATE)pdlgtemplate,
7453 s_hwnd,
7454 (DLGPROC)dialog_callback);
7455
7456 LocalFree(LocalHandle(pdlgtemplate));
7457 vim_free(tbuffer);
7458 vim_free(buttonWidths);
7459 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007460 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
Bram Moolenaar734a8672019-12-02 22:49:38 +01007462 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 (void)SetFocus(s_hwnd);
7464
7465 return nchar;
7466}
7467
Bram Moolenaar734a8672019-12-02 22:49:38 +01007468#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007469
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470/*
7471 * Put a simple element (basic class) onto a dialog template in memory.
7472 * return a pointer to where the next item should be added.
7473 *
7474 * parameters:
7475 * lStyle = additional style flags
7476 * (be careful, NT3.51 & Win32s will ignore the new ones)
7477 * x,y = x & y positions IN DIALOG UNITS
7478 * w,h = width and height IN DIALOG UNITS
7479 * Id = ID used in messages
7480 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7481 * caption = usually text or resource name
7482 *
7483 * TODO: use the length information noted here to enable the dialog creation
7484 * routines to work out more exactly how much memory they need to alloc.
7485 */
7486 static PWORD
7487add_dialog_element(
7488 PWORD p,
7489 DWORD lStyle,
7490 WORD x,
7491 WORD y,
7492 WORD w,
7493 WORD h,
7494 WORD Id,
7495 WORD clss,
7496 const char *caption)
7497{
7498 int nchar;
7499
Bram Moolenaar734a8672019-12-02 22:49:38 +01007500 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7502 *p++ = LOWORD(lStyle);
7503 *p++ = HIWORD(lStyle);
7504 *p++ = 0; // LOWORD (lExtendedStyle)
7505 *p++ = 0; // HIWORD (lExtendedStyle)
7506 *p++ = x;
7507 *p++ = y;
7508 *p++ = w;
7509 *p++ = h;
7510 *p++ = Id; //9 or 10 words in all
7511
7512 *p++ = (WORD)0xffff;
7513 *p++ = clss; //2 more here
7514
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007515 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 p += nchar;
7517
7518 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7519
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007520 return p; // total = 15 + strlen(caption) words
7521 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524
7525/*
7526 * Helper routine. Take an input pointer, return closest pointer that is
7527 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7528 */
7529 static LPWORD
7530lpwAlign(
7531 LPWORD lpIn)
7532{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007533 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007535 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 ul += 3;
7537 ul >>= 2;
7538 ul <<= 2;
7539 return (LPWORD)ul;
7540}
7541
7542/*
7543 * Helper routine. Takes second parameter as Ansi string, copies it to first
7544 * parameter as wide character (16-bits / char) string, and returns integer
7545 * number of wide characters (words) in string (including the trailing wide
7546 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007547 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7548 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 static int
7550nCopyAnsiToWideChar(
7551 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007552 LPSTR lpAnsiIn,
7553 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
7555 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007556 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 int i;
7558 WCHAR *wn;
7559
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007560 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007562 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007563 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (wn != NULL)
7565 {
7566 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007567 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 vim_free(wn);
7569 }
7570 }
7571 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007572 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 nChar = MultiByteToWideChar(
7574 enc_codepage > 0 ? enc_codepage : CP_ACP,
7575 MB_PRECOMPOSED,
7576 lpAnsiIn, len,
7577 lpWCStr, len);
7578 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007579 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
7582 return nChar;
7583}
7584
7585
7586#ifdef FEAT_TEAROFF
7587/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007588 * Lookup menu handle from "menu_id".
7589 */
7590 static HMENU
7591tearoff_lookup_menuhandle(
7592 vimmenu_T *menu,
7593 WORD menu_id)
7594{
7595 for ( ; menu != NULL; menu = menu->next)
7596 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007597 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007598 continue;
7599 if (menu_is_separator(menu->dname))
7600 continue;
7601 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7602 return menu->submenu_id;
7603 }
7604 return NULL;
7605}
7606
7607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 * The callback function for all the modeless dialogs that make up the
7609 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7610 * thinking its menus have been clicked), and go away when closed.
7611 */
7612 static LRESULT CALLBACK
7613tearoff_callback(
7614 HWND hwnd,
7615 UINT message,
7616 WPARAM wParam,
7617 LPARAM lParam)
7618{
7619 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007620 {
7621 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
Bram Moolenaar734a8672019-12-02 22:49:38 +01007625 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 HandleMouseHide(message, lParam);
7627
7628 if (message == WM_COMMAND)
7629 {
7630 if ((WORD)(LOWORD(wParam)) & 0x8000)
7631 {
7632 POINT mp;
7633 RECT rect;
7634
7635 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7636 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007637 vimmenu_T *menu;
7638
7639 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007641 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7643 (int)rect.right - 8,
7644 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007645 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 s_hwnd,
7647 NULL);
7648 /*
7649 * NOTE: The pop-up menu can eat the mouse up event.
7650 * We deal with this in normal.c.
7651 */
7652 }
7653 }
7654 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007655 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7657 /*
7658 * Give main window the focus back: this is so after
7659 * choosing a tearoff button you can start typing again
7660 * straight away.
7661 */
7662 (void)SetFocus(s_hwnd);
7663 return TRUE;
7664 }
7665 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7666 {
7667 DestroyWindow(hwnd);
7668 return TRUE;
7669 }
7670
Bram Moolenaar734a8672019-12-02 22:49:38 +01007671 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (message == WM_EXITSIZEMOVE)
7673 (void)SetActiveWindow(s_hwnd);
7674
7675 return FALSE;
7676}
7677#endif
7678
7679
7680/*
7681 * Decide whether to use the "new look" (small, non-bold font) or the "old
7682 * look" (big, clanky font) for dialogs, and work out a few values for use
7683 * later accordingly.
7684 */
7685 static void
7686get_dialog_font_metrics(void)
7687{
7688 HDC hdc;
7689 HFONT hfontTools = 0;
7690 DWORD dlgFontSize;
7691 SIZE size;
7692#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007693 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694#endif
7695
7696 s_usenewlook = FALSE;
7697
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007699 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007700 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702#endif
7703 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007704 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007706 if (hfontTools)
7707 {
7708 hdc = GetDC(s_hwnd);
7709 SelectObject(hdc, hfontTools);
7710 /*
7711 * GetTextMetrics() doesn't return the right value in
7712 * tmAveCharWidth, so we have to figure out the dialog base units
7713 * ourselves.
7714 */
7715 GetTextExtentPoint(hdc,
7716 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7717 52, &size);
7718 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007720 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7721 s_dlgfntheight = (WORD)size.cy;
7722 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724
7725 if (!s_usenewlook)
7726 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007727 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 s_dlgfntwidth = LOWORD(dlgFontSize);
7729 s_dlgfntheight = HIWORD(dlgFontSize);
7730 }
7731}
7732
7733#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7734/*
7735 * Create a pseudo-"tearoff menu" based on the child
7736 * items of a given menu pointer.
7737 */
7738 static void
7739gui_mch_tearoff(
7740 char_u *title,
7741 vimmenu_T *menu,
7742 int initX,
7743 int initY)
7744{
7745 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7746 int template_len;
7747 int nchar, textWidth, submenuWidth;
7748 DWORD lStyle;
7749 DWORD lExtendedStyle;
7750 WORD dlgwidth;
7751 WORD menuID;
7752 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007753 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 vimmenu_T *the_menu = menu;
7755 HWND hwnd;
7756 HDC hdc;
7757 HFONT font, oldFont;
7758 int col, spaceWidth, len;
7759 int columnWidths[2];
7760 char_u *label, *text;
7761 int acLen = 0;
7762 int nameLen;
7763 int padding0, padding1, padding2 = 0;
7764 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007765 int x;
7766 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007767# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007768 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771
7772 /*
7773 * If this menu is already torn off, move it to the mouse position.
7774 */
7775 if (IsWindow(menu->tearoff_handle))
7776 {
7777 POINT mp;
7778 if (GetCursorPos((LPPOINT)&mp))
7779 {
7780 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7781 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7782 }
7783 return;
7784 }
7785
7786 /*
7787 * Create a new tearoff.
7788 */
7789 if (*title == MNU_HIDDEN_CHAR)
7790 title++;
7791
Bram Moolenaar734a8672019-12-02 22:49:38 +01007792 // Allocate memory to store the dialog template. It's made bigger when
7793 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 template_len = DLG_ALLOC_SIZE;
7795 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7796 if (p == NULL)
7797 return;
7798
7799 hwnd = GetDesktopWindow();
7800 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007801# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7803 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007804 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 use_lfSysmenu = TRUE;
7806 }
7807 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007810 VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (s_usenewlook)
7812 oldFont = SelectFont(hdc, font);
7813 else
7814 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7815
Bram Moolenaar734a8672019-12-02 22:49:38 +01007816 // Calculate width of a single space. Used for padding columns to the
7817 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007818 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaar734a8672019-12-02 22:49:38 +01007820 // Figure out max width of the text column, the accelerator column and the
7821 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 submenuWidth = 0;
7823 for (col = 0; col < 2; col++)
7824 {
7825 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007826 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007828 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 text = (col == 0) ? pmenu->dname : pmenu->actext;
7830 if (text != NULL && *text != NUL)
7831 {
7832 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7833 if (textWidth > columnWidths[col])
7834 columnWidths[col] = textWidth;
7835 }
7836 if (pmenu->children != NULL)
7837 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7838 }
7839 }
7840 if (columnWidths[1] == 0)
7841 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007842 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 if (submenuWidth != 0)
7844 columnWidths[0] += submenuWidth;
7845 else
7846 columnWidths[0] += spaceWidth;
7847 }
7848 else
7849 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007850 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7852 columnWidths[1] += submenuWidth;
7853 }
7854
7855 /*
7856 * Now find the total width of our 'menu'.
7857 */
7858 textWidth = columnWidths[0] + columnWidths[1];
7859 if (submenuWidth != 0)
7860 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007861 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7863 textWidth += submenuWidth;
7864 }
7865 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7866 if (textWidth > dlgwidth)
7867 dlgwidth = textWidth;
7868 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7869
Bram Moolenaar734a8672019-12-02 22:49:38 +01007870 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 if (s_usenewlook)
7872 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7873 else
7874 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7875
7876 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7877 *p++ = LOWORD(lStyle);
7878 *p++ = HIWORD(lStyle);
7879 *p++ = LOWORD(lExtendedStyle);
7880 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007881 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007883 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007885 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 else
7887 *p++ = PixelToDialogX(initX); // x
7888 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007889 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 else
7891 *p++ = PixelToDialogY(initY); // y
7892 *p++ = PixelToDialogX(dlgwidth); // cx
7893 ptrueheight = p;
7894 *p++ = 0; // dialog height: changed later anyway
7895 *p++ = 0; // Menu
7896 *p++ = 0; // Class
7897
Bram Moolenaar734a8672019-12-02 22:49:38 +01007898 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007900 ? (LPSTR)title
7901 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 p += nchar;
7903
7904 if (s_usenewlook)
7905 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007906 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007907# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (use_lfSysmenu)
7909 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007910 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7912 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007913 wcscpy(p, lfSysmenu.lfFaceName);
7914 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
7919 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007920 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 }
7922 p += nchar;
7923 }
7924
7925 /*
7926 * Loop over all the items in the menu.
7927 * But skip over the tearbar.
7928 */
7929 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7930 menu = menu->children->next;
7931 else
7932 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007933 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 for ( ; menu != NULL; menu = menu->next)
7935 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007936 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 continue;
7938 if (menu_is_separator(menu->dname))
7939 {
7940 sepPadding += 3;
7941 continue;
7942 }
7943
Bram Moolenaar734a8672019-12-02 22:49:38 +01007944 // Check if there still is plenty of room in the template. Make it
7945 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7947 {
7948 WORD *newp;
7949
7950 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7951 if (newp != NULL)
7952 {
7953 template_len += 4096;
7954 mch_memmove(newp, pdlgtemplate,
7955 (char *)p - (char *)pdlgtemplate);
7956 p = newp + (p - pdlgtemplate);
7957 pnumitems = newp + (pnumitems - pdlgtemplate);
7958 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7959 LocalFree(LocalHandle(pdlgtemplate));
7960 pdlgtemplate = newp;
7961 }
7962 }
7963
Bram Moolenaar734a8672019-12-02 22:49:38 +01007964 // Figure out minimal length of this menu label. Use "name" for the
7965 // actual text, "dname" for estimating the displayed size. "name"
7966 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 len = nameLen = (int)STRLEN(menu->name);
7968 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7969 (int)STRLEN(menu->dname))) / spaceWidth;
7970 len += padding0;
7971
7972 if (menu->actext != NULL)
7973 {
7974 acLen = (int)STRLEN(menu->actext);
7975 len += acLen;
7976 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7977 }
7978 else
7979 textWidth = 0;
7980 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7981 len += padding1;
7982
7983 if (menu->children == NULL)
7984 {
7985 padding2 = submenuWidth / spaceWidth;
7986 len += padding2;
7987 menuID = (WORD)(menu->id);
7988 }
7989 else
7990 {
7991 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007992 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 }
7994
Bram Moolenaar734a8672019-12-02 22:49:38 +01007995 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007996 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (label == NULL)
7998 break;
7999
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008000 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008001 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008003 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 while (padding0-- > 0)
8005 *text++ = ' ';
8006 if (menu->actext != NULL)
8007 {
8008 STRNCPY(text, menu->actext, acLen);
8009 text += acLen;
8010 }
8011 while (padding1-- > 0)
8012 *text++ = ' ';
8013 if (menu->children != NULL)
8014 {
8015 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8016 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8017 }
8018 else
8019 {
8020 while (padding2-- > 0)
8021 *text++ = ' ';
8022 }
8023 *text = NUL;
8024
8025 /*
8026 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8027 * W95/NT4 it makes the tear-off look more like a menu.
8028 */
8029 p = add_dialog_element(p,
8030 BS_PUSHBUTTON|BS_LEFT,
8031 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8032 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8033 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8034 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008035 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 vim_free(label);
8037 (*pnumitems)++;
8038 }
8039
8040 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8041
8042
Bram Moolenaar734a8672019-12-02 22:49:38 +01008043 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008044 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008045 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 (LPDLGTEMPLATE)pdlgtemplate,
8047 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008048 (DLGPROC)tearoff_callback,
8049 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
8051 LocalFree(LocalHandle(pdlgtemplate));
8052 SelectFont(hdc, oldFont);
8053 DeleteObject(font);
8054 ReleaseDC(hwnd, hdc);
8055
8056 /*
8057 * Reassert ourselves as the active window. This is so that after creating
8058 * a tearoff, the user doesn't have to click with the mouse just to start
8059 * typing again!
8060 */
8061 (void)SetActiveWindow(s_hwnd);
8062
Bram Moolenaar734a8672019-12-02 22:49:38 +01008063 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 force_menu_update = TRUE;
8065}
8066#endif
8067
8068#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008069# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
Bram Moolenaar734a8672019-12-02 22:49:38 +01008071// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072# ifndef TBSTYLE_FLAT
8073# define TBSTYLE_FLAT 0x0800
8074# endif
8075
8076/*
8077 * Create the toolbar, initially unpopulated.
8078 * (just like the menu, there are no defaults, it's all
8079 * set up through menu.vim)
8080 */
8081 static void
8082initialise_toolbar(void)
8083{
8084 InitCommonControls();
8085 s_toolbarhwnd = CreateToolbarEx(
8086 s_hwnd,
8087 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8088 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008089 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008090 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 IDR_TOOLBAR1, // id of initial bitmap
8092 NULL,
8093 0, // initial number of buttons
8094 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8095 TOOLBAR_BUTTON_HEIGHT,
8096 TOOLBAR_BUTTON_WIDTH,
8097 TOOLBAR_BUTTON_HEIGHT,
8098 sizeof(TBBUTTON)
8099 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008100
8101 // Remove transparency from the toolbar to prevent the main window
8102 // background colour showing through
8103 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8104 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8105
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008106 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
8108 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008109
8110 update_toolbar_size();
8111}
8112
8113 static void
8114update_toolbar_size(void)
8115{
8116 int w, h;
8117 TBMETRICS tbm = {sizeof(TBMETRICS)};
8118
8119 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8120 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8121 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8122 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8123
8124 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8125 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8126 //TRACE("button size: %d, %d", w, h);
8127 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8128 gui.toolbar_height = h + 6;
8129
8130 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8131 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8132
8133 // TODO:
8134 // Currently, this function only updates the size of toolbar buttons.
8135 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136}
8137
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008138 static LRESULT CALLBACK
8139toolbar_wndproc(
8140 HWND hwnd,
8141 UINT uMsg,
8142 WPARAM wParam,
8143 LPARAM lParam)
8144{
8145 HandleMouseHide(uMsg, lParam);
8146 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8147}
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 static int
8150get_toolbar_bitmap(vimmenu_T *menu)
8151{
8152 int i = -1;
8153
8154 /*
8155 * Check user bitmaps first, unless builtin is specified.
8156 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008157 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
8159 char_u fname[MAXPATHL];
8160 HANDLE hbitmap = NULL;
8161
8162 if (menu->iconfile != NULL)
8163 {
8164 gui_find_iconfile(menu->iconfile, fname, "bmp");
8165 hbitmap = LoadImage(
8166 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008167 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 IMAGE_BITMAP,
8169 TOOLBAR_BUTTON_WIDTH,
8170 TOOLBAR_BUTTON_HEIGHT,
8171 LR_LOADFROMFILE |
8172 LR_LOADMAP3DCOLORS
8173 );
8174 }
8175
8176 /*
8177 * If the LoadImage call failed, or the "icon=" file
8178 * didn't exist or wasn't specified, try the menu name
8179 */
8180 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008181 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008182# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008183 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008184# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008185 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 hbitmap = LoadImage(
8187 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008188 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 IMAGE_BITMAP,
8190 TOOLBAR_BUTTON_WIDTH,
8191 TOOLBAR_BUTTON_HEIGHT,
8192 LR_LOADFROMFILE |
8193 LR_LOADMAP3DCOLORS
8194 );
8195
8196 if (hbitmap != NULL)
8197 {
8198 TBADDBITMAP tbAddBitmap;
8199
8200 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008201 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202
8203 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8204 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008205 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 }
8207 }
8208 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8209 i = menu->iconidx;
8210
8211 return i;
8212}
8213#endif
8214
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008215#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8216 static void
8217initialise_tabline(void)
8218{
8219 InitCommonControls();
8220
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008221 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008222 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008223 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008224 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008225 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008226
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008227 gui.tabline_height = TABLINE_HEIGHT;
8228
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008229 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008230}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008231
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008232/*
8233 * Get tabpage_T from POINT.
8234 */
8235 static tabpage_T *
8236GetTabFromPoint(
8237 HWND hWnd,
8238 POINT pt)
8239{
8240 tabpage_T *ptp = NULL;
8241
8242 if (gui_mch_showing_tabline())
8243 {
8244 TCHITTESTINFO htinfo;
8245 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008246 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008247 if (s_tabhwnd == hWnd)
8248 {
8249 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8250 if (idx != -1)
8251 ptp = find_tabpage(idx + 1);
8252 }
8253 }
8254 return ptp;
8255}
8256
8257static POINT s_pt = {0, 0};
8258static HCURSOR s_hCursor = NULL;
8259
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008260 static LRESULT CALLBACK
8261tabline_wndproc(
8262 HWND hwnd,
8263 UINT uMsg,
8264 WPARAM wParam,
8265 LPARAM lParam)
8266{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008267 POINT pt;
8268 tabpage_T *tp;
8269 RECT rect;
8270 int nCenter;
8271 int idx0;
8272 int idx1;
8273
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008274 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008275
8276 switch (uMsg)
8277 {
8278 case WM_LBUTTONDOWN:
8279 {
8280 s_pt.x = GET_X_LPARAM(lParam);
8281 s_pt.y = GET_Y_LPARAM(lParam);
8282 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008283 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008284 break;
8285 }
8286 case WM_MOUSEMOVE:
8287 if (GetCapture() == hwnd
8288 && ((wParam & MK_LBUTTON)) != 0)
8289 {
8290 pt.x = GET_X_LPARAM(lParam);
8291 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008292 if (abs(pt.x - s_pt.x) >
8293 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008294 {
8295 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8296
8297 tp = GetTabFromPoint(hwnd, pt);
8298 if (tp != NULL)
8299 {
8300 idx0 = tabpage_index(curtab) - 1;
8301 idx1 = tabpage_index(tp) - 1;
8302
8303 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8304 nCenter = rect.left + (rect.right - rect.left) / 2;
8305
Bram Moolenaar734a8672019-12-02 22:49:38 +01008306 // Check if the mouse cursor goes over the center of
8307 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008308 if ((idx0 < idx1) && (nCenter < pt.x))
8309 {
8310 tabpage_move(idx1 + 1);
8311 update_screen(0);
8312 }
8313 else if ((idx1 < idx0) && (pt.x < nCenter))
8314 {
8315 tabpage_move(idx1);
8316 update_screen(0);
8317 }
8318 }
8319 }
8320 }
8321 break;
8322 case WM_LBUTTONUP:
8323 {
8324 if (GetCapture() == hwnd)
8325 {
8326 SetCursor(s_hCursor);
8327 ReleaseCapture();
8328 }
8329 break;
8330 }
8331 default:
8332 break;
8333 }
8334
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008335 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8336}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008337#endif
8338
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8340/*
8341 * Make the GUI window come to the foreground.
8342 */
8343 void
8344gui_mch_set_foreground(void)
8345{
8346 if (IsIconic(s_hwnd))
8347 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8348 SetForegroundWindow(s_hwnd);
8349}
8350#endif
8351
8352#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8353 static void
8354dyn_imm_load(void)
8355{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008356 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 if (hLibImm == NULL)
8358 return;
8359
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 pImmGetCompositionStringW
8361 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8362 pImmGetContext
8363 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8364 pImmAssociateContext
8365 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8366 pImmReleaseContext
8367 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8368 pImmGetOpenStatus
8369 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8370 pImmSetOpenStatus
8371 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008372 pImmGetCompositionFontW
8373 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8374 pImmSetCompositionFontW
8375 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 pImmSetCompositionWindow
8377 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8378 pImmGetConversionStatus
8379 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008380 pImmSetConversionStatus
8381 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382
K.Takatab0b2b732022-01-19 12:59:21 +00008383 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 || pImmGetContext == NULL
8385 || pImmAssociateContext == NULL
8386 || pImmReleaseContext == NULL
8387 || pImmGetOpenStatus == NULL
8388 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008389 || pImmGetCompositionFontW == NULL
8390 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008392 || pImmGetConversionStatus == NULL
8393 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {
8395 FreeLibrary(hLibImm);
8396 hLibImm = NULL;
8397 pImmGetContext = NULL;
8398 return;
8399 }
8400
8401 return;
8402}
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#endif
8405
8406#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8407
8408# ifdef FEAT_XPM_W32
8409# define IMAGE_XPM 100
8410# endif
8411
8412typedef struct _signicon_t
8413{
8414 HANDLE hImage;
8415 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008416# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008417 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419} signicon_t;
8420
8421 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008422gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 signicon_t *sign;
8425 int x, y, w, h;
8426
8427 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8428 return;
8429
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008430# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008431 if (IS_ENABLE_DIRECTX())
8432 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008433# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 x = TEXT_X(col);
8436 y = TEXT_Y(row);
8437 w = gui.char_width * 2;
8438 h = gui.char_height;
8439 switch (sign->uType)
8440 {
8441 case IMAGE_BITMAP:
8442 {
8443 HDC hdcMem;
8444 HBITMAP hbmpOld;
8445
8446 hdcMem = CreateCompatibleDC(s_hdc);
8447 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8448 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8449 SelectObject(hdcMem, hbmpOld);
8450 DeleteDC(hdcMem);
8451 }
8452 break;
8453 case IMAGE_ICON:
8454 case IMAGE_CURSOR:
8455 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8456 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008457# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 case IMAGE_XPM:
8459 {
8460 HDC hdcMem;
8461 HBITMAP hbmpOld;
8462
8463 hdcMem = CreateCompatibleDC(s_hdc);
8464 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008465 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8467
8468 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008469 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8471 SelectObject(hdcMem, hbmpOld);
8472 DeleteDC(hdcMem);
8473 }
8474 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477}
8478
8479 static void
8480close_signicon_image(signicon_t *sign)
8481{
8482 if (sign)
8483 switch (sign->uType)
8484 {
8485 case IMAGE_BITMAP:
8486 DeleteObject((HGDIOBJ)sign->hImage);
8487 break;
8488 case IMAGE_CURSOR:
8489 DestroyCursor((HCURSOR)sign->hImage);
8490 break;
8491 case IMAGE_ICON:
8492 DestroyIcon((HICON)sign->hImage);
8493 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008494# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 case IMAGE_XPM:
8496 DeleteObject((HBITMAP)sign->hImage);
8497 DeleteObject((HBITMAP)sign->hShape);
8498 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 }
8501}
8502
8503 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008504gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 signicon_t sign, *psign;
8507 char_u *ext;
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008510 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (ext > signfile)
8512 {
8513 int do_load = 1;
8514
8515 if (!STRICMP(ext, ".bmp"))
8516 sign.uType = IMAGE_BITMAP;
8517 else if (!STRICMP(ext, ".ico"))
8518 sign.uType = IMAGE_ICON;
8519 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8520 sign.uType = IMAGE_CURSOR;
8521 else
8522 do_load = 0;
8523
8524 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008525 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 gui.char_width * 2, gui.char_height,
8527 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008528# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (!STRICMP(ext, ".xpm"))
8530 {
8531 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008532 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8533 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 }
8537
8538 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008539 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 *psign = sign;
8541
8542 if (!psign)
8543 {
8544 if (sign.hImage)
8545 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008546 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548 return (void *)psign;
8549
8550}
8551
8552 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008553gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 if (sign)
8556 {
8557 close_signicon_image((signicon_t *)sign);
8558 vim_free(sign);
8559 }
8560}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008563#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564
Bram Moolenaar734a8672019-12-02 22:49:38 +01008565/*
8566 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008567 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008569 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8571 * to get current mouse position).
8572 *
8573 * Trying to use as more Windows services as possible, and as less
8574 * IE version as possible :)).
8575 *
8576 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8577 * BalloonEval struct.
8578 * 2) Enable/Disable simply create/kill BalloonEval Timer
8579 * 3) When there was enough inactivity, timer procedure posts
8580 * async request to debugger
8581 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8582 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008583 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 */
8585
Bram Moolenaar45360022005-07-21 21:08:21 +00008586/*
8587 * determine whether installed Common Controls support multiline tooltips
8588 * (i.e. their version is >= 4.70
8589 */
8590 int
8591multiline_balloon_available(void)
8592{
8593 HINSTANCE hDll;
8594 static char comctl_dll[] = "comctl32.dll";
8595 static int multiline_tip = MAYBE;
8596
8597 if (multiline_tip != MAYBE)
8598 return multiline_tip;
8599
8600 hDll = GetModuleHandle(comctl_dll);
8601 if (hDll != NULL)
8602 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008603 DLLGETVERSIONPROC pGetVer;
8604 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008605
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008606 if (pGetVer != NULL)
8607 {
8608 DLLVERSIONINFO dvi;
8609 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008610
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008611 ZeroMemory(&dvi, sizeof(dvi));
8612 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008613
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008614 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008615
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008616 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008617 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008618 || (dvi.dwMajorVersion == 4
8619 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008620 {
8621 multiline_tip = TRUE;
8622 return multiline_tip;
8623 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008624 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008625 else
8626 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008627 // there is chance we have ancient CommCtl 4.70
8628 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008629 DWORD dwHandle = 0;
8630 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8631 if (len > 0)
8632 {
8633 VS_FIXEDFILEINFO *ver;
8634 UINT vlen = 0;
8635 void *data = alloc(len);
8636
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008637 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008638 && GetFileVersionInfo(comctl_dll, 0, len, data)
8639 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8640 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008641 && HIWORD(ver->dwFileVersionMS) > 4)
8642 || ((HIWORD(ver->dwFileVersionMS) == 4
8643 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008644 {
8645 vim_free(data);
8646 multiline_tip = TRUE;
8647 return multiline_tip;
8648 }
8649 vim_free(data);
8650 }
8651 }
8652 }
8653 multiline_tip = FALSE;
8654 return multiline_tip;
8655}
8656
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008657 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008658make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008659{
8660 TOOLINFOW *pti;
8661 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008662
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008663 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008664 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8665 else
8666 ToolInfoSize = sizeof(TOOLINFOW);
8667
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008668 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008669 if (pti == NULL)
8670 return;
8671
8672 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8673 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8674 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008675 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008676
8677 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8678 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8679
8680 pti->cbSize = ToolInfoSize;
8681 pti->uFlags = TTF_SUBCLASS;
8682 pti->hwnd = beval->target;
8683 pti->hinst = 0; // Don't use string resources
8684 pti->uId = ID_BEVAL_TOOLTIP;
8685
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008686 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008687 {
8688 RECT rect;
8689 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8690 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008691 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8692 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008693 // switch multiline tooltips on
8694 if (GetClientRect(s_textArea, &rect))
8695 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8696 (LPARAM)rect.right);
8697 }
8698 else
8699 {
8700 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008701 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8702 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008703 }
8704
8705 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8706 pti->rect.left = pt.x - 3;
8707 pti->rect.top = pt.y - 3;
8708 pti->rect.right = pt.x + 3;
8709 pti->rect.bottom = pt.y + 3;
8710
8711 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8712 // Make tooltip appear sooner.
8713 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8714 // I've performed some tests and it seems the longest possible life time
8715 // of tooltip is 30 seconds.
8716 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8717 /*
8718 * HACK: force tooltip to appear, because it'll not appear until
8719 * first mouse move. D*mn M$
8720 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8721 */
8722 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8723 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8724 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008725}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008726
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008728delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008730 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731}
8732
8733 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008734BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008735 HWND hwnd UNUSED,
8736 UINT uMsg UNUSED,
8737 UINT_PTR idEvent UNUSED,
8738 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
8740 POINT pt;
8741 RECT rect;
8742
8743 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8744 return;
8745
8746 GetCursorPos(&pt);
8747 if (WindowFromPoint(pt) != s_textArea)
8748 return;
8749
8750 ScreenToClient(s_textArea, &pt);
8751 GetClientRect(s_textArea, &rect);
8752 if (!PtInRect(&rect, pt))
8753 return;
8754
8755 if (LastActivity > 0
8756 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8757 && (cur_beval->showState != ShS_PENDING
8758 || abs(cur_beval->x - pt.x) > 3
8759 || abs(cur_beval->y - pt.y) > 3))
8760 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008761 // Pointer resting in one place long enough, it's time to show
8762 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 cur_beval->showState = ShS_PENDING;
8764 cur_beval->x = pt.x;
8765 cur_beval->y = pt.y;
8766
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008767 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769 if (cur_beval->msgCB != NULL)
8770 (*cur_beval->msgCB)(cur_beval, 0);
8771 }
8772}
8773
8774 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008775gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008777 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008779 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780}
8781
8782 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008783gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008785 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 if (beval == NULL)
8787 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008788 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008789 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008790 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791}
8792
8793 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008794gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
8796 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008797
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008798 vim_free(beval->msg);
8799 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8800 if (beval->msg == NULL)
8801 {
8802 delete_tooltip(beval);
8803 beval->showState = ShS_NEUTRAL;
8804 return;
8805 }
8806
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008807 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 if (beval->showState == ShS_SHOWING)
8809 return;
8810 GetCursorPos(&pt);
8811 ScreenToClient(s_textArea, &pt);
8812
8813 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008815 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 gui_mch_disable_beval_area(cur_beval);
8817 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008818 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008820 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821}
8822
8823 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008824gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008825 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008826 char_u *mesg,
8827 void (*mesgCB)(BalloonEval *, int),
8828 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008830 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 BalloonEval *beval;
8832
8833 if (mesg != NULL && mesgCB != NULL)
8834 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008835 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 return NULL;
8837 }
8838
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008839 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (beval != NULL)
8841 {
8842 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
8844 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 beval->msg = mesg;
8846 beval->msgCB = mesgCB;
8847 beval->clientData = clientData;
8848
8849 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 cur_beval = beval;
8851
8852 if (p_beval)
8853 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 }
8855 return beval;
8856}
8857
8858 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008859Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008861 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 return;
8863
8864 if (cur_beval != NULL)
8865 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008866 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008868 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008869 // TRACE0("TTN_SHOW {{{");
8870 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008871 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008872 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008873 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 delete_tooltip(cur_beval);
8875 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008876 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008879 break;
8880 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008881 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008882 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008883 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8884 info->lpszText = (LPSTR)info->lParam;
8885 info->uFlags |= TTF_DI_SETITEM;
8886 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008887 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008888 case TTN_GETDISPINFOW:
8889 {
8890 // if we get here then we have new common controls
8891 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8892 info->lpszText = (LPWSTR)info->lParam;
8893 info->uFlags |= TTF_DI_SETITEM;
8894 }
8895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 }
8897 }
8898}
8899
8900 static void
8901TrackUserActivity(UINT uMsg)
8902{
8903 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8904 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8905 LastActivity = GetTickCount();
8906}
8907
8908 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008909gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008911# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008912 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008913# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008914 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 vim_free(beval);
8916}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008917#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918
8919#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8920/*
8921 * We have multiple signs to draw at the same location. Draw the
8922 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8923 */
8924 void
8925netbeans_draw_multisign_indicator(int row)
8926{
8927 int i;
8928 int y;
8929 int x;
8930
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008931 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008932 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008933
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 x = 0;
8935 y = TEXT_Y(row);
8936
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008937# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008938 if (IS_ENABLE_DIRECTX())
8939 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008940# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008941
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 for (i = 0; i < gui.char_height - 3; i++)
8943 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8944
8945 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8946 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8947 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8948 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8949 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8950 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8951 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8952}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008953#endif