blob: 584e4ef3e63b0a24e6e568382369372afbf393ba [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
821/*
822 * Key hit, add it to the input buffer.
823 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 static void
825_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100826 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100828 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829{
830 char_u string[40];
831 int len = 0;
832
833 dead_key = 0;
834
835 len = char_to_string(ch, string, 40, FALSE);
836 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
837 {
838 trash_input_buf();
839 got_int = TRUE;
840 }
841
842 add_to_input_buf(string, len);
843}
844
845/*
846 * Alt-Key hit, add it to the input buffer.
847 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100848 static void
849_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100850 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100851 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100852 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100853{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100854 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100855 int len;
856 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100857 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100858
859 dead_key = 0;
860
Bram Moolenaar734a8672019-12-02 22:49:38 +0100861 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100862
Bram Moolenaar734a8672019-12-02 22:49:38 +0100863 // OK, we have a character key (given by ch) which was entered with the
864 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
865 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
866 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100867 modifiers = MOD_MASK_ALT;
868 if (GetKeyState(VK_SHIFT) & 0x8000)
869 modifiers |= MOD_MASK_SHIFT;
870 if (GetKeyState(VK_CONTROL) & 0x8000)
871 modifiers |= MOD_MASK_CTRL;
872
873 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100874 // remove the SHIFT modifier for keys where it's already included, e.g.,
875 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200876 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100877
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200878 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
879 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880 if (ch == CSI)
881 ch = K_CSI;
882
883 len = 0;
884 if (modifiers)
885 {
886 string[len++] = CSI;
887 string[len++] = KS_MODIFIER;
888 string[len++] = modifiers;
889 }
890
891 if (IS_SPECIAL((int)ch))
892 {
893 string[len++] = CSI;
894 string[len++] = K_SECOND((int)ch);
895 string[len++] = K_THIRD((int)ch);
896 }
897 else
898 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100899 // Although the documentation isn't clear about it, we assume "ch" is
900 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 len += char_to_string(ch, string + len, 40 - len, TRUE);
902 }
903
904 add_to_input_buf(string, len);
905}
906
907 static void
908_OnMouseEvent(
909 int button,
910 int x,
911 int y,
912 int repeated_click,
913 UINT keyFlags)
914{
915 int vim_modifiers = 0x0;
916
917 s_getting_focus = FALSE;
918
919 if (keyFlags & MK_SHIFT)
920 vim_modifiers |= MOUSE_SHIFT;
921 if (keyFlags & MK_CONTROL)
922 vim_modifiers |= MOUSE_CTRL;
923 if (GetKeyState(VK_MENU) & 0x8000)
924 vim_modifiers |= MOUSE_ALT;
925
926 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
927}
928
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100929 static void
930_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100931 HWND hwnd UNUSED,
932 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933 int x,
934 int y,
935 UINT keyFlags)
936{
937 static LONG s_prevTime = 0;
938
939 LONG currentTime = GetMessageTime();
940 int button = -1;
941 int repeated_click;
942
Bram Moolenaar734a8672019-12-02 22:49:38 +0100943 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944 (void)SetFocus(s_hwnd);
945
946 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
947 button = MOUSE_LEFT;
948 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
949 button = MOUSE_MIDDLE;
950 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
951 button = MOUSE_RIGHT;
952 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
953 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100954 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
955 }
956 else if (s_uMsg == WM_CAPTURECHANGED)
957 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100958 // on W95/NT4, somehow you get in here with an odd Msg
959 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100960 if (s_button_pending == MOUSE_LEFT)
961 button = MOUSE_RIGHT;
962 else
963 button = MOUSE_LEFT;
964 }
965 if (button >= 0)
966 {
967 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
968
969 /*
970 * Holding down the left and right buttons simulates pushing the middle
971 * button.
972 */
973 if (repeated_click
974 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
975 || (button == MOUSE_RIGHT
976 && s_button_pending == MOUSE_LEFT)))
977 {
978 /*
979 * Hmm, gui.c will ignore more than one button down at a time, so
980 * pretend we let go of it first.
981 */
982 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
983 button = MOUSE_MIDDLE;
984 repeated_click = FALSE;
985 s_button_pending = -1;
986 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
987 }
988 else if ((repeated_click)
989 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
990 {
991 if (s_button_pending > -1)
992 {
K.Takata45f9cfb2022-01-21 11:11:00 +0000993 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
994 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100995 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100996 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100997 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
998 }
999 else
1000 {
1001 /*
1002 * If this is the first press (i.e. not a multiple click) don't
1003 * action immediately, but store and wait for:
1004 * i) button-up
1005 * ii) mouse move
1006 * iii) another button press
1007 * before using it.
1008 * This enables us to make left+right simulate middle button,
1009 * without left or right being actioned first. The side-effect is
1010 * that if you click and hold the mouse without dragging, the
1011 * cursor doesn't move until you release the button. In practice
1012 * this is hardly a problem.
1013 */
1014 s_button_pending = button;
1015 s_x_pending = x;
1016 s_y_pending = y;
1017 s_kFlags_pending = keyFlags;
1018 }
1019
1020 s_prevTime = currentTime;
1021 }
1022}
1023
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001024 static void
1025_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001026 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001027 int x,
1028 int y,
1029 UINT keyFlags)
1030{
1031 int button;
1032
1033 s_getting_focus = FALSE;
1034 if (s_button_pending > -1)
1035 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001036 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001037 _OnMouseEvent(s_button_pending, s_x_pending,
1038 s_y_pending, FALSE, s_kFlags_pending);
1039 s_button_pending = -1;
1040 }
1041 if (s_uMsg == WM_MOUSEMOVE)
1042 {
1043 /*
1044 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1045 * down.
1046 */
1047 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1048 | MK_XBUTTON1 | MK_XBUTTON2)))
1049 {
1050 gui_mouse_moved(x, y);
1051 return;
1052 }
1053
1054 /*
1055 * While button is down, keep grabbing mouse move events when
1056 * the mouse goes outside the window
1057 */
1058 SetCapture(s_textArea);
1059 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001060 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001061 }
1062 else
1063 {
1064 ReleaseCapture();
1065 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001066 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001067 }
1068
1069 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1070}
1071
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001072 static void
1073_OnSizeTextArea(
1074 HWND hwnd UNUSED,
1075 UINT state UNUSED,
1076 int cx UNUSED,
1077 int cy UNUSED)
1078{
1079#if defined(FEAT_DIRECTX)
1080 if (IS_ENABLE_DIRECTX())
1081 directx_binddc();
1082#endif
1083}
1084
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001085#ifdef FEAT_MENU
1086/*
1087 * Find the vimmenu_T with the given id
1088 */
1089 static vimmenu_T *
1090gui_mswin_find_menu(
1091 vimmenu_T *pMenu,
1092 int id)
1093{
1094 vimmenu_T *pChildMenu;
1095
1096 while (pMenu)
1097 {
1098 if (pMenu->id == (UINT)id)
1099 break;
1100 if (pMenu->children != NULL)
1101 {
1102 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1103 if (pChildMenu)
1104 {
1105 pMenu = pChildMenu;
1106 break;
1107 }
1108 }
1109 pMenu = pMenu->next;
1110 }
1111 return pMenu;
1112}
1113
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 static void
1115_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001116 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001117 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001118 HWND hwndCtl UNUSED,
1119 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120{
1121 vimmenu_T *pMenu;
1122
1123 pMenu = gui_mswin_find_menu(root_menu, id);
1124 if (pMenu)
1125 gui_menu_cb(pMenu);
1126}
1127#endif
1128
1129#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001130/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001131 * Handle a Find/Replace window message.
1132 */
1133 static void
1134_OnFindRepl(void)
1135{
1136 int flags = 0;
1137 int down;
1138
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001139 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001140 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001141 (void)SetFocus(s_hwnd);
1142
1143 if (s_findrep_struct.Flags & FR_FINDNEXT)
1144 {
1145 flags = FRD_FINDNEXT;
1146
Bram Moolenaar734a8672019-12-02 22:49:38 +01001147 // Give main window the focus back: this is so the cursor isn't
1148 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001149 (void)SetFocus(s_hwnd);
1150 }
1151 else if (s_findrep_struct.Flags & FR_REPLACE)
1152 {
1153 flags = FRD_REPLACE;
1154
Bram Moolenaar734a8672019-12-02 22:49:38 +01001155 // Give main window the focus back: this is so the cursor isn't
1156 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 (void)SetFocus(s_hwnd);
1158 }
1159 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1160 {
1161 flags = FRD_REPLACEALL;
1162 }
1163
1164 if (flags != 0)
1165 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001166 char_u *p, *q;
1167
Bram Moolenaar734a8672019-12-02 22:49:38 +01001168 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001169 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1170 flags |= FRD_WHOLE_WORD;
1171 if (s_findrep_struct.Flags & FR_MATCHCASE)
1172 flags |= FRD_MATCH_CASE;
1173 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001174 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1175 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1176 if (p != NULL && q != NULL)
1177 gui_do_findrepl(flags, p, q, down);
1178 vim_free(p);
1179 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001180 }
1181}
1182#endif
1183
1184 static void
1185HandleMouseHide(UINT uMsg, LPARAM lParam)
1186{
1187 static LPARAM last_lParam = 0L;
1188
Bram Moolenaar734a8672019-12-02 22:49:38 +01001189 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1191 {
1192 if (lParam == last_lParam)
1193 return;
1194 last_lParam = lParam;
1195 }
1196
Bram Moolenaar734a8672019-12-02 22:49:38 +01001197 // Handle specially, to centralise coding. We need to be sure we catch all
1198 // possible events which should cause us to restore the cursor (as it is a
1199 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 switch (uMsg)
1201 {
1202 case WM_KEYUP:
1203 case WM_CHAR:
1204 /*
1205 * blank out the pointer if necessary
1206 */
1207 if (p_mh)
1208 gui_mch_mousehide(TRUE);
1209 break;
1210
Bram Moolenaar734a8672019-12-02 22:49:38 +01001211 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001212 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001213 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001214 case WM_LBUTTONDOWN:
1215 case WM_LBUTTONUP:
1216 case WM_MBUTTONDOWN:
1217 case WM_MBUTTONUP:
1218 case WM_RBUTTONDOWN:
1219 case WM_RBUTTONUP:
1220 case WM_XBUTTONDOWN:
1221 case WM_XBUTTONUP:
1222 case WM_NCMOUSEMOVE:
1223 case WM_NCLBUTTONDOWN:
1224 case WM_NCLBUTTONUP:
1225 case WM_NCMBUTTONDOWN:
1226 case WM_NCMBUTTONUP:
1227 case WM_NCRBUTTONDOWN:
1228 case WM_NCRBUTTONUP:
1229 case WM_KILLFOCUS:
1230 /*
1231 * if the pointer is currently hidden, then we should show it.
1232 */
1233 gui_mch_mousehide(FALSE);
1234 break;
1235 }
1236}
1237
1238 static LRESULT CALLBACK
1239_TextAreaWndProc(
1240 HWND hwnd,
1241 UINT uMsg,
1242 WPARAM wParam,
1243 LPARAM lParam)
1244{
1245 /*
1246 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1247 hwnd, uMsg, wParam, lParam);
1248 */
1249
1250 HandleMouseHide(uMsg, lParam);
1251
1252 s_uMsg = uMsg;
1253 s_wParam = wParam;
1254 s_lParam = lParam;
1255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001257 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001258#endif
1259
1260 switch (uMsg)
1261 {
1262 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1263 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1264 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1265 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1266 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1267 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1268 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1269 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1270 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1271 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1272 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1273 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1274 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1275 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001276 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001277
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001278#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001279 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1280 return TRUE;
1281#endif
1282 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001283 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001284 }
1285}
1286
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001287/*
1288 * Called when the foreground or background color has been changed.
1289 */
1290 void
1291gui_mch_new_colors(void)
1292{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001293 HBRUSH prevBrush;
1294
1295 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001296 prevBrush = (HBRUSH)SetClassLongPtr(
1297 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001298 InvalidateRect(s_hwnd, NULL, TRUE);
1299 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001300}
1301
1302/*
1303 * Set the colors to their default values.
1304 */
1305 void
1306gui_mch_def_colors(void)
1307{
1308 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1309 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1310 gui.def_norm_pixel = gui.norm_pixel;
1311 gui.def_back_pixel = gui.back_pixel;
1312}
1313
1314/*
1315 * Open the GUI window which was created by a call to gui_mch_init().
1316 */
1317 int
1318gui_mch_open(void)
1319{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001320 // Actually open the window, if not already visible
1321 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001322 if (!IsWindowVisible(s_hwnd))
1323 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1324
1325#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001326 // Init replace string here, so that we keep it when re-opening the
1327 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001328 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1329#endif
1330
1331 return OK;
1332}
1333
1334/*
1335 * Get the position of the top left corner of the window.
1336 */
1337 int
1338gui_mch_get_winpos(int *x, int *y)
1339{
1340 RECT rect;
1341
1342 GetWindowRect(s_hwnd, &rect);
1343 *x = rect.left;
1344 *y = rect.top;
1345 return OK;
1346}
1347
1348/*
1349 * Set the position of the top left corner of the window to the given
1350 * coordinates.
1351 */
1352 void
1353gui_mch_set_winpos(int x, int y)
1354{
1355 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1356 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1357}
1358 void
1359gui_mch_set_text_area_pos(int x, int y, int w, int h)
1360{
1361 static int oldx = 0;
1362 static int oldy = 0;
1363
1364 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1365
1366#ifdef FEAT_TOOLBAR
1367 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1368 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001369 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001370#endif
1371#if defined(FEAT_GUI_TABLINE)
1372 if (showing_tabline)
1373 {
1374 int top = 0;
1375 RECT rect;
1376
1377# ifdef FEAT_TOOLBAR
1378 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001379 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001380# endif
1381 GetClientRect(s_hwnd, &rect);
1382 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1383 }
1384#endif
1385
Bram Moolenaar734a8672019-12-02 22:49:38 +01001386 // When side scroll bar is unshown, the size of window will change.
1387 // then, the text area move left or right. thus client rect should be
1388 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001389 if (oldx != x || oldy != y)
1390 {
1391 InvalidateRect(s_hwnd, NULL, FALSE);
1392 oldx = x;
1393 oldy = y;
1394 }
1395}
1396
1397
1398/*
1399 * Scrollbar stuff:
1400 */
1401
1402 void
1403gui_mch_enable_scrollbar(
1404 scrollbar_T *sb,
1405 int flag)
1406{
1407 ShowScrollBar(sb->id, SB_CTL, flag);
1408
Bram Moolenaar734a8672019-12-02 22:49:38 +01001409 // TODO: When the window is maximized, the size of the window stays the
1410 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1411 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001412}
1413
1414 void
1415gui_mch_set_scrollbar_pos(
1416 scrollbar_T *sb,
1417 int x,
1418 int y,
1419 int w,
1420 int h)
1421{
1422 SetWindowPos(sb->id, NULL, x, y, w, h,
1423 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1424}
1425
Bram Moolenaar203ec772020-07-17 20:43:43 +02001426 int
1427gui_mch_get_scrollbar_xpadding(void)
1428{
1429 RECT rcTxt, rcWnd;
1430 int xpad;
1431
1432 GetWindowRect(s_textArea, &rcTxt);
1433 GetWindowRect(s_hwnd, &rcWnd);
1434 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001435 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1436 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001437 return (xpad < 0) ? 0 : xpad;
1438}
1439
1440 int
1441gui_mch_get_scrollbar_ypadding(void)
1442{
1443 RECT rcTxt, rcWnd;
1444 int ypad;
1445
1446 GetWindowRect(s_textArea, &rcTxt);
1447 GetWindowRect(s_hwnd, &rcWnd);
1448 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001449 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1450 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001451 return (ypad < 0) ? 0 : ypad;
1452}
1453
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001454 void
1455gui_mch_create_scrollbar(
1456 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001457 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001458{
1459 sb->id = CreateWindow(
1460 "SCROLLBAR", "Scrollbar",
1461 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001462 10, // Any value will do for now
1463 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001464 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001465 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001466}
1467
1468/*
1469 * Find the scrollbar with the given hwnd.
1470 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001471 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001472gui_mswin_find_scrollbar(HWND hwnd)
1473{
1474 win_T *wp;
1475
1476 if (gui.bottom_sbar.id == hwnd)
1477 return &gui.bottom_sbar;
1478 FOR_ALL_WINDOWS(wp)
1479 {
1480 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1481 return &wp->w_scrollbars[SBAR_LEFT];
1482 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1483 return &wp->w_scrollbars[SBAR_RIGHT];
1484 }
1485 return NULL;
1486}
1487
K.Takatac81e9bf2022-01-16 14:15:49 +00001488 static void
1489update_scrollbar_size(void)
1490{
1491 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1492 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1493}
1494
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001495/*
K.Takataabe628e2022-01-23 16:25:17 +00001496 * Get the average character size of a font.
1497 */
1498 static void
1499GetAverageFontSize(HDC hdc, SIZE *size)
1500{
1501 // GetTextMetrics() may not return the right value in tmAveCharWidth
1502 // for some fonts. Do our own average computation.
1503 GetTextExtentPoint(hdc,
1504 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1505 52, size);
1506 size->cx = (size->cx / 26 + 1) / 2;
1507}
1508
1509/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001510 * Get the character size of a font.
1511 */
1512 static void
1513GetFontSize(GuiFont font)
1514{
1515 HWND hwnd = GetDesktopWindow();
1516 HDC hdc = GetWindowDC(hwnd);
1517 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001518 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519 TEXTMETRIC tm;
1520
1521 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001522 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001523
K.Takataabe628e2022-01-23 16:25:17 +00001524 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 gui.char_height = tm.tmHeight + p_linespace;
1526
1527 SelectFont(hdc, hfntOld);
1528
1529 ReleaseDC(hwnd, hdc);
1530}
1531
1532/*
1533 * Adjust gui.char_height (after 'linespace' was changed).
1534 */
1535 int
1536gui_mch_adjust_charheight(void)
1537{
1538 GetFontSize(gui.norm_font);
1539 return OK;
1540}
1541
1542 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001543get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001544{
1545 HFONT font = NULL;
1546
Bram Moolenaar734a8672019-12-02 22:49:38 +01001547 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001548 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001549
1550 if (font == NULL)
1551 return NOFONT;
1552
1553 return (GuiFont)font;
1554}
1555
1556 static int
1557pixels_to_points(int pixels, int vertical)
1558{
1559 int points;
1560 HWND hwnd;
1561 HDC hdc;
1562
1563 hwnd = GetDesktopWindow();
1564 hdc = GetWindowDC(hwnd);
1565
1566 points = MulDiv(pixels, 72,
1567 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1568
1569 ReleaseDC(hwnd, hdc);
1570
1571 return points;
1572}
1573
1574 GuiFont
1575gui_mch_get_font(
1576 char_u *name,
1577 int giveErrorIfMissing)
1578{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001579 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001580 GuiFont font = NOFONT;
1581
1582 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001583 {
1584 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001585 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001586 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001588 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001589 return font;
1590}
1591
1592#if defined(FEAT_EVAL) || defined(PROTO)
1593/*
1594 * Return the name of font "font" in allocated memory.
1595 * Don't know how to get the actual name, thus use the provided name.
1596 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001598gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599{
1600 if (name == NULL)
1601 return NULL;
1602 return vim_strsave(name);
1603}
1604#endif
1605
1606 void
1607gui_mch_free_font(GuiFont font)
1608{
1609 if (font)
1610 DeleteObject((HFONT)font);
1611}
1612
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001613/*
1614 * Return the Pixel value (color) for the given color name.
1615 * Return INVALCOLOR for error.
1616 */
1617 guicolor_T
1618gui_mch_get_color(char_u *name)
1619{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001620 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001621
1622 typedef struct SysColorTable
1623 {
1624 char *name;
1625 int color;
1626 } SysColorTable;
1627
1628 static SysColorTable sys_table[] =
1629 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1631 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001632#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001633 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1634#endif
1635 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1636 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1637 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1638 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1639 {"SYS_DESKTOP", COLOR_DESKTOP},
1640 {"SYS_INFOBK", COLOR_INFOBK},
1641 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1642 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643 {"SYS_BTNFACE", COLOR_BTNFACE},
1644 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1645 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1646 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1647 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1648 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1649 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1650 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1651 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1652 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1653 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1654 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1655 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1656 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1657 {"SYS_MENU", COLOR_MENU},
1658 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1659 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1660 {"SYS_WINDOW", COLOR_WINDOW},
1661 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1662 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1663 };
1664
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665 /*
1666 * Try to look up a system colour.
1667 */
K.Takataeeec2542021-06-02 13:28:16 +02001668 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001669 if (STRICMP(name, sys_table[i].name) == 0)
1670 return GetSysColor(sys_table[i].color);
1671
Bram Moolenaarab302212016-04-26 20:59:29 +02001672 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001673}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001674
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001675 guicolor_T
1676gui_mch_get_rgb_color(int r, int g, int b)
1677{
1678 return gui_get_rgb_color_cmn(r, g, b);
1679}
1680
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001681/*
1682 * Return OK if the key with the termcap name "name" is supported.
1683 */
1684 int
1685gui_mch_haskey(char_u *name)
1686{
1687 int i;
1688
1689 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1690 if (name[0] == special_keys[i].vim_code0 &&
1691 name[1] == special_keys[i].vim_code1)
1692 return OK;
1693 return FAIL;
1694}
1695
1696 void
1697gui_mch_beep(void)
1698{
1699 MessageBeep(MB_OK);
1700}
1701/*
1702 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1703 */
1704 void
1705gui_mch_invert_rectangle(
1706 int r,
1707 int c,
1708 int nr,
1709 int nc)
1710{
1711 RECT rc;
1712
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001713#if defined(FEAT_DIRECTX)
1714 if (IS_ENABLE_DIRECTX())
1715 DWriteContext_Flush(s_dwc);
1716#endif
1717
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001718 /*
1719 * Note: InvertRect() excludes right and bottom of rectangle.
1720 */
1721 rc.left = FILL_X(c);
1722 rc.top = FILL_Y(r);
1723 rc.right = rc.left + nc * gui.char_width;
1724 rc.bottom = rc.top + nr * gui.char_height;
1725 InvertRect(s_hdc, &rc);
1726}
1727
1728/*
1729 * Iconify the GUI window.
1730 */
1731 void
1732gui_mch_iconify(void)
1733{
1734 ShowWindow(s_hwnd, SW_MINIMIZE);
1735}
1736
1737/*
1738 * Draw a cursor without focus.
1739 */
1740 void
1741gui_mch_draw_hollow_cursor(guicolor_T color)
1742{
1743 HBRUSH hbr;
1744 RECT rc;
1745
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001746#if defined(FEAT_DIRECTX)
1747 if (IS_ENABLE_DIRECTX())
1748 DWriteContext_Flush(s_dwc);
1749#endif
1750
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751 /*
1752 * Note: FrameRect() excludes right and bottom of rectangle.
1753 */
1754 rc.left = FILL_X(gui.col);
1755 rc.top = FILL_Y(gui.row);
1756 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001757 if (mb_lefthalve(gui.row, gui.col))
1758 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001759 rc.bottom = rc.top + gui.char_height;
1760 hbr = CreateSolidBrush(color);
1761 FrameRect(s_hdc, &rc, hbr);
1762 DeleteBrush(hbr);
1763}
1764/*
1765 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1766 * color "color".
1767 */
1768 void
1769gui_mch_draw_part_cursor(
1770 int w,
1771 int h,
1772 guicolor_T color)
1773{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001774 RECT rc;
1775
1776 /*
1777 * Note: FillRect() excludes right and bottom of rectangle.
1778 */
1779 rc.left =
1780#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001781 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001782 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1783#endif
1784 FILL_X(gui.col);
1785 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1786 rc.right = rc.left + w;
1787 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001788
Bram Moolenaar92467d32017-12-05 13:22:16 +01001789 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001790}
1791
1792
1793/*
1794 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1795 * dead key's nominal character and re-post the original message.
1796 */
1797 static void
1798outputDeadKey_rePost(MSG originalMsg)
1799{
1800 static MSG deadCharExpel;
1801
1802 if (!dead_key)
1803 return;
1804
1805 dead_key = 0;
1806
Bram Moolenaar734a8672019-12-02 22:49:38 +01001807 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001808 deadCharExpel.message = originalMsg.message;
1809 deadCharExpel.hwnd = originalMsg.hwnd;
1810 deadCharExpel.wParam = VK_SPACE;
1811
K.Takata4ac893f2022-01-20 12:44:28 +00001812 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001813
Bram Moolenaar734a8672019-12-02 22:49:38 +01001814 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001815 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1816 originalMsg.lParam);
1817}
1818
1819
1820/*
1821 * Process a single Windows message.
1822 * If one is not available we hang until one is.
1823 */
1824 static void
1825process_message(void)
1826{
1827 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001828 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001829 char_u string[40];
1830 int i;
1831 int modifiers = 0;
1832 int key;
1833#ifdef FEAT_MENU
1834 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1835#endif
1836
K.Takatab7057bd2022-01-21 11:37:07 +00001837 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001838
1839#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001840 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001841 if (msg.message == WM_OLE)
1842 {
1843 char_u *str = (char_u *)msg.lParam;
1844 if (str == NULL || *str == NUL)
1845 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001846 // Message can't be ours, forward it. Fixes problem with Ultramon
1847 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001848 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001849 }
1850 else
1851 {
1852 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001853 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001854 }
1855 return;
1856 }
1857#endif
1858
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001859#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001860 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001861 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862 {
1863 HandleMouseHide(msg.message, msg.lParam);
1864 return;
1865 }
1866#endif
1867
1868 /*
1869 * Check if it's a special key that we recognise. If not, call
1870 * TranslateMessage().
1871 */
1872 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1873 {
1874 vk = (int) msg.wParam;
1875
1876 /*
1877 * Handle dead keys in special conditions in other cases we let Windows
1878 * handle them and do not interfere.
1879 *
1880 * The dead_key flag must be reset on several occasions:
1881 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1882 * consumed at that point (This is when we let Windows combine the
1883 * dead character on its own)
1884 *
1885 * - Before doing something special such as regenerating keypresses to
1886 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001887 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 * immediately to _OnChar() (or _OnSysChar()).
1889 */
1890 if (dead_key)
1891 {
1892 /*
1893 * If a dead key was pressed and the user presses VK_SPACE,
1894 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1895 * with the dead char now, so do nothing special and let Windows
1896 * handle it.
1897 *
1898 * Note that VK_SPACE combines with the dead_key's character and
1899 * only one WM_CHAR will be generated by TranslateMessage(), in
1900 * the two other cases two WM_CHAR will be generated: the dead
1901 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1902 * user expects.
1903 */
1904 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1905 {
1906 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001907 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908 return;
1909 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001910 // In modes where we are not typing, dead keys should behave
1911 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001912 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1913 {
1914 outputDeadKey_rePost(msg);
1915 return;
1916 }
1917 }
1918
Bram Moolenaar734a8672019-12-02 22:49:38 +01001919 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001920 if (vk == VK_CANCEL)
1921 {
1922 trash_input_buf();
1923 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001924 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001925 string[0] = Ctrl_C;
1926 add_to_input_buf(string, 1);
1927 }
1928
1929 for (i = 0; special_keys[i].key_sym != 0; i++)
1930 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001931 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001932 if (special_keys[i].key_sym == vk
1933 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1934 {
1935 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001936 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001937 * is a key that would normally trigger the dead key nominal
1938 * character output (such as a NUMPAD printable character or
1939 * the TAB key, etc...).
1940 */
1941 if (dead_key && (special_keys[i].vim_code0 == 'K'
1942 || vk == VK_TAB || vk == CAR))
1943 {
1944 outputDeadKey_rePost(msg);
1945 return;
1946 }
1947
1948#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001949 // Check for <F10>: Windows selects the menu. When <F10> is
1950 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001951 if (vk == VK_F10
1952 && gui.menu_is_active
1953 && check_map(k10, State, FALSE, TRUE, FALSE,
1954 NULL, NULL) == NULL)
1955 break;
1956#endif
1957 if (GetKeyState(VK_SHIFT) & 0x8000)
1958 modifiers |= MOD_MASK_SHIFT;
1959 /*
1960 * Don't use caps-lock as shift, because these are special keys
1961 * being considered here, and we only want letters to get
1962 * shifted -- webb
1963 */
1964 /*
1965 if (GetKeyState(VK_CAPITAL) & 0x0001)
1966 modifiers ^= MOD_MASK_SHIFT;
1967 */
1968 if (GetKeyState(VK_CONTROL) & 0x8000)
1969 modifiers |= MOD_MASK_CTRL;
1970 if (GetKeyState(VK_MENU) & 0x8000)
1971 modifiers |= MOD_MASK_ALT;
1972
1973 if (special_keys[i].vim_code1 == NUL)
1974 key = special_keys[i].vim_code0;
1975 else
1976 key = TO_SPECIAL(special_keys[i].vim_code0,
1977 special_keys[i].vim_code1);
1978 key = simplify_key(key, &modifiers);
1979 if (key == CSI)
1980 key = K_CSI;
1981
1982 if (modifiers)
1983 {
1984 string[0] = CSI;
1985 string[1] = KS_MODIFIER;
1986 string[2] = modifiers;
1987 add_to_input_buf(string, 3);
1988 }
1989
1990 if (IS_SPECIAL(key))
1991 {
1992 string[0] = CSI;
1993 string[1] = K_SECOND(key);
1994 string[2] = K_THIRD(key);
1995 add_to_input_buf(string, 3);
1996 }
1997 else
1998 {
1999 int len;
2000
Bram Moolenaar734a8672019-12-02 22:49:38 +01002001 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002002 len = char_to_string(key, string, 40, FALSE);
2003 add_to_input_buf(string, len);
2004 }
2005 break;
2006 }
2007 }
2008 if (special_keys[i].key_sym == 0)
2009 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002010 // Some keys need C-S- where they should only need C-.
2011 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2012 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002013 if (vk != 0xff
2014 && (GetKeyState(VK_CONTROL) & 0x8000)
2015 && !(GetKeyState(VK_SHIFT) & 0x8000)
2016 && !(GetKeyState(VK_MENU) & 0x8000))
2017 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002018 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002019 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2020 {
2021 string[0] = Ctrl_HAT;
2022 add_to_input_buf(string, 1);
2023 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002024 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2025 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002026 {
2027 string[0] = Ctrl__;
2028 add_to_input_buf(string, 1);
2029 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002030 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002031 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2032 {
2033 string[0] = Ctrl_AT;
2034 add_to_input_buf(string, 1);
2035 }
2036 else
K.Takata4ac893f2022-01-20 12:44:28 +00002037 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002038 }
2039 else
K.Takata4ac893f2022-01-20 12:44:28 +00002040 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 }
2042 }
2043#ifdef FEAT_MBYTE_IME
2044 else if (msg.message == WM_IME_NOTIFY)
2045 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2046 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002047 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002048 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002049#endif
2050
2051#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002052 // Check for <F10>: Default effect is to select the menu. When <F10> is
2053 // mapped we need to stop it here to avoid strange effects (e.g., for the
2054 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002055 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2056 NULL, NULL) == NULL)
2057#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002058 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059}
2060
2061/*
2062 * Catch up with any queued events. This may put keyboard input into the
2063 * input buffer, call resize call-backs, trigger timers etc. If there is
2064 * nothing in the event queue (& no timers pending), then we return
2065 * immediately.
2066 */
2067 void
2068gui_mch_update(void)
2069{
2070 MSG msg;
2071
2072 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002073 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074 && !vim_is_input_buf_full())
2075 process_message();
2076}
2077
Bram Moolenaar4231da42016-06-02 14:30:04 +02002078 static void
2079remove_any_timer(void)
2080{
2081 MSG msg;
2082
2083 if (s_wait_timer != 0 && !s_timed_out)
2084 {
2085 KillTimer(NULL, s_wait_timer);
2086
Bram Moolenaar734a8672019-12-02 22:49:38 +01002087 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002088 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002089 ;
2090 s_wait_timer = 0;
2091 }
2092}
2093
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094/*
2095 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2096 * from the keyboard.
2097 * wtime == -1 Wait forever.
2098 * wtime == 0 This should never happen.
2099 * wtime > 0 Wait wtime milliseconds for a character.
2100 * Returns OK if a character was found to be available within the given time,
2101 * or FAIL otherwise.
2102 */
2103 int
2104gui_mch_wait_for_chars(int wtime)
2105{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106 int focus;
2107
2108 s_timed_out = FALSE;
2109
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002110 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002111 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002112 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 if (s_busy_processing)
2114 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002115
2116 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002117 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2118 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002119 }
2120
2121 allow_scrollbar = TRUE;
2122
2123 focus = gui.in_focus;
2124 while (!s_timed_out)
2125 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002126 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002127 if (gui.in_focus != focus)
2128 {
2129 if (gui.in_focus)
2130 gui_mch_start_blink();
2131 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002132 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133 focus = gui.in_focus;
2134 }
2135
2136 if (s_need_activate)
2137 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002138 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002139 s_need_activate = FALSE;
2140 }
2141
Bram Moolenaar4231da42016-06-02 14:30:04 +02002142#ifdef FEAT_TIMERS
2143 did_add_timer = FALSE;
2144#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002145#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002146 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002147 for (;;)
2148 {
2149 MSG msg;
2150
2151 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002152# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002153 if (did_add_timer)
2154 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002155# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002156 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002157 {
2158 process_message();
2159 break;
2160 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002161 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002162 // TODO: The 10 msec is a compromise between laggy response
2163 // and consuming more CPU time. Better would be to handle
2164 // channel messages when they arrive.
2165 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002166 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002167 break;
2168 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002169#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002170 // Don't use gui_mch_update() because then we will spin-lock until a
2171 // char arrives, instead we use GetMessage() to hang until an
2172 // event arrives. No need to check for input_buf_full because we are
2173 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002174 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002175#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002176
2177 if (input_available())
2178 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002179 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002180 allow_scrollbar = FALSE;
2181
Bram Moolenaar89c00032019-09-04 13:53:21 +02002182 // Clear pending mouse button, the release event may have been
2183 // taken by the dialog window. But don't do this when getting
2184 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185 if (!s_getting_focus)
2186 s_button_pending = -1;
2187
2188 return OK;
2189 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002190
2191#ifdef FEAT_TIMERS
2192 if (did_add_timer)
2193 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002194 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002195 remove_any_timer();
2196 break;
2197 }
2198#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002199 }
2200 allow_scrollbar = FALSE;
2201 return FAIL;
2202}
2203
2204/*
2205 * Clear a rectangular region of the screen from text pos (row1, col1) to
2206 * (row2, col2) inclusive.
2207 */
2208 void
2209gui_mch_clear_block(
2210 int row1,
2211 int col1,
2212 int row2,
2213 int col2)
2214{
2215 RECT rc;
2216
2217 /*
2218 * Clear one extra pixel at the far right, for when bold characters have
2219 * spilled over to the window border.
2220 * Note: FillRect() excludes right and bottom of rectangle.
2221 */
2222 rc.left = FILL_X(col1);
2223 rc.top = FILL_Y(row1);
2224 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2225 rc.bottom = FILL_Y(row2 + 1);
2226 clear_rect(&rc);
2227}
2228
2229/*
2230 * Clear the whole text window.
2231 */
2232 void
2233gui_mch_clear_all(void)
2234{
2235 RECT rc;
2236
2237 rc.left = 0;
2238 rc.top = 0;
2239 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2240 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2241 clear_rect(&rc);
2242}
2243/*
2244 * Menu stuff.
2245 */
2246
2247 void
2248gui_mch_enable_menu(int flag)
2249{
2250#ifdef FEAT_MENU
2251 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2252#endif
2253}
2254
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002255 void
2256gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002257 int x UNUSED,
2258 int y UNUSED,
2259 int w UNUSED,
2260 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002261{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002262 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263}
2264
2265#if defined(FEAT_MENU) || defined(PROTO)
2266/*
2267 * Make menu item hidden or not hidden
2268 */
2269 void
2270gui_mch_menu_hidden(
2271 vimmenu_T *menu,
2272 int hidden)
2273{
2274 /*
2275 * This doesn't do what we want. Hmm, just grey the menu items for now.
2276 */
2277 /*
2278 if (hidden)
2279 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2280 else
2281 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2282 */
2283 gui_mch_menu_grey(menu, hidden);
2284}
2285
2286/*
2287 * This is called after setting all the menus to grey/hidden or not.
2288 */
2289 void
2290gui_mch_draw_menubar(void)
2291{
2292 DrawMenuBar(s_hwnd);
2293}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002294#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296/*
2297 * Return the RGB value of a pixel as a long.
2298 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002299 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002300gui_mch_get_rgb(guicolor_T pixel)
2301{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002302 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2303 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002304}
2305
2306#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002307/*
2308 * Convert pixels in X to dialog units
2309 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310 static WORD
2311PixelToDialogX(int numPixels)
2312{
2313 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2314}
2315
Bram Moolenaar734a8672019-12-02 22:49:38 +01002316/*
2317 * Convert pixels in Y to dialog units
2318 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319 static WORD
2320PixelToDialogY(int numPixels)
2321{
2322 return (WORD)((numPixels * 8) / s_dlgfntheight);
2323}
2324
Bram Moolenaar734a8672019-12-02 22:49:38 +01002325/*
2326 * Return the width in pixels of the given text in the given DC.
2327 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328 static int
2329GetTextWidth(HDC hdc, char_u *str, int len)
2330{
2331 SIZE size;
2332
2333 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2334 return size.cx;
2335}
2336
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337/*
2338 * Return the width in pixels of the given text in the given DC, taking care
2339 * of 'encoding' to active codepage conversion.
2340 */
2341 static int
2342GetTextWidthEnc(HDC hdc, char_u *str, int len)
2343{
2344 SIZE size;
2345 WCHAR *wstr;
2346 int n;
2347 int wlen = len;
2348
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002349 wstr = enc_to_utf16(str, &wlen);
2350 if (wstr == NULL)
2351 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002353 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2354 vim_free(wstr);
2355 if (n)
2356 return size.cx;
2357 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002360static void get_work_area(RECT *spi_rect);
2361
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362/*
2363 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002364 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2365 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366 */
2367 static BOOL
2368CenterWindow(
2369 HWND hwndChild,
2370 HWND hwndParent)
2371{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002372 HMONITOR mon;
2373 MONITORINFO moninfo;
2374 RECT rChild, rParent, rScreen;
2375 int wChild, hChild, wParent, hParent;
2376 int xNew, yNew;
2377 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378
2379 GetWindowRect(hwndChild, &rChild);
2380 wChild = rChild.right - rChild.left;
2381 hChild = rChild.bottom - rChild.top;
2382
Bram Moolenaar734a8672019-12-02 22:49:38 +01002383 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002385 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002386 else
2387 GetWindowRect(hwndParent, &rParent);
2388 wParent = rParent.right - rParent.left;
2389 hParent = rParent.bottom - rParent.top;
2390
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002391 moninfo.cbSize = sizeof(MONITORINFO);
2392 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2393 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002395 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002397 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002399 hdc = GetDC(hwndChild);
2400 rScreen.left = 0;
2401 rScreen.top = 0;
2402 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2403 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2404 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002405 }
2406
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002407 xNew = rParent.left + ((wParent - wChild) / 2);
2408 if (xNew < rScreen.left)
2409 xNew = rScreen.left;
2410 else if ((xNew + wChild) > rScreen.right)
2411 xNew = rScreen.right - wChild;
2412
2413 yNew = rParent.top + ((hParent - hChild) / 2);
2414 if (yNew < rScreen.top)
2415 yNew = rScreen.top;
2416 else if ((yNew + hChild) > rScreen.bottom)
2417 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002418
2419 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2420 SWP_NOSIZE | SWP_NOZORDER);
2421}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002422#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002424#if defined(FEAT_TOOLBAR) || defined(PROTO)
2425 void
2426gui_mch_show_toolbar(int showit)
2427{
2428 if (s_toolbarhwnd == NULL)
2429 return;
2430
2431 if (showit)
2432 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002433 // Enable unicode support
2434 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2435 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436 ShowWindow(s_toolbarhwnd, SW_SHOW);
2437 }
2438 else
2439 ShowWindow(s_toolbarhwnd, SW_HIDE);
2440}
2441
Bram Moolenaar734a8672019-12-02 22:49:38 +01002442// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002443# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002444
2445#endif
2446
2447#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2448 static void
2449add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2450{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002451 WCHAR *wn;
2452 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002454 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002456 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002457
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002458 infow.cbSize = sizeof(infow);
2459 infow.fMask = MIIM_TYPE | MIIM_ID;
2460 infow.wID = item_id;
2461 infow.fType = MFT_STRING;
2462 infow.dwTypeData = wn;
2463 infow.cch = (UINT)wcslen(wn);
2464 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2465 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466}
2467
2468 static void
2469show_tabline_popup_menu(void)
2470{
2471 HMENU tab_pmenu;
2472 long rval;
2473 POINT pt;
2474
Bram Moolenaar734a8672019-12-02 22:49:38 +01002475 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002476 if (hold_gui_events
2477# ifdef FEAT_CMDWIN
2478 || cmdwin_type != 0
2479# endif
2480 )
2481 return;
2482
2483 tab_pmenu = CreatePopupMenu();
2484 if (tab_pmenu == NULL)
2485 return;
2486
2487 if (first_tabpage->tp_next != NULL)
2488 add_tabline_popup_menu_entry(tab_pmenu,
2489 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2490 add_tabline_popup_menu_entry(tab_pmenu,
2491 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2492 add_tabline_popup_menu_entry(tab_pmenu,
2493 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2494
2495 GetCursorPos(&pt);
2496 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2497 NULL);
2498
2499 DestroyMenu(tab_pmenu);
2500
Bram Moolenaar734a8672019-12-02 22:49:38 +01002501 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502 if (rval > 0)
2503 {
2504 TCHITTESTINFO htinfo;
2505 int idx;
2506
2507 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2508 return;
2509
2510 htinfo.pt.x = pt.x;
2511 htinfo.pt.y = pt.y;
2512 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2513 if (idx == -1)
2514 idx = 0;
2515 else
2516 idx += 1;
2517
2518 send_tabline_menu_event(idx, (int)rval);
2519 }
2520}
2521
2522/*
2523 * Show or hide the tabline.
2524 */
2525 void
2526gui_mch_show_tabline(int showit)
2527{
2528 if (s_tabhwnd == NULL)
2529 return;
2530
2531 if (!showit != !showing_tabline)
2532 {
2533 if (showit)
2534 ShowWindow(s_tabhwnd, SW_SHOW);
2535 else
2536 ShowWindow(s_tabhwnd, SW_HIDE);
2537 showing_tabline = showit;
2538 }
2539}
2540
2541/*
2542 * Return TRUE when tabline is displayed.
2543 */
2544 int
2545gui_mch_showing_tabline(void)
2546{
2547 return s_tabhwnd != NULL && showing_tabline;
2548}
2549
2550/*
2551 * Update the labels of the tabline.
2552 */
2553 void
2554gui_mch_update_tabline(void)
2555{
2556 tabpage_T *tp;
2557 TCITEM tie;
2558 int nr = 0;
2559 int curtabidx = 0;
2560 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002562
2563 if (s_tabhwnd == NULL)
2564 return;
2565
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002566 // Enable unicode support
2567 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002568
2569 tie.mask = TCIF_TEXT;
2570 tie.iImage = -1;
2571
Bram Moolenaar734a8672019-12-02 22:49:38 +01002572 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002573 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2574
Bram Moolenaar734a8672019-12-02 22:49:38 +01002575 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2577 {
2578 if (tp == curtab)
2579 curtabidx = nr;
2580
2581 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2582 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002583 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002584 tie.pszText = "-Empty-";
2585 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2586 tabadded = 1;
2587 }
2588
2589 get_tabline_label(tp, FALSE);
2590 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 wstr = enc_to_utf16(NameBuff, NULL);
2593 if (wstr != NULL)
2594 {
2595 TCITEMW tiw;
2596
2597 tiw.mask = TCIF_TEXT;
2598 tiw.iImage = -1;
2599 tiw.pszText = wstr;
2600 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2601 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002603 }
2604
Bram Moolenaar734a8672019-12-02 22:49:38 +01002605 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002606 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2607 TabCtrl_DeleteItem(s_tabhwnd, nr);
2608
2609 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2610 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2611
Bram Moolenaar734a8672019-12-02 22:49:38 +01002612 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2614 RedrawWindow(s_tabhwnd, NULL, NULL,
2615 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2616
2617 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2618 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2619}
2620
2621/*
2622 * Set the current tab to "nr". First tab is 1.
2623 */
2624 void
2625gui_mch_set_curtab(int nr)
2626{
2627 if (s_tabhwnd == NULL)
2628 return;
2629
2630 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2631 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2632}
2633
2634#endif
2635
2636/*
2637 * ":simalt" command.
2638 */
2639 void
2640ex_simalt(exarg_T *eap)
2641{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002642 char_u *keys = eap->arg;
2643 int fill_typebuf = FALSE;
2644 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002645
2646 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2647 while (*keys)
2648 {
2649 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002650 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002651 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2652 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002653 fill_typebuf = TRUE;
2654 }
2655 if (fill_typebuf)
2656 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002657 // Put a NOP in the typeahead buffer so that the message will get
2658 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002659 key_name[0] = K_SPECIAL;
2660 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002661 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002662 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002663#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002664 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002665#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002666 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002667 }
2668}
2669
2670/*
2671 * Create the find & replace dialogs.
2672 * You can't have both at once: ":find" when replace is showing, destroys
2673 * the replace dialog first, and the other way around.
2674 */
2675#ifdef MSWIN_FIND_REPLACE
2676 static void
2677initialise_findrep(char_u *initial_string)
2678{
2679 int wword = FALSE;
2680 int mcase = !p_ic;
2681 char_u *entry_text;
2682
Bram Moolenaar734a8672019-12-02 22:49:38 +01002683 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002684 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2685
2686 s_findrep_struct.hwndOwner = s_hwnd;
2687 s_findrep_struct.Flags = FR_DOWN;
2688 if (mcase)
2689 s_findrep_struct.Flags |= FR_MATCHCASE;
2690 if (wword)
2691 s_findrep_struct.Flags |= FR_WHOLEWORD;
2692 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002693 {
2694 WCHAR *p = enc_to_utf16(entry_text, NULL);
2695 if (p != NULL)
2696 {
2697 int len = s_findrep_struct.wFindWhatLen - 1;
2698
2699 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2700 s_findrep_struct.lpstrFindWhat[len] = NUL;
2701 vim_free(p);
2702 }
2703 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704 vim_free(entry_text);
2705}
2706#endif
2707
2708 static void
2709set_window_title(HWND hwnd, char *title)
2710{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002711 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712 {
2713 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002714
Bram Moolenaar734a8672019-12-02 22:49:38 +01002715 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2717 if (wbuf != NULL)
2718 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002719 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 }
2722 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002723 else
2724 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002725}
2726
2727 void
2728gui_mch_find_dialog(exarg_T *eap)
2729{
2730#ifdef MSWIN_FIND_REPLACE
2731 if (s_findrep_msg != 0)
2732 {
2733 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2734 DestroyWindow(s_findrep_hwnd);
2735
2736 if (!IsWindow(s_findrep_hwnd))
2737 {
2738 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002739 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740 }
2741
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002742 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743 (void)SetFocus(s_findrep_hwnd);
2744
2745 s_findrep_is_find = TRUE;
2746 }
2747#endif
2748}
2749
2750
2751 void
2752gui_mch_replace_dialog(exarg_T *eap)
2753{
2754#ifdef MSWIN_FIND_REPLACE
2755 if (s_findrep_msg != 0)
2756 {
2757 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2758 DestroyWindow(s_findrep_hwnd);
2759
2760 if (!IsWindow(s_findrep_hwnd))
2761 {
2762 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002763 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002764 }
2765
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002766 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767 (void)SetFocus(s_findrep_hwnd);
2768
2769 s_findrep_is_find = FALSE;
2770 }
2771#endif
2772}
2773
2774
2775/*
2776 * Set visibility of the pointer.
2777 */
2778 void
2779gui_mch_mousehide(int hide)
2780{
2781 if (hide != gui.pointer_hidden)
2782 {
2783 ShowCursor(!hide);
2784 gui.pointer_hidden = hide;
2785 }
2786}
2787
2788#ifdef FEAT_MENU
2789 static void
2790gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2791{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002792 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002793 gui_mch_mousehide(FALSE);
2794
2795 (void)TrackPopupMenu(
2796 (HMENU)menu->submenu_id,
2797 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2798 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002799 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002800 s_hwnd,
2801 NULL);
2802 /*
2803 * NOTE: The pop-up menu can eat the mouse up event.
2804 * We deal with this in normal.c.
2805 */
2806}
2807#endif
2808
2809/*
2810 * Got a message when the system will go down.
2811 */
2812 static void
2813_OnEndSession(void)
2814{
2815 getout_preserve_modified(1);
2816}
2817
2818/*
2819 * Get this message when the user clicks on the cross in the top right corner
2820 * of a Windows95 window.
2821 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002822 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002823_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824{
2825 gui_shell_closed();
2826}
2827
2828/*
2829 * Get a message when the window is being destroyed.
2830 */
2831 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002832_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002833{
2834 if (!destroying)
2835 _OnClose(hwnd);
2836}
2837
2838 static void
2839_OnPaint(
2840 HWND hwnd)
2841{
2842 if (!IsMinimized(hwnd))
2843 {
2844 PAINTSTRUCT ps;
2845
Bram Moolenaar734a8672019-12-02 22:49:38 +01002846 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002847 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848
Bram Moolenaar734a8672019-12-02 22:49:38 +01002849 // prevent multi-byte characters from misprinting on an invalid
2850 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851 if (has_mbyte)
2852 {
2853 RECT rect;
2854
2855 GetClientRect(hwnd, &rect);
2856 ps.rcPaint.left = rect.left;
2857 ps.rcPaint.right = rect.right;
2858 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859
2860 if (!IsRectEmpty(&ps.rcPaint))
2861 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2863 ps.rcPaint.right - ps.rcPaint.left + 1,
2864 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2865 }
2866
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867 EndPaint(hwnd, &ps);
2868 }
2869}
2870
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871 static void
2872_OnSize(
2873 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002874 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 int cx,
2876 int cy)
2877{
K.Takatac81e9bf2022-01-16 14:15:49 +00002878 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 {
2880 gui_resize_shell(cx, cy);
2881
Bram Moolenaar734a8672019-12-02 22:49:38 +01002882 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002883 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002884 }
2885}
2886
2887 static void
2888_OnSetFocus(
2889 HWND hwnd,
2890 HWND hwndOldFocus)
2891{
2892 gui_focus_change(TRUE);
2893 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002894 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002895}
2896
2897 static void
2898_OnKillFocus(
2899 HWND hwnd,
2900 HWND hwndNewFocus)
2901{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002902 if (destroying)
2903 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 gui_focus_change(FALSE);
2905 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002906 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907}
2908
2909/*
2910 * Get a message when the user switches back to vim
2911 */
2912 static LRESULT
2913_OnActivateApp(
2914 HWND hwnd,
2915 BOOL fActivate,
2916 DWORD dwThreadId)
2917{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002918 // we call gui_focus_change() in _OnSetFocus()
2919 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002920 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921}
2922
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923 void
2924gui_mch_destroy_scrollbar(scrollbar_T *sb)
2925{
2926 DestroyWindow(sb->id);
2927}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928
2929/*
2930 * Get current mouse coordinates in text window.
2931 */
2932 void
2933gui_mch_getmouse(int *x, int *y)
2934{
2935 RECT rct;
2936 POINT mp;
2937
2938 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002939 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002940 *x = (int)(mp.x - rct.left);
2941 *y = (int)(mp.y - rct.top);
2942}
2943
2944/*
2945 * Move mouse pointer to character at (x, y).
2946 */
2947 void
2948gui_mch_setmouse(int x, int y)
2949{
2950 RECT rct;
2951
2952 (void)GetWindowRect(s_textArea, &rct);
2953 (void)SetCursorPos(x + gui.border_offset + rct.left,
2954 y + gui.border_offset + rct.top);
2955}
2956
2957 static void
2958gui_mswin_get_valid_dimensions(
2959 int w,
2960 int h,
2961 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002962 int *valid_h,
2963 int *cols,
2964 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002965{
2966 int base_width, base_height;
2967
2968 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002969 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2970 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002971 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002972 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2973 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2974 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2975 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002976 *cols = (w - base_width) / gui.char_width;
2977 *rows = (h - base_height) / gui.char_height;
2978 *valid_w = base_width + *cols * gui.char_width;
2979 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980}
2981
2982 void
2983gui_mch_flash(int msec)
2984{
2985 RECT rc;
2986
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002987#if defined(FEAT_DIRECTX)
2988 if (IS_ENABLE_DIRECTX())
2989 DWriteContext_Flush(s_dwc);
2990#endif
2991
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002992 /*
2993 * Note: InvertRect() excludes right and bottom of rectangle.
2994 */
2995 rc.left = 0;
2996 rc.top = 0;
2997 rc.right = gui.num_cols * gui.char_width;
2998 rc.bottom = gui.num_rows * gui.char_height;
2999 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003000 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003001
Bram Moolenaar734a8672019-12-02 22:49:38 +01003002 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003003
3004 InvertRect(s_hdc, &rc);
3005}
3006
3007/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003008 * Check if the specified point is on-screen. (multi-monitor aware)
3009 */
3010 static BOOL
3011is_point_onscreen(int x, int y)
3012{
3013 POINT pt = {x, y};
3014
3015 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3016}
3017
3018/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003019 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003020 *
3021 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3022 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3023 * only works when the whole window is on-screen.
3024 */
3025 static BOOL
3026is_window_onscreen(HWND hwnd)
3027{
3028 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003029 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003030
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003031 GetClientRect(hwnd, &rc);
3032 p1.x = rc.left;
3033 p1.y = rc.top;
3034 p2.x = rc.right - 1;
3035 p2.y = rc.bottom - 1;
3036 ClientToScreen(hwnd, &p1);
3037 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003038
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003039 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003040 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003041 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003042 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003043 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003044 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003045 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003046 return FALSE;
3047 return TRUE;
3048}
3049
3050/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003051 * Return flags used for scrolling.
3052 * The SW_INVALIDATE is required when part of the window is covered or
3053 * off-screen. Refer to MS KB Q75236.
3054 */
3055 static int
3056get_scroll_flags(void)
3057{
3058 HWND hwnd;
3059 RECT rcVim, rcOther, rcDest;
3060
Bram Moolenaar185577e2020-10-29 20:08:21 +01003061 // Check if the window is (partly) off-screen.
3062 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003063 return SW_INVALIDATE;
3064
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003065 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003066 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003067 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3068 if (IsWindowVisible(hwnd))
3069 {
3070 GetWindowRect(hwnd, &rcOther);
3071 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3072 return SW_INVALIDATE;
3073 }
3074 return 0;
3075}
3076
3077/*
3078 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3079 * may not be scrolled out properly.
3080 * For gVim, when _OnScroll() is repeated, the character at the
3081 * previous cursor position may be left drawn after scroll.
3082 * The problem can be avoided by calling GetPixel() to get a pixel in
3083 * the region before ScrollWindowEx().
3084 */
3085 static void
3086intel_gpu_workaround(void)
3087{
3088 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3089}
3090
3091/*
3092 * Delete the given number of lines from the given row, scrolling up any
3093 * text further down within the scroll region.
3094 */
3095 void
3096gui_mch_delete_lines(
3097 int row,
3098 int num_lines)
3099{
3100 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003101
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003102 rc.left = FILL_X(gui.scroll_region_left);
3103 rc.right = FILL_X(gui.scroll_region_right + 1);
3104 rc.top = FILL_Y(row);
3105 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3106
Bram Moolenaar92467d32017-12-05 13:22:16 +01003107#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003108 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003109 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003110 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003111 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003112 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003113#endif
3114 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003115#if defined(FEAT_DIRECTX)
3116 if (IS_ENABLE_DIRECTX())
3117 DWriteContext_Flush(s_dwc);
3118#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003119 intel_gpu_workaround();
3120 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003121 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003122 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003123 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124
Bram Moolenaar734a8672019-12-02 22:49:38 +01003125 // This seems to be required to avoid the cursor disappearing when
3126 // scrolling such that the cursor ends up in the top-left character on
3127 // the screen... But why? (Webb)
3128 // It's probably fixed by disabling drawing the cursor while scrolling.
3129 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130
3131 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3132 gui.scroll_region_left,
3133 gui.scroll_region_bot, gui.scroll_region_right);
3134}
3135
3136/*
3137 * Insert the given number of lines before the given row, scrolling down any
3138 * following text within the scroll region.
3139 */
3140 void
3141gui_mch_insert_lines(
3142 int row,
3143 int num_lines)
3144{
3145 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003146
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003147 rc.left = FILL_X(gui.scroll_region_left);
3148 rc.right = FILL_X(gui.scroll_region_right + 1);
3149 rc.top = FILL_Y(row);
3150 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003151
3152#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003153 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003154 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003155 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003156 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003157 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003158#endif
3159 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003160#if defined(FEAT_DIRECTX)
3161 if (IS_ENABLE_DIRECTX())
3162 DWriteContext_Flush(s_dwc);
3163#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003164 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003165 // The SW_INVALIDATE is required when part of the window is covered or
3166 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003167 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003168 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003169 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 gui_clear_block(row, gui.scroll_region_left,
3173 row + num_lines - 1, gui.scroll_region_right);
3174}
3175
3176
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003178gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003179{
3180#if defined(FEAT_DIRECTX)
3181 DWriteContext_Close(s_dwc);
3182 DWrite_Final();
3183 s_dwc = NULL;
3184#endif
3185
3186 ReleaseDC(s_textArea, s_hdc);
3187 DeleteObject(s_brush);
3188
3189#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003190 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3192#endif
3193
Bram Moolenaar734a8672019-12-02 22:49:38 +01003194 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195 if (s_hwnd != NULL)
3196 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003197 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 DestroyWindow(s_hwnd);
3199 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200}
3201
3202 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204{
3205 char *p;
3206 char *res;
3207 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003208 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003209 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003210 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003212 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3213 if (font_name == NULL)
3214 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003216 quality_name = quality_id2name((int)lf.lfQuality);
3217
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003218 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003219 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003220 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221 if (res != NULL)
3222 {
3223 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003224 // make a normal font string out of the lf thing:
3225 points = pixels_to_points(
3226 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3227 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3228 sprintf((char *)p, "%s:h%d", font_name, points);
3229 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003230 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231 while (*p)
3232 {
3233 if (*p == ' ')
3234 *p = '_';
3235 ++p;
3236 }
3237 if (lf.lfItalic)
3238 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003239 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 STRCAT(p, ":b");
3241 if (lf.lfUnderline)
3242 STRCAT(p, ":u");
3243 if (lf.lfStrikeOut)
3244 STRCAT(p, ":s");
3245 if (charset_name != NULL)
3246 {
3247 STRCAT(p, ":c");
3248 STRCAT(p, charset_name);
3249 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003250 if (quality_name != NULL)
3251 {
3252 STRCAT(p, ":q");
3253 STRCAT(p, quality_name);
3254 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255 }
3256
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003257 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003258 return (char_u *)res;
3259}
3260
3261
3262#ifdef FEAT_MBYTE_IME
3263/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003264 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003265 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 */
3267 static void
3268update_im_font(void)
3269{
K.Takatac81e9bf2022-01-16 14:15:49 +00003270 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271
3272 if (p_guifontwide != NULL && *p_guifontwide != NUL
3273 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003274 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 norm_logfont = lf_wide;
3276 else
3277 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003278
3279 lf = norm_logfont;
3280 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3281 // Work around when PerMonitorV2 is not enabled in the process level.
3282 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3283 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284}
3285#endif
3286
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003287/*
3288 * Handler of gui.wide_font (p_guifontwide) changed notification.
3289 */
3290 void
3291gui_mch_wide_font_changed(void)
3292{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003293 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003294
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003295#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003297#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003298
3299 gui_mch_free_font(gui.wide_ital_font);
3300 gui.wide_ital_font = NOFONT;
3301 gui_mch_free_font(gui.wide_bold_font);
3302 gui.wide_bold_font = NOFONT;
3303 gui_mch_free_font(gui.wide_boldital_font);
3304 gui.wide_boldital_font = NOFONT;
3305
3306 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003307 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308 {
3309 if (!lf.lfItalic)
3310 {
3311 lf.lfItalic = TRUE;
3312 gui.wide_ital_font = get_font_handle(&lf);
3313 lf.lfItalic = FALSE;
3314 }
3315 if (lf.lfWeight < FW_BOLD)
3316 {
3317 lf.lfWeight = FW_BOLD;
3318 gui.wide_bold_font = get_font_handle(&lf);
3319 if (!lf.lfItalic)
3320 {
3321 lf.lfItalic = TRUE;
3322 gui.wide_boldital_font = get_font_handle(&lf);
3323 }
3324 }
3325 }
3326}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327
3328/*
3329 * Initialise vim to use the font with the given name.
3330 * Return FAIL if the font could not be loaded, OK otherwise.
3331 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003333gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334{
K.Takatac81e9bf2022-01-16 14:15:49 +00003335 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336 GuiFont font = NOFONT;
3337 char_u *p;
3338
Bram Moolenaar734a8672019-12-02 22:49:38 +01003339 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003341 {
3342 lfOrig = lf;
3343 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003345 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 if (font == NOFONT)
3347 return FAIL;
3348
3349 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003350 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003351#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 norm_logfont = lf;
3353 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003354 if (!s_in_dpichanged)
3355 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003356#endif
3357 gui_mch_free_font(gui.norm_font);
3358 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003359 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003360 GetFontSize(font);
3361
K.Takatac81e9bf2022-01-16 14:15:49 +00003362 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003363 if (p != NULL)
3364 {
3365 hl_set_font_name(p);
3366
Bram Moolenaar734a8672019-12-02 22:49:38 +01003367 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003368 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3369 {
3370 vim_free(p_guifont);
3371 p_guifont = p;
3372 }
3373 else
3374 vim_free(p);
3375 }
3376
3377 gui_mch_free_font(gui.ital_font);
3378 gui.ital_font = NOFONT;
3379 gui_mch_free_font(gui.bold_font);
3380 gui.bold_font = NOFONT;
3381 gui_mch_free_font(gui.boldital_font);
3382 gui.boldital_font = NOFONT;
3383
3384 if (!lf.lfItalic)
3385 {
3386 lf.lfItalic = TRUE;
3387 gui.ital_font = get_font_handle(&lf);
3388 lf.lfItalic = FALSE;
3389 }
3390 if (lf.lfWeight < FW_BOLD)
3391 {
3392 lf.lfWeight = FW_BOLD;
3393 gui.bold_font = get_font_handle(&lf);
3394 if (!lf.lfItalic)
3395 {
3396 lf.lfItalic = TRUE;
3397 gui.boldital_font = get_font_handle(&lf);
3398 }
3399 }
3400
3401 return OK;
3402}
3403
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404/*
3405 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003406 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407 */
3408 int
3409gui_mch_maximized(void)
3410{
3411 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003412 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413
3414 wp.length = sizeof(WINDOWPLACEMENT);
3415 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003416 {
3417 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003418 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003419 && wp.flags == WPF_RESTORETOMAXIMIZED))
3420 return TRUE;
3421 if (wp.showCmd == SW_SHOWMINIMIZED)
3422 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003424 // Assume the window is snapped when the sizes from two APIs differ.
3425 GetWindowRect(s_hwnd, &rc);
3426 if ((rc.right - rc.left !=
3427 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3428 || (rc.bottom - rc.top !=
3429 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3430 return TRUE;
3431 }
3432 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433}
3434
3435/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003436 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3437 * is set. Compute the new Rows and Columns. This is like resizing the
3438 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003439 */
3440 void
3441gui_mch_newfont(void)
3442{
3443 RECT rect;
3444
3445 GetWindowRect(s_hwnd, &rect);
3446 if (win_socket_id == 0)
3447 {
3448 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003449 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3450 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003452 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3453 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3454 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3455 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456 }
3457 else
3458 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003459 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003461 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462 }
3463}
3464
3465/*
3466 * Set the window title
3467 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003468 void
3469gui_mch_settitle(
3470 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003471 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472{
3473 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3474}
3475
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003476#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003477// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3478// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479static LPCSTR mshape_idcs[] =
3480{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003481 IDC_ARROW, // arrow
3482 MAKEINTRESOURCE(0), // blank
3483 IDC_IBEAM, // beam
3484 IDC_SIZENS, // updown
3485 IDC_SIZENS, // udsizing
3486 IDC_SIZEWE, // leftright
3487 IDC_SIZEWE, // lrsizing
3488 IDC_WAIT, // busy
3489 IDC_NO, // no
3490 IDC_ARROW, // crosshair
3491 IDC_ARROW, // hand1
3492 IDC_ARROW, // hand2
3493 IDC_ARROW, // pencil
3494 IDC_ARROW, // question
3495 IDC_ARROW, // right-arrow
3496 IDC_UPARROW, // up-arrow
3497 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498};
3499
3500 void
3501mch_set_mouse_shape(int shape)
3502{
3503 LPCSTR idc;
3504
3505 if (shape == MSHAPE_HIDE)
3506 ShowCursor(FALSE);
3507 else
3508 {
3509 if (shape >= MSHAPE_NUMBERED)
3510 idc = IDC_ARROW;
3511 else
3512 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003513 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 if (!p_mh)
3515 {
3516 POINT mp;
3517
Bram Moolenaar734a8672019-12-02 22:49:38 +01003518 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003519 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520 (void)SetCursorPos(mp.x, mp.y);
3521 ShowCursor(TRUE);
3522 }
3523 }
3524}
3525#endif
3526
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003527#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003528/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003529 * Wide version of convert_filter().
3530 */
3531 static WCHAR *
3532convert_filterW(char_u *s)
3533{
3534 char_u *tmp;
3535 int len;
3536 WCHAR *res;
3537
3538 tmp = convert_filter(s);
3539 if (tmp == NULL)
3540 return NULL;
3541 len = (int)STRLEN(s) + 3;
3542 res = enc_to_utf16(tmp, &len);
3543 vim_free(tmp);
3544 return res;
3545}
3546
3547/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003548 * Pop open a file browser and return the file selected, in allocated memory,
3549 * or NULL if Cancel is hit.
3550 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3551 * title - Title message for the file browser dialog.
3552 * dflt - Default name of file.
3553 * ext - Default extension to be added to files without extensions.
3554 * initdir - directory in which to open the browser (NULL = current dir)
3555 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003557 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003558gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003559 int saving,
3560 char_u *title,
3561 char_u *dflt,
3562 char_u *ext,
3563 char_u *initdir,
3564 char_u *filter)
3565{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003566 // We always use the wide function. This means enc_to_utf16() must work,
3567 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003568 OPENFILENAMEW fileStruct;
3569 WCHAR fileBuf[MAXPATHL];
3570 WCHAR *wp;
3571 int i;
3572 WCHAR *titlep = NULL;
3573 WCHAR *extp = NULL;
3574 WCHAR *initdirp = NULL;
3575 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003576 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003577 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578
3579 if (dflt == NULL)
3580 fileBuf[0] = NUL;
3581 else
3582 {
3583 wp = enc_to_utf16(dflt, NULL);
3584 if (wp == NULL)
3585 fileBuf[0] = NUL;
3586 else
3587 {
3588 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3589 fileBuf[i] = wp[i];
3590 fileBuf[i] = NUL;
3591 vim_free(wp);
3592 }
3593 }
3594
Bram Moolenaar734a8672019-12-02 22:49:38 +01003595 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596 filterp = convert_filterW(filter);
3597
Bram Moolenaara80faa82020-04-12 19:37:17 +02003598 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003599# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003600 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003601 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003602# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003603 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003604# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605
3606 if (title != NULL)
3607 titlep = enc_to_utf16(title, NULL);
3608 fileStruct.lpstrTitle = titlep;
3609
3610 if (ext != NULL)
3611 extp = enc_to_utf16(ext, NULL);
3612 fileStruct.lpstrDefExt = extp;
3613
3614 fileStruct.lpstrFile = fileBuf;
3615 fileStruct.nMaxFile = MAXPATHL;
3616 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003617 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3618 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003619 if (initdir != NULL && *initdir != NUL)
3620 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003621 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622 initdirp = enc_to_utf16(initdir, NULL);
3623 if (initdirp != NULL)
3624 {
3625 for (wp = initdirp; *wp != NUL; ++wp)
3626 if (*wp == '/')
3627 *wp = '\\';
3628 }
3629 fileStruct.lpstrInitialDir = initdirp;
3630 }
3631
3632 /*
3633 * TODO: Allow selection of multiple files. Needs another arg to this
3634 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3635 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3636 * files that don't exist yet, so I haven't put it in. What about
3637 * OFN_PATHMUSTEXIST?
3638 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3639 */
3640 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003641# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003642 if (curbuf->b_p_bin)
3643 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003644# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003645 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003646 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003648 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003649
3650 vim_free(filterp);
3651 vim_free(initdirp);
3652 vim_free(titlep);
3653 vim_free(extp);
3654
K.Takata14b8d6a2022-01-20 15:05:22 +00003655 if (!ret)
3656 return NULL;
3657
3658 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003660 if (p == NULL)
3661 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
Bram Moolenaar734a8672019-12-02 22:49:38 +01003663 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664 SetFocus(s_hwnd);
3665
Bram Moolenaar734a8672019-12-02 22:49:38 +01003666 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003667 q = vim_strsave(shorten_fname1(p));
3668 vim_free(p);
3669 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671
3672
3673/*
3674 * Convert the string s to the proper format for a filter string by replacing
3675 * the \t and \n delimiters with \0.
3676 * Returns the converted string in allocated memory.
3677 *
3678 * Keep in sync with convert_filterW() above!
3679 */
3680 static char_u *
3681convert_filter(char_u *s)
3682{
3683 char_u *res;
3684 unsigned s_len = (unsigned)STRLEN(s);
3685 unsigned i;
3686
3687 res = alloc(s_len + 3);
3688 if (res != NULL)
3689 {
3690 for (i = 0; i < s_len; ++i)
3691 if (s[i] == '\t' || s[i] == '\n')
3692 res[i] = '\0';
3693 else
3694 res[i] = s[i];
3695 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003696 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003697 res[s_len + 1] = NUL;
3698 res[s_len + 2] = NUL;
3699 }
3700 return res;
3701}
3702
3703/*
3704 * Select a directory.
3705 */
3706 char_u *
3707gui_mch_browsedir(char_u *title, char_u *initdir)
3708{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003709 // We fake this: Use a filter that doesn't select anything and a default
3710 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003711 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3712 initdir, (char_u *)_("Directory\t*.nothing\n"));
3713}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003714#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 static void
3717_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003718 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003719 HDROP hDrop)
3720{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003721#define BUFPATHLEN _MAX_PATH
3722#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003724 char szFile[BUFPATHLEN];
3725 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3726 UINT i;
3727 char_u **fnames;
3728 POINT pt;
3729 int_u modifiers = 0;
3730
Bram Moolenaar734a8672019-12-02 22:49:38 +01003731 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732
Bram Moolenaar734a8672019-12-02 22:49:38 +01003733 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003734 DragQueryPoint(hDrop, &pt);
3735 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3736
3737 reset_VIsual();
3738
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003739 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003740
3741 if (fnames != NULL)
3742 for (i = 0; i < cFiles; ++i)
3743 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003744 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3745 fnames[i] = utf16_to_enc(wszFile, NULL);
3746 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003747 {
3748 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3749 fnames[i] = vim_strsave((char_u *)szFile);
3750 }
3751 }
3752
3753 DragFinish(hDrop);
3754
3755 if (fnames != NULL)
3756 {
3757 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3758 modifiers |= MOUSE_SHIFT;
3759 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3760 modifiers |= MOUSE_CTRL;
3761 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3762 modifiers |= MOUSE_ALT;
3763
3764 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3765
3766 s_need_activate = TRUE;
3767 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768}
3769
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 static int
3771_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003772 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 HWND hwndCtl,
3774 UINT code,
3775 int pos)
3776{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003777 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778 scrollbar_T *sb, *sb_info;
3779 long val;
3780 int dragging = FALSE;
3781 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782 SCROLLINFO si;
3783
3784 si.cbSize = sizeof(si);
3785 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786
3787 sb = gui_mswin_find_scrollbar(hwndCtl);
3788 if (sb == NULL)
3789 return 0;
3790
Bram Moolenaar734a8672019-12-02 22:49:38 +01003791 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003792 {
3793 /*
3794 * Careful: need to get scrollbar info out of first (left) scrollbar
3795 * for window, but keep real scrollbar too because we must pass it to
3796 * gui_drag_scrollbar().
3797 */
3798 sb_info = &sb->wp->w_scrollbars[0];
3799 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003800 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801 sb_info = sb;
3802 val = sb_info->value;
3803
3804 switch (code)
3805 {
3806 case SB_THUMBTRACK:
3807 val = pos;
3808 dragging = TRUE;
3809 if (sb->scroll_shift > 0)
3810 val <<= sb->scroll_shift;
3811 break;
3812 case SB_LINEDOWN:
3813 val++;
3814 break;
3815 case SB_LINEUP:
3816 val--;
3817 break;
3818 case SB_PAGEDOWN:
3819 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3820 break;
3821 case SB_PAGEUP:
3822 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3823 break;
3824 case SB_TOP:
3825 val = 0;
3826 break;
3827 case SB_BOTTOM:
3828 val = sb_info->max;
3829 break;
3830 case SB_ENDSCROLL:
3831 if (prev_code == SB_THUMBTRACK)
3832 {
3833 /*
3834 * "pos" only gives us 16-bit data. In case of large file,
3835 * use GetScrollPos() which returns 32-bit. Unfortunately it
3836 * is not valid while the scrollbar is being dragged.
3837 */
3838 val = GetScrollPos(hwndCtl, SB_CTL);
3839 if (sb->scroll_shift > 0)
3840 val <<= sb->scroll_shift;
3841 }
3842 break;
3843
3844 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003845 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003846 return 0;
3847 }
3848 prev_code = code;
3849
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3851 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852
3853 /*
3854 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3855 */
3856 if (sb->wp != NULL)
3857 {
3858 scrollbar_T *sba = sb->wp->w_scrollbars;
3859 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3860
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 }
3863
Bram Moolenaar734a8672019-12-02 22:49:38 +01003864 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003865 s_busy_processing = TRUE;
3866
Bram Moolenaar734a8672019-12-02 22:49:38 +01003867 // When "allow_scrollbar" is FALSE still need to remember the new
3868 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003869 dont_scroll = !allow_scrollbar;
3870
Bram Moolenaara338adc2018-01-31 20:51:47 +01003871 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003873 mch_enable_flush();
3874 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875
3876 s_busy_processing = FALSE;
3877 dont_scroll = dont_scroll_save;
3878
3879 return 0;
3880}
3881
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883#ifdef FEAT_XPM_W32
3884# include "xpm_w32.h"
3885#endif
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
Bram Moolenaar734a8672019-12-02 22:49:38 +01003888// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889#define TEAROFF_PADDING_X 2
3890#define TEAROFF_BUTTON_PAD_X 8
3891#define TEAROFF_MIN_WIDTH 200
3892#define TEAROFF_SUBMENU_LABEL ">>"
3893#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3894
3895
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003896#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897# define ID_BEVAL_TOOLTIP 200
3898# define BEVAL_TEXT_LEN MAXPATHL
3899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003901static UINT_PTR beval_timer_id = 0;
3902static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003903#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003904
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003905
Bram Moolenaar734a8672019-12-02 22:49:38 +01003906// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908#ifdef FEAT_MENU
3909static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912/*
3913 * Use the system font for dialogs and tear-off menus. Remove this line to
3914 * use DLG_FONT_NAME.
3915 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003916#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#define VIM_CLASSW L"Vim"
3920
Bram Moolenaar734a8672019-12-02 22:49:38 +01003921// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3922// thus there should be room for every dialog. For tearoffs it's made bigger
3923// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924#define DLG_ALLOC_SIZE 16 * 1024
3925
3926/*
3927 * stuff for dialogs, menus, tearoffs etc.
3928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929static PWORD
3930add_dialog_element(
3931 PWORD p,
3932 DWORD lStyle,
3933 WORD x,
3934 WORD y,
3935 WORD w,
3936 WORD h,
3937 WORD Id,
3938 WORD clss,
3939 const char *caption);
3940static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003941static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003942#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945static void get_dialog_font_metrics(void);
3946
3947static int dialog_default_button = -1;
3948
Bram Moolenaar734a8672019-12-02 22:49:38 +01003949// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#ifdef FEAT_TOOLBAR
3953static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00003954static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003955static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00003957#else
3958# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#endif
3960
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003961#ifdef FEAT_GUI_TABLINE
3962static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003963static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003964#endif
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#ifdef FEAT_MBYTE_IME
3967static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
3968static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
3969#endif
3970#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
3971# ifdef NOIME
3972typedef struct tagCOMPOSITIONFORM {
3973 DWORD dwStyle;
3974 POINT ptCurrentPos;
3975 RECT rcArea;
3976} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
3977typedef HANDLE HIMC;
3978# endif
3979
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003980static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003981static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
3982static HIMC (WINAPI *pImmGetContext)(HWND);
3983static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
3984static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
3985static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
3986static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003987static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
3988static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003989static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
3990static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00003991static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992static void dyn_imm_load(void);
3993#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994# define pImmGetCompositionStringW ImmGetCompositionStringW
3995# define pImmGetContext ImmGetContext
3996# define pImmAssociateContext ImmAssociateContext
3997# define pImmReleaseContext ImmReleaseContext
3998# define pImmGetOpenStatus ImmGetOpenStatus
3999# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004000# define pImmGetCompositionFontW ImmGetCompositionFontW
4001# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002# define pImmSetCompositionWindow ImmSetCompositionWindow
4003# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004004# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif
4006
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#ifdef FEAT_MENU
4008/*
4009 * Figure out how high the menu bar is at the moment.
4010 */
4011 static int
4012gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004013 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 static int old_menu_height = -1;
4016
4017 RECT rc1, rc2;
4018 int num;
4019 int menu_height;
4020
4021 if (gui.menu_is_active)
4022 num = GetMenuItemCount(s_menuBar);
4023 else
4024 num = 0;
4025
4026 if (num == 0)
4027 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004028 else if (IsMinimized(s_hwnd))
4029 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004030 // The height of the menu cannot be determined while the window is
4031 // minimized. Take the previous height if the menu is changed in that
4032 // state, to avoid that Vim's vertical window size accidentally
4033 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004034 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 else
4037 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004038 /*
4039 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4040 * seem to have been set yet, so menu wraps in default window
4041 * width which is very narrow. Instead just return height of a
4042 * single menu item. Will still be wrong when the menu really
4043 * should wrap over more than one line.
4044 */
4045 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4046 if (gui.starting)
4047 menu_height = rc1.bottom - rc1.top + 1;
4048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004050 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4051 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053 }
4054
4055 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004056 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004057 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 return menu_height;
4060}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004061#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063
4064/*
4065 * Setup for the Intellimouse
4066 */
4067 static void
4068init_mouse_wheel(void)
4069{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004070 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
Bram Moolenaar734a8672019-12-02 22:49:38 +01004072 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004073 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4074 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075}
4076
4077
Bram Moolenaarae20f342019-11-05 21:09:23 +01004078/*
4079 * Intellimouse wheel handler.
4080 * Treat a mouse wheel event as if it were a scroll request.
4081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 static void
4083_OnMouseWheel(
4084 HWND hwnd,
4085 short zDelta)
4086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 int i;
4088 int size;
4089 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004090 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (mouse_scroll_lines == 0)
4093 init_mouse_wheel();
4094
Bram Moolenaarae20f342019-11-05 21:09:23 +01004095 wp = gui_mouse_window(FIND_POPUP);
4096
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004097#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004098 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004099 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004100 cmdarg_T cap;
4101 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004102
Bram Moolenaarae20f342019-11-05 21:09:23 +01004103 // Mouse hovers over popup window, scroll it if possible.
4104 mouse_row = wp->w_winrow;
4105 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004106 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004107 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4108 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4109 clear_oparg(&oa);
4110 cap.oap = &oa;
4111 nv_mousescroll(&cap);
4112 update_screen(0);
4113 setcursor();
4114 out_flush();
4115 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004116 }
4117#endif
4118
Bram Moolenaarae20f342019-11-05 21:09:23 +01004119 if (wp == NULL || !p_scf)
4120 wp = curwin;
4121
4122 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4123 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4124 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4125 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4126 else
4127 return;
4128 size = wp->w_height;
4129
Bram Moolenaara338adc2018-01-31 20:51:47 +01004130 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (mouse_scroll_lines > 0
4132 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4133 {
4134 for (i = mouse_scroll_lines; i > 0; --i)
4135 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4136 }
4137 else
4138 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004139 mch_enable_flush();
4140 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141}
4142
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004143#ifdef USE_SYSMENU_FONT
4144/*
4145 * Get Menu Font.
4146 * Return OK or FAIL.
4147 */
4148 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004149gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004150{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004151 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004152
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004153 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4154 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004155 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004156 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004157 &nm,
4158 0))
4159 return FAIL;
4160 *lf = nm.lfMenuFont;
4161 return OK;
4162}
4163#endif
4164
4165
4166#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4167/*
4168 * Set the GUI tabline font to the system menu font
4169 */
4170 static void
4171set_tabline_font(void)
4172{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004173 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004174 HFONT font;
4175 HWND hwnd;
4176 HDC hdc;
4177 HFONT hfntOld;
4178 TEXTMETRIC tm;
4179
4180 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4181 return;
4182
K.Takatac81e9bf2022-01-16 14:15:49 +00004183 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004184 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004185
4186 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4187
4188 /*
4189 * Compute the height of the font used for the tab text
4190 */
4191 hwnd = GetDesktopWindow();
4192 hdc = GetWindowDC(hwnd);
4193 hfntOld = SelectFont(hdc, font);
4194
4195 GetTextMetrics(hdc, &tm);
4196
4197 SelectFont(hdc, hfntOld);
4198 ReleaseDC(hwnd, hdc);
4199
4200 /*
4201 * The space used by the tab border and the space between the tab label
4202 * and the tab border is included as 7.
4203 */
4204 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4205}
K.Takatac81e9bf2022-01-16 14:15:49 +00004206#else
4207# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004208#endif
4209
Bram Moolenaar520470a2005-06-16 21:59:56 +00004210/*
4211 * Invoked when a setting was changed.
4212 */
4213 static LRESULT CALLBACK
4214_OnSettingChange(UINT n)
4215{
4216 if (n == SPI_SETWHEELSCROLLLINES)
4217 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4218 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004219 if (n == SPI_SETNONCLIENTMETRICS)
4220 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004221 return 0;
4222}
4223
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#ifdef FEAT_NETBEANS_INTG
4225 static void
4226_OnWindowPosChanged(
4227 HWND hwnd,
4228 const LPWINDOWPOS lpwpos)
4229{
4230 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004231 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4234 || lpwpos->cx != cx || lpwpos->cy != cy))
4235 {
4236 x = lpwpos->x;
4237 y = lpwpos->y;
4238 cx = lpwpos->cx;
4239 cy = lpwpos->cy;
4240 netbeans_frame_moved(x, y);
4241 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004242 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004243 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244}
4245#endif
4246
K.Takata4f2417f2021-06-05 16:25:32 +02004247
4248static HWND hwndTip = NULL;
4249
4250 static void
4251show_sizing_tip(int cols, int rows)
4252{
4253 TOOLINFOA ti = {sizeof(ti)};
4254 char buf[32];
4255
4256 ti.hwnd = s_hwnd;
4257 ti.uId = (UINT_PTR)s_hwnd;
4258 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4259 ti.lpszText = buf;
4260 sprintf(buf, "%dx%d", cols, rows);
4261 if (hwndTip == NULL)
4262 {
4263 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4264 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4265 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4266 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4267 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4268 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4269 }
4270 else
4271 {
4272 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4273 }
4274 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4275}
4276
4277 static void
4278destroy_sizing_tip(void)
4279{
4280 if (hwndTip != NULL)
4281 {
4282 DestroyWindow(hwndTip);
4283 hwndTip = NULL;
4284 }
4285}
4286
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 static int
4288_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 UINT fwSide,
4290 LPRECT lprc)
4291{
4292 int w, h;
4293 int valid_w, valid_h;
4294 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004295 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 w = lprc->right - lprc->left;
4298 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004299 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 w_offset = w - valid_w;
4301 h_offset = h - valid_h;
4302
4303 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4304 || fwSide == WMSZ_BOTTOMLEFT)
4305 lprc->left += w_offset;
4306 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4307 || fwSide == WMSZ_BOTTOMRIGHT)
4308 lprc->right -= w_offset;
4309
4310 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4311 || fwSide == WMSZ_TOPRIGHT)
4312 lprc->top += h_offset;
4313 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4314 || fwSide == WMSZ_BOTTOMRIGHT)
4315 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004316
4317 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return TRUE;
4319}
4320
K.Takata92000e22022-01-20 15:10:57 +00004321#ifdef FEAT_GUI_TABLINE
4322 static void
4323_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4324{
4325 if (gui_mch_showing_tabline())
4326 {
4327 POINT pt;
4328 RECT rect;
4329
4330 /*
4331 * If the cursor is on the tabline, display the tab menu
4332 */
4333 GetCursorPos(&pt);
4334 GetWindowRect(s_textArea, &rect);
4335 if (pt.y < rect.top)
4336 {
4337 show_tabline_popup_menu();
4338 return;
4339 }
4340 }
4341 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4342}
4343
4344 static void
4345_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4346{
4347 /*
4348 * If the user double clicked the tabline, create a new tab
4349 */
4350 if (gui_mch_showing_tabline())
4351 {
4352 POINT pt;
4353 RECT rect;
4354
4355 GetCursorPos(&pt);
4356 GetWindowRect(s_textArea, &rect);
4357 if (pt.y < rect.top)
4358 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4359 }
4360 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4361}
4362#endif
4363
4364 static UINT
4365_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4366{
4367 UINT result;
4368 int x, y;
4369
4370 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4371 if (result != HTCLIENT)
4372 return result;
4373
4374#ifdef FEAT_GUI_TABLINE
4375 if (gui_mch_showing_tabline())
4376 {
4377 RECT rct;
4378
4379 // If the cursor is on the GUI tabline, don't process this event
4380 GetWindowRect(s_textArea, &rct);
4381 if (yPos < rct.top)
4382 return result;
4383 }
4384#endif
4385 (void)gui_mch_get_winpos(&x, &y);
4386 xPos -= x;
4387
4388 if (xPos < 48) // <VN> TODO should use system metric?
4389 return HTBOTTOMLEFT;
4390 else
4391 return HTBOTTOMRIGHT;
4392}
4393
4394#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4395 static LRESULT
4396_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4397{
4398 switch (hdr->code)
4399 {
4400 case TTN_GETDISPINFOW:
4401 case TTN_GETDISPINFO:
4402 {
4403 char_u *str = NULL;
4404 static void *tt_text = NULL;
4405
4406 VIM_CLEAR(tt_text);
4407
4408# ifdef FEAT_GUI_TABLINE
4409 if (gui_mch_showing_tabline()
4410 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4411 {
4412 POINT pt;
4413 /*
4414 * Mouse is over the GUI tabline. Display the
4415 * tooltip for the tab under the cursor
4416 *
4417 * Get the cursor position within the tab control
4418 */
4419 GetCursorPos(&pt);
4420 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4421 {
4422 TCHITTESTINFO htinfo;
4423 int idx;
4424
4425 /*
4426 * Get the tab under the cursor
4427 */
4428 htinfo.pt.x = pt.x;
4429 htinfo.pt.y = pt.y;
4430 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4431 if (idx != -1)
4432 {
4433 tabpage_T *tp;
4434
4435 tp = find_tabpage(idx + 1);
4436 if (tp != NULL)
4437 {
4438 get_tabline_label(tp, TRUE);
4439 str = NameBuff;
4440 }
4441 }
4442 }
4443 }
4444# endif
4445# ifdef FEAT_TOOLBAR
4446# ifdef FEAT_GUI_TABLINE
4447 else
4448# endif
4449 {
4450 UINT idButton;
4451 vimmenu_T *pMenu;
4452
4453 idButton = (UINT) hdr->idFrom;
4454 pMenu = gui_mswin_find_menu(root_menu, idButton);
4455 if (pMenu)
4456 str = pMenu->strings[MENU_INDEX_TIP];
4457 }
4458# endif
4459 if (str == NULL)
4460 break;
4461
4462 // Set the maximum width, this also enables using \n for
4463 // line break.
4464 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4465
4466 if (hdr->code == TTN_GETDISPINFOW)
4467 {
4468 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4469
4470 tt_text = enc_to_utf16(str, NULL);
4471 lpdi->lpszText = tt_text;
4472 // can't show tooltip if failed
4473 }
4474 else
4475 {
4476 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4477
4478 if (STRLEN(str) < sizeof(lpdi->szText)
4479 || ((tt_text = vim_strsave(str)) == NULL))
4480 vim_strncpy((char_u *)lpdi->szText, str,
4481 sizeof(lpdi->szText) - 1);
4482 else
4483 lpdi->lpszText = tt_text;
4484 }
4485 }
4486 break;
4487
4488# ifdef FEAT_GUI_TABLINE
4489 case TCN_SELCHANGE:
4490 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4491 {
4492 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4493 return 0L;
4494 }
4495 break;
4496
4497 case NM_RCLICK:
4498 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4499 {
4500 show_tabline_popup_menu();
4501 return 0L;
4502 }
4503 break;
4504# endif
4505
4506 default:
4507 break;
4508 }
4509 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4510}
4511#endif
4512
4513#if defined(MENUHINTS) && defined(FEAT_MENU)
4514 static LRESULT
4515_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4516{
4517 if (((UINT) HIWORD(wParam)
4518 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4519 == MF_HILITE
4520 && (State & CMDLINE) == 0)
4521 {
4522 UINT idButton;
4523 vimmenu_T *pMenu;
4524 static int did_menu_tip = FALSE;
4525
4526 if (did_menu_tip)
4527 {
4528 msg_clr_cmdline();
4529 setcursor();
4530 out_flush();
4531 did_menu_tip = FALSE;
4532 }
4533
4534 idButton = (UINT)LOWORD(wParam);
4535 pMenu = gui_mswin_find_menu(root_menu, idButton);
4536 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4537 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4538 {
4539 ++msg_hist_off;
4540 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4541 --msg_hist_off;
4542 setcursor();
4543 out_flush();
4544 did_menu_tip = TRUE;
4545 }
4546 return 0L;
4547 }
4548 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4549}
4550#endif
4551
K.Takatac81e9bf2022-01-16 14:15:49 +00004552 static LRESULT
4553_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4554{
4555 s_dpi = ydpi;
4556 s_in_dpichanged = TRUE;
4557 //TRACE("DPI: %d", ydpi);
4558
4559 update_scrollbar_size();
4560 update_toolbar_size();
4561 set_tabline_font();
4562
4563 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4564 gui_get_wide_font();
4565 gui_mswin_get_menu_height(FALSE);
4566#ifdef FEAT_MBYTE_IME
4567 im_set_position(gui.row, gui.col);
4568#endif
4569 InvalidateRect(hwnd, NULL, TRUE);
4570
4571 s_in_dpichanged = FALSE;
4572 return 0L;
4573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575
4576 static LRESULT CALLBACK
4577_WndProc(
4578 HWND hwnd,
4579 UINT uMsg,
4580 WPARAM wParam,
4581 LPARAM lParam)
4582{
4583 /*
4584 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4585 hwnd, uMsg, wParam, lParam);
4586 */
4587
4588 HandleMouseHide(uMsg, lParam);
4589
4590 s_uMsg = uMsg;
4591 s_wParam = wParam;
4592 s_lParam = lParam;
4593
4594 switch (uMsg)
4595 {
4596 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4597 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004598 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004600 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4602 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4603 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4604 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4605#ifdef FEAT_MENU
4606 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4607#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004608 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4609 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4611 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004612 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4613 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4615 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4616 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4617#ifdef FEAT_NETBEANS_INTG
4618 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4619#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004620#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004621 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4622 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004623#endif
K.Takata92000e22022-01-20 15:10:57 +00004624 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004625
Bram Moolenaar734a8672019-12-02 22:49:38 +01004626 case WM_QUERYENDSESSION: // System wants to go down.
4627 gui_shell_closed(); // Will exit when no changed buffers.
4628 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004631 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004634 return 0L;
4635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 break;
4637
4638 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004639 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4640 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004641 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return 0L;
4643
4644 case WM_SYSCHAR:
4645 /*
4646 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4647 * shortcut key, handle like a typed ALT key, otherwise call Windows
4648 * ALT key handling.
4649 */
4650#ifdef FEAT_MENU
4651 if ( !gui.menu_is_active
4652 || p_wak[0] == 'n'
4653 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4654 )
4655#endif
4656 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004657 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 return 0L;
4659 }
4660#ifdef FEAT_MENU
4661 else
K.Takata4ac893f2022-01-20 12:44:28 +00004662 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#endif
4664
4665 case WM_SYSKEYUP:
4666#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004667 // This used to be done only when menu is active: ALT key is used for
4668 // that. But that caused problems when menu is disabled and using
4669 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4670 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004671 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004673 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674#endif
4675
K.Takata4f2417f2021-06-05 16:25:32 +02004676 case WM_EXITSIZEMOVE:
4677 destroy_sizing_tip();
4678 break;
4679
Bram Moolenaar734a8672019-12-02 22:49:38 +01004680 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004681 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 case WM_MOUSEWHEEL:
4684 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004685 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar734a8672019-12-02 22:49:38 +01004687 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004688 case WM_SETTINGCHANGE:
4689 return _OnSettingChange((UINT)wParam);
4690
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004691#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004693 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#endif
K.Takata92000e22022-01-20 15:10:57 +00004695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696#if defined(MENUHINTS) && defined(FEAT_MENU)
4697 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004698 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701#ifdef FEAT_MBYTE_IME
4702 case WM_IME_NOTIFY:
4703 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004704 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004705 return 1L;
4706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 case WM_IME_COMPOSITION:
4708 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004709 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004710 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004712 case WM_DPICHANGED:
4713 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4714 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004718 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#endif
K.Takata92000e22022-01-20 15:10:57 +00004721 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723
K.Takata4ac893f2022-01-20 12:44:28 +00004724 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725}
4726
4727/*
4728 * End of call-back routines
4729 */
4730
Bram Moolenaar734a8672019-12-02 22:49:38 +01004731// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732HWND vim_parent_hwnd = NULL;
4733
4734 static BOOL CALLBACK
4735FindWindowTitle(HWND hwnd, LPARAM lParam)
4736{
4737 char buf[2048];
4738 char *title = (char *)lParam;
4739
4740 if (GetWindowText(hwnd, buf, sizeof(buf)))
4741 {
4742 if (strstr(buf, title) != NULL)
4743 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004744 // Found it. Store the window ref. and quit searching if MDI
4745 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004747 if (vim_parent_hwnd != NULL)
4748 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004751 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752}
4753
4754/*
4755 * Invoked for '-P "title"' argument: search for parent application to open
4756 * our window in.
4757 */
4758 void
4759gui_mch_set_parent(char *title)
4760{
4761 EnumWindows(FindWindowTitle, (LPARAM)title);
4762 if (vim_parent_hwnd == NULL)
4763 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004764 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 mch_exit(2);
4766 }
4767}
4768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004769#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 static void
4771ole_error(char *arg)
4772{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004773 char buf[IOSIZE];
4774
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004775# ifdef VIMDLL
4776 gui.in_use = mch_is_gui_executable();
4777# endif
4778
Bram Moolenaar734a8672019-12-02 22:49:38 +01004779 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004780 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004781 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004782 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004786#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4787 static char *
4788gvim_error(void)
4789{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004790 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004791
4792 if (starting)
4793 {
4794 mch_errmsg(msg);
4795 mch_errmsg("\n");
4796 mch_exit(2);
4797 }
4798 return msg;
4799}
4800
4801 char *
4802gui_mch_do_spawn(char_u *arg)
4803{
4804 int len;
4805# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4806 char_u *session = NULL;
4807 LPWSTR tofree1 = NULL;
4808# endif
4809 WCHAR name[MAX_PATH];
4810 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4811 STARTUPINFOW si = {sizeof(si)};
4812 PROCESS_INFORMATION pi;
4813
4814 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4815 goto error;
4816 p = wcsrchr(name, L'\\');
4817 if (p == NULL)
4818 goto error;
4819 // Replace the executable name from vim(d).exe to gvim(d).exe.
4820# ifdef DEBUG
4821 wcscpy(p + 1, L"gvimd.exe");
4822# else
4823 wcscpy(p + 1, L"gvim.exe");
4824# endif
4825
4826# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4827 if (starting)
4828# endif
4829 {
4830 // Pass the command line to the new process.
4831 p = GetCommandLineW();
4832 // Skip 1st argument.
4833 while (*p && *p != L' ' && *p != L'\t')
4834 {
4835 if (*p == L'"')
4836 {
4837 while (*p && *p != L'"')
4838 ++p;
4839 if (*p)
4840 ++p;
4841 }
4842 else
4843 ++p;
4844 }
4845 cmd = p;
4846 }
4847# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4848 else
4849 {
4850 // Create a session file and pass it to the new process.
4851 LPWSTR wsession;
4852 char_u *savebg;
4853 int ret;
4854
4855 session = vim_tempname('s', FALSE);
4856 if (session == NULL)
4857 goto error;
4858 savebg = p_bg;
4859 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4860 ret = write_session_file(session);
4861 vim_free(p_bg);
4862 p_bg = savebg;
4863 if (!ret)
4864 goto error;
4865 wsession = enc_to_utf16(session, NULL);
4866 if (wsession == NULL)
4867 goto error;
4868 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004869 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004870 if (cmd == NULL)
4871 {
4872 vim_free(wsession);
4873 goto error;
4874 }
4875 tofree1 = cmd;
4876 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4877 wsession, wsession);
4878 vim_free(wsession);
4879 }
4880# endif
4881
4882 // Check additional arguments to the `:gui` command.
4883 if (arg != NULL)
4884 {
4885 warg = enc_to_utf16(arg, NULL);
4886 if (warg == NULL)
4887 goto error;
4888 tofree2 = warg;
4889 }
4890 else
4891 warg = L"";
4892
4893 // Set up the new command line.
4894 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004895 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004896 if (newcmd == NULL)
4897 goto error;
4898 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4899
4900 // Spawn a new GUI process.
4901 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4902 NULL, NULL, &si, &pi))
4903 goto error;
4904 CloseHandle(pi.hProcess);
4905 CloseHandle(pi.hThread);
4906 mch_exit(0);
4907
4908error:
4909# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4910 if (session)
4911 mch_remove(session);
4912 vim_free(session);
4913 vim_free(tofree1);
4914# endif
4915 vim_free(newcmd);
4916 vim_free(tofree2);
4917 return gvim_error();
4918}
4919#endif
4920
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921/*
4922 * Parse the GUI related command-line arguments. Any arguments used are
4923 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02004924 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
4926 void
4927gui_mch_prepare(int *argc, char **argv)
4928{
4929 int silent = FALSE;
4930 int idx;
4931
Bram Moolenaar734a8672019-12-02 22:49:38 +01004932 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4934 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004935 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4937 && (argv[2][0] == '-' || argv[2][0] == '/'))
4938 {
4939 silent = TRUE;
4940 idx = 2;
4941 }
4942 else
4943 idx = 1;
4944
Bram Moolenaar734a8672019-12-02 22:49:38 +01004945 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (STRICMP(argv[idx] + 1, "register") == 0)
4947 {
4948#ifdef FEAT_OLE
4949 RegisterMe(silent);
4950 mch_exit(0);
4951#else
4952 if (!silent)
4953 ole_error("register");
4954 mch_exit(2);
4955#endif
4956 }
4957
Bram Moolenaar734a8672019-12-02 22:49:38 +01004958 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4960 {
4961#ifdef FEAT_OLE
4962 UnregisterMe(!silent);
4963 mch_exit(0);
4964#else
4965 if (!silent)
4966 ole_error("unregister");
4967 mch_exit(2);
4968#endif
4969 }
4970
Bram Moolenaar734a8672019-12-02 22:49:38 +01004971 // Ignore an -embedding argument. It is only relevant if the
4972 // application wants to treat the case when it is started manually
4973 // differently from the case where it is started via automation (and
4974 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4976 {
4977#ifdef FEAT_OLE
4978 *argc = 1;
4979#else
4980 ole_error("embedding");
4981 mch_exit(2);
4982#endif
4983 }
4984 }
4985
4986#ifdef FEAT_OLE
4987 {
4988 int bDoRestart = FALSE;
4989
4990 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004991 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (bDoRestart)
4993 mch_exit(0);
4994 }
4995#endif
4996
4997#ifdef FEAT_NETBEANS_INTG
4998 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004999 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 int arg;
5001
5002 for (arg = 1; arg < *argc; arg++)
5003 if (strncmp("-nb", argv[arg], 3) == 0)
5004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 netbeansArg = argv[arg];
5006 mch_memmove(&argv[arg], &argv[arg + 1],
5007 (--*argc - arg) * sizeof(char *));
5008 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005009 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013}
5014
K.Takatac81e9bf2022-01-16 14:15:49 +00005015 static void
5016load_dpi_func(void)
5017{
5018 HMODULE hUser32;
5019
5020 hUser32 = GetModuleHandle("user32.dll");
5021 if (hUser32 == NULL)
5022 goto fail;
5023
5024 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5025 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5026 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5027 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5028 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5029 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5030
5031 if (pSetThreadDpiAwarenessContext != NULL)
5032 {
5033 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5034 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5035 if (oldctx != NULL)
5036 {
5037 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5038 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5039#ifdef DEBUG
5040 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5041 {
5042 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5043 }
5044#endif
5045 return;
5046 }
5047 }
5048
5049fail:
5050 // Disable PerMonitorV2 APIs.
5051 pGetDpiForSystem = stubGetDpiForSystem;
5052 pGetDpiForWindow = NULL;
5053 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5054 pSetThreadDpiAwarenessContext = NULL;
5055 pGetAwarenessFromDpiAwarenessContext = NULL;
5056}
5057
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058/*
5059 * Initialise the GUI. Create all the windows, set up all the call-backs
5060 * etc.
5061 */
5062 int
5063gui_mch_init(void)
5064{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005066 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
Bram Moolenaar734a8672019-12-02 22:49:38 +01005069 // Return here if the window was already opened (happens when
5070 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005072 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * Load the tearoff bitmap
5076 */
5077#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005078 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#endif
5080
K.Takatac81e9bf2022-01-16 14:15:49 +00005081 load_dpi_func();
5082
5083 s_dpi = pGetDpiForSystem();
5084 update_scrollbar_size();
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005087 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#endif
5089 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005090#ifdef FEAT_TOOLBAR
5091 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
5094 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5095
Bram Moolenaar734a8672019-12-02 22:49:38 +01005096 // First try using the wide version, so that we can use any title.
5097 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005098 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005100 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 wndclassw.lpfnWndProc = _WndProc;
5102 wndclassw.cbClsExtra = 0;
5103 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005104 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5106 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5107 wndclassw.hbrBackground = s_brush;
5108 wndclassw.lpszMenuName = NULL;
5109 wndclassw.lpszClassName = szVimWndClassW;
5110
K.Takata4ac893f2022-01-20 12:44:28 +00005111 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005112 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (vim_parent_hwnd != NULL)
5116 {
5117#ifdef HAVE_TRY_EXCEPT
5118 __try
5119 {
5120#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005121 // Open inside the specified parent window.
5122 // TODO: last argument should point to a CLIENTCREATESTRUCT
5123 // structure.
5124 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005126 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005127 WS_OVERLAPPEDWINDOW | WS_CHILD
5128 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5130 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005131 100, // Any value will do
5132 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005134 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135#ifdef HAVE_TRY_EXCEPT
5136 }
5137 __except(EXCEPTION_EXECUTE_HANDLER)
5138 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005139 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141#endif
5142 if (s_hwnd == NULL)
5143 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005144 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 mch_exit(2);
5146 }
5147 }
5148 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005149 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005150 // If the provided windowid is not valid reset it to zero, so that it
5151 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005152 if (IsWindow((HWND)win_socket_id) <= 0)
5153 win_socket_id = 0;
5154
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 // Create a window. If win_socket_id is not zero without border and
5156 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005157 s_hwnd = CreateWindowW(
5158 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005159 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5160 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005161 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5162 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005163 100, // Any value will do
5164 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005165 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005166 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005167 if (s_hwnd != NULL && win_socket_id != 0)
5168 {
5169 SetParent(s_hwnd, (HWND)win_socket_id);
5170 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5171 }
5172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 if (s_hwnd == NULL)
5175 return FAIL;
5176
K.Takatac81e9bf2022-01-16 14:15:49 +00005177 if (pGetDpiForWindow != NULL)
5178 {
5179 s_dpi = pGetDpiForWindow(s_hwnd);
5180 update_scrollbar_size();
5181 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5185 dyn_imm_load();
5186#endif
5187
Bram Moolenaar734a8672019-12-02 22:49:38 +01005188 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005189 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005190 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005191 wndclassw.style = CS_OWNDC;
5192 wndclassw.lpfnWndProc = _TextAreaWndProc;
5193 wndclassw.cbClsExtra = 0;
5194 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005195 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005196 wndclassw.hIcon = NULL;
5197 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5198 wndclassw.hbrBackground = NULL;
5199 wndclassw.lpszMenuName = NULL;
5200 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005201
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005202 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 return FAIL;
5204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005206 s_textArea = CreateWindowExW(
5207 0,
5208 szTextAreaClassW, L"Vim text area",
5209 WS_CHILD | WS_VISIBLE, 0, 0,
5210 100, // Any value will do for now
5211 100, // Any value will do for now
5212 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005213 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (s_textArea == NULL)
5216 return FAIL;
5217
Bram Moolenaar20321902016-02-17 12:30:17 +01005218#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005219 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005220 {
5221 HANDLE hIcon = NULL;
5222
5223 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005224 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005225 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005226#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#ifdef FEAT_MENU
5229 s_menuBar = CreateMenu();
5230#endif
5231 s_hdc = GetDC(s_textArea);
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005236 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
Bram Moolenaar734a8672019-12-02 22:49:38 +01005238 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 gui_mch_def_colors();
5240
Bram Moolenaar734a8672019-12-02 22:49:38 +01005241 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5242 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 set_normal_colors();
5244
5245 /*
5246 * Check that none of the colors are the same as the background color.
5247 * Then store the current values as the defaults.
5248 */
5249 gui_check_colors();
5250 gui.def_norm_pixel = gui.norm_pixel;
5251 gui.def_back_pixel = gui.back_pixel;
5252
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 // Get the colors for the highlight groups (gui_check_colors() might have
5254 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 highlight_gui_started();
5256
5257 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005258 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005260 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 /*
5263 * Set up for Intellimouse processing
5264 */
5265 init_mouse_wheel();
5266
5267 /*
5268 * compute a couple of metrics used for the dialogs
5269 */
5270 get_dialog_font_metrics();
5271#ifdef FEAT_TOOLBAR
5272 /*
5273 * Create the toolbar
5274 */
5275 initialise_toolbar();
5276#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005277#ifdef FEAT_GUI_TABLINE
5278 /*
5279 * Create the tabline
5280 */
5281 initialise_tabline();
5282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283#ifdef MSWIN_FIND_REPLACE
5284 /*
5285 * Initialise the dialog box stuff
5286 */
5287 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5288
Bram Moolenaar734a8672019-12-02 22:49:38 +01005289 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005291 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005293 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5295 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5296 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005299#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005300 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005301 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005302#endif
5303
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005304#ifdef FEAT_RENDER_OPTIONS
5305 if (p_rop)
5306 (void)gui_mch_set_rendering_options(p_rop);
5307#endif
5308
Bram Moolenaar748bf032005-02-02 23:04:36 +00005309theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005310 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005311 display_errors();
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 return OK;
5314}
5315
5316/*
5317 * Get the size of the screen, taking position on multiple monitors into
5318 * account (if supported).
5319 */
5320 static void
5321get_work_area(RECT *spi_rect)
5322{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005323 HMONITOR mon;
5324 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
Bram Moolenaar734a8672019-12-02 22:49:38 +01005326 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005327 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005328 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005330 moninfo.cbSize = sizeof(MONITORINFO);
5331 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005333 *spi_rect = moninfo.rcWork;
5334 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005337 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5339}
5340
5341/*
5342 * Set the size of the window to the given width and height in pixels.
5343 */
5344 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005345gui_mch_set_shellsize(
5346 int width,
5347 int height,
5348 int min_width UNUSED,
5349 int min_height UNUSED,
5350 int base_width UNUSED,
5351 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005352 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005355 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
Bram Moolenaar734a8672019-12-02 22:49:38 +01005358 // Try to keep window completely on screen.
5359 // Get position of the screen work area. This is the part that is not
5360 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 get_work_area(&workarea_rect);
5362
Bram Moolenaar734a8672019-12-02 22:49:38 +01005363 // Resizing a maximized window looks very strange, unzoom it first.
5364 // But don't do it when still starting up, it may have been requested in
5365 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005366 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005368
5369 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar734a8672019-12-02 22:49:38 +01005371 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005372 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5373 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5374 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5375 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5376 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5377 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
Bram Moolenaar734a8672019-12-02 22:49:38 +01005379 // The following should take care of keeping Vim on the same monitor, no
5380 // matter if the secondary monitor is left or right of the primary
5381 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005382 window_rect.right = window_rect.left + win_width;
5383 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005384
Bram Moolenaar734a8672019-12-02 22:49:38 +01005385 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005386 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5387 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005389 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5390 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005392 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5393 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005395 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5396 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005398 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5399 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
5401 SetActiveWindow(s_hwnd);
5402 SetFocus(s_hwnd);
5403
Bram Moolenaar734a8672019-12-02 22:49:38 +01005404 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406}
5407
5408
5409 void
5410gui_mch_set_scrollbar_thumb(
5411 scrollbar_T *sb,
5412 long val,
5413 long size,
5414 long max)
5415{
5416 SCROLLINFO info;
5417
5418 sb->scroll_shift = 0;
5419 while (max > 32767)
5420 {
5421 max = (max + 1) >> 1;
5422 val >>= 1;
5423 size >>= 1;
5424 ++sb->scroll_shift;
5425 }
5426
5427 if (sb->scroll_shift > 0)
5428 ++size;
5429
5430 info.cbSize = sizeof(info);
5431 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5432 info.nPos = val;
5433 info.nMin = 0;
5434 info.nMax = max;
5435 info.nPage = size;
5436 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5437}
5438
5439
5440/*
5441 * Set the current text font.
5442 */
5443 void
5444gui_mch_set_font(GuiFont font)
5445{
5446 gui.currFont = font;
5447}
5448
5449
5450/*
5451 * Set the current text foreground color.
5452 */
5453 void
5454gui_mch_set_fg_color(guicolor_T color)
5455{
5456 gui.currFgColor = color;
5457}
5458
5459/*
5460 * Set the current text background color.
5461 */
5462 void
5463gui_mch_set_bg_color(guicolor_T color)
5464{
5465 gui.currBgColor = color;
5466}
5467
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005468/*
5469 * Set the current text special color.
5470 */
5471 void
5472gui_mch_set_sp_color(guicolor_T color)
5473{
5474 gui.currSpColor = color;
5475}
5476
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005477#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478/*
5479 * Multi-byte handling, originally by Sung-Hoon Baek.
5480 * First static functions (no prototypes generated).
5481 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005482# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005483# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005484# endif
5485# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 * handle WM_IME_NOTIFY message
5489 */
5490 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005491_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 LRESULT lResult = 0;
5494 HIMC hImc;
5495
5496 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5497 return lResult;
5498 switch (dwCommand)
5499 {
5500 case IMN_SETOPENSTATUS:
5501 if (pImmGetOpenStatus(hImc))
5502 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005503 LOGFONTW lf = norm_logfont;
5504 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5505 // Work around when PerMonitorV2 is not enabled in the process level.
5506 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5507 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 im_set_position(gui.row, gui.col);
5509
Bram Moolenaar734a8672019-12-02 22:49:38 +01005510 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 State &= ~LANGMAP;
5512 if (State & INSERT)
5513 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005514# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005515 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5517 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005518 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 int old_row = gui.row;
5520 int old_col = gui.col;
5521
5522 // This must be called here before
5523 // status_redraw_curbuf(), otherwise the mode
5524 // message may appear in the wrong position.
5525 showmode();
5526 status_redraw_curbuf();
5527 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005528 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 gui.row = old_row;
5530 gui.col = old_col;
5531 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 }
5535 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005536 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 lResult = 0;
5538 break;
5539 }
5540 pImmReleaseContext(hWnd, hImc);
5541 return lResult;
5542}
5543
5544 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005545_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 char_u *ret;
5548 int len;
5549
Bram Moolenaar734a8672019-12-02 22:49:38 +01005550 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return 0;
5552
5553 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5554 if (ret != NULL)
5555 {
5556 add_to_input_buf_csi(ret, len);
5557 vim_free(ret);
5558 return 1;
5559 }
5560 return 0;
5561}
5562
5563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 * void GetResultStr()
5565 *
5566 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5567 * get complete composition string
5568 */
5569 static char_u *
5570GetResultStr(HWND hwnd, int GCS, int *lenp)
5571{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005572 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005573 LONG ret;
5574 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 char_u *convbuf = NULL;
5576
5577 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5578 return NULL;
5579
K.Takatab0b2b732022-01-19 12:59:21 +00005580 // Get the length of the composition string.
5581 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5582 if (ret <= 0)
5583 return NULL;
5584
5585 // Allocate the requested buffer plus space for the NUL character.
5586 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 if (buf == NULL)
5588 return NULL;
5589
K.Takatab0b2b732022-01-19 12:59:21 +00005590 // Reads in the composition string.
5591 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5592 *lenp = ret / sizeof(WCHAR);
5593
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005594 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 pImmReleaseContext(hwnd, hIMC);
5596 vim_free(buf);
5597 return convbuf;
5598}
5599#endif
5600
Bram Moolenaar734a8672019-12-02 22:49:38 +01005601// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005602#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604/*
5605 * set font to IM.
5606 */
5607 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005608im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 HIMC hImc;
5611
5612 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5613 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005614 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 pImmReleaseContext(s_hwnd, hImc);
5616 }
5617}
5618
5619/*
5620 * Notify cursor position to IM.
5621 */
5622 void
5623im_set_position(int row, int col)
5624{
5625 HIMC hImc;
5626
5627 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5628 {
5629 COMPOSITIONFORM cfs;
5630
5631 cfs.dwStyle = CFS_POINT;
5632 cfs.ptCurrentPos.x = FILL_X(col);
5633 cfs.ptCurrentPos.y = FILL_Y(row);
5634 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005635 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5636 {
5637 // Work around when PerMonitorV2 is not enabled in the process level.
5638 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5639 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 pImmSetCompositionWindow(hImc, &cfs);
5642
5643 pImmReleaseContext(s_hwnd, hImc);
5644 }
5645}
5646
5647/*
5648 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5649 */
5650 void
5651im_set_active(int active)
5652{
5653 HIMC hImc;
5654 static HIMC hImcOld = (HIMC)0;
5655
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005656# ifdef VIMDLL
5657 if (!gui.in_use && !gui.starting)
5658 {
5659 mbyte_im_set_active(active);
5660 return;
5661 }
5662# endif
5663
Bram Moolenaar734a8672019-12-02 22:49:38 +01005664 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
5666 if (p_imdisable)
5667 {
5668 if (hImcOld == (HIMC)0)
5669 {
5670 hImcOld = pImmGetContext(s_hwnd);
5671 if (hImcOld)
5672 pImmAssociateContext(s_hwnd, (HIMC)0);
5673 }
5674 active = FALSE;
5675 }
5676 else if (hImcOld != (HIMC)0)
5677 {
5678 pImmAssociateContext(s_hwnd, hImcOld);
5679 hImcOld = (HIMC)0;
5680 }
5681
5682 hImc = pImmGetContext(s_hwnd);
5683 if (hImc)
5684 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005685 /*
5686 * for Korean ime
5687 */
5688 HKL hKL = GetKeyboardLayout(0);
5689
5690 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5691 {
5692 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5693 static BOOL bSaved = FALSE;
5694
5695 if (active)
5696 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005697 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005698 if (bSaved)
5699 pImmSetConversionStatus(hImc, dwConversionSaved,
5700 dwSentenceSaved);
5701 bSaved = FALSE;
5702 }
5703 else
5704 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005705 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005706 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5707 &dwSentenceSaved))
5708 {
5709 bSaved = TRUE;
5710 pImmSetConversionStatus(hImc,
5711 dwConversionSaved & ~(IME_CMODE_NATIVE
5712 | IME_CMODE_FULLSHAPE),
5713 dwSentenceSaved);
5714 }
5715 }
5716 }
5717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 pImmSetOpenStatus(hImc, active);
5719 pImmReleaseContext(s_hwnd, hImc);
5720 }
5721 }
5722}
5723
5724/*
5725 * Get IM status. When IM is on, return not 0. Else return 0.
5726 */
5727 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005728im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 int status = 0;
5731 HIMC hImc;
5732
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005733# ifdef VIMDLL
5734 if (!gui.in_use && !gui.starting)
5735 return mbyte_im_get_status();
5736# endif
5737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5739 {
5740 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5741 pImmReleaseContext(s_hwnd, hImc);
5742 }
5743 return status;
5744}
5745
Bram Moolenaar734a8672019-12-02 22:49:38 +01005746#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005749/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005750 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005751 */
5752 static void
5753latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5754{
5755 int c;
5756
Bram Moolenaarca003e12006-03-17 23:19:38 +00005757 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005758 {
5759 c = *text++;
5760 switch (c)
5761 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005762 case 0xa4: c = 0x20ac; break; // euro
5763 case 0xa6: c = 0x0160; break; // S hat
5764 case 0xa8: c = 0x0161; break; // S -hat
5765 case 0xb4: c = 0x017d; break; // Z hat
5766 case 0xb8: c = 0x017e; break; // Z -hat
5767 case 0xbc: c = 0x0152; break; // OE
5768 case 0xbd: c = 0x0153; break; // oe
5769 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005770 }
5771 *unicodebuf++ = c;
5772 }
5773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
5775#ifdef FEAT_RIGHTLEFT
5776/*
5777 * What is this for? In the case where you are using Win98 or Win2K or later,
5778 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5779 * reverses the string sent to the TextOut... family. This sucks, because we
5780 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5781 * way to tell Windblows not to do this!
5782 *
5783 * The short of it is that this 'RevOut' only gets called if you are running
5784 * one of the new, "improved" MS OSes, and only if you are running in
5785 * 'rightleft' mode. It makes display take *slightly* longer, but not
5786 * noticeably so.
5787 */
5788 static void
K.Takata135e1522022-01-29 15:27:58 +00005789RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 int col,
5791 int row,
5792 UINT foptions,
5793 CONST RECT *pcliprect,
5794 LPCTSTR text,
5795 UINT len,
5796 CONST INT *padding)
5797{
5798 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005800 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005801 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005802 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803}
5804#endif
5805
Bram Moolenaar92467d32017-12-05 13:22:16 +01005806 static void
5807draw_line(
5808 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005809 int y1,
5810 int x2,
5811 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005812 COLORREF color)
5813{
5814#if defined(FEAT_DIRECTX)
5815 if (IS_ENABLE_DIRECTX())
5816 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5817 else
5818#endif
5819 {
5820 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5821 HPEN old_pen = SelectObject(s_hdc, hpen);
5822 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005823 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005824 LineTo(s_hdc, x2, y2);
5825 DeleteObject(SelectObject(s_hdc, old_pen));
5826 }
5827}
5828
5829 static void
5830set_pixel(
5831 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005832 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005833 COLORREF color)
5834{
5835#if defined(FEAT_DIRECTX)
5836 if (IS_ENABLE_DIRECTX())
5837 DWriteContext_SetPixel(s_dwc, x, y, color);
5838 else
5839#endif
5840 SetPixel(s_hdc, x, y, color);
5841}
5842
5843 static void
5844fill_rect(
5845 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005846 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005847 COLORREF color)
5848{
5849#if defined(FEAT_DIRECTX)
5850 if (IS_ENABLE_DIRECTX())
5851 DWriteContext_FillRect(s_dwc, rcp, color);
5852 else
5853#endif
5854 {
5855 HBRUSH hbr2;
5856
5857 if (hbr == NULL)
5858 hbr2 = CreateSolidBrush(color);
5859 else
5860 hbr2 = hbr;
5861 FillRect(s_hdc, rcp, hbr2);
5862 if (hbr == NULL)
5863 DeleteBrush(hbr2);
5864 }
5865}
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 void
5868gui_mch_draw_string(
5869 int row,
5870 int col,
5871 char_u *text,
5872 int len,
5873 int flags)
5874{
5875 static int *padding = NULL;
5876 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 const RECT *pcliprect = NULL;
5878 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 static WCHAR *unicodebuf = NULL;
5880 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005881 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 int y;
5884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 /*
5886 * Italic and bold text seems to have an extra row of pixels at the bottom
5887 * (below where the bottom of the character should be). If we draw the
5888 * characters with a solid background, the top row of pixels in the
5889 * character below will be overwritten. We can fix this by filling in the
5890 * background ourselves, to the correct character proportions, and then
5891 * writing the character in transparent mode. Still have a problem when
5892 * the character is "_", which gets written on to the character below.
5893 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5894 * pixel in their slots, which fixes the problem with the bottom row of
5895 * pixels. We still need this code because otherwise the top row of pixels
5896 * becomes a problem. - webb.
5897 */
5898 static HBRUSH hbr_cache[2] = {NULL, NULL};
5899 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5900 static int brush_lru = 0;
5901 HBRUSH hbr;
5902 RECT rc;
5903
5904 if (!(flags & DRAW_TRANSP))
5905 {
5906 /*
5907 * Clear background first.
5908 * Note: FillRect() excludes right and bottom of rectangle.
5909 */
5910 rc.left = FILL_X(col);
5911 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if (has_mbyte)
5913 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005914 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005915 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 rc.right = FILL_X(col + len);
5919 rc.bottom = FILL_Y(row + 1);
5920
Bram Moolenaar734a8672019-12-02 22:49:38 +01005921 // Cache the created brush, that saves a lot of time. We need two:
5922 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 if (gui.currBgColor == brush_color[0])
5924 {
5925 hbr = hbr_cache[0];
5926 brush_lru = 1;
5927 }
5928 else if (gui.currBgColor == brush_color[1])
5929 {
5930 hbr = hbr_cache[1];
5931 brush_lru = 0;
5932 }
5933 else
5934 {
5935 if (hbr_cache[brush_lru] != NULL)
5936 DeleteBrush(hbr_cache[brush_lru]);
5937 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5938 brush_color[brush_lru] = gui.currBgColor;
5939 hbr = hbr_cache[brush_lru];
5940 brush_lru = !brush_lru;
5941 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005942
Bram Moolenaar92467d32017-12-05 13:22:16 +01005943 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 SetBkMode(s_hdc, TRANSPARENT);
5946
5947 /*
5948 * When drawing block cursor, prevent inverted character spilling
5949 * over character cell (can happen with bold/italic)
5950 */
5951 if (flags & DRAW_CURSOR)
5952 {
5953 pcliprect = &rc;
5954 foptions = ETO_CLIPPED;
5955 }
5956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 SetTextColor(s_hdc, gui.currFgColor);
5958 SelectFont(s_hdc, gui.currFont);
5959
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005960#ifdef FEAT_DIRECTX
5961 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005962 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005963#endif
5964
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5966 {
K.Takata135e1522022-01-29 15:27:58 +00005967 int i;
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 vim_free(padding);
5970 pad_size = Columns;
5971
Bram Moolenaar734a8672019-12-02 22:49:38 +01005972 // Don't give an out-of-memory message here, it would call us
5973 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02005974 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 if (padding != NULL)
5976 for (i = 0; i < pad_size; i++)
5977 padding[i] = gui.char_width;
5978 }
5979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 /*
5981 * We have to provide the padding argument because italic and bold versions
5982 * of fixed-width fonts are often one pixel or so wider than their normal
5983 * versions.
5984 * No check for DRAW_BOLD, Windows will have done it already.
5985 */
5986
Bram Moolenaar734a8672019-12-02 22:49:38 +01005987 // Check if there are any UTF-8 characters. If not, use normal text
5988 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 if (enc_utf8)
5990 for (n = 0; n < len; ++n)
5991 if (text[n] >= 0x80)
5992 break;
5993
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005994#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005995 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5996 // required that unicode drawing routine, currently. So this forces it
5997 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005998 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01005999 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006000#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006001
Bram Moolenaar734a8672019-12-02 22:49:38 +01006002 // Check if the Unicode buffer exists and is big enough. Create it
6003 // with the same length as the multi-byte string, the number of wide
6004 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006005 if ((enc_utf8
6006 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6007 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 && (unicodebuf == NULL || len > unibuflen))
6009 {
6010 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006011 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006014 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 unibuflen = len;
6017 }
6018
6019 if (enc_utf8 && n < len && unicodebuf != NULL)
6020 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006021 // Output UTF-8 characters. Composing characters should be
6022 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006023 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006024 int wlen; // string length in words
6025 int clen; // string length in characters
6026 int cells; // cell width of string up to composing char
6027 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006028 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006030 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006031 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006033 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006035 c = utf_ptr2char(text + i);
6036 if (c >= 0x10000)
6037 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006038 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006039 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6040 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006041 }
6042 else
6043 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006044 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006045 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006046
6047 if (utf_iscomposing(c))
6048 cw = 0;
6049 else
6050 {
6051 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006052 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006053 cw = 1;
6054 }
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (unicodepdy != NULL)
6057 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006058 // Use unicodepdy to make characters fit as we expect, even
6059 // when the font uses different widths (e.g., bold character
6060 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006061 if (c >= 0x10000)
6062 {
6063 unicodepdy[wlen - 2] = cw * gui.char_width;
6064 unicodepdy[wlen - 1] = 0;
6065 }
6066 else
6067 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
6069 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006070 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006071 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006073#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006074 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006075 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006076 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006077 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006078 TEXT_X(col), TEXT_Y(row),
6079 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006080 gui.char_width, gui.currFgColor,
6081 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006082 }
6083 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006084#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006085 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6086 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006087 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006089 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006091 // If we want to display codepage data, and the current CP is not the
6092 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (unicodebuf != NULL)
6094 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006095 if (enc_latin9)
6096 latin9_to_ucs(text, len, unicodebuf);
6097 else
6098 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 MB_PRECOMPOSED,
6100 (char *)text, len,
6101 (LPWSTR)unicodebuf, unibuflen);
6102 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006103 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006104 // Use unicodepdy to make characters fit as we expect, even
6105 // when the font uses different widths (e.g., bold character
6106 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006107 if (unicodepdy != NULL)
6108 {
6109 int i;
6110 int cw;
6111
6112 for (i = 0; i < len; ++i)
6113 {
6114 cw = utf_char2cells(unicodebuf[i]);
6115 if (cw > 2)
6116 cw = 1;
6117 unicodepdy[i] = cw * gui.char_width;
6118 }
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006121 foptions, pcliprect, unicodebuf, len, unicodepdy);
6122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 }
6124 }
6125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006128 // Windows will mess up RL text, so we have to draw it character by
6129 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006130 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6132 foptions, pcliprect, (char *)text, len, padding);
6133 else
6134#endif
6135 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6136 foptions, pcliprect, (char *)text, len, padding);
6137 }
6138
Bram Moolenaar734a8672019-12-02 22:49:38 +01006139 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (flags & DRAW_UNDERL)
6141 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006142 // When p_linespace is 0, overwrite the bottom row of pixels.
6143 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (p_linespace > 1)
6146 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006147 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006149
Bram Moolenaar734a8672019-12-02 22:49:38 +01006150 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006151 if (flags & DRAW_STRIKE)
6152 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006153 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006154 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006155 }
6156
Bram Moolenaar734a8672019-12-02 22:49:38 +01006157 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006158 if (flags & DRAW_UNDERC)
6159 {
6160 int x;
6161 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006162 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006163
6164 y = FILL_Y(row + 1) - 1;
6165 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6166 {
6167 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006168 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006169 }
6170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171}
6172
6173
6174/*
6175 * Output routines.
6176 */
6177
Bram Moolenaar734a8672019-12-02 22:49:38 +01006178/*
6179 * Flush any output to the screen
6180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 void
6182gui_mch_flush(void)
6183{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006184#if defined(FEAT_DIRECTX)
6185 if (IS_ENABLE_DIRECTX())
6186 DWriteContext_Flush(s_dwc);
6187#endif
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 GdiFlush();
6190}
6191
6192 static void
6193clear_rect(RECT *rcp)
6194{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006195 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196}
6197
6198
Bram Moolenaarc716c302006-01-21 22:12:51 +00006199 void
6200gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6201{
6202 RECT workarea_rect;
6203
6204 get_work_area(&workarea_rect);
6205
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006206 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006207 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6208 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006209
Bram Moolenaar734a8672019-12-02 22:49:38 +01006210 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6211 // the menubar for MSwin, we subtract it from the screen height, so that
6212 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006213 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006214 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6215 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6216 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6217 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006218}
6219
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221#if defined(FEAT_MENU) || defined(PROTO)
6222/*
6223 * Add a sub menu to the menu bar.
6224 */
6225 void
6226gui_mch_add_menu(
6227 vimmenu_T *menu,
6228 int pos)
6229{
6230 vimmenu_T *parent = menu->parent;
6231
6232 menu->submenu_id = CreatePopupMenu();
6233 menu->id = s_menu_id++;
6234
6235 if (menu_is_menubar(menu->name))
6236 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006237 WCHAR *wn;
6238 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006240 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006241 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006242 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006244 infow.cbSize = sizeof(infow);
6245 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6246 | MIIM_SUBMENU;
6247 infow.dwItemData = (long_u)menu;
6248 infow.wID = menu->id;
6249 infow.fType = MFT_STRING;
6250 infow.dwTypeData = wn;
6251 infow.cch = (UINT)wcslen(wn);
6252 infow.hSubMenu = menu->submenu_id;
6253 InsertMenuItemW((parent == NULL)
6254 ? s_menuBar : parent->submenu_id,
6255 (UINT)pos, TRUE, &infow);
6256 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 }
6258
Bram Moolenaar734a8672019-12-02 22:49:38 +01006259 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (parent == NULL)
6261 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006262# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 else if (IsWindow(parent->tearoff_handle))
6264 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266}
6267
6268 void
6269gui_mch_show_popupmenu(vimmenu_T *menu)
6270{
6271 POINT mp;
6272
K.Takata45f9cfb2022-01-21 11:11:00 +00006273 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6275}
6276
6277 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006278gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 vimmenu_T *menu = gui_find_menu(path_name);
6281
6282 if (menu != NULL)
6283 {
6284 POINT p;
6285
Bram Moolenaar734a8672019-12-02 22:49:38 +01006286 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006288 if (mouse_pos)
6289 {
6290 int mx, my;
6291
6292 gui_mch_getmouse(&mx, &my);
6293 p.x += mx;
6294 p.y += my;
6295 }
6296 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006298 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6300 }
6301 msg_scroll = FALSE;
6302 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6303 }
6304}
6305
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006306# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307/*
6308 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6309 * create it as a pseudo-"tearoff menu".
6310 */
6311 void
6312gui_make_tearoff(char_u *path_name)
6313{
6314 vimmenu_T *menu = gui_find_menu(path_name);
6315
Bram Moolenaar734a8672019-12-02 22:49:38 +01006316 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 if (menu != NULL)
6318 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6319}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322/*
6323 * Add a menu item to a menu
6324 */
6325 void
6326gui_mch_add_menu_item(
6327 vimmenu_T *menu,
6328 int idx)
6329{
6330 vimmenu_T *parent = menu->parent;
6331
6332 menu->id = s_menu_id++;
6333 menu->submenu_id = NULL;
6334
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006335# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6337 {
6338 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6339 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6340 }
6341 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006342# endif
6343# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (menu_is_toolbar(parent->name))
6345 {
6346 TBBUTTON newtb;
6347
Bram Moolenaara80faa82020-04-12 19:37:17 +02006348 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 if (menu_is_separator(menu->name))
6350 {
6351 newtb.iBitmap = 0;
6352 newtb.fsStyle = TBSTYLE_SEP;
6353 }
6354 else
6355 {
6356 newtb.iBitmap = get_toolbar_bitmap(menu);
6357 newtb.fsStyle = TBSTYLE_BUTTON;
6358 }
6359 newtb.idCommand = menu->id;
6360 newtb.fsState = TBSTATE_ENABLED;
6361 newtb.iString = 0;
6362 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6363 (LPARAM)&newtb);
6364 menu->submenu_id = (HMENU)-1;
6365 }
6366 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006369 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006371 wn = enc_to_utf16(menu->name, NULL);
6372 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006374 InsertMenuW(parent->submenu_id, (UINT)idx,
6375 (menu_is_separator(menu->name)
6376 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6377 (UINT)menu->id, wn);
6378 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006380# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (IsWindow(parent->tearoff_handle))
6382 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385}
6386
6387/*
6388 * Destroy the machine specific menu widget.
6389 */
6390 void
6391gui_mch_destroy_menu(vimmenu_T *menu)
6392{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006393# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 /*
6395 * is this a toolbar button?
6396 */
6397 if (menu->submenu_id == (HMENU)-1)
6398 {
6399 int iButton;
6400
6401 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6402 (WPARAM)menu->id, 0);
6403 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6404 }
6405 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 if (menu->parent != NULL
6409 && menu_is_popup(menu->parent->dname)
6410 && menu->parent->submenu_id != NULL)
6411 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6412 else
6413 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6414 if (menu->submenu_id != NULL)
6415 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006416# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (IsWindow(menu->tearoff_handle))
6418 DestroyWindow(menu->tearoff_handle);
6419 if (menu->parent != NULL
6420 && menu->parent->children != NULL
6421 && IsWindow(menu->parent->tearoff_handle))
6422 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006423 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 menu->modes = 0;
6425 rebuild_tearoff(menu->parent);
6426 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
6429}
6430
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006431# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 static void
6433rebuild_tearoff(vimmenu_T *menu)
6434{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006435 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 char_u tbuf[128];
6437 RECT trect;
6438 RECT rct;
6439 RECT roct;
6440 int x, y;
6441
6442 HWND thwnd = menu->tearoff_handle;
6443
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006444 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 if (GetWindowRect(thwnd, &trect)
6446 && GetWindowRect(s_hwnd, &rct)
6447 && GetClientRect(s_hwnd, &roct))
6448 {
6449 x = trect.left - rct.left;
6450 y = (trect.top - rct.bottom + roct.bottom);
6451 }
6452 else
6453 {
6454 x = y = 0xffffL;
6455 }
6456 DestroyWindow(thwnd);
6457 if (menu->children != NULL)
6458 {
6459 gui_mch_tearoff(tbuf, menu, x, y);
6460 if (IsWindow(menu->tearoff_handle))
6461 (void) SetWindowPos(menu->tearoff_handle,
6462 NULL,
6463 (int)trect.left,
6464 (int)trect.top,
6465 0, 0,
6466 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6467 }
6468}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006469# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471/*
6472 * Make a menu either grey or not grey.
6473 */
6474 void
6475gui_mch_menu_grey(
6476 vimmenu_T *menu,
6477 int grey)
6478{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006479# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 /*
6481 * is this a toolbar button?
6482 */
6483 if (menu->submenu_id == (HMENU)-1)
6484 {
6485 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006486 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006489# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006490 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6491 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006493# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6495 {
6496 WORD menuID;
6497 HWND menuHandle;
6498
6499 /*
6500 * A tearoff button has changed state.
6501 */
6502 if (menu->children == NULL)
6503 menuID = (WORD)(menu->id);
6504 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006505 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6507 if (menuHandle)
6508 EnableWindow(menuHandle, !grey);
6509
6510 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512}
6513
Bram Moolenaar734a8672019-12-02 22:49:38 +01006514#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516
Bram Moolenaar734a8672019-12-02 22:49:38 +01006517// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006520#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6523/*
6524 * stuff for dialogs
6525 */
6526
6527/*
6528 * The callback routine used by all the dialogs. Very simple. First,
6529 * acknowledges the INITDIALOG message so that Windows knows to do standard
6530 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6531 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6532 * number.
6533 */
6534 static LRESULT CALLBACK
6535dialog_callback(
6536 HWND hwnd,
6537 UINT message,
6538 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006539 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 if (message == WM_INITDIALOG)
6542 {
6543 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006544 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 (void)SetFocus(hwnd);
6546 if (dialog_default_button > IDCANCEL)
6547 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006548 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006549 // We don't have a default, set focus on another element of the
6550 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006551 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 return FALSE;
6553 }
6554
6555 if (message == WM_COMMAND)
6556 {
6557 int button = LOWORD(wParam);
6558
Bram Moolenaar734a8672019-12-02 22:49:38 +01006559 // Don't end the dialog if something was selected that was
6560 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (button >= DLG_NONBUTTON_CONTROL)
6562 return TRUE;
6563
Bram Moolenaar734a8672019-12-02 22:49:38 +01006564 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006566 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006567 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006568 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006569
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006570 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6571 p = utf16_to_enc(wp, NULL);
6572 vim_strncpy(s_textfield, p, IOSIZE);
6573 vim_free(p);
6574 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 /*
6578 * Need to check for IDOK because if the user just hits Return to
6579 * accept the default value, some reason this is what we get.
6580 */
6581 if (button == IDOK)
6582 {
6583 if (dialog_default_button > IDCANCEL)
6584 EndDialog(hwnd, dialog_default_button);
6585 }
6586 else
6587 EndDialog(hwnd, button - IDCANCEL);
6588 return TRUE;
6589 }
6590
6591 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6592 {
6593 EndDialog(hwnd, 0);
6594 return TRUE;
6595 }
6596 return FALSE;
6597}
6598
6599/*
6600 * Create a dialog dynamically from the parameter strings.
6601 * type = type of dialog (question, alert, etc.)
6602 * title = dialog title. may be NULL for default title.
6603 * message = text to display. Dialog sizes to accommodate it.
6604 * buttons = '\n' separated list of button captions, default first.
6605 * dfltbutton = number of default button.
6606 *
6607 * This routine returns 1 if the first button is pressed,
6608 * 2 for the second, etc.
6609 *
6610 * 0 indicates Esc was pressed.
6611 * -1 for unexpected error
6612 *
6613 * If stubbing out this fn, return 1.
6614 */
6615
Bram Moolenaar734a8672019-12-02 22:49:38 +01006616static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
6618 "IDR_VIM",
6619 "IDR_VIM_ERROR",
6620 "IDR_VIM_ALERT",
6621 "IDR_VIM_INFO",
6622 "IDR_VIM_QUESTION"
6623};
6624
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 int
6626gui_mch_dialog(
6627 int type,
6628 char_u *title,
6629 char_u *message,
6630 char_u *buttons,
6631 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006632 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006633 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006636 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 int numButtons;
6638 int *buttonWidths, *buttonPositions;
6639 int buttonYpos;
6640 int nchar, i;
6641 DWORD lStyle;
6642 int dlgwidth = 0;
6643 int dlgheight;
6644 int editboxheight;
6645 int horizWidth = 0;
6646 int msgheight;
6647 char_u *pstart;
6648 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006649 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 char_u *tbuffer;
6651 RECT rect;
6652 HWND hwnd;
6653 HDC hdc;
6654 HFONT font, oldFont;
6655 TEXTMETRIC fontInfo;
6656 int fontHeight;
6657 int textWidth, minButtonWidth, messageWidth;
6658 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006659 int maxDialogHeight;
6660 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 int vertical;
6662 int dlgPaddingX;
6663 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006664# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006665 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006667# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006668 garray_T ga;
6669 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006670 int dlg_icon_width;
6671 int dlg_icon_height;
6672 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006674# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006675 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006676# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006677 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006678# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006679 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006680 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
Bram Moolenaar748bf032005-02-02 23:04:36 +00006683 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006684 {
6685 load_dpi_func();
6686 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006687 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006688 }
6689 else
6690 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 if ((type < 0) || (type > VIM_LAST_TYPE))
6693 type = 0;
6694
Bram Moolenaar734a8672019-12-02 22:49:38 +01006695 // allocate some memory for dialog template
6696 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006697 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006698 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700 if (p == NULL)
6701 return -1;
6702
6703 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006704 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6706 * const.
6707 */
6708 tbuffer = vim_strsave(buttons);
6709 if (tbuffer == NULL)
6710 return -1;
6711
Bram Moolenaar734a8672019-12-02 22:49:38 +01006712 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
Bram Moolenaar734a8672019-12-02 22:49:38 +01006714 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 numButtons = 1;
6716 for (i = 0; tbuffer[i] != '\0'; i++)
6717 {
6718 if (tbuffer[i] == DLG_BUTTON_SEP)
6719 numButtons++;
6720 }
6721 if (dfltbutton >= numButtons)
6722 dfltbutton = -1;
6723
Bram Moolenaar734a8672019-12-02 22:49:38 +01006724 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006725 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (buttonWidths == NULL)
6727 return -1;
6728
Bram Moolenaar734a8672019-12-02 22:49:38 +01006729 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006730 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (buttonPositions == NULL)
6732 return -1;
6733
6734 /*
6735 * Calculate how big the dialog must be.
6736 */
6737 hwnd = GetDesktopWindow();
6738 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006739# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6741 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006742 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 use_lfSysmenu = TRUE;
6744 }
6745 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006746# endif
K.Takatad1c58992022-01-23 12:31:57 +00006747 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6748 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6749
6750 oldFont = SelectFont(hdc, font);
6751 dlgPaddingX = DLG_PADDING_X;
6752 dlgPaddingY = DLG_PADDING_Y;
6753
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 GetTextMetrics(hdc, &fontInfo);
6755 fontHeight = fontInfo.tmHeight;
6756
Bram Moolenaar734a8672019-12-02 22:49:38 +01006757 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006758 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaar734a8672019-12-02 22:49:38 +01006760 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006761 if (s_hwnd == NULL)
6762 {
6763 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
Bram Moolenaar734a8672019-12-02 22:49:38 +01006765 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006766 get_work_area(&workarea_rect);
6767 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006768 if (maxDialogWidth > adjust_by_system_dpi(600))
6769 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006770 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006771 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006772 }
6773 else
6774 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006775 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006776 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006777 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006778 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6779 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6780 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6781 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006782
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006783 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006784 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6785 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6786 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6787 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6788 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006789 }
6790
Bram Moolenaar734a8672019-12-02 22:49:38 +01006791 // Set dlgwidth to width of message.
6792 // Copy the message into "ga", changing NL to CR-NL and inserting line
6793 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 pstart = message;
6795 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006796 msgheight = 0;
6797 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 do
6799 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006800 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006801
Bram Moolenaar734a8672019-12-02 22:49:38 +01006802 // Need to figure out where to break the string. The system does it
6803 // at a word boundary, which would mean we can't compute the number of
6804 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006805 textWidth = 0;
6806 last_white = NULL;
6807 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006808 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006809 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006810 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006811 && textWidth > maxDialogWidth * 3 / 4)
6812 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006813 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006814 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006815 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006816 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006817 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006818 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006819 textWidth = 0;
6820
6821 if (last_white != NULL)
6822 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006823 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006824 if (pend > last_white)
6825 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006826 pend = last_white + 1;
6827 last_white = NULL;
6828 }
6829 ga_append(&ga, '\r');
6830 ga_append(&ga, '\n');
6831 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006832 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006833
6834 while (--l >= 0)
6835 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006836 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006837 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006839
6840 ga_append(&ga, '\r');
6841 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 pstart = pend + 1;
6843 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006844
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006845 if (ga.ga_data != NULL)
6846 message = ga.ga_data;
6847
Bram Moolenaar734a8672019-12-02 22:49:38 +01006848 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006849
K.Takatac81e9bf2022-01-16 14:15:49 +00006850 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6851 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
K.Takatac81e9bf2022-01-16 14:15:49 +00006853 // Add width of icon to dlgwidth, and some space
6854 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6855 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6856
6857 if (msgheight < dlg_icon_height)
6858 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
6860 /*
6861 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006862 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006864 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (!vertical)
6866 {
6867 // Place buttons horizontally if they fit.
6868 horizWidth = dlgPaddingX;
6869 pstart = tbuffer;
6870 i = 0;
6871 do
6872 {
6873 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6874 if (pend == NULL)
6875 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006876 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 if (textWidth < minButtonWidth)
6878 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006879 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 buttonWidths[i] = textWidth;
6881 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006882 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 pstart = pend + 1;
6884 } while (*pend != NUL);
6885
6886 if (horizWidth > maxDialogWidth)
6887 vertical = TRUE; // Too wide to fit on the screen.
6888 else if (horizWidth > dlgwidth)
6889 dlgwidth = horizWidth;
6890 }
6891
6892 if (vertical)
6893 {
6894 // Stack buttons vertically.
6895 pstart = tbuffer;
6896 do
6897 {
6898 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6899 if (pend == NULL)
6900 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006901 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006902 textWidth += dlgPaddingX; // Padding within button
6903 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (textWidth > dlgwidth)
6905 dlgwidth = textWidth;
6906 pstart = pend + 1;
6907 } while (*pend != NUL);
6908 }
6909
6910 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
Bram Moolenaar734a8672019-12-02 22:49:38 +01006913 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006914 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
6916 add_long(lStyle);
6917 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006918 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 add_word(0); // NumberOfItems(will change later)
6920 add_word(10); // x
6921 add_word(10); // y
6922 add_word(PixelToDialogX(dlgwidth)); // cx
6923
6924 // Dialog height.
6925 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006926 dlgheight = msgheight + 2 * dlgPaddingY
6927 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 else
6929 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6930
6931 // Dialog needs to be taller if contains an edit box.
6932 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6933 if (textfield != NULL)
6934 dlgheight += editboxheight;
6935
Bram Moolenaar734a8672019-12-02 22:49:38 +01006936 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006937 if (dlgheight > maxDialogHeight)
6938 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006939 msgheight = msgheight - (dlgheight - maxDialogHeight);
6940 dlgheight = maxDialogHeight;
6941 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00006943 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006944 }
6945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 add_word(PixelToDialogY(dlgheight));
6947
6948 add_word(0); // Menu
6949 add_word(0); // Class
6950
Bram Moolenaar734a8672019-12-02 22:49:38 +01006951 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006952 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6953 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 p += nchar;
6955
K.Takatad1c58992022-01-23 12:31:57 +00006956 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006957# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00006958 if (use_lfSysmenu)
6959 {
6960 // point size
6961 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6962 GetDeviceCaps(hdc, LOGPIXELSY));
6963 wcscpy(p, lfSysmenu.lfFaceName);
6964 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 }
K.Takatad1c58992022-01-23 12:31:57 +00006966 else
6967# endif
6968 {
6969 *p++ = DLG_FONT_POINT_SIZE; // point size
6970 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
6971 }
6972 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974 buttonYpos = msgheight + 2 * dlgPaddingY;
6975
6976 if (textfield != NULL)
6977 buttonYpos += editboxheight;
6978
6979 pstart = tbuffer;
6980 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006981 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 for (i = 0; i < numButtons; i++)
6983 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006984 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 for ( pend = pstart;
6986 *pend && (*pend != DLG_BUTTON_SEP);
6987 pend++)
6988 ;
6989
6990 if (*pend)
6991 *pend = '\0';
6992
6993 /*
6994 * old NOTE:
6995 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6996 * the focus to the first tab-able button and in so doing makes that
6997 * the default!! Grrr. Workaround: Make the default button the only
6998 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6999 * he/she can use arrow keys.
7000 *
7001 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007002 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 * dialog. Also needed for when the textfield is the default control.
7004 * It appears to work now (perhaps not on Win95?).
7005 */
7006 if (vertical)
7007 {
7008 p = add_dialog_element(p,
7009 (i == dfltbutton
7010 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7011 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007012 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 + 2 * fontHeight * i),
7014 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7015 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007016 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018 else
7019 {
7020 p = add_dialog_element(p,
7021 (i == dfltbutton
7022 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7023 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007024 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 PixelToDialogX(buttonWidths[i]),
7026 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007027 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007029 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
7031 *pnumitems += numButtons;
7032
Bram Moolenaar734a8672019-12-02 22:49:38 +01007033 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 p = add_dialog_element(p, SS_ICON,
7035 PixelToDialogX(dlgPaddingX),
7036 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007037 PixelToDialogX(dlg_icon_width),
7038 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7040 dlg_icons[type]);
7041
Bram Moolenaar734a8672019-12-02 22:49:38 +01007042 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007043 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007044 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007045 PixelToDialogY(dlgPaddingY),
7046 (WORD)(PixelToDialogX(messageWidth) + 1),
7047 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007048 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
Bram Moolenaar734a8672019-12-02 22:49:38 +01007050 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 if (textfield != NULL)
7052 {
7053 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7054 PixelToDialogX(2 * dlgPaddingX),
7055 PixelToDialogY(2 * dlgPaddingY + msgheight),
7056 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7057 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007058 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 *pnumitems += 1;
7060 }
7061
7062 *pnumitems += 2;
7063
7064 SelectFont(hdc, oldFont);
7065 DeleteObject(font);
7066 ReleaseDC(hwnd, hdc);
7067
Bram Moolenaar734a8672019-12-02 22:49:38 +01007068 // Let the dialog_callback() function know which button to make default
7069 // If we have an edit box, make that the default. We also need to tell
7070 // dialog_callback() if this dialog contains an edit box or not. We do
7071 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (textfield != NULL)
7073 {
7074 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7075 s_textfield = textfield;
7076 }
7077 else
7078 {
7079 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7080 s_textfield = NULL;
7081 }
7082
Bram Moolenaar734a8672019-12-02 22:49:38 +01007083 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007085 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 (LPDLGTEMPLATE)pdlgtemplate,
7087 s_hwnd,
7088 (DLGPROC)dialog_callback);
7089
7090 LocalFree(LocalHandle(pdlgtemplate));
7091 vim_free(tbuffer);
7092 vim_free(buttonWidths);
7093 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007094 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
Bram Moolenaar734a8672019-12-02 22:49:38 +01007096 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 (void)SetFocus(s_hwnd);
7098
7099 return nchar;
7100}
7101
Bram Moolenaar734a8672019-12-02 22:49:38 +01007102#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104/*
7105 * Put a simple element (basic class) onto a dialog template in memory.
7106 * return a pointer to where the next item should be added.
7107 *
7108 * parameters:
7109 * lStyle = additional style flags
7110 * (be careful, NT3.51 & Win32s will ignore the new ones)
7111 * x,y = x & y positions IN DIALOG UNITS
7112 * w,h = width and height IN DIALOG UNITS
7113 * Id = ID used in messages
7114 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7115 * caption = usually text or resource name
7116 *
7117 * TODO: use the length information noted here to enable the dialog creation
7118 * routines to work out more exactly how much memory they need to alloc.
7119 */
7120 static PWORD
7121add_dialog_element(
7122 PWORD p,
7123 DWORD lStyle,
7124 WORD x,
7125 WORD y,
7126 WORD w,
7127 WORD h,
7128 WORD Id,
7129 WORD clss,
7130 const char *caption)
7131{
7132 int nchar;
7133
Bram Moolenaar734a8672019-12-02 22:49:38 +01007134 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7136 *p++ = LOWORD(lStyle);
7137 *p++ = HIWORD(lStyle);
7138 *p++ = 0; // LOWORD (lExtendedStyle)
7139 *p++ = 0; // HIWORD (lExtendedStyle)
7140 *p++ = x;
7141 *p++ = y;
7142 *p++ = w;
7143 *p++ = h;
7144 *p++ = Id; //9 or 10 words in all
7145
7146 *p++ = (WORD)0xffff;
7147 *p++ = clss; //2 more here
7148
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007149 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 p += nchar;
7151
7152 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7153
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007154 return p; // total = 15 + strlen(caption) words
7155 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156}
7157
7158
7159/*
7160 * Helper routine. Take an input pointer, return closest pointer that is
7161 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7162 */
7163 static LPWORD
7164lpwAlign(
7165 LPWORD lpIn)
7166{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007167 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 ul += 3;
7171 ul >>= 2;
7172 ul <<= 2;
7173 return (LPWORD)ul;
7174}
7175
7176/*
7177 * Helper routine. Takes second parameter as Ansi string, copies it to first
7178 * parameter as wide character (16-bits / char) string, and returns integer
7179 * number of wide characters (words) in string (including the trailing wide
7180 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007181 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7182 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 static int
7184nCopyAnsiToWideChar(
7185 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007186 LPSTR lpAnsiIn,
7187 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007190 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 int i;
7192 WCHAR *wn;
7193
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007194 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007196 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007197 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 if (wn != NULL)
7199 {
7200 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007201 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 vim_free(wn);
7203 }
7204 }
7205 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007206 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 nChar = MultiByteToWideChar(
7208 enc_codepage > 0 ? enc_codepage : CP_ACP,
7209 MB_PRECOMPOSED,
7210 lpAnsiIn, len,
7211 lpWCStr, len);
7212 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007213 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
7216 return nChar;
7217}
7218
7219
7220#ifdef FEAT_TEAROFF
7221/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007222 * Lookup menu handle from "menu_id".
7223 */
7224 static HMENU
7225tearoff_lookup_menuhandle(
7226 vimmenu_T *menu,
7227 WORD menu_id)
7228{
7229 for ( ; menu != NULL; menu = menu->next)
7230 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007231 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007232 continue;
7233 if (menu_is_separator(menu->dname))
7234 continue;
7235 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7236 return menu->submenu_id;
7237 }
7238 return NULL;
7239}
7240
7241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 * The callback function for all the modeless dialogs that make up the
7243 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7244 * thinking its menus have been clicked), and go away when closed.
7245 */
7246 static LRESULT CALLBACK
7247tearoff_callback(
7248 HWND hwnd,
7249 UINT message,
7250 WPARAM wParam,
7251 LPARAM lParam)
7252{
7253 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007254 {
7255 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaar734a8672019-12-02 22:49:38 +01007259 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 HandleMouseHide(message, lParam);
7261
7262 if (message == WM_COMMAND)
7263 {
7264 if ((WORD)(LOWORD(wParam)) & 0x8000)
7265 {
7266 POINT mp;
7267 RECT rect;
7268
7269 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7270 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007271 vimmenu_T *menu;
7272
7273 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007275 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7277 (int)rect.right - 8,
7278 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007279 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 s_hwnd,
7281 NULL);
7282 /*
7283 * NOTE: The pop-up menu can eat the mouse up event.
7284 * We deal with this in normal.c.
7285 */
7286 }
7287 }
7288 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007289 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7291 /*
7292 * Give main window the focus back: this is so after
7293 * choosing a tearoff button you can start typing again
7294 * straight away.
7295 */
7296 (void)SetFocus(s_hwnd);
7297 return TRUE;
7298 }
7299 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7300 {
7301 DestroyWindow(hwnd);
7302 return TRUE;
7303 }
7304
Bram Moolenaar734a8672019-12-02 22:49:38 +01007305 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (message == WM_EXITSIZEMOVE)
7307 (void)SetActiveWindow(s_hwnd);
7308
7309 return FALSE;
7310}
7311#endif
7312
7313
7314/*
K.Takatad1c58992022-01-23 12:31:57 +00007315 * Computes the dialog base units based on the current dialog font.
7316 * We don't use the GetDialogBaseUnits() API, because we don't use the
7317 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 */
7319 static void
7320get_dialog_font_metrics(void)
7321{
7322 HDC hdc;
7323 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 SIZE size;
7325#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007326 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#endif
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007330 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007331 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333#endif
7334 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007335 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
K.Takatad1c58992022-01-23 12:31:57 +00007337 hdc = GetDC(s_hwnd);
7338 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007339 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007340 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
K.Takataabe628e2022-01-23 16:25:17 +00007342 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007343 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7347/*
7348 * Create a pseudo-"tearoff menu" based on the child
7349 * items of a given menu pointer.
7350 */
7351 static void
7352gui_mch_tearoff(
7353 char_u *title,
7354 vimmenu_T *menu,
7355 int initX,
7356 int initY)
7357{
7358 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7359 int template_len;
7360 int nchar, textWidth, submenuWidth;
7361 DWORD lStyle;
7362 DWORD lExtendedStyle;
7363 WORD dlgwidth;
7364 WORD menuID;
7365 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007366 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 vimmenu_T *the_menu = menu;
7368 HWND hwnd;
7369 HDC hdc;
7370 HFONT font, oldFont;
7371 int col, spaceWidth, len;
7372 int columnWidths[2];
7373 char_u *label, *text;
7374 int acLen = 0;
7375 int nameLen;
7376 int padding0, padding1, padding2 = 0;
7377 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007378 int x;
7379 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007380# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007381 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 /*
7386 * If this menu is already torn off, move it to the mouse position.
7387 */
7388 if (IsWindow(menu->tearoff_handle))
7389 {
7390 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007391 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
7393 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7394 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7395 }
7396 return;
7397 }
7398
7399 /*
7400 * Create a new tearoff.
7401 */
7402 if (*title == MNU_HIDDEN_CHAR)
7403 title++;
7404
Bram Moolenaar734a8672019-12-02 22:49:38 +01007405 // Allocate memory to store the dialog template. It's made bigger when
7406 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 template_len = DLG_ALLOC_SIZE;
7408 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7409 if (p == NULL)
7410 return;
7411
7412 hwnd = GetDesktopWindow();
7413 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007414# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7416 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007417 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 use_lfSysmenu = TRUE;
7419 }
7420 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007421# endif
K.Takatad1c58992022-01-23 12:31:57 +00007422 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7423 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7424
7425 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
Bram Moolenaar734a8672019-12-02 22:49:38 +01007427 // Calculate width of a single space. Used for padding columns to the
7428 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007429 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
Bram Moolenaar734a8672019-12-02 22:49:38 +01007431 // Figure out max width of the text column, the accelerator column and the
7432 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 submenuWidth = 0;
7434 for (col = 0; col < 2; col++)
7435 {
7436 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007437 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007439 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 text = (col == 0) ? pmenu->dname : pmenu->actext;
7441 if (text != NULL && *text != NUL)
7442 {
7443 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7444 if (textWidth > columnWidths[col])
7445 columnWidths[col] = textWidth;
7446 }
7447 if (pmenu->children != NULL)
7448 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7449 }
7450 }
7451 if (columnWidths[1] == 0)
7452 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007453 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (submenuWidth != 0)
7455 columnWidths[0] += submenuWidth;
7456 else
7457 columnWidths[0] += spaceWidth;
7458 }
7459 else
7460 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007461 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7463 columnWidths[1] += submenuWidth;
7464 }
7465
7466 /*
7467 * Now find the total width of our 'menu'.
7468 */
7469 textWidth = columnWidths[0] + columnWidths[1];
7470 if (submenuWidth != 0)
7471 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007472 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7474 textWidth += submenuWidth;
7475 }
7476 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7477 if (textWidth > dlgwidth)
7478 dlgwidth = textWidth;
7479 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7480
Bram Moolenaar734a8672019-12-02 22:49:38 +01007481 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007482 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
7484 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7485 *p++ = LOWORD(lStyle);
7486 *p++ = HIWORD(lStyle);
7487 *p++ = LOWORD(lExtendedStyle);
7488 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007489 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007491 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007493 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 else
7495 *p++ = PixelToDialogX(initX); // x
7496 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007497 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 else
7499 *p++ = PixelToDialogY(initY); // y
7500 *p++ = PixelToDialogX(dlgwidth); // cx
7501 ptrueheight = p;
7502 *p++ = 0; // dialog height: changed later anyway
7503 *p++ = 0; // Menu
7504 *p++ = 0; // Class
7505
Bram Moolenaar734a8672019-12-02 22:49:38 +01007506 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007508 ? (LPSTR)title
7509 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 p += nchar;
7511
K.Takatad1c58992022-01-23 12:31:57 +00007512 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007513# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007514 if (use_lfSysmenu)
7515 {
7516 // point size
7517 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7518 GetDeviceCaps(hdc, LOGPIXELSY));
7519 wcscpy(p, lfSysmenu.lfFaceName);
7520 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
K.Takatad1c58992022-01-23 12:31:57 +00007522 else
7523# endif
7524 {
7525 *p++ = DLG_FONT_POINT_SIZE; // point size
7526 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7527 }
7528 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529
7530 /*
7531 * Loop over all the items in the menu.
7532 * But skip over the tearbar.
7533 */
7534 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7535 menu = menu->children->next;
7536 else
7537 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007538 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 for ( ; menu != NULL; menu = menu->next)
7540 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007541 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 continue;
7543 if (menu_is_separator(menu->dname))
7544 {
7545 sepPadding += 3;
7546 continue;
7547 }
7548
Bram Moolenaar734a8672019-12-02 22:49:38 +01007549 // Check if there still is plenty of room in the template. Make it
7550 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7552 {
7553 WORD *newp;
7554
7555 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7556 if (newp != NULL)
7557 {
7558 template_len += 4096;
7559 mch_memmove(newp, pdlgtemplate,
7560 (char *)p - (char *)pdlgtemplate);
7561 p = newp + (p - pdlgtemplate);
7562 pnumitems = newp + (pnumitems - pdlgtemplate);
7563 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7564 LocalFree(LocalHandle(pdlgtemplate));
7565 pdlgtemplate = newp;
7566 }
7567 }
7568
Bram Moolenaar734a8672019-12-02 22:49:38 +01007569 // Figure out minimal length of this menu label. Use "name" for the
7570 // actual text, "dname" for estimating the displayed size. "name"
7571 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 len = nameLen = (int)STRLEN(menu->name);
7573 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7574 (int)STRLEN(menu->dname))) / spaceWidth;
7575 len += padding0;
7576
7577 if (menu->actext != NULL)
7578 {
7579 acLen = (int)STRLEN(menu->actext);
7580 len += acLen;
7581 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7582 }
7583 else
7584 textWidth = 0;
7585 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7586 len += padding1;
7587
7588 if (menu->children == NULL)
7589 {
7590 padding2 = submenuWidth / spaceWidth;
7591 len += padding2;
7592 menuID = (WORD)(menu->id);
7593 }
7594 else
7595 {
7596 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007597 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599
Bram Moolenaar734a8672019-12-02 22:49:38 +01007600 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007601 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (label == NULL)
7603 break;
7604
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007605 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007606 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007608 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 while (padding0-- > 0)
7610 *text++ = ' ';
7611 if (menu->actext != NULL)
7612 {
7613 STRNCPY(text, menu->actext, acLen);
7614 text += acLen;
7615 }
7616 while (padding1-- > 0)
7617 *text++ = ' ';
7618 if (menu->children != NULL)
7619 {
7620 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7621 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7622 }
7623 else
7624 {
7625 while (padding2-- > 0)
7626 *text++ = ' ';
7627 }
7628 *text = NUL;
7629
7630 /*
7631 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7632 * W95/NT4 it makes the tear-off look more like a menu.
7633 */
7634 p = add_dialog_element(p,
7635 BS_PUSHBUTTON|BS_LEFT,
7636 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7637 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7638 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7639 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007640 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 vim_free(label);
7642 (*pnumitems)++;
7643 }
7644
7645 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7646
7647
Bram Moolenaar734a8672019-12-02 22:49:38 +01007648 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007649 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007650 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 (LPDLGTEMPLATE)pdlgtemplate,
7652 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007653 (DLGPROC)tearoff_callback,
7654 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
7656 LocalFree(LocalHandle(pdlgtemplate));
7657 SelectFont(hdc, oldFont);
7658 DeleteObject(font);
7659 ReleaseDC(hwnd, hdc);
7660
7661 /*
7662 * Reassert ourselves as the active window. This is so that after creating
7663 * a tearoff, the user doesn't have to click with the mouse just to start
7664 * typing again!
7665 */
7666 (void)SetActiveWindow(s_hwnd);
7667
Bram Moolenaar734a8672019-12-02 22:49:38 +01007668 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 force_menu_update = TRUE;
7670}
7671#endif
7672
7673#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007674# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676/*
7677 * Create the toolbar, initially unpopulated.
7678 * (just like the menu, there are no defaults, it's all
7679 * set up through menu.vim)
7680 */
7681 static void
7682initialise_toolbar(void)
7683{
7684 InitCommonControls();
7685 s_toolbarhwnd = CreateToolbarEx(
7686 s_hwnd,
7687 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7688 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007689 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007690 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 IDR_TOOLBAR1, // id of initial bitmap
7692 NULL,
7693 0, // initial number of buttons
7694 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7695 TOOLBAR_BUTTON_HEIGHT,
7696 TOOLBAR_BUTTON_WIDTH,
7697 TOOLBAR_BUTTON_HEIGHT,
7698 sizeof(TBBUTTON)
7699 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007700
7701 // Remove transparency from the toolbar to prevent the main window
7702 // background colour showing through
7703 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7704 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7705
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007706 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707
7708 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007709
7710 update_toolbar_size();
7711}
7712
7713 static void
7714update_toolbar_size(void)
7715{
7716 int w, h;
7717 TBMETRICS tbm = {sizeof(TBMETRICS)};
7718
7719 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7720 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7721 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7722 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7723
7724 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7725 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7726 //TRACE("button size: %d, %d", w, h);
7727 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7728 gui.toolbar_height = h + 6;
7729
7730 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7731 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7732
7733 // TODO:
7734 // Currently, this function only updates the size of toolbar buttons.
7735 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007738 static LRESULT CALLBACK
7739toolbar_wndproc(
7740 HWND hwnd,
7741 UINT uMsg,
7742 WPARAM wParam,
7743 LPARAM lParam)
7744{
7745 HandleMouseHide(uMsg, lParam);
7746 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7747}
7748
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 static int
7750get_toolbar_bitmap(vimmenu_T *menu)
7751{
7752 int i = -1;
7753
7754 /*
7755 * Check user bitmaps first, unless builtin is specified.
7756 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007757 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
7759 char_u fname[MAXPATHL];
7760 HANDLE hbitmap = NULL;
7761
7762 if (menu->iconfile != NULL)
7763 {
7764 gui_find_iconfile(menu->iconfile, fname, "bmp");
7765 hbitmap = LoadImage(
7766 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007767 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 IMAGE_BITMAP,
7769 TOOLBAR_BUTTON_WIDTH,
7770 TOOLBAR_BUTTON_HEIGHT,
7771 LR_LOADFROMFILE |
7772 LR_LOADMAP3DCOLORS
7773 );
7774 }
7775
7776 /*
7777 * If the LoadImage call failed, or the "icon=" file
7778 * didn't exist or wasn't specified, try the menu name
7779 */
7780 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007781 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007782# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007783 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007784# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007785 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 hbitmap = LoadImage(
7787 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007788 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 IMAGE_BITMAP,
7790 TOOLBAR_BUTTON_WIDTH,
7791 TOOLBAR_BUTTON_HEIGHT,
7792 LR_LOADFROMFILE |
7793 LR_LOADMAP3DCOLORS
7794 );
7795
7796 if (hbitmap != NULL)
7797 {
7798 TBADDBITMAP tbAddBitmap;
7799
7800 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007801 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
7803 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7804 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007805 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807 }
7808 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7809 i = menu->iconidx;
7810
7811 return i;
7812}
7813#endif
7814
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007815#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7816 static void
7817initialise_tabline(void)
7818{
7819 InitCommonControls();
7820
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007821 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007822 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007823 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007824 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007825 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007826
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007827 gui.tabline_height = TABLINE_HEIGHT;
7828
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007829 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007830}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007831
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007832/*
7833 * Get tabpage_T from POINT.
7834 */
7835 static tabpage_T *
7836GetTabFromPoint(
7837 HWND hWnd,
7838 POINT pt)
7839{
7840 tabpage_T *ptp = NULL;
7841
7842 if (gui_mch_showing_tabline())
7843 {
7844 TCHITTESTINFO htinfo;
7845 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007846 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007847 if (s_tabhwnd == hWnd)
7848 {
7849 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7850 if (idx != -1)
7851 ptp = find_tabpage(idx + 1);
7852 }
7853 }
7854 return ptp;
7855}
7856
7857static POINT s_pt = {0, 0};
7858static HCURSOR s_hCursor = NULL;
7859
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007860 static LRESULT CALLBACK
7861tabline_wndproc(
7862 HWND hwnd,
7863 UINT uMsg,
7864 WPARAM wParam,
7865 LPARAM lParam)
7866{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007867 POINT pt;
7868 tabpage_T *tp;
7869 RECT rect;
7870 int nCenter;
7871 int idx0;
7872 int idx1;
7873
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007874 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007875
7876 switch (uMsg)
7877 {
7878 case WM_LBUTTONDOWN:
7879 {
7880 s_pt.x = GET_X_LPARAM(lParam);
7881 s_pt.y = GET_Y_LPARAM(lParam);
7882 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007883 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007884 break;
7885 }
7886 case WM_MOUSEMOVE:
7887 if (GetCapture() == hwnd
7888 && ((wParam & MK_LBUTTON)) != 0)
7889 {
7890 pt.x = GET_X_LPARAM(lParam);
7891 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007892 if (abs(pt.x - s_pt.x) >
7893 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007894 {
7895 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7896
7897 tp = GetTabFromPoint(hwnd, pt);
7898 if (tp != NULL)
7899 {
7900 idx0 = tabpage_index(curtab) - 1;
7901 idx1 = tabpage_index(tp) - 1;
7902
7903 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7904 nCenter = rect.left + (rect.right - rect.left) / 2;
7905
Bram Moolenaar734a8672019-12-02 22:49:38 +01007906 // Check if the mouse cursor goes over the center of
7907 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007908 if ((idx0 < idx1) && (nCenter < pt.x))
7909 {
7910 tabpage_move(idx1 + 1);
7911 update_screen(0);
7912 }
7913 else if ((idx1 < idx0) && (pt.x < nCenter))
7914 {
7915 tabpage_move(idx1);
7916 update_screen(0);
7917 }
7918 }
7919 }
7920 }
7921 break;
7922 case WM_LBUTTONUP:
7923 {
7924 if (GetCapture() == hwnd)
7925 {
7926 SetCursor(s_hCursor);
7927 ReleaseCapture();
7928 }
7929 break;
7930 }
7931 default:
7932 break;
7933 }
7934
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007935 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7936}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007937#endif
7938
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7940/*
7941 * Make the GUI window come to the foreground.
7942 */
7943 void
7944gui_mch_set_foreground(void)
7945{
7946 if (IsIconic(s_hwnd))
7947 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7948 SetForegroundWindow(s_hwnd);
7949}
7950#endif
7951
7952#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7953 static void
7954dyn_imm_load(void)
7955{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007956 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 if (hLibImm == NULL)
7958 return;
7959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 pImmGetCompositionStringW
7961 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7962 pImmGetContext
7963 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7964 pImmAssociateContext
7965 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7966 pImmReleaseContext
7967 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7968 pImmGetOpenStatus
7969 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7970 pImmSetOpenStatus
7971 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007972 pImmGetCompositionFontW
7973 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7974 pImmSetCompositionFontW
7975 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 pImmSetCompositionWindow
7977 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7978 pImmGetConversionStatus
7979 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007980 pImmSetConversionStatus
7981 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
K.Takatab0b2b732022-01-19 12:59:21 +00007983 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 || pImmGetContext == NULL
7985 || pImmAssociateContext == NULL
7986 || pImmReleaseContext == NULL
7987 || pImmGetOpenStatus == NULL
7988 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007989 || pImmGetCompositionFontW == NULL
7990 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007992 || pImmGetConversionStatus == NULL
7993 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 FreeLibrary(hLibImm);
7996 hLibImm = NULL;
7997 pImmGetContext = NULL;
7998 return;
7999 }
8000
8001 return;
8002}
8003
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004#endif
8005
8006#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8007
8008# ifdef FEAT_XPM_W32
8009# define IMAGE_XPM 100
8010# endif
8011
8012typedef struct _signicon_t
8013{
8014 HANDLE hImage;
8015 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008016# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008017 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019} signicon_t;
8020
8021 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008022gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 signicon_t *sign;
8025 int x, y, w, h;
8026
8027 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8028 return;
8029
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008030# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008031 if (IS_ENABLE_DIRECTX())
8032 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008033# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008034
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 x = TEXT_X(col);
8036 y = TEXT_Y(row);
8037 w = gui.char_width * 2;
8038 h = gui.char_height;
8039 switch (sign->uType)
8040 {
8041 case IMAGE_BITMAP:
8042 {
8043 HDC hdcMem;
8044 HBITMAP hbmpOld;
8045
8046 hdcMem = CreateCompatibleDC(s_hdc);
8047 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8048 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8049 SelectObject(hdcMem, hbmpOld);
8050 DeleteDC(hdcMem);
8051 }
8052 break;
8053 case IMAGE_ICON:
8054 case IMAGE_CURSOR:
8055 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8056 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008057# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 case IMAGE_XPM:
8059 {
8060 HDC hdcMem;
8061 HBITMAP hbmpOld;
8062
8063 hdcMem = CreateCompatibleDC(s_hdc);
8064 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008065 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8067
8068 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008069 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8071 SelectObject(hdcMem, hbmpOld);
8072 DeleteDC(hdcMem);
8073 }
8074 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 }
8077}
8078
8079 static void
8080close_signicon_image(signicon_t *sign)
8081{
8082 if (sign)
8083 switch (sign->uType)
8084 {
8085 case IMAGE_BITMAP:
8086 DeleteObject((HGDIOBJ)sign->hImage);
8087 break;
8088 case IMAGE_CURSOR:
8089 DestroyCursor((HCURSOR)sign->hImage);
8090 break;
8091 case IMAGE_ICON:
8092 DestroyIcon((HICON)sign->hImage);
8093 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008094# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 case IMAGE_XPM:
8096 DeleteObject((HBITMAP)sign->hImage);
8097 DeleteObject((HBITMAP)sign->hShape);
8098 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
8101}
8102
8103 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008104gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 signicon_t sign, *psign;
8107 char_u *ext;
8108
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008110 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (ext > signfile)
8112 {
8113 int do_load = 1;
8114
8115 if (!STRICMP(ext, ".bmp"))
8116 sign.uType = IMAGE_BITMAP;
8117 else if (!STRICMP(ext, ".ico"))
8118 sign.uType = IMAGE_ICON;
8119 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8120 sign.uType = IMAGE_CURSOR;
8121 else
8122 do_load = 0;
8123
8124 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008125 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 gui.char_width * 2, gui.char_height,
8127 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008128# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 if (!STRICMP(ext, ".xpm"))
8130 {
8131 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008132 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8133 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008135# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137
8138 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008139 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 *psign = sign;
8141
8142 if (!psign)
8143 {
8144 if (sign.hImage)
8145 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008146 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 return (void *)psign;
8149
8150}
8151
8152 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008153gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154{
8155 if (sign)
8156 {
8157 close_signicon_image((signicon_t *)sign);
8158 vim_free(sign);
8159 }
8160}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008163#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
Bram Moolenaar734a8672019-12-02 22:49:38 +01008165/*
8166 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008167 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008169 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8171 * to get current mouse position).
8172 *
8173 * Trying to use as more Windows services as possible, and as less
8174 * IE version as possible :)).
8175 *
8176 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8177 * BalloonEval struct.
8178 * 2) Enable/Disable simply create/kill BalloonEval Timer
8179 * 3) When there was enough inactivity, timer procedure posts
8180 * async request to debugger
8181 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8182 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008183 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 */
8185
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008186 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008187make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008188{
K.Takata76687d22022-01-25 10:31:37 +00008189 TOOLINFOW *pti;
8190 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008191
K.Takata76687d22022-01-25 10:31:37 +00008192 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008193 if (pti == NULL)
8194 return;
8195
8196 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8197 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8198 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008199 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008200
8201 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8202 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8203
K.Takata76687d22022-01-25 10:31:37 +00008204 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008205 pti->uFlags = TTF_SUBCLASS;
8206 pti->hwnd = beval->target;
8207 pti->hinst = 0; // Don't use string resources
8208 pti->uId = ID_BEVAL_TOOLTIP;
8209
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008210 pti->lpszText = LPSTR_TEXTCALLBACKW;
8211 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8212 pti->lParam = (LPARAM)beval->tofree;
8213 // switch multiline tooltips on
8214 if (GetClientRect(s_textArea, &rect))
8215 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8216 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008217
8218 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8219 pti->rect.left = pt.x - 3;
8220 pti->rect.top = pt.y - 3;
8221 pti->rect.right = pt.x + 3;
8222 pti->rect.bottom = pt.y + 3;
8223
8224 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8225 // Make tooltip appear sooner.
8226 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8227 // I've performed some tests and it seems the longest possible life time
8228 // of tooltip is 30 seconds.
8229 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8230 /*
8231 * HACK: force tooltip to appear, because it'll not appear until
8232 * first mouse move. D*mn M$
8233 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8234 */
8235 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8236 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8237 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008238}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008239
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008241delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008243 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244}
8245
8246 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008247beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008248 HWND hwnd UNUSED,
8249 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008250 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008251 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252{
8253 POINT pt;
8254 RECT rect;
8255
8256 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8257 return;
8258
8259 GetCursorPos(&pt);
8260 if (WindowFromPoint(pt) != s_textArea)
8261 return;
8262
8263 ScreenToClient(s_textArea, &pt);
8264 GetClientRect(s_textArea, &rect);
8265 if (!PtInRect(&rect, pt))
8266 return;
8267
K.Takataa8ec4912022-02-03 14:32:33 +00008268 if (last_user_activity > 0
8269 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 && (cur_beval->showState != ShS_PENDING
8271 || abs(cur_beval->x - pt.x) > 3
8272 || abs(cur_beval->y - pt.y) > 3))
8273 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008274 // Pointer resting in one place long enough, it's time to show
8275 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 cur_beval->showState = ShS_PENDING;
8277 cur_beval->x = pt.x;
8278 cur_beval->y = pt.y;
8279
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (cur_beval->msgCB != NULL)
8281 (*cur_beval->msgCB)(cur_beval, 0);
8282 }
8283}
8284
8285 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008286gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
K.Takataa8ec4912022-02-03 14:32:33 +00008288 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289}
8290
8291 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008292gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (beval == NULL)
8295 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008296 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8297 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
8299
8300 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008301gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
8303 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008304
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008305 vim_free(beval->msg);
8306 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8307 if (beval->msg == NULL)
8308 {
8309 delete_tooltip(beval);
8310 beval->showState = ShS_NEUTRAL;
8311 return;
8312 }
8313
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (beval->showState == ShS_SHOWING)
8315 return;
8316 GetCursorPos(&pt);
8317 ScreenToClient(s_textArea, &pt);
8318
8319 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008321 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 gui_mch_disable_beval_area(cur_beval);
8323 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008324 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326}
8327
8328 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008329gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008330 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008331 char_u *mesg,
8332 void (*mesgCB)(BalloonEval *, int),
8333 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008335 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 BalloonEval *beval;
8337
8338 if (mesg != NULL && mesgCB != NULL)
8339 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008340 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 return NULL;
8342 }
8343
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008344 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (beval != NULL)
8346 {
8347 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 beval->msg = mesg;
8351 beval->msgCB = mesgCB;
8352 beval->clientData = clientData;
8353
8354 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 cur_beval = beval;
8356
8357 if (p_beval)
8358 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
8360 return beval;
8361}
8362
8363 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008364Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008366 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 return;
8368
8369 if (cur_beval != NULL)
8370 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008371 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008373 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008374 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008375 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 delete_tooltip(cur_beval);
8377 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008380 break;
8381 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008382 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008383 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008384 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008385 info->lpszText = (LPSTR)info->lParam;
8386 info->uFlags |= TTF_DI_SETITEM;
8387 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008388 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008389 case TTN_GETDISPINFOW:
8390 {
8391 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008392 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008393 info->lpszText = (LPWSTR)info->lParam;
8394 info->uFlags |= TTF_DI_SETITEM;
8395 }
8396 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 }
8398 }
8399}
8400
8401 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008402track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8405 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008406 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407}
8408
8409 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008410gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008412# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008413 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008414# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008415 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 vim_free(beval);
8417}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008418#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
8420#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8421/*
8422 * We have multiple signs to draw at the same location. Draw the
8423 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8424 */
8425 void
8426netbeans_draw_multisign_indicator(int row)
8427{
8428 int i;
8429 int y;
8430 int x;
8431
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008432 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008433 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 x = 0;
8436 y = TEXT_Y(row);
8437
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008438# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008439 if (IS_ENABLE_DIRECTX())
8440 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008441# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 for (i = 0; i < gui.char_height - 3; i++)
8444 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8445
8446 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8447 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8448 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8449 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8450 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8451 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8452 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8453}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008454#endif