blob: 79afa7a2d9d5678e464a04d7840eed1ca0a1acd9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000043#else
44# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010045#endif
46
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020047#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
48 int
49gui_mch_set_rendering_options(char_u *s)
50{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010051# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020052 char_u *p, *q;
53
54 int dx_enable = 0;
55 int dx_flags = 0;
56 float dx_gamma = 0.0f;
57 float dx_contrast = 0.0f;
58 float dx_level = 0.0f;
59 int dx_geom = 0;
60 int dx_renmode = 0;
61 int dx_taamode = 0;
62
Bram Moolenaar734a8672019-12-02 22:49:38 +010063 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100124 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100132
Bram Moolenaar734a8672019-12-02 22:49:38 +0100133 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100163# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183
Bram Moolenaar734a8672019-12-02 22:49:38 +0100184#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100185#ifdef DEBUG
186# include <tchar.h>
187#endif
188
Bram Moolenaar734a8672019-12-02 22:49:38 +0100189// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100190#ifndef PROTO
191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100192# ifndef __MINGW32__
193# include <shellapi.h>
194# endif
195# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
196# include <commctrl.h>
197# endif
198# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100199
Bram Moolenaar734a8672019-12-02 22:49:38 +0100200#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
202#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100203# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100204#endif
205
Bram Moolenaar734a8672019-12-02 22:49:38 +0100206// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100207#define DLG_PADDING_X 10
208#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100209#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100210#define DLG_VERT_PADDING_Y 4
211#define DLG_ICON_WIDTH 34
212#define DLG_ICON_HEIGHT 34
213#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000214#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100215#define DLG_FONT_POINT_SIZE 8
216#define DLG_MIN_MAX_WIDTH 400
217#define DLG_MIN_MAX_HEIGHT 400
218
Bram Moolenaar734a8672019-12-02 22:49:38 +0100219#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100220
K.Takatac81e9bf2022-01-16 14:15:49 +0000221#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100222# define WM_DPICHANGED 0x02E0
223#endif
224
225#ifndef WM_MOUSEHWHEEL
226# define WM_MOUSEHWHEEL 0x020E
227#endif
228
229#ifndef SPI_GETWHEELSCROLLCHARS
230# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000231#endif
232
LemonBoyc27747e2022-05-07 12:25:40 +0100233#ifndef SPI_SETWHEELSCROLLCHARS
234# define SPI_SETWHEELSCROLLCHARS 0x006D
235#endif
236
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100239 * Define a few things for generating prototypes. This is just to avoid
240 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100242# define APIENTRY
243# define CALLBACK
244# define CONST
245# define FAR
246# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200247# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200248# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100249# define _cdecl
250typedef int BOOL;
251typedef int BYTE;
252typedef int DWORD;
253typedef int WCHAR;
254typedef int ENUMLOGFONT;
255typedef int FINDREPLACE;
256typedef int HANDLE;
257typedef int HBITMAP;
258typedef int HBRUSH;
259typedef int HDROP;
260typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100261typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100262typedef int LPARAM;
263typedef int LPCREATESTRUCT;
264typedef int LPCSTR;
265typedef int LPCTSTR;
266typedef int LPRECT;
267typedef int LPSTR;
268typedef int LPWINDOWPOS;
269typedef int LPWORD;
270typedef int LRESULT;
271typedef int HRESULT;
272# undef MSG
273typedef int MSG;
274typedef int NEWTEXTMETRIC;
275typedef int OSVERSIONINFO;
276typedef int PWORD;
277typedef int RECT;
278typedef int UINT;
279typedef int WORD;
280typedef int WPARAM;
281typedef int POINT;
282typedef void *HINSTANCE;
283typedef void *HMENU;
284typedef void *HWND;
285typedef void *HDC;
286typedef void VOID;
287typedef int LPNMHDR;
288typedef int LONG;
289typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200290typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200291typedef int COLORREF;
292typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100293#endif
294
K.Takata45f9cfb2022-01-21 11:11:00 +0000295static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000317static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
K.Takatac81e9bf2022-01-16 14:15:49 +0000355#ifndef _DPI_AWARENESS_CONTEXTS_
356typedef HANDLE DPI_AWARENESS_CONTEXT;
357
358typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000359 DPI_AWARENESS_INVALID = -1,
360 DPI_AWARENESS_UNAWARE = 0,
361 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000362 DPI_AWARENESS_PER_MONITOR_AWARE = 2
363} DPI_AWARENESS;
364
K.Takatab0b2b732022-01-19 12:59:21 +0000365# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
366# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000367# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
368# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
369# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
370#endif
371
372#define DEFAULT_DPI 96
373static int s_dpi = DEFAULT_DPI;
374static BOOL s_in_dpichanged = FALSE;
375static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
376
377static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
378static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
379static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
380//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
381static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
382static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
383
384 static UINT WINAPI
385stubGetDpiForSystem(void)
386{
387 HWND hwnd = GetDesktopWindow();
388 HDC hdc = GetWindowDC(hwnd);
389 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
390 ReleaseDC(hwnd, hdc);
391 return dpi;
392}
393
394 static int WINAPI
395stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
396{
397 return GetSystemMetrics(nIndex);
398}
399
400 static int
401adjust_fontsize_by_dpi(int size)
402{
403 return size * s_dpi / (int)pGetDpiForSystem();
404}
405
406 static int
407adjust_by_system_dpi(int size)
408{
409 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
410}
411
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100412#if defined(FEAT_DIRECTX)
413 static int
414directx_enabled(void)
415{
416 if (s_dwc != NULL)
417 return 1;
418 else if (s_directx_load_attempted)
419 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100420 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100421 DWrite_Init();
422 s_directx_load_attempted = 1;
423 s_dwc = DWriteContext_Open();
424 directx_binddc();
425 return s_dwc != NULL ? 1 : 0;
426}
427
428 static void
429directx_binddc(void)
430{
431 if (s_textArea != NULL)
432 {
433 RECT rect;
434 GetClientRect(s_textArea, &rect);
435 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
436 }
437}
438#endif
439
Bram Moolenaar734a8672019-12-02 22:49:38 +0100440extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100441
442static struct
443{
444 UINT key_sym;
445 char_u vim_code0;
446 char_u vim_code1;
447} special_keys[] =
448{
449 {VK_UP, 'k', 'u'},
450 {VK_DOWN, 'k', 'd'},
451 {VK_LEFT, 'k', 'l'},
452 {VK_RIGHT, 'k', 'r'},
453
454 {VK_F1, 'k', '1'},
455 {VK_F2, 'k', '2'},
456 {VK_F3, 'k', '3'},
457 {VK_F4, 'k', '4'},
458 {VK_F5, 'k', '5'},
459 {VK_F6, 'k', '6'},
460 {VK_F7, 'k', '7'},
461 {VK_F8, 'k', '8'},
462 {VK_F9, 'k', '9'},
463 {VK_F10, 'k', ';'},
464
465 {VK_F11, 'F', '1'},
466 {VK_F12, 'F', '2'},
467 {VK_F13, 'F', '3'},
468 {VK_F14, 'F', '4'},
469 {VK_F15, 'F', '5'},
470 {VK_F16, 'F', '6'},
471 {VK_F17, 'F', '7'},
472 {VK_F18, 'F', '8'},
473 {VK_F19, 'F', '9'},
474 {VK_F20, 'F', 'A'},
475
476 {VK_F21, 'F', 'B'},
477#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100478 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479#endif
480 {VK_F22, 'F', 'C'},
481 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100482 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100483
484 {VK_HELP, '%', '1'},
485 {VK_BACK, 'k', 'b'},
486 {VK_INSERT, 'k', 'I'},
487 {VK_DELETE, 'k', 'D'},
488 {VK_HOME, 'k', 'h'},
489 {VK_END, '@', '7'},
490 {VK_PRIOR, 'k', 'P'},
491 {VK_NEXT, 'k', 'N'},
492 {VK_PRINT, '%', '9'},
493 {VK_ADD, 'K', '6'},
494 {VK_SUBTRACT, 'K', '7'},
495 {VK_DIVIDE, 'K', '8'},
496 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100497 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100498 {VK_DECIMAL, 'K', 'B'},
499
500 {VK_NUMPAD0, 'K', 'C'},
501 {VK_NUMPAD1, 'K', 'D'},
502 {VK_NUMPAD2, 'K', 'E'},
503 {VK_NUMPAD3, 'K', 'F'},
504 {VK_NUMPAD4, 'K', 'G'},
505 {VK_NUMPAD5, 'K', 'H'},
506 {VK_NUMPAD6, 'K', 'I'},
507 {VK_NUMPAD7, 'K', 'J'},
508 {VK_NUMPAD8, 'K', 'K'},
509 {VK_NUMPAD9, 'K', 'L'},
510
Bram Moolenaar734a8672019-12-02 22:49:38 +0100511 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100512 {VK_SPACE, ' ', NUL},
513 {VK_TAB, TAB, NUL},
514 {VK_ESCAPE, ESC, NUL},
515 {NL, NL, NUL},
516 {CAR, CAR, NUL},
517
Bram Moolenaar734a8672019-12-02 22:49:38 +0100518 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100519 {0, 0, 0}
520};
521
Bram Moolenaar734a8672019-12-02 22:49:38 +0100522// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523static int s_button_pending = -1;
524
Bram Moolenaar734a8672019-12-02 22:49:38 +0100525// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
526// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100527static int s_getting_focus = FALSE;
528
529static int s_x_pending;
530static int s_y_pending;
531static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000532static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200534static int dead_key = 0; // 0: no dead key, 1: dead key pressed
535static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
536 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100537
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100538#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100539// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100540static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000541static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542#endif
543
544/*
545 * For control IME.
546 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100547 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548 */
K.Takata4ac893f2022-01-20 12:44:28 +0000549#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100550// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100551static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100554#endif
555
556#ifdef FEAT_MBYTE_IME
557static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
558#endif
559
560#if defined(FEAT_BROWSE)
561static char_u *convert_filter(char_u *s);
562#endif
563
564#ifdef DEBUG_PRINT_ERROR
565/*
566 * Print out the last Windows error message
567 */
568 static void
569print_windows_error(void)
570{
571 LPVOID lpMsgBuf;
572
573 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
574 NULL, GetLastError(),
575 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
576 (LPTSTR) &lpMsgBuf, 0, NULL);
577 TRACE1("Error: %s\n", lpMsgBuf);
578 LocalFree(lpMsgBuf);
579}
580#endif
581
582/*
583 * Cursor blink functions.
584 *
585 * This is a simple state machine:
586 * BLINK_NONE not blinking at all
587 * BLINK_OFF blinking, cursor is not shown
588 * BLINK_ON blinking, cursor is shown
589 */
590
591#define BLINK_NONE 0
592#define BLINK_OFF 1
593#define BLINK_ON 2
594
595static int blink_state = BLINK_NONE;
596static long_u blink_waittime = 700;
597static long_u blink_ontime = 400;
598static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000599static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100600
Bram Moolenaar703a8042016-06-04 16:24:32 +0200601 int
602gui_mch_is_blinking(void)
603{
604 return blink_state != BLINK_NONE;
605}
606
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200607 int
608gui_mch_is_blink_off(void)
609{
610 return blink_state == BLINK_OFF;
611}
612
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100613 void
614gui_mch_set_blinking(long wait, long on, long off)
615{
616 blink_waittime = wait;
617 blink_ontime = on;
618 blink_offtime = off;
619}
620
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100621 static VOID CALLBACK
622_OnBlinkTimer(
623 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100624 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000625 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627{
628 MSG msg;
629
630 /*
631 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
632 */
633
634 KillTimer(NULL, idEvent);
635
Bram Moolenaar734a8672019-12-02 22:49:38 +0100636 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000637 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 ;
639
640 if (blink_state == BLINK_ON)
641 {
642 gui_undraw_cursor();
643 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000644 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100645 }
646 else
647 {
648 gui_update_cursor(TRUE, FALSE);
649 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000650 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100652 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653}
654
655 static void
656gui_mswin_rm_blink_timer(void)
657{
658 MSG msg;
659
660 if (blink_timer != 0)
661 {
662 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100663 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000664 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665 ;
666 blink_timer = 0;
667 }
668}
669
670/*
671 * Stop the cursor blinking. Show the cursor if it wasn't shown.
672 */
673 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100674gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100675{
676 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100677 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100678 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100680 gui_mch_flush();
681 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100682 blink_state = BLINK_NONE;
683}
684
685/*
686 * Start the cursor blinking. If it was already blinking, this restarts the
687 * waiting time and shows the cursor.
688 */
689 void
690gui_mch_start_blink(void)
691{
692 gui_mswin_rm_blink_timer();
693
Bram Moolenaar734a8672019-12-02 22:49:38 +0100694 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
696 {
K.Takataa8ec4912022-02-03 14:32:33 +0000697 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 blink_state = BLINK_ON;
699 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100700 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 }
702}
703
704/*
705 * Call-back routines.
706 */
707
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100708 static VOID CALLBACK
709_OnTimer(
710 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100711 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000712 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100713 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714{
715 MSG msg;
716
717 /*
718 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
719 */
720 KillTimer(NULL, idEvent);
721 s_timed_out = TRUE;
722
Bram Moolenaar734a8672019-12-02 22:49:38 +0100723 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000724 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100725 ;
726 if (idEvent == s_wait_timer)
727 s_wait_timer = 0;
728}
729
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730 static void
731_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100732 HWND hwnd UNUSED,
733 UINT ch UNUSED,
734 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100735{
736 dead_key = 1;
737}
738
739/*
740 * Convert Unicode character "ch" to bytes in "string[slen]".
741 * When "had_alt" is TRUE the ALT key was included in "ch".
742 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200743 * Because the Windows API uses UTF-16, we have to deal with surrogate
744 * pairs; this is where we choose to deal with them: if "ch" is a high
745 * surrogate, it will be stored, and the length returned will be zero; the next
746 * char_to_string call will then include the high surrogate, decoding the pair
747 * of UTF-16 code units to a single Unicode code point, presuming it is the
748 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749 */
750 static int
751char_to_string(int ch, char_u *string, int slen, int had_alt)
752{
753 int len;
754 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200756 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200758 if (surrogate_pending_ch != 0)
759 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100760 // We don't guarantee ch is a low surrogate to match the high surrogate
761 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200762 wstring[0] = surrogate_pending_ch;
763 wstring[1] = ch;
764 surrogate_pending_ch = 0;
765 len = 2;
766 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100767 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200768 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100769 // We don't have the entire code point yet, only the first UTF-16 code
770 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200771 surrogate_pending_ch = ch;
772 return 0;
773 }
774 else
775 {
776 wstring[0] = ch;
777 len = 1;
778 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200779
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
781 // "enc_codepage" is non-zero use the standard Win32 function,
782 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200783 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100784 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
786 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100787 // If we had included the ALT key into the character but now the
788 // upper bit is no longer set, that probably means the conversion
789 // failed. Convert the original character and set the upper bit
790 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100792 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 wstring[0] = ch & 0x7f;
794 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
795 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100796 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200797 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 }
799 }
800 else
801 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 ws = utf16_to_enc(wstring, &len);
803 if (ws == NULL)
804 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100805 else
806 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100807 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 len = slen;
809 mch_memmove(string, ws, len);
810 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 }
812 }
813
814 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100815 {
816 string[0] = ch;
817 len = 1;
818 }
819
820 for (i = 0; i < len; ++i)
821 if (string[i] == CSI && len <= slen - 2)
822 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100823 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
825 string[++i] = KS_EXTRA;
826 string[++i] = (int)KE_CSI;
827 len += 2;
828 }
829
830 return len;
831}
832
LemonBoy45684c62022-04-24 15:46:42 +0100833 static int
834get_active_modifiers(void)
835{
836 int modifiers = 0;
837
838 if (GetKeyState(VK_CONTROL) & 0x8000)
839 modifiers |= MOD_MASK_CTRL;
840 if (GetKeyState(VK_SHIFT) & 0x8000)
841 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100842 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
843 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100844 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100845 if (GetKeyState(VK_LMENU) & 0x8000)
846 modifiers |= MOD_MASK_ALT;
847 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
848 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100849
850 return modifiers;
851}
852
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100853/*
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,
LemonBoy77fc0b02022-04-22 22:45:52 +0100859 UINT cch,
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;
LemonBoy45684c62022-04-24 15:46:42 +0100864 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100865 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100866
867 dead_key = 0;
868
LemonBoy45684c62022-04-24 15:46:42 +0100869 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100870
871 ch = simplify_key(ch, &modifiers);
872 // remove the SHIFT modifier for keys where it's already included, e.g.,
873 // '(' and '*'
874 modifiers = may_remove_shift_modifier(modifiers, ch);
875
876 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
877 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
878 if (ch == CSI)
879 ch = K_CSI;
880
881 if (modifiers)
882 {
883 string[0] = CSI;
884 string[1] = KS_MODIFIER;
885 string[2] = modifiers;
886 add_to_input_buf(string, 3);
887 }
888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100889 len = char_to_string(ch, string, 40, FALSE);
890 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
891 {
892 trash_input_buf();
893 got_int = TRUE;
894 }
895
896 add_to_input_buf(string, len);
897}
898
899/*
900 * Alt-Key hit, add it to the input buffer.
901 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100902 static void
903_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100904 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100905 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100906 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100908 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909 int len;
910 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100911 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100912
913 dead_key = 0;
914
Bram Moolenaar734a8672019-12-02 22:49:38 +0100915 // OK, we have a character key (given by ch) which was entered with the
916 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
917 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
918 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100919 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100920 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100921 // remove the SHIFT modifier for keys where it's already included, e.g.,
922 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200923 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100924
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200925 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
926 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100927 if (ch == CSI)
928 ch = K_CSI;
929
930 len = 0;
931 if (modifiers)
932 {
933 string[len++] = CSI;
934 string[len++] = KS_MODIFIER;
935 string[len++] = modifiers;
936 }
937
938 if (IS_SPECIAL((int)ch))
939 {
940 string[len++] = CSI;
941 string[len++] = K_SECOND((int)ch);
942 string[len++] = K_THIRD((int)ch);
943 }
944 else
945 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100946 // Although the documentation isn't clear about it, we assume "ch" is
947 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100948 len += char_to_string(ch, string + len, 40 - len, TRUE);
949 }
950
951 add_to_input_buf(string, len);
952}
953
954 static void
955_OnMouseEvent(
956 int button,
957 int x,
958 int y,
959 int repeated_click,
960 UINT keyFlags)
961{
962 int vim_modifiers = 0x0;
963
964 s_getting_focus = FALSE;
965
966 if (keyFlags & MK_SHIFT)
967 vim_modifiers |= MOUSE_SHIFT;
968 if (keyFlags & MK_CONTROL)
969 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100970 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100971 vim_modifiers |= MOUSE_ALT;
972
973 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
974}
975
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100976 static void
977_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100978 HWND hwnd UNUSED,
979 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100980 int x,
981 int y,
982 UINT keyFlags)
983{
984 static LONG s_prevTime = 0;
985
986 LONG currentTime = GetMessageTime();
987 int button = -1;
988 int repeated_click;
989
Bram Moolenaar734a8672019-12-02 22:49:38 +0100990 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100991 (void)SetFocus(s_hwnd);
992
993 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
994 button = MOUSE_LEFT;
995 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
996 button = MOUSE_MIDDLE;
997 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
998 button = MOUSE_RIGHT;
999 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1000 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1002 }
1003 else if (s_uMsg == WM_CAPTURECHANGED)
1004 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001005 // on W95/NT4, somehow you get in here with an odd Msg
1006 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001007 if (s_button_pending == MOUSE_LEFT)
1008 button = MOUSE_RIGHT;
1009 else
1010 button = MOUSE_LEFT;
1011 }
1012 if (button >= 0)
1013 {
1014 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1015
1016 /*
1017 * Holding down the left and right buttons simulates pushing the middle
1018 * button.
1019 */
1020 if (repeated_click
1021 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1022 || (button == MOUSE_RIGHT
1023 && s_button_pending == MOUSE_LEFT)))
1024 {
1025 /*
1026 * Hmm, gui.c will ignore more than one button down at a time, so
1027 * pretend we let go of it first.
1028 */
1029 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1030 button = MOUSE_MIDDLE;
1031 repeated_click = FALSE;
1032 s_button_pending = -1;
1033 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1034 }
1035 else if ((repeated_click)
1036 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1037 {
1038 if (s_button_pending > -1)
1039 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001040 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1041 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001042 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001043 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001044 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1045 }
1046 else
1047 {
1048 /*
1049 * If this is the first press (i.e. not a multiple click) don't
1050 * action immediately, but store and wait for:
1051 * i) button-up
1052 * ii) mouse move
1053 * iii) another button press
1054 * before using it.
1055 * This enables us to make left+right simulate middle button,
1056 * without left or right being actioned first. The side-effect is
1057 * that if you click and hold the mouse without dragging, the
1058 * cursor doesn't move until you release the button. In practice
1059 * this is hardly a problem.
1060 */
1061 s_button_pending = button;
1062 s_x_pending = x;
1063 s_y_pending = y;
1064 s_kFlags_pending = keyFlags;
1065 }
1066
1067 s_prevTime = currentTime;
1068 }
1069}
1070
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001071 static void
1072_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001073 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001074 int x,
1075 int y,
1076 UINT keyFlags)
1077{
1078 int button;
1079
1080 s_getting_focus = FALSE;
1081 if (s_button_pending > -1)
1082 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001083 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001084 _OnMouseEvent(s_button_pending, s_x_pending,
1085 s_y_pending, FALSE, s_kFlags_pending);
1086 s_button_pending = -1;
1087 }
1088 if (s_uMsg == WM_MOUSEMOVE)
1089 {
1090 /*
1091 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1092 * down.
1093 */
1094 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1095 | MK_XBUTTON1 | MK_XBUTTON2)))
1096 {
1097 gui_mouse_moved(x, y);
1098 return;
1099 }
1100
1101 /*
1102 * While button is down, keep grabbing mouse move events when
1103 * the mouse goes outside the window
1104 */
1105 SetCapture(s_textArea);
1106 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001107 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 }
1109 else
1110 {
1111 ReleaseCapture();
1112 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001113 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 }
1115
1116 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1117}
1118
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001119 static void
1120_OnSizeTextArea(
1121 HWND hwnd UNUSED,
1122 UINT state UNUSED,
1123 int cx UNUSED,
1124 int cy UNUSED)
1125{
1126#if defined(FEAT_DIRECTX)
1127 if (IS_ENABLE_DIRECTX())
1128 directx_binddc();
1129#endif
1130}
1131
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001132#ifdef FEAT_MENU
1133/*
1134 * Find the vimmenu_T with the given id
1135 */
1136 static vimmenu_T *
1137gui_mswin_find_menu(
1138 vimmenu_T *pMenu,
1139 int id)
1140{
1141 vimmenu_T *pChildMenu;
1142
1143 while (pMenu)
1144 {
1145 if (pMenu->id == (UINT)id)
1146 break;
1147 if (pMenu->children != NULL)
1148 {
1149 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1150 if (pChildMenu)
1151 {
1152 pMenu = pChildMenu;
1153 break;
1154 }
1155 }
1156 pMenu = pMenu->next;
1157 }
1158 return pMenu;
1159}
1160
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001161 static void
1162_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001163 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001164 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001165 HWND hwndCtl UNUSED,
1166 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001167{
1168 vimmenu_T *pMenu;
1169
1170 pMenu = gui_mswin_find_menu(root_menu, id);
1171 if (pMenu)
1172 gui_menu_cb(pMenu);
1173}
1174#endif
1175
1176#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001177/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001178 * Handle a Find/Replace window message.
1179 */
1180 static void
1181_OnFindRepl(void)
1182{
1183 int flags = 0;
1184 int down;
1185
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001187 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001188 (void)SetFocus(s_hwnd);
1189
1190 if (s_findrep_struct.Flags & FR_FINDNEXT)
1191 {
1192 flags = FRD_FINDNEXT;
1193
Bram Moolenaar734a8672019-12-02 22:49:38 +01001194 // Give main window the focus back: this is so the cursor isn't
1195 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001196 (void)SetFocus(s_hwnd);
1197 }
1198 else if (s_findrep_struct.Flags & FR_REPLACE)
1199 {
1200 flags = FRD_REPLACE;
1201
Bram Moolenaar734a8672019-12-02 22:49:38 +01001202 // Give main window the focus back: this is so the cursor isn't
1203 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001204 (void)SetFocus(s_hwnd);
1205 }
1206 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1207 {
1208 flags = FRD_REPLACEALL;
1209 }
1210
1211 if (flags != 0)
1212 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001213 char_u *p, *q;
1214
Bram Moolenaar734a8672019-12-02 22:49:38 +01001215 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001216 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1217 flags |= FRD_WHOLE_WORD;
1218 if (s_findrep_struct.Flags & FR_MATCHCASE)
1219 flags |= FRD_MATCH_CASE;
1220 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001221 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1222 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1223 if (p != NULL && q != NULL)
1224 gui_do_findrepl(flags, p, q, down);
1225 vim_free(p);
1226 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001227 }
1228}
1229#endif
1230
1231 static void
1232HandleMouseHide(UINT uMsg, LPARAM lParam)
1233{
1234 static LPARAM last_lParam = 0L;
1235
Bram Moolenaar734a8672019-12-02 22:49:38 +01001236 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001237 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1238 {
1239 if (lParam == last_lParam)
1240 return;
1241 last_lParam = lParam;
1242 }
1243
Bram Moolenaar734a8672019-12-02 22:49:38 +01001244 // Handle specially, to centralise coding. We need to be sure we catch all
1245 // possible events which should cause us to restore the cursor (as it is a
1246 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001247 switch (uMsg)
1248 {
1249 case WM_KEYUP:
1250 case WM_CHAR:
1251 /*
1252 * blank out the pointer if necessary
1253 */
1254 if (p_mh)
1255 gui_mch_mousehide(TRUE);
1256 break;
1257
Bram Moolenaar734a8672019-12-02 22:49:38 +01001258 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001259 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001260 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261 case WM_LBUTTONDOWN:
1262 case WM_LBUTTONUP:
1263 case WM_MBUTTONDOWN:
1264 case WM_MBUTTONUP:
1265 case WM_RBUTTONDOWN:
1266 case WM_RBUTTONUP:
1267 case WM_XBUTTONDOWN:
1268 case WM_XBUTTONUP:
1269 case WM_NCMOUSEMOVE:
1270 case WM_NCLBUTTONDOWN:
1271 case WM_NCLBUTTONUP:
1272 case WM_NCMBUTTONDOWN:
1273 case WM_NCMBUTTONUP:
1274 case WM_NCRBUTTONDOWN:
1275 case WM_NCRBUTTONUP:
1276 case WM_KILLFOCUS:
1277 /*
1278 * if the pointer is currently hidden, then we should show it.
1279 */
1280 gui_mch_mousehide(FALSE);
1281 break;
1282 }
1283}
1284
1285 static LRESULT CALLBACK
1286_TextAreaWndProc(
1287 HWND hwnd,
1288 UINT uMsg,
1289 WPARAM wParam,
1290 LPARAM lParam)
1291{
1292 /*
1293 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1294 hwnd, uMsg, wParam, lParam);
1295 */
1296
1297 HandleMouseHide(uMsg, lParam);
1298
1299 s_uMsg = uMsg;
1300 s_wParam = wParam;
1301 s_lParam = lParam;
1302
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001303#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001304 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001305#endif
1306
1307 switch (uMsg)
1308 {
1309 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1310 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1311 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1312 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1313 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1314 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1315 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1316 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1317 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1318 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1319 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1320 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1321 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1322 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001323 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001324
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001325#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001326 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1327 return TRUE;
1328#endif
1329 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001330 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331 }
1332}
1333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001334/*
1335 * Called when the foreground or background color has been changed.
1336 */
1337 void
1338gui_mch_new_colors(void)
1339{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001340 HBRUSH prevBrush;
1341
1342 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001343 prevBrush = (HBRUSH)SetClassLongPtr(
1344 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001345 InvalidateRect(s_hwnd, NULL, TRUE);
1346 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001347}
1348
1349/*
1350 * Set the colors to their default values.
1351 */
1352 void
1353gui_mch_def_colors(void)
1354{
1355 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1356 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1357 gui.def_norm_pixel = gui.norm_pixel;
1358 gui.def_back_pixel = gui.back_pixel;
1359}
1360
1361/*
1362 * Open the GUI window which was created by a call to gui_mch_init().
1363 */
1364 int
1365gui_mch_open(void)
1366{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001367 // Actually open the window, if not already visible
1368 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001369 if (!IsWindowVisible(s_hwnd))
1370 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1371
1372#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001373 // Init replace string here, so that we keep it when re-opening the
1374 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001375 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1376#endif
1377
1378 return OK;
1379}
1380
1381/*
1382 * Get the position of the top left corner of the window.
1383 */
1384 int
1385gui_mch_get_winpos(int *x, int *y)
1386{
1387 RECT rect;
1388
1389 GetWindowRect(s_hwnd, &rect);
1390 *x = rect.left;
1391 *y = rect.top;
1392 return OK;
1393}
1394
1395/*
1396 * Set the position of the top left corner of the window to the given
1397 * coordinates.
1398 */
1399 void
1400gui_mch_set_winpos(int x, int y)
1401{
1402 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1403 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1404}
1405 void
1406gui_mch_set_text_area_pos(int x, int y, int w, int h)
1407{
1408 static int oldx = 0;
1409 static int oldy = 0;
1410
1411 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1412
1413#ifdef FEAT_TOOLBAR
1414 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1415 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001416 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001417#endif
1418#if defined(FEAT_GUI_TABLINE)
1419 if (showing_tabline)
1420 {
1421 int top = 0;
1422 RECT rect;
1423
1424# ifdef FEAT_TOOLBAR
1425 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001426 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001427# endif
1428 GetClientRect(s_hwnd, &rect);
1429 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1430 }
1431#endif
1432
Bram Moolenaar734a8672019-12-02 22:49:38 +01001433 // When side scroll bar is unshown, the size of window will change.
1434 // then, the text area move left or right. thus client rect should be
1435 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001436 if (oldx != x || oldy != y)
1437 {
1438 InvalidateRect(s_hwnd, NULL, FALSE);
1439 oldx = x;
1440 oldy = y;
1441 }
1442}
1443
1444
1445/*
1446 * Scrollbar stuff:
1447 */
1448
1449 void
1450gui_mch_enable_scrollbar(
1451 scrollbar_T *sb,
1452 int flag)
1453{
1454 ShowScrollBar(sb->id, SB_CTL, flag);
1455
Bram Moolenaar734a8672019-12-02 22:49:38 +01001456 // TODO: When the window is maximized, the size of the window stays the
1457 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1458 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001459}
1460
1461 void
1462gui_mch_set_scrollbar_pos(
1463 scrollbar_T *sb,
1464 int x,
1465 int y,
1466 int w,
1467 int h)
1468{
1469 SetWindowPos(sb->id, NULL, x, y, w, h,
1470 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1471}
1472
Bram Moolenaar203ec772020-07-17 20:43:43 +02001473 int
1474gui_mch_get_scrollbar_xpadding(void)
1475{
1476 RECT rcTxt, rcWnd;
1477 int xpad;
1478
1479 GetWindowRect(s_textArea, &rcTxt);
1480 GetWindowRect(s_hwnd, &rcWnd);
1481 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001482 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1483 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001484 return (xpad < 0) ? 0 : xpad;
1485}
1486
1487 int
1488gui_mch_get_scrollbar_ypadding(void)
1489{
1490 RECT rcTxt, rcWnd;
1491 int ypad;
1492
1493 GetWindowRect(s_textArea, &rcTxt);
1494 GetWindowRect(s_hwnd, &rcWnd);
1495 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001496 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1497 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001498 return (ypad < 0) ? 0 : ypad;
1499}
1500
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001501 void
1502gui_mch_create_scrollbar(
1503 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001504 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001505{
1506 sb->id = CreateWindow(
1507 "SCROLLBAR", "Scrollbar",
1508 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001509 10, // Any value will do for now
1510 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001511 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001512 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513}
1514
1515/*
1516 * Find the scrollbar with the given hwnd.
1517 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001518 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519gui_mswin_find_scrollbar(HWND hwnd)
1520{
1521 win_T *wp;
1522
1523 if (gui.bottom_sbar.id == hwnd)
1524 return &gui.bottom_sbar;
1525 FOR_ALL_WINDOWS(wp)
1526 {
1527 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1528 return &wp->w_scrollbars[SBAR_LEFT];
1529 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1530 return &wp->w_scrollbars[SBAR_RIGHT];
1531 }
1532 return NULL;
1533}
1534
K.Takatac81e9bf2022-01-16 14:15:49 +00001535 static void
1536update_scrollbar_size(void)
1537{
1538 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1539 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1540}
1541
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001542/*
K.Takataabe628e2022-01-23 16:25:17 +00001543 * Get the average character size of a font.
1544 */
1545 static void
1546GetAverageFontSize(HDC hdc, SIZE *size)
1547{
1548 // GetTextMetrics() may not return the right value in tmAveCharWidth
1549 // for some fonts. Do our own average computation.
1550 GetTextExtentPoint(hdc,
1551 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1552 52, size);
1553 size->cx = (size->cx / 26 + 1) / 2;
1554}
1555
1556/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001557 * Get the character size of a font.
1558 */
1559 static void
1560GetFontSize(GuiFont font)
1561{
1562 HWND hwnd = GetDesktopWindow();
1563 HDC hdc = GetWindowDC(hwnd);
1564 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001565 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566 TEXTMETRIC tm;
1567
1568 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001569 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001570
K.Takataabe628e2022-01-23 16:25:17 +00001571 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001572 gui.char_height = tm.tmHeight + p_linespace;
1573
1574 SelectFont(hdc, hfntOld);
1575
1576 ReleaseDC(hwnd, hdc);
1577}
1578
1579/*
1580 * Adjust gui.char_height (after 'linespace' was changed).
1581 */
1582 int
1583gui_mch_adjust_charheight(void)
1584{
1585 GetFontSize(gui.norm_font);
1586 return OK;
1587}
1588
1589 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001590get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001591{
1592 HFONT font = NULL;
1593
Bram Moolenaar734a8672019-12-02 22:49:38 +01001594 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001595 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596
1597 if (font == NULL)
1598 return NOFONT;
1599
1600 return (GuiFont)font;
1601}
1602
1603 static int
1604pixels_to_points(int pixels, int vertical)
1605{
1606 int points;
1607 HWND hwnd;
1608 HDC hdc;
1609
1610 hwnd = GetDesktopWindow();
1611 hdc = GetWindowDC(hwnd);
1612
1613 points = MulDiv(pixels, 72,
1614 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1615
1616 ReleaseDC(hwnd, hdc);
1617
1618 return points;
1619}
1620
1621 GuiFont
1622gui_mch_get_font(
1623 char_u *name,
1624 int giveErrorIfMissing)
1625{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001626 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 GuiFont font = NOFONT;
1628
1629 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001630 {
1631 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001632 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001633 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001635 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636 return font;
1637}
1638
1639#if defined(FEAT_EVAL) || defined(PROTO)
1640/*
1641 * Return the name of font "font" in allocated memory.
1642 * Don't know how to get the actual name, thus use the provided name.
1643 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001645gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646{
1647 if (name == NULL)
1648 return NULL;
1649 return vim_strsave(name);
1650}
1651#endif
1652
1653 void
1654gui_mch_free_font(GuiFont font)
1655{
1656 if (font)
1657 DeleteObject((HFONT)font);
1658}
1659
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001660/*
1661 * Return the Pixel value (color) for the given color name.
1662 * Return INVALCOLOR for error.
1663 */
1664 guicolor_T
1665gui_mch_get_color(char_u *name)
1666{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001667 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668
1669 typedef struct SysColorTable
1670 {
1671 char *name;
1672 int color;
1673 } SysColorTable;
1674
1675 static SysColorTable sys_table[] =
1676 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001677 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1678 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001679#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1681#endif
1682 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1683 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1684 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1685 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1686 {"SYS_DESKTOP", COLOR_DESKTOP},
1687 {"SYS_INFOBK", COLOR_INFOBK},
1688 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1689 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001690 {"SYS_BTNFACE", COLOR_BTNFACE},
1691 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1692 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1693 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1694 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1695 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1696 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1697 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1698 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1699 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1700 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1701 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1702 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1703 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1704 {"SYS_MENU", COLOR_MENU},
1705 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1706 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1707 {"SYS_WINDOW", COLOR_WINDOW},
1708 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1709 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1710 };
1711
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001712 /*
1713 * Try to look up a system colour.
1714 */
K.Takataeeec2542021-06-02 13:28:16 +02001715 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716 if (STRICMP(name, sys_table[i].name) == 0)
1717 return GetSysColor(sys_table[i].color);
1718
Bram Moolenaarab302212016-04-26 20:59:29 +02001719 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001720}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001721
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001722 guicolor_T
1723gui_mch_get_rgb_color(int r, int g, int b)
1724{
1725 return gui_get_rgb_color_cmn(r, g, b);
1726}
1727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728/*
1729 * Return OK if the key with the termcap name "name" is supported.
1730 */
1731 int
1732gui_mch_haskey(char_u *name)
1733{
1734 int i;
1735
1736 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001737 if (name[0] == special_keys[i].vim_code0
1738 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001739 return OK;
1740 return FAIL;
1741}
1742
1743 void
1744gui_mch_beep(void)
1745{
LemonBoy77771d32022-04-13 11:47:25 +01001746 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001747}
1748/*
1749 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1750 */
1751 void
1752gui_mch_invert_rectangle(
1753 int r,
1754 int c,
1755 int nr,
1756 int nc)
1757{
1758 RECT rc;
1759
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001760#if defined(FEAT_DIRECTX)
1761 if (IS_ENABLE_DIRECTX())
1762 DWriteContext_Flush(s_dwc);
1763#endif
1764
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001765 /*
1766 * Note: InvertRect() excludes right and bottom of rectangle.
1767 */
1768 rc.left = FILL_X(c);
1769 rc.top = FILL_Y(r);
1770 rc.right = rc.left + nc * gui.char_width;
1771 rc.bottom = rc.top + nr * gui.char_height;
1772 InvertRect(s_hdc, &rc);
1773}
1774
1775/*
1776 * Iconify the GUI window.
1777 */
1778 void
1779gui_mch_iconify(void)
1780{
1781 ShowWindow(s_hwnd, SW_MINIMIZE);
1782}
1783
1784/*
1785 * Draw a cursor without focus.
1786 */
1787 void
1788gui_mch_draw_hollow_cursor(guicolor_T color)
1789{
1790 HBRUSH hbr;
1791 RECT rc;
1792
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001793#if defined(FEAT_DIRECTX)
1794 if (IS_ENABLE_DIRECTX())
1795 DWriteContext_Flush(s_dwc);
1796#endif
1797
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001798 /*
1799 * Note: FrameRect() excludes right and bottom of rectangle.
1800 */
1801 rc.left = FILL_X(gui.col);
1802 rc.top = FILL_Y(gui.row);
1803 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001804 if (mb_lefthalve(gui.row, gui.col))
1805 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001806 rc.bottom = rc.top + gui.char_height;
1807 hbr = CreateSolidBrush(color);
1808 FrameRect(s_hdc, &rc, hbr);
1809 DeleteBrush(hbr);
1810}
1811/*
1812 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1813 * color "color".
1814 */
1815 void
1816gui_mch_draw_part_cursor(
1817 int w,
1818 int h,
1819 guicolor_T color)
1820{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821 RECT rc;
1822
1823 /*
1824 * Note: FillRect() excludes right and bottom of rectangle.
1825 */
1826 rc.left =
1827#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001828 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001829 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1830#endif
1831 FILL_X(gui.col);
1832 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1833 rc.right = rc.left + w;
1834 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001835
Bram Moolenaar92467d32017-12-05 13:22:16 +01001836 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001837}
1838
1839
1840/*
1841 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1842 * dead key's nominal character and re-post the original message.
1843 */
1844 static void
1845outputDeadKey_rePost(MSG originalMsg)
1846{
1847 static MSG deadCharExpel;
1848
1849 if (!dead_key)
1850 return;
1851
1852 dead_key = 0;
1853
Bram Moolenaar734a8672019-12-02 22:49:38 +01001854 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001855 deadCharExpel.message = originalMsg.message;
1856 deadCharExpel.hwnd = originalMsg.hwnd;
1857 deadCharExpel.wParam = VK_SPACE;
1858
K.Takata4ac893f2022-01-20 12:44:28 +00001859 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001860
Bram Moolenaar734a8672019-12-02 22:49:38 +01001861 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1863 originalMsg.lParam);
1864}
1865
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001866/*
1867 * Process a single Windows message.
1868 * If one is not available we hang until one is.
1869 */
1870 static void
1871process_message(void)
1872{
1873 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001874 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001875 char_u string[40];
1876 int i;
1877 int modifiers = 0;
1878 int key;
1879#ifdef FEAT_MENU
1880 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1881#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001882 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001883
K.Takatab7057bd2022-01-21 11:37:07 +00001884 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885
1886#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001887 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 if (msg.message == WM_OLE)
1889 {
1890 char_u *str = (char_u *)msg.lParam;
1891 if (str == NULL || *str == NUL)
1892 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // Message can't be ours, forward it. Fixes problem with Ultramon
1894 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001895 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 }
1897 else
1898 {
1899 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001900 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001901 }
1902 return;
1903 }
1904#endif
1905
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001907 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001908 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001909 {
1910 HandleMouseHide(msg.message, msg.lParam);
1911 return;
1912 }
1913#endif
1914
1915 /*
1916 * Check if it's a special key that we recognise. If not, call
1917 * TranslateMessage().
1918 */
1919 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1920 {
1921 vk = (int) msg.wParam;
1922
1923 /*
1924 * Handle dead keys in special conditions in other cases we let Windows
1925 * handle them and do not interfere.
1926 *
1927 * The dead_key flag must be reset on several occasions:
1928 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1929 * consumed at that point (This is when we let Windows combine the
1930 * dead character on its own)
1931 *
1932 * - Before doing something special such as regenerating keypresses to
1933 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001934 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 * immediately to _OnChar() (or _OnSysChar()).
1936 */
1937 if (dead_key)
1938 {
1939 /*
1940 * If a dead key was pressed and the user presses VK_SPACE,
1941 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1942 * with the dead char now, so do nothing special and let Windows
1943 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01001944 *
1945 * Note that VK_SPACE combines with the dead_key's character and
1946 * only one WM_CHAR will be generated by TranslateMessage(), in
1947 * the two other cases two WM_CHAR will be generated: the dead
1948 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1949 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001950 */
1951 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1952 {
1953 dead_key = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001954 TranslateMessage(&msg);
1955 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001956 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001957 // In modes where we are not typing, dead keys should behave
1958 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01001959 else if ((get_real_state()
1960 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001961 {
1962 outputDeadKey_rePost(msg);
1963 return;
1964 }
1965 }
1966
Bram Moolenaar734a8672019-12-02 22:49:38 +01001967 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001968 if (vk == VK_CANCEL)
1969 {
1970 trash_input_buf();
1971 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001972 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001973 string[0] = Ctrl_C;
1974 add_to_input_buf(string, 1);
1975 }
1976
LemonBoy77fc0b02022-04-22 22:45:52 +01001977 // This is an IME event or a synthetic keystroke, let Windows handle it.
1978 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1979 {
1980 TranslateMessage(&msg);
1981 return;
1982 }
1983
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001984 for (i = 0; special_keys[i].key_sym != 0; i++)
1985 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001986 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001987 if (special_keys[i].key_sym == vk
LemonBoy202b4bd2022-04-28 19:50:54 +01001988 && (vk != VK_SPACE || !(GetKeyState(VK_LMENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001989 {
1990 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001991 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001992 * is a key that would normally trigger the dead key nominal
1993 * character output (such as a NUMPAD printable character or
1994 * the TAB key, etc...).
1995 */
1996 if (dead_key && (special_keys[i].vim_code0 == 'K'
1997 || vk == VK_TAB || vk == CAR))
1998 {
1999 outputDeadKey_rePost(msg);
2000 return;
2001 }
2002
2003#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002004 // Check for <F10>: Windows selects the menu. When <F10> is
2005 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002006 if (vk == VK_F10
2007 && gui.menu_is_active
2008 && check_map(k10, State, FALSE, TRUE, FALSE,
2009 NULL, NULL) == NULL)
2010 break;
2011#endif
LemonBoy45684c62022-04-24 15:46:42 +01002012 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002013
2014 if (special_keys[i].vim_code1 == NUL)
2015 key = special_keys[i].vim_code0;
2016 else
2017 key = TO_SPECIAL(special_keys[i].vim_code0,
2018 special_keys[i].vim_code1);
2019 key = simplify_key(key, &modifiers);
2020 if (key == CSI)
2021 key = K_CSI;
2022
2023 if (modifiers)
2024 {
2025 string[0] = CSI;
2026 string[1] = KS_MODIFIER;
2027 string[2] = modifiers;
2028 add_to_input_buf(string, 3);
2029 }
2030
2031 if (IS_SPECIAL(key))
2032 {
2033 string[0] = CSI;
2034 string[1] = K_SECOND(key);
2035 string[2] = K_THIRD(key);
2036 add_to_input_buf(string, 3);
2037 }
2038 else
2039 {
2040 int len;
2041
Bram Moolenaar734a8672019-12-02 22:49:38 +01002042 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002043 len = char_to_string(key, string, 40, FALSE);
2044 add_to_input_buf(string, len);
2045 }
2046 break;
2047 }
2048 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002049
2050 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 if (special_keys[i].key_sym == 0)
2052 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002053 WCHAR ch[8];
2054 int len;
2055 int i;
2056 UINT scan_code;
2057
LemonBoy77fc0b02022-04-22 22:45:52 +01002058 // Construct the state table with only a few modifiers, we don't
2059 // really care about the presence of Ctrl/Alt as those modifiers are
2060 // handled by Vim separately.
2061 memset(keyboard_state, 0, 256);
2062 if (GetKeyState(VK_SHIFT) & 0x8000)
2063 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002064 if (GetKeyState(VK_CAPITAL) & 0x0001)
2065 keyboard_state[VK_CAPITAL] = 0x01;
LemonBoy45684c62022-04-24 15:46:42 +01002066 // Alt-Gr is synthesized as Alt + Ctrl.
LemonBoy202b4bd2022-04-28 19:50:54 +01002067 if ((GetKeyState(VK_RMENU) & 0x8000)
2068 && (GetKeyState(VK_CONTROL) & 0x8000))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002070 keyboard_state[VK_MENU] = 0x80;
2071 keyboard_state[VK_CONTROL] = 0x80;
2072 }
2073
2074 // Translate the virtual key according to the current keyboard
2075 // layout.
2076 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2077 // Convert the scan-code into a sequence of zero or more unicode
2078 // codepoints.
2079 // If this is a dead key ToUnicode returns a negative value.
2080 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2081 0);
2082 dead_key = len < 0;
2083
2084 if (len <= 0)
2085 return;
2086
2087 // Post the message as TranslateMessage would do.
2088 if (msg.message == WM_KEYDOWN)
2089 {
2090 for (i = 0; i < len; i++)
2091 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002092 }
2093 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002094 {
2095 for (i = 0; i < len; i++)
2096 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2097 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002098 }
2099 }
2100#ifdef FEAT_MBYTE_IME
2101 else if (msg.message == WM_IME_NOTIFY)
2102 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2103 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002104 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002105 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106#endif
2107
2108#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002109 // Check for <F10>: Default effect is to select the menu. When <F10> is
2110 // mapped we need to stop it here to avoid strange effects (e.g., for the
2111 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2113 NULL, NULL) == NULL)
2114#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002115 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116}
2117
2118/*
2119 * Catch up with any queued events. This may put keyboard input into the
2120 * input buffer, call resize call-backs, trigger timers etc. If there is
2121 * nothing in the event queue (& no timers pending), then we return
2122 * immediately.
2123 */
2124 void
2125gui_mch_update(void)
2126{
2127 MSG msg;
2128
2129 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002130 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002131 && !vim_is_input_buf_full())
2132 process_message();
2133}
2134
Bram Moolenaar4231da42016-06-02 14:30:04 +02002135 static void
2136remove_any_timer(void)
2137{
2138 MSG msg;
2139
2140 if (s_wait_timer != 0 && !s_timed_out)
2141 {
2142 KillTimer(NULL, s_wait_timer);
2143
Bram Moolenaar734a8672019-12-02 22:49:38 +01002144 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002145 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002146 ;
2147 s_wait_timer = 0;
2148 }
2149}
2150
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002151/*
2152 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2153 * from the keyboard.
2154 * wtime == -1 Wait forever.
2155 * wtime == 0 This should never happen.
2156 * wtime > 0 Wait wtime milliseconds for a character.
2157 * Returns OK if a character was found to be available within the given time,
2158 * or FAIL otherwise.
2159 */
2160 int
2161gui_mch_wait_for_chars(int wtime)
2162{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002163 int focus;
2164
2165 s_timed_out = FALSE;
2166
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002167 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002168 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002169 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002170 if (s_busy_processing)
2171 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002172
2173 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002174 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2175 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002176 }
2177
2178 allow_scrollbar = TRUE;
2179
2180 focus = gui.in_focus;
2181 while (!s_timed_out)
2182 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002183 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002184 if (gui.in_focus != focus)
2185 {
2186 if (gui.in_focus)
2187 gui_mch_start_blink();
2188 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002189 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002190 focus = gui.in_focus;
2191 }
2192
2193 if (s_need_activate)
2194 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002196 s_need_activate = FALSE;
2197 }
2198
Bram Moolenaar4231da42016-06-02 14:30:04 +02002199#ifdef FEAT_TIMERS
2200 did_add_timer = FALSE;
2201#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002202#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002203 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002204 for (;;)
2205 {
2206 MSG msg;
2207
2208 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002209# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002210 if (did_add_timer)
2211 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002212# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002213 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002214 {
2215 process_message();
2216 break;
2217 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002218 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002219 // TODO: The 10 msec is a compromise between laggy response
2220 // and consuming more CPU time. Better would be to handle
2221 // channel messages when they arrive.
2222 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002223 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002224 break;
2225 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002226#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002227 // Don't use gui_mch_update() because then we will spin-lock until a
2228 // char arrives, instead we use GetMessage() to hang until an
2229 // event arrives. No need to check for input_buf_full because we are
2230 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002231 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002232#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233
2234 if (input_available())
2235 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002236 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002237 allow_scrollbar = FALSE;
2238
Bram Moolenaar89c00032019-09-04 13:53:21 +02002239 // Clear pending mouse button, the release event may have been
2240 // taken by the dialog window. But don't do this when getting
2241 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002242 if (!s_getting_focus)
2243 s_button_pending = -1;
2244
2245 return OK;
2246 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002247
2248#ifdef FEAT_TIMERS
2249 if (did_add_timer)
2250 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002251 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002252 remove_any_timer();
2253 break;
2254 }
2255#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002256 }
2257 allow_scrollbar = FALSE;
2258 return FAIL;
2259}
2260
2261/*
2262 * Clear a rectangular region of the screen from text pos (row1, col1) to
2263 * (row2, col2) inclusive.
2264 */
2265 void
2266gui_mch_clear_block(
2267 int row1,
2268 int col1,
2269 int row2,
2270 int col2)
2271{
2272 RECT rc;
2273
2274 /*
2275 * Clear one extra pixel at the far right, for when bold characters have
2276 * spilled over to the window border.
2277 * Note: FillRect() excludes right and bottom of rectangle.
2278 */
2279 rc.left = FILL_X(col1);
2280 rc.top = FILL_Y(row1);
2281 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2282 rc.bottom = FILL_Y(row2 + 1);
2283 clear_rect(&rc);
2284}
2285
2286/*
2287 * Clear the whole text window.
2288 */
2289 void
2290gui_mch_clear_all(void)
2291{
2292 RECT rc;
2293
2294 rc.left = 0;
2295 rc.top = 0;
2296 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2297 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2298 clear_rect(&rc);
2299}
2300/*
2301 * Menu stuff.
2302 */
2303
2304 void
2305gui_mch_enable_menu(int flag)
2306{
2307#ifdef FEAT_MENU
2308 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2309#endif
2310}
2311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312 void
2313gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002314 int x UNUSED,
2315 int y UNUSED,
2316 int w UNUSED,
2317 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002318{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002319 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320}
2321
2322#if defined(FEAT_MENU) || defined(PROTO)
2323/*
2324 * Make menu item hidden or not hidden
2325 */
2326 void
2327gui_mch_menu_hidden(
2328 vimmenu_T *menu,
2329 int hidden)
2330{
2331 /*
2332 * This doesn't do what we want. Hmm, just grey the menu items for now.
2333 */
2334 /*
2335 if (hidden)
2336 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2337 else
2338 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2339 */
2340 gui_mch_menu_grey(menu, hidden);
2341}
2342
2343/*
2344 * This is called after setting all the menus to grey/hidden or not.
2345 */
2346 void
2347gui_mch_draw_menubar(void)
2348{
2349 DrawMenuBar(s_hwnd);
2350}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002351#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002353/*
2354 * Return the RGB value of a pixel as a long.
2355 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002356 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357gui_mch_get_rgb(guicolor_T pixel)
2358{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002359 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2360 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361}
2362
2363#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002364/*
2365 * Convert pixels in X to dialog units
2366 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 static WORD
2368PixelToDialogX(int numPixels)
2369{
2370 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2371}
2372
Bram Moolenaar734a8672019-12-02 22:49:38 +01002373/*
2374 * Convert pixels in Y to dialog units
2375 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 static WORD
2377PixelToDialogY(int numPixels)
2378{
2379 return (WORD)((numPixels * 8) / s_dlgfntheight);
2380}
2381
Bram Moolenaar734a8672019-12-02 22:49:38 +01002382/*
2383 * Return the width in pixels of the given text in the given DC.
2384 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002385 static int
2386GetTextWidth(HDC hdc, char_u *str, int len)
2387{
2388 SIZE size;
2389
2390 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2391 return size.cx;
2392}
2393
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394/*
2395 * Return the width in pixels of the given text in the given DC, taking care
2396 * of 'encoding' to active codepage conversion.
2397 */
2398 static int
2399GetTextWidthEnc(HDC hdc, char_u *str, int len)
2400{
2401 SIZE size;
2402 WCHAR *wstr;
2403 int n;
2404 int wlen = len;
2405
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002406 wstr = enc_to_utf16(str, &wlen);
2407 if (wstr == NULL)
2408 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002409
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002410 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2411 vim_free(wstr);
2412 if (n)
2413 return size.cx;
2414 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002416
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002417static void get_work_area(RECT *spi_rect);
2418
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419/*
2420 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002421 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2422 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423 */
2424 static BOOL
2425CenterWindow(
2426 HWND hwndChild,
2427 HWND hwndParent)
2428{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002429 HMONITOR mon;
2430 MONITORINFO moninfo;
2431 RECT rChild, rParent, rScreen;
2432 int wChild, hChild, wParent, hParent;
2433 int xNew, yNew;
2434 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002435
2436 GetWindowRect(hwndChild, &rChild);
2437 wChild = rChild.right - rChild.left;
2438 hChild = rChild.bottom - rChild.top;
2439
Bram Moolenaar734a8672019-12-02 22:49:38 +01002440 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002441 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002442 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443 else
2444 GetWindowRect(hwndParent, &rParent);
2445 wParent = rParent.right - rParent.left;
2446 hParent = rParent.bottom - rParent.top;
2447
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002448 moninfo.cbSize = sizeof(MONITORINFO);
2449 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2450 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002451 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002452 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002454 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002456 hdc = GetDC(hwndChild);
2457 rScreen.left = 0;
2458 rScreen.top = 0;
2459 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2460 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2461 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002462 }
2463
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002464 xNew = rParent.left + ((wParent - wChild) / 2);
2465 if (xNew < rScreen.left)
2466 xNew = rScreen.left;
2467 else if ((xNew + wChild) > rScreen.right)
2468 xNew = rScreen.right - wChild;
2469
2470 yNew = rParent.top + ((hParent - hChild) / 2);
2471 if (yNew < rScreen.top)
2472 yNew = rScreen.top;
2473 else if ((yNew + hChild) > rScreen.bottom)
2474 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475
2476 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2477 SWP_NOSIZE | SWP_NOZORDER);
2478}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002479#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002481#if defined(FEAT_TOOLBAR) || defined(PROTO)
2482 void
2483gui_mch_show_toolbar(int showit)
2484{
2485 if (s_toolbarhwnd == NULL)
2486 return;
2487
2488 if (showit)
2489 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002490 // Enable unicode support
2491 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2492 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002493 ShowWindow(s_toolbarhwnd, SW_SHOW);
2494 }
2495 else
2496 ShowWindow(s_toolbarhwnd, SW_HIDE);
2497}
2498
Bram Moolenaar734a8672019-12-02 22:49:38 +01002499// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002500# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002501
2502#endif
2503
2504#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2505 static void
2506add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2507{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002508 WCHAR *wn;
2509 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002510
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002511 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002512 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002513 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002515 infow.cbSize = sizeof(infow);
2516 infow.fMask = MIIM_TYPE | MIIM_ID;
2517 infow.wID = item_id;
2518 infow.fType = MFT_STRING;
2519 infow.dwTypeData = wn;
2520 infow.cch = (UINT)wcslen(wn);
2521 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2522 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002523}
2524
2525 static void
2526show_tabline_popup_menu(void)
2527{
2528 HMENU tab_pmenu;
2529 long rval;
2530 POINT pt;
2531
Bram Moolenaar734a8672019-12-02 22:49:38 +01002532 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002533 if (hold_gui_events
2534# ifdef FEAT_CMDWIN
2535 || cmdwin_type != 0
2536# endif
2537 )
2538 return;
2539
2540 tab_pmenu = CreatePopupMenu();
2541 if (tab_pmenu == NULL)
2542 return;
2543
2544 if (first_tabpage->tp_next != NULL)
2545 add_tabline_popup_menu_entry(tab_pmenu,
2546 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2547 add_tabline_popup_menu_entry(tab_pmenu,
2548 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2549 add_tabline_popup_menu_entry(tab_pmenu,
2550 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2551
2552 GetCursorPos(&pt);
2553 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2554 NULL);
2555
2556 DestroyMenu(tab_pmenu);
2557
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 if (rval > 0)
2560 {
2561 TCHITTESTINFO htinfo;
2562 int idx;
2563
2564 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2565 return;
2566
2567 htinfo.pt.x = pt.x;
2568 htinfo.pt.y = pt.y;
2569 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2570 if (idx == -1)
2571 idx = 0;
2572 else
2573 idx += 1;
2574
2575 send_tabline_menu_event(idx, (int)rval);
2576 }
2577}
2578
2579/*
2580 * Show or hide the tabline.
2581 */
2582 void
2583gui_mch_show_tabline(int showit)
2584{
2585 if (s_tabhwnd == NULL)
2586 return;
2587
2588 if (!showit != !showing_tabline)
2589 {
2590 if (showit)
2591 ShowWindow(s_tabhwnd, SW_SHOW);
2592 else
2593 ShowWindow(s_tabhwnd, SW_HIDE);
2594 showing_tabline = showit;
2595 }
2596}
2597
2598/*
2599 * Return TRUE when tabline is displayed.
2600 */
2601 int
2602gui_mch_showing_tabline(void)
2603{
2604 return s_tabhwnd != NULL && showing_tabline;
2605}
2606
2607/*
2608 * Update the labels of the tabline.
2609 */
2610 void
2611gui_mch_update_tabline(void)
2612{
2613 tabpage_T *tp;
2614 TCITEM tie;
2615 int nr = 0;
2616 int curtabidx = 0;
2617 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002618 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002619
2620 if (s_tabhwnd == NULL)
2621 return;
2622
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002623 // Enable unicode support
2624 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625
2626 tie.mask = TCIF_TEXT;
2627 tie.iImage = -1;
2628
Bram Moolenaar734a8672019-12-02 22:49:38 +01002629 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002630 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2631
Bram Moolenaar734a8672019-12-02 22:49:38 +01002632 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002633 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2634 {
2635 if (tp == curtab)
2636 curtabidx = nr;
2637
2638 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2639 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002640 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002641 tie.pszText = "-Empty-";
2642 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2643 tabadded = 1;
2644 }
2645
2646 get_tabline_label(tp, FALSE);
2647 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002648
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002649 wstr = enc_to_utf16(NameBuff, NULL);
2650 if (wstr != NULL)
2651 {
2652 TCITEMW tiw;
2653
2654 tiw.mask = TCIF_TEXT;
2655 tiw.iImage = -1;
2656 tiw.pszText = wstr;
2657 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2658 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002659 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002660 }
2661
Bram Moolenaar734a8672019-12-02 22:49:38 +01002662 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2664 TabCtrl_DeleteItem(s_tabhwnd, nr);
2665
2666 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2667 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2668
Bram Moolenaar734a8672019-12-02 22:49:38 +01002669 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2671 RedrawWindow(s_tabhwnd, NULL, NULL,
2672 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2673
2674 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2675 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2676}
2677
2678/*
2679 * Set the current tab to "nr". First tab is 1.
2680 */
2681 void
2682gui_mch_set_curtab(int nr)
2683{
2684 if (s_tabhwnd == NULL)
2685 return;
2686
2687 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2688 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2689}
2690
2691#endif
2692
2693/*
2694 * ":simalt" command.
2695 */
2696 void
2697ex_simalt(exarg_T *eap)
2698{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002699 char_u *keys = eap->arg;
2700 int fill_typebuf = FALSE;
2701 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702
2703 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2704 while (*keys)
2705 {
2706 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002707 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2709 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002710 fill_typebuf = TRUE;
2711 }
2712 if (fill_typebuf)
2713 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002714 // Put a NOP in the typeahead buffer so that the message will get
2715 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002716 key_name[0] = K_SPECIAL;
2717 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002718 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002719 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002720#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002721 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002722#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002723 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724 }
2725}
2726
2727/*
2728 * Create the find & replace dialogs.
2729 * You can't have both at once: ":find" when replace is showing, destroys
2730 * the replace dialog first, and the other way around.
2731 */
2732#ifdef MSWIN_FIND_REPLACE
2733 static void
2734initialise_findrep(char_u *initial_string)
2735{
2736 int wword = FALSE;
2737 int mcase = !p_ic;
2738 char_u *entry_text;
2739
Bram Moolenaar734a8672019-12-02 22:49:38 +01002740 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002741 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2742
2743 s_findrep_struct.hwndOwner = s_hwnd;
2744 s_findrep_struct.Flags = FR_DOWN;
2745 if (mcase)
2746 s_findrep_struct.Flags |= FR_MATCHCASE;
2747 if (wword)
2748 s_findrep_struct.Flags |= FR_WHOLEWORD;
2749 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002750 {
2751 WCHAR *p = enc_to_utf16(entry_text, NULL);
2752 if (p != NULL)
2753 {
2754 int len = s_findrep_struct.wFindWhatLen - 1;
2755
2756 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2757 s_findrep_struct.lpstrFindWhat[len] = NUL;
2758 vim_free(p);
2759 }
2760 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 vim_free(entry_text);
2762}
2763#endif
2764
2765 static void
2766set_window_title(HWND hwnd, char *title)
2767{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002768 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002769 {
2770 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002771
Bram Moolenaar734a8672019-12-02 22:49:38 +01002772 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2774 if (wbuf != NULL)
2775 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002776 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002778 }
2779 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002780 else
2781 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782}
2783
2784 void
2785gui_mch_find_dialog(exarg_T *eap)
2786{
2787#ifdef MSWIN_FIND_REPLACE
2788 if (s_findrep_msg != 0)
2789 {
2790 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2791 DestroyWindow(s_findrep_hwnd);
2792
2793 if (!IsWindow(s_findrep_hwnd))
2794 {
2795 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002796 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002797 }
2798
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002799 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002800 (void)SetFocus(s_findrep_hwnd);
2801
2802 s_findrep_is_find = TRUE;
2803 }
2804#endif
2805}
2806
2807
2808 void
2809gui_mch_replace_dialog(exarg_T *eap)
2810{
2811#ifdef MSWIN_FIND_REPLACE
2812 if (s_findrep_msg != 0)
2813 {
2814 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2815 DestroyWindow(s_findrep_hwnd);
2816
2817 if (!IsWindow(s_findrep_hwnd))
2818 {
2819 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002820 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002821 }
2822
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002823 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 (void)SetFocus(s_findrep_hwnd);
2825
2826 s_findrep_is_find = FALSE;
2827 }
2828#endif
2829}
2830
2831
2832/*
2833 * Set visibility of the pointer.
2834 */
2835 void
2836gui_mch_mousehide(int hide)
2837{
2838 if (hide != gui.pointer_hidden)
2839 {
2840 ShowCursor(!hide);
2841 gui.pointer_hidden = hide;
2842 }
2843}
2844
2845#ifdef FEAT_MENU
2846 static void
2847gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2848{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002849 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850 gui_mch_mousehide(FALSE);
2851
2852 (void)TrackPopupMenu(
2853 (HMENU)menu->submenu_id,
2854 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2855 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002856 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002857 s_hwnd,
2858 NULL);
2859 /*
2860 * NOTE: The pop-up menu can eat the mouse up event.
2861 * We deal with this in normal.c.
2862 */
2863}
2864#endif
2865
2866/*
2867 * Got a message when the system will go down.
2868 */
2869 static void
2870_OnEndSession(void)
2871{
2872 getout_preserve_modified(1);
2873}
2874
2875/*
2876 * Get this message when the user clicks on the cross in the top right corner
2877 * of a Windows95 window.
2878 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002880_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881{
2882 gui_shell_closed();
2883}
2884
2885/*
2886 * Get a message when the window is being destroyed.
2887 */
2888 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002889_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002890{
2891 if (!destroying)
2892 _OnClose(hwnd);
2893}
2894
2895 static void
2896_OnPaint(
2897 HWND hwnd)
2898{
2899 if (!IsMinimized(hwnd))
2900 {
2901 PAINTSTRUCT ps;
2902
Bram Moolenaar734a8672019-12-02 22:49:38 +01002903 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905
Bram Moolenaar734a8672019-12-02 22:49:38 +01002906 // prevent multi-byte characters from misprinting on an invalid
2907 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002908 if (has_mbyte)
2909 {
2910 RECT rect;
2911
2912 GetClientRect(hwnd, &rect);
2913 ps.rcPaint.left = rect.left;
2914 ps.rcPaint.right = rect.right;
2915 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002916
2917 if (!IsRectEmpty(&ps.rcPaint))
2918 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2920 ps.rcPaint.right - ps.rcPaint.left + 1,
2921 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2922 }
2923
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924 EndPaint(hwnd, &ps);
2925 }
2926}
2927
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928 static void
2929_OnSize(
2930 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002931 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932 int cx,
2933 int cy)
2934{
K.Takatac81e9bf2022-01-16 14:15:49 +00002935 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936 {
2937 gui_resize_shell(cx, cy);
2938
Bram Moolenaar734a8672019-12-02 22:49:38 +01002939 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941 }
2942}
2943
2944 static void
2945_OnSetFocus(
2946 HWND hwnd,
2947 HWND hwndOldFocus)
2948{
2949 gui_focus_change(TRUE);
2950 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002951 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002952}
2953
2954 static void
2955_OnKillFocus(
2956 HWND hwnd,
2957 HWND hwndNewFocus)
2958{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002959 if (destroying)
2960 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002961 gui_focus_change(FALSE);
2962 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002963 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964}
2965
2966/*
2967 * Get a message when the user switches back to vim
2968 */
2969 static LRESULT
2970_OnActivateApp(
2971 HWND hwnd,
2972 BOOL fActivate,
2973 DWORD dwThreadId)
2974{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002975 // we call gui_focus_change() in _OnSetFocus()
2976 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002977 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002978}
2979
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980 void
2981gui_mch_destroy_scrollbar(scrollbar_T *sb)
2982{
2983 DestroyWindow(sb->id);
2984}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985
2986/*
2987 * Get current mouse coordinates in text window.
2988 */
2989 void
2990gui_mch_getmouse(int *x, int *y)
2991{
2992 RECT rct;
2993 POINT mp;
2994
2995 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002996 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002997 *x = (int)(mp.x - rct.left);
2998 *y = (int)(mp.y - rct.top);
2999}
3000
3001/*
3002 * Move mouse pointer to character at (x, y).
3003 */
3004 void
3005gui_mch_setmouse(int x, int y)
3006{
3007 RECT rct;
3008
3009 (void)GetWindowRect(s_textArea, &rct);
3010 (void)SetCursorPos(x + gui.border_offset + rct.left,
3011 y + gui.border_offset + rct.top);
3012}
3013
3014 static void
3015gui_mswin_get_valid_dimensions(
3016 int w,
3017 int h,
3018 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003019 int *valid_h,
3020 int *cols,
3021 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003022{
3023 int base_width, base_height;
3024
3025 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003026 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3027 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003028 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003029 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3030 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3031 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3032 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003033 *cols = (w - base_width) / gui.char_width;
3034 *rows = (h - base_height) / gui.char_height;
3035 *valid_w = base_width + *cols * gui.char_width;
3036 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003037}
3038
3039 void
3040gui_mch_flash(int msec)
3041{
3042 RECT rc;
3043
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003044#if defined(FEAT_DIRECTX)
3045 if (IS_ENABLE_DIRECTX())
3046 DWriteContext_Flush(s_dwc);
3047#endif
3048
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003049 /*
3050 * Note: InvertRect() excludes right and bottom of rectangle.
3051 */
3052 rc.left = 0;
3053 rc.top = 0;
3054 rc.right = gui.num_cols * gui.char_width;
3055 rc.bottom = gui.num_rows * gui.char_height;
3056 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003057 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003058
Bram Moolenaar734a8672019-12-02 22:49:38 +01003059 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060
3061 InvertRect(s_hdc, &rc);
3062}
3063
3064/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003065 * Check if the specified point is on-screen. (multi-monitor aware)
3066 */
3067 static BOOL
3068is_point_onscreen(int x, int y)
3069{
3070 POINT pt = {x, y};
3071
3072 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3073}
3074
3075/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003076 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003077 *
3078 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3079 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3080 * only works when the whole window is on-screen.
3081 */
3082 static BOOL
3083is_window_onscreen(HWND hwnd)
3084{
3085 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003086 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003087
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003088 GetClientRect(hwnd, &rc);
3089 p1.x = rc.left;
3090 p1.y = rc.top;
3091 p2.x = rc.right - 1;
3092 p2.y = rc.bottom - 1;
3093 ClientToScreen(hwnd, &p1);
3094 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003095
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003096 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003097 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003098 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003099 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003100 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003101 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003102 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003103 return FALSE;
3104 return TRUE;
3105}
3106
3107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003108 * Return flags used for scrolling.
3109 * The SW_INVALIDATE is required when part of the window is covered or
3110 * off-screen. Refer to MS KB Q75236.
3111 */
3112 static int
3113get_scroll_flags(void)
3114{
3115 HWND hwnd;
3116 RECT rcVim, rcOther, rcDest;
3117
Bram Moolenaar185577e2020-10-29 20:08:21 +01003118 // Check if the window is (partly) off-screen.
3119 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003120 return SW_INVALIDATE;
3121
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003122 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003123 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3125 if (IsWindowVisible(hwnd))
3126 {
3127 GetWindowRect(hwnd, &rcOther);
3128 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3129 return SW_INVALIDATE;
3130 }
3131 return 0;
3132}
3133
3134/*
3135 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3136 * may not be scrolled out properly.
3137 * For gVim, when _OnScroll() is repeated, the character at the
3138 * previous cursor position may be left drawn after scroll.
3139 * The problem can be avoided by calling GetPixel() to get a pixel in
3140 * the region before ScrollWindowEx().
3141 */
3142 static void
3143intel_gpu_workaround(void)
3144{
3145 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3146}
3147
3148/*
3149 * Delete the given number of lines from the given row, scrolling up any
3150 * text further down within the scroll region.
3151 */
3152 void
3153gui_mch_delete_lines(
3154 int row,
3155 int num_lines)
3156{
3157 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003158
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003159 rc.left = FILL_X(gui.scroll_region_left);
3160 rc.right = FILL_X(gui.scroll_region_right + 1);
3161 rc.top = FILL_Y(row);
3162 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3163
Bram Moolenaar92467d32017-12-05 13:22:16 +01003164#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003165 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003167 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003169 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170#endif
3171 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003172#if defined(FEAT_DIRECTX)
3173 if (IS_ENABLE_DIRECTX())
3174 DWriteContext_Flush(s_dwc);
3175#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 intel_gpu_workaround();
3177 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003179 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003180 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003181
Bram Moolenaar734a8672019-12-02 22:49:38 +01003182 // This seems to be required to avoid the cursor disappearing when
3183 // scrolling such that the cursor ends up in the top-left character on
3184 // the screen... But why? (Webb)
3185 // It's probably fixed by disabling drawing the cursor while scrolling.
3186 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187
3188 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3189 gui.scroll_region_left,
3190 gui.scroll_region_bot, gui.scroll_region_right);
3191}
3192
3193/*
3194 * Insert the given number of lines before the given row, scrolling down any
3195 * following text within the scroll region.
3196 */
3197 void
3198gui_mch_insert_lines(
3199 int row,
3200 int num_lines)
3201{
3202 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003203
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 rc.left = FILL_X(gui.scroll_region_left);
3205 rc.right = FILL_X(gui.scroll_region_right + 1);
3206 rc.top = FILL_Y(row);
3207 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208
3209#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003210 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003211 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003212 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003213 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003214 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003215#endif
3216 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003217#if defined(FEAT_DIRECTX)
3218 if (IS_ENABLE_DIRECTX())
3219 DWriteContext_Flush(s_dwc);
3220#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003221 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003222 // The SW_INVALIDATE is required when part of the window is covered or
3223 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003224 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003226 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003227 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229 gui_clear_block(row, gui.scroll_region_left,
3230 row + num_lines - 1, gui.scroll_region_right);
3231}
3232
3233
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003235gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236{
3237#if defined(FEAT_DIRECTX)
3238 DWriteContext_Close(s_dwc);
3239 DWrite_Final();
3240 s_dwc = NULL;
3241#endif
3242
3243 ReleaseDC(s_textArea, s_hdc);
3244 DeleteObject(s_brush);
3245
3246#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003247 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3249#endif
3250
Bram Moolenaar734a8672019-12-02 22:49:38 +01003251 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252 if (s_hwnd != NULL)
3253 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003254 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255 DestroyWindow(s_hwnd);
3256 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257}
3258
3259 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003260logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261{
3262 char *p;
3263 char *res;
3264 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003265 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003266 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003267 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003269 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3270 if (font_name == NULL)
3271 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003273 quality_name = quality_id2name((int)lf.lfQuality);
3274
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003275 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003276 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003277 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278 if (res != NULL)
3279 {
3280 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003281 // make a normal font string out of the lf thing:
3282 points = pixels_to_points(
3283 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3284 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3285 sprintf((char *)p, "%s:h%d", font_name, points);
3286 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003287 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003288 while (*p)
3289 {
3290 if (*p == ' ')
3291 *p = '_';
3292 ++p;
3293 }
3294 if (lf.lfItalic)
3295 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003296 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003297 STRCAT(p, ":b");
3298 if (lf.lfUnderline)
3299 STRCAT(p, ":u");
3300 if (lf.lfStrikeOut)
3301 STRCAT(p, ":s");
3302 if (charset_name != NULL)
3303 {
3304 STRCAT(p, ":c");
3305 STRCAT(p, charset_name);
3306 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003307 if (quality_name != NULL)
3308 {
3309 STRCAT(p, ":q");
3310 STRCAT(p, quality_name);
3311 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003312 }
3313
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003314 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003315 return (char_u *)res;
3316}
3317
3318
3319#ifdef FEAT_MBYTE_IME
3320/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003321 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003322 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003323 */
3324 static void
3325update_im_font(void)
3326{
K.Takatac81e9bf2022-01-16 14:15:49 +00003327 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328
3329 if (p_guifontwide != NULL && *p_guifontwide != NUL
3330 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003331 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 norm_logfont = lf_wide;
3333 else
3334 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003335
3336 lf = norm_logfont;
3337 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3338 // Work around when PerMonitorV2 is not enabled in the process level.
3339 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3340 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341}
3342#endif
3343
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344/*
3345 * Handler of gui.wide_font (p_guifontwide) changed notification.
3346 */
3347 void
3348gui_mch_wide_font_changed(void)
3349{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003350 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003352#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003354#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355
3356 gui_mch_free_font(gui.wide_ital_font);
3357 gui.wide_ital_font = NOFONT;
3358 gui_mch_free_font(gui.wide_bold_font);
3359 gui.wide_bold_font = NOFONT;
3360 gui_mch_free_font(gui.wide_boldital_font);
3361 gui.wide_boldital_font = NOFONT;
3362
3363 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003364 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003365 {
3366 if (!lf.lfItalic)
3367 {
3368 lf.lfItalic = TRUE;
3369 gui.wide_ital_font = get_font_handle(&lf);
3370 lf.lfItalic = FALSE;
3371 }
3372 if (lf.lfWeight < FW_BOLD)
3373 {
3374 lf.lfWeight = FW_BOLD;
3375 gui.wide_bold_font = get_font_handle(&lf);
3376 if (!lf.lfItalic)
3377 {
3378 lf.lfItalic = TRUE;
3379 gui.wide_boldital_font = get_font_handle(&lf);
3380 }
3381 }
3382 }
3383}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384
3385/*
3386 * Initialise vim to use the font with the given name.
3387 * Return FAIL if the font could not be loaded, OK otherwise.
3388 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003390gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391{
K.Takatac81e9bf2022-01-16 14:15:49 +00003392 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393 GuiFont font = NOFONT;
3394 char_u *p;
3395
Bram Moolenaar734a8672019-12-02 22:49:38 +01003396 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003398 {
3399 lfOrig = lf;
3400 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003402 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 if (font == NOFONT)
3404 return FAIL;
3405
3406 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003407 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003408#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003409 norm_logfont = lf;
3410 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003411 if (!s_in_dpichanged)
3412 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413#endif
3414 gui_mch_free_font(gui.norm_font);
3415 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003416 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417 GetFontSize(font);
3418
K.Takatac81e9bf2022-01-16 14:15:49 +00003419 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 if (p != NULL)
3421 {
3422 hl_set_font_name(p);
3423
Bram Moolenaar734a8672019-12-02 22:49:38 +01003424 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003425 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3426 {
3427 vim_free(p_guifont);
3428 p_guifont = p;
3429 }
3430 else
3431 vim_free(p);
3432 }
3433
3434 gui_mch_free_font(gui.ital_font);
3435 gui.ital_font = NOFONT;
3436 gui_mch_free_font(gui.bold_font);
3437 gui.bold_font = NOFONT;
3438 gui_mch_free_font(gui.boldital_font);
3439 gui.boldital_font = NOFONT;
3440
3441 if (!lf.lfItalic)
3442 {
3443 lf.lfItalic = TRUE;
3444 gui.ital_font = get_font_handle(&lf);
3445 lf.lfItalic = FALSE;
3446 }
3447 if (lf.lfWeight < FW_BOLD)
3448 {
3449 lf.lfWeight = FW_BOLD;
3450 gui.bold_font = get_font_handle(&lf);
3451 if (!lf.lfItalic)
3452 {
3453 lf.lfItalic = TRUE;
3454 gui.boldital_font = get_font_handle(&lf);
3455 }
3456 }
3457
3458 return OK;
3459}
3460
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461/*
3462 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003463 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 */
3465 int
3466gui_mch_maximized(void)
3467{
3468 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003469 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470
3471 wp.length = sizeof(WINDOWPLACEMENT);
3472 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003473 {
3474 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003476 && wp.flags == WPF_RESTORETOMAXIMIZED))
3477 return TRUE;
3478 if (wp.showCmd == SW_SHOWMINIMIZED)
3479 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003481 // Assume the window is snapped when the sizes from two APIs differ.
3482 GetWindowRect(s_hwnd, &rc);
3483 if ((rc.right - rc.left !=
3484 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3485 || (rc.bottom - rc.top !=
3486 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3487 return TRUE;
3488 }
3489 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003490}
3491
3492/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003493 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3494 * is set. Compute the new Rows and Columns. This is like resizing the
3495 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003496 */
3497 void
3498gui_mch_newfont(void)
3499{
3500 RECT rect;
3501
3502 GetWindowRect(s_hwnd, &rect);
3503 if (win_socket_id == 0)
3504 {
3505 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003506 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3507 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003509 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3510 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3511 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3512 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 }
3514 else
3515 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003516 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003517 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003518 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519 }
3520}
3521
3522/*
3523 * Set the window title
3524 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003525 void
3526gui_mch_settitle(
3527 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003528 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003529{
3530 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3531}
3532
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003533#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003534// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3535// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003536static LPCSTR mshape_idcs[] =
3537{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003538 IDC_ARROW, // arrow
3539 MAKEINTRESOURCE(0), // blank
3540 IDC_IBEAM, // beam
3541 IDC_SIZENS, // updown
3542 IDC_SIZENS, // udsizing
3543 IDC_SIZEWE, // leftright
3544 IDC_SIZEWE, // lrsizing
3545 IDC_WAIT, // busy
3546 IDC_NO, // no
3547 IDC_ARROW, // crosshair
3548 IDC_ARROW, // hand1
3549 IDC_ARROW, // hand2
3550 IDC_ARROW, // pencil
3551 IDC_ARROW, // question
3552 IDC_ARROW, // right-arrow
3553 IDC_UPARROW, // up-arrow
3554 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555};
3556
3557 void
3558mch_set_mouse_shape(int shape)
3559{
3560 LPCSTR idc;
3561
3562 if (shape == MSHAPE_HIDE)
3563 ShowCursor(FALSE);
3564 else
3565 {
3566 if (shape >= MSHAPE_NUMBERED)
3567 idc = IDC_ARROW;
3568 else
3569 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003570 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571 if (!p_mh)
3572 {
3573 POINT mp;
3574
Bram Moolenaar734a8672019-12-02 22:49:38 +01003575 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003576 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577 (void)SetCursorPos(mp.x, mp.y);
3578 ShowCursor(TRUE);
3579 }
3580 }
3581}
3582#endif
3583
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003584#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003585/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003586 * Wide version of convert_filter().
3587 */
3588 static WCHAR *
3589convert_filterW(char_u *s)
3590{
3591 char_u *tmp;
3592 int len;
3593 WCHAR *res;
3594
3595 tmp = convert_filter(s);
3596 if (tmp == NULL)
3597 return NULL;
3598 len = (int)STRLEN(s) + 3;
3599 res = enc_to_utf16(tmp, &len);
3600 vim_free(tmp);
3601 return res;
3602}
3603
3604/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003605 * Pop open a file browser and return the file selected, in allocated memory,
3606 * or NULL if Cancel is hit.
3607 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3608 * title - Title message for the file browser dialog.
3609 * dflt - Default name of file.
3610 * ext - Default extension to be added to files without extensions.
3611 * initdir - directory in which to open the browser (NULL = current dir)
3612 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003613 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003614 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003615gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 int saving,
3617 char_u *title,
3618 char_u *dflt,
3619 char_u *ext,
3620 char_u *initdir,
3621 char_u *filter)
3622{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003623 // We always use the wide function. This means enc_to_utf16() must work,
3624 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003625 OPENFILENAMEW fileStruct;
3626 WCHAR fileBuf[MAXPATHL];
3627 WCHAR *wp;
3628 int i;
3629 WCHAR *titlep = NULL;
3630 WCHAR *extp = NULL;
3631 WCHAR *initdirp = NULL;
3632 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003633 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003634 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003635
3636 if (dflt == NULL)
3637 fileBuf[0] = NUL;
3638 else
3639 {
3640 wp = enc_to_utf16(dflt, NULL);
3641 if (wp == NULL)
3642 fileBuf[0] = NUL;
3643 else
3644 {
3645 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3646 fileBuf[i] = wp[i];
3647 fileBuf[i] = NUL;
3648 vim_free(wp);
3649 }
3650 }
3651
Bram Moolenaar734a8672019-12-02 22:49:38 +01003652 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 filterp = convert_filterW(filter);
3654
Bram Moolenaara80faa82020-04-12 19:37:17 +02003655 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003656# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003658 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003659# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003661# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663 if (title != NULL)
3664 titlep = enc_to_utf16(title, NULL);
3665 fileStruct.lpstrTitle = titlep;
3666
3667 if (ext != NULL)
3668 extp = enc_to_utf16(ext, NULL);
3669 fileStruct.lpstrDefExt = extp;
3670
3671 fileStruct.lpstrFile = fileBuf;
3672 fileStruct.nMaxFile = MAXPATHL;
3673 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003674 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3675 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003676 if (initdir != NULL && *initdir != NUL)
3677 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003678 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679 initdirp = enc_to_utf16(initdir, NULL);
3680 if (initdirp != NULL)
3681 {
3682 for (wp = initdirp; *wp != NUL; ++wp)
3683 if (*wp == '/')
3684 *wp = '\\';
3685 }
3686 fileStruct.lpstrInitialDir = initdirp;
3687 }
3688
3689 /*
3690 * TODO: Allow selection of multiple files. Needs another arg to this
3691 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3692 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3693 * files that don't exist yet, so I haven't put it in. What about
3694 * OFN_PATHMUSTEXIST?
3695 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3696 */
3697 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003698# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003699 if (curbuf->b_p_bin)
3700 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003701# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003703 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003705 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706
3707 vim_free(filterp);
3708 vim_free(initdirp);
3709 vim_free(titlep);
3710 vim_free(extp);
3711
K.Takata14b8d6a2022-01-20 15:05:22 +00003712 if (!ret)
3713 return NULL;
3714
3715 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003717 if (p == NULL)
3718 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003719
Bram Moolenaar734a8672019-12-02 22:49:38 +01003720 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003721 SetFocus(s_hwnd);
3722
Bram Moolenaar734a8672019-12-02 22:49:38 +01003723 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003724 q = vim_strsave(shorten_fname1(p));
3725 vim_free(p);
3726 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003727}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728
3729
3730/*
3731 * Convert the string s to the proper format for a filter string by replacing
3732 * the \t and \n delimiters with \0.
3733 * Returns the converted string in allocated memory.
3734 *
3735 * Keep in sync with convert_filterW() above!
3736 */
3737 static char_u *
3738convert_filter(char_u *s)
3739{
3740 char_u *res;
3741 unsigned s_len = (unsigned)STRLEN(s);
3742 unsigned i;
3743
3744 res = alloc(s_len + 3);
3745 if (res != NULL)
3746 {
3747 for (i = 0; i < s_len; ++i)
3748 if (s[i] == '\t' || s[i] == '\n')
3749 res[i] = '\0';
3750 else
3751 res[i] = s[i];
3752 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003753 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003754 res[s_len + 1] = NUL;
3755 res[s_len + 2] = NUL;
3756 }
3757 return res;
3758}
3759
3760/*
3761 * Select a directory.
3762 */
3763 char_u *
3764gui_mch_browsedir(char_u *title, char_u *initdir)
3765{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003766 // We fake this: Use a filter that doesn't select anything and a default
3767 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3769 initdir, (char_u *)_("Directory\t*.nothing\n"));
3770}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003771#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 static void
3774_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003775 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 HDROP hDrop)
3777{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003778#define BUFPATHLEN _MAX_PATH
3779#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 char szFile[BUFPATHLEN];
3782 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3783 UINT i;
3784 char_u **fnames;
3785 POINT pt;
3786 int_u modifiers = 0;
3787
Bram Moolenaar734a8672019-12-02 22:49:38 +01003788 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789 DragQueryPoint(hDrop, &pt);
3790 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3791
3792 reset_VIsual();
3793
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003794 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795
3796 if (fnames != NULL)
3797 for (i = 0; i < cFiles; ++i)
3798 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3800 fnames[i] = utf16_to_enc(wszFile, NULL);
3801 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 {
3803 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3804 fnames[i] = vim_strsave((char_u *)szFile);
3805 }
3806 }
3807
3808 DragFinish(hDrop);
3809
3810 if (fnames != NULL)
3811 {
LemonBoy45684c62022-04-24 15:46:42 +01003812 int kbd_modifiers = get_active_modifiers();
3813
3814 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003816 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003818 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 modifiers |= MOUSE_ALT;
3820
3821 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3822
3823 s_need_activate = TRUE;
3824 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825}
3826
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003827 static int
3828_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003829 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830 HWND hwndCtl,
3831 UINT code,
3832 int pos)
3833{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003834 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 scrollbar_T *sb, *sb_info;
3836 long val;
3837 int dragging = FALSE;
3838 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 SCROLLINFO si;
3840
3841 si.cbSize = sizeof(si);
3842 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843
3844 sb = gui_mswin_find_scrollbar(hwndCtl);
3845 if (sb == NULL)
3846 return 0;
3847
Bram Moolenaar734a8672019-12-02 22:49:38 +01003848 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003849 {
3850 /*
3851 * Careful: need to get scrollbar info out of first (left) scrollbar
3852 * for window, but keep real scrollbar too because we must pass it to
3853 * gui_drag_scrollbar().
3854 */
3855 sb_info = &sb->wp->w_scrollbars[0];
3856 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003857 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 sb_info = sb;
3859 val = sb_info->value;
3860
3861 switch (code)
3862 {
3863 case SB_THUMBTRACK:
3864 val = pos;
3865 dragging = TRUE;
3866 if (sb->scroll_shift > 0)
3867 val <<= sb->scroll_shift;
3868 break;
3869 case SB_LINEDOWN:
3870 val++;
3871 break;
3872 case SB_LINEUP:
3873 val--;
3874 break;
3875 case SB_PAGEDOWN:
3876 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3877 break;
3878 case SB_PAGEUP:
3879 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3880 break;
3881 case SB_TOP:
3882 val = 0;
3883 break;
3884 case SB_BOTTOM:
3885 val = sb_info->max;
3886 break;
3887 case SB_ENDSCROLL:
3888 if (prev_code == SB_THUMBTRACK)
3889 {
3890 /*
3891 * "pos" only gives us 16-bit data. In case of large file,
3892 * use GetScrollPos() which returns 32-bit. Unfortunately it
3893 * is not valid while the scrollbar is being dragged.
3894 */
3895 val = GetScrollPos(hwndCtl, SB_CTL);
3896 if (sb->scroll_shift > 0)
3897 val <<= sb->scroll_shift;
3898 }
3899 break;
3900
3901 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003902 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003903 return 0;
3904 }
3905 prev_code = code;
3906
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3908 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909
3910 /*
3911 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3912 */
3913 if (sb->wp != NULL)
3914 {
3915 scrollbar_T *sba = sb->wp->w_scrollbars;
3916 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3917
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919 }
3920
Bram Moolenaar734a8672019-12-02 22:49:38 +01003921 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 s_busy_processing = TRUE;
3923
Bram Moolenaar734a8672019-12-02 22:49:38 +01003924 // When "allow_scrollbar" is FALSE still need to remember the new
3925 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003926 dont_scroll = !allow_scrollbar;
3927
Bram Moolenaara338adc2018-01-31 20:51:47 +01003928 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003930 mch_enable_flush();
3931 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003932
3933 s_busy_processing = FALSE;
3934 dont_scroll = dont_scroll_save;
3935
3936 return 0;
3937}
3938
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#ifdef FEAT_XPM_W32
3941# include "xpm_w32.h"
3942#endif
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar734a8672019-12-02 22:49:38 +01003945// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#define TEAROFF_PADDING_X 2
3947#define TEAROFF_BUTTON_PAD_X 8
3948#define TEAROFF_MIN_WIDTH 200
3949#define TEAROFF_SUBMENU_LABEL ">>"
3950#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3951
3952
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003953#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954# define ID_BEVAL_TOOLTIP 200
3955# define BEVAL_TEXT_LEN MAXPATHL
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003958static UINT_PTR beval_timer_id = 0;
3959static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003960#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003961
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003962
Bram Moolenaar734a8672019-12-02 22:49:38 +01003963// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
3965#ifdef FEAT_MENU
3966static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969/*
3970 * Use the system font for dialogs and tear-off menus. Remove this line to
3971 * use DLG_FONT_NAME.
3972 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003973#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#define VIM_CLASSW L"Vim"
3977
Bram Moolenaar734a8672019-12-02 22:49:38 +01003978// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3979// thus there should be room for every dialog. For tearoffs it's made bigger
3980// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#define DLG_ALLOC_SIZE 16 * 1024
3982
3983/*
3984 * stuff for dialogs, menus, tearoffs etc.
3985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986static PWORD
3987add_dialog_element(
3988 PWORD p,
3989 DWORD lStyle,
3990 WORD x,
3991 WORD y,
3992 WORD w,
3993 WORD h,
3994 WORD Id,
3995 WORD clss,
3996 const char *caption);
3997static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003998static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003999#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002static void get_dialog_font_metrics(void);
4003
4004static int dialog_default_button = -1;
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#ifdef FEAT_TOOLBAR
4007static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004008static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004009static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004011#else
4012# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#endif
4014
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004015#ifdef FEAT_GUI_TABLINE
4016static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004017static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004018#endif
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_MBYTE_IME
4021static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4022static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4023#endif
4024#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4025# ifdef NOIME
4026typedef struct tagCOMPOSITIONFORM {
4027 DWORD dwStyle;
4028 POINT ptCurrentPos;
4029 RECT rcArea;
4030} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4031typedef HANDLE HIMC;
4032# endif
4033
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004034static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004035static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4036static HIMC (WINAPI *pImmGetContext)(HWND);
4037static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4038static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4039static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4040static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004041static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4042static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004043static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4044static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004045static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046static void dyn_imm_load(void);
4047#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048# define pImmGetCompositionStringW ImmGetCompositionStringW
4049# define pImmGetContext ImmGetContext
4050# define pImmAssociateContext ImmAssociateContext
4051# define pImmReleaseContext ImmReleaseContext
4052# define pImmGetOpenStatus ImmGetOpenStatus
4053# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004054# define pImmGetCompositionFontW ImmGetCompositionFontW
4055# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056# define pImmSetCompositionWindow ImmSetCompositionWindow
4057# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004058# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#endif
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#ifdef FEAT_MENU
4062/*
4063 * Figure out how high the menu bar is at the moment.
4064 */
4065 static int
4066gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004067 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 static int old_menu_height = -1;
4070
4071 RECT rc1, rc2;
4072 int num;
4073 int menu_height;
4074
4075 if (gui.menu_is_active)
4076 num = GetMenuItemCount(s_menuBar);
4077 else
4078 num = 0;
4079
4080 if (num == 0)
4081 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004082 else if (IsMinimized(s_hwnd))
4083 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004084 // The height of the menu cannot be determined while the window is
4085 // minimized. Take the previous height if the menu is changed in that
4086 // state, to avoid that Vim's vertical window size accidentally
4087 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004088 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004092 /*
4093 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4094 * seem to have been set yet, so menu wraps in default window
4095 * width which is very narrow. Instead just return height of a
4096 * single menu item. Will still be wrong when the menu really
4097 * should wrap over more than one line.
4098 */
4099 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4100 if (gui.starting)
4101 menu_height = rc1.bottom - rc1.top + 1;
4102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004104 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4105 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 }
4108
4109 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004110 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004111 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 return menu_height;
4114}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004115#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
4117
4118/*
4119 * Setup for the Intellimouse
4120 */
LemonBoyc27747e2022-05-07 12:25:40 +01004121 static long
4122mouse_vertical_scroll_step(void)
4123{
4124 UINT val;
4125 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4126 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4127 return 3; // Safe default;
4128}
4129
4130 static long
4131mouse_horizontal_scroll_step(void)
4132{
4133 UINT val;
4134 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4135 return (long)val;
4136 return 3; // Safe default;
4137}
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 static void
4140init_mouse_wheel(void)
4141{
LemonBoyc27747e2022-05-07 12:25:40 +01004142 // Get the default values for the horizontal and vertical scroll steps from
4143 // the system.
4144 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4145 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146}
4147
Bram Moolenaarae20f342019-11-05 21:09:23 +01004148/*
4149 * Intellimouse wheel handler.
4150 * Treat a mouse wheel event as if it were a scroll request.
4151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 static void
LemonBoy365d8f72022-05-05 19:23:07 +01004153_OnMouseWheel(HWND hwnd, short zDelta, LPARAM param, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
LemonBoy365d8f72022-05-05 19:23:07 +01004155 int button;
4156 win_T *wp;
4157 int modifiers, kbd_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
Bram Moolenaarae20f342019-11-05 21:09:23 +01004159 wp = gui_mouse_window(FIND_POPUP);
4160
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004161#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004162 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004163 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004164 cmdarg_T cap;
4165 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004166
Bram Moolenaarae20f342019-11-05 21:09:23 +01004167 // Mouse hovers over popup window, scroll it if possible.
4168 mouse_row = wp->w_winrow;
4169 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004170 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004171 if (horizontal)
4172 {
4173 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4174 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4175 }
4176 else
4177 {
4178 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4179 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4180 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004181 clear_oparg(&oa);
4182 cap.oap = &oa;
4183 nv_mousescroll(&cap);
4184 update_screen(0);
4185 setcursor();
4186 out_flush();
4187 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004188 }
4189#endif
4190
Bram Moolenaarae20f342019-11-05 21:09:23 +01004191 if (wp == NULL || !p_scf)
4192 wp = curwin;
4193
LemonBoy365d8f72022-05-05 19:23:07 +01004194 // Translate the scroll event into an event that Vim can process so that
4195 // the user has a chance to map the scrollwheel buttons.
4196 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004197 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 else
LemonBoy365d8f72022-05-05 19:23:07 +01004199 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004200
4201 kbd_modifiers = get_active_modifiers();
4202
4203 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4204 modifiers |= MOUSE_SHIFT;
4205 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4206 modifiers |= MOUSE_CTRL;
4207 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4208 modifiers |= MOUSE_ALT;
4209
4210 mch_disable_flush();
LemonBoyc27747e2022-05-07 12:25:40 +01004211 gui_send_mouse_event(button, GET_X_LPARAM(param), GET_Y_LPARAM(param),
LemonBoy365d8f72022-05-05 19:23:07 +01004212 FALSE, kbd_modifiers);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004213 mch_enable_flush();
4214 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215}
4216
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004217#ifdef USE_SYSMENU_FONT
4218/*
4219 * Get Menu Font.
4220 * Return OK or FAIL.
4221 */
4222 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004223gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004224{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004225 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004226
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004227 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4228 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004229 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004230 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004231 &nm,
4232 0))
4233 return FAIL;
4234 *lf = nm.lfMenuFont;
4235 return OK;
4236}
4237#endif
4238
4239
4240#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4241/*
4242 * Set the GUI tabline font to the system menu font
4243 */
4244 static void
4245set_tabline_font(void)
4246{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004247 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004248 HFONT font;
4249 HWND hwnd;
4250 HDC hdc;
4251 HFONT hfntOld;
4252 TEXTMETRIC tm;
4253
4254 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4255 return;
4256
K.Takatac81e9bf2022-01-16 14:15:49 +00004257 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004258 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004259
4260 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4261
4262 /*
4263 * Compute the height of the font used for the tab text
4264 */
4265 hwnd = GetDesktopWindow();
4266 hdc = GetWindowDC(hwnd);
4267 hfntOld = SelectFont(hdc, font);
4268
4269 GetTextMetrics(hdc, &tm);
4270
4271 SelectFont(hdc, hfntOld);
4272 ReleaseDC(hwnd, hdc);
4273
4274 /*
4275 * The space used by the tab border and the space between the tab label
4276 * and the tab border is included as 7.
4277 */
4278 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4279}
K.Takatac81e9bf2022-01-16 14:15:49 +00004280#else
4281# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004282#endif
4283
Bram Moolenaar520470a2005-06-16 21:59:56 +00004284/*
4285 * Invoked when a setting was changed.
4286 */
4287 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004288_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004289{
LemonBoy365d8f72022-05-05 19:23:07 +01004290 switch (param)
4291 {
4292 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004293 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004294 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004295 case SPI_SETWHEELSCROLLCHARS:
4296 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004297 break;
4298 case SPI_SETNONCLIENTMETRICS:
4299 set_tabline_font();
4300 break;
4301 default:
4302 break;
4303 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004304 return 0;
4305}
4306
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#ifdef FEAT_NETBEANS_INTG
4308 static void
4309_OnWindowPosChanged(
4310 HWND hwnd,
4311 const LPWINDOWPOS lpwpos)
4312{
4313 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004314 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4317 || lpwpos->cx != cx || lpwpos->cy != cy))
4318 {
4319 x = lpwpos->x;
4320 y = lpwpos->y;
4321 cx = lpwpos->cx;
4322 cy = lpwpos->cy;
4323 netbeans_frame_moved(x, y);
4324 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004325 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004326 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327}
4328#endif
4329
K.Takata4f2417f2021-06-05 16:25:32 +02004330
4331static HWND hwndTip = NULL;
4332
4333 static void
4334show_sizing_tip(int cols, int rows)
4335{
4336 TOOLINFOA ti = {sizeof(ti)};
4337 char buf[32];
4338
4339 ti.hwnd = s_hwnd;
4340 ti.uId = (UINT_PTR)s_hwnd;
4341 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4342 ti.lpszText = buf;
4343 sprintf(buf, "%dx%d", cols, rows);
4344 if (hwndTip == NULL)
4345 {
4346 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4347 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4348 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4349 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4350 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4351 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4352 }
4353 else
4354 {
4355 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4356 }
4357 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4358}
4359
4360 static void
4361destroy_sizing_tip(void)
4362{
4363 if (hwndTip != NULL)
4364 {
4365 DestroyWindow(hwndTip);
4366 hwndTip = NULL;
4367 }
4368}
4369
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 static int
4371_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 UINT fwSide,
4373 LPRECT lprc)
4374{
4375 int w, h;
4376 int valid_w, valid_h;
4377 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004378 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 w = lprc->right - lprc->left;
4381 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004382 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 w_offset = w - valid_w;
4384 h_offset = h - valid_h;
4385
4386 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4387 || fwSide == WMSZ_BOTTOMLEFT)
4388 lprc->left += w_offset;
4389 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4390 || fwSide == WMSZ_BOTTOMRIGHT)
4391 lprc->right -= w_offset;
4392
4393 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4394 || fwSide == WMSZ_TOPRIGHT)
4395 lprc->top += h_offset;
4396 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4397 || fwSide == WMSZ_BOTTOMRIGHT)
4398 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004399
4400 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 return TRUE;
4402}
4403
K.Takata92000e22022-01-20 15:10:57 +00004404#ifdef FEAT_GUI_TABLINE
4405 static void
4406_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4407{
4408 if (gui_mch_showing_tabline())
4409 {
4410 POINT pt;
4411 RECT rect;
4412
4413 /*
4414 * If the cursor is on the tabline, display the tab menu
4415 */
4416 GetCursorPos(&pt);
4417 GetWindowRect(s_textArea, &rect);
4418 if (pt.y < rect.top)
4419 {
4420 show_tabline_popup_menu();
4421 return;
4422 }
4423 }
4424 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4425}
4426
4427 static void
4428_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4429{
4430 /*
4431 * If the user double clicked the tabline, create a new tab
4432 */
4433 if (gui_mch_showing_tabline())
4434 {
4435 POINT pt;
4436 RECT rect;
4437
4438 GetCursorPos(&pt);
4439 GetWindowRect(s_textArea, &rect);
4440 if (pt.y < rect.top)
4441 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4442 }
4443 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4444}
4445#endif
4446
4447 static UINT
4448_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4449{
4450 UINT result;
4451 int x, y;
4452
4453 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4454 if (result != HTCLIENT)
4455 return result;
4456
4457#ifdef FEAT_GUI_TABLINE
4458 if (gui_mch_showing_tabline())
4459 {
4460 RECT rct;
4461
4462 // If the cursor is on the GUI tabline, don't process this event
4463 GetWindowRect(s_textArea, &rct);
4464 if (yPos < rct.top)
4465 return result;
4466 }
4467#endif
4468 (void)gui_mch_get_winpos(&x, &y);
4469 xPos -= x;
4470
4471 if (xPos < 48) // <VN> TODO should use system metric?
4472 return HTBOTTOMLEFT;
4473 else
4474 return HTBOTTOMRIGHT;
4475}
4476
4477#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4478 static LRESULT
4479_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4480{
4481 switch (hdr->code)
4482 {
4483 case TTN_GETDISPINFOW:
4484 case TTN_GETDISPINFO:
4485 {
4486 char_u *str = NULL;
4487 static void *tt_text = NULL;
4488
4489 VIM_CLEAR(tt_text);
4490
4491# ifdef FEAT_GUI_TABLINE
4492 if (gui_mch_showing_tabline()
4493 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4494 {
4495 POINT pt;
4496 /*
4497 * Mouse is over the GUI tabline. Display the
4498 * tooltip for the tab under the cursor
4499 *
4500 * Get the cursor position within the tab control
4501 */
4502 GetCursorPos(&pt);
4503 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4504 {
4505 TCHITTESTINFO htinfo;
4506 int idx;
4507
4508 /*
4509 * Get the tab under the cursor
4510 */
4511 htinfo.pt.x = pt.x;
4512 htinfo.pt.y = pt.y;
4513 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4514 if (idx != -1)
4515 {
4516 tabpage_T *tp;
4517
4518 tp = find_tabpage(idx + 1);
4519 if (tp != NULL)
4520 {
4521 get_tabline_label(tp, TRUE);
4522 str = NameBuff;
4523 }
4524 }
4525 }
4526 }
4527# endif
4528# ifdef FEAT_TOOLBAR
4529# ifdef FEAT_GUI_TABLINE
4530 else
4531# endif
4532 {
4533 UINT idButton;
4534 vimmenu_T *pMenu;
4535
4536 idButton = (UINT) hdr->idFrom;
4537 pMenu = gui_mswin_find_menu(root_menu, idButton);
4538 if (pMenu)
4539 str = pMenu->strings[MENU_INDEX_TIP];
4540 }
4541# endif
4542 if (str == NULL)
4543 break;
4544
4545 // Set the maximum width, this also enables using \n for
4546 // line break.
4547 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4548
4549 if (hdr->code == TTN_GETDISPINFOW)
4550 {
4551 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4552
4553 tt_text = enc_to_utf16(str, NULL);
4554 lpdi->lpszText = tt_text;
4555 // can't show tooltip if failed
4556 }
4557 else
4558 {
4559 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4560
4561 if (STRLEN(str) < sizeof(lpdi->szText)
4562 || ((tt_text = vim_strsave(str)) == NULL))
4563 vim_strncpy((char_u *)lpdi->szText, str,
4564 sizeof(lpdi->szText) - 1);
4565 else
4566 lpdi->lpszText = tt_text;
4567 }
4568 }
4569 break;
4570
4571# ifdef FEAT_GUI_TABLINE
4572 case TCN_SELCHANGE:
4573 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4574 {
4575 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4576 return 0L;
4577 }
4578 break;
4579
4580 case NM_RCLICK:
4581 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4582 {
4583 show_tabline_popup_menu();
4584 return 0L;
4585 }
4586 break;
4587# endif
4588
4589 default:
4590 break;
4591 }
4592 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4593}
4594#endif
4595
4596#if defined(MENUHINTS) && defined(FEAT_MENU)
4597 static LRESULT
4598_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4599{
4600 if (((UINT) HIWORD(wParam)
4601 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4602 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004603 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004604 {
4605 UINT idButton;
4606 vimmenu_T *pMenu;
4607 static int did_menu_tip = FALSE;
4608
4609 if (did_menu_tip)
4610 {
4611 msg_clr_cmdline();
4612 setcursor();
4613 out_flush();
4614 did_menu_tip = FALSE;
4615 }
4616
4617 idButton = (UINT)LOWORD(wParam);
4618 pMenu = gui_mswin_find_menu(root_menu, idButton);
4619 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4620 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4621 {
4622 ++msg_hist_off;
4623 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4624 --msg_hist_off;
4625 setcursor();
4626 out_flush();
4627 did_menu_tip = TRUE;
4628 }
4629 return 0L;
4630 }
4631 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4632}
4633#endif
4634
K.Takatac81e9bf2022-01-16 14:15:49 +00004635 static LRESULT
4636_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4637{
4638 s_dpi = ydpi;
4639 s_in_dpichanged = TRUE;
4640 //TRACE("DPI: %d", ydpi);
4641
4642 update_scrollbar_size();
4643 update_toolbar_size();
4644 set_tabline_font();
4645
4646 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4647 gui_get_wide_font();
4648 gui_mswin_get_menu_height(FALSE);
4649#ifdef FEAT_MBYTE_IME
4650 im_set_position(gui.row, gui.col);
4651#endif
4652 InvalidateRect(hwnd, NULL, TRUE);
4653
4654 s_in_dpichanged = FALSE;
4655 return 0L;
4656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658
4659 static LRESULT CALLBACK
4660_WndProc(
4661 HWND hwnd,
4662 UINT uMsg,
4663 WPARAM wParam,
4664 LPARAM lParam)
4665{
LemonBoy77fc0b02022-04-22 22:45:52 +01004666 // TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4667 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 HandleMouseHide(uMsg, lParam);
4670
4671 s_uMsg = uMsg;
4672 s_wParam = wParam;
4673 s_lParam = lParam;
4674
4675 switch (uMsg)
4676 {
4677 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4678 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004679 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004681 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4683 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4684 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4685 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4686#ifdef FEAT_MENU
4687 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4688#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004689 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4690 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4692 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004693 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4694 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4696 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4697 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4698#ifdef FEAT_NETBEANS_INTG
4699 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4700#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004701#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004702 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4703 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004704#endif
K.Takata92000e22022-01-20 15:10:57 +00004705 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004706
Bram Moolenaar734a8672019-12-02 22:49:38 +01004707 case WM_QUERYENDSESSION: // System wants to go down.
4708 gui_shell_closed(); // Will exit when no changed buffers.
4709 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004712 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004715 return 0L;
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 break;
4718
4719 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004720 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4721 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004722 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return 0L;
4724
4725 case WM_SYSCHAR:
4726 /*
4727 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4728 * shortcut key, handle like a typed ALT key, otherwise call Windows
4729 * ALT key handling.
4730 */
4731#ifdef FEAT_MENU
4732 if ( !gui.menu_is_active
4733 || p_wak[0] == 'n'
4734 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4735 )
4736#endif
4737 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004738 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return 0L;
4740 }
4741#ifdef FEAT_MENU
4742 else
K.Takata4ac893f2022-01-20 12:44:28 +00004743 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744#endif
4745
4746 case WM_SYSKEYUP:
4747#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004748 // This used to be done only when menu is active: ALT key is used for
4749 // that. But that caused problems when menu is disabled and using
4750 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4751 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004752 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004754 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#endif
4756
K.Takata4f2417f2021-06-05 16:25:32 +02004757 case WM_EXITSIZEMOVE:
4758 destroy_sizing_tip();
4759 break;
4760
Bram Moolenaar734a8672019-12-02 22:49:38 +01004761 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004765 case WM_MOUSEHWHEEL:
4766 _OnMouseWheel(hwnd, HIWORD(wParam), lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004767 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
Bram Moolenaar734a8672019-12-02 22:49:38 +01004769 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004770 case WM_SETTINGCHANGE:
4771 return _OnSettingChange((UINT)wParam);
4772
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004773#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004775 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776#endif
K.Takata92000e22022-01-20 15:10:57 +00004777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778#if defined(MENUHINTS) && defined(FEAT_MENU)
4779 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004780 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782
4783#ifdef FEAT_MBYTE_IME
4784 case WM_IME_NOTIFY:
4785 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004786 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004787 return 1L;
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 case WM_IME_COMPOSITION:
4790 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004791 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004792 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004794 case WM_DPICHANGED:
4795 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4796 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004800 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802#endif
K.Takata92000e22022-01-20 15:10:57 +00004803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805
K.Takata4ac893f2022-01-20 12:44:28 +00004806 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807}
4808
4809/*
4810 * End of call-back routines
4811 */
4812
Bram Moolenaar734a8672019-12-02 22:49:38 +01004813// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814HWND vim_parent_hwnd = NULL;
4815
4816 static BOOL CALLBACK
4817FindWindowTitle(HWND hwnd, LPARAM lParam)
4818{
4819 char buf[2048];
4820 char *title = (char *)lParam;
4821
4822 if (GetWindowText(hwnd, buf, sizeof(buf)))
4823 {
4824 if (strstr(buf, title) != NULL)
4825 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004826 // Found it. Store the window ref. and quit searching if MDI
4827 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004829 if (vim_parent_hwnd != NULL)
4830 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004833 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834}
4835
4836/*
4837 * Invoked for '-P "title"' argument: search for parent application to open
4838 * our window in.
4839 */
4840 void
4841gui_mch_set_parent(char *title)
4842{
4843 EnumWindows(FindWindowTitle, (LPARAM)title);
4844 if (vim_parent_hwnd == NULL)
4845 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004846 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 mch_exit(2);
4848 }
4849}
4850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 static void
4853ole_error(char *arg)
4854{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004855 char buf[IOSIZE];
4856
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004857# ifdef VIMDLL
4858 gui.in_use = mch_is_gui_executable();
4859# endif
4860
Bram Moolenaar734a8672019-12-02 22:49:38 +01004861 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004862 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004863 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004864 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004868#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4869 static char *
4870gvim_error(void)
4871{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004872 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004873
4874 if (starting)
4875 {
4876 mch_errmsg(msg);
4877 mch_errmsg("\n");
4878 mch_exit(2);
4879 }
4880 return msg;
4881}
4882
4883 char *
4884gui_mch_do_spawn(char_u *arg)
4885{
4886 int len;
4887# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4888 char_u *session = NULL;
4889 LPWSTR tofree1 = NULL;
4890# endif
4891 WCHAR name[MAX_PATH];
4892 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4893 STARTUPINFOW si = {sizeof(si)};
4894 PROCESS_INFORMATION pi;
4895
4896 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4897 goto error;
4898 p = wcsrchr(name, L'\\');
4899 if (p == NULL)
4900 goto error;
4901 // Replace the executable name from vim(d).exe to gvim(d).exe.
4902# ifdef DEBUG
4903 wcscpy(p + 1, L"gvimd.exe");
4904# else
4905 wcscpy(p + 1, L"gvim.exe");
4906# endif
4907
4908# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4909 if (starting)
4910# endif
4911 {
4912 // Pass the command line to the new process.
4913 p = GetCommandLineW();
4914 // Skip 1st argument.
4915 while (*p && *p != L' ' && *p != L'\t')
4916 {
4917 if (*p == L'"')
4918 {
4919 while (*p && *p != L'"')
4920 ++p;
4921 if (*p)
4922 ++p;
4923 }
4924 else
4925 ++p;
4926 }
4927 cmd = p;
4928 }
4929# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4930 else
4931 {
4932 // Create a session file and pass it to the new process.
4933 LPWSTR wsession;
4934 char_u *savebg;
4935 int ret;
4936
4937 session = vim_tempname('s', FALSE);
4938 if (session == NULL)
4939 goto error;
4940 savebg = p_bg;
4941 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4942 ret = write_session_file(session);
4943 vim_free(p_bg);
4944 p_bg = savebg;
4945 if (!ret)
4946 goto error;
4947 wsession = enc_to_utf16(session, NULL);
4948 if (wsession == NULL)
4949 goto error;
4950 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004951 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004952 if (cmd == NULL)
4953 {
4954 vim_free(wsession);
4955 goto error;
4956 }
4957 tofree1 = cmd;
4958 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4959 wsession, wsession);
4960 vim_free(wsession);
4961 }
4962# endif
4963
4964 // Check additional arguments to the `:gui` command.
4965 if (arg != NULL)
4966 {
4967 warg = enc_to_utf16(arg, NULL);
4968 if (warg == NULL)
4969 goto error;
4970 tofree2 = warg;
4971 }
4972 else
4973 warg = L"";
4974
4975 // Set up the new command line.
4976 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004977 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004978 if (newcmd == NULL)
4979 goto error;
4980 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4981
4982 // Spawn a new GUI process.
4983 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4984 NULL, NULL, &si, &pi))
4985 goto error;
4986 CloseHandle(pi.hProcess);
4987 CloseHandle(pi.hThread);
4988 mch_exit(0);
4989
4990error:
4991# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4992 if (session)
4993 mch_remove(session);
4994 vim_free(session);
4995 vim_free(tofree1);
4996# endif
4997 vim_free(newcmd);
4998 vim_free(tofree2);
4999 return gvim_error();
5000}
5001#endif
5002
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003/*
5004 * Parse the GUI related command-line arguments. Any arguments used are
5005 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005006 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
5008 void
5009gui_mch_prepare(int *argc, char **argv)
5010{
5011 int silent = FALSE;
5012 int idx;
5013
Bram Moolenaar734a8672019-12-02 22:49:38 +01005014 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5016 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005017 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5019 && (argv[2][0] == '-' || argv[2][0] == '/'))
5020 {
5021 silent = TRUE;
5022 idx = 2;
5023 }
5024 else
5025 idx = 1;
5026
Bram Moolenaar734a8672019-12-02 22:49:38 +01005027 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (STRICMP(argv[idx] + 1, "register") == 0)
5029 {
5030#ifdef FEAT_OLE
5031 RegisterMe(silent);
5032 mch_exit(0);
5033#else
5034 if (!silent)
5035 ole_error("register");
5036 mch_exit(2);
5037#endif
5038 }
5039
Bram Moolenaar734a8672019-12-02 22:49:38 +01005040 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5042 {
5043#ifdef FEAT_OLE
5044 UnregisterMe(!silent);
5045 mch_exit(0);
5046#else
5047 if (!silent)
5048 ole_error("unregister");
5049 mch_exit(2);
5050#endif
5051 }
5052
Bram Moolenaar734a8672019-12-02 22:49:38 +01005053 // Ignore an -embedding argument. It is only relevant if the
5054 // application wants to treat the case when it is started manually
5055 // differently from the case where it is started via automation (and
5056 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5058 {
5059#ifdef FEAT_OLE
5060 *argc = 1;
5061#else
5062 ole_error("embedding");
5063 mch_exit(2);
5064#endif
5065 }
5066 }
5067
5068#ifdef FEAT_OLE
5069 {
5070 int bDoRestart = FALSE;
5071
5072 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005073 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (bDoRestart)
5075 mch_exit(0);
5076 }
5077#endif
5078
5079#ifdef FEAT_NETBEANS_INTG
5080 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005081 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 int arg;
5083
5084 for (arg = 1; arg < *argc; arg++)
5085 if (strncmp("-nb", argv[arg], 3) == 0)
5086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 netbeansArg = argv[arg];
5088 mch_memmove(&argv[arg], &argv[arg + 1],
5089 (--*argc - arg) * sizeof(char *));
5090 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005091 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095}
5096
K.Takatac81e9bf2022-01-16 14:15:49 +00005097 static void
5098load_dpi_func(void)
5099{
5100 HMODULE hUser32;
5101
5102 hUser32 = GetModuleHandle("user32.dll");
5103 if (hUser32 == NULL)
5104 goto fail;
5105
5106 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5107 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5108 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5109 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5110 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5111 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5112
5113 if (pSetThreadDpiAwarenessContext != NULL)
5114 {
5115 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5116 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5117 if (oldctx != NULL)
5118 {
5119 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5120 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5121#ifdef DEBUG
5122 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5123 {
5124 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5125 }
5126#endif
5127 return;
5128 }
5129 }
5130
5131fail:
5132 // Disable PerMonitorV2 APIs.
5133 pGetDpiForSystem = stubGetDpiForSystem;
5134 pGetDpiForWindow = NULL;
5135 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5136 pSetThreadDpiAwarenessContext = NULL;
5137 pGetAwarenessFromDpiAwarenessContext = NULL;
5138}
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140/*
5141 * Initialise the GUI. Create all the windows, set up all the call-backs
5142 * etc.
5143 */
5144 int
5145gui_mch_init(void)
5146{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005148 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
Bram Moolenaar734a8672019-12-02 22:49:38 +01005151 // Return here if the window was already opened (happens when
5152 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005154 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 /*
5157 * Load the tearoff bitmap
5158 */
5159#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005160 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#endif
5162
K.Takatac81e9bf2022-01-16 14:15:49 +00005163 load_dpi_func();
5164
5165 s_dpi = pGetDpiForSystem();
5166 update_scrollbar_size();
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005169 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#endif
5171 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005172#ifdef FEAT_TOOLBAR
5173 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
5176 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5177
Bram Moolenaar734a8672019-12-02 22:49:38 +01005178 // First try using the wide version, so that we can use any title.
5179 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005180 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005182 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 wndclassw.lpfnWndProc = _WndProc;
5184 wndclassw.cbClsExtra = 0;
5185 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005186 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5188 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5189 wndclassw.hbrBackground = s_brush;
5190 wndclassw.lpszMenuName = NULL;
5191 wndclassw.lpszClassName = szVimWndClassW;
5192
K.Takata4ac893f2022-01-20 12:44:28 +00005193 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (vim_parent_hwnd != NULL)
5198 {
5199#ifdef HAVE_TRY_EXCEPT
5200 __try
5201 {
5202#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005203 // Open inside the specified parent window.
5204 // TODO: last argument should point to a CLIENTCREATESTRUCT
5205 // structure.
5206 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005208 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005209 WS_OVERLAPPEDWINDOW | WS_CHILD
5210 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5212 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005213 100, // Any value will do
5214 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005216 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#ifdef HAVE_TRY_EXCEPT
5218 }
5219 __except(EXCEPTION_EXECUTE_HANDLER)
5220 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005221 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223#endif
5224 if (s_hwnd == NULL)
5225 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005226 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 mch_exit(2);
5228 }
5229 }
5230 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005231 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005232 // If the provided windowid is not valid reset it to zero, so that it
5233 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005234 if (IsWindow((HWND)win_socket_id) <= 0)
5235 win_socket_id = 0;
5236
Bram Moolenaar734a8672019-12-02 22:49:38 +01005237 // Create a window. If win_socket_id is not zero without border and
5238 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005239 s_hwnd = CreateWindowW(
5240 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005241 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5242 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005243 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5244 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005245 100, // Any value will do
5246 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005247 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005248 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005249 if (s_hwnd != NULL && win_socket_id != 0)
5250 {
5251 SetParent(s_hwnd, (HWND)win_socket_id);
5252 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5253 }
5254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256 if (s_hwnd == NULL)
5257 return FAIL;
5258
K.Takatac81e9bf2022-01-16 14:15:49 +00005259 if (pGetDpiForWindow != NULL)
5260 {
5261 s_dpi = pGetDpiForWindow(s_hwnd);
5262 update_scrollbar_size();
5263 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5264 }
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5267 dyn_imm_load();
5268#endif
5269
Bram Moolenaar734a8672019-12-02 22:49:38 +01005270 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005271 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005272 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005273 wndclassw.style = CS_OWNDC;
5274 wndclassw.lpfnWndProc = _TextAreaWndProc;
5275 wndclassw.cbClsExtra = 0;
5276 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005277 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005278 wndclassw.hIcon = NULL;
5279 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5280 wndclassw.hbrBackground = NULL;
5281 wndclassw.lpszMenuName = NULL;
5282 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005283
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005284 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 return FAIL;
5286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005288 s_textArea = CreateWindowExW(
5289 0,
5290 szTextAreaClassW, L"Vim text area",
5291 WS_CHILD | WS_VISIBLE, 0, 0,
5292 100, // Any value will do for now
5293 100, // Any value will do for now
5294 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005295 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (s_textArea == NULL)
5298 return FAIL;
5299
Bram Moolenaar20321902016-02-17 12:30:17 +01005300#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005301 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005302 {
5303 HANDLE hIcon = NULL;
5304
5305 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005306 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005307 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005308#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310#ifdef FEAT_MENU
5311 s_menuBar = CreateMenu();
5312#endif
5313 s_hdc = GetDC(s_textArea);
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaar734a8672019-12-02 22:49:38 +01005317 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005318 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar734a8672019-12-02 22:49:38 +01005320 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 gui_mch_def_colors();
5322
Bram Moolenaar734a8672019-12-02 22:49:38 +01005323 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5324 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 set_normal_colors();
5326
5327 /*
5328 * Check that none of the colors are the same as the background color.
5329 * Then store the current values as the defaults.
5330 */
5331 gui_check_colors();
5332 gui.def_norm_pixel = gui.norm_pixel;
5333 gui.def_back_pixel = gui.back_pixel;
5334
Bram Moolenaar734a8672019-12-02 22:49:38 +01005335 // Get the colors for the highlight groups (gui_check_colors() might have
5336 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 highlight_gui_started();
5338
5339 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005340 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005342 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 /*
5345 * Set up for Intellimouse processing
5346 */
5347 init_mouse_wheel();
5348
5349 /*
5350 * compute a couple of metrics used for the dialogs
5351 */
5352 get_dialog_font_metrics();
5353#ifdef FEAT_TOOLBAR
5354 /*
5355 * Create the toolbar
5356 */
5357 initialise_toolbar();
5358#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005359#ifdef FEAT_GUI_TABLINE
5360 /*
5361 * Create the tabline
5362 */
5363 initialise_tabline();
5364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365#ifdef MSWIN_FIND_REPLACE
5366 /*
5367 * Initialise the dialog box stuff
5368 */
5369 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5370
Bram Moolenaar734a8672019-12-02 22:49:38 +01005371 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005373 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005375 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5377 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5378 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005381#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005382 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005383 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005384#endif
5385
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005386#ifdef FEAT_RENDER_OPTIONS
5387 if (p_rop)
5388 (void)gui_mch_set_rendering_options(p_rop);
5389#endif
5390
Bram Moolenaar748bf032005-02-02 23:04:36 +00005391theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005392 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005393 display_errors();
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 return OK;
5396}
5397
5398/*
5399 * Get the size of the screen, taking position on multiple monitors into
5400 * account (if supported).
5401 */
5402 static void
5403get_work_area(RECT *spi_rect)
5404{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005405 HMONITOR mon;
5406 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
Bram Moolenaar734a8672019-12-02 22:49:38 +01005408 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005409 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005410 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005412 moninfo.cbSize = sizeof(MONITORINFO);
5413 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005415 *spi_rect = moninfo.rcWork;
5416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005419 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5421}
5422
5423/*
5424 * Set the size of the window to the given width and height in pixels.
5425 */
5426 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005427gui_mch_set_shellsize(
5428 int width,
5429 int height,
5430 int min_width UNUSED,
5431 int min_height UNUSED,
5432 int base_width UNUSED,
5433 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005434 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005437 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaar734a8672019-12-02 22:49:38 +01005440 // Try to keep window completely on screen.
5441 // Get position of the screen work area. This is the part that is not
5442 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 get_work_area(&workarea_rect);
5444
Bram Moolenaar734a8672019-12-02 22:49:38 +01005445 // Resizing a maximized window looks very strange, unzoom it first.
5446 // But don't do it when still starting up, it may have been requested in
5447 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005448 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005450
5451 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
Bram Moolenaar734a8672019-12-02 22:49:38 +01005453 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005454 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5455 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5456 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5457 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5458 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5459 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar734a8672019-12-02 22:49:38 +01005461 // The following should take care of keeping Vim on the same monitor, no
5462 // matter if the secondary monitor is left or right of the primary
5463 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005464 window_rect.right = window_rect.left + win_width;
5465 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005466
Bram Moolenaar734a8672019-12-02 22:49:38 +01005467 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005468 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5469 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005471 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5472 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005474 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5475 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005477 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5478 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005480 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5481 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 SetActiveWindow(s_hwnd);
5484 SetFocus(s_hwnd);
5485
Bram Moolenaar734a8672019-12-02 22:49:38 +01005486 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
5489
5490
5491 void
5492gui_mch_set_scrollbar_thumb(
5493 scrollbar_T *sb,
5494 long val,
5495 long size,
5496 long max)
5497{
5498 SCROLLINFO info;
5499
5500 sb->scroll_shift = 0;
5501 while (max > 32767)
5502 {
5503 max = (max + 1) >> 1;
5504 val >>= 1;
5505 size >>= 1;
5506 ++sb->scroll_shift;
5507 }
5508
5509 if (sb->scroll_shift > 0)
5510 ++size;
5511
5512 info.cbSize = sizeof(info);
5513 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5514 info.nPos = val;
5515 info.nMin = 0;
5516 info.nMax = max;
5517 info.nPage = size;
5518 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5519}
5520
5521
5522/*
5523 * Set the current text font.
5524 */
5525 void
5526gui_mch_set_font(GuiFont font)
5527{
5528 gui.currFont = font;
5529}
5530
5531
5532/*
5533 * Set the current text foreground color.
5534 */
5535 void
5536gui_mch_set_fg_color(guicolor_T color)
5537{
5538 gui.currFgColor = color;
5539}
5540
5541/*
5542 * Set the current text background color.
5543 */
5544 void
5545gui_mch_set_bg_color(guicolor_T color)
5546{
5547 gui.currBgColor = color;
5548}
5549
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005550/*
5551 * Set the current text special color.
5552 */
5553 void
5554gui_mch_set_sp_color(guicolor_T color)
5555{
5556 gui.currSpColor = color;
5557}
5558
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005559#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560/*
5561 * Multi-byte handling, originally by Sung-Hoon Baek.
5562 * First static functions (no prototypes generated).
5563 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005564# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005565# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005566# endif
5567# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * handle WM_IME_NOTIFY message
5571 */
5572 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005573_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 LRESULT lResult = 0;
5576 HIMC hImc;
5577
5578 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5579 return lResult;
5580 switch (dwCommand)
5581 {
5582 case IMN_SETOPENSTATUS:
5583 if (pImmGetOpenStatus(hImc))
5584 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005585 LOGFONTW lf = norm_logfont;
5586 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5587 // Work around when PerMonitorV2 is not enabled in the process level.
5588 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5589 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 im_set_position(gui.row, gui.col);
5591
Bram Moolenaar734a8672019-12-02 22:49:38 +01005592 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005593 State &= ~MODE_LANGMAP;
5594 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005596# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005597 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5599 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005600 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 int old_row = gui.row;
5602 int old_col = gui.col;
5603
5604 // This must be called here before
5605 // status_redraw_curbuf(), otherwise the mode
5606 // message may appear in the wrong position.
5607 showmode();
5608 status_redraw_curbuf();
5609 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005610 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 gui.row = old_row;
5612 gui.col = old_col;
5613 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616 }
5617 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005618 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 lResult = 0;
5620 break;
5621 }
5622 pImmReleaseContext(hWnd, hImc);
5623 return lResult;
5624}
5625
5626 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005627_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 char_u *ret;
5630 int len;
5631
Bram Moolenaar734a8672019-12-02 22:49:38 +01005632 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return 0;
5634
5635 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5636 if (ret != NULL)
5637 {
5638 add_to_input_buf_csi(ret, len);
5639 vim_free(ret);
5640 return 1;
5641 }
5642 return 0;
5643}
5644
5645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 * void GetResultStr()
5647 *
5648 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5649 * get complete composition string
5650 */
5651 static char_u *
5652GetResultStr(HWND hwnd, int GCS, int *lenp)
5653{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005654 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005655 LONG ret;
5656 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 char_u *convbuf = NULL;
5658
5659 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5660 return NULL;
5661
K.Takatab0b2b732022-01-19 12:59:21 +00005662 // Get the length of the composition string.
5663 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5664 if (ret <= 0)
5665 return NULL;
5666
5667 // Allocate the requested buffer plus space for the NUL character.
5668 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 if (buf == NULL)
5670 return NULL;
5671
K.Takatab0b2b732022-01-19 12:59:21 +00005672 // Reads in the composition string.
5673 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5674 *lenp = ret / sizeof(WCHAR);
5675
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005676 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 pImmReleaseContext(hwnd, hIMC);
5678 vim_free(buf);
5679 return convbuf;
5680}
5681#endif
5682
Bram Moolenaar734a8672019-12-02 22:49:38 +01005683// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005684#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686/*
5687 * set font to IM.
5688 */
5689 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005690im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 HIMC hImc;
5693
5694 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5695 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005696 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 pImmReleaseContext(s_hwnd, hImc);
5698 }
5699}
5700
5701/*
5702 * Notify cursor position to IM.
5703 */
5704 void
5705im_set_position(int row, int col)
5706{
5707 HIMC hImc;
5708
5709 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5710 {
5711 COMPOSITIONFORM cfs;
5712
5713 cfs.dwStyle = CFS_POINT;
5714 cfs.ptCurrentPos.x = FILL_X(col);
5715 cfs.ptCurrentPos.y = FILL_Y(row);
5716 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005717 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5718 {
5719 // Work around when PerMonitorV2 is not enabled in the process level.
5720 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5721 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 pImmSetCompositionWindow(hImc, &cfs);
5724
5725 pImmReleaseContext(s_hwnd, hImc);
5726 }
5727}
5728
5729/*
5730 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5731 */
5732 void
5733im_set_active(int active)
5734{
5735 HIMC hImc;
5736 static HIMC hImcOld = (HIMC)0;
5737
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005738# ifdef VIMDLL
5739 if (!gui.in_use && !gui.starting)
5740 {
5741 mbyte_im_set_active(active);
5742 return;
5743 }
5744# endif
5745
Bram Moolenaar734a8672019-12-02 22:49:38 +01005746 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (p_imdisable)
5749 {
5750 if (hImcOld == (HIMC)0)
5751 {
5752 hImcOld = pImmGetContext(s_hwnd);
5753 if (hImcOld)
5754 pImmAssociateContext(s_hwnd, (HIMC)0);
5755 }
5756 active = FALSE;
5757 }
5758 else if (hImcOld != (HIMC)0)
5759 {
5760 pImmAssociateContext(s_hwnd, hImcOld);
5761 hImcOld = (HIMC)0;
5762 }
5763
5764 hImc = pImmGetContext(s_hwnd);
5765 if (hImc)
5766 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005767 /*
5768 * for Korean ime
5769 */
5770 HKL hKL = GetKeyboardLayout(0);
5771
5772 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5773 {
5774 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5775 static BOOL bSaved = FALSE;
5776
5777 if (active)
5778 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005779 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005780 if (bSaved)
5781 pImmSetConversionStatus(hImc, dwConversionSaved,
5782 dwSentenceSaved);
5783 bSaved = FALSE;
5784 }
5785 else
5786 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005787 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005788 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5789 &dwSentenceSaved))
5790 {
5791 bSaved = TRUE;
5792 pImmSetConversionStatus(hImc,
5793 dwConversionSaved & ~(IME_CMODE_NATIVE
5794 | IME_CMODE_FULLSHAPE),
5795 dwSentenceSaved);
5796 }
5797 }
5798 }
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 pImmSetOpenStatus(hImc, active);
5801 pImmReleaseContext(s_hwnd, hImc);
5802 }
5803 }
5804}
5805
5806/*
5807 * Get IM status. When IM is on, return not 0. Else return 0.
5808 */
5809 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005810im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 int status = 0;
5813 HIMC hImc;
5814
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005815# ifdef VIMDLL
5816 if (!gui.in_use && !gui.starting)
5817 return mbyte_im_get_status();
5818# endif
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5821 {
5822 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5823 pImmReleaseContext(s_hwnd, hImc);
5824 }
5825 return status;
5826}
5827
Bram Moolenaar734a8672019-12-02 22:49:38 +01005828#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005831/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005832 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005833 */
5834 static void
5835latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5836{
5837 int c;
5838
Bram Moolenaarca003e12006-03-17 23:19:38 +00005839 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005840 {
5841 c = *text++;
5842 switch (c)
5843 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005844 case 0xa4: c = 0x20ac; break; // euro
5845 case 0xa6: c = 0x0160; break; // S hat
5846 case 0xa8: c = 0x0161; break; // S -hat
5847 case 0xb4: c = 0x017d; break; // Z hat
5848 case 0xb8: c = 0x017e; break; // Z -hat
5849 case 0xbc: c = 0x0152; break; // OE
5850 case 0xbd: c = 0x0153; break; // oe
5851 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005852 }
5853 *unicodebuf++ = c;
5854 }
5855}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857#ifdef FEAT_RIGHTLEFT
5858/*
5859 * What is this for? In the case where you are using Win98 or Win2K or later,
5860 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5861 * reverses the string sent to the TextOut... family. This sucks, because we
5862 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5863 * way to tell Windblows not to do this!
5864 *
5865 * The short of it is that this 'RevOut' only gets called if you are running
5866 * one of the new, "improved" MS OSes, and only if you are running in
5867 * 'rightleft' mode. It makes display take *slightly* longer, but not
5868 * noticeably so.
5869 */
5870 static void
K.Takata135e1522022-01-29 15:27:58 +00005871RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 int col,
5873 int row,
5874 UINT foptions,
5875 CONST RECT *pcliprect,
5876 LPCTSTR text,
5877 UINT len,
5878 CONST INT *padding)
5879{
5880 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005882 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005883 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005884 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885}
5886#endif
5887
Bram Moolenaar92467d32017-12-05 13:22:16 +01005888 static void
5889draw_line(
5890 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005891 int y1,
5892 int x2,
5893 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005894 COLORREF color)
5895{
5896#if defined(FEAT_DIRECTX)
5897 if (IS_ENABLE_DIRECTX())
5898 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5899 else
5900#endif
5901 {
5902 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5903 HPEN old_pen = SelectObject(s_hdc, hpen);
5904 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005905 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005906 LineTo(s_hdc, x2, y2);
5907 DeleteObject(SelectObject(s_hdc, old_pen));
5908 }
5909}
5910
5911 static void
5912set_pixel(
5913 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005914 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005915 COLORREF color)
5916{
5917#if defined(FEAT_DIRECTX)
5918 if (IS_ENABLE_DIRECTX())
5919 DWriteContext_SetPixel(s_dwc, x, y, color);
5920 else
5921#endif
5922 SetPixel(s_hdc, x, y, color);
5923}
5924
5925 static void
5926fill_rect(
5927 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005928 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005929 COLORREF color)
5930{
5931#if defined(FEAT_DIRECTX)
5932 if (IS_ENABLE_DIRECTX())
5933 DWriteContext_FillRect(s_dwc, rcp, color);
5934 else
5935#endif
5936 {
5937 HBRUSH hbr2;
5938
5939 if (hbr == NULL)
5940 hbr2 = CreateSolidBrush(color);
5941 else
5942 hbr2 = hbr;
5943 FillRect(s_hdc, rcp, hbr2);
5944 if (hbr == NULL)
5945 DeleteBrush(hbr2);
5946 }
5947}
5948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 void
5950gui_mch_draw_string(
5951 int row,
5952 int col,
5953 char_u *text,
5954 int len,
5955 int flags)
5956{
5957 static int *padding = NULL;
5958 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 const RECT *pcliprect = NULL;
5960 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 static WCHAR *unicodebuf = NULL;
5962 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005963 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 int y;
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 /*
5968 * Italic and bold text seems to have an extra row of pixels at the bottom
5969 * (below where the bottom of the character should be). If we draw the
5970 * characters with a solid background, the top row of pixels in the
5971 * character below will be overwritten. We can fix this by filling in the
5972 * background ourselves, to the correct character proportions, and then
5973 * writing the character in transparent mode. Still have a problem when
5974 * the character is "_", which gets written on to the character below.
5975 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5976 * pixel in their slots, which fixes the problem with the bottom row of
5977 * pixels. We still need this code because otherwise the top row of pixels
5978 * becomes a problem. - webb.
5979 */
5980 static HBRUSH hbr_cache[2] = {NULL, NULL};
5981 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5982 static int brush_lru = 0;
5983 HBRUSH hbr;
5984 RECT rc;
5985
5986 if (!(flags & DRAW_TRANSP))
5987 {
5988 /*
5989 * Clear background first.
5990 * Note: FillRect() excludes right and bottom of rectangle.
5991 */
5992 rc.left = FILL_X(col);
5993 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (has_mbyte)
5995 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005996 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005997 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
5999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 rc.right = FILL_X(col + len);
6001 rc.bottom = FILL_Y(row + 1);
6002
Bram Moolenaar734a8672019-12-02 22:49:38 +01006003 // Cache the created brush, that saves a lot of time. We need two:
6004 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 if (gui.currBgColor == brush_color[0])
6006 {
6007 hbr = hbr_cache[0];
6008 brush_lru = 1;
6009 }
6010 else if (gui.currBgColor == brush_color[1])
6011 {
6012 hbr = hbr_cache[1];
6013 brush_lru = 0;
6014 }
6015 else
6016 {
6017 if (hbr_cache[brush_lru] != NULL)
6018 DeleteBrush(hbr_cache[brush_lru]);
6019 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6020 brush_color[brush_lru] = gui.currBgColor;
6021 hbr = hbr_cache[brush_lru];
6022 brush_lru = !brush_lru;
6023 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006024
Bram Moolenaar92467d32017-12-05 13:22:16 +01006025 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027 SetBkMode(s_hdc, TRANSPARENT);
6028
6029 /*
6030 * When drawing block cursor, prevent inverted character spilling
6031 * over character cell (can happen with bold/italic)
6032 */
6033 if (flags & DRAW_CURSOR)
6034 {
6035 pcliprect = &rc;
6036 foptions = ETO_CLIPPED;
6037 }
6038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 SetTextColor(s_hdc, gui.currFgColor);
6040 SelectFont(s_hdc, gui.currFont);
6041
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006042#ifdef FEAT_DIRECTX
6043 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006044 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006045#endif
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6048 {
K.Takata135e1522022-01-29 15:27:58 +00006049 int i;
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 vim_free(padding);
6052 pad_size = Columns;
6053
Bram Moolenaar734a8672019-12-02 22:49:38 +01006054 // Don't give an out-of-memory message here, it would call us
6055 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006056 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (padding != NULL)
6058 for (i = 0; i < pad_size; i++)
6059 padding[i] = gui.char_width;
6060 }
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 /*
6063 * We have to provide the padding argument because italic and bold versions
6064 * of fixed-width fonts are often one pixel or so wider than their normal
6065 * versions.
6066 * No check for DRAW_BOLD, Windows will have done it already.
6067 */
6068
Bram Moolenaar734a8672019-12-02 22:49:38 +01006069 // Check if there are any UTF-8 characters. If not, use normal text
6070 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (enc_utf8)
6072 for (n = 0; n < len; ++n)
6073 if (text[n] >= 0x80)
6074 break;
6075
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006076#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006077 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6078 // required that unicode drawing routine, currently. So this forces it
6079 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006080 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006081 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006082#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006083
Bram Moolenaar734a8672019-12-02 22:49:38 +01006084 // Check if the Unicode buffer exists and is big enough. Create it
6085 // with the same length as the multi-byte string, the number of wide
6086 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006087 if ((enc_utf8
6088 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6089 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 && (unicodebuf == NULL || len > unibuflen))
6091 {
6092 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006093 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
6095 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006096 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098 unibuflen = len;
6099 }
6100
6101 if (enc_utf8 && n < len && unicodebuf != NULL)
6102 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006103 // Output UTF-8 characters. Composing characters should be
6104 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006105 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006106 int wlen; // string length in words
6107 int clen; // string length in characters
6108 int cells; // cell width of string up to composing char
6109 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006110 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006112 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006113 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006115 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006117 c = utf_ptr2char(text + i);
6118 if (c >= 0x10000)
6119 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006120 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006121 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6122 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006123 }
6124 else
6125 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006126 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006127 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006128
6129 if (utf_iscomposing(c))
6130 cw = 0;
6131 else
6132 {
6133 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006134 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006135 cw = 1;
6136 }
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (unicodepdy != NULL)
6139 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006140 // Use unicodepdy to make characters fit as we expect, even
6141 // when the font uses different widths (e.g., bold character
6142 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006143 if (c >= 0x10000)
6144 {
6145 unicodepdy[wlen - 2] = cw * gui.char_width;
6146 unicodepdy[wlen - 1] = 0;
6147 }
6148 else
6149 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 }
6151 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006152 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006153 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006155#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006156 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006157 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006158 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006159 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006160 TEXT_X(col), TEXT_Y(row),
6161 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006162 gui.char_width, gui.currFgColor,
6163 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006164 }
6165 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006166#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006167 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6168 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006169 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006171 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006173 // If we want to display codepage data, and the current CP is not the
6174 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (unicodebuf != NULL)
6176 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006177 if (enc_latin9)
6178 latin9_to_ucs(text, len, unicodebuf);
6179 else
6180 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 MB_PRECOMPOSED,
6182 (char *)text, len,
6183 (LPWSTR)unicodebuf, unibuflen);
6184 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006185 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006186 // Use unicodepdy to make characters fit as we expect, even
6187 // when the font uses different widths (e.g., bold character
6188 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006189 if (unicodepdy != NULL)
6190 {
6191 int i;
6192 int cw;
6193
6194 for (i = 0; i < len; ++i)
6195 {
6196 cw = utf_char2cells(unicodebuf[i]);
6197 if (cw > 2)
6198 cw = 1;
6199 unicodepdy[i] = cw * gui.char_width;
6200 }
6201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006203 foptions, pcliprect, unicodebuf, len, unicodepdy);
6204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 }
6206 }
6207 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006210 // Windows will mess up RL text, so we have to draw it character by
6211 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006212 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6214 foptions, pcliprect, (char *)text, len, padding);
6215 else
6216#endif
6217 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6218 foptions, pcliprect, (char *)text, len, padding);
6219 }
6220
Bram Moolenaar734a8672019-12-02 22:49:38 +01006221 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (flags & DRAW_UNDERL)
6223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // When p_linespace is 0, overwrite the bottom row of pixels.
6225 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (p_linespace > 1)
6228 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006229 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006231
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006233 if (flags & DRAW_STRIKE)
6234 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006235 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006236 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006237 }
6238
Bram Moolenaar734a8672019-12-02 22:49:38 +01006239 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006240 if (flags & DRAW_UNDERC)
6241 {
6242 int x;
6243 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006244 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006245
6246 y = FILL_Y(row + 1) - 1;
6247 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6248 {
6249 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006250 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006251 }
6252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253}
6254
6255
6256/*
6257 * Output routines.
6258 */
6259
Bram Moolenaar734a8672019-12-02 22:49:38 +01006260/*
6261 * Flush any output to the screen
6262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 void
6264gui_mch_flush(void)
6265{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006266#if defined(FEAT_DIRECTX)
6267 if (IS_ENABLE_DIRECTX())
6268 DWriteContext_Flush(s_dwc);
6269#endif
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 GdiFlush();
6272}
6273
6274 static void
6275clear_rect(RECT *rcp)
6276{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006277 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278}
6279
6280
Bram Moolenaarc716c302006-01-21 22:12:51 +00006281 void
6282gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6283{
6284 RECT workarea_rect;
6285
6286 get_work_area(&workarea_rect);
6287
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006288 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006289 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6290 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006291
Bram Moolenaar734a8672019-12-02 22:49:38 +01006292 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6293 // the menubar for MSwin, we subtract it from the screen height, so that
6294 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006295 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006296 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6297 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6298 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6299 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006300}
6301
6302
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303#if defined(FEAT_MENU) || defined(PROTO)
6304/*
6305 * Add a sub menu to the menu bar.
6306 */
6307 void
6308gui_mch_add_menu(
6309 vimmenu_T *menu,
6310 int pos)
6311{
6312 vimmenu_T *parent = menu->parent;
6313
6314 menu->submenu_id = CreatePopupMenu();
6315 menu->id = s_menu_id++;
6316
6317 if (menu_is_menubar(menu->name))
6318 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006319 WCHAR *wn;
6320 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006322 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006323 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006324 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006326 infow.cbSize = sizeof(infow);
6327 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6328 | MIIM_SUBMENU;
6329 infow.dwItemData = (long_u)menu;
6330 infow.wID = menu->id;
6331 infow.fType = MFT_STRING;
6332 infow.dwTypeData = wn;
6333 infow.cch = (UINT)wcslen(wn);
6334 infow.hSubMenu = menu->submenu_id;
6335 InsertMenuItemW((parent == NULL)
6336 ? s_menuBar : parent->submenu_id,
6337 (UINT)pos, TRUE, &infow);
6338 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 }
6340
Bram Moolenaar734a8672019-12-02 22:49:38 +01006341 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (parent == NULL)
6343 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006344# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 else if (IsWindow(parent->tearoff_handle))
6346 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350 void
6351gui_mch_show_popupmenu(vimmenu_T *menu)
6352{
6353 POINT mp;
6354
K.Takata45f9cfb2022-01-21 11:11:00 +00006355 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6357}
6358
6359 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006360gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 vimmenu_T *menu = gui_find_menu(path_name);
6363
6364 if (menu != NULL)
6365 {
6366 POINT p;
6367
Bram Moolenaar734a8672019-12-02 22:49:38 +01006368 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006370 if (mouse_pos)
6371 {
6372 int mx, my;
6373
6374 gui_mch_getmouse(&mx, &my);
6375 p.x += mx;
6376 p.y += my;
6377 }
6378 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006380 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6382 }
6383 msg_scroll = FALSE;
6384 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6385 }
6386}
6387
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006388# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389/*
6390 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6391 * create it as a pseudo-"tearoff menu".
6392 */
6393 void
6394gui_make_tearoff(char_u *path_name)
6395{
6396 vimmenu_T *menu = gui_find_menu(path_name);
6397
Bram Moolenaar734a8672019-12-02 22:49:38 +01006398 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (menu != NULL)
6400 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6401}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404/*
6405 * Add a menu item to a menu
6406 */
6407 void
6408gui_mch_add_menu_item(
6409 vimmenu_T *menu,
6410 int idx)
6411{
6412 vimmenu_T *parent = menu->parent;
6413
6414 menu->id = s_menu_id++;
6415 menu->submenu_id = NULL;
6416
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006417# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6419 {
6420 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6421 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6422 }
6423 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006424# endif
6425# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (menu_is_toolbar(parent->name))
6427 {
6428 TBBUTTON newtb;
6429
Bram Moolenaara80faa82020-04-12 19:37:17 +02006430 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (menu_is_separator(menu->name))
6432 {
6433 newtb.iBitmap = 0;
6434 newtb.fsStyle = TBSTYLE_SEP;
6435 }
6436 else
6437 {
6438 newtb.iBitmap = get_toolbar_bitmap(menu);
6439 newtb.fsStyle = TBSTYLE_BUTTON;
6440 }
6441 newtb.idCommand = menu->id;
6442 newtb.fsState = TBSTATE_ENABLED;
6443 newtb.iString = 0;
6444 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6445 (LPARAM)&newtb);
6446 menu->submenu_id = (HMENU)-1;
6447 }
6448 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006451 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006453 wn = enc_to_utf16(menu->name, NULL);
6454 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006456 InsertMenuW(parent->submenu_id, (UINT)idx,
6457 (menu_is_separator(menu->name)
6458 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6459 (UINT)menu->id, wn);
6460 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006462# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (IsWindow(parent->tearoff_handle))
6464 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
6467}
6468
6469/*
6470 * Destroy the machine specific menu widget.
6471 */
6472 void
6473gui_mch_destroy_menu(vimmenu_T *menu)
6474{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006475# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 /*
6477 * is this a toolbar button?
6478 */
6479 if (menu->submenu_id == (HMENU)-1)
6480 {
6481 int iButton;
6482
6483 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6484 (WPARAM)menu->id, 0);
6485 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6486 }
6487 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006488# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 {
6490 if (menu->parent != NULL
6491 && menu_is_popup(menu->parent->dname)
6492 && menu->parent->submenu_id != NULL)
6493 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6494 else
6495 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6496 if (menu->submenu_id != NULL)
6497 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006498# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (IsWindow(menu->tearoff_handle))
6500 DestroyWindow(menu->tearoff_handle);
6501 if (menu->parent != NULL
6502 && menu->parent->children != NULL
6503 && IsWindow(menu->parent->tearoff_handle))
6504 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006505 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 menu->modes = 0;
6507 rebuild_tearoff(menu->parent);
6508 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 }
6511}
6512
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006513# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 static void
6515rebuild_tearoff(vimmenu_T *menu)
6516{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006517 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 char_u tbuf[128];
6519 RECT trect;
6520 RECT rct;
6521 RECT roct;
6522 int x, y;
6523
6524 HWND thwnd = menu->tearoff_handle;
6525
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006526 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (GetWindowRect(thwnd, &trect)
6528 && GetWindowRect(s_hwnd, &rct)
6529 && GetClientRect(s_hwnd, &roct))
6530 {
6531 x = trect.left - rct.left;
6532 y = (trect.top - rct.bottom + roct.bottom);
6533 }
6534 else
6535 {
6536 x = y = 0xffffL;
6537 }
6538 DestroyWindow(thwnd);
6539 if (menu->children != NULL)
6540 {
6541 gui_mch_tearoff(tbuf, menu, x, y);
6542 if (IsWindow(menu->tearoff_handle))
6543 (void) SetWindowPos(menu->tearoff_handle,
6544 NULL,
6545 (int)trect.left,
6546 (int)trect.top,
6547 0, 0,
6548 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6549 }
6550}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006551# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553/*
6554 * Make a menu either grey or not grey.
6555 */
6556 void
6557gui_mch_menu_grey(
6558 vimmenu_T *menu,
6559 int grey)
6560{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006561# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 /*
6563 * is this a toolbar button?
6564 */
6565 if (menu->submenu_id == (HMENU)-1)
6566 {
6567 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006568 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
6570 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006571# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006572 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6573 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006575# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6577 {
6578 WORD menuID;
6579 HWND menuHandle;
6580
6581 /*
6582 * A tearoff button has changed state.
6583 */
6584 if (menu->children == NULL)
6585 menuID = (WORD)(menu->id);
6586 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006587 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6589 if (menuHandle)
6590 EnableWindow(menuHandle, !grey);
6591
6592 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594}
6595
Bram Moolenaar734a8672019-12-02 22:49:38 +01006596#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598
Bram Moolenaar734a8672019-12-02 22:49:38 +01006599// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006602#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6605/*
6606 * stuff for dialogs
6607 */
6608
6609/*
6610 * The callback routine used by all the dialogs. Very simple. First,
6611 * acknowledges the INITDIALOG message so that Windows knows to do standard
6612 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6613 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6614 * number.
6615 */
6616 static LRESULT CALLBACK
6617dialog_callback(
6618 HWND hwnd,
6619 UINT message,
6620 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006621 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 if (message == WM_INITDIALOG)
6624 {
6625 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006626 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 (void)SetFocus(hwnd);
6628 if (dialog_default_button > IDCANCEL)
6629 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006630 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006631 // We don't have a default, set focus on another element of the
6632 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006633 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 return FALSE;
6635 }
6636
6637 if (message == WM_COMMAND)
6638 {
6639 int button = LOWORD(wParam);
6640
Bram Moolenaar734a8672019-12-02 22:49:38 +01006641 // Don't end the dialog if something was selected that was
6642 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 if (button >= DLG_NONBUTTON_CONTROL)
6644 return TRUE;
6645
Bram Moolenaar734a8672019-12-02 22:49:38 +01006646 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006648 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006649 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006650 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006651
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006652 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6653 p = utf16_to_enc(wp, NULL);
6654 vim_strncpy(s_textfield, p, IOSIZE);
6655 vim_free(p);
6656 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
6659 /*
6660 * Need to check for IDOK because if the user just hits Return to
6661 * accept the default value, some reason this is what we get.
6662 */
6663 if (button == IDOK)
6664 {
6665 if (dialog_default_button > IDCANCEL)
6666 EndDialog(hwnd, dialog_default_button);
6667 }
6668 else
6669 EndDialog(hwnd, button - IDCANCEL);
6670 return TRUE;
6671 }
6672
6673 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6674 {
6675 EndDialog(hwnd, 0);
6676 return TRUE;
6677 }
6678 return FALSE;
6679}
6680
6681/*
6682 * Create a dialog dynamically from the parameter strings.
6683 * type = type of dialog (question, alert, etc.)
6684 * title = dialog title. may be NULL for default title.
6685 * message = text to display. Dialog sizes to accommodate it.
6686 * buttons = '\n' separated list of button captions, default first.
6687 * dfltbutton = number of default button.
6688 *
6689 * This routine returns 1 if the first button is pressed,
6690 * 2 for the second, etc.
6691 *
6692 * 0 indicates Esc was pressed.
6693 * -1 for unexpected error
6694 *
6695 * If stubbing out this fn, return 1.
6696 */
6697
Bram Moolenaar734a8672019-12-02 22:49:38 +01006698static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 "IDR_VIM",
6701 "IDR_VIM_ERROR",
6702 "IDR_VIM_ALERT",
6703 "IDR_VIM_INFO",
6704 "IDR_VIM_QUESTION"
6705};
6706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 int
6708gui_mch_dialog(
6709 int type,
6710 char_u *title,
6711 char_u *message,
6712 char_u *buttons,
6713 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006714 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006715 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006718 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 int numButtons;
6720 int *buttonWidths, *buttonPositions;
6721 int buttonYpos;
6722 int nchar, i;
6723 DWORD lStyle;
6724 int dlgwidth = 0;
6725 int dlgheight;
6726 int editboxheight;
6727 int horizWidth = 0;
6728 int msgheight;
6729 char_u *pstart;
6730 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006731 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 char_u *tbuffer;
6733 RECT rect;
6734 HWND hwnd;
6735 HDC hdc;
6736 HFONT font, oldFont;
6737 TEXTMETRIC fontInfo;
6738 int fontHeight;
6739 int textWidth, minButtonWidth, messageWidth;
6740 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006741 int maxDialogHeight;
6742 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 int vertical;
6744 int dlgPaddingX;
6745 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006746# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006747 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006749# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006750 garray_T ga;
6751 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006752 int dlg_icon_width;
6753 int dlg_icon_height;
6754 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006756# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006757 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006758# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006759 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006760# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006761 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006762 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
Bram Moolenaar748bf032005-02-02 23:04:36 +00006765 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006766 {
6767 load_dpi_func();
6768 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006769 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006770 }
6771 else
6772 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 if ((type < 0) || (type > VIM_LAST_TYPE))
6775 type = 0;
6776
Bram Moolenaar734a8672019-12-02 22:49:38 +01006777 // allocate some memory for dialog template
6778 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006779 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006780 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
6782 if (p == NULL)
6783 return -1;
6784
6785 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006786 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6788 * const.
6789 */
6790 tbuffer = vim_strsave(buttons);
6791 if (tbuffer == NULL)
6792 return -1;
6793
Bram Moolenaar734a8672019-12-02 22:49:38 +01006794 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
Bram Moolenaar734a8672019-12-02 22:49:38 +01006796 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 numButtons = 1;
6798 for (i = 0; tbuffer[i] != '\0'; i++)
6799 {
6800 if (tbuffer[i] == DLG_BUTTON_SEP)
6801 numButtons++;
6802 }
6803 if (dfltbutton >= numButtons)
6804 dfltbutton = -1;
6805
Bram Moolenaar734a8672019-12-02 22:49:38 +01006806 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006807 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (buttonWidths == NULL)
6809 return -1;
6810
Bram Moolenaar734a8672019-12-02 22:49:38 +01006811 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006812 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (buttonPositions == NULL)
6814 return -1;
6815
6816 /*
6817 * Calculate how big the dialog must be.
6818 */
6819 hwnd = GetDesktopWindow();
6820 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006821# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6823 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006824 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 use_lfSysmenu = TRUE;
6826 }
6827 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006828# endif
K.Takatad1c58992022-01-23 12:31:57 +00006829 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6830 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6831
6832 oldFont = SelectFont(hdc, font);
6833 dlgPaddingX = DLG_PADDING_X;
6834 dlgPaddingY = DLG_PADDING_Y;
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 GetTextMetrics(hdc, &fontInfo);
6837 fontHeight = fontInfo.tmHeight;
6838
Bram Moolenaar734a8672019-12-02 22:49:38 +01006839 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006840 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
Bram Moolenaar734a8672019-12-02 22:49:38 +01006842 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006843 if (s_hwnd == NULL)
6844 {
6845 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
Bram Moolenaar734a8672019-12-02 22:49:38 +01006847 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006848 get_work_area(&workarea_rect);
6849 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006850 if (maxDialogWidth > adjust_by_system_dpi(600))
6851 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006852 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006853 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006854 }
6855 else
6856 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006857 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006858 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006859 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006860 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6861 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6862 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6863 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006864
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006865 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006866 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6867 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6868 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6869 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6870 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006871 }
6872
Bram Moolenaar734a8672019-12-02 22:49:38 +01006873 // Set dlgwidth to width of message.
6874 // Copy the message into "ga", changing NL to CR-NL and inserting line
6875 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 pstart = message;
6877 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006878 msgheight = 0;
6879 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 do
6881 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006882 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006883
Bram Moolenaar734a8672019-12-02 22:49:38 +01006884 // Need to figure out where to break the string. The system does it
6885 // at a word boundary, which would mean we can't compute the number of
6886 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006887 textWidth = 0;
6888 last_white = NULL;
6889 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006890 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006891 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006892 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006893 && textWidth > maxDialogWidth * 3 / 4)
6894 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006895 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006896 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006897 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006898 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006899 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006900 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006901 textWidth = 0;
6902
6903 if (last_white != NULL)
6904 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006905 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006906 if (pend > last_white)
6907 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006908 pend = last_white + 1;
6909 last_white = NULL;
6910 }
6911 ga_append(&ga, '\r');
6912 ga_append(&ga, '\n');
6913 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006914 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006915
6916 while (--l >= 0)
6917 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006918 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006919 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006921
6922 ga_append(&ga, '\r');
6923 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 pstart = pend + 1;
6925 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006926
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 if (ga.ga_data != NULL)
6928 message = ga.ga_data;
6929
Bram Moolenaar734a8672019-12-02 22:49:38 +01006930 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006931
K.Takatac81e9bf2022-01-16 14:15:49 +00006932 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6933 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
K.Takatac81e9bf2022-01-16 14:15:49 +00006935 // Add width of icon to dlgwidth, and some space
6936 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6937 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6938
6939 if (msgheight < dlg_icon_height)
6940 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 /*
6943 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006944 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006946 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 if (!vertical)
6948 {
6949 // Place buttons horizontally if they fit.
6950 horizWidth = dlgPaddingX;
6951 pstart = tbuffer;
6952 i = 0;
6953 do
6954 {
6955 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6956 if (pend == NULL)
6957 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006958 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (textWidth < minButtonWidth)
6960 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006961 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 buttonWidths[i] = textWidth;
6963 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006964 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 pstart = pend + 1;
6966 } while (*pend != NUL);
6967
6968 if (horizWidth > maxDialogWidth)
6969 vertical = TRUE; // Too wide to fit on the screen.
6970 else if (horizWidth > dlgwidth)
6971 dlgwidth = horizWidth;
6972 }
6973
6974 if (vertical)
6975 {
6976 // Stack buttons vertically.
6977 pstart = tbuffer;
6978 do
6979 {
6980 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6981 if (pend == NULL)
6982 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006983 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006984 textWidth += dlgPaddingX; // Padding within button
6985 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (textWidth > dlgwidth)
6987 dlgwidth = textWidth;
6988 pstart = pend + 1;
6989 } while (*pend != NUL);
6990 }
6991
6992 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006993 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
Bram Moolenaar734a8672019-12-02 22:49:38 +01006995 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006996 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998 add_long(lStyle);
6999 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007000 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 add_word(0); // NumberOfItems(will change later)
7002 add_word(10); // x
7003 add_word(10); // y
7004 add_word(PixelToDialogX(dlgwidth)); // cx
7005
7006 // Dialog height.
7007 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007008 dlgheight = msgheight + 2 * dlgPaddingY
7009 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 else
7011 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7012
7013 // Dialog needs to be taller if contains an edit box.
7014 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7015 if (textfield != NULL)
7016 dlgheight += editboxheight;
7017
Bram Moolenaar734a8672019-12-02 22:49:38 +01007018 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007019 if (dlgheight > maxDialogHeight)
7020 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007021 msgheight = msgheight - (dlgheight - maxDialogHeight);
7022 dlgheight = maxDialogHeight;
7023 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007024 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007025 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007026 }
7027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 add_word(PixelToDialogY(dlgheight));
7029
7030 add_word(0); // Menu
7031 add_word(0); // Class
7032
Bram Moolenaar734a8672019-12-02 22:49:38 +01007033 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007034 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7035 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 p += nchar;
7037
K.Takatad1c58992022-01-23 12:31:57 +00007038 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007039# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007040 if (use_lfSysmenu)
7041 {
7042 // point size
7043 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7044 GetDeviceCaps(hdc, LOGPIXELSY));
7045 wcscpy(p, lfSysmenu.lfFaceName);
7046 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
K.Takatad1c58992022-01-23 12:31:57 +00007048 else
7049# endif
7050 {
7051 *p++ = DLG_FONT_POINT_SIZE; // point size
7052 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7053 }
7054 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 buttonYpos = msgheight + 2 * dlgPaddingY;
7057
7058 if (textfield != NULL)
7059 buttonYpos += editboxheight;
7060
7061 pstart = tbuffer;
7062 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007063 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 for (i = 0; i < numButtons; i++)
7065 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007066 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 for ( pend = pstart;
7068 *pend && (*pend != DLG_BUTTON_SEP);
7069 pend++)
7070 ;
7071
7072 if (*pend)
7073 *pend = '\0';
7074
7075 /*
7076 * old NOTE:
7077 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7078 * the focus to the first tab-able button and in so doing makes that
7079 * the default!! Grrr. Workaround: Make the default button the only
7080 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7081 * he/she can use arrow keys.
7082 *
7083 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007084 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 * dialog. Also needed for when the textfield is the default control.
7086 * It appears to work now (perhaps not on Win95?).
7087 */
7088 if (vertical)
7089 {
7090 p = add_dialog_element(p,
7091 (i == dfltbutton
7092 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7093 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007094 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 + 2 * fontHeight * i),
7096 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7097 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007098 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100 else
7101 {
7102 p = add_dialog_element(p,
7103 (i == dfltbutton
7104 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7105 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007106 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 PixelToDialogX(buttonWidths[i]),
7108 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007109 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007111 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113 *pnumitems += numButtons;
7114
Bram Moolenaar734a8672019-12-02 22:49:38 +01007115 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 p = add_dialog_element(p, SS_ICON,
7117 PixelToDialogX(dlgPaddingX),
7118 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007119 PixelToDialogX(dlg_icon_width),
7120 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7122 dlg_icons[type]);
7123
Bram Moolenaar734a8672019-12-02 22:49:38 +01007124 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007125 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007126 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007127 PixelToDialogY(dlgPaddingY),
7128 (WORD)(PixelToDialogX(messageWidth) + 1),
7129 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007130 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaar734a8672019-12-02 22:49:38 +01007132 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (textfield != NULL)
7134 {
7135 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7136 PixelToDialogX(2 * dlgPaddingX),
7137 PixelToDialogY(2 * dlgPaddingY + msgheight),
7138 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7139 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007140 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 *pnumitems += 1;
7142 }
7143
7144 *pnumitems += 2;
7145
7146 SelectFont(hdc, oldFont);
7147 DeleteObject(font);
7148 ReleaseDC(hwnd, hdc);
7149
Bram Moolenaar734a8672019-12-02 22:49:38 +01007150 // Let the dialog_callback() function know which button to make default
7151 // If we have an edit box, make that the default. We also need to tell
7152 // dialog_callback() if this dialog contains an edit box or not. We do
7153 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (textfield != NULL)
7155 {
7156 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7157 s_textfield = textfield;
7158 }
7159 else
7160 {
7161 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7162 s_textfield = NULL;
7163 }
7164
Bram Moolenaar734a8672019-12-02 22:49:38 +01007165 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007167 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 (LPDLGTEMPLATE)pdlgtemplate,
7169 s_hwnd,
7170 (DLGPROC)dialog_callback);
7171
7172 LocalFree(LocalHandle(pdlgtemplate));
7173 vim_free(tbuffer);
7174 vim_free(buttonWidths);
7175 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007176 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar734a8672019-12-02 22:49:38 +01007178 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 (void)SetFocus(s_hwnd);
7180
7181 return nchar;
7182}
7183
Bram Moolenaar734a8672019-12-02 22:49:38 +01007184#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186/*
7187 * Put a simple element (basic class) onto a dialog template in memory.
7188 * return a pointer to where the next item should be added.
7189 *
7190 * parameters:
7191 * lStyle = additional style flags
7192 * (be careful, NT3.51 & Win32s will ignore the new ones)
7193 * x,y = x & y positions IN DIALOG UNITS
7194 * w,h = width and height IN DIALOG UNITS
7195 * Id = ID used in messages
7196 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7197 * caption = usually text or resource name
7198 *
7199 * TODO: use the length information noted here to enable the dialog creation
7200 * routines to work out more exactly how much memory they need to alloc.
7201 */
7202 static PWORD
7203add_dialog_element(
7204 PWORD p,
7205 DWORD lStyle,
7206 WORD x,
7207 WORD y,
7208 WORD w,
7209 WORD h,
7210 WORD Id,
7211 WORD clss,
7212 const char *caption)
7213{
7214 int nchar;
7215
Bram Moolenaar734a8672019-12-02 22:49:38 +01007216 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7218 *p++ = LOWORD(lStyle);
7219 *p++ = HIWORD(lStyle);
7220 *p++ = 0; // LOWORD (lExtendedStyle)
7221 *p++ = 0; // HIWORD (lExtendedStyle)
7222 *p++ = x;
7223 *p++ = y;
7224 *p++ = w;
7225 *p++ = h;
7226 *p++ = Id; //9 or 10 words in all
7227
7228 *p++ = (WORD)0xffff;
7229 *p++ = clss; //2 more here
7230
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007231 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 p += nchar;
7233
7234 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7235
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007236 return p; // total = 15 + strlen(caption) words
7237 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238}
7239
7240
7241/*
7242 * Helper routine. Take an input pointer, return closest pointer that is
7243 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7244 */
7245 static LPWORD
7246lpwAlign(
7247 LPWORD lpIn)
7248{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007249 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007251 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 ul += 3;
7253 ul >>= 2;
7254 ul <<= 2;
7255 return (LPWORD)ul;
7256}
7257
7258/*
7259 * Helper routine. Takes second parameter as Ansi string, copies it to first
7260 * parameter as wide character (16-bits / char) string, and returns integer
7261 * number of wide characters (words) in string (including the trailing wide
7262 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007263 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7264 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 static int
7266nCopyAnsiToWideChar(
7267 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007268 LPSTR lpAnsiIn,
7269 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007272 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 int i;
7274 WCHAR *wn;
7275
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007276 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007278 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007279 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (wn != NULL)
7281 {
7282 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 vim_free(wn);
7285 }
7286 }
7287 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007288 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 nChar = MultiByteToWideChar(
7290 enc_codepage > 0 ? enc_codepage : CP_ACP,
7291 MB_PRECOMPOSED,
7292 lpAnsiIn, len,
7293 lpWCStr, len);
7294 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007295 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
7298 return nChar;
7299}
7300
7301
7302#ifdef FEAT_TEAROFF
7303/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007304 * Lookup menu handle from "menu_id".
7305 */
7306 static HMENU
7307tearoff_lookup_menuhandle(
7308 vimmenu_T *menu,
7309 WORD menu_id)
7310{
7311 for ( ; menu != NULL; menu = menu->next)
7312 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007313 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007314 continue;
7315 if (menu_is_separator(menu->dname))
7316 continue;
7317 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7318 return menu->submenu_id;
7319 }
7320 return NULL;
7321}
7322
7323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 * The callback function for all the modeless dialogs that make up the
7325 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7326 * thinking its menus have been clicked), and go away when closed.
7327 */
7328 static LRESULT CALLBACK
7329tearoff_callback(
7330 HWND hwnd,
7331 UINT message,
7332 WPARAM wParam,
7333 LPARAM lParam)
7334{
7335 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007336 {
7337 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 HandleMouseHide(message, lParam);
7343
7344 if (message == WM_COMMAND)
7345 {
7346 if ((WORD)(LOWORD(wParam)) & 0x8000)
7347 {
7348 POINT mp;
7349 RECT rect;
7350
7351 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7352 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007353 vimmenu_T *menu;
7354
7355 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007357 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7359 (int)rect.right - 8,
7360 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 s_hwnd,
7363 NULL);
7364 /*
7365 * NOTE: The pop-up menu can eat the mouse up event.
7366 * We deal with this in normal.c.
7367 */
7368 }
7369 }
7370 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007371 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7373 /*
7374 * Give main window the focus back: this is so after
7375 * choosing a tearoff button you can start typing again
7376 * straight away.
7377 */
7378 (void)SetFocus(s_hwnd);
7379 return TRUE;
7380 }
7381 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7382 {
7383 DestroyWindow(hwnd);
7384 return TRUE;
7385 }
7386
Bram Moolenaar734a8672019-12-02 22:49:38 +01007387 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (message == WM_EXITSIZEMOVE)
7389 (void)SetActiveWindow(s_hwnd);
7390
7391 return FALSE;
7392}
7393#endif
7394
7395
7396/*
K.Takatad1c58992022-01-23 12:31:57 +00007397 * Computes the dialog base units based on the current dialog font.
7398 * We don't use the GetDialogBaseUnits() API, because we don't use the
7399 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 */
7401 static void
7402get_dialog_font_metrics(void)
7403{
7404 HDC hdc;
7405 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 SIZE size;
7407#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007408 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409#endif
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007412 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007413 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#endif
7416 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007417 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
K.Takatad1c58992022-01-23 12:31:57 +00007419 hdc = GetDC(s_hwnd);
7420 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007421 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007422 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
K.Takataabe628e2022-01-23 16:25:17 +00007424 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007425 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426}
7427
7428#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7429/*
7430 * Create a pseudo-"tearoff menu" based on the child
7431 * items of a given menu pointer.
7432 */
7433 static void
7434gui_mch_tearoff(
7435 char_u *title,
7436 vimmenu_T *menu,
7437 int initX,
7438 int initY)
7439{
7440 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7441 int template_len;
7442 int nchar, textWidth, submenuWidth;
7443 DWORD lStyle;
7444 DWORD lExtendedStyle;
7445 WORD dlgwidth;
7446 WORD menuID;
7447 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007448 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 vimmenu_T *the_menu = menu;
7450 HWND hwnd;
7451 HDC hdc;
7452 HFONT font, oldFont;
7453 int col, spaceWidth, len;
7454 int columnWidths[2];
7455 char_u *label, *text;
7456 int acLen = 0;
7457 int nameLen;
7458 int padding0, padding1, padding2 = 0;
7459 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007460 int x;
7461 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007462# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007463 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
7467 /*
7468 * If this menu is already torn off, move it to the mouse position.
7469 */
7470 if (IsWindow(menu->tearoff_handle))
7471 {
7472 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007473 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 {
7475 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7476 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7477 }
7478 return;
7479 }
7480
7481 /*
7482 * Create a new tearoff.
7483 */
7484 if (*title == MNU_HIDDEN_CHAR)
7485 title++;
7486
Bram Moolenaar734a8672019-12-02 22:49:38 +01007487 // Allocate memory to store the dialog template. It's made bigger when
7488 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 template_len = DLG_ALLOC_SIZE;
7490 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7491 if (p == NULL)
7492 return;
7493
7494 hwnd = GetDesktopWindow();
7495 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007496# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7498 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007499 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 use_lfSysmenu = TRUE;
7501 }
7502 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007503# endif
K.Takatad1c58992022-01-23 12:31:57 +00007504 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7505 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7506
7507 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
Bram Moolenaar734a8672019-12-02 22:49:38 +01007509 // Calculate width of a single space. Used for padding columns to the
7510 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007511 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
Bram Moolenaar734a8672019-12-02 22:49:38 +01007513 // Figure out max width of the text column, the accelerator column and the
7514 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 submenuWidth = 0;
7516 for (col = 0; col < 2; col++)
7517 {
7518 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007519 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007521 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 text = (col == 0) ? pmenu->dname : pmenu->actext;
7523 if (text != NULL && *text != NUL)
7524 {
7525 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7526 if (textWidth > columnWidths[col])
7527 columnWidths[col] = textWidth;
7528 }
7529 if (pmenu->children != NULL)
7530 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7531 }
7532 }
7533 if (columnWidths[1] == 0)
7534 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007535 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (submenuWidth != 0)
7537 columnWidths[0] += submenuWidth;
7538 else
7539 columnWidths[0] += spaceWidth;
7540 }
7541 else
7542 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007543 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7545 columnWidths[1] += submenuWidth;
7546 }
7547
7548 /*
7549 * Now find the total width of our 'menu'.
7550 */
7551 textWidth = columnWidths[0] + columnWidths[1];
7552 if (submenuWidth != 0)
7553 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007554 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7556 textWidth += submenuWidth;
7557 }
7558 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7559 if (textWidth > dlgwidth)
7560 dlgwidth = textWidth;
7561 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7562
Bram Moolenaar734a8672019-12-02 22:49:38 +01007563 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007564 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565
7566 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7567 *p++ = LOWORD(lStyle);
7568 *p++ = HIWORD(lStyle);
7569 *p++ = LOWORD(lExtendedStyle);
7570 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007571 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007573 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007575 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 else
7577 *p++ = PixelToDialogX(initX); // x
7578 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007579 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 else
7581 *p++ = PixelToDialogY(initY); // y
7582 *p++ = PixelToDialogX(dlgwidth); // cx
7583 ptrueheight = p;
7584 *p++ = 0; // dialog height: changed later anyway
7585 *p++ = 0; // Menu
7586 *p++ = 0; // Class
7587
Bram Moolenaar734a8672019-12-02 22:49:38 +01007588 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007590 ? (LPSTR)title
7591 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 p += nchar;
7593
K.Takatad1c58992022-01-23 12:31:57 +00007594 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007595# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007596 if (use_lfSysmenu)
7597 {
7598 // point size
7599 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7600 GetDeviceCaps(hdc, LOGPIXELSY));
7601 wcscpy(p, lfSysmenu.lfFaceName);
7602 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
K.Takatad1c58992022-01-23 12:31:57 +00007604 else
7605# endif
7606 {
7607 *p++ = DLG_FONT_POINT_SIZE; // point size
7608 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7609 }
7610 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612 /*
7613 * Loop over all the items in the menu.
7614 * But skip over the tearbar.
7615 */
7616 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7617 menu = menu->children->next;
7618 else
7619 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007620 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 for ( ; menu != NULL; menu = menu->next)
7622 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007623 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 continue;
7625 if (menu_is_separator(menu->dname))
7626 {
7627 sepPadding += 3;
7628 continue;
7629 }
7630
Bram Moolenaar734a8672019-12-02 22:49:38 +01007631 // Check if there still is plenty of room in the template. Make it
7632 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7634 {
7635 WORD *newp;
7636
7637 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7638 if (newp != NULL)
7639 {
7640 template_len += 4096;
7641 mch_memmove(newp, pdlgtemplate,
7642 (char *)p - (char *)pdlgtemplate);
7643 p = newp + (p - pdlgtemplate);
7644 pnumitems = newp + (pnumitems - pdlgtemplate);
7645 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7646 LocalFree(LocalHandle(pdlgtemplate));
7647 pdlgtemplate = newp;
7648 }
7649 }
7650
Bram Moolenaar734a8672019-12-02 22:49:38 +01007651 // Figure out minimal length of this menu label. Use "name" for the
7652 // actual text, "dname" for estimating the displayed size. "name"
7653 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 len = nameLen = (int)STRLEN(menu->name);
7655 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7656 (int)STRLEN(menu->dname))) / spaceWidth;
7657 len += padding0;
7658
7659 if (menu->actext != NULL)
7660 {
7661 acLen = (int)STRLEN(menu->actext);
7662 len += acLen;
7663 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7664 }
7665 else
7666 textWidth = 0;
7667 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7668 len += padding1;
7669
7670 if (menu->children == NULL)
7671 {
7672 padding2 = submenuWidth / spaceWidth;
7673 len += padding2;
7674 menuID = (WORD)(menu->id);
7675 }
7676 else
7677 {
7678 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007679 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
7681
Bram Moolenaar734a8672019-12-02 22:49:38 +01007682 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007683 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (label == NULL)
7685 break;
7686
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007687 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007688 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007690 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 while (padding0-- > 0)
7692 *text++ = ' ';
7693 if (menu->actext != NULL)
7694 {
7695 STRNCPY(text, menu->actext, acLen);
7696 text += acLen;
7697 }
7698 while (padding1-- > 0)
7699 *text++ = ' ';
7700 if (menu->children != NULL)
7701 {
7702 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7703 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7704 }
7705 else
7706 {
7707 while (padding2-- > 0)
7708 *text++ = ' ';
7709 }
7710 *text = NUL;
7711
7712 /*
7713 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7714 * W95/NT4 it makes the tear-off look more like a menu.
7715 */
7716 p = add_dialog_element(p,
7717 BS_PUSHBUTTON|BS_LEFT,
7718 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7719 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7720 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7721 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007722 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 vim_free(label);
7724 (*pnumitems)++;
7725 }
7726
7727 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7728
7729
Bram Moolenaar734a8672019-12-02 22:49:38 +01007730 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007731 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007732 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 (LPDLGTEMPLATE)pdlgtemplate,
7734 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007735 (DLGPROC)tearoff_callback,
7736 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737
7738 LocalFree(LocalHandle(pdlgtemplate));
7739 SelectFont(hdc, oldFont);
7740 DeleteObject(font);
7741 ReleaseDC(hwnd, hdc);
7742
7743 /*
7744 * Reassert ourselves as the active window. This is so that after creating
7745 * a tearoff, the user doesn't have to click with the mouse just to start
7746 * typing again!
7747 */
7748 (void)SetActiveWindow(s_hwnd);
7749
Bram Moolenaar734a8672019-12-02 22:49:38 +01007750 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 force_menu_update = TRUE;
7752}
7753#endif
7754
7755#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007756# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758/*
7759 * Create the toolbar, initially unpopulated.
7760 * (just like the menu, there are no defaults, it's all
7761 * set up through menu.vim)
7762 */
7763 static void
7764initialise_toolbar(void)
7765{
7766 InitCommonControls();
7767 s_toolbarhwnd = CreateToolbarEx(
7768 s_hwnd,
7769 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7770 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007771 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007772 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 IDR_TOOLBAR1, // id of initial bitmap
7774 NULL,
7775 0, // initial number of buttons
7776 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7777 TOOLBAR_BUTTON_HEIGHT,
7778 TOOLBAR_BUTTON_WIDTH,
7779 TOOLBAR_BUTTON_HEIGHT,
7780 sizeof(TBBUTTON)
7781 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007782
7783 // Remove transparency from the toolbar to prevent the main window
7784 // background colour showing through
7785 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7786 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7787
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007788 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
7790 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007791
7792 update_toolbar_size();
7793}
7794
7795 static void
7796update_toolbar_size(void)
7797{
7798 int w, h;
7799 TBMETRICS tbm = {sizeof(TBMETRICS)};
7800
7801 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7802 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7803 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7804 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7805
7806 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7807 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7808 //TRACE("button size: %d, %d", w, h);
7809 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7810 gui.toolbar_height = h + 6;
7811
7812 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7813 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7814
7815 // TODO:
7816 // Currently, this function only updates the size of toolbar buttons.
7817 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818}
7819
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007820 static LRESULT CALLBACK
7821toolbar_wndproc(
7822 HWND hwnd,
7823 UINT uMsg,
7824 WPARAM wParam,
7825 LPARAM lParam)
7826{
7827 HandleMouseHide(uMsg, lParam);
7828 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7829}
7830
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 static int
7832get_toolbar_bitmap(vimmenu_T *menu)
7833{
7834 int i = -1;
7835
7836 /*
7837 * Check user bitmaps first, unless builtin is specified.
7838 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007839 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {
7841 char_u fname[MAXPATHL];
7842 HANDLE hbitmap = NULL;
7843
7844 if (menu->iconfile != NULL)
7845 {
7846 gui_find_iconfile(menu->iconfile, fname, "bmp");
7847 hbitmap = LoadImage(
7848 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007849 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 IMAGE_BITMAP,
7851 TOOLBAR_BUTTON_WIDTH,
7852 TOOLBAR_BUTTON_HEIGHT,
7853 LR_LOADFROMFILE |
7854 LR_LOADMAP3DCOLORS
7855 );
7856 }
7857
7858 /*
7859 * If the LoadImage call failed, or the "icon=" file
7860 * didn't exist or wasn't specified, try the menu name
7861 */
7862 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007863 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007864# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007865 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007866# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007867 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 hbitmap = LoadImage(
7869 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007870 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 IMAGE_BITMAP,
7872 TOOLBAR_BUTTON_WIDTH,
7873 TOOLBAR_BUTTON_HEIGHT,
7874 LR_LOADFROMFILE |
7875 LR_LOADMAP3DCOLORS
7876 );
7877
7878 if (hbitmap != NULL)
7879 {
7880 TBADDBITMAP tbAddBitmap;
7881
7882 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007883 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884
7885 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7886 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007887 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
7889 }
7890 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7891 i = menu->iconidx;
7892
7893 return i;
7894}
7895#endif
7896
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007897#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7898 static void
7899initialise_tabline(void)
7900{
7901 InitCommonControls();
7902
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007903 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007904 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007905 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007906 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007907 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007908
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007909 gui.tabline_height = TABLINE_HEIGHT;
7910
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007911 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007912}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007913
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007914/*
7915 * Get tabpage_T from POINT.
7916 */
7917 static tabpage_T *
7918GetTabFromPoint(
7919 HWND hWnd,
7920 POINT pt)
7921{
7922 tabpage_T *ptp = NULL;
7923
7924 if (gui_mch_showing_tabline())
7925 {
7926 TCHITTESTINFO htinfo;
7927 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007928 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007929 if (s_tabhwnd == hWnd)
7930 {
7931 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7932 if (idx != -1)
7933 ptp = find_tabpage(idx + 1);
7934 }
7935 }
7936 return ptp;
7937}
7938
7939static POINT s_pt = {0, 0};
7940static HCURSOR s_hCursor = NULL;
7941
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007942 static LRESULT CALLBACK
7943tabline_wndproc(
7944 HWND hwnd,
7945 UINT uMsg,
7946 WPARAM wParam,
7947 LPARAM lParam)
7948{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007949 POINT pt;
7950 tabpage_T *tp;
7951 RECT rect;
7952 int nCenter;
7953 int idx0;
7954 int idx1;
7955
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007956 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007957
7958 switch (uMsg)
7959 {
7960 case WM_LBUTTONDOWN:
7961 {
7962 s_pt.x = GET_X_LPARAM(lParam);
7963 s_pt.y = GET_Y_LPARAM(lParam);
7964 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007965 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007966 break;
7967 }
7968 case WM_MOUSEMOVE:
7969 if (GetCapture() == hwnd
7970 && ((wParam & MK_LBUTTON)) != 0)
7971 {
7972 pt.x = GET_X_LPARAM(lParam);
7973 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007974 if (abs(pt.x - s_pt.x) >
7975 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007976 {
7977 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7978
7979 tp = GetTabFromPoint(hwnd, pt);
7980 if (tp != NULL)
7981 {
7982 idx0 = tabpage_index(curtab) - 1;
7983 idx1 = tabpage_index(tp) - 1;
7984
7985 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7986 nCenter = rect.left + (rect.right - rect.left) / 2;
7987
Bram Moolenaar734a8672019-12-02 22:49:38 +01007988 // Check if the mouse cursor goes over the center of
7989 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007990 if ((idx0 < idx1) && (nCenter < pt.x))
7991 {
7992 tabpage_move(idx1 + 1);
7993 update_screen(0);
7994 }
7995 else if ((idx1 < idx0) && (pt.x < nCenter))
7996 {
7997 tabpage_move(idx1);
7998 update_screen(0);
7999 }
8000 }
8001 }
8002 }
8003 break;
8004 case WM_LBUTTONUP:
8005 {
8006 if (GetCapture() == hwnd)
8007 {
8008 SetCursor(s_hCursor);
8009 ReleaseCapture();
8010 }
8011 break;
8012 }
8013 default:
8014 break;
8015 }
8016
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008017 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8018}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008019#endif
8020
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8022/*
8023 * Make the GUI window come to the foreground.
8024 */
8025 void
8026gui_mch_set_foreground(void)
8027{
8028 if (IsIconic(s_hwnd))
8029 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8030 SetForegroundWindow(s_hwnd);
8031}
8032#endif
8033
8034#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8035 static void
8036dyn_imm_load(void)
8037{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008038 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (hLibImm == NULL)
8040 return;
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 pImmGetCompositionStringW
8043 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8044 pImmGetContext
8045 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8046 pImmAssociateContext
8047 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8048 pImmReleaseContext
8049 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8050 pImmGetOpenStatus
8051 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8052 pImmSetOpenStatus
8053 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008054 pImmGetCompositionFontW
8055 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8056 pImmSetCompositionFontW
8057 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 pImmSetCompositionWindow
8059 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8060 pImmGetConversionStatus
8061 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008062 pImmSetConversionStatus
8063 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064
K.Takatab0b2b732022-01-19 12:59:21 +00008065 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 || pImmGetContext == NULL
8067 || pImmAssociateContext == NULL
8068 || pImmReleaseContext == NULL
8069 || pImmGetOpenStatus == NULL
8070 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008071 || pImmGetCompositionFontW == NULL
8072 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008074 || pImmGetConversionStatus == NULL
8075 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
8077 FreeLibrary(hLibImm);
8078 hLibImm = NULL;
8079 pImmGetContext = NULL;
8080 return;
8081 }
8082
8083 return;
8084}
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#endif
8087
8088#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8089
8090# ifdef FEAT_XPM_W32
8091# define IMAGE_XPM 100
8092# endif
8093
8094typedef struct _signicon_t
8095{
8096 HANDLE hImage;
8097 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008098# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008099 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101} signicon_t;
8102
8103 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008104gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 signicon_t *sign;
8107 int x, y, w, h;
8108
8109 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8110 return;
8111
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008112# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008113 if (IS_ENABLE_DIRECTX())
8114 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008115# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008116
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 x = TEXT_X(col);
8118 y = TEXT_Y(row);
8119 w = gui.char_width * 2;
8120 h = gui.char_height;
8121 switch (sign->uType)
8122 {
8123 case IMAGE_BITMAP:
8124 {
8125 HDC hdcMem;
8126 HBITMAP hbmpOld;
8127
8128 hdcMem = CreateCompatibleDC(s_hdc);
8129 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8130 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8131 SelectObject(hdcMem, hbmpOld);
8132 DeleteDC(hdcMem);
8133 }
8134 break;
8135 case IMAGE_ICON:
8136 case IMAGE_CURSOR:
8137 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8138 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008139# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 case IMAGE_XPM:
8141 {
8142 HDC hdcMem;
8143 HBITMAP hbmpOld;
8144
8145 hdcMem = CreateCompatibleDC(s_hdc);
8146 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008147 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8149
8150 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008151 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8153 SelectObject(hdcMem, hbmpOld);
8154 DeleteDC(hdcMem);
8155 }
8156 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 }
8159}
8160
8161 static void
8162close_signicon_image(signicon_t *sign)
8163{
8164 if (sign)
8165 switch (sign->uType)
8166 {
8167 case IMAGE_BITMAP:
8168 DeleteObject((HGDIOBJ)sign->hImage);
8169 break;
8170 case IMAGE_CURSOR:
8171 DestroyCursor((HCURSOR)sign->hImage);
8172 break;
8173 case IMAGE_ICON:
8174 DestroyIcon((HICON)sign->hImage);
8175 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008176# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 case IMAGE_XPM:
8178 DeleteObject((HBITMAP)sign->hImage);
8179 DeleteObject((HBITMAP)sign->hShape);
8180 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 }
8183}
8184
8185 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008186gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187{
8188 signicon_t sign, *psign;
8189 char_u *ext;
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008192 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 if (ext > signfile)
8194 {
8195 int do_load = 1;
8196
8197 if (!STRICMP(ext, ".bmp"))
8198 sign.uType = IMAGE_BITMAP;
8199 else if (!STRICMP(ext, ".ico"))
8200 sign.uType = IMAGE_ICON;
8201 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8202 sign.uType = IMAGE_CURSOR;
8203 else
8204 do_load = 0;
8205
8206 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008207 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 gui.char_width * 2, gui.char_height,
8209 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008210# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (!STRICMP(ext, ".xpm"))
8212 {
8213 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008214 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8215 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 }
8219
8220 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008221 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 *psign = sign;
8223
8224 if (!psign)
8225 {
8226 if (sign.hImage)
8227 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008228 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 }
8230 return (void *)psign;
8231
8232}
8233
8234 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008235gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 if (sign)
8238 {
8239 close_signicon_image((signicon_t *)sign);
8240 vim_free(sign);
8241 }
8242}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008245#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
Bram Moolenaar734a8672019-12-02 22:49:38 +01008247/*
8248 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008249 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008251 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8253 * to get current mouse position).
8254 *
8255 * Trying to use as more Windows services as possible, and as less
8256 * IE version as possible :)).
8257 *
8258 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8259 * BalloonEval struct.
8260 * 2) Enable/Disable simply create/kill BalloonEval Timer
8261 * 3) When there was enough inactivity, timer procedure posts
8262 * async request to debugger
8263 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8264 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008265 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008268 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008269make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008270{
K.Takata76687d22022-01-25 10:31:37 +00008271 TOOLINFOW *pti;
8272 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008273
K.Takata76687d22022-01-25 10:31:37 +00008274 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008275 if (pti == NULL)
8276 return;
8277
8278 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8279 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8280 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008281 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008282
8283 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8284 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8285
K.Takata76687d22022-01-25 10:31:37 +00008286 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008287 pti->uFlags = TTF_SUBCLASS;
8288 pti->hwnd = beval->target;
8289 pti->hinst = 0; // Don't use string resources
8290 pti->uId = ID_BEVAL_TOOLTIP;
8291
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008292 pti->lpszText = LPSTR_TEXTCALLBACKW;
8293 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8294 pti->lParam = (LPARAM)beval->tofree;
8295 // switch multiline tooltips on
8296 if (GetClientRect(s_textArea, &rect))
8297 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8298 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008299
8300 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8301 pti->rect.left = pt.x - 3;
8302 pti->rect.top = pt.y - 3;
8303 pti->rect.right = pt.x + 3;
8304 pti->rect.bottom = pt.y + 3;
8305
8306 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8307 // Make tooltip appear sooner.
8308 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8309 // I've performed some tests and it seems the longest possible life time
8310 // of tooltip is 30 seconds.
8311 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8312 /*
8313 * HACK: force tooltip to appear, because it'll not appear until
8314 * first mouse move. D*mn M$
8315 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8316 */
8317 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8318 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8319 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008320}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008323delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008325 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326}
8327
8328 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008329beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008330 HWND hwnd UNUSED,
8331 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008332 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008333 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 POINT pt;
8336 RECT rect;
8337
8338 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8339 return;
8340
8341 GetCursorPos(&pt);
8342 if (WindowFromPoint(pt) != s_textArea)
8343 return;
8344
8345 ScreenToClient(s_textArea, &pt);
8346 GetClientRect(s_textArea, &rect);
8347 if (!PtInRect(&rect, pt))
8348 return;
8349
K.Takataa8ec4912022-02-03 14:32:33 +00008350 if (last_user_activity > 0
8351 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 && (cur_beval->showState != ShS_PENDING
8353 || abs(cur_beval->x - pt.x) > 3
8354 || abs(cur_beval->y - pt.y) > 3))
8355 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008356 // Pointer resting in one place long enough, it's time to show
8357 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 cur_beval->showState = ShS_PENDING;
8359 cur_beval->x = pt.x;
8360 cur_beval->y = pt.y;
8361
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 if (cur_beval->msgCB != NULL)
8363 (*cur_beval->msgCB)(cur_beval, 0);
8364 }
8365}
8366
8367 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008368gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
K.Takataa8ec4912022-02-03 14:32:33 +00008370 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371}
8372
8373 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008374gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if (beval == NULL)
8377 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008378 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8379 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380}
8381
8382 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008383gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384{
8385 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008386
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008387 vim_free(beval->msg);
8388 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8389 if (beval->msg == NULL)
8390 {
8391 delete_tooltip(beval);
8392 beval->showState = ShS_NEUTRAL;
8393 return;
8394 }
8395
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 if (beval->showState == ShS_SHOWING)
8397 return;
8398 GetCursorPos(&pt);
8399 ScreenToClient(s_textArea, &pt);
8400
8401 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008403 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 gui_mch_disable_beval_area(cur_beval);
8405 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008406 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409
8410 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008411gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008412 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008413 char_u *mesg,
8414 void (*mesgCB)(BalloonEval *, int),
8415 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008417 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 BalloonEval *beval;
8419
8420 if (mesg != NULL && mesgCB != NULL)
8421 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008422 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 return NULL;
8424 }
8425
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008426 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (beval != NULL)
8428 {
8429 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430
8431 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 beval->msg = mesg;
8433 beval->msgCB = mesgCB;
8434 beval->clientData = clientData;
8435
8436 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 cur_beval = beval;
8438
8439 if (p_beval)
8440 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 }
8442 return beval;
8443}
8444
8445 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008446Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008448 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 return;
8450
8451 if (cur_beval != NULL)
8452 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008453 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008455 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008456 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008457 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 delete_tooltip(cur_beval);
8459 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460
8461 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008462 break;
8463 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008464 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008465 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008466 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008467 info->lpszText = (LPSTR)info->lParam;
8468 info->uFlags |= TTF_DI_SETITEM;
8469 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008470 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008471 case TTN_GETDISPINFOW:
8472 {
8473 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008474 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008475 info->lpszText = (LPWSTR)info->lParam;
8476 info->uFlags |= TTF_DI_SETITEM;
8477 }
8478 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
8480 }
8481}
8482
8483 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008484track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8487 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008488 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489}
8490
8491 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008492gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008494# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008495 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008496# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008497 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 vim_free(beval);
8499}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008500#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501
8502#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8503/*
8504 * We have multiple signs to draw at the same location. Draw the
8505 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8506 */
8507 void
8508netbeans_draw_multisign_indicator(int row)
8509{
8510 int i;
8511 int y;
8512 int x;
8513
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008514 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008515 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 x = 0;
8518 y = TEXT_Y(row);
8519
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008520# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008521 if (IS_ENABLE_DIRECTX())
8522 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008523# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 for (i = 0; i < gui.char_height - 3; i++)
8526 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8527
8528 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8529 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8530 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8531 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8532 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8533 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8534 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8535}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008536#endif