blob: 9308e47852df9e90013a485c8aa04df129aa1263 [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
222# define WM_DPICHANGED 0x02E0
223#endif
224
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100225#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100227 * Define a few things for generating prototypes. This is just to avoid
228 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100230# define APIENTRY
231# define CALLBACK
232# define CONST
233# define FAR
234# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200235# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200236# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237# define _cdecl
238typedef int BOOL;
239typedef int BYTE;
240typedef int DWORD;
241typedef int WCHAR;
242typedef int ENUMLOGFONT;
243typedef int FINDREPLACE;
244typedef int HANDLE;
245typedef int HBITMAP;
246typedef int HBRUSH;
247typedef int HDROP;
248typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100249typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100250typedef int LPARAM;
251typedef int LPCREATESTRUCT;
252typedef int LPCSTR;
253typedef int LPCTSTR;
254typedef int LPRECT;
255typedef int LPSTR;
256typedef int LPWINDOWPOS;
257typedef int LPWORD;
258typedef int LRESULT;
259typedef int HRESULT;
260# undef MSG
261typedef int MSG;
262typedef int NEWTEXTMETRIC;
263typedef int OSVERSIONINFO;
264typedef int PWORD;
265typedef int RECT;
266typedef int UINT;
267typedef int WORD;
268typedef int WPARAM;
269typedef int POINT;
270typedef void *HINSTANCE;
271typedef void *HMENU;
272typedef void *HWND;
273typedef void *HDC;
274typedef void VOID;
275typedef int LPNMHDR;
276typedef int LONG;
277typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200278typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200279typedef int COLORREF;
280typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100281#endif
282
K.Takata45f9cfb2022-01-21 11:11:00 +0000283static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100284static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100285static void clear_rect(RECT *rcp);
286
Bram Moolenaar734a8672019-12-02 22:49:38 +0100287static WORD s_dlgfntheight; // height of the dialog font
288static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289
290#ifdef FEAT_MENU
291static HMENU s_menuBar = NULL;
292#endif
293#ifdef FEAT_TEAROFF
294static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100295static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100296#endif
297
Bram Moolenaar734a8672019-12-02 22:49:38 +0100298// Flag that is set while processing a message that must not be interrupted by
299// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100300static int s_busy_processing = FALSE;
301
Bram Moolenaar734a8672019-12-02 22:49:38 +0100302static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000305static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200306static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200308static int s_findrep_is_find; // TRUE for find dialog, FALSE
309 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312HWND s_hwnd = NULL;
313static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100314static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef FEAT_TOOLBAR
317static HWND s_toolbarhwnd = NULL;
318static WNDPROC s_toolbar_wndproc = NULL;
319#endif
320
321#ifdef FEAT_GUI_TABLINE
322static HWND s_tabhwnd = NULL;
323static WNDPROC s_tabline_wndproc = NULL;
324static int showing_tabline = 0;
325#endif
326
327static WPARAM s_wParam = 0;
328static LPARAM s_lParam = 0;
329
330static HWND s_textArea = NULL;
331static UINT s_uMsg = 0;
332
Bram Moolenaar734a8672019-12-02 22:49:38 +0100333static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100334
335static int s_need_activate = FALSE;
336
Bram Moolenaar734a8672019-12-02 22:49:38 +0100337// This variable is set when waiting for an event, which is the only moment
338// scrollbar dragging can be done directly. It's not allowed while commands
339// are executed, because it may move the cursor and that may cause unexpected
340// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100341static int allow_scrollbar = FALSE;
342
K.Takatac81e9bf2022-01-16 14:15:49 +0000343#ifndef _DPI_AWARENESS_CONTEXTS_
344typedef HANDLE DPI_AWARENESS_CONTEXT;
345
346typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000347 DPI_AWARENESS_INVALID = -1,
348 DPI_AWARENESS_UNAWARE = 0,
349 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000350 DPI_AWARENESS_PER_MONITOR_AWARE = 2
351} DPI_AWARENESS;
352
K.Takatab0b2b732022-01-19 12:59:21 +0000353# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
354# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000355# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
356# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
357# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
358#endif
359
360#define DEFAULT_DPI 96
361static int s_dpi = DEFAULT_DPI;
362static BOOL s_in_dpichanged = FALSE;
363static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
364
365static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
366static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
367static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
368//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
369static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
370static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
371
372 static UINT WINAPI
373stubGetDpiForSystem(void)
374{
375 HWND hwnd = GetDesktopWindow();
376 HDC hdc = GetWindowDC(hwnd);
377 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
378 ReleaseDC(hwnd, hdc);
379 return dpi;
380}
381
382 static int WINAPI
383stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
384{
385 return GetSystemMetrics(nIndex);
386}
387
388 static int
389adjust_fontsize_by_dpi(int size)
390{
391 return size * s_dpi / (int)pGetDpiForSystem();
392}
393
394 static int
395adjust_by_system_dpi(int size)
396{
397 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
398}
399
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100400#if defined(FEAT_DIRECTX)
401 static int
402directx_enabled(void)
403{
404 if (s_dwc != NULL)
405 return 1;
406 else if (s_directx_load_attempted)
407 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100408 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100409 DWrite_Init();
410 s_directx_load_attempted = 1;
411 s_dwc = DWriteContext_Open();
412 directx_binddc();
413 return s_dwc != NULL ? 1 : 0;
414}
415
416 static void
417directx_binddc(void)
418{
419 if (s_textArea != NULL)
420 {
421 RECT rect;
422 GetClientRect(s_textArea, &rect);
423 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
424 }
425}
426#endif
427
Bram Moolenaar734a8672019-12-02 22:49:38 +0100428extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100429
430static struct
431{
432 UINT key_sym;
433 char_u vim_code0;
434 char_u vim_code1;
435} special_keys[] =
436{
437 {VK_UP, 'k', 'u'},
438 {VK_DOWN, 'k', 'd'},
439 {VK_LEFT, 'k', 'l'},
440 {VK_RIGHT, 'k', 'r'},
441
442 {VK_F1, 'k', '1'},
443 {VK_F2, 'k', '2'},
444 {VK_F3, 'k', '3'},
445 {VK_F4, 'k', '4'},
446 {VK_F5, 'k', '5'},
447 {VK_F6, 'k', '6'},
448 {VK_F7, 'k', '7'},
449 {VK_F8, 'k', '8'},
450 {VK_F9, 'k', '9'},
451 {VK_F10, 'k', ';'},
452
453 {VK_F11, 'F', '1'},
454 {VK_F12, 'F', '2'},
455 {VK_F13, 'F', '3'},
456 {VK_F14, 'F', '4'},
457 {VK_F15, 'F', '5'},
458 {VK_F16, 'F', '6'},
459 {VK_F17, 'F', '7'},
460 {VK_F18, 'F', '8'},
461 {VK_F19, 'F', '9'},
462 {VK_F20, 'F', 'A'},
463
464 {VK_F21, 'F', 'B'},
465#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100466 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100467#endif
468 {VK_F22, 'F', 'C'},
469 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471
472 {VK_HELP, '%', '1'},
473 {VK_BACK, 'k', 'b'},
474 {VK_INSERT, 'k', 'I'},
475 {VK_DELETE, 'k', 'D'},
476 {VK_HOME, 'k', 'h'},
477 {VK_END, '@', '7'},
478 {VK_PRIOR, 'k', 'P'},
479 {VK_NEXT, 'k', 'N'},
480 {VK_PRINT, '%', '9'},
481 {VK_ADD, 'K', '6'},
482 {VK_SUBTRACT, 'K', '7'},
483 {VK_DIVIDE, 'K', '8'},
484 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100485 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100486 {VK_DECIMAL, 'K', 'B'},
487
488 {VK_NUMPAD0, 'K', 'C'},
489 {VK_NUMPAD1, 'K', 'D'},
490 {VK_NUMPAD2, 'K', 'E'},
491 {VK_NUMPAD3, 'K', 'F'},
492 {VK_NUMPAD4, 'K', 'G'},
493 {VK_NUMPAD5, 'K', 'H'},
494 {VK_NUMPAD6, 'K', 'I'},
495 {VK_NUMPAD7, 'K', 'J'},
496 {VK_NUMPAD8, 'K', 'K'},
497 {VK_NUMPAD9, 'K', 'L'},
498
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_SPACE, ' ', NUL},
501 {VK_TAB, TAB, NUL},
502 {VK_ESCAPE, ESC, NUL},
503 {NL, NL, NUL},
504 {CAR, CAR, NUL},
505
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100507 {0, 0, 0}
508};
509
Bram Moolenaar734a8672019-12-02 22:49:38 +0100510// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511static int s_button_pending = -1;
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
514// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100515static int s_getting_focus = FALSE;
516
517static int s_x_pending;
518static int s_y_pending;
519static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000520static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200522static int dead_key = 0; // 0: no dead key, 1: dead key pressed
523static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
524 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100526#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100528static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000529static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100530#endif
531
532/*
533 * For control IME.
534 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100535 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100536 */
K.Takata4ac893f2022-01-20 12:44:28 +0000537#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100538// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100539static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100540// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100541static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542#endif
543
544#ifdef FEAT_MBYTE_IME
545static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
546#endif
547
548#if defined(FEAT_BROWSE)
549static char_u *convert_filter(char_u *s);
550#endif
551
552#ifdef DEBUG_PRINT_ERROR
553/*
554 * Print out the last Windows error message
555 */
556 static void
557print_windows_error(void)
558{
559 LPVOID lpMsgBuf;
560
561 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
562 NULL, GetLastError(),
563 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
564 (LPTSTR) &lpMsgBuf, 0, NULL);
565 TRACE1("Error: %s\n", lpMsgBuf);
566 LocalFree(lpMsgBuf);
567}
568#endif
569
570/*
571 * Cursor blink functions.
572 *
573 * This is a simple state machine:
574 * BLINK_NONE not blinking at all
575 * BLINK_OFF blinking, cursor is not shown
576 * BLINK_ON blinking, cursor is shown
577 */
578
579#define BLINK_NONE 0
580#define BLINK_OFF 1
581#define BLINK_ON 2
582
583static int blink_state = BLINK_NONE;
584static long_u blink_waittime = 700;
585static long_u blink_ontime = 400;
586static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000587static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100588
Bram Moolenaar703a8042016-06-04 16:24:32 +0200589 int
590gui_mch_is_blinking(void)
591{
592 return blink_state != BLINK_NONE;
593}
594
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200595 int
596gui_mch_is_blink_off(void)
597{
598 return blink_state == BLINK_OFF;
599}
600
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100601 void
602gui_mch_set_blinking(long wait, long on, long off)
603{
604 blink_waittime = wait;
605 blink_ontime = on;
606 blink_offtime = off;
607}
608
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609 static VOID CALLBACK
610_OnBlinkTimer(
611 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100612 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000613 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100614 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615{
616 MSG msg;
617
618 /*
619 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
620 */
621
622 KillTimer(NULL, idEvent);
623
Bram Moolenaar734a8672019-12-02 22:49:38 +0100624 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000625 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100626 ;
627
628 if (blink_state == BLINK_ON)
629 {
630 gui_undraw_cursor();
631 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000632 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633 }
634 else
635 {
636 gui_update_cursor(TRUE, FALSE);
637 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000638 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100640 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641}
642
643 static void
644gui_mswin_rm_blink_timer(void)
645{
646 MSG msg;
647
648 if (blink_timer != 0)
649 {
650 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100651 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000652 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 ;
654 blink_timer = 0;
655 }
656}
657
658/*
659 * Stop the cursor blinking. Show the cursor if it wasn't shown.
660 */
661 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100662gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100663{
664 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100665 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100666 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100667 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100668 gui_mch_flush();
669 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670 blink_state = BLINK_NONE;
671}
672
673/*
674 * Start the cursor blinking. If it was already blinking, this restarts the
675 * waiting time and shows the cursor.
676 */
677 void
678gui_mch_start_blink(void)
679{
680 gui_mswin_rm_blink_timer();
681
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
684 {
K.Takataa8ec4912022-02-03 14:32:33 +0000685 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 blink_state = BLINK_ON;
687 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100688 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100689 }
690}
691
692/*
693 * Call-back routines.
694 */
695
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100696 static VOID CALLBACK
697_OnTimer(
698 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100699 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000700 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100701 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702{
703 MSG msg;
704
705 /*
706 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
707 */
708 KillTimer(NULL, idEvent);
709 s_timed_out = TRUE;
710
Bram Moolenaar734a8672019-12-02 22:49:38 +0100711 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000712 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100713 ;
714 if (idEvent == s_wait_timer)
715 s_wait_timer = 0;
716}
717
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100718 static void
719_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100720 HWND hwnd UNUSED,
721 UINT ch UNUSED,
722 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100723{
724 dead_key = 1;
725}
726
727/*
728 * Convert Unicode character "ch" to bytes in "string[slen]".
729 * When "had_alt" is TRUE the ALT key was included in "ch".
730 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200731 * Because the Windows API uses UTF-16, we have to deal with surrogate
732 * pairs; this is where we choose to deal with them: if "ch" is a high
733 * surrogate, it will be stored, and the length returned will be zero; the next
734 * char_to_string call will then include the high surrogate, decoding the pair
735 * of UTF-16 code units to a single Unicode code point, presuming it is the
736 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100737 */
738 static int
739char_to_string(int ch, char_u *string, int slen, int had_alt)
740{
741 int len;
742 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100743 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200744 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100745
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200746 if (surrogate_pending_ch != 0)
747 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100748 // We don't guarantee ch is a low surrogate to match the high surrogate
749 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200750 wstring[0] = surrogate_pending_ch;
751 wstring[1] = ch;
752 surrogate_pending_ch = 0;
753 len = 2;
754 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100755 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200756 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100757 // We don't have the entire code point yet, only the first UTF-16 code
758 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200759 surrogate_pending_ch = ch;
760 return 0;
761 }
762 else
763 {
764 wstring[0] = ch;
765 len = 1;
766 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767
Bram Moolenaar734a8672019-12-02 22:49:38 +0100768 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
769 // "enc_codepage" is non-zero use the standard Win32 function,
770 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200771 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200773 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
774 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // If we had included the ALT key into the character but now the
776 // upper bit is no longer set, that probably means the conversion
777 // failed. Convert the original character and set the upper bit
778 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200779 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100780 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200781 wstring[0] = ch & 0x7f;
782 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
783 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100784 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100786 }
787 }
788 else
789 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790 ws = utf16_to_enc(wstring, &len);
791 if (ws == NULL)
792 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 else
794 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100795 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 len = slen;
797 mch_memmove(string, ws, len);
798 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 }
800 }
801
802 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 {
804 string[0] = ch;
805 len = 1;
806 }
807
808 for (i = 0; i < len; ++i)
809 if (string[i] == CSI && len <= slen - 2)
810 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100811 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
813 string[++i] = KS_EXTRA;
814 string[++i] = (int)KE_CSI;
815 len += 2;
816 }
817
818 return len;
819}
820
LemonBoy45684c62022-04-24 15:46:42 +0100821 static int
822get_active_modifiers(void)
823{
824 int modifiers = 0;
825
826 if (GetKeyState(VK_CONTROL) & 0x8000)
827 modifiers |= MOD_MASK_CTRL;
828 if (GetKeyState(VK_SHIFT) & 0x8000)
829 modifiers |= MOD_MASK_SHIFT;
830 if (GetKeyState(VK_MENU) & 0x8000)
831 modifiers |= MOD_MASK_ALT;
832 // Windows handles Ctrl + Alt as AltGr, in that case no modifier is actually
833 // pressed.
834 if ((modifiers & (MOD_MASK_CTRL | MOD_MASK_ALT)) ==
835 (MOD_MASK_CTRL | MOD_MASK_ALT))
836 modifiers &= ~(MOD_MASK_CTRL | MOD_MASK_ALT);
837
838 return modifiers;
839}
840
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100841/*
842 * Key hit, add it to the input buffer.
843 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100844 static void
845_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100846 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100847 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100848 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100849{
850 char_u string[40];
851 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100852 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100853 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100854
855 dead_key = 0;
856
LemonBoy45684c62022-04-24 15:46:42 +0100857 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100858
859 ch = simplify_key(ch, &modifiers);
860 // remove the SHIFT modifier for keys where it's already included, e.g.,
861 // '(' and '*'
862 modifiers = may_remove_shift_modifier(modifiers, ch);
863
864 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
865 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
866 if (ch == CSI)
867 ch = K_CSI;
868
869 if (modifiers)
870 {
871 string[0] = CSI;
872 string[1] = KS_MODIFIER;
873 string[2] = modifiers;
874 add_to_input_buf(string, 3);
875 }
876
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100877 len = char_to_string(ch, string, 40, FALSE);
878 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
879 {
880 trash_input_buf();
881 got_int = TRUE;
882 }
883
884 add_to_input_buf(string, len);
885}
886
887/*
888 * Alt-Key hit, add it to the input buffer.
889 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100890 static void
891_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100892 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100893 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100894 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100895{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100896 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100897 int len;
898 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100899 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100900
901 dead_key = 0;
902
Bram Moolenaar734a8672019-12-02 22:49:38 +0100903 // OK, we have a character key (given by ch) which was entered with the
904 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
905 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
906 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100907 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100908 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100909 // remove the SHIFT modifier for keys where it's already included, e.g.,
910 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200911 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100912
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200913 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
914 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100915 if (ch == CSI)
916 ch = K_CSI;
917
918 len = 0;
919 if (modifiers)
920 {
921 string[len++] = CSI;
922 string[len++] = KS_MODIFIER;
923 string[len++] = modifiers;
924 }
925
926 if (IS_SPECIAL((int)ch))
927 {
928 string[len++] = CSI;
929 string[len++] = K_SECOND((int)ch);
930 string[len++] = K_THIRD((int)ch);
931 }
932 else
933 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100934 // Although the documentation isn't clear about it, we assume "ch" is
935 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100936 len += char_to_string(ch, string + len, 40 - len, TRUE);
937 }
938
939 add_to_input_buf(string, len);
940}
941
942 static void
943_OnMouseEvent(
944 int button,
945 int x,
946 int y,
947 int repeated_click,
948 UINT keyFlags)
949{
950 int vim_modifiers = 0x0;
951
952 s_getting_focus = FALSE;
953
954 if (keyFlags & MK_SHIFT)
955 vim_modifiers |= MOUSE_SHIFT;
956 if (keyFlags & MK_CONTROL)
957 vim_modifiers |= MOUSE_CTRL;
958 if (GetKeyState(VK_MENU) & 0x8000)
959 vim_modifiers |= MOUSE_ALT;
960
961 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
962}
963
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100964 static void
965_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100966 HWND hwnd UNUSED,
967 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100968 int x,
969 int y,
970 UINT keyFlags)
971{
972 static LONG s_prevTime = 0;
973
974 LONG currentTime = GetMessageTime();
975 int button = -1;
976 int repeated_click;
977
Bram Moolenaar734a8672019-12-02 22:49:38 +0100978 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100979 (void)SetFocus(s_hwnd);
980
981 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
982 button = MOUSE_LEFT;
983 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
984 button = MOUSE_MIDDLE;
985 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
986 button = MOUSE_RIGHT;
987 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
988 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100989 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
990 }
991 else if (s_uMsg == WM_CAPTURECHANGED)
992 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100993 // on W95/NT4, somehow you get in here with an odd Msg
994 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100995 if (s_button_pending == MOUSE_LEFT)
996 button = MOUSE_RIGHT;
997 else
998 button = MOUSE_LEFT;
999 }
1000 if (button >= 0)
1001 {
1002 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1003
1004 /*
1005 * Holding down the left and right buttons simulates pushing the middle
1006 * button.
1007 */
1008 if (repeated_click
1009 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1010 || (button == MOUSE_RIGHT
1011 && s_button_pending == MOUSE_LEFT)))
1012 {
1013 /*
1014 * Hmm, gui.c will ignore more than one button down at a time, so
1015 * pretend we let go of it first.
1016 */
1017 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1018 button = MOUSE_MIDDLE;
1019 repeated_click = FALSE;
1020 s_button_pending = -1;
1021 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1022 }
1023 else if ((repeated_click)
1024 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1025 {
1026 if (s_button_pending > -1)
1027 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001028 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1029 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001030 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001031 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001032 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1033 }
1034 else
1035 {
1036 /*
1037 * If this is the first press (i.e. not a multiple click) don't
1038 * action immediately, but store and wait for:
1039 * i) button-up
1040 * ii) mouse move
1041 * iii) another button press
1042 * before using it.
1043 * This enables us to make left+right simulate middle button,
1044 * without left or right being actioned first. The side-effect is
1045 * that if you click and hold the mouse without dragging, the
1046 * cursor doesn't move until you release the button. In practice
1047 * this is hardly a problem.
1048 */
1049 s_button_pending = button;
1050 s_x_pending = x;
1051 s_y_pending = y;
1052 s_kFlags_pending = keyFlags;
1053 }
1054
1055 s_prevTime = currentTime;
1056 }
1057}
1058
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001059 static void
1060_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001061 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062 int x,
1063 int y,
1064 UINT keyFlags)
1065{
1066 int button;
1067
1068 s_getting_focus = FALSE;
1069 if (s_button_pending > -1)
1070 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001071 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001072 _OnMouseEvent(s_button_pending, s_x_pending,
1073 s_y_pending, FALSE, s_kFlags_pending);
1074 s_button_pending = -1;
1075 }
1076 if (s_uMsg == WM_MOUSEMOVE)
1077 {
1078 /*
1079 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1080 * down.
1081 */
1082 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1083 | MK_XBUTTON1 | MK_XBUTTON2)))
1084 {
1085 gui_mouse_moved(x, y);
1086 return;
1087 }
1088
1089 /*
1090 * While button is down, keep grabbing mouse move events when
1091 * the mouse goes outside the window
1092 */
1093 SetCapture(s_textArea);
1094 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001095 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 }
1097 else
1098 {
1099 ReleaseCapture();
1100 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001101 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102 }
1103
1104 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1105}
1106
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001107 static void
1108_OnSizeTextArea(
1109 HWND hwnd UNUSED,
1110 UINT state UNUSED,
1111 int cx UNUSED,
1112 int cy UNUSED)
1113{
1114#if defined(FEAT_DIRECTX)
1115 if (IS_ENABLE_DIRECTX())
1116 directx_binddc();
1117#endif
1118}
1119
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120#ifdef FEAT_MENU
1121/*
1122 * Find the vimmenu_T with the given id
1123 */
1124 static vimmenu_T *
1125gui_mswin_find_menu(
1126 vimmenu_T *pMenu,
1127 int id)
1128{
1129 vimmenu_T *pChildMenu;
1130
1131 while (pMenu)
1132 {
1133 if (pMenu->id == (UINT)id)
1134 break;
1135 if (pMenu->children != NULL)
1136 {
1137 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1138 if (pChildMenu)
1139 {
1140 pMenu = pChildMenu;
1141 break;
1142 }
1143 }
1144 pMenu = pMenu->next;
1145 }
1146 return pMenu;
1147}
1148
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001149 static void
1150_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001151 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001153 HWND hwndCtl UNUSED,
1154 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001155{
1156 vimmenu_T *pMenu;
1157
1158 pMenu = gui_mswin_find_menu(root_menu, id);
1159 if (pMenu)
1160 gui_menu_cb(pMenu);
1161}
1162#endif
1163
1164#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001165/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001166 * Handle a Find/Replace window message.
1167 */
1168 static void
1169_OnFindRepl(void)
1170{
1171 int flags = 0;
1172 int down;
1173
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001174 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001175 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001176 (void)SetFocus(s_hwnd);
1177
1178 if (s_findrep_struct.Flags & FR_FINDNEXT)
1179 {
1180 flags = FRD_FINDNEXT;
1181
Bram Moolenaar734a8672019-12-02 22:49:38 +01001182 // Give main window the focus back: this is so the cursor isn't
1183 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 (void)SetFocus(s_hwnd);
1185 }
1186 else if (s_findrep_struct.Flags & FR_REPLACE)
1187 {
1188 flags = FRD_REPLACE;
1189
Bram Moolenaar734a8672019-12-02 22:49:38 +01001190 // Give main window the focus back: this is so the cursor isn't
1191 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 (void)SetFocus(s_hwnd);
1193 }
1194 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1195 {
1196 flags = FRD_REPLACEALL;
1197 }
1198
1199 if (flags != 0)
1200 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001201 char_u *p, *q;
1202
Bram Moolenaar734a8672019-12-02 22:49:38 +01001203 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001204 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1205 flags |= FRD_WHOLE_WORD;
1206 if (s_findrep_struct.Flags & FR_MATCHCASE)
1207 flags |= FRD_MATCH_CASE;
1208 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001209 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1210 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1211 if (p != NULL && q != NULL)
1212 gui_do_findrepl(flags, p, q, down);
1213 vim_free(p);
1214 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001215 }
1216}
1217#endif
1218
1219 static void
1220HandleMouseHide(UINT uMsg, LPARAM lParam)
1221{
1222 static LPARAM last_lParam = 0L;
1223
Bram Moolenaar734a8672019-12-02 22:49:38 +01001224 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001225 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1226 {
1227 if (lParam == last_lParam)
1228 return;
1229 last_lParam = lParam;
1230 }
1231
Bram Moolenaar734a8672019-12-02 22:49:38 +01001232 // Handle specially, to centralise coding. We need to be sure we catch all
1233 // possible events which should cause us to restore the cursor (as it is a
1234 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 switch (uMsg)
1236 {
1237 case WM_KEYUP:
1238 case WM_CHAR:
1239 /*
1240 * blank out the pointer if necessary
1241 */
1242 if (p_mh)
1243 gui_mch_mousehide(TRUE);
1244 break;
1245
Bram Moolenaar734a8672019-12-02 22:49:38 +01001246 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001247 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001248 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249 case WM_LBUTTONDOWN:
1250 case WM_LBUTTONUP:
1251 case WM_MBUTTONDOWN:
1252 case WM_MBUTTONUP:
1253 case WM_RBUTTONDOWN:
1254 case WM_RBUTTONUP:
1255 case WM_XBUTTONDOWN:
1256 case WM_XBUTTONUP:
1257 case WM_NCMOUSEMOVE:
1258 case WM_NCLBUTTONDOWN:
1259 case WM_NCLBUTTONUP:
1260 case WM_NCMBUTTONDOWN:
1261 case WM_NCMBUTTONUP:
1262 case WM_NCRBUTTONDOWN:
1263 case WM_NCRBUTTONUP:
1264 case WM_KILLFOCUS:
1265 /*
1266 * if the pointer is currently hidden, then we should show it.
1267 */
1268 gui_mch_mousehide(FALSE);
1269 break;
1270 }
1271}
1272
1273 static LRESULT CALLBACK
1274_TextAreaWndProc(
1275 HWND hwnd,
1276 UINT uMsg,
1277 WPARAM wParam,
1278 LPARAM lParam)
1279{
1280 /*
1281 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1282 hwnd, uMsg, wParam, lParam);
1283 */
1284
1285 HandleMouseHide(uMsg, lParam);
1286
1287 s_uMsg = uMsg;
1288 s_wParam = wParam;
1289 s_lParam = lParam;
1290
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001291#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001292 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001293#endif
1294
1295 switch (uMsg)
1296 {
1297 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1298 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1299 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1300 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1301 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1302 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1303 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1304 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1305 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1306 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1307 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1308 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1309 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1310 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001311 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001312
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001313#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001314 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1315 return TRUE;
1316#endif
1317 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001318 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001319 }
1320}
1321
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001322/*
1323 * Called when the foreground or background color has been changed.
1324 */
1325 void
1326gui_mch_new_colors(void)
1327{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001328 HBRUSH prevBrush;
1329
1330 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001331 prevBrush = (HBRUSH)SetClassLongPtr(
1332 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001333 InvalidateRect(s_hwnd, NULL, TRUE);
1334 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001335}
1336
1337/*
1338 * Set the colors to their default values.
1339 */
1340 void
1341gui_mch_def_colors(void)
1342{
1343 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1344 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1345 gui.def_norm_pixel = gui.norm_pixel;
1346 gui.def_back_pixel = gui.back_pixel;
1347}
1348
1349/*
1350 * Open the GUI window which was created by a call to gui_mch_init().
1351 */
1352 int
1353gui_mch_open(void)
1354{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001355 // Actually open the window, if not already visible
1356 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001357 if (!IsWindowVisible(s_hwnd))
1358 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1359
1360#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001361 // Init replace string here, so that we keep it when re-opening the
1362 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001363 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1364#endif
1365
1366 return OK;
1367}
1368
1369/*
1370 * Get the position of the top left corner of the window.
1371 */
1372 int
1373gui_mch_get_winpos(int *x, int *y)
1374{
1375 RECT rect;
1376
1377 GetWindowRect(s_hwnd, &rect);
1378 *x = rect.left;
1379 *y = rect.top;
1380 return OK;
1381}
1382
1383/*
1384 * Set the position of the top left corner of the window to the given
1385 * coordinates.
1386 */
1387 void
1388gui_mch_set_winpos(int x, int y)
1389{
1390 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1391 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1392}
1393 void
1394gui_mch_set_text_area_pos(int x, int y, int w, int h)
1395{
1396 static int oldx = 0;
1397 static int oldy = 0;
1398
1399 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1400
1401#ifdef FEAT_TOOLBAR
1402 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1403 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001404 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001405#endif
1406#if defined(FEAT_GUI_TABLINE)
1407 if (showing_tabline)
1408 {
1409 int top = 0;
1410 RECT rect;
1411
1412# ifdef FEAT_TOOLBAR
1413 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001414 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001415# endif
1416 GetClientRect(s_hwnd, &rect);
1417 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1418 }
1419#endif
1420
Bram Moolenaar734a8672019-12-02 22:49:38 +01001421 // When side scroll bar is unshown, the size of window will change.
1422 // then, the text area move left or right. thus client rect should be
1423 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001424 if (oldx != x || oldy != y)
1425 {
1426 InvalidateRect(s_hwnd, NULL, FALSE);
1427 oldx = x;
1428 oldy = y;
1429 }
1430}
1431
1432
1433/*
1434 * Scrollbar stuff:
1435 */
1436
1437 void
1438gui_mch_enable_scrollbar(
1439 scrollbar_T *sb,
1440 int flag)
1441{
1442 ShowScrollBar(sb->id, SB_CTL, flag);
1443
Bram Moolenaar734a8672019-12-02 22:49:38 +01001444 // TODO: When the window is maximized, the size of the window stays the
1445 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1446 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001447}
1448
1449 void
1450gui_mch_set_scrollbar_pos(
1451 scrollbar_T *sb,
1452 int x,
1453 int y,
1454 int w,
1455 int h)
1456{
1457 SetWindowPos(sb->id, NULL, x, y, w, h,
1458 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1459}
1460
Bram Moolenaar203ec772020-07-17 20:43:43 +02001461 int
1462gui_mch_get_scrollbar_xpadding(void)
1463{
1464 RECT rcTxt, rcWnd;
1465 int xpad;
1466
1467 GetWindowRect(s_textArea, &rcTxt);
1468 GetWindowRect(s_hwnd, &rcWnd);
1469 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001470 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1471 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001472 return (xpad < 0) ? 0 : xpad;
1473}
1474
1475 int
1476gui_mch_get_scrollbar_ypadding(void)
1477{
1478 RECT rcTxt, rcWnd;
1479 int ypad;
1480
1481 GetWindowRect(s_textArea, &rcTxt);
1482 GetWindowRect(s_hwnd, &rcWnd);
1483 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001484 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1485 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001486 return (ypad < 0) ? 0 : ypad;
1487}
1488
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001489 void
1490gui_mch_create_scrollbar(
1491 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001492 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001493{
1494 sb->id = CreateWindow(
1495 "SCROLLBAR", "Scrollbar",
1496 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001497 10, // Any value will do for now
1498 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001499 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001500 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001501}
1502
1503/*
1504 * Find the scrollbar with the given hwnd.
1505 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001506 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001507gui_mswin_find_scrollbar(HWND hwnd)
1508{
1509 win_T *wp;
1510
1511 if (gui.bottom_sbar.id == hwnd)
1512 return &gui.bottom_sbar;
1513 FOR_ALL_WINDOWS(wp)
1514 {
1515 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1516 return &wp->w_scrollbars[SBAR_LEFT];
1517 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1518 return &wp->w_scrollbars[SBAR_RIGHT];
1519 }
1520 return NULL;
1521}
1522
K.Takatac81e9bf2022-01-16 14:15:49 +00001523 static void
1524update_scrollbar_size(void)
1525{
1526 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1527 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1528}
1529
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001530/*
K.Takataabe628e2022-01-23 16:25:17 +00001531 * Get the average character size of a font.
1532 */
1533 static void
1534GetAverageFontSize(HDC hdc, SIZE *size)
1535{
1536 // GetTextMetrics() may not return the right value in tmAveCharWidth
1537 // for some fonts. Do our own average computation.
1538 GetTextExtentPoint(hdc,
1539 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1540 52, size);
1541 size->cx = (size->cx / 26 + 1) / 2;
1542}
1543
1544/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001545 * Get the character size of a font.
1546 */
1547 static void
1548GetFontSize(GuiFont font)
1549{
1550 HWND hwnd = GetDesktopWindow();
1551 HDC hdc = GetWindowDC(hwnd);
1552 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001553 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554 TEXTMETRIC tm;
1555
1556 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001557 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001558
K.Takataabe628e2022-01-23 16:25:17 +00001559 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001560 gui.char_height = tm.tmHeight + p_linespace;
1561
1562 SelectFont(hdc, hfntOld);
1563
1564 ReleaseDC(hwnd, hdc);
1565}
1566
1567/*
1568 * Adjust gui.char_height (after 'linespace' was changed).
1569 */
1570 int
1571gui_mch_adjust_charheight(void)
1572{
1573 GetFontSize(gui.norm_font);
1574 return OK;
1575}
1576
1577 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001578get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001579{
1580 HFONT font = NULL;
1581
Bram Moolenaar734a8672019-12-02 22:49:38 +01001582 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001583 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
1585 if (font == NULL)
1586 return NOFONT;
1587
1588 return (GuiFont)font;
1589}
1590
1591 static int
1592pixels_to_points(int pixels, int vertical)
1593{
1594 int points;
1595 HWND hwnd;
1596 HDC hdc;
1597
1598 hwnd = GetDesktopWindow();
1599 hdc = GetWindowDC(hwnd);
1600
1601 points = MulDiv(pixels, 72,
1602 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1603
1604 ReleaseDC(hwnd, hdc);
1605
1606 return points;
1607}
1608
1609 GuiFont
1610gui_mch_get_font(
1611 char_u *name,
1612 int giveErrorIfMissing)
1613{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001614 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001615 GuiFont font = NOFONT;
1616
1617 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001618 {
1619 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001620 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001621 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001623 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001624 return font;
1625}
1626
1627#if defined(FEAT_EVAL) || defined(PROTO)
1628/*
1629 * Return the name of font "font" in allocated memory.
1630 * Don't know how to get the actual name, thus use the provided name.
1631 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001632 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001633gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634{
1635 if (name == NULL)
1636 return NULL;
1637 return vim_strsave(name);
1638}
1639#endif
1640
1641 void
1642gui_mch_free_font(GuiFont font)
1643{
1644 if (font)
1645 DeleteObject((HFONT)font);
1646}
1647
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648/*
1649 * Return the Pixel value (color) for the given color name.
1650 * Return INVALCOLOR for error.
1651 */
1652 guicolor_T
1653gui_mch_get_color(char_u *name)
1654{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001655 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656
1657 typedef struct SysColorTable
1658 {
1659 char *name;
1660 int color;
1661 } SysColorTable;
1662
1663 static SysColorTable sys_table[] =
1664 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1666 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001667#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1669#endif
1670 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1671 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1672 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1673 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1674 {"SYS_DESKTOP", COLOR_DESKTOP},
1675 {"SYS_INFOBK", COLOR_INFOBK},
1676 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1677 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678 {"SYS_BTNFACE", COLOR_BTNFACE},
1679 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1680 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1681 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1682 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1683 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1684 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1685 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1686 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1687 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1688 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1689 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1690 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1691 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1692 {"SYS_MENU", COLOR_MENU},
1693 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1694 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1695 {"SYS_WINDOW", COLOR_WINDOW},
1696 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1697 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1698 };
1699
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700 /*
1701 * Try to look up a system colour.
1702 */
K.Takataeeec2542021-06-02 13:28:16 +02001703 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001704 if (STRICMP(name, sys_table[i].name) == 0)
1705 return GetSysColor(sys_table[i].color);
1706
Bram Moolenaarab302212016-04-26 20:59:29 +02001707 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001709
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001710 guicolor_T
1711gui_mch_get_rgb_color(int r, int g, int b)
1712{
1713 return gui_get_rgb_color_cmn(r, g, b);
1714}
1715
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001716/*
1717 * Return OK if the key with the termcap name "name" is supported.
1718 */
1719 int
1720gui_mch_haskey(char_u *name)
1721{
1722 int i;
1723
1724 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1725 if (name[0] == special_keys[i].vim_code0 &&
1726 name[1] == special_keys[i].vim_code1)
1727 return OK;
1728 return FAIL;
1729}
1730
1731 void
1732gui_mch_beep(void)
1733{
LemonBoy77771d32022-04-13 11:47:25 +01001734 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001735}
1736/*
1737 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1738 */
1739 void
1740gui_mch_invert_rectangle(
1741 int r,
1742 int c,
1743 int nr,
1744 int nc)
1745{
1746 RECT rc;
1747
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001748#if defined(FEAT_DIRECTX)
1749 if (IS_ENABLE_DIRECTX())
1750 DWriteContext_Flush(s_dwc);
1751#endif
1752
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753 /*
1754 * Note: InvertRect() excludes right and bottom of rectangle.
1755 */
1756 rc.left = FILL_X(c);
1757 rc.top = FILL_Y(r);
1758 rc.right = rc.left + nc * gui.char_width;
1759 rc.bottom = rc.top + nr * gui.char_height;
1760 InvertRect(s_hdc, &rc);
1761}
1762
1763/*
1764 * Iconify the GUI window.
1765 */
1766 void
1767gui_mch_iconify(void)
1768{
1769 ShowWindow(s_hwnd, SW_MINIMIZE);
1770}
1771
1772/*
1773 * Draw a cursor without focus.
1774 */
1775 void
1776gui_mch_draw_hollow_cursor(guicolor_T color)
1777{
1778 HBRUSH hbr;
1779 RECT rc;
1780
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001781#if defined(FEAT_DIRECTX)
1782 if (IS_ENABLE_DIRECTX())
1783 DWriteContext_Flush(s_dwc);
1784#endif
1785
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001786 /*
1787 * Note: FrameRect() excludes right and bottom of rectangle.
1788 */
1789 rc.left = FILL_X(gui.col);
1790 rc.top = FILL_Y(gui.row);
1791 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 if (mb_lefthalve(gui.row, gui.col))
1793 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001794 rc.bottom = rc.top + gui.char_height;
1795 hbr = CreateSolidBrush(color);
1796 FrameRect(s_hdc, &rc, hbr);
1797 DeleteBrush(hbr);
1798}
1799/*
1800 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1801 * color "color".
1802 */
1803 void
1804gui_mch_draw_part_cursor(
1805 int w,
1806 int h,
1807 guicolor_T color)
1808{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001809 RECT rc;
1810
1811 /*
1812 * Note: FillRect() excludes right and bottom of rectangle.
1813 */
1814 rc.left =
1815#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001816 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1818#endif
1819 FILL_X(gui.col);
1820 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1821 rc.right = rc.left + w;
1822 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001823
Bram Moolenaar92467d32017-12-05 13:22:16 +01001824 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825}
1826
1827
1828/*
1829 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1830 * dead key's nominal character and re-post the original message.
1831 */
1832 static void
1833outputDeadKey_rePost(MSG originalMsg)
1834{
1835 static MSG deadCharExpel;
1836
1837 if (!dead_key)
1838 return;
1839
1840 dead_key = 0;
1841
Bram Moolenaar734a8672019-12-02 22:49:38 +01001842 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001843 deadCharExpel.message = originalMsg.message;
1844 deadCharExpel.hwnd = originalMsg.hwnd;
1845 deadCharExpel.wParam = VK_SPACE;
1846
K.Takata4ac893f2022-01-20 12:44:28 +00001847 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001848
Bram Moolenaar734a8672019-12-02 22:49:38 +01001849 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001850 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1851 originalMsg.lParam);
1852}
1853
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001854/*
1855 * Process a single Windows message.
1856 * If one is not available we hang until one is.
1857 */
1858 static void
1859process_message(void)
1860{
1861 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001862 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001863 char_u string[40];
1864 int i;
1865 int modifiers = 0;
1866 int key;
1867#ifdef FEAT_MENU
1868 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1869#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001870 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871
K.Takatab7057bd2022-01-21 11:37:07 +00001872 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001873
1874#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001875 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001876 if (msg.message == WM_OLE)
1877 {
1878 char_u *str = (char_u *)msg.lParam;
1879 if (str == NULL || *str == NUL)
1880 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001881 // Message can't be ours, forward it. Fixes problem with Ultramon
1882 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001883 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001884 }
1885 else
1886 {
1887 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001888 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001889 }
1890 return;
1891 }
1892#endif
1893
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001895 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001896 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001897 {
1898 HandleMouseHide(msg.message, msg.lParam);
1899 return;
1900 }
1901#endif
1902
1903 /*
1904 * Check if it's a special key that we recognise. If not, call
1905 * TranslateMessage().
1906 */
1907 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1908 {
1909 vk = (int) msg.wParam;
1910
1911 /*
1912 * Handle dead keys in special conditions in other cases we let Windows
1913 * handle them and do not interfere.
1914 *
1915 * The dead_key flag must be reset on several occasions:
1916 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1917 * consumed at that point (This is when we let Windows combine the
1918 * dead character on its own)
1919 *
1920 * - Before doing something special such as regenerating keypresses to
1921 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001922 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001923 * immediately to _OnChar() (or _OnSysChar()).
1924 */
1925 if (dead_key)
1926 {
1927 /*
1928 * If a dead key was pressed and the user presses VK_SPACE,
1929 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1930 * with the dead char now, so do nothing special and let Windows
1931 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01001932 *
1933 * Note that VK_SPACE combines with the dead_key's character and
1934 * only one WM_CHAR will be generated by TranslateMessage(), in
1935 * the two other cases two WM_CHAR will be generated: the dead
1936 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1937 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001938 */
1939 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1940 {
1941 dead_key = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001942 TranslateMessage(&msg);
1943 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001944 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001945 // In modes where we are not typing, dead keys should behave
1946 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001947 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1948 {
1949 outputDeadKey_rePost(msg);
1950 return;
1951 }
1952 }
1953
Bram Moolenaar734a8672019-12-02 22:49:38 +01001954 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001955 if (vk == VK_CANCEL)
1956 {
1957 trash_input_buf();
1958 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001959 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001960 string[0] = Ctrl_C;
1961 add_to_input_buf(string, 1);
1962 }
1963
LemonBoy77fc0b02022-04-22 22:45:52 +01001964 // This is an IME event or a synthetic keystroke, let Windows handle it.
1965 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1966 {
1967 TranslateMessage(&msg);
1968 return;
1969 }
1970
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001971 for (i = 0; special_keys[i].key_sym != 0; i++)
1972 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001973 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001974 if (special_keys[i].key_sym == vk
1975 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1976 {
1977 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001978 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001979 * is a key that would normally trigger the dead key nominal
1980 * character output (such as a NUMPAD printable character or
1981 * the TAB key, etc...).
1982 */
1983 if (dead_key && (special_keys[i].vim_code0 == 'K'
1984 || vk == VK_TAB || vk == CAR))
1985 {
1986 outputDeadKey_rePost(msg);
1987 return;
1988 }
1989
1990#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001991 // Check for <F10>: Windows selects the menu. When <F10> is
1992 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001993 if (vk == VK_F10
1994 && gui.menu_is_active
1995 && check_map(k10, State, FALSE, TRUE, FALSE,
1996 NULL, NULL) == NULL)
1997 break;
1998#endif
LemonBoy45684c62022-04-24 15:46:42 +01001999 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002000
2001 if (special_keys[i].vim_code1 == NUL)
2002 key = special_keys[i].vim_code0;
2003 else
2004 key = TO_SPECIAL(special_keys[i].vim_code0,
2005 special_keys[i].vim_code1);
2006 key = simplify_key(key, &modifiers);
2007 if (key == CSI)
2008 key = K_CSI;
2009
2010 if (modifiers)
2011 {
2012 string[0] = CSI;
2013 string[1] = KS_MODIFIER;
2014 string[2] = modifiers;
2015 add_to_input_buf(string, 3);
2016 }
2017
2018 if (IS_SPECIAL(key))
2019 {
2020 string[0] = CSI;
2021 string[1] = K_SECOND(key);
2022 string[2] = K_THIRD(key);
2023 add_to_input_buf(string, 3);
2024 }
2025 else
2026 {
2027 int len;
2028
Bram Moolenaar734a8672019-12-02 22:49:38 +01002029 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 len = char_to_string(key, string, 40, FALSE);
2031 add_to_input_buf(string, len);
2032 }
2033 break;
2034 }
2035 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002036
2037 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002038 if (special_keys[i].key_sym == 0)
2039 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002040 WCHAR ch[8];
2041 int len;
2042 int i;
2043 UINT scan_code;
2044
LemonBoy77fc0b02022-04-22 22:45:52 +01002045 // Construct the state table with only a few modifiers, we don't
2046 // really care about the presence of Ctrl/Alt as those modifiers are
2047 // handled by Vim separately.
2048 memset(keyboard_state, 0, 256);
2049 if (GetKeyState(VK_SHIFT) & 0x8000)
2050 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002051 if (GetKeyState(VK_CAPITAL) & 0x0001)
2052 keyboard_state[VK_CAPITAL] = 0x01;
LemonBoy45684c62022-04-24 15:46:42 +01002053 // Alt-Gr is synthesized as Alt + Ctrl.
2054 if ((GetKeyState(VK_MENU) & 0x8000) &&
2055 (GetKeyState(VK_CONTROL) & 0x8000))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002056 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002057 keyboard_state[VK_MENU] = 0x80;
2058 keyboard_state[VK_CONTROL] = 0x80;
2059 }
2060
2061 // Translate the virtual key according to the current keyboard
2062 // layout.
2063 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2064 // Convert the scan-code into a sequence of zero or more unicode
2065 // codepoints.
2066 // If this is a dead key ToUnicode returns a negative value.
2067 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2068 0);
2069 dead_key = len < 0;
2070
2071 if (len <= 0)
2072 return;
2073
2074 // Post the message as TranslateMessage would do.
2075 if (msg.message == WM_KEYDOWN)
2076 {
2077 for (i = 0; i < len; i++)
2078 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002079 }
2080 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002081 {
2082 for (i = 0; i < len; i++)
2083 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2084 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002085 }
2086 }
2087#ifdef FEAT_MBYTE_IME
2088 else if (msg.message == WM_IME_NOTIFY)
2089 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2090 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002091 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002092 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093#endif
2094
2095#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002096 // Check for <F10>: Default effect is to select the menu. When <F10> is
2097 // mapped we need to stop it here to avoid strange effects (e.g., for the
2098 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002099 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2100 NULL, NULL) == NULL)
2101#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002102 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002103}
2104
2105/*
2106 * Catch up with any queued events. This may put keyboard input into the
2107 * input buffer, call resize call-backs, trigger timers etc. If there is
2108 * nothing in the event queue (& no timers pending), then we return
2109 * immediately.
2110 */
2111 void
2112gui_mch_update(void)
2113{
2114 MSG msg;
2115
2116 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002117 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002118 && !vim_is_input_buf_full())
2119 process_message();
2120}
2121
Bram Moolenaar4231da42016-06-02 14:30:04 +02002122 static void
2123remove_any_timer(void)
2124{
2125 MSG msg;
2126
2127 if (s_wait_timer != 0 && !s_timed_out)
2128 {
2129 KillTimer(NULL, s_wait_timer);
2130
Bram Moolenaar734a8672019-12-02 22:49:38 +01002131 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002132 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002133 ;
2134 s_wait_timer = 0;
2135 }
2136}
2137
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002138/*
2139 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2140 * from the keyboard.
2141 * wtime == -1 Wait forever.
2142 * wtime == 0 This should never happen.
2143 * wtime > 0 Wait wtime milliseconds for a character.
2144 * Returns OK if a character was found to be available within the given time,
2145 * or FAIL otherwise.
2146 */
2147 int
2148gui_mch_wait_for_chars(int wtime)
2149{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002150 int focus;
2151
2152 s_timed_out = FALSE;
2153
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002154 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002156 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002157 if (s_busy_processing)
2158 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002159
2160 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002161 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2162 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002163 }
2164
2165 allow_scrollbar = TRUE;
2166
2167 focus = gui.in_focus;
2168 while (!s_timed_out)
2169 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002170 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002171 if (gui.in_focus != focus)
2172 {
2173 if (gui.in_focus)
2174 gui_mch_start_blink();
2175 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002176 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002177 focus = gui.in_focus;
2178 }
2179
2180 if (s_need_activate)
2181 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002182 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002183 s_need_activate = FALSE;
2184 }
2185
Bram Moolenaar4231da42016-06-02 14:30:04 +02002186#ifdef FEAT_TIMERS
2187 did_add_timer = FALSE;
2188#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002189#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002190 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002191 for (;;)
2192 {
2193 MSG msg;
2194
2195 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002196# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002197 if (did_add_timer)
2198 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002199# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002200 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002201 {
2202 process_message();
2203 break;
2204 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002205 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002206 // TODO: The 10 msec is a compromise between laggy response
2207 // and consuming more CPU time. Better would be to handle
2208 // channel messages when they arrive.
2209 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002210 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002211 break;
2212 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002213#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002214 // Don't use gui_mch_update() because then we will spin-lock until a
2215 // char arrives, instead we use GetMessage() to hang until an
2216 // event arrives. No need to check for input_buf_full because we are
2217 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002218 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002219#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002220
2221 if (input_available())
2222 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002223 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002224 allow_scrollbar = FALSE;
2225
Bram Moolenaar89c00032019-09-04 13:53:21 +02002226 // Clear pending mouse button, the release event may have been
2227 // taken by the dialog window. But don't do this when getting
2228 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002229 if (!s_getting_focus)
2230 s_button_pending = -1;
2231
2232 return OK;
2233 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002234
2235#ifdef FEAT_TIMERS
2236 if (did_add_timer)
2237 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002238 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002239 remove_any_timer();
2240 break;
2241 }
2242#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002243 }
2244 allow_scrollbar = FALSE;
2245 return FAIL;
2246}
2247
2248/*
2249 * Clear a rectangular region of the screen from text pos (row1, col1) to
2250 * (row2, col2) inclusive.
2251 */
2252 void
2253gui_mch_clear_block(
2254 int row1,
2255 int col1,
2256 int row2,
2257 int col2)
2258{
2259 RECT rc;
2260
2261 /*
2262 * Clear one extra pixel at the far right, for when bold characters have
2263 * spilled over to the window border.
2264 * Note: FillRect() excludes right and bottom of rectangle.
2265 */
2266 rc.left = FILL_X(col1);
2267 rc.top = FILL_Y(row1);
2268 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2269 rc.bottom = FILL_Y(row2 + 1);
2270 clear_rect(&rc);
2271}
2272
2273/*
2274 * Clear the whole text window.
2275 */
2276 void
2277gui_mch_clear_all(void)
2278{
2279 RECT rc;
2280
2281 rc.left = 0;
2282 rc.top = 0;
2283 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2284 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2285 clear_rect(&rc);
2286}
2287/*
2288 * Menu stuff.
2289 */
2290
2291 void
2292gui_mch_enable_menu(int flag)
2293{
2294#ifdef FEAT_MENU
2295 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2296#endif
2297}
2298
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002299 void
2300gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002301 int x UNUSED,
2302 int y UNUSED,
2303 int w UNUSED,
2304 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002306 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307}
2308
2309#if defined(FEAT_MENU) || defined(PROTO)
2310/*
2311 * Make menu item hidden or not hidden
2312 */
2313 void
2314gui_mch_menu_hidden(
2315 vimmenu_T *menu,
2316 int hidden)
2317{
2318 /*
2319 * This doesn't do what we want. Hmm, just grey the menu items for now.
2320 */
2321 /*
2322 if (hidden)
2323 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2324 else
2325 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2326 */
2327 gui_mch_menu_grey(menu, hidden);
2328}
2329
2330/*
2331 * This is called after setting all the menus to grey/hidden or not.
2332 */
2333 void
2334gui_mch_draw_menubar(void)
2335{
2336 DrawMenuBar(s_hwnd);
2337}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002338#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002340/*
2341 * Return the RGB value of a pixel as a long.
2342 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002343 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002344gui_mch_get_rgb(guicolor_T pixel)
2345{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002346 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2347 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002348}
2349
2350#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002351/*
2352 * Convert pixels in X to dialog units
2353 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002354 static WORD
2355PixelToDialogX(int numPixels)
2356{
2357 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2358}
2359
Bram Moolenaar734a8672019-12-02 22:49:38 +01002360/*
2361 * Convert pixels in Y to dialog units
2362 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002363 static WORD
2364PixelToDialogY(int numPixels)
2365{
2366 return (WORD)((numPixels * 8) / s_dlgfntheight);
2367}
2368
Bram Moolenaar734a8672019-12-02 22:49:38 +01002369/*
2370 * Return the width in pixels of the given text in the given DC.
2371 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002372 static int
2373GetTextWidth(HDC hdc, char_u *str, int len)
2374{
2375 SIZE size;
2376
2377 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2378 return size.cx;
2379}
2380
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002381/*
2382 * Return the width in pixels of the given text in the given DC, taking care
2383 * of 'encoding' to active codepage conversion.
2384 */
2385 static int
2386GetTextWidthEnc(HDC hdc, char_u *str, int len)
2387{
2388 SIZE size;
2389 WCHAR *wstr;
2390 int n;
2391 int wlen = len;
2392
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002393 wstr = enc_to_utf16(str, &wlen);
2394 if (wstr == NULL)
2395 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002397 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2398 vim_free(wstr);
2399 if (n)
2400 return size.cx;
2401 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002402}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002403
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002404static void get_work_area(RECT *spi_rect);
2405
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406/*
2407 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002408 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2409 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002410 */
2411 static BOOL
2412CenterWindow(
2413 HWND hwndChild,
2414 HWND hwndParent)
2415{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002416 HMONITOR mon;
2417 MONITORINFO moninfo;
2418 RECT rChild, rParent, rScreen;
2419 int wChild, hChild, wParent, hParent;
2420 int xNew, yNew;
2421 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002422
2423 GetWindowRect(hwndChild, &rChild);
2424 wChild = rChild.right - rChild.left;
2425 hChild = rChild.bottom - rChild.top;
2426
Bram Moolenaar734a8672019-12-02 22:49:38 +01002427 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002428 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002429 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002430 else
2431 GetWindowRect(hwndParent, &rParent);
2432 wParent = rParent.right - rParent.left;
2433 hParent = rParent.bottom - rParent.top;
2434
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002435 moninfo.cbSize = sizeof(MONITORINFO);
2436 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2437 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002439 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002440 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002441 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002443 hdc = GetDC(hwndChild);
2444 rScreen.left = 0;
2445 rScreen.top = 0;
2446 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2447 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2448 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002449 }
2450
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002451 xNew = rParent.left + ((wParent - wChild) / 2);
2452 if (xNew < rScreen.left)
2453 xNew = rScreen.left;
2454 else if ((xNew + wChild) > rScreen.right)
2455 xNew = rScreen.right - wChild;
2456
2457 yNew = rParent.top + ((hParent - hChild) / 2);
2458 if (yNew < rScreen.top)
2459 yNew = rScreen.top;
2460 else if ((yNew + hChild) > rScreen.bottom)
2461 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002462
2463 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2464 SWP_NOSIZE | SWP_NOZORDER);
2465}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002466#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002467
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002468#if defined(FEAT_TOOLBAR) || defined(PROTO)
2469 void
2470gui_mch_show_toolbar(int showit)
2471{
2472 if (s_toolbarhwnd == NULL)
2473 return;
2474
2475 if (showit)
2476 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002477 // Enable unicode support
2478 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2479 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480 ShowWindow(s_toolbarhwnd, SW_SHOW);
2481 }
2482 else
2483 ShowWindow(s_toolbarhwnd, SW_HIDE);
2484}
2485
Bram Moolenaar734a8672019-12-02 22:49:38 +01002486// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002487# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488
2489#endif
2490
2491#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2492 static void
2493add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2494{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002495 WCHAR *wn;
2496 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002497
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002498 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002499 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002500 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002501
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002502 infow.cbSize = sizeof(infow);
2503 infow.fMask = MIIM_TYPE | MIIM_ID;
2504 infow.wID = item_id;
2505 infow.fType = MFT_STRING;
2506 infow.dwTypeData = wn;
2507 infow.cch = (UINT)wcslen(wn);
2508 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2509 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002510}
2511
2512 static void
2513show_tabline_popup_menu(void)
2514{
2515 HMENU tab_pmenu;
2516 long rval;
2517 POINT pt;
2518
Bram Moolenaar734a8672019-12-02 22:49:38 +01002519 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002520 if (hold_gui_events
2521# ifdef FEAT_CMDWIN
2522 || cmdwin_type != 0
2523# endif
2524 )
2525 return;
2526
2527 tab_pmenu = CreatePopupMenu();
2528 if (tab_pmenu == NULL)
2529 return;
2530
2531 if (first_tabpage->tp_next != NULL)
2532 add_tabline_popup_menu_entry(tab_pmenu,
2533 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2534 add_tabline_popup_menu_entry(tab_pmenu,
2535 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2536 add_tabline_popup_menu_entry(tab_pmenu,
2537 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2538
2539 GetCursorPos(&pt);
2540 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2541 NULL);
2542
2543 DestroyMenu(tab_pmenu);
2544
Bram Moolenaar734a8672019-12-02 22:49:38 +01002545 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002546 if (rval > 0)
2547 {
2548 TCHITTESTINFO htinfo;
2549 int idx;
2550
2551 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2552 return;
2553
2554 htinfo.pt.x = pt.x;
2555 htinfo.pt.y = pt.y;
2556 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2557 if (idx == -1)
2558 idx = 0;
2559 else
2560 idx += 1;
2561
2562 send_tabline_menu_event(idx, (int)rval);
2563 }
2564}
2565
2566/*
2567 * Show or hide the tabline.
2568 */
2569 void
2570gui_mch_show_tabline(int showit)
2571{
2572 if (s_tabhwnd == NULL)
2573 return;
2574
2575 if (!showit != !showing_tabline)
2576 {
2577 if (showit)
2578 ShowWindow(s_tabhwnd, SW_SHOW);
2579 else
2580 ShowWindow(s_tabhwnd, SW_HIDE);
2581 showing_tabline = showit;
2582 }
2583}
2584
2585/*
2586 * Return TRUE when tabline is displayed.
2587 */
2588 int
2589gui_mch_showing_tabline(void)
2590{
2591 return s_tabhwnd != NULL && showing_tabline;
2592}
2593
2594/*
2595 * Update the labels of the tabline.
2596 */
2597 void
2598gui_mch_update_tabline(void)
2599{
2600 tabpage_T *tp;
2601 TCITEM tie;
2602 int nr = 0;
2603 int curtabidx = 0;
2604 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002605 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002606
2607 if (s_tabhwnd == NULL)
2608 return;
2609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002610 // Enable unicode support
2611 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002612
2613 tie.mask = TCIF_TEXT;
2614 tie.iImage = -1;
2615
Bram Moolenaar734a8672019-12-02 22:49:38 +01002616 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2618
Bram Moolenaar734a8672019-12-02 22:49:38 +01002619 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2621 {
2622 if (tp == curtab)
2623 curtabidx = nr;
2624
2625 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2626 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002627 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628 tie.pszText = "-Empty-";
2629 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2630 tabadded = 1;
2631 }
2632
2633 get_tabline_label(tp, FALSE);
2634 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002635
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002636 wstr = enc_to_utf16(NameBuff, NULL);
2637 if (wstr != NULL)
2638 {
2639 TCITEMW tiw;
2640
2641 tiw.mask = TCIF_TEXT;
2642 tiw.iImage = -1;
2643 tiw.pszText = wstr;
2644 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2645 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002646 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647 }
2648
Bram Moolenaar734a8672019-12-02 22:49:38 +01002649 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002650 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2651 TabCtrl_DeleteItem(s_tabhwnd, nr);
2652
2653 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2654 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2655
Bram Moolenaar734a8672019-12-02 22:49:38 +01002656 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002657 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2658 RedrawWindow(s_tabhwnd, NULL, NULL,
2659 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2660
2661 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2662 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2663}
2664
2665/*
2666 * Set the current tab to "nr". First tab is 1.
2667 */
2668 void
2669gui_mch_set_curtab(int nr)
2670{
2671 if (s_tabhwnd == NULL)
2672 return;
2673
2674 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2675 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2676}
2677
2678#endif
2679
2680/*
2681 * ":simalt" command.
2682 */
2683 void
2684ex_simalt(exarg_T *eap)
2685{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002686 char_u *keys = eap->arg;
2687 int fill_typebuf = FALSE;
2688 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002689
2690 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2691 while (*keys)
2692 {
2693 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002694 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002695 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2696 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002697 fill_typebuf = TRUE;
2698 }
2699 if (fill_typebuf)
2700 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002701 // Put a NOP in the typeahead buffer so that the message will get
2702 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002703 key_name[0] = K_SPECIAL;
2704 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002705 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002706 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002707#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002708 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002709#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002710 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002711 }
2712}
2713
2714/*
2715 * Create the find & replace dialogs.
2716 * You can't have both at once: ":find" when replace is showing, destroys
2717 * the replace dialog first, and the other way around.
2718 */
2719#ifdef MSWIN_FIND_REPLACE
2720 static void
2721initialise_findrep(char_u *initial_string)
2722{
2723 int wword = FALSE;
2724 int mcase = !p_ic;
2725 char_u *entry_text;
2726
Bram Moolenaar734a8672019-12-02 22:49:38 +01002727 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002728 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2729
2730 s_findrep_struct.hwndOwner = s_hwnd;
2731 s_findrep_struct.Flags = FR_DOWN;
2732 if (mcase)
2733 s_findrep_struct.Flags |= FR_MATCHCASE;
2734 if (wword)
2735 s_findrep_struct.Flags |= FR_WHOLEWORD;
2736 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002737 {
2738 WCHAR *p = enc_to_utf16(entry_text, NULL);
2739 if (p != NULL)
2740 {
2741 int len = s_findrep_struct.wFindWhatLen - 1;
2742
2743 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2744 s_findrep_struct.lpstrFindWhat[len] = NUL;
2745 vim_free(p);
2746 }
2747 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002748 vim_free(entry_text);
2749}
2750#endif
2751
2752 static void
2753set_window_title(HWND hwnd, char *title)
2754{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002755 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002756 {
2757 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758
Bram Moolenaar734a8672019-12-02 22:49:38 +01002759 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2761 if (wbuf != NULL)
2762 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002763 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002764 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002765 }
2766 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002767 else
2768 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002769}
2770
2771 void
2772gui_mch_find_dialog(exarg_T *eap)
2773{
2774#ifdef MSWIN_FIND_REPLACE
2775 if (s_findrep_msg != 0)
2776 {
2777 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2778 DestroyWindow(s_findrep_hwnd);
2779
2780 if (!IsWindow(s_findrep_hwnd))
2781 {
2782 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002783 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002784 }
2785
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002786 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002787 (void)SetFocus(s_findrep_hwnd);
2788
2789 s_findrep_is_find = TRUE;
2790 }
2791#endif
2792}
2793
2794
2795 void
2796gui_mch_replace_dialog(exarg_T *eap)
2797{
2798#ifdef MSWIN_FIND_REPLACE
2799 if (s_findrep_msg != 0)
2800 {
2801 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2802 DestroyWindow(s_findrep_hwnd);
2803
2804 if (!IsWindow(s_findrep_hwnd))
2805 {
2806 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002807 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002808 }
2809
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002810 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811 (void)SetFocus(s_findrep_hwnd);
2812
2813 s_findrep_is_find = FALSE;
2814 }
2815#endif
2816}
2817
2818
2819/*
2820 * Set visibility of the pointer.
2821 */
2822 void
2823gui_mch_mousehide(int hide)
2824{
2825 if (hide != gui.pointer_hidden)
2826 {
2827 ShowCursor(!hide);
2828 gui.pointer_hidden = hide;
2829 }
2830}
2831
2832#ifdef FEAT_MENU
2833 static void
2834gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2835{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002836 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002837 gui_mch_mousehide(FALSE);
2838
2839 (void)TrackPopupMenu(
2840 (HMENU)menu->submenu_id,
2841 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2842 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002843 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002844 s_hwnd,
2845 NULL);
2846 /*
2847 * NOTE: The pop-up menu can eat the mouse up event.
2848 * We deal with this in normal.c.
2849 */
2850}
2851#endif
2852
2853/*
2854 * Got a message when the system will go down.
2855 */
2856 static void
2857_OnEndSession(void)
2858{
2859 getout_preserve_modified(1);
2860}
2861
2862/*
2863 * Get this message when the user clicks on the cross in the top right corner
2864 * of a Windows95 window.
2865 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002866 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002867_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002868{
2869 gui_shell_closed();
2870}
2871
2872/*
2873 * Get a message when the window is being destroyed.
2874 */
2875 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002876_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877{
2878 if (!destroying)
2879 _OnClose(hwnd);
2880}
2881
2882 static void
2883_OnPaint(
2884 HWND hwnd)
2885{
2886 if (!IsMinimized(hwnd))
2887 {
2888 PAINTSTRUCT ps;
2889
Bram Moolenaar734a8672019-12-02 22:49:38 +01002890 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002891 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002892
Bram Moolenaar734a8672019-12-02 22:49:38 +01002893 // prevent multi-byte characters from misprinting on an invalid
2894 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002895 if (has_mbyte)
2896 {
2897 RECT rect;
2898
2899 GetClientRect(hwnd, &rect);
2900 ps.rcPaint.left = rect.left;
2901 ps.rcPaint.right = rect.right;
2902 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903
2904 if (!IsRectEmpty(&ps.rcPaint))
2905 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002906 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2907 ps.rcPaint.right - ps.rcPaint.left + 1,
2908 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2909 }
2910
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002911 EndPaint(hwnd, &ps);
2912 }
2913}
2914
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915 static void
2916_OnSize(
2917 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002918 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919 int cx,
2920 int cy)
2921{
K.Takatac81e9bf2022-01-16 14:15:49 +00002922 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 {
2924 gui_resize_shell(cx, cy);
2925
Bram Moolenaar734a8672019-12-02 22:49:38 +01002926 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002927 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928 }
2929}
2930
2931 static void
2932_OnSetFocus(
2933 HWND hwnd,
2934 HWND hwndOldFocus)
2935{
2936 gui_focus_change(TRUE);
2937 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002938 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939}
2940
2941 static void
2942_OnKillFocus(
2943 HWND hwnd,
2944 HWND hwndNewFocus)
2945{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002946 if (destroying)
2947 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002948 gui_focus_change(FALSE);
2949 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002950 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002951}
2952
2953/*
2954 * Get a message when the user switches back to vim
2955 */
2956 static LRESULT
2957_OnActivateApp(
2958 HWND hwnd,
2959 BOOL fActivate,
2960 DWORD dwThreadId)
2961{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002962 // we call gui_focus_change() in _OnSetFocus()
2963 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002964 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002965}
2966
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002967 void
2968gui_mch_destroy_scrollbar(scrollbar_T *sb)
2969{
2970 DestroyWindow(sb->id);
2971}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002972
2973/*
2974 * Get current mouse coordinates in text window.
2975 */
2976 void
2977gui_mch_getmouse(int *x, int *y)
2978{
2979 RECT rct;
2980 POINT mp;
2981
2982 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002983 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984 *x = (int)(mp.x - rct.left);
2985 *y = (int)(mp.y - rct.top);
2986}
2987
2988/*
2989 * Move mouse pointer to character at (x, y).
2990 */
2991 void
2992gui_mch_setmouse(int x, int y)
2993{
2994 RECT rct;
2995
2996 (void)GetWindowRect(s_textArea, &rct);
2997 (void)SetCursorPos(x + gui.border_offset + rct.left,
2998 y + gui.border_offset + rct.top);
2999}
3000
3001 static void
3002gui_mswin_get_valid_dimensions(
3003 int w,
3004 int h,
3005 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003006 int *valid_h,
3007 int *cols,
3008 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003009{
3010 int base_width, base_height;
3011
3012 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003013 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3014 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003016 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3017 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3018 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3019 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003020 *cols = (w - base_width) / gui.char_width;
3021 *rows = (h - base_height) / gui.char_height;
3022 *valid_w = base_width + *cols * gui.char_width;
3023 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003024}
3025
3026 void
3027gui_mch_flash(int msec)
3028{
3029 RECT rc;
3030
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003031#if defined(FEAT_DIRECTX)
3032 if (IS_ENABLE_DIRECTX())
3033 DWriteContext_Flush(s_dwc);
3034#endif
3035
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036 /*
3037 * Note: InvertRect() excludes right and bottom of rectangle.
3038 */
3039 rc.left = 0;
3040 rc.top = 0;
3041 rc.right = gui.num_cols * gui.char_width;
3042 rc.bottom = gui.num_rows * gui.char_height;
3043 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003044 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003045
Bram Moolenaar734a8672019-12-02 22:49:38 +01003046 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003047
3048 InvertRect(s_hdc, &rc);
3049}
3050
3051/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003052 * Check if the specified point is on-screen. (multi-monitor aware)
3053 */
3054 static BOOL
3055is_point_onscreen(int x, int y)
3056{
3057 POINT pt = {x, y};
3058
3059 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3060}
3061
3062/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003063 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003064 *
3065 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3066 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3067 * only works when the whole window is on-screen.
3068 */
3069 static BOOL
3070is_window_onscreen(HWND hwnd)
3071{
3072 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003073 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003074
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003075 GetClientRect(hwnd, &rc);
3076 p1.x = rc.left;
3077 p1.y = rc.top;
3078 p2.x = rc.right - 1;
3079 p2.y = rc.bottom - 1;
3080 ClientToScreen(hwnd, &p1);
3081 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003082
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003083 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003084 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003085 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003086 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003087 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003088 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003089 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003090 return FALSE;
3091 return TRUE;
3092}
3093
3094/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003095 * Return flags used for scrolling.
3096 * The SW_INVALIDATE is required when part of the window is covered or
3097 * off-screen. Refer to MS KB Q75236.
3098 */
3099 static int
3100get_scroll_flags(void)
3101{
3102 HWND hwnd;
3103 RECT rcVim, rcOther, rcDest;
3104
Bram Moolenaar185577e2020-10-29 20:08:21 +01003105 // Check if the window is (partly) off-screen.
3106 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107 return SW_INVALIDATE;
3108
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003109 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003110 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003111 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3112 if (IsWindowVisible(hwnd))
3113 {
3114 GetWindowRect(hwnd, &rcOther);
3115 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3116 return SW_INVALIDATE;
3117 }
3118 return 0;
3119}
3120
3121/*
3122 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3123 * may not be scrolled out properly.
3124 * For gVim, when _OnScroll() is repeated, the character at the
3125 * previous cursor position may be left drawn after scroll.
3126 * The problem can be avoided by calling GetPixel() to get a pixel in
3127 * the region before ScrollWindowEx().
3128 */
3129 static void
3130intel_gpu_workaround(void)
3131{
3132 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3133}
3134
3135/*
3136 * Delete the given number of lines from the given row, scrolling up any
3137 * text further down within the scroll region.
3138 */
3139 void
3140gui_mch_delete_lines(
3141 int row,
3142 int num_lines)
3143{
3144 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003145
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003146 rc.left = FILL_X(gui.scroll_region_left);
3147 rc.right = FILL_X(gui.scroll_region_right + 1);
3148 rc.top = FILL_Y(row);
3149 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3150
Bram Moolenaar92467d32017-12-05 13:22:16 +01003151#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003152 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003153 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003154 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003155 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003156 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003157#endif
3158 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003159#if defined(FEAT_DIRECTX)
3160 if (IS_ENABLE_DIRECTX())
3161 DWriteContext_Flush(s_dwc);
3162#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 intel_gpu_workaround();
3164 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003165 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003166 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003167 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003168
Bram Moolenaar734a8672019-12-02 22:49:38 +01003169 // This seems to be required to avoid the cursor disappearing when
3170 // scrolling such that the cursor ends up in the top-left character on
3171 // the screen... But why? (Webb)
3172 // It's probably fixed by disabling drawing the cursor while scrolling.
3173 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003174
3175 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3176 gui.scroll_region_left,
3177 gui.scroll_region_bot, gui.scroll_region_right);
3178}
3179
3180/*
3181 * Insert the given number of lines before the given row, scrolling down any
3182 * following text within the scroll region.
3183 */
3184 void
3185gui_mch_insert_lines(
3186 int row,
3187 int num_lines)
3188{
3189 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003190
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191 rc.left = FILL_X(gui.scroll_region_left);
3192 rc.right = FILL_X(gui.scroll_region_right + 1);
3193 rc.top = FILL_Y(row);
3194 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003195
3196#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003197 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003198 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003199 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003200 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003201 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003202#endif
3203 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003204#if defined(FEAT_DIRECTX)
3205 if (IS_ENABLE_DIRECTX())
3206 DWriteContext_Flush(s_dwc);
3207#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003209 // The SW_INVALIDATE is required when part of the window is covered or
3210 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003211 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003213 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003214 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003216 gui_clear_block(row, gui.scroll_region_left,
3217 row + num_lines - 1, gui.scroll_region_right);
3218}
3219
3220
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003222gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003223{
3224#if defined(FEAT_DIRECTX)
3225 DWriteContext_Close(s_dwc);
3226 DWrite_Final();
3227 s_dwc = NULL;
3228#endif
3229
3230 ReleaseDC(s_textArea, s_hdc);
3231 DeleteObject(s_brush);
3232
3233#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003234 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3236#endif
3237
Bram Moolenaar734a8672019-12-02 22:49:38 +01003238 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 if (s_hwnd != NULL)
3240 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003241 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242 DestroyWindow(s_hwnd);
3243 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003244}
3245
3246 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003247logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248{
3249 char *p;
3250 char *res;
3251 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003252 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003253 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003254 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003256 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3257 if (font_name == NULL)
3258 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003259 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003260 quality_name = quality_id2name((int)lf.lfQuality);
3261
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003262 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003263 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003264 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003265 if (res != NULL)
3266 {
3267 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003268 // make a normal font string out of the lf thing:
3269 points = pixels_to_points(
3270 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3271 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3272 sprintf((char *)p, "%s:h%d", font_name, points);
3273 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003274 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 while (*p)
3276 {
3277 if (*p == ' ')
3278 *p = '_';
3279 ++p;
3280 }
3281 if (lf.lfItalic)
3282 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003283 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284 STRCAT(p, ":b");
3285 if (lf.lfUnderline)
3286 STRCAT(p, ":u");
3287 if (lf.lfStrikeOut)
3288 STRCAT(p, ":s");
3289 if (charset_name != NULL)
3290 {
3291 STRCAT(p, ":c");
3292 STRCAT(p, charset_name);
3293 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003294 if (quality_name != NULL)
3295 {
3296 STRCAT(p, ":q");
3297 STRCAT(p, quality_name);
3298 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003299 }
3300
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003301 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003302 return (char_u *)res;
3303}
3304
3305
3306#ifdef FEAT_MBYTE_IME
3307/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003308 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003309 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310 */
3311 static void
3312update_im_font(void)
3313{
K.Takatac81e9bf2022-01-16 14:15:49 +00003314 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003315
3316 if (p_guifontwide != NULL && *p_guifontwide != NUL
3317 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003318 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003319 norm_logfont = lf_wide;
3320 else
3321 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003322
3323 lf = norm_logfont;
3324 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3325 // Work around when PerMonitorV2 is not enabled in the process level.
3326 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3327 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328}
3329#endif
3330
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331/*
3332 * Handler of gui.wide_font (p_guifontwide) changed notification.
3333 */
3334 void
3335gui_mch_wide_font_changed(void)
3336{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003337 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003339#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003341#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342
3343 gui_mch_free_font(gui.wide_ital_font);
3344 gui.wide_ital_font = NOFONT;
3345 gui_mch_free_font(gui.wide_bold_font);
3346 gui.wide_bold_font = NOFONT;
3347 gui_mch_free_font(gui.wide_boldital_font);
3348 gui.wide_boldital_font = NOFONT;
3349
3350 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003351 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 {
3353 if (!lf.lfItalic)
3354 {
3355 lf.lfItalic = TRUE;
3356 gui.wide_ital_font = get_font_handle(&lf);
3357 lf.lfItalic = FALSE;
3358 }
3359 if (lf.lfWeight < FW_BOLD)
3360 {
3361 lf.lfWeight = FW_BOLD;
3362 gui.wide_bold_font = get_font_handle(&lf);
3363 if (!lf.lfItalic)
3364 {
3365 lf.lfItalic = TRUE;
3366 gui.wide_boldital_font = get_font_handle(&lf);
3367 }
3368 }
3369 }
3370}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003371
3372/*
3373 * Initialise vim to use the font with the given name.
3374 * Return FAIL if the font could not be loaded, OK otherwise.
3375 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003377gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378{
K.Takatac81e9bf2022-01-16 14:15:49 +00003379 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003380 GuiFont font = NOFONT;
3381 char_u *p;
3382
Bram Moolenaar734a8672019-12-02 22:49:38 +01003383 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003385 {
3386 lfOrig = lf;
3387 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003389 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003390 if (font == NOFONT)
3391 return FAIL;
3392
3393 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003394 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003395#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396 norm_logfont = lf;
3397 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003398 if (!s_in_dpichanged)
3399 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400#endif
3401 gui_mch_free_font(gui.norm_font);
3402 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003403 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404 GetFontSize(font);
3405
K.Takatac81e9bf2022-01-16 14:15:49 +00003406 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407 if (p != NULL)
3408 {
3409 hl_set_font_name(p);
3410
Bram Moolenaar734a8672019-12-02 22:49:38 +01003411 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3413 {
3414 vim_free(p_guifont);
3415 p_guifont = p;
3416 }
3417 else
3418 vim_free(p);
3419 }
3420
3421 gui_mch_free_font(gui.ital_font);
3422 gui.ital_font = NOFONT;
3423 gui_mch_free_font(gui.bold_font);
3424 gui.bold_font = NOFONT;
3425 gui_mch_free_font(gui.boldital_font);
3426 gui.boldital_font = NOFONT;
3427
3428 if (!lf.lfItalic)
3429 {
3430 lf.lfItalic = TRUE;
3431 gui.ital_font = get_font_handle(&lf);
3432 lf.lfItalic = FALSE;
3433 }
3434 if (lf.lfWeight < FW_BOLD)
3435 {
3436 lf.lfWeight = FW_BOLD;
3437 gui.bold_font = get_font_handle(&lf);
3438 if (!lf.lfItalic)
3439 {
3440 lf.lfItalic = TRUE;
3441 gui.boldital_font = get_font_handle(&lf);
3442 }
3443 }
3444
3445 return OK;
3446}
3447
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003448/*
3449 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003450 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 */
3452 int
3453gui_mch_maximized(void)
3454{
3455 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003456 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003457
3458 wp.length = sizeof(WINDOWPLACEMENT);
3459 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003460 {
3461 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003463 && wp.flags == WPF_RESTORETOMAXIMIZED))
3464 return TRUE;
3465 if (wp.showCmd == SW_SHOWMINIMIZED)
3466 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003467
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003468 // Assume the window is snapped when the sizes from two APIs differ.
3469 GetWindowRect(s_hwnd, &rc);
3470 if ((rc.right - rc.left !=
3471 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3472 || (rc.bottom - rc.top !=
3473 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3474 return TRUE;
3475 }
3476 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003477}
3478
3479/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003480 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3481 * is set. Compute the new Rows and Columns. This is like resizing the
3482 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483 */
3484 void
3485gui_mch_newfont(void)
3486{
3487 RECT rect;
3488
3489 GetWindowRect(s_hwnd, &rect);
3490 if (win_socket_id == 0)
3491 {
3492 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003493 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3494 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003495 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003496 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3497 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3498 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3499 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 }
3501 else
3502 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003503 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003505 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 }
3507}
3508
3509/*
3510 * Set the window title
3511 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512 void
3513gui_mch_settitle(
3514 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003515 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516{
3517 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3518}
3519
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003520#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003521// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3522// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523static LPCSTR mshape_idcs[] =
3524{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003525 IDC_ARROW, // arrow
3526 MAKEINTRESOURCE(0), // blank
3527 IDC_IBEAM, // beam
3528 IDC_SIZENS, // updown
3529 IDC_SIZENS, // udsizing
3530 IDC_SIZEWE, // leftright
3531 IDC_SIZEWE, // lrsizing
3532 IDC_WAIT, // busy
3533 IDC_NO, // no
3534 IDC_ARROW, // crosshair
3535 IDC_ARROW, // hand1
3536 IDC_ARROW, // hand2
3537 IDC_ARROW, // pencil
3538 IDC_ARROW, // question
3539 IDC_ARROW, // right-arrow
3540 IDC_UPARROW, // up-arrow
3541 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542};
3543
3544 void
3545mch_set_mouse_shape(int shape)
3546{
3547 LPCSTR idc;
3548
3549 if (shape == MSHAPE_HIDE)
3550 ShowCursor(FALSE);
3551 else
3552 {
3553 if (shape >= MSHAPE_NUMBERED)
3554 idc = IDC_ARROW;
3555 else
3556 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003557 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003558 if (!p_mh)
3559 {
3560 POINT mp;
3561
Bram Moolenaar734a8672019-12-02 22:49:38 +01003562 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003563 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564 (void)SetCursorPos(mp.x, mp.y);
3565 ShowCursor(TRUE);
3566 }
3567 }
3568}
3569#endif
3570
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003571#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 * Wide version of convert_filter().
3574 */
3575 static WCHAR *
3576convert_filterW(char_u *s)
3577{
3578 char_u *tmp;
3579 int len;
3580 WCHAR *res;
3581
3582 tmp = convert_filter(s);
3583 if (tmp == NULL)
3584 return NULL;
3585 len = (int)STRLEN(s) + 3;
3586 res = enc_to_utf16(tmp, &len);
3587 vim_free(tmp);
3588 return res;
3589}
3590
3591/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003592 * Pop open a file browser and return the file selected, in allocated memory,
3593 * or NULL if Cancel is hit.
3594 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3595 * title - Title message for the file browser dialog.
3596 * dflt - Default name of file.
3597 * ext - Default extension to be added to files without extensions.
3598 * initdir - directory in which to open the browser (NULL = current dir)
3599 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003600 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003601 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003602gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003603 int saving,
3604 char_u *title,
3605 char_u *dflt,
3606 char_u *ext,
3607 char_u *initdir,
3608 char_u *filter)
3609{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003610 // We always use the wide function. This means enc_to_utf16() must work,
3611 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003612 OPENFILENAMEW fileStruct;
3613 WCHAR fileBuf[MAXPATHL];
3614 WCHAR *wp;
3615 int i;
3616 WCHAR *titlep = NULL;
3617 WCHAR *extp = NULL;
3618 WCHAR *initdirp = NULL;
3619 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003620 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003621 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622
3623 if (dflt == NULL)
3624 fileBuf[0] = NUL;
3625 else
3626 {
3627 wp = enc_to_utf16(dflt, NULL);
3628 if (wp == NULL)
3629 fileBuf[0] = NUL;
3630 else
3631 {
3632 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3633 fileBuf[i] = wp[i];
3634 fileBuf[i] = NUL;
3635 vim_free(wp);
3636 }
3637 }
3638
Bram Moolenaar734a8672019-12-02 22:49:38 +01003639 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003640 filterp = convert_filterW(filter);
3641
Bram Moolenaara80faa82020-04-12 19:37:17 +02003642 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003643# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003644 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003645 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003646# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003648# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003649
3650 if (title != NULL)
3651 titlep = enc_to_utf16(title, NULL);
3652 fileStruct.lpstrTitle = titlep;
3653
3654 if (ext != NULL)
3655 extp = enc_to_utf16(ext, NULL);
3656 fileStruct.lpstrDefExt = extp;
3657
3658 fileStruct.lpstrFile = fileBuf;
3659 fileStruct.nMaxFile = MAXPATHL;
3660 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003661 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3662 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663 if (initdir != NULL && *initdir != NUL)
3664 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003665 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666 initdirp = enc_to_utf16(initdir, NULL);
3667 if (initdirp != NULL)
3668 {
3669 for (wp = initdirp; *wp != NUL; ++wp)
3670 if (*wp == '/')
3671 *wp = '\\';
3672 }
3673 fileStruct.lpstrInitialDir = initdirp;
3674 }
3675
3676 /*
3677 * TODO: Allow selection of multiple files. Needs another arg to this
3678 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3679 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3680 * files that don't exist yet, so I haven't put it in. What about
3681 * OFN_PATHMUSTEXIST?
3682 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3683 */
3684 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003685# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 if (curbuf->b_p_bin)
3687 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003688# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003690 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003692 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003693
3694 vim_free(filterp);
3695 vim_free(initdirp);
3696 vim_free(titlep);
3697 vim_free(extp);
3698
K.Takata14b8d6a2022-01-20 15:05:22 +00003699 if (!ret)
3700 return NULL;
3701
3702 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003703 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003704 if (p == NULL)
3705 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706
Bram Moolenaar734a8672019-12-02 22:49:38 +01003707 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003708 SetFocus(s_hwnd);
3709
Bram Moolenaar734a8672019-12-02 22:49:38 +01003710 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003711 q = vim_strsave(shorten_fname1(p));
3712 vim_free(p);
3713 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715
3716
3717/*
3718 * Convert the string s to the proper format for a filter string by replacing
3719 * the \t and \n delimiters with \0.
3720 * Returns the converted string in allocated memory.
3721 *
3722 * Keep in sync with convert_filterW() above!
3723 */
3724 static char_u *
3725convert_filter(char_u *s)
3726{
3727 char_u *res;
3728 unsigned s_len = (unsigned)STRLEN(s);
3729 unsigned i;
3730
3731 res = alloc(s_len + 3);
3732 if (res != NULL)
3733 {
3734 for (i = 0; i < s_len; ++i)
3735 if (s[i] == '\t' || s[i] == '\n')
3736 res[i] = '\0';
3737 else
3738 res[i] = s[i];
3739 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003740 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003741 res[s_len + 1] = NUL;
3742 res[s_len + 2] = NUL;
3743 }
3744 return res;
3745}
3746
3747/*
3748 * Select a directory.
3749 */
3750 char_u *
3751gui_mch_browsedir(char_u *title, char_u *initdir)
3752{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003753 // We fake this: Use a filter that doesn't select anything and a default
3754 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3756 initdir, (char_u *)_("Directory\t*.nothing\n"));
3757}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003758#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 static void
3761_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003762 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003763 HDROP hDrop)
3764{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003765#define BUFPATHLEN _MAX_PATH
3766#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 char szFile[BUFPATHLEN];
3769 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3770 UINT i;
3771 char_u **fnames;
3772 POINT pt;
3773 int_u modifiers = 0;
3774
Bram Moolenaar734a8672019-12-02 22:49:38 +01003775 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 DragQueryPoint(hDrop, &pt);
3777 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3778
3779 reset_VIsual();
3780
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003781 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782
3783 if (fnames != NULL)
3784 for (i = 0; i < cFiles; ++i)
3785 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3787 fnames[i] = utf16_to_enc(wszFile, NULL);
3788 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789 {
3790 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3791 fnames[i] = vim_strsave((char_u *)szFile);
3792 }
3793 }
3794
3795 DragFinish(hDrop);
3796
3797 if (fnames != NULL)
3798 {
LemonBoy45684c62022-04-24 15:46:42 +01003799 int kbd_modifiers = get_active_modifiers();
3800
3801 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003803 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003804 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003805 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003806 modifiers |= MOUSE_ALT;
3807
3808 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3809
3810 s_need_activate = TRUE;
3811 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812}
3813
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 static int
3815_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003816 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 HWND hwndCtl,
3818 UINT code,
3819 int pos)
3820{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003821 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003822 scrollbar_T *sb, *sb_info;
3823 long val;
3824 int dragging = FALSE;
3825 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003826 SCROLLINFO si;
3827
3828 si.cbSize = sizeof(si);
3829 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830
3831 sb = gui_mswin_find_scrollbar(hwndCtl);
3832 if (sb == NULL)
3833 return 0;
3834
Bram Moolenaar734a8672019-12-02 22:49:38 +01003835 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003836 {
3837 /*
3838 * Careful: need to get scrollbar info out of first (left) scrollbar
3839 * for window, but keep real scrollbar too because we must pass it to
3840 * gui_drag_scrollbar().
3841 */
3842 sb_info = &sb->wp->w_scrollbars[0];
3843 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003844 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845 sb_info = sb;
3846 val = sb_info->value;
3847
3848 switch (code)
3849 {
3850 case SB_THUMBTRACK:
3851 val = pos;
3852 dragging = TRUE;
3853 if (sb->scroll_shift > 0)
3854 val <<= sb->scroll_shift;
3855 break;
3856 case SB_LINEDOWN:
3857 val++;
3858 break;
3859 case SB_LINEUP:
3860 val--;
3861 break;
3862 case SB_PAGEDOWN:
3863 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3864 break;
3865 case SB_PAGEUP:
3866 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3867 break;
3868 case SB_TOP:
3869 val = 0;
3870 break;
3871 case SB_BOTTOM:
3872 val = sb_info->max;
3873 break;
3874 case SB_ENDSCROLL:
3875 if (prev_code == SB_THUMBTRACK)
3876 {
3877 /*
3878 * "pos" only gives us 16-bit data. In case of large file,
3879 * use GetScrollPos() which returns 32-bit. Unfortunately it
3880 * is not valid while the scrollbar is being dragged.
3881 */
3882 val = GetScrollPos(hwndCtl, SB_CTL);
3883 if (sb->scroll_shift > 0)
3884 val <<= sb->scroll_shift;
3885 }
3886 break;
3887
3888 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003889 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890 return 0;
3891 }
3892 prev_code = code;
3893
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003894 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3895 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003896
3897 /*
3898 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3899 */
3900 if (sb->wp != NULL)
3901 {
3902 scrollbar_T *sba = sb->wp->w_scrollbars;
3903 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3904
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906 }
3907
Bram Moolenaar734a8672019-12-02 22:49:38 +01003908 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909 s_busy_processing = TRUE;
3910
Bram Moolenaar734a8672019-12-02 22:49:38 +01003911 // When "allow_scrollbar" is FALSE still need to remember the new
3912 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913 dont_scroll = !allow_scrollbar;
3914
Bram Moolenaara338adc2018-01-31 20:51:47 +01003915 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003916 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003917 mch_enable_flush();
3918 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919
3920 s_busy_processing = FALSE;
3921 dont_scroll = dont_scroll_save;
3922
3923 return 0;
3924}
3925
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#ifdef FEAT_XPM_W32
3928# include "xpm_w32.h"
3929#endif
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
Bram Moolenaar734a8672019-12-02 22:49:38 +01003932// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#define TEAROFF_PADDING_X 2
3934#define TEAROFF_BUTTON_PAD_X 8
3935#define TEAROFF_MIN_WIDTH 200
3936#define TEAROFF_SUBMENU_LABEL ">>"
3937#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3938
3939
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003940#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941# define ID_BEVAL_TOOLTIP 200
3942# define BEVAL_TEXT_LEN MAXPATHL
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003945static UINT_PTR beval_timer_id = 0;
3946static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003947#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003948
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003949
Bram Moolenaar734a8672019-12-02 22:49:38 +01003950// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952#ifdef FEAT_MENU
3953static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
3956/*
3957 * Use the system font for dialogs and tear-off menus. Remove this line to
3958 * use DLG_FONT_NAME.
3959 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003960#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#define VIM_CLASSW L"Vim"
3964
Bram Moolenaar734a8672019-12-02 22:49:38 +01003965// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3966// thus there should be room for every dialog. For tearoffs it's made bigger
3967// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968#define DLG_ALLOC_SIZE 16 * 1024
3969
3970/*
3971 * stuff for dialogs, menus, tearoffs etc.
3972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973static PWORD
3974add_dialog_element(
3975 PWORD p,
3976 DWORD lStyle,
3977 WORD x,
3978 WORD y,
3979 WORD w,
3980 WORD h,
3981 WORD Id,
3982 WORD clss,
3983 const char *caption);
3984static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003985static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003986#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989static void get_dialog_font_metrics(void);
3990
3991static int dialog_default_button = -1;
3992
Bram Moolenaar734a8672019-12-02 22:49:38 +01003993// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996#ifdef FEAT_TOOLBAR
3997static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00003998static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003999static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004001#else
4002# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#endif
4004
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004005#ifdef FEAT_GUI_TABLINE
4006static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004007static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004008#endif
4009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010#ifdef FEAT_MBYTE_IME
4011static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4012static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4013#endif
4014#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4015# ifdef NOIME
4016typedef struct tagCOMPOSITIONFORM {
4017 DWORD dwStyle;
4018 POINT ptCurrentPos;
4019 RECT rcArea;
4020} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4021typedef HANDLE HIMC;
4022# endif
4023
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004024static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004025static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4026static HIMC (WINAPI *pImmGetContext)(HWND);
4027static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4028static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4029static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4030static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004031static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4032static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004033static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4034static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004035static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036static void dyn_imm_load(void);
4037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038# define pImmGetCompositionStringW ImmGetCompositionStringW
4039# define pImmGetContext ImmGetContext
4040# define pImmAssociateContext ImmAssociateContext
4041# define pImmReleaseContext ImmReleaseContext
4042# define pImmGetOpenStatus ImmGetOpenStatus
4043# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004044# define pImmGetCompositionFontW ImmGetCompositionFontW
4045# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046# define pImmSetCompositionWindow ImmSetCompositionWindow
4047# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004048# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
4050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#ifdef FEAT_MENU
4052/*
4053 * Figure out how high the menu bar is at the moment.
4054 */
4055 static int
4056gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004057 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
4059 static int old_menu_height = -1;
4060
4061 RECT rc1, rc2;
4062 int num;
4063 int menu_height;
4064
4065 if (gui.menu_is_active)
4066 num = GetMenuItemCount(s_menuBar);
4067 else
4068 num = 0;
4069
4070 if (num == 0)
4071 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004072 else if (IsMinimized(s_hwnd))
4073 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004074 // The height of the menu cannot be determined while the window is
4075 // minimized. Take the previous height if the menu is changed in that
4076 // state, to avoid that Vim's vertical window size accidentally
4077 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004078 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 else
4081 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004082 /*
4083 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4084 * seem to have been set yet, so menu wraps in default window
4085 * width which is very narrow. Instead just return height of a
4086 * single menu item. Will still be wrong when the menu really
4087 * should wrap over more than one line.
4088 */
4089 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4090 if (gui.starting)
4091 menu_height = rc1.bottom - rc1.top + 1;
4092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004094 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4095 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 }
4098
4099 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004100 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004101 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 return menu_height;
4104}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004105#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107
4108/*
4109 * Setup for the Intellimouse
4110 */
4111 static void
4112init_mouse_wheel(void)
4113{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004114 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaar734a8672019-12-02 22:49:38 +01004116 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004117 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4118 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119}
4120
4121
Bram Moolenaarae20f342019-11-05 21:09:23 +01004122/*
4123 * Intellimouse wheel handler.
4124 * Treat a mouse wheel event as if it were a scroll request.
4125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 static void
4127_OnMouseWheel(
4128 HWND hwnd,
4129 short zDelta)
4130{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int i;
4132 int size;
4133 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004134 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (mouse_scroll_lines == 0)
4137 init_mouse_wheel();
4138
Bram Moolenaarae20f342019-11-05 21:09:23 +01004139 wp = gui_mouse_window(FIND_POPUP);
4140
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004141#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004142 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004143 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004144 cmdarg_T cap;
4145 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004146
Bram Moolenaarae20f342019-11-05 21:09:23 +01004147 // Mouse hovers over popup window, scroll it if possible.
4148 mouse_row = wp->w_winrow;
4149 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004150 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004151 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4152 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4153 clear_oparg(&oa);
4154 cap.oap = &oa;
4155 nv_mousescroll(&cap);
4156 update_screen(0);
4157 setcursor();
4158 out_flush();
4159 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004160 }
4161#endif
4162
Bram Moolenaarae20f342019-11-05 21:09:23 +01004163 if (wp == NULL || !p_scf)
4164 wp = curwin;
4165
4166 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4167 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4168 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4169 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4170 else
4171 return;
4172 size = wp->w_height;
4173
Bram Moolenaara338adc2018-01-31 20:51:47 +01004174 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 if (mouse_scroll_lines > 0
4176 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4177 {
4178 for (i = mouse_scroll_lines; i > 0; --i)
4179 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4180 }
4181 else
4182 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004183 mch_enable_flush();
4184 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185}
4186
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004187#ifdef USE_SYSMENU_FONT
4188/*
4189 * Get Menu Font.
4190 * Return OK or FAIL.
4191 */
4192 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004193gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004194{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004195 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004196
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004197 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4198 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004199 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004200 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004201 &nm,
4202 0))
4203 return FAIL;
4204 *lf = nm.lfMenuFont;
4205 return OK;
4206}
4207#endif
4208
4209
4210#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4211/*
4212 * Set the GUI tabline font to the system menu font
4213 */
4214 static void
4215set_tabline_font(void)
4216{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004217 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004218 HFONT font;
4219 HWND hwnd;
4220 HDC hdc;
4221 HFONT hfntOld;
4222 TEXTMETRIC tm;
4223
4224 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4225 return;
4226
K.Takatac81e9bf2022-01-16 14:15:49 +00004227 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004228 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004229
4230 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4231
4232 /*
4233 * Compute the height of the font used for the tab text
4234 */
4235 hwnd = GetDesktopWindow();
4236 hdc = GetWindowDC(hwnd);
4237 hfntOld = SelectFont(hdc, font);
4238
4239 GetTextMetrics(hdc, &tm);
4240
4241 SelectFont(hdc, hfntOld);
4242 ReleaseDC(hwnd, hdc);
4243
4244 /*
4245 * The space used by the tab border and the space between the tab label
4246 * and the tab border is included as 7.
4247 */
4248 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4249}
K.Takatac81e9bf2022-01-16 14:15:49 +00004250#else
4251# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004252#endif
4253
Bram Moolenaar520470a2005-06-16 21:59:56 +00004254/*
4255 * Invoked when a setting was changed.
4256 */
4257 static LRESULT CALLBACK
4258_OnSettingChange(UINT n)
4259{
4260 if (n == SPI_SETWHEELSCROLLLINES)
4261 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4262 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004263 if (n == SPI_SETNONCLIENTMETRICS)
4264 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004265 return 0;
4266}
4267
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#ifdef FEAT_NETBEANS_INTG
4269 static void
4270_OnWindowPosChanged(
4271 HWND hwnd,
4272 const LPWINDOWPOS lpwpos)
4273{
4274 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004275 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4278 || lpwpos->cx != cx || lpwpos->cy != cy))
4279 {
4280 x = lpwpos->x;
4281 y = lpwpos->y;
4282 cx = lpwpos->cx;
4283 cy = lpwpos->cy;
4284 netbeans_frame_moved(x, y);
4285 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004286 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004287 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288}
4289#endif
4290
K.Takata4f2417f2021-06-05 16:25:32 +02004291
4292static HWND hwndTip = NULL;
4293
4294 static void
4295show_sizing_tip(int cols, int rows)
4296{
4297 TOOLINFOA ti = {sizeof(ti)};
4298 char buf[32];
4299
4300 ti.hwnd = s_hwnd;
4301 ti.uId = (UINT_PTR)s_hwnd;
4302 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4303 ti.lpszText = buf;
4304 sprintf(buf, "%dx%d", cols, rows);
4305 if (hwndTip == NULL)
4306 {
4307 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4308 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4309 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4310 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4311 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4312 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4313 }
4314 else
4315 {
4316 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4317 }
4318 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4319}
4320
4321 static void
4322destroy_sizing_tip(void)
4323{
4324 if (hwndTip != NULL)
4325 {
4326 DestroyWindow(hwndTip);
4327 hwndTip = NULL;
4328 }
4329}
4330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 static int
4332_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 UINT fwSide,
4334 LPRECT lprc)
4335{
4336 int w, h;
4337 int valid_w, valid_h;
4338 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004339 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341 w = lprc->right - lprc->left;
4342 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004343 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 w_offset = w - valid_w;
4345 h_offset = h - valid_h;
4346
4347 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4348 || fwSide == WMSZ_BOTTOMLEFT)
4349 lprc->left += w_offset;
4350 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4351 || fwSide == WMSZ_BOTTOMRIGHT)
4352 lprc->right -= w_offset;
4353
4354 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4355 || fwSide == WMSZ_TOPRIGHT)
4356 lprc->top += h_offset;
4357 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4358 || fwSide == WMSZ_BOTTOMRIGHT)
4359 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004360
4361 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 return TRUE;
4363}
4364
K.Takata92000e22022-01-20 15:10:57 +00004365#ifdef FEAT_GUI_TABLINE
4366 static void
4367_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4368{
4369 if (gui_mch_showing_tabline())
4370 {
4371 POINT pt;
4372 RECT rect;
4373
4374 /*
4375 * If the cursor is on the tabline, display the tab menu
4376 */
4377 GetCursorPos(&pt);
4378 GetWindowRect(s_textArea, &rect);
4379 if (pt.y < rect.top)
4380 {
4381 show_tabline_popup_menu();
4382 return;
4383 }
4384 }
4385 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4386}
4387
4388 static void
4389_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4390{
4391 /*
4392 * If the user double clicked the tabline, create a new tab
4393 */
4394 if (gui_mch_showing_tabline())
4395 {
4396 POINT pt;
4397 RECT rect;
4398
4399 GetCursorPos(&pt);
4400 GetWindowRect(s_textArea, &rect);
4401 if (pt.y < rect.top)
4402 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4403 }
4404 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4405}
4406#endif
4407
4408 static UINT
4409_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4410{
4411 UINT result;
4412 int x, y;
4413
4414 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4415 if (result != HTCLIENT)
4416 return result;
4417
4418#ifdef FEAT_GUI_TABLINE
4419 if (gui_mch_showing_tabline())
4420 {
4421 RECT rct;
4422
4423 // If the cursor is on the GUI tabline, don't process this event
4424 GetWindowRect(s_textArea, &rct);
4425 if (yPos < rct.top)
4426 return result;
4427 }
4428#endif
4429 (void)gui_mch_get_winpos(&x, &y);
4430 xPos -= x;
4431
4432 if (xPos < 48) // <VN> TODO should use system metric?
4433 return HTBOTTOMLEFT;
4434 else
4435 return HTBOTTOMRIGHT;
4436}
4437
4438#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4439 static LRESULT
4440_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4441{
4442 switch (hdr->code)
4443 {
4444 case TTN_GETDISPINFOW:
4445 case TTN_GETDISPINFO:
4446 {
4447 char_u *str = NULL;
4448 static void *tt_text = NULL;
4449
4450 VIM_CLEAR(tt_text);
4451
4452# ifdef FEAT_GUI_TABLINE
4453 if (gui_mch_showing_tabline()
4454 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4455 {
4456 POINT pt;
4457 /*
4458 * Mouse is over the GUI tabline. Display the
4459 * tooltip for the tab under the cursor
4460 *
4461 * Get the cursor position within the tab control
4462 */
4463 GetCursorPos(&pt);
4464 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4465 {
4466 TCHITTESTINFO htinfo;
4467 int idx;
4468
4469 /*
4470 * Get the tab under the cursor
4471 */
4472 htinfo.pt.x = pt.x;
4473 htinfo.pt.y = pt.y;
4474 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4475 if (idx != -1)
4476 {
4477 tabpage_T *tp;
4478
4479 tp = find_tabpage(idx + 1);
4480 if (tp != NULL)
4481 {
4482 get_tabline_label(tp, TRUE);
4483 str = NameBuff;
4484 }
4485 }
4486 }
4487 }
4488# endif
4489# ifdef FEAT_TOOLBAR
4490# ifdef FEAT_GUI_TABLINE
4491 else
4492# endif
4493 {
4494 UINT idButton;
4495 vimmenu_T *pMenu;
4496
4497 idButton = (UINT) hdr->idFrom;
4498 pMenu = gui_mswin_find_menu(root_menu, idButton);
4499 if (pMenu)
4500 str = pMenu->strings[MENU_INDEX_TIP];
4501 }
4502# endif
4503 if (str == NULL)
4504 break;
4505
4506 // Set the maximum width, this also enables using \n for
4507 // line break.
4508 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4509
4510 if (hdr->code == TTN_GETDISPINFOW)
4511 {
4512 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4513
4514 tt_text = enc_to_utf16(str, NULL);
4515 lpdi->lpszText = tt_text;
4516 // can't show tooltip if failed
4517 }
4518 else
4519 {
4520 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4521
4522 if (STRLEN(str) < sizeof(lpdi->szText)
4523 || ((tt_text = vim_strsave(str)) == NULL))
4524 vim_strncpy((char_u *)lpdi->szText, str,
4525 sizeof(lpdi->szText) - 1);
4526 else
4527 lpdi->lpszText = tt_text;
4528 }
4529 }
4530 break;
4531
4532# ifdef FEAT_GUI_TABLINE
4533 case TCN_SELCHANGE:
4534 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4535 {
4536 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4537 return 0L;
4538 }
4539 break;
4540
4541 case NM_RCLICK:
4542 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4543 {
4544 show_tabline_popup_menu();
4545 return 0L;
4546 }
4547 break;
4548# endif
4549
4550 default:
4551 break;
4552 }
4553 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4554}
4555#endif
4556
4557#if defined(MENUHINTS) && defined(FEAT_MENU)
4558 static LRESULT
4559_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4560{
4561 if (((UINT) HIWORD(wParam)
4562 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4563 == MF_HILITE
4564 && (State & CMDLINE) == 0)
4565 {
4566 UINT idButton;
4567 vimmenu_T *pMenu;
4568 static int did_menu_tip = FALSE;
4569
4570 if (did_menu_tip)
4571 {
4572 msg_clr_cmdline();
4573 setcursor();
4574 out_flush();
4575 did_menu_tip = FALSE;
4576 }
4577
4578 idButton = (UINT)LOWORD(wParam);
4579 pMenu = gui_mswin_find_menu(root_menu, idButton);
4580 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4581 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4582 {
4583 ++msg_hist_off;
4584 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4585 --msg_hist_off;
4586 setcursor();
4587 out_flush();
4588 did_menu_tip = TRUE;
4589 }
4590 return 0L;
4591 }
4592 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4593}
4594#endif
4595
K.Takatac81e9bf2022-01-16 14:15:49 +00004596 static LRESULT
4597_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4598{
4599 s_dpi = ydpi;
4600 s_in_dpichanged = TRUE;
4601 //TRACE("DPI: %d", ydpi);
4602
4603 update_scrollbar_size();
4604 update_toolbar_size();
4605 set_tabline_font();
4606
4607 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4608 gui_get_wide_font();
4609 gui_mswin_get_menu_height(FALSE);
4610#ifdef FEAT_MBYTE_IME
4611 im_set_position(gui.row, gui.col);
4612#endif
4613 InvalidateRect(hwnd, NULL, TRUE);
4614
4615 s_in_dpichanged = FALSE;
4616 return 0L;
4617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619
4620 static LRESULT CALLBACK
4621_WndProc(
4622 HWND hwnd,
4623 UINT uMsg,
4624 WPARAM wParam,
4625 LPARAM lParam)
4626{
LemonBoy77fc0b02022-04-22 22:45:52 +01004627 // TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4628 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630 HandleMouseHide(uMsg, lParam);
4631
4632 s_uMsg = uMsg;
4633 s_wParam = wParam;
4634 s_lParam = lParam;
4635
4636 switch (uMsg)
4637 {
4638 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4639 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004640 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004642 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4644 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4645 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4646 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4647#ifdef FEAT_MENU
4648 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4649#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004650 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4651 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4653 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004654 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4655 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4657 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4658 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4659#ifdef FEAT_NETBEANS_INTG
4660 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4661#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004662#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004663 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4664 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004665#endif
K.Takata92000e22022-01-20 15:10:57 +00004666 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004667
Bram Moolenaar734a8672019-12-02 22:49:38 +01004668 case WM_QUERYENDSESSION: // System wants to go down.
4669 gui_shell_closed(); // Will exit when no changed buffers.
4670 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004673 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004676 return 0L;
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 break;
4679
4680 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004681 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4682 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return 0L;
4685
4686 case WM_SYSCHAR:
4687 /*
4688 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4689 * shortcut key, handle like a typed ALT key, otherwise call Windows
4690 * ALT key handling.
4691 */
4692#ifdef FEAT_MENU
4693 if ( !gui.menu_is_active
4694 || p_wak[0] == 'n'
4695 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4696 )
4697#endif
4698 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004699 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return 0L;
4701 }
4702#ifdef FEAT_MENU
4703 else
K.Takata4ac893f2022-01-20 12:44:28 +00004704 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#endif
4706
4707 case WM_SYSKEYUP:
4708#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004709 // This used to be done only when menu is active: ALT key is used for
4710 // that. But that caused problems when menu is disabled and using
4711 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4712 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004713 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004715 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716#endif
4717
K.Takata4f2417f2021-06-05 16:25:32 +02004718 case WM_EXITSIZEMOVE:
4719 destroy_sizing_tip();
4720 break;
4721
Bram Moolenaar734a8672019-12-02 22:49:38 +01004722 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 case WM_MOUSEWHEEL:
4726 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004727 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
Bram Moolenaar734a8672019-12-02 22:49:38 +01004729 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004730 case WM_SETTINGCHANGE:
4731 return _OnSettingChange((UINT)wParam);
4732
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004733#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004735 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#endif
K.Takata92000e22022-01-20 15:10:57 +00004737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#if defined(MENUHINTS) && defined(FEAT_MENU)
4739 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004740 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
4743#ifdef FEAT_MBYTE_IME
4744 case WM_IME_NOTIFY:
4745 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004746 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004747 return 1L;
4748
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 case WM_IME_COMPOSITION:
4750 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004751 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004752 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004754 case WM_DPICHANGED:
4755 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4756 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757
4758 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004760 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#endif
K.Takata92000e22022-01-20 15:10:57 +00004763 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765
K.Takata4ac893f2022-01-20 12:44:28 +00004766 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767}
4768
4769/*
4770 * End of call-back routines
4771 */
4772
Bram Moolenaar734a8672019-12-02 22:49:38 +01004773// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774HWND vim_parent_hwnd = NULL;
4775
4776 static BOOL CALLBACK
4777FindWindowTitle(HWND hwnd, LPARAM lParam)
4778{
4779 char buf[2048];
4780 char *title = (char *)lParam;
4781
4782 if (GetWindowText(hwnd, buf, sizeof(buf)))
4783 {
4784 if (strstr(buf, title) != NULL)
4785 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004786 // Found it. Store the window ref. and quit searching if MDI
4787 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004789 if (vim_parent_hwnd != NULL)
4790 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004793 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794}
4795
4796/*
4797 * Invoked for '-P "title"' argument: search for parent application to open
4798 * our window in.
4799 */
4800 void
4801gui_mch_set_parent(char *title)
4802{
4803 EnumWindows(FindWindowTitle, (LPARAM)title);
4804 if (vim_parent_hwnd == NULL)
4805 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004806 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 mch_exit(2);
4808 }
4809}
4810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 static void
4813ole_error(char *arg)
4814{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004815 char buf[IOSIZE];
4816
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004817# ifdef VIMDLL
4818 gui.in_use = mch_is_gui_executable();
4819# endif
4820
Bram Moolenaar734a8672019-12-02 22:49:38 +01004821 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004822 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004823 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004824 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004828#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4829 static char *
4830gvim_error(void)
4831{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004832 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004833
4834 if (starting)
4835 {
4836 mch_errmsg(msg);
4837 mch_errmsg("\n");
4838 mch_exit(2);
4839 }
4840 return msg;
4841}
4842
4843 char *
4844gui_mch_do_spawn(char_u *arg)
4845{
4846 int len;
4847# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4848 char_u *session = NULL;
4849 LPWSTR tofree1 = NULL;
4850# endif
4851 WCHAR name[MAX_PATH];
4852 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4853 STARTUPINFOW si = {sizeof(si)};
4854 PROCESS_INFORMATION pi;
4855
4856 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4857 goto error;
4858 p = wcsrchr(name, L'\\');
4859 if (p == NULL)
4860 goto error;
4861 // Replace the executable name from vim(d).exe to gvim(d).exe.
4862# ifdef DEBUG
4863 wcscpy(p + 1, L"gvimd.exe");
4864# else
4865 wcscpy(p + 1, L"gvim.exe");
4866# endif
4867
4868# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4869 if (starting)
4870# endif
4871 {
4872 // Pass the command line to the new process.
4873 p = GetCommandLineW();
4874 // Skip 1st argument.
4875 while (*p && *p != L' ' && *p != L'\t')
4876 {
4877 if (*p == L'"')
4878 {
4879 while (*p && *p != L'"')
4880 ++p;
4881 if (*p)
4882 ++p;
4883 }
4884 else
4885 ++p;
4886 }
4887 cmd = p;
4888 }
4889# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4890 else
4891 {
4892 // Create a session file and pass it to the new process.
4893 LPWSTR wsession;
4894 char_u *savebg;
4895 int ret;
4896
4897 session = vim_tempname('s', FALSE);
4898 if (session == NULL)
4899 goto error;
4900 savebg = p_bg;
4901 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4902 ret = write_session_file(session);
4903 vim_free(p_bg);
4904 p_bg = savebg;
4905 if (!ret)
4906 goto error;
4907 wsession = enc_to_utf16(session, NULL);
4908 if (wsession == NULL)
4909 goto error;
4910 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004911 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004912 if (cmd == NULL)
4913 {
4914 vim_free(wsession);
4915 goto error;
4916 }
4917 tofree1 = cmd;
4918 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4919 wsession, wsession);
4920 vim_free(wsession);
4921 }
4922# endif
4923
4924 // Check additional arguments to the `:gui` command.
4925 if (arg != NULL)
4926 {
4927 warg = enc_to_utf16(arg, NULL);
4928 if (warg == NULL)
4929 goto error;
4930 tofree2 = warg;
4931 }
4932 else
4933 warg = L"";
4934
4935 // Set up the new command line.
4936 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004937 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004938 if (newcmd == NULL)
4939 goto error;
4940 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4941
4942 // Spawn a new GUI process.
4943 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4944 NULL, NULL, &si, &pi))
4945 goto error;
4946 CloseHandle(pi.hProcess);
4947 CloseHandle(pi.hThread);
4948 mch_exit(0);
4949
4950error:
4951# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4952 if (session)
4953 mch_remove(session);
4954 vim_free(session);
4955 vim_free(tofree1);
4956# endif
4957 vim_free(newcmd);
4958 vim_free(tofree2);
4959 return gvim_error();
4960}
4961#endif
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * Parse the GUI related command-line arguments. Any arguments used are
4965 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02004966 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 */
4968 void
4969gui_mch_prepare(int *argc, char **argv)
4970{
4971 int silent = FALSE;
4972 int idx;
4973
Bram Moolenaar734a8672019-12-02 22:49:38 +01004974 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4976 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004977 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4979 && (argv[2][0] == '-' || argv[2][0] == '/'))
4980 {
4981 silent = TRUE;
4982 idx = 2;
4983 }
4984 else
4985 idx = 1;
4986
Bram Moolenaar734a8672019-12-02 22:49:38 +01004987 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (STRICMP(argv[idx] + 1, "register") == 0)
4989 {
4990#ifdef FEAT_OLE
4991 RegisterMe(silent);
4992 mch_exit(0);
4993#else
4994 if (!silent)
4995 ole_error("register");
4996 mch_exit(2);
4997#endif
4998 }
4999
Bram Moolenaar734a8672019-12-02 22:49:38 +01005000 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5002 {
5003#ifdef FEAT_OLE
5004 UnregisterMe(!silent);
5005 mch_exit(0);
5006#else
5007 if (!silent)
5008 ole_error("unregister");
5009 mch_exit(2);
5010#endif
5011 }
5012
Bram Moolenaar734a8672019-12-02 22:49:38 +01005013 // Ignore an -embedding argument. It is only relevant if the
5014 // application wants to treat the case when it is started manually
5015 // differently from the case where it is started via automation (and
5016 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5018 {
5019#ifdef FEAT_OLE
5020 *argc = 1;
5021#else
5022 ole_error("embedding");
5023 mch_exit(2);
5024#endif
5025 }
5026 }
5027
5028#ifdef FEAT_OLE
5029 {
5030 int bDoRestart = FALSE;
5031
5032 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005033 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (bDoRestart)
5035 mch_exit(0);
5036 }
5037#endif
5038
5039#ifdef FEAT_NETBEANS_INTG
5040 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005041 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 int arg;
5043
5044 for (arg = 1; arg < *argc; arg++)
5045 if (strncmp("-nb", argv[arg], 3) == 0)
5046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 netbeansArg = argv[arg];
5048 mch_memmove(&argv[arg], &argv[arg + 1],
5049 (--*argc - arg) * sizeof(char *));
5050 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005051 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056
K.Takatac81e9bf2022-01-16 14:15:49 +00005057 static void
5058load_dpi_func(void)
5059{
5060 HMODULE hUser32;
5061
5062 hUser32 = GetModuleHandle("user32.dll");
5063 if (hUser32 == NULL)
5064 goto fail;
5065
5066 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5067 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5068 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5069 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5070 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5071 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5072
5073 if (pSetThreadDpiAwarenessContext != NULL)
5074 {
5075 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5076 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5077 if (oldctx != NULL)
5078 {
5079 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5080 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5081#ifdef DEBUG
5082 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5083 {
5084 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5085 }
5086#endif
5087 return;
5088 }
5089 }
5090
5091fail:
5092 // Disable PerMonitorV2 APIs.
5093 pGetDpiForSystem = stubGetDpiForSystem;
5094 pGetDpiForWindow = NULL;
5095 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5096 pSetThreadDpiAwarenessContext = NULL;
5097 pGetAwarenessFromDpiAwarenessContext = NULL;
5098}
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
5101 * Initialise the GUI. Create all the windows, set up all the call-backs
5102 * etc.
5103 */
5104 int
5105gui_mch_init(void)
5106{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005108 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar734a8672019-12-02 22:49:38 +01005111 // Return here if the window was already opened (happens when
5112 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005114 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 /*
5117 * Load the tearoff bitmap
5118 */
5119#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005120 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#endif
5122
K.Takatac81e9bf2022-01-16 14:15:49 +00005123 load_dpi_func();
5124
5125 s_dpi = pGetDpiForSystem();
5126 update_scrollbar_size();
5127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005129 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130#endif
5131 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005132#ifdef FEAT_TOOLBAR
5133 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5137
Bram Moolenaar734a8672019-12-02 22:49:38 +01005138 // First try using the wide version, so that we can use any title.
5139 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005140 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005142 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 wndclassw.lpfnWndProc = _WndProc;
5144 wndclassw.cbClsExtra = 0;
5145 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005146 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5148 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5149 wndclassw.hbrBackground = s_brush;
5150 wndclassw.lpszMenuName = NULL;
5151 wndclassw.lpszClassName = szVimWndClassW;
5152
K.Takata4ac893f2022-01-20 12:44:28 +00005153 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005154 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (vim_parent_hwnd != NULL)
5158 {
5159#ifdef HAVE_TRY_EXCEPT
5160 __try
5161 {
5162#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005163 // Open inside the specified parent window.
5164 // TODO: last argument should point to a CLIENTCREATESTRUCT
5165 // structure.
5166 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005168 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005169 WS_OVERLAPPEDWINDOW | WS_CHILD
5170 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5172 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005173 100, // Any value will do
5174 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005176 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef HAVE_TRY_EXCEPT
5178 }
5179 __except(EXCEPTION_EXECUTE_HANDLER)
5180 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005181 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183#endif
5184 if (s_hwnd == NULL)
5185 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005186 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 mch_exit(2);
5188 }
5189 }
5190 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005191 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005192 // If the provided windowid is not valid reset it to zero, so that it
5193 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005194 if (IsWindow((HWND)win_socket_id) <= 0)
5195 win_socket_id = 0;
5196
Bram Moolenaar734a8672019-12-02 22:49:38 +01005197 // Create a window. If win_socket_id is not zero without border and
5198 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005199 s_hwnd = CreateWindowW(
5200 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005201 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5202 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005203 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5204 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005205 100, // Any value will do
5206 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005207 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005208 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005209 if (s_hwnd != NULL && win_socket_id != 0)
5210 {
5211 SetParent(s_hwnd, (HWND)win_socket_id);
5212 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5213 }
5214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
5216 if (s_hwnd == NULL)
5217 return FAIL;
5218
K.Takatac81e9bf2022-01-16 14:15:49 +00005219 if (pGetDpiForWindow != NULL)
5220 {
5221 s_dpi = pGetDpiForWindow(s_hwnd);
5222 update_scrollbar_size();
5223 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5224 }
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5227 dyn_imm_load();
5228#endif
5229
Bram Moolenaar734a8672019-12-02 22:49:38 +01005230 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005231 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005232 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005233 wndclassw.style = CS_OWNDC;
5234 wndclassw.lpfnWndProc = _TextAreaWndProc;
5235 wndclassw.cbClsExtra = 0;
5236 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005237 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005238 wndclassw.hIcon = NULL;
5239 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5240 wndclassw.hbrBackground = NULL;
5241 wndclassw.lpszMenuName = NULL;
5242 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005243
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005244 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return FAIL;
5246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005248 s_textArea = CreateWindowExW(
5249 0,
5250 szTextAreaClassW, L"Vim text area",
5251 WS_CHILD | WS_VISIBLE, 0, 0,
5252 100, // Any value will do for now
5253 100, // Any value will do for now
5254 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005255 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 if (s_textArea == NULL)
5258 return FAIL;
5259
Bram Moolenaar20321902016-02-17 12:30:17 +01005260#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005261 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005262 {
5263 HANDLE hIcon = NULL;
5264
5265 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005266 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005267 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005268#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#ifdef FEAT_MENU
5271 s_menuBar = CreateMenu();
5272#endif
5273 s_hdc = GetDC(s_textArea);
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
Bram Moolenaar734a8672019-12-02 22:49:38 +01005277 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005278 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaar734a8672019-12-02 22:49:38 +01005280 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 gui_mch_def_colors();
5282
Bram Moolenaar734a8672019-12-02 22:49:38 +01005283 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5284 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 set_normal_colors();
5286
5287 /*
5288 * Check that none of the colors are the same as the background color.
5289 * Then store the current values as the defaults.
5290 */
5291 gui_check_colors();
5292 gui.def_norm_pixel = gui.norm_pixel;
5293 gui.def_back_pixel = gui.back_pixel;
5294
Bram Moolenaar734a8672019-12-02 22:49:38 +01005295 // Get the colors for the highlight groups (gui_check_colors() might have
5296 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 highlight_gui_started();
5298
5299 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005300 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005302 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 /*
5305 * Set up for Intellimouse processing
5306 */
5307 init_mouse_wheel();
5308
5309 /*
5310 * compute a couple of metrics used for the dialogs
5311 */
5312 get_dialog_font_metrics();
5313#ifdef FEAT_TOOLBAR
5314 /*
5315 * Create the toolbar
5316 */
5317 initialise_toolbar();
5318#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005319#ifdef FEAT_GUI_TABLINE
5320 /*
5321 * Create the tabline
5322 */
5323 initialise_tabline();
5324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#ifdef MSWIN_FIND_REPLACE
5326 /*
5327 * Initialise the dialog box stuff
5328 */
5329 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5330
Bram Moolenaar734a8672019-12-02 22:49:38 +01005331 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005333 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005335 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5337 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5338 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005341#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005342 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005343 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005344#endif
5345
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005346#ifdef FEAT_RENDER_OPTIONS
5347 if (p_rop)
5348 (void)gui_mch_set_rendering_options(p_rop);
5349#endif
5350
Bram Moolenaar748bf032005-02-02 23:04:36 +00005351theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005352 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005353 display_errors();
5354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return OK;
5356}
5357
5358/*
5359 * Get the size of the screen, taking position on multiple monitors into
5360 * account (if supported).
5361 */
5362 static void
5363get_work_area(RECT *spi_rect)
5364{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005365 HMONITOR mon;
5366 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
Bram Moolenaar734a8672019-12-02 22:49:38 +01005368 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005369 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005370 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005372 moninfo.cbSize = sizeof(MONITORINFO);
5373 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005375 *spi_rect = moninfo.rcWork;
5376 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005379 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5381}
5382
5383/*
5384 * Set the size of the window to the given width and height in pixels.
5385 */
5386 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005387gui_mch_set_shellsize(
5388 int width,
5389 int height,
5390 int min_width UNUSED,
5391 int min_height UNUSED,
5392 int base_width UNUSED,
5393 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005394 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005397 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // Try to keep window completely on screen.
5401 // Get position of the screen work area. This is the part that is not
5402 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 get_work_area(&workarea_rect);
5404
Bram Moolenaar734a8672019-12-02 22:49:38 +01005405 // Resizing a maximized window looks very strange, unzoom it first.
5406 // But don't do it when still starting up, it may have been requested in
5407 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005408 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005410
5411 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
Bram Moolenaar734a8672019-12-02 22:49:38 +01005413 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005414 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5415 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5416 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5417 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5418 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5419 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar734a8672019-12-02 22:49:38 +01005421 // The following should take care of keeping Vim on the same monitor, no
5422 // matter if the secondary monitor is left or right of the primary
5423 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005424 window_rect.right = window_rect.left + win_width;
5425 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005426
Bram Moolenaar734a8672019-12-02 22:49:38 +01005427 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005428 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5429 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005431 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5432 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005434 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5435 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005437 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5438 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005440 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5441 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443 SetActiveWindow(s_hwnd);
5444 SetFocus(s_hwnd);
5445
Bram Moolenaar734a8672019-12-02 22:49:38 +01005446 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448}
5449
5450
5451 void
5452gui_mch_set_scrollbar_thumb(
5453 scrollbar_T *sb,
5454 long val,
5455 long size,
5456 long max)
5457{
5458 SCROLLINFO info;
5459
5460 sb->scroll_shift = 0;
5461 while (max > 32767)
5462 {
5463 max = (max + 1) >> 1;
5464 val >>= 1;
5465 size >>= 1;
5466 ++sb->scroll_shift;
5467 }
5468
5469 if (sb->scroll_shift > 0)
5470 ++size;
5471
5472 info.cbSize = sizeof(info);
5473 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5474 info.nPos = val;
5475 info.nMin = 0;
5476 info.nMax = max;
5477 info.nPage = size;
5478 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5479}
5480
5481
5482/*
5483 * Set the current text font.
5484 */
5485 void
5486gui_mch_set_font(GuiFont font)
5487{
5488 gui.currFont = font;
5489}
5490
5491
5492/*
5493 * Set the current text foreground color.
5494 */
5495 void
5496gui_mch_set_fg_color(guicolor_T color)
5497{
5498 gui.currFgColor = color;
5499}
5500
5501/*
5502 * Set the current text background color.
5503 */
5504 void
5505gui_mch_set_bg_color(guicolor_T color)
5506{
5507 gui.currBgColor = color;
5508}
5509
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005510/*
5511 * Set the current text special color.
5512 */
5513 void
5514gui_mch_set_sp_color(guicolor_T color)
5515{
5516 gui.currSpColor = color;
5517}
5518
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005519#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520/*
5521 * Multi-byte handling, originally by Sung-Hoon Baek.
5522 * First static functions (no prototypes generated).
5523 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005524# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005525# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005526# endif
5527# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 * handle WM_IME_NOTIFY message
5531 */
5532 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005533_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 LRESULT lResult = 0;
5536 HIMC hImc;
5537
5538 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5539 return lResult;
5540 switch (dwCommand)
5541 {
5542 case IMN_SETOPENSTATUS:
5543 if (pImmGetOpenStatus(hImc))
5544 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005545 LOGFONTW lf = norm_logfont;
5546 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5547 // Work around when PerMonitorV2 is not enabled in the process level.
5548 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5549 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 im_set_position(gui.row, gui.col);
5551
Bram Moolenaar734a8672019-12-02 22:49:38 +01005552 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 State &= ~LANGMAP;
5554 if (State & INSERT)
5555 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005556# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005557 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5559 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005560 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 int old_row = gui.row;
5562 int old_col = gui.col;
5563
5564 // This must be called here before
5565 // status_redraw_curbuf(), otherwise the mode
5566 // message may appear in the wrong position.
5567 showmode();
5568 status_redraw_curbuf();
5569 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005570 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 gui.row = old_row;
5572 gui.col = old_col;
5573 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 }
5577 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005578 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 lResult = 0;
5580 break;
5581 }
5582 pImmReleaseContext(hWnd, hImc);
5583 return lResult;
5584}
5585
5586 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005587_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 char_u *ret;
5590 int len;
5591
Bram Moolenaar734a8672019-12-02 22:49:38 +01005592 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 return 0;
5594
5595 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5596 if (ret != NULL)
5597 {
5598 add_to_input_buf_csi(ret, len);
5599 vim_free(ret);
5600 return 1;
5601 }
5602 return 0;
5603}
5604
5605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 * void GetResultStr()
5607 *
5608 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5609 * get complete composition string
5610 */
5611 static char_u *
5612GetResultStr(HWND hwnd, int GCS, int *lenp)
5613{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005614 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005615 LONG ret;
5616 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 char_u *convbuf = NULL;
5618
5619 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5620 return NULL;
5621
K.Takatab0b2b732022-01-19 12:59:21 +00005622 // Get the length of the composition string.
5623 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5624 if (ret <= 0)
5625 return NULL;
5626
5627 // Allocate the requested buffer plus space for the NUL character.
5628 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (buf == NULL)
5630 return NULL;
5631
K.Takatab0b2b732022-01-19 12:59:21 +00005632 // Reads in the composition string.
5633 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5634 *lenp = ret / sizeof(WCHAR);
5635
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005636 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 pImmReleaseContext(hwnd, hIMC);
5638 vim_free(buf);
5639 return convbuf;
5640}
5641#endif
5642
Bram Moolenaar734a8672019-12-02 22:49:38 +01005643// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005644#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
5646/*
5647 * set font to IM.
5648 */
5649 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005650im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 HIMC hImc;
5653
5654 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5655 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005656 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 pImmReleaseContext(s_hwnd, hImc);
5658 }
5659}
5660
5661/*
5662 * Notify cursor position to IM.
5663 */
5664 void
5665im_set_position(int row, int col)
5666{
5667 HIMC hImc;
5668
5669 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5670 {
5671 COMPOSITIONFORM cfs;
5672
5673 cfs.dwStyle = CFS_POINT;
5674 cfs.ptCurrentPos.x = FILL_X(col);
5675 cfs.ptCurrentPos.y = FILL_Y(row);
5676 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005677 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5678 {
5679 // Work around when PerMonitorV2 is not enabled in the process level.
5680 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5681 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 pImmSetCompositionWindow(hImc, &cfs);
5684
5685 pImmReleaseContext(s_hwnd, hImc);
5686 }
5687}
5688
5689/*
5690 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5691 */
5692 void
5693im_set_active(int active)
5694{
5695 HIMC hImc;
5696 static HIMC hImcOld = (HIMC)0;
5697
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005698# ifdef VIMDLL
5699 if (!gui.in_use && !gui.starting)
5700 {
5701 mbyte_im_set_active(active);
5702 return;
5703 }
5704# endif
5705
Bram Moolenaar734a8672019-12-02 22:49:38 +01005706 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 if (p_imdisable)
5709 {
5710 if (hImcOld == (HIMC)0)
5711 {
5712 hImcOld = pImmGetContext(s_hwnd);
5713 if (hImcOld)
5714 pImmAssociateContext(s_hwnd, (HIMC)0);
5715 }
5716 active = FALSE;
5717 }
5718 else if (hImcOld != (HIMC)0)
5719 {
5720 pImmAssociateContext(s_hwnd, hImcOld);
5721 hImcOld = (HIMC)0;
5722 }
5723
5724 hImc = pImmGetContext(s_hwnd);
5725 if (hImc)
5726 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005727 /*
5728 * for Korean ime
5729 */
5730 HKL hKL = GetKeyboardLayout(0);
5731
5732 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5733 {
5734 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5735 static BOOL bSaved = FALSE;
5736
5737 if (active)
5738 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005739 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005740 if (bSaved)
5741 pImmSetConversionStatus(hImc, dwConversionSaved,
5742 dwSentenceSaved);
5743 bSaved = FALSE;
5744 }
5745 else
5746 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005747 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005748 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5749 &dwSentenceSaved))
5750 {
5751 bSaved = TRUE;
5752 pImmSetConversionStatus(hImc,
5753 dwConversionSaved & ~(IME_CMODE_NATIVE
5754 | IME_CMODE_FULLSHAPE),
5755 dwSentenceSaved);
5756 }
5757 }
5758 }
5759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 pImmSetOpenStatus(hImc, active);
5761 pImmReleaseContext(s_hwnd, hImc);
5762 }
5763 }
5764}
5765
5766/*
5767 * Get IM status. When IM is on, return not 0. Else return 0.
5768 */
5769 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005770im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 int status = 0;
5773 HIMC hImc;
5774
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005775# ifdef VIMDLL
5776 if (!gui.in_use && !gui.starting)
5777 return mbyte_im_get_status();
5778# endif
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5781 {
5782 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5783 pImmReleaseContext(s_hwnd, hImc);
5784 }
5785 return status;
5786}
5787
Bram Moolenaar734a8672019-12-02 22:49:38 +01005788#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005791/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005792 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005793 */
5794 static void
5795latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5796{
5797 int c;
5798
Bram Moolenaarca003e12006-03-17 23:19:38 +00005799 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005800 {
5801 c = *text++;
5802 switch (c)
5803 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005804 case 0xa4: c = 0x20ac; break; // euro
5805 case 0xa6: c = 0x0160; break; // S hat
5806 case 0xa8: c = 0x0161; break; // S -hat
5807 case 0xb4: c = 0x017d; break; // Z hat
5808 case 0xb8: c = 0x017e; break; // Z -hat
5809 case 0xbc: c = 0x0152; break; // OE
5810 case 0xbd: c = 0x0153; break; // oe
5811 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005812 }
5813 *unicodebuf++ = c;
5814 }
5815}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
5817#ifdef FEAT_RIGHTLEFT
5818/*
5819 * What is this for? In the case where you are using Win98 or Win2K or later,
5820 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5821 * reverses the string sent to the TextOut... family. This sucks, because we
5822 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5823 * way to tell Windblows not to do this!
5824 *
5825 * The short of it is that this 'RevOut' only gets called if you are running
5826 * one of the new, "improved" MS OSes, and only if you are running in
5827 * 'rightleft' mode. It makes display take *slightly* longer, but not
5828 * noticeably so.
5829 */
5830 static void
K.Takata135e1522022-01-29 15:27:58 +00005831RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 int col,
5833 int row,
5834 UINT foptions,
5835 CONST RECT *pcliprect,
5836 LPCTSTR text,
5837 UINT len,
5838 CONST INT *padding)
5839{
5840 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005842 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005843 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005844 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845}
5846#endif
5847
Bram Moolenaar92467d32017-12-05 13:22:16 +01005848 static void
5849draw_line(
5850 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005851 int y1,
5852 int x2,
5853 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005854 COLORREF color)
5855{
5856#if defined(FEAT_DIRECTX)
5857 if (IS_ENABLE_DIRECTX())
5858 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5859 else
5860#endif
5861 {
5862 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5863 HPEN old_pen = SelectObject(s_hdc, hpen);
5864 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005865 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005866 LineTo(s_hdc, x2, y2);
5867 DeleteObject(SelectObject(s_hdc, old_pen));
5868 }
5869}
5870
5871 static void
5872set_pixel(
5873 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005874 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005875 COLORREF color)
5876{
5877#if defined(FEAT_DIRECTX)
5878 if (IS_ENABLE_DIRECTX())
5879 DWriteContext_SetPixel(s_dwc, x, y, color);
5880 else
5881#endif
5882 SetPixel(s_hdc, x, y, color);
5883}
5884
5885 static void
5886fill_rect(
5887 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005888 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005889 COLORREF color)
5890{
5891#if defined(FEAT_DIRECTX)
5892 if (IS_ENABLE_DIRECTX())
5893 DWriteContext_FillRect(s_dwc, rcp, color);
5894 else
5895#endif
5896 {
5897 HBRUSH hbr2;
5898
5899 if (hbr == NULL)
5900 hbr2 = CreateSolidBrush(color);
5901 else
5902 hbr2 = hbr;
5903 FillRect(s_hdc, rcp, hbr2);
5904 if (hbr == NULL)
5905 DeleteBrush(hbr2);
5906 }
5907}
5908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 void
5910gui_mch_draw_string(
5911 int row,
5912 int col,
5913 char_u *text,
5914 int len,
5915 int flags)
5916{
5917 static int *padding = NULL;
5918 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 const RECT *pcliprect = NULL;
5920 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 static WCHAR *unicodebuf = NULL;
5922 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005923 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 int y;
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 /*
5928 * Italic and bold text seems to have an extra row of pixels at the bottom
5929 * (below where the bottom of the character should be). If we draw the
5930 * characters with a solid background, the top row of pixels in the
5931 * character below will be overwritten. We can fix this by filling in the
5932 * background ourselves, to the correct character proportions, and then
5933 * writing the character in transparent mode. Still have a problem when
5934 * the character is "_", which gets written on to the character below.
5935 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5936 * pixel in their slots, which fixes the problem with the bottom row of
5937 * pixels. We still need this code because otherwise the top row of pixels
5938 * becomes a problem. - webb.
5939 */
5940 static HBRUSH hbr_cache[2] = {NULL, NULL};
5941 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5942 static int brush_lru = 0;
5943 HBRUSH hbr;
5944 RECT rc;
5945
5946 if (!(flags & DRAW_TRANSP))
5947 {
5948 /*
5949 * Clear background first.
5950 * Note: FillRect() excludes right and bottom of rectangle.
5951 */
5952 rc.left = FILL_X(col);
5953 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (has_mbyte)
5955 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005956 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005957 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 }
5959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 rc.right = FILL_X(col + len);
5961 rc.bottom = FILL_Y(row + 1);
5962
Bram Moolenaar734a8672019-12-02 22:49:38 +01005963 // Cache the created brush, that saves a lot of time. We need two:
5964 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (gui.currBgColor == brush_color[0])
5966 {
5967 hbr = hbr_cache[0];
5968 brush_lru = 1;
5969 }
5970 else if (gui.currBgColor == brush_color[1])
5971 {
5972 hbr = hbr_cache[1];
5973 brush_lru = 0;
5974 }
5975 else
5976 {
5977 if (hbr_cache[brush_lru] != NULL)
5978 DeleteBrush(hbr_cache[brush_lru]);
5979 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5980 brush_color[brush_lru] = gui.currBgColor;
5981 hbr = hbr_cache[brush_lru];
5982 brush_lru = !brush_lru;
5983 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005984
Bram Moolenaar92467d32017-12-05 13:22:16 +01005985 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 SetBkMode(s_hdc, TRANSPARENT);
5988
5989 /*
5990 * When drawing block cursor, prevent inverted character spilling
5991 * over character cell (can happen with bold/italic)
5992 */
5993 if (flags & DRAW_CURSOR)
5994 {
5995 pcliprect = &rc;
5996 foptions = ETO_CLIPPED;
5997 }
5998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 SetTextColor(s_hdc, gui.currFgColor);
6000 SelectFont(s_hdc, gui.currFont);
6001
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006002#ifdef FEAT_DIRECTX
6003 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006004 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006005#endif
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6008 {
K.Takata135e1522022-01-29 15:27:58 +00006009 int i;
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 vim_free(padding);
6012 pad_size = Columns;
6013
Bram Moolenaar734a8672019-12-02 22:49:38 +01006014 // Don't give an out-of-memory message here, it would call us
6015 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006016 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 if (padding != NULL)
6018 for (i = 0; i < pad_size; i++)
6019 padding[i] = gui.char_width;
6020 }
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 /*
6023 * We have to provide the padding argument because italic and bold versions
6024 * of fixed-width fonts are often one pixel or so wider than their normal
6025 * versions.
6026 * No check for DRAW_BOLD, Windows will have done it already.
6027 */
6028
Bram Moolenaar734a8672019-12-02 22:49:38 +01006029 // Check if there are any UTF-8 characters. If not, use normal text
6030 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 if (enc_utf8)
6032 for (n = 0; n < len; ++n)
6033 if (text[n] >= 0x80)
6034 break;
6035
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006036#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006037 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6038 // required that unicode drawing routine, currently. So this forces it
6039 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006040 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006041 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006042#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006043
Bram Moolenaar734a8672019-12-02 22:49:38 +01006044 // Check if the Unicode buffer exists and is big enough. Create it
6045 // with the same length as the multi-byte string, the number of wide
6046 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006047 if ((enc_utf8
6048 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6049 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 && (unicodebuf == NULL || len > unibuflen))
6051 {
6052 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006053 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
6055 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006056 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
6058 unibuflen = len;
6059 }
6060
6061 if (enc_utf8 && n < len && unicodebuf != NULL)
6062 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006063 // Output UTF-8 characters. Composing characters should be
6064 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006065 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006066 int wlen; // string length in words
6067 int clen; // string length in characters
6068 int cells; // cell width of string up to composing char
6069 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006070 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006072 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006073 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006075 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006077 c = utf_ptr2char(text + i);
6078 if (c >= 0x10000)
6079 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006080 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006081 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6082 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006083 }
6084 else
6085 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006086 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006087 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006088
6089 if (utf_iscomposing(c))
6090 cw = 0;
6091 else
6092 {
6093 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006094 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006095 cw = 1;
6096 }
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (unicodepdy != NULL)
6099 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006100 // Use unicodepdy to make characters fit as we expect, even
6101 // when the font uses different widths (e.g., bold character
6102 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006103 if (c >= 0x10000)
6104 {
6105 unicodepdy[wlen - 2] = cw * gui.char_width;
6106 unicodepdy[wlen - 1] = 0;
6107 }
6108 else
6109 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 }
6111 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006112 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006113 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006115#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006116 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006117 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006118 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006119 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006120 TEXT_X(col), TEXT_Y(row),
6121 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006122 gui.char_width, gui.currFgColor,
6123 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006124 }
6125 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006126#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006127 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6128 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006129 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006131 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006133 // If we want to display codepage data, and the current CP is not the
6134 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (unicodebuf != NULL)
6136 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006137 if (enc_latin9)
6138 latin9_to_ucs(text, len, unicodebuf);
6139 else
6140 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 MB_PRECOMPOSED,
6142 (char *)text, len,
6143 (LPWSTR)unicodebuf, unibuflen);
6144 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006145 {
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 Moolenaar19a09a12005-03-04 23:39:37 +00006149 if (unicodepdy != NULL)
6150 {
6151 int i;
6152 int cw;
6153
6154 for (i = 0; i < len; ++i)
6155 {
6156 cw = utf_char2cells(unicodebuf[i]);
6157 if (cw > 2)
6158 cw = 1;
6159 unicodepdy[i] = cw * gui.char_width;
6160 }
6161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006163 foptions, pcliprect, unicodebuf, len, unicodepdy);
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 }
6166 }
6167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
6169#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006170 // Windows will mess up RL text, so we have to draw it character by
6171 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006172 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6174 foptions, pcliprect, (char *)text, len, padding);
6175 else
6176#endif
6177 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6178 foptions, pcliprect, (char *)text, len, padding);
6179 }
6180
Bram Moolenaar734a8672019-12-02 22:49:38 +01006181 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (flags & DRAW_UNDERL)
6183 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006184 // When p_linespace is 0, overwrite the bottom row of pixels.
6185 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (p_linespace > 1)
6188 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006189 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006191
Bram Moolenaar734a8672019-12-02 22:49:38 +01006192 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006193 if (flags & DRAW_STRIKE)
6194 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006195 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006196 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006197 }
6198
Bram Moolenaar734a8672019-12-02 22:49:38 +01006199 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006200 if (flags & DRAW_UNDERC)
6201 {
6202 int x;
6203 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006204 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006205
6206 y = FILL_Y(row + 1) - 1;
6207 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6208 {
6209 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006210 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006211 }
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213}
6214
6215
6216/*
6217 * Output routines.
6218 */
6219
Bram Moolenaar734a8672019-12-02 22:49:38 +01006220/*
6221 * Flush any output to the screen
6222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 void
6224gui_mch_flush(void)
6225{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006226#if defined(FEAT_DIRECTX)
6227 if (IS_ENABLE_DIRECTX())
6228 DWriteContext_Flush(s_dwc);
6229#endif
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 GdiFlush();
6232}
6233
6234 static void
6235clear_rect(RECT *rcp)
6236{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006237 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238}
6239
6240
Bram Moolenaarc716c302006-01-21 22:12:51 +00006241 void
6242gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6243{
6244 RECT workarea_rect;
6245
6246 get_work_area(&workarea_rect);
6247
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006248 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006249 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6250 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006251
Bram Moolenaar734a8672019-12-02 22:49:38 +01006252 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6253 // the menubar for MSwin, we subtract it from the screen height, so that
6254 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006255 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006256 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6257 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6258 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6259 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006260}
6261
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263#if defined(FEAT_MENU) || defined(PROTO)
6264/*
6265 * Add a sub menu to the menu bar.
6266 */
6267 void
6268gui_mch_add_menu(
6269 vimmenu_T *menu,
6270 int pos)
6271{
6272 vimmenu_T *parent = menu->parent;
6273
6274 menu->submenu_id = CreatePopupMenu();
6275 menu->id = s_menu_id++;
6276
6277 if (menu_is_menubar(menu->name))
6278 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006279 WCHAR *wn;
6280 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006282 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006283 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006284 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006286 infow.cbSize = sizeof(infow);
6287 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6288 | MIIM_SUBMENU;
6289 infow.dwItemData = (long_u)menu;
6290 infow.wID = menu->id;
6291 infow.fType = MFT_STRING;
6292 infow.dwTypeData = wn;
6293 infow.cch = (UINT)wcslen(wn);
6294 infow.hSubMenu = menu->submenu_id;
6295 InsertMenuItemW((parent == NULL)
6296 ? s_menuBar : parent->submenu_id,
6297 (UINT)pos, TRUE, &infow);
6298 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
6300
Bram Moolenaar734a8672019-12-02 22:49:38 +01006301 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (parent == NULL)
6303 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006304# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 else if (IsWindow(parent->tearoff_handle))
6306 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308}
6309
6310 void
6311gui_mch_show_popupmenu(vimmenu_T *menu)
6312{
6313 POINT mp;
6314
K.Takata45f9cfb2022-01-21 11:11:00 +00006315 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6317}
6318
6319 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006320gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321{
6322 vimmenu_T *menu = gui_find_menu(path_name);
6323
6324 if (menu != NULL)
6325 {
6326 POINT p;
6327
Bram Moolenaar734a8672019-12-02 22:49:38 +01006328 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006330 if (mouse_pos)
6331 {
6332 int mx, my;
6333
6334 gui_mch_getmouse(&mx, &my);
6335 p.x += mx;
6336 p.y += my;
6337 }
6338 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006340 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6342 }
6343 msg_scroll = FALSE;
6344 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6345 }
6346}
6347
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006348# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349/*
6350 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6351 * create it as a pseudo-"tearoff menu".
6352 */
6353 void
6354gui_make_tearoff(char_u *path_name)
6355{
6356 vimmenu_T *menu = gui_find_menu(path_name);
6357
Bram Moolenaar734a8672019-12-02 22:49:38 +01006358 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (menu != NULL)
6360 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6361}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
6364/*
6365 * Add a menu item to a menu
6366 */
6367 void
6368gui_mch_add_menu_item(
6369 vimmenu_T *menu,
6370 int idx)
6371{
6372 vimmenu_T *parent = menu->parent;
6373
6374 menu->id = s_menu_id++;
6375 menu->submenu_id = NULL;
6376
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006377# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6379 {
6380 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6381 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6382 }
6383 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006384# endif
6385# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (menu_is_toolbar(parent->name))
6387 {
6388 TBBUTTON newtb;
6389
Bram Moolenaara80faa82020-04-12 19:37:17 +02006390 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 if (menu_is_separator(menu->name))
6392 {
6393 newtb.iBitmap = 0;
6394 newtb.fsStyle = TBSTYLE_SEP;
6395 }
6396 else
6397 {
6398 newtb.iBitmap = get_toolbar_bitmap(menu);
6399 newtb.fsStyle = TBSTYLE_BUTTON;
6400 }
6401 newtb.idCommand = menu->id;
6402 newtb.fsState = TBSTATE_ENABLED;
6403 newtb.iString = 0;
6404 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6405 (LPARAM)&newtb);
6406 menu->submenu_id = (HMENU)-1;
6407 }
6408 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006411 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006413 wn = enc_to_utf16(menu->name, NULL);
6414 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006416 InsertMenuW(parent->submenu_id, (UINT)idx,
6417 (menu_is_separator(menu->name)
6418 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6419 (UINT)menu->id, wn);
6420 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006422# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (IsWindow(parent->tearoff_handle))
6424 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427}
6428
6429/*
6430 * Destroy the machine specific menu widget.
6431 */
6432 void
6433gui_mch_destroy_menu(vimmenu_T *menu)
6434{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006435# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 /*
6437 * is this a toolbar button?
6438 */
6439 if (menu->submenu_id == (HMENU)-1)
6440 {
6441 int iButton;
6442
6443 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6444 (WPARAM)menu->id, 0);
6445 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6446 }
6447 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
6450 if (menu->parent != NULL
6451 && menu_is_popup(menu->parent->dname)
6452 && menu->parent->submenu_id != NULL)
6453 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6454 else
6455 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6456 if (menu->submenu_id != NULL)
6457 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006458# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (IsWindow(menu->tearoff_handle))
6460 DestroyWindow(menu->tearoff_handle);
6461 if (menu->parent != NULL
6462 && menu->parent->children != NULL
6463 && IsWindow(menu->parent->tearoff_handle))
6464 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006465 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 menu->modes = 0;
6467 rebuild_tearoff(menu->parent);
6468 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
6471}
6472
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006473# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 static void
6475rebuild_tearoff(vimmenu_T *menu)
6476{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006477 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 char_u tbuf[128];
6479 RECT trect;
6480 RECT rct;
6481 RECT roct;
6482 int x, y;
6483
6484 HWND thwnd = menu->tearoff_handle;
6485
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006486 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (GetWindowRect(thwnd, &trect)
6488 && GetWindowRect(s_hwnd, &rct)
6489 && GetClientRect(s_hwnd, &roct))
6490 {
6491 x = trect.left - rct.left;
6492 y = (trect.top - rct.bottom + roct.bottom);
6493 }
6494 else
6495 {
6496 x = y = 0xffffL;
6497 }
6498 DestroyWindow(thwnd);
6499 if (menu->children != NULL)
6500 {
6501 gui_mch_tearoff(tbuf, menu, x, y);
6502 if (IsWindow(menu->tearoff_handle))
6503 (void) SetWindowPos(menu->tearoff_handle,
6504 NULL,
6505 (int)trect.left,
6506 (int)trect.top,
6507 0, 0,
6508 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6509 }
6510}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006511# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512
6513/*
6514 * Make a menu either grey or not grey.
6515 */
6516 void
6517gui_mch_menu_grey(
6518 vimmenu_T *menu,
6519 int grey)
6520{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006521# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 /*
6523 * is this a toolbar button?
6524 */
6525 if (menu->submenu_id == (HMENU)-1)
6526 {
6527 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006528 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
6530 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006531# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006532 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6533 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006535# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6537 {
6538 WORD menuID;
6539 HWND menuHandle;
6540
6541 /*
6542 * A tearoff button has changed state.
6543 */
6544 if (menu->children == NULL)
6545 menuID = (WORD)(menu->id);
6546 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006547 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6549 if (menuHandle)
6550 EnableWindow(menuHandle, !grey);
6551
6552 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554}
6555
Bram Moolenaar734a8672019-12-02 22:49:38 +01006556#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558
Bram Moolenaar734a8672019-12-02 22:49:38 +01006559// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006562#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6565/*
6566 * stuff for dialogs
6567 */
6568
6569/*
6570 * The callback routine used by all the dialogs. Very simple. First,
6571 * acknowledges the INITDIALOG message so that Windows knows to do standard
6572 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6573 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6574 * number.
6575 */
6576 static LRESULT CALLBACK
6577dialog_callback(
6578 HWND hwnd,
6579 UINT message,
6580 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006581 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 if (message == WM_INITDIALOG)
6584 {
6585 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006586 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 (void)SetFocus(hwnd);
6588 if (dialog_default_button > IDCANCEL)
6589 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006590 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006591 // We don't have a default, set focus on another element of the
6592 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006593 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 return FALSE;
6595 }
6596
6597 if (message == WM_COMMAND)
6598 {
6599 int button = LOWORD(wParam);
6600
Bram Moolenaar734a8672019-12-02 22:49:38 +01006601 // Don't end the dialog if something was selected that was
6602 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (button >= DLG_NONBUTTON_CONTROL)
6604 return TRUE;
6605
Bram Moolenaar734a8672019-12-02 22:49:38 +01006606 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006608 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006609 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006610 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006611
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006612 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6613 p = utf16_to_enc(wp, NULL);
6614 vim_strncpy(s_textfield, p, IOSIZE);
6615 vim_free(p);
6616 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 /*
6620 * Need to check for IDOK because if the user just hits Return to
6621 * accept the default value, some reason this is what we get.
6622 */
6623 if (button == IDOK)
6624 {
6625 if (dialog_default_button > IDCANCEL)
6626 EndDialog(hwnd, dialog_default_button);
6627 }
6628 else
6629 EndDialog(hwnd, button - IDCANCEL);
6630 return TRUE;
6631 }
6632
6633 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6634 {
6635 EndDialog(hwnd, 0);
6636 return TRUE;
6637 }
6638 return FALSE;
6639}
6640
6641/*
6642 * Create a dialog dynamically from the parameter strings.
6643 * type = type of dialog (question, alert, etc.)
6644 * title = dialog title. may be NULL for default title.
6645 * message = text to display. Dialog sizes to accommodate it.
6646 * buttons = '\n' separated list of button captions, default first.
6647 * dfltbutton = number of default button.
6648 *
6649 * This routine returns 1 if the first button is pressed,
6650 * 2 for the second, etc.
6651 *
6652 * 0 indicates Esc was pressed.
6653 * -1 for unexpected error
6654 *
6655 * If stubbing out this fn, return 1.
6656 */
6657
Bram Moolenaar734a8672019-12-02 22:49:38 +01006658static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 "IDR_VIM",
6661 "IDR_VIM_ERROR",
6662 "IDR_VIM_ALERT",
6663 "IDR_VIM_INFO",
6664 "IDR_VIM_QUESTION"
6665};
6666
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 int
6668gui_mch_dialog(
6669 int type,
6670 char_u *title,
6671 char_u *message,
6672 char_u *buttons,
6673 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006674 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006675 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006678 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 int numButtons;
6680 int *buttonWidths, *buttonPositions;
6681 int buttonYpos;
6682 int nchar, i;
6683 DWORD lStyle;
6684 int dlgwidth = 0;
6685 int dlgheight;
6686 int editboxheight;
6687 int horizWidth = 0;
6688 int msgheight;
6689 char_u *pstart;
6690 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006691 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 char_u *tbuffer;
6693 RECT rect;
6694 HWND hwnd;
6695 HDC hdc;
6696 HFONT font, oldFont;
6697 TEXTMETRIC fontInfo;
6698 int fontHeight;
6699 int textWidth, minButtonWidth, messageWidth;
6700 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006701 int maxDialogHeight;
6702 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 int vertical;
6704 int dlgPaddingX;
6705 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006706# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006707 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006709# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006710 garray_T ga;
6711 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006712 int dlg_icon_width;
6713 int dlg_icon_height;
6714 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006716# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006717 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006718# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006719 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006720# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006721 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006722 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
Bram Moolenaar748bf032005-02-02 23:04:36 +00006725 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006726 {
6727 load_dpi_func();
6728 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006729 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006730 }
6731 else
6732 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 if ((type < 0) || (type > VIM_LAST_TYPE))
6735 type = 0;
6736
Bram Moolenaar734a8672019-12-02 22:49:38 +01006737 // allocate some memory for dialog template
6738 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006739 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006740 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 if (p == NULL)
6743 return -1;
6744
6745 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006746 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6748 * const.
6749 */
6750 tbuffer = vim_strsave(buttons);
6751 if (tbuffer == NULL)
6752 return -1;
6753
Bram Moolenaar734a8672019-12-02 22:49:38 +01006754 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar734a8672019-12-02 22:49:38 +01006756 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 numButtons = 1;
6758 for (i = 0; tbuffer[i] != '\0'; i++)
6759 {
6760 if (tbuffer[i] == DLG_BUTTON_SEP)
6761 numButtons++;
6762 }
6763 if (dfltbutton >= numButtons)
6764 dfltbutton = -1;
6765
Bram Moolenaar734a8672019-12-02 22:49:38 +01006766 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006767 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (buttonWidths == NULL)
6769 return -1;
6770
Bram Moolenaar734a8672019-12-02 22:49:38 +01006771 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006772 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 if (buttonPositions == NULL)
6774 return -1;
6775
6776 /*
6777 * Calculate how big the dialog must be.
6778 */
6779 hwnd = GetDesktopWindow();
6780 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006781# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6783 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006784 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 use_lfSysmenu = TRUE;
6786 }
6787 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006788# endif
K.Takatad1c58992022-01-23 12:31:57 +00006789 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6790 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6791
6792 oldFont = SelectFont(hdc, font);
6793 dlgPaddingX = DLG_PADDING_X;
6794 dlgPaddingY = DLG_PADDING_Y;
6795
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 GetTextMetrics(hdc, &fontInfo);
6797 fontHeight = fontInfo.tmHeight;
6798
Bram Moolenaar734a8672019-12-02 22:49:38 +01006799 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006800 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar734a8672019-12-02 22:49:38 +01006802 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006803 if (s_hwnd == NULL)
6804 {
6805 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
Bram Moolenaar734a8672019-12-02 22:49:38 +01006807 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006808 get_work_area(&workarea_rect);
6809 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006810 if (maxDialogWidth > adjust_by_system_dpi(600))
6811 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006812 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006813 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006814 }
6815 else
6816 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006817 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006818 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006819 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006820 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6821 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6822 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6823 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006824
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006825 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006826 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6827 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6828 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6829 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6830 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006831 }
6832
Bram Moolenaar734a8672019-12-02 22:49:38 +01006833 // Set dlgwidth to width of message.
6834 // Copy the message into "ga", changing NL to CR-NL and inserting line
6835 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 pstart = message;
6837 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006838 msgheight = 0;
6839 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 do
6841 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006842 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006843
Bram Moolenaar734a8672019-12-02 22:49:38 +01006844 // Need to figure out where to break the string. The system does it
6845 // at a word boundary, which would mean we can't compute the number of
6846 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006847 textWidth = 0;
6848 last_white = NULL;
6849 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006850 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006851 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006852 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006853 && textWidth > maxDialogWidth * 3 / 4)
6854 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006855 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006856 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006857 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006858 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006859 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006860 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006861 textWidth = 0;
6862
6863 if (last_white != NULL)
6864 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006865 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006866 if (pend > last_white)
6867 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006868 pend = last_white + 1;
6869 last_white = NULL;
6870 }
6871 ga_append(&ga, '\r');
6872 ga_append(&ga, '\n');
6873 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006874 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006875
6876 while (--l >= 0)
6877 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006878 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006879 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006881
6882 ga_append(&ga, '\r');
6883 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 pstart = pend + 1;
6885 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006886
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006887 if (ga.ga_data != NULL)
6888 message = ga.ga_data;
6889
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006891
K.Takatac81e9bf2022-01-16 14:15:49 +00006892 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6893 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
K.Takatac81e9bf2022-01-16 14:15:49 +00006895 // Add width of icon to dlgwidth, and some space
6896 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6897 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6898
6899 if (msgheight < dlg_icon_height)
6900 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901
6902 /*
6903 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006904 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006906 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (!vertical)
6908 {
6909 // Place buttons horizontally if they fit.
6910 horizWidth = dlgPaddingX;
6911 pstart = tbuffer;
6912 i = 0;
6913 do
6914 {
6915 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6916 if (pend == NULL)
6917 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006918 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (textWidth < minButtonWidth)
6920 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006921 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 buttonWidths[i] = textWidth;
6923 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006924 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 pstart = pend + 1;
6926 } while (*pend != NUL);
6927
6928 if (horizWidth > maxDialogWidth)
6929 vertical = TRUE; // Too wide to fit on the screen.
6930 else if (horizWidth > dlgwidth)
6931 dlgwidth = horizWidth;
6932 }
6933
6934 if (vertical)
6935 {
6936 // Stack buttons vertically.
6937 pstart = tbuffer;
6938 do
6939 {
6940 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6941 if (pend == NULL)
6942 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006943 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 textWidth += dlgPaddingX; // Padding within button
6945 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 if (textWidth > dlgwidth)
6947 dlgwidth = textWidth;
6948 pstart = pend + 1;
6949 } while (*pend != NUL);
6950 }
6951
6952 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006953 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaar734a8672019-12-02 22:49:38 +01006955 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006956 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 add_long(lStyle);
6959 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006960 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 add_word(0); // NumberOfItems(will change later)
6962 add_word(10); // x
6963 add_word(10); // y
6964 add_word(PixelToDialogX(dlgwidth)); // cx
6965
6966 // Dialog height.
6967 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006968 dlgheight = msgheight + 2 * dlgPaddingY
6969 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 else
6971 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6972
6973 // Dialog needs to be taller if contains an edit box.
6974 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6975 if (textfield != NULL)
6976 dlgheight += editboxheight;
6977
Bram Moolenaar734a8672019-12-02 22:49:38 +01006978 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006979 if (dlgheight > maxDialogHeight)
6980 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006981 msgheight = msgheight - (dlgheight - maxDialogHeight);
6982 dlgheight = maxDialogHeight;
6983 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006984 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00006985 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006986 }
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 add_word(PixelToDialogY(dlgheight));
6989
6990 add_word(0); // Menu
6991 add_word(0); // Class
6992
Bram Moolenaar734a8672019-12-02 22:49:38 +01006993 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006994 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6995 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 p += nchar;
6997
K.Takatad1c58992022-01-23 12:31:57 +00006998 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006999# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007000 if (use_lfSysmenu)
7001 {
7002 // point size
7003 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7004 GetDeviceCaps(hdc, LOGPIXELSY));
7005 wcscpy(p, lfSysmenu.lfFaceName);
7006 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
K.Takatad1c58992022-01-23 12:31:57 +00007008 else
7009# endif
7010 {
7011 *p++ = DLG_FONT_POINT_SIZE; // point size
7012 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7013 }
7014 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 buttonYpos = msgheight + 2 * dlgPaddingY;
7017
7018 if (textfield != NULL)
7019 buttonYpos += editboxheight;
7020
7021 pstart = tbuffer;
7022 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007023 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 for (i = 0; i < numButtons; i++)
7025 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007026 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 for ( pend = pstart;
7028 *pend && (*pend != DLG_BUTTON_SEP);
7029 pend++)
7030 ;
7031
7032 if (*pend)
7033 *pend = '\0';
7034
7035 /*
7036 * old NOTE:
7037 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7038 * the focus to the first tab-able button and in so doing makes that
7039 * the default!! Grrr. Workaround: Make the default button the only
7040 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7041 * he/she can use arrow keys.
7042 *
7043 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007044 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 * dialog. Also needed for when the textfield is the default control.
7046 * It appears to work now (perhaps not on Win95?).
7047 */
7048 if (vertical)
7049 {
7050 p = add_dialog_element(p,
7051 (i == dfltbutton
7052 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7053 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007054 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 + 2 * fontHeight * i),
7056 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7057 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007058 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060 else
7061 {
7062 p = add_dialog_element(p,
7063 (i == dfltbutton
7064 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7065 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007066 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 PixelToDialogX(buttonWidths[i]),
7068 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007069 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007071 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
7073 *pnumitems += numButtons;
7074
Bram Moolenaar734a8672019-12-02 22:49:38 +01007075 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 p = add_dialog_element(p, SS_ICON,
7077 PixelToDialogX(dlgPaddingX),
7078 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007079 PixelToDialogX(dlg_icon_width),
7080 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7082 dlg_icons[type]);
7083
Bram Moolenaar734a8672019-12-02 22:49:38 +01007084 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007085 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007086 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007087 PixelToDialogY(dlgPaddingY),
7088 (WORD)(PixelToDialogX(messageWidth) + 1),
7089 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007090 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
Bram Moolenaar734a8672019-12-02 22:49:38 +01007092 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (textfield != NULL)
7094 {
7095 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7096 PixelToDialogX(2 * dlgPaddingX),
7097 PixelToDialogY(2 * dlgPaddingY + msgheight),
7098 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7099 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007100 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 *pnumitems += 1;
7102 }
7103
7104 *pnumitems += 2;
7105
7106 SelectFont(hdc, oldFont);
7107 DeleteObject(font);
7108 ReleaseDC(hwnd, hdc);
7109
Bram Moolenaar734a8672019-12-02 22:49:38 +01007110 // Let the dialog_callback() function know which button to make default
7111 // If we have an edit box, make that the default. We also need to tell
7112 // dialog_callback() if this dialog contains an edit box or not. We do
7113 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (textfield != NULL)
7115 {
7116 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7117 s_textfield = textfield;
7118 }
7119 else
7120 {
7121 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7122 s_textfield = NULL;
7123 }
7124
Bram Moolenaar734a8672019-12-02 22:49:38 +01007125 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007127 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 (LPDLGTEMPLATE)pdlgtemplate,
7129 s_hwnd,
7130 (DLGPROC)dialog_callback);
7131
7132 LocalFree(LocalHandle(pdlgtemplate));
7133 vim_free(tbuffer);
7134 vim_free(buttonWidths);
7135 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007136 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
Bram Moolenaar734a8672019-12-02 22:49:38 +01007138 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 (void)SetFocus(s_hwnd);
7140
7141 return nchar;
7142}
7143
Bram Moolenaar734a8672019-12-02 22:49:38 +01007144#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146/*
7147 * Put a simple element (basic class) onto a dialog template in memory.
7148 * return a pointer to where the next item should be added.
7149 *
7150 * parameters:
7151 * lStyle = additional style flags
7152 * (be careful, NT3.51 & Win32s will ignore the new ones)
7153 * x,y = x & y positions IN DIALOG UNITS
7154 * w,h = width and height IN DIALOG UNITS
7155 * Id = ID used in messages
7156 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7157 * caption = usually text or resource name
7158 *
7159 * TODO: use the length information noted here to enable the dialog creation
7160 * routines to work out more exactly how much memory they need to alloc.
7161 */
7162 static PWORD
7163add_dialog_element(
7164 PWORD p,
7165 DWORD lStyle,
7166 WORD x,
7167 WORD y,
7168 WORD w,
7169 WORD h,
7170 WORD Id,
7171 WORD clss,
7172 const char *caption)
7173{
7174 int nchar;
7175
Bram Moolenaar734a8672019-12-02 22:49:38 +01007176 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7178 *p++ = LOWORD(lStyle);
7179 *p++ = HIWORD(lStyle);
7180 *p++ = 0; // LOWORD (lExtendedStyle)
7181 *p++ = 0; // HIWORD (lExtendedStyle)
7182 *p++ = x;
7183 *p++ = y;
7184 *p++ = w;
7185 *p++ = h;
7186 *p++ = Id; //9 or 10 words in all
7187
7188 *p++ = (WORD)0xffff;
7189 *p++ = clss; //2 more here
7190
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007191 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 p += nchar;
7193
7194 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7195
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007196 return p; // total = 15 + strlen(caption) words
7197 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198}
7199
7200
7201/*
7202 * Helper routine. Take an input pointer, return closest pointer that is
7203 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7204 */
7205 static LPWORD
7206lpwAlign(
7207 LPWORD lpIn)
7208{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007209 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007211 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 ul += 3;
7213 ul >>= 2;
7214 ul <<= 2;
7215 return (LPWORD)ul;
7216}
7217
7218/*
7219 * Helper routine. Takes second parameter as Ansi string, copies it to first
7220 * parameter as wide character (16-bits / char) string, and returns integer
7221 * number of wide characters (words) in string (including the trailing wide
7222 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007223 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7224 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 static int
7226nCopyAnsiToWideChar(
7227 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007228 LPSTR lpAnsiIn,
7229 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007232 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 int i;
7234 WCHAR *wn;
7235
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007236 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007238 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007239 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 if (wn != NULL)
7241 {
7242 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007243 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 vim_free(wn);
7245 }
7246 }
7247 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007248 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 nChar = MultiByteToWideChar(
7250 enc_codepage > 0 ? enc_codepage : CP_ACP,
7251 MB_PRECOMPOSED,
7252 lpAnsiIn, len,
7253 lpWCStr, len);
7254 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007255 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258 return nChar;
7259}
7260
7261
7262#ifdef FEAT_TEAROFF
7263/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007264 * Lookup menu handle from "menu_id".
7265 */
7266 static HMENU
7267tearoff_lookup_menuhandle(
7268 vimmenu_T *menu,
7269 WORD menu_id)
7270{
7271 for ( ; menu != NULL; menu = menu->next)
7272 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007273 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007274 continue;
7275 if (menu_is_separator(menu->dname))
7276 continue;
7277 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7278 return menu->submenu_id;
7279 }
7280 return NULL;
7281}
7282
7283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 * The callback function for all the modeless dialogs that make up the
7285 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7286 * thinking its menus have been clicked), and go away when closed.
7287 */
7288 static LRESULT CALLBACK
7289tearoff_callback(
7290 HWND hwnd,
7291 UINT message,
7292 WPARAM wParam,
7293 LPARAM lParam)
7294{
7295 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007296 {
7297 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaar734a8672019-12-02 22:49:38 +01007301 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 HandleMouseHide(message, lParam);
7303
7304 if (message == WM_COMMAND)
7305 {
7306 if ((WORD)(LOWORD(wParam)) & 0x8000)
7307 {
7308 POINT mp;
7309 RECT rect;
7310
7311 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7312 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007313 vimmenu_T *menu;
7314
7315 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007317 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7319 (int)rect.right - 8,
7320 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007321 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 s_hwnd,
7323 NULL);
7324 /*
7325 * NOTE: The pop-up menu can eat the mouse up event.
7326 * We deal with this in normal.c.
7327 */
7328 }
7329 }
7330 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007331 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7333 /*
7334 * Give main window the focus back: this is so after
7335 * choosing a tearoff button you can start typing again
7336 * straight away.
7337 */
7338 (void)SetFocus(s_hwnd);
7339 return TRUE;
7340 }
7341 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7342 {
7343 DestroyWindow(hwnd);
7344 return TRUE;
7345 }
7346
Bram Moolenaar734a8672019-12-02 22:49:38 +01007347 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 if (message == WM_EXITSIZEMOVE)
7349 (void)SetActiveWindow(s_hwnd);
7350
7351 return FALSE;
7352}
7353#endif
7354
7355
7356/*
K.Takatad1c58992022-01-23 12:31:57 +00007357 * Computes the dialog base units based on the current dialog font.
7358 * We don't use the GetDialogBaseUnits() API, because we don't use the
7359 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 */
7361 static void
7362get_dialog_font_metrics(void)
7363{
7364 HDC hdc;
7365 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 SIZE size;
7367#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007368 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#endif
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007372 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007373 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#endif
7376 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007377 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378
K.Takatad1c58992022-01-23 12:31:57 +00007379 hdc = GetDC(s_hwnd);
7380 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007381 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007382 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
K.Takataabe628e2022-01-23 16:25:17 +00007384 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007385 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386}
7387
7388#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7389/*
7390 * Create a pseudo-"tearoff menu" based on the child
7391 * items of a given menu pointer.
7392 */
7393 static void
7394gui_mch_tearoff(
7395 char_u *title,
7396 vimmenu_T *menu,
7397 int initX,
7398 int initY)
7399{
7400 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7401 int template_len;
7402 int nchar, textWidth, submenuWidth;
7403 DWORD lStyle;
7404 DWORD lExtendedStyle;
7405 WORD dlgwidth;
7406 WORD menuID;
7407 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007408 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 vimmenu_T *the_menu = menu;
7410 HWND hwnd;
7411 HDC hdc;
7412 HFONT font, oldFont;
7413 int col, spaceWidth, len;
7414 int columnWidths[2];
7415 char_u *label, *text;
7416 int acLen = 0;
7417 int nameLen;
7418 int padding0, padding1, padding2 = 0;
7419 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007420 int x;
7421 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007422# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007423 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
7427 /*
7428 * If this menu is already torn off, move it to the mouse position.
7429 */
7430 if (IsWindow(menu->tearoff_handle))
7431 {
7432 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007433 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
7435 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7436 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7437 }
7438 return;
7439 }
7440
7441 /*
7442 * Create a new tearoff.
7443 */
7444 if (*title == MNU_HIDDEN_CHAR)
7445 title++;
7446
Bram Moolenaar734a8672019-12-02 22:49:38 +01007447 // Allocate memory to store the dialog template. It's made bigger when
7448 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 template_len = DLG_ALLOC_SIZE;
7450 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7451 if (p == NULL)
7452 return;
7453
7454 hwnd = GetDesktopWindow();
7455 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007456# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7458 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007459 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 use_lfSysmenu = TRUE;
7461 }
7462 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007463# endif
K.Takatad1c58992022-01-23 12:31:57 +00007464 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7465 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7466
7467 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
Bram Moolenaar734a8672019-12-02 22:49:38 +01007469 // Calculate width of a single space. Used for padding columns to the
7470 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007471 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
Bram Moolenaar734a8672019-12-02 22:49:38 +01007473 // Figure out max width of the text column, the accelerator column and the
7474 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 submenuWidth = 0;
7476 for (col = 0; col < 2; col++)
7477 {
7478 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007479 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007481 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 text = (col == 0) ? pmenu->dname : pmenu->actext;
7483 if (text != NULL && *text != NUL)
7484 {
7485 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7486 if (textWidth > columnWidths[col])
7487 columnWidths[col] = textWidth;
7488 }
7489 if (pmenu->children != NULL)
7490 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7491 }
7492 }
7493 if (columnWidths[1] == 0)
7494 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007495 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (submenuWidth != 0)
7497 columnWidths[0] += submenuWidth;
7498 else
7499 columnWidths[0] += spaceWidth;
7500 }
7501 else
7502 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007503 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7505 columnWidths[1] += submenuWidth;
7506 }
7507
7508 /*
7509 * Now find the total width of our 'menu'.
7510 */
7511 textWidth = columnWidths[0] + columnWidths[1];
7512 if (submenuWidth != 0)
7513 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007514 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7516 textWidth += submenuWidth;
7517 }
7518 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7519 if (textWidth > dlgwidth)
7520 dlgwidth = textWidth;
7521 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7522
Bram Moolenaar734a8672019-12-02 22:49:38 +01007523 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007524 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7527 *p++ = LOWORD(lStyle);
7528 *p++ = HIWORD(lStyle);
7529 *p++ = LOWORD(lExtendedStyle);
7530 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007531 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007533 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007535 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 else
7537 *p++ = PixelToDialogX(initX); // x
7538 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007539 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 else
7541 *p++ = PixelToDialogY(initY); // y
7542 *p++ = PixelToDialogX(dlgwidth); // cx
7543 ptrueheight = p;
7544 *p++ = 0; // dialog height: changed later anyway
7545 *p++ = 0; // Menu
7546 *p++ = 0; // Class
7547
Bram Moolenaar734a8672019-12-02 22:49:38 +01007548 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007550 ? (LPSTR)title
7551 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 p += nchar;
7553
K.Takatad1c58992022-01-23 12:31:57 +00007554 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007555# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007556 if (use_lfSysmenu)
7557 {
7558 // point size
7559 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7560 GetDeviceCaps(hdc, LOGPIXELSY));
7561 wcscpy(p, lfSysmenu.lfFaceName);
7562 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
K.Takatad1c58992022-01-23 12:31:57 +00007564 else
7565# endif
7566 {
7567 *p++ = DLG_FONT_POINT_SIZE; // point size
7568 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7569 }
7570 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 /*
7573 * Loop over all the items in the menu.
7574 * But skip over the tearbar.
7575 */
7576 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7577 menu = menu->children->next;
7578 else
7579 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007580 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 for ( ; menu != NULL; menu = menu->next)
7582 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007583 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 continue;
7585 if (menu_is_separator(menu->dname))
7586 {
7587 sepPadding += 3;
7588 continue;
7589 }
7590
Bram Moolenaar734a8672019-12-02 22:49:38 +01007591 // Check if there still is plenty of room in the template. Make it
7592 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7594 {
7595 WORD *newp;
7596
7597 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7598 if (newp != NULL)
7599 {
7600 template_len += 4096;
7601 mch_memmove(newp, pdlgtemplate,
7602 (char *)p - (char *)pdlgtemplate);
7603 p = newp + (p - pdlgtemplate);
7604 pnumitems = newp + (pnumitems - pdlgtemplate);
7605 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7606 LocalFree(LocalHandle(pdlgtemplate));
7607 pdlgtemplate = newp;
7608 }
7609 }
7610
Bram Moolenaar734a8672019-12-02 22:49:38 +01007611 // Figure out minimal length of this menu label. Use "name" for the
7612 // actual text, "dname" for estimating the displayed size. "name"
7613 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 len = nameLen = (int)STRLEN(menu->name);
7615 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7616 (int)STRLEN(menu->dname))) / spaceWidth;
7617 len += padding0;
7618
7619 if (menu->actext != NULL)
7620 {
7621 acLen = (int)STRLEN(menu->actext);
7622 len += acLen;
7623 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7624 }
7625 else
7626 textWidth = 0;
7627 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7628 len += padding1;
7629
7630 if (menu->children == NULL)
7631 {
7632 padding2 = submenuWidth / spaceWidth;
7633 len += padding2;
7634 menuID = (WORD)(menu->id);
7635 }
7636 else
7637 {
7638 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007639 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 }
7641
Bram Moolenaar734a8672019-12-02 22:49:38 +01007642 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007643 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (label == NULL)
7645 break;
7646
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007647 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007648 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007650 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 while (padding0-- > 0)
7652 *text++ = ' ';
7653 if (menu->actext != NULL)
7654 {
7655 STRNCPY(text, menu->actext, acLen);
7656 text += acLen;
7657 }
7658 while (padding1-- > 0)
7659 *text++ = ' ';
7660 if (menu->children != NULL)
7661 {
7662 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7663 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7664 }
7665 else
7666 {
7667 while (padding2-- > 0)
7668 *text++ = ' ';
7669 }
7670 *text = NUL;
7671
7672 /*
7673 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7674 * W95/NT4 it makes the tear-off look more like a menu.
7675 */
7676 p = add_dialog_element(p,
7677 BS_PUSHBUTTON|BS_LEFT,
7678 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7679 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7680 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7681 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007682 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 vim_free(label);
7684 (*pnumitems)++;
7685 }
7686
7687 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7688
7689
Bram Moolenaar734a8672019-12-02 22:49:38 +01007690 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007691 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007692 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 (LPDLGTEMPLATE)pdlgtemplate,
7694 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007695 (DLGPROC)tearoff_callback,
7696 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
7698 LocalFree(LocalHandle(pdlgtemplate));
7699 SelectFont(hdc, oldFont);
7700 DeleteObject(font);
7701 ReleaseDC(hwnd, hdc);
7702
7703 /*
7704 * Reassert ourselves as the active window. This is so that after creating
7705 * a tearoff, the user doesn't have to click with the mouse just to start
7706 * typing again!
7707 */
7708 (void)SetActiveWindow(s_hwnd);
7709
Bram Moolenaar734a8672019-12-02 22:49:38 +01007710 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 force_menu_update = TRUE;
7712}
7713#endif
7714
7715#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007716# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718/*
7719 * Create the toolbar, initially unpopulated.
7720 * (just like the menu, there are no defaults, it's all
7721 * set up through menu.vim)
7722 */
7723 static void
7724initialise_toolbar(void)
7725{
7726 InitCommonControls();
7727 s_toolbarhwnd = CreateToolbarEx(
7728 s_hwnd,
7729 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7730 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007731 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007732 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 IDR_TOOLBAR1, // id of initial bitmap
7734 NULL,
7735 0, // initial number of buttons
7736 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7737 TOOLBAR_BUTTON_HEIGHT,
7738 TOOLBAR_BUTTON_WIDTH,
7739 TOOLBAR_BUTTON_HEIGHT,
7740 sizeof(TBBUTTON)
7741 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007742
7743 // Remove transparency from the toolbar to prevent the main window
7744 // background colour showing through
7745 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7746 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7747
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007748 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
7750 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007751
7752 update_toolbar_size();
7753}
7754
7755 static void
7756update_toolbar_size(void)
7757{
7758 int w, h;
7759 TBMETRICS tbm = {sizeof(TBMETRICS)};
7760
7761 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7762 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7763 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7764 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7765
7766 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7767 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7768 //TRACE("button size: %d, %d", w, h);
7769 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7770 gui.toolbar_height = h + 6;
7771
7772 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7773 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7774
7775 // TODO:
7776 // Currently, this function only updates the size of toolbar buttons.
7777 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778}
7779
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007780 static LRESULT CALLBACK
7781toolbar_wndproc(
7782 HWND hwnd,
7783 UINT uMsg,
7784 WPARAM wParam,
7785 LPARAM lParam)
7786{
7787 HandleMouseHide(uMsg, lParam);
7788 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7789}
7790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 static int
7792get_toolbar_bitmap(vimmenu_T *menu)
7793{
7794 int i = -1;
7795
7796 /*
7797 * Check user bitmaps first, unless builtin is specified.
7798 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007799 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {
7801 char_u fname[MAXPATHL];
7802 HANDLE hbitmap = NULL;
7803
7804 if (menu->iconfile != NULL)
7805 {
7806 gui_find_iconfile(menu->iconfile, fname, "bmp");
7807 hbitmap = LoadImage(
7808 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007809 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 IMAGE_BITMAP,
7811 TOOLBAR_BUTTON_WIDTH,
7812 TOOLBAR_BUTTON_HEIGHT,
7813 LR_LOADFROMFILE |
7814 LR_LOADMAP3DCOLORS
7815 );
7816 }
7817
7818 /*
7819 * If the LoadImage call failed, or the "icon=" file
7820 * didn't exist or wasn't specified, try the menu name
7821 */
7822 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007823 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007824# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007825 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007826# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007827 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 hbitmap = LoadImage(
7829 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007830 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 IMAGE_BITMAP,
7832 TOOLBAR_BUTTON_WIDTH,
7833 TOOLBAR_BUTTON_HEIGHT,
7834 LR_LOADFROMFILE |
7835 LR_LOADMAP3DCOLORS
7836 );
7837
7838 if (hbitmap != NULL)
7839 {
7840 TBADDBITMAP tbAddBitmap;
7841
7842 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007843 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7846 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007847 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849 }
7850 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7851 i = menu->iconidx;
7852
7853 return i;
7854}
7855#endif
7856
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007857#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7858 static void
7859initialise_tabline(void)
7860{
7861 InitCommonControls();
7862
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007863 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007864 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007865 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007866 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007867 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007868
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007869 gui.tabline_height = TABLINE_HEIGHT;
7870
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007871 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007872}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007873
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007874/*
7875 * Get tabpage_T from POINT.
7876 */
7877 static tabpage_T *
7878GetTabFromPoint(
7879 HWND hWnd,
7880 POINT pt)
7881{
7882 tabpage_T *ptp = NULL;
7883
7884 if (gui_mch_showing_tabline())
7885 {
7886 TCHITTESTINFO htinfo;
7887 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007888 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007889 if (s_tabhwnd == hWnd)
7890 {
7891 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7892 if (idx != -1)
7893 ptp = find_tabpage(idx + 1);
7894 }
7895 }
7896 return ptp;
7897}
7898
7899static POINT s_pt = {0, 0};
7900static HCURSOR s_hCursor = NULL;
7901
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007902 static LRESULT CALLBACK
7903tabline_wndproc(
7904 HWND hwnd,
7905 UINT uMsg,
7906 WPARAM wParam,
7907 LPARAM lParam)
7908{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007909 POINT pt;
7910 tabpage_T *tp;
7911 RECT rect;
7912 int nCenter;
7913 int idx0;
7914 int idx1;
7915
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007916 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007917
7918 switch (uMsg)
7919 {
7920 case WM_LBUTTONDOWN:
7921 {
7922 s_pt.x = GET_X_LPARAM(lParam);
7923 s_pt.y = GET_Y_LPARAM(lParam);
7924 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007925 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007926 break;
7927 }
7928 case WM_MOUSEMOVE:
7929 if (GetCapture() == hwnd
7930 && ((wParam & MK_LBUTTON)) != 0)
7931 {
7932 pt.x = GET_X_LPARAM(lParam);
7933 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007934 if (abs(pt.x - s_pt.x) >
7935 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007936 {
7937 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7938
7939 tp = GetTabFromPoint(hwnd, pt);
7940 if (tp != NULL)
7941 {
7942 idx0 = tabpage_index(curtab) - 1;
7943 idx1 = tabpage_index(tp) - 1;
7944
7945 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7946 nCenter = rect.left + (rect.right - rect.left) / 2;
7947
Bram Moolenaar734a8672019-12-02 22:49:38 +01007948 // Check if the mouse cursor goes over the center of
7949 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007950 if ((idx0 < idx1) && (nCenter < pt.x))
7951 {
7952 tabpage_move(idx1 + 1);
7953 update_screen(0);
7954 }
7955 else if ((idx1 < idx0) && (pt.x < nCenter))
7956 {
7957 tabpage_move(idx1);
7958 update_screen(0);
7959 }
7960 }
7961 }
7962 }
7963 break;
7964 case WM_LBUTTONUP:
7965 {
7966 if (GetCapture() == hwnd)
7967 {
7968 SetCursor(s_hCursor);
7969 ReleaseCapture();
7970 }
7971 break;
7972 }
7973 default:
7974 break;
7975 }
7976
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007977 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7978}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007979#endif
7980
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7982/*
7983 * Make the GUI window come to the foreground.
7984 */
7985 void
7986gui_mch_set_foreground(void)
7987{
7988 if (IsIconic(s_hwnd))
7989 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7990 SetForegroundWindow(s_hwnd);
7991}
7992#endif
7993
7994#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7995 static void
7996dyn_imm_load(void)
7997{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007998 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (hLibImm == NULL)
8000 return;
8001
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 pImmGetCompositionStringW
8003 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8004 pImmGetContext
8005 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8006 pImmAssociateContext
8007 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8008 pImmReleaseContext
8009 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8010 pImmGetOpenStatus
8011 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8012 pImmSetOpenStatus
8013 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008014 pImmGetCompositionFontW
8015 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8016 pImmSetCompositionFontW
8017 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 pImmSetCompositionWindow
8019 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8020 pImmGetConversionStatus
8021 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008022 pImmSetConversionStatus
8023 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024
K.Takatab0b2b732022-01-19 12:59:21 +00008025 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 || pImmGetContext == NULL
8027 || pImmAssociateContext == NULL
8028 || pImmReleaseContext == NULL
8029 || pImmGetOpenStatus == NULL
8030 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008031 || pImmGetCompositionFontW == NULL
8032 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008034 || pImmGetConversionStatus == NULL
8035 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {
8037 FreeLibrary(hLibImm);
8038 hLibImm = NULL;
8039 pImmGetContext = NULL;
8040 return;
8041 }
8042
8043 return;
8044}
8045
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046#endif
8047
8048#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8049
8050# ifdef FEAT_XPM_W32
8051# define IMAGE_XPM 100
8052# endif
8053
8054typedef struct _signicon_t
8055{
8056 HANDLE hImage;
8057 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008058# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008059 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061} signicon_t;
8062
8063 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008064gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066 signicon_t *sign;
8067 int x, y, w, h;
8068
8069 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8070 return;
8071
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008072# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008073 if (IS_ENABLE_DIRECTX())
8074 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008075# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008076
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 x = TEXT_X(col);
8078 y = TEXT_Y(row);
8079 w = gui.char_width * 2;
8080 h = gui.char_height;
8081 switch (sign->uType)
8082 {
8083 case IMAGE_BITMAP:
8084 {
8085 HDC hdcMem;
8086 HBITMAP hbmpOld;
8087
8088 hdcMem = CreateCompatibleDC(s_hdc);
8089 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8090 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8091 SelectObject(hdcMem, hbmpOld);
8092 DeleteDC(hdcMem);
8093 }
8094 break;
8095 case IMAGE_ICON:
8096 case IMAGE_CURSOR:
8097 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8098 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008099# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 case IMAGE_XPM:
8101 {
8102 HDC hdcMem;
8103 HBITMAP hbmpOld;
8104
8105 hdcMem = CreateCompatibleDC(s_hdc);
8106 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008107 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8109
8110 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008111 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8113 SelectObject(hdcMem, hbmpOld);
8114 DeleteDC(hdcMem);
8115 }
8116 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
8119}
8120
8121 static void
8122close_signicon_image(signicon_t *sign)
8123{
8124 if (sign)
8125 switch (sign->uType)
8126 {
8127 case IMAGE_BITMAP:
8128 DeleteObject((HGDIOBJ)sign->hImage);
8129 break;
8130 case IMAGE_CURSOR:
8131 DestroyCursor((HCURSOR)sign->hImage);
8132 break;
8133 case IMAGE_ICON:
8134 DestroyIcon((HICON)sign->hImage);
8135 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008136# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 case IMAGE_XPM:
8138 DeleteObject((HBITMAP)sign->hImage);
8139 DeleteObject((HBITMAP)sign->hShape);
8140 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 }
8143}
8144
8145 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008146gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147{
8148 signicon_t sign, *psign;
8149 char_u *ext;
8150
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008152 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (ext > signfile)
8154 {
8155 int do_load = 1;
8156
8157 if (!STRICMP(ext, ".bmp"))
8158 sign.uType = IMAGE_BITMAP;
8159 else if (!STRICMP(ext, ".ico"))
8160 sign.uType = IMAGE_ICON;
8161 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8162 sign.uType = IMAGE_CURSOR;
8163 else
8164 do_load = 0;
8165
8166 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008167 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 gui.char_width * 2, gui.char_height,
8169 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008170# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 if (!STRICMP(ext, ".xpm"))
8172 {
8173 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008174 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8175 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179
8180 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008181 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 *psign = sign;
8183
8184 if (!psign)
8185 {
8186 if (sign.hImage)
8187 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008188 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 }
8190 return (void *)psign;
8191
8192}
8193
8194 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008195gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196{
8197 if (sign)
8198 {
8199 close_signicon_image((signicon_t *)sign);
8200 vim_free(sign);
8201 }
8202}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008205#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
Bram Moolenaar734a8672019-12-02 22:49:38 +01008207/*
8208 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008209 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008211 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8213 * to get current mouse position).
8214 *
8215 * Trying to use as more Windows services as possible, and as less
8216 * IE version as possible :)).
8217 *
8218 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8219 * BalloonEval struct.
8220 * 2) Enable/Disable simply create/kill BalloonEval Timer
8221 * 3) When there was enough inactivity, timer procedure posts
8222 * async request to debugger
8223 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8224 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008225 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 */
8227
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008228 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008229make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008230{
K.Takata76687d22022-01-25 10:31:37 +00008231 TOOLINFOW *pti;
8232 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008233
K.Takata76687d22022-01-25 10:31:37 +00008234 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008235 if (pti == NULL)
8236 return;
8237
8238 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8239 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8240 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008241 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008242
8243 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8244 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8245
K.Takata76687d22022-01-25 10:31:37 +00008246 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008247 pti->uFlags = TTF_SUBCLASS;
8248 pti->hwnd = beval->target;
8249 pti->hinst = 0; // Don't use string resources
8250 pti->uId = ID_BEVAL_TOOLTIP;
8251
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008252 pti->lpszText = LPSTR_TEXTCALLBACKW;
8253 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8254 pti->lParam = (LPARAM)beval->tofree;
8255 // switch multiline tooltips on
8256 if (GetClientRect(s_textArea, &rect))
8257 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8258 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008259
8260 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8261 pti->rect.left = pt.x - 3;
8262 pti->rect.top = pt.y - 3;
8263 pti->rect.right = pt.x + 3;
8264 pti->rect.bottom = pt.y + 3;
8265
8266 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8267 // Make tooltip appear sooner.
8268 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8269 // I've performed some tests and it seems the longest possible life time
8270 // of tooltip is 30 seconds.
8271 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8272 /*
8273 * HACK: force tooltip to appear, because it'll not appear until
8274 * first mouse move. D*mn M$
8275 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8276 */
8277 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8278 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8279 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008280}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008281
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008283delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008285 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286}
8287
8288 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008289beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008290 HWND hwnd UNUSED,
8291 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008292 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008293 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
8295 POINT pt;
8296 RECT rect;
8297
8298 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8299 return;
8300
8301 GetCursorPos(&pt);
8302 if (WindowFromPoint(pt) != s_textArea)
8303 return;
8304
8305 ScreenToClient(s_textArea, &pt);
8306 GetClientRect(s_textArea, &rect);
8307 if (!PtInRect(&rect, pt))
8308 return;
8309
K.Takataa8ec4912022-02-03 14:32:33 +00008310 if (last_user_activity > 0
8311 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 && (cur_beval->showState != ShS_PENDING
8313 || abs(cur_beval->x - pt.x) > 3
8314 || abs(cur_beval->y - pt.y) > 3))
8315 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008316 // Pointer resting in one place long enough, it's time to show
8317 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 cur_beval->showState = ShS_PENDING;
8319 cur_beval->x = pt.x;
8320 cur_beval->y = pt.y;
8321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 if (cur_beval->msgCB != NULL)
8323 (*cur_beval->msgCB)(cur_beval, 0);
8324 }
8325}
8326
8327 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008328gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
K.Takataa8ec4912022-02-03 14:32:33 +00008330 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008334gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 if (beval == NULL)
8337 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008338 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8339 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
8342 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008343gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008346
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008347 vim_free(beval->msg);
8348 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8349 if (beval->msg == NULL)
8350 {
8351 delete_tooltip(beval);
8352 beval->showState = ShS_NEUTRAL;
8353 return;
8354 }
8355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 if (beval->showState == ShS_SHOWING)
8357 return;
8358 GetCursorPos(&pt);
8359 ScreenToClient(s_textArea, &pt);
8360
8361 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008363 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 gui_mch_disable_beval_area(cur_beval);
8365 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008366 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368}
8369
8370 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008371gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008372 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008373 char_u *mesg,
8374 void (*mesgCB)(BalloonEval *, int),
8375 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008377 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 BalloonEval *beval;
8379
8380 if (mesg != NULL && mesgCB != NULL)
8381 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008382 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 return NULL;
8384 }
8385
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008386 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 if (beval != NULL)
8388 {
8389 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390
8391 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 beval->msg = mesg;
8393 beval->msgCB = mesgCB;
8394 beval->clientData = clientData;
8395
8396 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 cur_beval = beval;
8398
8399 if (p_beval)
8400 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 }
8402 return beval;
8403}
8404
8405 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008406Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008408 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 return;
8410
8411 if (cur_beval != NULL)
8412 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008413 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008415 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008416 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008417 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 delete_tooltip(cur_beval);
8419 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420
8421 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008422 break;
8423 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008424 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008425 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008426 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008427 info->lpszText = (LPSTR)info->lParam;
8428 info->uFlags |= TTF_DI_SETITEM;
8429 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008430 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008431 case TTN_GETDISPINFOW:
8432 {
8433 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008434 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008435 info->lpszText = (LPWSTR)info->lParam;
8436 info->uFlags |= TTF_DI_SETITEM;
8437 }
8438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
8440 }
8441}
8442
8443 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008444track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445{
8446 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8447 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008448 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449}
8450
8451 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008452gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008454# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008455 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008456# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008457 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 vim_free(beval);
8459}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008460#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
8462#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8463/*
8464 * We have multiple signs to draw at the same location. Draw the
8465 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8466 */
8467 void
8468netbeans_draw_multisign_indicator(int row)
8469{
8470 int i;
8471 int y;
8472 int x;
8473
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008474 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008475 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 x = 0;
8478 y = TEXT_Y(row);
8479
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008480# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008481 if (IS_ENABLE_DIRECTX())
8482 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008483# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 for (i = 0; i < gui.char_height - 3; i++)
8486 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8487
8488 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8489 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8490 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8491 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8492 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8493 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8494 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8495}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008496#endif