blob: 472cebf733506d8241557851dd57677d7dfb9384 [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;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100275typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100276typedef int OSVERSIONINFO;
277typedef int PWORD;
278typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100279typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100280typedef int UINT;
281typedef int WORD;
282typedef int WPARAM;
283typedef int POINT;
284typedef void *HINSTANCE;
285typedef void *HMENU;
286typedef void *HWND;
287typedef void *HDC;
288typedef void VOID;
289typedef int LPNMHDR;
290typedef int LONG;
291typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200292typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200293typedef int COLORREF;
294typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100295#endif
296
K.Takata45f9cfb2022-01-21 11:11:00 +0000297static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100298static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299static void clear_rect(RECT *rcp);
300
Bram Moolenaar734a8672019-12-02 22:49:38 +0100301static WORD s_dlgfntheight; // height of the dialog font
302static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef FEAT_MENU
305static HMENU s_menuBar = NULL;
306#endif
307#ifdef FEAT_TEAROFF
308static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaar734a8672019-12-02 22:49:38 +0100312// Flag that is set while processing a message that must not be interrupted by
313// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314static int s_busy_processing = FALSE;
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100317
318#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000319static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200322static int s_findrep_is_find; // TRUE for find dialog, FALSE
323 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324#endif
325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
Bram Moolenaar734a8672019-12-02 22:49:38 +0100347static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100348
349static int s_need_activate = FALSE;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351// This variable is set when waiting for an event, which is the only moment
352// scrollbar dragging can be done directly. It's not allowed while commands
353// are executed, because it may move the cursor and that may cause unexpected
354// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100355static int allow_scrollbar = FALSE;
356
K.Takatac81e9bf2022-01-16 14:15:49 +0000357#ifndef _DPI_AWARENESS_CONTEXTS_
358typedef HANDLE DPI_AWARENESS_CONTEXT;
359
360typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000361 DPI_AWARENESS_INVALID = -1,
362 DPI_AWARENESS_UNAWARE = 0,
363 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000364 DPI_AWARENESS_PER_MONITOR_AWARE = 2
365} DPI_AWARENESS;
366
K.Takatab0b2b732022-01-19 12:59:21 +0000367# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
368# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000369# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
370# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
371# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
372#endif
373
374#define DEFAULT_DPI 96
375static int s_dpi = DEFAULT_DPI;
376static BOOL s_in_dpichanged = FALSE;
377static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
378
379static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
380static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
381static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
382//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
383static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
384static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
385
386 static UINT WINAPI
387stubGetDpiForSystem(void)
388{
389 HWND hwnd = GetDesktopWindow();
390 HDC hdc = GetWindowDC(hwnd);
391 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
392 ReleaseDC(hwnd, hdc);
393 return dpi;
394}
395
396 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100397stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000398{
399 return GetSystemMetrics(nIndex);
400}
401
402 static int
403adjust_fontsize_by_dpi(int size)
404{
405 return size * s_dpi / (int)pGetDpiForSystem();
406}
407
408 static int
409adjust_by_system_dpi(int size)
410{
411 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
412}
413
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100414#if defined(FEAT_DIRECTX)
415 static int
416directx_enabled(void)
417{
418 if (s_dwc != NULL)
419 return 1;
420 else if (s_directx_load_attempted)
421 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100422 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100423 DWrite_Init();
424 s_directx_load_attempted = 1;
425 s_dwc = DWriteContext_Open();
426 directx_binddc();
427 return s_dwc != NULL ? 1 : 0;
428}
429
430 static void
431directx_binddc(void)
432{
433 if (s_textArea != NULL)
434 {
435 RECT rect;
436 GetClientRect(s_textArea, &rect);
437 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
438 }
439}
440#endif
441
Bram Moolenaar734a8672019-12-02 22:49:38 +0100442extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100443
444static struct
445{
446 UINT key_sym;
447 char_u vim_code0;
448 char_u vim_code1;
449} special_keys[] =
450{
451 {VK_UP, 'k', 'u'},
452 {VK_DOWN, 'k', 'd'},
453 {VK_LEFT, 'k', 'l'},
454 {VK_RIGHT, 'k', 'r'},
455
456 {VK_F1, 'k', '1'},
457 {VK_F2, 'k', '2'},
458 {VK_F3, 'k', '3'},
459 {VK_F4, 'k', '4'},
460 {VK_F5, 'k', '5'},
461 {VK_F6, 'k', '6'},
462 {VK_F7, 'k', '7'},
463 {VK_F8, 'k', '8'},
464 {VK_F9, 'k', '9'},
465 {VK_F10, 'k', ';'},
466
467 {VK_F11, 'F', '1'},
468 {VK_F12, 'F', '2'},
469 {VK_F13, 'F', '3'},
470 {VK_F14, 'F', '4'},
471 {VK_F15, 'F', '5'},
472 {VK_F16, 'F', '6'},
473 {VK_F17, 'F', '7'},
474 {VK_F18, 'F', '8'},
475 {VK_F19, 'F', '9'},
476 {VK_F20, 'F', 'A'},
477
478 {VK_F21, 'F', 'B'},
479#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100480 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100481#endif
482 {VK_F22, 'F', 'C'},
483 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485
486 {VK_HELP, '%', '1'},
487 {VK_BACK, 'k', 'b'},
488 {VK_INSERT, 'k', 'I'},
489 {VK_DELETE, 'k', 'D'},
490 {VK_HOME, 'k', 'h'},
491 {VK_END, '@', '7'},
492 {VK_PRIOR, 'k', 'P'},
493 {VK_NEXT, 'k', 'N'},
494 {VK_PRINT, '%', '9'},
495 {VK_ADD, 'K', '6'},
496 {VK_SUBTRACT, 'K', '7'},
497 {VK_DIVIDE, 'K', '8'},
498 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_DECIMAL, 'K', 'B'},
501
502 {VK_NUMPAD0, 'K', 'C'},
503 {VK_NUMPAD1, 'K', 'D'},
504 {VK_NUMPAD2, 'K', 'E'},
505 {VK_NUMPAD3, 'K', 'F'},
506 {VK_NUMPAD4, 'K', 'G'},
507 {VK_NUMPAD5, 'K', 'H'},
508 {VK_NUMPAD6, 'K', 'I'},
509 {VK_NUMPAD7, 'K', 'J'},
510 {VK_NUMPAD8, 'K', 'K'},
511 {VK_NUMPAD9, 'K', 'L'},
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100514 {VK_SPACE, ' ', NUL},
515 {VK_TAB, TAB, NUL},
516 {VK_ESCAPE, ESC, NUL},
517 {NL, NL, NUL},
518 {CAR, CAR, NUL},
519
Bram Moolenaar734a8672019-12-02 22:49:38 +0100520 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521 {0, 0, 0}
522};
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525static int s_button_pending = -1;
526
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
528// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_getting_focus = FALSE;
530
531static int s_x_pending;
532static int s_y_pending;
533static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000534static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100535static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200536static int dead_key = 0; // 0: no dead key, 1: dead key pressed
537static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
538 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100540#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100541// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000543static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100544#endif
545
546/*
547 * For control IME.
548 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100549 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100550 */
K.Takata4ac893f2022-01-20 12:44:28 +0000551#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100552// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100554// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100555static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100556#endif
557
558#ifdef FEAT_MBYTE_IME
559static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
560#endif
561
562#if defined(FEAT_BROWSE)
563static char_u *convert_filter(char_u *s);
564#endif
565
566#ifdef DEBUG_PRINT_ERROR
567/*
568 * Print out the last Windows error message
569 */
570 static void
571print_windows_error(void)
572{
573 LPVOID lpMsgBuf;
574
575 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
576 NULL, GetLastError(),
577 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
578 (LPTSTR) &lpMsgBuf, 0, NULL);
579 TRACE1("Error: %s\n", lpMsgBuf);
580 LocalFree(lpMsgBuf);
581}
582#endif
583
584/*
585 * Cursor blink functions.
586 *
587 * This is a simple state machine:
588 * BLINK_NONE not blinking at all
589 * BLINK_OFF blinking, cursor is not shown
590 * BLINK_ON blinking, cursor is shown
591 */
592
593#define BLINK_NONE 0
594#define BLINK_OFF 1
595#define BLINK_ON 2
596
597static int blink_state = BLINK_NONE;
598static long_u blink_waittime = 700;
599static long_u blink_ontime = 400;
600static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000601static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100602
Bram Moolenaar703a8042016-06-04 16:24:32 +0200603 int
604gui_mch_is_blinking(void)
605{
606 return blink_state != BLINK_NONE;
607}
608
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200609 int
610gui_mch_is_blink_off(void)
611{
612 return blink_state == BLINK_OFF;
613}
614
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615 void
616gui_mch_set_blinking(long wait, long on, long off)
617{
618 blink_waittime = wait;
619 blink_ontime = on;
620 blink_offtime = off;
621}
622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100623 static VOID CALLBACK
624_OnBlinkTimer(
625 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100626 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000627 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100628 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100629{
630 MSG msg;
631
632 /*
633 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
634 */
635
636 KillTimer(NULL, idEvent);
637
Bram Moolenaar734a8672019-12-02 22:49:38 +0100638 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000639 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100640 ;
641
642 if (blink_state == BLINK_ON)
643 {
644 gui_undraw_cursor();
645 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000646 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100647 }
648 else
649 {
650 gui_update_cursor(TRUE, FALSE);
651 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000652 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100654 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100655}
656
657 static void
658gui_mswin_rm_blink_timer(void)
659{
660 MSG msg;
661
662 if (blink_timer != 0)
663 {
664 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100665 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000666 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100667 ;
668 blink_timer = 0;
669 }
670}
671
672/*
673 * Stop the cursor blinking. Show the cursor if it wasn't shown.
674 */
675 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100676gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100677{
678 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100679 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100680 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100682 gui_mch_flush();
683 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100684 blink_state = BLINK_NONE;
685}
686
687/*
688 * Start the cursor blinking. If it was already blinking, this restarts the
689 * waiting time and shows the cursor.
690 */
691 void
692gui_mch_start_blink(void)
693{
694 gui_mswin_rm_blink_timer();
695
Bram Moolenaar734a8672019-12-02 22:49:38 +0100696 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100697 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
698 {
K.Takataa8ec4912022-02-03 14:32:33 +0000699 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100700 blink_state = BLINK_ON;
701 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100702 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100703 }
704}
705
706/*
707 * Call-back routines.
708 */
709
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100710 static VOID CALLBACK
711_OnTimer(
712 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100713 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000714 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100715 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716{
717 MSG msg;
718
719 /*
720 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
721 */
722 KillTimer(NULL, idEvent);
723 s_timed_out = TRUE;
724
Bram Moolenaar734a8672019-12-02 22:49:38 +0100725 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000726 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100727 ;
728 if (idEvent == s_wait_timer)
729 s_wait_timer = 0;
730}
731
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100732 static void
733_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100734 HWND hwnd UNUSED,
735 UINT ch UNUSED,
736 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100737{
738 dead_key = 1;
739}
740
741/*
742 * Convert Unicode character "ch" to bytes in "string[slen]".
743 * When "had_alt" is TRUE the ALT key was included in "ch".
744 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200745 * Because the Windows API uses UTF-16, we have to deal with surrogate
746 * pairs; this is where we choose to deal with them: if "ch" is a high
747 * surrogate, it will be stored, and the length returned will be zero; the next
748 * char_to_string call will then include the high surrogate, decoding the pair
749 * of UTF-16 code units to a single Unicode code point, presuming it is the
750 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100751 */
752 static int
753char_to_string(int ch, char_u *string, int slen, int had_alt)
754{
755 int len;
756 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200758 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100759
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200760 if (surrogate_pending_ch != 0)
761 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100762 // We don't guarantee ch is a low surrogate to match the high surrogate
763 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200764 wstring[0] = surrogate_pending_ch;
765 wstring[1] = ch;
766 surrogate_pending_ch = 0;
767 len = 2;
768 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100769 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200770 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100771 // We don't have the entire code point yet, only the first UTF-16 code
772 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200773 surrogate_pending_ch = ch;
774 return 0;
775 }
776 else
777 {
778 wstring[0] = ch;
779 len = 1;
780 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200781
Bram Moolenaar734a8672019-12-02 22:49:38 +0100782 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
783 // "enc_codepage" is non-zero use the standard Win32 function,
784 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100786 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200787 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
788 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100789 // If we had included the ALT key into the character but now the
790 // upper bit is no longer set, that probably means the conversion
791 // failed. Convert the original character and set the upper bit
792 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100794 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200795 wstring[0] = ch & 0x7f;
796 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
797 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100798 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100800 }
801 }
802 else
803 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200804 ws = utf16_to_enc(wstring, &len);
805 if (ws == NULL)
806 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100807 else
808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100809 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200810 len = slen;
811 mch_memmove(string, ws, len);
812 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100813 }
814 }
815
816 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 {
818 string[0] = ch;
819 len = 1;
820 }
821
822 for (i = 0; i < len; ++i)
823 if (string[i] == CSI && len <= slen - 2)
824 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100825 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100826 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
827 string[++i] = KS_EXTRA;
828 string[++i] = (int)KE_CSI;
829 len += 2;
830 }
831
832 return len;
833}
834
LemonBoy45684c62022-04-24 15:46:42 +0100835 static int
836get_active_modifiers(void)
837{
838 int modifiers = 0;
839
840 if (GetKeyState(VK_CONTROL) & 0x8000)
841 modifiers |= MOD_MASK_CTRL;
842 if (GetKeyState(VK_SHIFT) & 0x8000)
843 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100844 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
845 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100846 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100847 if (GetKeyState(VK_LMENU) & 0x8000)
848 modifiers |= MOD_MASK_ALT;
849 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
850 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100851
852 return modifiers;
853}
854
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100855/*
856 * Key hit, add it to the input buffer.
857 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100858 static void
859_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100860 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100861 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100862 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100863{
864 char_u string[40];
865 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100866 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100867 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100868
869 dead_key = 0;
870
LemonBoy45684c62022-04-24 15:46:42 +0100871 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100872
873 ch = simplify_key(ch, &modifiers);
874 // remove the SHIFT modifier for keys where it's already included, e.g.,
875 // '(' and '*'
876 modifiers = may_remove_shift_modifier(modifiers, ch);
877
878 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
879 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
880 if (ch == CSI)
881 ch = K_CSI;
882
883 if (modifiers)
884 {
885 string[0] = CSI;
886 string[1] = KS_MODIFIER;
887 string[2] = modifiers;
888 add_to_input_buf(string, 3);
889 }
890
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100891 len = char_to_string(ch, string, 40, FALSE);
892 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
893 {
894 trash_input_buf();
895 got_int = TRUE;
896 }
897
898 add_to_input_buf(string, len);
899}
900
901/*
902 * Alt-Key hit, add it to the input buffer.
903 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100904 static void
905_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100906 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100908 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100910 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100911 int len;
912 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100913 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100914
915 dead_key = 0;
916
Bram Moolenaar734a8672019-12-02 22:49:38 +0100917 // OK, we have a character key (given by ch) which was entered with the
918 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
919 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
920 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100921 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100922 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100923 // remove the SHIFT modifier for keys where it's already included, e.g.,
924 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200925 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100926
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200927 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
928 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100929 if (ch == CSI)
930 ch = K_CSI;
931
932 len = 0;
933 if (modifiers)
934 {
935 string[len++] = CSI;
936 string[len++] = KS_MODIFIER;
937 string[len++] = modifiers;
938 }
939
940 if (IS_SPECIAL((int)ch))
941 {
942 string[len++] = CSI;
943 string[len++] = K_SECOND((int)ch);
944 string[len++] = K_THIRD((int)ch);
945 }
946 else
947 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100948 // Although the documentation isn't clear about it, we assume "ch" is
949 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950 len += char_to_string(ch, string + len, 40 - len, TRUE);
951 }
952
953 add_to_input_buf(string, len);
954}
955
956 static void
957_OnMouseEvent(
958 int button,
959 int x,
960 int y,
961 int repeated_click,
962 UINT keyFlags)
963{
964 int vim_modifiers = 0x0;
965
966 s_getting_focus = FALSE;
967
968 if (keyFlags & MK_SHIFT)
969 vim_modifiers |= MOUSE_SHIFT;
970 if (keyFlags & MK_CONTROL)
971 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100972 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100973 vim_modifiers |= MOUSE_ALT;
974
975 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
976}
977
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100978 static void
979_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100980 HWND hwnd UNUSED,
981 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100982 int x,
983 int y,
984 UINT keyFlags)
985{
986 static LONG s_prevTime = 0;
987
988 LONG currentTime = GetMessageTime();
989 int button = -1;
990 int repeated_click;
991
Bram Moolenaar734a8672019-12-02 22:49:38 +0100992 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100993 (void)SetFocus(s_hwnd);
994
995 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
996 button = MOUSE_LEFT;
997 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
998 button = MOUSE_MIDDLE;
999 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1000 button = MOUSE_RIGHT;
1001 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1002 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001003 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1004 }
1005 else if (s_uMsg == WM_CAPTURECHANGED)
1006 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001007 // on W95/NT4, somehow you get in here with an odd Msg
1008 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001009 if (s_button_pending == MOUSE_LEFT)
1010 button = MOUSE_RIGHT;
1011 else
1012 button = MOUSE_LEFT;
1013 }
1014 if (button >= 0)
1015 {
1016 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1017
1018 /*
1019 * Holding down the left and right buttons simulates pushing the middle
1020 * button.
1021 */
1022 if (repeated_click
1023 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1024 || (button == MOUSE_RIGHT
1025 && s_button_pending == MOUSE_LEFT)))
1026 {
1027 /*
1028 * Hmm, gui.c will ignore more than one button down at a time, so
1029 * pretend we let go of it first.
1030 */
1031 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1032 button = MOUSE_MIDDLE;
1033 repeated_click = FALSE;
1034 s_button_pending = -1;
1035 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1036 }
1037 else if ((repeated_click)
1038 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1039 {
1040 if (s_button_pending > -1)
1041 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001042 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1043 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001044 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001045 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001046 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1047 }
1048 else
1049 {
1050 /*
1051 * If this is the first press (i.e. not a multiple click) don't
1052 * action immediately, but store and wait for:
1053 * i) button-up
1054 * ii) mouse move
1055 * iii) another button press
1056 * before using it.
1057 * This enables us to make left+right simulate middle button,
1058 * without left or right being actioned first. The side-effect is
1059 * that if you click and hold the mouse without dragging, the
1060 * cursor doesn't move until you release the button. In practice
1061 * this is hardly a problem.
1062 */
1063 s_button_pending = button;
1064 s_x_pending = x;
1065 s_y_pending = y;
1066 s_kFlags_pending = keyFlags;
1067 }
1068
1069 s_prevTime = currentTime;
1070 }
1071}
1072
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001073 static void
1074_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001075 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001076 int x,
1077 int y,
1078 UINT keyFlags)
1079{
1080 int button;
1081
1082 s_getting_focus = FALSE;
1083 if (s_button_pending > -1)
1084 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001085 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001086 _OnMouseEvent(s_button_pending, s_x_pending,
1087 s_y_pending, FALSE, s_kFlags_pending);
1088 s_button_pending = -1;
1089 }
1090 if (s_uMsg == WM_MOUSEMOVE)
1091 {
1092 /*
1093 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1094 * down.
1095 */
1096 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1097 | MK_XBUTTON1 | MK_XBUTTON2)))
1098 {
1099 gui_mouse_moved(x, y);
1100 return;
1101 }
1102
1103 /*
1104 * While button is down, keep grabbing mouse move events when
1105 * the mouse goes outside the window
1106 */
1107 SetCapture(s_textArea);
1108 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001109 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001110 }
1111 else
1112 {
1113 ReleaseCapture();
1114 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001115 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 }
1117
1118 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1119}
1120
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001121 static void
1122_OnSizeTextArea(
1123 HWND hwnd UNUSED,
1124 UINT state UNUSED,
1125 int cx UNUSED,
1126 int cy UNUSED)
1127{
1128#if defined(FEAT_DIRECTX)
1129 if (IS_ENABLE_DIRECTX())
1130 directx_binddc();
1131#endif
1132}
1133
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001134#ifdef FEAT_MENU
1135/*
1136 * Find the vimmenu_T with the given id
1137 */
1138 static vimmenu_T *
1139gui_mswin_find_menu(
1140 vimmenu_T *pMenu,
1141 int id)
1142{
1143 vimmenu_T *pChildMenu;
1144
1145 while (pMenu)
1146 {
1147 if (pMenu->id == (UINT)id)
1148 break;
1149 if (pMenu->children != NULL)
1150 {
1151 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1152 if (pChildMenu)
1153 {
1154 pMenu = pChildMenu;
1155 break;
1156 }
1157 }
1158 pMenu = pMenu->next;
1159 }
1160 return pMenu;
1161}
1162
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001163 static void
1164_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001165 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001166 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001167 HWND hwndCtl UNUSED,
1168 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001169{
1170 vimmenu_T *pMenu;
1171
1172 pMenu = gui_mswin_find_menu(root_menu, id);
1173 if (pMenu)
1174 gui_menu_cb(pMenu);
1175}
1176#endif
1177
1178#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001179/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001180 * Handle a Find/Replace window message.
1181 */
1182 static void
1183_OnFindRepl(void)
1184{
1185 int flags = 0;
1186 int down;
1187
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001188 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001189 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 (void)SetFocus(s_hwnd);
1191
1192 if (s_findrep_struct.Flags & FR_FINDNEXT)
1193 {
1194 flags = FRD_FINDNEXT;
1195
Bram Moolenaar734a8672019-12-02 22:49:38 +01001196 // Give main window the focus back: this is so the cursor isn't
1197 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198 (void)SetFocus(s_hwnd);
1199 }
1200 else if (s_findrep_struct.Flags & FR_REPLACE)
1201 {
1202 flags = FRD_REPLACE;
1203
Bram Moolenaar734a8672019-12-02 22:49:38 +01001204 // Give main window the focus back: this is so the cursor isn't
1205 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001206 (void)SetFocus(s_hwnd);
1207 }
1208 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1209 {
1210 flags = FRD_REPLACEALL;
1211 }
1212
1213 if (flags != 0)
1214 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001215 char_u *p, *q;
1216
Bram Moolenaar734a8672019-12-02 22:49:38 +01001217 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001218 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1219 flags |= FRD_WHOLE_WORD;
1220 if (s_findrep_struct.Flags & FR_MATCHCASE)
1221 flags |= FRD_MATCH_CASE;
1222 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001223 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1224 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1225 if (p != NULL && q != NULL)
1226 gui_do_findrepl(flags, p, q, down);
1227 vim_free(p);
1228 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001229 }
1230}
1231#endif
1232
1233 static void
1234HandleMouseHide(UINT uMsg, LPARAM lParam)
1235{
1236 static LPARAM last_lParam = 0L;
1237
Bram Moolenaar734a8672019-12-02 22:49:38 +01001238 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001239 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1240 {
1241 if (lParam == last_lParam)
1242 return;
1243 last_lParam = lParam;
1244 }
1245
Bram Moolenaar734a8672019-12-02 22:49:38 +01001246 // Handle specially, to centralise coding. We need to be sure we catch all
1247 // possible events which should cause us to restore the cursor (as it is a
1248 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249 switch (uMsg)
1250 {
1251 case WM_KEYUP:
1252 case WM_CHAR:
1253 /*
1254 * blank out the pointer if necessary
1255 */
1256 if (p_mh)
1257 gui_mch_mousehide(TRUE);
1258 break;
1259
Bram Moolenaar734a8672019-12-02 22:49:38 +01001260 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001262 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263 case WM_LBUTTONDOWN:
1264 case WM_LBUTTONUP:
1265 case WM_MBUTTONDOWN:
1266 case WM_MBUTTONUP:
1267 case WM_RBUTTONDOWN:
1268 case WM_RBUTTONUP:
1269 case WM_XBUTTONDOWN:
1270 case WM_XBUTTONUP:
1271 case WM_NCMOUSEMOVE:
1272 case WM_NCLBUTTONDOWN:
1273 case WM_NCLBUTTONUP:
1274 case WM_NCMBUTTONDOWN:
1275 case WM_NCMBUTTONUP:
1276 case WM_NCRBUTTONDOWN:
1277 case WM_NCRBUTTONUP:
1278 case WM_KILLFOCUS:
1279 /*
1280 * if the pointer is currently hidden, then we should show it.
1281 */
1282 gui_mch_mousehide(FALSE);
1283 break;
1284 }
1285}
1286
1287 static LRESULT CALLBACK
1288_TextAreaWndProc(
1289 HWND hwnd,
1290 UINT uMsg,
1291 WPARAM wParam,
1292 LPARAM lParam)
1293{
1294 /*
1295 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1296 hwnd, uMsg, wParam, lParam);
1297 */
1298
1299 HandleMouseHide(uMsg, lParam);
1300
1301 s_uMsg = uMsg;
1302 s_wParam = wParam;
1303 s_lParam = lParam;
1304
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001305#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001306 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001307#endif
1308
1309 switch (uMsg)
1310 {
1311 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1312 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1313 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1314 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1315 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1316 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1317 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1318 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1319 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1320 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1321 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1322 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1323 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1324 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001325 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001326
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001327#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001328 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1329 return TRUE;
1330#endif
1331 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001332 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001333 }
1334}
1335
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001336/*
1337 * Called when the foreground or background color has been changed.
1338 */
1339 void
1340gui_mch_new_colors(void)
1341{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001342 HBRUSH prevBrush;
1343
1344 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001345 prevBrush = (HBRUSH)SetClassLongPtr(
1346 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001347 InvalidateRect(s_hwnd, NULL, TRUE);
1348 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001349}
1350
1351/*
1352 * Set the colors to their default values.
1353 */
1354 void
1355gui_mch_def_colors(void)
1356{
1357 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1358 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1359 gui.def_norm_pixel = gui.norm_pixel;
1360 gui.def_back_pixel = gui.back_pixel;
1361}
1362
1363/*
1364 * Open the GUI window which was created by a call to gui_mch_init().
1365 */
1366 int
1367gui_mch_open(void)
1368{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001369 // Actually open the window, if not already visible
1370 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001371 if (!IsWindowVisible(s_hwnd))
1372 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1373
1374#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001375 // Init replace string here, so that we keep it when re-opening the
1376 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001377 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1378#endif
1379
1380 return OK;
1381}
1382
1383/*
1384 * Get the position of the top left corner of the window.
1385 */
1386 int
1387gui_mch_get_winpos(int *x, int *y)
1388{
1389 RECT rect;
1390
1391 GetWindowRect(s_hwnd, &rect);
1392 *x = rect.left;
1393 *y = rect.top;
1394 return OK;
1395}
1396
1397/*
1398 * Set the position of the top left corner of the window to the given
1399 * coordinates.
1400 */
1401 void
1402gui_mch_set_winpos(int x, int y)
1403{
1404 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1405 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1406}
1407 void
1408gui_mch_set_text_area_pos(int x, int y, int w, int h)
1409{
1410 static int oldx = 0;
1411 static int oldy = 0;
1412
1413 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1414
1415#ifdef FEAT_TOOLBAR
1416 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1417 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001418 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001419#endif
1420#if defined(FEAT_GUI_TABLINE)
1421 if (showing_tabline)
1422 {
1423 int top = 0;
1424 RECT rect;
1425
1426# ifdef FEAT_TOOLBAR
1427 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001428 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001429# endif
1430 GetClientRect(s_hwnd, &rect);
1431 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1432 }
1433#endif
1434
Bram Moolenaar734a8672019-12-02 22:49:38 +01001435 // When side scroll bar is unshown, the size of window will change.
1436 // then, the text area move left or right. thus client rect should be
1437 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001438 if (oldx != x || oldy != y)
1439 {
1440 InvalidateRect(s_hwnd, NULL, FALSE);
1441 oldx = x;
1442 oldy = y;
1443 }
1444}
1445
1446
1447/*
1448 * Scrollbar stuff:
1449 */
1450
1451 void
1452gui_mch_enable_scrollbar(
1453 scrollbar_T *sb,
1454 int flag)
1455{
1456 ShowScrollBar(sb->id, SB_CTL, flag);
1457
Bram Moolenaar734a8672019-12-02 22:49:38 +01001458 // TODO: When the window is maximized, the size of the window stays the
1459 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1460 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001461}
1462
1463 void
1464gui_mch_set_scrollbar_pos(
1465 scrollbar_T *sb,
1466 int x,
1467 int y,
1468 int w,
1469 int h)
1470{
1471 SetWindowPos(sb->id, NULL, x, y, w, h,
1472 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1473}
1474
Bram Moolenaar203ec772020-07-17 20:43:43 +02001475 int
1476gui_mch_get_scrollbar_xpadding(void)
1477{
1478 RECT rcTxt, rcWnd;
1479 int xpad;
1480
1481 GetWindowRect(s_textArea, &rcTxt);
1482 GetWindowRect(s_hwnd, &rcWnd);
1483 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001484 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1485 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001486 return (xpad < 0) ? 0 : xpad;
1487}
1488
1489 int
1490gui_mch_get_scrollbar_ypadding(void)
1491{
1492 RECT rcTxt, rcWnd;
1493 int ypad;
1494
1495 GetWindowRect(s_textArea, &rcTxt);
1496 GetWindowRect(s_hwnd, &rcWnd);
1497 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001498 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1499 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001500 return (ypad < 0) ? 0 : ypad;
1501}
1502
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001503 void
1504gui_mch_create_scrollbar(
1505 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001506 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001507{
1508 sb->id = CreateWindow(
1509 "SCROLLBAR", "Scrollbar",
1510 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001511 10, // Any value will do for now
1512 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001514 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515}
1516
1517/*
1518 * Find the scrollbar with the given hwnd.
1519 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001520 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001521gui_mswin_find_scrollbar(HWND hwnd)
1522{
1523 win_T *wp;
1524
1525 if (gui.bottom_sbar.id == hwnd)
1526 return &gui.bottom_sbar;
1527 FOR_ALL_WINDOWS(wp)
1528 {
1529 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1530 return &wp->w_scrollbars[SBAR_LEFT];
1531 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1532 return &wp->w_scrollbars[SBAR_RIGHT];
1533 }
1534 return NULL;
1535}
1536
K.Takatac81e9bf2022-01-16 14:15:49 +00001537 static void
1538update_scrollbar_size(void)
1539{
1540 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1541 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1542}
1543
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001544/*
K.Takataabe628e2022-01-23 16:25:17 +00001545 * Get the average character size of a font.
1546 */
1547 static void
1548GetAverageFontSize(HDC hdc, SIZE *size)
1549{
1550 // GetTextMetrics() may not return the right value in tmAveCharWidth
1551 // for some fonts. Do our own average computation.
1552 GetTextExtentPoint(hdc,
1553 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1554 52, size);
1555 size->cx = (size->cx / 26 + 1) / 2;
1556}
1557
1558/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559 * Get the character size of a font.
1560 */
1561 static void
1562GetFontSize(GuiFont font)
1563{
1564 HWND hwnd = GetDesktopWindow();
1565 HDC hdc = GetWindowDC(hwnd);
1566 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001567 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568 TEXTMETRIC tm;
1569
1570 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001571 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001572
K.Takataabe628e2022-01-23 16:25:17 +00001573 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001574 gui.char_height = tm.tmHeight + p_linespace;
1575
1576 SelectFont(hdc, hfntOld);
1577
1578 ReleaseDC(hwnd, hdc);
1579}
1580
1581/*
1582 * Adjust gui.char_height (after 'linespace' was changed).
1583 */
1584 int
1585gui_mch_adjust_charheight(void)
1586{
1587 GetFontSize(gui.norm_font);
1588 return OK;
1589}
1590
1591 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001592get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001593{
1594 HFONT font = NULL;
1595
Bram Moolenaar734a8672019-12-02 22:49:38 +01001596 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001597 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001598
1599 if (font == NULL)
1600 return NOFONT;
1601
1602 return (GuiFont)font;
1603}
1604
1605 static int
1606pixels_to_points(int pixels, int vertical)
1607{
1608 int points;
1609 HWND hwnd;
1610 HDC hdc;
1611
1612 hwnd = GetDesktopWindow();
1613 hdc = GetWindowDC(hwnd);
1614
1615 points = MulDiv(pixels, 72,
1616 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1617
1618 ReleaseDC(hwnd, hdc);
1619
1620 return points;
1621}
1622
1623 GuiFont
1624gui_mch_get_font(
1625 char_u *name,
1626 int giveErrorIfMissing)
1627{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001628 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001629 GuiFont font = NOFONT;
1630
1631 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001632 {
1633 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001635 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001637 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001638 return font;
1639}
1640
1641#if defined(FEAT_EVAL) || defined(PROTO)
1642/*
1643 * Return the name of font "font" in allocated memory.
1644 * Don't know how to get the actual name, thus use the provided name.
1645 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001647gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648{
1649 if (name == NULL)
1650 return NULL;
1651 return vim_strsave(name);
1652}
1653#endif
1654
1655 void
1656gui_mch_free_font(GuiFont font)
1657{
1658 if (font)
1659 DeleteObject((HFONT)font);
1660}
1661
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001662/*
1663 * Return the Pixel value (color) for the given color name.
1664 * Return INVALCOLOR for error.
1665 */
1666 guicolor_T
1667gui_mch_get_color(char_u *name)
1668{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001669 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001670
1671 typedef struct SysColorTable
1672 {
1673 char *name;
1674 int color;
1675 } SysColorTable;
1676
1677 static SysColorTable sys_table[] =
1678 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001679 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1680 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001681#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001682 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1683#endif
1684 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1685 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1686 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1687 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1688 {"SYS_DESKTOP", COLOR_DESKTOP},
1689 {"SYS_INFOBK", COLOR_INFOBK},
1690 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1691 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692 {"SYS_BTNFACE", COLOR_BTNFACE},
1693 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1694 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1695 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1696 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1697 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1698 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1699 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1700 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1701 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1702 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1703 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1704 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1705 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1706 {"SYS_MENU", COLOR_MENU},
1707 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1708 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1709 {"SYS_WINDOW", COLOR_WINDOW},
1710 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1711 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1712 };
1713
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001714 /*
1715 * Try to look up a system colour.
1716 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001717 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001718 if (STRICMP(name, sys_table[i].name) == 0)
1719 return GetSysColor(sys_table[i].color);
1720
Bram Moolenaarab302212016-04-26 20:59:29 +02001721 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001722}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001723
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001724 guicolor_T
1725gui_mch_get_rgb_color(int r, int g, int b)
1726{
1727 return gui_get_rgb_color_cmn(r, g, b);
1728}
1729
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001730/*
1731 * Return OK if the key with the termcap name "name" is supported.
1732 */
1733 int
1734gui_mch_haskey(char_u *name)
1735{
1736 int i;
1737
1738 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001739 if (name[0] == special_keys[i].vim_code0
1740 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001741 return OK;
1742 return FAIL;
1743}
1744
1745 void
1746gui_mch_beep(void)
1747{
LemonBoy77771d32022-04-13 11:47:25 +01001748 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001749}
1750/*
1751 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1752 */
1753 void
1754gui_mch_invert_rectangle(
1755 int r,
1756 int c,
1757 int nr,
1758 int nc)
1759{
1760 RECT rc;
1761
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001762#if defined(FEAT_DIRECTX)
1763 if (IS_ENABLE_DIRECTX())
1764 DWriteContext_Flush(s_dwc);
1765#endif
1766
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001767 /*
1768 * Note: InvertRect() excludes right and bottom of rectangle.
1769 */
1770 rc.left = FILL_X(c);
1771 rc.top = FILL_Y(r);
1772 rc.right = rc.left + nc * gui.char_width;
1773 rc.bottom = rc.top + nr * gui.char_height;
1774 InvertRect(s_hdc, &rc);
1775}
1776
1777/*
1778 * Iconify the GUI window.
1779 */
1780 void
1781gui_mch_iconify(void)
1782{
1783 ShowWindow(s_hwnd, SW_MINIMIZE);
1784}
1785
1786/*
1787 * Draw a cursor without focus.
1788 */
1789 void
1790gui_mch_draw_hollow_cursor(guicolor_T color)
1791{
1792 HBRUSH hbr;
1793 RECT rc;
1794
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001795#if defined(FEAT_DIRECTX)
1796 if (IS_ENABLE_DIRECTX())
1797 DWriteContext_Flush(s_dwc);
1798#endif
1799
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800 /*
1801 * Note: FrameRect() excludes right and bottom of rectangle.
1802 */
1803 rc.left = FILL_X(gui.col);
1804 rc.top = FILL_Y(gui.row);
1805 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001806 if (mb_lefthalve(gui.row, gui.col))
1807 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001808 rc.bottom = rc.top + gui.char_height;
1809 hbr = CreateSolidBrush(color);
1810 FrameRect(s_hdc, &rc, hbr);
1811 DeleteBrush(hbr);
1812}
1813/*
1814 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1815 * color "color".
1816 */
1817 void
1818gui_mch_draw_part_cursor(
1819 int w,
1820 int h,
1821 guicolor_T color)
1822{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 RECT rc;
1824
1825 /*
1826 * Note: FillRect() excludes right and bottom of rectangle.
1827 */
1828 rc.left =
1829#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001830 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001831 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1832#endif
1833 FILL_X(gui.col);
1834 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1835 rc.right = rc.left + w;
1836 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001837
Bram Moolenaar92467d32017-12-05 13:22:16 +01001838 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001839}
1840
1841
1842/*
1843 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1844 * dead key's nominal character and re-post the original message.
1845 */
1846 static void
1847outputDeadKey_rePost(MSG originalMsg)
1848{
1849 static MSG deadCharExpel;
1850
1851 if (!dead_key)
1852 return;
1853
1854 dead_key = 0;
1855
Bram Moolenaar734a8672019-12-02 22:49:38 +01001856 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001857 deadCharExpel.message = originalMsg.message;
1858 deadCharExpel.hwnd = originalMsg.hwnd;
1859 deadCharExpel.wParam = VK_SPACE;
1860
K.Takata4ac893f2022-01-20 12:44:28 +00001861 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1865 originalMsg.lParam);
1866}
1867
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001868/*
1869 * Process a single Windows message.
1870 * If one is not available we hang until one is.
1871 */
1872 static void
1873process_message(void)
1874{
1875 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001876 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001877 char_u string[40];
1878 int i;
1879 int modifiers = 0;
1880 int key;
1881#ifdef FEAT_MENU
1882 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1883#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001884 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885
K.Takatab7057bd2022-01-21 11:37:07 +00001886 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001887
1888#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001889 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001890 if (msg.message == WM_OLE)
1891 {
1892 char_u *str = (char_u *)msg.lParam;
1893 if (str == NULL || *str == NUL)
1894 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001895 // Message can't be ours, forward it. Fixes problem with Ultramon
1896 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001897 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898 }
1899 else
1900 {
1901 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001902 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001903 }
1904 return;
1905 }
1906#endif
1907
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001909 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001910 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001911 {
1912 HandleMouseHide(msg.message, msg.lParam);
1913 return;
1914 }
1915#endif
1916
1917 /*
1918 * Check if it's a special key that we recognise. If not, call
1919 * TranslateMessage().
1920 */
1921 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1922 {
1923 vk = (int) msg.wParam;
1924
1925 /*
1926 * Handle dead keys in special conditions in other cases we let Windows
1927 * handle them and do not interfere.
1928 *
1929 * The dead_key flag must be reset on several occasions:
1930 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1931 * consumed at that point (This is when we let Windows combine the
1932 * dead character on its own)
1933 *
1934 * - Before doing something special such as regenerating keypresses to
1935 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001936 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001937 * immediately to _OnChar() (or _OnSysChar()).
1938 */
1939 if (dead_key)
1940 {
1941 /*
1942 * If a dead key was pressed and the user presses VK_SPACE,
1943 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1944 * with the dead char now, so do nothing special and let Windows
1945 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01001946 *
1947 * Note that VK_SPACE combines with the dead_key's character and
1948 * only one WM_CHAR will be generated by TranslateMessage(), in
1949 * the two other cases two WM_CHAR will be generated: the dead
1950 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1951 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001952 */
1953 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1954 {
1955 dead_key = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001956 TranslateMessage(&msg);
1957 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001958 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001959 // In modes where we are not typing, dead keys should behave
1960 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01001961 else if ((get_real_state()
1962 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001963 {
1964 outputDeadKey_rePost(msg);
1965 return;
1966 }
1967 }
1968
Bram Moolenaar734a8672019-12-02 22:49:38 +01001969 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001970 if (vk == VK_CANCEL)
1971 {
1972 trash_input_buf();
1973 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001974 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001975 string[0] = Ctrl_C;
1976 add_to_input_buf(string, 1);
1977 }
1978
LemonBoy77fc0b02022-04-22 22:45:52 +01001979 // This is an IME event or a synthetic keystroke, let Windows handle it.
1980 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1981 {
1982 TranslateMessage(&msg);
1983 return;
1984 }
1985
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001986 for (i = 0; special_keys[i].key_sym != 0; i++)
1987 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001988 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001989 if (special_keys[i].key_sym == vk
LemonBoy202b4bd2022-04-28 19:50:54 +01001990 && (vk != VK_SPACE || !(GetKeyState(VK_LMENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001991 {
1992 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001993 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001994 * is a key that would normally trigger the dead key nominal
1995 * character output (such as a NUMPAD printable character or
1996 * the TAB key, etc...).
1997 */
1998 if (dead_key && (special_keys[i].vim_code0 == 'K'
1999 || vk == VK_TAB || vk == CAR))
2000 {
2001 outputDeadKey_rePost(msg);
2002 return;
2003 }
2004
2005#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002006 // Check for <F10>: Windows selects the menu. When <F10> is
2007 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002008 if (vk == VK_F10
2009 && gui.menu_is_active
2010 && check_map(k10, State, FALSE, TRUE, FALSE,
2011 NULL, NULL) == NULL)
2012 break;
2013#endif
LemonBoy45684c62022-04-24 15:46:42 +01002014 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002015
2016 if (special_keys[i].vim_code1 == NUL)
2017 key = special_keys[i].vim_code0;
2018 else
2019 key = TO_SPECIAL(special_keys[i].vim_code0,
2020 special_keys[i].vim_code1);
2021 key = simplify_key(key, &modifiers);
2022 if (key == CSI)
2023 key = K_CSI;
2024
2025 if (modifiers)
2026 {
2027 string[0] = CSI;
2028 string[1] = KS_MODIFIER;
2029 string[2] = modifiers;
2030 add_to_input_buf(string, 3);
2031 }
2032
2033 if (IS_SPECIAL(key))
2034 {
2035 string[0] = CSI;
2036 string[1] = K_SECOND(key);
2037 string[2] = K_THIRD(key);
2038 add_to_input_buf(string, 3);
2039 }
2040 else
2041 {
2042 int len;
2043
Bram Moolenaar734a8672019-12-02 22:49:38 +01002044 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002045 len = char_to_string(key, string, 40, FALSE);
2046 add_to_input_buf(string, len);
2047 }
2048 break;
2049 }
2050 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002051
2052 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002053 if (special_keys[i].key_sym == 0)
2054 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002055 WCHAR ch[8];
2056 int len;
2057 int i;
2058 UINT scan_code;
2059
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002060 // Construct the state table with only a few modifiers, we don't
2061 // really care about the presence of Ctrl/Alt as those modifiers are
2062 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002063 memset(keyboard_state, 0, 256);
2064 if (GetKeyState(VK_SHIFT) & 0x8000)
2065 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002066 if (GetKeyState(VK_CAPITAL) & 0x0001)
2067 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002068 // Alt-Gr is synthesized as Alt + Ctrl.
2069 if ((GetKeyState(VK_RMENU) & 0x8000)
2070 && (GetKeyState(VK_CONTROL) & 0x8000))
2071 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002072 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002073 keyboard_state[VK_CONTROL] = 0x80;
2074 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002075
2076 // Translate the virtual key according to the current keyboard
2077 // layout.
2078 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2079 // Convert the scan-code into a sequence of zero or more unicode
2080 // codepoints.
2081 // If this is a dead key ToUnicode returns a negative value.
2082 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2083 0);
2084 dead_key = len < 0;
2085
2086 if (len <= 0)
2087 return;
2088
2089 // Post the message as TranslateMessage would do.
2090 if (msg.message == WM_KEYDOWN)
2091 {
2092 for (i = 0; i < len; i++)
2093 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094 }
2095 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002096 {
2097 for (i = 0; i < len; i++)
2098 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2099 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002100 }
2101 }
2102#ifdef FEAT_MBYTE_IME
2103 else if (msg.message == WM_IME_NOTIFY)
2104 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2105 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002106 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002107 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108#endif
2109
2110#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002111 // Check for <F10>: Default effect is to select the menu. When <F10> is
2112 // mapped we need to stop it here to avoid strange effects (e.g., for the
2113 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2115 NULL, NULL) == NULL)
2116#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002117 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002118}
2119
2120/*
2121 * Catch up with any queued events. This may put keyboard input into the
2122 * input buffer, call resize call-backs, trigger timers etc. If there is
2123 * nothing in the event queue (& no timers pending), then we return
2124 * immediately.
2125 */
2126 void
2127gui_mch_update(void)
2128{
2129 MSG msg;
2130
2131 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002132 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133 && !vim_is_input_buf_full())
2134 process_message();
2135}
2136
Bram Moolenaar4231da42016-06-02 14:30:04 +02002137 static void
2138remove_any_timer(void)
2139{
2140 MSG msg;
2141
2142 if (s_wait_timer != 0 && !s_timed_out)
2143 {
2144 KillTimer(NULL, s_wait_timer);
2145
Bram Moolenaar734a8672019-12-02 22:49:38 +01002146 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002147 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002148 ;
2149 s_wait_timer = 0;
2150 }
2151}
2152
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153/*
2154 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2155 * from the keyboard.
2156 * wtime == -1 Wait forever.
2157 * wtime == 0 This should never happen.
2158 * wtime > 0 Wait wtime milliseconds for a character.
2159 * Returns OK if a character was found to be available within the given time,
2160 * or FAIL otherwise.
2161 */
2162 int
2163gui_mch_wait_for_chars(int wtime)
2164{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002165 int focus;
2166
2167 s_timed_out = FALSE;
2168
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002169 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002170 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002171 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002172 if (s_busy_processing)
2173 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002174
2175 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002176 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2177 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002178 }
2179
2180 allow_scrollbar = TRUE;
2181
2182 focus = gui.in_focus;
2183 while (!s_timed_out)
2184 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002185 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186 if (gui.in_focus != focus)
2187 {
2188 if (gui.in_focus)
2189 gui_mch_start_blink();
2190 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002191 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002192 focus = gui.in_focus;
2193 }
2194
2195 if (s_need_activate)
2196 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002197 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002198 s_need_activate = FALSE;
2199 }
2200
Bram Moolenaar4231da42016-06-02 14:30:04 +02002201#ifdef FEAT_TIMERS
2202 did_add_timer = FALSE;
2203#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002204#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002205 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002206 for (;;)
2207 {
2208 MSG msg;
2209
2210 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002211# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002212 if (did_add_timer)
2213 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002214# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002215 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002216 {
2217 process_message();
2218 break;
2219 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002220 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002221 // TODO: The 10 msec is a compromise between laggy response
2222 // and consuming more CPU time. Better would be to handle
2223 // channel messages when they arrive.
2224 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002225 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002226 break;
2227 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002228#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002229 // Don't use gui_mch_update() because then we will spin-lock until a
2230 // char arrives, instead we use GetMessage() to hang until an
2231 // event arrives. No need to check for input_buf_full because we are
2232 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002234#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002235
2236 if (input_available())
2237 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002238 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002239 allow_scrollbar = FALSE;
2240
Bram Moolenaar89c00032019-09-04 13:53:21 +02002241 // Clear pending mouse button, the release event may have been
2242 // taken by the dialog window. But don't do this when getting
2243 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002244 if (!s_getting_focus)
2245 s_button_pending = -1;
2246
2247 return OK;
2248 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002249
2250#ifdef FEAT_TIMERS
2251 if (did_add_timer)
2252 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002253 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002254 remove_any_timer();
2255 break;
2256 }
2257#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002258 }
2259 allow_scrollbar = FALSE;
2260 return FAIL;
2261}
2262
2263/*
2264 * Clear a rectangular region of the screen from text pos (row1, col1) to
2265 * (row2, col2) inclusive.
2266 */
2267 void
2268gui_mch_clear_block(
2269 int row1,
2270 int col1,
2271 int row2,
2272 int col2)
2273{
2274 RECT rc;
2275
2276 /*
2277 * Clear one extra pixel at the far right, for when bold characters have
2278 * spilled over to the window border.
2279 * Note: FillRect() excludes right and bottom of rectangle.
2280 */
2281 rc.left = FILL_X(col1);
2282 rc.top = FILL_Y(row1);
2283 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2284 rc.bottom = FILL_Y(row2 + 1);
2285 clear_rect(&rc);
2286}
2287
2288/*
2289 * Clear the whole text window.
2290 */
2291 void
2292gui_mch_clear_all(void)
2293{
2294 RECT rc;
2295
2296 rc.left = 0;
2297 rc.top = 0;
2298 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2299 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2300 clear_rect(&rc);
2301}
2302/*
2303 * Menu stuff.
2304 */
2305
2306 void
2307gui_mch_enable_menu(int flag)
2308{
2309#ifdef FEAT_MENU
2310 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2311#endif
2312}
2313
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314 void
2315gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002316 int x UNUSED,
2317 int y UNUSED,
2318 int w UNUSED,
2319 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002321 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002322}
2323
2324#if defined(FEAT_MENU) || defined(PROTO)
2325/*
2326 * Make menu item hidden or not hidden
2327 */
2328 void
2329gui_mch_menu_hidden(
2330 vimmenu_T *menu,
2331 int hidden)
2332{
2333 /*
2334 * This doesn't do what we want. Hmm, just grey the menu items for now.
2335 */
2336 /*
2337 if (hidden)
2338 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2339 else
2340 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2341 */
2342 gui_mch_menu_grey(menu, hidden);
2343}
2344
2345/*
2346 * This is called after setting all the menus to grey/hidden or not.
2347 */
2348 void
2349gui_mch_draw_menubar(void)
2350{
2351 DrawMenuBar(s_hwnd);
2352}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002353#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002354
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355/*
2356 * Return the RGB value of a pixel as a long.
2357 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002358 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359gui_mch_get_rgb(guicolor_T pixel)
2360{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002361 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2362 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002363}
2364
2365#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002366/*
2367 * Convert pixels in X to dialog units
2368 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 static WORD
2370PixelToDialogX(int numPixels)
2371{
2372 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2373}
2374
Bram Moolenaar734a8672019-12-02 22:49:38 +01002375/*
2376 * Convert pixels in Y to dialog units
2377 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378 static WORD
2379PixelToDialogY(int numPixels)
2380{
2381 return (WORD)((numPixels * 8) / s_dlgfntheight);
2382}
2383
Bram Moolenaar734a8672019-12-02 22:49:38 +01002384/*
2385 * Return the width in pixels of the given text in the given DC.
2386 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 static int
2388GetTextWidth(HDC hdc, char_u *str, int len)
2389{
2390 SIZE size;
2391
2392 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2393 return size.cx;
2394}
2395
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396/*
2397 * Return the width in pixels of the given text in the given DC, taking care
2398 * of 'encoding' to active codepage conversion.
2399 */
2400 static int
2401GetTextWidthEnc(HDC hdc, char_u *str, int len)
2402{
2403 SIZE size;
2404 WCHAR *wstr;
2405 int n;
2406 int wlen = len;
2407
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002408 wstr = enc_to_utf16(str, &wlen);
2409 if (wstr == NULL)
2410 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002411
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002412 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2413 vim_free(wstr);
2414 if (n)
2415 return size.cx;
2416 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002417}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002418
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002419static void get_work_area(RECT *spi_rect);
2420
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002421/*
2422 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002423 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2424 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002425 */
2426 static BOOL
2427CenterWindow(
2428 HWND hwndChild,
2429 HWND hwndParent)
2430{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002431 HMONITOR mon;
2432 MONITORINFO moninfo;
2433 RECT rChild, rParent, rScreen;
2434 int wChild, hChild, wParent, hParent;
2435 int xNew, yNew;
2436 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002437
2438 GetWindowRect(hwndChild, &rChild);
2439 wChild = rChild.right - rChild.left;
2440 hChild = rChild.bottom - rChild.top;
2441
Bram Moolenaar734a8672019-12-02 22:49:38 +01002442 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002444 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445 else
2446 GetWindowRect(hwndParent, &rParent);
2447 wParent = rParent.right - rParent.left;
2448 hParent = rParent.bottom - rParent.top;
2449
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002450 moninfo.cbSize = sizeof(MONITORINFO);
2451 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2452 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002454 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002456 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002457 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002458 hdc = GetDC(hwndChild);
2459 rScreen.left = 0;
2460 rScreen.top = 0;
2461 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2462 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2463 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002464 }
2465
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002466 xNew = rParent.left + ((wParent - wChild) / 2);
2467 if (xNew < rScreen.left)
2468 xNew = rScreen.left;
2469 else if ((xNew + wChild) > rScreen.right)
2470 xNew = rScreen.right - wChild;
2471
2472 yNew = rParent.top + ((hParent - hChild) / 2);
2473 if (yNew < rScreen.top)
2474 yNew = rScreen.top;
2475 else if ((yNew + hChild) > rScreen.bottom)
2476 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002477
2478 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2479 SWP_NOSIZE | SWP_NOZORDER);
2480}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002481#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002482
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002483#if defined(FEAT_TOOLBAR) || defined(PROTO)
2484 void
2485gui_mch_show_toolbar(int showit)
2486{
2487 if (s_toolbarhwnd == NULL)
2488 return;
2489
2490 if (showit)
2491 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002492 // Enable unicode support
2493 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2494 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002495 ShowWindow(s_toolbarhwnd, SW_SHOW);
2496 }
2497 else
2498 ShowWindow(s_toolbarhwnd, SW_HIDE);
2499}
2500
Bram Moolenaar734a8672019-12-02 22:49:38 +01002501// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002502# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503
2504#endif
2505
2506#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2507 static void
2508add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2509{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002510 WCHAR *wn;
2511 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002512
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002513 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002515 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002517 infow.cbSize = sizeof(infow);
2518 infow.fMask = MIIM_TYPE | MIIM_ID;
2519 infow.wID = item_id;
2520 infow.fType = MFT_STRING;
2521 infow.dwTypeData = wn;
2522 infow.cch = (UINT)wcslen(wn);
2523 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2524 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002525}
2526
2527 static void
2528show_tabline_popup_menu(void)
2529{
2530 HMENU tab_pmenu;
2531 long rval;
2532 POINT pt;
2533
Bram Moolenaar734a8672019-12-02 22:49:38 +01002534 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002535 if (hold_gui_events
2536# ifdef FEAT_CMDWIN
2537 || cmdwin_type != 0
2538# endif
2539 )
2540 return;
2541
2542 tab_pmenu = CreatePopupMenu();
2543 if (tab_pmenu == NULL)
2544 return;
2545
2546 if (first_tabpage->tp_next != NULL)
2547 add_tabline_popup_menu_entry(tab_pmenu,
2548 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2549 add_tabline_popup_menu_entry(tab_pmenu,
2550 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2551 add_tabline_popup_menu_entry(tab_pmenu,
2552 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2553
2554 GetCursorPos(&pt);
2555 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2556 NULL);
2557
2558 DestroyMenu(tab_pmenu);
2559
Bram Moolenaar734a8672019-12-02 22:49:38 +01002560 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561 if (rval > 0)
2562 {
2563 TCHITTESTINFO htinfo;
2564 int idx;
2565
2566 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2567 return;
2568
2569 htinfo.pt.x = pt.x;
2570 htinfo.pt.y = pt.y;
2571 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2572 if (idx == -1)
2573 idx = 0;
2574 else
2575 idx += 1;
2576
2577 send_tabline_menu_event(idx, (int)rval);
2578 }
2579}
2580
2581/*
2582 * Show or hide the tabline.
2583 */
2584 void
2585gui_mch_show_tabline(int showit)
2586{
2587 if (s_tabhwnd == NULL)
2588 return;
2589
2590 if (!showit != !showing_tabline)
2591 {
2592 if (showit)
2593 ShowWindow(s_tabhwnd, SW_SHOW);
2594 else
2595 ShowWindow(s_tabhwnd, SW_HIDE);
2596 showing_tabline = showit;
2597 }
2598}
2599
2600/*
2601 * Return TRUE when tabline is displayed.
2602 */
2603 int
2604gui_mch_showing_tabline(void)
2605{
2606 return s_tabhwnd != NULL && showing_tabline;
2607}
2608
2609/*
2610 * Update the labels of the tabline.
2611 */
2612 void
2613gui_mch_update_tabline(void)
2614{
2615 tabpage_T *tp;
2616 TCITEM tie;
2617 int nr = 0;
2618 int curtabidx = 0;
2619 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002621
2622 if (s_tabhwnd == NULL)
2623 return;
2624
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002625 // Enable unicode support
2626 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002627
2628 tie.mask = TCIF_TEXT;
2629 tie.iImage = -1;
2630
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2633
Bram Moolenaar734a8672019-12-02 22:49:38 +01002634 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002635 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2636 {
2637 if (tp == curtab)
2638 curtabidx = nr;
2639
2640 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2641 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002642 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002643 tie.pszText = "-Empty-";
2644 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2645 tabadded = 1;
2646 }
2647
2648 get_tabline_label(tp, FALSE);
2649 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002650
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002651 wstr = enc_to_utf16(NameBuff, NULL);
2652 if (wstr != NULL)
2653 {
2654 TCITEMW tiw;
2655
2656 tiw.mask = TCIF_TEXT;
2657 tiw.iImage = -1;
2658 tiw.pszText = wstr;
2659 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2660 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002661 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 }
2663
Bram Moolenaar734a8672019-12-02 22:49:38 +01002664 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002665 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2666 TabCtrl_DeleteItem(s_tabhwnd, nr);
2667
2668 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2669 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2670
Bram Moolenaar734a8672019-12-02 22:49:38 +01002671 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002672 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2673 RedrawWindow(s_tabhwnd, NULL, NULL,
2674 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2675
2676 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2677 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2678}
2679
2680/*
2681 * Set the current tab to "nr". First tab is 1.
2682 */
2683 void
2684gui_mch_set_curtab(int nr)
2685{
2686 if (s_tabhwnd == NULL)
2687 return;
2688
2689 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2690 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2691}
2692
2693#endif
2694
2695/*
2696 * ":simalt" command.
2697 */
2698 void
2699ex_simalt(exarg_T *eap)
2700{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002701 char_u *keys = eap->arg;
2702 int fill_typebuf = FALSE;
2703 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704
2705 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2706 while (*keys)
2707 {
2708 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002709 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002710 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2711 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002712 fill_typebuf = TRUE;
2713 }
2714 if (fill_typebuf)
2715 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002716 // Put a NOP in the typeahead buffer so that the message will get
2717 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002718 key_name[0] = K_SPECIAL;
2719 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002720 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002721 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002722#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002723 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002724#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002725 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002726 }
2727}
2728
2729/*
2730 * Create the find & replace dialogs.
2731 * You can't have both at once: ":find" when replace is showing, destroys
2732 * the replace dialog first, and the other way around.
2733 */
2734#ifdef MSWIN_FIND_REPLACE
2735 static void
2736initialise_findrep(char_u *initial_string)
2737{
2738 int wword = FALSE;
2739 int mcase = !p_ic;
2740 char_u *entry_text;
2741
Bram Moolenaar734a8672019-12-02 22:49:38 +01002742 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2744
2745 s_findrep_struct.hwndOwner = s_hwnd;
2746 s_findrep_struct.Flags = FR_DOWN;
2747 if (mcase)
2748 s_findrep_struct.Flags |= FR_MATCHCASE;
2749 if (wword)
2750 s_findrep_struct.Flags |= FR_WHOLEWORD;
2751 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002752 {
2753 WCHAR *p = enc_to_utf16(entry_text, NULL);
2754 if (p != NULL)
2755 {
2756 int len = s_findrep_struct.wFindWhatLen - 1;
2757
2758 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2759 s_findrep_struct.lpstrFindWhat[len] = NUL;
2760 vim_free(p);
2761 }
2762 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002763 vim_free(entry_text);
2764}
2765#endif
2766
2767 static void
2768set_window_title(HWND hwnd, char *title)
2769{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002770 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002771 {
2772 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773
Bram Moolenaar734a8672019-12-02 22:49:38 +01002774 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002775 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2776 if (wbuf != NULL)
2777 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002778 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002779 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002780 }
2781 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002782 else
2783 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002784}
2785
2786 void
2787gui_mch_find_dialog(exarg_T *eap)
2788{
2789#ifdef MSWIN_FIND_REPLACE
2790 if (s_findrep_msg != 0)
2791 {
2792 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2793 DestroyWindow(s_findrep_hwnd);
2794
2795 if (!IsWindow(s_findrep_hwnd))
2796 {
2797 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002798 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 }
2800
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002801 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002802 (void)SetFocus(s_findrep_hwnd);
2803
2804 s_findrep_is_find = TRUE;
2805 }
2806#endif
2807}
2808
2809
2810 void
2811gui_mch_replace_dialog(exarg_T *eap)
2812{
2813#ifdef MSWIN_FIND_REPLACE
2814 if (s_findrep_msg != 0)
2815 {
2816 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2817 DestroyWindow(s_findrep_hwnd);
2818
2819 if (!IsWindow(s_findrep_hwnd))
2820 {
2821 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002822 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 }
2824
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002825 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002826 (void)SetFocus(s_findrep_hwnd);
2827
2828 s_findrep_is_find = FALSE;
2829 }
2830#endif
2831}
2832
2833
2834/*
2835 * Set visibility of the pointer.
2836 */
2837 void
2838gui_mch_mousehide(int hide)
2839{
2840 if (hide != gui.pointer_hidden)
2841 {
2842 ShowCursor(!hide);
2843 gui.pointer_hidden = hide;
2844 }
2845}
2846
2847#ifdef FEAT_MENU
2848 static void
2849gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2850{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002851 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 gui_mch_mousehide(FALSE);
2853
2854 (void)TrackPopupMenu(
2855 (HMENU)menu->submenu_id,
2856 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2857 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002858 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859 s_hwnd,
2860 NULL);
2861 /*
2862 * NOTE: The pop-up menu can eat the mouse up event.
2863 * We deal with this in normal.c.
2864 */
2865}
2866#endif
2867
2868/*
2869 * Got a message when the system will go down.
2870 */
2871 static void
2872_OnEndSession(void)
2873{
2874 getout_preserve_modified(1);
2875}
2876
2877/*
2878 * Get this message when the user clicks on the cross in the top right corner
2879 * of a Windows95 window.
2880 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002882_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002883{
2884 gui_shell_closed();
2885}
2886
2887/*
2888 * Get a message when the window is being destroyed.
2889 */
2890 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002891_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002892{
2893 if (!destroying)
2894 _OnClose(hwnd);
2895}
2896
2897 static void
2898_OnPaint(
2899 HWND hwnd)
2900{
2901 if (!IsMinimized(hwnd))
2902 {
2903 PAINTSTRUCT ps;
2904
Bram Moolenaar734a8672019-12-02 22:49:38 +01002905 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002906 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907
Bram Moolenaar734a8672019-12-02 22:49:38 +01002908 // prevent multi-byte characters from misprinting on an invalid
2909 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002910 if (has_mbyte)
2911 {
2912 RECT rect;
2913
2914 GetClientRect(hwnd, &rect);
2915 ps.rcPaint.left = rect.left;
2916 ps.rcPaint.right = rect.right;
2917 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918
2919 if (!IsRectEmpty(&ps.rcPaint))
2920 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2922 ps.rcPaint.right - ps.rcPaint.left + 1,
2923 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2924 }
2925
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002926 EndPaint(hwnd, &ps);
2927 }
2928}
2929
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002930 static void
2931_OnSize(
2932 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002933 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002934 int cx,
2935 int cy)
2936{
K.Takatac81e9bf2022-01-16 14:15:49 +00002937 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 {
2939 gui_resize_shell(cx, cy);
2940
Bram Moolenaar734a8672019-12-02 22:49:38 +01002941 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002942 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002943 }
2944}
2945
2946 static void
2947_OnSetFocus(
2948 HWND hwnd,
2949 HWND hwndOldFocus)
2950{
2951 gui_focus_change(TRUE);
2952 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002953 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954}
2955
2956 static void
2957_OnKillFocus(
2958 HWND hwnd,
2959 HWND hwndNewFocus)
2960{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002961 if (destroying)
2962 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002963 gui_focus_change(FALSE);
2964 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002965 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002966}
2967
2968/*
2969 * Get a message when the user switches back to vim
2970 */
2971 static LRESULT
2972_OnActivateApp(
2973 HWND hwnd,
2974 BOOL fActivate,
2975 DWORD dwThreadId)
2976{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002977 // we call gui_focus_change() in _OnSetFocus()
2978 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002979 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980}
2981
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002982 void
2983gui_mch_destroy_scrollbar(scrollbar_T *sb)
2984{
2985 DestroyWindow(sb->id);
2986}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987
2988/*
2989 * Get current mouse coordinates in text window.
2990 */
2991 void
2992gui_mch_getmouse(int *x, int *y)
2993{
2994 RECT rct;
2995 POINT mp;
2996
2997 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002998 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002999 *x = (int)(mp.x - rct.left);
3000 *y = (int)(mp.y - rct.top);
3001}
3002
3003/*
3004 * Move mouse pointer to character at (x, y).
3005 */
3006 void
3007gui_mch_setmouse(int x, int y)
3008{
3009 RECT rct;
3010
3011 (void)GetWindowRect(s_textArea, &rct);
3012 (void)SetCursorPos(x + gui.border_offset + rct.left,
3013 y + gui.border_offset + rct.top);
3014}
3015
3016 static void
3017gui_mswin_get_valid_dimensions(
3018 int w,
3019 int h,
3020 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003021 int *valid_h,
3022 int *cols,
3023 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003024{
3025 int base_width, base_height;
3026
3027 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003028 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3029 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003030 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003031 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3032 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3033 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3034 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003035 *cols = (w - base_width) / gui.char_width;
3036 *rows = (h - base_height) / gui.char_height;
3037 *valid_w = base_width + *cols * gui.char_width;
3038 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003039}
3040
3041 void
3042gui_mch_flash(int msec)
3043{
3044 RECT rc;
3045
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003046#if defined(FEAT_DIRECTX)
3047 if (IS_ENABLE_DIRECTX())
3048 DWriteContext_Flush(s_dwc);
3049#endif
3050
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003051 /*
3052 * Note: InvertRect() excludes right and bottom of rectangle.
3053 */
3054 rc.left = 0;
3055 rc.top = 0;
3056 rc.right = gui.num_cols * gui.char_width;
3057 rc.bottom = gui.num_rows * gui.char_height;
3058 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003059 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060
Bram Moolenaar734a8672019-12-02 22:49:38 +01003061 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003062
3063 InvertRect(s_hdc, &rc);
3064}
3065
3066/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003067 * Check if the specified point is on-screen. (multi-monitor aware)
3068 */
3069 static BOOL
3070is_point_onscreen(int x, int y)
3071{
3072 POINT pt = {x, y};
3073
3074 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3075}
3076
3077/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003078 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003079 *
3080 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3081 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3082 * only works when the whole window is on-screen.
3083 */
3084 static BOOL
3085is_window_onscreen(HWND hwnd)
3086{
3087 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003088 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003089
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003090 GetClientRect(hwnd, &rc);
3091 p1.x = rc.left;
3092 p1.y = rc.top;
3093 p2.x = rc.right - 1;
3094 p2.y = rc.bottom - 1;
3095 ClientToScreen(hwnd, &p1);
3096 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003097
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003098 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003099 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003100 if (!is_point_onscreen(p1.x, p2.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, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003103 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003104 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003105 return FALSE;
3106 return TRUE;
3107}
3108
3109/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003110 * Return flags used for scrolling.
3111 * The SW_INVALIDATE is required when part of the window is covered or
3112 * off-screen. Refer to MS KB Q75236.
3113 */
3114 static int
3115get_scroll_flags(void)
3116{
3117 HWND hwnd;
3118 RECT rcVim, rcOther, rcDest;
3119
Bram Moolenaar185577e2020-10-29 20:08:21 +01003120 // Check if the window is (partly) off-screen.
3121 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003122 return SW_INVALIDATE;
3123
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003124 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003125 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3127 if (IsWindowVisible(hwnd))
3128 {
3129 GetWindowRect(hwnd, &rcOther);
3130 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3131 return SW_INVALIDATE;
3132 }
3133 return 0;
3134}
3135
3136/*
3137 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3138 * may not be scrolled out properly.
3139 * For gVim, when _OnScroll() is repeated, the character at the
3140 * previous cursor position may be left drawn after scroll.
3141 * The problem can be avoided by calling GetPixel() to get a pixel in
3142 * the region before ScrollWindowEx().
3143 */
3144 static void
3145intel_gpu_workaround(void)
3146{
3147 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3148}
3149
3150/*
3151 * Delete the given number of lines from the given row, scrolling up any
3152 * text further down within the scroll region.
3153 */
3154 void
3155gui_mch_delete_lines(
3156 int row,
3157 int num_lines)
3158{
3159 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003160
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 rc.left = FILL_X(gui.scroll_region_left);
3162 rc.right = FILL_X(gui.scroll_region_right + 1);
3163 rc.top = FILL_Y(row);
3164 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3165
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003167 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003169 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003171 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003172#endif
3173 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003174#if defined(FEAT_DIRECTX)
3175 if (IS_ENABLE_DIRECTX())
3176 DWriteContext_Flush(s_dwc);
3177#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003178 intel_gpu_workaround();
3179 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003181 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003182 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003183
Bram Moolenaar734a8672019-12-02 22:49:38 +01003184 // This seems to be required to avoid the cursor disappearing when
3185 // scrolling such that the cursor ends up in the top-left character on
3186 // the screen... But why? (Webb)
3187 // It's probably fixed by disabling drawing the cursor while scrolling.
3188 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189
3190 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3191 gui.scroll_region_left,
3192 gui.scroll_region_bot, gui.scroll_region_right);
3193}
3194
3195/*
3196 * Insert the given number of lines before the given row, scrolling down any
3197 * following text within the scroll region.
3198 */
3199 void
3200gui_mch_insert_lines(
3201 int row,
3202 int num_lines)
3203{
3204 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003205
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003206 rc.left = FILL_X(gui.scroll_region_left);
3207 rc.right = FILL_X(gui.scroll_region_right + 1);
3208 rc.top = FILL_Y(row);
3209 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003210
3211#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003212 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003213 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003214 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003215 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003216 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003217#endif
3218 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003219#if defined(FEAT_DIRECTX)
3220 if (IS_ENABLE_DIRECTX())
3221 DWriteContext_Flush(s_dwc);
3222#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003223 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003224 // The SW_INVALIDATE is required when part of the window is covered or
3225 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003226 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003227 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003228 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003229 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003230
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231 gui_clear_block(row, gui.scroll_region_left,
3232 row + num_lines - 1, gui.scroll_region_right);
3233}
3234
3235
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003237gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238{
3239#if defined(FEAT_DIRECTX)
3240 DWriteContext_Close(s_dwc);
3241 DWrite_Final();
3242 s_dwc = NULL;
3243#endif
3244
3245 ReleaseDC(s_textArea, s_hdc);
3246 DeleteObject(s_brush);
3247
3248#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003249 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003250 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3251#endif
3252
Bram Moolenaar734a8672019-12-02 22:49:38 +01003253 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003254 if (s_hwnd != NULL)
3255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003256 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257 DestroyWindow(s_hwnd);
3258 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003259}
3260
3261 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003262logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003263{
3264 char *p;
3265 char *res;
3266 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003267 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003268 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003269 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003270
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003271 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3272 if (font_name == NULL)
3273 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003274 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003275 quality_name = quality_id2name((int)lf.lfQuality);
3276
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003277 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003278 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003279 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003280 if (res != NULL)
3281 {
3282 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003283 // make a normal font string out of the lf thing:
3284 points = pixels_to_points(
3285 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3286 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3287 sprintf((char *)p, "%s:h%d", font_name, points);
3288 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003289 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003290 while (*p)
3291 {
3292 if (*p == ' ')
3293 *p = '_';
3294 ++p;
3295 }
3296 if (lf.lfItalic)
3297 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003298 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003299 STRCAT(p, ":b");
3300 if (lf.lfUnderline)
3301 STRCAT(p, ":u");
3302 if (lf.lfStrikeOut)
3303 STRCAT(p, ":s");
3304 if (charset_name != NULL)
3305 {
3306 STRCAT(p, ":c");
3307 STRCAT(p, charset_name);
3308 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003309 if (quality_name != NULL)
3310 {
3311 STRCAT(p, ":q");
3312 STRCAT(p, quality_name);
3313 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003314 }
3315
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003316 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003317 return (char_u *)res;
3318}
3319
3320
3321#ifdef FEAT_MBYTE_IME
3322/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003323 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003324 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325 */
3326 static void
3327update_im_font(void)
3328{
K.Takatac81e9bf2022-01-16 14:15:49 +00003329 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330
3331 if (p_guifontwide != NULL && *p_guifontwide != NUL
3332 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003333 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334 norm_logfont = lf_wide;
3335 else
3336 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003337
3338 lf = norm_logfont;
3339 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3340 // Work around when PerMonitorV2 is not enabled in the process level.
3341 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3342 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343}
3344#endif
3345
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346/*
3347 * Handler of gui.wide_font (p_guifontwide) changed notification.
3348 */
3349 void
3350gui_mch_wide_font_changed(void)
3351{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003352 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003354#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003356#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003357
3358 gui_mch_free_font(gui.wide_ital_font);
3359 gui.wide_ital_font = NOFONT;
3360 gui_mch_free_font(gui.wide_bold_font);
3361 gui.wide_bold_font = NOFONT;
3362 gui_mch_free_font(gui.wide_boldital_font);
3363 gui.wide_boldital_font = NOFONT;
3364
3365 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003366 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003367 {
3368 if (!lf.lfItalic)
3369 {
3370 lf.lfItalic = TRUE;
3371 gui.wide_ital_font = get_font_handle(&lf);
3372 lf.lfItalic = FALSE;
3373 }
3374 if (lf.lfWeight < FW_BOLD)
3375 {
3376 lf.lfWeight = FW_BOLD;
3377 gui.wide_bold_font = get_font_handle(&lf);
3378 if (!lf.lfItalic)
3379 {
3380 lf.lfItalic = TRUE;
3381 gui.wide_boldital_font = get_font_handle(&lf);
3382 }
3383 }
3384 }
3385}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003386
3387/*
3388 * Initialise vim to use the font with the given name.
3389 * Return FAIL if the font could not be loaded, OK otherwise.
3390 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003392gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393{
K.Takatac81e9bf2022-01-16 14:15:49 +00003394 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 GuiFont font = NOFONT;
3396 char_u *p;
3397
Bram Moolenaar734a8672019-12-02 22:49:38 +01003398 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003399 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003400 {
3401 lfOrig = lf;
3402 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003404 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003405 if (font == NOFONT)
3406 return FAIL;
3407
3408 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003409 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003410#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411 norm_logfont = lf;
3412 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003413 if (!s_in_dpichanged)
3414 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415#endif
3416 gui_mch_free_font(gui.norm_font);
3417 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003418 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419 GetFontSize(font);
3420
K.Takatac81e9bf2022-01-16 14:15:49 +00003421 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003422 if (p != NULL)
3423 {
3424 hl_set_font_name(p);
3425
Bram Moolenaar734a8672019-12-02 22:49:38 +01003426 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003427 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3428 {
3429 vim_free(p_guifont);
3430 p_guifont = p;
3431 }
3432 else
3433 vim_free(p);
3434 }
3435
3436 gui_mch_free_font(gui.ital_font);
3437 gui.ital_font = NOFONT;
3438 gui_mch_free_font(gui.bold_font);
3439 gui.bold_font = NOFONT;
3440 gui_mch_free_font(gui.boldital_font);
3441 gui.boldital_font = NOFONT;
3442
3443 if (!lf.lfItalic)
3444 {
3445 lf.lfItalic = TRUE;
3446 gui.ital_font = get_font_handle(&lf);
3447 lf.lfItalic = FALSE;
3448 }
3449 if (lf.lfWeight < FW_BOLD)
3450 {
3451 lf.lfWeight = FW_BOLD;
3452 gui.bold_font = get_font_handle(&lf);
3453 if (!lf.lfItalic)
3454 {
3455 lf.lfItalic = TRUE;
3456 gui.boldital_font = get_font_handle(&lf);
3457 }
3458 }
3459
3460 return OK;
3461}
3462
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463/*
3464 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003465 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466 */
3467 int
3468gui_mch_maximized(void)
3469{
3470 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003471 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472
3473 wp.length = sizeof(WINDOWPLACEMENT);
3474 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003475 {
3476 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003477 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003478 && wp.flags == WPF_RESTORETOMAXIMIZED))
3479 return TRUE;
3480 if (wp.showCmd == SW_SHOWMINIMIZED)
3481 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003483 // Assume the window is snapped when the sizes from two APIs differ.
3484 GetWindowRect(s_hwnd, &rc);
3485 if ((rc.right - rc.left !=
3486 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3487 || (rc.bottom - rc.top !=
3488 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3489 return TRUE;
3490 }
3491 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492}
3493
3494/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003495 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3496 * is set. Compute the new Rows and Columns. This is like resizing the
3497 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498 */
3499 void
3500gui_mch_newfont(void)
3501{
3502 RECT rect;
3503
3504 GetWindowRect(s_hwnd, &rect);
3505 if (win_socket_id == 0)
3506 {
3507 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003508 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3509 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003511 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3512 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3513 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3514 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515 }
3516 else
3517 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003518 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003519 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003520 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003521 }
3522}
3523
3524/*
3525 * Set the window title
3526 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 void
3528gui_mch_settitle(
3529 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003530 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531{
3532 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3533}
3534
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003535#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003536// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3537// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003538static LPCSTR mshape_idcs[] =
3539{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003540 IDC_ARROW, // arrow
3541 MAKEINTRESOURCE(0), // blank
3542 IDC_IBEAM, // beam
3543 IDC_SIZENS, // updown
3544 IDC_SIZENS, // udsizing
3545 IDC_SIZEWE, // leftright
3546 IDC_SIZEWE, // lrsizing
3547 IDC_WAIT, // busy
3548 IDC_NO, // no
3549 IDC_ARROW, // crosshair
3550 IDC_ARROW, // hand1
3551 IDC_ARROW, // hand2
3552 IDC_ARROW, // pencil
3553 IDC_ARROW, // question
3554 IDC_ARROW, // right-arrow
3555 IDC_UPARROW, // up-arrow
3556 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557};
3558
3559 void
3560mch_set_mouse_shape(int shape)
3561{
3562 LPCSTR idc;
3563
3564 if (shape == MSHAPE_HIDE)
3565 ShowCursor(FALSE);
3566 else
3567 {
3568 if (shape >= MSHAPE_NUMBERED)
3569 idc = IDC_ARROW;
3570 else
3571 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003572 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 if (!p_mh)
3574 {
3575 POINT mp;
3576
Bram Moolenaar734a8672019-12-02 22:49:38 +01003577 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003578 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003579 (void)SetCursorPos(mp.x, mp.y);
3580 ShowCursor(TRUE);
3581 }
3582 }
3583}
3584#endif
3585
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003586#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588 * Wide version of convert_filter().
3589 */
3590 static WCHAR *
3591convert_filterW(char_u *s)
3592{
3593 char_u *tmp;
3594 int len;
3595 WCHAR *res;
3596
3597 tmp = convert_filter(s);
3598 if (tmp == NULL)
3599 return NULL;
3600 len = (int)STRLEN(s) + 3;
3601 res = enc_to_utf16(tmp, &len);
3602 vim_free(tmp);
3603 return res;
3604}
3605
3606/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003607 * Pop open a file browser and return the file selected, in allocated memory,
3608 * or NULL if Cancel is hit.
3609 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3610 * title - Title message for the file browser dialog.
3611 * dflt - Default name of file.
3612 * ext - Default extension to be added to files without extensions.
3613 * initdir - directory in which to open the browser (NULL = current dir)
3614 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003616 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003617gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618 int saving,
3619 char_u *title,
3620 char_u *dflt,
3621 char_u *ext,
3622 char_u *initdir,
3623 char_u *filter)
3624{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003625 // We always use the wide function. This means enc_to_utf16() must work,
3626 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 OPENFILENAMEW fileStruct;
3628 WCHAR fileBuf[MAXPATHL];
3629 WCHAR *wp;
3630 int i;
3631 WCHAR *titlep = NULL;
3632 WCHAR *extp = NULL;
3633 WCHAR *initdirp = NULL;
3634 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003635 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003636 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003637
3638 if (dflt == NULL)
3639 fileBuf[0] = NUL;
3640 else
3641 {
3642 wp = enc_to_utf16(dflt, NULL);
3643 if (wp == NULL)
3644 fileBuf[0] = NUL;
3645 else
3646 {
3647 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3648 fileBuf[i] = wp[i];
3649 fileBuf[i] = NUL;
3650 vim_free(wp);
3651 }
3652 }
3653
Bram Moolenaar734a8672019-12-02 22:49:38 +01003654 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 filterp = convert_filterW(filter);
3656
Bram Moolenaara80faa82020-04-12 19:37:17 +02003657 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003658# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003659 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003660 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003661# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003663# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664
3665 if (title != NULL)
3666 titlep = enc_to_utf16(title, NULL);
3667 fileStruct.lpstrTitle = titlep;
3668
3669 if (ext != NULL)
3670 extp = enc_to_utf16(ext, NULL);
3671 fileStruct.lpstrDefExt = extp;
3672
3673 fileStruct.lpstrFile = fileBuf;
3674 fileStruct.nMaxFile = MAXPATHL;
3675 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003676 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3677 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003678 if (initdir != NULL && *initdir != NUL)
3679 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003680 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003681 initdirp = enc_to_utf16(initdir, NULL);
3682 if (initdirp != NULL)
3683 {
3684 for (wp = initdirp; *wp != NUL; ++wp)
3685 if (*wp == '/')
3686 *wp = '\\';
3687 }
3688 fileStruct.lpstrInitialDir = initdirp;
3689 }
3690
3691 /*
3692 * TODO: Allow selection of multiple files. Needs another arg to this
3693 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3694 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3695 * files that don't exist yet, so I haven't put it in. What about
3696 * OFN_PATHMUSTEXIST?
3697 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3698 */
3699 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003700# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003701 if (curbuf->b_p_bin)
3702 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003703# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003705 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003707 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003708
3709 vim_free(filterp);
3710 vim_free(initdirp);
3711 vim_free(titlep);
3712 vim_free(extp);
3713
K.Takata14b8d6a2022-01-20 15:05:22 +00003714 if (!ret)
3715 return NULL;
3716
3717 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003719 if (p == NULL)
3720 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003721
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723 SetFocus(s_hwnd);
3724
Bram Moolenaar734a8672019-12-02 22:49:38 +01003725 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003726 q = vim_strsave(shorten_fname1(p));
3727 vim_free(p);
3728 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003730
3731
3732/*
3733 * Convert the string s to the proper format for a filter string by replacing
3734 * the \t and \n delimiters with \0.
3735 * Returns the converted string in allocated memory.
3736 *
3737 * Keep in sync with convert_filterW() above!
3738 */
3739 static char_u *
3740convert_filter(char_u *s)
3741{
3742 char_u *res;
3743 unsigned s_len = (unsigned)STRLEN(s);
3744 unsigned i;
3745
3746 res = alloc(s_len + 3);
3747 if (res != NULL)
3748 {
3749 for (i = 0; i < s_len; ++i)
3750 if (s[i] == '\t' || s[i] == '\n')
3751 res[i] = '\0';
3752 else
3753 res[i] = s[i];
3754 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003755 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756 res[s_len + 1] = NUL;
3757 res[s_len + 2] = NUL;
3758 }
3759 return res;
3760}
3761
3762/*
3763 * Select a directory.
3764 */
3765 char_u *
3766gui_mch_browsedir(char_u *title, char_u *initdir)
3767{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003768 // We fake this: Use a filter that doesn't select anything and a default
3769 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3771 initdir, (char_u *)_("Directory\t*.nothing\n"));
3772}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003773#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003774
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775 static void
3776_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003777 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778 HDROP hDrop)
3779{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003780#define BUFPATHLEN _MAX_PATH
3781#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 char szFile[BUFPATHLEN];
3784 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3785 UINT i;
3786 char_u **fnames;
3787 POINT pt;
3788 int_u modifiers = 0;
3789
Bram Moolenaar734a8672019-12-02 22:49:38 +01003790 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 DragQueryPoint(hDrop, &pt);
3792 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3793
3794 reset_VIsual();
3795
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003796 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797
3798 if (fnames != NULL)
3799 for (i = 0; i < cFiles; ++i)
3800 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3802 fnames[i] = utf16_to_enc(wszFile, NULL);
3803 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003804 {
3805 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3806 fnames[i] = vim_strsave((char_u *)szFile);
3807 }
3808 }
3809
3810 DragFinish(hDrop);
3811
3812 if (fnames != NULL)
3813 {
LemonBoy45684c62022-04-24 15:46:42 +01003814 int kbd_modifiers = get_active_modifiers();
3815
3816 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003818 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003820 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 modifiers |= MOUSE_ALT;
3822
3823 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3824
3825 s_need_activate = TRUE;
3826 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003827}
3828
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829 static int
3830_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003831 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003832 HWND hwndCtl,
3833 UINT code,
3834 int pos)
3835{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003836 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003837 scrollbar_T *sb, *sb_info;
3838 long val;
3839 int dragging = FALSE;
3840 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841 SCROLLINFO si;
3842
3843 si.cbSize = sizeof(si);
3844 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845
3846 sb = gui_mswin_find_scrollbar(hwndCtl);
3847 if (sb == NULL)
3848 return 0;
3849
Bram Moolenaar734a8672019-12-02 22:49:38 +01003850 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 {
3852 /*
3853 * Careful: need to get scrollbar info out of first (left) scrollbar
3854 * for window, but keep real scrollbar too because we must pass it to
3855 * gui_drag_scrollbar().
3856 */
3857 sb_info = &sb->wp->w_scrollbars[0];
3858 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003859 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003860 sb_info = sb;
3861 val = sb_info->value;
3862
3863 switch (code)
3864 {
3865 case SB_THUMBTRACK:
3866 val = pos;
3867 dragging = TRUE;
3868 if (sb->scroll_shift > 0)
3869 val <<= sb->scroll_shift;
3870 break;
3871 case SB_LINEDOWN:
3872 val++;
3873 break;
3874 case SB_LINEUP:
3875 val--;
3876 break;
3877 case SB_PAGEDOWN:
3878 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3879 break;
3880 case SB_PAGEUP:
3881 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3882 break;
3883 case SB_TOP:
3884 val = 0;
3885 break;
3886 case SB_BOTTOM:
3887 val = sb_info->max;
3888 break;
3889 case SB_ENDSCROLL:
3890 if (prev_code == SB_THUMBTRACK)
3891 {
3892 /*
3893 * "pos" only gives us 16-bit data. In case of large file,
3894 * use GetScrollPos() which returns 32-bit. Unfortunately it
3895 * is not valid while the scrollbar is being dragged.
3896 */
3897 val = GetScrollPos(hwndCtl, SB_CTL);
3898 if (sb->scroll_shift > 0)
3899 val <<= sb->scroll_shift;
3900 }
3901 break;
3902
3903 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003904 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905 return 0;
3906 }
3907 prev_code = code;
3908
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3910 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003911
3912 /*
3913 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3914 */
3915 if (sb->wp != NULL)
3916 {
3917 scrollbar_T *sba = sb->wp->w_scrollbars;
3918 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3919
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003920 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921 }
3922
Bram Moolenaar734a8672019-12-02 22:49:38 +01003923 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924 s_busy_processing = TRUE;
3925
Bram Moolenaar734a8672019-12-02 22:49:38 +01003926 // When "allow_scrollbar" is FALSE still need to remember the new
3927 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 dont_scroll = !allow_scrollbar;
3929
Bram Moolenaara338adc2018-01-31 20:51:47 +01003930 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003932 mch_enable_flush();
3933 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003934
3935 s_busy_processing = FALSE;
3936 dont_scroll = dont_scroll_save;
3937
3938 return 0;
3939}
3940
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942#ifdef FEAT_XPM_W32
3943# include "xpm_w32.h"
3944#endif
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
Bram Moolenaar734a8672019-12-02 22:49:38 +01003947// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#define TEAROFF_PADDING_X 2
3949#define TEAROFF_BUTTON_PAD_X 8
3950#define TEAROFF_MIN_WIDTH 200
3951#define TEAROFF_SUBMENU_LABEL ">>"
3952#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3953
3954
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003955#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956# define ID_BEVAL_TOOLTIP 200
3957# define BEVAL_TEXT_LEN MAXPATHL
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003960static UINT_PTR beval_timer_id = 0;
3961static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003962#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003963
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003964
Bram Moolenaar734a8672019-12-02 22:49:38 +01003965// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967#ifdef FEAT_MENU
3968static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971/*
3972 * Use the system font for dialogs and tear-off menus. Remove this line to
3973 * use DLG_FONT_NAME.
3974 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003975#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978#define VIM_CLASSW L"Vim"
3979
Bram Moolenaar734a8672019-12-02 22:49:38 +01003980// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3981// thus there should be room for every dialog. For tearoffs it's made bigger
3982// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#define DLG_ALLOC_SIZE 16 * 1024
3984
3985/*
3986 * stuff for dialogs, menus, tearoffs etc.
3987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988static PWORD
3989add_dialog_element(
3990 PWORD p,
3991 DWORD lStyle,
3992 WORD x,
3993 WORD y,
3994 WORD w,
3995 WORD h,
3996 WORD Id,
3997 WORD clss,
3998 const char *caption);
3999static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004000static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004001#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004static void get_dialog_font_metrics(void);
4005
4006static int dialog_default_button = -1;
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#ifdef FEAT_TOOLBAR
4009static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004010static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004011static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004013#else
4014# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
4016
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004017#ifdef FEAT_GUI_TABLINE
4018static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004019static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004020#endif
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#ifdef FEAT_MBYTE_IME
4023static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4024static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4025#endif
4026#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4027# ifdef NOIME
4028typedef struct tagCOMPOSITIONFORM {
4029 DWORD dwStyle;
4030 POINT ptCurrentPos;
4031 RECT rcArea;
4032} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4033typedef HANDLE HIMC;
4034# endif
4035
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004036static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004037static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4038static HIMC (WINAPI *pImmGetContext)(HWND);
4039static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4040static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4041static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4042static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004043static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4044static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004045static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4046static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004047static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048static void dyn_imm_load(void);
4049#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050# define pImmGetCompositionStringW ImmGetCompositionStringW
4051# define pImmGetContext ImmGetContext
4052# define pImmAssociateContext ImmAssociateContext
4053# define pImmReleaseContext ImmReleaseContext
4054# define pImmGetOpenStatus ImmGetOpenStatus
4055# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004056# define pImmGetCompositionFontW ImmGetCompositionFontW
4057# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058# define pImmSetCompositionWindow ImmSetCompositionWindow
4059# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004060# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#endif
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063#ifdef FEAT_MENU
4064/*
4065 * Figure out how high the menu bar is at the moment.
4066 */
4067 static int
4068gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004069 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
4071 static int old_menu_height = -1;
4072
4073 RECT rc1, rc2;
4074 int num;
4075 int menu_height;
4076
4077 if (gui.menu_is_active)
4078 num = GetMenuItemCount(s_menuBar);
4079 else
4080 num = 0;
4081
4082 if (num == 0)
4083 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004084 else if (IsMinimized(s_hwnd))
4085 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004086 // The height of the menu cannot be determined while the window is
4087 // minimized. Take the previous height if the menu is changed in that
4088 // state, to avoid that Vim's vertical window size accidentally
4089 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004090 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 else
4093 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004094 /*
4095 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4096 * seem to have been set yet, so menu wraps in default window
4097 * width which is very narrow. Instead just return height of a
4098 * single menu item. Will still be wrong when the menu really
4099 * should wrap over more than one line.
4100 */
4101 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4102 if (gui.starting)
4103 menu_height = rc1.bottom - rc1.top + 1;
4104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004106 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4107 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 }
4110
4111 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004112 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004113 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 return menu_height;
4116}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004117#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
4119
4120/*
4121 * Setup for the Intellimouse
4122 */
LemonBoyc27747e2022-05-07 12:25:40 +01004123 static long
4124mouse_vertical_scroll_step(void)
4125{
4126 UINT val;
4127 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4128 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4129 return 3; // Safe default;
4130}
4131
4132 static long
4133mouse_horizontal_scroll_step(void)
4134{
4135 UINT val;
4136 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4137 return (long)val;
4138 return 3; // Safe default;
4139}
4140
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 static void
4142init_mouse_wheel(void)
4143{
LemonBoyc27747e2022-05-07 12:25:40 +01004144 // Get the default values for the horizontal and vertical scroll steps from
4145 // the system.
4146 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4147 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
Bram Moolenaarae20f342019-11-05 21:09:23 +01004150/*
LemonBoya773d842022-05-10 20:54:46 +01004151 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004154_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
LemonBoy365d8f72022-05-05 19:23:07 +01004156 int button;
4157 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004158 int modifiers = 0;
4159 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004160 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4161 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Bram Moolenaarae20f342019-11-05 21:09:23 +01004163 wp = gui_mouse_window(FIND_POPUP);
4164
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004165#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004166 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004167 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004168 cmdarg_T cap;
4169 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004170
Bram Moolenaarae20f342019-11-05 21:09:23 +01004171 // Mouse hovers over popup window, scroll it if possible.
4172 mouse_row = wp->w_winrow;
4173 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004174 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004175 if (horizontal)
4176 {
4177 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4178 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4179 }
4180 else
4181 {
4182 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4183 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4184 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004185 clear_oparg(&oa);
4186 cap.oap = &oa;
4187 nv_mousescroll(&cap);
4188 update_screen(0);
4189 setcursor();
4190 out_flush();
4191 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004192 }
4193#endif
4194
Bram Moolenaarae20f342019-11-05 21:09:23 +01004195 if (wp == NULL || !p_scf)
4196 wp = curwin;
4197
LemonBoy365d8f72022-05-05 19:23:07 +01004198 // Translate the scroll event into an event that Vim can process so that
4199 // the user has a chance to map the scrollwheel buttons.
4200 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004201 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 else
LemonBoy365d8f72022-05-05 19:23:07 +01004203 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004204
4205 kbd_modifiers = get_active_modifiers();
4206
4207 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4208 modifiers |= MOUSE_SHIFT;
4209 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4210 modifiers |= MOUSE_CTRL;
4211 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4212 modifiers |= MOUSE_ALT;
4213
LemonBoya773d842022-05-10 20:54:46 +01004214 // The cursor position is relative to the upper-left corner of the screen.
4215 pt.x = GET_X_LPARAM(lParam);
4216 pt.y = GET_Y_LPARAM(lParam);
4217 ScreenToClient(s_textArea, &pt);
4218
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004219 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004222#ifdef USE_SYSMENU_FONT
4223/*
4224 * Get Menu Font.
4225 * Return OK or FAIL.
4226 */
4227 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004228gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004229{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004230 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004231
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004232 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4233 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004234 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004235 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004236 &nm,
4237 0))
4238 return FAIL;
4239 *lf = nm.lfMenuFont;
4240 return OK;
4241}
4242#endif
4243
4244
4245#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4246/*
4247 * Set the GUI tabline font to the system menu font
4248 */
4249 static void
4250set_tabline_font(void)
4251{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004252 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004253 HFONT font;
4254 HWND hwnd;
4255 HDC hdc;
4256 HFONT hfntOld;
4257 TEXTMETRIC tm;
4258
4259 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4260 return;
4261
K.Takatac81e9bf2022-01-16 14:15:49 +00004262 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004263 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004264
4265 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4266
4267 /*
4268 * Compute the height of the font used for the tab text
4269 */
4270 hwnd = GetDesktopWindow();
4271 hdc = GetWindowDC(hwnd);
4272 hfntOld = SelectFont(hdc, font);
4273
4274 GetTextMetrics(hdc, &tm);
4275
4276 SelectFont(hdc, hfntOld);
4277 ReleaseDC(hwnd, hdc);
4278
4279 /*
4280 * The space used by the tab border and the space between the tab label
4281 * and the tab border is included as 7.
4282 */
4283 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4284}
K.Takatac81e9bf2022-01-16 14:15:49 +00004285#else
4286# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004287#endif
4288
Bram Moolenaar520470a2005-06-16 21:59:56 +00004289/*
4290 * Invoked when a setting was changed.
4291 */
4292 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004293_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004294{
LemonBoy365d8f72022-05-05 19:23:07 +01004295 switch (param)
4296 {
4297 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004298 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004299 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004300 case SPI_SETWHEELSCROLLCHARS:
4301 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004302 break;
4303 case SPI_SETNONCLIENTMETRICS:
4304 set_tabline_font();
4305 break;
4306 default:
4307 break;
4308 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004309 return 0;
4310}
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312#ifdef FEAT_NETBEANS_INTG
4313 static void
4314_OnWindowPosChanged(
4315 HWND hwnd,
4316 const LPWINDOWPOS lpwpos)
4317{
4318 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004319 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4322 || lpwpos->cx != cx || lpwpos->cy != cy))
4323 {
4324 x = lpwpos->x;
4325 y = lpwpos->y;
4326 cx = lpwpos->cx;
4327 cy = lpwpos->cy;
4328 netbeans_frame_moved(x, y);
4329 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004330 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004331 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332}
4333#endif
4334
K.Takata4f2417f2021-06-05 16:25:32 +02004335
4336static HWND hwndTip = NULL;
4337
4338 static void
4339show_sizing_tip(int cols, int rows)
4340{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004341 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004342 char buf[32];
4343
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004344 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004345 ti.hwnd = s_hwnd;
4346 ti.uId = (UINT_PTR)s_hwnd;
4347 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4348 ti.lpszText = buf;
4349 sprintf(buf, "%dx%d", cols, rows);
4350 if (hwndTip == NULL)
4351 {
4352 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4353 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4354 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4355 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4356 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4357 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4358 }
4359 else
4360 {
4361 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4362 }
4363 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4364}
4365
4366 static void
4367destroy_sizing_tip(void)
4368{
4369 if (hwndTip != NULL)
4370 {
4371 DestroyWindow(hwndTip);
4372 hwndTip = NULL;
4373 }
4374}
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 static int
4377_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 UINT fwSide,
4379 LPRECT lprc)
4380{
4381 int w, h;
4382 int valid_w, valid_h;
4383 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004384 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 w = lprc->right - lprc->left;
4387 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004388 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 w_offset = w - valid_w;
4390 h_offset = h - valid_h;
4391
4392 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4393 || fwSide == WMSZ_BOTTOMLEFT)
4394 lprc->left += w_offset;
4395 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4396 || fwSide == WMSZ_BOTTOMRIGHT)
4397 lprc->right -= w_offset;
4398
4399 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4400 || fwSide == WMSZ_TOPRIGHT)
4401 lprc->top += h_offset;
4402 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4403 || fwSide == WMSZ_BOTTOMRIGHT)
4404 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004405
4406 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 return TRUE;
4408}
4409
K.Takata92000e22022-01-20 15:10:57 +00004410#ifdef FEAT_GUI_TABLINE
4411 static void
4412_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4413{
4414 if (gui_mch_showing_tabline())
4415 {
4416 POINT pt;
4417 RECT rect;
4418
4419 /*
4420 * If the cursor is on the tabline, display the tab menu
4421 */
4422 GetCursorPos(&pt);
4423 GetWindowRect(s_textArea, &rect);
4424 if (pt.y < rect.top)
4425 {
4426 show_tabline_popup_menu();
4427 return;
4428 }
4429 }
4430 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4431}
4432
4433 static void
4434_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4435{
4436 /*
4437 * If the user double clicked the tabline, create a new tab
4438 */
4439 if (gui_mch_showing_tabline())
4440 {
4441 POINT pt;
4442 RECT rect;
4443
4444 GetCursorPos(&pt);
4445 GetWindowRect(s_textArea, &rect);
4446 if (pt.y < rect.top)
4447 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4448 }
4449 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4450}
4451#endif
4452
4453 static UINT
4454_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4455{
4456 UINT result;
4457 int x, y;
4458
4459 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4460 if (result != HTCLIENT)
4461 return result;
4462
4463#ifdef FEAT_GUI_TABLINE
4464 if (gui_mch_showing_tabline())
4465 {
4466 RECT rct;
4467
4468 // If the cursor is on the GUI tabline, don't process this event
4469 GetWindowRect(s_textArea, &rct);
4470 if (yPos < rct.top)
4471 return result;
4472 }
4473#endif
4474 (void)gui_mch_get_winpos(&x, &y);
4475 xPos -= x;
4476
4477 if (xPos < 48) // <VN> TODO should use system metric?
4478 return HTBOTTOMLEFT;
4479 else
4480 return HTBOTTOMRIGHT;
4481}
4482
4483#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4484 static LRESULT
4485_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4486{
4487 switch (hdr->code)
4488 {
4489 case TTN_GETDISPINFOW:
4490 case TTN_GETDISPINFO:
4491 {
4492 char_u *str = NULL;
4493 static void *tt_text = NULL;
4494
4495 VIM_CLEAR(tt_text);
4496
4497# ifdef FEAT_GUI_TABLINE
4498 if (gui_mch_showing_tabline()
4499 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4500 {
4501 POINT pt;
4502 /*
4503 * Mouse is over the GUI tabline. Display the
4504 * tooltip for the tab under the cursor
4505 *
4506 * Get the cursor position within the tab control
4507 */
4508 GetCursorPos(&pt);
4509 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4510 {
4511 TCHITTESTINFO htinfo;
4512 int idx;
4513
4514 /*
4515 * Get the tab under the cursor
4516 */
4517 htinfo.pt.x = pt.x;
4518 htinfo.pt.y = pt.y;
4519 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4520 if (idx != -1)
4521 {
4522 tabpage_T *tp;
4523
4524 tp = find_tabpage(idx + 1);
4525 if (tp != NULL)
4526 {
4527 get_tabline_label(tp, TRUE);
4528 str = NameBuff;
4529 }
4530 }
4531 }
4532 }
4533# endif
4534# ifdef FEAT_TOOLBAR
4535# ifdef FEAT_GUI_TABLINE
4536 else
4537# endif
4538 {
4539 UINT idButton;
4540 vimmenu_T *pMenu;
4541
4542 idButton = (UINT) hdr->idFrom;
4543 pMenu = gui_mswin_find_menu(root_menu, idButton);
4544 if (pMenu)
4545 str = pMenu->strings[MENU_INDEX_TIP];
4546 }
4547# endif
4548 if (str == NULL)
4549 break;
4550
4551 // Set the maximum width, this also enables using \n for
4552 // line break.
4553 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4554
4555 if (hdr->code == TTN_GETDISPINFOW)
4556 {
4557 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4558
4559 tt_text = enc_to_utf16(str, NULL);
4560 lpdi->lpszText = tt_text;
4561 // can't show tooltip if failed
4562 }
4563 else
4564 {
4565 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4566
4567 if (STRLEN(str) < sizeof(lpdi->szText)
4568 || ((tt_text = vim_strsave(str)) == NULL))
4569 vim_strncpy((char_u *)lpdi->szText, str,
4570 sizeof(lpdi->szText) - 1);
4571 else
4572 lpdi->lpszText = tt_text;
4573 }
4574 }
4575 break;
4576
4577# ifdef FEAT_GUI_TABLINE
4578 case TCN_SELCHANGE:
4579 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4580 {
4581 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4582 return 0L;
4583 }
4584 break;
4585
4586 case NM_RCLICK:
4587 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4588 {
4589 show_tabline_popup_menu();
4590 return 0L;
4591 }
4592 break;
4593# endif
4594
4595 default:
4596 break;
4597 }
4598 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4599}
4600#endif
4601
4602#if defined(MENUHINTS) && defined(FEAT_MENU)
4603 static LRESULT
4604_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4605{
4606 if (((UINT) HIWORD(wParam)
4607 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4608 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004609 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004610 {
4611 UINT idButton;
4612 vimmenu_T *pMenu;
4613 static int did_menu_tip = FALSE;
4614
4615 if (did_menu_tip)
4616 {
4617 msg_clr_cmdline();
4618 setcursor();
4619 out_flush();
4620 did_menu_tip = FALSE;
4621 }
4622
4623 idButton = (UINT)LOWORD(wParam);
4624 pMenu = gui_mswin_find_menu(root_menu, idButton);
4625 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4626 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4627 {
4628 ++msg_hist_off;
4629 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4630 --msg_hist_off;
4631 setcursor();
4632 out_flush();
4633 did_menu_tip = TRUE;
4634 }
4635 return 0L;
4636 }
4637 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4638}
4639#endif
4640
K.Takatac81e9bf2022-01-16 14:15:49 +00004641 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004642_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004643{
4644 s_dpi = ydpi;
4645 s_in_dpichanged = TRUE;
4646 //TRACE("DPI: %d", ydpi);
4647
4648 update_scrollbar_size();
4649 update_toolbar_size();
4650 set_tabline_font();
4651
4652 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4653 gui_get_wide_font();
4654 gui_mswin_get_menu_height(FALSE);
4655#ifdef FEAT_MBYTE_IME
4656 im_set_position(gui.row, gui.col);
4657#endif
4658 InvalidateRect(hwnd, NULL, TRUE);
4659
4660 s_in_dpichanged = FALSE;
4661 return 0L;
4662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664
4665 static LRESULT CALLBACK
4666_WndProc(
4667 HWND hwnd,
4668 UINT uMsg,
4669 WPARAM wParam,
4670 LPARAM lParam)
4671{
LemonBoya773d842022-05-10 20:54:46 +01004672 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4673 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 HandleMouseHide(uMsg, lParam);
4676
4677 s_uMsg = uMsg;
4678 s_wParam = wParam;
4679 s_lParam = lParam;
4680
4681 switch (uMsg)
4682 {
4683 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4684 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004685 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004687 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4689 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4690 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4691 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4692#ifdef FEAT_MENU
4693 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4694#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004695 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4696 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4698 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004699 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4700 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4702 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4703 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4704#ifdef FEAT_NETBEANS_INTG
4705 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4706#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004707#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004708 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4709 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004710#endif
K.Takata92000e22022-01-20 15:10:57 +00004711 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004712
Bram Moolenaar734a8672019-12-02 22:49:38 +01004713 case WM_QUERYENDSESSION: // System wants to go down.
4714 gui_shell_closed(); // Will exit when no changed buffers.
4715 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004718 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004721 return 0L;
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 break;
4724
4725 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004726 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4727 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004728 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return 0L;
4730
4731 case WM_SYSCHAR:
4732 /*
4733 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4734 * shortcut key, handle like a typed ALT key, otherwise call Windows
4735 * ALT key handling.
4736 */
4737#ifdef FEAT_MENU
4738 if ( !gui.menu_is_active
4739 || p_wak[0] == 'n'
4740 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4741 )
4742#endif
4743 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004744 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return 0L;
4746 }
4747#ifdef FEAT_MENU
4748 else
K.Takata4ac893f2022-01-20 12:44:28 +00004749 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750#endif
4751
4752 case WM_SYSKEYUP:
4753#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004754 // This used to be done only when menu is active: ALT key is used for
4755 // that. But that caused problems when menu is disabled and using
4756 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4757 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004758 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004760 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762
K.Takata4f2417f2021-06-05 16:25:32 +02004763 case WM_EXITSIZEMOVE:
4764 destroy_sizing_tip();
4765 break;
4766
Bram Moolenaar734a8672019-12-02 22:49:38 +01004767 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004771 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004772 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004773 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaar734a8672019-12-02 22:49:38 +01004775 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004776 case WM_SETTINGCHANGE:
4777 return _OnSettingChange((UINT)wParam);
4778
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004779#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004781 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782#endif
K.Takata92000e22022-01-20 15:10:57 +00004783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#if defined(MENUHINTS) && defined(FEAT_MENU)
4785 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004786 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
4789#ifdef FEAT_MBYTE_IME
4790 case WM_IME_NOTIFY:
4791 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004792 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004793 return 1L;
4794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 case WM_IME_COMPOSITION:
4796 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004797 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004798 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004800 case WM_DPICHANGED:
4801 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4802 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004806 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808#endif
K.Takata92000e22022-01-20 15:10:57 +00004809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811
K.Takata4ac893f2022-01-20 12:44:28 +00004812 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813}
4814
4815/*
4816 * End of call-back routines
4817 */
4818
Bram Moolenaar734a8672019-12-02 22:49:38 +01004819// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820HWND vim_parent_hwnd = NULL;
4821
4822 static BOOL CALLBACK
4823FindWindowTitle(HWND hwnd, LPARAM lParam)
4824{
4825 char buf[2048];
4826 char *title = (char *)lParam;
4827
4828 if (GetWindowText(hwnd, buf, sizeof(buf)))
4829 {
4830 if (strstr(buf, title) != NULL)
4831 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004832 // Found it. Store the window ref. and quit searching if MDI
4833 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004835 if (vim_parent_hwnd != NULL)
4836 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004839 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840}
4841
4842/*
4843 * Invoked for '-P "title"' argument: search for parent application to open
4844 * our window in.
4845 */
4846 void
4847gui_mch_set_parent(char *title)
4848{
4849 EnumWindows(FindWindowTitle, (LPARAM)title);
4850 if (vim_parent_hwnd == NULL)
4851 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004852 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 mch_exit(2);
4854 }
4855}
4856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 static void
4859ole_error(char *arg)
4860{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004861 char buf[IOSIZE];
4862
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004863# ifdef VIMDLL
4864 gui.in_use = mch_is_gui_executable();
4865# endif
4866
Bram Moolenaar734a8672019-12-02 22:49:38 +01004867 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004868 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004869 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004870 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004874#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4875 static char *
4876gvim_error(void)
4877{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004878 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004879
4880 if (starting)
4881 {
4882 mch_errmsg(msg);
4883 mch_errmsg("\n");
4884 mch_exit(2);
4885 }
4886 return msg;
4887}
4888
4889 char *
4890gui_mch_do_spawn(char_u *arg)
4891{
4892 int len;
4893# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4894 char_u *session = NULL;
4895 LPWSTR tofree1 = NULL;
4896# endif
4897 WCHAR name[MAX_PATH];
4898 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4899 STARTUPINFOW si = {sizeof(si)};
4900 PROCESS_INFORMATION pi;
4901
4902 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4903 goto error;
4904 p = wcsrchr(name, L'\\');
4905 if (p == NULL)
4906 goto error;
4907 // Replace the executable name from vim(d).exe to gvim(d).exe.
4908# ifdef DEBUG
4909 wcscpy(p + 1, L"gvimd.exe");
4910# else
4911 wcscpy(p + 1, L"gvim.exe");
4912# endif
4913
4914# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4915 if (starting)
4916# endif
4917 {
4918 // Pass the command line to the new process.
4919 p = GetCommandLineW();
4920 // Skip 1st argument.
4921 while (*p && *p != L' ' && *p != L'\t')
4922 {
4923 if (*p == L'"')
4924 {
4925 while (*p && *p != L'"')
4926 ++p;
4927 if (*p)
4928 ++p;
4929 }
4930 else
4931 ++p;
4932 }
4933 cmd = p;
4934 }
4935# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4936 else
4937 {
4938 // Create a session file and pass it to the new process.
4939 LPWSTR wsession;
4940 char_u *savebg;
4941 int ret;
4942
4943 session = vim_tempname('s', FALSE);
4944 if (session == NULL)
4945 goto error;
4946 savebg = p_bg;
4947 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4948 ret = write_session_file(session);
4949 vim_free(p_bg);
4950 p_bg = savebg;
4951 if (!ret)
4952 goto error;
4953 wsession = enc_to_utf16(session, NULL);
4954 if (wsession == NULL)
4955 goto error;
4956 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004957 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004958 if (cmd == NULL)
4959 {
4960 vim_free(wsession);
4961 goto error;
4962 }
4963 tofree1 = cmd;
4964 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4965 wsession, wsession);
4966 vim_free(wsession);
4967 }
4968# endif
4969
4970 // Check additional arguments to the `:gui` command.
4971 if (arg != NULL)
4972 {
4973 warg = enc_to_utf16(arg, NULL);
4974 if (warg == NULL)
4975 goto error;
4976 tofree2 = warg;
4977 }
4978 else
4979 warg = L"";
4980
4981 // Set up the new command line.
4982 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004983 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004984 if (newcmd == NULL)
4985 goto error;
4986 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4987
4988 // Spawn a new GUI process.
4989 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4990 NULL, NULL, &si, &pi))
4991 goto error;
4992 CloseHandle(pi.hProcess);
4993 CloseHandle(pi.hThread);
4994 mch_exit(0);
4995
4996error:
4997# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4998 if (session)
4999 mch_remove(session);
5000 vim_free(session);
5001 vim_free(tofree1);
5002# endif
5003 vim_free(newcmd);
5004 vim_free(tofree2);
5005 return gvim_error();
5006}
5007#endif
5008
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009/*
5010 * Parse the GUI related command-line arguments. Any arguments used are
5011 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005012 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 */
5014 void
5015gui_mch_prepare(int *argc, char **argv)
5016{
5017 int silent = FALSE;
5018 int idx;
5019
Bram Moolenaar734a8672019-12-02 22:49:38 +01005020 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5022 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005023 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5025 && (argv[2][0] == '-' || argv[2][0] == '/'))
5026 {
5027 silent = TRUE;
5028 idx = 2;
5029 }
5030 else
5031 idx = 1;
5032
Bram Moolenaar734a8672019-12-02 22:49:38 +01005033 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (STRICMP(argv[idx] + 1, "register") == 0)
5035 {
5036#ifdef FEAT_OLE
5037 RegisterMe(silent);
5038 mch_exit(0);
5039#else
5040 if (!silent)
5041 ole_error("register");
5042 mch_exit(2);
5043#endif
5044 }
5045
Bram Moolenaar734a8672019-12-02 22:49:38 +01005046 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5048 {
5049#ifdef FEAT_OLE
5050 UnregisterMe(!silent);
5051 mch_exit(0);
5052#else
5053 if (!silent)
5054 ole_error("unregister");
5055 mch_exit(2);
5056#endif
5057 }
5058
Bram Moolenaar734a8672019-12-02 22:49:38 +01005059 // Ignore an -embedding argument. It is only relevant if the
5060 // application wants to treat the case when it is started manually
5061 // differently from the case where it is started via automation (and
5062 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5064 {
5065#ifdef FEAT_OLE
5066 *argc = 1;
5067#else
5068 ole_error("embedding");
5069 mch_exit(2);
5070#endif
5071 }
5072 }
5073
5074#ifdef FEAT_OLE
5075 {
5076 int bDoRestart = FALSE;
5077
5078 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005079 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (bDoRestart)
5081 mch_exit(0);
5082 }
5083#endif
5084
5085#ifdef FEAT_NETBEANS_INTG
5086 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005087 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 int arg;
5089
5090 for (arg = 1; arg < *argc; arg++)
5091 if (strncmp("-nb", argv[arg], 3) == 0)
5092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 netbeansArg = argv[arg];
5094 mch_memmove(&argv[arg], &argv[arg + 1],
5095 (--*argc - arg) * sizeof(char *));
5096 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005097 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101}
5102
K.Takatac81e9bf2022-01-16 14:15:49 +00005103 static void
5104load_dpi_func(void)
5105{
5106 HMODULE hUser32;
5107
5108 hUser32 = GetModuleHandle("user32.dll");
5109 if (hUser32 == NULL)
5110 goto fail;
5111
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005112 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5113 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5114 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005115 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005116 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5117 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005118
5119 if (pSetThreadDpiAwarenessContext != NULL)
5120 {
5121 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5122 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5123 if (oldctx != NULL)
5124 {
5125 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5126 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5127#ifdef DEBUG
5128 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5129 {
5130 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5131 }
5132#endif
5133 return;
5134 }
5135 }
5136
5137fail:
5138 // Disable PerMonitorV2 APIs.
5139 pGetDpiForSystem = stubGetDpiForSystem;
5140 pGetDpiForWindow = NULL;
5141 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5142 pSetThreadDpiAwarenessContext = NULL;
5143 pGetAwarenessFromDpiAwarenessContext = NULL;
5144}
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146/*
5147 * Initialise the GUI. Create all the windows, set up all the call-backs
5148 * etc.
5149 */
5150 int
5151gui_mch_init(void)
5152{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005154 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
Bram Moolenaar734a8672019-12-02 22:49:38 +01005157 // Return here if the window was already opened (happens when
5158 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005160 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162 /*
5163 * Load the tearoff bitmap
5164 */
5165#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005166 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#endif
5168
K.Takatac81e9bf2022-01-16 14:15:49 +00005169 load_dpi_func();
5170
5171 s_dpi = pGetDpiForSystem();
5172 update_scrollbar_size();
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005175 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#endif
5177 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005178#ifdef FEAT_TOOLBAR
5179 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
5182 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5183
Bram Moolenaar734a8672019-12-02 22:49:38 +01005184 // First try using the wide version, so that we can use any title.
5185 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005186 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005188 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 wndclassw.lpfnWndProc = _WndProc;
5190 wndclassw.cbClsExtra = 0;
5191 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005192 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5194 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5195 wndclassw.hbrBackground = s_brush;
5196 wndclassw.lpszMenuName = NULL;
5197 wndclassw.lpszClassName = szVimWndClassW;
5198
K.Takata4ac893f2022-01-20 12:44:28 +00005199 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (vim_parent_hwnd != NULL)
5204 {
5205#ifdef HAVE_TRY_EXCEPT
5206 __try
5207 {
5208#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005209 // Open inside the specified parent window.
5210 // TODO: last argument should point to a CLIENTCREATESTRUCT
5211 // structure.
5212 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005214 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005215 WS_OVERLAPPEDWINDOW | WS_CHILD
5216 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5218 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005219 100, // Any value will do
5220 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005222 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#ifdef HAVE_TRY_EXCEPT
5224 }
5225 __except(EXCEPTION_EXECUTE_HANDLER)
5226 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005227 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229#endif
5230 if (s_hwnd == NULL)
5231 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005232 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 mch_exit(2);
5234 }
5235 }
5236 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005237 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005238 // If the provided windowid is not valid reset it to zero, so that it
5239 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005240 if (IsWindow((HWND)win_socket_id) <= 0)
5241 win_socket_id = 0;
5242
Bram Moolenaar734a8672019-12-02 22:49:38 +01005243 // Create a window. If win_socket_id is not zero without border and
5244 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005245 s_hwnd = CreateWindowW(
5246 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005247 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5248 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005249 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5250 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005251 100, // Any value will do
5252 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005253 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005254 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005255 if (s_hwnd != NULL && win_socket_id != 0)
5256 {
5257 SetParent(s_hwnd, (HWND)win_socket_id);
5258 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5259 }
5260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 if (s_hwnd == NULL)
5263 return FAIL;
5264
K.Takatac81e9bf2022-01-16 14:15:49 +00005265 if (pGetDpiForWindow != NULL)
5266 {
5267 s_dpi = pGetDpiForWindow(s_hwnd);
5268 update_scrollbar_size();
5269 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5270 }
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5273 dyn_imm_load();
5274#endif
5275
Bram Moolenaar734a8672019-12-02 22:49:38 +01005276 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005277 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005278 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005279 wndclassw.style = CS_OWNDC;
5280 wndclassw.lpfnWndProc = _TextAreaWndProc;
5281 wndclassw.cbClsExtra = 0;
5282 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005283 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005284 wndclassw.hIcon = NULL;
5285 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5286 wndclassw.hbrBackground = NULL;
5287 wndclassw.lpszMenuName = NULL;
5288 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005289
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005290 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return FAIL;
5292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005294 s_textArea = CreateWindowExW(
5295 0,
5296 szTextAreaClassW, L"Vim text area",
5297 WS_CHILD | WS_VISIBLE, 0, 0,
5298 100, // Any value will do for now
5299 100, // Any value will do for now
5300 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005301 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (s_textArea == NULL)
5304 return FAIL;
5305
Bram Moolenaar20321902016-02-17 12:30:17 +01005306#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005307 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005308 {
5309 HANDLE hIcon = NULL;
5310
5311 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005312 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005313 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005314#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005315
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#ifdef FEAT_MENU
5317 s_menuBar = CreateMenu();
5318#endif
5319 s_hdc = GetDC(s_textArea);
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
Bram Moolenaar734a8672019-12-02 22:49:38 +01005323 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005324 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
Bram Moolenaar734a8672019-12-02 22:49:38 +01005326 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 gui_mch_def_colors();
5328
Bram Moolenaar734a8672019-12-02 22:49:38 +01005329 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5330 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 set_normal_colors();
5332
5333 /*
5334 * Check that none of the colors are the same as the background color.
5335 * Then store the current values as the defaults.
5336 */
5337 gui_check_colors();
5338 gui.def_norm_pixel = gui.norm_pixel;
5339 gui.def_back_pixel = gui.back_pixel;
5340
Bram Moolenaar734a8672019-12-02 22:49:38 +01005341 // Get the colors for the highlight groups (gui_check_colors() might have
5342 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 highlight_gui_started();
5344
5345 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005346 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005348 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350 /*
5351 * Set up for Intellimouse processing
5352 */
5353 init_mouse_wheel();
5354
5355 /*
5356 * compute a couple of metrics used for the dialogs
5357 */
5358 get_dialog_font_metrics();
5359#ifdef FEAT_TOOLBAR
5360 /*
5361 * Create the toolbar
5362 */
5363 initialise_toolbar();
5364#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005365#ifdef FEAT_GUI_TABLINE
5366 /*
5367 * Create the tabline
5368 */
5369 initialise_tabline();
5370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef MSWIN_FIND_REPLACE
5372 /*
5373 * Initialise the dialog box stuff
5374 */
5375 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5376
Bram Moolenaar734a8672019-12-02 22:49:38 +01005377 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005379 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005381 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5383 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5384 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005387#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005388 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005389 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005390#endif
5391
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005392#ifdef FEAT_RENDER_OPTIONS
5393 if (p_rop)
5394 (void)gui_mch_set_rendering_options(p_rop);
5395#endif
5396
Bram Moolenaar748bf032005-02-02 23:04:36 +00005397theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005398 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005399 display_errors();
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 return OK;
5402}
5403
5404/*
5405 * Get the size of the screen, taking position on multiple monitors into
5406 * account (if supported).
5407 */
5408 static void
5409get_work_area(RECT *spi_rect)
5410{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005411 HMONITOR mon;
5412 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaar734a8672019-12-02 22:49:38 +01005414 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005415 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005416 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005418 moninfo.cbSize = sizeof(MONITORINFO);
5419 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005421 *spi_rect = moninfo.rcWork;
5422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005425 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5427}
5428
5429/*
5430 * Set the size of the window to the given width and height in pixels.
5431 */
5432 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005433gui_mch_set_shellsize(
5434 int width,
5435 int height,
5436 int min_width UNUSED,
5437 int min_height UNUSED,
5438 int base_width UNUSED,
5439 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005440 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005443 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar734a8672019-12-02 22:49:38 +01005446 // Try to keep window completely on screen.
5447 // Get position of the screen work area. This is the part that is not
5448 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 get_work_area(&workarea_rect);
5450
Bram Moolenaar734a8672019-12-02 22:49:38 +01005451 // Resizing a maximized window looks very strange, unzoom it first.
5452 // But don't do it when still starting up, it may have been requested in
5453 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005454 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005456
5457 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
Bram Moolenaar734a8672019-12-02 22:49:38 +01005459 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005460 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5461 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5462 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5463 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5464 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5465 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar734a8672019-12-02 22:49:38 +01005467 // The following should take care of keeping Vim on the same monitor, no
5468 // matter if the secondary monitor is left or right of the primary
5469 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005470 window_rect.right = window_rect.left + win_width;
5471 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005472
Bram Moolenaar734a8672019-12-02 22:49:38 +01005473 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005474 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5475 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005477 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5478 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005480 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5481 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005483 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5484 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005486 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5487 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 SetActiveWindow(s_hwnd);
5490 SetFocus(s_hwnd);
5491
Bram Moolenaar734a8672019-12-02 22:49:38 +01005492 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494}
5495
5496
5497 void
5498gui_mch_set_scrollbar_thumb(
5499 scrollbar_T *sb,
5500 long val,
5501 long size,
5502 long max)
5503{
5504 SCROLLINFO info;
5505
5506 sb->scroll_shift = 0;
5507 while (max > 32767)
5508 {
5509 max = (max + 1) >> 1;
5510 val >>= 1;
5511 size >>= 1;
5512 ++sb->scroll_shift;
5513 }
5514
5515 if (sb->scroll_shift > 0)
5516 ++size;
5517
5518 info.cbSize = sizeof(info);
5519 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5520 info.nPos = val;
5521 info.nMin = 0;
5522 info.nMax = max;
5523 info.nPage = size;
5524 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5525}
5526
5527
5528/*
5529 * Set the current text font.
5530 */
5531 void
5532gui_mch_set_font(GuiFont font)
5533{
5534 gui.currFont = font;
5535}
5536
5537
5538/*
5539 * Set the current text foreground color.
5540 */
5541 void
5542gui_mch_set_fg_color(guicolor_T color)
5543{
5544 gui.currFgColor = color;
5545}
5546
5547/*
5548 * Set the current text background color.
5549 */
5550 void
5551gui_mch_set_bg_color(guicolor_T color)
5552{
5553 gui.currBgColor = color;
5554}
5555
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005556/*
5557 * Set the current text special color.
5558 */
5559 void
5560gui_mch_set_sp_color(guicolor_T color)
5561{
5562 gui.currSpColor = color;
5563}
5564
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005565#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566/*
5567 * Multi-byte handling, originally by Sung-Hoon Baek.
5568 * First static functions (no prototypes generated).
5569 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005570# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005571# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005572# endif
5573# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 * handle WM_IME_NOTIFY message
5577 */
5578 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005579_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 LRESULT lResult = 0;
5582 HIMC hImc;
5583
5584 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5585 return lResult;
5586 switch (dwCommand)
5587 {
5588 case IMN_SETOPENSTATUS:
5589 if (pImmGetOpenStatus(hImc))
5590 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005591 LOGFONTW lf = norm_logfont;
5592 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5593 // Work around when PerMonitorV2 is not enabled in the process level.
5594 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5595 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 im_set_position(gui.row, gui.col);
5597
Bram Moolenaar734a8672019-12-02 22:49:38 +01005598 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005599 State &= ~MODE_LANGMAP;
5600 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005602# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005603 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5605 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005606 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int old_row = gui.row;
5608 int old_col = gui.col;
5609
5610 // This must be called here before
5611 // status_redraw_curbuf(), otherwise the mode
5612 // message may appear in the wrong position.
5613 showmode();
5614 status_redraw_curbuf();
5615 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005616 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 gui.row = old_row;
5618 gui.col = old_col;
5619 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
5622 }
5623 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005624 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 lResult = 0;
5626 break;
5627 }
5628 pImmReleaseContext(hWnd, hImc);
5629 return lResult;
5630}
5631
5632 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005633_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 char_u *ret;
5636 int len;
5637
Bram Moolenaar734a8672019-12-02 22:49:38 +01005638 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 return 0;
5640
5641 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5642 if (ret != NULL)
5643 {
5644 add_to_input_buf_csi(ret, len);
5645 vim_free(ret);
5646 return 1;
5647 }
5648 return 0;
5649}
5650
5651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 * void GetResultStr()
5653 *
5654 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5655 * get complete composition string
5656 */
5657 static char_u *
5658GetResultStr(HWND hwnd, int GCS, int *lenp)
5659{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005660 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005661 LONG ret;
5662 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 char_u *convbuf = NULL;
5664
5665 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5666 return NULL;
5667
K.Takatab0b2b732022-01-19 12:59:21 +00005668 // Get the length of the composition string.
5669 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5670 if (ret <= 0)
5671 return NULL;
5672
5673 // Allocate the requested buffer plus space for the NUL character.
5674 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (buf == NULL)
5676 return NULL;
5677
K.Takatab0b2b732022-01-19 12:59:21 +00005678 // Reads in the composition string.
5679 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5680 *lenp = ret / sizeof(WCHAR);
5681
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005682 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 pImmReleaseContext(hwnd, hIMC);
5684 vim_free(buf);
5685 return convbuf;
5686}
5687#endif
5688
Bram Moolenaar734a8672019-12-02 22:49:38 +01005689// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005690#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692/*
5693 * set font to IM.
5694 */
5695 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005696im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 HIMC hImc;
5699
5700 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5701 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005702 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 pImmReleaseContext(s_hwnd, hImc);
5704 }
5705}
5706
5707/*
5708 * Notify cursor position to IM.
5709 */
5710 void
5711im_set_position(int row, int col)
5712{
5713 HIMC hImc;
5714
5715 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5716 {
5717 COMPOSITIONFORM cfs;
5718
5719 cfs.dwStyle = CFS_POINT;
5720 cfs.ptCurrentPos.x = FILL_X(col);
5721 cfs.ptCurrentPos.y = FILL_Y(row);
5722 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005723 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5724 {
5725 // Work around when PerMonitorV2 is not enabled in the process level.
5726 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5727 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 pImmSetCompositionWindow(hImc, &cfs);
5730
5731 pImmReleaseContext(s_hwnd, hImc);
5732 }
5733}
5734
5735/*
5736 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5737 */
5738 void
5739im_set_active(int active)
5740{
5741 HIMC hImc;
5742 static HIMC hImcOld = (HIMC)0;
5743
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005744# ifdef VIMDLL
5745 if (!gui.in_use && !gui.starting)
5746 {
5747 mbyte_im_set_active(active);
5748 return;
5749 }
5750# endif
5751
Bram Moolenaar734a8672019-12-02 22:49:38 +01005752 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (p_imdisable)
5755 {
5756 if (hImcOld == (HIMC)0)
5757 {
5758 hImcOld = pImmGetContext(s_hwnd);
5759 if (hImcOld)
5760 pImmAssociateContext(s_hwnd, (HIMC)0);
5761 }
5762 active = FALSE;
5763 }
5764 else if (hImcOld != (HIMC)0)
5765 {
5766 pImmAssociateContext(s_hwnd, hImcOld);
5767 hImcOld = (HIMC)0;
5768 }
5769
5770 hImc = pImmGetContext(s_hwnd);
5771 if (hImc)
5772 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005773 /*
5774 * for Korean ime
5775 */
5776 HKL hKL = GetKeyboardLayout(0);
5777
5778 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5779 {
5780 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5781 static BOOL bSaved = FALSE;
5782
5783 if (active)
5784 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005785 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005786 if (bSaved)
5787 pImmSetConversionStatus(hImc, dwConversionSaved,
5788 dwSentenceSaved);
5789 bSaved = FALSE;
5790 }
5791 else
5792 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005793 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005794 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5795 &dwSentenceSaved))
5796 {
5797 bSaved = TRUE;
5798 pImmSetConversionStatus(hImc,
5799 dwConversionSaved & ~(IME_CMODE_NATIVE
5800 | IME_CMODE_FULLSHAPE),
5801 dwSentenceSaved);
5802 }
5803 }
5804 }
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 pImmSetOpenStatus(hImc, active);
5807 pImmReleaseContext(s_hwnd, hImc);
5808 }
5809 }
5810}
5811
5812/*
5813 * Get IM status. When IM is on, return not 0. Else return 0.
5814 */
5815 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005816im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 int status = 0;
5819 HIMC hImc;
5820
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005821# ifdef VIMDLL
5822 if (!gui.in_use && !gui.starting)
5823 return mbyte_im_get_status();
5824# endif
5825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5827 {
5828 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5829 pImmReleaseContext(s_hwnd, hImc);
5830 }
5831 return status;
5832}
5833
Bram Moolenaar734a8672019-12-02 22:49:38 +01005834#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005837/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005838 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005839 */
5840 static void
5841latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5842{
5843 int c;
5844
Bram Moolenaarca003e12006-03-17 23:19:38 +00005845 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005846 {
5847 c = *text++;
5848 switch (c)
5849 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005850 case 0xa4: c = 0x20ac; break; // euro
5851 case 0xa6: c = 0x0160; break; // S hat
5852 case 0xa8: c = 0x0161; break; // S -hat
5853 case 0xb4: c = 0x017d; break; // Z hat
5854 case 0xb8: c = 0x017e; break; // Z -hat
5855 case 0xbc: c = 0x0152; break; // OE
5856 case 0xbd: c = 0x0153; break; // oe
5857 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005858 }
5859 *unicodebuf++ = c;
5860 }
5861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863#ifdef FEAT_RIGHTLEFT
5864/*
5865 * What is this for? In the case where you are using Win98 or Win2K or later,
5866 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5867 * reverses the string sent to the TextOut... family. This sucks, because we
5868 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5869 * way to tell Windblows not to do this!
5870 *
5871 * The short of it is that this 'RevOut' only gets called if you are running
5872 * one of the new, "improved" MS OSes, and only if you are running in
5873 * 'rightleft' mode. It makes display take *slightly* longer, but not
5874 * noticeably so.
5875 */
5876 static void
K.Takata135e1522022-01-29 15:27:58 +00005877RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 int col,
5879 int row,
5880 UINT foptions,
5881 CONST RECT *pcliprect,
5882 LPCTSTR text,
5883 UINT len,
5884 CONST INT *padding)
5885{
5886 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005888 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005889 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005890 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891}
5892#endif
5893
Bram Moolenaar92467d32017-12-05 13:22:16 +01005894 static void
5895draw_line(
5896 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005897 int y1,
5898 int x2,
5899 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005900 COLORREF color)
5901{
5902#if defined(FEAT_DIRECTX)
5903 if (IS_ENABLE_DIRECTX())
5904 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5905 else
5906#endif
5907 {
5908 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5909 HPEN old_pen = SelectObject(s_hdc, hpen);
5910 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005911 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005912 LineTo(s_hdc, x2, y2);
5913 DeleteObject(SelectObject(s_hdc, old_pen));
5914 }
5915}
5916
5917 static void
5918set_pixel(
5919 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005920 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005921 COLORREF color)
5922{
5923#if defined(FEAT_DIRECTX)
5924 if (IS_ENABLE_DIRECTX())
5925 DWriteContext_SetPixel(s_dwc, x, y, color);
5926 else
5927#endif
5928 SetPixel(s_hdc, x, y, color);
5929}
5930
5931 static void
5932fill_rect(
5933 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005934 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005935 COLORREF color)
5936{
5937#if defined(FEAT_DIRECTX)
5938 if (IS_ENABLE_DIRECTX())
5939 DWriteContext_FillRect(s_dwc, rcp, color);
5940 else
5941#endif
5942 {
5943 HBRUSH hbr2;
5944
5945 if (hbr == NULL)
5946 hbr2 = CreateSolidBrush(color);
5947 else
5948 hbr2 = hbr;
5949 FillRect(s_hdc, rcp, hbr2);
5950 if (hbr == NULL)
5951 DeleteBrush(hbr2);
5952 }
5953}
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 void
5956gui_mch_draw_string(
5957 int row,
5958 int col,
5959 char_u *text,
5960 int len,
5961 int flags)
5962{
5963 static int *padding = NULL;
5964 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 const RECT *pcliprect = NULL;
5966 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 static WCHAR *unicodebuf = NULL;
5968 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005969 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 int y;
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 /*
5974 * Italic and bold text seems to have an extra row of pixels at the bottom
5975 * (below where the bottom of the character should be). If we draw the
5976 * characters with a solid background, the top row of pixels in the
5977 * character below will be overwritten. We can fix this by filling in the
5978 * background ourselves, to the correct character proportions, and then
5979 * writing the character in transparent mode. Still have a problem when
5980 * the character is "_", which gets written on to the character below.
5981 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5982 * pixel in their slots, which fixes the problem with the bottom row of
5983 * pixels. We still need this code because otherwise the top row of pixels
5984 * becomes a problem. - webb.
5985 */
5986 static HBRUSH hbr_cache[2] = {NULL, NULL};
5987 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5988 static int brush_lru = 0;
5989 HBRUSH hbr;
5990 RECT rc;
5991
5992 if (!(flags & DRAW_TRANSP))
5993 {
5994 /*
5995 * Clear background first.
5996 * Note: FillRect() excludes right and bottom of rectangle.
5997 */
5998 rc.left = FILL_X(col);
5999 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (has_mbyte)
6001 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006002 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006003 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 }
6005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 rc.right = FILL_X(col + len);
6007 rc.bottom = FILL_Y(row + 1);
6008
Bram Moolenaar734a8672019-12-02 22:49:38 +01006009 // Cache the created brush, that saves a lot of time. We need two:
6010 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 if (gui.currBgColor == brush_color[0])
6012 {
6013 hbr = hbr_cache[0];
6014 brush_lru = 1;
6015 }
6016 else if (gui.currBgColor == brush_color[1])
6017 {
6018 hbr = hbr_cache[1];
6019 brush_lru = 0;
6020 }
6021 else
6022 {
6023 if (hbr_cache[brush_lru] != NULL)
6024 DeleteBrush(hbr_cache[brush_lru]);
6025 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6026 brush_color[brush_lru] = gui.currBgColor;
6027 hbr = hbr_cache[brush_lru];
6028 brush_lru = !brush_lru;
6029 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006030
Bram Moolenaar92467d32017-12-05 13:22:16 +01006031 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033 SetBkMode(s_hdc, TRANSPARENT);
6034
6035 /*
6036 * When drawing block cursor, prevent inverted character spilling
6037 * over character cell (can happen with bold/italic)
6038 */
6039 if (flags & DRAW_CURSOR)
6040 {
6041 pcliprect = &rc;
6042 foptions = ETO_CLIPPED;
6043 }
6044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 SetTextColor(s_hdc, gui.currFgColor);
6046 SelectFont(s_hdc, gui.currFont);
6047
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006048#ifdef FEAT_DIRECTX
6049 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006050 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006051#endif
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6054 {
K.Takata135e1522022-01-29 15:27:58 +00006055 int i;
6056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 vim_free(padding);
6058 pad_size = Columns;
6059
Bram Moolenaar734a8672019-12-02 22:49:38 +01006060 // Don't give an out-of-memory message here, it would call us
6061 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006062 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (padding != NULL)
6064 for (i = 0; i < pad_size; i++)
6065 padding[i] = gui.char_width;
6066 }
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 /*
6069 * We have to provide the padding argument because italic and bold versions
6070 * of fixed-width fonts are often one pixel or so wider than their normal
6071 * versions.
6072 * No check for DRAW_BOLD, Windows will have done it already.
6073 */
6074
Bram Moolenaar734a8672019-12-02 22:49:38 +01006075 // Check if there are any UTF-8 characters. If not, use normal text
6076 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (enc_utf8)
6078 for (n = 0; n < len; ++n)
6079 if (text[n] >= 0x80)
6080 break;
6081
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006082#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006083 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6084 // required that unicode drawing routine, currently. So this forces it
6085 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006086 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006087 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006088#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006089
Bram Moolenaar734a8672019-12-02 22:49:38 +01006090 // Check if the Unicode buffer exists and is big enough. Create it
6091 // with the same length as the multi-byte string, the number of wide
6092 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006093 if ((enc_utf8
6094 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6095 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 && (unicodebuf == NULL || len > unibuflen))
6097 {
6098 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006099 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006102 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 unibuflen = len;
6105 }
6106
6107 if (enc_utf8 && n < len && unicodebuf != NULL)
6108 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006109 // Output UTF-8 characters. Composing characters should be
6110 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006111 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006112 int wlen; // string length in words
6113 int clen; // string length in characters
6114 int cells; // cell width of string up to composing char
6115 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006116 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006118 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006119 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006121 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006123 c = utf_ptr2char(text + i);
6124 if (c >= 0x10000)
6125 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006126 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006127 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6128 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006129 }
6130 else
6131 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006132 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006133 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006134
6135 if (utf_iscomposing(c))
6136 cw = 0;
6137 else
6138 {
6139 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006140 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006141 cw = 1;
6142 }
6143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (unicodepdy != NULL)
6145 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006146 // Use unicodepdy to make characters fit as we expect, even
6147 // when the font uses different widths (e.g., bold character
6148 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006149 if (c >= 0x10000)
6150 {
6151 unicodepdy[wlen - 2] = cw * gui.char_width;
6152 unicodepdy[wlen - 1] = 0;
6153 }
6154 else
6155 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 }
6157 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006158 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006159 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006161#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006162 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006163 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006164 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006165 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006166 TEXT_X(col), TEXT_Y(row),
6167 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006168 gui.char_width, gui.currFgColor,
6169 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006170 }
6171 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006172#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006173 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6174 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006175 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006177 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179 // If we want to display codepage data, and the current CP is not the
6180 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 if (unicodebuf != NULL)
6182 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006183 if (enc_latin9)
6184 latin9_to_ucs(text, len, unicodebuf);
6185 else
6186 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 MB_PRECOMPOSED,
6188 (char *)text, len,
6189 (LPWSTR)unicodebuf, unibuflen);
6190 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006191 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006192 // Use unicodepdy to make characters fit as we expect, even
6193 // when the font uses different widths (e.g., bold character
6194 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006195 if (unicodepdy != NULL)
6196 {
6197 int i;
6198 int cw;
6199
6200 for (i = 0; i < len; ++i)
6201 {
6202 cw = utf_char2cells(unicodebuf[i]);
6203 if (cw > 2)
6204 cw = 1;
6205 unicodepdy[i] = cw * gui.char_width;
6206 }
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006209 foptions, pcliprect, unicodebuf, len, unicodepdy);
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
6212 }
6213 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006216 // Windows will mess up RL text, so we have to draw it character by
6217 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006218 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6220 foptions, pcliprect, (char *)text, len, padding);
6221 else
6222#endif
6223 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6224 foptions, pcliprect, (char *)text, len, padding);
6225 }
6226
Bram Moolenaar734a8672019-12-02 22:49:38 +01006227 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (flags & DRAW_UNDERL)
6229 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006230 // When p_linespace is 0, overwrite the bottom row of pixels.
6231 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (p_linespace > 1)
6234 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006235 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006237
Bram Moolenaar734a8672019-12-02 22:49:38 +01006238 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006239 if (flags & DRAW_STRIKE)
6240 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006241 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006242 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006243 }
6244
Bram Moolenaar734a8672019-12-02 22:49:38 +01006245 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006246 if (flags & DRAW_UNDERC)
6247 {
6248 int x;
6249 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006250 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006251
6252 y = FILL_Y(row + 1) - 1;
6253 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6254 {
6255 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006256 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006257 }
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259}
6260
6261
6262/*
6263 * Output routines.
6264 */
6265
Bram Moolenaar734a8672019-12-02 22:49:38 +01006266/*
6267 * Flush any output to the screen
6268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 void
6270gui_mch_flush(void)
6271{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006272#if defined(FEAT_DIRECTX)
6273 if (IS_ENABLE_DIRECTX())
6274 DWriteContext_Flush(s_dwc);
6275#endif
6276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 GdiFlush();
6278}
6279
6280 static void
6281clear_rect(RECT *rcp)
6282{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006283 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284}
6285
6286
Bram Moolenaarc716c302006-01-21 22:12:51 +00006287 void
6288gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6289{
6290 RECT workarea_rect;
6291
6292 get_work_area(&workarea_rect);
6293
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006294 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006295 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6296 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006297
Bram Moolenaar734a8672019-12-02 22:49:38 +01006298 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6299 // the menubar for MSwin, we subtract it from the screen height, so that
6300 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006301 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006302 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6303 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6304 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6305 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006306}
6307
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309#if defined(FEAT_MENU) || defined(PROTO)
6310/*
6311 * Add a sub menu to the menu bar.
6312 */
6313 void
6314gui_mch_add_menu(
6315 vimmenu_T *menu,
6316 int pos)
6317{
6318 vimmenu_T *parent = menu->parent;
6319
6320 menu->submenu_id = CreatePopupMenu();
6321 menu->id = s_menu_id++;
6322
6323 if (menu_is_menubar(menu->name))
6324 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006325 WCHAR *wn;
6326 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006328 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006329 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006330 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006332 infow.cbSize = sizeof(infow);
6333 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6334 | MIIM_SUBMENU;
6335 infow.dwItemData = (long_u)menu;
6336 infow.wID = menu->id;
6337 infow.fType = MFT_STRING;
6338 infow.dwTypeData = wn;
6339 infow.cch = (UINT)wcslen(wn);
6340 infow.hSubMenu = menu->submenu_id;
6341 InsertMenuItemW((parent == NULL)
6342 ? s_menuBar : parent->submenu_id,
6343 (UINT)pos, TRUE, &infow);
6344 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 }
6346
Bram Moolenaar734a8672019-12-02 22:49:38 +01006347 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (parent == NULL)
6349 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006350# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 else if (IsWindow(parent->tearoff_handle))
6352 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356 void
6357gui_mch_show_popupmenu(vimmenu_T *menu)
6358{
6359 POINT mp;
6360
K.Takata45f9cfb2022-01-21 11:11:00 +00006361 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6363}
6364
6365 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006366gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 vimmenu_T *menu = gui_find_menu(path_name);
6369
6370 if (menu != NULL)
6371 {
6372 POINT p;
6373
Bram Moolenaar734a8672019-12-02 22:49:38 +01006374 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006376 if (mouse_pos)
6377 {
6378 int mx, my;
6379
6380 gui_mch_getmouse(&mx, &my);
6381 p.x += mx;
6382 p.y += my;
6383 }
6384 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006386 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6388 }
6389 msg_scroll = FALSE;
6390 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6391 }
6392}
6393
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006394# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395/*
6396 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6397 * create it as a pseudo-"tearoff menu".
6398 */
6399 void
6400gui_make_tearoff(char_u *path_name)
6401{
6402 vimmenu_T *menu = gui_find_menu(path_name);
6403
Bram Moolenaar734a8672019-12-02 22:49:38 +01006404 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (menu != NULL)
6406 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6407}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410/*
6411 * Add a menu item to a menu
6412 */
6413 void
6414gui_mch_add_menu_item(
6415 vimmenu_T *menu,
6416 int idx)
6417{
6418 vimmenu_T *parent = menu->parent;
6419
6420 menu->id = s_menu_id++;
6421 menu->submenu_id = NULL;
6422
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006423# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6425 {
6426 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6427 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6428 }
6429 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006430# endif
6431# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (menu_is_toolbar(parent->name))
6433 {
6434 TBBUTTON newtb;
6435
Bram Moolenaara80faa82020-04-12 19:37:17 +02006436 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (menu_is_separator(menu->name))
6438 {
6439 newtb.iBitmap = 0;
6440 newtb.fsStyle = TBSTYLE_SEP;
6441 }
6442 else
6443 {
6444 newtb.iBitmap = get_toolbar_bitmap(menu);
6445 newtb.fsStyle = TBSTYLE_BUTTON;
6446 }
6447 newtb.idCommand = menu->id;
6448 newtb.fsState = TBSTATE_ENABLED;
6449 newtb.iString = 0;
6450 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6451 (LPARAM)&newtb);
6452 menu->submenu_id = (HMENU)-1;
6453 }
6454 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006457 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006459 wn = enc_to_utf16(menu->name, NULL);
6460 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006462 InsertMenuW(parent->submenu_id, (UINT)idx,
6463 (menu_is_separator(menu->name)
6464 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6465 (UINT)menu->id, wn);
6466 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006468# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 if (IsWindow(parent->tearoff_handle))
6470 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 }
6473}
6474
6475/*
6476 * Destroy the machine specific menu widget.
6477 */
6478 void
6479gui_mch_destroy_menu(vimmenu_T *menu)
6480{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006481# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 /*
6483 * is this a toolbar button?
6484 */
6485 if (menu->submenu_id == (HMENU)-1)
6486 {
6487 int iButton;
6488
6489 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6490 (WPARAM)menu->id, 0);
6491 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6492 }
6493 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
6496 if (menu->parent != NULL
6497 && menu_is_popup(menu->parent->dname)
6498 && menu->parent->submenu_id != NULL)
6499 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6500 else
6501 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6502 if (menu->submenu_id != NULL)
6503 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006504# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (IsWindow(menu->tearoff_handle))
6506 DestroyWindow(menu->tearoff_handle);
6507 if (menu->parent != NULL
6508 && menu->parent->children != NULL
6509 && IsWindow(menu->parent->tearoff_handle))
6510 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006511 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 menu->modes = 0;
6513 rebuild_tearoff(menu->parent);
6514 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
6517}
6518
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006519# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 static void
6521rebuild_tearoff(vimmenu_T *menu)
6522{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006523 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 char_u tbuf[128];
6525 RECT trect;
6526 RECT rct;
6527 RECT roct;
6528 int x, y;
6529
6530 HWND thwnd = menu->tearoff_handle;
6531
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006532 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (GetWindowRect(thwnd, &trect)
6534 && GetWindowRect(s_hwnd, &rct)
6535 && GetClientRect(s_hwnd, &roct))
6536 {
6537 x = trect.left - rct.left;
6538 y = (trect.top - rct.bottom + roct.bottom);
6539 }
6540 else
6541 {
6542 x = y = 0xffffL;
6543 }
6544 DestroyWindow(thwnd);
6545 if (menu->children != NULL)
6546 {
6547 gui_mch_tearoff(tbuf, menu, x, y);
6548 if (IsWindow(menu->tearoff_handle))
6549 (void) SetWindowPos(menu->tearoff_handle,
6550 NULL,
6551 (int)trect.left,
6552 (int)trect.top,
6553 0, 0,
6554 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6555 }
6556}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006557# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559/*
6560 * Make a menu either grey or not grey.
6561 */
6562 void
6563gui_mch_menu_grey(
6564 vimmenu_T *menu,
6565 int grey)
6566{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006567# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 /*
6569 * is this a toolbar button?
6570 */
6571 if (menu->submenu_id == (HMENU)-1)
6572 {
6573 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006574 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006577# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006578 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6579 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006581# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6583 {
6584 WORD menuID;
6585 HWND menuHandle;
6586
6587 /*
6588 * A tearoff button has changed state.
6589 */
6590 if (menu->children == NULL)
6591 menuID = (WORD)(menu->id);
6592 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006593 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6595 if (menuHandle)
6596 EnableWindow(menuHandle, !grey);
6597
6598 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600}
6601
Bram Moolenaar734a8672019-12-02 22:49:38 +01006602#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604
Bram Moolenaar734a8672019-12-02 22:49:38 +01006605// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006608#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
6610#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6611/*
6612 * stuff for dialogs
6613 */
6614
6615/*
6616 * The callback routine used by all the dialogs. Very simple. First,
6617 * acknowledges the INITDIALOG message so that Windows knows to do standard
6618 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6619 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6620 * number.
6621 */
6622 static LRESULT CALLBACK
6623dialog_callback(
6624 HWND hwnd,
6625 UINT message,
6626 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006627 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 if (message == WM_INITDIALOG)
6630 {
6631 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006632 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 (void)SetFocus(hwnd);
6634 if (dialog_default_button > IDCANCEL)
6635 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006636 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006637 // We don't have a default, set focus on another element of the
6638 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006639 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 return FALSE;
6641 }
6642
6643 if (message == WM_COMMAND)
6644 {
6645 int button = LOWORD(wParam);
6646
Bram Moolenaar734a8672019-12-02 22:49:38 +01006647 // Don't end the dialog if something was selected that was
6648 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 if (button >= DLG_NONBUTTON_CONTROL)
6650 return TRUE;
6651
Bram Moolenaar734a8672019-12-02 22:49:38 +01006652 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006654 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006655 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006656 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006657
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006658 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6659 p = utf16_to_enc(wp, NULL);
6660 vim_strncpy(s_textfield, p, IOSIZE);
6661 vim_free(p);
6662 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
6665 /*
6666 * Need to check for IDOK because if the user just hits Return to
6667 * accept the default value, some reason this is what we get.
6668 */
6669 if (button == IDOK)
6670 {
6671 if (dialog_default_button > IDCANCEL)
6672 EndDialog(hwnd, dialog_default_button);
6673 }
6674 else
6675 EndDialog(hwnd, button - IDCANCEL);
6676 return TRUE;
6677 }
6678
6679 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6680 {
6681 EndDialog(hwnd, 0);
6682 return TRUE;
6683 }
6684 return FALSE;
6685}
6686
6687/*
6688 * Create a dialog dynamically from the parameter strings.
6689 * type = type of dialog (question, alert, etc.)
6690 * title = dialog title. may be NULL for default title.
6691 * message = text to display. Dialog sizes to accommodate it.
6692 * buttons = '\n' separated list of button captions, default first.
6693 * dfltbutton = number of default button.
6694 *
6695 * This routine returns 1 if the first button is pressed,
6696 * 2 for the second, etc.
6697 *
6698 * 0 indicates Esc was pressed.
6699 * -1 for unexpected error
6700 *
6701 * If stubbing out this fn, return 1.
6702 */
6703
Bram Moolenaar734a8672019-12-02 22:49:38 +01006704static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 "IDR_VIM",
6707 "IDR_VIM_ERROR",
6708 "IDR_VIM_ALERT",
6709 "IDR_VIM_INFO",
6710 "IDR_VIM_QUESTION"
6711};
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 int
6714gui_mch_dialog(
6715 int type,
6716 char_u *title,
6717 char_u *message,
6718 char_u *buttons,
6719 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006720 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006721 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006724 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 int numButtons;
6726 int *buttonWidths, *buttonPositions;
6727 int buttonYpos;
6728 int nchar, i;
6729 DWORD lStyle;
6730 int dlgwidth = 0;
6731 int dlgheight;
6732 int editboxheight;
6733 int horizWidth = 0;
6734 int msgheight;
6735 char_u *pstart;
6736 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006737 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 char_u *tbuffer;
6739 RECT rect;
6740 HWND hwnd;
6741 HDC hdc;
6742 HFONT font, oldFont;
6743 TEXTMETRIC fontInfo;
6744 int fontHeight;
6745 int textWidth, minButtonWidth, messageWidth;
6746 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006747 int maxDialogHeight;
6748 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 int vertical;
6750 int dlgPaddingX;
6751 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006752# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006753 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006755# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006756 garray_T ga;
6757 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006758 int dlg_icon_width;
6759 int dlg_icon_height;
6760 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006762# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006763 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006764# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006765 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006766# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006767 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006768 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
Bram Moolenaar748bf032005-02-02 23:04:36 +00006771 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006772 {
6773 load_dpi_func();
6774 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006775 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006776 }
6777 else
6778 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
6780 if ((type < 0) || (type > VIM_LAST_TYPE))
6781 type = 0;
6782
Bram Moolenaar734a8672019-12-02 22:49:38 +01006783 // allocate some memory for dialog template
6784 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006785 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006786 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 if (p == NULL)
6789 return -1;
6790
6791 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006792 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6794 * const.
6795 */
6796 tbuffer = vim_strsave(buttons);
6797 if (tbuffer == NULL)
6798 return -1;
6799
Bram Moolenaar734a8672019-12-02 22:49:38 +01006800 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar734a8672019-12-02 22:49:38 +01006802 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 numButtons = 1;
6804 for (i = 0; tbuffer[i] != '\0'; i++)
6805 {
6806 if (tbuffer[i] == DLG_BUTTON_SEP)
6807 numButtons++;
6808 }
6809 if (dfltbutton >= numButtons)
6810 dfltbutton = -1;
6811
Bram Moolenaar734a8672019-12-02 22:49:38 +01006812 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006813 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 if (buttonWidths == NULL)
6815 return -1;
6816
Bram Moolenaar734a8672019-12-02 22:49:38 +01006817 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006818 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (buttonPositions == NULL)
6820 return -1;
6821
6822 /*
6823 * Calculate how big the dialog must be.
6824 */
6825 hwnd = GetDesktopWindow();
6826 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006827# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6829 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006830 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 use_lfSysmenu = TRUE;
6832 }
6833 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006834# endif
K.Takatad1c58992022-01-23 12:31:57 +00006835 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6836 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6837
6838 oldFont = SelectFont(hdc, font);
6839 dlgPaddingX = DLG_PADDING_X;
6840 dlgPaddingY = DLG_PADDING_Y;
6841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 GetTextMetrics(hdc, &fontInfo);
6843 fontHeight = fontInfo.tmHeight;
6844
Bram Moolenaar734a8672019-12-02 22:49:38 +01006845 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006846 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar734a8672019-12-02 22:49:38 +01006848 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006849 if (s_hwnd == NULL)
6850 {
6851 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
Bram Moolenaar734a8672019-12-02 22:49:38 +01006853 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006854 get_work_area(&workarea_rect);
6855 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006856 if (maxDialogWidth > adjust_by_system_dpi(600))
6857 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006858 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006859 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006860 }
6861 else
6862 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006863 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006864 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006865 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006866 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6867 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6868 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6869 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006870
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006871 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006872 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6873 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6874 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6875 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6876 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006877 }
6878
Bram Moolenaar734a8672019-12-02 22:49:38 +01006879 // Set dlgwidth to width of message.
6880 // Copy the message into "ga", changing NL to CR-NL and inserting line
6881 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 pstart = message;
6883 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006884 msgheight = 0;
6885 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 do
6887 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006888 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006889
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 // Need to figure out where to break the string. The system does it
6891 // at a word boundary, which would mean we can't compute the number of
6892 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006893 textWidth = 0;
6894 last_white = NULL;
6895 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006896 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006897 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006898 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006899 && textWidth > maxDialogWidth * 3 / 4)
6900 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006901 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006902 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006903 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006904 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006905 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006906 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006907 textWidth = 0;
6908
6909 if (last_white != NULL)
6910 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006912 if (pend > last_white)
6913 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006914 pend = last_white + 1;
6915 last_white = NULL;
6916 }
6917 ga_append(&ga, '\r');
6918 ga_append(&ga, '\n');
6919 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006920 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006921
6922 while (--l >= 0)
6923 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006924 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927
6928 ga_append(&ga, '\r');
6929 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 pstart = pend + 1;
6931 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006932
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006933 if (ga.ga_data != NULL)
6934 message = ga.ga_data;
6935
Bram Moolenaar734a8672019-12-02 22:49:38 +01006936 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006937
K.Takatac81e9bf2022-01-16 14:15:49 +00006938 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6939 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
K.Takatac81e9bf2022-01-16 14:15:49 +00006941 // Add width of icon to dlgwidth, and some space
6942 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6943 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6944
6945 if (msgheight < dlg_icon_height)
6946 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 /*
6949 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006950 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006952 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (!vertical)
6954 {
6955 // Place buttons horizontally if they fit.
6956 horizWidth = dlgPaddingX;
6957 pstart = tbuffer;
6958 i = 0;
6959 do
6960 {
6961 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6962 if (pend == NULL)
6963 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006964 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (textWidth < minButtonWidth)
6966 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006967 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 buttonWidths[i] = textWidth;
6969 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006970 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 pstart = pend + 1;
6972 } while (*pend != NUL);
6973
6974 if (horizWidth > maxDialogWidth)
6975 vertical = TRUE; // Too wide to fit on the screen.
6976 else if (horizWidth > dlgwidth)
6977 dlgwidth = horizWidth;
6978 }
6979
6980 if (vertical)
6981 {
6982 // Stack buttons vertically.
6983 pstart = tbuffer;
6984 do
6985 {
6986 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6987 if (pend == NULL)
6988 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006989 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 textWidth += dlgPaddingX; // Padding within button
6991 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (textWidth > dlgwidth)
6993 dlgwidth = textWidth;
6994 pstart = pend + 1;
6995 } while (*pend != NUL);
6996 }
6997
6998 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006999 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
Bram Moolenaar734a8672019-12-02 22:49:38 +01007001 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007002 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 add_long(lStyle);
7005 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007006 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 add_word(0); // NumberOfItems(will change later)
7008 add_word(10); // x
7009 add_word(10); // y
7010 add_word(PixelToDialogX(dlgwidth)); // cx
7011
7012 // Dialog height.
7013 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007014 dlgheight = msgheight + 2 * dlgPaddingY
7015 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 else
7017 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7018
7019 // Dialog needs to be taller if contains an edit box.
7020 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7021 if (textfield != NULL)
7022 dlgheight += editboxheight;
7023
Bram Moolenaar734a8672019-12-02 22:49:38 +01007024 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007025 if (dlgheight > maxDialogHeight)
7026 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007027 msgheight = msgheight - (dlgheight - maxDialogHeight);
7028 dlgheight = maxDialogHeight;
7029 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007030 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007031 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007032 }
7033
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 add_word(PixelToDialogY(dlgheight));
7035
7036 add_word(0); // Menu
7037 add_word(0); // Class
7038
Bram Moolenaar734a8672019-12-02 22:49:38 +01007039 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007040 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7041 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 p += nchar;
7043
K.Takatad1c58992022-01-23 12:31:57 +00007044 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007045# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007046 if (use_lfSysmenu)
7047 {
7048 // point size
7049 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7050 GetDeviceCaps(hdc, LOGPIXELSY));
7051 wcscpy(p, lfSysmenu.lfFaceName);
7052 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
K.Takatad1c58992022-01-23 12:31:57 +00007054 else
7055# endif
7056 {
7057 *p++ = DLG_FONT_POINT_SIZE; // point size
7058 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7059 }
7060 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
7062 buttonYpos = msgheight + 2 * dlgPaddingY;
7063
7064 if (textfield != NULL)
7065 buttonYpos += editboxheight;
7066
7067 pstart = tbuffer;
7068 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007069 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 for (i = 0; i < numButtons; i++)
7071 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007072 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 for ( pend = pstart;
7074 *pend && (*pend != DLG_BUTTON_SEP);
7075 pend++)
7076 ;
7077
7078 if (*pend)
7079 *pend = '\0';
7080
7081 /*
7082 * old NOTE:
7083 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7084 * the focus to the first tab-able button and in so doing makes that
7085 * the default!! Grrr. Workaround: Make the default button the only
7086 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7087 * he/she can use arrow keys.
7088 *
7089 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007090 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 * dialog. Also needed for when the textfield is the default control.
7092 * It appears to work now (perhaps not on Win95?).
7093 */
7094 if (vertical)
7095 {
7096 p = add_dialog_element(p,
7097 (i == dfltbutton
7098 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7099 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007100 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 + 2 * fontHeight * i),
7102 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7103 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007104 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
7106 else
7107 {
7108 p = add_dialog_element(p,
7109 (i == dfltbutton
7110 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7111 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007112 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 PixelToDialogX(buttonWidths[i]),
7114 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007115 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007117 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119 *pnumitems += numButtons;
7120
Bram Moolenaar734a8672019-12-02 22:49:38 +01007121 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 p = add_dialog_element(p, SS_ICON,
7123 PixelToDialogX(dlgPaddingX),
7124 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007125 PixelToDialogX(dlg_icon_width),
7126 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7128 dlg_icons[type]);
7129
Bram Moolenaar734a8672019-12-02 22:49:38 +01007130 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007131 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007132 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007133 PixelToDialogY(dlgPaddingY),
7134 (WORD)(PixelToDialogX(messageWidth) + 1),
7135 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007136 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
Bram Moolenaar734a8672019-12-02 22:49:38 +01007138 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (textfield != NULL)
7140 {
7141 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7142 PixelToDialogX(2 * dlgPaddingX),
7143 PixelToDialogY(2 * dlgPaddingY + msgheight),
7144 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7145 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007146 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 *pnumitems += 1;
7148 }
7149
7150 *pnumitems += 2;
7151
7152 SelectFont(hdc, oldFont);
7153 DeleteObject(font);
7154 ReleaseDC(hwnd, hdc);
7155
Bram Moolenaar734a8672019-12-02 22:49:38 +01007156 // Let the dialog_callback() function know which button to make default
7157 // If we have an edit box, make that the default. We also need to tell
7158 // dialog_callback() if this dialog contains an edit box or not. We do
7159 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (textfield != NULL)
7161 {
7162 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7163 s_textfield = textfield;
7164 }
7165 else
7166 {
7167 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7168 s_textfield = NULL;
7169 }
7170
Bram Moolenaar734a8672019-12-02 22:49:38 +01007171 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007173 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 (LPDLGTEMPLATE)pdlgtemplate,
7175 s_hwnd,
7176 (DLGPROC)dialog_callback);
7177
7178 LocalFree(LocalHandle(pdlgtemplate));
7179 vim_free(tbuffer);
7180 vim_free(buttonWidths);
7181 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007182 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaar734a8672019-12-02 22:49:38 +01007184 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 (void)SetFocus(s_hwnd);
7186
7187 return nchar;
7188}
7189
Bram Moolenaar734a8672019-12-02 22:49:38 +01007190#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192/*
7193 * Put a simple element (basic class) onto a dialog template in memory.
7194 * return a pointer to where the next item should be added.
7195 *
7196 * parameters:
7197 * lStyle = additional style flags
7198 * (be careful, NT3.51 & Win32s will ignore the new ones)
7199 * x,y = x & y positions IN DIALOG UNITS
7200 * w,h = width and height IN DIALOG UNITS
7201 * Id = ID used in messages
7202 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7203 * caption = usually text or resource name
7204 *
7205 * TODO: use the length information noted here to enable the dialog creation
7206 * routines to work out more exactly how much memory they need to alloc.
7207 */
7208 static PWORD
7209add_dialog_element(
7210 PWORD p,
7211 DWORD lStyle,
7212 WORD x,
7213 WORD y,
7214 WORD w,
7215 WORD h,
7216 WORD Id,
7217 WORD clss,
7218 const char *caption)
7219{
7220 int nchar;
7221
Bram Moolenaar734a8672019-12-02 22:49:38 +01007222 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7224 *p++ = LOWORD(lStyle);
7225 *p++ = HIWORD(lStyle);
7226 *p++ = 0; // LOWORD (lExtendedStyle)
7227 *p++ = 0; // HIWORD (lExtendedStyle)
7228 *p++ = x;
7229 *p++ = y;
7230 *p++ = w;
7231 *p++ = h;
7232 *p++ = Id; //9 or 10 words in all
7233
7234 *p++ = (WORD)0xffff;
7235 *p++ = clss; //2 more here
7236
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007237 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 p += nchar;
7239
7240 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7241
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007242 return p; // total = 15 + strlen(caption) words
7243 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244}
7245
7246
7247/*
7248 * Helper routine. Take an input pointer, return closest pointer that is
7249 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7250 */
7251 static LPWORD
7252lpwAlign(
7253 LPWORD lpIn)
7254{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007255 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007257 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 ul += 3;
7259 ul >>= 2;
7260 ul <<= 2;
7261 return (LPWORD)ul;
7262}
7263
7264/*
7265 * Helper routine. Takes second parameter as Ansi string, copies it to first
7266 * parameter as wide character (16-bits / char) string, and returns integer
7267 * number of wide characters (words) in string (including the trailing wide
7268 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007269 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7270 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 static int
7272nCopyAnsiToWideChar(
7273 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007274 LPSTR lpAnsiIn,
7275 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007278 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 int i;
7280 WCHAR *wn;
7281
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007282 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007284 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007285 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 if (wn != NULL)
7287 {
7288 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 vim_free(wn);
7291 }
7292 }
7293 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007294 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 nChar = MultiByteToWideChar(
7296 enc_codepage > 0 ? enc_codepage : CP_ACP,
7297 MB_PRECOMPOSED,
7298 lpAnsiIn, len,
7299 lpWCStr, len);
7300 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007301 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304 return nChar;
7305}
7306
7307
7308#ifdef FEAT_TEAROFF
7309/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007310 * Lookup menu handle from "menu_id".
7311 */
7312 static HMENU
7313tearoff_lookup_menuhandle(
7314 vimmenu_T *menu,
7315 WORD menu_id)
7316{
7317 for ( ; menu != NULL; menu = menu->next)
7318 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007319 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007320 continue;
7321 if (menu_is_separator(menu->dname))
7322 continue;
7323 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7324 return menu->submenu_id;
7325 }
7326 return NULL;
7327}
7328
7329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 * The callback function for all the modeless dialogs that make up the
7331 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7332 * thinking its menus have been clicked), and go away when closed.
7333 */
7334 static LRESULT CALLBACK
7335tearoff_callback(
7336 HWND hwnd,
7337 UINT message,
7338 WPARAM wParam,
7339 LPARAM lParam)
7340{
7341 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007342 {
7343 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
Bram Moolenaar734a8672019-12-02 22:49:38 +01007347 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 HandleMouseHide(message, lParam);
7349
7350 if (message == WM_COMMAND)
7351 {
7352 if ((WORD)(LOWORD(wParam)) & 0x8000)
7353 {
7354 POINT mp;
7355 RECT rect;
7356
7357 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7358 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007359 vimmenu_T *menu;
7360
7361 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007363 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7365 (int)rect.right - 8,
7366 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007367 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 s_hwnd,
7369 NULL);
7370 /*
7371 * NOTE: The pop-up menu can eat the mouse up event.
7372 * We deal with this in normal.c.
7373 */
7374 }
7375 }
7376 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007377 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7379 /*
7380 * Give main window the focus back: this is so after
7381 * choosing a tearoff button you can start typing again
7382 * straight away.
7383 */
7384 (void)SetFocus(s_hwnd);
7385 return TRUE;
7386 }
7387 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7388 {
7389 DestroyWindow(hwnd);
7390 return TRUE;
7391 }
7392
Bram Moolenaar734a8672019-12-02 22:49:38 +01007393 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (message == WM_EXITSIZEMOVE)
7395 (void)SetActiveWindow(s_hwnd);
7396
7397 return FALSE;
7398}
7399#endif
7400
7401
7402/*
K.Takatad1c58992022-01-23 12:31:57 +00007403 * Computes the dialog base units based on the current dialog font.
7404 * We don't use the GetDialogBaseUnits() API, because we don't use the
7405 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 */
7407 static void
7408get_dialog_font_metrics(void)
7409{
7410 HDC hdc;
7411 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 SIZE size;
7413#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007414 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#endif
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007418 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007419 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421#endif
7422 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007423 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
K.Takatad1c58992022-01-23 12:31:57 +00007425 hdc = GetDC(s_hwnd);
7426 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007427 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007428 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
K.Takataabe628e2022-01-23 16:25:17 +00007430 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007431 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7435/*
7436 * Create a pseudo-"tearoff menu" based on the child
7437 * items of a given menu pointer.
7438 */
7439 static void
7440gui_mch_tearoff(
7441 char_u *title,
7442 vimmenu_T *menu,
7443 int initX,
7444 int initY)
7445{
7446 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7447 int template_len;
7448 int nchar, textWidth, submenuWidth;
7449 DWORD lStyle;
7450 DWORD lExtendedStyle;
7451 WORD dlgwidth;
7452 WORD menuID;
7453 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007454 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 vimmenu_T *the_menu = menu;
7456 HWND hwnd;
7457 HDC hdc;
7458 HFONT font, oldFont;
7459 int col, spaceWidth, len;
7460 int columnWidths[2];
7461 char_u *label, *text;
7462 int acLen = 0;
7463 int nameLen;
7464 int padding0, padding1, padding2 = 0;
7465 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007466 int x;
7467 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007468# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007469 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473 /*
7474 * If this menu is already torn off, move it to the mouse position.
7475 */
7476 if (IsWindow(menu->tearoff_handle))
7477 {
7478 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007479 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {
7481 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7482 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7483 }
7484 return;
7485 }
7486
7487 /*
7488 * Create a new tearoff.
7489 */
7490 if (*title == MNU_HIDDEN_CHAR)
7491 title++;
7492
Bram Moolenaar734a8672019-12-02 22:49:38 +01007493 // Allocate memory to store the dialog template. It's made bigger when
7494 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 template_len = DLG_ALLOC_SIZE;
7496 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7497 if (p == NULL)
7498 return;
7499
7500 hwnd = GetDesktopWindow();
7501 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007502# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7504 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007505 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 use_lfSysmenu = TRUE;
7507 }
7508 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007509# endif
K.Takatad1c58992022-01-23 12:31:57 +00007510 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7511 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7512
7513 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
Bram Moolenaar734a8672019-12-02 22:49:38 +01007515 // Calculate width of a single space. Used for padding columns to the
7516 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007517 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
Bram Moolenaar734a8672019-12-02 22:49:38 +01007519 // Figure out max width of the text column, the accelerator column and the
7520 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 submenuWidth = 0;
7522 for (col = 0; col < 2; col++)
7523 {
7524 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007525 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007527 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 text = (col == 0) ? pmenu->dname : pmenu->actext;
7529 if (text != NULL && *text != NUL)
7530 {
7531 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7532 if (textWidth > columnWidths[col])
7533 columnWidths[col] = textWidth;
7534 }
7535 if (pmenu->children != NULL)
7536 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7537 }
7538 }
7539 if (columnWidths[1] == 0)
7540 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007541 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 if (submenuWidth != 0)
7543 columnWidths[0] += submenuWidth;
7544 else
7545 columnWidths[0] += spaceWidth;
7546 }
7547 else
7548 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007549 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7551 columnWidths[1] += submenuWidth;
7552 }
7553
7554 /*
7555 * Now find the total width of our 'menu'.
7556 */
7557 textWidth = columnWidths[0] + columnWidths[1];
7558 if (submenuWidth != 0)
7559 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007560 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7562 textWidth += submenuWidth;
7563 }
7564 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7565 if (textWidth > dlgwidth)
7566 dlgwidth = textWidth;
7567 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7568
Bram Moolenaar734a8672019-12-02 22:49:38 +01007569 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007570 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7573 *p++ = LOWORD(lStyle);
7574 *p++ = HIWORD(lStyle);
7575 *p++ = LOWORD(lExtendedStyle);
7576 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007577 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007579 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007581 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 else
7583 *p++ = PixelToDialogX(initX); // x
7584 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007585 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 else
7587 *p++ = PixelToDialogY(initY); // y
7588 *p++ = PixelToDialogX(dlgwidth); // cx
7589 ptrueheight = p;
7590 *p++ = 0; // dialog height: changed later anyway
7591 *p++ = 0; // Menu
7592 *p++ = 0; // Class
7593
Bram Moolenaar734a8672019-12-02 22:49:38 +01007594 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007596 ? (LPSTR)title
7597 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 p += nchar;
7599
K.Takatad1c58992022-01-23 12:31:57 +00007600 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007601# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007602 if (use_lfSysmenu)
7603 {
7604 // point size
7605 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7606 GetDeviceCaps(hdc, LOGPIXELSY));
7607 wcscpy(p, lfSysmenu.lfFaceName);
7608 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
K.Takatad1c58992022-01-23 12:31:57 +00007610 else
7611# endif
7612 {
7613 *p++ = DLG_FONT_POINT_SIZE; // point size
7614 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7615 }
7616 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
7618 /*
7619 * Loop over all the items in the menu.
7620 * But skip over the tearbar.
7621 */
7622 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7623 menu = menu->children->next;
7624 else
7625 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007626 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 for ( ; menu != NULL; menu = menu->next)
7628 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007629 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 continue;
7631 if (menu_is_separator(menu->dname))
7632 {
7633 sepPadding += 3;
7634 continue;
7635 }
7636
Bram Moolenaar734a8672019-12-02 22:49:38 +01007637 // Check if there still is plenty of room in the template. Make it
7638 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7640 {
7641 WORD *newp;
7642
7643 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7644 if (newp != NULL)
7645 {
7646 template_len += 4096;
7647 mch_memmove(newp, pdlgtemplate,
7648 (char *)p - (char *)pdlgtemplate);
7649 p = newp + (p - pdlgtemplate);
7650 pnumitems = newp + (pnumitems - pdlgtemplate);
7651 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7652 LocalFree(LocalHandle(pdlgtemplate));
7653 pdlgtemplate = newp;
7654 }
7655 }
7656
Bram Moolenaar734a8672019-12-02 22:49:38 +01007657 // Figure out minimal length of this menu label. Use "name" for the
7658 // actual text, "dname" for estimating the displayed size. "name"
7659 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 len = nameLen = (int)STRLEN(menu->name);
7661 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7662 (int)STRLEN(menu->dname))) / spaceWidth;
7663 len += padding0;
7664
7665 if (menu->actext != NULL)
7666 {
7667 acLen = (int)STRLEN(menu->actext);
7668 len += acLen;
7669 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7670 }
7671 else
7672 textWidth = 0;
7673 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7674 len += padding1;
7675
7676 if (menu->children == NULL)
7677 {
7678 padding2 = submenuWidth / spaceWidth;
7679 len += padding2;
7680 menuID = (WORD)(menu->id);
7681 }
7682 else
7683 {
7684 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007685 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687
Bram Moolenaar734a8672019-12-02 22:49:38 +01007688 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007689 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (label == NULL)
7691 break;
7692
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007693 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007694 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007696 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 while (padding0-- > 0)
7698 *text++ = ' ';
7699 if (menu->actext != NULL)
7700 {
7701 STRNCPY(text, menu->actext, acLen);
7702 text += acLen;
7703 }
7704 while (padding1-- > 0)
7705 *text++ = ' ';
7706 if (menu->children != NULL)
7707 {
7708 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7709 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7710 }
7711 else
7712 {
7713 while (padding2-- > 0)
7714 *text++ = ' ';
7715 }
7716 *text = NUL;
7717
7718 /*
7719 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7720 * W95/NT4 it makes the tear-off look more like a menu.
7721 */
7722 p = add_dialog_element(p,
7723 BS_PUSHBUTTON|BS_LEFT,
7724 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7725 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7726 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7727 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007728 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 vim_free(label);
7730 (*pnumitems)++;
7731 }
7732
7733 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7734
7735
Bram Moolenaar734a8672019-12-02 22:49:38 +01007736 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007737 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007738 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 (LPDLGTEMPLATE)pdlgtemplate,
7740 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007741 (DLGPROC)tearoff_callback,
7742 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
7744 LocalFree(LocalHandle(pdlgtemplate));
7745 SelectFont(hdc, oldFont);
7746 DeleteObject(font);
7747 ReleaseDC(hwnd, hdc);
7748
7749 /*
7750 * Reassert ourselves as the active window. This is so that after creating
7751 * a tearoff, the user doesn't have to click with the mouse just to start
7752 * typing again!
7753 */
7754 (void)SetActiveWindow(s_hwnd);
7755
Bram Moolenaar734a8672019-12-02 22:49:38 +01007756 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 force_menu_update = TRUE;
7758}
7759#endif
7760
7761#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007762# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764/*
7765 * Create the toolbar, initially unpopulated.
7766 * (just like the menu, there are no defaults, it's all
7767 * set up through menu.vim)
7768 */
7769 static void
7770initialise_toolbar(void)
7771{
7772 InitCommonControls();
7773 s_toolbarhwnd = CreateToolbarEx(
7774 s_hwnd,
7775 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7776 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007777 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007778 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 IDR_TOOLBAR1, // id of initial bitmap
7780 NULL,
7781 0, // initial number of buttons
7782 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7783 TOOLBAR_BUTTON_HEIGHT,
7784 TOOLBAR_BUTTON_WIDTH,
7785 TOOLBAR_BUTTON_HEIGHT,
7786 sizeof(TBBUTTON)
7787 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007788
7789 // Remove transparency from the toolbar to prevent the main window
7790 // background colour showing through
7791 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7792 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7793
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007794 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
7796 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007797
7798 update_toolbar_size();
7799}
7800
7801 static void
7802update_toolbar_size(void)
7803{
7804 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007805 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007806
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007807 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007808 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7809 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7810 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7811 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7812
7813 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7814 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7815 //TRACE("button size: %d, %d", w, h);
7816 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7817 gui.toolbar_height = h + 6;
7818
7819 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7820 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7821
7822 // TODO:
7823 // Currently, this function only updates the size of toolbar buttons.
7824 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825}
7826
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007827 static LRESULT CALLBACK
7828toolbar_wndproc(
7829 HWND hwnd,
7830 UINT uMsg,
7831 WPARAM wParam,
7832 LPARAM lParam)
7833{
7834 HandleMouseHide(uMsg, lParam);
7835 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7836}
7837
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 static int
7839get_toolbar_bitmap(vimmenu_T *menu)
7840{
7841 int i = -1;
7842
7843 /*
7844 * Check user bitmaps first, unless builtin is specified.
7845 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007846 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
7848 char_u fname[MAXPATHL];
7849 HANDLE hbitmap = NULL;
7850
7851 if (menu->iconfile != NULL)
7852 {
7853 gui_find_iconfile(menu->iconfile, fname, "bmp");
7854 hbitmap = LoadImage(
7855 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007856 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 IMAGE_BITMAP,
7858 TOOLBAR_BUTTON_WIDTH,
7859 TOOLBAR_BUTTON_HEIGHT,
7860 LR_LOADFROMFILE |
7861 LR_LOADMAP3DCOLORS
7862 );
7863 }
7864
7865 /*
7866 * If the LoadImage call failed, or the "icon=" file
7867 * didn't exist or wasn't specified, try the menu name
7868 */
7869 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007870 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007871# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007872 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007873# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007874 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 hbitmap = LoadImage(
7876 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007877 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 IMAGE_BITMAP,
7879 TOOLBAR_BUTTON_WIDTH,
7880 TOOLBAR_BUTTON_HEIGHT,
7881 LR_LOADFROMFILE |
7882 LR_LOADMAP3DCOLORS
7883 );
7884
7885 if (hbitmap != NULL)
7886 {
7887 TBADDBITMAP tbAddBitmap;
7888
7889 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007890 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
7892 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7893 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007894 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 }
7897 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7898 i = menu->iconidx;
7899
7900 return i;
7901}
7902#endif
7903
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007904#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7905 static void
7906initialise_tabline(void)
7907{
7908 InitCommonControls();
7909
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007910 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007911 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007912 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007913 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007914 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007915
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007916 gui.tabline_height = TABLINE_HEIGHT;
7917
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007918 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007919}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007920
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007921/*
7922 * Get tabpage_T from POINT.
7923 */
7924 static tabpage_T *
7925GetTabFromPoint(
7926 HWND hWnd,
7927 POINT pt)
7928{
7929 tabpage_T *ptp = NULL;
7930
7931 if (gui_mch_showing_tabline())
7932 {
7933 TCHITTESTINFO htinfo;
7934 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007935 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007936 if (s_tabhwnd == hWnd)
7937 {
7938 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7939 if (idx != -1)
7940 ptp = find_tabpage(idx + 1);
7941 }
7942 }
7943 return ptp;
7944}
7945
7946static POINT s_pt = {0, 0};
7947static HCURSOR s_hCursor = NULL;
7948
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007949 static LRESULT CALLBACK
7950tabline_wndproc(
7951 HWND hwnd,
7952 UINT uMsg,
7953 WPARAM wParam,
7954 LPARAM lParam)
7955{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007956 POINT pt;
7957 tabpage_T *tp;
7958 RECT rect;
7959 int nCenter;
7960 int idx0;
7961 int idx1;
7962
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007963 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007964
7965 switch (uMsg)
7966 {
7967 case WM_LBUTTONDOWN:
7968 {
7969 s_pt.x = GET_X_LPARAM(lParam);
7970 s_pt.y = GET_Y_LPARAM(lParam);
7971 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007972 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007973 break;
7974 }
7975 case WM_MOUSEMOVE:
7976 if (GetCapture() == hwnd
7977 && ((wParam & MK_LBUTTON)) != 0)
7978 {
7979 pt.x = GET_X_LPARAM(lParam);
7980 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007981 if (abs(pt.x - s_pt.x) >
7982 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007983 {
7984 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7985
7986 tp = GetTabFromPoint(hwnd, pt);
7987 if (tp != NULL)
7988 {
7989 idx0 = tabpage_index(curtab) - 1;
7990 idx1 = tabpage_index(tp) - 1;
7991
7992 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7993 nCenter = rect.left + (rect.right - rect.left) / 2;
7994
Bram Moolenaar734a8672019-12-02 22:49:38 +01007995 // Check if the mouse cursor goes over the center of
7996 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007997 if ((idx0 < idx1) && (nCenter < pt.x))
7998 {
7999 tabpage_move(idx1 + 1);
8000 update_screen(0);
8001 }
8002 else if ((idx1 < idx0) && (pt.x < nCenter))
8003 {
8004 tabpage_move(idx1);
8005 update_screen(0);
8006 }
8007 }
8008 }
8009 }
8010 break;
8011 case WM_LBUTTONUP:
8012 {
8013 if (GetCapture() == hwnd)
8014 {
8015 SetCursor(s_hCursor);
8016 ReleaseCapture();
8017 }
8018 break;
8019 }
8020 default:
8021 break;
8022 }
8023
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008024 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8025}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008026#endif
8027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8029/*
8030 * Make the GUI window come to the foreground.
8031 */
8032 void
8033gui_mch_set_foreground(void)
8034{
8035 if (IsIconic(s_hwnd))
8036 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8037 SetForegroundWindow(s_hwnd);
8038}
8039#endif
8040
8041#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8042 static void
8043dyn_imm_load(void)
8044{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008045 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 if (hLibImm == NULL)
8047 return;
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008050 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008052 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008054 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008056 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008058 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008060 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008061 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008062 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008063 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008064 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008066 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008068 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008069 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008070 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
K.Takatab0b2b732022-01-19 12:59:21 +00008072 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 || pImmGetContext == NULL
8074 || pImmAssociateContext == NULL
8075 || pImmReleaseContext == NULL
8076 || pImmGetOpenStatus == NULL
8077 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008078 || pImmGetCompositionFontW == NULL
8079 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008081 || pImmGetConversionStatus == NULL
8082 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
8084 FreeLibrary(hLibImm);
8085 hLibImm = NULL;
8086 pImmGetContext = NULL;
8087 return;
8088 }
8089
8090 return;
8091}
8092
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#endif
8094
8095#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8096
8097# ifdef FEAT_XPM_W32
8098# define IMAGE_XPM 100
8099# endif
8100
8101typedef struct _signicon_t
8102{
8103 HANDLE hImage;
8104 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008105# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008106 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108} signicon_t;
8109
8110 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008111gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113 signicon_t *sign;
8114 int x, y, w, h;
8115
8116 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8117 return;
8118
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008119# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008120 if (IS_ENABLE_DIRECTX())
8121 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008122# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 x = TEXT_X(col);
8125 y = TEXT_Y(row);
8126 w = gui.char_width * 2;
8127 h = gui.char_height;
8128 switch (sign->uType)
8129 {
8130 case IMAGE_BITMAP:
8131 {
8132 HDC hdcMem;
8133 HBITMAP hbmpOld;
8134
8135 hdcMem = CreateCompatibleDC(s_hdc);
8136 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8137 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8138 SelectObject(hdcMem, hbmpOld);
8139 DeleteDC(hdcMem);
8140 }
8141 break;
8142 case IMAGE_ICON:
8143 case IMAGE_CURSOR:
8144 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8145 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008146# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 case IMAGE_XPM:
8148 {
8149 HDC hdcMem;
8150 HBITMAP hbmpOld;
8151
8152 hdcMem = CreateCompatibleDC(s_hdc);
8153 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008154 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8156
8157 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008158 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8160 SelectObject(hdcMem, hbmpOld);
8161 DeleteDC(hdcMem);
8162 }
8163 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 }
8166}
8167
8168 static void
8169close_signicon_image(signicon_t *sign)
8170{
8171 if (sign)
8172 switch (sign->uType)
8173 {
8174 case IMAGE_BITMAP:
8175 DeleteObject((HGDIOBJ)sign->hImage);
8176 break;
8177 case IMAGE_CURSOR:
8178 DestroyCursor((HCURSOR)sign->hImage);
8179 break;
8180 case IMAGE_ICON:
8181 DestroyIcon((HICON)sign->hImage);
8182 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008183# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 case IMAGE_XPM:
8185 DeleteObject((HBITMAP)sign->hImage);
8186 DeleteObject((HBITMAP)sign->hShape);
8187 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008188# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 }
8190}
8191
8192 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008193gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 signicon_t sign, *psign;
8196 char_u *ext;
8197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008199 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 if (ext > signfile)
8201 {
8202 int do_load = 1;
8203
8204 if (!STRICMP(ext, ".bmp"))
8205 sign.uType = IMAGE_BITMAP;
8206 else if (!STRICMP(ext, ".ico"))
8207 sign.uType = IMAGE_ICON;
8208 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8209 sign.uType = IMAGE_CURSOR;
8210 else
8211 do_load = 0;
8212
8213 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008214 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 gui.char_width * 2, gui.char_height,
8216 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008217# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 if (!STRICMP(ext, ".xpm"))
8219 {
8220 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008221 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8222 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 }
8226
8227 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008228 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 *psign = sign;
8230
8231 if (!psign)
8232 {
8233 if (sign.hImage)
8234 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008235 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237 return (void *)psign;
8238
8239}
8240
8241 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008242gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244 if (sign)
8245 {
8246 close_signicon_image((signicon_t *)sign);
8247 vim_free(sign);
8248 }
8249}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008252#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaar734a8672019-12-02 22:49:38 +01008254/*
8255 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008256 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008258 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8260 * to get current mouse position).
8261 *
8262 * Trying to use as more Windows services as possible, and as less
8263 * IE version as possible :)).
8264 *
8265 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8266 * BalloonEval struct.
8267 * 2) Enable/Disable simply create/kill BalloonEval Timer
8268 * 3) When there was enough inactivity, timer procedure posts
8269 * async request to debugger
8270 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8271 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008272 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 */
8274
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008275 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008276make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008277{
K.Takata76687d22022-01-25 10:31:37 +00008278 TOOLINFOW *pti;
8279 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008280
K.Takata76687d22022-01-25 10:31:37 +00008281 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008282 if (pti == NULL)
8283 return;
8284
8285 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8286 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8287 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008288 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008289
8290 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8291 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8292
K.Takata76687d22022-01-25 10:31:37 +00008293 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008294 pti->uFlags = TTF_SUBCLASS;
8295 pti->hwnd = beval->target;
8296 pti->hinst = 0; // Don't use string resources
8297 pti->uId = ID_BEVAL_TOOLTIP;
8298
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008299 pti->lpszText = LPSTR_TEXTCALLBACKW;
8300 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8301 pti->lParam = (LPARAM)beval->tofree;
8302 // switch multiline tooltips on
8303 if (GetClientRect(s_textArea, &rect))
8304 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8305 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008306
8307 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8308 pti->rect.left = pt.x - 3;
8309 pti->rect.top = pt.y - 3;
8310 pti->rect.right = pt.x + 3;
8311 pti->rect.bottom = pt.y + 3;
8312
8313 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8314 // Make tooltip appear sooner.
8315 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8316 // I've performed some tests and it seems the longest possible life time
8317 // of tooltip is 30 seconds.
8318 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8319 /*
8320 * HACK: force tooltip to appear, because it'll not appear until
8321 * first mouse move. D*mn M$
8322 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8323 */
8324 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8325 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8326 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008327}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008330delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008332 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333}
8334
8335 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008336beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008337 HWND hwnd UNUSED,
8338 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008339 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008340 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341{
8342 POINT pt;
8343 RECT rect;
8344
8345 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8346 return;
8347
8348 GetCursorPos(&pt);
8349 if (WindowFromPoint(pt) != s_textArea)
8350 return;
8351
8352 ScreenToClient(s_textArea, &pt);
8353 GetClientRect(s_textArea, &rect);
8354 if (!PtInRect(&rect, pt))
8355 return;
8356
K.Takataa8ec4912022-02-03 14:32:33 +00008357 if (last_user_activity > 0
8358 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 && (cur_beval->showState != ShS_PENDING
8360 || abs(cur_beval->x - pt.x) > 3
8361 || abs(cur_beval->y - pt.y) > 3))
8362 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008363 // Pointer resting in one place long enough, it's time to show
8364 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 cur_beval->showState = ShS_PENDING;
8366 cur_beval->x = pt.x;
8367 cur_beval->y = pt.y;
8368
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if (cur_beval->msgCB != NULL)
8370 (*cur_beval->msgCB)(cur_beval, 0);
8371 }
8372}
8373
8374 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008375gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376{
K.Takataa8ec4912022-02-03 14:32:33 +00008377 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378}
8379
8380 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008381gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 if (beval == NULL)
8384 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008385 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8386 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387}
8388
8389 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008390gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391{
8392 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008393
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008394 vim_free(beval->msg);
8395 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8396 if (beval->msg == NULL)
8397 {
8398 delete_tooltip(beval);
8399 beval->showState = ShS_NEUTRAL;
8400 return;
8401 }
8402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (beval->showState == ShS_SHOWING)
8404 return;
8405 GetCursorPos(&pt);
8406 ScreenToClient(s_textArea, &pt);
8407
8408 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008410 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 gui_mch_disable_beval_area(cur_beval);
8412 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008413 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415}
8416
8417 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008418gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008419 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008420 char_u *mesg,
8421 void (*mesgCB)(BalloonEval *, int),
8422 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008424 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 BalloonEval *beval;
8426
8427 if (mesg != NULL && mesgCB != NULL)
8428 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008429 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 return NULL;
8431 }
8432
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008433 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 if (beval != NULL)
8435 {
8436 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437
8438 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 beval->msg = mesg;
8440 beval->msgCB = mesgCB;
8441 beval->clientData = clientData;
8442
8443 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 cur_beval = beval;
8445
8446 if (p_beval)
8447 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449 return beval;
8450}
8451
8452 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008453Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008455 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 return;
8457
8458 if (cur_beval != NULL)
8459 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008460 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008462 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008463 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008464 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 delete_tooltip(cur_beval);
8466 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
8468 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008469 break;
8470 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008472 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008473 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008474 info->lpszText = (LPSTR)info->lParam;
8475 info->uFlags |= TTF_DI_SETITEM;
8476 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008477 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008478 case TTN_GETDISPINFOW:
8479 {
8480 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008481 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008482 info->lpszText = (LPWSTR)info->lParam;
8483 info->uFlags |= TTF_DI_SETITEM;
8484 }
8485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 }
8487 }
8488}
8489
8490 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008491track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8494 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008495 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
8497
8498 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008499gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008501# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008502 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008503# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008504 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 vim_free(beval);
8506}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008507#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
8509#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8510/*
8511 * We have multiple signs to draw at the same location. Draw the
8512 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8513 */
8514 void
8515netbeans_draw_multisign_indicator(int row)
8516{
8517 int i;
8518 int y;
8519 int x;
8520
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008521 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008522 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 x = 0;
8525 y = TEXT_Y(row);
8526
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008527# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008528 if (IS_ENABLE_DIRECTX())
8529 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008530# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 for (i = 0; i < gui.char_height - 3; i++)
8533 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8534
8535 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8536 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8537 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8538 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8539 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8540 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8541 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8542}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008543#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008544
8545#if defined(FEAT_EVAL) || defined(PROTO)
8546 int
8547test_gui_w32_sendevent(dict_T *args)
8548{
8549 char_u *event;
8550 INPUT inputs[1];
8551
Bram Moolenaard61efa52022-07-23 09:52:04 +01008552 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008553 if (event == NULL)
8554 return FALSE;
8555
8556 ZeroMemory(inputs, sizeof(inputs));
8557
8558 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8559 {
8560 WORD vkCode;
8561
Bram Moolenaard61efa52022-07-23 09:52:04 +01008562 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008563 if (vkCode <= 0 || vkCode >= 0xFF)
8564 {
8565 semsg(_(e_invalid_argument_nr), (long)vkCode);
8566 return FALSE;
8567 }
8568
8569 inputs[0].type = INPUT_KEYBOARD;
8570 inputs[0].ki.wVk = vkCode;
8571 if (STRICMP(event, "keyup") == 0)
8572 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
8573 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8574 }
8575 else
8576 semsg(_(e_invalid_argument_str), event);
8577
8578 vim_free(event);
8579
8580 return TRUE;
8581}
8582#endif