blob: 4a8917faec2bd874cfa3535b3d307a02b29348d0 [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;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200520static UINT 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);
529static void TrackUserActivity(UINT uMsg);
530#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;
587static UINT blink_timer = 0;
588
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,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100613 UINT 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;
632 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
633 (TIMERPROC)_OnBlinkTimer);
634 }
635 else
636 {
637 gui_update_cursor(TRUE, FALSE);
638 blink_state = BLINK_ON;
639 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100640 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100642 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100643}
644
645 static void
646gui_mswin_rm_blink_timer(void)
647{
648 MSG msg;
649
650 if (blink_timer != 0)
651 {
652 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100653 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000654 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100655 ;
656 blink_timer = 0;
657 }
658}
659
660/*
661 * Stop the cursor blinking. Show the cursor if it wasn't shown.
662 */
663 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100664gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665{
666 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100667 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100668 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100670 gui_mch_flush();
671 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672 blink_state = BLINK_NONE;
673}
674
675/*
676 * Start the cursor blinking. If it was already blinking, this restarts the
677 * waiting time and shows the cursor.
678 */
679 void
680gui_mch_start_blink(void)
681{
682 gui_mswin_rm_blink_timer();
683
Bram Moolenaar734a8672019-12-02 22:49:38 +0100684 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100685 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
686 {
687 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
688 (TIMERPROC)_OnBlinkTimer);
689 blink_state = BLINK_ON;
690 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100691 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100692 }
693}
694
695/*
696 * Call-back routines.
697 */
698
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100699 static VOID CALLBACK
700_OnTimer(
701 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100702 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100703 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100704 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100705{
706 MSG msg;
707
708 /*
709 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
710 */
711 KillTimer(NULL, idEvent);
712 s_timed_out = TRUE;
713
Bram Moolenaar734a8672019-12-02 22:49:38 +0100714 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000715 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716 ;
717 if (idEvent == s_wait_timer)
718 s_wait_timer = 0;
719}
720
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100721 static void
722_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100723 HWND hwnd UNUSED,
724 UINT ch UNUSED,
725 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100726{
727 dead_key = 1;
728}
729
730/*
731 * Convert Unicode character "ch" to bytes in "string[slen]".
732 * When "had_alt" is TRUE the ALT key was included in "ch".
733 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200734 * Because the Windows API uses UTF-16, we have to deal with surrogate
735 * pairs; this is where we choose to deal with them: if "ch" is a high
736 * surrogate, it will be stored, and the length returned will be zero; the next
737 * char_to_string call will then include the high surrogate, decoding the pair
738 * of UTF-16 code units to a single Unicode code point, presuming it is the
739 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100740 */
741 static int
742char_to_string(int ch, char_u *string, int slen, int had_alt)
743{
744 int len;
745 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100746 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200747 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100748
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200749 if (surrogate_pending_ch != 0)
750 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100751 // We don't guarantee ch is a low surrogate to match the high surrogate
752 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200753 wstring[0] = surrogate_pending_ch;
754 wstring[1] = ch;
755 surrogate_pending_ch = 0;
756 len = 2;
757 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100758 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200759 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100760 // We don't have the entire code point yet, only the first UTF-16 code
761 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200762 surrogate_pending_ch = ch;
763 return 0;
764 }
765 else
766 {
767 wstring[0] = ch;
768 len = 1;
769 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200770
Bram Moolenaar734a8672019-12-02 22:49:38 +0100771 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
772 // "enc_codepage" is non-zero use the standard Win32 function,
773 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200774 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200776 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
777 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100778 // If we had included the ALT key into the character but now the
779 // upper bit is no longer set, that probably means the conversion
780 // failed. Convert the original character and set the upper bit
781 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200782 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100783 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200784 wstring[0] = ch & 0x7f;
785 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
786 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100787 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200788 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100789 }
790 }
791 else
792 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 ws = utf16_to_enc(wstring, &len);
794 if (ws == NULL)
795 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 else
797 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100798 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799 len = slen;
800 mch_memmove(string, ws, len);
801 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100802 }
803 }
804
805 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100806 {
807 string[0] = ch;
808 len = 1;
809 }
810
811 for (i = 0; i < len; ++i)
812 if (string[i] == CSI && len <= slen - 2)
813 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100814 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100815 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
816 string[++i] = KS_EXTRA;
817 string[++i] = (int)KE_CSI;
818 len += 2;
819 }
820
821 return len;
822}
823
824/*
825 * Key hit, add it to the input buffer.
826 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827 static void
828_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100829 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100830 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100831 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100832{
833 char_u string[40];
834 int len = 0;
835
836 dead_key = 0;
837
838 len = char_to_string(ch, string, 40, FALSE);
839 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
840 {
841 trash_input_buf();
842 got_int = TRUE;
843 }
844
845 add_to_input_buf(string, len);
846}
847
848/*
849 * Alt-Key hit, add it to the input buffer.
850 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100851 static void
852_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100853 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100854 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100855 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100856{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100857 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100858 int len;
859 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100860 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861
862 dead_key = 0;
863
Bram Moolenaar734a8672019-12-02 22:49:38 +0100864 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100865
Bram Moolenaar734a8672019-12-02 22:49:38 +0100866 // OK, we have a character key (given by ch) which was entered with the
867 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
868 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
869 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870 modifiers = MOD_MASK_ALT;
871 if (GetKeyState(VK_SHIFT) & 0x8000)
872 modifiers |= MOD_MASK_SHIFT;
873 if (GetKeyState(VK_CONTROL) & 0x8000)
874 modifiers |= MOD_MASK_CTRL;
875
876 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100877 // remove the SHIFT modifier for keys where it's already included, e.g.,
878 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200879 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200881 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
882 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100883 if (ch == CSI)
884 ch = K_CSI;
885
886 len = 0;
887 if (modifiers)
888 {
889 string[len++] = CSI;
890 string[len++] = KS_MODIFIER;
891 string[len++] = modifiers;
892 }
893
894 if (IS_SPECIAL((int)ch))
895 {
896 string[len++] = CSI;
897 string[len++] = K_SECOND((int)ch);
898 string[len++] = K_THIRD((int)ch);
899 }
900 else
901 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100902 // Although the documentation isn't clear about it, we assume "ch" is
903 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100904 len += char_to_string(ch, string + len, 40 - len, TRUE);
905 }
906
907 add_to_input_buf(string, len);
908}
909
910 static void
911_OnMouseEvent(
912 int button,
913 int x,
914 int y,
915 int repeated_click,
916 UINT keyFlags)
917{
918 int vim_modifiers = 0x0;
919
920 s_getting_focus = FALSE;
921
922 if (keyFlags & MK_SHIFT)
923 vim_modifiers |= MOUSE_SHIFT;
924 if (keyFlags & MK_CONTROL)
925 vim_modifiers |= MOUSE_CTRL;
926 if (GetKeyState(VK_MENU) & 0x8000)
927 vim_modifiers |= MOUSE_ALT;
928
929 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
930}
931
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 static void
933_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100934 HWND hwnd UNUSED,
935 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100936 int x,
937 int y,
938 UINT keyFlags)
939{
940 static LONG s_prevTime = 0;
941
942 LONG currentTime = GetMessageTime();
943 int button = -1;
944 int repeated_click;
945
Bram Moolenaar734a8672019-12-02 22:49:38 +0100946 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100947 (void)SetFocus(s_hwnd);
948
949 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
950 button = MOUSE_LEFT;
951 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
952 button = MOUSE_MIDDLE;
953 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
954 button = MOUSE_RIGHT;
955 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
956 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100957 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
958 }
959 else if (s_uMsg == WM_CAPTURECHANGED)
960 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100961 // on W95/NT4, somehow you get in here with an odd Msg
962 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100963 if (s_button_pending == MOUSE_LEFT)
964 button = MOUSE_RIGHT;
965 else
966 button = MOUSE_LEFT;
967 }
968 if (button >= 0)
969 {
970 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
971
972 /*
973 * Holding down the left and right buttons simulates pushing the middle
974 * button.
975 */
976 if (repeated_click
977 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
978 || (button == MOUSE_RIGHT
979 && s_button_pending == MOUSE_LEFT)))
980 {
981 /*
982 * Hmm, gui.c will ignore more than one button down at a time, so
983 * pretend we let go of it first.
984 */
985 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
986 button = MOUSE_MIDDLE;
987 repeated_click = FALSE;
988 s_button_pending = -1;
989 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
990 }
991 else if ((repeated_click)
992 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
993 {
994 if (s_button_pending > -1)
995 {
K.Takata45f9cfb2022-01-21 11:11:00 +0000996 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
997 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100998 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100999 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001000 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1001 }
1002 else
1003 {
1004 /*
1005 * If this is the first press (i.e. not a multiple click) don't
1006 * action immediately, but store and wait for:
1007 * i) button-up
1008 * ii) mouse move
1009 * iii) another button press
1010 * before using it.
1011 * This enables us to make left+right simulate middle button,
1012 * without left or right being actioned first. The side-effect is
1013 * that if you click and hold the mouse without dragging, the
1014 * cursor doesn't move until you release the button. In practice
1015 * this is hardly a problem.
1016 */
1017 s_button_pending = button;
1018 s_x_pending = x;
1019 s_y_pending = y;
1020 s_kFlags_pending = keyFlags;
1021 }
1022
1023 s_prevTime = currentTime;
1024 }
1025}
1026
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001027 static void
1028_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001029 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001030 int x,
1031 int y,
1032 UINT keyFlags)
1033{
1034 int button;
1035
1036 s_getting_focus = FALSE;
1037 if (s_button_pending > -1)
1038 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001039 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001040 _OnMouseEvent(s_button_pending, s_x_pending,
1041 s_y_pending, FALSE, s_kFlags_pending);
1042 s_button_pending = -1;
1043 }
1044 if (s_uMsg == WM_MOUSEMOVE)
1045 {
1046 /*
1047 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1048 * down.
1049 */
1050 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1051 | MK_XBUTTON1 | MK_XBUTTON2)))
1052 {
1053 gui_mouse_moved(x, y);
1054 return;
1055 }
1056
1057 /*
1058 * While button is down, keep grabbing mouse move events when
1059 * the mouse goes outside the window
1060 */
1061 SetCapture(s_textArea);
1062 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001063 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001064 }
1065 else
1066 {
1067 ReleaseCapture();
1068 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001069 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001070 }
1071
1072 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1073}
1074
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001075 static void
1076_OnSizeTextArea(
1077 HWND hwnd UNUSED,
1078 UINT state UNUSED,
1079 int cx UNUSED,
1080 int cy UNUSED)
1081{
1082#if defined(FEAT_DIRECTX)
1083 if (IS_ENABLE_DIRECTX())
1084 directx_binddc();
1085#endif
1086}
1087
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001088#ifdef FEAT_MENU
1089/*
1090 * Find the vimmenu_T with the given id
1091 */
1092 static vimmenu_T *
1093gui_mswin_find_menu(
1094 vimmenu_T *pMenu,
1095 int id)
1096{
1097 vimmenu_T *pChildMenu;
1098
1099 while (pMenu)
1100 {
1101 if (pMenu->id == (UINT)id)
1102 break;
1103 if (pMenu->children != NULL)
1104 {
1105 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1106 if (pChildMenu)
1107 {
1108 pMenu = pChildMenu;
1109 break;
1110 }
1111 }
1112 pMenu = pMenu->next;
1113 }
1114 return pMenu;
1115}
1116
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001117 static void
1118_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001119 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001121 HWND hwndCtl UNUSED,
1122 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001123{
1124 vimmenu_T *pMenu;
1125
1126 pMenu = gui_mswin_find_menu(root_menu, id);
1127 if (pMenu)
1128 gui_menu_cb(pMenu);
1129}
1130#endif
1131
1132#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001133/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001134 * Handle a Find/Replace window message.
1135 */
1136 static void
1137_OnFindRepl(void)
1138{
1139 int flags = 0;
1140 int down;
1141
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001142 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001143 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001144 (void)SetFocus(s_hwnd);
1145
1146 if (s_findrep_struct.Flags & FR_FINDNEXT)
1147 {
1148 flags = FRD_FINDNEXT;
1149
Bram Moolenaar734a8672019-12-02 22:49:38 +01001150 // Give main window the focus back: this is so the cursor isn't
1151 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 (void)SetFocus(s_hwnd);
1153 }
1154 else if (s_findrep_struct.Flags & FR_REPLACE)
1155 {
1156 flags = FRD_REPLACE;
1157
Bram Moolenaar734a8672019-12-02 22:49:38 +01001158 // Give main window the focus back: this is so the cursor isn't
1159 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001160 (void)SetFocus(s_hwnd);
1161 }
1162 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1163 {
1164 flags = FRD_REPLACEALL;
1165 }
1166
1167 if (flags != 0)
1168 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001169 char_u *p, *q;
1170
Bram Moolenaar734a8672019-12-02 22:49:38 +01001171 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001172 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1173 flags |= FRD_WHOLE_WORD;
1174 if (s_findrep_struct.Flags & FR_MATCHCASE)
1175 flags |= FRD_MATCH_CASE;
1176 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001177 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1178 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1179 if (p != NULL && q != NULL)
1180 gui_do_findrepl(flags, p, q, down);
1181 vim_free(p);
1182 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001183 }
1184}
1185#endif
1186
1187 static void
1188HandleMouseHide(UINT uMsg, LPARAM lParam)
1189{
1190 static LPARAM last_lParam = 0L;
1191
Bram Moolenaar734a8672019-12-02 22:49:38 +01001192 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001193 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1194 {
1195 if (lParam == last_lParam)
1196 return;
1197 last_lParam = lParam;
1198 }
1199
Bram Moolenaar734a8672019-12-02 22:49:38 +01001200 // Handle specially, to centralise coding. We need to be sure we catch all
1201 // possible events which should cause us to restore the cursor (as it is a
1202 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001203 switch (uMsg)
1204 {
1205 case WM_KEYUP:
1206 case WM_CHAR:
1207 /*
1208 * blank out the pointer if necessary
1209 */
1210 if (p_mh)
1211 gui_mch_mousehide(TRUE);
1212 break;
1213
Bram Moolenaar734a8672019-12-02 22:49:38 +01001214 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001215 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001216 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001217 case WM_LBUTTONDOWN:
1218 case WM_LBUTTONUP:
1219 case WM_MBUTTONDOWN:
1220 case WM_MBUTTONUP:
1221 case WM_RBUTTONDOWN:
1222 case WM_RBUTTONUP:
1223 case WM_XBUTTONDOWN:
1224 case WM_XBUTTONUP:
1225 case WM_NCMOUSEMOVE:
1226 case WM_NCLBUTTONDOWN:
1227 case WM_NCLBUTTONUP:
1228 case WM_NCMBUTTONDOWN:
1229 case WM_NCMBUTTONUP:
1230 case WM_NCRBUTTONDOWN:
1231 case WM_NCRBUTTONUP:
1232 case WM_KILLFOCUS:
1233 /*
1234 * if the pointer is currently hidden, then we should show it.
1235 */
1236 gui_mch_mousehide(FALSE);
1237 break;
1238 }
1239}
1240
1241 static LRESULT CALLBACK
1242_TextAreaWndProc(
1243 HWND hwnd,
1244 UINT uMsg,
1245 WPARAM wParam,
1246 LPARAM lParam)
1247{
1248 /*
1249 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1250 hwnd, uMsg, wParam, lParam);
1251 */
1252
1253 HandleMouseHide(uMsg, lParam);
1254
1255 s_uMsg = uMsg;
1256 s_wParam = wParam;
1257 s_lParam = lParam;
1258
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001259#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001260 TrackUserActivity(uMsg);
1261#endif
1262
1263 switch (uMsg)
1264 {
1265 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1266 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1267 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1268 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1269 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1270 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1271 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1272 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1273 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1274 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1275 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1276 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1277 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1278 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001279 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001280
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001281#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1283 return TRUE;
1284#endif
1285 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001286 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001287 }
1288}
1289
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001290/*
1291 * Called when the foreground or background color has been changed.
1292 */
1293 void
1294gui_mch_new_colors(void)
1295{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001296 HBRUSH prevBrush;
1297
1298 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001299 prevBrush = (HBRUSH)SetClassLongPtr(
1300 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001301 InvalidateRect(s_hwnd, NULL, TRUE);
1302 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001303}
1304
1305/*
1306 * Set the colors to their default values.
1307 */
1308 void
1309gui_mch_def_colors(void)
1310{
1311 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1312 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1313 gui.def_norm_pixel = gui.norm_pixel;
1314 gui.def_back_pixel = gui.back_pixel;
1315}
1316
1317/*
1318 * Open the GUI window which was created by a call to gui_mch_init().
1319 */
1320 int
1321gui_mch_open(void)
1322{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001323 // Actually open the window, if not already visible
1324 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001325 if (!IsWindowVisible(s_hwnd))
1326 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1327
1328#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001329 // Init replace string here, so that we keep it when re-opening the
1330 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1332#endif
1333
1334 return OK;
1335}
1336
1337/*
1338 * Get the position of the top left corner of the window.
1339 */
1340 int
1341gui_mch_get_winpos(int *x, int *y)
1342{
1343 RECT rect;
1344
1345 GetWindowRect(s_hwnd, &rect);
1346 *x = rect.left;
1347 *y = rect.top;
1348 return OK;
1349}
1350
1351/*
1352 * Set the position of the top left corner of the window to the given
1353 * coordinates.
1354 */
1355 void
1356gui_mch_set_winpos(int x, int y)
1357{
1358 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1359 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1360}
1361 void
1362gui_mch_set_text_area_pos(int x, int y, int w, int h)
1363{
1364 static int oldx = 0;
1365 static int oldy = 0;
1366
1367 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1368
1369#ifdef FEAT_TOOLBAR
1370 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1371 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001372 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001373#endif
1374#if defined(FEAT_GUI_TABLINE)
1375 if (showing_tabline)
1376 {
1377 int top = 0;
1378 RECT rect;
1379
1380# ifdef FEAT_TOOLBAR
1381 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001382 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001383# endif
1384 GetClientRect(s_hwnd, &rect);
1385 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1386 }
1387#endif
1388
Bram Moolenaar734a8672019-12-02 22:49:38 +01001389 // When side scroll bar is unshown, the size of window will change.
1390 // then, the text area move left or right. thus client rect should be
1391 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001392 if (oldx != x || oldy != y)
1393 {
1394 InvalidateRect(s_hwnd, NULL, FALSE);
1395 oldx = x;
1396 oldy = y;
1397 }
1398}
1399
1400
1401/*
1402 * Scrollbar stuff:
1403 */
1404
1405 void
1406gui_mch_enable_scrollbar(
1407 scrollbar_T *sb,
1408 int flag)
1409{
1410 ShowScrollBar(sb->id, SB_CTL, flag);
1411
Bram Moolenaar734a8672019-12-02 22:49:38 +01001412 // TODO: When the window is maximized, the size of the window stays the
1413 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1414 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001415}
1416
1417 void
1418gui_mch_set_scrollbar_pos(
1419 scrollbar_T *sb,
1420 int x,
1421 int y,
1422 int w,
1423 int h)
1424{
1425 SetWindowPos(sb->id, NULL, x, y, w, h,
1426 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1427}
1428
Bram Moolenaar203ec772020-07-17 20:43:43 +02001429 int
1430gui_mch_get_scrollbar_xpadding(void)
1431{
1432 RECT rcTxt, rcWnd;
1433 int xpad;
1434
1435 GetWindowRect(s_textArea, &rcTxt);
1436 GetWindowRect(s_hwnd, &rcWnd);
1437 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001438 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1439 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001440 return (xpad < 0) ? 0 : xpad;
1441}
1442
1443 int
1444gui_mch_get_scrollbar_ypadding(void)
1445{
1446 RECT rcTxt, rcWnd;
1447 int ypad;
1448
1449 GetWindowRect(s_textArea, &rcTxt);
1450 GetWindowRect(s_hwnd, &rcWnd);
1451 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001452 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1453 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001454 return (ypad < 0) ? 0 : ypad;
1455}
1456
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001457 void
1458gui_mch_create_scrollbar(
1459 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001460 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001461{
1462 sb->id = CreateWindow(
1463 "SCROLLBAR", "Scrollbar",
1464 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001465 10, // Any value will do for now
1466 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001467 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001468 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001469}
1470
1471/*
1472 * Find the scrollbar with the given hwnd.
1473 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001474 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001475gui_mswin_find_scrollbar(HWND hwnd)
1476{
1477 win_T *wp;
1478
1479 if (gui.bottom_sbar.id == hwnd)
1480 return &gui.bottom_sbar;
1481 FOR_ALL_WINDOWS(wp)
1482 {
1483 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1484 return &wp->w_scrollbars[SBAR_LEFT];
1485 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1486 return &wp->w_scrollbars[SBAR_RIGHT];
1487 }
1488 return NULL;
1489}
1490
K.Takatac81e9bf2022-01-16 14:15:49 +00001491 static void
1492update_scrollbar_size(void)
1493{
1494 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1495 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1496}
1497
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001498/*
K.Takataabe628e2022-01-23 16:25:17 +00001499 * Get the average character size of a font.
1500 */
1501 static void
1502GetAverageFontSize(HDC hdc, SIZE *size)
1503{
1504 // GetTextMetrics() may not return the right value in tmAveCharWidth
1505 // for some fonts. Do our own average computation.
1506 GetTextExtentPoint(hdc,
1507 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1508 52, size);
1509 size->cx = (size->cx / 26 + 1) / 2;
1510}
1511
1512/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513 * Get the character size of a font.
1514 */
1515 static void
1516GetFontSize(GuiFont font)
1517{
1518 HWND hwnd = GetDesktopWindow();
1519 HDC hdc = GetWindowDC(hwnd);
1520 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001521 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001522 TEXTMETRIC tm;
1523
1524 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001525 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001526
K.Takataabe628e2022-01-23 16:25:17 +00001527 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001528 gui.char_height = tm.tmHeight + p_linespace;
1529
1530 SelectFont(hdc, hfntOld);
1531
1532 ReleaseDC(hwnd, hdc);
1533}
1534
1535/*
1536 * Adjust gui.char_height (after 'linespace' was changed).
1537 */
1538 int
1539gui_mch_adjust_charheight(void)
1540{
1541 GetFontSize(gui.norm_font);
1542 return OK;
1543}
1544
1545 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001546get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001547{
1548 HFONT font = NULL;
1549
Bram Moolenaar734a8672019-12-02 22:49:38 +01001550 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001551 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001552
1553 if (font == NULL)
1554 return NOFONT;
1555
1556 return (GuiFont)font;
1557}
1558
1559 static int
1560pixels_to_points(int pixels, int vertical)
1561{
1562 int points;
1563 HWND hwnd;
1564 HDC hdc;
1565
1566 hwnd = GetDesktopWindow();
1567 hdc = GetWindowDC(hwnd);
1568
1569 points = MulDiv(pixels, 72,
1570 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1571
1572 ReleaseDC(hwnd, hdc);
1573
1574 return points;
1575}
1576
1577 GuiFont
1578gui_mch_get_font(
1579 char_u *name,
1580 int giveErrorIfMissing)
1581{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001582 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001583 GuiFont font = NOFONT;
1584
1585 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001586 {
1587 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001588 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001589 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001591 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592 return font;
1593}
1594
1595#if defined(FEAT_EVAL) || defined(PROTO)
1596/*
1597 * Return the name of font "font" in allocated memory.
1598 * Don't know how to get the actual name, thus use the provided name.
1599 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001600 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001601gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001602{
1603 if (name == NULL)
1604 return NULL;
1605 return vim_strsave(name);
1606}
1607#endif
1608
1609 void
1610gui_mch_free_font(GuiFont font)
1611{
1612 if (font)
1613 DeleteObject((HFONT)font);
1614}
1615
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001616/*
1617 * Return the Pixel value (color) for the given color name.
1618 * Return INVALCOLOR for error.
1619 */
1620 guicolor_T
1621gui_mch_get_color(char_u *name)
1622{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001623 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001624
1625 typedef struct SysColorTable
1626 {
1627 char *name;
1628 int color;
1629 } SysColorTable;
1630
1631 static SysColorTable sys_table[] =
1632 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001633 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1634 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001635#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1637#endif
1638 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1639 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1640 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1641 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1642 {"SYS_DESKTOP", COLOR_DESKTOP},
1643 {"SYS_INFOBK", COLOR_INFOBK},
1644 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1645 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646 {"SYS_BTNFACE", COLOR_BTNFACE},
1647 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1648 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1649 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1650 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1651 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1652 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1653 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1654 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1655 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1656 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1657 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1658 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1659 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1660 {"SYS_MENU", COLOR_MENU},
1661 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1662 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1663 {"SYS_WINDOW", COLOR_WINDOW},
1664 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1665 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1666 };
1667
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001668 /*
1669 * Try to look up a system colour.
1670 */
K.Takataeeec2542021-06-02 13:28:16 +02001671 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001672 if (STRICMP(name, sys_table[i].name) == 0)
1673 return GetSysColor(sys_table[i].color);
1674
Bram Moolenaarab302212016-04-26 20:59:29 +02001675 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001676}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001677
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001678 guicolor_T
1679gui_mch_get_rgb_color(int r, int g, int b)
1680{
1681 return gui_get_rgb_color_cmn(r, g, b);
1682}
1683
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001684/*
1685 * Return OK if the key with the termcap name "name" is supported.
1686 */
1687 int
1688gui_mch_haskey(char_u *name)
1689{
1690 int i;
1691
1692 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1693 if (name[0] == special_keys[i].vim_code0 &&
1694 name[1] == special_keys[i].vim_code1)
1695 return OK;
1696 return FAIL;
1697}
1698
1699 void
1700gui_mch_beep(void)
1701{
1702 MessageBeep(MB_OK);
1703}
1704/*
1705 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1706 */
1707 void
1708gui_mch_invert_rectangle(
1709 int r,
1710 int c,
1711 int nr,
1712 int nc)
1713{
1714 RECT rc;
1715
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001716#if defined(FEAT_DIRECTX)
1717 if (IS_ENABLE_DIRECTX())
1718 DWriteContext_Flush(s_dwc);
1719#endif
1720
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001721 /*
1722 * Note: InvertRect() excludes right and bottom of rectangle.
1723 */
1724 rc.left = FILL_X(c);
1725 rc.top = FILL_Y(r);
1726 rc.right = rc.left + nc * gui.char_width;
1727 rc.bottom = rc.top + nr * gui.char_height;
1728 InvertRect(s_hdc, &rc);
1729}
1730
1731/*
1732 * Iconify the GUI window.
1733 */
1734 void
1735gui_mch_iconify(void)
1736{
1737 ShowWindow(s_hwnd, SW_MINIMIZE);
1738}
1739
1740/*
1741 * Draw a cursor without focus.
1742 */
1743 void
1744gui_mch_draw_hollow_cursor(guicolor_T color)
1745{
1746 HBRUSH hbr;
1747 RECT rc;
1748
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001749#if defined(FEAT_DIRECTX)
1750 if (IS_ENABLE_DIRECTX())
1751 DWriteContext_Flush(s_dwc);
1752#endif
1753
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001754 /*
1755 * Note: FrameRect() excludes right and bottom of rectangle.
1756 */
1757 rc.left = FILL_X(gui.col);
1758 rc.top = FILL_Y(gui.row);
1759 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001760 if (mb_lefthalve(gui.row, gui.col))
1761 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001762 rc.bottom = rc.top + gui.char_height;
1763 hbr = CreateSolidBrush(color);
1764 FrameRect(s_hdc, &rc, hbr);
1765 DeleteBrush(hbr);
1766}
1767/*
1768 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1769 * color "color".
1770 */
1771 void
1772gui_mch_draw_part_cursor(
1773 int w,
1774 int h,
1775 guicolor_T color)
1776{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 RECT rc;
1778
1779 /*
1780 * Note: FillRect() excludes right and bottom of rectangle.
1781 */
1782 rc.left =
1783#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001784 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001785 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1786#endif
1787 FILL_X(gui.col);
1788 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1789 rc.right = rc.left + w;
1790 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001791
Bram Moolenaar92467d32017-12-05 13:22:16 +01001792 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001793}
1794
1795
1796/*
1797 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1798 * dead key's nominal character and re-post the original message.
1799 */
1800 static void
1801outputDeadKey_rePost(MSG originalMsg)
1802{
1803 static MSG deadCharExpel;
1804
1805 if (!dead_key)
1806 return;
1807
1808 dead_key = 0;
1809
Bram Moolenaar734a8672019-12-02 22:49:38 +01001810 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001811 deadCharExpel.message = originalMsg.message;
1812 deadCharExpel.hwnd = originalMsg.hwnd;
1813 deadCharExpel.wParam = VK_SPACE;
1814
K.Takata4ac893f2022-01-20 12:44:28 +00001815 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001816
Bram Moolenaar734a8672019-12-02 22:49:38 +01001817 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1819 originalMsg.lParam);
1820}
1821
1822
1823/*
1824 * Process a single Windows message.
1825 * If one is not available we hang until one is.
1826 */
1827 static void
1828process_message(void)
1829{
1830 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001831 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001832 char_u string[40];
1833 int i;
1834 int modifiers = 0;
1835 int key;
1836#ifdef FEAT_MENU
1837 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1838#endif
1839
K.Takatab7057bd2022-01-21 11:37:07 +00001840 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001841
1842#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001843 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001844 if (msg.message == WM_OLE)
1845 {
1846 char_u *str = (char_u *)msg.lParam;
1847 if (str == NULL || *str == NUL)
1848 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001849 // Message can't be ours, forward it. Fixes problem with Ultramon
1850 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001851 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001852 }
1853 else
1854 {
1855 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001856 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001857 }
1858 return;
1859 }
1860#endif
1861
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001864 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001865 {
1866 HandleMouseHide(msg.message, msg.lParam);
1867 return;
1868 }
1869#endif
1870
1871 /*
1872 * Check if it's a special key that we recognise. If not, call
1873 * TranslateMessage().
1874 */
1875 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1876 {
1877 vk = (int) msg.wParam;
1878
1879 /*
1880 * Handle dead keys in special conditions in other cases we let Windows
1881 * handle them and do not interfere.
1882 *
1883 * The dead_key flag must be reset on several occasions:
1884 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1885 * consumed at that point (This is when we let Windows combine the
1886 * dead character on its own)
1887 *
1888 * - Before doing something special such as regenerating keypresses to
1889 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001890 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001891 * immediately to _OnChar() (or _OnSysChar()).
1892 */
1893 if (dead_key)
1894 {
1895 /*
1896 * If a dead key was pressed and the user presses VK_SPACE,
1897 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1898 * with the dead char now, so do nothing special and let Windows
1899 * handle it.
1900 *
1901 * Note that VK_SPACE combines with the dead_key's character and
1902 * only one WM_CHAR will be generated by TranslateMessage(), in
1903 * the two other cases two WM_CHAR will be generated: the dead
1904 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1905 * user expects.
1906 */
1907 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1908 {
1909 dead_key = 0;
K.Takata4ac893f2022-01-20 12:44:28 +00001910 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001911 return;
1912 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001913 // In modes where we are not typing, dead keys should behave
1914 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001915 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1916 {
1917 outputDeadKey_rePost(msg);
1918 return;
1919 }
1920 }
1921
Bram Moolenaar734a8672019-12-02 22:49:38 +01001922 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001923 if (vk == VK_CANCEL)
1924 {
1925 trash_input_buf();
1926 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001927 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001928 string[0] = Ctrl_C;
1929 add_to_input_buf(string, 1);
1930 }
1931
1932 for (i = 0; special_keys[i].key_sym != 0; i++)
1933 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001934 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 if (special_keys[i].key_sym == vk
1936 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1937 {
1938 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001939 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001940 * is a key that would normally trigger the dead key nominal
1941 * character output (such as a NUMPAD printable character or
1942 * the TAB key, etc...).
1943 */
1944 if (dead_key && (special_keys[i].vim_code0 == 'K'
1945 || vk == VK_TAB || vk == CAR))
1946 {
1947 outputDeadKey_rePost(msg);
1948 return;
1949 }
1950
1951#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001952 // Check for <F10>: Windows selects the menu. When <F10> is
1953 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001954 if (vk == VK_F10
1955 && gui.menu_is_active
1956 && check_map(k10, State, FALSE, TRUE, FALSE,
1957 NULL, NULL) == NULL)
1958 break;
1959#endif
1960 if (GetKeyState(VK_SHIFT) & 0x8000)
1961 modifiers |= MOD_MASK_SHIFT;
1962 /*
1963 * Don't use caps-lock as shift, because these are special keys
1964 * being considered here, and we only want letters to get
1965 * shifted -- webb
1966 */
1967 /*
1968 if (GetKeyState(VK_CAPITAL) & 0x0001)
1969 modifiers ^= MOD_MASK_SHIFT;
1970 */
1971 if (GetKeyState(VK_CONTROL) & 0x8000)
1972 modifiers |= MOD_MASK_CTRL;
1973 if (GetKeyState(VK_MENU) & 0x8000)
1974 modifiers |= MOD_MASK_ALT;
1975
1976 if (special_keys[i].vim_code1 == NUL)
1977 key = special_keys[i].vim_code0;
1978 else
1979 key = TO_SPECIAL(special_keys[i].vim_code0,
1980 special_keys[i].vim_code1);
1981 key = simplify_key(key, &modifiers);
1982 if (key == CSI)
1983 key = K_CSI;
1984
1985 if (modifiers)
1986 {
1987 string[0] = CSI;
1988 string[1] = KS_MODIFIER;
1989 string[2] = modifiers;
1990 add_to_input_buf(string, 3);
1991 }
1992
1993 if (IS_SPECIAL(key))
1994 {
1995 string[0] = CSI;
1996 string[1] = K_SECOND(key);
1997 string[2] = K_THIRD(key);
1998 add_to_input_buf(string, 3);
1999 }
2000 else
2001 {
2002 int len;
2003
Bram Moolenaar734a8672019-12-02 22:49:38 +01002004 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002005 len = char_to_string(key, string, 40, FALSE);
2006 add_to_input_buf(string, len);
2007 }
2008 break;
2009 }
2010 }
2011 if (special_keys[i].key_sym == 0)
2012 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002013 // Some keys need C-S- where they should only need C-.
2014 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2015 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002016 if (vk != 0xff
2017 && (GetKeyState(VK_CONTROL) & 0x8000)
2018 && !(GetKeyState(VK_SHIFT) & 0x8000)
2019 && !(GetKeyState(VK_MENU) & 0x8000))
2020 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002021 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002022 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2023 {
2024 string[0] = Ctrl_HAT;
2025 add_to_input_buf(string, 1);
2026 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002027 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2028 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002029 {
2030 string[0] = Ctrl__;
2031 add_to_input_buf(string, 1);
2032 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002033 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002034 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2035 {
2036 string[0] = Ctrl_AT;
2037 add_to_input_buf(string, 1);
2038 }
2039 else
K.Takata4ac893f2022-01-20 12:44:28 +00002040 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002041 }
2042 else
K.Takata4ac893f2022-01-20 12:44:28 +00002043 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002044 }
2045 }
2046#ifdef FEAT_MBYTE_IME
2047 else if (msg.message == WM_IME_NOTIFY)
2048 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2049 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002050 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002051 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002052#endif
2053
2054#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002055 // Check for <F10>: Default effect is to select the menu. When <F10> is
2056 // mapped we need to stop it here to avoid strange effects (e.g., for the
2057 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002058 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2059 NULL, NULL) == NULL)
2060#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002061 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002062}
2063
2064/*
2065 * Catch up with any queued events. This may put keyboard input into the
2066 * input buffer, call resize call-backs, trigger timers etc. If there is
2067 * nothing in the event queue (& no timers pending), then we return
2068 * immediately.
2069 */
2070 void
2071gui_mch_update(void)
2072{
2073 MSG msg;
2074
2075 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002076 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002077 && !vim_is_input_buf_full())
2078 process_message();
2079}
2080
Bram Moolenaar4231da42016-06-02 14:30:04 +02002081 static void
2082remove_any_timer(void)
2083{
2084 MSG msg;
2085
2086 if (s_wait_timer != 0 && !s_timed_out)
2087 {
2088 KillTimer(NULL, s_wait_timer);
2089
Bram Moolenaar734a8672019-12-02 22:49:38 +01002090 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002091 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002092 ;
2093 s_wait_timer = 0;
2094 }
2095}
2096
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002097/*
2098 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2099 * from the keyboard.
2100 * wtime == -1 Wait forever.
2101 * wtime == 0 This should never happen.
2102 * wtime > 0 Wait wtime milliseconds for a character.
2103 * Returns OK if a character was found to be available within the given time,
2104 * or FAIL otherwise.
2105 */
2106 int
2107gui_mch_wait_for_chars(int wtime)
2108{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002109 int focus;
2110
2111 s_timed_out = FALSE;
2112
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002113 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002115 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002116 if (s_busy_processing)
2117 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002118
2119 // When called with "wtime" zero, just want one msec.
2120 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 (TIMERPROC)_OnTimer);
2122 }
2123
2124 allow_scrollbar = TRUE;
2125
2126 focus = gui.in_focus;
2127 while (!s_timed_out)
2128 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002129 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002130 if (gui.in_focus != focus)
2131 {
2132 if (gui.in_focus)
2133 gui_mch_start_blink();
2134 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002135 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002136 focus = gui.in_focus;
2137 }
2138
2139 if (s_need_activate)
2140 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002141 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002142 s_need_activate = FALSE;
2143 }
2144
Bram Moolenaar4231da42016-06-02 14:30:04 +02002145#ifdef FEAT_TIMERS
2146 did_add_timer = FALSE;
2147#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002149 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002150 for (;;)
2151 {
2152 MSG msg;
2153
2154 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002155# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002156 if (did_add_timer)
2157 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002158# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002159 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002160 {
2161 process_message();
2162 break;
2163 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002164 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002165 // TODO: The 10 msec is a compromise between laggy response
2166 // and consuming more CPU time. Better would be to handle
2167 // channel messages when they arrive.
2168 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002169 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002170 break;
2171 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002172#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002173 // Don't use gui_mch_update() because then we will spin-lock until a
2174 // char arrives, instead we use GetMessage() to hang until an
2175 // event arrives. No need to check for input_buf_full because we are
2176 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002177 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002178#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002179
2180 if (input_available())
2181 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002182 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002183 allow_scrollbar = FALSE;
2184
Bram Moolenaar89c00032019-09-04 13:53:21 +02002185 // Clear pending mouse button, the release event may have been
2186 // taken by the dialog window. But don't do this when getting
2187 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002188 if (!s_getting_focus)
2189 s_button_pending = -1;
2190
2191 return OK;
2192 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002193
2194#ifdef FEAT_TIMERS
2195 if (did_add_timer)
2196 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002197 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002198 remove_any_timer();
2199 break;
2200 }
2201#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002202 }
2203 allow_scrollbar = FALSE;
2204 return FAIL;
2205}
2206
2207/*
2208 * Clear a rectangular region of the screen from text pos (row1, col1) to
2209 * (row2, col2) inclusive.
2210 */
2211 void
2212gui_mch_clear_block(
2213 int row1,
2214 int col1,
2215 int row2,
2216 int col2)
2217{
2218 RECT rc;
2219
2220 /*
2221 * Clear one extra pixel at the far right, for when bold characters have
2222 * spilled over to the window border.
2223 * Note: FillRect() excludes right and bottom of rectangle.
2224 */
2225 rc.left = FILL_X(col1);
2226 rc.top = FILL_Y(row1);
2227 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2228 rc.bottom = FILL_Y(row2 + 1);
2229 clear_rect(&rc);
2230}
2231
2232/*
2233 * Clear the whole text window.
2234 */
2235 void
2236gui_mch_clear_all(void)
2237{
2238 RECT rc;
2239
2240 rc.left = 0;
2241 rc.top = 0;
2242 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2243 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2244 clear_rect(&rc);
2245}
2246/*
2247 * Menu stuff.
2248 */
2249
2250 void
2251gui_mch_enable_menu(int flag)
2252{
2253#ifdef FEAT_MENU
2254 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2255#endif
2256}
2257
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002258 void
2259gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002260 int x UNUSED,
2261 int y UNUSED,
2262 int w UNUSED,
2263 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002264{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002265 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002266}
2267
2268#if defined(FEAT_MENU) || defined(PROTO)
2269/*
2270 * Make menu item hidden or not hidden
2271 */
2272 void
2273gui_mch_menu_hidden(
2274 vimmenu_T *menu,
2275 int hidden)
2276{
2277 /*
2278 * This doesn't do what we want. Hmm, just grey the menu items for now.
2279 */
2280 /*
2281 if (hidden)
2282 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2283 else
2284 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2285 */
2286 gui_mch_menu_grey(menu, hidden);
2287}
2288
2289/*
2290 * This is called after setting all the menus to grey/hidden or not.
2291 */
2292 void
2293gui_mch_draw_menubar(void)
2294{
2295 DrawMenuBar(s_hwnd);
2296}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002297#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002298
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002299/*
2300 * Return the RGB value of a pixel as a long.
2301 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002302 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303gui_mch_get_rgb(guicolor_T pixel)
2304{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002305 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2306 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307}
2308
2309#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002310/*
2311 * Convert pixels in X to dialog units
2312 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002313 static WORD
2314PixelToDialogX(int numPixels)
2315{
2316 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2317}
2318
Bram Moolenaar734a8672019-12-02 22:49:38 +01002319/*
2320 * Convert pixels in Y to dialog units
2321 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002322 static WORD
2323PixelToDialogY(int numPixels)
2324{
2325 return (WORD)((numPixels * 8) / s_dlgfntheight);
2326}
2327
Bram Moolenaar734a8672019-12-02 22:49:38 +01002328/*
2329 * Return the width in pixels of the given text in the given DC.
2330 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331 static int
2332GetTextWidth(HDC hdc, char_u *str, int len)
2333{
2334 SIZE size;
2335
2336 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2337 return size.cx;
2338}
2339
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002340/*
2341 * Return the width in pixels of the given text in the given DC, taking care
2342 * of 'encoding' to active codepage conversion.
2343 */
2344 static int
2345GetTextWidthEnc(HDC hdc, char_u *str, int len)
2346{
2347 SIZE size;
2348 WCHAR *wstr;
2349 int n;
2350 int wlen = len;
2351
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002352 wstr = enc_to_utf16(str, &wlen);
2353 if (wstr == NULL)
2354 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002356 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2357 vim_free(wstr);
2358 if (n)
2359 return size.cx;
2360 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002362
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002363static void get_work_area(RECT *spi_rect);
2364
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002365/*
2366 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002367 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2368 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 */
2370 static BOOL
2371CenterWindow(
2372 HWND hwndChild,
2373 HWND hwndParent)
2374{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002375 HMONITOR mon;
2376 MONITORINFO moninfo;
2377 RECT rChild, rParent, rScreen;
2378 int wChild, hChild, wParent, hParent;
2379 int xNew, yNew;
2380 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002381
2382 GetWindowRect(hwndChild, &rChild);
2383 wChild = rChild.right - rChild.left;
2384 hChild = rChild.bottom - rChild.top;
2385
Bram Moolenaar734a8672019-12-02 22:49:38 +01002386 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002388 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002389 else
2390 GetWindowRect(hwndParent, &rParent);
2391 wParent = rParent.right - rParent.left;
2392 hParent = rParent.bottom - rParent.top;
2393
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002394 moninfo.cbSize = sizeof(MONITORINFO);
2395 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2396 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002398 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002400 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002401 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002402 hdc = GetDC(hwndChild);
2403 rScreen.left = 0;
2404 rScreen.top = 0;
2405 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2406 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2407 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408 }
2409
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002410 xNew = rParent.left + ((wParent - wChild) / 2);
2411 if (xNew < rScreen.left)
2412 xNew = rScreen.left;
2413 else if ((xNew + wChild) > rScreen.right)
2414 xNew = rScreen.right - wChild;
2415
2416 yNew = rParent.top + ((hParent - hChild) / 2);
2417 if (yNew < rScreen.top)
2418 yNew = rScreen.top;
2419 else if ((yNew + hChild) > rScreen.bottom)
2420 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002421
2422 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2423 SWP_NOSIZE | SWP_NOZORDER);
2424}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002425#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002427#if defined(FEAT_TOOLBAR) || defined(PROTO)
2428 void
2429gui_mch_show_toolbar(int showit)
2430{
2431 if (s_toolbarhwnd == NULL)
2432 return;
2433
2434 if (showit)
2435 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002436 // Enable unicode support
2437 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2438 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002439 ShowWindow(s_toolbarhwnd, SW_SHOW);
2440 }
2441 else
2442 ShowWindow(s_toolbarhwnd, SW_HIDE);
2443}
2444
Bram Moolenaar734a8672019-12-02 22:49:38 +01002445// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002446# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002447
2448#endif
2449
2450#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2451 static void
2452add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2453{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002454 WCHAR *wn;
2455 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002456
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002457 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002459 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002460
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002461 infow.cbSize = sizeof(infow);
2462 infow.fMask = MIIM_TYPE | MIIM_ID;
2463 infow.wID = item_id;
2464 infow.fType = MFT_STRING;
2465 infow.dwTypeData = wn;
2466 infow.cch = (UINT)wcslen(wn);
2467 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2468 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469}
2470
2471 static void
2472show_tabline_popup_menu(void)
2473{
2474 HMENU tab_pmenu;
2475 long rval;
2476 POINT pt;
2477
Bram Moolenaar734a8672019-12-02 22:49:38 +01002478 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479 if (hold_gui_events
2480# ifdef FEAT_CMDWIN
2481 || cmdwin_type != 0
2482# endif
2483 )
2484 return;
2485
2486 tab_pmenu = CreatePopupMenu();
2487 if (tab_pmenu == NULL)
2488 return;
2489
2490 if (first_tabpage->tp_next != NULL)
2491 add_tabline_popup_menu_entry(tab_pmenu,
2492 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2493 add_tabline_popup_menu_entry(tab_pmenu,
2494 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2495 add_tabline_popup_menu_entry(tab_pmenu,
2496 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2497
2498 GetCursorPos(&pt);
2499 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2500 NULL);
2501
2502 DestroyMenu(tab_pmenu);
2503
Bram Moolenaar734a8672019-12-02 22:49:38 +01002504 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505 if (rval > 0)
2506 {
2507 TCHITTESTINFO htinfo;
2508 int idx;
2509
2510 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2511 return;
2512
2513 htinfo.pt.x = pt.x;
2514 htinfo.pt.y = pt.y;
2515 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2516 if (idx == -1)
2517 idx = 0;
2518 else
2519 idx += 1;
2520
2521 send_tabline_menu_event(idx, (int)rval);
2522 }
2523}
2524
2525/*
2526 * Show or hide the tabline.
2527 */
2528 void
2529gui_mch_show_tabline(int showit)
2530{
2531 if (s_tabhwnd == NULL)
2532 return;
2533
2534 if (!showit != !showing_tabline)
2535 {
2536 if (showit)
2537 ShowWindow(s_tabhwnd, SW_SHOW);
2538 else
2539 ShowWindow(s_tabhwnd, SW_HIDE);
2540 showing_tabline = showit;
2541 }
2542}
2543
2544/*
2545 * Return TRUE when tabline is displayed.
2546 */
2547 int
2548gui_mch_showing_tabline(void)
2549{
2550 return s_tabhwnd != NULL && showing_tabline;
2551}
2552
2553/*
2554 * Update the labels of the tabline.
2555 */
2556 void
2557gui_mch_update_tabline(void)
2558{
2559 tabpage_T *tp;
2560 TCITEM tie;
2561 int nr = 0;
2562 int curtabidx = 0;
2563 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002564 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002565
2566 if (s_tabhwnd == NULL)
2567 return;
2568
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002569 // Enable unicode support
2570 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002571
2572 tie.mask = TCIF_TEXT;
2573 tie.iImage = -1;
2574
Bram Moolenaar734a8672019-12-02 22:49:38 +01002575 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2577
Bram Moolenaar734a8672019-12-02 22:49:38 +01002578 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002579 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2580 {
2581 if (tp == curtab)
2582 curtabidx = nr;
2583
2584 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2585 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002586 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587 tie.pszText = "-Empty-";
2588 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2589 tabadded = 1;
2590 }
2591
2592 get_tabline_label(tp, FALSE);
2593 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002595 wstr = enc_to_utf16(NameBuff, NULL);
2596 if (wstr != NULL)
2597 {
2598 TCITEMW tiw;
2599
2600 tiw.mask = TCIF_TEXT;
2601 tiw.iImage = -1;
2602 tiw.pszText = wstr;
2603 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2604 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002605 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002606 }
2607
Bram Moolenaar734a8672019-12-02 22:49:38 +01002608 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2610 TabCtrl_DeleteItem(s_tabhwnd, nr);
2611
2612 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2613 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2614
Bram Moolenaar734a8672019-12-02 22:49:38 +01002615 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002616 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2617 RedrawWindow(s_tabhwnd, NULL, NULL,
2618 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2619
2620 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2621 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2622}
2623
2624/*
2625 * Set the current tab to "nr". First tab is 1.
2626 */
2627 void
2628gui_mch_set_curtab(int nr)
2629{
2630 if (s_tabhwnd == NULL)
2631 return;
2632
2633 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2634 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2635}
2636
2637#endif
2638
2639/*
2640 * ":simalt" command.
2641 */
2642 void
2643ex_simalt(exarg_T *eap)
2644{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002645 char_u *keys = eap->arg;
2646 int fill_typebuf = FALSE;
2647 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002648
2649 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2650 while (*keys)
2651 {
2652 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002653 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002654 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2655 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002656 fill_typebuf = TRUE;
2657 }
2658 if (fill_typebuf)
2659 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002660 // Put a NOP in the typeahead buffer so that the message will get
2661 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002662 key_name[0] = K_SPECIAL;
2663 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002664 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002665 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002666#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002667 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002668#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002669 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670 }
2671}
2672
2673/*
2674 * Create the find & replace dialogs.
2675 * You can't have both at once: ":find" when replace is showing, destroys
2676 * the replace dialog first, and the other way around.
2677 */
2678#ifdef MSWIN_FIND_REPLACE
2679 static void
2680initialise_findrep(char_u *initial_string)
2681{
2682 int wword = FALSE;
2683 int mcase = !p_ic;
2684 char_u *entry_text;
2685
Bram Moolenaar734a8672019-12-02 22:49:38 +01002686 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002687 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2688
2689 s_findrep_struct.hwndOwner = s_hwnd;
2690 s_findrep_struct.Flags = FR_DOWN;
2691 if (mcase)
2692 s_findrep_struct.Flags |= FR_MATCHCASE;
2693 if (wword)
2694 s_findrep_struct.Flags |= FR_WHOLEWORD;
2695 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002696 {
2697 WCHAR *p = enc_to_utf16(entry_text, NULL);
2698 if (p != NULL)
2699 {
2700 int len = s_findrep_struct.wFindWhatLen - 1;
2701
2702 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2703 s_findrep_struct.lpstrFindWhat[len] = NUL;
2704 vim_free(p);
2705 }
2706 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707 vim_free(entry_text);
2708}
2709#endif
2710
2711 static void
2712set_window_title(HWND hwnd, char *title)
2713{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002714 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002715 {
2716 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002717
Bram Moolenaar734a8672019-12-02 22:49:38 +01002718 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002719 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2720 if (wbuf != NULL)
2721 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002722 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724 }
2725 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002726 else
2727 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002728}
2729
2730 void
2731gui_mch_find_dialog(exarg_T *eap)
2732{
2733#ifdef MSWIN_FIND_REPLACE
2734 if (s_findrep_msg != 0)
2735 {
2736 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2737 DestroyWindow(s_findrep_hwnd);
2738
2739 if (!IsWindow(s_findrep_hwnd))
2740 {
2741 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002742 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002743 }
2744
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002745 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002746 (void)SetFocus(s_findrep_hwnd);
2747
2748 s_findrep_is_find = TRUE;
2749 }
2750#endif
2751}
2752
2753
2754 void
2755gui_mch_replace_dialog(exarg_T *eap)
2756{
2757#ifdef MSWIN_FIND_REPLACE
2758 if (s_findrep_msg != 0)
2759 {
2760 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2761 DestroyWindow(s_findrep_hwnd);
2762
2763 if (!IsWindow(s_findrep_hwnd))
2764 {
2765 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002766 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767 }
2768
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002769 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002770 (void)SetFocus(s_findrep_hwnd);
2771
2772 s_findrep_is_find = FALSE;
2773 }
2774#endif
2775}
2776
2777
2778/*
2779 * Set visibility of the pointer.
2780 */
2781 void
2782gui_mch_mousehide(int hide)
2783{
2784 if (hide != gui.pointer_hidden)
2785 {
2786 ShowCursor(!hide);
2787 gui.pointer_hidden = hide;
2788 }
2789}
2790
2791#ifdef FEAT_MENU
2792 static void
2793gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2794{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002795 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002796 gui_mch_mousehide(FALSE);
2797
2798 (void)TrackPopupMenu(
2799 (HMENU)menu->submenu_id,
2800 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2801 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002802 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002803 s_hwnd,
2804 NULL);
2805 /*
2806 * NOTE: The pop-up menu can eat the mouse up event.
2807 * We deal with this in normal.c.
2808 */
2809}
2810#endif
2811
2812/*
2813 * Got a message when the system will go down.
2814 */
2815 static void
2816_OnEndSession(void)
2817{
2818 getout_preserve_modified(1);
2819}
2820
2821/*
2822 * Get this message when the user clicks on the cross in the top right corner
2823 * of a Windows95 window.
2824 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002825 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002826_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002827{
2828 gui_shell_closed();
2829}
2830
2831/*
2832 * Get a message when the window is being destroyed.
2833 */
2834 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002835_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002836{
2837 if (!destroying)
2838 _OnClose(hwnd);
2839}
2840
2841 static void
2842_OnPaint(
2843 HWND hwnd)
2844{
2845 if (!IsMinimized(hwnd))
2846 {
2847 PAINTSTRUCT ps;
2848
Bram Moolenaar734a8672019-12-02 22:49:38 +01002849 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851
Bram Moolenaar734a8672019-12-02 22:49:38 +01002852 // prevent multi-byte characters from misprinting on an invalid
2853 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002854 if (has_mbyte)
2855 {
2856 RECT rect;
2857
2858 GetClientRect(hwnd, &rect);
2859 ps.rcPaint.left = rect.left;
2860 ps.rcPaint.right = rect.right;
2861 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862
2863 if (!IsRectEmpty(&ps.rcPaint))
2864 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2866 ps.rcPaint.right - ps.rcPaint.left + 1,
2867 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2868 }
2869
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002870 EndPaint(hwnd, &ps);
2871 }
2872}
2873
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002874 static void
2875_OnSize(
2876 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002877 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002878 int cx,
2879 int cy)
2880{
K.Takatac81e9bf2022-01-16 14:15:49 +00002881 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002882 {
2883 gui_resize_shell(cx, cy);
2884
Bram Moolenaar734a8672019-12-02 22:49:38 +01002885 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002886 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002887 }
2888}
2889
2890 static void
2891_OnSetFocus(
2892 HWND hwnd,
2893 HWND hwndOldFocus)
2894{
2895 gui_focus_change(TRUE);
2896 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002897 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002898}
2899
2900 static void
2901_OnKillFocus(
2902 HWND hwnd,
2903 HWND hwndNewFocus)
2904{
2905 gui_focus_change(FALSE);
2906 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002907 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002908}
2909
2910/*
2911 * Get a message when the user switches back to vim
2912 */
2913 static LRESULT
2914_OnActivateApp(
2915 HWND hwnd,
2916 BOOL fActivate,
2917 DWORD dwThreadId)
2918{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002919 // we call gui_focus_change() in _OnSetFocus()
2920 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002921 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002922}
2923
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924 void
2925gui_mch_destroy_scrollbar(scrollbar_T *sb)
2926{
2927 DestroyWindow(sb->id);
2928}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002929
2930/*
2931 * Get current mouse coordinates in text window.
2932 */
2933 void
2934gui_mch_getmouse(int *x, int *y)
2935{
2936 RECT rct;
2937 POINT mp;
2938
2939 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002940 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941 *x = (int)(mp.x - rct.left);
2942 *y = (int)(mp.y - rct.top);
2943}
2944
2945/*
2946 * Move mouse pointer to character at (x, y).
2947 */
2948 void
2949gui_mch_setmouse(int x, int y)
2950{
2951 RECT rct;
2952
2953 (void)GetWindowRect(s_textArea, &rct);
2954 (void)SetCursorPos(x + gui.border_offset + rct.left,
2955 y + gui.border_offset + rct.top);
2956}
2957
2958 static void
2959gui_mswin_get_valid_dimensions(
2960 int w,
2961 int h,
2962 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002963 int *valid_h,
2964 int *cols,
2965 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002966{
2967 int base_width, base_height;
2968
2969 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00002970 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
2971 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002972 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00002973 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
2974 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
2975 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
2976 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02002977 *cols = (w - base_width) / gui.char_width;
2978 *rows = (h - base_height) / gui.char_height;
2979 *valid_w = base_width + *cols * gui.char_width;
2980 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002981}
2982
2983 void
2984gui_mch_flash(int msec)
2985{
2986 RECT rc;
2987
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002988#if defined(FEAT_DIRECTX)
2989 if (IS_ENABLE_DIRECTX())
2990 DWriteContext_Flush(s_dwc);
2991#endif
2992
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002993 /*
2994 * Note: InvertRect() excludes right and bottom of rectangle.
2995 */
2996 rc.left = 0;
2997 rc.top = 0;
2998 rc.right = gui.num_cols * gui.char_width;
2999 rc.bottom = gui.num_rows * gui.char_height;
3000 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003001 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003002
Bram Moolenaar734a8672019-12-02 22:49:38 +01003003 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003004
3005 InvertRect(s_hdc, &rc);
3006}
3007
3008/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003009 * Check if the specified point is on-screen. (multi-monitor aware)
3010 */
3011 static BOOL
3012is_point_onscreen(int x, int y)
3013{
3014 POINT pt = {x, y};
3015
3016 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3017}
3018
3019/*
3020 * Check if the whole area of the specified window is on-screen.
3021 *
3022 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3023 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3024 * only works when the whole window is on-screen.
3025 */
3026 static BOOL
3027is_window_onscreen(HWND hwnd)
3028{
3029 RECT rc;
3030
3031 GetWindowRect(hwnd, &rc);
3032
3033 if (!is_point_onscreen(rc.left, rc.top))
3034 return FALSE;
3035 if (!is_point_onscreen(rc.left, rc.bottom))
3036 return FALSE;
3037 if (!is_point_onscreen(rc.right, rc.top))
3038 return FALSE;
3039 if (!is_point_onscreen(rc.right, rc.bottom))
3040 return FALSE;
3041 return TRUE;
3042}
3043
3044/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003045 * Return flags used for scrolling.
3046 * The SW_INVALIDATE is required when part of the window is covered or
3047 * off-screen. Refer to MS KB Q75236.
3048 */
3049 static int
3050get_scroll_flags(void)
3051{
3052 HWND hwnd;
3053 RECT rcVim, rcOther, rcDest;
3054
Bram Moolenaar185577e2020-10-29 20:08:21 +01003055 // Check if the window is (partly) off-screen.
3056 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057 return SW_INVALIDATE;
3058
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003059 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003060 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003061 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3062 if (IsWindowVisible(hwnd))
3063 {
3064 GetWindowRect(hwnd, &rcOther);
3065 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3066 return SW_INVALIDATE;
3067 }
3068 return 0;
3069}
3070
3071/*
3072 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3073 * may not be scrolled out properly.
3074 * For gVim, when _OnScroll() is repeated, the character at the
3075 * previous cursor position may be left drawn after scroll.
3076 * The problem can be avoided by calling GetPixel() to get a pixel in
3077 * the region before ScrollWindowEx().
3078 */
3079 static void
3080intel_gpu_workaround(void)
3081{
3082 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3083}
3084
3085/*
3086 * Delete the given number of lines from the given row, scrolling up any
3087 * text further down within the scroll region.
3088 */
3089 void
3090gui_mch_delete_lines(
3091 int row,
3092 int num_lines)
3093{
3094 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003095
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003096 rc.left = FILL_X(gui.scroll_region_left);
3097 rc.right = FILL_X(gui.scroll_region_right + 1);
3098 rc.top = FILL_Y(row);
3099 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3100
Bram Moolenaar92467d32017-12-05 13:22:16 +01003101#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003102 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003103 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003104 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003105 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003106 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003107#endif
3108 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003109#if defined(FEAT_DIRECTX)
3110 if (IS_ENABLE_DIRECTX())
3111 DWriteContext_Flush(s_dwc);
3112#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003113 intel_gpu_workaround();
3114 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003115 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003116 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003117 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003118
Bram Moolenaar734a8672019-12-02 22:49:38 +01003119 // This seems to be required to avoid the cursor disappearing when
3120 // scrolling such that the cursor ends up in the top-left character on
3121 // the screen... But why? (Webb)
3122 // It's probably fixed by disabling drawing the cursor while scrolling.
3123 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124
3125 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3126 gui.scroll_region_left,
3127 gui.scroll_region_bot, gui.scroll_region_right);
3128}
3129
3130/*
3131 * Insert the given number of lines before the given row, scrolling down any
3132 * following text within the scroll region.
3133 */
3134 void
3135gui_mch_insert_lines(
3136 int row,
3137 int num_lines)
3138{
3139 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003140
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003141 rc.left = FILL_X(gui.scroll_region_left);
3142 rc.right = FILL_X(gui.scroll_region_right + 1);
3143 rc.top = FILL_Y(row);
3144 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003145
3146#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003147 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003148 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003149 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003150 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003151 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003152#endif
3153 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003154#if defined(FEAT_DIRECTX)
3155 if (IS_ENABLE_DIRECTX())
3156 DWriteContext_Flush(s_dwc);
3157#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003158 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003159 // The SW_INVALIDATE is required when part of the window is covered or
3160 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003161 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003162 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003163 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003164 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003165
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003166 gui_clear_block(row, gui.scroll_region_left,
3167 row + num_lines - 1, gui.scroll_region_right);
3168}
3169
3170
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003172gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003173{
3174#if defined(FEAT_DIRECTX)
3175 DWriteContext_Close(s_dwc);
3176 DWrite_Final();
3177 s_dwc = NULL;
3178#endif
3179
3180 ReleaseDC(s_textArea, s_hdc);
3181 DeleteObject(s_brush);
3182
3183#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003184 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003185 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3186#endif
3187
Bram Moolenaar734a8672019-12-02 22:49:38 +01003188 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189 if (s_hwnd != NULL)
3190 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003191 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003192 DestroyWindow(s_hwnd);
3193 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194}
3195
3196 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003197logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198{
3199 char *p;
3200 char *res;
3201 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003202 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003204 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003206 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3207 if (font_name == NULL)
3208 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003210 quality_name = quality_id2name((int)lf.lfQuality);
3211
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003212 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003213 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003214 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 if (res != NULL)
3216 {
3217 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003218 // make a normal font string out of the lf thing:
3219 points = pixels_to_points(
3220 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3221 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3222 sprintf((char *)p, "%s:h%d", font_name, points);
3223 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003224 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 while (*p)
3226 {
3227 if (*p == ' ')
3228 *p = '_';
3229 ++p;
3230 }
3231 if (lf.lfItalic)
3232 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003233 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234 STRCAT(p, ":b");
3235 if (lf.lfUnderline)
3236 STRCAT(p, ":u");
3237 if (lf.lfStrikeOut)
3238 STRCAT(p, ":s");
3239 if (charset_name != NULL)
3240 {
3241 STRCAT(p, ":c");
3242 STRCAT(p, charset_name);
3243 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003244 if (quality_name != NULL)
3245 {
3246 STRCAT(p, ":q");
3247 STRCAT(p, quality_name);
3248 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 }
3250
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003251 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252 return (char_u *)res;
3253}
3254
3255
3256#ifdef FEAT_MBYTE_IME
3257/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003258 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003259 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003260 */
3261 static void
3262update_im_font(void)
3263{
K.Takatac81e9bf2022-01-16 14:15:49 +00003264 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003265
3266 if (p_guifontwide != NULL && *p_guifontwide != NUL
3267 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003268 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003269 norm_logfont = lf_wide;
3270 else
3271 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003272
3273 lf = norm_logfont;
3274 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3275 // Work around when PerMonitorV2 is not enabled in the process level.
3276 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3277 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278}
3279#endif
3280
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003281/*
3282 * Handler of gui.wide_font (p_guifontwide) changed notification.
3283 */
3284 void
3285gui_mch_wide_font_changed(void)
3286{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003287 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003288
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003289#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003290 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003291#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003292
3293 gui_mch_free_font(gui.wide_ital_font);
3294 gui.wide_ital_font = NOFONT;
3295 gui_mch_free_font(gui.wide_bold_font);
3296 gui.wide_bold_font = NOFONT;
3297 gui_mch_free_font(gui.wide_boldital_font);
3298 gui.wide_boldital_font = NOFONT;
3299
3300 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003301 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003302 {
3303 if (!lf.lfItalic)
3304 {
3305 lf.lfItalic = TRUE;
3306 gui.wide_ital_font = get_font_handle(&lf);
3307 lf.lfItalic = FALSE;
3308 }
3309 if (lf.lfWeight < FW_BOLD)
3310 {
3311 lf.lfWeight = FW_BOLD;
3312 gui.wide_bold_font = get_font_handle(&lf);
3313 if (!lf.lfItalic)
3314 {
3315 lf.lfItalic = TRUE;
3316 gui.wide_boldital_font = get_font_handle(&lf);
3317 }
3318 }
3319 }
3320}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003321
3322/*
3323 * Initialise vim to use the font with the given name.
3324 * Return FAIL if the font could not be loaded, OK otherwise.
3325 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003327gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328{
K.Takatac81e9bf2022-01-16 14:15:49 +00003329 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330 GuiFont font = NOFONT;
3331 char_u *p;
3332
Bram Moolenaar734a8672019-12-02 22:49:38 +01003333 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003335 {
3336 lfOrig = lf;
3337 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003338 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003339 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340 if (font == NOFONT)
3341 return FAIL;
3342
3343 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003344 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003345#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 norm_logfont = lf;
3347 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003348 if (!s_in_dpichanged)
3349 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350#endif
3351 gui_mch_free_font(gui.norm_font);
3352 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003353 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354 GetFontSize(font);
3355
K.Takatac81e9bf2022-01-16 14:15:49 +00003356 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003357 if (p != NULL)
3358 {
3359 hl_set_font_name(p);
3360
Bram Moolenaar734a8672019-12-02 22:49:38 +01003361 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003362 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3363 {
3364 vim_free(p_guifont);
3365 p_guifont = p;
3366 }
3367 else
3368 vim_free(p);
3369 }
3370
3371 gui_mch_free_font(gui.ital_font);
3372 gui.ital_font = NOFONT;
3373 gui_mch_free_font(gui.bold_font);
3374 gui.bold_font = NOFONT;
3375 gui_mch_free_font(gui.boldital_font);
3376 gui.boldital_font = NOFONT;
3377
3378 if (!lf.lfItalic)
3379 {
3380 lf.lfItalic = TRUE;
3381 gui.ital_font = get_font_handle(&lf);
3382 lf.lfItalic = FALSE;
3383 }
3384 if (lf.lfWeight < FW_BOLD)
3385 {
3386 lf.lfWeight = FW_BOLD;
3387 gui.bold_font = get_font_handle(&lf);
3388 if (!lf.lfItalic)
3389 {
3390 lf.lfItalic = TRUE;
3391 gui.boldital_font = get_font_handle(&lf);
3392 }
3393 }
3394
3395 return OK;
3396}
3397
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398/*
3399 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003400 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 */
3402 int
3403gui_mch_maximized(void)
3404{
3405 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003406 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407
3408 wp.length = sizeof(WINDOWPLACEMENT);
3409 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003410 {
3411 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003412 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003413 && wp.flags == WPF_RESTORETOMAXIMIZED))
3414 return TRUE;
3415 if (wp.showCmd == SW_SHOWMINIMIZED)
3416 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003418 // Assume the window is snapped when the sizes from two APIs differ.
3419 GetWindowRect(s_hwnd, &rc);
3420 if ((rc.right - rc.left !=
3421 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3422 || (rc.bottom - rc.top !=
3423 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3424 return TRUE;
3425 }
3426 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003427}
3428
3429/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003430 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3431 * is set. Compute the new Rows and Columns. This is like resizing the
3432 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433 */
3434 void
3435gui_mch_newfont(void)
3436{
3437 RECT rect;
3438
3439 GetWindowRect(s_hwnd, &rect);
3440 if (win_socket_id == 0)
3441 {
3442 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003443 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3444 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003445 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003446 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3447 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3448 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3449 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003450 }
3451 else
3452 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003453 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003454 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003455 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456 }
3457}
3458
3459/*
3460 * Set the window title
3461 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462 void
3463gui_mch_settitle(
3464 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003465 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466{
3467 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3468}
3469
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003470#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003471// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3472// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473static LPCSTR mshape_idcs[] =
3474{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003475 IDC_ARROW, // arrow
3476 MAKEINTRESOURCE(0), // blank
3477 IDC_IBEAM, // beam
3478 IDC_SIZENS, // updown
3479 IDC_SIZENS, // udsizing
3480 IDC_SIZEWE, // leftright
3481 IDC_SIZEWE, // lrsizing
3482 IDC_WAIT, // busy
3483 IDC_NO, // no
3484 IDC_ARROW, // crosshair
3485 IDC_ARROW, // hand1
3486 IDC_ARROW, // hand2
3487 IDC_ARROW, // pencil
3488 IDC_ARROW, // question
3489 IDC_ARROW, // right-arrow
3490 IDC_UPARROW, // up-arrow
3491 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492};
3493
3494 void
3495mch_set_mouse_shape(int shape)
3496{
3497 LPCSTR idc;
3498
3499 if (shape == MSHAPE_HIDE)
3500 ShowCursor(FALSE);
3501 else
3502 {
3503 if (shape >= MSHAPE_NUMBERED)
3504 idc = IDC_ARROW;
3505 else
3506 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003507 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508 if (!p_mh)
3509 {
3510 POINT mp;
3511
Bram Moolenaar734a8672019-12-02 22:49:38 +01003512 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003513 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 (void)SetCursorPos(mp.x, mp.y);
3515 ShowCursor(TRUE);
3516 }
3517 }
3518}
3519#endif
3520
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003521#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523 * Wide version of convert_filter().
3524 */
3525 static WCHAR *
3526convert_filterW(char_u *s)
3527{
3528 char_u *tmp;
3529 int len;
3530 WCHAR *res;
3531
3532 tmp = convert_filter(s);
3533 if (tmp == NULL)
3534 return NULL;
3535 len = (int)STRLEN(s) + 3;
3536 res = enc_to_utf16(tmp, &len);
3537 vim_free(tmp);
3538 return res;
3539}
3540
3541/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003542 * Pop open a file browser and return the file selected, in allocated memory,
3543 * or NULL if Cancel is hit.
3544 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3545 * title - Title message for the file browser dialog.
3546 * dflt - Default name of file.
3547 * ext - Default extension to be added to files without extensions.
3548 * initdir - directory in which to open the browser (NULL = current dir)
3549 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003551 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003552gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003553 int saving,
3554 char_u *title,
3555 char_u *dflt,
3556 char_u *ext,
3557 char_u *initdir,
3558 char_u *filter)
3559{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003560 // We always use the wide function. This means enc_to_utf16() must work,
3561 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003562 OPENFILENAMEW fileStruct;
3563 WCHAR fileBuf[MAXPATHL];
3564 WCHAR *wp;
3565 int i;
3566 WCHAR *titlep = NULL;
3567 WCHAR *extp = NULL;
3568 WCHAR *initdirp = NULL;
3569 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003570 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003571 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572
3573 if (dflt == NULL)
3574 fileBuf[0] = NUL;
3575 else
3576 {
3577 wp = enc_to_utf16(dflt, NULL);
3578 if (wp == NULL)
3579 fileBuf[0] = NUL;
3580 else
3581 {
3582 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3583 fileBuf[i] = wp[i];
3584 fileBuf[i] = NUL;
3585 vim_free(wp);
3586 }
3587 }
3588
Bram Moolenaar734a8672019-12-02 22:49:38 +01003589 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003590 filterp = convert_filterW(filter);
3591
Bram Moolenaara80faa82020-04-12 19:37:17 +02003592 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003593# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003594 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003595 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003596# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003597 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003598# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599
3600 if (title != NULL)
3601 titlep = enc_to_utf16(title, NULL);
3602 fileStruct.lpstrTitle = titlep;
3603
3604 if (ext != NULL)
3605 extp = enc_to_utf16(ext, NULL);
3606 fileStruct.lpstrDefExt = extp;
3607
3608 fileStruct.lpstrFile = fileBuf;
3609 fileStruct.nMaxFile = MAXPATHL;
3610 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003611 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3612 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003613 if (initdir != NULL && *initdir != NUL)
3614 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003615 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 initdirp = enc_to_utf16(initdir, NULL);
3617 if (initdirp != NULL)
3618 {
3619 for (wp = initdirp; *wp != NUL; ++wp)
3620 if (*wp == '/')
3621 *wp = '\\';
3622 }
3623 fileStruct.lpstrInitialDir = initdirp;
3624 }
3625
3626 /*
3627 * TODO: Allow selection of multiple files. Needs another arg to this
3628 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3629 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3630 * files that don't exist yet, so I haven't put it in. What about
3631 * OFN_PATHMUSTEXIST?
3632 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3633 */
3634 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003635# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003636 if (curbuf->b_p_bin)
3637 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003638# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003639 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003640 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003641 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003642 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003643
3644 vim_free(filterp);
3645 vim_free(initdirp);
3646 vim_free(titlep);
3647 vim_free(extp);
3648
K.Takata14b8d6a2022-01-20 15:05:22 +00003649 if (!ret)
3650 return NULL;
3651
3652 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003654 if (p == NULL)
3655 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 SetFocus(s_hwnd);
3659
Bram Moolenaar734a8672019-12-02 22:49:38 +01003660 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003661 q = vim_strsave(shorten_fname1(p));
3662 vim_free(p);
3663 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003665
3666
3667/*
3668 * Convert the string s to the proper format for a filter string by replacing
3669 * the \t and \n delimiters with \0.
3670 * Returns the converted string in allocated memory.
3671 *
3672 * Keep in sync with convert_filterW() above!
3673 */
3674 static char_u *
3675convert_filter(char_u *s)
3676{
3677 char_u *res;
3678 unsigned s_len = (unsigned)STRLEN(s);
3679 unsigned i;
3680
3681 res = alloc(s_len + 3);
3682 if (res != NULL)
3683 {
3684 for (i = 0; i < s_len; ++i)
3685 if (s[i] == '\t' || s[i] == '\n')
3686 res[i] = '\0';
3687 else
3688 res[i] = s[i];
3689 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003690 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 res[s_len + 1] = NUL;
3692 res[s_len + 2] = NUL;
3693 }
3694 return res;
3695}
3696
3697/*
3698 * Select a directory.
3699 */
3700 char_u *
3701gui_mch_browsedir(char_u *title, char_u *initdir)
3702{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003703 // We fake this: Use a filter that doesn't select anything and a default
3704 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3706 initdir, (char_u *)_("Directory\t*.nothing\n"));
3707}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003708#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003709
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 static void
3711_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003712 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003713 HDROP hDrop)
3714{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003715#define BUFPATHLEN _MAX_PATH
3716#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003717 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718 char szFile[BUFPATHLEN];
3719 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3720 UINT i;
3721 char_u **fnames;
3722 POINT pt;
3723 int_u modifiers = 0;
3724
Bram Moolenaar734a8672019-12-02 22:49:38 +01003725 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728 DragQueryPoint(hDrop, &pt);
3729 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3730
3731 reset_VIsual();
3732
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003733 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003734
3735 if (fnames != NULL)
3736 for (i = 0; i < cFiles; ++i)
3737 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3739 fnames[i] = utf16_to_enc(wszFile, NULL);
3740 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003741 {
3742 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3743 fnames[i] = vim_strsave((char_u *)szFile);
3744 }
3745 }
3746
3747 DragFinish(hDrop);
3748
3749 if (fnames != NULL)
3750 {
3751 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3752 modifiers |= MOUSE_SHIFT;
3753 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3754 modifiers |= MOUSE_CTRL;
3755 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3756 modifiers |= MOUSE_ALT;
3757
3758 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3759
3760 s_need_activate = TRUE;
3761 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762}
3763
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 static int
3765_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003766 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 HWND hwndCtl,
3768 UINT code,
3769 int pos)
3770{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003771 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 scrollbar_T *sb, *sb_info;
3773 long val;
3774 int dragging = FALSE;
3775 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 SCROLLINFO si;
3777
3778 si.cbSize = sizeof(si);
3779 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780
3781 sb = gui_mswin_find_scrollbar(hwndCtl);
3782 if (sb == NULL)
3783 return 0;
3784
Bram Moolenaar734a8672019-12-02 22:49:38 +01003785 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 {
3787 /*
3788 * Careful: need to get scrollbar info out of first (left) scrollbar
3789 * for window, but keep real scrollbar too because we must pass it to
3790 * gui_drag_scrollbar().
3791 */
3792 sb_info = &sb->wp->w_scrollbars[0];
3793 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003794 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 sb_info = sb;
3796 val = sb_info->value;
3797
3798 switch (code)
3799 {
3800 case SB_THUMBTRACK:
3801 val = pos;
3802 dragging = TRUE;
3803 if (sb->scroll_shift > 0)
3804 val <<= sb->scroll_shift;
3805 break;
3806 case SB_LINEDOWN:
3807 val++;
3808 break;
3809 case SB_LINEUP:
3810 val--;
3811 break;
3812 case SB_PAGEDOWN:
3813 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3814 break;
3815 case SB_PAGEUP:
3816 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3817 break;
3818 case SB_TOP:
3819 val = 0;
3820 break;
3821 case SB_BOTTOM:
3822 val = sb_info->max;
3823 break;
3824 case SB_ENDSCROLL:
3825 if (prev_code == SB_THUMBTRACK)
3826 {
3827 /*
3828 * "pos" only gives us 16-bit data. In case of large file,
3829 * use GetScrollPos() which returns 32-bit. Unfortunately it
3830 * is not valid while the scrollbar is being dragged.
3831 */
3832 val = GetScrollPos(hwndCtl, SB_CTL);
3833 if (sb->scroll_shift > 0)
3834 val <<= sb->scroll_shift;
3835 }
3836 break;
3837
3838 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003839 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003840 return 0;
3841 }
3842 prev_code = code;
3843
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003844 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3845 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003846
3847 /*
3848 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3849 */
3850 if (sb->wp != NULL)
3851 {
3852 scrollbar_T *sba = sb->wp->w_scrollbars;
3853 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3854
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003856 }
3857
Bram Moolenaar734a8672019-12-02 22:49:38 +01003858 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003859 s_busy_processing = TRUE;
3860
Bram Moolenaar734a8672019-12-02 22:49:38 +01003861 // When "allow_scrollbar" is FALSE still need to remember the new
3862 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863 dont_scroll = !allow_scrollbar;
3864
Bram Moolenaara338adc2018-01-31 20:51:47 +01003865 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003866 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003867 mch_enable_flush();
3868 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003869
3870 s_busy_processing = FALSE;
3871 dont_scroll = dont_scroll_save;
3872
3873 return 0;
3874}
3875
3876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877#ifdef FEAT_XPM_W32
3878# include "xpm_w32.h"
3879#endif
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar734a8672019-12-02 22:49:38 +01003882// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883#define TEAROFF_PADDING_X 2
3884#define TEAROFF_BUTTON_PAD_X 8
3885#define TEAROFF_MIN_WIDTH 200
3886#define TEAROFF_SUBMENU_LABEL ">>"
3887#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3888
3889
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003890#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891# define ID_BEVAL_TOOLTIP 200
3892# define BEVAL_TEXT_LEN MAXPATHL
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003895static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896static DWORD LastActivity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003897#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003898
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003899
Bram Moolenaar734a8672019-12-02 22:49:38 +01003900// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
3902#ifdef FEAT_MENU
3903static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906/*
3907 * Use the system font for dialogs and tear-off menus. Remove this line to
3908 * use DLG_FONT_NAME.
3909 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003910#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#define VIM_CLASSW L"Vim"
3914
Bram Moolenaar734a8672019-12-02 22:49:38 +01003915// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3916// thus there should be room for every dialog. For tearoffs it's made bigger
3917// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918#define DLG_ALLOC_SIZE 16 * 1024
3919
3920/*
3921 * stuff for dialogs, menus, tearoffs etc.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923static PWORD
3924add_dialog_element(
3925 PWORD p,
3926 DWORD lStyle,
3927 WORD x,
3928 WORD y,
3929 WORD w,
3930 WORD h,
3931 WORD Id,
3932 WORD clss,
3933 const char *caption);
3934static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003935static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003936#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939static void get_dialog_font_metrics(void);
3940
3941static int dialog_default_button = -1;
3942
Bram Moolenaar734a8672019-12-02 22:49:38 +01003943// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#ifdef FEAT_TOOLBAR
3947static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00003948static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003949static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00003951#else
3952# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953#endif
3954
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003955#ifdef FEAT_GUI_TABLINE
3956static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003957static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003958#endif
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#ifdef FEAT_MBYTE_IME
3961static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
3962static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
3963#endif
3964#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
3965# ifdef NOIME
3966typedef struct tagCOMPOSITIONFORM {
3967 DWORD dwStyle;
3968 POINT ptCurrentPos;
3969 RECT rcArea;
3970} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
3971typedef HANDLE HIMC;
3972# endif
3973
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003974static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003975static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
3976static HIMC (WINAPI *pImmGetContext)(HWND);
3977static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
3978static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
3979static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
3980static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003981static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
3982static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003983static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
3984static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00003985static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986static void dyn_imm_load(void);
3987#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988# define pImmGetCompositionStringW ImmGetCompositionStringW
3989# define pImmGetContext ImmGetContext
3990# define pImmAssociateContext ImmAssociateContext
3991# define pImmReleaseContext ImmReleaseContext
3992# define pImmGetOpenStatus ImmGetOpenStatus
3993# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003994# define pImmGetCompositionFontW ImmGetCompositionFontW
3995# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996# define pImmSetCompositionWindow ImmSetCompositionWindow
3997# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00003998# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#endif
4000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001#ifdef FEAT_MENU
4002/*
4003 * Figure out how high the menu bar is at the moment.
4004 */
4005 static int
4006gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004007 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
4009 static int old_menu_height = -1;
4010
4011 RECT rc1, rc2;
4012 int num;
4013 int menu_height;
4014
4015 if (gui.menu_is_active)
4016 num = GetMenuItemCount(s_menuBar);
4017 else
4018 num = 0;
4019
4020 if (num == 0)
4021 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004022 else if (IsMinimized(s_hwnd))
4023 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004024 // The height of the menu cannot be determined while the window is
4025 // minimized. Take the previous height if the menu is changed in that
4026 // state, to avoid that Vim's vertical window size accidentally
4027 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004028 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 else
4031 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004032 /*
4033 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4034 * seem to have been set yet, so menu wraps in default window
4035 * width which is very narrow. Instead just return height of a
4036 * single menu item. Will still be wrong when the menu really
4037 * should wrap over more than one line.
4038 */
4039 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4040 if (gui.starting)
4041 menu_height = rc1.bottom - rc1.top + 1;
4042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004044 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4045 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047 }
4048
4049 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004050 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004051 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053 return menu_height;
4054}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004055#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057
4058/*
4059 * Setup for the Intellimouse
4060 */
4061 static void
4062init_mouse_wheel(void)
4063{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004064 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
Bram Moolenaar734a8672019-12-02 22:49:38 +01004066 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004067 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4068 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070
4071
Bram Moolenaarae20f342019-11-05 21:09:23 +01004072/*
4073 * Intellimouse wheel handler.
4074 * Treat a mouse wheel event as if it were a scroll request.
4075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 static void
4077_OnMouseWheel(
4078 HWND hwnd,
4079 short zDelta)
4080{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 int i;
4082 int size;
4083 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004084 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (mouse_scroll_lines == 0)
4087 init_mouse_wheel();
4088
Bram Moolenaarae20f342019-11-05 21:09:23 +01004089 wp = gui_mouse_window(FIND_POPUP);
4090
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004091#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004092 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004093 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004094 cmdarg_T cap;
4095 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004096
Bram Moolenaarae20f342019-11-05 21:09:23 +01004097 // Mouse hovers over popup window, scroll it if possible.
4098 mouse_row = wp->w_winrow;
4099 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004100 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004101 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4102 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4103 clear_oparg(&oa);
4104 cap.oap = &oa;
4105 nv_mousescroll(&cap);
4106 update_screen(0);
4107 setcursor();
4108 out_flush();
4109 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004110 }
4111#endif
4112
Bram Moolenaarae20f342019-11-05 21:09:23 +01004113 if (wp == NULL || !p_scf)
4114 wp = curwin;
4115
4116 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4117 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4118 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4119 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4120 else
4121 return;
4122 size = wp->w_height;
4123
Bram Moolenaara338adc2018-01-31 20:51:47 +01004124 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if (mouse_scroll_lines > 0
4126 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4127 {
4128 for (i = mouse_scroll_lines; i > 0; --i)
4129 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4130 }
4131 else
4132 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004133 mch_enable_flush();
4134 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135}
4136
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004137#ifdef USE_SYSMENU_FONT
4138/*
4139 * Get Menu Font.
4140 * Return OK or FAIL.
4141 */
4142 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004143gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004144{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004145 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004146
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004147 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4148 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004149 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004150 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004151 &nm,
4152 0))
4153 return FAIL;
4154 *lf = nm.lfMenuFont;
4155 return OK;
4156}
4157#endif
4158
4159
4160#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4161/*
4162 * Set the GUI tabline font to the system menu font
4163 */
4164 static void
4165set_tabline_font(void)
4166{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004167 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004168 HFONT font;
4169 HWND hwnd;
4170 HDC hdc;
4171 HFONT hfntOld;
4172 TEXTMETRIC tm;
4173
4174 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4175 return;
4176
K.Takatac81e9bf2022-01-16 14:15:49 +00004177 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004178 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004179
4180 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4181
4182 /*
4183 * Compute the height of the font used for the tab text
4184 */
4185 hwnd = GetDesktopWindow();
4186 hdc = GetWindowDC(hwnd);
4187 hfntOld = SelectFont(hdc, font);
4188
4189 GetTextMetrics(hdc, &tm);
4190
4191 SelectFont(hdc, hfntOld);
4192 ReleaseDC(hwnd, hdc);
4193
4194 /*
4195 * The space used by the tab border and the space between the tab label
4196 * and the tab border is included as 7.
4197 */
4198 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4199}
K.Takatac81e9bf2022-01-16 14:15:49 +00004200#else
4201# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004202#endif
4203
Bram Moolenaar520470a2005-06-16 21:59:56 +00004204/*
4205 * Invoked when a setting was changed.
4206 */
4207 static LRESULT CALLBACK
4208_OnSettingChange(UINT n)
4209{
4210 if (n == SPI_SETWHEELSCROLLLINES)
4211 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4212 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004213 if (n == SPI_SETNONCLIENTMETRICS)
4214 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004215 return 0;
4216}
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#ifdef FEAT_NETBEANS_INTG
4219 static void
4220_OnWindowPosChanged(
4221 HWND hwnd,
4222 const LPWINDOWPOS lpwpos)
4223{
4224 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004225 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4228 || lpwpos->cx != cx || lpwpos->cy != cy))
4229 {
4230 x = lpwpos->x;
4231 y = lpwpos->y;
4232 cx = lpwpos->cx;
4233 cy = lpwpos->cy;
4234 netbeans_frame_moved(x, y);
4235 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004236 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004237 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238}
4239#endif
4240
K.Takata4f2417f2021-06-05 16:25:32 +02004241
4242static HWND hwndTip = NULL;
4243
4244 static void
4245show_sizing_tip(int cols, int rows)
4246{
4247 TOOLINFOA ti = {sizeof(ti)};
4248 char buf[32];
4249
4250 ti.hwnd = s_hwnd;
4251 ti.uId = (UINT_PTR)s_hwnd;
4252 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4253 ti.lpszText = buf;
4254 sprintf(buf, "%dx%d", cols, rows);
4255 if (hwndTip == NULL)
4256 {
4257 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4258 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4259 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4260 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4261 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4262 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4263 }
4264 else
4265 {
4266 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4267 }
4268 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4269}
4270
4271 static void
4272destroy_sizing_tip(void)
4273{
4274 if (hwndTip != NULL)
4275 {
4276 DestroyWindow(hwndTip);
4277 hwndTip = NULL;
4278 }
4279}
4280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 static int
4282_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 UINT fwSide,
4284 LPRECT lprc)
4285{
4286 int w, h;
4287 int valid_w, valid_h;
4288 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004289 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 w = lprc->right - lprc->left;
4292 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004293 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 w_offset = w - valid_w;
4295 h_offset = h - valid_h;
4296
4297 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4298 || fwSide == WMSZ_BOTTOMLEFT)
4299 lprc->left += w_offset;
4300 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4301 || fwSide == WMSZ_BOTTOMRIGHT)
4302 lprc->right -= w_offset;
4303
4304 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4305 || fwSide == WMSZ_TOPRIGHT)
4306 lprc->top += h_offset;
4307 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4308 || fwSide == WMSZ_BOTTOMRIGHT)
4309 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004310
4311 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return TRUE;
4313}
4314
K.Takata92000e22022-01-20 15:10:57 +00004315#ifdef FEAT_GUI_TABLINE
4316 static void
4317_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4318{
4319 if (gui_mch_showing_tabline())
4320 {
4321 POINT pt;
4322 RECT rect;
4323
4324 /*
4325 * If the cursor is on the tabline, display the tab menu
4326 */
4327 GetCursorPos(&pt);
4328 GetWindowRect(s_textArea, &rect);
4329 if (pt.y < rect.top)
4330 {
4331 show_tabline_popup_menu();
4332 return;
4333 }
4334 }
4335 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4336}
4337
4338 static void
4339_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4340{
4341 /*
4342 * If the user double clicked the tabline, create a new tab
4343 */
4344 if (gui_mch_showing_tabline())
4345 {
4346 POINT pt;
4347 RECT rect;
4348
4349 GetCursorPos(&pt);
4350 GetWindowRect(s_textArea, &rect);
4351 if (pt.y < rect.top)
4352 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4353 }
4354 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4355}
4356#endif
4357
4358 static UINT
4359_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4360{
4361 UINT result;
4362 int x, y;
4363
4364 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4365 if (result != HTCLIENT)
4366 return result;
4367
4368#ifdef FEAT_GUI_TABLINE
4369 if (gui_mch_showing_tabline())
4370 {
4371 RECT rct;
4372
4373 // If the cursor is on the GUI tabline, don't process this event
4374 GetWindowRect(s_textArea, &rct);
4375 if (yPos < rct.top)
4376 return result;
4377 }
4378#endif
4379 (void)gui_mch_get_winpos(&x, &y);
4380 xPos -= x;
4381
4382 if (xPos < 48) // <VN> TODO should use system metric?
4383 return HTBOTTOMLEFT;
4384 else
4385 return HTBOTTOMRIGHT;
4386}
4387
4388#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4389 static LRESULT
4390_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4391{
4392 switch (hdr->code)
4393 {
4394 case TTN_GETDISPINFOW:
4395 case TTN_GETDISPINFO:
4396 {
4397 char_u *str = NULL;
4398 static void *tt_text = NULL;
4399
4400 VIM_CLEAR(tt_text);
4401
4402# ifdef FEAT_GUI_TABLINE
4403 if (gui_mch_showing_tabline()
4404 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4405 {
4406 POINT pt;
4407 /*
4408 * Mouse is over the GUI tabline. Display the
4409 * tooltip for the tab under the cursor
4410 *
4411 * Get the cursor position within the tab control
4412 */
4413 GetCursorPos(&pt);
4414 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4415 {
4416 TCHITTESTINFO htinfo;
4417 int idx;
4418
4419 /*
4420 * Get the tab under the cursor
4421 */
4422 htinfo.pt.x = pt.x;
4423 htinfo.pt.y = pt.y;
4424 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4425 if (idx != -1)
4426 {
4427 tabpage_T *tp;
4428
4429 tp = find_tabpage(idx + 1);
4430 if (tp != NULL)
4431 {
4432 get_tabline_label(tp, TRUE);
4433 str = NameBuff;
4434 }
4435 }
4436 }
4437 }
4438# endif
4439# ifdef FEAT_TOOLBAR
4440# ifdef FEAT_GUI_TABLINE
4441 else
4442# endif
4443 {
4444 UINT idButton;
4445 vimmenu_T *pMenu;
4446
4447 idButton = (UINT) hdr->idFrom;
4448 pMenu = gui_mswin_find_menu(root_menu, idButton);
4449 if (pMenu)
4450 str = pMenu->strings[MENU_INDEX_TIP];
4451 }
4452# endif
4453 if (str == NULL)
4454 break;
4455
4456 // Set the maximum width, this also enables using \n for
4457 // line break.
4458 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4459
4460 if (hdr->code == TTN_GETDISPINFOW)
4461 {
4462 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4463
4464 tt_text = enc_to_utf16(str, NULL);
4465 lpdi->lpszText = tt_text;
4466 // can't show tooltip if failed
4467 }
4468 else
4469 {
4470 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4471
4472 if (STRLEN(str) < sizeof(lpdi->szText)
4473 || ((tt_text = vim_strsave(str)) == NULL))
4474 vim_strncpy((char_u *)lpdi->szText, str,
4475 sizeof(lpdi->szText) - 1);
4476 else
4477 lpdi->lpszText = tt_text;
4478 }
4479 }
4480 break;
4481
4482# ifdef FEAT_GUI_TABLINE
4483 case TCN_SELCHANGE:
4484 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4485 {
4486 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4487 return 0L;
4488 }
4489 break;
4490
4491 case NM_RCLICK:
4492 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4493 {
4494 show_tabline_popup_menu();
4495 return 0L;
4496 }
4497 break;
4498# endif
4499
4500 default:
4501 break;
4502 }
4503 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4504}
4505#endif
4506
4507#if defined(MENUHINTS) && defined(FEAT_MENU)
4508 static LRESULT
4509_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4510{
4511 if (((UINT) HIWORD(wParam)
4512 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4513 == MF_HILITE
4514 && (State & CMDLINE) == 0)
4515 {
4516 UINT idButton;
4517 vimmenu_T *pMenu;
4518 static int did_menu_tip = FALSE;
4519
4520 if (did_menu_tip)
4521 {
4522 msg_clr_cmdline();
4523 setcursor();
4524 out_flush();
4525 did_menu_tip = FALSE;
4526 }
4527
4528 idButton = (UINT)LOWORD(wParam);
4529 pMenu = gui_mswin_find_menu(root_menu, idButton);
4530 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4531 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4532 {
4533 ++msg_hist_off;
4534 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4535 --msg_hist_off;
4536 setcursor();
4537 out_flush();
4538 did_menu_tip = TRUE;
4539 }
4540 return 0L;
4541 }
4542 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4543}
4544#endif
4545
K.Takatac81e9bf2022-01-16 14:15:49 +00004546 static LRESULT
4547_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4548{
4549 s_dpi = ydpi;
4550 s_in_dpichanged = TRUE;
4551 //TRACE("DPI: %d", ydpi);
4552
4553 update_scrollbar_size();
4554 update_toolbar_size();
4555 set_tabline_font();
4556
4557 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4558 gui_get_wide_font();
4559 gui_mswin_get_menu_height(FALSE);
4560#ifdef FEAT_MBYTE_IME
4561 im_set_position(gui.row, gui.col);
4562#endif
4563 InvalidateRect(hwnd, NULL, TRUE);
4564
4565 s_in_dpichanged = FALSE;
4566 return 0L;
4567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569
4570 static LRESULT CALLBACK
4571_WndProc(
4572 HWND hwnd,
4573 UINT uMsg,
4574 WPARAM wParam,
4575 LPARAM lParam)
4576{
4577 /*
4578 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4579 hwnd, uMsg, wParam, lParam);
4580 */
4581
4582 HandleMouseHide(uMsg, lParam);
4583
4584 s_uMsg = uMsg;
4585 s_wParam = wParam;
4586 s_lParam = lParam;
4587
4588 switch (uMsg)
4589 {
4590 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4591 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004592 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004594 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4596 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4597 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4598 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4599#ifdef FEAT_MENU
4600 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4601#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004602 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4603 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4605 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004606 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4607 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4609 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4610 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4611#ifdef FEAT_NETBEANS_INTG
4612 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4613#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004614#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004615 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4616 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004617#endif
K.Takata92000e22022-01-20 15:10:57 +00004618 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004619
Bram Moolenaar734a8672019-12-02 22:49:38 +01004620 case WM_QUERYENDSESSION: // System wants to go down.
4621 gui_shell_closed(); // Will exit when no changed buffers.
4622 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004625 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004628 return 0L;
4629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 break;
4631
4632 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004633 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4634 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004635 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 return 0L;
4637
4638 case WM_SYSCHAR:
4639 /*
4640 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4641 * shortcut key, handle like a typed ALT key, otherwise call Windows
4642 * ALT key handling.
4643 */
4644#ifdef FEAT_MENU
4645 if ( !gui.menu_is_active
4646 || p_wak[0] == 'n'
4647 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4648 )
4649#endif
4650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004651 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 return 0L;
4653 }
4654#ifdef FEAT_MENU
4655 else
K.Takata4ac893f2022-01-20 12:44:28 +00004656 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#endif
4658
4659 case WM_SYSKEYUP:
4660#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004661 // This used to be done only when menu is active: ALT key is used for
4662 // that. But that caused problems when menu is disabled and using
4663 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4664 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004665 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004667 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#endif
4669
K.Takata4f2417f2021-06-05 16:25:32 +02004670 case WM_EXITSIZEMOVE:
4671 destroy_sizing_tip();
4672 break;
4673
Bram Moolenaar734a8672019-12-02 22:49:38 +01004674 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677 case WM_MOUSEWHEEL:
4678 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004679 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
Bram Moolenaar734a8672019-12-02 22:49:38 +01004681 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004682 case WM_SETTINGCHANGE:
4683 return _OnSettingChange((UINT)wParam);
4684
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004685#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004687 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#endif
K.Takata92000e22022-01-20 15:10:57 +00004689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#if defined(MENUHINTS) && defined(FEAT_MENU)
4691 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004692 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
4695#ifdef FEAT_MBYTE_IME
4696 case WM_IME_NOTIFY:
4697 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004698 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004699 return 1L;
4700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 case WM_IME_COMPOSITION:
4702 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004703 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004704 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004706 case WM_DPICHANGED:
4707 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4708 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
4710 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004712 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#endif
K.Takata92000e22022-01-20 15:10:57 +00004715 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717
K.Takata4ac893f2022-01-20 12:44:28 +00004718 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719}
4720
4721/*
4722 * End of call-back routines
4723 */
4724
Bram Moolenaar734a8672019-12-02 22:49:38 +01004725// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726HWND vim_parent_hwnd = NULL;
4727
4728 static BOOL CALLBACK
4729FindWindowTitle(HWND hwnd, LPARAM lParam)
4730{
4731 char buf[2048];
4732 char *title = (char *)lParam;
4733
4734 if (GetWindowText(hwnd, buf, sizeof(buf)))
4735 {
4736 if (strstr(buf, title) != NULL)
4737 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004738 // Found it. Store the window ref. and quit searching if MDI
4739 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004741 if (vim_parent_hwnd != NULL)
4742 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004745 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746}
4747
4748/*
4749 * Invoked for '-P "title"' argument: search for parent application to open
4750 * our window in.
4751 */
4752 void
4753gui_mch_set_parent(char *title)
4754{
4755 EnumWindows(FindWindowTitle, (LPARAM)title);
4756 if (vim_parent_hwnd == NULL)
4757 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004758 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 mch_exit(2);
4760 }
4761}
4762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 static void
4765ole_error(char *arg)
4766{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004767 char buf[IOSIZE];
4768
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004769# ifdef VIMDLL
4770 gui.in_use = mch_is_gui_executable();
4771# endif
4772
Bram Moolenaar734a8672019-12-02 22:49:38 +01004773 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004774 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004775 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004776 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004780#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4781 static char *
4782gvim_error(void)
4783{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004784 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004785
4786 if (starting)
4787 {
4788 mch_errmsg(msg);
4789 mch_errmsg("\n");
4790 mch_exit(2);
4791 }
4792 return msg;
4793}
4794
4795 char *
4796gui_mch_do_spawn(char_u *arg)
4797{
4798 int len;
4799# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4800 char_u *session = NULL;
4801 LPWSTR tofree1 = NULL;
4802# endif
4803 WCHAR name[MAX_PATH];
4804 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4805 STARTUPINFOW si = {sizeof(si)};
4806 PROCESS_INFORMATION pi;
4807
4808 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4809 goto error;
4810 p = wcsrchr(name, L'\\');
4811 if (p == NULL)
4812 goto error;
4813 // Replace the executable name from vim(d).exe to gvim(d).exe.
4814# ifdef DEBUG
4815 wcscpy(p + 1, L"gvimd.exe");
4816# else
4817 wcscpy(p + 1, L"gvim.exe");
4818# endif
4819
4820# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4821 if (starting)
4822# endif
4823 {
4824 // Pass the command line to the new process.
4825 p = GetCommandLineW();
4826 // Skip 1st argument.
4827 while (*p && *p != L' ' && *p != L'\t')
4828 {
4829 if (*p == L'"')
4830 {
4831 while (*p && *p != L'"')
4832 ++p;
4833 if (*p)
4834 ++p;
4835 }
4836 else
4837 ++p;
4838 }
4839 cmd = p;
4840 }
4841# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4842 else
4843 {
4844 // Create a session file and pass it to the new process.
4845 LPWSTR wsession;
4846 char_u *savebg;
4847 int ret;
4848
4849 session = vim_tempname('s', FALSE);
4850 if (session == NULL)
4851 goto error;
4852 savebg = p_bg;
4853 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4854 ret = write_session_file(session);
4855 vim_free(p_bg);
4856 p_bg = savebg;
4857 if (!ret)
4858 goto error;
4859 wsession = enc_to_utf16(session, NULL);
4860 if (wsession == NULL)
4861 goto error;
4862 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004863 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004864 if (cmd == NULL)
4865 {
4866 vim_free(wsession);
4867 goto error;
4868 }
4869 tofree1 = cmd;
4870 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4871 wsession, wsession);
4872 vim_free(wsession);
4873 }
4874# endif
4875
4876 // Check additional arguments to the `:gui` command.
4877 if (arg != NULL)
4878 {
4879 warg = enc_to_utf16(arg, NULL);
4880 if (warg == NULL)
4881 goto error;
4882 tofree2 = warg;
4883 }
4884 else
4885 warg = L"";
4886
4887 // Set up the new command line.
4888 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004889 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004890 if (newcmd == NULL)
4891 goto error;
4892 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4893
4894 // Spawn a new GUI process.
4895 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4896 NULL, NULL, &si, &pi))
4897 goto error;
4898 CloseHandle(pi.hProcess);
4899 CloseHandle(pi.hThread);
4900 mch_exit(0);
4901
4902error:
4903# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4904 if (session)
4905 mch_remove(session);
4906 vim_free(session);
4907 vim_free(tofree1);
4908# endif
4909 vim_free(newcmd);
4910 vim_free(tofree2);
4911 return gvim_error();
4912}
4913#endif
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915/*
4916 * Parse the GUI related command-line arguments. Any arguments used are
4917 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02004918 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 */
4920 void
4921gui_mch_prepare(int *argc, char **argv)
4922{
4923 int silent = FALSE;
4924 int idx;
4925
Bram Moolenaar734a8672019-12-02 22:49:38 +01004926 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4928 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004929 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4931 && (argv[2][0] == '-' || argv[2][0] == '/'))
4932 {
4933 silent = TRUE;
4934 idx = 2;
4935 }
4936 else
4937 idx = 1;
4938
Bram Moolenaar734a8672019-12-02 22:49:38 +01004939 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (STRICMP(argv[idx] + 1, "register") == 0)
4941 {
4942#ifdef FEAT_OLE
4943 RegisterMe(silent);
4944 mch_exit(0);
4945#else
4946 if (!silent)
4947 ole_error("register");
4948 mch_exit(2);
4949#endif
4950 }
4951
Bram Moolenaar734a8672019-12-02 22:49:38 +01004952 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4954 {
4955#ifdef FEAT_OLE
4956 UnregisterMe(!silent);
4957 mch_exit(0);
4958#else
4959 if (!silent)
4960 ole_error("unregister");
4961 mch_exit(2);
4962#endif
4963 }
4964
Bram Moolenaar734a8672019-12-02 22:49:38 +01004965 // Ignore an -embedding argument. It is only relevant if the
4966 // application wants to treat the case when it is started manually
4967 // differently from the case where it is started via automation (and
4968 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4970 {
4971#ifdef FEAT_OLE
4972 *argc = 1;
4973#else
4974 ole_error("embedding");
4975 mch_exit(2);
4976#endif
4977 }
4978 }
4979
4980#ifdef FEAT_OLE
4981 {
4982 int bDoRestart = FALSE;
4983
4984 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004985 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (bDoRestart)
4987 mch_exit(0);
4988 }
4989#endif
4990
4991#ifdef FEAT_NETBEANS_INTG
4992 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004993 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 int arg;
4995
4996 for (arg = 1; arg < *argc; arg++)
4997 if (strncmp("-nb", argv[arg], 3) == 0)
4998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 netbeansArg = argv[arg];
5000 mch_memmove(&argv[arg], &argv[arg + 1],
5001 (--*argc - arg) * sizeof(char *));
5002 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005003 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007}
5008
K.Takatac81e9bf2022-01-16 14:15:49 +00005009 static void
5010load_dpi_func(void)
5011{
5012 HMODULE hUser32;
5013
5014 hUser32 = GetModuleHandle("user32.dll");
5015 if (hUser32 == NULL)
5016 goto fail;
5017
5018 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5019 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5020 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5021 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5022 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5023 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5024
5025 if (pSetThreadDpiAwarenessContext != NULL)
5026 {
5027 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5028 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5029 if (oldctx != NULL)
5030 {
5031 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5032 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5033#ifdef DEBUG
5034 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5035 {
5036 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5037 }
5038#endif
5039 return;
5040 }
5041 }
5042
5043fail:
5044 // Disable PerMonitorV2 APIs.
5045 pGetDpiForSystem = stubGetDpiForSystem;
5046 pGetDpiForWindow = NULL;
5047 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5048 pSetThreadDpiAwarenessContext = NULL;
5049 pGetAwarenessFromDpiAwarenessContext = NULL;
5050}
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052/*
5053 * Initialise the GUI. Create all the windows, set up all the call-backs
5054 * etc.
5055 */
5056 int
5057gui_mch_init(void)
5058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005060 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
Bram Moolenaar734a8672019-12-02 22:49:38 +01005063 // Return here if the window was already opened (happens when
5064 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005066 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
5069 * Load the tearoff bitmap
5070 */
5071#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005072 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073#endif
5074
K.Takatac81e9bf2022-01-16 14:15:49 +00005075 load_dpi_func();
5076
5077 s_dpi = pGetDpiForSystem();
5078 update_scrollbar_size();
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005081 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#endif
5083 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005084#ifdef FEAT_TOOLBAR
5085 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5089
Bram Moolenaar734a8672019-12-02 22:49:38 +01005090 // First try using the wide version, so that we can use any title.
5091 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005092 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005094 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 wndclassw.lpfnWndProc = _WndProc;
5096 wndclassw.cbClsExtra = 0;
5097 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005098 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5100 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5101 wndclassw.hbrBackground = s_brush;
5102 wndclassw.lpszMenuName = NULL;
5103 wndclassw.lpszClassName = szVimWndClassW;
5104
K.Takata4ac893f2022-01-20 12:44:28 +00005105 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (vim_parent_hwnd != NULL)
5110 {
5111#ifdef HAVE_TRY_EXCEPT
5112 __try
5113 {
5114#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005115 // Open inside the specified parent window.
5116 // TODO: last argument should point to a CLIENTCREATESTRUCT
5117 // structure.
5118 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005120 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005121 WS_OVERLAPPEDWINDOW | WS_CHILD
5122 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5124 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005125 100, // Any value will do
5126 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005128 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#ifdef HAVE_TRY_EXCEPT
5130 }
5131 __except(EXCEPTION_EXECUTE_HANDLER)
5132 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005133 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135#endif
5136 if (s_hwnd == NULL)
5137 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005138 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 mch_exit(2);
5140 }
5141 }
5142 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005143 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005144 // If the provided windowid is not valid reset it to zero, so that it
5145 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005146 if (IsWindow((HWND)win_socket_id) <= 0)
5147 win_socket_id = 0;
5148
Bram Moolenaar734a8672019-12-02 22:49:38 +01005149 // Create a window. If win_socket_id is not zero without border and
5150 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005151 s_hwnd = CreateWindowW(
5152 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005153 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5154 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005155 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5156 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005157 100, // Any value will do
5158 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005159 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005160 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005161 if (s_hwnd != NULL && win_socket_id != 0)
5162 {
5163 SetParent(s_hwnd, (HWND)win_socket_id);
5164 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5165 }
5166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
5168 if (s_hwnd == NULL)
5169 return FAIL;
5170
K.Takatac81e9bf2022-01-16 14:15:49 +00005171 if (pGetDpiForWindow != NULL)
5172 {
5173 s_dpi = pGetDpiForWindow(s_hwnd);
5174 update_scrollbar_size();
5175 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5176 }
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5179 dyn_imm_load();
5180#endif
5181
Bram Moolenaar734a8672019-12-02 22:49:38 +01005182 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005183 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005184 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005185 wndclassw.style = CS_OWNDC;
5186 wndclassw.lpfnWndProc = _TextAreaWndProc;
5187 wndclassw.cbClsExtra = 0;
5188 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005189 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005190 wndclassw.hIcon = NULL;
5191 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5192 wndclassw.hbrBackground = NULL;
5193 wndclassw.lpszMenuName = NULL;
5194 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005195
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005196 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 return FAIL;
5198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005200 s_textArea = CreateWindowExW(
5201 0,
5202 szTextAreaClassW, L"Vim text area",
5203 WS_CHILD | WS_VISIBLE, 0, 0,
5204 100, // Any value will do for now
5205 100, // Any value will do for now
5206 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005207 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (s_textArea == NULL)
5210 return FAIL;
5211
Bram Moolenaar20321902016-02-17 12:30:17 +01005212#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005213 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005214 {
5215 HANDLE hIcon = NULL;
5216
5217 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005218 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005219 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005220#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222#ifdef FEAT_MENU
5223 s_menuBar = CreateMenu();
5224#endif
5225 s_hdc = GetDC(s_textArea);
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
Bram Moolenaar734a8672019-12-02 22:49:38 +01005229 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005230 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
Bram Moolenaar734a8672019-12-02 22:49:38 +01005232 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 gui_mch_def_colors();
5234
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5236 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 set_normal_colors();
5238
5239 /*
5240 * Check that none of the colors are the same as the background color.
5241 * Then store the current values as the defaults.
5242 */
5243 gui_check_colors();
5244 gui.def_norm_pixel = gui.norm_pixel;
5245 gui.def_back_pixel = gui.back_pixel;
5246
Bram Moolenaar734a8672019-12-02 22:49:38 +01005247 // Get the colors for the highlight groups (gui_check_colors() might have
5248 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 highlight_gui_started();
5250
5251 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005252 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005254 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256 /*
5257 * Set up for Intellimouse processing
5258 */
5259 init_mouse_wheel();
5260
5261 /*
5262 * compute a couple of metrics used for the dialogs
5263 */
5264 get_dialog_font_metrics();
5265#ifdef FEAT_TOOLBAR
5266 /*
5267 * Create the toolbar
5268 */
5269 initialise_toolbar();
5270#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005271#ifdef FEAT_GUI_TABLINE
5272 /*
5273 * Create the tabline
5274 */
5275 initialise_tabline();
5276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#ifdef MSWIN_FIND_REPLACE
5278 /*
5279 * Initialise the dialog box stuff
5280 */
5281 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5282
Bram Moolenaar734a8672019-12-02 22:49:38 +01005283 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005285 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005287 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5289 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5290 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005293#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005294 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005295 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005296#endif
5297
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005298#ifdef FEAT_RENDER_OPTIONS
5299 if (p_rop)
5300 (void)gui_mch_set_rendering_options(p_rop);
5301#endif
5302
Bram Moolenaar748bf032005-02-02 23:04:36 +00005303theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005304 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005305 display_errors();
5306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 return OK;
5308}
5309
5310/*
5311 * Get the size of the screen, taking position on multiple monitors into
5312 * account (if supported).
5313 */
5314 static void
5315get_work_area(RECT *spi_rect)
5316{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005317 HMONITOR mon;
5318 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar734a8672019-12-02 22:49:38 +01005320 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005321 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005322 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005324 moninfo.cbSize = sizeof(MONITORINFO);
5325 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005327 *spi_rect = moninfo.rcWork;
5328 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005331 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5333}
5334
5335/*
5336 * Set the size of the window to the given width and height in pixels.
5337 */
5338 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005339gui_mch_set_shellsize(
5340 int width,
5341 int height,
5342 int min_width UNUSED,
5343 int min_height UNUSED,
5344 int base_width UNUSED,
5345 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005346 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005349 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaar734a8672019-12-02 22:49:38 +01005352 // Try to keep window completely on screen.
5353 // Get position of the screen work area. This is the part that is not
5354 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 get_work_area(&workarea_rect);
5356
Bram Moolenaar734a8672019-12-02 22:49:38 +01005357 // Resizing a maximized window looks very strange, unzoom it first.
5358 // But don't do it when still starting up, it may have been requested in
5359 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005360 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005362
5363 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaar734a8672019-12-02 22:49:38 +01005365 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005366 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5367 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5368 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5369 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5370 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5371 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar734a8672019-12-02 22:49:38 +01005373 // The following should take care of keeping Vim on the same monitor, no
5374 // matter if the secondary monitor is left or right of the primary
5375 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005376 window_rect.right = window_rect.left + win_width;
5377 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005378
Bram Moolenaar734a8672019-12-02 22:49:38 +01005379 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005380 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5381 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005383 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5384 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005386 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5387 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005389 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5390 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005392 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5393 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395 SetActiveWindow(s_hwnd);
5396 SetFocus(s_hwnd);
5397
Bram Moolenaar734a8672019-12-02 22:49:38 +01005398 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400}
5401
5402
5403 void
5404gui_mch_set_scrollbar_thumb(
5405 scrollbar_T *sb,
5406 long val,
5407 long size,
5408 long max)
5409{
5410 SCROLLINFO info;
5411
5412 sb->scroll_shift = 0;
5413 while (max > 32767)
5414 {
5415 max = (max + 1) >> 1;
5416 val >>= 1;
5417 size >>= 1;
5418 ++sb->scroll_shift;
5419 }
5420
5421 if (sb->scroll_shift > 0)
5422 ++size;
5423
5424 info.cbSize = sizeof(info);
5425 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5426 info.nPos = val;
5427 info.nMin = 0;
5428 info.nMax = max;
5429 info.nPage = size;
5430 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5431}
5432
5433
5434/*
5435 * Set the current text font.
5436 */
5437 void
5438gui_mch_set_font(GuiFont font)
5439{
5440 gui.currFont = font;
5441}
5442
5443
5444/*
5445 * Set the current text foreground color.
5446 */
5447 void
5448gui_mch_set_fg_color(guicolor_T color)
5449{
5450 gui.currFgColor = color;
5451}
5452
5453/*
5454 * Set the current text background color.
5455 */
5456 void
5457gui_mch_set_bg_color(guicolor_T color)
5458{
5459 gui.currBgColor = color;
5460}
5461
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005462/*
5463 * Set the current text special color.
5464 */
5465 void
5466gui_mch_set_sp_color(guicolor_T color)
5467{
5468 gui.currSpColor = color;
5469}
5470
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005471#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472/*
5473 * Multi-byte handling, originally by Sung-Hoon Baek.
5474 * First static functions (no prototypes generated).
5475 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005476# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005477# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005478# endif
5479# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * handle WM_IME_NOTIFY message
5483 */
5484 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005485_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 LRESULT lResult = 0;
5488 HIMC hImc;
5489
5490 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5491 return lResult;
5492 switch (dwCommand)
5493 {
5494 case IMN_SETOPENSTATUS:
5495 if (pImmGetOpenStatus(hImc))
5496 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005497 LOGFONTW lf = norm_logfont;
5498 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5499 // Work around when PerMonitorV2 is not enabled in the process level.
5500 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5501 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 im_set_position(gui.row, gui.col);
5503
Bram Moolenaar734a8672019-12-02 22:49:38 +01005504 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 State &= ~LANGMAP;
5506 if (State & INSERT)
5507 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005508# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005509 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5511 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005512 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 int old_row = gui.row;
5514 int old_col = gui.col;
5515
5516 // This must be called here before
5517 // status_redraw_curbuf(), otherwise the mode
5518 // message may appear in the wrong position.
5519 showmode();
5520 status_redraw_curbuf();
5521 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005522 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 gui.row = old_row;
5524 gui.col = old_col;
5525 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 }
5528 }
5529 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005530 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 lResult = 0;
5532 break;
5533 }
5534 pImmReleaseContext(hWnd, hImc);
5535 return lResult;
5536}
5537
5538 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005539_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 char_u *ret;
5542 int len;
5543
Bram Moolenaar734a8672019-12-02 22:49:38 +01005544 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 return 0;
5546
5547 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5548 if (ret != NULL)
5549 {
5550 add_to_input_buf_csi(ret, len);
5551 vim_free(ret);
5552 return 1;
5553 }
5554 return 0;
5555}
5556
5557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 * void GetResultStr()
5559 *
5560 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5561 * get complete composition string
5562 */
5563 static char_u *
5564GetResultStr(HWND hwnd, int GCS, int *lenp)
5565{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005566 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005567 LONG ret;
5568 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 char_u *convbuf = NULL;
5570
5571 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5572 return NULL;
5573
K.Takatab0b2b732022-01-19 12:59:21 +00005574 // Get the length of the composition string.
5575 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5576 if (ret <= 0)
5577 return NULL;
5578
5579 // Allocate the requested buffer plus space for the NUL character.
5580 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (buf == NULL)
5582 return NULL;
5583
K.Takatab0b2b732022-01-19 12:59:21 +00005584 // Reads in the composition string.
5585 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5586 *lenp = ret / sizeof(WCHAR);
5587
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005588 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 pImmReleaseContext(hwnd, hIMC);
5590 vim_free(buf);
5591 return convbuf;
5592}
5593#endif
5594
Bram Moolenaar734a8672019-12-02 22:49:38 +01005595// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005596#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598/*
5599 * set font to IM.
5600 */
5601 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005602im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 HIMC hImc;
5605
5606 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5607 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005608 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 pImmReleaseContext(s_hwnd, hImc);
5610 }
5611}
5612
5613/*
5614 * Notify cursor position to IM.
5615 */
5616 void
5617im_set_position(int row, int col)
5618{
5619 HIMC hImc;
5620
5621 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5622 {
5623 COMPOSITIONFORM cfs;
5624
5625 cfs.dwStyle = CFS_POINT;
5626 cfs.ptCurrentPos.x = FILL_X(col);
5627 cfs.ptCurrentPos.y = FILL_Y(row);
5628 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005629 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5630 {
5631 // Work around when PerMonitorV2 is not enabled in the process level.
5632 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5633 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 pImmSetCompositionWindow(hImc, &cfs);
5636
5637 pImmReleaseContext(s_hwnd, hImc);
5638 }
5639}
5640
5641/*
5642 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5643 */
5644 void
5645im_set_active(int active)
5646{
5647 HIMC hImc;
5648 static HIMC hImcOld = (HIMC)0;
5649
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005650# ifdef VIMDLL
5651 if (!gui.in_use && !gui.starting)
5652 {
5653 mbyte_im_set_active(active);
5654 return;
5655 }
5656# endif
5657
Bram Moolenaar734a8672019-12-02 22:49:38 +01005658 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
5660 if (p_imdisable)
5661 {
5662 if (hImcOld == (HIMC)0)
5663 {
5664 hImcOld = pImmGetContext(s_hwnd);
5665 if (hImcOld)
5666 pImmAssociateContext(s_hwnd, (HIMC)0);
5667 }
5668 active = FALSE;
5669 }
5670 else if (hImcOld != (HIMC)0)
5671 {
5672 pImmAssociateContext(s_hwnd, hImcOld);
5673 hImcOld = (HIMC)0;
5674 }
5675
5676 hImc = pImmGetContext(s_hwnd);
5677 if (hImc)
5678 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005679 /*
5680 * for Korean ime
5681 */
5682 HKL hKL = GetKeyboardLayout(0);
5683
5684 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5685 {
5686 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5687 static BOOL bSaved = FALSE;
5688
5689 if (active)
5690 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005691 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005692 if (bSaved)
5693 pImmSetConversionStatus(hImc, dwConversionSaved,
5694 dwSentenceSaved);
5695 bSaved = FALSE;
5696 }
5697 else
5698 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005699 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005700 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5701 &dwSentenceSaved))
5702 {
5703 bSaved = TRUE;
5704 pImmSetConversionStatus(hImc,
5705 dwConversionSaved & ~(IME_CMODE_NATIVE
5706 | IME_CMODE_FULLSHAPE),
5707 dwSentenceSaved);
5708 }
5709 }
5710 }
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 pImmSetOpenStatus(hImc, active);
5713 pImmReleaseContext(s_hwnd, hImc);
5714 }
5715 }
5716}
5717
5718/*
5719 * Get IM status. When IM is on, return not 0. Else return 0.
5720 */
5721 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005722im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 int status = 0;
5725 HIMC hImc;
5726
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005727# ifdef VIMDLL
5728 if (!gui.in_use && !gui.starting)
5729 return mbyte_im_get_status();
5730# endif
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5733 {
5734 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5735 pImmReleaseContext(s_hwnd, hImc);
5736 }
5737 return status;
5738}
5739
Bram Moolenaar734a8672019-12-02 22:49:38 +01005740#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005743/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005744 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005745 */
5746 static void
5747latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5748{
5749 int c;
5750
Bram Moolenaarca003e12006-03-17 23:19:38 +00005751 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005752 {
5753 c = *text++;
5754 switch (c)
5755 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005756 case 0xa4: c = 0x20ac; break; // euro
5757 case 0xa6: c = 0x0160; break; // S hat
5758 case 0xa8: c = 0x0161; break; // S -hat
5759 case 0xb4: c = 0x017d; break; // Z hat
5760 case 0xb8: c = 0x017e; break; // Z -hat
5761 case 0xbc: c = 0x0152; break; // OE
5762 case 0xbd: c = 0x0153; break; // oe
5763 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005764 }
5765 *unicodebuf++ = c;
5766 }
5767}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769#ifdef FEAT_RIGHTLEFT
5770/*
5771 * What is this for? In the case where you are using Win98 or Win2K or later,
5772 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5773 * reverses the string sent to the TextOut... family. This sucks, because we
5774 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5775 * way to tell Windblows not to do this!
5776 *
5777 * The short of it is that this 'RevOut' only gets called if you are running
5778 * one of the new, "improved" MS OSes, and only if you are running in
5779 * 'rightleft' mode. It makes display take *slightly* longer, but not
5780 * noticeably so.
5781 */
5782 static void
5783RevOut( HDC s_hdc,
5784 int col,
5785 int row,
5786 UINT foptions,
5787 CONST RECT *pcliprect,
5788 LPCTSTR text,
5789 UINT len,
5790 CONST INT *padding)
5791{
5792 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005794 for (ix = 0; ix < (int)len; ++ix)
5795 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5796 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797}
5798#endif
5799
Bram Moolenaar92467d32017-12-05 13:22:16 +01005800 static void
5801draw_line(
5802 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005803 int y1,
5804 int x2,
5805 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005806 COLORREF color)
5807{
5808#if defined(FEAT_DIRECTX)
5809 if (IS_ENABLE_DIRECTX())
5810 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5811 else
5812#endif
5813 {
5814 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5815 HPEN old_pen = SelectObject(s_hdc, hpen);
5816 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005817 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005818 LineTo(s_hdc, x2, y2);
5819 DeleteObject(SelectObject(s_hdc, old_pen));
5820 }
5821}
5822
5823 static void
5824set_pixel(
5825 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005826 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005827 COLORREF color)
5828{
5829#if defined(FEAT_DIRECTX)
5830 if (IS_ENABLE_DIRECTX())
5831 DWriteContext_SetPixel(s_dwc, x, y, color);
5832 else
5833#endif
5834 SetPixel(s_hdc, x, y, color);
5835}
5836
5837 static void
5838fill_rect(
5839 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005840 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005841 COLORREF color)
5842{
5843#if defined(FEAT_DIRECTX)
5844 if (IS_ENABLE_DIRECTX())
5845 DWriteContext_FillRect(s_dwc, rcp, color);
5846 else
5847#endif
5848 {
5849 HBRUSH hbr2;
5850
5851 if (hbr == NULL)
5852 hbr2 = CreateSolidBrush(color);
5853 else
5854 hbr2 = hbr;
5855 FillRect(s_hdc, rcp, hbr2);
5856 if (hbr == NULL)
5857 DeleteBrush(hbr2);
5858 }
5859}
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 void
5862gui_mch_draw_string(
5863 int row,
5864 int col,
5865 char_u *text,
5866 int len,
5867 int flags)
5868{
5869 static int *padding = NULL;
5870 static int pad_size = 0;
5871 int i;
5872 const RECT *pcliprect = NULL;
5873 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 static WCHAR *unicodebuf = NULL;
5875 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005876 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 int y;
5879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 /*
5881 * Italic and bold text seems to have an extra row of pixels at the bottom
5882 * (below where the bottom of the character should be). If we draw the
5883 * characters with a solid background, the top row of pixels in the
5884 * character below will be overwritten. We can fix this by filling in the
5885 * background ourselves, to the correct character proportions, and then
5886 * writing the character in transparent mode. Still have a problem when
5887 * the character is "_", which gets written on to the character below.
5888 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5889 * pixel in their slots, which fixes the problem with the bottom row of
5890 * pixels. We still need this code because otherwise the top row of pixels
5891 * becomes a problem. - webb.
5892 */
5893 static HBRUSH hbr_cache[2] = {NULL, NULL};
5894 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5895 static int brush_lru = 0;
5896 HBRUSH hbr;
5897 RECT rc;
5898
5899 if (!(flags & DRAW_TRANSP))
5900 {
5901 /*
5902 * Clear background first.
5903 * Note: FillRect() excludes right and bottom of rectangle.
5904 */
5905 rc.left = FILL_X(col);
5906 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 if (has_mbyte)
5908 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005909 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005910 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
5912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 rc.right = FILL_X(col + len);
5914 rc.bottom = FILL_Y(row + 1);
5915
Bram Moolenaar734a8672019-12-02 22:49:38 +01005916 // Cache the created brush, that saves a lot of time. We need two:
5917 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 if (gui.currBgColor == brush_color[0])
5919 {
5920 hbr = hbr_cache[0];
5921 brush_lru = 1;
5922 }
5923 else if (gui.currBgColor == brush_color[1])
5924 {
5925 hbr = hbr_cache[1];
5926 brush_lru = 0;
5927 }
5928 else
5929 {
5930 if (hbr_cache[brush_lru] != NULL)
5931 DeleteBrush(hbr_cache[brush_lru]);
5932 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5933 brush_color[brush_lru] = gui.currBgColor;
5934 hbr = hbr_cache[brush_lru];
5935 brush_lru = !brush_lru;
5936 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005937
Bram Moolenaar92467d32017-12-05 13:22:16 +01005938 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
5940 SetBkMode(s_hdc, TRANSPARENT);
5941
5942 /*
5943 * When drawing block cursor, prevent inverted character spilling
5944 * over character cell (can happen with bold/italic)
5945 */
5946 if (flags & DRAW_CURSOR)
5947 {
5948 pcliprect = &rc;
5949 foptions = ETO_CLIPPED;
5950 }
5951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 SetTextColor(s_hdc, gui.currFgColor);
5953 SelectFont(s_hdc, gui.currFont);
5954
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005955#ifdef FEAT_DIRECTX
5956 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005957 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005958#endif
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5961 {
5962 vim_free(padding);
5963 pad_size = Columns;
5964
Bram Moolenaar734a8672019-12-02 22:49:38 +01005965 // Don't give an out-of-memory message here, it would call us
5966 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02005967 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (padding != NULL)
5969 for (i = 0; i < pad_size; i++)
5970 padding[i] = gui.char_width;
5971 }
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 /*
5974 * We have to provide the padding argument because italic and bold versions
5975 * of fixed-width fonts are often one pixel or so wider than their normal
5976 * versions.
5977 * No check for DRAW_BOLD, Windows will have done it already.
5978 */
5979
Bram Moolenaar734a8672019-12-02 22:49:38 +01005980 // Check if there are any UTF-8 characters. If not, use normal text
5981 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 if (enc_utf8)
5983 for (n = 0; n < len; ++n)
5984 if (text[n] >= 0x80)
5985 break;
5986
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005987#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005988 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5989 // required that unicode drawing routine, currently. So this forces it
5990 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005991 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01005992 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005993#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005994
Bram Moolenaar734a8672019-12-02 22:49:38 +01005995 // Check if the Unicode buffer exists and is big enough. Create it
5996 // with the same length as the multi-byte string, the number of wide
5997 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005998 if ((enc_utf8
5999 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6000 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 && (unicodebuf == NULL || len > unibuflen))
6002 {
6003 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006004 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006007 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
6009 unibuflen = len;
6010 }
6011
6012 if (enc_utf8 && n < len && unicodebuf != NULL)
6013 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006014 // Output UTF-8 characters. Composing characters should be
6015 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006016 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006017 int wlen; // string length in words
6018 int clen; // string length in characters
6019 int cells; // cell width of string up to composing char
6020 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006021 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006023 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006024 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006026 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006028 c = utf_ptr2char(text + i);
6029 if (c >= 0x10000)
6030 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006031 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006032 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6033 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034 }
6035 else
6036 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006037 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006039
6040 if (utf_iscomposing(c))
6041 cw = 0;
6042 else
6043 {
6044 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006045 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006046 cw = 1;
6047 }
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 if (unicodepdy != NULL)
6050 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006051 // Use unicodepdy to make characters fit as we expect, even
6052 // when the font uses different widths (e.g., bold character
6053 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006054 if (c >= 0x10000)
6055 {
6056 unicodepdy[wlen - 2] = cw * gui.char_width;
6057 unicodepdy[wlen - 1] = 0;
6058 }
6059 else
6060 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 }
6062 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006063 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006064 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006066#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006067 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006068 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006069 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006070 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006071 TEXT_X(col), TEXT_Y(row),
6072 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006073 gui.char_width, gui.currFgColor,
6074 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006075 }
6076 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006077#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006078 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6079 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006080 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006082 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006084 // If we want to display codepage data, and the current CP is not the
6085 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (unicodebuf != NULL)
6087 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006088 if (enc_latin9)
6089 latin9_to_ucs(text, len, unicodebuf);
6090 else
6091 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 MB_PRECOMPOSED,
6093 (char *)text, len,
6094 (LPWSTR)unicodebuf, unibuflen);
6095 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006096 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006097 // Use unicodepdy to make characters fit as we expect, even
6098 // when the font uses different widths (e.g., bold character
6099 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006100 if (unicodepdy != NULL)
6101 {
6102 int i;
6103 int cw;
6104
6105 for (i = 0; i < len; ++i)
6106 {
6107 cw = utf_char2cells(unicodebuf[i]);
6108 if (cw > 2)
6109 cw = 1;
6110 unicodepdy[i] = cw * gui.char_width;
6111 }
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006114 foptions, pcliprect, unicodebuf, len, unicodepdy);
6115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117 }
6118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006121 // Windows will mess up RL text, so we have to draw it character by
6122 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006123 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6125 foptions, pcliprect, (char *)text, len, padding);
6126 else
6127#endif
6128 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6129 foptions, pcliprect, (char *)text, len, padding);
6130 }
6131
Bram Moolenaar734a8672019-12-02 22:49:38 +01006132 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (flags & DRAW_UNDERL)
6134 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006135 // When p_linespace is 0, overwrite the bottom row of pixels.
6136 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (p_linespace > 1)
6139 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006140 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006142
Bram Moolenaar734a8672019-12-02 22:49:38 +01006143 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006144 if (flags & DRAW_STRIKE)
6145 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006146 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006147 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006148 }
6149
Bram Moolenaar734a8672019-12-02 22:49:38 +01006150 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006151 if (flags & DRAW_UNDERC)
6152 {
6153 int x;
6154 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006155 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006156
6157 y = FILL_Y(row + 1) - 1;
6158 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6159 {
6160 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006161 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006162 }
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164}
6165
6166
6167/*
6168 * Output routines.
6169 */
6170
Bram Moolenaar734a8672019-12-02 22:49:38 +01006171/*
6172 * Flush any output to the screen
6173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 void
6175gui_mch_flush(void)
6176{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006177#if defined(FEAT_DIRECTX)
6178 if (IS_ENABLE_DIRECTX())
6179 DWriteContext_Flush(s_dwc);
6180#endif
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 GdiFlush();
6183}
6184
6185 static void
6186clear_rect(RECT *rcp)
6187{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006188 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189}
6190
6191
Bram Moolenaarc716c302006-01-21 22:12:51 +00006192 void
6193gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6194{
6195 RECT workarea_rect;
6196
6197 get_work_area(&workarea_rect);
6198
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006199 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006200 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6201 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006202
Bram Moolenaar734a8672019-12-02 22:49:38 +01006203 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6204 // the menubar for MSwin, we subtract it from the screen height, so that
6205 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006206 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006207 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6208 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6209 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6210 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006211}
6212
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214#if defined(FEAT_MENU) || defined(PROTO)
6215/*
6216 * Add a sub menu to the menu bar.
6217 */
6218 void
6219gui_mch_add_menu(
6220 vimmenu_T *menu,
6221 int pos)
6222{
6223 vimmenu_T *parent = menu->parent;
6224
6225 menu->submenu_id = CreatePopupMenu();
6226 menu->id = s_menu_id++;
6227
6228 if (menu_is_menubar(menu->name))
6229 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006230 WCHAR *wn;
6231 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006233 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006234 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006235 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006237 infow.cbSize = sizeof(infow);
6238 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6239 | MIIM_SUBMENU;
6240 infow.dwItemData = (long_u)menu;
6241 infow.wID = menu->id;
6242 infow.fType = MFT_STRING;
6243 infow.dwTypeData = wn;
6244 infow.cch = (UINT)wcslen(wn);
6245 infow.hSubMenu = menu->submenu_id;
6246 InsertMenuItemW((parent == NULL)
6247 ? s_menuBar : parent->submenu_id,
6248 (UINT)pos, TRUE, &infow);
6249 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251
Bram Moolenaar734a8672019-12-02 22:49:38 +01006252 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (parent == NULL)
6254 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006255# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 else if (IsWindow(parent->tearoff_handle))
6257 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259}
6260
6261 void
6262gui_mch_show_popupmenu(vimmenu_T *menu)
6263{
6264 POINT mp;
6265
K.Takata45f9cfb2022-01-21 11:11:00 +00006266 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6268}
6269
6270 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006271gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 vimmenu_T *menu = gui_find_menu(path_name);
6274
6275 if (menu != NULL)
6276 {
6277 POINT p;
6278
Bram Moolenaar734a8672019-12-02 22:49:38 +01006279 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006281 if (mouse_pos)
6282 {
6283 int mx, my;
6284
6285 gui_mch_getmouse(&mx, &my);
6286 p.x += mx;
6287 p.y += my;
6288 }
6289 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006291 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6293 }
6294 msg_scroll = FALSE;
6295 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6296 }
6297}
6298
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006299# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300/*
6301 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6302 * create it as a pseudo-"tearoff menu".
6303 */
6304 void
6305gui_make_tearoff(char_u *path_name)
6306{
6307 vimmenu_T *menu = gui_find_menu(path_name);
6308
Bram Moolenaar734a8672019-12-02 22:49:38 +01006309 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (menu != NULL)
6311 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6312}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315/*
6316 * Add a menu item to a menu
6317 */
6318 void
6319gui_mch_add_menu_item(
6320 vimmenu_T *menu,
6321 int idx)
6322{
6323 vimmenu_T *parent = menu->parent;
6324
6325 menu->id = s_menu_id++;
6326 menu->submenu_id = NULL;
6327
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006328# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6330 {
6331 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6332 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6333 }
6334 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006335# endif
6336# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (menu_is_toolbar(parent->name))
6338 {
6339 TBBUTTON newtb;
6340
Bram Moolenaara80faa82020-04-12 19:37:17 +02006341 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (menu_is_separator(menu->name))
6343 {
6344 newtb.iBitmap = 0;
6345 newtb.fsStyle = TBSTYLE_SEP;
6346 }
6347 else
6348 {
6349 newtb.iBitmap = get_toolbar_bitmap(menu);
6350 newtb.fsStyle = TBSTYLE_BUTTON;
6351 }
6352 newtb.idCommand = menu->id;
6353 newtb.fsState = TBSTATE_ENABLED;
6354 newtb.iString = 0;
6355 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6356 (LPARAM)&newtb);
6357 menu->submenu_id = (HMENU)-1;
6358 }
6359 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006362 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006364 wn = enc_to_utf16(menu->name, NULL);
6365 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006367 InsertMenuW(parent->submenu_id, (UINT)idx,
6368 (menu_is_separator(menu->name)
6369 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6370 (UINT)menu->id, wn);
6371 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006373# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (IsWindow(parent->tearoff_handle))
6375 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 }
6378}
6379
6380/*
6381 * Destroy the machine specific menu widget.
6382 */
6383 void
6384gui_mch_destroy_menu(vimmenu_T *menu)
6385{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006386# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 /*
6388 * is this a toolbar button?
6389 */
6390 if (menu->submenu_id == (HMENU)-1)
6391 {
6392 int iButton;
6393
6394 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6395 (WPARAM)menu->id, 0);
6396 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6397 }
6398 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 if (menu->parent != NULL
6402 && menu_is_popup(menu->parent->dname)
6403 && menu->parent->submenu_id != NULL)
6404 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6405 else
6406 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6407 if (menu->submenu_id != NULL)
6408 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006409# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (IsWindow(menu->tearoff_handle))
6411 DestroyWindow(menu->tearoff_handle);
6412 if (menu->parent != NULL
6413 && menu->parent->children != NULL
6414 && IsWindow(menu->parent->tearoff_handle))
6415 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006416 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 menu->modes = 0;
6418 rebuild_tearoff(menu->parent);
6419 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422}
6423
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006424# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 static void
6426rebuild_tearoff(vimmenu_T *menu)
6427{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006428 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 char_u tbuf[128];
6430 RECT trect;
6431 RECT rct;
6432 RECT roct;
6433 int x, y;
6434
6435 HWND thwnd = menu->tearoff_handle;
6436
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006437 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (GetWindowRect(thwnd, &trect)
6439 && GetWindowRect(s_hwnd, &rct)
6440 && GetClientRect(s_hwnd, &roct))
6441 {
6442 x = trect.left - rct.left;
6443 y = (trect.top - rct.bottom + roct.bottom);
6444 }
6445 else
6446 {
6447 x = y = 0xffffL;
6448 }
6449 DestroyWindow(thwnd);
6450 if (menu->children != NULL)
6451 {
6452 gui_mch_tearoff(tbuf, menu, x, y);
6453 if (IsWindow(menu->tearoff_handle))
6454 (void) SetWindowPos(menu->tearoff_handle,
6455 NULL,
6456 (int)trect.left,
6457 (int)trect.top,
6458 0, 0,
6459 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6460 }
6461}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006462# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464/*
6465 * Make a menu either grey or not grey.
6466 */
6467 void
6468gui_mch_menu_grey(
6469 vimmenu_T *menu,
6470 int grey)
6471{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006472# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /*
6474 * is this a toolbar button?
6475 */
6476 if (menu->submenu_id == (HMENU)-1)
6477 {
6478 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006479 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006482# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006483 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6484 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006486# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6488 {
6489 WORD menuID;
6490 HWND menuHandle;
6491
6492 /*
6493 * A tearoff button has changed state.
6494 */
6495 if (menu->children == NULL)
6496 menuID = (WORD)(menu->id);
6497 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006498 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6500 if (menuHandle)
6501 EnableWindow(menuHandle, !grey);
6502
6503 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505}
6506
Bram Moolenaar734a8672019-12-02 22:49:38 +01006507#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509
Bram Moolenaar734a8672019-12-02 22:49:38 +01006510// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006513#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6516/*
6517 * stuff for dialogs
6518 */
6519
6520/*
6521 * The callback routine used by all the dialogs. Very simple. First,
6522 * acknowledges the INITDIALOG message so that Windows knows to do standard
6523 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6524 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6525 * number.
6526 */
6527 static LRESULT CALLBACK
6528dialog_callback(
6529 HWND hwnd,
6530 UINT message,
6531 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006532 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 if (message == WM_INITDIALOG)
6535 {
6536 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006537 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 (void)SetFocus(hwnd);
6539 if (dialog_default_button > IDCANCEL)
6540 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006541 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006542 // We don't have a default, set focus on another element of the
6543 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006544 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 return FALSE;
6546 }
6547
6548 if (message == WM_COMMAND)
6549 {
6550 int button = LOWORD(wParam);
6551
Bram Moolenaar734a8672019-12-02 22:49:38 +01006552 // Don't end the dialog if something was selected that was
6553 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (button >= DLG_NONBUTTON_CONTROL)
6555 return TRUE;
6556
Bram Moolenaar734a8672019-12-02 22:49:38 +01006557 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006559 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006560 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006561 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006562
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006563 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6564 p = utf16_to_enc(wp, NULL);
6565 vim_strncpy(s_textfield, p, IOSIZE);
6566 vim_free(p);
6567 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 /*
6571 * Need to check for IDOK because if the user just hits Return to
6572 * accept the default value, some reason this is what we get.
6573 */
6574 if (button == IDOK)
6575 {
6576 if (dialog_default_button > IDCANCEL)
6577 EndDialog(hwnd, dialog_default_button);
6578 }
6579 else
6580 EndDialog(hwnd, button - IDCANCEL);
6581 return TRUE;
6582 }
6583
6584 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6585 {
6586 EndDialog(hwnd, 0);
6587 return TRUE;
6588 }
6589 return FALSE;
6590}
6591
6592/*
6593 * Create a dialog dynamically from the parameter strings.
6594 * type = type of dialog (question, alert, etc.)
6595 * title = dialog title. may be NULL for default title.
6596 * message = text to display. Dialog sizes to accommodate it.
6597 * buttons = '\n' separated list of button captions, default first.
6598 * dfltbutton = number of default button.
6599 *
6600 * This routine returns 1 if the first button is pressed,
6601 * 2 for the second, etc.
6602 *
6603 * 0 indicates Esc was pressed.
6604 * -1 for unexpected error
6605 *
6606 * If stubbing out this fn, return 1.
6607 */
6608
Bram Moolenaar734a8672019-12-02 22:49:38 +01006609static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 "IDR_VIM",
6612 "IDR_VIM_ERROR",
6613 "IDR_VIM_ALERT",
6614 "IDR_VIM_INFO",
6615 "IDR_VIM_QUESTION"
6616};
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 int
6619gui_mch_dialog(
6620 int type,
6621 char_u *title,
6622 char_u *message,
6623 char_u *buttons,
6624 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006625 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006626 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006629 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int numButtons;
6631 int *buttonWidths, *buttonPositions;
6632 int buttonYpos;
6633 int nchar, i;
6634 DWORD lStyle;
6635 int dlgwidth = 0;
6636 int dlgheight;
6637 int editboxheight;
6638 int horizWidth = 0;
6639 int msgheight;
6640 char_u *pstart;
6641 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006642 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 char_u *tbuffer;
6644 RECT rect;
6645 HWND hwnd;
6646 HDC hdc;
6647 HFONT font, oldFont;
6648 TEXTMETRIC fontInfo;
6649 int fontHeight;
6650 int textWidth, minButtonWidth, messageWidth;
6651 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006652 int maxDialogHeight;
6653 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 int vertical;
6655 int dlgPaddingX;
6656 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006657# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006658 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006660# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006661 garray_T ga;
6662 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006663 int dlg_icon_width;
6664 int dlg_icon_height;
6665 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006667# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006668 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006669# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006670 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006671# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006672 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006673 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
Bram Moolenaar748bf032005-02-02 23:04:36 +00006676 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006677 {
6678 load_dpi_func();
6679 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006680 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006681 }
6682 else
6683 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685 if ((type < 0) || (type > VIM_LAST_TYPE))
6686 type = 0;
6687
Bram Moolenaar734a8672019-12-02 22:49:38 +01006688 // allocate some memory for dialog template
6689 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006690 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006691 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 if (p == NULL)
6694 return -1;
6695
6696 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006697 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6699 * const.
6700 */
6701 tbuffer = vim_strsave(buttons);
6702 if (tbuffer == NULL)
6703 return -1;
6704
Bram Moolenaar734a8672019-12-02 22:49:38 +01006705 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
Bram Moolenaar734a8672019-12-02 22:49:38 +01006707 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 numButtons = 1;
6709 for (i = 0; tbuffer[i] != '\0'; i++)
6710 {
6711 if (tbuffer[i] == DLG_BUTTON_SEP)
6712 numButtons++;
6713 }
6714 if (dfltbutton >= numButtons)
6715 dfltbutton = -1;
6716
Bram Moolenaar734a8672019-12-02 22:49:38 +01006717 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006718 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (buttonWidths == NULL)
6720 return -1;
6721
Bram Moolenaar734a8672019-12-02 22:49:38 +01006722 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006723 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (buttonPositions == NULL)
6725 return -1;
6726
6727 /*
6728 * Calculate how big the dialog must be.
6729 */
6730 hwnd = GetDesktopWindow();
6731 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006732# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6734 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006735 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 use_lfSysmenu = TRUE;
6737 }
6738 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006739# endif
K.Takatad1c58992022-01-23 12:31:57 +00006740 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6741 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6742
6743 oldFont = SelectFont(hdc, font);
6744 dlgPaddingX = DLG_PADDING_X;
6745 dlgPaddingY = DLG_PADDING_Y;
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 GetTextMetrics(hdc, &fontInfo);
6748 fontHeight = fontInfo.tmHeight;
6749
Bram Moolenaar734a8672019-12-02 22:49:38 +01006750 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006751 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar734a8672019-12-02 22:49:38 +01006753 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006754 if (s_hwnd == NULL)
6755 {
6756 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
Bram Moolenaar734a8672019-12-02 22:49:38 +01006758 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006759 get_work_area(&workarea_rect);
6760 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006761 if (maxDialogWidth > adjust_by_system_dpi(600))
6762 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006763 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006764 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006765 }
6766 else
6767 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006768 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006769 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006770 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006771 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6772 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6773 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6774 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006775
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006776 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006777 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6778 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6779 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6780 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6781 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006782 }
6783
Bram Moolenaar734a8672019-12-02 22:49:38 +01006784 // Set dlgwidth to width of message.
6785 // Copy the message into "ga", changing NL to CR-NL and inserting line
6786 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 pstart = message;
6788 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006789 msgheight = 0;
6790 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 do
6792 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006793 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006794
Bram Moolenaar734a8672019-12-02 22:49:38 +01006795 // Need to figure out where to break the string. The system does it
6796 // at a word boundary, which would mean we can't compute the number of
6797 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006798 textWidth = 0;
6799 last_white = NULL;
6800 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006801 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006802 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006803 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006804 && textWidth > maxDialogWidth * 3 / 4)
6805 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006806 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006807 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006809 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006810 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006811 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006812 textWidth = 0;
6813
6814 if (last_white != NULL)
6815 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006816 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006817 if (pend > last_white)
6818 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006819 pend = last_white + 1;
6820 last_white = NULL;
6821 }
6822 ga_append(&ga, '\r');
6823 ga_append(&ga, '\n');
6824 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006825 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006826
6827 while (--l >= 0)
6828 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006829 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006830 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006832
6833 ga_append(&ga, '\r');
6834 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 pstart = pend + 1;
6836 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006837
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006838 if (ga.ga_data != NULL)
6839 message = ga.ga_data;
6840
Bram Moolenaar734a8672019-12-02 22:49:38 +01006841 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006842
K.Takatac81e9bf2022-01-16 14:15:49 +00006843 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6844 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
K.Takatac81e9bf2022-01-16 14:15:49 +00006846 // Add width of icon to dlgwidth, and some space
6847 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6848 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6849
6850 if (msgheight < dlg_icon_height)
6851 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853 /*
6854 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006855 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006857 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if (!vertical)
6859 {
6860 // Place buttons horizontally if they fit.
6861 horizWidth = dlgPaddingX;
6862 pstart = tbuffer;
6863 i = 0;
6864 do
6865 {
6866 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6867 if (pend == NULL)
6868 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006869 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (textWidth < minButtonWidth)
6871 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006872 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 buttonWidths[i] = textWidth;
6874 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006875 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 pstart = pend + 1;
6877 } while (*pend != NUL);
6878
6879 if (horizWidth > maxDialogWidth)
6880 vertical = TRUE; // Too wide to fit on the screen.
6881 else if (horizWidth > dlgwidth)
6882 dlgwidth = horizWidth;
6883 }
6884
6885 if (vertical)
6886 {
6887 // Stack buttons vertically.
6888 pstart = tbuffer;
6889 do
6890 {
6891 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6892 if (pend == NULL)
6893 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006894 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006895 textWidth += dlgPaddingX; // Padding within button
6896 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 if (textWidth > dlgwidth)
6898 dlgwidth = textWidth;
6899 pstart = pend + 1;
6900 } while (*pend != NUL);
6901 }
6902
6903 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006904 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar734a8672019-12-02 22:49:38 +01006906 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006907 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 add_long(lStyle);
6910 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 add_word(0); // NumberOfItems(will change later)
6913 add_word(10); // x
6914 add_word(10); // y
6915 add_word(PixelToDialogX(dlgwidth)); // cx
6916
6917 // Dialog height.
6918 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006919 dlgheight = msgheight + 2 * dlgPaddingY
6920 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 else
6922 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6923
6924 // Dialog needs to be taller if contains an edit box.
6925 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6926 if (textfield != NULL)
6927 dlgheight += editboxheight;
6928
Bram Moolenaar734a8672019-12-02 22:49:38 +01006929 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006930 if (dlgheight > maxDialogHeight)
6931 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006932 msgheight = msgheight - (dlgheight - maxDialogHeight);
6933 dlgheight = maxDialogHeight;
6934 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006935 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00006936 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006937 }
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 add_word(PixelToDialogY(dlgheight));
6940
6941 add_word(0); // Menu
6942 add_word(0); // Class
6943
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006945 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6946 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 p += nchar;
6948
K.Takatad1c58992022-01-23 12:31:57 +00006949 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006950# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00006951 if (use_lfSysmenu)
6952 {
6953 // point size
6954 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6955 GetDeviceCaps(hdc, LOGPIXELSY));
6956 wcscpy(p, lfSysmenu.lfFaceName);
6957 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
K.Takatad1c58992022-01-23 12:31:57 +00006959 else
6960# endif
6961 {
6962 *p++ = DLG_FONT_POINT_SIZE; // point size
6963 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
6964 }
6965 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 buttonYpos = msgheight + 2 * dlgPaddingY;
6968
6969 if (textfield != NULL)
6970 buttonYpos += editboxheight;
6971
6972 pstart = tbuffer;
6973 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006974 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 for (i = 0; i < numButtons; i++)
6976 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006977 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 for ( pend = pstart;
6979 *pend && (*pend != DLG_BUTTON_SEP);
6980 pend++)
6981 ;
6982
6983 if (*pend)
6984 *pend = '\0';
6985
6986 /*
6987 * old NOTE:
6988 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6989 * the focus to the first tab-able button and in so doing makes that
6990 * the default!! Grrr. Workaround: Make the default button the only
6991 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6992 * he/she can use arrow keys.
6993 *
6994 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00006995 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 * dialog. Also needed for when the textfield is the default control.
6997 * It appears to work now (perhaps not on Win95?).
6998 */
6999 if (vertical)
7000 {
7001 p = add_dialog_element(p,
7002 (i == dfltbutton
7003 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7004 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007005 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 + 2 * fontHeight * i),
7007 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7008 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007009 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011 else
7012 {
7013 p = add_dialog_element(p,
7014 (i == dfltbutton
7015 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7016 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007017 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 PixelToDialogX(buttonWidths[i]),
7019 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007020 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007022 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024 *pnumitems += numButtons;
7025
Bram Moolenaar734a8672019-12-02 22:49:38 +01007026 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 p = add_dialog_element(p, SS_ICON,
7028 PixelToDialogX(dlgPaddingX),
7029 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007030 PixelToDialogX(dlg_icon_width),
7031 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7033 dlg_icons[type]);
7034
Bram Moolenaar734a8672019-12-02 22:49:38 +01007035 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007036 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007037 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007038 PixelToDialogY(dlgPaddingY),
7039 (WORD)(PixelToDialogX(messageWidth) + 1),
7040 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007041 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar734a8672019-12-02 22:49:38 +01007043 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (textfield != NULL)
7045 {
7046 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7047 PixelToDialogX(2 * dlgPaddingX),
7048 PixelToDialogY(2 * dlgPaddingY + msgheight),
7049 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7050 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007051 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 *pnumitems += 1;
7053 }
7054
7055 *pnumitems += 2;
7056
7057 SelectFont(hdc, oldFont);
7058 DeleteObject(font);
7059 ReleaseDC(hwnd, hdc);
7060
Bram Moolenaar734a8672019-12-02 22:49:38 +01007061 // Let the dialog_callback() function know which button to make default
7062 // If we have an edit box, make that the default. We also need to tell
7063 // dialog_callback() if this dialog contains an edit box or not. We do
7064 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (textfield != NULL)
7066 {
7067 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7068 s_textfield = textfield;
7069 }
7070 else
7071 {
7072 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7073 s_textfield = NULL;
7074 }
7075
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007078 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 (LPDLGTEMPLATE)pdlgtemplate,
7080 s_hwnd,
7081 (DLGPROC)dialog_callback);
7082
7083 LocalFree(LocalHandle(pdlgtemplate));
7084 vim_free(tbuffer);
7085 vim_free(buttonWidths);
7086 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007087 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
Bram Moolenaar734a8672019-12-02 22:49:38 +01007089 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 (void)SetFocus(s_hwnd);
7091
7092 return nchar;
7093}
7094
Bram Moolenaar734a8672019-12-02 22:49:38 +01007095#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097/*
7098 * Put a simple element (basic class) onto a dialog template in memory.
7099 * return a pointer to where the next item should be added.
7100 *
7101 * parameters:
7102 * lStyle = additional style flags
7103 * (be careful, NT3.51 & Win32s will ignore the new ones)
7104 * x,y = x & y positions IN DIALOG UNITS
7105 * w,h = width and height IN DIALOG UNITS
7106 * Id = ID used in messages
7107 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7108 * caption = usually text or resource name
7109 *
7110 * TODO: use the length information noted here to enable the dialog creation
7111 * routines to work out more exactly how much memory they need to alloc.
7112 */
7113 static PWORD
7114add_dialog_element(
7115 PWORD p,
7116 DWORD lStyle,
7117 WORD x,
7118 WORD y,
7119 WORD w,
7120 WORD h,
7121 WORD Id,
7122 WORD clss,
7123 const char *caption)
7124{
7125 int nchar;
7126
Bram Moolenaar734a8672019-12-02 22:49:38 +01007127 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7129 *p++ = LOWORD(lStyle);
7130 *p++ = HIWORD(lStyle);
7131 *p++ = 0; // LOWORD (lExtendedStyle)
7132 *p++ = 0; // HIWORD (lExtendedStyle)
7133 *p++ = x;
7134 *p++ = y;
7135 *p++ = w;
7136 *p++ = h;
7137 *p++ = Id; //9 or 10 words in all
7138
7139 *p++ = (WORD)0xffff;
7140 *p++ = clss; //2 more here
7141
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007142 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 p += nchar;
7144
7145 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7146
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007147 return p; // total = 15 + strlen(caption) words
7148 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
7151
7152/*
7153 * Helper routine. Take an input pointer, return closest pointer that is
7154 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7155 */
7156 static LPWORD
7157lpwAlign(
7158 LPWORD lpIn)
7159{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007160 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007162 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 ul += 3;
7164 ul >>= 2;
7165 ul <<= 2;
7166 return (LPWORD)ul;
7167}
7168
7169/*
7170 * Helper routine. Takes second parameter as Ansi string, copies it to first
7171 * parameter as wide character (16-bits / char) string, and returns integer
7172 * number of wide characters (words) in string (including the trailing wide
7173 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007174 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7175 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 static int
7177nCopyAnsiToWideChar(
7178 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007179 LPSTR lpAnsiIn,
7180 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007183 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 int i;
7185 WCHAR *wn;
7186
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007187 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007189 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007190 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (wn != NULL)
7192 {
7193 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007194 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 vim_free(wn);
7196 }
7197 }
7198 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007199 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 nChar = MultiByteToWideChar(
7201 enc_codepage > 0 ? enc_codepage : CP_ACP,
7202 MB_PRECOMPOSED,
7203 lpAnsiIn, len,
7204 lpWCStr, len);
7205 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007206 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
7209 return nChar;
7210}
7211
7212
7213#ifdef FEAT_TEAROFF
7214/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007215 * Lookup menu handle from "menu_id".
7216 */
7217 static HMENU
7218tearoff_lookup_menuhandle(
7219 vimmenu_T *menu,
7220 WORD menu_id)
7221{
7222 for ( ; menu != NULL; menu = menu->next)
7223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007224 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007225 continue;
7226 if (menu_is_separator(menu->dname))
7227 continue;
7228 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7229 return menu->submenu_id;
7230 }
7231 return NULL;
7232}
7233
7234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 * The callback function for all the modeless dialogs that make up the
7236 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7237 * thinking its menus have been clicked), and go away when closed.
7238 */
7239 static LRESULT CALLBACK
7240tearoff_callback(
7241 HWND hwnd,
7242 UINT message,
7243 WPARAM wParam,
7244 LPARAM lParam)
7245{
7246 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007247 {
7248 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar734a8672019-12-02 22:49:38 +01007252 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 HandleMouseHide(message, lParam);
7254
7255 if (message == WM_COMMAND)
7256 {
7257 if ((WORD)(LOWORD(wParam)) & 0x8000)
7258 {
7259 POINT mp;
7260 RECT rect;
7261
7262 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7263 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007264 vimmenu_T *menu;
7265
7266 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007268 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7270 (int)rect.right - 8,
7271 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007272 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 s_hwnd,
7274 NULL);
7275 /*
7276 * NOTE: The pop-up menu can eat the mouse up event.
7277 * We deal with this in normal.c.
7278 */
7279 }
7280 }
7281 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007282 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7284 /*
7285 * Give main window the focus back: this is so after
7286 * choosing a tearoff button you can start typing again
7287 * straight away.
7288 */
7289 (void)SetFocus(s_hwnd);
7290 return TRUE;
7291 }
7292 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7293 {
7294 DestroyWindow(hwnd);
7295 return TRUE;
7296 }
7297
Bram Moolenaar734a8672019-12-02 22:49:38 +01007298 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (message == WM_EXITSIZEMOVE)
7300 (void)SetActiveWindow(s_hwnd);
7301
7302 return FALSE;
7303}
7304#endif
7305
7306
7307/*
K.Takatad1c58992022-01-23 12:31:57 +00007308 * Computes the dialog base units based on the current dialog font.
7309 * We don't use the GetDialogBaseUnits() API, because we don't use the
7310 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 */
7312 static void
7313get_dialog_font_metrics(void)
7314{
7315 HDC hdc;
7316 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 SIZE size;
7318#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007319 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#endif
7321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007323 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007324 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326#endif
7327 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007328 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
K.Takatad1c58992022-01-23 12:31:57 +00007330 hdc = GetDC(s_hwnd);
7331 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007332 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007333 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
K.Takataabe628e2022-01-23 16:25:17 +00007335 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007336 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
7339#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7340/*
7341 * Create a pseudo-"tearoff menu" based on the child
7342 * items of a given menu pointer.
7343 */
7344 static void
7345gui_mch_tearoff(
7346 char_u *title,
7347 vimmenu_T *menu,
7348 int initX,
7349 int initY)
7350{
7351 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7352 int template_len;
7353 int nchar, textWidth, submenuWidth;
7354 DWORD lStyle;
7355 DWORD lExtendedStyle;
7356 WORD dlgwidth;
7357 WORD menuID;
7358 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007359 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 vimmenu_T *the_menu = menu;
7361 HWND hwnd;
7362 HDC hdc;
7363 HFONT font, oldFont;
7364 int col, spaceWidth, len;
7365 int columnWidths[2];
7366 char_u *label, *text;
7367 int acLen = 0;
7368 int nameLen;
7369 int padding0, padding1, padding2 = 0;
7370 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007371 int x;
7372 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007373# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007374 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378 /*
7379 * If this menu is already torn off, move it to the mouse position.
7380 */
7381 if (IsWindow(menu->tearoff_handle))
7382 {
7383 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007384 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7387 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7388 }
7389 return;
7390 }
7391
7392 /*
7393 * Create a new tearoff.
7394 */
7395 if (*title == MNU_HIDDEN_CHAR)
7396 title++;
7397
Bram Moolenaar734a8672019-12-02 22:49:38 +01007398 // Allocate memory to store the dialog template. It's made bigger when
7399 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 template_len = DLG_ALLOC_SIZE;
7401 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7402 if (p == NULL)
7403 return;
7404
7405 hwnd = GetDesktopWindow();
7406 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007407# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7409 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007410 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 use_lfSysmenu = TRUE;
7412 }
7413 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007414# endif
K.Takatad1c58992022-01-23 12:31:57 +00007415 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7416 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7417
7418 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar734a8672019-12-02 22:49:38 +01007420 // Calculate width of a single space. Used for padding columns to the
7421 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007422 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 // Figure out max width of the text column, the accelerator column and the
7425 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 submenuWidth = 0;
7427 for (col = 0; col < 2; col++)
7428 {
7429 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007430 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007432 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 text = (col == 0) ? pmenu->dname : pmenu->actext;
7434 if (text != NULL && *text != NUL)
7435 {
7436 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7437 if (textWidth > columnWidths[col])
7438 columnWidths[col] = textWidth;
7439 }
7440 if (pmenu->children != NULL)
7441 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7442 }
7443 }
7444 if (columnWidths[1] == 0)
7445 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007446 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 if (submenuWidth != 0)
7448 columnWidths[0] += submenuWidth;
7449 else
7450 columnWidths[0] += spaceWidth;
7451 }
7452 else
7453 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007454 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7456 columnWidths[1] += submenuWidth;
7457 }
7458
7459 /*
7460 * Now find the total width of our 'menu'.
7461 */
7462 textWidth = columnWidths[0] + columnWidths[1];
7463 if (submenuWidth != 0)
7464 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007465 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7467 textWidth += submenuWidth;
7468 }
7469 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7470 if (textWidth > dlgwidth)
7471 dlgwidth = textWidth;
7472 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7473
Bram Moolenaar734a8672019-12-02 22:49:38 +01007474 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007475 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
7477 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7478 *p++ = LOWORD(lStyle);
7479 *p++ = HIWORD(lStyle);
7480 *p++ = LOWORD(lExtendedStyle);
7481 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007482 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007484 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007486 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
7488 *p++ = PixelToDialogX(initX); // x
7489 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007490 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
7492 *p++ = PixelToDialogY(initY); // y
7493 *p++ = PixelToDialogX(dlgwidth); // cx
7494 ptrueheight = p;
7495 *p++ = 0; // dialog height: changed later anyway
7496 *p++ = 0; // Menu
7497 *p++ = 0; // Class
7498
Bram Moolenaar734a8672019-12-02 22:49:38 +01007499 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007501 ? (LPSTR)title
7502 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 p += nchar;
7504
K.Takatad1c58992022-01-23 12:31:57 +00007505 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007506# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007507 if (use_lfSysmenu)
7508 {
7509 // point size
7510 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7511 GetDeviceCaps(hdc, LOGPIXELSY));
7512 wcscpy(p, lfSysmenu.lfFaceName);
7513 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
K.Takatad1c58992022-01-23 12:31:57 +00007515 else
7516# endif
7517 {
7518 *p++ = DLG_FONT_POINT_SIZE; // point size
7519 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7520 }
7521 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522
7523 /*
7524 * Loop over all the items in the menu.
7525 * But skip over the tearbar.
7526 */
7527 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7528 menu = menu->children->next;
7529 else
7530 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007531 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 for ( ; menu != NULL; menu = menu->next)
7533 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007534 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 continue;
7536 if (menu_is_separator(menu->dname))
7537 {
7538 sepPadding += 3;
7539 continue;
7540 }
7541
Bram Moolenaar734a8672019-12-02 22:49:38 +01007542 // Check if there still is plenty of room in the template. Make it
7543 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7545 {
7546 WORD *newp;
7547
7548 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7549 if (newp != NULL)
7550 {
7551 template_len += 4096;
7552 mch_memmove(newp, pdlgtemplate,
7553 (char *)p - (char *)pdlgtemplate);
7554 p = newp + (p - pdlgtemplate);
7555 pnumitems = newp + (pnumitems - pdlgtemplate);
7556 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7557 LocalFree(LocalHandle(pdlgtemplate));
7558 pdlgtemplate = newp;
7559 }
7560 }
7561
Bram Moolenaar734a8672019-12-02 22:49:38 +01007562 // Figure out minimal length of this menu label. Use "name" for the
7563 // actual text, "dname" for estimating the displayed size. "name"
7564 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 len = nameLen = (int)STRLEN(menu->name);
7566 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7567 (int)STRLEN(menu->dname))) / spaceWidth;
7568 len += padding0;
7569
7570 if (menu->actext != NULL)
7571 {
7572 acLen = (int)STRLEN(menu->actext);
7573 len += acLen;
7574 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7575 }
7576 else
7577 textWidth = 0;
7578 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7579 len += padding1;
7580
7581 if (menu->children == NULL)
7582 {
7583 padding2 = submenuWidth / spaceWidth;
7584 len += padding2;
7585 menuID = (WORD)(menu->id);
7586 }
7587 else
7588 {
7589 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007590 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592
Bram Moolenaar734a8672019-12-02 22:49:38 +01007593 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007594 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (label == NULL)
7596 break;
7597
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007598 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007599 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007601 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 while (padding0-- > 0)
7603 *text++ = ' ';
7604 if (menu->actext != NULL)
7605 {
7606 STRNCPY(text, menu->actext, acLen);
7607 text += acLen;
7608 }
7609 while (padding1-- > 0)
7610 *text++ = ' ';
7611 if (menu->children != NULL)
7612 {
7613 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7614 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7615 }
7616 else
7617 {
7618 while (padding2-- > 0)
7619 *text++ = ' ';
7620 }
7621 *text = NUL;
7622
7623 /*
7624 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7625 * W95/NT4 it makes the tear-off look more like a menu.
7626 */
7627 p = add_dialog_element(p,
7628 BS_PUSHBUTTON|BS_LEFT,
7629 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7630 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7631 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7632 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007633 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 vim_free(label);
7635 (*pnumitems)++;
7636 }
7637
7638 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7639
7640
Bram Moolenaar734a8672019-12-02 22:49:38 +01007641 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007642 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007643 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 (LPDLGTEMPLATE)pdlgtemplate,
7645 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007646 (DLGPROC)tearoff_callback,
7647 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649 LocalFree(LocalHandle(pdlgtemplate));
7650 SelectFont(hdc, oldFont);
7651 DeleteObject(font);
7652 ReleaseDC(hwnd, hdc);
7653
7654 /*
7655 * Reassert ourselves as the active window. This is so that after creating
7656 * a tearoff, the user doesn't have to click with the mouse just to start
7657 * typing again!
7658 */
7659 (void)SetActiveWindow(s_hwnd);
7660
Bram Moolenaar734a8672019-12-02 22:49:38 +01007661 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 force_menu_update = TRUE;
7663}
7664#endif
7665
7666#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007667# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669/*
7670 * Create the toolbar, initially unpopulated.
7671 * (just like the menu, there are no defaults, it's all
7672 * set up through menu.vim)
7673 */
7674 static void
7675initialise_toolbar(void)
7676{
7677 InitCommonControls();
7678 s_toolbarhwnd = CreateToolbarEx(
7679 s_hwnd,
7680 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7681 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007682 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007683 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 IDR_TOOLBAR1, // id of initial bitmap
7685 NULL,
7686 0, // initial number of buttons
7687 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7688 TOOLBAR_BUTTON_HEIGHT,
7689 TOOLBAR_BUTTON_WIDTH,
7690 TOOLBAR_BUTTON_HEIGHT,
7691 sizeof(TBBUTTON)
7692 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007693
7694 // Remove transparency from the toolbar to prevent the main window
7695 // background colour showing through
7696 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7697 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7698
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007699 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700
7701 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007702
7703 update_toolbar_size();
7704}
7705
7706 static void
7707update_toolbar_size(void)
7708{
7709 int w, h;
7710 TBMETRICS tbm = {sizeof(TBMETRICS)};
7711
7712 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7713 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7714 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7715 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7716
7717 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7718 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7719 //TRACE("button size: %d, %d", w, h);
7720 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7721 gui.toolbar_height = h + 6;
7722
7723 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7724 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7725
7726 // TODO:
7727 // Currently, this function only updates the size of toolbar buttons.
7728 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007731 static LRESULT CALLBACK
7732toolbar_wndproc(
7733 HWND hwnd,
7734 UINT uMsg,
7735 WPARAM wParam,
7736 LPARAM lParam)
7737{
7738 HandleMouseHide(uMsg, lParam);
7739 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7740}
7741
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 static int
7743get_toolbar_bitmap(vimmenu_T *menu)
7744{
7745 int i = -1;
7746
7747 /*
7748 * Check user bitmaps first, unless builtin is specified.
7749 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007750 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 char_u fname[MAXPATHL];
7753 HANDLE hbitmap = NULL;
7754
7755 if (menu->iconfile != NULL)
7756 {
7757 gui_find_iconfile(menu->iconfile, fname, "bmp");
7758 hbitmap = LoadImage(
7759 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007760 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 IMAGE_BITMAP,
7762 TOOLBAR_BUTTON_WIDTH,
7763 TOOLBAR_BUTTON_HEIGHT,
7764 LR_LOADFROMFILE |
7765 LR_LOADMAP3DCOLORS
7766 );
7767 }
7768
7769 /*
7770 * If the LoadImage call failed, or the "icon=" file
7771 * didn't exist or wasn't specified, try the menu name
7772 */
7773 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007774 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007775# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007776 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007777# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007778 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 hbitmap = LoadImage(
7780 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007781 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 IMAGE_BITMAP,
7783 TOOLBAR_BUTTON_WIDTH,
7784 TOOLBAR_BUTTON_HEIGHT,
7785 LR_LOADFROMFILE |
7786 LR_LOADMAP3DCOLORS
7787 );
7788
7789 if (hbitmap != NULL)
7790 {
7791 TBADDBITMAP tbAddBitmap;
7792
7793 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007794 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
7796 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7797 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007798 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800 }
7801 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7802 i = menu->iconidx;
7803
7804 return i;
7805}
7806#endif
7807
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007808#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7809 static void
7810initialise_tabline(void)
7811{
7812 InitCommonControls();
7813
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007814 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007815 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007816 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007817 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007818 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007819
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007820 gui.tabline_height = TABLINE_HEIGHT;
7821
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007822 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007823}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007824
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007825/*
7826 * Get tabpage_T from POINT.
7827 */
7828 static tabpage_T *
7829GetTabFromPoint(
7830 HWND hWnd,
7831 POINT pt)
7832{
7833 tabpage_T *ptp = NULL;
7834
7835 if (gui_mch_showing_tabline())
7836 {
7837 TCHITTESTINFO htinfo;
7838 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007839 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007840 if (s_tabhwnd == hWnd)
7841 {
7842 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7843 if (idx != -1)
7844 ptp = find_tabpage(idx + 1);
7845 }
7846 }
7847 return ptp;
7848}
7849
7850static POINT s_pt = {0, 0};
7851static HCURSOR s_hCursor = NULL;
7852
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007853 static LRESULT CALLBACK
7854tabline_wndproc(
7855 HWND hwnd,
7856 UINT uMsg,
7857 WPARAM wParam,
7858 LPARAM lParam)
7859{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007860 POINT pt;
7861 tabpage_T *tp;
7862 RECT rect;
7863 int nCenter;
7864 int idx0;
7865 int idx1;
7866
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007867 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007868
7869 switch (uMsg)
7870 {
7871 case WM_LBUTTONDOWN:
7872 {
7873 s_pt.x = GET_X_LPARAM(lParam);
7874 s_pt.y = GET_Y_LPARAM(lParam);
7875 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007876 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007877 break;
7878 }
7879 case WM_MOUSEMOVE:
7880 if (GetCapture() == hwnd
7881 && ((wParam & MK_LBUTTON)) != 0)
7882 {
7883 pt.x = GET_X_LPARAM(lParam);
7884 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007885 if (abs(pt.x - s_pt.x) >
7886 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007887 {
7888 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7889
7890 tp = GetTabFromPoint(hwnd, pt);
7891 if (tp != NULL)
7892 {
7893 idx0 = tabpage_index(curtab) - 1;
7894 idx1 = tabpage_index(tp) - 1;
7895
7896 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7897 nCenter = rect.left + (rect.right - rect.left) / 2;
7898
Bram Moolenaar734a8672019-12-02 22:49:38 +01007899 // Check if the mouse cursor goes over the center of
7900 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007901 if ((idx0 < idx1) && (nCenter < pt.x))
7902 {
7903 tabpage_move(idx1 + 1);
7904 update_screen(0);
7905 }
7906 else if ((idx1 < idx0) && (pt.x < nCenter))
7907 {
7908 tabpage_move(idx1);
7909 update_screen(0);
7910 }
7911 }
7912 }
7913 }
7914 break;
7915 case WM_LBUTTONUP:
7916 {
7917 if (GetCapture() == hwnd)
7918 {
7919 SetCursor(s_hCursor);
7920 ReleaseCapture();
7921 }
7922 break;
7923 }
7924 default:
7925 break;
7926 }
7927
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007928 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7929}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007930#endif
7931
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7933/*
7934 * Make the GUI window come to the foreground.
7935 */
7936 void
7937gui_mch_set_foreground(void)
7938{
7939 if (IsIconic(s_hwnd))
7940 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7941 SetForegroundWindow(s_hwnd);
7942}
7943#endif
7944
7945#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7946 static void
7947dyn_imm_load(void)
7948{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007949 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (hLibImm == NULL)
7951 return;
7952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 pImmGetCompositionStringW
7954 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7955 pImmGetContext
7956 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7957 pImmAssociateContext
7958 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7959 pImmReleaseContext
7960 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7961 pImmGetOpenStatus
7962 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7963 pImmSetOpenStatus
7964 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007965 pImmGetCompositionFontW
7966 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7967 pImmSetCompositionFontW
7968 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 pImmSetCompositionWindow
7970 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7971 pImmGetConversionStatus
7972 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007973 pImmSetConversionStatus
7974 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
K.Takatab0b2b732022-01-19 12:59:21 +00007976 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 || pImmGetContext == NULL
7978 || pImmAssociateContext == NULL
7979 || pImmReleaseContext == NULL
7980 || pImmGetOpenStatus == NULL
7981 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007982 || pImmGetCompositionFontW == NULL
7983 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007985 || pImmGetConversionStatus == NULL
7986 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
7988 FreeLibrary(hLibImm);
7989 hLibImm = NULL;
7990 pImmGetContext = NULL;
7991 return;
7992 }
7993
7994 return;
7995}
7996
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997#endif
7998
7999#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8000
8001# ifdef FEAT_XPM_W32
8002# define IMAGE_XPM 100
8003# endif
8004
8005typedef struct _signicon_t
8006{
8007 HANDLE hImage;
8008 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008009# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008010 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012} signicon_t;
8013
8014 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008015gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 signicon_t *sign;
8018 int x, y, w, h;
8019
8020 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8021 return;
8022
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008023# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008024 if (IS_ENABLE_DIRECTX())
8025 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008026# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 x = TEXT_X(col);
8029 y = TEXT_Y(row);
8030 w = gui.char_width * 2;
8031 h = gui.char_height;
8032 switch (sign->uType)
8033 {
8034 case IMAGE_BITMAP:
8035 {
8036 HDC hdcMem;
8037 HBITMAP hbmpOld;
8038
8039 hdcMem = CreateCompatibleDC(s_hdc);
8040 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8041 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8042 SelectObject(hdcMem, hbmpOld);
8043 DeleteDC(hdcMem);
8044 }
8045 break;
8046 case IMAGE_ICON:
8047 case IMAGE_CURSOR:
8048 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8049 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008050# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 case IMAGE_XPM:
8052 {
8053 HDC hdcMem;
8054 HBITMAP hbmpOld;
8055
8056 hdcMem = CreateCompatibleDC(s_hdc);
8057 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008058 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8060
8061 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008062 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8064 SelectObject(hdcMem, hbmpOld);
8065 DeleteDC(hdcMem);
8066 }
8067 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 }
8070}
8071
8072 static void
8073close_signicon_image(signicon_t *sign)
8074{
8075 if (sign)
8076 switch (sign->uType)
8077 {
8078 case IMAGE_BITMAP:
8079 DeleteObject((HGDIOBJ)sign->hImage);
8080 break;
8081 case IMAGE_CURSOR:
8082 DestroyCursor((HCURSOR)sign->hImage);
8083 break;
8084 case IMAGE_ICON:
8085 DestroyIcon((HICON)sign->hImage);
8086 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008087# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 case IMAGE_XPM:
8089 DeleteObject((HBITMAP)sign->hImage);
8090 DeleteObject((HBITMAP)sign->hShape);
8091 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094}
8095
8096 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008097gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 signicon_t sign, *psign;
8100 char_u *ext;
8101
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008103 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 if (ext > signfile)
8105 {
8106 int do_load = 1;
8107
8108 if (!STRICMP(ext, ".bmp"))
8109 sign.uType = IMAGE_BITMAP;
8110 else if (!STRICMP(ext, ".ico"))
8111 sign.uType = IMAGE_ICON;
8112 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8113 sign.uType = IMAGE_CURSOR;
8114 else
8115 do_load = 0;
8116
8117 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008118 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 gui.char_width * 2, gui.char_height,
8120 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008121# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (!STRICMP(ext, ".xpm"))
8123 {
8124 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008125 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8126 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
8130
8131 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008132 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 *psign = sign;
8134
8135 if (!psign)
8136 {
8137 if (sign.hImage)
8138 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008139 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 return (void *)psign;
8142
8143}
8144
8145 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008146gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147{
8148 if (sign)
8149 {
8150 close_signicon_image((signicon_t *)sign);
8151 vim_free(sign);
8152 }
8153}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008156#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157
Bram Moolenaar734a8672019-12-02 22:49:38 +01008158/*
8159 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008160 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008162 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8164 * to get current mouse position).
8165 *
8166 * Trying to use as more Windows services as possible, and as less
8167 * IE version as possible :)).
8168 *
8169 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8170 * BalloonEval struct.
8171 * 2) Enable/Disable simply create/kill BalloonEval Timer
8172 * 3) When there was enough inactivity, timer procedure posts
8173 * async request to debugger
8174 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8175 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008176 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 */
8178
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008179 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008180make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008181{
K.Takata76687d22022-01-25 10:31:37 +00008182 TOOLINFOW *pti;
8183 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008184
K.Takata76687d22022-01-25 10:31:37 +00008185 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008186 if (pti == NULL)
8187 return;
8188
8189 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8190 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8191 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008192 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008193
8194 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8195 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8196
K.Takata76687d22022-01-25 10:31:37 +00008197 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008198 pti->uFlags = TTF_SUBCLASS;
8199 pti->hwnd = beval->target;
8200 pti->hinst = 0; // Don't use string resources
8201 pti->uId = ID_BEVAL_TOOLTIP;
8202
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008203 pti->lpszText = LPSTR_TEXTCALLBACKW;
8204 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8205 pti->lParam = (LPARAM)beval->tofree;
8206 // switch multiline tooltips on
8207 if (GetClientRect(s_textArea, &rect))
8208 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8209 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008210
8211 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8212 pti->rect.left = pt.x - 3;
8213 pti->rect.top = pt.y - 3;
8214 pti->rect.right = pt.x + 3;
8215 pti->rect.bottom = pt.y + 3;
8216
8217 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8218 // Make tooltip appear sooner.
8219 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8220 // I've performed some tests and it seems the longest possible life time
8221 // of tooltip is 30 seconds.
8222 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8223 /*
8224 * HACK: force tooltip to appear, because it'll not appear until
8225 * first mouse move. D*mn M$
8226 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8227 */
8228 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8229 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8230 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008231}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008234delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008236 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237}
8238
8239 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008240BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008241 HWND hwnd UNUSED,
8242 UINT uMsg UNUSED,
8243 UINT_PTR idEvent UNUSED,
8244 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
8246 POINT pt;
8247 RECT rect;
8248
8249 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8250 return;
8251
8252 GetCursorPos(&pt);
8253 if (WindowFromPoint(pt) != s_textArea)
8254 return;
8255
8256 ScreenToClient(s_textArea, &pt);
8257 GetClientRect(s_textArea, &rect);
8258 if (!PtInRect(&rect, pt))
8259 return;
8260
8261 if (LastActivity > 0
8262 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8263 && (cur_beval->showState != ShS_PENDING
8264 || abs(cur_beval->x - pt.x) > 3
8265 || abs(cur_beval->y - pt.y) > 3))
8266 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008267 // Pointer resting in one place long enough, it's time to show
8268 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 cur_beval->showState = ShS_PENDING;
8270 cur_beval->x = pt.x;
8271 cur_beval->y = pt.y;
8272
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008273 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
8275 if (cur_beval->msgCB != NULL)
8276 (*cur_beval->msgCB)(cur_beval, 0);
8277 }
8278}
8279
8280 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008281gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008283 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008285 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286}
8287
8288 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008289gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008291 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (beval == NULL)
8293 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008294 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008295 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008296 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297}
8298
8299 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008300gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
8302 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008303
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008304 vim_free(beval->msg);
8305 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8306 if (beval->msg == NULL)
8307 {
8308 delete_tooltip(beval);
8309 beval->showState = ShS_NEUTRAL;
8310 return;
8311 }
8312
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008313 // TRACE0("gui_mch_post_balloon {{{");
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 Moolenaare2cc9702005-03-15 22:43:58 +00008326 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327}
8328
8329 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008330gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008331 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008332 char_u *mesg,
8333 void (*mesgCB)(BalloonEval *, int),
8334 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008336 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 BalloonEval *beval;
8338
8339 if (mesg != NULL && mesgCB != NULL)
8340 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008341 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 return NULL;
8343 }
8344
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008345 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (beval != NULL)
8347 {
8348 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
8350 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 beval->msg = mesg;
8352 beval->msgCB = mesgCB;
8353 beval->clientData = clientData;
8354
8355 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 cur_beval = beval;
8357
8358 if (p_beval)
8359 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
8361 return beval;
8362}
8363
8364 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008365Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008367 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 return;
8369
8370 if (cur_beval != NULL)
8371 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008372 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008374 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008375 // TRACE0("TTN_SHOW {{{");
8376 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008377 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008378 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008379 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 delete_tooltip(cur_beval);
8381 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008382 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008385 break;
8386 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008387 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008388 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008389 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008390 info->lpszText = (LPSTR)info->lParam;
8391 info->uFlags |= TTF_DI_SETITEM;
8392 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008393 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008394 case TTN_GETDISPINFOW:
8395 {
8396 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008397 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008398 info->lpszText = (LPWSTR)info->lParam;
8399 info->uFlags |= TTF_DI_SETITEM;
8400 }
8401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 }
8403 }
8404}
8405
8406 static void
8407TrackUserActivity(UINT uMsg)
8408{
8409 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8410 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8411 LastActivity = GetTickCount();
8412}
8413
8414 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008415gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008417# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008418 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008419# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008420 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 vim_free(beval);
8422}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008423#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424
8425#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8426/*
8427 * We have multiple signs to draw at the same location. Draw the
8428 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8429 */
8430 void
8431netbeans_draw_multisign_indicator(int row)
8432{
8433 int i;
8434 int y;
8435 int x;
8436
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008437 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008438 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 x = 0;
8441 y = TEXT_Y(row);
8442
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008443# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008444 if (IS_ENABLE_DIRECTX())
8445 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008446# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 for (i = 0; i < gui.char_height - 3; i++)
8449 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8450
8451 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8452 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8453 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8454 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8455 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8456 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8457 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8458}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008459#endif