blob: caed8e9de8cb56d307db1ac55b0495f5d3a24914 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000043#else
44# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010045#endif
46
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020047#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
48 int
49gui_mch_set_rendering_options(char_u *s)
50{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010051# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020052 char_u *p, *q;
53
54 int dx_enable = 0;
55 int dx_flags = 0;
56 float dx_gamma = 0.0f;
57 float dx_contrast = 0.0f;
58 float dx_level = 0.0f;
59 int dx_geom = 0;
60 int dx_renmode = 0;
61 int dx_taamode = 0;
62
Bram Moolenaar734a8672019-12-02 22:49:38 +010063 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020064 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100124 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100132
Bram Moolenaar734a8672019-12-02 22:49:38 +0100133 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
159
160 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100163# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * These are new in Windows ME/XP, only defined in recent compilers.
169 */
170#ifndef HANDLE_WM_XBUTTONUP
171# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
172 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
173#endif
174#ifndef HANDLE_WM_XBUTTONDOWN
175# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
176 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
177#endif
178#ifndef HANDLE_WM_XBUTTONDBLCLK
179# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
180 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
181#endif
182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183
Bram Moolenaar734a8672019-12-02 22:49:38 +0100184#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100185#ifdef DEBUG
186# include <tchar.h>
187#endif
188
Bram Moolenaar734a8672019-12-02 22:49:38 +0100189// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100190#ifndef PROTO
191
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100192# ifndef __MINGW32__
193# include <shellapi.h>
194# endif
195# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
196# include <commctrl.h>
197# endif
198# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100199
Bram Moolenaar734a8672019-12-02 22:49:38 +0100200#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
202#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100203# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100204#endif
205
Bram Moolenaar734a8672019-12-02 22:49:38 +0100206// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100207#define DLG_PADDING_X 10
208#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100209#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100210#define DLG_VERT_PADDING_Y 4
211#define DLG_ICON_WIDTH 34
212#define DLG_ICON_HEIGHT 34
213#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000214#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100215#define DLG_FONT_POINT_SIZE 8
216#define DLG_MIN_MAX_WIDTH 400
217#define DLG_MIN_MAX_HEIGHT 400
218
Bram Moolenaar734a8672019-12-02 22:49:38 +0100219#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100220
K.Takatac81e9bf2022-01-16 14:15:49 +0000221#ifndef WM_DPICHANGED
222# define WM_DPICHANGED 0x02E0
223#endif
224
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100225#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100227 * Define a few things for generating prototypes. This is just to avoid
228 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100230# define APIENTRY
231# define CALLBACK
232# define CONST
233# define FAR
234# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200235# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200236# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100237# define _cdecl
238typedef int BOOL;
239typedef int BYTE;
240typedef int DWORD;
241typedef int WCHAR;
242typedef int ENUMLOGFONT;
243typedef int FINDREPLACE;
244typedef int HANDLE;
245typedef int HBITMAP;
246typedef int HBRUSH;
247typedef int HDROP;
248typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100249typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100250typedef int LPARAM;
251typedef int LPCREATESTRUCT;
252typedef int LPCSTR;
253typedef int LPCTSTR;
254typedef int LPRECT;
255typedef int LPSTR;
256typedef int LPWINDOWPOS;
257typedef int LPWORD;
258typedef int LRESULT;
259typedef int HRESULT;
260# undef MSG
261typedef int MSG;
262typedef int NEWTEXTMETRIC;
263typedef int OSVERSIONINFO;
264typedef int PWORD;
265typedef int RECT;
266typedef int UINT;
267typedef int WORD;
268typedef int WPARAM;
269typedef int POINT;
270typedef void *HINSTANCE;
271typedef void *HMENU;
272typedef void *HWND;
273typedef void *HDC;
274typedef void VOID;
275typedef int LPNMHDR;
276typedef int LONG;
277typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200278typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200279typedef int COLORREF;
280typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100281#endif
282
K.Takata45f9cfb2022-01-21 11:11:00 +0000283static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100284static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100285static void clear_rect(RECT *rcp);
286
Bram Moolenaar734a8672019-12-02 22:49:38 +0100287static WORD s_dlgfntheight; // height of the dialog font
288static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289
290#ifdef FEAT_MENU
291static HMENU s_menuBar = NULL;
292#endif
293#ifdef FEAT_TEAROFF
294static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100295static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100296#endif
297
Bram Moolenaar734a8672019-12-02 22:49:38 +0100298// Flag that is set while processing a message that must not be interrupted by
299// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100300static int s_busy_processing = FALSE;
301
Bram Moolenaar734a8672019-12-02 22:49:38 +0100302static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303
304#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000305static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200306static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200308static int s_findrep_is_find; // TRUE for find dialog, FALSE
309 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100310#endif
311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312HWND s_hwnd = NULL;
313static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100314static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef FEAT_TOOLBAR
317static HWND s_toolbarhwnd = NULL;
318static WNDPROC s_toolbar_wndproc = NULL;
319#endif
320
321#ifdef FEAT_GUI_TABLINE
322static HWND s_tabhwnd = NULL;
323static WNDPROC s_tabline_wndproc = NULL;
324static int showing_tabline = 0;
325#endif
326
327static WPARAM s_wParam = 0;
328static LPARAM s_lParam = 0;
329
330static HWND s_textArea = NULL;
331static UINT s_uMsg = 0;
332
Bram Moolenaar734a8672019-12-02 22:49:38 +0100333static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100334
335static int s_need_activate = FALSE;
336
Bram Moolenaar734a8672019-12-02 22:49:38 +0100337// This variable is set when waiting for an event, which is the only moment
338// scrollbar dragging can be done directly. It's not allowed while commands
339// are executed, because it may move the cursor and that may cause unexpected
340// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100341static int allow_scrollbar = FALSE;
342
K.Takatac81e9bf2022-01-16 14:15:49 +0000343#ifndef _DPI_AWARENESS_CONTEXTS_
344typedef HANDLE DPI_AWARENESS_CONTEXT;
345
346typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000347 DPI_AWARENESS_INVALID = -1,
348 DPI_AWARENESS_UNAWARE = 0,
349 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000350 DPI_AWARENESS_PER_MONITOR_AWARE = 2
351} DPI_AWARENESS;
352
K.Takatab0b2b732022-01-19 12:59:21 +0000353# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
354# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000355# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
356# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
357# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
358#endif
359
360#define DEFAULT_DPI 96
361static int s_dpi = DEFAULT_DPI;
362static BOOL s_in_dpichanged = FALSE;
363static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
364
365static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
366static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
367static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
368//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
369static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
370static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
371
372 static UINT WINAPI
373stubGetDpiForSystem(void)
374{
375 HWND hwnd = GetDesktopWindow();
376 HDC hdc = GetWindowDC(hwnd);
377 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
378 ReleaseDC(hwnd, hdc);
379 return dpi;
380}
381
382 static int WINAPI
383stubGetSystemMetricsForDpi(int nIndex, UINT dpi)
384{
385 return GetSystemMetrics(nIndex);
386}
387
388 static int
389adjust_fontsize_by_dpi(int size)
390{
391 return size * s_dpi / (int)pGetDpiForSystem();
392}
393
394 static int
395adjust_by_system_dpi(int size)
396{
397 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
398}
399
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100400#if defined(FEAT_DIRECTX)
401 static int
402directx_enabled(void)
403{
404 if (s_dwc != NULL)
405 return 1;
406 else if (s_directx_load_attempted)
407 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100408 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100409 DWrite_Init();
410 s_directx_load_attempted = 1;
411 s_dwc = DWriteContext_Open();
412 directx_binddc();
413 return s_dwc != NULL ? 1 : 0;
414}
415
416 static void
417directx_binddc(void)
418{
419 if (s_textArea != NULL)
420 {
421 RECT rect;
422 GetClientRect(s_textArea, &rect);
423 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
424 }
425}
426#endif
427
Bram Moolenaar734a8672019-12-02 22:49:38 +0100428extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100429
430static struct
431{
432 UINT key_sym;
433 char_u vim_code0;
434 char_u vim_code1;
435} special_keys[] =
436{
437 {VK_UP, 'k', 'u'},
438 {VK_DOWN, 'k', 'd'},
439 {VK_LEFT, 'k', 'l'},
440 {VK_RIGHT, 'k', 'r'},
441
442 {VK_F1, 'k', '1'},
443 {VK_F2, 'k', '2'},
444 {VK_F3, 'k', '3'},
445 {VK_F4, 'k', '4'},
446 {VK_F5, 'k', '5'},
447 {VK_F6, 'k', '6'},
448 {VK_F7, 'k', '7'},
449 {VK_F8, 'k', '8'},
450 {VK_F9, 'k', '9'},
451 {VK_F10, 'k', ';'},
452
453 {VK_F11, 'F', '1'},
454 {VK_F12, 'F', '2'},
455 {VK_F13, 'F', '3'},
456 {VK_F14, 'F', '4'},
457 {VK_F15, 'F', '5'},
458 {VK_F16, 'F', '6'},
459 {VK_F17, 'F', '7'},
460 {VK_F18, 'F', '8'},
461 {VK_F19, 'F', '9'},
462 {VK_F20, 'F', 'A'},
463
464 {VK_F21, 'F', 'B'},
465#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100466 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100467#endif
468 {VK_F22, 'F', 'C'},
469 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471
472 {VK_HELP, '%', '1'},
473 {VK_BACK, 'k', 'b'},
474 {VK_INSERT, 'k', 'I'},
475 {VK_DELETE, 'k', 'D'},
476 {VK_HOME, 'k', 'h'},
477 {VK_END, '@', '7'},
478 {VK_PRIOR, 'k', 'P'},
479 {VK_NEXT, 'k', 'N'},
480 {VK_PRINT, '%', '9'},
481 {VK_ADD, 'K', '6'},
482 {VK_SUBTRACT, 'K', '7'},
483 {VK_DIVIDE, 'K', '8'},
484 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100485 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100486 {VK_DECIMAL, 'K', 'B'},
487
488 {VK_NUMPAD0, 'K', 'C'},
489 {VK_NUMPAD1, 'K', 'D'},
490 {VK_NUMPAD2, 'K', 'E'},
491 {VK_NUMPAD3, 'K', 'F'},
492 {VK_NUMPAD4, 'K', 'G'},
493 {VK_NUMPAD5, 'K', 'H'},
494 {VK_NUMPAD6, 'K', 'I'},
495 {VK_NUMPAD7, 'K', 'J'},
496 {VK_NUMPAD8, 'K', 'K'},
497 {VK_NUMPAD9, 'K', 'L'},
498
Bram Moolenaar734a8672019-12-02 22:49:38 +0100499 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 {VK_SPACE, ' ', NUL},
501 {VK_TAB, TAB, NUL},
502 {VK_ESCAPE, ESC, NUL},
503 {NL, NL, NUL},
504 {CAR, CAR, NUL},
505
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100507 {0, 0, 0}
508};
509
Bram Moolenaar734a8672019-12-02 22:49:38 +0100510// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511static int s_button_pending = -1;
512
Bram Moolenaar734a8672019-12-02 22:49:38 +0100513// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
514// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100515static int s_getting_focus = FALSE;
516
517static int s_x_pending;
518static int s_y_pending;
519static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000520static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200522static int dead_key = 0; // 0: no dead key, 1: dead key pressed
523static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
524 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100526#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100527// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100528static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000529static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100530#endif
531
532/*
533 * For control IME.
534 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100535 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100536 */
K.Takata4ac893f2022-01-20 12:44:28 +0000537#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100538// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100539static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100540// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100541static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100542#endif
543
544#ifdef FEAT_MBYTE_IME
545static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
546#endif
547
548#if defined(FEAT_BROWSE)
549static char_u *convert_filter(char_u *s);
550#endif
551
552#ifdef DEBUG_PRINT_ERROR
553/*
554 * Print out the last Windows error message
555 */
556 static void
557print_windows_error(void)
558{
559 LPVOID lpMsgBuf;
560
561 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
562 NULL, GetLastError(),
563 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
564 (LPTSTR) &lpMsgBuf, 0, NULL);
565 TRACE1("Error: %s\n", lpMsgBuf);
566 LocalFree(lpMsgBuf);
567}
568#endif
569
570/*
571 * Cursor blink functions.
572 *
573 * This is a simple state machine:
574 * BLINK_NONE not blinking at all
575 * BLINK_OFF blinking, cursor is not shown
576 * BLINK_ON blinking, cursor is shown
577 */
578
579#define BLINK_NONE 0
580#define BLINK_OFF 1
581#define BLINK_ON 2
582
583static int blink_state = BLINK_NONE;
584static long_u blink_waittime = 700;
585static long_u blink_ontime = 400;
586static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000587static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100588
Bram Moolenaar703a8042016-06-04 16:24:32 +0200589 int
590gui_mch_is_blinking(void)
591{
592 return blink_state != BLINK_NONE;
593}
594
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200595 int
596gui_mch_is_blink_off(void)
597{
598 return blink_state == BLINK_OFF;
599}
600
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100601 void
602gui_mch_set_blinking(long wait, long on, long off)
603{
604 blink_waittime = wait;
605 blink_ontime = on;
606 blink_offtime = off;
607}
608
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609 static VOID CALLBACK
610_OnBlinkTimer(
611 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100612 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000613 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100614 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615{
616 MSG msg;
617
618 /*
619 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
620 */
621
622 KillTimer(NULL, idEvent);
623
Bram Moolenaar734a8672019-12-02 22:49:38 +0100624 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000625 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100626 ;
627
628 if (blink_state == BLINK_ON)
629 {
630 gui_undraw_cursor();
631 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000632 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633 }
634 else
635 {
636 gui_update_cursor(TRUE, FALSE);
637 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000638 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100640 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641}
642
643 static void
644gui_mswin_rm_blink_timer(void)
645{
646 MSG msg;
647
648 if (blink_timer != 0)
649 {
650 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100651 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000652 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 ;
654 blink_timer = 0;
655 }
656}
657
658/*
659 * Stop the cursor blinking. Show the cursor if it wasn't shown.
660 */
661 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100662gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100663{
664 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100665 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100666 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100667 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100668 gui_mch_flush();
669 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670 blink_state = BLINK_NONE;
671}
672
673/*
674 * Start the cursor blinking. If it was already blinking, this restarts the
675 * waiting time and shows the cursor.
676 */
677 void
678gui_mch_start_blink(void)
679{
680 gui_mswin_rm_blink_timer();
681
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
684 {
K.Takataa8ec4912022-02-03 14:32:33 +0000685 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686 blink_state = BLINK_ON;
687 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100688 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100689 }
690}
691
692/*
693 * Call-back routines.
694 */
695
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100696 static VOID CALLBACK
697_OnTimer(
698 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100699 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000700 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100701 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702{
703 MSG msg;
704
705 /*
706 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
707 */
708 KillTimer(NULL, idEvent);
709 s_timed_out = TRUE;
710
Bram Moolenaar734a8672019-12-02 22:49:38 +0100711 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000712 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100713 ;
714 if (idEvent == s_wait_timer)
715 s_wait_timer = 0;
716}
717
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100718 static void
719_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100720 HWND hwnd UNUSED,
721 UINT ch UNUSED,
722 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100723{
724 dead_key = 1;
725}
726
727/*
728 * Convert Unicode character "ch" to bytes in "string[slen]".
729 * When "had_alt" is TRUE the ALT key was included in "ch".
730 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200731 * Because the Windows API uses UTF-16, we have to deal with surrogate
732 * pairs; this is where we choose to deal with them: if "ch" is a high
733 * surrogate, it will be stored, and the length returned will be zero; the next
734 * char_to_string call will then include the high surrogate, decoding the pair
735 * of UTF-16 code units to a single Unicode code point, presuming it is the
736 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100737 */
738 static int
739char_to_string(int ch, char_u *string, int slen, int had_alt)
740{
741 int len;
742 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100743 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200744 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100745
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200746 if (surrogate_pending_ch != 0)
747 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100748 // We don't guarantee ch is a low surrogate to match the high surrogate
749 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200750 wstring[0] = surrogate_pending_ch;
751 wstring[1] = ch;
752 surrogate_pending_ch = 0;
753 len = 2;
754 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100755 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200756 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100757 // We don't have the entire code point yet, only the first UTF-16 code
758 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200759 surrogate_pending_ch = ch;
760 return 0;
761 }
762 else
763 {
764 wstring[0] = ch;
765 len = 1;
766 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767
Bram Moolenaar734a8672019-12-02 22:49:38 +0100768 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
769 // "enc_codepage" is non-zero use the standard Win32 function,
770 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200771 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200773 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
774 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // If we had included the ALT key into the character but now the
776 // upper bit is no longer set, that probably means the conversion
777 // failed. Convert the original character and set the upper bit
778 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200779 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100780 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200781 wstring[0] = ch & 0x7f;
782 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
783 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100784 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100786 }
787 }
788 else
789 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790 ws = utf16_to_enc(wstring, &len);
791 if (ws == NULL)
792 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 else
794 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100795 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 len = slen;
797 mch_memmove(string, ws, len);
798 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 }
800 }
801
802 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 {
804 string[0] = ch;
805 len = 1;
806 }
807
808 for (i = 0; i < len; ++i)
809 if (string[i] == CSI && len <= slen - 2)
810 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100811 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
813 string[++i] = KS_EXTRA;
814 string[++i] = (int)KE_CSI;
815 len += 2;
816 }
817
818 return len;
819}
820
821/*
822 * Key hit, add it to the input buffer.
823 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 static void
825_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100826 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100827 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100828 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829{
830 char_u string[40];
831 int len = 0;
LemonBoy77fc0b02022-04-22 22:45:52 +0100832 int modifiers = 0;
833 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100834
835 dead_key = 0;
836
LemonBoy77fc0b02022-04-22 22:45:52 +0100837 if (GetKeyState(VK_SHIFT) & 0x8000)
838 modifiers |= MOD_MASK_SHIFT;
839 if (GetKeyState(VK_CONTROL) & 0x8000)
840 modifiers |= MOD_MASK_CTRL;
841
842 ch = simplify_key(ch, &modifiers);
843 // remove the SHIFT modifier for keys where it's already included, e.g.,
844 // '(' and '*'
845 modifiers = may_remove_shift_modifier(modifiers, ch);
846
847 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
848 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
849 if (ch == CSI)
850 ch = K_CSI;
851
852 if (modifiers)
853 {
854 string[0] = CSI;
855 string[1] = KS_MODIFIER;
856 string[2] = modifiers;
857 add_to_input_buf(string, 3);
858 }
859
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100860 len = char_to_string(ch, string, 40, FALSE);
861 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
862 {
863 trash_input_buf();
864 got_int = TRUE;
865 }
866
867 add_to_input_buf(string, len);
868}
869
870/*
871 * Alt-Key hit, add it to the input buffer.
872 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100873 static void
874_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100875 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100876 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100877 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100878{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100879 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880 int len;
881 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100882 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100883
884 dead_key = 0;
885
Bram Moolenaar734a8672019-12-02 22:49:38 +0100886 // OK, we have a character key (given by ch) which was entered with the
887 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
888 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
889 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100890 modifiers = MOD_MASK_ALT;
891 if (GetKeyState(VK_SHIFT) & 0x8000)
892 modifiers |= MOD_MASK_SHIFT;
893 if (GetKeyState(VK_CONTROL) & 0x8000)
894 modifiers |= MOD_MASK_CTRL;
895
896 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100897 // remove the SHIFT modifier for keys where it's already included, e.g.,
898 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200899 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100900
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200901 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
902 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 if (ch == CSI)
904 ch = K_CSI;
905
906 len = 0;
907 if (modifiers)
908 {
909 string[len++] = CSI;
910 string[len++] = KS_MODIFIER;
911 string[len++] = modifiers;
912 }
913
914 if (IS_SPECIAL((int)ch))
915 {
916 string[len++] = CSI;
917 string[len++] = K_SECOND((int)ch);
918 string[len++] = K_THIRD((int)ch);
919 }
920 else
921 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100922 // Although the documentation isn't clear about it, we assume "ch" is
923 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100924 len += char_to_string(ch, string + len, 40 - len, TRUE);
925 }
926
927 add_to_input_buf(string, len);
928}
929
930 static void
931_OnMouseEvent(
932 int button,
933 int x,
934 int y,
935 int repeated_click,
936 UINT keyFlags)
937{
938 int vim_modifiers = 0x0;
939
940 s_getting_focus = FALSE;
941
942 if (keyFlags & MK_SHIFT)
943 vim_modifiers |= MOUSE_SHIFT;
944 if (keyFlags & MK_CONTROL)
945 vim_modifiers |= MOUSE_CTRL;
946 if (GetKeyState(VK_MENU) & 0x8000)
947 vim_modifiers |= MOUSE_ALT;
948
949 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
950}
951
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100952 static void
953_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100954 HWND hwnd UNUSED,
955 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100956 int x,
957 int y,
958 UINT keyFlags)
959{
960 static LONG s_prevTime = 0;
961
962 LONG currentTime = GetMessageTime();
963 int button = -1;
964 int repeated_click;
965
Bram Moolenaar734a8672019-12-02 22:49:38 +0100966 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100967 (void)SetFocus(s_hwnd);
968
969 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
970 button = MOUSE_LEFT;
971 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
972 button = MOUSE_MIDDLE;
973 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
974 button = MOUSE_RIGHT;
975 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
976 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100977 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
978 }
979 else if (s_uMsg == WM_CAPTURECHANGED)
980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100981 // on W95/NT4, somehow you get in here with an odd Msg
982 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100983 if (s_button_pending == MOUSE_LEFT)
984 button = MOUSE_RIGHT;
985 else
986 button = MOUSE_LEFT;
987 }
988 if (button >= 0)
989 {
990 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
991
992 /*
993 * Holding down the left and right buttons simulates pushing the middle
994 * button.
995 */
996 if (repeated_click
997 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
998 || (button == MOUSE_RIGHT
999 && s_button_pending == MOUSE_LEFT)))
1000 {
1001 /*
1002 * Hmm, gui.c will ignore more than one button down at a time, so
1003 * pretend we let go of it first.
1004 */
1005 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1006 button = MOUSE_MIDDLE;
1007 repeated_click = FALSE;
1008 s_button_pending = -1;
1009 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1010 }
1011 else if ((repeated_click)
1012 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1013 {
1014 if (s_button_pending > -1)
1015 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001016 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1017 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001018 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001019 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001020 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1021 }
1022 else
1023 {
1024 /*
1025 * If this is the first press (i.e. not a multiple click) don't
1026 * action immediately, but store and wait for:
1027 * i) button-up
1028 * ii) mouse move
1029 * iii) another button press
1030 * before using it.
1031 * This enables us to make left+right simulate middle button,
1032 * without left or right being actioned first. The side-effect is
1033 * that if you click and hold the mouse without dragging, the
1034 * cursor doesn't move until you release the button. In practice
1035 * this is hardly a problem.
1036 */
1037 s_button_pending = button;
1038 s_x_pending = x;
1039 s_y_pending = y;
1040 s_kFlags_pending = keyFlags;
1041 }
1042
1043 s_prevTime = currentTime;
1044 }
1045}
1046
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001047 static void
1048_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001049 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001050 int x,
1051 int y,
1052 UINT keyFlags)
1053{
1054 int button;
1055
1056 s_getting_focus = FALSE;
1057 if (s_button_pending > -1)
1058 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001059 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001060 _OnMouseEvent(s_button_pending, s_x_pending,
1061 s_y_pending, FALSE, s_kFlags_pending);
1062 s_button_pending = -1;
1063 }
1064 if (s_uMsg == WM_MOUSEMOVE)
1065 {
1066 /*
1067 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1068 * down.
1069 */
1070 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1071 | MK_XBUTTON1 | MK_XBUTTON2)))
1072 {
1073 gui_mouse_moved(x, y);
1074 return;
1075 }
1076
1077 /*
1078 * While button is down, keep grabbing mouse move events when
1079 * the mouse goes outside the window
1080 */
1081 SetCapture(s_textArea);
1082 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001083 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001084 }
1085 else
1086 {
1087 ReleaseCapture();
1088 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001089 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001090 }
1091
1092 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1093}
1094
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001095 static void
1096_OnSizeTextArea(
1097 HWND hwnd UNUSED,
1098 UINT state UNUSED,
1099 int cx UNUSED,
1100 int cy UNUSED)
1101{
1102#if defined(FEAT_DIRECTX)
1103 if (IS_ENABLE_DIRECTX())
1104 directx_binddc();
1105#endif
1106}
1107
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108#ifdef FEAT_MENU
1109/*
1110 * Find the vimmenu_T with the given id
1111 */
1112 static vimmenu_T *
1113gui_mswin_find_menu(
1114 vimmenu_T *pMenu,
1115 int id)
1116{
1117 vimmenu_T *pChildMenu;
1118
1119 while (pMenu)
1120 {
1121 if (pMenu->id == (UINT)id)
1122 break;
1123 if (pMenu->children != NULL)
1124 {
1125 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1126 if (pChildMenu)
1127 {
1128 pMenu = pChildMenu;
1129 break;
1130 }
1131 }
1132 pMenu = pMenu->next;
1133 }
1134 return pMenu;
1135}
1136
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001137 static void
1138_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001139 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001140 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001141 HWND hwndCtl UNUSED,
1142 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001143{
1144 vimmenu_T *pMenu;
1145
1146 pMenu = gui_mswin_find_menu(root_menu, id);
1147 if (pMenu)
1148 gui_menu_cb(pMenu);
1149}
1150#endif
1151
1152#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001153/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001154 * Handle a Find/Replace window message.
1155 */
1156 static void
1157_OnFindRepl(void)
1158{
1159 int flags = 0;
1160 int down;
1161
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001162 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001163 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001164 (void)SetFocus(s_hwnd);
1165
1166 if (s_findrep_struct.Flags & FR_FINDNEXT)
1167 {
1168 flags = FRD_FINDNEXT;
1169
Bram Moolenaar734a8672019-12-02 22:49:38 +01001170 // Give main window the focus back: this is so the cursor isn't
1171 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001172 (void)SetFocus(s_hwnd);
1173 }
1174 else if (s_findrep_struct.Flags & FR_REPLACE)
1175 {
1176 flags = FRD_REPLACE;
1177
Bram Moolenaar734a8672019-12-02 22:49:38 +01001178 // Give main window the focus back: this is so the cursor isn't
1179 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001180 (void)SetFocus(s_hwnd);
1181 }
1182 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1183 {
1184 flags = FRD_REPLACEALL;
1185 }
1186
1187 if (flags != 0)
1188 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001189 char_u *p, *q;
1190
Bram Moolenaar734a8672019-12-02 22:49:38 +01001191 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1193 flags |= FRD_WHOLE_WORD;
1194 if (s_findrep_struct.Flags & FR_MATCHCASE)
1195 flags |= FRD_MATCH_CASE;
1196 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001197 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1198 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1199 if (p != NULL && q != NULL)
1200 gui_do_findrepl(flags, p, q, down);
1201 vim_free(p);
1202 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001203 }
1204}
1205#endif
1206
1207 static void
1208HandleMouseHide(UINT uMsg, LPARAM lParam)
1209{
1210 static LPARAM last_lParam = 0L;
1211
Bram Moolenaar734a8672019-12-02 22:49:38 +01001212 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001213 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1214 {
1215 if (lParam == last_lParam)
1216 return;
1217 last_lParam = lParam;
1218 }
1219
Bram Moolenaar734a8672019-12-02 22:49:38 +01001220 // Handle specially, to centralise coding. We need to be sure we catch all
1221 // possible events which should cause us to restore the cursor (as it is a
1222 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001223 switch (uMsg)
1224 {
1225 case WM_KEYUP:
1226 case WM_CHAR:
1227 /*
1228 * blank out the pointer if necessary
1229 */
1230 if (p_mh)
1231 gui_mch_mousehide(TRUE);
1232 break;
1233
Bram Moolenaar734a8672019-12-02 22:49:38 +01001234 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001236 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001237 case WM_LBUTTONDOWN:
1238 case WM_LBUTTONUP:
1239 case WM_MBUTTONDOWN:
1240 case WM_MBUTTONUP:
1241 case WM_RBUTTONDOWN:
1242 case WM_RBUTTONUP:
1243 case WM_XBUTTONDOWN:
1244 case WM_XBUTTONUP:
1245 case WM_NCMOUSEMOVE:
1246 case WM_NCLBUTTONDOWN:
1247 case WM_NCLBUTTONUP:
1248 case WM_NCMBUTTONDOWN:
1249 case WM_NCMBUTTONUP:
1250 case WM_NCRBUTTONDOWN:
1251 case WM_NCRBUTTONUP:
1252 case WM_KILLFOCUS:
1253 /*
1254 * if the pointer is currently hidden, then we should show it.
1255 */
1256 gui_mch_mousehide(FALSE);
1257 break;
1258 }
1259}
1260
1261 static LRESULT CALLBACK
1262_TextAreaWndProc(
1263 HWND hwnd,
1264 UINT uMsg,
1265 WPARAM wParam,
1266 LPARAM lParam)
1267{
1268 /*
1269 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1270 hwnd, uMsg, wParam, lParam);
1271 */
1272
1273 HandleMouseHide(uMsg, lParam);
1274
1275 s_uMsg = uMsg;
1276 s_wParam = wParam;
1277 s_lParam = lParam;
1278
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001279#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001280 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001281#endif
1282
1283 switch (uMsg)
1284 {
1285 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1286 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1287 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1288 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1289 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1290 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1291 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1292 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1293 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1294 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1295 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1296 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1297 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1298 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001299 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001300
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001301#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001302 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1303 return TRUE;
1304#endif
1305 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001306 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001307 }
1308}
1309
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001310/*
1311 * Called when the foreground or background color has been changed.
1312 */
1313 void
1314gui_mch_new_colors(void)
1315{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001316 HBRUSH prevBrush;
1317
1318 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001319 prevBrush = (HBRUSH)SetClassLongPtr(
1320 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001321 InvalidateRect(s_hwnd, NULL, TRUE);
1322 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001323}
1324
1325/*
1326 * Set the colors to their default values.
1327 */
1328 void
1329gui_mch_def_colors(void)
1330{
1331 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1332 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1333 gui.def_norm_pixel = gui.norm_pixel;
1334 gui.def_back_pixel = gui.back_pixel;
1335}
1336
1337/*
1338 * Open the GUI window which was created by a call to gui_mch_init().
1339 */
1340 int
1341gui_mch_open(void)
1342{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001343 // Actually open the window, if not already visible
1344 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001345 if (!IsWindowVisible(s_hwnd))
1346 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1347
1348#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001349 // Init replace string here, so that we keep it when re-opening the
1350 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001351 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1352#endif
1353
1354 return OK;
1355}
1356
1357/*
1358 * Get the position of the top left corner of the window.
1359 */
1360 int
1361gui_mch_get_winpos(int *x, int *y)
1362{
1363 RECT rect;
1364
1365 GetWindowRect(s_hwnd, &rect);
1366 *x = rect.left;
1367 *y = rect.top;
1368 return OK;
1369}
1370
1371/*
1372 * Set the position of the top left corner of the window to the given
1373 * coordinates.
1374 */
1375 void
1376gui_mch_set_winpos(int x, int y)
1377{
1378 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1379 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1380}
1381 void
1382gui_mch_set_text_area_pos(int x, int y, int w, int h)
1383{
1384 static int oldx = 0;
1385 static int oldy = 0;
1386
1387 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1388
1389#ifdef FEAT_TOOLBAR
1390 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1391 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001392 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001393#endif
1394#if defined(FEAT_GUI_TABLINE)
1395 if (showing_tabline)
1396 {
1397 int top = 0;
1398 RECT rect;
1399
1400# ifdef FEAT_TOOLBAR
1401 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001402 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001403# endif
1404 GetClientRect(s_hwnd, &rect);
1405 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1406 }
1407#endif
1408
Bram Moolenaar734a8672019-12-02 22:49:38 +01001409 // When side scroll bar is unshown, the size of window will change.
1410 // then, the text area move left or right. thus client rect should be
1411 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001412 if (oldx != x || oldy != y)
1413 {
1414 InvalidateRect(s_hwnd, NULL, FALSE);
1415 oldx = x;
1416 oldy = y;
1417 }
1418}
1419
1420
1421/*
1422 * Scrollbar stuff:
1423 */
1424
1425 void
1426gui_mch_enable_scrollbar(
1427 scrollbar_T *sb,
1428 int flag)
1429{
1430 ShowScrollBar(sb->id, SB_CTL, flag);
1431
Bram Moolenaar734a8672019-12-02 22:49:38 +01001432 // TODO: When the window is maximized, the size of the window stays the
1433 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1434 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001435}
1436
1437 void
1438gui_mch_set_scrollbar_pos(
1439 scrollbar_T *sb,
1440 int x,
1441 int y,
1442 int w,
1443 int h)
1444{
1445 SetWindowPos(sb->id, NULL, x, y, w, h,
1446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1447}
1448
Bram Moolenaar203ec772020-07-17 20:43:43 +02001449 int
1450gui_mch_get_scrollbar_xpadding(void)
1451{
1452 RECT rcTxt, rcWnd;
1453 int xpad;
1454
1455 GetWindowRect(s_textArea, &rcTxt);
1456 GetWindowRect(s_hwnd, &rcWnd);
1457 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001458 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1459 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001460 return (xpad < 0) ? 0 : xpad;
1461}
1462
1463 int
1464gui_mch_get_scrollbar_ypadding(void)
1465{
1466 RECT rcTxt, rcWnd;
1467 int ypad;
1468
1469 GetWindowRect(s_textArea, &rcTxt);
1470 GetWindowRect(s_hwnd, &rcWnd);
1471 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001472 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1473 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001474 return (ypad < 0) ? 0 : ypad;
1475}
1476
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001477 void
1478gui_mch_create_scrollbar(
1479 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001480 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001481{
1482 sb->id = CreateWindow(
1483 "SCROLLBAR", "Scrollbar",
1484 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001485 10, // Any value will do for now
1486 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001487 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001488 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001489}
1490
1491/*
1492 * Find the scrollbar with the given hwnd.
1493 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001494 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001495gui_mswin_find_scrollbar(HWND hwnd)
1496{
1497 win_T *wp;
1498
1499 if (gui.bottom_sbar.id == hwnd)
1500 return &gui.bottom_sbar;
1501 FOR_ALL_WINDOWS(wp)
1502 {
1503 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1504 return &wp->w_scrollbars[SBAR_LEFT];
1505 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1506 return &wp->w_scrollbars[SBAR_RIGHT];
1507 }
1508 return NULL;
1509}
1510
K.Takatac81e9bf2022-01-16 14:15:49 +00001511 static void
1512update_scrollbar_size(void)
1513{
1514 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1515 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1516}
1517
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001518/*
K.Takataabe628e2022-01-23 16:25:17 +00001519 * Get the average character size of a font.
1520 */
1521 static void
1522GetAverageFontSize(HDC hdc, SIZE *size)
1523{
1524 // GetTextMetrics() may not return the right value in tmAveCharWidth
1525 // for some fonts. Do our own average computation.
1526 GetTextExtentPoint(hdc,
1527 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1528 52, size);
1529 size->cx = (size->cx / 26 + 1) / 2;
1530}
1531
1532/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001533 * Get the character size of a font.
1534 */
1535 static void
1536GetFontSize(GuiFont font)
1537{
1538 HWND hwnd = GetDesktopWindow();
1539 HDC hdc = GetWindowDC(hwnd);
1540 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001541 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001542 TEXTMETRIC tm;
1543
1544 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001545 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001546
K.Takataabe628e2022-01-23 16:25:17 +00001547 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001548 gui.char_height = tm.tmHeight + p_linespace;
1549
1550 SelectFont(hdc, hfntOld);
1551
1552 ReleaseDC(hwnd, hdc);
1553}
1554
1555/*
1556 * Adjust gui.char_height (after 'linespace' was changed).
1557 */
1558 int
1559gui_mch_adjust_charheight(void)
1560{
1561 GetFontSize(gui.norm_font);
1562 return OK;
1563}
1564
1565 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001566get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001567{
1568 HFONT font = NULL;
1569
Bram Moolenaar734a8672019-12-02 22:49:38 +01001570 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001571 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001572
1573 if (font == NULL)
1574 return NOFONT;
1575
1576 return (GuiFont)font;
1577}
1578
1579 static int
1580pixels_to_points(int pixels, int vertical)
1581{
1582 int points;
1583 HWND hwnd;
1584 HDC hdc;
1585
1586 hwnd = GetDesktopWindow();
1587 hdc = GetWindowDC(hwnd);
1588
1589 points = MulDiv(pixels, 72,
1590 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1591
1592 ReleaseDC(hwnd, hdc);
1593
1594 return points;
1595}
1596
1597 GuiFont
1598gui_mch_get_font(
1599 char_u *name,
1600 int giveErrorIfMissing)
1601{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001602 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001603 GuiFont font = NOFONT;
1604
1605 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001606 {
1607 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001608 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001609 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001610 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001611 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001612 return font;
1613}
1614
1615#if defined(FEAT_EVAL) || defined(PROTO)
1616/*
1617 * Return the name of font "font" in allocated memory.
1618 * Don't know how to get the actual name, thus use the provided name.
1619 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001620 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001621gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622{
1623 if (name == NULL)
1624 return NULL;
1625 return vim_strsave(name);
1626}
1627#endif
1628
1629 void
1630gui_mch_free_font(GuiFont font)
1631{
1632 if (font)
1633 DeleteObject((HFONT)font);
1634}
1635
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636/*
1637 * Return the Pixel value (color) for the given color name.
1638 * Return INVALCOLOR for error.
1639 */
1640 guicolor_T
1641gui_mch_get_color(char_u *name)
1642{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001643 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644
1645 typedef struct SysColorTable
1646 {
1647 char *name;
1648 int color;
1649 } SysColorTable;
1650
1651 static SysColorTable sys_table[] =
1652 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1654 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001655#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1657#endif
1658 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1659 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1660 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1661 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1662 {"SYS_DESKTOP", COLOR_DESKTOP},
1663 {"SYS_INFOBK", COLOR_INFOBK},
1664 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1665 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001666 {"SYS_BTNFACE", COLOR_BTNFACE},
1667 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1668 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1669 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1670 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1671 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1672 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1673 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1674 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1675 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1676 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1677 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1678 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1679 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1680 {"SYS_MENU", COLOR_MENU},
1681 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1682 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1683 {"SYS_WINDOW", COLOR_WINDOW},
1684 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1685 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1686 };
1687
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001688 /*
1689 * Try to look up a system colour.
1690 */
K.Takataeeec2542021-06-02 13:28:16 +02001691 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692 if (STRICMP(name, sys_table[i].name) == 0)
1693 return GetSysColor(sys_table[i].color);
1694
Bram Moolenaarab302212016-04-26 20:59:29 +02001695 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001696}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001697
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001698 guicolor_T
1699gui_mch_get_rgb_color(int r, int g, int b)
1700{
1701 return gui_get_rgb_color_cmn(r, g, b);
1702}
1703
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001704/*
1705 * Return OK if the key with the termcap name "name" is supported.
1706 */
1707 int
1708gui_mch_haskey(char_u *name)
1709{
1710 int i;
1711
1712 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1713 if (name[0] == special_keys[i].vim_code0 &&
1714 name[1] == special_keys[i].vim_code1)
1715 return OK;
1716 return FAIL;
1717}
1718
1719 void
1720gui_mch_beep(void)
1721{
LemonBoy77771d32022-04-13 11:47:25 +01001722 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001723}
1724/*
1725 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1726 */
1727 void
1728gui_mch_invert_rectangle(
1729 int r,
1730 int c,
1731 int nr,
1732 int nc)
1733{
1734 RECT rc;
1735
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001736#if defined(FEAT_DIRECTX)
1737 if (IS_ENABLE_DIRECTX())
1738 DWriteContext_Flush(s_dwc);
1739#endif
1740
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001741 /*
1742 * Note: InvertRect() excludes right and bottom of rectangle.
1743 */
1744 rc.left = FILL_X(c);
1745 rc.top = FILL_Y(r);
1746 rc.right = rc.left + nc * gui.char_width;
1747 rc.bottom = rc.top + nr * gui.char_height;
1748 InvertRect(s_hdc, &rc);
1749}
1750
1751/*
1752 * Iconify the GUI window.
1753 */
1754 void
1755gui_mch_iconify(void)
1756{
1757 ShowWindow(s_hwnd, SW_MINIMIZE);
1758}
1759
1760/*
1761 * Draw a cursor without focus.
1762 */
1763 void
1764gui_mch_draw_hollow_cursor(guicolor_T color)
1765{
1766 HBRUSH hbr;
1767 RECT rc;
1768
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001769#if defined(FEAT_DIRECTX)
1770 if (IS_ENABLE_DIRECTX())
1771 DWriteContext_Flush(s_dwc);
1772#endif
1773
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001774 /*
1775 * Note: FrameRect() excludes right and bottom of rectangle.
1776 */
1777 rc.left = FILL_X(gui.col);
1778 rc.top = FILL_Y(gui.row);
1779 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001780 if (mb_lefthalve(gui.row, gui.col))
1781 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001782 rc.bottom = rc.top + gui.char_height;
1783 hbr = CreateSolidBrush(color);
1784 FrameRect(s_hdc, &rc, hbr);
1785 DeleteBrush(hbr);
1786}
1787/*
1788 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1789 * color "color".
1790 */
1791 void
1792gui_mch_draw_part_cursor(
1793 int w,
1794 int h,
1795 guicolor_T color)
1796{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001797 RECT rc;
1798
1799 /*
1800 * Note: FillRect() excludes right and bottom of rectangle.
1801 */
1802 rc.left =
1803#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001804 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001805 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1806#endif
1807 FILL_X(gui.col);
1808 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1809 rc.right = rc.left + w;
1810 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001811
Bram Moolenaar92467d32017-12-05 13:22:16 +01001812 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001813}
1814
1815
1816/*
1817 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1818 * dead key's nominal character and re-post the original message.
1819 */
1820 static void
1821outputDeadKey_rePost(MSG originalMsg)
1822{
1823 static MSG deadCharExpel;
1824
1825 if (!dead_key)
1826 return;
1827
1828 dead_key = 0;
1829
Bram Moolenaar734a8672019-12-02 22:49:38 +01001830 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001831 deadCharExpel.message = originalMsg.message;
1832 deadCharExpel.hwnd = originalMsg.hwnd;
1833 deadCharExpel.wParam = VK_SPACE;
1834
K.Takata4ac893f2022-01-20 12:44:28 +00001835 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001836
Bram Moolenaar734a8672019-12-02 22:49:38 +01001837 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001838 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1839 originalMsg.lParam);
1840}
1841
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001842/*
1843 * Process a single Windows message.
1844 * If one is not available we hang until one is.
1845 */
1846 static void
1847process_message(void)
1848{
1849 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001850 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851 char_u string[40];
1852 int i;
1853 int modifiers = 0;
1854 int key;
1855#ifdef FEAT_MENU
1856 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1857#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001858 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001859
K.Takatab7057bd2022-01-21 11:37:07 +00001860 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001861
1862#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001863 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001864 if (msg.message == WM_OLE)
1865 {
1866 char_u *str = (char_u *)msg.lParam;
1867 if (str == NULL || *str == NUL)
1868 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001869 // Message can't be ours, forward it. Fixes problem with Ultramon
1870 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001871 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872 }
1873 else
1874 {
1875 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001876 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001877 }
1878 return;
1879 }
1880#endif
1881
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001882#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001883 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001884 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885 {
1886 HandleMouseHide(msg.message, msg.lParam);
1887 return;
1888 }
1889#endif
1890
1891 /*
1892 * Check if it's a special key that we recognise. If not, call
1893 * TranslateMessage().
1894 */
1895 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1896 {
1897 vk = (int) msg.wParam;
1898
1899 /*
1900 * Handle dead keys in special conditions in other cases we let Windows
1901 * handle them and do not interfere.
1902 *
1903 * The dead_key flag must be reset on several occasions:
1904 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1905 * consumed at that point (This is when we let Windows combine the
1906 * dead character on its own)
1907 *
1908 * - Before doing something special such as regenerating keypresses to
1909 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001910 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001911 * immediately to _OnChar() (or _OnSysChar()).
1912 */
1913 if (dead_key)
1914 {
1915 /*
1916 * If a dead key was pressed and the user presses VK_SPACE,
1917 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1918 * with the dead char now, so do nothing special and let Windows
1919 * handle it.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001920 */
1921 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1922 {
1923 dead_key = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001924 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001925 // In modes where we are not typing, dead keys should behave
1926 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001927 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1928 {
1929 outputDeadKey_rePost(msg);
1930 return;
1931 }
1932 }
1933
Bram Moolenaar734a8672019-12-02 22:49:38 +01001934 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935 if (vk == VK_CANCEL)
1936 {
1937 trash_input_buf();
1938 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001939 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001940 string[0] = Ctrl_C;
1941 add_to_input_buf(string, 1);
1942 }
1943
LemonBoy77fc0b02022-04-22 22:45:52 +01001944 // This is an IME event or a synthetic keystroke, let Windows handle it.
1945 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
1946 {
1947 TranslateMessage(&msg);
1948 return;
1949 }
1950
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001951 for (i = 0; special_keys[i].key_sym != 0; i++)
1952 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001953 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001954 if (special_keys[i].key_sym == vk
1955 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1956 {
1957 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001958 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001959 * is a key that would normally trigger the dead key nominal
1960 * character output (such as a NUMPAD printable character or
1961 * the TAB key, etc...).
1962 */
1963 if (dead_key && (special_keys[i].vim_code0 == 'K'
1964 || vk == VK_TAB || vk == CAR))
1965 {
1966 outputDeadKey_rePost(msg);
1967 return;
1968 }
1969
1970#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001971 // Check for <F10>: Windows selects the menu. When <F10> is
1972 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001973 if (vk == VK_F10
1974 && gui.menu_is_active
1975 && check_map(k10, State, FALSE, TRUE, FALSE,
1976 NULL, NULL) == NULL)
1977 break;
1978#endif
1979 if (GetKeyState(VK_SHIFT) & 0x8000)
1980 modifiers |= MOD_MASK_SHIFT;
1981 /*
1982 * Don't use caps-lock as shift, because these are special keys
1983 * being considered here, and we only want letters to get
1984 * shifted -- webb
1985 */
1986 /*
1987 if (GetKeyState(VK_CAPITAL) & 0x0001)
1988 modifiers ^= MOD_MASK_SHIFT;
1989 */
1990 if (GetKeyState(VK_CONTROL) & 0x8000)
1991 modifiers |= MOD_MASK_CTRL;
1992 if (GetKeyState(VK_MENU) & 0x8000)
1993 modifiers |= MOD_MASK_ALT;
1994
1995 if (special_keys[i].vim_code1 == NUL)
1996 key = special_keys[i].vim_code0;
1997 else
1998 key = TO_SPECIAL(special_keys[i].vim_code0,
1999 special_keys[i].vim_code1);
2000 key = simplify_key(key, &modifiers);
2001 if (key == CSI)
2002 key = K_CSI;
2003
2004 if (modifiers)
2005 {
2006 string[0] = CSI;
2007 string[1] = KS_MODIFIER;
2008 string[2] = modifiers;
2009 add_to_input_buf(string, 3);
2010 }
2011
2012 if (IS_SPECIAL(key))
2013 {
2014 string[0] = CSI;
2015 string[1] = K_SECOND(key);
2016 string[2] = K_THIRD(key);
2017 add_to_input_buf(string, 3);
2018 }
2019 else
2020 {
2021 int len;
2022
Bram Moolenaar734a8672019-12-02 22:49:38 +01002023 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002024 len = char_to_string(key, string, 40, FALSE);
2025 add_to_input_buf(string, len);
2026 }
2027 break;
2028 }
2029 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002030
2031 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002032 if (special_keys[i].key_sym == 0)
2033 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002034 WCHAR ch[8];
2035 int len;
2036 int i;
2037 UINT scan_code;
2038
2039 if (GetKeyState(VK_SHIFT) & 0x8000)
2040 modifiers |= MOD_MASK_SHIFT;
2041 if (GetKeyState(VK_CONTROL) & 0x8000)
2042 modifiers |= MOD_MASK_CTRL;
2043 if (GetKeyState(VK_LMENU) & 0x8000)
2044 modifiers |= MOD_MASK_ALT;
2045
2046 // Construct the state table with only a few modifiers, we don't
2047 // really care about the presence of Ctrl/Alt as those modifiers are
2048 // handled by Vim separately.
2049 memset(keyboard_state, 0, 256);
2050 if (GetKeyState(VK_SHIFT) & 0x8000)
2051 keyboard_state[VK_SHIFT] = 0x80;
2052 if (GetKeyState(VK_RMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002053 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002054 keyboard_state[VK_MENU] = 0x80;
2055 keyboard_state[VK_CONTROL] = 0x80;
2056 }
2057
2058 // Translate the virtual key according to the current keyboard
2059 // layout.
2060 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2061 // Convert the scan-code into a sequence of zero or more unicode
2062 // codepoints.
2063 // If this is a dead key ToUnicode returns a negative value.
2064 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2065 0);
2066 dead_key = len < 0;
2067
2068 if (len <= 0)
2069 return;
2070
2071 // Post the message as TranslateMessage would do.
2072 if (msg.message == WM_KEYDOWN)
2073 {
2074 for (i = 0; i < len; i++)
2075 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002076 }
2077 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002078 {
2079 for (i = 0; i < len; i++)
2080 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2081 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002082 }
2083 }
2084#ifdef FEAT_MBYTE_IME
2085 else if (msg.message == WM_IME_NOTIFY)
2086 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2087 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002088 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002089 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002090#endif
2091
2092#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002093 // Check for <F10>: Default effect is to select the menu. When <F10> is
2094 // mapped we need to stop it here to avoid strange effects (e.g., for the
2095 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002096 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2097 NULL, NULL) == NULL)
2098#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002099 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002100}
2101
2102/*
2103 * Catch up with any queued events. This may put keyboard input into the
2104 * input buffer, call resize call-backs, trigger timers etc. If there is
2105 * nothing in the event queue (& no timers pending), then we return
2106 * immediately.
2107 */
2108 void
2109gui_mch_update(void)
2110{
2111 MSG msg;
2112
2113 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002114 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002115 && !vim_is_input_buf_full())
2116 process_message();
2117}
2118
Bram Moolenaar4231da42016-06-02 14:30:04 +02002119 static void
2120remove_any_timer(void)
2121{
2122 MSG msg;
2123
2124 if (s_wait_timer != 0 && !s_timed_out)
2125 {
2126 KillTimer(NULL, s_wait_timer);
2127
Bram Moolenaar734a8672019-12-02 22:49:38 +01002128 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002129 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002130 ;
2131 s_wait_timer = 0;
2132 }
2133}
2134
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002135/*
2136 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2137 * from the keyboard.
2138 * wtime == -1 Wait forever.
2139 * wtime == 0 This should never happen.
2140 * wtime > 0 Wait wtime milliseconds for a character.
2141 * Returns OK if a character was found to be available within the given time,
2142 * or FAIL otherwise.
2143 */
2144 int
2145gui_mch_wait_for_chars(int wtime)
2146{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002147 int focus;
2148
2149 s_timed_out = FALSE;
2150
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002151 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002152 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002153 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002154 if (s_busy_processing)
2155 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002156
2157 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002158 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2159 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002160 }
2161
2162 allow_scrollbar = TRUE;
2163
2164 focus = gui.in_focus;
2165 while (!s_timed_out)
2166 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002167 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002168 if (gui.in_focus != focus)
2169 {
2170 if (gui.in_focus)
2171 gui_mch_start_blink();
2172 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002173 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002174 focus = gui.in_focus;
2175 }
2176
2177 if (s_need_activate)
2178 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002179 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002180 s_need_activate = FALSE;
2181 }
2182
Bram Moolenaar4231da42016-06-02 14:30:04 +02002183#ifdef FEAT_TIMERS
2184 did_add_timer = FALSE;
2185#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002186#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002187 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002188 for (;;)
2189 {
2190 MSG msg;
2191
2192 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002193# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002194 if (did_add_timer)
2195 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002196# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002197 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002198 {
2199 process_message();
2200 break;
2201 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002202 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002203 // TODO: The 10 msec is a compromise between laggy response
2204 // and consuming more CPU time. Better would be to handle
2205 // channel messages when they arrive.
2206 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002207 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002208 break;
2209 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002210#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002211 // Don't use gui_mch_update() because then we will spin-lock until a
2212 // char arrives, instead we use GetMessage() to hang until an
2213 // event arrives. No need to check for input_buf_full because we are
2214 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002215 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002216#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002217
2218 if (input_available())
2219 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002220 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002221 allow_scrollbar = FALSE;
2222
Bram Moolenaar89c00032019-09-04 13:53:21 +02002223 // Clear pending mouse button, the release event may have been
2224 // taken by the dialog window. But don't do this when getting
2225 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002226 if (!s_getting_focus)
2227 s_button_pending = -1;
2228
2229 return OK;
2230 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002231
2232#ifdef FEAT_TIMERS
2233 if (did_add_timer)
2234 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002235 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002236 remove_any_timer();
2237 break;
2238 }
2239#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002240 }
2241 allow_scrollbar = FALSE;
2242 return FAIL;
2243}
2244
2245/*
2246 * Clear a rectangular region of the screen from text pos (row1, col1) to
2247 * (row2, col2) inclusive.
2248 */
2249 void
2250gui_mch_clear_block(
2251 int row1,
2252 int col1,
2253 int row2,
2254 int col2)
2255{
2256 RECT rc;
2257
2258 /*
2259 * Clear one extra pixel at the far right, for when bold characters have
2260 * spilled over to the window border.
2261 * Note: FillRect() excludes right and bottom of rectangle.
2262 */
2263 rc.left = FILL_X(col1);
2264 rc.top = FILL_Y(row1);
2265 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2266 rc.bottom = FILL_Y(row2 + 1);
2267 clear_rect(&rc);
2268}
2269
2270/*
2271 * Clear the whole text window.
2272 */
2273 void
2274gui_mch_clear_all(void)
2275{
2276 RECT rc;
2277
2278 rc.left = 0;
2279 rc.top = 0;
2280 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2281 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2282 clear_rect(&rc);
2283}
2284/*
2285 * Menu stuff.
2286 */
2287
2288 void
2289gui_mch_enable_menu(int flag)
2290{
2291#ifdef FEAT_MENU
2292 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2293#endif
2294}
2295
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296 void
2297gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002298 int x UNUSED,
2299 int y UNUSED,
2300 int w UNUSED,
2301 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002303 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002304}
2305
2306#if defined(FEAT_MENU) || defined(PROTO)
2307/*
2308 * Make menu item hidden or not hidden
2309 */
2310 void
2311gui_mch_menu_hidden(
2312 vimmenu_T *menu,
2313 int hidden)
2314{
2315 /*
2316 * This doesn't do what we want. Hmm, just grey the menu items for now.
2317 */
2318 /*
2319 if (hidden)
2320 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2321 else
2322 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2323 */
2324 gui_mch_menu_grey(menu, hidden);
2325}
2326
2327/*
2328 * This is called after setting all the menus to grey/hidden or not.
2329 */
2330 void
2331gui_mch_draw_menubar(void)
2332{
2333 DrawMenuBar(s_hwnd);
2334}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002335#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002336
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337/*
2338 * Return the RGB value of a pixel as a long.
2339 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002340 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341gui_mch_get_rgb(guicolor_T pixel)
2342{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002343 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2344 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002345}
2346
2347#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002348/*
2349 * Convert pixels in X to dialog units
2350 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351 static WORD
2352PixelToDialogX(int numPixels)
2353{
2354 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2355}
2356
Bram Moolenaar734a8672019-12-02 22:49:38 +01002357/*
2358 * Convert pixels in Y to dialog units
2359 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002360 static WORD
2361PixelToDialogY(int numPixels)
2362{
2363 return (WORD)((numPixels * 8) / s_dlgfntheight);
2364}
2365
Bram Moolenaar734a8672019-12-02 22:49:38 +01002366/*
2367 * Return the width in pixels of the given text in the given DC.
2368 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 static int
2370GetTextWidth(HDC hdc, char_u *str, int len)
2371{
2372 SIZE size;
2373
2374 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2375 return size.cx;
2376}
2377
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378/*
2379 * Return the width in pixels of the given text in the given DC, taking care
2380 * of 'encoding' to active codepage conversion.
2381 */
2382 static int
2383GetTextWidthEnc(HDC hdc, char_u *str, int len)
2384{
2385 SIZE size;
2386 WCHAR *wstr;
2387 int n;
2388 int wlen = len;
2389
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002390 wstr = enc_to_utf16(str, &wlen);
2391 if (wstr == NULL)
2392 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002394 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2395 vim_free(wstr);
2396 if (n)
2397 return size.cx;
2398 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002401static void get_work_area(RECT *spi_rect);
2402
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002403/*
2404 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002405 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2406 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 */
2408 static BOOL
2409CenterWindow(
2410 HWND hwndChild,
2411 HWND hwndParent)
2412{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002413 HMONITOR mon;
2414 MONITORINFO moninfo;
2415 RECT rChild, rParent, rScreen;
2416 int wChild, hChild, wParent, hParent;
2417 int xNew, yNew;
2418 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419
2420 GetWindowRect(hwndChild, &rChild);
2421 wChild = rChild.right - rChild.left;
2422 hChild = rChild.bottom - rChild.top;
2423
Bram Moolenaar734a8672019-12-02 22:49:38 +01002424 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002425 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002426 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002427 else
2428 GetWindowRect(hwndParent, &rParent);
2429 wParent = rParent.right - rParent.left;
2430 hParent = rParent.bottom - rParent.top;
2431
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002432 moninfo.cbSize = sizeof(MONITORINFO);
2433 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2434 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002435 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002436 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002437 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002438 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002439 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002440 hdc = GetDC(hwndChild);
2441 rScreen.left = 0;
2442 rScreen.top = 0;
2443 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2444 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2445 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002446 }
2447
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002448 xNew = rParent.left + ((wParent - wChild) / 2);
2449 if (xNew < rScreen.left)
2450 xNew = rScreen.left;
2451 else if ((xNew + wChild) > rScreen.right)
2452 xNew = rScreen.right - wChild;
2453
2454 yNew = rParent.top + ((hParent - hChild) / 2);
2455 if (yNew < rScreen.top)
2456 yNew = rScreen.top;
2457 else if ((yNew + hChild) > rScreen.bottom)
2458 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002459
2460 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2461 SWP_NOSIZE | SWP_NOZORDER);
2462}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002463#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002464
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002465#if defined(FEAT_TOOLBAR) || defined(PROTO)
2466 void
2467gui_mch_show_toolbar(int showit)
2468{
2469 if (s_toolbarhwnd == NULL)
2470 return;
2471
2472 if (showit)
2473 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002474 // Enable unicode support
2475 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2476 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002477 ShowWindow(s_toolbarhwnd, SW_SHOW);
2478 }
2479 else
2480 ShowWindow(s_toolbarhwnd, SW_HIDE);
2481}
2482
Bram Moolenaar734a8672019-12-02 22:49:38 +01002483// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002484# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002485
2486#endif
2487
2488#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2489 static void
2490add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2491{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002492 WCHAR *wn;
2493 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002494
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002495 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002496 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002497 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002498
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002499 infow.cbSize = sizeof(infow);
2500 infow.fMask = MIIM_TYPE | MIIM_ID;
2501 infow.wID = item_id;
2502 infow.fType = MFT_STRING;
2503 infow.dwTypeData = wn;
2504 infow.cch = (UINT)wcslen(wn);
2505 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2506 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002507}
2508
2509 static void
2510show_tabline_popup_menu(void)
2511{
2512 HMENU tab_pmenu;
2513 long rval;
2514 POINT pt;
2515
Bram Moolenaar734a8672019-12-02 22:49:38 +01002516 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002517 if (hold_gui_events
2518# ifdef FEAT_CMDWIN
2519 || cmdwin_type != 0
2520# endif
2521 )
2522 return;
2523
2524 tab_pmenu = CreatePopupMenu();
2525 if (tab_pmenu == NULL)
2526 return;
2527
2528 if (first_tabpage->tp_next != NULL)
2529 add_tabline_popup_menu_entry(tab_pmenu,
2530 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2531 add_tabline_popup_menu_entry(tab_pmenu,
2532 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2533 add_tabline_popup_menu_entry(tab_pmenu,
2534 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2535
2536 GetCursorPos(&pt);
2537 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2538 NULL);
2539
2540 DestroyMenu(tab_pmenu);
2541
Bram Moolenaar734a8672019-12-02 22:49:38 +01002542 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002543 if (rval > 0)
2544 {
2545 TCHITTESTINFO htinfo;
2546 int idx;
2547
2548 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2549 return;
2550
2551 htinfo.pt.x = pt.x;
2552 htinfo.pt.y = pt.y;
2553 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2554 if (idx == -1)
2555 idx = 0;
2556 else
2557 idx += 1;
2558
2559 send_tabline_menu_event(idx, (int)rval);
2560 }
2561}
2562
2563/*
2564 * Show or hide the tabline.
2565 */
2566 void
2567gui_mch_show_tabline(int showit)
2568{
2569 if (s_tabhwnd == NULL)
2570 return;
2571
2572 if (!showit != !showing_tabline)
2573 {
2574 if (showit)
2575 ShowWindow(s_tabhwnd, SW_SHOW);
2576 else
2577 ShowWindow(s_tabhwnd, SW_HIDE);
2578 showing_tabline = showit;
2579 }
2580}
2581
2582/*
2583 * Return TRUE when tabline is displayed.
2584 */
2585 int
2586gui_mch_showing_tabline(void)
2587{
2588 return s_tabhwnd != NULL && showing_tabline;
2589}
2590
2591/*
2592 * Update the labels of the tabline.
2593 */
2594 void
2595gui_mch_update_tabline(void)
2596{
2597 tabpage_T *tp;
2598 TCITEM tie;
2599 int nr = 0;
2600 int curtabidx = 0;
2601 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002603
2604 if (s_tabhwnd == NULL)
2605 return;
2606
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002607 // Enable unicode support
2608 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609
2610 tie.mask = TCIF_TEXT;
2611 tie.iImage = -1;
2612
Bram Moolenaar734a8672019-12-02 22:49:38 +01002613 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2615
Bram Moolenaar734a8672019-12-02 22:49:38 +01002616 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2618 {
2619 if (tp == curtab)
2620 curtabidx = nr;
2621
2622 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2623 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002624 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625 tie.pszText = "-Empty-";
2626 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2627 tabadded = 1;
2628 }
2629
2630 get_tabline_label(tp, FALSE);
2631 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002633 wstr = enc_to_utf16(NameBuff, NULL);
2634 if (wstr != NULL)
2635 {
2636 TCITEMW tiw;
2637
2638 tiw.mask = TCIF_TEXT;
2639 tiw.iImage = -1;
2640 tiw.pszText = wstr;
2641 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2642 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002643 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002644 }
2645
Bram Moolenaar734a8672019-12-02 22:49:38 +01002646 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2648 TabCtrl_DeleteItem(s_tabhwnd, nr);
2649
2650 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2651 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2652
Bram Moolenaar734a8672019-12-02 22:49:38 +01002653 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002654 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2655 RedrawWindow(s_tabhwnd, NULL, NULL,
2656 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2657
2658 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2659 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2660}
2661
2662/*
2663 * Set the current tab to "nr". First tab is 1.
2664 */
2665 void
2666gui_mch_set_curtab(int nr)
2667{
2668 if (s_tabhwnd == NULL)
2669 return;
2670
2671 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2672 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2673}
2674
2675#endif
2676
2677/*
2678 * ":simalt" command.
2679 */
2680 void
2681ex_simalt(exarg_T *eap)
2682{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002683 char_u *keys = eap->arg;
2684 int fill_typebuf = FALSE;
2685 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002686
2687 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2688 while (*keys)
2689 {
2690 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002691 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002692 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2693 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002694 fill_typebuf = TRUE;
2695 }
2696 if (fill_typebuf)
2697 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002698 // Put a NOP in the typeahead buffer so that the message will get
2699 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002700 key_name[0] = K_SPECIAL;
2701 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002702 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002703 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002704#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002705 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002706#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002707 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708 }
2709}
2710
2711/*
2712 * Create the find & replace dialogs.
2713 * You can't have both at once: ":find" when replace is showing, destroys
2714 * the replace dialog first, and the other way around.
2715 */
2716#ifdef MSWIN_FIND_REPLACE
2717 static void
2718initialise_findrep(char_u *initial_string)
2719{
2720 int wword = FALSE;
2721 int mcase = !p_ic;
2722 char_u *entry_text;
2723
Bram Moolenaar734a8672019-12-02 22:49:38 +01002724 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002725 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2726
2727 s_findrep_struct.hwndOwner = s_hwnd;
2728 s_findrep_struct.Flags = FR_DOWN;
2729 if (mcase)
2730 s_findrep_struct.Flags |= FR_MATCHCASE;
2731 if (wword)
2732 s_findrep_struct.Flags |= FR_WHOLEWORD;
2733 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002734 {
2735 WCHAR *p = enc_to_utf16(entry_text, NULL);
2736 if (p != NULL)
2737 {
2738 int len = s_findrep_struct.wFindWhatLen - 1;
2739
2740 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2741 s_findrep_struct.lpstrFindWhat[len] = NUL;
2742 vim_free(p);
2743 }
2744 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 vim_free(entry_text);
2746}
2747#endif
2748
2749 static void
2750set_window_title(HWND hwnd, char *title)
2751{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002752 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002753 {
2754 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002755
Bram Moolenaar734a8672019-12-02 22:49:38 +01002756 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002757 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2758 if (wbuf != NULL)
2759 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002760 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002762 }
2763 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002764 else
2765 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002766}
2767
2768 void
2769gui_mch_find_dialog(exarg_T *eap)
2770{
2771#ifdef MSWIN_FIND_REPLACE
2772 if (s_findrep_msg != 0)
2773 {
2774 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2775 DestroyWindow(s_findrep_hwnd);
2776
2777 if (!IsWindow(s_findrep_hwnd))
2778 {
2779 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002780 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781 }
2782
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002783 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002784 (void)SetFocus(s_findrep_hwnd);
2785
2786 s_findrep_is_find = TRUE;
2787 }
2788#endif
2789}
2790
2791
2792 void
2793gui_mch_replace_dialog(exarg_T *eap)
2794{
2795#ifdef MSWIN_FIND_REPLACE
2796 if (s_findrep_msg != 0)
2797 {
2798 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2799 DestroyWindow(s_findrep_hwnd);
2800
2801 if (!IsWindow(s_findrep_hwnd))
2802 {
2803 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002804 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 }
2806
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002807 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002808 (void)SetFocus(s_findrep_hwnd);
2809
2810 s_findrep_is_find = FALSE;
2811 }
2812#endif
2813}
2814
2815
2816/*
2817 * Set visibility of the pointer.
2818 */
2819 void
2820gui_mch_mousehide(int hide)
2821{
2822 if (hide != gui.pointer_hidden)
2823 {
2824 ShowCursor(!hide);
2825 gui.pointer_hidden = hide;
2826 }
2827}
2828
2829#ifdef FEAT_MENU
2830 static void
2831gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2832{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002833 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002834 gui_mch_mousehide(FALSE);
2835
2836 (void)TrackPopupMenu(
2837 (HMENU)menu->submenu_id,
2838 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2839 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002840 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002841 s_hwnd,
2842 NULL);
2843 /*
2844 * NOTE: The pop-up menu can eat the mouse up event.
2845 * We deal with this in normal.c.
2846 */
2847}
2848#endif
2849
2850/*
2851 * Got a message when the system will go down.
2852 */
2853 static void
2854_OnEndSession(void)
2855{
2856 getout_preserve_modified(1);
2857}
2858
2859/*
2860 * Get this message when the user clicks on the cross in the top right corner
2861 * of a Windows95 window.
2862 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002863 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002864_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865{
2866 gui_shell_closed();
2867}
2868
2869/*
2870 * Get a message when the window is being destroyed.
2871 */
2872 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002873_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002874{
2875 if (!destroying)
2876 _OnClose(hwnd);
2877}
2878
2879 static void
2880_OnPaint(
2881 HWND hwnd)
2882{
2883 if (!IsMinimized(hwnd))
2884 {
2885 PAINTSTRUCT ps;
2886
Bram Moolenaar734a8672019-12-02 22:49:38 +01002887 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002888 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889
Bram Moolenaar734a8672019-12-02 22:49:38 +01002890 // prevent multi-byte characters from misprinting on an invalid
2891 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002892 if (has_mbyte)
2893 {
2894 RECT rect;
2895
2896 GetClientRect(hwnd, &rect);
2897 ps.rcPaint.left = rect.left;
2898 ps.rcPaint.right = rect.right;
2899 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900
2901 if (!IsRectEmpty(&ps.rcPaint))
2902 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2904 ps.rcPaint.right - ps.rcPaint.left + 1,
2905 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2906 }
2907
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002908 EndPaint(hwnd, &ps);
2909 }
2910}
2911
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002912 static void
2913_OnSize(
2914 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002915 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002916 int cx,
2917 int cy)
2918{
K.Takatac81e9bf2022-01-16 14:15:49 +00002919 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002920 {
2921 gui_resize_shell(cx, cy);
2922
Bram Moolenaar734a8672019-12-02 22:49:38 +01002923 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002925 }
2926}
2927
2928 static void
2929_OnSetFocus(
2930 HWND hwnd,
2931 HWND hwndOldFocus)
2932{
2933 gui_focus_change(TRUE);
2934 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00002935 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936}
2937
2938 static void
2939_OnKillFocus(
2940 HWND hwnd,
2941 HWND hwndNewFocus)
2942{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00002943 if (destroying)
2944 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002945 gui_focus_change(FALSE);
2946 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00002947 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002948}
2949
2950/*
2951 * Get a message when the user switches back to vim
2952 */
2953 static LRESULT
2954_OnActivateApp(
2955 HWND hwnd,
2956 BOOL fActivate,
2957 DWORD dwThreadId)
2958{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002959 // we call gui_focus_change() in _OnSetFocus()
2960 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00002961 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002962}
2963
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964 void
2965gui_mch_destroy_scrollbar(scrollbar_T *sb)
2966{
2967 DestroyWindow(sb->id);
2968}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002969
2970/*
2971 * Get current mouse coordinates in text window.
2972 */
2973 void
2974gui_mch_getmouse(int *x, int *y)
2975{
2976 RECT rct;
2977 POINT mp;
2978
2979 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00002980 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002981 *x = (int)(mp.x - rct.left);
2982 *y = (int)(mp.y - rct.top);
2983}
2984
2985/*
2986 * Move mouse pointer to character at (x, y).
2987 */
2988 void
2989gui_mch_setmouse(int x, int y)
2990{
2991 RECT rct;
2992
2993 (void)GetWindowRect(s_textArea, &rct);
2994 (void)SetCursorPos(x + gui.border_offset + rct.left,
2995 y + gui.border_offset + rct.top);
2996}
2997
2998 static void
2999gui_mswin_get_valid_dimensions(
3000 int w,
3001 int h,
3002 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003003 int *valid_h,
3004 int *cols,
3005 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003006{
3007 int base_width, base_height;
3008
3009 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003010 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3011 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003012 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003013 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3014 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3015 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3016 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003017 *cols = (w - base_width) / gui.char_width;
3018 *rows = (h - base_height) / gui.char_height;
3019 *valid_w = base_width + *cols * gui.char_width;
3020 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021}
3022
3023 void
3024gui_mch_flash(int msec)
3025{
3026 RECT rc;
3027
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003028#if defined(FEAT_DIRECTX)
3029 if (IS_ENABLE_DIRECTX())
3030 DWriteContext_Flush(s_dwc);
3031#endif
3032
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003033 /*
3034 * Note: InvertRect() excludes right and bottom of rectangle.
3035 */
3036 rc.left = 0;
3037 rc.top = 0;
3038 rc.right = gui.num_cols * gui.char_width;
3039 rc.bottom = gui.num_rows * gui.char_height;
3040 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003041 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003042
Bram Moolenaar734a8672019-12-02 22:49:38 +01003043 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044
3045 InvertRect(s_hdc, &rc);
3046}
3047
3048/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003049 * Check if the specified point is on-screen. (multi-monitor aware)
3050 */
3051 static BOOL
3052is_point_onscreen(int x, int y)
3053{
3054 POINT pt = {x, y};
3055
3056 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3057}
3058
3059/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003060 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003061 *
3062 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3063 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3064 * only works when the whole window is on-screen.
3065 */
3066 static BOOL
3067is_window_onscreen(HWND hwnd)
3068{
3069 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003070 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003071
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003072 GetClientRect(hwnd, &rc);
3073 p1.x = rc.left;
3074 p1.y = rc.top;
3075 p2.x = rc.right - 1;
3076 p2.y = rc.bottom - 1;
3077 ClientToScreen(hwnd, &p1);
3078 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003079
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003080 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003081 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003082 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003083 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003084 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003085 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003086 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003087 return FALSE;
3088 return TRUE;
3089}
3090
3091/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003092 * Return flags used for scrolling.
3093 * The SW_INVALIDATE is required when part of the window is covered or
3094 * off-screen. Refer to MS KB Q75236.
3095 */
3096 static int
3097get_scroll_flags(void)
3098{
3099 HWND hwnd;
3100 RECT rcVim, rcOther, rcDest;
3101
Bram Moolenaar185577e2020-10-29 20:08:21 +01003102 // Check if the window is (partly) off-screen.
3103 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003104 return SW_INVALIDATE;
3105
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003106 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003107 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003108 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3109 if (IsWindowVisible(hwnd))
3110 {
3111 GetWindowRect(hwnd, &rcOther);
3112 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3113 return SW_INVALIDATE;
3114 }
3115 return 0;
3116}
3117
3118/*
3119 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3120 * may not be scrolled out properly.
3121 * For gVim, when _OnScroll() is repeated, the character at the
3122 * previous cursor position may be left drawn after scroll.
3123 * The problem can be avoided by calling GetPixel() to get a pixel in
3124 * the region before ScrollWindowEx().
3125 */
3126 static void
3127intel_gpu_workaround(void)
3128{
3129 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3130}
3131
3132/*
3133 * Delete the given number of lines from the given row, scrolling up any
3134 * text further down within the scroll region.
3135 */
3136 void
3137gui_mch_delete_lines(
3138 int row,
3139 int num_lines)
3140{
3141 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003142
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003143 rc.left = FILL_X(gui.scroll_region_left);
3144 rc.right = FILL_X(gui.scroll_region_right + 1);
3145 rc.top = FILL_Y(row);
3146 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3147
Bram Moolenaar92467d32017-12-05 13:22:16 +01003148#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003149 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003150 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003151 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003152 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003153 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003154#endif
3155 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003156#if defined(FEAT_DIRECTX)
3157 if (IS_ENABLE_DIRECTX())
3158 DWriteContext_Flush(s_dwc);
3159#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160 intel_gpu_workaround();
3161 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 Moolenaar734a8672019-12-02 22:49:38 +01003166 // This seems to be required to avoid the cursor disappearing when
3167 // scrolling such that the cursor ends up in the top-left character on
3168 // the screen... But why? (Webb)
3169 // It's probably fixed by disabling drawing the cursor while scrolling.
3170 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171
3172 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3173 gui.scroll_region_left,
3174 gui.scroll_region_bot, gui.scroll_region_right);
3175}
3176
3177/*
3178 * Insert the given number of lines before the given row, scrolling down any
3179 * following text within the scroll region.
3180 */
3181 void
3182gui_mch_insert_lines(
3183 int row,
3184 int num_lines)
3185{
3186 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003187
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188 rc.left = FILL_X(gui.scroll_region_left);
3189 rc.right = FILL_X(gui.scroll_region_right + 1);
3190 rc.top = FILL_Y(row);
3191 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003192
3193#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003194 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003195 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003196 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003197 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003198 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003199#endif
3200 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003201#if defined(FEAT_DIRECTX)
3202 if (IS_ENABLE_DIRECTX())
3203 DWriteContext_Flush(s_dwc);
3204#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003205 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003206 // The SW_INVALIDATE is required when part of the window is covered or
3207 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003210 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003211 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213 gui_clear_block(row, gui.scroll_region_left,
3214 row + num_lines - 1, gui.scroll_region_right);
3215}
3216
3217
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003219gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220{
3221#if defined(FEAT_DIRECTX)
3222 DWriteContext_Close(s_dwc);
3223 DWrite_Final();
3224 s_dwc = NULL;
3225#endif
3226
3227 ReleaseDC(s_textArea, s_hdc);
3228 DeleteObject(s_brush);
3229
3230#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003231 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003232 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3233#endif
3234
Bram Moolenaar734a8672019-12-02 22:49:38 +01003235 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236 if (s_hwnd != NULL)
3237 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003238 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 DestroyWindow(s_hwnd);
3240 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003241}
3242
3243 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003244logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003245{
3246 char *p;
3247 char *res;
3248 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003249 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003250 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003251 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003253 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3254 if (font_name == NULL)
3255 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003257 quality_name = quality_id2name((int)lf.lfQuality);
3258
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003259 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003260 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003261 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262 if (res != NULL)
3263 {
3264 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003265 // make a normal font string out of the lf thing:
3266 points = pixels_to_points(
3267 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3268 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3269 sprintf((char *)p, "%s:h%d", font_name, points);
3270 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003271 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 while (*p)
3273 {
3274 if (*p == ' ')
3275 *p = '_';
3276 ++p;
3277 }
3278 if (lf.lfItalic)
3279 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003280 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003281 STRCAT(p, ":b");
3282 if (lf.lfUnderline)
3283 STRCAT(p, ":u");
3284 if (lf.lfStrikeOut)
3285 STRCAT(p, ":s");
3286 if (charset_name != NULL)
3287 {
3288 STRCAT(p, ":c");
3289 STRCAT(p, charset_name);
3290 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003291 if (quality_name != NULL)
3292 {
3293 STRCAT(p, ":q");
3294 STRCAT(p, quality_name);
3295 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296 }
3297
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003298 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003299 return (char_u *)res;
3300}
3301
3302
3303#ifdef FEAT_MBYTE_IME
3304/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003305 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003306 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307 */
3308 static void
3309update_im_font(void)
3310{
K.Takatac81e9bf2022-01-16 14:15:49 +00003311 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003312
3313 if (p_guifontwide != NULL && *p_guifontwide != NUL
3314 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003315 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003316 norm_logfont = lf_wide;
3317 else
3318 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003319
3320 lf = norm_logfont;
3321 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3322 // Work around when PerMonitorV2 is not enabled in the process level.
3323 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3324 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325}
3326#endif
3327
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328/*
3329 * Handler of gui.wide_font (p_guifontwide) changed notification.
3330 */
3331 void
3332gui_mch_wide_font_changed(void)
3333{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003334 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003335
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003336#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003338#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339
3340 gui_mch_free_font(gui.wide_ital_font);
3341 gui.wide_ital_font = NOFONT;
3342 gui_mch_free_font(gui.wide_bold_font);
3343 gui.wide_bold_font = NOFONT;
3344 gui_mch_free_font(gui.wide_boldital_font);
3345 gui.wide_boldital_font = NOFONT;
3346
3347 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003348 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003349 {
3350 if (!lf.lfItalic)
3351 {
3352 lf.lfItalic = TRUE;
3353 gui.wide_ital_font = get_font_handle(&lf);
3354 lf.lfItalic = FALSE;
3355 }
3356 if (lf.lfWeight < FW_BOLD)
3357 {
3358 lf.lfWeight = FW_BOLD;
3359 gui.wide_bold_font = get_font_handle(&lf);
3360 if (!lf.lfItalic)
3361 {
3362 lf.lfItalic = TRUE;
3363 gui.wide_boldital_font = get_font_handle(&lf);
3364 }
3365 }
3366 }
3367}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003368
3369/*
3370 * Initialise vim to use the font with the given name.
3371 * Return FAIL if the font could not be loaded, OK otherwise.
3372 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003373 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003374gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375{
K.Takatac81e9bf2022-01-16 14:15:49 +00003376 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 GuiFont font = NOFONT;
3378 char_u *p;
3379
Bram Moolenaar734a8672019-12-02 22:49:38 +01003380 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003381 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003382 {
3383 lfOrig = lf;
3384 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003386 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 if (font == NOFONT)
3388 return FAIL;
3389
3390 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003391 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003392#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393 norm_logfont = lf;
3394 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003395 if (!s_in_dpichanged)
3396 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397#endif
3398 gui_mch_free_font(gui.norm_font);
3399 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003400 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 GetFontSize(font);
3402
K.Takatac81e9bf2022-01-16 14:15:49 +00003403 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404 if (p != NULL)
3405 {
3406 hl_set_font_name(p);
3407
Bram Moolenaar734a8672019-12-02 22:49:38 +01003408 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003409 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3410 {
3411 vim_free(p_guifont);
3412 p_guifont = p;
3413 }
3414 else
3415 vim_free(p);
3416 }
3417
3418 gui_mch_free_font(gui.ital_font);
3419 gui.ital_font = NOFONT;
3420 gui_mch_free_font(gui.bold_font);
3421 gui.bold_font = NOFONT;
3422 gui_mch_free_font(gui.boldital_font);
3423 gui.boldital_font = NOFONT;
3424
3425 if (!lf.lfItalic)
3426 {
3427 lf.lfItalic = TRUE;
3428 gui.ital_font = get_font_handle(&lf);
3429 lf.lfItalic = FALSE;
3430 }
3431 if (lf.lfWeight < FW_BOLD)
3432 {
3433 lf.lfWeight = FW_BOLD;
3434 gui.bold_font = get_font_handle(&lf);
3435 if (!lf.lfItalic)
3436 {
3437 lf.lfItalic = TRUE;
3438 gui.boldital_font = get_font_handle(&lf);
3439 }
3440 }
3441
3442 return OK;
3443}
3444
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003445/*
3446 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003447 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003448 */
3449 int
3450gui_mch_maximized(void)
3451{
3452 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003453 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003454
3455 wp.length = sizeof(WINDOWPLACEMENT);
3456 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003457 {
3458 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003460 && wp.flags == WPF_RESTORETOMAXIMIZED))
3461 return TRUE;
3462 if (wp.showCmd == SW_SHOWMINIMIZED)
3463 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003465 // Assume the window is snapped when the sizes from two APIs differ.
3466 GetWindowRect(s_hwnd, &rc);
3467 if ((rc.right - rc.left !=
3468 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3469 || (rc.bottom - rc.top !=
3470 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3471 return TRUE;
3472 }
3473 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474}
3475
3476/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003477 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3478 * is set. Compute the new Rows and Columns. This is like resizing the
3479 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480 */
3481 void
3482gui_mch_newfont(void)
3483{
3484 RECT rect;
3485
3486 GetWindowRect(s_hwnd, &rect);
3487 if (win_socket_id == 0)
3488 {
3489 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003490 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3491 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003493 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3494 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3495 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3496 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497 }
3498 else
3499 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003500 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003502 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 }
3504}
3505
3506/*
3507 * Set the window title
3508 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003509 void
3510gui_mch_settitle(
3511 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003512 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513{
3514 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3515}
3516
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003517#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003518// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3519// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520static LPCSTR mshape_idcs[] =
3521{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003522 IDC_ARROW, // arrow
3523 MAKEINTRESOURCE(0), // blank
3524 IDC_IBEAM, // beam
3525 IDC_SIZENS, // updown
3526 IDC_SIZENS, // udsizing
3527 IDC_SIZEWE, // leftright
3528 IDC_SIZEWE, // lrsizing
3529 IDC_WAIT, // busy
3530 IDC_NO, // no
3531 IDC_ARROW, // crosshair
3532 IDC_ARROW, // hand1
3533 IDC_ARROW, // hand2
3534 IDC_ARROW, // pencil
3535 IDC_ARROW, // question
3536 IDC_ARROW, // right-arrow
3537 IDC_UPARROW, // up-arrow
3538 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539};
3540
3541 void
3542mch_set_mouse_shape(int shape)
3543{
3544 LPCSTR idc;
3545
3546 if (shape == MSHAPE_HIDE)
3547 ShowCursor(FALSE);
3548 else
3549 {
3550 if (shape >= MSHAPE_NUMBERED)
3551 idc = IDC_ARROW;
3552 else
3553 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003554 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555 if (!p_mh)
3556 {
3557 POINT mp;
3558
Bram Moolenaar734a8672019-12-02 22:49:38 +01003559 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003560 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561 (void)SetCursorPos(mp.x, mp.y);
3562 ShowCursor(TRUE);
3563 }
3564 }
3565}
3566#endif
3567
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003568#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003570 * Wide version of convert_filter().
3571 */
3572 static WCHAR *
3573convert_filterW(char_u *s)
3574{
3575 char_u *tmp;
3576 int len;
3577 WCHAR *res;
3578
3579 tmp = convert_filter(s);
3580 if (tmp == NULL)
3581 return NULL;
3582 len = (int)STRLEN(s) + 3;
3583 res = enc_to_utf16(tmp, &len);
3584 vim_free(tmp);
3585 return res;
3586}
3587
3588/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003589 * Pop open a file browser and return the file selected, in allocated memory,
3590 * or NULL if Cancel is hit.
3591 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3592 * title - Title message for the file browser dialog.
3593 * dflt - Default name of file.
3594 * ext - Default extension to be added to files without extensions.
3595 * initdir - directory in which to open the browser (NULL = current dir)
3596 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003597 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003598 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003599gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003600 int saving,
3601 char_u *title,
3602 char_u *dflt,
3603 char_u *ext,
3604 char_u *initdir,
3605 char_u *filter)
3606{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003607 // We always use the wide function. This means enc_to_utf16() must work,
3608 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003609 OPENFILENAMEW fileStruct;
3610 WCHAR fileBuf[MAXPATHL];
3611 WCHAR *wp;
3612 int i;
3613 WCHAR *titlep = NULL;
3614 WCHAR *extp = NULL;
3615 WCHAR *initdirp = NULL;
3616 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003617 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003618 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003619
3620 if (dflt == NULL)
3621 fileBuf[0] = NUL;
3622 else
3623 {
3624 wp = enc_to_utf16(dflt, NULL);
3625 if (wp == NULL)
3626 fileBuf[0] = NUL;
3627 else
3628 {
3629 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3630 fileBuf[i] = wp[i];
3631 fileBuf[i] = NUL;
3632 vim_free(wp);
3633 }
3634 }
3635
Bram Moolenaar734a8672019-12-02 22:49:38 +01003636 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003637 filterp = convert_filterW(filter);
3638
Bram Moolenaara80faa82020-04-12 19:37:17 +02003639 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003640# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003641 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003642 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003643# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003644 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003645# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003646
3647 if (title != NULL)
3648 titlep = enc_to_utf16(title, NULL);
3649 fileStruct.lpstrTitle = titlep;
3650
3651 if (ext != NULL)
3652 extp = enc_to_utf16(ext, NULL);
3653 fileStruct.lpstrDefExt = extp;
3654
3655 fileStruct.lpstrFile = fileBuf;
3656 fileStruct.nMaxFile = MAXPATHL;
3657 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003658 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3659 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660 if (initdir != NULL && *initdir != NUL)
3661 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003662 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663 initdirp = enc_to_utf16(initdir, NULL);
3664 if (initdirp != NULL)
3665 {
3666 for (wp = initdirp; *wp != NUL; ++wp)
3667 if (*wp == '/')
3668 *wp = '\\';
3669 }
3670 fileStruct.lpstrInitialDir = initdirp;
3671 }
3672
3673 /*
3674 * TODO: Allow selection of multiple files. Needs another arg to this
3675 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3676 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3677 * files that don't exist yet, so I haven't put it in. What about
3678 * OFN_PATHMUSTEXIST?
3679 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3680 */
3681 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003682# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683 if (curbuf->b_p_bin)
3684 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003685# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003687 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003689 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003690
3691 vim_free(filterp);
3692 vim_free(initdirp);
3693 vim_free(titlep);
3694 vim_free(extp);
3695
K.Takata14b8d6a2022-01-20 15:05:22 +00003696 if (!ret)
3697 return NULL;
3698
3699 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003701 if (p == NULL)
3702 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003703
Bram Moolenaar734a8672019-12-02 22:49:38 +01003704 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705 SetFocus(s_hwnd);
3706
Bram Moolenaar734a8672019-12-02 22:49:38 +01003707 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003708 q = vim_strsave(shorten_fname1(p));
3709 vim_free(p);
3710 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003711}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712
3713
3714/*
3715 * Convert the string s to the proper format for a filter string by replacing
3716 * the \t and \n delimiters with \0.
3717 * Returns the converted string in allocated memory.
3718 *
3719 * Keep in sync with convert_filterW() above!
3720 */
3721 static char_u *
3722convert_filter(char_u *s)
3723{
3724 char_u *res;
3725 unsigned s_len = (unsigned)STRLEN(s);
3726 unsigned i;
3727
3728 res = alloc(s_len + 3);
3729 if (res != NULL)
3730 {
3731 for (i = 0; i < s_len; ++i)
3732 if (s[i] == '\t' || s[i] == '\n')
3733 res[i] = '\0';
3734 else
3735 res[i] = s[i];
3736 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003737 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738 res[s_len + 1] = NUL;
3739 res[s_len + 2] = NUL;
3740 }
3741 return res;
3742}
3743
3744/*
3745 * Select a directory.
3746 */
3747 char_u *
3748gui_mch_browsedir(char_u *title, char_u *initdir)
3749{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003750 // We fake this: Use a filter that doesn't select anything and a default
3751 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003752 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3753 initdir, (char_u *)_("Directory\t*.nothing\n"));
3754}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003755#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 static void
3758_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003759 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 HDROP hDrop)
3761{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003762#define BUFPATHLEN _MAX_PATH
3763#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003765 char szFile[BUFPATHLEN];
3766 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3767 UINT i;
3768 char_u **fnames;
3769 POINT pt;
3770 int_u modifiers = 0;
3771
Bram Moolenaar734a8672019-12-02 22:49:38 +01003772 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 DragQueryPoint(hDrop, &pt);
3774 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3775
3776 reset_VIsual();
3777
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003778 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779
3780 if (fnames != NULL)
3781 for (i = 0; i < cFiles; ++i)
3782 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3784 fnames[i] = utf16_to_enc(wszFile, NULL);
3785 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 {
3787 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3788 fnames[i] = vim_strsave((char_u *)szFile);
3789 }
3790 }
3791
3792 DragFinish(hDrop);
3793
3794 if (fnames != NULL)
3795 {
3796 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3797 modifiers |= MOUSE_SHIFT;
3798 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3799 modifiers |= MOUSE_CTRL;
3800 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3801 modifiers |= MOUSE_ALT;
3802
3803 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3804
3805 s_need_activate = TRUE;
3806 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807}
3808
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 static int
3810_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003811 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 HWND hwndCtl,
3813 UINT code,
3814 int pos)
3815{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003816 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 scrollbar_T *sb, *sb_info;
3818 long val;
3819 int dragging = FALSE;
3820 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 SCROLLINFO si;
3822
3823 si.cbSize = sizeof(si);
3824 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003825
3826 sb = gui_mswin_find_scrollbar(hwndCtl);
3827 if (sb == NULL)
3828 return 0;
3829
Bram Moolenaar734a8672019-12-02 22:49:38 +01003830 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003831 {
3832 /*
3833 * Careful: need to get scrollbar info out of first (left) scrollbar
3834 * for window, but keep real scrollbar too because we must pass it to
3835 * gui_drag_scrollbar().
3836 */
3837 sb_info = &sb->wp->w_scrollbars[0];
3838 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003839 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003840 sb_info = sb;
3841 val = sb_info->value;
3842
3843 switch (code)
3844 {
3845 case SB_THUMBTRACK:
3846 val = pos;
3847 dragging = TRUE;
3848 if (sb->scroll_shift > 0)
3849 val <<= sb->scroll_shift;
3850 break;
3851 case SB_LINEDOWN:
3852 val++;
3853 break;
3854 case SB_LINEUP:
3855 val--;
3856 break;
3857 case SB_PAGEDOWN:
3858 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3859 break;
3860 case SB_PAGEUP:
3861 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3862 break;
3863 case SB_TOP:
3864 val = 0;
3865 break;
3866 case SB_BOTTOM:
3867 val = sb_info->max;
3868 break;
3869 case SB_ENDSCROLL:
3870 if (prev_code == SB_THUMBTRACK)
3871 {
3872 /*
3873 * "pos" only gives us 16-bit data. In case of large file,
3874 * use GetScrollPos() which returns 32-bit. Unfortunately it
3875 * is not valid while the scrollbar is being dragged.
3876 */
3877 val = GetScrollPos(hwndCtl, SB_CTL);
3878 if (sb->scroll_shift > 0)
3879 val <<= sb->scroll_shift;
3880 }
3881 break;
3882
3883 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003884 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003885 return 0;
3886 }
3887 prev_code = code;
3888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003889 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3890 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003891
3892 /*
3893 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3894 */
3895 if (sb->wp != NULL)
3896 {
3897 scrollbar_T *sba = sb->wp->w_scrollbars;
3898 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3899
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003900 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003901 }
3902
Bram Moolenaar734a8672019-12-02 22:49:38 +01003903 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003904 s_busy_processing = TRUE;
3905
Bram Moolenaar734a8672019-12-02 22:49:38 +01003906 // When "allow_scrollbar" is FALSE still need to remember the new
3907 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 dont_scroll = !allow_scrollbar;
3909
Bram Moolenaara338adc2018-01-31 20:51:47 +01003910 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003911 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003912 mch_enable_flush();
3913 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914
3915 s_busy_processing = FALSE;
3916 dont_scroll = dont_scroll_save;
3917
3918 return 0;
3919}
3920
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922#ifdef FEAT_XPM_W32
3923# include "xpm_w32.h"
3924#endif
3925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar734a8672019-12-02 22:49:38 +01003927// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#define TEAROFF_PADDING_X 2
3929#define TEAROFF_BUTTON_PAD_X 8
3930#define TEAROFF_MIN_WIDTH 200
3931#define TEAROFF_SUBMENU_LABEL ">>"
3932#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3933
3934
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003935#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936# define ID_BEVAL_TOOLTIP 200
3937# define BEVAL_TEXT_LEN MAXPATHL
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00003940static UINT_PTR beval_timer_id = 0;
3941static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003942#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00003943
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003944
Bram Moolenaar734a8672019-12-02 22:49:38 +01003945// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946
3947#ifdef FEAT_MENU
3948static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02003949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951/*
3952 * Use the system font for dialogs and tear-off menus. Remove this line to
3953 * use DLG_FONT_NAME.
3954 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02003955#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#define VIM_CLASSW L"Vim"
3959
Bram Moolenaar734a8672019-12-02 22:49:38 +01003960// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
3961// thus there should be room for every dialog. For tearoffs it's made bigger
3962// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963#define DLG_ALLOC_SIZE 16 * 1024
3964
3965/*
3966 * stuff for dialogs, menus, tearoffs etc.
3967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968static PWORD
3969add_dialog_element(
3970 PWORD p,
3971 DWORD lStyle,
3972 WORD x,
3973 WORD y,
3974 WORD w,
3975 WORD h,
3976 WORD Id,
3977 WORD clss,
3978 const char *caption);
3979static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02003980static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003981#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01003983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984static void get_dialog_font_metrics(void);
3985
3986static int dialog_default_button = -1;
3987
Bram Moolenaar734a8672019-12-02 22:49:38 +01003988// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991#ifdef FEAT_TOOLBAR
3992static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00003993static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02003994static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00003996#else
3997# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#endif
3999
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004000#ifdef FEAT_GUI_TABLINE
4001static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004002static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004003#endif
4004
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#ifdef FEAT_MBYTE_IME
4006static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4007static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4008#endif
4009#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4010# ifdef NOIME
4011typedef struct tagCOMPOSITIONFORM {
4012 DWORD dwStyle;
4013 POINT ptCurrentPos;
4014 RECT rcArea;
4015} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4016typedef HANDLE HIMC;
4017# endif
4018
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004019static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004020static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4021static HIMC (WINAPI *pImmGetContext)(HWND);
4022static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4023static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4024static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4025static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004026static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4027static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004028static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4029static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004030static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031static void dyn_imm_load(void);
4032#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033# define pImmGetCompositionStringW ImmGetCompositionStringW
4034# define pImmGetContext ImmGetContext
4035# define pImmAssociateContext ImmAssociateContext
4036# define pImmReleaseContext ImmReleaseContext
4037# define pImmGetOpenStatus ImmGetOpenStatus
4038# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004039# define pImmGetCompositionFontW ImmGetCompositionFontW
4040# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041# define pImmSetCompositionWindow ImmSetCompositionWindow
4042# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004043# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#endif
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#ifdef FEAT_MENU
4047/*
4048 * Figure out how high the menu bar is at the moment.
4049 */
4050 static int
4051gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004052 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053{
4054 static int old_menu_height = -1;
4055
4056 RECT rc1, rc2;
4057 int num;
4058 int menu_height;
4059
4060 if (gui.menu_is_active)
4061 num = GetMenuItemCount(s_menuBar);
4062 else
4063 num = 0;
4064
4065 if (num == 0)
4066 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004067 else if (IsMinimized(s_hwnd))
4068 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004069 // The height of the menu cannot be determined while the window is
4070 // minimized. Take the previous height if the menu is changed in that
4071 // state, to avoid that Vim's vertical window size accidentally
4072 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004073 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 else
4076 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004077 /*
4078 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4079 * seem to have been set yet, so menu wraps in default window
4080 * width which is very narrow. Instead just return height of a
4081 * single menu item. Will still be wrong when the menu really
4082 * should wrap over more than one line.
4083 */
4084 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4085 if (gui.starting)
4086 menu_height = rc1.bottom - rc1.top + 1;
4087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004089 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4090 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092 }
4093
4094 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004095 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004096 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 return menu_height;
4099}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004100#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
4102
4103/*
4104 * Setup for the Intellimouse
4105 */
4106 static void
4107init_mouse_wheel(void)
4108{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004109 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
Bram Moolenaar734a8672019-12-02 22:49:38 +01004111 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004112 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4113 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115
4116
Bram Moolenaarae20f342019-11-05 21:09:23 +01004117/*
4118 * Intellimouse wheel handler.
4119 * Treat a mouse wheel event as if it were a scroll request.
4120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 static void
4122_OnMouseWheel(
4123 HWND hwnd,
4124 short zDelta)
4125{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 int i;
4127 int size;
4128 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004129 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (mouse_scroll_lines == 0)
4132 init_mouse_wheel();
4133
Bram Moolenaarae20f342019-11-05 21:09:23 +01004134 wp = gui_mouse_window(FIND_POPUP);
4135
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004136#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004137 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004138 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004139 cmdarg_T cap;
4140 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004141
Bram Moolenaarae20f342019-11-05 21:09:23 +01004142 // Mouse hovers over popup window, scroll it if possible.
4143 mouse_row = wp->w_winrow;
4144 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004145 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004146 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4147 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4148 clear_oparg(&oa);
4149 cap.oap = &oa;
4150 nv_mousescroll(&cap);
4151 update_screen(0);
4152 setcursor();
4153 out_flush();
4154 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004155 }
4156#endif
4157
Bram Moolenaarae20f342019-11-05 21:09:23 +01004158 if (wp == NULL || !p_scf)
4159 wp = curwin;
4160
4161 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4162 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4163 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4164 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4165 else
4166 return;
4167 size = wp->w_height;
4168
Bram Moolenaara338adc2018-01-31 20:51:47 +01004169 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (mouse_scroll_lines > 0
4171 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4172 {
4173 for (i = mouse_scroll_lines; i > 0; --i)
4174 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4175 }
4176 else
4177 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004178 mch_enable_flush();
4179 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180}
4181
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004182#ifdef USE_SYSMENU_FONT
4183/*
4184 * Get Menu Font.
4185 * Return OK or FAIL.
4186 */
4187 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004188gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004189{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004190 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004191
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004192 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4193 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004194 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004195 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004196 &nm,
4197 0))
4198 return FAIL;
4199 *lf = nm.lfMenuFont;
4200 return OK;
4201}
4202#endif
4203
4204
4205#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4206/*
4207 * Set the GUI tabline font to the system menu font
4208 */
4209 static void
4210set_tabline_font(void)
4211{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004212 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004213 HFONT font;
4214 HWND hwnd;
4215 HDC hdc;
4216 HFONT hfntOld;
4217 TEXTMETRIC tm;
4218
4219 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4220 return;
4221
K.Takatac81e9bf2022-01-16 14:15:49 +00004222 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004223 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004224
4225 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4226
4227 /*
4228 * Compute the height of the font used for the tab text
4229 */
4230 hwnd = GetDesktopWindow();
4231 hdc = GetWindowDC(hwnd);
4232 hfntOld = SelectFont(hdc, font);
4233
4234 GetTextMetrics(hdc, &tm);
4235
4236 SelectFont(hdc, hfntOld);
4237 ReleaseDC(hwnd, hdc);
4238
4239 /*
4240 * The space used by the tab border and the space between the tab label
4241 * and the tab border is included as 7.
4242 */
4243 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4244}
K.Takatac81e9bf2022-01-16 14:15:49 +00004245#else
4246# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004247#endif
4248
Bram Moolenaar520470a2005-06-16 21:59:56 +00004249/*
4250 * Invoked when a setting was changed.
4251 */
4252 static LRESULT CALLBACK
4253_OnSettingChange(UINT n)
4254{
4255 if (n == SPI_SETWHEELSCROLLLINES)
4256 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4257 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004258 if (n == SPI_SETNONCLIENTMETRICS)
4259 set_tabline_font();
Bram Moolenaar520470a2005-06-16 21:59:56 +00004260 return 0;
4261}
4262
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef FEAT_NETBEANS_INTG
4264 static void
4265_OnWindowPosChanged(
4266 HWND hwnd,
4267 const LPWINDOWPOS lpwpos)
4268{
4269 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004270 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4273 || lpwpos->cx != cx || lpwpos->cy != cy))
4274 {
4275 x = lpwpos->x;
4276 y = lpwpos->y;
4277 cx = lpwpos->cx;
4278 cy = lpwpos->cy;
4279 netbeans_frame_moved(x, y);
4280 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004281 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004282 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283}
4284#endif
4285
K.Takata4f2417f2021-06-05 16:25:32 +02004286
4287static HWND hwndTip = NULL;
4288
4289 static void
4290show_sizing_tip(int cols, int rows)
4291{
4292 TOOLINFOA ti = {sizeof(ti)};
4293 char buf[32];
4294
4295 ti.hwnd = s_hwnd;
4296 ti.uId = (UINT_PTR)s_hwnd;
4297 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4298 ti.lpszText = buf;
4299 sprintf(buf, "%dx%d", cols, rows);
4300 if (hwndTip == NULL)
4301 {
4302 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4303 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4304 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4305 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4306 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4307 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4308 }
4309 else
4310 {
4311 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4312 }
4313 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4314}
4315
4316 static void
4317destroy_sizing_tip(void)
4318{
4319 if (hwndTip != NULL)
4320 {
4321 DestroyWindow(hwndTip);
4322 hwndTip = NULL;
4323 }
4324}
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 static int
4327_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 UINT fwSide,
4329 LPRECT lprc)
4330{
4331 int w, h;
4332 int valid_w, valid_h;
4333 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004334 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
4336 w = lprc->right - lprc->left;
4337 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004338 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 w_offset = w - valid_w;
4340 h_offset = h - valid_h;
4341
4342 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4343 || fwSide == WMSZ_BOTTOMLEFT)
4344 lprc->left += w_offset;
4345 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4346 || fwSide == WMSZ_BOTTOMRIGHT)
4347 lprc->right -= w_offset;
4348
4349 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4350 || fwSide == WMSZ_TOPRIGHT)
4351 lprc->top += h_offset;
4352 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4353 || fwSide == WMSZ_BOTTOMRIGHT)
4354 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004355
4356 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 return TRUE;
4358}
4359
K.Takata92000e22022-01-20 15:10:57 +00004360#ifdef FEAT_GUI_TABLINE
4361 static void
4362_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4363{
4364 if (gui_mch_showing_tabline())
4365 {
4366 POINT pt;
4367 RECT rect;
4368
4369 /*
4370 * If the cursor is on the tabline, display the tab menu
4371 */
4372 GetCursorPos(&pt);
4373 GetWindowRect(s_textArea, &rect);
4374 if (pt.y < rect.top)
4375 {
4376 show_tabline_popup_menu();
4377 return;
4378 }
4379 }
4380 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4381}
4382
4383 static void
4384_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4385{
4386 /*
4387 * If the user double clicked the tabline, create a new tab
4388 */
4389 if (gui_mch_showing_tabline())
4390 {
4391 POINT pt;
4392 RECT rect;
4393
4394 GetCursorPos(&pt);
4395 GetWindowRect(s_textArea, &rect);
4396 if (pt.y < rect.top)
4397 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4398 }
4399 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4400}
4401#endif
4402
4403 static UINT
4404_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4405{
4406 UINT result;
4407 int x, y;
4408
4409 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4410 if (result != HTCLIENT)
4411 return result;
4412
4413#ifdef FEAT_GUI_TABLINE
4414 if (gui_mch_showing_tabline())
4415 {
4416 RECT rct;
4417
4418 // If the cursor is on the GUI tabline, don't process this event
4419 GetWindowRect(s_textArea, &rct);
4420 if (yPos < rct.top)
4421 return result;
4422 }
4423#endif
4424 (void)gui_mch_get_winpos(&x, &y);
4425 xPos -= x;
4426
4427 if (xPos < 48) // <VN> TODO should use system metric?
4428 return HTBOTTOMLEFT;
4429 else
4430 return HTBOTTOMRIGHT;
4431}
4432
4433#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4434 static LRESULT
4435_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4436{
4437 switch (hdr->code)
4438 {
4439 case TTN_GETDISPINFOW:
4440 case TTN_GETDISPINFO:
4441 {
4442 char_u *str = NULL;
4443 static void *tt_text = NULL;
4444
4445 VIM_CLEAR(tt_text);
4446
4447# ifdef FEAT_GUI_TABLINE
4448 if (gui_mch_showing_tabline()
4449 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4450 {
4451 POINT pt;
4452 /*
4453 * Mouse is over the GUI tabline. Display the
4454 * tooltip for the tab under the cursor
4455 *
4456 * Get the cursor position within the tab control
4457 */
4458 GetCursorPos(&pt);
4459 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4460 {
4461 TCHITTESTINFO htinfo;
4462 int idx;
4463
4464 /*
4465 * Get the tab under the cursor
4466 */
4467 htinfo.pt.x = pt.x;
4468 htinfo.pt.y = pt.y;
4469 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4470 if (idx != -1)
4471 {
4472 tabpage_T *tp;
4473
4474 tp = find_tabpage(idx + 1);
4475 if (tp != NULL)
4476 {
4477 get_tabline_label(tp, TRUE);
4478 str = NameBuff;
4479 }
4480 }
4481 }
4482 }
4483# endif
4484# ifdef FEAT_TOOLBAR
4485# ifdef FEAT_GUI_TABLINE
4486 else
4487# endif
4488 {
4489 UINT idButton;
4490 vimmenu_T *pMenu;
4491
4492 idButton = (UINT) hdr->idFrom;
4493 pMenu = gui_mswin_find_menu(root_menu, idButton);
4494 if (pMenu)
4495 str = pMenu->strings[MENU_INDEX_TIP];
4496 }
4497# endif
4498 if (str == NULL)
4499 break;
4500
4501 // Set the maximum width, this also enables using \n for
4502 // line break.
4503 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4504
4505 if (hdr->code == TTN_GETDISPINFOW)
4506 {
4507 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4508
4509 tt_text = enc_to_utf16(str, NULL);
4510 lpdi->lpszText = tt_text;
4511 // can't show tooltip if failed
4512 }
4513 else
4514 {
4515 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4516
4517 if (STRLEN(str) < sizeof(lpdi->szText)
4518 || ((tt_text = vim_strsave(str)) == NULL))
4519 vim_strncpy((char_u *)lpdi->szText, str,
4520 sizeof(lpdi->szText) - 1);
4521 else
4522 lpdi->lpszText = tt_text;
4523 }
4524 }
4525 break;
4526
4527# ifdef FEAT_GUI_TABLINE
4528 case TCN_SELCHANGE:
4529 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4530 {
4531 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4532 return 0L;
4533 }
4534 break;
4535
4536 case NM_RCLICK:
4537 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4538 {
4539 show_tabline_popup_menu();
4540 return 0L;
4541 }
4542 break;
4543# endif
4544
4545 default:
4546 break;
4547 }
4548 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4549}
4550#endif
4551
4552#if defined(MENUHINTS) && defined(FEAT_MENU)
4553 static LRESULT
4554_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4555{
4556 if (((UINT) HIWORD(wParam)
4557 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4558 == MF_HILITE
4559 && (State & CMDLINE) == 0)
4560 {
4561 UINT idButton;
4562 vimmenu_T *pMenu;
4563 static int did_menu_tip = FALSE;
4564
4565 if (did_menu_tip)
4566 {
4567 msg_clr_cmdline();
4568 setcursor();
4569 out_flush();
4570 did_menu_tip = FALSE;
4571 }
4572
4573 idButton = (UINT)LOWORD(wParam);
4574 pMenu = gui_mswin_find_menu(root_menu, idButton);
4575 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4576 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4577 {
4578 ++msg_hist_off;
4579 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4580 --msg_hist_off;
4581 setcursor();
4582 out_flush();
4583 did_menu_tip = TRUE;
4584 }
4585 return 0L;
4586 }
4587 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4588}
4589#endif
4590
K.Takatac81e9bf2022-01-16 14:15:49 +00004591 static LRESULT
4592_OnDpiChanged(HWND hwnd, UINT xdpi, UINT ydpi, RECT *rc)
4593{
4594 s_dpi = ydpi;
4595 s_in_dpichanged = TRUE;
4596 //TRACE("DPI: %d", ydpi);
4597
4598 update_scrollbar_size();
4599 update_toolbar_size();
4600 set_tabline_font();
4601
4602 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4603 gui_get_wide_font();
4604 gui_mswin_get_menu_height(FALSE);
4605#ifdef FEAT_MBYTE_IME
4606 im_set_position(gui.row, gui.col);
4607#endif
4608 InvalidateRect(hwnd, NULL, TRUE);
4609
4610 s_in_dpichanged = FALSE;
4611 return 0L;
4612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
4614
4615 static LRESULT CALLBACK
4616_WndProc(
4617 HWND hwnd,
4618 UINT uMsg,
4619 WPARAM wParam,
4620 LPARAM lParam)
4621{
LemonBoy77fc0b02022-04-22 22:45:52 +01004622 // TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4623 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
4625 HandleMouseHide(uMsg, lParam);
4626
4627 s_uMsg = uMsg;
4628 s_wParam = wParam;
4629 s_lParam = lParam;
4630
4631 switch (uMsg)
4632 {
4633 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4634 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004635 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004637 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4639 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4640 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4641 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4642#ifdef FEAT_MENU
4643 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4644#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004645 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4646 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4648 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004649 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4650 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4652 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4653 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4654#ifdef FEAT_NETBEANS_INTG
4655 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4656#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004657#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004658 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4659 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004660#endif
K.Takata92000e22022-01-20 15:10:57 +00004661 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004662
Bram Moolenaar734a8672019-12-02 22:49:38 +01004663 case WM_QUERYENDSESSION: // System wants to go down.
4664 gui_shell_closed(); // Will exit when no changed buffers.
4665 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
4667 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004668 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004671 return 0L;
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 break;
4674
4675 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004676 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4677 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004678 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 return 0L;
4680
4681 case WM_SYSCHAR:
4682 /*
4683 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4684 * shortcut key, handle like a typed ALT key, otherwise call Windows
4685 * ALT key handling.
4686 */
4687#ifdef FEAT_MENU
4688 if ( !gui.menu_is_active
4689 || p_wak[0] == 'n'
4690 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4691 )
4692#endif
4693 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004694 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 return 0L;
4696 }
4697#ifdef FEAT_MENU
4698 else
K.Takata4ac893f2022-01-20 12:44:28 +00004699 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#endif
4701
4702 case WM_SYSKEYUP:
4703#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004704 // This used to be done only when menu is active: ALT key is used for
4705 // that. But that caused problems when menu is disabled and using
4706 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4707 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004708 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004710 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#endif
4712
K.Takata4f2417f2021-06-05 16:25:32 +02004713 case WM_EXITSIZEMOVE:
4714 destroy_sizing_tip();
4715 break;
4716
Bram Moolenaar734a8672019-12-02 22:49:38 +01004717 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 case WM_MOUSEWHEEL:
4721 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004722 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar734a8672019-12-02 22:49:38 +01004724 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004725 case WM_SETTINGCHANGE:
4726 return _OnSettingChange((UINT)wParam);
4727
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004728#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004730 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731#endif
K.Takata92000e22022-01-20 15:10:57 +00004732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#if defined(MENUHINTS) && defined(FEAT_MENU)
4734 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004735 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738#ifdef FEAT_MBYTE_IME
4739 case WM_IME_NOTIFY:
4740 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004741 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004742 return 1L;
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 case WM_IME_COMPOSITION:
4745 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004746 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004747 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004749 case WM_DPICHANGED:
4750 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4751 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004755 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#endif
K.Takata92000e22022-01-20 15:10:57 +00004758 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760
K.Takata4ac893f2022-01-20 12:44:28 +00004761 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762}
4763
4764/*
4765 * End of call-back routines
4766 */
4767
Bram Moolenaar734a8672019-12-02 22:49:38 +01004768// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769HWND vim_parent_hwnd = NULL;
4770
4771 static BOOL CALLBACK
4772FindWindowTitle(HWND hwnd, LPARAM lParam)
4773{
4774 char buf[2048];
4775 char *title = (char *)lParam;
4776
4777 if (GetWindowText(hwnd, buf, sizeof(buf)))
4778 {
4779 if (strstr(buf, title) != NULL)
4780 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004781 // Found it. Store the window ref. and quit searching if MDI
4782 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004784 if (vim_parent_hwnd != NULL)
4785 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004788 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789}
4790
4791/*
4792 * Invoked for '-P "title"' argument: search for parent application to open
4793 * our window in.
4794 */
4795 void
4796gui_mch_set_parent(char *title)
4797{
4798 EnumWindows(FindWindowTitle, (LPARAM)title);
4799 if (vim_parent_hwnd == NULL)
4800 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004801 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 mch_exit(2);
4803 }
4804}
4805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 static void
4808ole_error(char *arg)
4809{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004810 char buf[IOSIZE];
4811
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004812# ifdef VIMDLL
4813 gui.in_use = mch_is_gui_executable();
4814# endif
4815
Bram Moolenaar734a8672019-12-02 22:49:38 +01004816 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004817 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004818 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004819 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004823#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4824 static char *
4825gvim_error(void)
4826{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004827 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004828
4829 if (starting)
4830 {
4831 mch_errmsg(msg);
4832 mch_errmsg("\n");
4833 mch_exit(2);
4834 }
4835 return msg;
4836}
4837
4838 char *
4839gui_mch_do_spawn(char_u *arg)
4840{
4841 int len;
4842# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4843 char_u *session = NULL;
4844 LPWSTR tofree1 = NULL;
4845# endif
4846 WCHAR name[MAX_PATH];
4847 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4848 STARTUPINFOW si = {sizeof(si)};
4849 PROCESS_INFORMATION pi;
4850
4851 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4852 goto error;
4853 p = wcsrchr(name, L'\\');
4854 if (p == NULL)
4855 goto error;
4856 // Replace the executable name from vim(d).exe to gvim(d).exe.
4857# ifdef DEBUG
4858 wcscpy(p + 1, L"gvimd.exe");
4859# else
4860 wcscpy(p + 1, L"gvim.exe");
4861# endif
4862
4863# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4864 if (starting)
4865# endif
4866 {
4867 // Pass the command line to the new process.
4868 p = GetCommandLineW();
4869 // Skip 1st argument.
4870 while (*p && *p != L' ' && *p != L'\t')
4871 {
4872 if (*p == L'"')
4873 {
4874 while (*p && *p != L'"')
4875 ++p;
4876 if (*p)
4877 ++p;
4878 }
4879 else
4880 ++p;
4881 }
4882 cmd = p;
4883 }
4884# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4885 else
4886 {
4887 // Create a session file and pass it to the new process.
4888 LPWSTR wsession;
4889 char_u *savebg;
4890 int ret;
4891
4892 session = vim_tempname('s', FALSE);
4893 if (session == NULL)
4894 goto error;
4895 savebg = p_bg;
4896 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4897 ret = write_session_file(session);
4898 vim_free(p_bg);
4899 p_bg = savebg;
4900 if (!ret)
4901 goto error;
4902 wsession = enc_to_utf16(session, NULL);
4903 if (wsession == NULL)
4904 goto error;
4905 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004906 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004907 if (cmd == NULL)
4908 {
4909 vim_free(wsession);
4910 goto error;
4911 }
4912 tofree1 = cmd;
4913 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4914 wsession, wsession);
4915 vim_free(wsession);
4916 }
4917# endif
4918
4919 // Check additional arguments to the `:gui` command.
4920 if (arg != NULL)
4921 {
4922 warg = enc_to_utf16(arg, NULL);
4923 if (warg == NULL)
4924 goto error;
4925 tofree2 = warg;
4926 }
4927 else
4928 warg = L"";
4929
4930 // Set up the new command line.
4931 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004932 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004933 if (newcmd == NULL)
4934 goto error;
4935 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4936
4937 // Spawn a new GUI process.
4938 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4939 NULL, NULL, &si, &pi))
4940 goto error;
4941 CloseHandle(pi.hProcess);
4942 CloseHandle(pi.hThread);
4943 mch_exit(0);
4944
4945error:
4946# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4947 if (session)
4948 mch_remove(session);
4949 vim_free(session);
4950 vim_free(tofree1);
4951# endif
4952 vim_free(newcmd);
4953 vim_free(tofree2);
4954 return gvim_error();
4955}
4956#endif
4957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958/*
4959 * Parse the GUI related command-line arguments. Any arguments used are
4960 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02004961 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 */
4963 void
4964gui_mch_prepare(int *argc, char **argv)
4965{
4966 int silent = FALSE;
4967 int idx;
4968
Bram Moolenaar734a8672019-12-02 22:49:38 +01004969 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4971 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004972 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4974 && (argv[2][0] == '-' || argv[2][0] == '/'))
4975 {
4976 silent = TRUE;
4977 idx = 2;
4978 }
4979 else
4980 idx = 1;
4981
Bram Moolenaar734a8672019-12-02 22:49:38 +01004982 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (STRICMP(argv[idx] + 1, "register") == 0)
4984 {
4985#ifdef FEAT_OLE
4986 RegisterMe(silent);
4987 mch_exit(0);
4988#else
4989 if (!silent)
4990 ole_error("register");
4991 mch_exit(2);
4992#endif
4993 }
4994
Bram Moolenaar734a8672019-12-02 22:49:38 +01004995 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4997 {
4998#ifdef FEAT_OLE
4999 UnregisterMe(!silent);
5000 mch_exit(0);
5001#else
5002 if (!silent)
5003 ole_error("unregister");
5004 mch_exit(2);
5005#endif
5006 }
5007
Bram Moolenaar734a8672019-12-02 22:49:38 +01005008 // Ignore an -embedding argument. It is only relevant if the
5009 // application wants to treat the case when it is started manually
5010 // differently from the case where it is started via automation (and
5011 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5013 {
5014#ifdef FEAT_OLE
5015 *argc = 1;
5016#else
5017 ole_error("embedding");
5018 mch_exit(2);
5019#endif
5020 }
5021 }
5022
5023#ifdef FEAT_OLE
5024 {
5025 int bDoRestart = FALSE;
5026
5027 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005028 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (bDoRestart)
5030 mch_exit(0);
5031 }
5032#endif
5033
5034#ifdef FEAT_NETBEANS_INTG
5035 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005036 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 int arg;
5038
5039 for (arg = 1; arg < *argc; arg++)
5040 if (strncmp("-nb", argv[arg], 3) == 0)
5041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 netbeansArg = argv[arg];
5043 mch_memmove(&argv[arg], &argv[arg + 1],
5044 (--*argc - arg) * sizeof(char *));
5045 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005046 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050}
5051
K.Takatac81e9bf2022-01-16 14:15:49 +00005052 static void
5053load_dpi_func(void)
5054{
5055 HMODULE hUser32;
5056
5057 hUser32 = GetModuleHandle("user32.dll");
5058 if (hUser32 == NULL)
5059 goto fail;
5060
5061 pGetDpiForSystem = (void*)GetProcAddress(hUser32, "GetDpiForSystem");
5062 pGetDpiForWindow = (void*)GetProcAddress(hUser32, "GetDpiForWindow");
5063 pGetSystemMetricsForDpi = (void*)GetProcAddress(hUser32, "GetSystemMetricsForDpi");
5064 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
5065 pSetThreadDpiAwarenessContext = (void*)GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5066 pGetAwarenessFromDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
5067
5068 if (pSetThreadDpiAwarenessContext != NULL)
5069 {
5070 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5071 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5072 if (oldctx != NULL)
5073 {
5074 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5075 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5076#ifdef DEBUG
5077 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5078 {
5079 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5080 }
5081#endif
5082 return;
5083 }
5084 }
5085
5086fail:
5087 // Disable PerMonitorV2 APIs.
5088 pGetDpiForSystem = stubGetDpiForSystem;
5089 pGetDpiForWindow = NULL;
5090 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5091 pSetThreadDpiAwarenessContext = NULL;
5092 pGetAwarenessFromDpiAwarenessContext = NULL;
5093}
5094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095/*
5096 * Initialise the GUI. Create all the windows, set up all the call-backs
5097 * etc.
5098 */
5099 int
5100gui_mch_init(void)
5101{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005103 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
Bram Moolenaar734a8672019-12-02 22:49:38 +01005106 // Return here if the window was already opened (happens when
5107 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005109 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111 /*
5112 * Load the tearoff bitmap
5113 */
5114#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005115 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#endif
5117
K.Takatac81e9bf2022-01-16 14:15:49 +00005118 load_dpi_func();
5119
5120 s_dpi = pGetDpiForSystem();
5121 update_scrollbar_size();
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005124 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125#endif
5126 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005127#ifdef FEAT_TOOLBAR
5128 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
5131 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5132
Bram Moolenaar734a8672019-12-02 22:49:38 +01005133 // First try using the wide version, so that we can use any title.
5134 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005135 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005137 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 wndclassw.lpfnWndProc = _WndProc;
5139 wndclassw.cbClsExtra = 0;
5140 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005141 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5143 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5144 wndclassw.hbrBackground = s_brush;
5145 wndclassw.lpszMenuName = NULL;
5146 wndclassw.lpszClassName = szVimWndClassW;
5147
K.Takata4ac893f2022-01-20 12:44:28 +00005148 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (vim_parent_hwnd != NULL)
5153 {
5154#ifdef HAVE_TRY_EXCEPT
5155 __try
5156 {
5157#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005158 // Open inside the specified parent window.
5159 // TODO: last argument should point to a CLIENTCREATESTRUCT
5160 // structure.
5161 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005163 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005164 WS_OVERLAPPEDWINDOW | WS_CHILD
5165 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5167 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005168 100, // Any value will do
5169 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005171 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#ifdef HAVE_TRY_EXCEPT
5173 }
5174 __except(EXCEPTION_EXECUTE_HANDLER)
5175 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005176 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178#endif
5179 if (s_hwnd == NULL)
5180 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005181 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 mch_exit(2);
5183 }
5184 }
5185 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005186 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005187 // If the provided windowid is not valid reset it to zero, so that it
5188 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005189 if (IsWindow((HWND)win_socket_id) <= 0)
5190 win_socket_id = 0;
5191
Bram Moolenaar734a8672019-12-02 22:49:38 +01005192 // Create a window. If win_socket_id is not zero without border and
5193 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005194 s_hwnd = CreateWindowW(
5195 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005196 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5197 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005198 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5199 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005200 100, // Any value will do
5201 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005202 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005203 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005204 if (s_hwnd != NULL && win_socket_id != 0)
5205 {
5206 SetParent(s_hwnd, (HWND)win_socket_id);
5207 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5208 }
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 if (s_hwnd == NULL)
5212 return FAIL;
5213
K.Takatac81e9bf2022-01-16 14:15:49 +00005214 if (pGetDpiForWindow != NULL)
5215 {
5216 s_dpi = pGetDpiForWindow(s_hwnd);
5217 update_scrollbar_size();
5218 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5219 }
5220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5222 dyn_imm_load();
5223#endif
5224
Bram Moolenaar734a8672019-12-02 22:49:38 +01005225 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005226 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005227 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005228 wndclassw.style = CS_OWNDC;
5229 wndclassw.lpfnWndProc = _TextAreaWndProc;
5230 wndclassw.cbClsExtra = 0;
5231 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005232 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005233 wndclassw.hIcon = NULL;
5234 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5235 wndclassw.hbrBackground = NULL;
5236 wndclassw.lpszMenuName = NULL;
5237 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005238
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005239 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return FAIL;
5241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005243 s_textArea = CreateWindowExW(
5244 0,
5245 szTextAreaClassW, L"Vim text area",
5246 WS_CHILD | WS_VISIBLE, 0, 0,
5247 100, // Any value will do for now
5248 100, // Any value will do for now
5249 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005250 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (s_textArea == NULL)
5253 return FAIL;
5254
Bram Moolenaar20321902016-02-17 12:30:17 +01005255#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005256 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005257 {
5258 HANDLE hIcon = NULL;
5259
5260 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005261 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005262 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005263#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#ifdef FEAT_MENU
5266 s_menuBar = CreateMenu();
5267#endif
5268 s_hdc = GetDC(s_textArea);
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
Bram Moolenaar734a8672019-12-02 22:49:38 +01005272 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005273 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaar734a8672019-12-02 22:49:38 +01005275 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 gui_mch_def_colors();
5277
Bram Moolenaar734a8672019-12-02 22:49:38 +01005278 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5279 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 set_normal_colors();
5281
5282 /*
5283 * Check that none of the colors are the same as the background color.
5284 * Then store the current values as the defaults.
5285 */
5286 gui_check_colors();
5287 gui.def_norm_pixel = gui.norm_pixel;
5288 gui.def_back_pixel = gui.back_pixel;
5289
Bram Moolenaar734a8672019-12-02 22:49:38 +01005290 // Get the colors for the highlight groups (gui_check_colors() might have
5291 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 highlight_gui_started();
5293
5294 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005295 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005297 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 /*
5300 * Set up for Intellimouse processing
5301 */
5302 init_mouse_wheel();
5303
5304 /*
5305 * compute a couple of metrics used for the dialogs
5306 */
5307 get_dialog_font_metrics();
5308#ifdef FEAT_TOOLBAR
5309 /*
5310 * Create the toolbar
5311 */
5312 initialise_toolbar();
5313#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005314#ifdef FEAT_GUI_TABLINE
5315 /*
5316 * Create the tabline
5317 */
5318 initialise_tabline();
5319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#ifdef MSWIN_FIND_REPLACE
5321 /*
5322 * Initialise the dialog box stuff
5323 */
5324 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5325
Bram Moolenaar734a8672019-12-02 22:49:38 +01005326 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005328 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005330 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5332 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5333 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005336#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005337 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005338 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005339#endif
5340
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005341#ifdef FEAT_RENDER_OPTIONS
5342 if (p_rop)
5343 (void)gui_mch_set_rendering_options(p_rop);
5344#endif
5345
Bram Moolenaar748bf032005-02-02 23:04:36 +00005346theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005347 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005348 display_errors();
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return OK;
5351}
5352
5353/*
5354 * Get the size of the screen, taking position on multiple monitors into
5355 * account (if supported).
5356 */
5357 static void
5358get_work_area(RECT *spi_rect)
5359{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005360 HMONITOR mon;
5361 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
Bram Moolenaar734a8672019-12-02 22:49:38 +01005363 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005364 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005365 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005367 moninfo.cbSize = sizeof(MONITORINFO);
5368 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005370 *spi_rect = moninfo.rcWork;
5371 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005374 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5376}
5377
5378/*
5379 * Set the size of the window to the given width and height in pixels.
5380 */
5381 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005382gui_mch_set_shellsize(
5383 int width,
5384 int height,
5385 int min_width UNUSED,
5386 int min_height UNUSED,
5387 int base_width UNUSED,
5388 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005389 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005392 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaar734a8672019-12-02 22:49:38 +01005395 // Try to keep window completely on screen.
5396 // Get position of the screen work area. This is the part that is not
5397 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 get_work_area(&workarea_rect);
5399
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // Resizing a maximized window looks very strange, unzoom it first.
5401 // But don't do it when still starting up, it may have been requested in
5402 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005403 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005405
5406 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
Bram Moolenaar734a8672019-12-02 22:49:38 +01005408 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005409 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5410 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5411 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5412 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5413 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5414 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
Bram Moolenaar734a8672019-12-02 22:49:38 +01005416 // The following should take care of keeping Vim on the same monitor, no
5417 // matter if the secondary monitor is left or right of the primary
5418 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005419 window_rect.right = window_rect.left + win_width;
5420 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005421
Bram Moolenaar734a8672019-12-02 22:49:38 +01005422 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005423 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5424 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005426 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5427 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005429 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5430 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005432 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5433 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005435 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5436 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437
5438 SetActiveWindow(s_hwnd);
5439 SetFocus(s_hwnd);
5440
Bram Moolenaar734a8672019-12-02 22:49:38 +01005441 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443}
5444
5445
5446 void
5447gui_mch_set_scrollbar_thumb(
5448 scrollbar_T *sb,
5449 long val,
5450 long size,
5451 long max)
5452{
5453 SCROLLINFO info;
5454
5455 sb->scroll_shift = 0;
5456 while (max > 32767)
5457 {
5458 max = (max + 1) >> 1;
5459 val >>= 1;
5460 size >>= 1;
5461 ++sb->scroll_shift;
5462 }
5463
5464 if (sb->scroll_shift > 0)
5465 ++size;
5466
5467 info.cbSize = sizeof(info);
5468 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5469 info.nPos = val;
5470 info.nMin = 0;
5471 info.nMax = max;
5472 info.nPage = size;
5473 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5474}
5475
5476
5477/*
5478 * Set the current text font.
5479 */
5480 void
5481gui_mch_set_font(GuiFont font)
5482{
5483 gui.currFont = font;
5484}
5485
5486
5487/*
5488 * Set the current text foreground color.
5489 */
5490 void
5491gui_mch_set_fg_color(guicolor_T color)
5492{
5493 gui.currFgColor = color;
5494}
5495
5496/*
5497 * Set the current text background color.
5498 */
5499 void
5500gui_mch_set_bg_color(guicolor_T color)
5501{
5502 gui.currBgColor = color;
5503}
5504
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005505/*
5506 * Set the current text special color.
5507 */
5508 void
5509gui_mch_set_sp_color(guicolor_T color)
5510{
5511 gui.currSpColor = color;
5512}
5513
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005514#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515/*
5516 * Multi-byte handling, originally by Sung-Hoon Baek.
5517 * First static functions (no prototypes generated).
5518 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005519# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005520# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005521# endif
5522# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 * handle WM_IME_NOTIFY message
5526 */
5527 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005528_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 LRESULT lResult = 0;
5531 HIMC hImc;
5532
5533 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5534 return lResult;
5535 switch (dwCommand)
5536 {
5537 case IMN_SETOPENSTATUS:
5538 if (pImmGetOpenStatus(hImc))
5539 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005540 LOGFONTW lf = norm_logfont;
5541 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5542 // Work around when PerMonitorV2 is not enabled in the process level.
5543 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5544 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 im_set_position(gui.row, gui.col);
5546
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 State &= ~LANGMAP;
5549 if (State & INSERT)
5550 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005551# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005552 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5554 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005555 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 int old_row = gui.row;
5557 int old_col = gui.col;
5558
5559 // This must be called here before
5560 // status_redraw_curbuf(), otherwise the mode
5561 // message may appear in the wrong position.
5562 showmode();
5563 status_redraw_curbuf();
5564 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005565 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 gui.row = old_row;
5567 gui.col = old_col;
5568 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 }
5572 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005573 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 lResult = 0;
5575 break;
5576 }
5577 pImmReleaseContext(hWnd, hImc);
5578 return lResult;
5579}
5580
5581 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005582_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 char_u *ret;
5585 int len;
5586
Bram Moolenaar734a8672019-12-02 22:49:38 +01005587 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 return 0;
5589
5590 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5591 if (ret != NULL)
5592 {
5593 add_to_input_buf_csi(ret, len);
5594 vim_free(ret);
5595 return 1;
5596 }
5597 return 0;
5598}
5599
5600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 * void GetResultStr()
5602 *
5603 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5604 * get complete composition string
5605 */
5606 static char_u *
5607GetResultStr(HWND hwnd, int GCS, int *lenp)
5608{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005609 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005610 LONG ret;
5611 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 char_u *convbuf = NULL;
5613
5614 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5615 return NULL;
5616
K.Takatab0b2b732022-01-19 12:59:21 +00005617 // Get the length of the composition string.
5618 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5619 if (ret <= 0)
5620 return NULL;
5621
5622 // Allocate the requested buffer plus space for the NUL character.
5623 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 if (buf == NULL)
5625 return NULL;
5626
K.Takatab0b2b732022-01-19 12:59:21 +00005627 // Reads in the composition string.
5628 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5629 *lenp = ret / sizeof(WCHAR);
5630
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005631 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 pImmReleaseContext(hwnd, hIMC);
5633 vim_free(buf);
5634 return convbuf;
5635}
5636#endif
5637
Bram Moolenaar734a8672019-12-02 22:49:38 +01005638// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005639#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
5641/*
5642 * set font to IM.
5643 */
5644 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005645im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 HIMC hImc;
5648
5649 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5650 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005651 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 pImmReleaseContext(s_hwnd, hImc);
5653 }
5654}
5655
5656/*
5657 * Notify cursor position to IM.
5658 */
5659 void
5660im_set_position(int row, int col)
5661{
5662 HIMC hImc;
5663
5664 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5665 {
5666 COMPOSITIONFORM cfs;
5667
5668 cfs.dwStyle = CFS_POINT;
5669 cfs.ptCurrentPos.x = FILL_X(col);
5670 cfs.ptCurrentPos.y = FILL_Y(row);
5671 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005672 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5673 {
5674 // Work around when PerMonitorV2 is not enabled in the process level.
5675 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5676 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 pImmSetCompositionWindow(hImc, &cfs);
5679
5680 pImmReleaseContext(s_hwnd, hImc);
5681 }
5682}
5683
5684/*
5685 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5686 */
5687 void
5688im_set_active(int active)
5689{
5690 HIMC hImc;
5691 static HIMC hImcOld = (HIMC)0;
5692
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005693# ifdef VIMDLL
5694 if (!gui.in_use && !gui.starting)
5695 {
5696 mbyte_im_set_active(active);
5697 return;
5698 }
5699# endif
5700
Bram Moolenaar734a8672019-12-02 22:49:38 +01005701 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
5703 if (p_imdisable)
5704 {
5705 if (hImcOld == (HIMC)0)
5706 {
5707 hImcOld = pImmGetContext(s_hwnd);
5708 if (hImcOld)
5709 pImmAssociateContext(s_hwnd, (HIMC)0);
5710 }
5711 active = FALSE;
5712 }
5713 else if (hImcOld != (HIMC)0)
5714 {
5715 pImmAssociateContext(s_hwnd, hImcOld);
5716 hImcOld = (HIMC)0;
5717 }
5718
5719 hImc = pImmGetContext(s_hwnd);
5720 if (hImc)
5721 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005722 /*
5723 * for Korean ime
5724 */
5725 HKL hKL = GetKeyboardLayout(0);
5726
5727 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5728 {
5729 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5730 static BOOL bSaved = FALSE;
5731
5732 if (active)
5733 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005734 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005735 if (bSaved)
5736 pImmSetConversionStatus(hImc, dwConversionSaved,
5737 dwSentenceSaved);
5738 bSaved = FALSE;
5739 }
5740 else
5741 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005742 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005743 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5744 &dwSentenceSaved))
5745 {
5746 bSaved = TRUE;
5747 pImmSetConversionStatus(hImc,
5748 dwConversionSaved & ~(IME_CMODE_NATIVE
5749 | IME_CMODE_FULLSHAPE),
5750 dwSentenceSaved);
5751 }
5752 }
5753 }
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 pImmSetOpenStatus(hImc, active);
5756 pImmReleaseContext(s_hwnd, hImc);
5757 }
5758 }
5759}
5760
5761/*
5762 * Get IM status. When IM is on, return not 0. Else return 0.
5763 */
5764 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005765im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 int status = 0;
5768 HIMC hImc;
5769
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005770# ifdef VIMDLL
5771 if (!gui.in_use && !gui.starting)
5772 return mbyte_im_get_status();
5773# endif
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5776 {
5777 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5778 pImmReleaseContext(s_hwnd, hImc);
5779 }
5780 return status;
5781}
5782
Bram Moolenaar734a8672019-12-02 22:49:38 +01005783#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005786/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005787 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005788 */
5789 static void
5790latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5791{
5792 int c;
5793
Bram Moolenaarca003e12006-03-17 23:19:38 +00005794 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005795 {
5796 c = *text++;
5797 switch (c)
5798 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005799 case 0xa4: c = 0x20ac; break; // euro
5800 case 0xa6: c = 0x0160; break; // S hat
5801 case 0xa8: c = 0x0161; break; // S -hat
5802 case 0xb4: c = 0x017d; break; // Z hat
5803 case 0xb8: c = 0x017e; break; // Z -hat
5804 case 0xbc: c = 0x0152; break; // OE
5805 case 0xbd: c = 0x0153; break; // oe
5806 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005807 }
5808 *unicodebuf++ = c;
5809 }
5810}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
5812#ifdef FEAT_RIGHTLEFT
5813/*
5814 * What is this for? In the case where you are using Win98 or Win2K or later,
5815 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5816 * reverses the string sent to the TextOut... family. This sucks, because we
5817 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5818 * way to tell Windblows not to do this!
5819 *
5820 * The short of it is that this 'RevOut' only gets called if you are running
5821 * one of the new, "improved" MS OSes, and only if you are running in
5822 * 'rightleft' mode. It makes display take *slightly* longer, but not
5823 * noticeably so.
5824 */
5825 static void
K.Takata135e1522022-01-29 15:27:58 +00005826RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 int col,
5828 int row,
5829 UINT foptions,
5830 CONST RECT *pcliprect,
5831 LPCTSTR text,
5832 UINT len,
5833 CONST INT *padding)
5834{
5835 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005837 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005838 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005839 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840}
5841#endif
5842
Bram Moolenaar92467d32017-12-05 13:22:16 +01005843 static void
5844draw_line(
5845 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005846 int y1,
5847 int x2,
5848 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005849 COLORREF color)
5850{
5851#if defined(FEAT_DIRECTX)
5852 if (IS_ENABLE_DIRECTX())
5853 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5854 else
5855#endif
5856 {
5857 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5858 HPEN old_pen = SelectObject(s_hdc, hpen);
5859 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005860 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005861 LineTo(s_hdc, x2, y2);
5862 DeleteObject(SelectObject(s_hdc, old_pen));
5863 }
5864}
5865
5866 static void
5867set_pixel(
5868 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005869 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005870 COLORREF color)
5871{
5872#if defined(FEAT_DIRECTX)
5873 if (IS_ENABLE_DIRECTX())
5874 DWriteContext_SetPixel(s_dwc, x, y, color);
5875 else
5876#endif
5877 SetPixel(s_hdc, x, y, color);
5878}
5879
5880 static void
5881fill_rect(
5882 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005883 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005884 COLORREF color)
5885{
5886#if defined(FEAT_DIRECTX)
5887 if (IS_ENABLE_DIRECTX())
5888 DWriteContext_FillRect(s_dwc, rcp, color);
5889 else
5890#endif
5891 {
5892 HBRUSH hbr2;
5893
5894 if (hbr == NULL)
5895 hbr2 = CreateSolidBrush(color);
5896 else
5897 hbr2 = hbr;
5898 FillRect(s_hdc, rcp, hbr2);
5899 if (hbr == NULL)
5900 DeleteBrush(hbr2);
5901 }
5902}
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 void
5905gui_mch_draw_string(
5906 int row,
5907 int col,
5908 char_u *text,
5909 int len,
5910 int flags)
5911{
5912 static int *padding = NULL;
5913 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 const RECT *pcliprect = NULL;
5915 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 static WCHAR *unicodebuf = NULL;
5917 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005918 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 int y;
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 /*
5923 * Italic and bold text seems to have an extra row of pixels at the bottom
5924 * (below where the bottom of the character should be). If we draw the
5925 * characters with a solid background, the top row of pixels in the
5926 * character below will be overwritten. We can fix this by filling in the
5927 * background ourselves, to the correct character proportions, and then
5928 * writing the character in transparent mode. Still have a problem when
5929 * the character is "_", which gets written on to the character below.
5930 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5931 * pixel in their slots, which fixes the problem with the bottom row of
5932 * pixels. We still need this code because otherwise the top row of pixels
5933 * becomes a problem. - webb.
5934 */
5935 static HBRUSH hbr_cache[2] = {NULL, NULL};
5936 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5937 static int brush_lru = 0;
5938 HBRUSH hbr;
5939 RECT rc;
5940
5941 if (!(flags & DRAW_TRANSP))
5942 {
5943 /*
5944 * Clear background first.
5945 * Note: FillRect() excludes right and bottom of rectangle.
5946 */
5947 rc.left = FILL_X(col);
5948 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if (has_mbyte)
5950 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005951 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02005952 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 }
5954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 rc.right = FILL_X(col + len);
5956 rc.bottom = FILL_Y(row + 1);
5957
Bram Moolenaar734a8672019-12-02 22:49:38 +01005958 // Cache the created brush, that saves a lot of time. We need two:
5959 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (gui.currBgColor == brush_color[0])
5961 {
5962 hbr = hbr_cache[0];
5963 brush_lru = 1;
5964 }
5965 else if (gui.currBgColor == brush_color[1])
5966 {
5967 hbr = hbr_cache[1];
5968 brush_lru = 0;
5969 }
5970 else
5971 {
5972 if (hbr_cache[brush_lru] != NULL)
5973 DeleteBrush(hbr_cache[brush_lru]);
5974 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5975 brush_color[brush_lru] = gui.currBgColor;
5976 hbr = hbr_cache[brush_lru];
5977 brush_lru = !brush_lru;
5978 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005979
Bram Moolenaar92467d32017-12-05 13:22:16 +01005980 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
5982 SetBkMode(s_hdc, TRANSPARENT);
5983
5984 /*
5985 * When drawing block cursor, prevent inverted character spilling
5986 * over character cell (can happen with bold/italic)
5987 */
5988 if (flags & DRAW_CURSOR)
5989 {
5990 pcliprect = &rc;
5991 foptions = ETO_CLIPPED;
5992 }
5993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 SetTextColor(s_hdc, gui.currFgColor);
5995 SelectFont(s_hdc, gui.currFont);
5996
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005997#ifdef FEAT_DIRECTX
5998 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005999 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006000#endif
6001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6003 {
K.Takata135e1522022-01-29 15:27:58 +00006004 int i;
6005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 vim_free(padding);
6007 pad_size = Columns;
6008
Bram Moolenaar734a8672019-12-02 22:49:38 +01006009 // Don't give an out-of-memory message here, it would call us
6010 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006011 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (padding != NULL)
6013 for (i = 0; i < pad_size; i++)
6014 padding[i] = gui.char_width;
6015 }
6016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 /*
6018 * We have to provide the padding argument because italic and bold versions
6019 * of fixed-width fonts are often one pixel or so wider than their normal
6020 * versions.
6021 * No check for DRAW_BOLD, Windows will have done it already.
6022 */
6023
Bram Moolenaar734a8672019-12-02 22:49:38 +01006024 // Check if there are any UTF-8 characters. If not, use normal text
6025 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (enc_utf8)
6027 for (n = 0; n < len; ++n)
6028 if (text[n] >= 0x80)
6029 break;
6030
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006031#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006032 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6033 // required that unicode drawing routine, currently. So this forces it
6034 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006035 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006036 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006037#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006038
Bram Moolenaar734a8672019-12-02 22:49:38 +01006039 // Check if the Unicode buffer exists and is big enough. Create it
6040 // with the same length as the multi-byte string, the number of wide
6041 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006042 if ((enc_utf8
6043 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6044 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 && (unicodebuf == NULL || len > unibuflen))
6046 {
6047 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006048 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006051 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053 unibuflen = len;
6054 }
6055
6056 if (enc_utf8 && n < len && unicodebuf != NULL)
6057 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006058 // Output UTF-8 characters. Composing characters should be
6059 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006060 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006061 int wlen; // string length in words
6062 int clen; // string length in characters
6063 int cells; // cell width of string up to composing char
6064 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006065 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006067 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006068 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006070 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006072 c = utf_ptr2char(text + i);
6073 if (c >= 0x10000)
6074 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006075 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006076 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6077 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006078 }
6079 else
6080 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006081 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006082 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006083
6084 if (utf_iscomposing(c))
6085 cw = 0;
6086 else
6087 {
6088 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006089 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006090 cw = 1;
6091 }
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (unicodepdy != NULL)
6094 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006095 // Use unicodepdy to make characters fit as we expect, even
6096 // when the font uses different widths (e.g., bold character
6097 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006098 if (c >= 0x10000)
6099 {
6100 unicodepdy[wlen - 2] = cw * gui.char_width;
6101 unicodepdy[wlen - 1] = 0;
6102 }
6103 else
6104 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006107 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006108 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006110#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006111 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006112 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006113 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006114 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006115 TEXT_X(col), TEXT_Y(row),
6116 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006117 gui.char_width, gui.currFgColor,
6118 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006119 }
6120 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006121#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006122 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6123 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006124 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006126 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006128 // If we want to display codepage data, and the current CP is not the
6129 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (unicodebuf != NULL)
6131 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006132 if (enc_latin9)
6133 latin9_to_ucs(text, len, unicodebuf);
6134 else
6135 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 MB_PRECOMPOSED,
6137 (char *)text, len,
6138 (LPWSTR)unicodebuf, unibuflen);
6139 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006140 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006141 // Use unicodepdy to make characters fit as we expect, even
6142 // when the font uses different widths (e.g., bold character
6143 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006144 if (unicodepdy != NULL)
6145 {
6146 int i;
6147 int cw;
6148
6149 for (i = 0; i < len; ++i)
6150 {
6151 cw = utf_char2cells(unicodebuf[i]);
6152 if (cw > 2)
6153 cw = 1;
6154 unicodepdy[i] = cw * gui.char_width;
6155 }
6156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006158 foptions, pcliprect, unicodebuf, len, unicodepdy);
6159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161 }
6162 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006165 // Windows will mess up RL text, so we have to draw it character by
6166 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006167 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6169 foptions, pcliprect, (char *)text, len, padding);
6170 else
6171#endif
6172 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6173 foptions, pcliprect, (char *)text, len, padding);
6174 }
6175
Bram Moolenaar734a8672019-12-02 22:49:38 +01006176 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 if (flags & DRAW_UNDERL)
6178 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179 // When p_linespace is 0, overwrite the bottom row of pixels.
6180 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (p_linespace > 1)
6183 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006184 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006186
Bram Moolenaar734a8672019-12-02 22:49:38 +01006187 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006188 if (flags & DRAW_STRIKE)
6189 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006190 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006191 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006192 }
6193
Bram Moolenaar734a8672019-12-02 22:49:38 +01006194 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006195 if (flags & DRAW_UNDERC)
6196 {
6197 int x;
6198 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006199 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006200
6201 y = FILL_Y(row + 1) - 1;
6202 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6203 {
6204 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006205 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006206 }
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208}
6209
6210
6211/*
6212 * Output routines.
6213 */
6214
Bram Moolenaar734a8672019-12-02 22:49:38 +01006215/*
6216 * Flush any output to the screen
6217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 void
6219gui_mch_flush(void)
6220{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006221#if defined(FEAT_DIRECTX)
6222 if (IS_ENABLE_DIRECTX())
6223 DWriteContext_Flush(s_dwc);
6224#endif
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 GdiFlush();
6227}
6228
6229 static void
6230clear_rect(RECT *rcp)
6231{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006232 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233}
6234
6235
Bram Moolenaarc716c302006-01-21 22:12:51 +00006236 void
6237gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6238{
6239 RECT workarea_rect;
6240
6241 get_work_area(&workarea_rect);
6242
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006243 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006244 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6245 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006246
Bram Moolenaar734a8672019-12-02 22:49:38 +01006247 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6248 // the menubar for MSwin, we subtract it from the screen height, so that
6249 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006250 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006251 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6252 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6253 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6254 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006255}
6256
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258#if defined(FEAT_MENU) || defined(PROTO)
6259/*
6260 * Add a sub menu to the menu bar.
6261 */
6262 void
6263gui_mch_add_menu(
6264 vimmenu_T *menu,
6265 int pos)
6266{
6267 vimmenu_T *parent = menu->parent;
6268
6269 menu->submenu_id = CreatePopupMenu();
6270 menu->id = s_menu_id++;
6271
6272 if (menu_is_menubar(menu->name))
6273 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006274 WCHAR *wn;
6275 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006277 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006278 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006279 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006281 infow.cbSize = sizeof(infow);
6282 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6283 | MIIM_SUBMENU;
6284 infow.dwItemData = (long_u)menu;
6285 infow.wID = menu->id;
6286 infow.fType = MFT_STRING;
6287 infow.dwTypeData = wn;
6288 infow.cch = (UINT)wcslen(wn);
6289 infow.hSubMenu = menu->submenu_id;
6290 InsertMenuItemW((parent == NULL)
6291 ? s_menuBar : parent->submenu_id,
6292 (UINT)pos, TRUE, &infow);
6293 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 }
6295
Bram Moolenaar734a8672019-12-02 22:49:38 +01006296 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (parent == NULL)
6298 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006299# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 else if (IsWindow(parent->tearoff_handle))
6301 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303}
6304
6305 void
6306gui_mch_show_popupmenu(vimmenu_T *menu)
6307{
6308 POINT mp;
6309
K.Takata45f9cfb2022-01-21 11:11:00 +00006310 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6312}
6313
6314 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006315gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 vimmenu_T *menu = gui_find_menu(path_name);
6318
6319 if (menu != NULL)
6320 {
6321 POINT p;
6322
Bram Moolenaar734a8672019-12-02 22:49:38 +01006323 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006325 if (mouse_pos)
6326 {
6327 int mx, my;
6328
6329 gui_mch_getmouse(&mx, &my);
6330 p.x += mx;
6331 p.y += my;
6332 }
6333 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006335 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6337 }
6338 msg_scroll = FALSE;
6339 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6340 }
6341}
6342
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006343# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344/*
6345 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6346 * create it as a pseudo-"tearoff menu".
6347 */
6348 void
6349gui_make_tearoff(char_u *path_name)
6350{
6351 vimmenu_T *menu = gui_find_menu(path_name);
6352
Bram Moolenaar734a8672019-12-02 22:49:38 +01006353 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (menu != NULL)
6355 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6356}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
6359/*
6360 * Add a menu item to a menu
6361 */
6362 void
6363gui_mch_add_menu_item(
6364 vimmenu_T *menu,
6365 int idx)
6366{
6367 vimmenu_T *parent = menu->parent;
6368
6369 menu->id = s_menu_id++;
6370 menu->submenu_id = NULL;
6371
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006372# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6374 {
6375 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6376 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6377 }
6378 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006379# endif
6380# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (menu_is_toolbar(parent->name))
6382 {
6383 TBBUTTON newtb;
6384
Bram Moolenaara80faa82020-04-12 19:37:17 +02006385 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (menu_is_separator(menu->name))
6387 {
6388 newtb.iBitmap = 0;
6389 newtb.fsStyle = TBSTYLE_SEP;
6390 }
6391 else
6392 {
6393 newtb.iBitmap = get_toolbar_bitmap(menu);
6394 newtb.fsStyle = TBSTYLE_BUTTON;
6395 }
6396 newtb.idCommand = menu->id;
6397 newtb.fsState = TBSTATE_ENABLED;
6398 newtb.iString = 0;
6399 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6400 (LPARAM)&newtb);
6401 menu->submenu_id = (HMENU)-1;
6402 }
6403 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006406 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006408 wn = enc_to_utf16(menu->name, NULL);
6409 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006411 InsertMenuW(parent->submenu_id, (UINT)idx,
6412 (menu_is_separator(menu->name)
6413 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6414 (UINT)menu->id, wn);
6415 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006417# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (IsWindow(parent->tearoff_handle))
6419 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422}
6423
6424/*
6425 * Destroy the machine specific menu widget.
6426 */
6427 void
6428gui_mch_destroy_menu(vimmenu_T *menu)
6429{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006430# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 /*
6432 * is this a toolbar button?
6433 */
6434 if (menu->submenu_id == (HMENU)-1)
6435 {
6436 int iButton;
6437
6438 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6439 (WPARAM)menu->id, 0);
6440 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6441 }
6442 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
6445 if (menu->parent != NULL
6446 && menu_is_popup(menu->parent->dname)
6447 && menu->parent->submenu_id != NULL)
6448 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6449 else
6450 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6451 if (menu->submenu_id != NULL)
6452 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006453# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (IsWindow(menu->tearoff_handle))
6455 DestroyWindow(menu->tearoff_handle);
6456 if (menu->parent != NULL
6457 && menu->parent->children != NULL
6458 && IsWindow(menu->parent->tearoff_handle))
6459 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006460 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 menu->modes = 0;
6462 rebuild_tearoff(menu->parent);
6463 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
6466}
6467
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006468# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 static void
6470rebuild_tearoff(vimmenu_T *menu)
6471{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006472 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 char_u tbuf[128];
6474 RECT trect;
6475 RECT rct;
6476 RECT roct;
6477 int x, y;
6478
6479 HWND thwnd = menu->tearoff_handle;
6480
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006481 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (GetWindowRect(thwnd, &trect)
6483 && GetWindowRect(s_hwnd, &rct)
6484 && GetClientRect(s_hwnd, &roct))
6485 {
6486 x = trect.left - rct.left;
6487 y = (trect.top - rct.bottom + roct.bottom);
6488 }
6489 else
6490 {
6491 x = y = 0xffffL;
6492 }
6493 DestroyWindow(thwnd);
6494 if (menu->children != NULL)
6495 {
6496 gui_mch_tearoff(tbuf, menu, x, y);
6497 if (IsWindow(menu->tearoff_handle))
6498 (void) SetWindowPos(menu->tearoff_handle,
6499 NULL,
6500 (int)trect.left,
6501 (int)trect.top,
6502 0, 0,
6503 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6504 }
6505}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006506# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508/*
6509 * Make a menu either grey or not grey.
6510 */
6511 void
6512gui_mch_menu_grey(
6513 vimmenu_T *menu,
6514 int grey)
6515{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006516# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 /*
6518 * is this a toolbar button?
6519 */
6520 if (menu->submenu_id == (HMENU)-1)
6521 {
6522 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006523 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
6525 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006526# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006527 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6528 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006530# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6532 {
6533 WORD menuID;
6534 HWND menuHandle;
6535
6536 /*
6537 * A tearoff button has changed state.
6538 */
6539 if (menu->children == NULL)
6540 menuID = (WORD)(menu->id);
6541 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006542 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6544 if (menuHandle)
6545 EnableWindow(menuHandle, !grey);
6546
6547 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549}
6550
Bram Moolenaar734a8672019-12-02 22:49:38 +01006551#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552
6553
Bram Moolenaar734a8672019-12-02 22:49:38 +01006554// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006557#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6560/*
6561 * stuff for dialogs
6562 */
6563
6564/*
6565 * The callback routine used by all the dialogs. Very simple. First,
6566 * acknowledges the INITDIALOG message so that Windows knows to do standard
6567 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6568 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6569 * number.
6570 */
6571 static LRESULT CALLBACK
6572dialog_callback(
6573 HWND hwnd,
6574 UINT message,
6575 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006576 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 if (message == WM_INITDIALOG)
6579 {
6580 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006581 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 (void)SetFocus(hwnd);
6583 if (dialog_default_button > IDCANCEL)
6584 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006585 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006586 // We don't have a default, set focus on another element of the
6587 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006588 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 return FALSE;
6590 }
6591
6592 if (message == WM_COMMAND)
6593 {
6594 int button = LOWORD(wParam);
6595
Bram Moolenaar734a8672019-12-02 22:49:38 +01006596 // Don't end the dialog if something was selected that was
6597 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 if (button >= DLG_NONBUTTON_CONTROL)
6599 return TRUE;
6600
Bram Moolenaar734a8672019-12-02 22:49:38 +01006601 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006603 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006604 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006605 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006606
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006607 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6608 p = utf16_to_enc(wp, NULL);
6609 vim_strncpy(s_textfield, p, IOSIZE);
6610 vim_free(p);
6611 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614 /*
6615 * Need to check for IDOK because if the user just hits Return to
6616 * accept the default value, some reason this is what we get.
6617 */
6618 if (button == IDOK)
6619 {
6620 if (dialog_default_button > IDCANCEL)
6621 EndDialog(hwnd, dialog_default_button);
6622 }
6623 else
6624 EndDialog(hwnd, button - IDCANCEL);
6625 return TRUE;
6626 }
6627
6628 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6629 {
6630 EndDialog(hwnd, 0);
6631 return TRUE;
6632 }
6633 return FALSE;
6634}
6635
6636/*
6637 * Create a dialog dynamically from the parameter strings.
6638 * type = type of dialog (question, alert, etc.)
6639 * title = dialog title. may be NULL for default title.
6640 * message = text to display. Dialog sizes to accommodate it.
6641 * buttons = '\n' separated list of button captions, default first.
6642 * dfltbutton = number of default button.
6643 *
6644 * This routine returns 1 if the first button is pressed,
6645 * 2 for the second, etc.
6646 *
6647 * 0 indicates Esc was pressed.
6648 * -1 for unexpected error
6649 *
6650 * If stubbing out this fn, return 1.
6651 */
6652
Bram Moolenaar734a8672019-12-02 22:49:38 +01006653static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655 "IDR_VIM",
6656 "IDR_VIM_ERROR",
6657 "IDR_VIM_ALERT",
6658 "IDR_VIM_INFO",
6659 "IDR_VIM_QUESTION"
6660};
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 int
6663gui_mch_dialog(
6664 int type,
6665 char_u *title,
6666 char_u *message,
6667 char_u *buttons,
6668 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006669 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006670 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006673 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 int numButtons;
6675 int *buttonWidths, *buttonPositions;
6676 int buttonYpos;
6677 int nchar, i;
6678 DWORD lStyle;
6679 int dlgwidth = 0;
6680 int dlgheight;
6681 int editboxheight;
6682 int horizWidth = 0;
6683 int msgheight;
6684 char_u *pstart;
6685 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006686 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 char_u *tbuffer;
6688 RECT rect;
6689 HWND hwnd;
6690 HDC hdc;
6691 HFONT font, oldFont;
6692 TEXTMETRIC fontInfo;
6693 int fontHeight;
6694 int textWidth, minButtonWidth, messageWidth;
6695 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006696 int maxDialogHeight;
6697 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 int vertical;
6699 int dlgPaddingX;
6700 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006701# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006702 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006704# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006705 garray_T ga;
6706 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006707 int dlg_icon_width;
6708 int dlg_icon_height;
6709 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006711# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006712 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006713# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006714 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006715# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006716 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006717 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
Bram Moolenaar748bf032005-02-02 23:04:36 +00006720 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006721 {
6722 load_dpi_func();
6723 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006724 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006725 }
6726 else
6727 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
6729 if ((type < 0) || (type > VIM_LAST_TYPE))
6730 type = 0;
6731
Bram Moolenaar734a8672019-12-02 22:49:38 +01006732 // allocate some memory for dialog template
6733 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006734 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006735 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
6737 if (p == NULL)
6738 return -1;
6739
6740 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006741 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6743 * const.
6744 */
6745 tbuffer = vim_strsave(buttons);
6746 if (tbuffer == NULL)
6747 return -1;
6748
Bram Moolenaar734a8672019-12-02 22:49:38 +01006749 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar734a8672019-12-02 22:49:38 +01006751 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 numButtons = 1;
6753 for (i = 0; tbuffer[i] != '\0'; i++)
6754 {
6755 if (tbuffer[i] == DLG_BUTTON_SEP)
6756 numButtons++;
6757 }
6758 if (dfltbutton >= numButtons)
6759 dfltbutton = -1;
6760
Bram Moolenaar734a8672019-12-02 22:49:38 +01006761 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006762 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if (buttonWidths == NULL)
6764 return -1;
6765
Bram Moolenaar734a8672019-12-02 22:49:38 +01006766 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006767 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (buttonPositions == NULL)
6769 return -1;
6770
6771 /*
6772 * Calculate how big the dialog must be.
6773 */
6774 hwnd = GetDesktopWindow();
6775 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006776# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6778 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006779 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 use_lfSysmenu = TRUE;
6781 }
6782 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006783# endif
K.Takatad1c58992022-01-23 12:31:57 +00006784 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6785 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6786
6787 oldFont = SelectFont(hdc, font);
6788 dlgPaddingX = DLG_PADDING_X;
6789 dlgPaddingY = DLG_PADDING_Y;
6790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 GetTextMetrics(hdc, &fontInfo);
6792 fontHeight = fontInfo.tmHeight;
6793
Bram Moolenaar734a8672019-12-02 22:49:38 +01006794 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006795 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
Bram Moolenaar734a8672019-12-02 22:49:38 +01006797 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006798 if (s_hwnd == NULL)
6799 {
6800 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar734a8672019-12-02 22:49:38 +01006802 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006803 get_work_area(&workarea_rect);
6804 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006805 if (maxDialogWidth > adjust_by_system_dpi(600))
6806 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006807 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006808 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006809 }
6810 else
6811 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006812 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006813 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006814 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006815 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6816 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6817 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6818 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006819
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006820 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006821 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6822 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6823 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6824 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6825 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006826 }
6827
Bram Moolenaar734a8672019-12-02 22:49:38 +01006828 // Set dlgwidth to width of message.
6829 // Copy the message into "ga", changing NL to CR-NL and inserting line
6830 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 pstart = message;
6832 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006833 msgheight = 0;
6834 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 do
6836 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006837 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006838
Bram Moolenaar734a8672019-12-02 22:49:38 +01006839 // Need to figure out where to break the string. The system does it
6840 // at a word boundary, which would mean we can't compute the number of
6841 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006842 textWidth = 0;
6843 last_white = NULL;
6844 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006845 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006846 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006847 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006848 && textWidth > maxDialogWidth * 3 / 4)
6849 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006850 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006851 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006852 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006853 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006854 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006855 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006856 textWidth = 0;
6857
6858 if (last_white != NULL)
6859 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006861 if (pend > last_white)
6862 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006863 pend = last_white + 1;
6864 last_white = NULL;
6865 }
6866 ga_append(&ga, '\r');
6867 ga_append(&ga, '\n');
6868 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006869 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006870
6871 while (--l >= 0)
6872 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006873 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006874 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006876
6877 ga_append(&ga, '\r');
6878 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 pstart = pend + 1;
6880 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006881
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006882 if (ga.ga_data != NULL)
6883 message = ga.ga_data;
6884
Bram Moolenaar734a8672019-12-02 22:49:38 +01006885 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006886
K.Takatac81e9bf2022-01-16 14:15:49 +00006887 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
6888 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
K.Takatac81e9bf2022-01-16 14:15:49 +00006890 // Add width of icon to dlgwidth, and some space
6891 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
6892 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
6893
6894 if (msgheight < dlg_icon_height)
6895 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897 /*
6898 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006899 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006901 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (!vertical)
6903 {
6904 // Place buttons horizontally if they fit.
6905 horizWidth = dlgPaddingX;
6906 pstart = tbuffer;
6907 i = 0;
6908 do
6909 {
6910 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6911 if (pend == NULL)
6912 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006913 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (textWidth < minButtonWidth)
6915 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006916 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 buttonWidths[i] = textWidth;
6918 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006919 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 pstart = pend + 1;
6921 } while (*pend != NUL);
6922
6923 if (horizWidth > maxDialogWidth)
6924 vertical = TRUE; // Too wide to fit on the screen.
6925 else if (horizWidth > dlgwidth)
6926 dlgwidth = horizWidth;
6927 }
6928
6929 if (vertical)
6930 {
6931 // Stack buttons vertically.
6932 pstart = tbuffer;
6933 do
6934 {
6935 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6936 if (pend == NULL)
6937 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006938 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006939 textWidth += dlgPaddingX; // Padding within button
6940 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (textWidth > dlgwidth)
6942 dlgwidth = textWidth;
6943 pstart = pend + 1;
6944 } while (*pend != NUL);
6945 }
6946
6947 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006948 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar734a8672019-12-02 22:49:38 +01006950 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00006951 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953 add_long(lStyle);
6954 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006955 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 add_word(0); // NumberOfItems(will change later)
6957 add_word(10); // x
6958 add_word(10); // y
6959 add_word(PixelToDialogX(dlgwidth)); // cx
6960
6961 // Dialog height.
6962 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006963 dlgheight = msgheight + 2 * dlgPaddingY
6964 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 else
6966 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6967
6968 // Dialog needs to be taller if contains an edit box.
6969 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6970 if (textfield != NULL)
6971 dlgheight += editboxheight;
6972
Bram Moolenaar734a8672019-12-02 22:49:38 +01006973 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006974 if (dlgheight > maxDialogHeight)
6975 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006976 msgheight = msgheight - (dlgheight - maxDialogHeight);
6977 dlgheight = maxDialogHeight;
6978 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006979 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00006980 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006981 }
6982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 add_word(PixelToDialogY(dlgheight));
6984
6985 add_word(0); // Menu
6986 add_word(0); // Class
6987
Bram Moolenaar734a8672019-12-02 22:49:38 +01006988 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006989 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6990 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 p += nchar;
6992
K.Takatad1c58992022-01-23 12:31:57 +00006993 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006994# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00006995 if (use_lfSysmenu)
6996 {
6997 // point size
6998 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6999 GetDeviceCaps(hdc, LOGPIXELSY));
7000 wcscpy(p, lfSysmenu.lfFaceName);
7001 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
K.Takatad1c58992022-01-23 12:31:57 +00007003 else
7004# endif
7005 {
7006 *p++ = DLG_FONT_POINT_SIZE; // point size
7007 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7008 }
7009 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 buttonYpos = msgheight + 2 * dlgPaddingY;
7012
7013 if (textfield != NULL)
7014 buttonYpos += editboxheight;
7015
7016 pstart = tbuffer;
7017 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007018 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 for (i = 0; i < numButtons; i++)
7020 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007021 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 for ( pend = pstart;
7023 *pend && (*pend != DLG_BUTTON_SEP);
7024 pend++)
7025 ;
7026
7027 if (*pend)
7028 *pend = '\0';
7029
7030 /*
7031 * old NOTE:
7032 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7033 * the focus to the first tab-able button and in so doing makes that
7034 * the default!! Grrr. Workaround: Make the default button the only
7035 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7036 * he/she can use arrow keys.
7037 *
7038 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007039 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 * dialog. Also needed for when the textfield is the default control.
7041 * It appears to work now (perhaps not on Win95?).
7042 */
7043 if (vertical)
7044 {
7045 p = add_dialog_element(p,
7046 (i == dfltbutton
7047 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7048 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007049 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 + 2 * fontHeight * i),
7051 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7052 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007053 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055 else
7056 {
7057 p = add_dialog_element(p,
7058 (i == dfltbutton
7059 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7060 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007061 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 PixelToDialogX(buttonWidths[i]),
7063 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007064 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007066 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068 *pnumitems += numButtons;
7069
Bram Moolenaar734a8672019-12-02 22:49:38 +01007070 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 p = add_dialog_element(p, SS_ICON,
7072 PixelToDialogX(dlgPaddingX),
7073 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007074 PixelToDialogX(dlg_icon_width),
7075 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7077 dlg_icons[type]);
7078
Bram Moolenaar734a8672019-12-02 22:49:38 +01007079 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007080 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007081 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007082 PixelToDialogY(dlgPaddingY),
7083 (WORD)(PixelToDialogX(messageWidth) + 1),
7084 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007085 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
Bram Moolenaar734a8672019-12-02 22:49:38 +01007087 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (textfield != NULL)
7089 {
7090 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7091 PixelToDialogX(2 * dlgPaddingX),
7092 PixelToDialogY(2 * dlgPaddingY + msgheight),
7093 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7094 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007095 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 *pnumitems += 1;
7097 }
7098
7099 *pnumitems += 2;
7100
7101 SelectFont(hdc, oldFont);
7102 DeleteObject(font);
7103 ReleaseDC(hwnd, hdc);
7104
Bram Moolenaar734a8672019-12-02 22:49:38 +01007105 // Let the dialog_callback() function know which button to make default
7106 // If we have an edit box, make that the default. We also need to tell
7107 // dialog_callback() if this dialog contains an edit box or not. We do
7108 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 if (textfield != NULL)
7110 {
7111 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7112 s_textfield = textfield;
7113 }
7114 else
7115 {
7116 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7117 s_textfield = NULL;
7118 }
7119
Bram Moolenaar734a8672019-12-02 22:49:38 +01007120 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007122 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 (LPDLGTEMPLATE)pdlgtemplate,
7124 s_hwnd,
7125 (DLGPROC)dialog_callback);
7126
7127 LocalFree(LocalHandle(pdlgtemplate));
7128 vim_free(tbuffer);
7129 vim_free(buttonWidths);
7130 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007131 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaar734a8672019-12-02 22:49:38 +01007133 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 (void)SetFocus(s_hwnd);
7135
7136 return nchar;
7137}
7138
Bram Moolenaar734a8672019-12-02 22:49:38 +01007139#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141/*
7142 * Put a simple element (basic class) onto a dialog template in memory.
7143 * return a pointer to where the next item should be added.
7144 *
7145 * parameters:
7146 * lStyle = additional style flags
7147 * (be careful, NT3.51 & Win32s will ignore the new ones)
7148 * x,y = x & y positions IN DIALOG UNITS
7149 * w,h = width and height IN DIALOG UNITS
7150 * Id = ID used in messages
7151 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7152 * caption = usually text or resource name
7153 *
7154 * TODO: use the length information noted here to enable the dialog creation
7155 * routines to work out more exactly how much memory they need to alloc.
7156 */
7157 static PWORD
7158add_dialog_element(
7159 PWORD p,
7160 DWORD lStyle,
7161 WORD x,
7162 WORD y,
7163 WORD w,
7164 WORD h,
7165 WORD Id,
7166 WORD clss,
7167 const char *caption)
7168{
7169 int nchar;
7170
Bram Moolenaar734a8672019-12-02 22:49:38 +01007171 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7173 *p++ = LOWORD(lStyle);
7174 *p++ = HIWORD(lStyle);
7175 *p++ = 0; // LOWORD (lExtendedStyle)
7176 *p++ = 0; // HIWORD (lExtendedStyle)
7177 *p++ = x;
7178 *p++ = y;
7179 *p++ = w;
7180 *p++ = h;
7181 *p++ = Id; //9 or 10 words in all
7182
7183 *p++ = (WORD)0xffff;
7184 *p++ = clss; //2 more here
7185
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007186 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 p += nchar;
7188
7189 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7190
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007191 return p; // total = 15 + strlen(caption) words
7192 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195
7196/*
7197 * Helper routine. Take an input pointer, return closest pointer that is
7198 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7199 */
7200 static LPWORD
7201lpwAlign(
7202 LPWORD lpIn)
7203{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007204 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007206 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 ul += 3;
7208 ul >>= 2;
7209 ul <<= 2;
7210 return (LPWORD)ul;
7211}
7212
7213/*
7214 * Helper routine. Takes second parameter as Ansi string, copies it to first
7215 * parameter as wide character (16-bits / char) string, and returns integer
7216 * number of wide characters (words) in string (including the trailing wide
7217 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007218 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7219 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 static int
7221nCopyAnsiToWideChar(
7222 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007223 LPSTR lpAnsiIn,
7224 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007227 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 int i;
7229 WCHAR *wn;
7230
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007231 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007233 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007234 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 if (wn != NULL)
7236 {
7237 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007238 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 vim_free(wn);
7240 }
7241 }
7242 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007243 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 nChar = MultiByteToWideChar(
7245 enc_codepage > 0 ? enc_codepage : CP_ACP,
7246 MB_PRECOMPOSED,
7247 lpAnsiIn, len,
7248 lpWCStr, len);
7249 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007250 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 return nChar;
7254}
7255
7256
7257#ifdef FEAT_TEAROFF
7258/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007259 * Lookup menu handle from "menu_id".
7260 */
7261 static HMENU
7262tearoff_lookup_menuhandle(
7263 vimmenu_T *menu,
7264 WORD menu_id)
7265{
7266 for ( ; menu != NULL; menu = menu->next)
7267 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007268 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007269 continue;
7270 if (menu_is_separator(menu->dname))
7271 continue;
7272 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7273 return menu->submenu_id;
7274 }
7275 return NULL;
7276}
7277
7278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 * The callback function for all the modeless dialogs that make up the
7280 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7281 * thinking its menus have been clicked), and go away when closed.
7282 */
7283 static LRESULT CALLBACK
7284tearoff_callback(
7285 HWND hwnd,
7286 UINT message,
7287 WPARAM wParam,
7288 LPARAM lParam)
7289{
7290 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007291 {
7292 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
Bram Moolenaar734a8672019-12-02 22:49:38 +01007296 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 HandleMouseHide(message, lParam);
7298
7299 if (message == WM_COMMAND)
7300 {
7301 if ((WORD)(LOWORD(wParam)) & 0x8000)
7302 {
7303 POINT mp;
7304 RECT rect;
7305
7306 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7307 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007308 vimmenu_T *menu;
7309
7310 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007312 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7314 (int)rect.right - 8,
7315 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007316 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 s_hwnd,
7318 NULL);
7319 /*
7320 * NOTE: The pop-up menu can eat the mouse up event.
7321 * We deal with this in normal.c.
7322 */
7323 }
7324 }
7325 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007326 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7328 /*
7329 * Give main window the focus back: this is so after
7330 * choosing a tearoff button you can start typing again
7331 * straight away.
7332 */
7333 (void)SetFocus(s_hwnd);
7334 return TRUE;
7335 }
7336 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7337 {
7338 DestroyWindow(hwnd);
7339 return TRUE;
7340 }
7341
Bram Moolenaar734a8672019-12-02 22:49:38 +01007342 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (message == WM_EXITSIZEMOVE)
7344 (void)SetActiveWindow(s_hwnd);
7345
7346 return FALSE;
7347}
7348#endif
7349
7350
7351/*
K.Takatad1c58992022-01-23 12:31:57 +00007352 * Computes the dialog base units based on the current dialog font.
7353 * We don't use the GetDialogBaseUnits() API, because we don't use the
7354 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
7356 static void
7357get_dialog_font_metrics(void)
7358{
7359 HDC hdc;
7360 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 SIZE size;
7362#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007363 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364#endif
7365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007367 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007368 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007369 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370#endif
7371 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007372 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
K.Takatad1c58992022-01-23 12:31:57 +00007374 hdc = GetDC(s_hwnd);
7375 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007376 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007377 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378
K.Takataabe628e2022-01-23 16:25:17 +00007379 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007380 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7384/*
7385 * Create a pseudo-"tearoff menu" based on the child
7386 * items of a given menu pointer.
7387 */
7388 static void
7389gui_mch_tearoff(
7390 char_u *title,
7391 vimmenu_T *menu,
7392 int initX,
7393 int initY)
7394{
7395 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7396 int template_len;
7397 int nchar, textWidth, submenuWidth;
7398 DWORD lStyle;
7399 DWORD lExtendedStyle;
7400 WORD dlgwidth;
7401 WORD menuID;
7402 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007403 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 vimmenu_T *the_menu = menu;
7405 HWND hwnd;
7406 HDC hdc;
7407 HFONT font, oldFont;
7408 int col, spaceWidth, len;
7409 int columnWidths[2];
7410 char_u *label, *text;
7411 int acLen = 0;
7412 int nameLen;
7413 int padding0, padding1, padding2 = 0;
7414 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007415 int x;
7416 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007417# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007418 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
7422 /*
7423 * If this menu is already torn off, move it to the mouse position.
7424 */
7425 if (IsWindow(menu->tearoff_handle))
7426 {
7427 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007428 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
7430 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7431 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7432 }
7433 return;
7434 }
7435
7436 /*
7437 * Create a new tearoff.
7438 */
7439 if (*title == MNU_HIDDEN_CHAR)
7440 title++;
7441
Bram Moolenaar734a8672019-12-02 22:49:38 +01007442 // Allocate memory to store the dialog template. It's made bigger when
7443 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 template_len = DLG_ALLOC_SIZE;
7445 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7446 if (p == NULL)
7447 return;
7448
7449 hwnd = GetDesktopWindow();
7450 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007451# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7453 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007454 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 use_lfSysmenu = TRUE;
7456 }
7457 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007458# endif
K.Takatad1c58992022-01-23 12:31:57 +00007459 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7460 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7461
7462 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
Bram Moolenaar734a8672019-12-02 22:49:38 +01007464 // Calculate width of a single space. Used for padding columns to the
7465 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007466 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar734a8672019-12-02 22:49:38 +01007468 // Figure out max width of the text column, the accelerator column and the
7469 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 submenuWidth = 0;
7471 for (col = 0; col < 2; col++)
7472 {
7473 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007474 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007476 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 text = (col == 0) ? pmenu->dname : pmenu->actext;
7478 if (text != NULL && *text != NUL)
7479 {
7480 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7481 if (textWidth > columnWidths[col])
7482 columnWidths[col] = textWidth;
7483 }
7484 if (pmenu->children != NULL)
7485 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7486 }
7487 }
7488 if (columnWidths[1] == 0)
7489 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007490 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (submenuWidth != 0)
7492 columnWidths[0] += submenuWidth;
7493 else
7494 columnWidths[0] += spaceWidth;
7495 }
7496 else
7497 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007498 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7500 columnWidths[1] += submenuWidth;
7501 }
7502
7503 /*
7504 * Now find the total width of our 'menu'.
7505 */
7506 textWidth = columnWidths[0] + columnWidths[1];
7507 if (submenuWidth != 0)
7508 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007509 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7511 textWidth += submenuWidth;
7512 }
7513 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7514 if (textWidth > dlgwidth)
7515 dlgwidth = textWidth;
7516 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7517
Bram Moolenaar734a8672019-12-02 22:49:38 +01007518 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007519 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7522 *p++ = LOWORD(lStyle);
7523 *p++ = HIWORD(lStyle);
7524 *p++ = LOWORD(lExtendedStyle);
7525 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007526 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007528 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007530 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 else
7532 *p++ = PixelToDialogX(initX); // x
7533 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007534 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 else
7536 *p++ = PixelToDialogY(initY); // y
7537 *p++ = PixelToDialogX(dlgwidth); // cx
7538 ptrueheight = p;
7539 *p++ = 0; // dialog height: changed later anyway
7540 *p++ = 0; // Menu
7541 *p++ = 0; // Class
7542
Bram Moolenaar734a8672019-12-02 22:49:38 +01007543 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007545 ? (LPSTR)title
7546 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 p += nchar;
7548
K.Takatad1c58992022-01-23 12:31:57 +00007549 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007550# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007551 if (use_lfSysmenu)
7552 {
7553 // point size
7554 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7555 GetDeviceCaps(hdc, LOGPIXELSY));
7556 wcscpy(p, lfSysmenu.lfFaceName);
7557 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
K.Takatad1c58992022-01-23 12:31:57 +00007559 else
7560# endif
7561 {
7562 *p++ = DLG_FONT_POINT_SIZE; // point size
7563 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7564 }
7565 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566
7567 /*
7568 * Loop over all the items in the menu.
7569 * But skip over the tearbar.
7570 */
7571 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7572 menu = menu->children->next;
7573 else
7574 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007575 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 for ( ; menu != NULL; menu = menu->next)
7577 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007578 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 continue;
7580 if (menu_is_separator(menu->dname))
7581 {
7582 sepPadding += 3;
7583 continue;
7584 }
7585
Bram Moolenaar734a8672019-12-02 22:49:38 +01007586 // Check if there still is plenty of room in the template. Make it
7587 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7589 {
7590 WORD *newp;
7591
7592 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7593 if (newp != NULL)
7594 {
7595 template_len += 4096;
7596 mch_memmove(newp, pdlgtemplate,
7597 (char *)p - (char *)pdlgtemplate);
7598 p = newp + (p - pdlgtemplate);
7599 pnumitems = newp + (pnumitems - pdlgtemplate);
7600 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7601 LocalFree(LocalHandle(pdlgtemplate));
7602 pdlgtemplate = newp;
7603 }
7604 }
7605
Bram Moolenaar734a8672019-12-02 22:49:38 +01007606 // Figure out minimal length of this menu label. Use "name" for the
7607 // actual text, "dname" for estimating the displayed size. "name"
7608 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 len = nameLen = (int)STRLEN(menu->name);
7610 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7611 (int)STRLEN(menu->dname))) / spaceWidth;
7612 len += padding0;
7613
7614 if (menu->actext != NULL)
7615 {
7616 acLen = (int)STRLEN(menu->actext);
7617 len += acLen;
7618 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7619 }
7620 else
7621 textWidth = 0;
7622 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7623 len += padding1;
7624
7625 if (menu->children == NULL)
7626 {
7627 padding2 = submenuWidth / spaceWidth;
7628 len += padding2;
7629 menuID = (WORD)(menu->id);
7630 }
7631 else
7632 {
7633 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007634 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636
Bram Moolenaar734a8672019-12-02 22:49:38 +01007637 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007638 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 if (label == NULL)
7640 break;
7641
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007642 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007643 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007645 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 while (padding0-- > 0)
7647 *text++ = ' ';
7648 if (menu->actext != NULL)
7649 {
7650 STRNCPY(text, menu->actext, acLen);
7651 text += acLen;
7652 }
7653 while (padding1-- > 0)
7654 *text++ = ' ';
7655 if (menu->children != NULL)
7656 {
7657 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7658 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7659 }
7660 else
7661 {
7662 while (padding2-- > 0)
7663 *text++ = ' ';
7664 }
7665 *text = NUL;
7666
7667 /*
7668 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7669 * W95/NT4 it makes the tear-off look more like a menu.
7670 */
7671 p = add_dialog_element(p,
7672 BS_PUSHBUTTON|BS_LEFT,
7673 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7674 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7675 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7676 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007677 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 vim_free(label);
7679 (*pnumitems)++;
7680 }
7681
7682 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7683
7684
Bram Moolenaar734a8672019-12-02 22:49:38 +01007685 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007686 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007687 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 (LPDLGTEMPLATE)pdlgtemplate,
7689 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007690 (DLGPROC)tearoff_callback,
7691 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692
7693 LocalFree(LocalHandle(pdlgtemplate));
7694 SelectFont(hdc, oldFont);
7695 DeleteObject(font);
7696 ReleaseDC(hwnd, hdc);
7697
7698 /*
7699 * Reassert ourselves as the active window. This is so that after creating
7700 * a tearoff, the user doesn't have to click with the mouse just to start
7701 * typing again!
7702 */
7703 (void)SetActiveWindow(s_hwnd);
7704
Bram Moolenaar734a8672019-12-02 22:49:38 +01007705 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 force_menu_update = TRUE;
7707}
7708#endif
7709
7710#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007711# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713/*
7714 * Create the toolbar, initially unpopulated.
7715 * (just like the menu, there are no defaults, it's all
7716 * set up through menu.vim)
7717 */
7718 static void
7719initialise_toolbar(void)
7720{
7721 InitCommonControls();
7722 s_toolbarhwnd = CreateToolbarEx(
7723 s_hwnd,
7724 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7725 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007726 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007727 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 IDR_TOOLBAR1, // id of initial bitmap
7729 NULL,
7730 0, // initial number of buttons
7731 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7732 TOOLBAR_BUTTON_HEIGHT,
7733 TOOLBAR_BUTTON_WIDTH,
7734 TOOLBAR_BUTTON_HEIGHT,
7735 sizeof(TBBUTTON)
7736 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007737
7738 // Remove transparency from the toolbar to prevent the main window
7739 // background colour showing through
7740 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7741 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7742
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007743 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
7745 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007746
7747 update_toolbar_size();
7748}
7749
7750 static void
7751update_toolbar_size(void)
7752{
7753 int w, h;
7754 TBMETRICS tbm = {sizeof(TBMETRICS)};
7755
7756 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7757 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7758 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7759 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7760
7761 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7762 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7763 //TRACE("button size: %d, %d", w, h);
7764 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7765 gui.toolbar_height = h + 6;
7766
7767 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7768 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7769
7770 // TODO:
7771 // Currently, this function only updates the size of toolbar buttons.
7772 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007775 static LRESULT CALLBACK
7776toolbar_wndproc(
7777 HWND hwnd,
7778 UINT uMsg,
7779 WPARAM wParam,
7780 LPARAM lParam)
7781{
7782 HandleMouseHide(uMsg, lParam);
7783 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7784}
7785
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 static int
7787get_toolbar_bitmap(vimmenu_T *menu)
7788{
7789 int i = -1;
7790
7791 /*
7792 * Check user bitmaps first, unless builtin is specified.
7793 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007794 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {
7796 char_u fname[MAXPATHL];
7797 HANDLE hbitmap = NULL;
7798
7799 if (menu->iconfile != NULL)
7800 {
7801 gui_find_iconfile(menu->iconfile, fname, "bmp");
7802 hbitmap = LoadImage(
7803 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007804 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 IMAGE_BITMAP,
7806 TOOLBAR_BUTTON_WIDTH,
7807 TOOLBAR_BUTTON_HEIGHT,
7808 LR_LOADFROMFILE |
7809 LR_LOADMAP3DCOLORS
7810 );
7811 }
7812
7813 /*
7814 * If the LoadImage call failed, or the "icon=" file
7815 * didn't exist or wasn't specified, try the menu name
7816 */
7817 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007818 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007819# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007820 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007821# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007822 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 hbitmap = LoadImage(
7824 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007825 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 IMAGE_BITMAP,
7827 TOOLBAR_BUTTON_WIDTH,
7828 TOOLBAR_BUTTON_HEIGHT,
7829 LR_LOADFROMFILE |
7830 LR_LOADMAP3DCOLORS
7831 );
7832
7833 if (hbitmap != NULL)
7834 {
7835 TBADDBITMAP tbAddBitmap;
7836
7837 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007838 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839
7840 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7841 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007842 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844 }
7845 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7846 i = menu->iconidx;
7847
7848 return i;
7849}
7850#endif
7851
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007852#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7853 static void
7854initialise_tabline(void)
7855{
7856 InitCommonControls();
7857
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007858 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007859 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007860 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007861 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007862 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007863
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007864 gui.tabline_height = TABLINE_HEIGHT;
7865
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007866 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007867}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007868
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007869/*
7870 * Get tabpage_T from POINT.
7871 */
7872 static tabpage_T *
7873GetTabFromPoint(
7874 HWND hWnd,
7875 POINT pt)
7876{
7877 tabpage_T *ptp = NULL;
7878
7879 if (gui_mch_showing_tabline())
7880 {
7881 TCHITTESTINFO htinfo;
7882 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00007883 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007884 if (s_tabhwnd == hWnd)
7885 {
7886 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7887 if (idx != -1)
7888 ptp = find_tabpage(idx + 1);
7889 }
7890 }
7891 return ptp;
7892}
7893
7894static POINT s_pt = {0, 0};
7895static HCURSOR s_hCursor = NULL;
7896
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007897 static LRESULT CALLBACK
7898tabline_wndproc(
7899 HWND hwnd,
7900 UINT uMsg,
7901 WPARAM wParam,
7902 LPARAM lParam)
7903{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007904 POINT pt;
7905 tabpage_T *tp;
7906 RECT rect;
7907 int nCenter;
7908 int idx0;
7909 int idx1;
7910
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007911 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007912
7913 switch (uMsg)
7914 {
7915 case WM_LBUTTONDOWN:
7916 {
7917 s_pt.x = GET_X_LPARAM(lParam);
7918 s_pt.y = GET_Y_LPARAM(lParam);
7919 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007920 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007921 break;
7922 }
7923 case WM_MOUSEMOVE:
7924 if (GetCapture() == hwnd
7925 && ((wParam & MK_LBUTTON)) != 0)
7926 {
7927 pt.x = GET_X_LPARAM(lParam);
7928 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00007929 if (abs(pt.x - s_pt.x) >
7930 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007931 {
7932 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7933
7934 tp = GetTabFromPoint(hwnd, pt);
7935 if (tp != NULL)
7936 {
7937 idx0 = tabpage_index(curtab) - 1;
7938 idx1 = tabpage_index(tp) - 1;
7939
7940 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7941 nCenter = rect.left + (rect.right - rect.left) / 2;
7942
Bram Moolenaar734a8672019-12-02 22:49:38 +01007943 // Check if the mouse cursor goes over the center of
7944 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007945 if ((idx0 < idx1) && (nCenter < pt.x))
7946 {
7947 tabpage_move(idx1 + 1);
7948 update_screen(0);
7949 }
7950 else if ((idx1 < idx0) && (pt.x < nCenter))
7951 {
7952 tabpage_move(idx1);
7953 update_screen(0);
7954 }
7955 }
7956 }
7957 }
7958 break;
7959 case WM_LBUTTONUP:
7960 {
7961 if (GetCapture() == hwnd)
7962 {
7963 SetCursor(s_hCursor);
7964 ReleaseCapture();
7965 }
7966 break;
7967 }
7968 default:
7969 break;
7970 }
7971
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007972 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7973}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007974#endif
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7977/*
7978 * Make the GUI window come to the foreground.
7979 */
7980 void
7981gui_mch_set_foreground(void)
7982{
7983 if (IsIconic(s_hwnd))
7984 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7985 SetForegroundWindow(s_hwnd);
7986}
7987#endif
7988
7989#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7990 static void
7991dyn_imm_load(void)
7992{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007993 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (hLibImm == NULL)
7995 return;
7996
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 pImmGetCompositionStringW
7998 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7999 pImmGetContext
8000 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8001 pImmAssociateContext
8002 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8003 pImmReleaseContext
8004 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8005 pImmGetOpenStatus
8006 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8007 pImmSetOpenStatus
8008 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008009 pImmGetCompositionFontW
8010 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8011 pImmSetCompositionFontW
8012 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 pImmSetCompositionWindow
8014 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8015 pImmGetConversionStatus
8016 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008017 pImmSetConversionStatus
8018 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
K.Takatab0b2b732022-01-19 12:59:21 +00008020 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 || pImmGetContext == NULL
8022 || pImmAssociateContext == NULL
8023 || pImmReleaseContext == NULL
8024 || pImmGetOpenStatus == NULL
8025 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008026 || pImmGetCompositionFontW == NULL
8027 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008029 || pImmGetConversionStatus == NULL
8030 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {
8032 FreeLibrary(hLibImm);
8033 hLibImm = NULL;
8034 pImmGetContext = NULL;
8035 return;
8036 }
8037
8038 return;
8039}
8040
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041#endif
8042
8043#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8044
8045# ifdef FEAT_XPM_W32
8046# define IMAGE_XPM 100
8047# endif
8048
8049typedef struct _signicon_t
8050{
8051 HANDLE hImage;
8052 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008053# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008054 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056} signicon_t;
8057
8058 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008059gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 signicon_t *sign;
8062 int x, y, w, h;
8063
8064 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8065 return;
8066
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008067# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008068 if (IS_ENABLE_DIRECTX())
8069 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008070# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008071
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 x = TEXT_X(col);
8073 y = TEXT_Y(row);
8074 w = gui.char_width * 2;
8075 h = gui.char_height;
8076 switch (sign->uType)
8077 {
8078 case IMAGE_BITMAP:
8079 {
8080 HDC hdcMem;
8081 HBITMAP hbmpOld;
8082
8083 hdcMem = CreateCompatibleDC(s_hdc);
8084 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8085 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8086 SelectObject(hdcMem, hbmpOld);
8087 DeleteDC(hdcMem);
8088 }
8089 break;
8090 case IMAGE_ICON:
8091 case IMAGE_CURSOR:
8092 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8093 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008094# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 case IMAGE_XPM:
8096 {
8097 HDC hdcMem;
8098 HBITMAP hbmpOld;
8099
8100 hdcMem = CreateCompatibleDC(s_hdc);
8101 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008102 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8104
8105 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008106 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8108 SelectObject(hdcMem, hbmpOld);
8109 DeleteDC(hdcMem);
8110 }
8111 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 }
8114}
8115
8116 static void
8117close_signicon_image(signicon_t *sign)
8118{
8119 if (sign)
8120 switch (sign->uType)
8121 {
8122 case IMAGE_BITMAP:
8123 DeleteObject((HGDIOBJ)sign->hImage);
8124 break;
8125 case IMAGE_CURSOR:
8126 DestroyCursor((HCURSOR)sign->hImage);
8127 break;
8128 case IMAGE_ICON:
8129 DestroyIcon((HICON)sign->hImage);
8130 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008131# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 case IMAGE_XPM:
8133 DeleteObject((HBITMAP)sign->hImage);
8134 DeleteObject((HBITMAP)sign->hShape);
8135 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138}
8139
8140 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008141gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 signicon_t sign, *psign;
8144 char_u *ext;
8145
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008147 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 if (ext > signfile)
8149 {
8150 int do_load = 1;
8151
8152 if (!STRICMP(ext, ".bmp"))
8153 sign.uType = IMAGE_BITMAP;
8154 else if (!STRICMP(ext, ".ico"))
8155 sign.uType = IMAGE_ICON;
8156 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8157 sign.uType = IMAGE_CURSOR;
8158 else
8159 do_load = 0;
8160
8161 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008162 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 gui.char_width * 2, gui.char_height,
8164 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008165# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (!STRICMP(ext, ".xpm"))
8167 {
8168 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008169 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8170 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 }
8174
8175 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008176 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 *psign = sign;
8178
8179 if (!psign)
8180 {
8181 if (sign.hImage)
8182 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008183 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185 return (void *)psign;
8186
8187}
8188
8189 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008190gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 if (sign)
8193 {
8194 close_signicon_image((signicon_t *)sign);
8195 vim_free(sign);
8196 }
8197}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008200#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
Bram Moolenaar734a8672019-12-02 22:49:38 +01008202/*
8203 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008204 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008206 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8208 * to get current mouse position).
8209 *
8210 * Trying to use as more Windows services as possible, and as less
8211 * IE version as possible :)).
8212 *
8213 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8214 * BalloonEval struct.
8215 * 2) Enable/Disable simply create/kill BalloonEval Timer
8216 * 3) When there was enough inactivity, timer procedure posts
8217 * async request to debugger
8218 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8219 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008220 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 */
8222
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008223 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008224make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008225{
K.Takata76687d22022-01-25 10:31:37 +00008226 TOOLINFOW *pti;
8227 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008228
K.Takata76687d22022-01-25 10:31:37 +00008229 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008230 if (pti == NULL)
8231 return;
8232
8233 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8234 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8235 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008236 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008237
8238 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8239 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8240
K.Takata76687d22022-01-25 10:31:37 +00008241 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008242 pti->uFlags = TTF_SUBCLASS;
8243 pti->hwnd = beval->target;
8244 pti->hinst = 0; // Don't use string resources
8245 pti->uId = ID_BEVAL_TOOLTIP;
8246
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008247 pti->lpszText = LPSTR_TEXTCALLBACKW;
8248 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8249 pti->lParam = (LPARAM)beval->tofree;
8250 // switch multiline tooltips on
8251 if (GetClientRect(s_textArea, &rect))
8252 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8253 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008254
8255 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8256 pti->rect.left = pt.x - 3;
8257 pti->rect.top = pt.y - 3;
8258 pti->rect.right = pt.x + 3;
8259 pti->rect.bottom = pt.y + 3;
8260
8261 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8262 // Make tooltip appear sooner.
8263 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8264 // I've performed some tests and it seems the longest possible life time
8265 // of tooltip is 30 seconds.
8266 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8267 /*
8268 * HACK: force tooltip to appear, because it'll not appear until
8269 * first mouse move. D*mn M$
8270 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8271 */
8272 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8273 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8274 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008275}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008278delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008280 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281}
8282
8283 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008284beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008285 HWND hwnd UNUSED,
8286 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008287 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008288 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 POINT pt;
8291 RECT rect;
8292
8293 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8294 return;
8295
8296 GetCursorPos(&pt);
8297 if (WindowFromPoint(pt) != s_textArea)
8298 return;
8299
8300 ScreenToClient(s_textArea, &pt);
8301 GetClientRect(s_textArea, &rect);
8302 if (!PtInRect(&rect, pt))
8303 return;
8304
K.Takataa8ec4912022-02-03 14:32:33 +00008305 if (last_user_activity > 0
8306 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 && (cur_beval->showState != ShS_PENDING
8308 || abs(cur_beval->x - pt.x) > 3
8309 || abs(cur_beval->y - pt.y) > 3))
8310 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008311 // Pointer resting in one place long enough, it's time to show
8312 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 cur_beval->showState = ShS_PENDING;
8314 cur_beval->x = pt.x;
8315 cur_beval->y = pt.y;
8316
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (cur_beval->msgCB != NULL)
8318 (*cur_beval->msgCB)(cur_beval, 0);
8319 }
8320}
8321
8322 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008323gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
K.Takataa8ec4912022-02-03 14:32:33 +00008325 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326}
8327
8328 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008329gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (beval == NULL)
8332 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008333 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8334 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335}
8336
8337 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008338gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008341
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008342 vim_free(beval->msg);
8343 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8344 if (beval->msg == NULL)
8345 {
8346 delete_tooltip(beval);
8347 beval->showState = ShS_NEUTRAL;
8348 return;
8349 }
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (beval->showState == ShS_SHOWING)
8352 return;
8353 GetCursorPos(&pt);
8354 ScreenToClient(s_textArea, &pt);
8355
8356 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008358 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 gui_mch_disable_beval_area(cur_beval);
8360 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008361 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363}
8364
8365 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008366gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008367 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008368 char_u *mesg,
8369 void (*mesgCB)(BalloonEval *, int),
8370 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008372 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 BalloonEval *beval;
8374
8375 if (mesg != NULL && mesgCB != NULL)
8376 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008377 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 return NULL;
8379 }
8380
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008381 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (beval != NULL)
8383 {
8384 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385
8386 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 beval->msg = mesg;
8388 beval->msgCB = mesgCB;
8389 beval->clientData = clientData;
8390
8391 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 cur_beval = beval;
8393
8394 if (p_beval)
8395 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 }
8397 return beval;
8398}
8399
8400 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008401Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008403 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 return;
8405
8406 if (cur_beval != NULL)
8407 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008408 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008410 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008411 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008412 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 delete_tooltip(cur_beval);
8414 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
8416 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008417 break;
8418 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008419 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008420 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008421 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008422 info->lpszText = (LPSTR)info->lParam;
8423 info->uFlags |= TTF_DI_SETITEM;
8424 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008425 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008426 case TTN_GETDISPINFOW:
8427 {
8428 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008429 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008430 info->lpszText = (LPWSTR)info->lParam;
8431 info->uFlags |= TTF_DI_SETITEM;
8432 }
8433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435 }
8436}
8437
8438 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008439track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8442 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008443 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444}
8445
8446 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008447gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008449# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008450 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008451# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008452 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 vim_free(beval);
8454}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008455#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456
8457#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8458/*
8459 * We have multiple signs to draw at the same location. Draw the
8460 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8461 */
8462 void
8463netbeans_draw_multisign_indicator(int row)
8464{
8465 int i;
8466 int y;
8467 int x;
8468
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008469 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008470 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 x = 0;
8473 y = TEXT_Y(row);
8474
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008475# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008476 if (IS_ENABLE_DIRECTX())
8477 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008478# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008479
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 for (i = 0; i < gui.char_height - 3; i++)
8481 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8482
8483 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8484 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8485 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8486 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8487 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8488 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8489 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8490}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008491#endif