blob: b2968614405f013c69becf1a03cfe90f26295f70 [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);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010049# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
Bram Moolenaar734a8672019-12-02 22:49:38 +010061 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100120 else if (STRCMP(name, "scrlines") == 0)
121 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100122 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 else
125 return FAIL;
126 }
127
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100128 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100129 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 if (dx_enable)
133 {
134 if (!directx_enabled())
135 return FAIL;
136 DWriteContext_SetRenderingParams(s_dwc, NULL);
137 if (dx_flags)
138 {
139 DWriteRenderingParams param;
140 DWriteContext_GetRenderingParams(s_dwc, &param);
141 if (dx_flags & (1 << 0))
142 param.gamma = dx_gamma;
143 if (dx_flags & (1 << 1))
144 param.enhancedContrast = dx_contrast;
145 if (dx_flags & (1 << 2))
146 param.clearTypeLevel = dx_level;
147 if (dx_flags & (1 << 3))
148 param.pixelGeometry = dx_geom;
149 if (dx_flags & (1 << 4))
150 param.renderingMode = dx_renmode;
151 if (dx_flags & (1 << 5))
152 param.textAntialiasMode = dx_taamode;
153 DWriteContext_SetRenderingParams(s_dwc, &param);
154 }
155 }
156 s_directx_enabled = dx_enable;
157
158 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100159# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162}
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * These are new in Windows ME/XP, only defined in recent compilers.
167 */
168#ifndef HANDLE_WM_XBUTTONUP
169# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
170 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
171#endif
172#ifndef HANDLE_WM_XBUTTONDOWN
173# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDBLCLK
177# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100181
Bram Moolenaar734a8672019-12-02 22:49:38 +0100182#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
Bram Moolenaar734a8672019-12-02 22:49:38 +0100187// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188#ifndef PROTO
189
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100190# ifndef __MINGW32__
191# include <shellapi.h>
192# endif
193# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
194# include <commctrl.h>
195# endif
196# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifdef GLOBAL_IME
199# include "glbl_ime.h"
200# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
Bram Moolenaar734a8672019-12-02 22:49:38 +0100202#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
204#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100205# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100206#endif
207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200243# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200244# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define _cdecl
246typedef int BOOL;
247typedef int BYTE;
248typedef int DWORD;
249typedef int WCHAR;
250typedef int ENUMLOGFONT;
251typedef int FINDREPLACE;
252typedef int HANDLE;
253typedef int HBITMAP;
254typedef int HBRUSH;
255typedef int HDROP;
256typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100257typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100258typedef int LPARAM;
259typedef int LPCREATESTRUCT;
260typedef int LPCSTR;
261typedef int LPCTSTR;
262typedef int LPRECT;
263typedef int LPSTR;
264typedef int LPWINDOWPOS;
265typedef int LPWORD;
266typedef int LRESULT;
267typedef int HRESULT;
268# undef MSG
269typedef int MSG;
270typedef int NEWTEXTMETRIC;
271typedef int OSVERSIONINFO;
272typedef int PWORD;
273typedef int RECT;
274typedef int UINT;
275typedef int WORD;
276typedef int WPARAM;
277typedef int POINT;
278typedef void *HINSTANCE;
279typedef void *HMENU;
280typedef void *HWND;
281typedef void *HDC;
282typedef void VOID;
283typedef int LPNMHDR;
284typedef int LONG;
285typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200286typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200287typedef int COLORREF;
288typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
295static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200317static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
355#ifdef GLOBAL_IME
356# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
357#else
358# define MyTranslateMessage(x) TranslateMessage(x)
359#endif
360
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100361#if defined(FEAT_DIRECTX)
362 static int
363directx_enabled(void)
364{
365 if (s_dwc != NULL)
366 return 1;
367 else if (s_directx_load_attempted)
368 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100369 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100370 DWrite_Init();
371 s_directx_load_attempted = 1;
372 s_dwc = DWriteContext_Open();
373 directx_binddc();
374 return s_dwc != NULL ? 1 : 0;
375}
376
377 static void
378directx_binddc(void)
379{
380 if (s_textArea != NULL)
381 {
382 RECT rect;
383 GetClientRect(s_textArea, &rect);
384 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
385 }
386}
387#endif
388
Bram Moolenaar734a8672019-12-02 22:49:38 +0100389// use of WindowProc depends on Global IME
Bram Moolenaar945c8572020-07-17 22:17:03 +0200390static LRESULT WINAPI MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100391
Bram Moolenaar734a8672019-12-02 22:49:38 +0100392extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100393
394static struct
395{
396 UINT key_sym;
397 char_u vim_code0;
398 char_u vim_code1;
399} special_keys[] =
400{
401 {VK_UP, 'k', 'u'},
402 {VK_DOWN, 'k', 'd'},
403 {VK_LEFT, 'k', 'l'},
404 {VK_RIGHT, 'k', 'r'},
405
406 {VK_F1, 'k', '1'},
407 {VK_F2, 'k', '2'},
408 {VK_F3, 'k', '3'},
409 {VK_F4, 'k', '4'},
410 {VK_F5, 'k', '5'},
411 {VK_F6, 'k', '6'},
412 {VK_F7, 'k', '7'},
413 {VK_F8, 'k', '8'},
414 {VK_F9, 'k', '9'},
415 {VK_F10, 'k', ';'},
416
417 {VK_F11, 'F', '1'},
418 {VK_F12, 'F', '2'},
419 {VK_F13, 'F', '3'},
420 {VK_F14, 'F', '4'},
421 {VK_F15, 'F', '5'},
422 {VK_F16, 'F', '6'},
423 {VK_F17, 'F', '7'},
424 {VK_F18, 'F', '8'},
425 {VK_F19, 'F', '9'},
426 {VK_F20, 'F', 'A'},
427
428 {VK_F21, 'F', 'B'},
429#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100430 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100431#endif
432 {VK_F22, 'F', 'C'},
433 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100434 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100435
436 {VK_HELP, '%', '1'},
437 {VK_BACK, 'k', 'b'},
438 {VK_INSERT, 'k', 'I'},
439 {VK_DELETE, 'k', 'D'},
440 {VK_HOME, 'k', 'h'},
441 {VK_END, '@', '7'},
442 {VK_PRIOR, 'k', 'P'},
443 {VK_NEXT, 'k', 'N'},
444 {VK_PRINT, '%', '9'},
445 {VK_ADD, 'K', '6'},
446 {VK_SUBTRACT, 'K', '7'},
447 {VK_DIVIDE, 'K', '8'},
448 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100449 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100450 {VK_DECIMAL, 'K', 'B'},
451
452 {VK_NUMPAD0, 'K', 'C'},
453 {VK_NUMPAD1, 'K', 'D'},
454 {VK_NUMPAD2, 'K', 'E'},
455 {VK_NUMPAD3, 'K', 'F'},
456 {VK_NUMPAD4, 'K', 'G'},
457 {VK_NUMPAD5, 'K', 'H'},
458 {VK_NUMPAD6, 'K', 'I'},
459 {VK_NUMPAD7, 'K', 'J'},
460 {VK_NUMPAD8, 'K', 'K'},
461 {VK_NUMPAD9, 'K', 'L'},
462
Bram Moolenaar734a8672019-12-02 22:49:38 +0100463 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100464 {VK_SPACE, ' ', NUL},
465 {VK_TAB, TAB, NUL},
466 {VK_ESCAPE, ESC, NUL},
467 {NL, NL, NUL},
468 {CAR, CAR, NUL},
469
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471 {0, 0, 0}
472};
473
Bram Moolenaar734a8672019-12-02 22:49:38 +0100474// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100475static int s_button_pending = -1;
476
Bram Moolenaar734a8672019-12-02 22:49:38 +0100477// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
478// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479static int s_getting_focus = FALSE;
480
481static int s_x_pending;
482static int s_y_pending;
483static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200484static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200486static int dead_key = 0; // 0: no dead key, 1: dead key pressed
487static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
488 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100490#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100491// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
493static void TrackUserActivity(UINT uMsg);
494#endif
495
496/*
497 * For control IME.
498 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100499 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100501#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100502// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100503static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#endif
505#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100507static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100508#endif
509
510#ifdef FEAT_MBYTE_IME
511static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
512#endif
513
514#if defined(FEAT_BROWSE)
515static char_u *convert_filter(char_u *s);
516#endif
517
518#ifdef DEBUG_PRINT_ERROR
519/*
520 * Print out the last Windows error message
521 */
522 static void
523print_windows_error(void)
524{
525 LPVOID lpMsgBuf;
526
527 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
528 NULL, GetLastError(),
529 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
530 (LPTSTR) &lpMsgBuf, 0, NULL);
531 TRACE1("Error: %s\n", lpMsgBuf);
532 LocalFree(lpMsgBuf);
533}
534#endif
535
536/*
537 * Cursor blink functions.
538 *
539 * This is a simple state machine:
540 * BLINK_NONE not blinking at all
541 * BLINK_OFF blinking, cursor is not shown
542 * BLINK_ON blinking, cursor is shown
543 */
544
545#define BLINK_NONE 0
546#define BLINK_OFF 1
547#define BLINK_ON 2
548
549static int blink_state = BLINK_NONE;
550static long_u blink_waittime = 700;
551static long_u blink_ontime = 400;
552static long_u blink_offtime = 250;
553static UINT blink_timer = 0;
554
Bram Moolenaar703a8042016-06-04 16:24:32 +0200555 int
556gui_mch_is_blinking(void)
557{
558 return blink_state != BLINK_NONE;
559}
560
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200561 int
562gui_mch_is_blink_off(void)
563{
564 return blink_state == BLINK_OFF;
565}
566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100567 void
568gui_mch_set_blinking(long wait, long on, long off)
569{
570 blink_waittime = wait;
571 blink_ontime = on;
572 blink_offtime = off;
573}
574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 static VOID CALLBACK
576_OnBlinkTimer(
577 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100578 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100580 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100581{
582 MSG msg;
583
584 /*
585 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
586 */
587
588 KillTimer(NULL, idEvent);
589
Bram Moolenaar734a8672019-12-02 22:49:38 +0100590 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100591 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
592 ;
593
594 if (blink_state == BLINK_ON)
595 {
596 gui_undraw_cursor();
597 blink_state = BLINK_OFF;
598 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
599 (TIMERPROC)_OnBlinkTimer);
600 }
601 else
602 {
603 gui_update_cursor(TRUE, FALSE);
604 blink_state = BLINK_ON;
605 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100606 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100607 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100608 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609}
610
611 static void
612gui_mswin_rm_blink_timer(void)
613{
614 MSG msg;
615
616 if (blink_timer != 0)
617 {
618 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100619 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100620 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
621 ;
622 blink_timer = 0;
623 }
624}
625
626/*
627 * Stop the cursor blinking. Show the cursor if it wasn't shown.
628 */
629 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100630gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100631{
632 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100634 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100636 gui_mch_flush();
637 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 blink_state = BLINK_NONE;
639}
640
641/*
642 * Start the cursor blinking. If it was already blinking, this restarts the
643 * waiting time and shows the cursor.
644 */
645 void
646gui_mch_start_blink(void)
647{
648 gui_mswin_rm_blink_timer();
649
Bram Moolenaar734a8672019-12-02 22:49:38 +0100650 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
652 {
653 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
654 (TIMERPROC)_OnBlinkTimer);
655 blink_state = BLINK_ON;
656 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100657 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100658 }
659}
660
661/*
662 * Call-back routines.
663 */
664
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665 static VOID CALLBACK
666_OnTimer(
667 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100668 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100670 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671{
672 MSG msg;
673
674 /*
675 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
676 */
677 KillTimer(NULL, idEvent);
678 s_timed_out = TRUE;
679
Bram Moolenaar734a8672019-12-02 22:49:38 +0100680 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
682 ;
683 if (idEvent == s_wait_timer)
684 s_wait_timer = 0;
685}
686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100687 static void
688_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100689 HWND hwnd UNUSED,
690 UINT ch UNUSED,
691 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100692{
693 dead_key = 1;
694}
695
696/*
697 * Convert Unicode character "ch" to bytes in "string[slen]".
698 * When "had_alt" is TRUE the ALT key was included in "ch".
699 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200700 * Because the Windows API uses UTF-16, we have to deal with surrogate
701 * pairs; this is where we choose to deal with them: if "ch" is a high
702 * surrogate, it will be stored, and the length returned will be zero; the next
703 * char_to_string call will then include the high surrogate, decoding the pair
704 * of UTF-16 code units to a single Unicode code point, presuming it is the
705 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 */
707 static int
708char_to_string(int ch, char_u *string, int slen, int had_alt)
709{
710 int len;
711 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200713 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200715 if (surrogate_pending_ch != 0)
716 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100717 // We don't guarantee ch is a low surrogate to match the high surrogate
718 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200719 wstring[0] = surrogate_pending_ch;
720 wstring[1] = ch;
721 surrogate_pending_ch = 0;
722 len = 2;
723 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100724 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200725 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100726 // We don't have the entire code point yet, only the first UTF-16 code
727 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200728 surrogate_pending_ch = ch;
729 return 0;
730 }
731 else
732 {
733 wstring[0] = ch;
734 len = 1;
735 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200736
Bram Moolenaar734a8672019-12-02 22:49:38 +0100737 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
738 // "enc_codepage" is non-zero use the standard Win32 function,
739 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200740 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200742 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
743 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100744 // If we had included the ALT key into the character but now the
745 // upper bit is no longer set, that probably means the conversion
746 // failed. Convert the original character and set the upper bit
747 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200748 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200750 wstring[0] = ch & 0x7f;
751 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
752 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100753 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200754 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 }
756 }
757 else
758 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200759 ws = utf16_to_enc(wstring, &len);
760 if (ws == NULL)
761 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762 else
763 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100764 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200765 len = slen;
766 mch_memmove(string, ws, len);
767 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100768 }
769 }
770
771 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
773 string[0] = ch;
774 len = 1;
775 }
776
777 for (i = 0; i < len; ++i)
778 if (string[i] == CSI && len <= slen - 2)
779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100781 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
782 string[++i] = KS_EXTRA;
783 string[++i] = (int)KE_CSI;
784 len += 2;
785 }
786
787 return len;
788}
789
790/*
791 * Key hit, add it to the input buffer.
792 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 static void
794_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100795 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100797 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798{
799 char_u string[40];
800 int len = 0;
801
802 dead_key = 0;
803
804 len = char_to_string(ch, string, 40, FALSE);
805 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
806 {
807 trash_input_buf();
808 got_int = TRUE;
809 }
810
811 add_to_input_buf(string, len);
812}
813
814/*
815 * Alt-Key hit, add it to the input buffer.
816 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 static void
818_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100819 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100821 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100823 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 int len;
825 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100826 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827
828 dead_key = 0;
829
Bram Moolenaar734a8672019-12-02 22:49:38 +0100830 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100831
Bram Moolenaar734a8672019-12-02 22:49:38 +0100832 // OK, we have a character key (given by ch) which was entered with the
833 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
834 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
835 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100836 modifiers = MOD_MASK_ALT;
837 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);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100843 // remove the SHIFT modifier for keys where it's already included, e.g.,
844 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200845 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100846
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200847 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
848 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100849 if (ch == CSI)
850 ch = K_CSI;
851
852 len = 0;
853 if (modifiers)
854 {
855 string[len++] = CSI;
856 string[len++] = KS_MODIFIER;
857 string[len++] = modifiers;
858 }
859
860 if (IS_SPECIAL((int)ch))
861 {
862 string[len++] = CSI;
863 string[len++] = K_SECOND((int)ch);
864 string[len++] = K_THIRD((int)ch);
865 }
866 else
867 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100868 // Although the documentation isn't clear about it, we assume "ch" is
869 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870 len += char_to_string(ch, string + len, 40 - len, TRUE);
871 }
872
873 add_to_input_buf(string, len);
874}
875
876 static void
877_OnMouseEvent(
878 int button,
879 int x,
880 int y,
881 int repeated_click,
882 UINT keyFlags)
883{
884 int vim_modifiers = 0x0;
885
886 s_getting_focus = FALSE;
887
888 if (keyFlags & MK_SHIFT)
889 vim_modifiers |= MOUSE_SHIFT;
890 if (keyFlags & MK_CONTROL)
891 vim_modifiers |= MOUSE_CTRL;
892 if (GetKeyState(VK_MENU) & 0x8000)
893 vim_modifiers |= MOUSE_ALT;
894
895 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
896}
897
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100898 static void
899_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100900 HWND hwnd UNUSED,
901 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100902 int x,
903 int y,
904 UINT keyFlags)
905{
906 static LONG s_prevTime = 0;
907
908 LONG currentTime = GetMessageTime();
909 int button = -1;
910 int repeated_click;
911
Bram Moolenaar734a8672019-12-02 22:49:38 +0100912 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100913 (void)SetFocus(s_hwnd);
914
915 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
916 button = MOUSE_LEFT;
917 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
918 button = MOUSE_MIDDLE;
919 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
920 button = MOUSE_RIGHT;
921 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
922 {
923#ifndef GET_XBUTTON_WPARAM
924# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
925#endif
926 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
927 }
928 else if (s_uMsg == WM_CAPTURECHANGED)
929 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100930 // on W95/NT4, somehow you get in here with an odd Msg
931 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 if (s_button_pending == MOUSE_LEFT)
933 button = MOUSE_RIGHT;
934 else
935 button = MOUSE_LEFT;
936 }
937 if (button >= 0)
938 {
939 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
940
941 /*
942 * Holding down the left and right buttons simulates pushing the middle
943 * button.
944 */
945 if (repeated_click
946 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
947 || (button == MOUSE_RIGHT
948 && s_button_pending == MOUSE_LEFT)))
949 {
950 /*
951 * Hmm, gui.c will ignore more than one button down at a time, so
952 * pretend we let go of it first.
953 */
954 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
955 button = MOUSE_MIDDLE;
956 repeated_click = FALSE;
957 s_button_pending = -1;
958 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
959 }
960 else if ((repeated_click)
961 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
962 {
963 if (s_button_pending > -1)
964 {
965 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
966 s_button_pending = -1;
967 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100968 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100969 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
970 }
971 else
972 {
973 /*
974 * If this is the first press (i.e. not a multiple click) don't
975 * action immediately, but store and wait for:
976 * i) button-up
977 * ii) mouse move
978 * iii) another button press
979 * before using it.
980 * This enables us to make left+right simulate middle button,
981 * without left or right being actioned first. The side-effect is
982 * that if you click and hold the mouse without dragging, the
983 * cursor doesn't move until you release the button. In practice
984 * this is hardly a problem.
985 */
986 s_button_pending = button;
987 s_x_pending = x;
988 s_y_pending = y;
989 s_kFlags_pending = keyFlags;
990 }
991
992 s_prevTime = currentTime;
993 }
994}
995
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100996 static void
997_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100998 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100999 int x,
1000 int y,
1001 UINT keyFlags)
1002{
1003 int button;
1004
1005 s_getting_focus = FALSE;
1006 if (s_button_pending > -1)
1007 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001008 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001009 _OnMouseEvent(s_button_pending, s_x_pending,
1010 s_y_pending, FALSE, s_kFlags_pending);
1011 s_button_pending = -1;
1012 }
1013 if (s_uMsg == WM_MOUSEMOVE)
1014 {
1015 /*
1016 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1017 * down.
1018 */
1019 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1020 | MK_XBUTTON1 | MK_XBUTTON2)))
1021 {
1022 gui_mouse_moved(x, y);
1023 return;
1024 }
1025
1026 /*
1027 * While button is down, keep grabbing mouse move events when
1028 * the mouse goes outside the window
1029 */
1030 SetCapture(s_textArea);
1031 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001032 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001033 }
1034 else
1035 {
1036 ReleaseCapture();
1037 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001038 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001039 }
1040
1041 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1042}
1043
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001044 static void
1045_OnSizeTextArea(
1046 HWND hwnd UNUSED,
1047 UINT state UNUSED,
1048 int cx UNUSED,
1049 int cy UNUSED)
1050{
1051#if defined(FEAT_DIRECTX)
1052 if (IS_ENABLE_DIRECTX())
1053 directx_binddc();
1054#endif
1055}
1056
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057#ifdef FEAT_MENU
1058/*
1059 * Find the vimmenu_T with the given id
1060 */
1061 static vimmenu_T *
1062gui_mswin_find_menu(
1063 vimmenu_T *pMenu,
1064 int id)
1065{
1066 vimmenu_T *pChildMenu;
1067
1068 while (pMenu)
1069 {
1070 if (pMenu->id == (UINT)id)
1071 break;
1072 if (pMenu->children != NULL)
1073 {
1074 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1075 if (pChildMenu)
1076 {
1077 pMenu = pChildMenu;
1078 break;
1079 }
1080 }
1081 pMenu = pMenu->next;
1082 }
1083 return pMenu;
1084}
1085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001086 static void
1087_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001088 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001089 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001090 HWND hwndCtl UNUSED,
1091 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092{
1093 vimmenu_T *pMenu;
1094
1095 pMenu = gui_mswin_find_menu(root_menu, id);
1096 if (pMenu)
1097 gui_menu_cb(pMenu);
1098}
1099#endif
1100
1101#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103 * Handle a Find/Replace window message.
1104 */
1105 static void
1106_OnFindRepl(void)
1107{
1108 int flags = 0;
1109 int down;
1110
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001111 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001112 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001113 (void)SetFocus(s_hwnd);
1114
1115 if (s_findrep_struct.Flags & FR_FINDNEXT)
1116 {
1117 flags = FRD_FINDNEXT;
1118
Bram Moolenaar734a8672019-12-02 22:49:38 +01001119 // Give main window the focus back: this is so the cursor isn't
1120 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001121 (void)SetFocus(s_hwnd);
1122 }
1123 else if (s_findrep_struct.Flags & FR_REPLACE)
1124 {
1125 flags = FRD_REPLACE;
1126
Bram Moolenaar734a8672019-12-02 22:49:38 +01001127 // Give main window the focus back: this is so the cursor isn't
1128 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001129 (void)SetFocus(s_hwnd);
1130 }
1131 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1132 {
1133 flags = FRD_REPLACEALL;
1134 }
1135
1136 if (flags != 0)
1137 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001138 char_u *p, *q;
1139
Bram Moolenaar734a8672019-12-02 22:49:38 +01001140 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001141 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1142 flags |= FRD_WHOLE_WORD;
1143 if (s_findrep_struct.Flags & FR_MATCHCASE)
1144 flags |= FRD_MATCH_CASE;
1145 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001146 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1147 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1148 if (p != NULL && q != NULL)
1149 gui_do_findrepl(flags, p, q, down);
1150 vim_free(p);
1151 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 }
1153}
1154#endif
1155
1156 static void
1157HandleMouseHide(UINT uMsg, LPARAM lParam)
1158{
1159 static LPARAM last_lParam = 0L;
1160
Bram Moolenaar734a8672019-12-02 22:49:38 +01001161 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001162 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1163 {
1164 if (lParam == last_lParam)
1165 return;
1166 last_lParam = lParam;
1167 }
1168
Bram Moolenaar734a8672019-12-02 22:49:38 +01001169 // Handle specially, to centralise coding. We need to be sure we catch all
1170 // possible events which should cause us to restore the cursor (as it is a
1171 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001172 switch (uMsg)
1173 {
1174 case WM_KEYUP:
1175 case WM_CHAR:
1176 /*
1177 * blank out the pointer if necessary
1178 */
1179 if (p_mh)
1180 gui_mch_mousehide(TRUE);
1181 break;
1182
Bram Moolenaar734a8672019-12-02 22:49:38 +01001183 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001185 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 case WM_LBUTTONDOWN:
1187 case WM_LBUTTONUP:
1188 case WM_MBUTTONDOWN:
1189 case WM_MBUTTONUP:
1190 case WM_RBUTTONDOWN:
1191 case WM_RBUTTONUP:
1192 case WM_XBUTTONDOWN:
1193 case WM_XBUTTONUP:
1194 case WM_NCMOUSEMOVE:
1195 case WM_NCLBUTTONDOWN:
1196 case WM_NCLBUTTONUP:
1197 case WM_NCMBUTTONDOWN:
1198 case WM_NCMBUTTONUP:
1199 case WM_NCRBUTTONDOWN:
1200 case WM_NCRBUTTONUP:
1201 case WM_KILLFOCUS:
1202 /*
1203 * if the pointer is currently hidden, then we should show it.
1204 */
1205 gui_mch_mousehide(FALSE);
1206 break;
1207 }
1208}
1209
1210 static LRESULT CALLBACK
1211_TextAreaWndProc(
1212 HWND hwnd,
1213 UINT uMsg,
1214 WPARAM wParam,
1215 LPARAM lParam)
1216{
1217 /*
1218 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1219 hwnd, uMsg, wParam, lParam);
1220 */
1221
1222 HandleMouseHide(uMsg, lParam);
1223
1224 s_uMsg = uMsg;
1225 s_wParam = wParam;
1226 s_lParam = lParam;
1227
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001228#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001229 TrackUserActivity(uMsg);
1230#endif
1231
1232 switch (uMsg)
1233 {
1234 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1235 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1236 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1237 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1238 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1239 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1240 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1241 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1242 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1243 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1245 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1247 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001248 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001250#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001251 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1252 return TRUE;
1253#endif
1254 default:
1255 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1256 }
1257}
1258
Bram Moolenaar945c8572020-07-17 22:17:03 +02001259 static LRESULT WINAPI
1260MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001262#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001264#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001265 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266#endif
1267}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269/*
1270 * Called when the foreground or background color has been changed.
1271 */
1272 void
1273gui_mch_new_colors(void)
1274{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001275 HBRUSH prevBrush;
1276
1277 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001278 prevBrush = (HBRUSH)SetClassLongPtr(
1279 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001280 InvalidateRect(s_hwnd, NULL, TRUE);
1281 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282}
1283
1284/*
1285 * Set the colors to their default values.
1286 */
1287 void
1288gui_mch_def_colors(void)
1289{
1290 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1291 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1292 gui.def_norm_pixel = gui.norm_pixel;
1293 gui.def_back_pixel = gui.back_pixel;
1294}
1295
1296/*
1297 * Open the GUI window which was created by a call to gui_mch_init().
1298 */
1299 int
1300gui_mch_open(void)
1301{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001302 // Actually open the window, if not already visible
1303 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304 if (!IsWindowVisible(s_hwnd))
1305 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1306
1307#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001308 // Init replace string here, so that we keep it when re-opening the
1309 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001310 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1311#endif
1312
1313 return OK;
1314}
1315
1316/*
1317 * Get the position of the top left corner of the window.
1318 */
1319 int
1320gui_mch_get_winpos(int *x, int *y)
1321{
1322 RECT rect;
1323
1324 GetWindowRect(s_hwnd, &rect);
1325 *x = rect.left;
1326 *y = rect.top;
1327 return OK;
1328}
1329
1330/*
1331 * Set the position of the top left corner of the window to the given
1332 * coordinates.
1333 */
1334 void
1335gui_mch_set_winpos(int x, int y)
1336{
1337 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1338 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1339}
1340 void
1341gui_mch_set_text_area_pos(int x, int y, int w, int h)
1342{
1343 static int oldx = 0;
1344 static int oldy = 0;
1345
1346 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1347
1348#ifdef FEAT_TOOLBAR
1349 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1350 SendMessage(s_toolbarhwnd, WM_SIZE,
1351 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1352#endif
1353#if defined(FEAT_GUI_TABLINE)
1354 if (showing_tabline)
1355 {
1356 int top = 0;
1357 RECT rect;
1358
1359# ifdef FEAT_TOOLBAR
1360 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1361 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1362# endif
1363 GetClientRect(s_hwnd, &rect);
1364 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1365 }
1366#endif
1367
Bram Moolenaar734a8672019-12-02 22:49:38 +01001368 // When side scroll bar is unshown, the size of window will change.
1369 // then, the text area move left or right. thus client rect should be
1370 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001371 if (oldx != x || oldy != y)
1372 {
1373 InvalidateRect(s_hwnd, NULL, FALSE);
1374 oldx = x;
1375 oldy = y;
1376 }
1377}
1378
1379
1380/*
1381 * Scrollbar stuff:
1382 */
1383
1384 void
1385gui_mch_enable_scrollbar(
1386 scrollbar_T *sb,
1387 int flag)
1388{
1389 ShowScrollBar(sb->id, SB_CTL, flag);
1390
Bram Moolenaar734a8672019-12-02 22:49:38 +01001391 // TODO: When the window is maximized, the size of the window stays the
1392 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1393 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001394}
1395
1396 void
1397gui_mch_set_scrollbar_pos(
1398 scrollbar_T *sb,
1399 int x,
1400 int y,
1401 int w,
1402 int h)
1403{
1404 SetWindowPos(sb->id, NULL, x, y, w, h,
1405 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1406}
1407
Bram Moolenaar203ec772020-07-17 20:43:43 +02001408 int
1409gui_mch_get_scrollbar_xpadding(void)
1410{
1411 RECT rcTxt, rcWnd;
1412 int xpad;
1413
1414 GetWindowRect(s_textArea, &rcTxt);
1415 GetWindowRect(s_hwnd, &rcWnd);
1416 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1417 - GetSystemMetrics(SM_CXFRAME)
1418 - GetSystemMetrics(SM_CXPADDEDBORDER);
1419 return (xpad < 0) ? 0 : xpad;
1420}
1421
1422 int
1423gui_mch_get_scrollbar_ypadding(void)
1424{
1425 RECT rcTxt, rcWnd;
1426 int ypad;
1427
1428 GetWindowRect(s_textArea, &rcTxt);
1429 GetWindowRect(s_hwnd, &rcWnd);
1430 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1431 - GetSystemMetrics(SM_CYFRAME)
1432 - GetSystemMetrics(SM_CXPADDEDBORDER);
1433 return (ypad < 0) ? 0 : ypad;
1434}
1435
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001436 void
1437gui_mch_create_scrollbar(
1438 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001439 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001440{
1441 sb->id = CreateWindow(
1442 "SCROLLBAR", "Scrollbar",
1443 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001444 10, // Any value will do for now
1445 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001446 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001447 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448}
1449
1450/*
1451 * Find the scrollbar with the given hwnd.
1452 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001453 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001454gui_mswin_find_scrollbar(HWND hwnd)
1455{
1456 win_T *wp;
1457
1458 if (gui.bottom_sbar.id == hwnd)
1459 return &gui.bottom_sbar;
1460 FOR_ALL_WINDOWS(wp)
1461 {
1462 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1463 return &wp->w_scrollbars[SBAR_LEFT];
1464 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1465 return &wp->w_scrollbars[SBAR_RIGHT];
1466 }
1467 return NULL;
1468}
1469
1470/*
1471 * Get the character size of a font.
1472 */
1473 static void
1474GetFontSize(GuiFont font)
1475{
1476 HWND hwnd = GetDesktopWindow();
1477 HDC hdc = GetWindowDC(hwnd);
1478 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001479 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001480 TEXTMETRIC tm;
1481
1482 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001483 // GetTextMetrics() may not return the right value in tmAveCharWidth
1484 // for some fonts. Do our own average computation.
1485 GetTextExtentPoint(hdc,
1486 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1487 52, &size);
1488 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001489
1490 gui.char_height = tm.tmHeight + p_linespace;
1491
1492 SelectFont(hdc, hfntOld);
1493
1494 ReleaseDC(hwnd, hdc);
1495}
1496
1497/*
1498 * Adjust gui.char_height (after 'linespace' was changed).
1499 */
1500 int
1501gui_mch_adjust_charheight(void)
1502{
1503 GetFontSize(gui.norm_font);
1504 return OK;
1505}
1506
1507 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001508get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001509{
1510 HFONT font = NULL;
1511
Bram Moolenaar734a8672019-12-02 22:49:38 +01001512 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001513 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001514
1515 if (font == NULL)
1516 return NOFONT;
1517
1518 return (GuiFont)font;
1519}
1520
1521 static int
1522pixels_to_points(int pixels, int vertical)
1523{
1524 int points;
1525 HWND hwnd;
1526 HDC hdc;
1527
1528 hwnd = GetDesktopWindow();
1529 hdc = GetWindowDC(hwnd);
1530
1531 points = MulDiv(pixels, 72,
1532 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1533
1534 ReleaseDC(hwnd, hdc);
1535
1536 return points;
1537}
1538
1539 GuiFont
1540gui_mch_get_font(
1541 char_u *name,
1542 int giveErrorIfMissing)
1543{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001544 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001545 GuiFont font = NOFONT;
1546
1547 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1548 font = get_font_handle(&lf);
1549 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001550 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001551 return font;
1552}
1553
1554#if defined(FEAT_EVAL) || defined(PROTO)
1555/*
1556 * Return the name of font "font" in allocated memory.
1557 * Don't know how to get the actual name, thus use the provided name.
1558 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001560gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001561{
1562 if (name == NULL)
1563 return NULL;
1564 return vim_strsave(name);
1565}
1566#endif
1567
1568 void
1569gui_mch_free_font(GuiFont font)
1570{
1571 if (font)
1572 DeleteObject((HFONT)font);
1573}
1574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001575/*
1576 * Return the Pixel value (color) for the given color name.
1577 * Return INVALCOLOR for error.
1578 */
1579 guicolor_T
1580gui_mch_get_color(char_u *name)
1581{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001582 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001583
1584 typedef struct SysColorTable
1585 {
1586 char *name;
1587 int color;
1588 } SysColorTable;
1589
1590 static SysColorTable sys_table[] =
1591 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1593 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001594#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1596#endif
1597 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1598 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1599 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1600 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1601 {"SYS_DESKTOP", COLOR_DESKTOP},
1602 {"SYS_INFOBK", COLOR_INFOBK},
1603 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1604 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001605 {"SYS_BTNFACE", COLOR_BTNFACE},
1606 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1607 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1608 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1609 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1610 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1611 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1612 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1613 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1614 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1615 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1616 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1617 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1618 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1619 {"SYS_MENU", COLOR_MENU},
1620 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1621 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1622 {"SYS_WINDOW", COLOR_WINDOW},
1623 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1624 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1625 };
1626
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 /*
1628 * Try to look up a system colour.
1629 */
1630 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1631 if (STRICMP(name, sys_table[i].name) == 0)
1632 return GetSysColor(sys_table[i].color);
1633
Bram Moolenaarab302212016-04-26 20:59:29 +02001634 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001635}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001636
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001637 guicolor_T
1638gui_mch_get_rgb_color(int r, int g, int b)
1639{
1640 return gui_get_rgb_color_cmn(r, g, b);
1641}
1642
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643/*
1644 * Return OK if the key with the termcap name "name" is supported.
1645 */
1646 int
1647gui_mch_haskey(char_u *name)
1648{
1649 int i;
1650
1651 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1652 if (name[0] == special_keys[i].vim_code0 &&
1653 name[1] == special_keys[i].vim_code1)
1654 return OK;
1655 return FAIL;
1656}
1657
1658 void
1659gui_mch_beep(void)
1660{
1661 MessageBeep(MB_OK);
1662}
1663/*
1664 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1665 */
1666 void
1667gui_mch_invert_rectangle(
1668 int r,
1669 int c,
1670 int nr,
1671 int nc)
1672{
1673 RECT rc;
1674
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001675#if defined(FEAT_DIRECTX)
1676 if (IS_ENABLE_DIRECTX())
1677 DWriteContext_Flush(s_dwc);
1678#endif
1679
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680 /*
1681 * Note: InvertRect() excludes right and bottom of rectangle.
1682 */
1683 rc.left = FILL_X(c);
1684 rc.top = FILL_Y(r);
1685 rc.right = rc.left + nc * gui.char_width;
1686 rc.bottom = rc.top + nr * gui.char_height;
1687 InvertRect(s_hdc, &rc);
1688}
1689
1690/*
1691 * Iconify the GUI window.
1692 */
1693 void
1694gui_mch_iconify(void)
1695{
1696 ShowWindow(s_hwnd, SW_MINIMIZE);
1697}
1698
1699/*
1700 * Draw a cursor without focus.
1701 */
1702 void
1703gui_mch_draw_hollow_cursor(guicolor_T color)
1704{
1705 HBRUSH hbr;
1706 RECT rc;
1707
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001708#if defined(FEAT_DIRECTX)
1709 if (IS_ENABLE_DIRECTX())
1710 DWriteContext_Flush(s_dwc);
1711#endif
1712
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001713 /*
1714 * Note: FrameRect() excludes right and bottom of rectangle.
1715 */
1716 rc.left = FILL_X(gui.col);
1717 rc.top = FILL_Y(gui.row);
1718 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001719 if (mb_lefthalve(gui.row, gui.col))
1720 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001721 rc.bottom = rc.top + gui.char_height;
1722 hbr = CreateSolidBrush(color);
1723 FrameRect(s_hdc, &rc, hbr);
1724 DeleteBrush(hbr);
1725}
1726/*
1727 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1728 * color "color".
1729 */
1730 void
1731gui_mch_draw_part_cursor(
1732 int w,
1733 int h,
1734 guicolor_T color)
1735{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001736 RECT rc;
1737
1738 /*
1739 * Note: FillRect() excludes right and bottom of rectangle.
1740 */
1741 rc.left =
1742#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001743 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001744 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1745#endif
1746 FILL_X(gui.col);
1747 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1748 rc.right = rc.left + w;
1749 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001750
Bram Moolenaar92467d32017-12-05 13:22:16 +01001751 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001752}
1753
1754
1755/*
1756 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1757 * dead key's nominal character and re-post the original message.
1758 */
1759 static void
1760outputDeadKey_rePost(MSG originalMsg)
1761{
1762 static MSG deadCharExpel;
1763
1764 if (!dead_key)
1765 return;
1766
1767 dead_key = 0;
1768
Bram Moolenaar734a8672019-12-02 22:49:38 +01001769 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001770 deadCharExpel.message = originalMsg.message;
1771 deadCharExpel.hwnd = originalMsg.hwnd;
1772 deadCharExpel.wParam = VK_SPACE;
1773
1774 MyTranslateMessage(&deadCharExpel);
1775
Bram Moolenaar734a8672019-12-02 22:49:38 +01001776 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1778 originalMsg.lParam);
1779}
1780
1781
1782/*
1783 * Process a single Windows message.
1784 * If one is not available we hang until one is.
1785 */
1786 static void
1787process_message(void)
1788{
1789 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001790 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001791 char_u string[40];
1792 int i;
1793 int modifiers = 0;
1794 int key;
1795#ifdef FEAT_MENU
1796 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1797#endif
1798
1799 pGetMessage(&msg, NULL, 0, 0);
1800
1801#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001802 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001803 if (msg.message == WM_OLE)
1804 {
1805 char_u *str = (char_u *)msg.lParam;
1806 if (str == NULL || *str == NUL)
1807 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001808 // Message can't be ours, forward it. Fixes problem with Ultramon
1809 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001810 pDispatchMessage(&msg);
1811 }
1812 else
1813 {
1814 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001815 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001816 }
1817 return;
1818 }
1819#endif
1820
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001822 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1824 {
1825 HandleMouseHide(msg.message, msg.lParam);
1826 return;
1827 }
1828#endif
1829
1830 /*
1831 * Check if it's a special key that we recognise. If not, call
1832 * TranslateMessage().
1833 */
1834 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1835 {
1836 vk = (int) msg.wParam;
1837
1838 /*
1839 * Handle dead keys in special conditions in other cases we let Windows
1840 * handle them and do not interfere.
1841 *
1842 * The dead_key flag must be reset on several occasions:
1843 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1844 * consumed at that point (This is when we let Windows combine the
1845 * dead character on its own)
1846 *
1847 * - Before doing something special such as regenerating keypresses to
1848 * expel the dead character as this could trigger an infinite loop if
1849 * for some reason MyTranslateMessage() do not trigger a call
1850 * immediately to _OnChar() (or _OnSysChar()).
1851 */
1852 if (dead_key)
1853 {
1854 /*
1855 * If a dead key was pressed and the user presses VK_SPACE,
1856 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1857 * with the dead char now, so do nothing special and let Windows
1858 * handle it.
1859 *
1860 * Note that VK_SPACE combines with the dead_key's character and
1861 * only one WM_CHAR will be generated by TranslateMessage(), in
1862 * the two other cases two WM_CHAR will be generated: the dead
1863 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1864 * user expects.
1865 */
1866 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1867 {
1868 dead_key = 0;
1869 MyTranslateMessage(&msg);
1870 return;
1871 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001872 // In modes where we are not typing, dead keys should behave
1873 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1875 {
1876 outputDeadKey_rePost(msg);
1877 return;
1878 }
1879 }
1880
Bram Moolenaar734a8672019-12-02 22:49:38 +01001881 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001882 if (vk == VK_CANCEL)
1883 {
1884 trash_input_buf();
1885 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001886 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001887 string[0] = Ctrl_C;
1888 add_to_input_buf(string, 1);
1889 }
1890
1891 for (i = 0; special_keys[i].key_sym != 0; i++)
1892 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894 if (special_keys[i].key_sym == vk
1895 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1896 {
1897 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001898 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001899 * is a key that would normally trigger the dead key nominal
1900 * character output (such as a NUMPAD printable character or
1901 * the TAB key, etc...).
1902 */
1903 if (dead_key && (special_keys[i].vim_code0 == 'K'
1904 || vk == VK_TAB || vk == CAR))
1905 {
1906 outputDeadKey_rePost(msg);
1907 return;
1908 }
1909
1910#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001911 // Check for <F10>: Windows selects the menu. When <F10> is
1912 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001913 if (vk == VK_F10
1914 && gui.menu_is_active
1915 && check_map(k10, State, FALSE, TRUE, FALSE,
1916 NULL, NULL) == NULL)
1917 break;
1918#endif
1919 if (GetKeyState(VK_SHIFT) & 0x8000)
1920 modifiers |= MOD_MASK_SHIFT;
1921 /*
1922 * Don't use caps-lock as shift, because these are special keys
1923 * being considered here, and we only want letters to get
1924 * shifted -- webb
1925 */
1926 /*
1927 if (GetKeyState(VK_CAPITAL) & 0x0001)
1928 modifiers ^= MOD_MASK_SHIFT;
1929 */
1930 if (GetKeyState(VK_CONTROL) & 0x8000)
1931 modifiers |= MOD_MASK_CTRL;
1932 if (GetKeyState(VK_MENU) & 0x8000)
1933 modifiers |= MOD_MASK_ALT;
1934
1935 if (special_keys[i].vim_code1 == NUL)
1936 key = special_keys[i].vim_code0;
1937 else
1938 key = TO_SPECIAL(special_keys[i].vim_code0,
1939 special_keys[i].vim_code1);
1940 key = simplify_key(key, &modifiers);
1941 if (key == CSI)
1942 key = K_CSI;
1943
1944 if (modifiers)
1945 {
1946 string[0] = CSI;
1947 string[1] = KS_MODIFIER;
1948 string[2] = modifiers;
1949 add_to_input_buf(string, 3);
1950 }
1951
1952 if (IS_SPECIAL(key))
1953 {
1954 string[0] = CSI;
1955 string[1] = K_SECOND(key);
1956 string[2] = K_THIRD(key);
1957 add_to_input_buf(string, 3);
1958 }
1959 else
1960 {
1961 int len;
1962
Bram Moolenaar734a8672019-12-02 22:49:38 +01001963 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001964 len = char_to_string(key, string, 40, FALSE);
1965 add_to_input_buf(string, len);
1966 }
1967 break;
1968 }
1969 }
1970 if (special_keys[i].key_sym == 0)
1971 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001972 // Some keys need C-S- where they should only need C-.
1973 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1974 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001975 if (vk != 0xff
1976 && (GetKeyState(VK_CONTROL) & 0x8000)
1977 && !(GetKeyState(VK_SHIFT) & 0x8000)
1978 && !(GetKeyState(VK_MENU) & 0x8000))
1979 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001980 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001981 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1982 {
1983 string[0] = Ctrl_HAT;
1984 add_to_input_buf(string, 1);
1985 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001986 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
1987 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001988 {
1989 string[0] = Ctrl__;
1990 add_to_input_buf(string, 1);
1991 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001992 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001993 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1994 {
1995 string[0] = Ctrl_AT;
1996 add_to_input_buf(string, 1);
1997 }
1998 else
1999 MyTranslateMessage(&msg);
2000 }
2001 else
2002 MyTranslateMessage(&msg);
2003 }
2004 }
2005#ifdef FEAT_MBYTE_IME
2006 else if (msg.message == WM_IME_NOTIFY)
2007 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2008 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002009 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002010 MyTranslateMessage(&msg);
2011#endif
2012#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002013// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002014 else if (msg.message == WM_IME_STARTCOMPOSITION)
2015 {
2016 POINT point;
2017
2018 global_ime_set_font(&norm_logfont);
2019 point.x = FILL_X(gui.col);
2020 point.y = FILL_Y(gui.row);
2021 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2022 global_ime_set_position(&point);
2023 }
2024#endif
2025
2026#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002027 // Check for <F10>: Default effect is to select the menu. When <F10> is
2028 // mapped we need to stop it here to avoid strange effects (e.g., for the
2029 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2031 NULL, NULL) == NULL)
2032#endif
2033 pDispatchMessage(&msg);
2034}
2035
2036/*
2037 * Catch up with any queued events. This may put keyboard input into the
2038 * input buffer, call resize call-backs, trigger timers etc. If there is
2039 * nothing in the event queue (& no timers pending), then we return
2040 * immediately.
2041 */
2042 void
2043gui_mch_update(void)
2044{
2045 MSG msg;
2046
2047 if (!s_busy_processing)
2048 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2049 && !vim_is_input_buf_full())
2050 process_message();
2051}
2052
Bram Moolenaar4231da42016-06-02 14:30:04 +02002053 static void
2054remove_any_timer(void)
2055{
2056 MSG msg;
2057
2058 if (s_wait_timer != 0 && !s_timed_out)
2059 {
2060 KillTimer(NULL, s_wait_timer);
2061
Bram Moolenaar734a8672019-12-02 22:49:38 +01002062 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002063 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2064 ;
2065 s_wait_timer = 0;
2066 }
2067}
2068
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069/*
2070 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2071 * from the keyboard.
2072 * wtime == -1 Wait forever.
2073 * wtime == 0 This should never happen.
2074 * wtime > 0 Wait wtime milliseconds for a character.
2075 * Returns OK if a character was found to be available within the given time,
2076 * or FAIL otherwise.
2077 */
2078 int
2079gui_mch_wait_for_chars(int wtime)
2080{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081 int focus;
2082
2083 s_timed_out = FALSE;
2084
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002085 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002086 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002087 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088 if (s_busy_processing)
2089 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002090
2091 // When called with "wtime" zero, just want one msec.
2092 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 (TIMERPROC)_OnTimer);
2094 }
2095
2096 allow_scrollbar = TRUE;
2097
2098 focus = gui.in_focus;
2099 while (!s_timed_out)
2100 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002101 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002102 if (gui.in_focus != focus)
2103 {
2104 if (gui.in_focus)
2105 gui_mch_start_blink();
2106 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002107 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108 focus = gui.in_focus;
2109 }
2110
2111 if (s_need_activate)
2112 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 s_need_activate = FALSE;
2115 }
2116
Bram Moolenaar4231da42016-06-02 14:30:04 +02002117#ifdef FEAT_TIMERS
2118 did_add_timer = FALSE;
2119#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002121 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002122 for (;;)
2123 {
2124 MSG msg;
2125
2126 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002127# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002128 if (did_add_timer)
2129 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002130# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002131 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2132 {
2133 process_message();
2134 break;
2135 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 else if (input_available()
2137 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2138 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002139 break;
2140 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002141#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002142 // Don't use gui_mch_update() because then we will spin-lock until a
2143 // char arrives, instead we use GetMessage() to hang until an
2144 // event arrives. No need to check for input_buf_full because we are
2145 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002146 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002147#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148
2149 if (input_available())
2150 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002151 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002152 allow_scrollbar = FALSE;
2153
Bram Moolenaar89c00032019-09-04 13:53:21 +02002154 // Clear pending mouse button, the release event may have been
2155 // taken by the dialog window. But don't do this when getting
2156 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002157 if (!s_getting_focus)
2158 s_button_pending = -1;
2159
2160 return OK;
2161 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002162
2163#ifdef FEAT_TIMERS
2164 if (did_add_timer)
2165 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002166 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002167 remove_any_timer();
2168 break;
2169 }
2170#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002171 }
2172 allow_scrollbar = FALSE;
2173 return FAIL;
2174}
2175
2176/*
2177 * Clear a rectangular region of the screen from text pos (row1, col1) to
2178 * (row2, col2) inclusive.
2179 */
2180 void
2181gui_mch_clear_block(
2182 int row1,
2183 int col1,
2184 int row2,
2185 int col2)
2186{
2187 RECT rc;
2188
2189 /*
2190 * Clear one extra pixel at the far right, for when bold characters have
2191 * spilled over to the window border.
2192 * Note: FillRect() excludes right and bottom of rectangle.
2193 */
2194 rc.left = FILL_X(col1);
2195 rc.top = FILL_Y(row1);
2196 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2197 rc.bottom = FILL_Y(row2 + 1);
2198 clear_rect(&rc);
2199}
2200
2201/*
2202 * Clear the whole text window.
2203 */
2204 void
2205gui_mch_clear_all(void)
2206{
2207 RECT rc;
2208
2209 rc.left = 0;
2210 rc.top = 0;
2211 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2212 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2213 clear_rect(&rc);
2214}
2215/*
2216 * Menu stuff.
2217 */
2218
2219 void
2220gui_mch_enable_menu(int flag)
2221{
2222#ifdef FEAT_MENU
2223 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2224#endif
2225}
2226
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002227 void
2228gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002229 int x UNUSED,
2230 int y UNUSED,
2231 int w UNUSED,
2232 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002234 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002235}
2236
2237#if defined(FEAT_MENU) || defined(PROTO)
2238/*
2239 * Make menu item hidden or not hidden
2240 */
2241 void
2242gui_mch_menu_hidden(
2243 vimmenu_T *menu,
2244 int hidden)
2245{
2246 /*
2247 * This doesn't do what we want. Hmm, just grey the menu items for now.
2248 */
2249 /*
2250 if (hidden)
2251 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2252 else
2253 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2254 */
2255 gui_mch_menu_grey(menu, hidden);
2256}
2257
2258/*
2259 * This is called after setting all the menus to grey/hidden or not.
2260 */
2261 void
2262gui_mch_draw_menubar(void)
2263{
2264 DrawMenuBar(s_hwnd);
2265}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002266#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002267
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002268/*
2269 * Return the RGB value of a pixel as a long.
2270 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002271 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002272gui_mch_get_rgb(guicolor_T pixel)
2273{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002274 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2275 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002276}
2277
2278#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002279/*
2280 * Convert pixels in X to dialog units
2281 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002282 static WORD
2283PixelToDialogX(int numPixels)
2284{
2285 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2286}
2287
Bram Moolenaar734a8672019-12-02 22:49:38 +01002288/*
2289 * Convert pixels in Y to dialog units
2290 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002291 static WORD
2292PixelToDialogY(int numPixels)
2293{
2294 return (WORD)((numPixels * 8) / s_dlgfntheight);
2295}
2296
Bram Moolenaar734a8672019-12-02 22:49:38 +01002297/*
2298 * Return the width in pixels of the given text in the given DC.
2299 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002300 static int
2301GetTextWidth(HDC hdc, char_u *str, int len)
2302{
2303 SIZE size;
2304
2305 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2306 return size.cx;
2307}
2308
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309/*
2310 * Return the width in pixels of the given text in the given DC, taking care
2311 * of 'encoding' to active codepage conversion.
2312 */
2313 static int
2314GetTextWidthEnc(HDC hdc, char_u *str, int len)
2315{
2316 SIZE size;
2317 WCHAR *wstr;
2318 int n;
2319 int wlen = len;
2320
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002321 wstr = enc_to_utf16(str, &wlen);
2322 if (wstr == NULL)
2323 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002324
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002325 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2326 vim_free(wstr);
2327 if (n)
2328 return size.cx;
2329 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002330}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002332static void get_work_area(RECT *spi_rect);
2333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002334/*
2335 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002336 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2337 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 */
2339 static BOOL
2340CenterWindow(
2341 HWND hwndChild,
2342 HWND hwndParent)
2343{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002344 HMONITOR mon;
2345 MONITORINFO moninfo;
2346 RECT rChild, rParent, rScreen;
2347 int wChild, hChild, wParent, hParent;
2348 int xNew, yNew;
2349 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002350
2351 GetWindowRect(hwndChild, &rChild);
2352 wChild = rChild.right - rChild.left;
2353 hChild = rChild.bottom - rChild.top;
2354
Bram Moolenaar734a8672019-12-02 22:49:38 +01002355 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002356 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002357 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 else
2359 GetWindowRect(hwndParent, &rParent);
2360 wParent = rParent.right - rParent.left;
2361 hParent = rParent.bottom - rParent.top;
2362
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002363 moninfo.cbSize = sizeof(MONITORINFO);
2364 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2365 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002367 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002368 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002369 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002370 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002371 hdc = GetDC(hwndChild);
2372 rScreen.left = 0;
2373 rScreen.top = 0;
2374 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2375 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2376 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377 }
2378
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002379 xNew = rParent.left + ((wParent - wChild) / 2);
2380 if (xNew < rScreen.left)
2381 xNew = rScreen.left;
2382 else if ((xNew + wChild) > rScreen.right)
2383 xNew = rScreen.right - wChild;
2384
2385 yNew = rParent.top + ((hParent - hChild) / 2);
2386 if (yNew < rScreen.top)
2387 yNew = rScreen.top;
2388 else if ((yNew + hChild) > rScreen.bottom)
2389 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002390
2391 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2392 SWP_NOSIZE | SWP_NOZORDER);
2393}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002394#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002395
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396#if defined(FEAT_TOOLBAR) || defined(PROTO)
2397 void
2398gui_mch_show_toolbar(int showit)
2399{
2400 if (s_toolbarhwnd == NULL)
2401 return;
2402
2403 if (showit)
2404 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002405# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002406 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002407# define TB_SETUNICODEFORMAT 0x2005
2408# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002409 // Enable unicode support
2410 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2411 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002412 ShowWindow(s_toolbarhwnd, SW_SHOW);
2413 }
2414 else
2415 ShowWindow(s_toolbarhwnd, SW_HIDE);
2416}
2417
Bram Moolenaar734a8672019-12-02 22:49:38 +01002418// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002419# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002420
2421#endif
2422
2423#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2424 static void
2425add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2426{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002427 WCHAR *wn;
2428 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002429
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002430 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002431 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002432 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002434 infow.cbSize = sizeof(infow);
2435 infow.fMask = MIIM_TYPE | MIIM_ID;
2436 infow.wID = item_id;
2437 infow.fType = MFT_STRING;
2438 infow.dwTypeData = wn;
2439 infow.cch = (UINT)wcslen(wn);
2440 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2441 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442}
2443
2444 static void
2445show_tabline_popup_menu(void)
2446{
2447 HMENU tab_pmenu;
2448 long rval;
2449 POINT pt;
2450
Bram Moolenaar734a8672019-12-02 22:49:38 +01002451 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002452 if (hold_gui_events
2453# ifdef FEAT_CMDWIN
2454 || cmdwin_type != 0
2455# endif
2456 )
2457 return;
2458
2459 tab_pmenu = CreatePopupMenu();
2460 if (tab_pmenu == NULL)
2461 return;
2462
2463 if (first_tabpage->tp_next != NULL)
2464 add_tabline_popup_menu_entry(tab_pmenu,
2465 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2466 add_tabline_popup_menu_entry(tab_pmenu,
2467 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2468 add_tabline_popup_menu_entry(tab_pmenu,
2469 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2470
2471 GetCursorPos(&pt);
2472 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2473 NULL);
2474
2475 DestroyMenu(tab_pmenu);
2476
Bram Moolenaar734a8672019-12-02 22:49:38 +01002477 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002478 if (rval > 0)
2479 {
2480 TCHITTESTINFO htinfo;
2481 int idx;
2482
2483 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2484 return;
2485
2486 htinfo.pt.x = pt.x;
2487 htinfo.pt.y = pt.y;
2488 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2489 if (idx == -1)
2490 idx = 0;
2491 else
2492 idx += 1;
2493
2494 send_tabline_menu_event(idx, (int)rval);
2495 }
2496}
2497
2498/*
2499 * Show or hide the tabline.
2500 */
2501 void
2502gui_mch_show_tabline(int showit)
2503{
2504 if (s_tabhwnd == NULL)
2505 return;
2506
2507 if (!showit != !showing_tabline)
2508 {
2509 if (showit)
2510 ShowWindow(s_tabhwnd, SW_SHOW);
2511 else
2512 ShowWindow(s_tabhwnd, SW_HIDE);
2513 showing_tabline = showit;
2514 }
2515}
2516
2517/*
2518 * Return TRUE when tabline is displayed.
2519 */
2520 int
2521gui_mch_showing_tabline(void)
2522{
2523 return s_tabhwnd != NULL && showing_tabline;
2524}
2525
2526/*
2527 * Update the labels of the tabline.
2528 */
2529 void
2530gui_mch_update_tabline(void)
2531{
2532 tabpage_T *tp;
2533 TCITEM tie;
2534 int nr = 0;
2535 int curtabidx = 0;
2536 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002537 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002538
2539 if (s_tabhwnd == NULL)
2540 return;
2541
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002542# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002543 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002544# define CCM_SETUNICODEFORMAT 0x2005
2545# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002546 // Enable unicode support
2547 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002548
2549 tie.mask = TCIF_TEXT;
2550 tie.iImage = -1;
2551
Bram Moolenaar734a8672019-12-02 22:49:38 +01002552 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2554
Bram Moolenaar734a8672019-12-02 22:49:38 +01002555 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002556 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2557 {
2558 if (tp == curtab)
2559 curtabidx = nr;
2560
2561 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2562 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002563 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002564 tie.pszText = "-Empty-";
2565 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2566 tabadded = 1;
2567 }
2568
2569 get_tabline_label(tp, FALSE);
2570 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002571
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002572 wstr = enc_to_utf16(NameBuff, NULL);
2573 if (wstr != NULL)
2574 {
2575 TCITEMW tiw;
2576
2577 tiw.mask = TCIF_TEXT;
2578 tiw.iImage = -1;
2579 tiw.pszText = wstr;
2580 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2581 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583 }
2584
Bram Moolenaar734a8672019-12-02 22:49:38 +01002585 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2587 TabCtrl_DeleteItem(s_tabhwnd, nr);
2588
2589 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2590 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2591
Bram Moolenaar734a8672019-12-02 22:49:38 +01002592 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2594 RedrawWindow(s_tabhwnd, NULL, NULL,
2595 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2596
2597 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2598 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2599}
2600
2601/*
2602 * Set the current tab to "nr". First tab is 1.
2603 */
2604 void
2605gui_mch_set_curtab(int nr)
2606{
2607 if (s_tabhwnd == NULL)
2608 return;
2609
2610 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2611 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2612}
2613
2614#endif
2615
2616/*
2617 * ":simalt" command.
2618 */
2619 void
2620ex_simalt(exarg_T *eap)
2621{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002622 char_u *keys = eap->arg;
2623 int fill_typebuf = FALSE;
2624 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625
2626 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2627 while (*keys)
2628 {
2629 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002630 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002631 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2632 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002633 fill_typebuf = TRUE;
2634 }
2635 if (fill_typebuf)
2636 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002637 // Put a NOP in the typeahead buffer so that the message will get
2638 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002639 key_name[0] = K_SPECIAL;
2640 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002641 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002642 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002643#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002644 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002645#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002646 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647 }
2648}
2649
2650/*
2651 * Create the find & replace dialogs.
2652 * You can't have both at once: ":find" when replace is showing, destroys
2653 * the replace dialog first, and the other way around.
2654 */
2655#ifdef MSWIN_FIND_REPLACE
2656 static void
2657initialise_findrep(char_u *initial_string)
2658{
2659 int wword = FALSE;
2660 int mcase = !p_ic;
2661 char_u *entry_text;
2662
Bram Moolenaar734a8672019-12-02 22:49:38 +01002663 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002664 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2665
2666 s_findrep_struct.hwndOwner = s_hwnd;
2667 s_findrep_struct.Flags = FR_DOWN;
2668 if (mcase)
2669 s_findrep_struct.Flags |= FR_MATCHCASE;
2670 if (wword)
2671 s_findrep_struct.Flags |= FR_WHOLEWORD;
2672 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002673 {
2674 WCHAR *p = enc_to_utf16(entry_text, NULL);
2675 if (p != NULL)
2676 {
2677 int len = s_findrep_struct.wFindWhatLen - 1;
2678
2679 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2680 s_findrep_struct.lpstrFindWhat[len] = NUL;
2681 vim_free(p);
2682 }
2683 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002684 vim_free(entry_text);
2685}
2686#endif
2687
2688 static void
2689set_window_title(HWND hwnd, char *title)
2690{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002691 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002692 {
2693 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002694
Bram Moolenaar734a8672019-12-02 22:49:38 +01002695 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002696 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2697 if (wbuf != NULL)
2698 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002699 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002700 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701 }
2702 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002703 else
2704 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002705}
2706
2707 void
2708gui_mch_find_dialog(exarg_T *eap)
2709{
2710#ifdef MSWIN_FIND_REPLACE
2711 if (s_findrep_msg != 0)
2712 {
2713 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2714 DestroyWindow(s_findrep_hwnd);
2715
2716 if (!IsWindow(s_findrep_hwnd))
2717 {
2718 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002719 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 }
2721
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002722 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 (void)SetFocus(s_findrep_hwnd);
2724
2725 s_findrep_is_find = TRUE;
2726 }
2727#endif
2728}
2729
2730
2731 void
2732gui_mch_replace_dialog(exarg_T *eap)
2733{
2734#ifdef MSWIN_FIND_REPLACE
2735 if (s_findrep_msg != 0)
2736 {
2737 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2738 DestroyWindow(s_findrep_hwnd);
2739
2740 if (!IsWindow(s_findrep_hwnd))
2741 {
2742 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002743 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 }
2745
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002746 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 (void)SetFocus(s_findrep_hwnd);
2748
2749 s_findrep_is_find = FALSE;
2750 }
2751#endif
2752}
2753
2754
2755/*
2756 * Set visibility of the pointer.
2757 */
2758 void
2759gui_mch_mousehide(int hide)
2760{
2761 if (hide != gui.pointer_hidden)
2762 {
2763 ShowCursor(!hide);
2764 gui.pointer_hidden = hide;
2765 }
2766}
2767
2768#ifdef FEAT_MENU
2769 static void
2770gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2771{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002772 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 gui_mch_mousehide(FALSE);
2774
2775 (void)TrackPopupMenu(
2776 (HMENU)menu->submenu_id,
2777 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2778 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002779 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002780 s_hwnd,
2781 NULL);
2782 /*
2783 * NOTE: The pop-up menu can eat the mouse up event.
2784 * We deal with this in normal.c.
2785 */
2786}
2787#endif
2788
2789/*
2790 * Got a message when the system will go down.
2791 */
2792 static void
2793_OnEndSession(void)
2794{
2795 getout_preserve_modified(1);
2796}
2797
2798/*
2799 * Get this message when the user clicks on the cross in the top right corner
2800 * of a Windows95 window.
2801 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002802 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002803_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002804{
2805 gui_shell_closed();
2806}
2807
2808/*
2809 * Get a message when the window is being destroyed.
2810 */
2811 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002812_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002813{
2814 if (!destroying)
2815 _OnClose(hwnd);
2816}
2817
2818 static void
2819_OnPaint(
2820 HWND hwnd)
2821{
2822 if (!IsMinimized(hwnd))
2823 {
2824 PAINTSTRUCT ps;
2825
Bram Moolenaar734a8672019-12-02 22:49:38 +01002826 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002827 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828
Bram Moolenaar734a8672019-12-02 22:49:38 +01002829 // prevent multi-byte characters from misprinting on an invalid
2830 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831 if (has_mbyte)
2832 {
2833 RECT rect;
2834
2835 GetClientRect(hwnd, &rect);
2836 ps.rcPaint.left = rect.left;
2837 ps.rcPaint.right = rect.right;
2838 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002839
2840 if (!IsRectEmpty(&ps.rcPaint))
2841 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2843 ps.rcPaint.right - ps.rcPaint.left + 1,
2844 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2845 }
2846
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002847 EndPaint(hwnd, &ps);
2848 }
2849}
2850
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851 static void
2852_OnSize(
2853 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002854 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002855 int cx,
2856 int cy)
2857{
2858 if (!IsMinimized(hwnd))
2859 {
2860 gui_resize_shell(cx, cy);
2861
2862#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002863 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002864 gui_mswin_get_menu_height(TRUE);
2865#endif
2866 }
2867}
2868
2869 static void
2870_OnSetFocus(
2871 HWND hwnd,
2872 HWND hwndOldFocus)
2873{
2874 gui_focus_change(TRUE);
2875 s_getting_focus = TRUE;
2876 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2877}
2878
2879 static void
2880_OnKillFocus(
2881 HWND hwnd,
2882 HWND hwndNewFocus)
2883{
2884 gui_focus_change(FALSE);
2885 s_getting_focus = FALSE;
2886 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2887}
2888
2889/*
2890 * Get a message when the user switches back to vim
2891 */
2892 static LRESULT
2893_OnActivateApp(
2894 HWND hwnd,
2895 BOOL fActivate,
2896 DWORD dwThreadId)
2897{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002898 // we call gui_focus_change() in _OnSetFocus()
2899 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2901}
2902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 void
2904gui_mch_destroy_scrollbar(scrollbar_T *sb)
2905{
2906 DestroyWindow(sb->id);
2907}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002908
2909/*
2910 * Get current mouse coordinates in text window.
2911 */
2912 void
2913gui_mch_getmouse(int *x, int *y)
2914{
2915 RECT rct;
2916 POINT mp;
2917
2918 (void)GetWindowRect(s_textArea, &rct);
2919 (void)GetCursorPos((LPPOINT)&mp);
2920 *x = (int)(mp.x - rct.left);
2921 *y = (int)(mp.y - rct.top);
2922}
2923
2924/*
2925 * Move mouse pointer to character at (x, y).
2926 */
2927 void
2928gui_mch_setmouse(int x, int y)
2929{
2930 RECT rct;
2931
2932 (void)GetWindowRect(s_textArea, &rct);
2933 (void)SetCursorPos(x + gui.border_offset + rct.left,
2934 y + gui.border_offset + rct.top);
2935}
2936
2937 static void
2938gui_mswin_get_valid_dimensions(
2939 int w,
2940 int h,
2941 int *valid_w,
2942 int *valid_h)
2943{
2944 int base_width, base_height;
2945
2946 base_width = gui_get_base_width()
2947 + (GetSystemMetrics(SM_CXFRAME) +
2948 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2949 base_height = gui_get_base_height()
2950 + (GetSystemMetrics(SM_CYFRAME) +
2951 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2952 + GetSystemMetrics(SM_CYCAPTION)
2953#ifdef FEAT_MENU
2954 + gui_mswin_get_menu_height(FALSE)
2955#endif
2956 ;
2957 *valid_w = base_width +
2958 ((w - base_width) / gui.char_width) * gui.char_width;
2959 *valid_h = base_height +
2960 ((h - base_height) / gui.char_height) * gui.char_height;
2961}
2962
2963 void
2964gui_mch_flash(int msec)
2965{
2966 RECT rc;
2967
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002968#if defined(FEAT_DIRECTX)
2969 if (IS_ENABLE_DIRECTX())
2970 DWriteContext_Flush(s_dwc);
2971#endif
2972
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002973 /*
2974 * Note: InvertRect() excludes right and bottom of rectangle.
2975 */
2976 rc.left = 0;
2977 rc.top = 0;
2978 rc.right = gui.num_cols * gui.char_width;
2979 rc.bottom = gui.num_rows * gui.char_height;
2980 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002981 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002982
Bram Moolenaar734a8672019-12-02 22:49:38 +01002983 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984
2985 InvertRect(s_hdc, &rc);
2986}
2987
2988/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01002989 * Check if the specified point is on-screen. (multi-monitor aware)
2990 */
2991 static BOOL
2992is_point_onscreen(int x, int y)
2993{
2994 POINT pt = {x, y};
2995
2996 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
2997}
2998
2999/*
3000 * Check if the whole area of the specified window is on-screen.
3001 *
3002 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3003 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3004 * only works when the whole window is on-screen.
3005 */
3006 static BOOL
3007is_window_onscreen(HWND hwnd)
3008{
3009 RECT rc;
3010
3011 GetWindowRect(hwnd, &rc);
3012
3013 if (!is_point_onscreen(rc.left, rc.top))
3014 return FALSE;
3015 if (!is_point_onscreen(rc.left, rc.bottom))
3016 return FALSE;
3017 if (!is_point_onscreen(rc.right, rc.top))
3018 return FALSE;
3019 if (!is_point_onscreen(rc.right, rc.bottom))
3020 return FALSE;
3021 return TRUE;
3022}
3023
3024/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003025 * Return flags used for scrolling.
3026 * The SW_INVALIDATE is required when part of the window is covered or
3027 * off-screen. Refer to MS KB Q75236.
3028 */
3029 static int
3030get_scroll_flags(void)
3031{
3032 HWND hwnd;
3033 RECT rcVim, rcOther, rcDest;
3034
Bram Moolenaar185577e2020-10-29 20:08:21 +01003035 // Check if the window is (partly) off-screen.
3036 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003037 return SW_INVALIDATE;
3038
Bram Moolenaar734a8672019-12-02 22:49:38 +01003039 // Check if there is an window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003040 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003041 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3042 if (IsWindowVisible(hwnd))
3043 {
3044 GetWindowRect(hwnd, &rcOther);
3045 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3046 return SW_INVALIDATE;
3047 }
3048 return 0;
3049}
3050
3051/*
3052 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3053 * may not be scrolled out properly.
3054 * For gVim, when _OnScroll() is repeated, the character at the
3055 * previous cursor position may be left drawn after scroll.
3056 * The problem can be avoided by calling GetPixel() to get a pixel in
3057 * the region before ScrollWindowEx().
3058 */
3059 static void
3060intel_gpu_workaround(void)
3061{
3062 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3063}
3064
3065/*
3066 * Delete the given number of lines from the given row, scrolling up any
3067 * text further down within the scroll region.
3068 */
3069 void
3070gui_mch_delete_lines(
3071 int row,
3072 int num_lines)
3073{
3074 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003075
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 rc.left = FILL_X(gui.scroll_region_left);
3077 rc.right = FILL_X(gui.scroll_region_right + 1);
3078 rc.top = FILL_Y(row);
3079 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3080
Bram Moolenaar92467d32017-12-05 13:22:16 +01003081#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003082 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003083 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003084 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003085 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003086 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003087#endif
3088 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003089#if defined(FEAT_DIRECTX)
3090 if (IS_ENABLE_DIRECTX())
3091 DWriteContext_Flush(s_dwc);
3092#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003093 intel_gpu_workaround();
3094 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003095 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003096 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003097 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003098
Bram Moolenaar734a8672019-12-02 22:49:38 +01003099 // This seems to be required to avoid the cursor disappearing when
3100 // scrolling such that the cursor ends up in the top-left character on
3101 // the screen... But why? (Webb)
3102 // It's probably fixed by disabling drawing the cursor while scrolling.
3103 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003104
3105 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3106 gui.scroll_region_left,
3107 gui.scroll_region_bot, gui.scroll_region_right);
3108}
3109
3110/*
3111 * Insert the given number of lines before the given row, scrolling down any
3112 * following text within the scroll region.
3113 */
3114 void
3115gui_mch_insert_lines(
3116 int row,
3117 int num_lines)
3118{
3119 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003120
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003121 rc.left = FILL_X(gui.scroll_region_left);
3122 rc.right = FILL_X(gui.scroll_region_right + 1);
3123 rc.top = FILL_Y(row);
3124 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003125
3126#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003127 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003128 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003129 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003130 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003131 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003132#endif
3133 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003134#if defined(FEAT_DIRECTX)
3135 if (IS_ENABLE_DIRECTX())
3136 DWriteContext_Flush(s_dwc);
3137#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003138 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003139 // The SW_INVALIDATE is required when part of the window is covered or
3140 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003141 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003142 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003143 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003144 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003145
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003146 gui_clear_block(row, gui.scroll_region_left,
3147 row + num_lines - 1, gui.scroll_region_right);
3148}
3149
3150
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003152gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003153{
3154#if defined(FEAT_DIRECTX)
3155 DWriteContext_Close(s_dwc);
3156 DWrite_Final();
3157 s_dwc = NULL;
3158#endif
3159
3160 ReleaseDC(s_textArea, s_hdc);
3161 DeleteObject(s_brush);
3162
3163#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003164 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003165 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3166#endif
3167
Bram Moolenaar734a8672019-12-02 22:49:38 +01003168 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003169 if (s_hwnd != NULL)
3170 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003171 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 DestroyWindow(s_hwnd);
3173 }
3174
3175#ifdef GLOBAL_IME
3176 global_ime_end();
3177#endif
3178}
3179
3180 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003181logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003182{
3183 char *p;
3184 char *res;
3185 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003186 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003187 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003188 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003190 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3191 if (font_name == NULL)
3192 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003194 quality_name = quality_id2name((int)lf.lfQuality);
3195
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003196 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003197 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003198 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 if (res != NULL)
3200 {
3201 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003202 // make a normal font string out of the lf thing:
3203 points = pixels_to_points(
3204 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3205 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3206 sprintf((char *)p, "%s:h%d", font_name, points);
3207 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003208 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209 while (*p)
3210 {
3211 if (*p == ' ')
3212 *p = '_';
3213 ++p;
3214 }
3215 if (lf.lfItalic)
3216 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003217 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218 STRCAT(p, ":b");
3219 if (lf.lfUnderline)
3220 STRCAT(p, ":u");
3221 if (lf.lfStrikeOut)
3222 STRCAT(p, ":s");
3223 if (charset_name != NULL)
3224 {
3225 STRCAT(p, ":c");
3226 STRCAT(p, charset_name);
3227 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003228 if (quality_name != NULL)
3229 {
3230 STRCAT(p, ":q");
3231 STRCAT(p, quality_name);
3232 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233 }
3234
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003235 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236 return (char_u *)res;
3237}
3238
3239
3240#ifdef FEAT_MBYTE_IME
3241/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003242 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003243 * 'guifont'
3244 */
3245 static void
3246update_im_font(void)
3247{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003248 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249
3250 if (p_guifontwide != NULL && *p_guifontwide != NUL
3251 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003252 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003253 norm_logfont = lf_wide;
3254 else
3255 norm_logfont = sub_logfont;
3256 im_set_font(&norm_logfont);
3257}
3258#endif
3259
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003260/*
3261 * Handler of gui.wide_font (p_guifontwide) changed notification.
3262 */
3263 void
3264gui_mch_wide_font_changed(void)
3265{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003266 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003268#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003269 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003270#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271
3272 gui_mch_free_font(gui.wide_ital_font);
3273 gui.wide_ital_font = NOFONT;
3274 gui_mch_free_font(gui.wide_bold_font);
3275 gui.wide_bold_font = NOFONT;
3276 gui_mch_free_font(gui.wide_boldital_font);
3277 gui.wide_boldital_font = NOFONT;
3278
3279 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003280 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003281 {
3282 if (!lf.lfItalic)
3283 {
3284 lf.lfItalic = TRUE;
3285 gui.wide_ital_font = get_font_handle(&lf);
3286 lf.lfItalic = FALSE;
3287 }
3288 if (lf.lfWeight < FW_BOLD)
3289 {
3290 lf.lfWeight = FW_BOLD;
3291 gui.wide_bold_font = get_font_handle(&lf);
3292 if (!lf.lfItalic)
3293 {
3294 lf.lfItalic = TRUE;
3295 gui.wide_boldital_font = get_font_handle(&lf);
3296 }
3297 }
3298 }
3299}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003300
3301/*
3302 * Initialise vim to use the font with the given name.
3303 * Return FAIL if the font could not be loaded, OK otherwise.
3304 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003305 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003306gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003308 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309 GuiFont font = NOFONT;
3310 char_u *p;
3311
Bram Moolenaar734a8672019-12-02 22:49:38 +01003312 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003313 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3314 font = get_font_handle(&lf);
3315 if (font == NOFONT)
3316 return FAIL;
3317
3318 if (font_name == NULL)
3319 font_name = (char_u *)lf.lfFaceName;
3320#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3321 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003322#endif
3323#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003324 sub_logfont = lf;
3325#endif
3326#ifdef FEAT_MBYTE_IME
3327 update_im_font();
3328#endif
3329 gui_mch_free_font(gui.norm_font);
3330 gui.norm_font = font;
3331 current_font_height = lf.lfHeight;
3332 GetFontSize(font);
3333
3334 p = logfont2name(lf);
3335 if (p != NULL)
3336 {
3337 hl_set_font_name(p);
3338
Bram Moolenaar734a8672019-12-02 22:49:38 +01003339 // When setting 'guifont' to "*" replace it with the actual font name.
3340 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3342 {
3343 vim_free(p_guifont);
3344 p_guifont = p;
3345 }
3346 else
3347 vim_free(p);
3348 }
3349
3350 gui_mch_free_font(gui.ital_font);
3351 gui.ital_font = NOFONT;
3352 gui_mch_free_font(gui.bold_font);
3353 gui.bold_font = NOFONT;
3354 gui_mch_free_font(gui.boldital_font);
3355 gui.boldital_font = NOFONT;
3356
3357 if (!lf.lfItalic)
3358 {
3359 lf.lfItalic = TRUE;
3360 gui.ital_font = get_font_handle(&lf);
3361 lf.lfItalic = FALSE;
3362 }
3363 if (lf.lfWeight < FW_BOLD)
3364 {
3365 lf.lfWeight = FW_BOLD;
3366 gui.bold_font = get_font_handle(&lf);
3367 if (!lf.lfItalic)
3368 {
3369 lf.lfItalic = TRUE;
3370 gui.boldital_font = get_font_handle(&lf);
3371 }
3372 }
3373
3374 return OK;
3375}
3376
3377#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003378# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379#endif
3380
3381/*
3382 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003383 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384 */
3385 int
3386gui_mch_maximized(void)
3387{
3388 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003389 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003390
3391 wp.length = sizeof(WINDOWPLACEMENT);
3392 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003393 {
3394 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003396 && wp.flags == WPF_RESTORETOMAXIMIZED))
3397 return TRUE;
3398 if (wp.showCmd == SW_SHOWMINIMIZED)
3399 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003401 // Assume the window is snapped when the sizes from two APIs differ.
3402 GetWindowRect(s_hwnd, &rc);
3403 if ((rc.right - rc.left !=
3404 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3405 || (rc.bottom - rc.top !=
3406 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3407 return TRUE;
3408 }
3409 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003410}
3411
3412/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003413 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3414 * is set. Compute the new Rows and Columns. This is like resizing the
3415 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416 */
3417 void
3418gui_mch_newfont(void)
3419{
3420 RECT rect;
3421
3422 GetWindowRect(s_hwnd, &rect);
3423 if (win_socket_id == 0)
3424 {
3425 gui_resize_shell(rect.right - rect.left
3426 - (GetSystemMetrics(SM_CXFRAME) +
3427 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3428 rect.bottom - rect.top
3429 - (GetSystemMetrics(SM_CYFRAME) +
3430 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3431 - GetSystemMetrics(SM_CYCAPTION)
3432#ifdef FEAT_MENU
3433 - gui_mswin_get_menu_height(FALSE)
3434#endif
3435 );
3436 }
3437 else
3438 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003439 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003440 gui_resize_shell(rect.right - rect.left,
3441 rect.bottom - rect.top
3442#ifdef FEAT_MENU
3443 - gui_mswin_get_menu_height(FALSE)
3444#endif
3445 );
3446 }
3447}
3448
3449/*
3450 * Set the window title
3451 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452 void
3453gui_mch_settitle(
3454 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003455 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456{
3457 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3458}
3459
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003460#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003461// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3462// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463static LPCSTR mshape_idcs[] =
3464{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003465 IDC_ARROW, // arrow
3466 MAKEINTRESOURCE(0), // blank
3467 IDC_IBEAM, // beam
3468 IDC_SIZENS, // updown
3469 IDC_SIZENS, // udsizing
3470 IDC_SIZEWE, // leftright
3471 IDC_SIZEWE, // lrsizing
3472 IDC_WAIT, // busy
3473 IDC_NO, // no
3474 IDC_ARROW, // crosshair
3475 IDC_ARROW, // hand1
3476 IDC_ARROW, // hand2
3477 IDC_ARROW, // pencil
3478 IDC_ARROW, // question
3479 IDC_ARROW, // right-arrow
3480 IDC_UPARROW, // up-arrow
3481 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482};
3483
3484 void
3485mch_set_mouse_shape(int shape)
3486{
3487 LPCSTR idc;
3488
3489 if (shape == MSHAPE_HIDE)
3490 ShowCursor(FALSE);
3491 else
3492 {
3493 if (shape >= MSHAPE_NUMBERED)
3494 idc = IDC_ARROW;
3495 else
3496 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003497 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003498 if (!p_mh)
3499 {
3500 POINT mp;
3501
Bram Moolenaar734a8672019-12-02 22:49:38 +01003502 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 (void)GetCursorPos((LPPOINT)&mp);
3504 (void)SetCursorPos(mp.x, mp.y);
3505 ShowCursor(TRUE);
3506 }
3507 }
3508}
3509#endif
3510
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003511#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 * Wide version of convert_filter().
3514 */
3515 static WCHAR *
3516convert_filterW(char_u *s)
3517{
3518 char_u *tmp;
3519 int len;
3520 WCHAR *res;
3521
3522 tmp = convert_filter(s);
3523 if (tmp == NULL)
3524 return NULL;
3525 len = (int)STRLEN(s) + 3;
3526 res = enc_to_utf16(tmp, &len);
3527 vim_free(tmp);
3528 return res;
3529}
3530
3531/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003532 * Pop open a file browser and return the file selected, in allocated memory,
3533 * or NULL if Cancel is hit.
3534 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3535 * title - Title message for the file browser dialog.
3536 * dflt - Default name of file.
3537 * ext - Default extension to be added to files without extensions.
3538 * initdir - directory in which to open the browser (NULL = current dir)
3539 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003541 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003542gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543 int saving,
3544 char_u *title,
3545 char_u *dflt,
3546 char_u *ext,
3547 char_u *initdir,
3548 char_u *filter)
3549{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003550 // We always use the wide function. This means enc_to_utf16() must work,
3551 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003552 OPENFILENAMEW fileStruct;
3553 WCHAR fileBuf[MAXPATHL];
3554 WCHAR *wp;
3555 int i;
3556 WCHAR *titlep = NULL;
3557 WCHAR *extp = NULL;
3558 WCHAR *initdirp = NULL;
3559 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003560 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561
3562 if (dflt == NULL)
3563 fileBuf[0] = NUL;
3564 else
3565 {
3566 wp = enc_to_utf16(dflt, NULL);
3567 if (wp == NULL)
3568 fileBuf[0] = NUL;
3569 else
3570 {
3571 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3572 fileBuf[i] = wp[i];
3573 fileBuf[i] = NUL;
3574 vim_free(wp);
3575 }
3576 }
3577
Bram Moolenaar734a8672019-12-02 22:49:38 +01003578 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003579 filterp = convert_filterW(filter);
3580
Bram Moolenaara80faa82020-04-12 19:37:17 +02003581 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003582# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003583 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003584 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003585# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003586 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003587# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588
3589 if (title != NULL)
3590 titlep = enc_to_utf16(title, NULL);
3591 fileStruct.lpstrTitle = titlep;
3592
3593 if (ext != NULL)
3594 extp = enc_to_utf16(ext, NULL);
3595 fileStruct.lpstrDefExt = extp;
3596
3597 fileStruct.lpstrFile = fileBuf;
3598 fileStruct.nMaxFile = MAXPATHL;
3599 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003600 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3601 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003602 if (initdir != NULL && *initdir != NUL)
3603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003604 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605 initdirp = enc_to_utf16(initdir, NULL);
3606 if (initdirp != NULL)
3607 {
3608 for (wp = initdirp; *wp != NUL; ++wp)
3609 if (*wp == '/')
3610 *wp = '\\';
3611 }
3612 fileStruct.lpstrInitialDir = initdirp;
3613 }
3614
3615 /*
3616 * TODO: Allow selection of multiple files. Needs another arg to this
3617 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3618 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3619 * files that don't exist yet, so I haven't put it in. What about
3620 * OFN_PATHMUSTEXIST?
3621 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3622 */
3623 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003624# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003625 if (curbuf->b_p_bin)
3626 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003627# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 if (saving)
3629 {
3630 if (!GetSaveFileNameW(&fileStruct))
3631 return NULL;
3632 }
3633 else
3634 {
3635 if (!GetOpenFileNameW(&fileStruct))
3636 return NULL;
3637 }
3638
3639 vim_free(filterp);
3640 vim_free(initdirp);
3641 vim_free(titlep);
3642 vim_free(extp);
3643
Bram Moolenaar734a8672019-12-02 22:49:38 +01003644 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003645 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003646 if (p == NULL)
3647 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003648
Bram Moolenaar734a8672019-12-02 22:49:38 +01003649 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 SetFocus(s_hwnd);
3651
Bram Moolenaar734a8672019-12-02 22:49:38 +01003652 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003653 q = vim_strsave(shorten_fname1(p));
3654 vim_free(p);
3655 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657
3658
3659/*
3660 * Convert the string s to the proper format for a filter string by replacing
3661 * the \t and \n delimiters with \0.
3662 * Returns the converted string in allocated memory.
3663 *
3664 * Keep in sync with convert_filterW() above!
3665 */
3666 static char_u *
3667convert_filter(char_u *s)
3668{
3669 char_u *res;
3670 unsigned s_len = (unsigned)STRLEN(s);
3671 unsigned i;
3672
3673 res = alloc(s_len + 3);
3674 if (res != NULL)
3675 {
3676 for (i = 0; i < s_len; ++i)
3677 if (s[i] == '\t' || s[i] == '\n')
3678 res[i] = '\0';
3679 else
3680 res[i] = s[i];
3681 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003682 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683 res[s_len + 1] = NUL;
3684 res[s_len + 2] = NUL;
3685 }
3686 return res;
3687}
3688
3689/*
3690 * Select a directory.
3691 */
3692 char_u *
3693gui_mch_browsedir(char_u *title, char_u *initdir)
3694{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003695 // We fake this: Use a filter that doesn't select anything and a default
3696 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003697 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3698 initdir, (char_u *)_("Directory\t*.nothing\n"));
3699}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003700#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003701
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702 static void
3703_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003704 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705 HDROP hDrop)
3706{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003707#define BUFPATHLEN _MAX_PATH
3708#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003709 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 char szFile[BUFPATHLEN];
3711 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3712 UINT i;
3713 char_u **fnames;
3714 POINT pt;
3715 int_u modifiers = 0;
3716
Bram Moolenaar734a8672019-12-02 22:49:38 +01003717 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718
Bram Moolenaar734a8672019-12-02 22:49:38 +01003719 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 DragQueryPoint(hDrop, &pt);
3721 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3722
3723 reset_VIsual();
3724
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003725 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726
3727 if (fnames != NULL)
3728 for (i = 0; i < cFiles; ++i)
3729 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003730 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3731 fnames[i] = utf16_to_enc(wszFile, NULL);
3732 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733 {
3734 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3735 fnames[i] = vim_strsave((char_u *)szFile);
3736 }
3737 }
3738
3739 DragFinish(hDrop);
3740
3741 if (fnames != NULL)
3742 {
3743 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3744 modifiers |= MOUSE_SHIFT;
3745 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3746 modifiers |= MOUSE_CTRL;
3747 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3748 modifiers |= MOUSE_ALT;
3749
3750 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3751
3752 s_need_activate = TRUE;
3753 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003754}
3755
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756 static int
3757_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003758 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759 HWND hwndCtl,
3760 UINT code,
3761 int pos)
3762{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003763 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 scrollbar_T *sb, *sb_info;
3765 long val;
3766 int dragging = FALSE;
3767 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 SCROLLINFO si;
3769
3770 si.cbSize = sizeof(si);
3771 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772
3773 sb = gui_mswin_find_scrollbar(hwndCtl);
3774 if (sb == NULL)
3775 return 0;
3776
Bram Moolenaar734a8672019-12-02 22:49:38 +01003777 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778 {
3779 /*
3780 * Careful: need to get scrollbar info out of first (left) scrollbar
3781 * for window, but keep real scrollbar too because we must pass it to
3782 * gui_drag_scrollbar().
3783 */
3784 sb_info = &sb->wp->w_scrollbars[0];
3785 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003786 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003787 sb_info = sb;
3788 val = sb_info->value;
3789
3790 switch (code)
3791 {
3792 case SB_THUMBTRACK:
3793 val = pos;
3794 dragging = TRUE;
3795 if (sb->scroll_shift > 0)
3796 val <<= sb->scroll_shift;
3797 break;
3798 case SB_LINEDOWN:
3799 val++;
3800 break;
3801 case SB_LINEUP:
3802 val--;
3803 break;
3804 case SB_PAGEDOWN:
3805 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3806 break;
3807 case SB_PAGEUP:
3808 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3809 break;
3810 case SB_TOP:
3811 val = 0;
3812 break;
3813 case SB_BOTTOM:
3814 val = sb_info->max;
3815 break;
3816 case SB_ENDSCROLL:
3817 if (prev_code == SB_THUMBTRACK)
3818 {
3819 /*
3820 * "pos" only gives us 16-bit data. In case of large file,
3821 * use GetScrollPos() which returns 32-bit. Unfortunately it
3822 * is not valid while the scrollbar is being dragged.
3823 */
3824 val = GetScrollPos(hwndCtl, SB_CTL);
3825 if (sb->scroll_shift > 0)
3826 val <<= sb->scroll_shift;
3827 }
3828 break;
3829
3830 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003831 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003832 return 0;
3833 }
3834 prev_code = code;
3835
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003836 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3837 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838
3839 /*
3840 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3841 */
3842 if (sb->wp != NULL)
3843 {
3844 scrollbar_T *sba = sb->wp->w_scrollbars;
3845 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3846
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003848 }
3849
Bram Moolenaar734a8672019-12-02 22:49:38 +01003850 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 s_busy_processing = TRUE;
3852
Bram Moolenaar734a8672019-12-02 22:49:38 +01003853 // When "allow_scrollbar" is FALSE still need to remember the new
3854 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 dont_scroll = !allow_scrollbar;
3856
Bram Moolenaara338adc2018-01-31 20:51:47 +01003857 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003859 mch_enable_flush();
3860 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861
3862 s_busy_processing = FALSE;
3863 dont_scroll = dont_scroll_save;
3864
3865 return 0;
3866}
3867
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#ifdef FEAT_XPM_W32
3870# include "xpm_w32.h"
3871#endif
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873#ifdef __MINGW32__
3874/*
3875 * Add a lot of missing defines.
3876 * They are not always missing, we need the #ifndef's.
3877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878# ifndef IsMinimized
3879# define IsMinimized(hwnd) IsIconic(hwnd)
3880# endif
3881# ifndef IsMaximized
3882# define IsMaximized(hwnd) IsZoomed(hwnd)
3883# endif
3884# ifndef SelectFont
3885# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3886# endif
3887# ifndef GetStockBrush
3888# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3889# endif
3890# ifndef DeleteBrush
3891# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3892# endif
3893
3894# ifndef HANDLE_WM_RBUTTONDBLCLK
3895# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3896 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3897# endif
3898# ifndef HANDLE_WM_MBUTTONUP
3899# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3900 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3901# endif
3902# ifndef HANDLE_WM_MBUTTONDBLCLK
3903# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3904 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3905# endif
3906# ifndef HANDLE_WM_LBUTTONDBLCLK
3907# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3908 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3909# endif
3910# ifndef HANDLE_WM_RBUTTONDOWN
3911# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3912 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3913# endif
3914# ifndef HANDLE_WM_MOUSEMOVE
3915# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3916 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3917# endif
3918# ifndef HANDLE_WM_RBUTTONUP
3919# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3920 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3921# endif
3922# ifndef HANDLE_WM_MBUTTONDOWN
3923# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3924 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3925# endif
3926# ifndef HANDLE_WM_LBUTTONUP
3927# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3928 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3929# endif
3930# ifndef HANDLE_WM_LBUTTONDOWN
3931# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3932 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3933# endif
3934# ifndef HANDLE_WM_SYSCHAR
3935# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3936 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3937# endif
3938# ifndef HANDLE_WM_ACTIVATEAPP
3939# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3940 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3941# endif
3942# ifndef HANDLE_WM_WINDOWPOSCHANGING
3943# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3944 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3945# endif
3946# ifndef HANDLE_WM_VSCROLL
3947# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3948 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3949# endif
3950# ifndef HANDLE_WM_SETFOCUS
3951# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3952 ((fn)((hwnd), (HWND)(wParam)), 0L)
3953# endif
3954# ifndef HANDLE_WM_KILLFOCUS
3955# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3956 ((fn)((hwnd), (HWND)(wParam)), 0L)
3957# endif
3958# ifndef HANDLE_WM_HSCROLL
3959# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3960 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3961# endif
3962# ifndef HANDLE_WM_DROPFILES
3963# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3964 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3965# endif
3966# ifndef HANDLE_WM_CHAR
3967# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3968 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3969# endif
3970# ifndef HANDLE_WM_SYSDEADCHAR
3971# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3972 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3973# endif
3974# ifndef HANDLE_WM_DEADCHAR
3975# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3976 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3977# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003978#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980
Bram Moolenaar734a8672019-12-02 22:49:38 +01003981// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982#define TEAROFF_PADDING_X 2
3983#define TEAROFF_BUTTON_PAD_X 8
3984#define TEAROFF_MIN_WIDTH 200
3985#define TEAROFF_SUBMENU_LABEL ">>"
3986#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3987
3988
Bram Moolenaar734a8672019-12-02 22:49:38 +01003989// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003991# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#endif
3993
3994
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003995#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996# define ID_BEVAL_TOOLTIP 200
3997# define BEVAL_TEXT_LEN MAXPATHL
3998
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003999# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004000// Work around old versions of basetsd.h which wrongly declares
4001// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004002# undef UINT_PTR
4003# define UINT_PTR UINT
4004# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004007static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004009
Bram Moolenaar82881492012-11-20 16:53:39 +01004010
Bram Moolenaar734a8672019-12-02 22:49:38 +01004011// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004012# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004013
Bram Moolenaar45360022005-07-21 21:08:21 +00004014/*
4015 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004016 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004017 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004018# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004019
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004020# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004021
4022typedef struct _DllVersionInfo
4023{
4024 DWORD cbSize;
4025 DWORD dwMajorVersion;
4026 DWORD dwMinorVersion;
4027 DWORD dwBuildNumber;
4028 DWORD dwPlatformID;
4029} DLLVERSIONINFO;
4030
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004031# ifndef PROTO
4032# include <poppack.h>
4033# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004034
Bram Moolenaar45360022005-07-21 21:08:21 +00004035typedef struct tagTOOLINFOA_NEW
4036{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004037 UINT cbSize;
4038 UINT uFlags;
4039 HWND hwnd;
4040 UINT_PTR uId;
4041 RECT rect;
4042 HINSTANCE hinst;
4043 LPSTR lpszText;
4044 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004045} TOOLINFO_NEW;
4046
4047typedef struct tagNMTTDISPINFO_NEW
4048{
4049 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004050 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004051 char szText[80];
4052 HINSTANCE hinst;
4053 UINT uFlags;
4054 LPARAM lParam;
4055} NMTTDISPINFO_NEW;
4056
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004057typedef struct tagTOOLINFOW_NEW
4058{
4059 UINT cbSize;
4060 UINT uFlags;
4061 HWND hwnd;
4062 UINT_PTR uId;
4063 RECT rect;
4064 HINSTANCE hinst;
4065 LPWSTR lpszText;
4066 LPARAM lParam;
4067 void *lpReserved;
4068} TOOLINFOW_NEW;
4069
4070typedef struct tagNMTTDISPINFOW_NEW
4071{
4072 NMHDR hdr;
4073 LPWSTR lpszText;
4074 WCHAR szText[80];
4075 HINSTANCE hinst;
4076 UINT uFlags;
4077 LPARAM lParam;
4078} NMTTDISPINFOW_NEW;
4079
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004080
Bram Moolenaar45360022005-07-21 21:08:21 +00004081typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004082# ifndef TTM_SETMAXTIPWIDTH
4083# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004086# ifndef TTF_DI_SETITEM
4087# define TTF_DI_SETITEM 0x8000
4088# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004089
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004090# ifndef TTN_GETDISPINFO
4091# define TTN_GETDISPINFO (TTN_FIRST - 0)
4092# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004093
Bram Moolenaar734a8672019-12-02 22:49:38 +01004094#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004095
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004096#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004097// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4098// it here if LPNMTTDISPINFO isn't defined.
4099// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4100// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004101# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4102typedef struct tagNMTTDISPINFOA {
4103 NMHDR hdr;
4104 LPSTR lpszText;
4105 char szText[80];
4106 HINSTANCE hinst;
4107 UINT uFlags;
4108 LPARAM lParam;
4109} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4110# define LPNMTTDISPINFO LPNMTTDISPINFOA
4111
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004112typedef struct tagNMTTDISPINFOW {
4113 NMHDR hdr;
4114 LPWSTR lpszText;
4115 WCHAR szText[80];
4116 HINSTANCE hinst;
4117 UINT uFlags;
4118 LPARAM lParam;
4119} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004120# endif
4121#endif
4122
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004123#ifndef TTN_GETDISPINFOW
4124# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4125#endif
4126
Bram Moolenaar734a8672019-12-02 22:49:38 +01004127// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129#ifdef FEAT_MENU
4130static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133/*
4134 * Use the system font for dialogs and tear-off menus. Remove this line to
4135 * use DLG_FONT_NAME.
4136 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004137#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#define VIM_CLASSW L"Vim"
4141
Bram Moolenaar734a8672019-12-02 22:49:38 +01004142// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4143// thus there should be room for every dialog. For tearoffs it's made bigger
4144// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#define DLG_ALLOC_SIZE 16 * 1024
4146
4147/*
4148 * stuff for dialogs, menus, tearoffs etc.
4149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150static PWORD
4151add_dialog_element(
4152 PWORD p,
4153 DWORD lStyle,
4154 WORD x,
4155 WORD y,
4156 WORD w,
4157 WORD h,
4158 WORD Id,
4159 WORD clss,
4160 const char *caption);
4161static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004162static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004163#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166static void get_dialog_font_metrics(void);
4167
4168static int dialog_default_button = -1;
4169
Bram Moolenaar734a8672019-12-02 22:49:38 +01004170// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaar734a8672019-12-02 22:49:38 +01004173static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#ifdef FEAT_TOOLBAR
4175static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004176static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177static int get_toolbar_bitmap(vimmenu_T *menu);
4178#endif
4179
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004180#ifdef FEAT_GUI_TABLINE
4181static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004182static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004183#endif
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#ifdef FEAT_MBYTE_IME
4186static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4187static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4188#endif
4189#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4190# ifdef NOIME
4191typedef struct tagCOMPOSITIONFORM {
4192 DWORD dwStyle;
4193 POINT ptCurrentPos;
4194 RECT rcArea;
4195} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4196typedef HANDLE HIMC;
4197# endif
4198
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004199static HINSTANCE hLibImm = NULL;
4200static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4201static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4202static HIMC (WINAPI *pImmGetContext)(HWND);
4203static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4204static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4205static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4206static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004207static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4208static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004209static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4210static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004211static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212static void dyn_imm_load(void);
4213#else
4214# define pImmGetCompositionStringA ImmGetCompositionStringA
4215# define pImmGetCompositionStringW ImmGetCompositionStringW
4216# define pImmGetContext ImmGetContext
4217# define pImmAssociateContext ImmAssociateContext
4218# define pImmReleaseContext ImmReleaseContext
4219# define pImmGetOpenStatus ImmGetOpenStatus
4220# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004221# define pImmGetCompositionFontW ImmGetCompositionFontW
4222# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223# define pImmSetCompositionWindow ImmSetCompositionWindow
4224# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004225# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#endif
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#ifdef FEAT_MENU
4229/*
4230 * Figure out how high the menu bar is at the moment.
4231 */
4232 static int
4233gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004234 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 static int old_menu_height = -1;
4237
4238 RECT rc1, rc2;
4239 int num;
4240 int menu_height;
4241
4242 if (gui.menu_is_active)
4243 num = GetMenuItemCount(s_menuBar);
4244 else
4245 num = 0;
4246
4247 if (num == 0)
4248 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004249 else if (IsMinimized(s_hwnd))
4250 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004251 // The height of the menu cannot be determined while the window is
4252 // minimized. Take the previous height if the menu is changed in that
4253 // state, to avoid that Vim's vertical window size accidentally
4254 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004255 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 else
4258 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004259 /*
4260 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4261 * seem to have been set yet, so menu wraps in default window
4262 * width which is very narrow. Instead just return height of a
4263 * single menu item. Will still be wrong when the menu really
4264 * should wrap over more than one line.
4265 */
4266 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4267 if (gui.starting)
4268 menu_height = rc1.bottom - rc1.top + 1;
4269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004271 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4272 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275
4276 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004277 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004278 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 return menu_height;
4281}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004282#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
4284
4285/*
4286 * Setup for the Intellimouse
4287 */
4288 static void
4289init_mouse_wheel(void)
4290{
4291
4292#ifndef SPI_GETWHEELSCROLLLINES
4293# define SPI_GETWHEELSCROLLLINES 104
4294#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004295#ifndef SPI_SETWHEELSCROLLLINES
4296# define SPI_SETWHEELSCROLLLINES 105
4297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
Bram Moolenaar734a8672019-12-02 22:49:38 +01004299#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4300#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4302#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4303
Bram Moolenaar734a8672019-12-02 22:49:38 +01004304 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaar734a8672019-12-02 22:49:38 +01004306 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004307 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4308 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309}
4310
4311
Bram Moolenaarae20f342019-11-05 21:09:23 +01004312/*
4313 * Intellimouse wheel handler.
4314 * Treat a mouse wheel event as if it were a scroll request.
4315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 static void
4317_OnMouseWheel(
4318 HWND hwnd,
4319 short zDelta)
4320{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int i;
4322 int size;
4323 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004324 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (mouse_scroll_lines == 0)
4327 init_mouse_wheel();
4328
Bram Moolenaarae20f342019-11-05 21:09:23 +01004329 wp = gui_mouse_window(FIND_POPUP);
4330
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004331#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004332 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004333 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004334 cmdarg_T cap;
4335 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004336
Bram Moolenaarae20f342019-11-05 21:09:23 +01004337 // Mouse hovers over popup window, scroll it if possible.
4338 mouse_row = wp->w_winrow;
4339 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004340 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004341 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4342 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4343 clear_oparg(&oa);
4344 cap.oap = &oa;
4345 nv_mousescroll(&cap);
4346 update_screen(0);
4347 setcursor();
4348 out_flush();
4349 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004350 }
4351#endif
4352
Bram Moolenaarae20f342019-11-05 21:09:23 +01004353 if (wp == NULL || !p_scf)
4354 wp = curwin;
4355
4356 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4357 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4358 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4359 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4360 else
4361 return;
4362 size = wp->w_height;
4363
Bram Moolenaara338adc2018-01-31 20:51:47 +01004364 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 if (mouse_scroll_lines > 0
4366 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4367 {
4368 for (i = mouse_scroll_lines; i > 0; --i)
4369 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4370 }
4371 else
4372 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004373 mch_enable_flush();
4374 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375}
4376
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004377#ifdef USE_SYSMENU_FONT
4378/*
4379 * Get Menu Font.
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004383gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004384{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004385 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004386
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004387 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4388 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004389 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004390 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004391 &nm,
4392 0))
4393 return FAIL;
4394 *lf = nm.lfMenuFont;
4395 return OK;
4396}
4397#endif
4398
4399
4400#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4401/*
4402 * Set the GUI tabline font to the system menu font
4403 */
4404 static void
4405set_tabline_font(void)
4406{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004407 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004408 HFONT font;
4409 HWND hwnd;
4410 HDC hdc;
4411 HFONT hfntOld;
4412 TEXTMETRIC tm;
4413
4414 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4415 return;
4416
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004417 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004418
4419 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4420
4421 /*
4422 * Compute the height of the font used for the tab text
4423 */
4424 hwnd = GetDesktopWindow();
4425 hdc = GetWindowDC(hwnd);
4426 hfntOld = SelectFont(hdc, font);
4427
4428 GetTextMetrics(hdc, &tm);
4429
4430 SelectFont(hdc, hfntOld);
4431 ReleaseDC(hwnd, hdc);
4432
4433 /*
4434 * The space used by the tab border and the space between the tab label
4435 * and the tab border is included as 7.
4436 */
4437 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4438}
4439#endif
4440
Bram Moolenaar520470a2005-06-16 21:59:56 +00004441/*
4442 * Invoked when a setting was changed.
4443 */
4444 static LRESULT CALLBACK
4445_OnSettingChange(UINT n)
4446{
4447 if (n == SPI_SETWHEELSCROLLLINES)
4448 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4449 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004450#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4451 if (n == SPI_SETNONCLIENTMETRICS)
4452 set_tabline_font();
4453#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004454 return 0;
4455}
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457#ifdef FEAT_NETBEANS_INTG
4458 static void
4459_OnWindowPosChanged(
4460 HWND hwnd,
4461 const LPWINDOWPOS lpwpos)
4462{
4463 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004464 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4467 || lpwpos->cx != cx || lpwpos->cy != cy))
4468 {
4469 x = lpwpos->x;
4470 y = lpwpos->y;
4471 cx = lpwpos->cx;
4472 cy = lpwpos->cy;
4473 netbeans_frame_moved(x, y);
4474 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004475 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4477}
4478#endif
4479
4480 static int
4481_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 UINT fwSide,
4483 LPRECT lprc)
4484{
4485 int w, h;
4486 int valid_w, valid_h;
4487 int w_offset, h_offset;
4488
4489 w = lprc->right - lprc->left;
4490 h = lprc->bottom - lprc->top;
4491 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4492 w_offset = w - valid_w;
4493 h_offset = h - valid_h;
4494
4495 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4496 || fwSide == WMSZ_BOTTOMLEFT)
4497 lprc->left += w_offset;
4498 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4499 || fwSide == WMSZ_BOTTOMRIGHT)
4500 lprc->right -= w_offset;
4501
4502 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4503 || fwSide == WMSZ_TOPRIGHT)
4504 lprc->top += h_offset;
4505 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4506 || fwSide == WMSZ_BOTTOMRIGHT)
4507 lprc->bottom -= h_offset;
4508 return TRUE;
4509}
4510
4511
4512
4513 static LRESULT CALLBACK
4514_WndProc(
4515 HWND hwnd,
4516 UINT uMsg,
4517 WPARAM wParam,
4518 LPARAM lParam)
4519{
4520 /*
4521 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4522 hwnd, uMsg, wParam, lParam);
4523 */
4524
4525 HandleMouseHide(uMsg, lParam);
4526
4527 s_uMsg = uMsg;
4528 s_wParam = wParam;
4529 s_lParam = lParam;
4530
4531 switch (uMsg)
4532 {
4533 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4534 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004535 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004537 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4539 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4540 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4541 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4542#ifdef FEAT_MENU
4543 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4544#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004545 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4546 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4548 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004549 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4550 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4552 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4553 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4554#ifdef FEAT_NETBEANS_INTG
4555 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4556#endif
4557
Bram Moolenaarafa24992006-03-27 20:58:26 +00004558#ifdef FEAT_GUI_TABLINE
4559 case WM_RBUTTONUP:
4560 {
4561 if (gui_mch_showing_tabline())
4562 {
4563 POINT pt;
4564 RECT rect;
4565
4566 /*
4567 * If the cursor is on the tabline, display the tab menu
4568 */
4569 GetCursorPos((LPPOINT)&pt);
4570 GetWindowRect(s_textArea, &rect);
4571 if (pt.y < rect.top)
4572 {
4573 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004574 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004575 }
4576 }
4577 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4578 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004579 case WM_LBUTTONDBLCLK:
4580 {
4581 /*
4582 * If the user double clicked the tabline, create a new tab
4583 */
4584 if (gui_mch_showing_tabline())
4585 {
4586 POINT pt;
4587 RECT rect;
4588
4589 GetCursorPos((LPPOINT)&pt);
4590 GetWindowRect(s_textArea, &rect);
4591 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004592 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004593 }
4594 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4595 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004596#endif
4597
Bram Moolenaar734a8672019-12-02 22:49:38 +01004598 case WM_QUERYENDSESSION: // System wants to go down.
4599 gui_shell_closed(); // Will exit when no changed buffers.
4600 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
4602 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004603 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004606 return 0L;
4607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 break;
4609
4610 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004611 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4612 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004613 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 return 0L;
4615
4616 case WM_SYSCHAR:
4617 /*
4618 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4619 * shortcut key, handle like a typed ALT key, otherwise call Windows
4620 * ALT key handling.
4621 */
4622#ifdef FEAT_MENU
4623 if ( !gui.menu_is_active
4624 || p_wak[0] == 'n'
4625 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4626 )
4627#endif
4628 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004629 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 return 0L;
4631 }
4632#ifdef FEAT_MENU
4633 else
4634 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4635#endif
4636
4637 case WM_SYSKEYUP:
4638#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004639 // This used to be done only when menu is active: ALT key is used for
4640 // that. But that caused problems when menu is disabled and using
4641 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4642 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4644#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004645 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#endif
4647
Bram Moolenaar734a8672019-12-02 22:49:38 +01004648 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
4651 case WM_MOUSEWHEEL:
4652 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004653 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaar734a8672019-12-02 22:49:38 +01004655 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004656 case WM_SETTINGCHANGE:
4657 return _OnSettingChange((UINT)wParam);
4658
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004659#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 case WM_NOTIFY:
4661 switch (((LPNMHDR) lParam)->code)
4662 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004663 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004664 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004666 LPNMHDR hdr = (LPNMHDR)lParam;
4667 char_u *str = NULL;
4668 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004669
Bram Moolenaard23a8232018-02-10 18:45:26 +01004670 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004671
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004672# ifdef FEAT_GUI_TABLINE
4673 if (gui_mch_showing_tabline()
4674 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004675 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004676 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004677 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004678 * Mouse is over the GUI tabline. Display the
4679 * tooltip for the tab under the cursor
4680 *
4681 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004682 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004683 GetCursorPos(&pt);
4684 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004685 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004686 TCHITTESTINFO htinfo;
4687 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004688
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004689 /*
4690 * Get the tab under the cursor
4691 */
4692 htinfo.pt.x = pt.x;
4693 htinfo.pt.y = pt.y;
4694 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4695 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004696 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004697 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004698
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004699 tp = find_tabpage(idx + 1);
4700 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004701 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004702 get_tabline_label(tp, TRUE);
4703 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004704 }
4705 }
4706 }
4707 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004708# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004709# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004710# ifdef FEAT_GUI_TABLINE
4711 else
4712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004714 UINT idButton;
4715 vimmenu_T *pMenu;
4716
4717 idButton = (UINT) hdr->idFrom;
4718 pMenu = gui_mswin_find_menu(root_menu, idButton);
4719 if (pMenu)
4720 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004722# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004723 if (str != NULL)
4724 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004725 if (hdr->code == TTN_GETDISPINFOW)
4726 {
4727 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4728
Bram Moolenaar734a8672019-12-02 22:49:38 +01004729 // Set the maximum width, this also enables using
4730 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004731 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4732 0, 500);
4733
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004734 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004735 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004736 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004737 }
4738 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004739 {
4740 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4741
Bram Moolenaar734a8672019-12-02 22:49:38 +01004742 // Set the maximum width, this also enables using
4743 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004744 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4745 0, 500);
4746
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004747 if (STRLEN(str) < sizeof(lpdi->szText)
4748 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004749 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004750 sizeof(lpdi->szText) - 1);
4751 else
4752 lpdi->lpszText = tt_text;
4753 }
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004757# ifdef FEAT_GUI_TABLINE
4758 case TCN_SELCHANGE:
4759 if (gui_mch_showing_tabline()
4760 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004761 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004762 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004763 return 0L;
4764 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004765 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004766
4767 case NM_RCLICK:
4768 if (gui_mch_showing_tabline()
4769 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004770 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004771 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004772 return 0L;
4773 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004774 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004777# ifdef FEAT_GUI_TABLINE
4778 if (gui_mch_showing_tabline()
4779 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4780 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 break;
4783 }
4784 break;
4785#endif
4786#if defined(MENUHINTS) && defined(FEAT_MENU)
4787 case WM_MENUSELECT:
4788 if (((UINT) HIWORD(wParam)
4789 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4790 == MF_HILITE
4791 && (State & CMDLINE) == 0)
4792 {
4793 UINT idButton;
4794 vimmenu_T *pMenu;
4795 static int did_menu_tip = FALSE;
4796
4797 if (did_menu_tip)
4798 {
4799 msg_clr_cmdline();
4800 setcursor();
4801 out_flush();
4802 did_menu_tip = FALSE;
4803 }
4804
4805 idButton = (UINT)LOWORD(wParam);
4806 pMenu = gui_mswin_find_menu(root_menu, idButton);
4807 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4808 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4809 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004810 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004811 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004812 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 setcursor();
4814 out_flush();
4815 did_menu_tip = TRUE;
4816 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004817 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819 break;
4820#endif
4821 case WM_NCHITTEST:
4822 {
4823 LRESULT result;
4824 int x, y;
4825 int xPos = GET_X_LPARAM(lParam);
4826
4827 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4828 if (result == HTCLIENT)
4829 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004830#ifdef FEAT_GUI_TABLINE
4831 if (gui_mch_showing_tabline())
4832 {
4833 int yPos = GET_Y_LPARAM(lParam);
4834 RECT rct;
4835
Bram Moolenaar734a8672019-12-02 22:49:38 +01004836 // If the cursor is on the GUI tabline, don't process this
4837 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004838 GetWindowRect(s_textArea, &rct);
4839 if (yPos < rct.top)
4840 return result;
4841 }
4842#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004843 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 xPos -= x;
4845
Bram Moolenaar734a8672019-12-02 22:49:38 +01004846 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 return HTBOTTOMLEFT;
4848 else
4849 return HTBOTTOMRIGHT;
4850 }
4851 else
4852 return result;
4853 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004854 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856#ifdef FEAT_MBYTE_IME
4857 case WM_IME_NOTIFY:
4858 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4859 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004860 return 1L;
4861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 case WM_IME_COMPOSITION:
4863 if (!_OnImeComposition(hwnd, wParam, lParam))
4864 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004865 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#endif
4867
4868 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004870 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872#endif
4873 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4874 }
4875
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004876 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877}
4878
4879/*
4880 * End of call-back routines
4881 */
4882
Bram Moolenaar734a8672019-12-02 22:49:38 +01004883// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884HWND vim_parent_hwnd = NULL;
4885
4886 static BOOL CALLBACK
4887FindWindowTitle(HWND hwnd, LPARAM lParam)
4888{
4889 char buf[2048];
4890 char *title = (char *)lParam;
4891
4892 if (GetWindowText(hwnd, buf, sizeof(buf)))
4893 {
4894 if (strstr(buf, title) != NULL)
4895 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004896 // Found it. Store the window ref. and quit searching if MDI
4897 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004899 if (vim_parent_hwnd != NULL)
4900 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004903 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904}
4905
4906/*
4907 * Invoked for '-P "title"' argument: search for parent application to open
4908 * our window in.
4909 */
4910 void
4911gui_mch_set_parent(char *title)
4912{
4913 EnumWindows(FindWindowTitle, (LPARAM)title);
4914 if (vim_parent_hwnd == NULL)
4915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004916 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 mch_exit(2);
4918 }
4919}
4920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 static void
4923ole_error(char *arg)
4924{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004925 char buf[IOSIZE];
4926
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004927# ifdef VIMDLL
4928 gui.in_use = mch_is_gui_executable();
4929# endif
4930
Bram Moolenaar734a8672019-12-02 22:49:38 +01004931 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004932 vim_snprintf(buf, IOSIZE,
4933 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4934 arg);
4935 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004939#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4940 static char *
4941gvim_error(void)
4942{
4943 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4944
4945 if (starting)
4946 {
4947 mch_errmsg(msg);
4948 mch_errmsg("\n");
4949 mch_exit(2);
4950 }
4951 return msg;
4952}
4953
4954 char *
4955gui_mch_do_spawn(char_u *arg)
4956{
4957 int len;
4958# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4959 char_u *session = NULL;
4960 LPWSTR tofree1 = NULL;
4961# endif
4962 WCHAR name[MAX_PATH];
4963 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4964 STARTUPINFOW si = {sizeof(si)};
4965 PROCESS_INFORMATION pi;
4966
4967 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4968 goto error;
4969 p = wcsrchr(name, L'\\');
4970 if (p == NULL)
4971 goto error;
4972 // Replace the executable name from vim(d).exe to gvim(d).exe.
4973# ifdef DEBUG
4974 wcscpy(p + 1, L"gvimd.exe");
4975# else
4976 wcscpy(p + 1, L"gvim.exe");
4977# endif
4978
4979# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4980 if (starting)
4981# endif
4982 {
4983 // Pass the command line to the new process.
4984 p = GetCommandLineW();
4985 // Skip 1st argument.
4986 while (*p && *p != L' ' && *p != L'\t')
4987 {
4988 if (*p == L'"')
4989 {
4990 while (*p && *p != L'"')
4991 ++p;
4992 if (*p)
4993 ++p;
4994 }
4995 else
4996 ++p;
4997 }
4998 cmd = p;
4999 }
5000# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5001 else
5002 {
5003 // Create a session file and pass it to the new process.
5004 LPWSTR wsession;
5005 char_u *savebg;
5006 int ret;
5007
5008 session = vim_tempname('s', FALSE);
5009 if (session == NULL)
5010 goto error;
5011 savebg = p_bg;
5012 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5013 ret = write_session_file(session);
5014 vim_free(p_bg);
5015 p_bg = savebg;
5016 if (!ret)
5017 goto error;
5018 wsession = enc_to_utf16(session, NULL);
5019 if (wsession == NULL)
5020 goto error;
5021 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005022 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005023 if (cmd == NULL)
5024 {
5025 vim_free(wsession);
5026 goto error;
5027 }
5028 tofree1 = cmd;
5029 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5030 wsession, wsession);
5031 vim_free(wsession);
5032 }
5033# endif
5034
5035 // Check additional arguments to the `:gui` command.
5036 if (arg != NULL)
5037 {
5038 warg = enc_to_utf16(arg, NULL);
5039 if (warg == NULL)
5040 goto error;
5041 tofree2 = warg;
5042 }
5043 else
5044 warg = L"";
5045
5046 // Set up the new command line.
5047 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005048 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005049 if (newcmd == NULL)
5050 goto error;
5051 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5052
5053 // Spawn a new GUI process.
5054 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5055 NULL, NULL, &si, &pi))
5056 goto error;
5057 CloseHandle(pi.hProcess);
5058 CloseHandle(pi.hThread);
5059 mch_exit(0);
5060
5061error:
5062# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5063 if (session)
5064 mch_remove(session);
5065 vim_free(session);
5066 vim_free(tofree1);
5067# endif
5068 vim_free(newcmd);
5069 vim_free(tofree2);
5070 return gvim_error();
5071}
5072#endif
5073
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074/*
5075 * Parse the GUI related command-line arguments. Any arguments used are
5076 * deleted from argv, and *argc is decremented accordingly. This is called
5077 * when vim is started, whether or not the GUI has been started.
5078 */
5079 void
5080gui_mch_prepare(int *argc, char **argv)
5081{
5082 int silent = FALSE;
5083 int idx;
5084
Bram Moolenaar734a8672019-12-02 22:49:38 +01005085 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5087 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005088 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5090 && (argv[2][0] == '-' || argv[2][0] == '/'))
5091 {
5092 silent = TRUE;
5093 idx = 2;
5094 }
5095 else
5096 idx = 1;
5097
Bram Moolenaar734a8672019-12-02 22:49:38 +01005098 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (STRICMP(argv[idx] + 1, "register") == 0)
5100 {
5101#ifdef FEAT_OLE
5102 RegisterMe(silent);
5103 mch_exit(0);
5104#else
5105 if (!silent)
5106 ole_error("register");
5107 mch_exit(2);
5108#endif
5109 }
5110
Bram Moolenaar734a8672019-12-02 22:49:38 +01005111 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5113 {
5114#ifdef FEAT_OLE
5115 UnregisterMe(!silent);
5116 mch_exit(0);
5117#else
5118 if (!silent)
5119 ole_error("unregister");
5120 mch_exit(2);
5121#endif
5122 }
5123
Bram Moolenaar734a8672019-12-02 22:49:38 +01005124 // Ignore an -embedding argument. It is only relevant if the
5125 // application wants to treat the case when it is started manually
5126 // differently from the case where it is started via automation (and
5127 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5129 {
5130#ifdef FEAT_OLE
5131 *argc = 1;
5132#else
5133 ole_error("embedding");
5134 mch_exit(2);
5135#endif
5136 }
5137 }
5138
5139#ifdef FEAT_OLE
5140 {
5141 int bDoRestart = FALSE;
5142
5143 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005144 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (bDoRestart)
5146 mch_exit(0);
5147 }
5148#endif
5149
5150#ifdef FEAT_NETBEANS_INTG
5151 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005152 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 int arg;
5154
5155 for (arg = 1; arg < *argc; arg++)
5156 if (strncmp("-nb", argv[arg], 3) == 0)
5157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 netbeansArg = argv[arg];
5159 mch_memmove(&argv[arg], &argv[arg + 1],
5160 (--*argc - arg) * sizeof(char *));
5161 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005162 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166}
5167
5168/*
5169 * Initialise the GUI. Create all the windows, set up all the call-backs
5170 * etc.
5171 */
5172 int
5173gui_mch_init(void)
5174{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005176 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#ifdef GLOBAL_IME
5179 ATOM atom;
5180#endif
5181
Bram Moolenaar734a8672019-12-02 22:49:38 +01005182 // Return here if the window was already opened (happens when
5183 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005185 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
5187 /*
5188 * Load the tearoff bitmap
5189 */
5190#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005191 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
5193
5194 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5195 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5196#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005197 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#endif
5199 gui.border_width = 0;
5200
5201 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5202
Bram Moolenaar734a8672019-12-02 22:49:38 +01005203 // First try using the wide version, so that we can use any title.
5204 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005205 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005207 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 wndclassw.lpfnWndProc = _WndProc;
5209 wndclassw.cbClsExtra = 0;
5210 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005211 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5213 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5214 wndclassw.hbrBackground = s_brush;
5215 wndclassw.lpszMenuName = NULL;
5216 wndclassw.lpszClassName = szVimWndClassW;
5217
5218 if ((
5219#ifdef GLOBAL_IME
5220 atom =
5221#endif
5222 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (vim_parent_hwnd != NULL)
5227 {
5228#ifdef HAVE_TRY_EXCEPT
5229 __try
5230 {
5231#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005232 // Open inside the specified parent window.
5233 // TODO: last argument should point to a CLIENTCREATESTRUCT
5234 // structure.
5235 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005237 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005238 WS_OVERLAPPEDWINDOW | WS_CHILD
5239 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5241 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005242 100, // Any value will do
5243 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005245 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#ifdef HAVE_TRY_EXCEPT
5247 }
5248 __except(EXCEPTION_EXECUTE_HANDLER)
5249 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005250 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252#endif
5253 if (s_hwnd == NULL)
5254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005255 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 mch_exit(2);
5257 }
5258 }
5259 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005260 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005261 // If the provided windowid is not valid reset it to zero, so that it
5262 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005263 if (IsWindow((HWND)win_socket_id) <= 0)
5264 win_socket_id = 0;
5265
Bram Moolenaar734a8672019-12-02 22:49:38 +01005266 // Create a window. If win_socket_id is not zero without border and
5267 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005268 s_hwnd = CreateWindowW(
5269 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005270 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5271 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005272 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5273 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005274 100, // Any value will do
5275 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005276 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005277 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005278 if (s_hwnd != NULL && win_socket_id != 0)
5279 {
5280 SetParent(s_hwnd, (HWND)win_socket_id);
5281 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5282 }
5283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 if (s_hwnd == NULL)
5286 return FAIL;
5287
5288#ifdef GLOBAL_IME
5289 global_ime_init(atom, s_hwnd);
5290#endif
5291#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5292 dyn_imm_load();
5293#endif
5294
Bram Moolenaar734a8672019-12-02 22:49:38 +01005295 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005296 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005297 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005298 wndclassw.style = CS_OWNDC;
5299 wndclassw.lpfnWndProc = _TextAreaWndProc;
5300 wndclassw.cbClsExtra = 0;
5301 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005302 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005303 wndclassw.hIcon = NULL;
5304 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5305 wndclassw.hbrBackground = NULL;
5306 wndclassw.lpszMenuName = NULL;
5307 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005308
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005309 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return FAIL;
5311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005313 s_textArea = CreateWindowExW(
5314 0,
5315 szTextAreaClassW, L"Vim text area",
5316 WS_CHILD | WS_VISIBLE, 0, 0,
5317 100, // Any value will do for now
5318 100, // Any value will do for now
5319 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005320 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (s_textArea == NULL)
5323 return FAIL;
5324
Bram Moolenaar20321902016-02-17 12:30:17 +01005325#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005326 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005327 {
5328 HANDLE hIcon = NULL;
5329
5330 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005331 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005332 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005333#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335#ifdef FEAT_MENU
5336 s_menuBar = CreateMenu();
5337#endif
5338 s_hdc = GetDC(s_textArea);
5339
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
Bram Moolenaar734a8672019-12-02 22:49:38 +01005342 // Do we need to bother with this?
5343 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaar734a8672019-12-02 22:49:38 +01005345 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 gui_mch_def_colors();
5347
Bram Moolenaar734a8672019-12-02 22:49:38 +01005348 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5349 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 set_normal_colors();
5351
5352 /*
5353 * Check that none of the colors are the same as the background color.
5354 * Then store the current values as the defaults.
5355 */
5356 gui_check_colors();
5357 gui.def_norm_pixel = gui.norm_pixel;
5358 gui.def_back_pixel = gui.back_pixel;
5359
Bram Moolenaar734a8672019-12-02 22:49:38 +01005360 // Get the colors for the highlight groups (gui_check_colors() might have
5361 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 highlight_gui_started();
5363
5364 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005365 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005367 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369 /*
5370 * Set up for Intellimouse processing
5371 */
5372 init_mouse_wheel();
5373
5374 /*
5375 * compute a couple of metrics used for the dialogs
5376 */
5377 get_dialog_font_metrics();
5378#ifdef FEAT_TOOLBAR
5379 /*
5380 * Create the toolbar
5381 */
5382 initialise_toolbar();
5383#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005384#ifdef FEAT_GUI_TABLINE
5385 /*
5386 * Create the tabline
5387 */
5388 initialise_tabline();
5389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#ifdef MSWIN_FIND_REPLACE
5391 /*
5392 * Initialise the dialog box stuff
5393 */
5394 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5395
Bram Moolenaar734a8672019-12-02 22:49:38 +01005396 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005398 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005400 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5402 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5403 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005406#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005407# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005408// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005409# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005410# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005411# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005412# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005413 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005414 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005415#endif
5416
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005417#ifdef FEAT_RENDER_OPTIONS
5418 if (p_rop)
5419 (void)gui_mch_set_rendering_options(p_rop);
5420#endif
5421
Bram Moolenaar748bf032005-02-02 23:04:36 +00005422theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005423 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005424 display_errors();
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return OK;
5427}
5428
5429/*
5430 * Get the size of the screen, taking position on multiple monitors into
5431 * account (if supported).
5432 */
5433 static void
5434get_work_area(RECT *spi_rect)
5435{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005436 HMONITOR mon;
5437 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaar734a8672019-12-02 22:49:38 +01005439 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005440 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005441 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005443 moninfo.cbSize = sizeof(MONITORINFO);
5444 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005446 *spi_rect = moninfo.rcWork;
5447 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005450 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5452}
5453
5454/*
5455 * Set the size of the window to the given width and height in pixels.
5456 */
5457 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005458gui_mch_set_shellsize(
5459 int width,
5460 int height,
5461 int min_width UNUSED,
5462 int min_height UNUSED,
5463 int base_width UNUSED,
5464 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005465 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005468 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar734a8672019-12-02 22:49:38 +01005471 // Try to keep window completely on screen.
5472 // Get position of the screen work area. This is the part that is not
5473 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 get_work_area(&workarea_rect);
5475
Bram Moolenaar734a8672019-12-02 22:49:38 +01005476 // Resizing a maximized window looks very strange, unzoom it first.
5477 // But don't do it when still starting up, it may have been requested in
5478 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005479 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005481
5482 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
Bram Moolenaar734a8672019-12-02 22:49:38 +01005484 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005485 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005486 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005487 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005488 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 + GetSystemMetrics(SM_CYCAPTION)
5490#ifdef FEAT_MENU
5491 + gui_mswin_get_menu_height(FALSE)
5492#endif
5493 ;
5494
Bram Moolenaar734a8672019-12-02 22:49:38 +01005495 // The following should take care of keeping Vim on the same monitor, no
5496 // matter if the secondary monitor is left or right of the primary
5497 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005498 window_rect.right = window_rect.left + win_width;
5499 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005500
Bram Moolenaar734a8672019-12-02 22:49:38 +01005501 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005502 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5503 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005505 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5506 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005508 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5509 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005511 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5512 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005514 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5515 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 SetActiveWindow(s_hwnd);
5518 SetFocus(s_hwnd);
5519
5520#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005521 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 gui_mswin_get_menu_height(!gui.starting);
5523#endif
5524}
5525
5526
5527 void
5528gui_mch_set_scrollbar_thumb(
5529 scrollbar_T *sb,
5530 long val,
5531 long size,
5532 long max)
5533{
5534 SCROLLINFO info;
5535
5536 sb->scroll_shift = 0;
5537 while (max > 32767)
5538 {
5539 max = (max + 1) >> 1;
5540 val >>= 1;
5541 size >>= 1;
5542 ++sb->scroll_shift;
5543 }
5544
5545 if (sb->scroll_shift > 0)
5546 ++size;
5547
5548 info.cbSize = sizeof(info);
5549 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5550 info.nPos = val;
5551 info.nMin = 0;
5552 info.nMax = max;
5553 info.nPage = size;
5554 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5555}
5556
5557
5558/*
5559 * Set the current text font.
5560 */
5561 void
5562gui_mch_set_font(GuiFont font)
5563{
5564 gui.currFont = font;
5565}
5566
5567
5568/*
5569 * Set the current text foreground color.
5570 */
5571 void
5572gui_mch_set_fg_color(guicolor_T color)
5573{
5574 gui.currFgColor = color;
5575}
5576
5577/*
5578 * Set the current text background color.
5579 */
5580 void
5581gui_mch_set_bg_color(guicolor_T color)
5582{
5583 gui.currBgColor = color;
5584}
5585
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005586/*
5587 * Set the current text special color.
5588 */
5589 void
5590gui_mch_set_sp_color(guicolor_T color)
5591{
5592 gui.currSpColor = color;
5593}
5594
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005595#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596/*
5597 * Multi-byte handling, originally by Sung-Hoon Baek.
5598 * First static functions (no prototypes generated).
5599 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005600# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005601# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005602# endif
5603# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 * handle WM_IME_NOTIFY message
5607 */
5608 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005609_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
5611 LRESULT lResult = 0;
5612 HIMC hImc;
5613
5614 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5615 return lResult;
5616 switch (dwCommand)
5617 {
5618 case IMN_SETOPENSTATUS:
5619 if (pImmGetOpenStatus(hImc))
5620 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005621 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 im_set_position(gui.row, gui.col);
5623
Bram Moolenaar734a8672019-12-02 22:49:38 +01005624 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 State &= ~LANGMAP;
5626 if (State & INSERT)
5627 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005628# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005629 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5631 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005632 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 int old_row = gui.row;
5634 int old_col = gui.col;
5635
5636 // This must be called here before
5637 // status_redraw_curbuf(), otherwise the mode
5638 // message may appear in the wrong position.
5639 showmode();
5640 status_redraw_curbuf();
5641 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005642 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 gui.row = old_row;
5644 gui.col = old_col;
5645 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648 }
5649 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005650 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 lResult = 0;
5652 break;
5653 }
5654 pImmReleaseContext(hWnd, hImc);
5655 return lResult;
5656}
5657
5658 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005659_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 char_u *ret;
5662 int len;
5663
Bram Moolenaar734a8672019-12-02 22:49:38 +01005664 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 return 0;
5666
5667 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5668 if (ret != NULL)
5669 {
5670 add_to_input_buf_csi(ret, len);
5671 vim_free(ret);
5672 return 1;
5673 }
5674 return 0;
5675}
5676
5677/*
5678 * get the current composition string, in UCS-2; *lenp is the number of
5679 * *lenp is the number of Unicode characters.
5680 */
5681 static short_u *
5682GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5683{
5684 LONG ret;
5685 LPWSTR wbuf = NULL;
5686 char_u *buf;
5687
5688 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005689 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaar734a8672019-12-02 22:49:38 +01005691 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5693 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005694 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
5696 if (ret > 0)
5697 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005698 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005699 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (wbuf != NULL)
5701 {
5702 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5703 *lenp = ret / sizeof(WCHAR);
5704 }
5705 return (short_u *)wbuf;
5706 }
5707
Bram Moolenaar734a8672019-12-02 22:49:38 +01005708 // ret < 0; we got an error, so try the ANSI version. This'll work
5709 // on 9x/ME, but only if the codepage happens to be set to whatever
5710 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5712 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005713 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 buf = alloc(ret);
5716 if (buf == NULL)
5717 return NULL;
5718 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5719
Bram Moolenaar734a8672019-12-02 22:49:38 +01005720 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005721 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 vim_free(buf);
5723
5724 return (short_u *)wbuf;
5725}
5726
5727/*
5728 * void GetResultStr()
5729 *
5730 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5731 * get complete composition string
5732 */
5733 static char_u *
5734GetResultStr(HWND hwnd, int GCS, int *lenp)
5735{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005736 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 short_u *buf = NULL;
5738 char_u *convbuf = NULL;
5739
5740 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5741 return NULL;
5742
Bram Moolenaar734a8672019-12-02 22:49:38 +01005743 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5745 if (buf == NULL)
5746 return NULL;
5747
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005748 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 pImmReleaseContext(hwnd, hIMC);
5750 vim_free(buf);
5751 return convbuf;
5752}
5753#endif
5754
Bram Moolenaar734a8672019-12-02 22:49:38 +01005755// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005756#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758/*
5759 * set font to IM.
5760 */
5761 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005762im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 HIMC hImc;
5765
5766 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5767 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005768 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 pImmReleaseContext(s_hwnd, hImc);
5770 }
5771}
5772
5773/*
5774 * Notify cursor position to IM.
5775 */
5776 void
5777im_set_position(int row, int col)
5778{
5779 HIMC hImc;
5780
5781 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5782 {
5783 COMPOSITIONFORM cfs;
5784
5785 cfs.dwStyle = CFS_POINT;
5786 cfs.ptCurrentPos.x = FILL_X(col);
5787 cfs.ptCurrentPos.y = FILL_Y(row);
5788 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5789 pImmSetCompositionWindow(hImc, &cfs);
5790
5791 pImmReleaseContext(s_hwnd, hImc);
5792 }
5793}
5794
5795/*
5796 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5797 */
5798 void
5799im_set_active(int active)
5800{
5801 HIMC hImc;
5802 static HIMC hImcOld = (HIMC)0;
5803
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005804# ifdef VIMDLL
5805 if (!gui.in_use && !gui.starting)
5806 {
5807 mbyte_im_set_active(active);
5808 return;
5809 }
5810# endif
5811
Bram Moolenaar734a8672019-12-02 22:49:38 +01005812 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
5814 if (p_imdisable)
5815 {
5816 if (hImcOld == (HIMC)0)
5817 {
5818 hImcOld = pImmGetContext(s_hwnd);
5819 if (hImcOld)
5820 pImmAssociateContext(s_hwnd, (HIMC)0);
5821 }
5822 active = FALSE;
5823 }
5824 else if (hImcOld != (HIMC)0)
5825 {
5826 pImmAssociateContext(s_hwnd, hImcOld);
5827 hImcOld = (HIMC)0;
5828 }
5829
5830 hImc = pImmGetContext(s_hwnd);
5831 if (hImc)
5832 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005833 /*
5834 * for Korean ime
5835 */
5836 HKL hKL = GetKeyboardLayout(0);
5837
5838 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5839 {
5840 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5841 static BOOL bSaved = FALSE;
5842
5843 if (active)
5844 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005845 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005846 if (bSaved)
5847 pImmSetConversionStatus(hImc, dwConversionSaved,
5848 dwSentenceSaved);
5849 bSaved = FALSE;
5850 }
5851 else
5852 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005853 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005854 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5855 &dwSentenceSaved))
5856 {
5857 bSaved = TRUE;
5858 pImmSetConversionStatus(hImc,
5859 dwConversionSaved & ~(IME_CMODE_NATIVE
5860 | IME_CMODE_FULLSHAPE),
5861 dwSentenceSaved);
5862 }
5863 }
5864 }
5865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 pImmSetOpenStatus(hImc, active);
5867 pImmReleaseContext(s_hwnd, hImc);
5868 }
5869 }
5870}
5871
5872/*
5873 * Get IM status. When IM is on, return not 0. Else return 0.
5874 */
5875 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005876im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 int status = 0;
5879 HIMC hImc;
5880
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005881# ifdef VIMDLL
5882 if (!gui.in_use && !gui.starting)
5883 return mbyte_im_get_status();
5884# endif
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5887 {
5888 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5889 pImmReleaseContext(s_hwnd, hImc);
5890 }
5891 return status;
5892}
5893
Bram Moolenaar734a8672019-12-02 22:49:38 +01005894#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005896#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005897// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
5899/*
5900 * Notify cursor position to IM.
5901 */
5902 void
5903im_set_position(int row, int col)
5904{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005905 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 POINT p;
5907
5908 p.x = FILL_X(col);
5909 p.y = FILL_Y(row);
5910 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5911 global_ime_set_position(&p);
5912}
5913
5914/*
5915 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5916 */
5917 void
5918im_set_active(int active)
5919{
5920 global_ime_set_status(active);
5921}
5922
5923/*
5924 * Get IM status. When IM is on, return not 0. Else return 0.
5925 */
5926 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005927im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
5929 return global_ime_get_status();
5930}
5931#endif
5932
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005933/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005934 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005935 */
5936 static void
5937latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5938{
5939 int c;
5940
Bram Moolenaarca003e12006-03-17 23:19:38 +00005941 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005942 {
5943 c = *text++;
5944 switch (c)
5945 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005946 case 0xa4: c = 0x20ac; break; // euro
5947 case 0xa6: c = 0x0160; break; // S hat
5948 case 0xa8: c = 0x0161; break; // S -hat
5949 case 0xb4: c = 0x017d; break; // Z hat
5950 case 0xb8: c = 0x017e; break; // Z -hat
5951 case 0xbc: c = 0x0152; break; // OE
5952 case 0xbd: c = 0x0153; break; // oe
5953 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005954 }
5955 *unicodebuf++ = c;
5956 }
5957}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
5959#ifdef FEAT_RIGHTLEFT
5960/*
5961 * What is this for? In the case where you are using Win98 or Win2K or later,
5962 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5963 * reverses the string sent to the TextOut... family. This sucks, because we
5964 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5965 * way to tell Windblows not to do this!
5966 *
5967 * The short of it is that this 'RevOut' only gets called if you are running
5968 * one of the new, "improved" MS OSes, and only if you are running in
5969 * 'rightleft' mode. It makes display take *slightly* longer, but not
5970 * noticeably so.
5971 */
5972 static void
5973RevOut( HDC s_hdc,
5974 int col,
5975 int row,
5976 UINT foptions,
5977 CONST RECT *pcliprect,
5978 LPCTSTR text,
5979 UINT len,
5980 CONST INT *padding)
5981{
5982 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005984 for (ix = 0; ix < (int)len; ++ix)
5985 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5986 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987}
5988#endif
5989
Bram Moolenaar92467d32017-12-05 13:22:16 +01005990 static void
5991draw_line(
5992 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005993 int y1,
5994 int x2,
5995 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005996 COLORREF color)
5997{
5998#if defined(FEAT_DIRECTX)
5999 if (IS_ENABLE_DIRECTX())
6000 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6001 else
6002#endif
6003 {
6004 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6005 HPEN old_pen = SelectObject(s_hdc, hpen);
6006 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006007 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006008 LineTo(s_hdc, x2, y2);
6009 DeleteObject(SelectObject(s_hdc, old_pen));
6010 }
6011}
6012
6013 static void
6014set_pixel(
6015 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006016 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006017 COLORREF color)
6018{
6019#if defined(FEAT_DIRECTX)
6020 if (IS_ENABLE_DIRECTX())
6021 DWriteContext_SetPixel(s_dwc, x, y, color);
6022 else
6023#endif
6024 SetPixel(s_hdc, x, y, color);
6025}
6026
6027 static void
6028fill_rect(
6029 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006030 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006031 COLORREF color)
6032{
6033#if defined(FEAT_DIRECTX)
6034 if (IS_ENABLE_DIRECTX())
6035 DWriteContext_FillRect(s_dwc, rcp, color);
6036 else
6037#endif
6038 {
6039 HBRUSH hbr2;
6040
6041 if (hbr == NULL)
6042 hbr2 = CreateSolidBrush(color);
6043 else
6044 hbr2 = hbr;
6045 FillRect(s_hdc, rcp, hbr2);
6046 if (hbr == NULL)
6047 DeleteBrush(hbr2);
6048 }
6049}
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 void
6052gui_mch_draw_string(
6053 int row,
6054 int col,
6055 char_u *text,
6056 int len,
6057 int flags)
6058{
6059 static int *padding = NULL;
6060 static int pad_size = 0;
6061 int i;
6062 const RECT *pcliprect = NULL;
6063 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 static WCHAR *unicodebuf = NULL;
6065 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006066 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 int y;
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 /*
6071 * Italic and bold text seems to have an extra row of pixels at the bottom
6072 * (below where the bottom of the character should be). If we draw the
6073 * characters with a solid background, the top row of pixels in the
6074 * character below will be overwritten. We can fix this by filling in the
6075 * background ourselves, to the correct character proportions, and then
6076 * writing the character in transparent mode. Still have a problem when
6077 * the character is "_", which gets written on to the character below.
6078 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6079 * pixel in their slots, which fixes the problem with the bottom row of
6080 * pixels. We still need this code because otherwise the top row of pixels
6081 * becomes a problem. - webb.
6082 */
6083 static HBRUSH hbr_cache[2] = {NULL, NULL};
6084 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6085 static int brush_lru = 0;
6086 HBRUSH hbr;
6087 RECT rc;
6088
6089 if (!(flags & DRAW_TRANSP))
6090 {
6091 /*
6092 * Clear background first.
6093 * Note: FillRect() excludes right and bottom of rectangle.
6094 */
6095 rc.left = FILL_X(col);
6096 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (has_mbyte)
6098 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006099 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006100 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
6102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 rc.right = FILL_X(col + len);
6104 rc.bottom = FILL_Y(row + 1);
6105
Bram Moolenaar734a8672019-12-02 22:49:38 +01006106 // Cache the created brush, that saves a lot of time. We need two:
6107 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (gui.currBgColor == brush_color[0])
6109 {
6110 hbr = hbr_cache[0];
6111 brush_lru = 1;
6112 }
6113 else if (gui.currBgColor == brush_color[1])
6114 {
6115 hbr = hbr_cache[1];
6116 brush_lru = 0;
6117 }
6118 else
6119 {
6120 if (hbr_cache[brush_lru] != NULL)
6121 DeleteBrush(hbr_cache[brush_lru]);
6122 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6123 brush_color[brush_lru] = gui.currBgColor;
6124 hbr = hbr_cache[brush_lru];
6125 brush_lru = !brush_lru;
6126 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006127
Bram Moolenaar92467d32017-12-05 13:22:16 +01006128 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 SetBkMode(s_hdc, TRANSPARENT);
6131
6132 /*
6133 * When drawing block cursor, prevent inverted character spilling
6134 * over character cell (can happen with bold/italic)
6135 */
6136 if (flags & DRAW_CURSOR)
6137 {
6138 pcliprect = &rc;
6139 foptions = ETO_CLIPPED;
6140 }
6141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 SetTextColor(s_hdc, gui.currFgColor);
6143 SelectFont(s_hdc, gui.currFont);
6144
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006145#ifdef FEAT_DIRECTX
6146 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006147 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006148#endif
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6151 {
6152 vim_free(padding);
6153 pad_size = Columns;
6154
Bram Moolenaar734a8672019-12-02 22:49:38 +01006155 // Don't give an out-of-memory message here, it would call us
6156 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006157 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (padding != NULL)
6159 for (i = 0; i < pad_size; i++)
6160 padding[i] = gui.char_width;
6161 }
6162
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 /*
6164 * We have to provide the padding argument because italic and bold versions
6165 * of fixed-width fonts are often one pixel or so wider than their normal
6166 * versions.
6167 * No check for DRAW_BOLD, Windows will have done it already.
6168 */
6169
Bram Moolenaar734a8672019-12-02 22:49:38 +01006170 // Check if there are any UTF-8 characters. If not, use normal text
6171 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (enc_utf8)
6173 for (n = 0; n < len; ++n)
6174 if (text[n] >= 0x80)
6175 break;
6176
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006177#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006178 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6179 // required that unicode drawing routine, currently. So this forces it
6180 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006181 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006182 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006183#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006184
Bram Moolenaar734a8672019-12-02 22:49:38 +01006185 // Check if the Unicode buffer exists and is big enough. Create it
6186 // with the same length as the multi-byte string, the number of wide
6187 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006188 if ((enc_utf8
6189 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6190 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 && (unicodebuf == NULL || len > unibuflen))
6192 {
6193 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006194 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195
6196 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006197 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 unibuflen = len;
6200 }
6201
6202 if (enc_utf8 && n < len && unicodebuf != NULL)
6203 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006204 // Output UTF-8 characters. Composing characters should be
6205 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006206 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006207 int wlen; // string length in words
6208 int clen; // string length in characters
6209 int cells; // cell width of string up to composing char
6210 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006211 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006213 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006214 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006216 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006218 c = utf_ptr2char(text + i);
6219 if (c >= 0x10000)
6220 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006221 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006222 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6223 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006224 }
6225 else
6226 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006227 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006228 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006229
6230 if (utf_iscomposing(c))
6231 cw = 0;
6232 else
6233 {
6234 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006235 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006236 cw = 1;
6237 }
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (unicodepdy != NULL)
6240 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006241 // Use unicodepdy to make characters fit as we expect, even
6242 // when the font uses different widths (e.g., bold character
6243 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006244 if (c >= 0x10000)
6245 {
6246 unicodepdy[wlen - 2] = cw * gui.char_width;
6247 unicodepdy[wlen - 1] = 0;
6248 }
6249 else
6250 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006253 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006254 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006256#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006257 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006258 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006259 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006260 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006261 TEXT_X(col), TEXT_Y(row),
6262 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006263 gui.char_width, gui.currFgColor,
6264 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006265 }
6266 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006267#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006268 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6269 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006270 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006272 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006274 // If we want to display codepage data, and the current CP is not the
6275 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (unicodebuf != NULL)
6277 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006278 if (enc_latin9)
6279 latin9_to_ucs(text, len, unicodebuf);
6280 else
6281 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 MB_PRECOMPOSED,
6283 (char *)text, len,
6284 (LPWSTR)unicodebuf, unibuflen);
6285 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006286 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006287 // Use unicodepdy to make characters fit as we expect, even
6288 // when the font uses different widths (e.g., bold character
6289 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006290 if (unicodepdy != NULL)
6291 {
6292 int i;
6293 int cw;
6294
6295 for (i = 0; i < len; ++i)
6296 {
6297 cw = utf_char2cells(unicodebuf[i]);
6298 if (cw > 2)
6299 cw = 1;
6300 unicodepdy[i] = cw * gui.char_width;
6301 }
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006304 foptions, pcliprect, unicodebuf, len, unicodepdy);
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307 }
6308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
6310#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006311 // Windows will mess up RL text, so we have to draw it character by
6312 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006313 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6315 foptions, pcliprect, (char *)text, len, padding);
6316 else
6317#endif
6318 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6319 foptions, pcliprect, (char *)text, len, padding);
6320 }
6321
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (flags & DRAW_UNDERL)
6324 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006325 // When p_linespace is 0, overwrite the bottom row of pixels.
6326 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (p_linespace > 1)
6329 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006330 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006332
Bram Moolenaar734a8672019-12-02 22:49:38 +01006333 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006334 if (flags & DRAW_STRIKE)
6335 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006336 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006337 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006338 }
6339
Bram Moolenaar734a8672019-12-02 22:49:38 +01006340 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006341 if (flags & DRAW_UNDERC)
6342 {
6343 int x;
6344 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006345 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006346
6347 y = FILL_Y(row + 1) - 1;
6348 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6349 {
6350 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006351 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006352 }
6353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356
6357/*
6358 * Output routines.
6359 */
6360
Bram Moolenaar734a8672019-12-02 22:49:38 +01006361/*
6362 * Flush any output to the screen
6363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 void
6365gui_mch_flush(void)
6366{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006367#if defined(FEAT_DIRECTX)
6368 if (IS_ENABLE_DIRECTX())
6369 DWriteContext_Flush(s_dwc);
6370#endif
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 GdiFlush();
6373}
6374
6375 static void
6376clear_rect(RECT *rcp)
6377{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006378 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379}
6380
6381
Bram Moolenaarc716c302006-01-21 22:12:51 +00006382 void
6383gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6384{
6385 RECT workarea_rect;
6386
6387 get_work_area(&workarea_rect);
6388
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006389 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006390 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006391 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006392
Bram Moolenaar734a8672019-12-02 22:49:38 +01006393 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6394 // the menubar for MSwin, we subtract it from the screen height, so that
6395 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006396 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006397 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006398 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006399 - GetSystemMetrics(SM_CYCAPTION)
6400#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006401 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006402#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006403 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006404}
6405
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407#if defined(FEAT_MENU) || defined(PROTO)
6408/*
6409 * Add a sub menu to the menu bar.
6410 */
6411 void
6412gui_mch_add_menu(
6413 vimmenu_T *menu,
6414 int pos)
6415{
6416 vimmenu_T *parent = menu->parent;
6417
6418 menu->submenu_id = CreatePopupMenu();
6419 menu->id = s_menu_id++;
6420
6421 if (menu_is_menubar(menu->name))
6422 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006423 WCHAR *wn;
6424 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006426 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006427 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006428 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006430 infow.cbSize = sizeof(infow);
6431 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6432 | MIIM_SUBMENU;
6433 infow.dwItemData = (long_u)menu;
6434 infow.wID = menu->id;
6435 infow.fType = MFT_STRING;
6436 infow.dwTypeData = wn;
6437 infow.cch = (UINT)wcslen(wn);
6438 infow.hSubMenu = menu->submenu_id;
6439 InsertMenuItemW((parent == NULL)
6440 ? s_menuBar : parent->submenu_id,
6441 (UINT)pos, TRUE, &infow);
6442 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 }
6444
Bram Moolenaar734a8672019-12-02 22:49:38 +01006445 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (parent == NULL)
6447 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006448# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 else if (IsWindow(parent->tearoff_handle))
6450 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452}
6453
6454 void
6455gui_mch_show_popupmenu(vimmenu_T *menu)
6456{
6457 POINT mp;
6458
6459 (void)GetCursorPos((LPPOINT)&mp);
6460 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6461}
6462
6463 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006464gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 vimmenu_T *menu = gui_find_menu(path_name);
6467
6468 if (menu != NULL)
6469 {
6470 POINT p;
6471
Bram Moolenaar734a8672019-12-02 22:49:38 +01006472 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006474 if (mouse_pos)
6475 {
6476 int mx, my;
6477
6478 gui_mch_getmouse(&mx, &my);
6479 p.x += mx;
6480 p.y += my;
6481 }
6482 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006484 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6486 }
6487 msg_scroll = FALSE;
6488 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6489 }
6490}
6491
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006492# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493/*
6494 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6495 * create it as a pseudo-"tearoff menu".
6496 */
6497 void
6498gui_make_tearoff(char_u *path_name)
6499{
6500 vimmenu_T *menu = gui_find_menu(path_name);
6501
Bram Moolenaar734a8672019-12-02 22:49:38 +01006502 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (menu != NULL)
6504 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6505}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508/*
6509 * Add a menu item to a menu
6510 */
6511 void
6512gui_mch_add_menu_item(
6513 vimmenu_T *menu,
6514 int idx)
6515{
6516 vimmenu_T *parent = menu->parent;
6517
6518 menu->id = s_menu_id++;
6519 menu->submenu_id = NULL;
6520
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006521# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6523 {
6524 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6525 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6526 }
6527 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006528# endif
6529# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (menu_is_toolbar(parent->name))
6531 {
6532 TBBUTTON newtb;
6533
Bram Moolenaara80faa82020-04-12 19:37:17 +02006534 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (menu_is_separator(menu->name))
6536 {
6537 newtb.iBitmap = 0;
6538 newtb.fsStyle = TBSTYLE_SEP;
6539 }
6540 else
6541 {
6542 newtb.iBitmap = get_toolbar_bitmap(menu);
6543 newtb.fsStyle = TBSTYLE_BUTTON;
6544 }
6545 newtb.idCommand = menu->id;
6546 newtb.fsState = TBSTATE_ENABLED;
6547 newtb.iString = 0;
6548 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6549 (LPARAM)&newtb);
6550 menu->submenu_id = (HMENU)-1;
6551 }
6552 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006555 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006557 wn = enc_to_utf16(menu->name, NULL);
6558 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006560 InsertMenuW(parent->submenu_id, (UINT)idx,
6561 (menu_is_separator(menu->name)
6562 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6563 (UINT)menu->id, wn);
6564 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006566# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 if (IsWindow(parent->tearoff_handle))
6568 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 }
6571}
6572
6573/*
6574 * Destroy the machine specific menu widget.
6575 */
6576 void
6577gui_mch_destroy_menu(vimmenu_T *menu)
6578{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006579# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 /*
6581 * is this a toolbar button?
6582 */
6583 if (menu->submenu_id == (HMENU)-1)
6584 {
6585 int iButton;
6586
6587 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6588 (WPARAM)menu->id, 0);
6589 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6590 }
6591 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 {
6594 if (menu->parent != NULL
6595 && menu_is_popup(menu->parent->dname)
6596 && menu->parent->submenu_id != NULL)
6597 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6598 else
6599 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6600 if (menu->submenu_id != NULL)
6601 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006602# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (IsWindow(menu->tearoff_handle))
6604 DestroyWindow(menu->tearoff_handle);
6605 if (menu->parent != NULL
6606 && menu->parent->children != NULL
6607 && IsWindow(menu->parent->tearoff_handle))
6608 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006609 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 menu->modes = 0;
6611 rebuild_tearoff(menu->parent);
6612 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615}
6616
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006617# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 static void
6619rebuild_tearoff(vimmenu_T *menu)
6620{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006621 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 char_u tbuf[128];
6623 RECT trect;
6624 RECT rct;
6625 RECT roct;
6626 int x, y;
6627
6628 HWND thwnd = menu->tearoff_handle;
6629
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006630 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (GetWindowRect(thwnd, &trect)
6632 && GetWindowRect(s_hwnd, &rct)
6633 && GetClientRect(s_hwnd, &roct))
6634 {
6635 x = trect.left - rct.left;
6636 y = (trect.top - rct.bottom + roct.bottom);
6637 }
6638 else
6639 {
6640 x = y = 0xffffL;
6641 }
6642 DestroyWindow(thwnd);
6643 if (menu->children != NULL)
6644 {
6645 gui_mch_tearoff(tbuf, menu, x, y);
6646 if (IsWindow(menu->tearoff_handle))
6647 (void) SetWindowPos(menu->tearoff_handle,
6648 NULL,
6649 (int)trect.left,
6650 (int)trect.top,
6651 0, 0,
6652 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6653 }
6654}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006655# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657/*
6658 * Make a menu either grey or not grey.
6659 */
6660 void
6661gui_mch_menu_grey(
6662 vimmenu_T *menu,
6663 int grey)
6664{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006665# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 /*
6667 * is this a toolbar button?
6668 */
6669 if (menu->submenu_id == (HMENU)-1)
6670 {
6671 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6672 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6673 }
6674 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006675# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006676 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6677 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006679# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6681 {
6682 WORD menuID;
6683 HWND menuHandle;
6684
6685 /*
6686 * A tearoff button has changed state.
6687 */
6688 if (menu->children == NULL)
6689 menuID = (WORD)(menu->id);
6690 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006691 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6693 if (menuHandle)
6694 EnableWindow(menuHandle, !grey);
6695
6696 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
Bram Moolenaar734a8672019-12-02 22:49:38 +01006700#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702
Bram Moolenaar734a8672019-12-02 22:49:38 +01006703// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6706#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006707#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6710/*
6711 * stuff for dialogs
6712 */
6713
6714/*
6715 * The callback routine used by all the dialogs. Very simple. First,
6716 * acknowledges the INITDIALOG message so that Windows knows to do standard
6717 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6718 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6719 * number.
6720 */
6721 static LRESULT CALLBACK
6722dialog_callback(
6723 HWND hwnd,
6724 UINT message,
6725 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006726 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 if (message == WM_INITDIALOG)
6729 {
6730 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006731 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 (void)SetFocus(hwnd);
6733 if (dialog_default_button > IDCANCEL)
6734 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006735 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006736 // We don't have a default, set focus on another element of the
6737 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006738 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 return FALSE;
6740 }
6741
6742 if (message == WM_COMMAND)
6743 {
6744 int button = LOWORD(wParam);
6745
Bram Moolenaar734a8672019-12-02 22:49:38 +01006746 // Don't end the dialog if something was selected that was
6747 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (button >= DLG_NONBUTTON_CONTROL)
6749 return TRUE;
6750
Bram Moolenaar734a8672019-12-02 22:49:38 +01006751 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006753 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006754 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006755 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006756
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006757 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6758 p = utf16_to_enc(wp, NULL);
6759 vim_strncpy(s_textfield, p, IOSIZE);
6760 vim_free(p);
6761 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763
6764 /*
6765 * Need to check for IDOK because if the user just hits Return to
6766 * accept the default value, some reason this is what we get.
6767 */
6768 if (button == IDOK)
6769 {
6770 if (dialog_default_button > IDCANCEL)
6771 EndDialog(hwnd, dialog_default_button);
6772 }
6773 else
6774 EndDialog(hwnd, button - IDCANCEL);
6775 return TRUE;
6776 }
6777
6778 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6779 {
6780 EndDialog(hwnd, 0);
6781 return TRUE;
6782 }
6783 return FALSE;
6784}
6785
6786/*
6787 * Create a dialog dynamically from the parameter strings.
6788 * type = type of dialog (question, alert, etc.)
6789 * title = dialog title. may be NULL for default title.
6790 * message = text to display. Dialog sizes to accommodate it.
6791 * buttons = '\n' separated list of button captions, default first.
6792 * dfltbutton = number of default button.
6793 *
6794 * This routine returns 1 if the first button is pressed,
6795 * 2 for the second, etc.
6796 *
6797 * 0 indicates Esc was pressed.
6798 * -1 for unexpected error
6799 *
6800 * If stubbing out this fn, return 1.
6801 */
6802
Bram Moolenaar734a8672019-12-02 22:49:38 +01006803static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 "IDR_VIM",
6806 "IDR_VIM_ERROR",
6807 "IDR_VIM_ALERT",
6808 "IDR_VIM_INFO",
6809 "IDR_VIM_QUESTION"
6810};
6811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 int
6813gui_mch_dialog(
6814 int type,
6815 char_u *title,
6816 char_u *message,
6817 char_u *buttons,
6818 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006819 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006820 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006823 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 int numButtons;
6825 int *buttonWidths, *buttonPositions;
6826 int buttonYpos;
6827 int nchar, i;
6828 DWORD lStyle;
6829 int dlgwidth = 0;
6830 int dlgheight;
6831 int editboxheight;
6832 int horizWidth = 0;
6833 int msgheight;
6834 char_u *pstart;
6835 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006836 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 char_u *tbuffer;
6838 RECT rect;
6839 HWND hwnd;
6840 HDC hdc;
6841 HFONT font, oldFont;
6842 TEXTMETRIC fontInfo;
6843 int fontHeight;
6844 int textWidth, minButtonWidth, messageWidth;
6845 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006846 int maxDialogHeight;
6847 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 int vertical;
6849 int dlgPaddingX;
6850 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006851# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006852 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006854# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006855 garray_T ga;
6856 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006858# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006859 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006860# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006861 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006862# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006863 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006864 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006865# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
Bram Moolenaar748bf032005-02-02 23:04:36 +00006867 if (s_hwnd == NULL)
6868 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
6870 if ((type < 0) || (type > VIM_LAST_TYPE))
6871 type = 0;
6872
Bram Moolenaar734a8672019-12-02 22:49:38 +01006873 // allocate some memory for dialog template
6874 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006875 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006876 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878 if (p == NULL)
6879 return -1;
6880
6881 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006882 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6884 * const.
6885 */
6886 tbuffer = vim_strsave(buttons);
6887 if (tbuffer == NULL)
6888 return -1;
6889
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891
Bram Moolenaar734a8672019-12-02 22:49:38 +01006892 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 numButtons = 1;
6894 for (i = 0; tbuffer[i] != '\0'; i++)
6895 {
6896 if (tbuffer[i] == DLG_BUTTON_SEP)
6897 numButtons++;
6898 }
6899 if (dfltbutton >= numButtons)
6900 dfltbutton = -1;
6901
Bram Moolenaar734a8672019-12-02 22:49:38 +01006902 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006903 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (buttonWidths == NULL)
6905 return -1;
6906
Bram Moolenaar734a8672019-12-02 22:49:38 +01006907 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006908 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (buttonPositions == NULL)
6910 return -1;
6911
6912 /*
6913 * Calculate how big the dialog must be.
6914 */
6915 hwnd = GetDesktopWindow();
6916 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006917# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6919 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006920 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 use_lfSysmenu = TRUE;
6922 }
6923 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6926 VARIABLE_PITCH , DLG_FONT_NAME);
6927 if (s_usenewlook)
6928 {
6929 oldFont = SelectFont(hdc, font);
6930 dlgPaddingX = DLG_PADDING_X;
6931 dlgPaddingY = DLG_PADDING_Y;
6932 }
6933 else
6934 {
6935 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6936 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6937 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6938 }
6939 GetTextMetrics(hdc, &fontInfo);
6940 fontHeight = fontInfo.tmHeight;
6941
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006943 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaar734a8672019-12-02 22:49:38 +01006945 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006946 if (s_hwnd == NULL)
6947 {
6948 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar734a8672019-12-02 22:49:38 +01006950 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006951 get_work_area(&workarea_rect);
6952 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6953 if (maxDialogWidth > 600)
6954 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006955 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006956 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957 }
6958 else
6959 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006960 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006961 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006962 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006963 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006964 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006965 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6966 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006967
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006968 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006969 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006970 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006971 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006972 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6973 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6974 }
6975
Bram Moolenaar734a8672019-12-02 22:49:38 +01006976 // Set dlgwidth to width of message.
6977 // Copy the message into "ga", changing NL to CR-NL and inserting line
6978 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 pstart = message;
6980 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006981 msgheight = 0;
6982 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 do
6984 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006985 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006986
Bram Moolenaar734a8672019-12-02 22:49:38 +01006987 // Need to figure out where to break the string. The system does it
6988 // at a word boundary, which would mean we can't compute the number of
6989 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006990 textWidth = 0;
6991 last_white = NULL;
6992 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006993 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006994 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006995 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006996 && textWidth > maxDialogWidth * 3 / 4)
6997 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006998 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006999 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007000 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007001 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007002 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007003 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007004 textWidth = 0;
7005
7006 if (last_white != NULL)
7007 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007008 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007009 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007010 pend = last_white + 1;
7011 last_white = NULL;
7012 }
7013 ga_append(&ga, '\r');
7014 ga_append(&ga, '\n');
7015 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007016 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007017
7018 while (--l >= 0)
7019 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007020 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007021 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007023
7024 ga_append(&ga, '\r');
7025 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 pstart = pend + 1;
7027 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007028
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007029 if (ga.ga_data != NULL)
7030 message = ga.ga_data;
7031
Bram Moolenaar734a8672019-12-02 22:49:38 +01007032 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007033
Bram Moolenaar734a8672019-12-02 22:49:38 +01007034 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02007035 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7036 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038 if (msgheight < DLG_ICON_HEIGHT)
7039 msgheight = DLG_ICON_HEIGHT;
7040
7041 /*
7042 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007043 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007045 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (!vertical)
7047 {
7048 // Place buttons horizontally if they fit.
7049 horizWidth = dlgPaddingX;
7050 pstart = tbuffer;
7051 i = 0;
7052 do
7053 {
7054 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7055 if (pend == NULL)
7056 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007057 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 if (textWidth < minButtonWidth)
7059 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007060 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 buttonWidths[i] = textWidth;
7062 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007063 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 pstart = pend + 1;
7065 } while (*pend != NUL);
7066
7067 if (horizWidth > maxDialogWidth)
7068 vertical = TRUE; // Too wide to fit on the screen.
7069 else if (horizWidth > dlgwidth)
7070 dlgwidth = horizWidth;
7071 }
7072
7073 if (vertical)
7074 {
7075 // Stack buttons vertically.
7076 pstart = tbuffer;
7077 do
7078 {
7079 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7080 if (pend == NULL)
7081 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007082 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007083 textWidth += dlgPaddingX; // Padding within button
7084 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 if (textWidth > dlgwidth)
7086 dlgwidth = textWidth;
7087 pstart = pend + 1;
7088 } while (*pend != NUL);
7089 }
7090
7091 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007092 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar734a8672019-12-02 22:49:38 +01007094 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (s_usenewlook)
7096 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7097 else
7098 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7099
7100 add_long(lStyle);
7101 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007102 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 add_word(0); // NumberOfItems(will change later)
7104 add_word(10); // x
7105 add_word(10); // y
7106 add_word(PixelToDialogX(dlgwidth)); // cx
7107
7108 // Dialog height.
7109 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007110 dlgheight = msgheight + 2 * dlgPaddingY
7111 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 else
7113 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7114
7115 // Dialog needs to be taller if contains an edit box.
7116 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7117 if (textfield != NULL)
7118 dlgheight += editboxheight;
7119
Bram Moolenaar734a8672019-12-02 22:49:38 +01007120 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007121 if (dlgheight > maxDialogHeight)
7122 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007123 msgheight = msgheight - (dlgheight - maxDialogHeight);
7124 dlgheight = maxDialogHeight;
7125 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007126 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007127 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007128 }
7129
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 add_word(PixelToDialogY(dlgheight));
7131
7132 add_word(0); // Menu
7133 add_word(0); // Class
7134
Bram Moolenaar734a8672019-12-02 22:49:38 +01007135 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007136 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7137 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 p += nchar;
7139
7140 if (s_usenewlook)
7141 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007142 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007143# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (use_lfSysmenu)
7145 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007146 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7148 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007149 wcscpy(p, lfSysmenu.lfFaceName);
7150 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
7155 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007156 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
7158 p += nchar;
7159 }
7160
7161 buttonYpos = msgheight + 2 * dlgPaddingY;
7162
7163 if (textfield != NULL)
7164 buttonYpos += editboxheight;
7165
7166 pstart = tbuffer;
7167 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007168 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 for (i = 0; i < numButtons; i++)
7170 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007171 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 for ( pend = pstart;
7173 *pend && (*pend != DLG_BUTTON_SEP);
7174 pend++)
7175 ;
7176
7177 if (*pend)
7178 *pend = '\0';
7179
7180 /*
7181 * old NOTE:
7182 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7183 * the focus to the first tab-able button and in so doing makes that
7184 * the default!! Grrr. Workaround: Make the default button the only
7185 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7186 * he/she can use arrow keys.
7187 *
7188 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007189 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 * dialog. Also needed for when the textfield is the default control.
7191 * It appears to work now (perhaps not on Win95?).
7192 */
7193 if (vertical)
7194 {
7195 p = add_dialog_element(p,
7196 (i == dfltbutton
7197 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7198 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007199 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 + 2 * fontHeight * i),
7201 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7202 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007203 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 else
7206 {
7207 p = add_dialog_element(p,
7208 (i == dfltbutton
7209 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7210 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007211 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 PixelToDialogX(buttonWidths[i]),
7213 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007214 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007216 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 }
7218 *pnumitems += numButtons;
7219
Bram Moolenaar734a8672019-12-02 22:49:38 +01007220 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 p = add_dialog_element(p, SS_ICON,
7222 PixelToDialogX(dlgPaddingX),
7223 PixelToDialogY(dlgPaddingY),
7224 PixelToDialogX(DLG_ICON_WIDTH),
7225 PixelToDialogY(DLG_ICON_HEIGHT),
7226 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7227 dlg_icons[type]);
7228
Bram Moolenaar734a8672019-12-02 22:49:38 +01007229 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007230 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7231 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7232 PixelToDialogY(dlgPaddingY),
7233 (WORD)(PixelToDialogX(messageWidth) + 1),
7234 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007235 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
Bram Moolenaar734a8672019-12-02 22:49:38 +01007237 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 if (textfield != NULL)
7239 {
7240 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7241 PixelToDialogX(2 * dlgPaddingX),
7242 PixelToDialogY(2 * dlgPaddingY + msgheight),
7243 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7244 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007245 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 *pnumitems += 1;
7247 }
7248
7249 *pnumitems += 2;
7250
7251 SelectFont(hdc, oldFont);
7252 DeleteObject(font);
7253 ReleaseDC(hwnd, hdc);
7254
Bram Moolenaar734a8672019-12-02 22:49:38 +01007255 // Let the dialog_callback() function know which button to make default
7256 // If we have an edit box, make that the default. We also need to tell
7257 // dialog_callback() if this dialog contains an edit box or not. We do
7258 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (textfield != NULL)
7260 {
7261 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7262 s_textfield = textfield;
7263 }
7264 else
7265 {
7266 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7267 s_textfield = NULL;
7268 }
7269
Bram Moolenaar734a8672019-12-02 22:49:38 +01007270 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007272 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 (LPDLGTEMPLATE)pdlgtemplate,
7274 s_hwnd,
7275 (DLGPROC)dialog_callback);
7276
7277 LocalFree(LocalHandle(pdlgtemplate));
7278 vim_free(tbuffer);
7279 vim_free(buttonWidths);
7280 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007281 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar734a8672019-12-02 22:49:38 +01007283 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 (void)SetFocus(s_hwnd);
7285
7286 return nchar;
7287}
7288
Bram Moolenaar734a8672019-12-02 22:49:38 +01007289#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291/*
7292 * Put a simple element (basic class) onto a dialog template in memory.
7293 * return a pointer to where the next item should be added.
7294 *
7295 * parameters:
7296 * lStyle = additional style flags
7297 * (be careful, NT3.51 & Win32s will ignore the new ones)
7298 * x,y = x & y positions IN DIALOG UNITS
7299 * w,h = width and height IN DIALOG UNITS
7300 * Id = ID used in messages
7301 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7302 * caption = usually text or resource name
7303 *
7304 * TODO: use the length information noted here to enable the dialog creation
7305 * routines to work out more exactly how much memory they need to alloc.
7306 */
7307 static PWORD
7308add_dialog_element(
7309 PWORD p,
7310 DWORD lStyle,
7311 WORD x,
7312 WORD y,
7313 WORD w,
7314 WORD h,
7315 WORD Id,
7316 WORD clss,
7317 const char *caption)
7318{
7319 int nchar;
7320
Bram Moolenaar734a8672019-12-02 22:49:38 +01007321 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7323 *p++ = LOWORD(lStyle);
7324 *p++ = HIWORD(lStyle);
7325 *p++ = 0; // LOWORD (lExtendedStyle)
7326 *p++ = 0; // HIWORD (lExtendedStyle)
7327 *p++ = x;
7328 *p++ = y;
7329 *p++ = w;
7330 *p++ = h;
7331 *p++ = Id; //9 or 10 words in all
7332
7333 *p++ = (WORD)0xffff;
7334 *p++ = clss; //2 more here
7335
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007336 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 p += nchar;
7338
7339 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7340
7341 return p; //total = 15+ (strlen(caption)) words
7342 // = 30 + 2(strlen(caption) bytes reqd
7343}
7344
7345
7346/*
7347 * Helper routine. Take an input pointer, return closest pointer that is
7348 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7349 */
7350 static LPWORD
7351lpwAlign(
7352 LPWORD lpIn)
7353{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007354 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007356 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 ul += 3;
7358 ul >>= 2;
7359 ul <<= 2;
7360 return (LPWORD)ul;
7361}
7362
7363/*
7364 * Helper routine. Takes second parameter as Ansi string, copies it to first
7365 * parameter as wide character (16-bits / char) string, and returns integer
7366 * number of wide characters (words) in string (including the trailing wide
7367 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007368 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7369 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 static int
7371nCopyAnsiToWideChar(
7372 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007373 LPSTR lpAnsiIn,
7374 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007377 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 int i;
7379 WCHAR *wn;
7380
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007381 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007383 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007384 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (wn != NULL)
7386 {
7387 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007388 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 vim_free(wn);
7390 }
7391 }
7392 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007393 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 nChar = MultiByteToWideChar(
7395 enc_codepage > 0 ? enc_codepage : CP_ACP,
7396 MB_PRECOMPOSED,
7397 lpAnsiIn, len,
7398 lpWCStr, len);
7399 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007400 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 return nChar;
7404}
7405
7406
7407#ifdef FEAT_TEAROFF
7408/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007409 * Lookup menu handle from "menu_id".
7410 */
7411 static HMENU
7412tearoff_lookup_menuhandle(
7413 vimmenu_T *menu,
7414 WORD menu_id)
7415{
7416 for ( ; menu != NULL; menu = menu->next)
7417 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007418 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007419 continue;
7420 if (menu_is_separator(menu->dname))
7421 continue;
7422 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7423 return menu->submenu_id;
7424 }
7425 return NULL;
7426}
7427
7428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 * The callback function for all the modeless dialogs that make up the
7430 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7431 * thinking its menus have been clicked), and go away when closed.
7432 */
7433 static LRESULT CALLBACK
7434tearoff_callback(
7435 HWND hwnd,
7436 UINT message,
7437 WPARAM wParam,
7438 LPARAM lParam)
7439{
7440 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007441 {
7442 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
Bram Moolenaar734a8672019-12-02 22:49:38 +01007446 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 HandleMouseHide(message, lParam);
7448
7449 if (message == WM_COMMAND)
7450 {
7451 if ((WORD)(LOWORD(wParam)) & 0x8000)
7452 {
7453 POINT mp;
7454 RECT rect;
7455
7456 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7457 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007458 vimmenu_T *menu;
7459
7460 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007462 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7464 (int)rect.right - 8,
7465 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007466 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 s_hwnd,
7468 NULL);
7469 /*
7470 * NOTE: The pop-up menu can eat the mouse up event.
7471 * We deal with this in normal.c.
7472 */
7473 }
7474 }
7475 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007476 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7478 /*
7479 * Give main window the focus back: this is so after
7480 * choosing a tearoff button you can start typing again
7481 * straight away.
7482 */
7483 (void)SetFocus(s_hwnd);
7484 return TRUE;
7485 }
7486 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7487 {
7488 DestroyWindow(hwnd);
7489 return TRUE;
7490 }
7491
Bram Moolenaar734a8672019-12-02 22:49:38 +01007492 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 if (message == WM_EXITSIZEMOVE)
7494 (void)SetActiveWindow(s_hwnd);
7495
7496 return FALSE;
7497}
7498#endif
7499
7500
7501/*
7502 * Decide whether to use the "new look" (small, non-bold font) or the "old
7503 * look" (big, clanky font) for dialogs, and work out a few values for use
7504 * later accordingly.
7505 */
7506 static void
7507get_dialog_font_metrics(void)
7508{
7509 HDC hdc;
7510 HFONT hfontTools = 0;
7511 DWORD dlgFontSize;
7512 SIZE size;
7513#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007514 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#endif
7516
7517 s_usenewlook = FALSE;
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007520 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007521 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#endif
7524 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7525 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7526
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007527 if (hfontTools)
7528 {
7529 hdc = GetDC(s_hwnd);
7530 SelectObject(hdc, hfontTools);
7531 /*
7532 * GetTextMetrics() doesn't return the right value in
7533 * tmAveCharWidth, so we have to figure out the dialog base units
7534 * ourselves.
7535 */
7536 GetTextExtentPoint(hdc,
7537 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7538 52, &size);
7539 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007541 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7542 s_dlgfntheight = (WORD)size.cy;
7543 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 }
7545
7546 if (!s_usenewlook)
7547 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007548 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 s_dlgfntwidth = LOWORD(dlgFontSize);
7550 s_dlgfntheight = HIWORD(dlgFontSize);
7551 }
7552}
7553
7554#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7555/*
7556 * Create a pseudo-"tearoff menu" based on the child
7557 * items of a given menu pointer.
7558 */
7559 static void
7560gui_mch_tearoff(
7561 char_u *title,
7562 vimmenu_T *menu,
7563 int initX,
7564 int initY)
7565{
7566 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7567 int template_len;
7568 int nchar, textWidth, submenuWidth;
7569 DWORD lStyle;
7570 DWORD lExtendedStyle;
7571 WORD dlgwidth;
7572 WORD menuID;
7573 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007574 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 vimmenu_T *the_menu = menu;
7576 HWND hwnd;
7577 HDC hdc;
7578 HFONT font, oldFont;
7579 int col, spaceWidth, len;
7580 int columnWidths[2];
7581 char_u *label, *text;
7582 int acLen = 0;
7583 int nameLen;
7584 int padding0, padding1, padding2 = 0;
7585 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007586 int x;
7587 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007588# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007589 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
7593 /*
7594 * If this menu is already torn off, move it to the mouse position.
7595 */
7596 if (IsWindow(menu->tearoff_handle))
7597 {
7598 POINT mp;
7599 if (GetCursorPos((LPPOINT)&mp))
7600 {
7601 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7602 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7603 }
7604 return;
7605 }
7606
7607 /*
7608 * Create a new tearoff.
7609 */
7610 if (*title == MNU_HIDDEN_CHAR)
7611 title++;
7612
Bram Moolenaar734a8672019-12-02 22:49:38 +01007613 // Allocate memory to store the dialog template. It's made bigger when
7614 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 template_len = DLG_ALLOC_SIZE;
7616 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7617 if (p == NULL)
7618 return;
7619
7620 hwnd = GetDesktopWindow();
7621 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007622# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7624 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007625 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 use_lfSysmenu = TRUE;
7627 }
7628 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7631 VARIABLE_PITCH , DLG_FONT_NAME);
7632 if (s_usenewlook)
7633 oldFont = SelectFont(hdc, font);
7634 else
7635 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7636
Bram Moolenaar734a8672019-12-02 22:49:38 +01007637 // Calculate width of a single space. Used for padding columns to the
7638 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007639 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640
Bram Moolenaar734a8672019-12-02 22:49:38 +01007641 // Figure out max width of the text column, the accelerator column and the
7642 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 submenuWidth = 0;
7644 for (col = 0; col < 2; col++)
7645 {
7646 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007647 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007649 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 text = (col == 0) ? pmenu->dname : pmenu->actext;
7651 if (text != NULL && *text != NUL)
7652 {
7653 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7654 if (textWidth > columnWidths[col])
7655 columnWidths[col] = textWidth;
7656 }
7657 if (pmenu->children != NULL)
7658 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7659 }
7660 }
7661 if (columnWidths[1] == 0)
7662 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007663 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 if (submenuWidth != 0)
7665 columnWidths[0] += submenuWidth;
7666 else
7667 columnWidths[0] += spaceWidth;
7668 }
7669 else
7670 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007671 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7673 columnWidths[1] += submenuWidth;
7674 }
7675
7676 /*
7677 * Now find the total width of our 'menu'.
7678 */
7679 textWidth = columnWidths[0] + columnWidths[1];
7680 if (submenuWidth != 0)
7681 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007682 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7684 textWidth += submenuWidth;
7685 }
7686 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7687 if (textWidth > dlgwidth)
7688 dlgwidth = textWidth;
7689 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7690
Bram Moolenaar734a8672019-12-02 22:49:38 +01007691 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 if (s_usenewlook)
7693 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7694 else
7695 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7696
7697 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7698 *p++ = LOWORD(lStyle);
7699 *p++ = HIWORD(lStyle);
7700 *p++ = LOWORD(lExtendedStyle);
7701 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007702 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007704 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007706 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 else
7708 *p++ = PixelToDialogX(initX); // x
7709 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007710 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
7712 *p++ = PixelToDialogY(initY); // y
7713 *p++ = PixelToDialogX(dlgwidth); // cx
7714 ptrueheight = p;
7715 *p++ = 0; // dialog height: changed later anyway
7716 *p++ = 0; // Menu
7717 *p++ = 0; // Class
7718
Bram Moolenaar734a8672019-12-02 22:49:38 +01007719 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007721 ? (LPSTR)title
7722 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 p += nchar;
7724
7725 if (s_usenewlook)
7726 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007727 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007728# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 if (use_lfSysmenu)
7730 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007731 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7733 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007734 wcscpy(p, lfSysmenu.lfFaceName);
7735 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
7737 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
7740 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007741 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743 p += nchar;
7744 }
7745
7746 /*
7747 * Loop over all the items in the menu.
7748 * But skip over the tearbar.
7749 */
7750 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7751 menu = menu->children->next;
7752 else
7753 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007754 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 for ( ; menu != NULL; menu = menu->next)
7756 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007757 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 continue;
7759 if (menu_is_separator(menu->dname))
7760 {
7761 sepPadding += 3;
7762 continue;
7763 }
7764
Bram Moolenaar734a8672019-12-02 22:49:38 +01007765 // Check if there still is plenty of room in the template. Make it
7766 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7768 {
7769 WORD *newp;
7770
7771 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7772 if (newp != NULL)
7773 {
7774 template_len += 4096;
7775 mch_memmove(newp, pdlgtemplate,
7776 (char *)p - (char *)pdlgtemplate);
7777 p = newp + (p - pdlgtemplate);
7778 pnumitems = newp + (pnumitems - pdlgtemplate);
7779 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7780 LocalFree(LocalHandle(pdlgtemplate));
7781 pdlgtemplate = newp;
7782 }
7783 }
7784
Bram Moolenaar734a8672019-12-02 22:49:38 +01007785 // Figure out minimal length of this menu label. Use "name" for the
7786 // actual text, "dname" for estimating the displayed size. "name"
7787 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 len = nameLen = (int)STRLEN(menu->name);
7789 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7790 (int)STRLEN(menu->dname))) / spaceWidth;
7791 len += padding0;
7792
7793 if (menu->actext != NULL)
7794 {
7795 acLen = (int)STRLEN(menu->actext);
7796 len += acLen;
7797 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7798 }
7799 else
7800 textWidth = 0;
7801 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7802 len += padding1;
7803
7804 if (menu->children == NULL)
7805 {
7806 padding2 = submenuWidth / spaceWidth;
7807 len += padding2;
7808 menuID = (WORD)(menu->id);
7809 }
7810 else
7811 {
7812 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007813 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815
Bram Moolenaar734a8672019-12-02 22:49:38 +01007816 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007817 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 if (label == NULL)
7819 break;
7820
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007821 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007822 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007824 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 while (padding0-- > 0)
7826 *text++ = ' ';
7827 if (menu->actext != NULL)
7828 {
7829 STRNCPY(text, menu->actext, acLen);
7830 text += acLen;
7831 }
7832 while (padding1-- > 0)
7833 *text++ = ' ';
7834 if (menu->children != NULL)
7835 {
7836 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7837 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7838 }
7839 else
7840 {
7841 while (padding2-- > 0)
7842 *text++ = ' ';
7843 }
7844 *text = NUL;
7845
7846 /*
7847 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7848 * W95/NT4 it makes the tear-off look more like a menu.
7849 */
7850 p = add_dialog_element(p,
7851 BS_PUSHBUTTON|BS_LEFT,
7852 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7853 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7854 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7855 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007856 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 vim_free(label);
7858 (*pnumitems)++;
7859 }
7860
7861 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7862
7863
Bram Moolenaar734a8672019-12-02 22:49:38 +01007864 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007865 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007866 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 (LPDLGTEMPLATE)pdlgtemplate,
7868 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007869 (DLGPROC)tearoff_callback,
7870 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871
7872 LocalFree(LocalHandle(pdlgtemplate));
7873 SelectFont(hdc, oldFont);
7874 DeleteObject(font);
7875 ReleaseDC(hwnd, hdc);
7876
7877 /*
7878 * Reassert ourselves as the active window. This is so that after creating
7879 * a tearoff, the user doesn't have to click with the mouse just to start
7880 * typing again!
7881 */
7882 (void)SetActiveWindow(s_hwnd);
7883
Bram Moolenaar734a8672019-12-02 22:49:38 +01007884 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 force_menu_update = TRUE;
7886}
7887#endif
7888
7889#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007890# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaar734a8672019-12-02 22:49:38 +01007892// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893# ifndef TBSTYLE_FLAT
7894# define TBSTYLE_FLAT 0x0800
7895# endif
7896
7897/*
7898 * Create the toolbar, initially unpopulated.
7899 * (just like the menu, there are no defaults, it's all
7900 * set up through menu.vim)
7901 */
7902 static void
7903initialise_toolbar(void)
7904{
7905 InitCommonControls();
7906 s_toolbarhwnd = CreateToolbarEx(
7907 s_hwnd,
7908 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7909 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007910 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007911 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 IDR_TOOLBAR1, // id of initial bitmap
7913 NULL,
7914 0, // initial number of buttons
7915 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7916 TOOLBAR_BUTTON_HEIGHT,
7917 TOOLBAR_BUTTON_WIDTH,
7918 TOOLBAR_BUTTON_HEIGHT,
7919 sizeof(TBBUTTON)
7920 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007921
7922 // Remove transparency from the toolbar to prevent the main window
7923 // background colour showing through
7924 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7925 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7926
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007927 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7930}
7931
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007932 static LRESULT CALLBACK
7933toolbar_wndproc(
7934 HWND hwnd,
7935 UINT uMsg,
7936 WPARAM wParam,
7937 LPARAM lParam)
7938{
7939 HandleMouseHide(uMsg, lParam);
7940 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7941}
7942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 static int
7944get_toolbar_bitmap(vimmenu_T *menu)
7945{
7946 int i = -1;
7947
7948 /*
7949 * Check user bitmaps first, unless builtin is specified.
7950 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007951 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {
7953 char_u fname[MAXPATHL];
7954 HANDLE hbitmap = NULL;
7955
7956 if (menu->iconfile != NULL)
7957 {
7958 gui_find_iconfile(menu->iconfile, fname, "bmp");
7959 hbitmap = LoadImage(
7960 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007961 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 IMAGE_BITMAP,
7963 TOOLBAR_BUTTON_WIDTH,
7964 TOOLBAR_BUTTON_HEIGHT,
7965 LR_LOADFROMFILE |
7966 LR_LOADMAP3DCOLORS
7967 );
7968 }
7969
7970 /*
7971 * If the LoadImage call failed, or the "icon=" file
7972 * didn't exist or wasn't specified, try the menu name
7973 */
7974 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007975 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007976# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007977 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007978# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007979 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 hbitmap = LoadImage(
7981 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007982 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 IMAGE_BITMAP,
7984 TOOLBAR_BUTTON_WIDTH,
7985 TOOLBAR_BUTTON_HEIGHT,
7986 LR_LOADFROMFILE |
7987 LR_LOADMAP3DCOLORS
7988 );
7989
7990 if (hbitmap != NULL)
7991 {
7992 TBADDBITMAP tbAddBitmap;
7993
7994 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007995 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7998 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007999 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 }
8001 }
8002 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8003 i = menu->iconidx;
8004
8005 return i;
8006}
8007#endif
8008
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008009#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8010 static void
8011initialise_tabline(void)
8012{
8013 InitCommonControls();
8014
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008015 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008016 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008017 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008018 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008019 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008020
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008021 gui.tabline_height = TABLINE_HEIGHT;
8022
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008023# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008024 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008025# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008026}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008027
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008028/*
8029 * Get tabpage_T from POINT.
8030 */
8031 static tabpage_T *
8032GetTabFromPoint(
8033 HWND hWnd,
8034 POINT pt)
8035{
8036 tabpage_T *ptp = NULL;
8037
8038 if (gui_mch_showing_tabline())
8039 {
8040 TCHITTESTINFO htinfo;
8041 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008042 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008043 if (s_tabhwnd == hWnd)
8044 {
8045 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8046 if (idx != -1)
8047 ptp = find_tabpage(idx + 1);
8048 }
8049 }
8050 return ptp;
8051}
8052
8053static POINT s_pt = {0, 0};
8054static HCURSOR s_hCursor = NULL;
8055
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008056 static LRESULT CALLBACK
8057tabline_wndproc(
8058 HWND hwnd,
8059 UINT uMsg,
8060 WPARAM wParam,
8061 LPARAM lParam)
8062{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008063 POINT pt;
8064 tabpage_T *tp;
8065 RECT rect;
8066 int nCenter;
8067 int idx0;
8068 int idx1;
8069
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008070 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008071
8072 switch (uMsg)
8073 {
8074 case WM_LBUTTONDOWN:
8075 {
8076 s_pt.x = GET_X_LPARAM(lParam);
8077 s_pt.y = GET_Y_LPARAM(lParam);
8078 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008079 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008080 break;
8081 }
8082 case WM_MOUSEMOVE:
8083 if (GetCapture() == hwnd
8084 && ((wParam & MK_LBUTTON)) != 0)
8085 {
8086 pt.x = GET_X_LPARAM(lParam);
8087 pt.y = s_pt.y;
8088 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8089 {
8090 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8091
8092 tp = GetTabFromPoint(hwnd, pt);
8093 if (tp != NULL)
8094 {
8095 idx0 = tabpage_index(curtab) - 1;
8096 idx1 = tabpage_index(tp) - 1;
8097
8098 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8099 nCenter = rect.left + (rect.right - rect.left) / 2;
8100
Bram Moolenaar734a8672019-12-02 22:49:38 +01008101 // Check if the mouse cursor goes over the center of
8102 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008103 if ((idx0 < idx1) && (nCenter < pt.x))
8104 {
8105 tabpage_move(idx1 + 1);
8106 update_screen(0);
8107 }
8108 else if ((idx1 < idx0) && (pt.x < nCenter))
8109 {
8110 tabpage_move(idx1);
8111 update_screen(0);
8112 }
8113 }
8114 }
8115 }
8116 break;
8117 case WM_LBUTTONUP:
8118 {
8119 if (GetCapture() == hwnd)
8120 {
8121 SetCursor(s_hCursor);
8122 ReleaseCapture();
8123 }
8124 break;
8125 }
8126 default:
8127 break;
8128 }
8129
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008130 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8131}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008132#endif
8133
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8135/*
8136 * Make the GUI window come to the foreground.
8137 */
8138 void
8139gui_mch_set_foreground(void)
8140{
8141 if (IsIconic(s_hwnd))
8142 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8143 SetForegroundWindow(s_hwnd);
8144}
8145#endif
8146
8147#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8148 static void
8149dyn_imm_load(void)
8150{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008151 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 if (hLibImm == NULL)
8153 return;
8154
8155 pImmGetCompositionStringA
8156 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8157 pImmGetCompositionStringW
8158 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8159 pImmGetContext
8160 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8161 pImmAssociateContext
8162 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8163 pImmReleaseContext
8164 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8165 pImmGetOpenStatus
8166 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8167 pImmSetOpenStatus
8168 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008169 pImmGetCompositionFontW
8170 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8171 pImmSetCompositionFontW
8172 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 pImmSetCompositionWindow
8174 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8175 pImmGetConversionStatus
8176 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008177 pImmSetConversionStatus
8178 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 if ( pImmGetCompositionStringA == NULL
8181 || pImmGetCompositionStringW == NULL
8182 || pImmGetContext == NULL
8183 || pImmAssociateContext == NULL
8184 || pImmReleaseContext == NULL
8185 || pImmGetOpenStatus == NULL
8186 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008187 || pImmGetCompositionFontW == NULL
8188 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008190 || pImmGetConversionStatus == NULL
8191 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 FreeLibrary(hLibImm);
8194 hLibImm = NULL;
8195 pImmGetContext = NULL;
8196 return;
8197 }
8198
8199 return;
8200}
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202#endif
8203
8204#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8205
8206# ifdef FEAT_XPM_W32
8207# define IMAGE_XPM 100
8208# endif
8209
8210typedef struct _signicon_t
8211{
8212 HANDLE hImage;
8213 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008214# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008215 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217} signicon_t;
8218
8219 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008220gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 signicon_t *sign;
8223 int x, y, w, h;
8224
8225 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8226 return;
8227
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008228# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008229 if (IS_ENABLE_DIRECTX())
8230 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008231# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 x = TEXT_X(col);
8234 y = TEXT_Y(row);
8235 w = gui.char_width * 2;
8236 h = gui.char_height;
8237 switch (sign->uType)
8238 {
8239 case IMAGE_BITMAP:
8240 {
8241 HDC hdcMem;
8242 HBITMAP hbmpOld;
8243
8244 hdcMem = CreateCompatibleDC(s_hdc);
8245 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8246 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8247 SelectObject(hdcMem, hbmpOld);
8248 DeleteDC(hdcMem);
8249 }
8250 break;
8251 case IMAGE_ICON:
8252 case IMAGE_CURSOR:
8253 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8254 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008255# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 case IMAGE_XPM:
8257 {
8258 HDC hdcMem;
8259 HBITMAP hbmpOld;
8260
8261 hdcMem = CreateCompatibleDC(s_hdc);
8262 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008263 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8265
8266 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008267 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8269 SelectObject(hdcMem, hbmpOld);
8270 DeleteDC(hdcMem);
8271 }
8272 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
8275}
8276
8277 static void
8278close_signicon_image(signicon_t *sign)
8279{
8280 if (sign)
8281 switch (sign->uType)
8282 {
8283 case IMAGE_BITMAP:
8284 DeleteObject((HGDIOBJ)sign->hImage);
8285 break;
8286 case IMAGE_CURSOR:
8287 DestroyCursor((HCURSOR)sign->hImage);
8288 break;
8289 case IMAGE_ICON:
8290 DestroyIcon((HICON)sign->hImage);
8291 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008292# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 case IMAGE_XPM:
8294 DeleteObject((HBITMAP)sign->hImage);
8295 DeleteObject((HBITMAP)sign->hShape);
8296 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
8299}
8300
8301 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008302gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 signicon_t sign, *psign;
8305 char_u *ext;
8306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008308 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (ext > signfile)
8310 {
8311 int do_load = 1;
8312
8313 if (!STRICMP(ext, ".bmp"))
8314 sign.uType = IMAGE_BITMAP;
8315 else if (!STRICMP(ext, ".ico"))
8316 sign.uType = IMAGE_ICON;
8317 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8318 sign.uType = IMAGE_CURSOR;
8319 else
8320 do_load = 0;
8321
8322 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008323 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 gui.char_width * 2, gui.char_height,
8325 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008326# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 if (!STRICMP(ext, ".xpm"))
8328 {
8329 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008330 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8331 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
8335
8336 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008337 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 *psign = sign;
8339
8340 if (!psign)
8341 {
8342 if (sign.hImage)
8343 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008344 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 }
8346 return (void *)psign;
8347
8348}
8349
8350 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008351gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 if (sign)
8354 {
8355 close_signicon_image((signicon_t *)sign);
8356 vim_free(sign);
8357 }
8358}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008361#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
Bram Moolenaar734a8672019-12-02 22:49:38 +01008363/*
8364 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008365 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008367 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8369 * to get current mouse position).
8370 *
8371 * Trying to use as more Windows services as possible, and as less
8372 * IE version as possible :)).
8373 *
8374 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8375 * BalloonEval struct.
8376 * 2) Enable/Disable simply create/kill BalloonEval Timer
8377 * 3) When there was enough inactivity, timer procedure posts
8378 * async request to debugger
8379 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8380 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008381 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 */
8383
Bram Moolenaar45360022005-07-21 21:08:21 +00008384/*
8385 * determine whether installed Common Controls support multiline tooltips
8386 * (i.e. their version is >= 4.70
8387 */
8388 int
8389multiline_balloon_available(void)
8390{
8391 HINSTANCE hDll;
8392 static char comctl_dll[] = "comctl32.dll";
8393 static int multiline_tip = MAYBE;
8394
8395 if (multiline_tip != MAYBE)
8396 return multiline_tip;
8397
8398 hDll = GetModuleHandle(comctl_dll);
8399 if (hDll != NULL)
8400 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008401 DLLGETVERSIONPROC pGetVer;
8402 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008403
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008404 if (pGetVer != NULL)
8405 {
8406 DLLVERSIONINFO dvi;
8407 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008408
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008409 ZeroMemory(&dvi, sizeof(dvi));
8410 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008411
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008412 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008413
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008414 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008415 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008416 || (dvi.dwMajorVersion == 4
8417 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008418 {
8419 multiline_tip = TRUE;
8420 return multiline_tip;
8421 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008422 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008423 else
8424 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008425 // there is chance we have ancient CommCtl 4.70
8426 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008427 DWORD dwHandle = 0;
8428 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8429 if (len > 0)
8430 {
8431 VS_FIXEDFILEINFO *ver;
8432 UINT vlen = 0;
8433 void *data = alloc(len);
8434
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008435 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008436 && GetFileVersionInfo(comctl_dll, 0, len, data)
8437 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8438 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008439 && HIWORD(ver->dwFileVersionMS) > 4)
8440 || ((HIWORD(ver->dwFileVersionMS) == 4
8441 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008442 {
8443 vim_free(data);
8444 multiline_tip = TRUE;
8445 return multiline_tip;
8446 }
8447 vim_free(data);
8448 }
8449 }
8450 }
8451 multiline_tip = FALSE;
8452 return multiline_tip;
8453}
8454
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008455 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008456make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008457{
8458 TOOLINFOW *pti;
8459 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008460
8461 if (multiline_balloon_available() == TRUE)
8462 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8463 else
8464 ToolInfoSize = sizeof(TOOLINFOW);
8465
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008466 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008467 if (pti == NULL)
8468 return;
8469
8470 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8471 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8472 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008473 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008474
8475 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8476 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8477
8478 pti->cbSize = ToolInfoSize;
8479 pti->uFlags = TTF_SUBCLASS;
8480 pti->hwnd = beval->target;
8481 pti->hinst = 0; // Don't use string resources
8482 pti->uId = ID_BEVAL_TOOLTIP;
8483
8484 if (multiline_balloon_available() == TRUE)
8485 {
8486 RECT rect;
8487 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8488 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008489 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8490 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008491 // switch multiline tooltips on
8492 if (GetClientRect(s_textArea, &rect))
8493 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8494 (LPARAM)rect.right);
8495 }
8496 else
8497 {
8498 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008499 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8500 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008501 }
8502
8503 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8504 pti->rect.left = pt.x - 3;
8505 pti->rect.top = pt.y - 3;
8506 pti->rect.right = pt.x + 3;
8507 pti->rect.bottom = pt.y + 3;
8508
8509 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8510 // Make tooltip appear sooner.
8511 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8512 // I've performed some tests and it seems the longest possible life time
8513 // of tooltip is 30 seconds.
8514 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8515 /*
8516 * HACK: force tooltip to appear, because it'll not appear until
8517 * first mouse move. D*mn M$
8518 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8519 */
8520 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8521 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8522 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008523}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008526delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008528 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008532BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008533 HWND hwnd UNUSED,
8534 UINT uMsg UNUSED,
8535 UINT_PTR idEvent UNUSED,
8536 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 POINT pt;
8539 RECT rect;
8540
8541 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8542 return;
8543
8544 GetCursorPos(&pt);
8545 if (WindowFromPoint(pt) != s_textArea)
8546 return;
8547
8548 ScreenToClient(s_textArea, &pt);
8549 GetClientRect(s_textArea, &rect);
8550 if (!PtInRect(&rect, pt))
8551 return;
8552
8553 if (LastActivity > 0
8554 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8555 && (cur_beval->showState != ShS_PENDING
8556 || abs(cur_beval->x - pt.x) > 3
8557 || abs(cur_beval->y - pt.y) > 3))
8558 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008559 // Pointer resting in one place long enough, it's time to show
8560 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 cur_beval->showState = ShS_PENDING;
8562 cur_beval->x = pt.x;
8563 cur_beval->y = pt.y;
8564
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008565 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 if (cur_beval->msgCB != NULL)
8568 (*cur_beval->msgCB)(cur_beval, 0);
8569 }
8570}
8571
8572 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008573gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008575 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008577 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579
8580 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008581gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008583 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (beval == NULL)
8585 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008586 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008587 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008588 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
8591 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008592gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008595
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008596 vim_free(beval->msg);
8597 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8598 if (beval->msg == NULL)
8599 {
8600 delete_tooltip(beval);
8601 beval->showState = ShS_NEUTRAL;
8602 return;
8603 }
8604
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008605 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (beval->showState == ShS_SHOWING)
8607 return;
8608 GetCursorPos(&pt);
8609 ScreenToClient(s_textArea, &pt);
8610
8611 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008613 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 gui_mch_disable_beval_area(cur_beval);
8615 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008616 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008618 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619}
8620
8621 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008622gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008623 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008624 char_u *mesg,
8625 void (*mesgCB)(BalloonEval *, int),
8626 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008628 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 BalloonEval *beval;
8630
8631 if (mesg != NULL && mesgCB != NULL)
8632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008633 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 return NULL;
8635 }
8636
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008637 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (beval != NULL)
8639 {
8640 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 beval->msg = mesg;
8644 beval->msgCB = mesgCB;
8645 beval->clientData = clientData;
8646
8647 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 cur_beval = beval;
8649
8650 if (p_beval)
8651 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653 return beval;
8654}
8655
8656 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008657Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008659 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 return;
8661
8662 if (cur_beval != NULL)
8663 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008664 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008666 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008667 // TRACE0("TTN_SHOW {{{");
8668 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008669 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008670 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008671 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 delete_tooltip(cur_beval);
8673 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008674 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
8676 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008677 break;
8678 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008679 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008680 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008681 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8682 info->lpszText = (LPSTR)info->lParam;
8683 info->uFlags |= TTF_DI_SETITEM;
8684 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008685 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008686 case TTN_GETDISPINFOW:
8687 {
8688 // if we get here then we have new common controls
8689 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8690 info->lpszText = (LPWSTR)info->lParam;
8691 info->uFlags |= TTF_DI_SETITEM;
8692 }
8693 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 }
8695 }
8696}
8697
8698 static void
8699TrackUserActivity(UINT uMsg)
8700{
8701 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8702 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8703 LastActivity = GetTickCount();
8704}
8705
8706 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008707gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008709# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008710 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008711# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008712 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 vim_free(beval);
8714}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008715#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8718/*
8719 * We have multiple signs to draw at the same location. Draw the
8720 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8721 */
8722 void
8723netbeans_draw_multisign_indicator(int row)
8724{
8725 int i;
8726 int y;
8727 int x;
8728
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008729 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008730 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 x = 0;
8733 y = TEXT_Y(row);
8734
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008735# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008736 if (IS_ENABLE_DIRECTX())
8737 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008738# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 for (i = 0; i < gui.char_height - 3; i++)
8741 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8742
8743 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8744 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8745 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8746 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8747 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8748 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8749 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8750}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008751#endif