blob: 80a70e25ce3cbb647e387d19a255e6509b516fe2 [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()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002137 // TODO: The 10 msec is a compromise between laggy response
2138 // and consuming more CPU time. Better would be to handle
2139 // channel messages when they arrive.
2140 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002141 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002142 break;
2143 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002144#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002145 // Don't use gui_mch_update() because then we will spin-lock until a
2146 // char arrives, instead we use GetMessage() to hang until an
2147 // event arrives. No need to check for input_buf_full because we are
2148 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002150#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002151
2152 if (input_available())
2153 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002154 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155 allow_scrollbar = FALSE;
2156
Bram Moolenaar89c00032019-09-04 13:53:21 +02002157 // Clear pending mouse button, the release event may have been
2158 // taken by the dialog window. But don't do this when getting
2159 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002160 if (!s_getting_focus)
2161 s_button_pending = -1;
2162
2163 return OK;
2164 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002165
2166#ifdef FEAT_TIMERS
2167 if (did_add_timer)
2168 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002169 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002170 remove_any_timer();
2171 break;
2172 }
2173#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002174 }
2175 allow_scrollbar = FALSE;
2176 return FAIL;
2177}
2178
2179/*
2180 * Clear a rectangular region of the screen from text pos (row1, col1) to
2181 * (row2, col2) inclusive.
2182 */
2183 void
2184gui_mch_clear_block(
2185 int row1,
2186 int col1,
2187 int row2,
2188 int col2)
2189{
2190 RECT rc;
2191
2192 /*
2193 * Clear one extra pixel at the far right, for when bold characters have
2194 * spilled over to the window border.
2195 * Note: FillRect() excludes right and bottom of rectangle.
2196 */
2197 rc.left = FILL_X(col1);
2198 rc.top = FILL_Y(row1);
2199 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2200 rc.bottom = FILL_Y(row2 + 1);
2201 clear_rect(&rc);
2202}
2203
2204/*
2205 * Clear the whole text window.
2206 */
2207 void
2208gui_mch_clear_all(void)
2209{
2210 RECT rc;
2211
2212 rc.left = 0;
2213 rc.top = 0;
2214 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2215 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2216 clear_rect(&rc);
2217}
2218/*
2219 * Menu stuff.
2220 */
2221
2222 void
2223gui_mch_enable_menu(int flag)
2224{
2225#ifdef FEAT_MENU
2226 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2227#endif
2228}
2229
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230 void
2231gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002232 int x UNUSED,
2233 int y UNUSED,
2234 int w UNUSED,
2235 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002236{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002237 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002238}
2239
2240#if defined(FEAT_MENU) || defined(PROTO)
2241/*
2242 * Make menu item hidden or not hidden
2243 */
2244 void
2245gui_mch_menu_hidden(
2246 vimmenu_T *menu,
2247 int hidden)
2248{
2249 /*
2250 * This doesn't do what we want. Hmm, just grey the menu items for now.
2251 */
2252 /*
2253 if (hidden)
2254 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2255 else
2256 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2257 */
2258 gui_mch_menu_grey(menu, hidden);
2259}
2260
2261/*
2262 * This is called after setting all the menus to grey/hidden or not.
2263 */
2264 void
2265gui_mch_draw_menubar(void)
2266{
2267 DrawMenuBar(s_hwnd);
2268}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002269#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002270
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271/*
2272 * Return the RGB value of a pixel as a long.
2273 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002274 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275gui_mch_get_rgb(guicolor_T pixel)
2276{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002277 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2278 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002279}
2280
2281#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002282/*
2283 * Convert pixels in X to dialog units
2284 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002285 static WORD
2286PixelToDialogX(int numPixels)
2287{
2288 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2289}
2290
Bram Moolenaar734a8672019-12-02 22:49:38 +01002291/*
2292 * Convert pixels in Y to dialog units
2293 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002294 static WORD
2295PixelToDialogY(int numPixels)
2296{
2297 return (WORD)((numPixels * 8) / s_dlgfntheight);
2298}
2299
Bram Moolenaar734a8672019-12-02 22:49:38 +01002300/*
2301 * Return the width in pixels of the given text in the given DC.
2302 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 static int
2304GetTextWidth(HDC hdc, char_u *str, int len)
2305{
2306 SIZE size;
2307
2308 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2309 return size.cx;
2310}
2311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312/*
2313 * Return the width in pixels of the given text in the given DC, taking care
2314 * of 'encoding' to active codepage conversion.
2315 */
2316 static int
2317GetTextWidthEnc(HDC hdc, char_u *str, int len)
2318{
2319 SIZE size;
2320 WCHAR *wstr;
2321 int n;
2322 int wlen = len;
2323
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002324 wstr = enc_to_utf16(str, &wlen);
2325 if (wstr == NULL)
2326 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002327
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002328 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2329 vim_free(wstr);
2330 if (n)
2331 return size.cx;
2332 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002333}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002334
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002335static void get_work_area(RECT *spi_rect);
2336
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337/*
2338 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002339 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2340 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341 */
2342 static BOOL
2343CenterWindow(
2344 HWND hwndChild,
2345 HWND hwndParent)
2346{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002347 HMONITOR mon;
2348 MONITORINFO moninfo;
2349 RECT rChild, rParent, rScreen;
2350 int wChild, hChild, wParent, hParent;
2351 int xNew, yNew;
2352 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002353
2354 GetWindowRect(hwndChild, &rChild);
2355 wChild = rChild.right - rChild.left;
2356 hChild = rChild.bottom - rChild.top;
2357
Bram Moolenaar734a8672019-12-02 22:49:38 +01002358 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002360 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361 else
2362 GetWindowRect(hwndParent, &rParent);
2363 wParent = rParent.right - rParent.left;
2364 hParent = rParent.bottom - rParent.top;
2365
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002366 moninfo.cbSize = sizeof(MONITORINFO);
2367 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2368 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002372 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002373 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 hdc = GetDC(hwndChild);
2375 rScreen.left = 0;
2376 rScreen.top = 0;
2377 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2378 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2379 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002380 }
2381
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002382 xNew = rParent.left + ((wParent - wChild) / 2);
2383 if (xNew < rScreen.left)
2384 xNew = rScreen.left;
2385 else if ((xNew + wChild) > rScreen.right)
2386 xNew = rScreen.right - wChild;
2387
2388 yNew = rParent.top + ((hParent - hChild) / 2);
2389 if (yNew < rScreen.top)
2390 yNew = rScreen.top;
2391 else if ((yNew + hChild) > rScreen.bottom)
2392 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393
2394 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2395 SWP_NOSIZE | SWP_NOZORDER);
2396}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002397#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399#if defined(FEAT_TOOLBAR) || defined(PROTO)
2400 void
2401gui_mch_show_toolbar(int showit)
2402{
2403 if (s_toolbarhwnd == NULL)
2404 return;
2405
2406 if (showit)
2407 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002408# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002409 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002410# define TB_SETUNICODEFORMAT 0x2005
2411# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002412 // Enable unicode support
2413 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2414 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 ShowWindow(s_toolbarhwnd, SW_SHOW);
2416 }
2417 else
2418 ShowWindow(s_toolbarhwnd, SW_HIDE);
2419}
2420
Bram Moolenaar734a8672019-12-02 22:49:38 +01002421// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002422# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423
2424#endif
2425
2426#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2427 static void
2428add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2429{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002430 WCHAR *wn;
2431 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002433 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002435 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002437 infow.cbSize = sizeof(infow);
2438 infow.fMask = MIIM_TYPE | MIIM_ID;
2439 infow.wID = item_id;
2440 infow.fType = MFT_STRING;
2441 infow.dwTypeData = wn;
2442 infow.cch = (UINT)wcslen(wn);
2443 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2444 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445}
2446
2447 static void
2448show_tabline_popup_menu(void)
2449{
2450 HMENU tab_pmenu;
2451 long rval;
2452 POINT pt;
2453
Bram Moolenaar734a8672019-12-02 22:49:38 +01002454 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 if (hold_gui_events
2456# ifdef FEAT_CMDWIN
2457 || cmdwin_type != 0
2458# endif
2459 )
2460 return;
2461
2462 tab_pmenu = CreatePopupMenu();
2463 if (tab_pmenu == NULL)
2464 return;
2465
2466 if (first_tabpage->tp_next != NULL)
2467 add_tabline_popup_menu_entry(tab_pmenu,
2468 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2469 add_tabline_popup_menu_entry(tab_pmenu,
2470 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2471 add_tabline_popup_menu_entry(tab_pmenu,
2472 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2473
2474 GetCursorPos(&pt);
2475 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2476 NULL);
2477
2478 DestroyMenu(tab_pmenu);
2479
Bram Moolenaar734a8672019-12-02 22:49:38 +01002480 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002481 if (rval > 0)
2482 {
2483 TCHITTESTINFO htinfo;
2484 int idx;
2485
2486 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2487 return;
2488
2489 htinfo.pt.x = pt.x;
2490 htinfo.pt.y = pt.y;
2491 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2492 if (idx == -1)
2493 idx = 0;
2494 else
2495 idx += 1;
2496
2497 send_tabline_menu_event(idx, (int)rval);
2498 }
2499}
2500
2501/*
2502 * Show or hide the tabline.
2503 */
2504 void
2505gui_mch_show_tabline(int showit)
2506{
2507 if (s_tabhwnd == NULL)
2508 return;
2509
2510 if (!showit != !showing_tabline)
2511 {
2512 if (showit)
2513 ShowWindow(s_tabhwnd, SW_SHOW);
2514 else
2515 ShowWindow(s_tabhwnd, SW_HIDE);
2516 showing_tabline = showit;
2517 }
2518}
2519
2520/*
2521 * Return TRUE when tabline is displayed.
2522 */
2523 int
2524gui_mch_showing_tabline(void)
2525{
2526 return s_tabhwnd != NULL && showing_tabline;
2527}
2528
2529/*
2530 * Update the labels of the tabline.
2531 */
2532 void
2533gui_mch_update_tabline(void)
2534{
2535 tabpage_T *tp;
2536 TCITEM tie;
2537 int nr = 0;
2538 int curtabidx = 0;
2539 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002540 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541
2542 if (s_tabhwnd == NULL)
2543 return;
2544
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002545# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002546 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002547# define CCM_SETUNICODEFORMAT 0x2005
2548# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002549 // Enable unicode support
2550 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002551
2552 tie.mask = TCIF_TEXT;
2553 tie.iImage = -1;
2554
Bram Moolenaar734a8672019-12-02 22:49:38 +01002555 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002556 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2557
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2560 {
2561 if (tp == curtab)
2562 curtabidx = nr;
2563
2564 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2565 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002566 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002567 tie.pszText = "-Empty-";
2568 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2569 tabadded = 1;
2570 }
2571
2572 get_tabline_label(tp, FALSE);
2573 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002574
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002575 wstr = enc_to_utf16(NameBuff, NULL);
2576 if (wstr != NULL)
2577 {
2578 TCITEMW tiw;
2579
2580 tiw.mask = TCIF_TEXT;
2581 tiw.iImage = -1;
2582 tiw.pszText = wstr;
2583 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2584 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002585 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586 }
2587
Bram Moolenaar734a8672019-12-02 22:49:38 +01002588 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002589 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2590 TabCtrl_DeleteItem(s_tabhwnd, nr);
2591
2592 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2593 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2594
Bram Moolenaar734a8672019-12-02 22:49:38 +01002595 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002596 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2597 RedrawWindow(s_tabhwnd, NULL, NULL,
2598 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2599
2600 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2601 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2602}
2603
2604/*
2605 * Set the current tab to "nr". First tab is 1.
2606 */
2607 void
2608gui_mch_set_curtab(int nr)
2609{
2610 if (s_tabhwnd == NULL)
2611 return;
2612
2613 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2614 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2615}
2616
2617#endif
2618
2619/*
2620 * ":simalt" command.
2621 */
2622 void
2623ex_simalt(exarg_T *eap)
2624{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002625 char_u *keys = eap->arg;
2626 int fill_typebuf = FALSE;
2627 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628
2629 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2630 while (*keys)
2631 {
2632 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002633 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002634 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2635 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002636 fill_typebuf = TRUE;
2637 }
2638 if (fill_typebuf)
2639 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002640 // Put a NOP in the typeahead buffer so that the message will get
2641 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002642 key_name[0] = K_SPECIAL;
2643 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002644 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002645 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002646#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002647 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002648#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002649 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002650 }
2651}
2652
2653/*
2654 * Create the find & replace dialogs.
2655 * You can't have both at once: ":find" when replace is showing, destroys
2656 * the replace dialog first, and the other way around.
2657 */
2658#ifdef MSWIN_FIND_REPLACE
2659 static void
2660initialise_findrep(char_u *initial_string)
2661{
2662 int wword = FALSE;
2663 int mcase = !p_ic;
2664 char_u *entry_text;
2665
Bram Moolenaar734a8672019-12-02 22:49:38 +01002666 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002667 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2668
2669 s_findrep_struct.hwndOwner = s_hwnd;
2670 s_findrep_struct.Flags = FR_DOWN;
2671 if (mcase)
2672 s_findrep_struct.Flags |= FR_MATCHCASE;
2673 if (wword)
2674 s_findrep_struct.Flags |= FR_WHOLEWORD;
2675 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002676 {
2677 WCHAR *p = enc_to_utf16(entry_text, NULL);
2678 if (p != NULL)
2679 {
2680 int len = s_findrep_struct.wFindWhatLen - 1;
2681
2682 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2683 s_findrep_struct.lpstrFindWhat[len] = NUL;
2684 vim_free(p);
2685 }
2686 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002687 vim_free(entry_text);
2688}
2689#endif
2690
2691 static void
2692set_window_title(HWND hwnd, char *title)
2693{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002694 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002695 {
2696 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697
Bram Moolenaar734a8672019-12-02 22:49:38 +01002698 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2700 if (wbuf != NULL)
2701 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002702 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002703 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704 }
2705 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002706 else
2707 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708}
2709
2710 void
2711gui_mch_find_dialog(exarg_T *eap)
2712{
2713#ifdef MSWIN_FIND_REPLACE
2714 if (s_findrep_msg != 0)
2715 {
2716 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2717 DestroyWindow(s_findrep_hwnd);
2718
2719 if (!IsWindow(s_findrep_hwnd))
2720 {
2721 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002722 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 }
2724
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002725 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002726 (void)SetFocus(s_findrep_hwnd);
2727
2728 s_findrep_is_find = TRUE;
2729 }
2730#endif
2731}
2732
2733
2734 void
2735gui_mch_replace_dialog(exarg_T *eap)
2736{
2737#ifdef MSWIN_FIND_REPLACE
2738 if (s_findrep_msg != 0)
2739 {
2740 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2741 DestroyWindow(s_findrep_hwnd);
2742
2743 if (!IsWindow(s_findrep_hwnd))
2744 {
2745 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002746 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 }
2748
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002749 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002750 (void)SetFocus(s_findrep_hwnd);
2751
2752 s_findrep_is_find = FALSE;
2753 }
2754#endif
2755}
2756
2757
2758/*
2759 * Set visibility of the pointer.
2760 */
2761 void
2762gui_mch_mousehide(int hide)
2763{
2764 if (hide != gui.pointer_hidden)
2765 {
2766 ShowCursor(!hide);
2767 gui.pointer_hidden = hide;
2768 }
2769}
2770
2771#ifdef FEAT_MENU
2772 static void
2773gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2774{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002775 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002776 gui_mch_mousehide(FALSE);
2777
2778 (void)TrackPopupMenu(
2779 (HMENU)menu->submenu_id,
2780 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2781 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002782 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783 s_hwnd,
2784 NULL);
2785 /*
2786 * NOTE: The pop-up menu can eat the mouse up event.
2787 * We deal with this in normal.c.
2788 */
2789}
2790#endif
2791
2792/*
2793 * Got a message when the system will go down.
2794 */
2795 static void
2796_OnEndSession(void)
2797{
2798 getout_preserve_modified(1);
2799}
2800
2801/*
2802 * Get this message when the user clicks on the cross in the top right corner
2803 * of a Windows95 window.
2804 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002806_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002807{
2808 gui_shell_closed();
2809}
2810
2811/*
2812 * Get a message when the window is being destroyed.
2813 */
2814 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002815_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816{
2817 if (!destroying)
2818 _OnClose(hwnd);
2819}
2820
2821 static void
2822_OnPaint(
2823 HWND hwnd)
2824{
2825 if (!IsMinimized(hwnd))
2826 {
2827 PAINTSTRUCT ps;
2828
Bram Moolenaar734a8672019-12-02 22:49:38 +01002829 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002830 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831
Bram Moolenaar734a8672019-12-02 22:49:38 +01002832 // prevent multi-byte characters from misprinting on an invalid
2833 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002834 if (has_mbyte)
2835 {
2836 RECT rect;
2837
2838 GetClientRect(hwnd, &rect);
2839 ps.rcPaint.left = rect.left;
2840 ps.rcPaint.right = rect.right;
2841 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842
2843 if (!IsRectEmpty(&ps.rcPaint))
2844 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002845 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2846 ps.rcPaint.right - ps.rcPaint.left + 1,
2847 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2848 }
2849
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850 EndPaint(hwnd, &ps);
2851 }
2852}
2853
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002854 static void
2855_OnSize(
2856 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002857 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 int cx,
2859 int cy)
2860{
2861 if (!IsMinimized(hwnd))
2862 {
2863 gui_resize_shell(cx, cy);
2864
2865#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002866 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867 gui_mswin_get_menu_height(TRUE);
2868#endif
2869 }
2870}
2871
2872 static void
2873_OnSetFocus(
2874 HWND hwnd,
2875 HWND hwndOldFocus)
2876{
2877 gui_focus_change(TRUE);
2878 s_getting_focus = TRUE;
2879 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2880}
2881
2882 static void
2883_OnKillFocus(
2884 HWND hwnd,
2885 HWND hwndNewFocus)
2886{
2887 gui_focus_change(FALSE);
2888 s_getting_focus = FALSE;
2889 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2890}
2891
2892/*
2893 * Get a message when the user switches back to vim
2894 */
2895 static LRESULT
2896_OnActivateApp(
2897 HWND hwnd,
2898 BOOL fActivate,
2899 DWORD dwThreadId)
2900{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002901 // we call gui_focus_change() in _OnSetFocus()
2902 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2904}
2905
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002906 void
2907gui_mch_destroy_scrollbar(scrollbar_T *sb)
2908{
2909 DestroyWindow(sb->id);
2910}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002911
2912/*
2913 * Get current mouse coordinates in text window.
2914 */
2915 void
2916gui_mch_getmouse(int *x, int *y)
2917{
2918 RECT rct;
2919 POINT mp;
2920
2921 (void)GetWindowRect(s_textArea, &rct);
2922 (void)GetCursorPos((LPPOINT)&mp);
2923 *x = (int)(mp.x - rct.left);
2924 *y = (int)(mp.y - rct.top);
2925}
2926
2927/*
2928 * Move mouse pointer to character at (x, y).
2929 */
2930 void
2931gui_mch_setmouse(int x, int y)
2932{
2933 RECT rct;
2934
2935 (void)GetWindowRect(s_textArea, &rct);
2936 (void)SetCursorPos(x + gui.border_offset + rct.left,
2937 y + gui.border_offset + rct.top);
2938}
2939
2940 static void
2941gui_mswin_get_valid_dimensions(
2942 int w,
2943 int h,
2944 int *valid_w,
2945 int *valid_h)
2946{
2947 int base_width, base_height;
2948
2949 base_width = gui_get_base_width()
2950 + (GetSystemMetrics(SM_CXFRAME) +
2951 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2952 base_height = gui_get_base_height()
2953 + (GetSystemMetrics(SM_CYFRAME) +
2954 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2955 + GetSystemMetrics(SM_CYCAPTION)
2956#ifdef FEAT_MENU
2957 + gui_mswin_get_menu_height(FALSE)
2958#endif
2959 ;
2960 *valid_w = base_width +
2961 ((w - base_width) / gui.char_width) * gui.char_width;
2962 *valid_h = base_height +
2963 ((h - base_height) / gui.char_height) * gui.char_height;
2964}
2965
2966 void
2967gui_mch_flash(int msec)
2968{
2969 RECT rc;
2970
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002971#if defined(FEAT_DIRECTX)
2972 if (IS_ENABLE_DIRECTX())
2973 DWriteContext_Flush(s_dwc);
2974#endif
2975
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002976 /*
2977 * Note: InvertRect() excludes right and bottom of rectangle.
2978 */
2979 rc.left = 0;
2980 rc.top = 0;
2981 rc.right = gui.num_cols * gui.char_width;
2982 rc.bottom = gui.num_rows * gui.char_height;
2983 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002984 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985
Bram Moolenaar734a8672019-12-02 22:49:38 +01002986 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987
2988 InvertRect(s_hdc, &rc);
2989}
2990
2991/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01002992 * Check if the specified point is on-screen. (multi-monitor aware)
2993 */
2994 static BOOL
2995is_point_onscreen(int x, int y)
2996{
2997 POINT pt = {x, y};
2998
2999 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3000}
3001
3002/*
3003 * Check if the whole area of the specified window is on-screen.
3004 *
3005 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3006 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3007 * only works when the whole window is on-screen.
3008 */
3009 static BOOL
3010is_window_onscreen(HWND hwnd)
3011{
3012 RECT rc;
3013
3014 GetWindowRect(hwnd, &rc);
3015
3016 if (!is_point_onscreen(rc.left, rc.top))
3017 return FALSE;
3018 if (!is_point_onscreen(rc.left, rc.bottom))
3019 return FALSE;
3020 if (!is_point_onscreen(rc.right, rc.top))
3021 return FALSE;
3022 if (!is_point_onscreen(rc.right, rc.bottom))
3023 return FALSE;
3024 return TRUE;
3025}
3026
3027/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003028 * Return flags used for scrolling.
3029 * The SW_INVALIDATE is required when part of the window is covered or
3030 * off-screen. Refer to MS KB Q75236.
3031 */
3032 static int
3033get_scroll_flags(void)
3034{
3035 HWND hwnd;
3036 RECT rcVim, rcOther, rcDest;
3037
Bram Moolenaar185577e2020-10-29 20:08:21 +01003038 // Check if the window is (partly) off-screen.
3039 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003040 return SW_INVALIDATE;
3041
Bram Moolenaar734a8672019-12-02 22:49:38 +01003042 // Check if there is an window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003043 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3045 if (IsWindowVisible(hwnd))
3046 {
3047 GetWindowRect(hwnd, &rcOther);
3048 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3049 return SW_INVALIDATE;
3050 }
3051 return 0;
3052}
3053
3054/*
3055 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3056 * may not be scrolled out properly.
3057 * For gVim, when _OnScroll() is repeated, the character at the
3058 * previous cursor position may be left drawn after scroll.
3059 * The problem can be avoided by calling GetPixel() to get a pixel in
3060 * the region before ScrollWindowEx().
3061 */
3062 static void
3063intel_gpu_workaround(void)
3064{
3065 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3066}
3067
3068/*
3069 * Delete the given number of lines from the given row, scrolling up any
3070 * text further down within the scroll region.
3071 */
3072 void
3073gui_mch_delete_lines(
3074 int row,
3075 int num_lines)
3076{
3077 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003078
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003079 rc.left = FILL_X(gui.scroll_region_left);
3080 rc.right = FILL_X(gui.scroll_region_right + 1);
3081 rc.top = FILL_Y(row);
3082 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3083
Bram Moolenaar92467d32017-12-05 13:22:16 +01003084#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003085 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003086 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003087 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003088 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003089 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003090#endif
3091 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003092#if defined(FEAT_DIRECTX)
3093 if (IS_ENABLE_DIRECTX())
3094 DWriteContext_Flush(s_dwc);
3095#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003096 intel_gpu_workaround();
3097 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003098 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003099 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003100 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003101
Bram Moolenaar734a8672019-12-02 22:49:38 +01003102 // This seems to be required to avoid the cursor disappearing when
3103 // scrolling such that the cursor ends up in the top-left character on
3104 // the screen... But why? (Webb)
3105 // It's probably fixed by disabling drawing the cursor while scrolling.
3106 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107
3108 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3109 gui.scroll_region_left,
3110 gui.scroll_region_bot, gui.scroll_region_right);
3111}
3112
3113/*
3114 * Insert the given number of lines before the given row, scrolling down any
3115 * following text within the scroll region.
3116 */
3117 void
3118gui_mch_insert_lines(
3119 int row,
3120 int num_lines)
3121{
3122 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003123
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124 rc.left = FILL_X(gui.scroll_region_left);
3125 rc.right = FILL_X(gui.scroll_region_right + 1);
3126 rc.top = FILL_Y(row);
3127 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003128
3129#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003130 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003131 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003132 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003133 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003134 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003135#endif
3136 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003137#if defined(FEAT_DIRECTX)
3138 if (IS_ENABLE_DIRECTX())
3139 DWriteContext_Flush(s_dwc);
3140#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003141 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003142 // The SW_INVALIDATE is required when part of the window is covered or
3143 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003144 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003145 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003146 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003147 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003148
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149 gui_clear_block(row, gui.scroll_region_left,
3150 row + num_lines - 1, gui.scroll_region_right);
3151}
3152
3153
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003154 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003155gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156{
3157#if defined(FEAT_DIRECTX)
3158 DWriteContext_Close(s_dwc);
3159 DWrite_Final();
3160 s_dwc = NULL;
3161#endif
3162
3163 ReleaseDC(s_textArea, s_hdc);
3164 DeleteObject(s_brush);
3165
3166#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003167 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003168 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3169#endif
3170
Bram Moolenaar734a8672019-12-02 22:49:38 +01003171 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 if (s_hwnd != NULL)
3173 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003174 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003175 DestroyWindow(s_hwnd);
3176 }
3177
3178#ifdef GLOBAL_IME
3179 global_ime_end();
3180#endif
3181}
3182
3183 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003184logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003185{
3186 char *p;
3187 char *res;
3188 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003189 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003190 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003191 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003192
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003193 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3194 if (font_name == NULL)
3195 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003196 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003197 quality_name = quality_id2name((int)lf.lfQuality);
3198
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003199 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003200 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003201 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003202 if (res != NULL)
3203 {
3204 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003205 // make a normal font string out of the lf thing:
3206 points = pixels_to_points(
3207 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3208 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3209 sprintf((char *)p, "%s:h%d", font_name, points);
3210 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003211 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212 while (*p)
3213 {
3214 if (*p == ' ')
3215 *p = '_';
3216 ++p;
3217 }
3218 if (lf.lfItalic)
3219 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003220 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221 STRCAT(p, ":b");
3222 if (lf.lfUnderline)
3223 STRCAT(p, ":u");
3224 if (lf.lfStrikeOut)
3225 STRCAT(p, ":s");
3226 if (charset_name != NULL)
3227 {
3228 STRCAT(p, ":c");
3229 STRCAT(p, charset_name);
3230 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003231 if (quality_name != NULL)
3232 {
3233 STRCAT(p, ":q");
3234 STRCAT(p, quality_name);
3235 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236 }
3237
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003238 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 return (char_u *)res;
3240}
3241
3242
3243#ifdef FEAT_MBYTE_IME
3244/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003245 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003246 * 'guifont'
3247 */
3248 static void
3249update_im_font(void)
3250{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003251 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252
3253 if (p_guifontwide != NULL && *p_guifontwide != NUL
3254 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003255 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256 norm_logfont = lf_wide;
3257 else
3258 norm_logfont = sub_logfont;
3259 im_set_font(&norm_logfont);
3260}
3261#endif
3262
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003263/*
3264 * Handler of gui.wide_font (p_guifontwide) changed notification.
3265 */
3266 void
3267gui_mch_wide_font_changed(void)
3268{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003269 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003270
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003271#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003273#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003274
3275 gui_mch_free_font(gui.wide_ital_font);
3276 gui.wide_ital_font = NOFONT;
3277 gui_mch_free_font(gui.wide_bold_font);
3278 gui.wide_bold_font = NOFONT;
3279 gui_mch_free_font(gui.wide_boldital_font);
3280 gui.wide_boldital_font = NOFONT;
3281
3282 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003283 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284 {
3285 if (!lf.lfItalic)
3286 {
3287 lf.lfItalic = TRUE;
3288 gui.wide_ital_font = get_font_handle(&lf);
3289 lf.lfItalic = FALSE;
3290 }
3291 if (lf.lfWeight < FW_BOLD)
3292 {
3293 lf.lfWeight = FW_BOLD;
3294 gui.wide_bold_font = get_font_handle(&lf);
3295 if (!lf.lfItalic)
3296 {
3297 lf.lfItalic = TRUE;
3298 gui.wide_boldital_font = get_font_handle(&lf);
3299 }
3300 }
3301 }
3302}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303
3304/*
3305 * Initialise vim to use the font with the given name.
3306 * Return FAIL if the font could not be loaded, OK otherwise.
3307 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003309gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003311 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003312 GuiFont font = NOFONT;
3313 char_u *p;
3314
Bram Moolenaar734a8672019-12-02 22:49:38 +01003315 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003316 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3317 font = get_font_handle(&lf);
3318 if (font == NOFONT)
3319 return FAIL;
3320
3321 if (font_name == NULL)
3322 font_name = (char_u *)lf.lfFaceName;
3323#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3324 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003325#endif
3326#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327 sub_logfont = lf;
3328#endif
3329#ifdef FEAT_MBYTE_IME
3330 update_im_font();
3331#endif
3332 gui_mch_free_font(gui.norm_font);
3333 gui.norm_font = font;
3334 current_font_height = lf.lfHeight;
3335 GetFontSize(font);
3336
3337 p = logfont2name(lf);
3338 if (p != NULL)
3339 {
3340 hl_set_font_name(p);
3341
Bram Moolenaar734a8672019-12-02 22:49:38 +01003342 // When setting 'guifont' to "*" replace it with the actual font name.
3343 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3345 {
3346 vim_free(p_guifont);
3347 p_guifont = p;
3348 }
3349 else
3350 vim_free(p);
3351 }
3352
3353 gui_mch_free_font(gui.ital_font);
3354 gui.ital_font = NOFONT;
3355 gui_mch_free_font(gui.bold_font);
3356 gui.bold_font = NOFONT;
3357 gui_mch_free_font(gui.boldital_font);
3358 gui.boldital_font = NOFONT;
3359
3360 if (!lf.lfItalic)
3361 {
3362 lf.lfItalic = TRUE;
3363 gui.ital_font = get_font_handle(&lf);
3364 lf.lfItalic = FALSE;
3365 }
3366 if (lf.lfWeight < FW_BOLD)
3367 {
3368 lf.lfWeight = FW_BOLD;
3369 gui.bold_font = get_font_handle(&lf);
3370 if (!lf.lfItalic)
3371 {
3372 lf.lfItalic = TRUE;
3373 gui.boldital_font = get_font_handle(&lf);
3374 }
3375 }
3376
3377 return OK;
3378}
3379
3380#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003381# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003382#endif
3383
3384/*
3385 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003386 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 */
3388 int
3389gui_mch_maximized(void)
3390{
3391 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003392 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393
3394 wp.length = sizeof(WINDOWPLACEMENT);
3395 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003396 {
3397 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003399 && wp.flags == WPF_RESTORETOMAXIMIZED))
3400 return TRUE;
3401 if (wp.showCmd == SW_SHOWMINIMIZED)
3402 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003404 // Assume the window is snapped when the sizes from two APIs differ.
3405 GetWindowRect(s_hwnd, &rc);
3406 if ((rc.right - rc.left !=
3407 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3408 || (rc.bottom - rc.top !=
3409 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3410 return TRUE;
3411 }
3412 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413}
3414
3415/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003416 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3417 * is set. Compute the new Rows and Columns. This is like resizing the
3418 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419 */
3420 void
3421gui_mch_newfont(void)
3422{
3423 RECT rect;
3424
3425 GetWindowRect(s_hwnd, &rect);
3426 if (win_socket_id == 0)
3427 {
3428 gui_resize_shell(rect.right - rect.left
3429 - (GetSystemMetrics(SM_CXFRAME) +
3430 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3431 rect.bottom - rect.top
3432 - (GetSystemMetrics(SM_CYFRAME) +
3433 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3434 - GetSystemMetrics(SM_CYCAPTION)
3435#ifdef FEAT_MENU
3436 - gui_mswin_get_menu_height(FALSE)
3437#endif
3438 );
3439 }
3440 else
3441 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003442 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003443 gui_resize_shell(rect.right - rect.left,
3444 rect.bottom - rect.top
3445#ifdef FEAT_MENU
3446 - gui_mswin_get_menu_height(FALSE)
3447#endif
3448 );
3449 }
3450}
3451
3452/*
3453 * Set the window title
3454 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003455 void
3456gui_mch_settitle(
3457 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003458 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459{
3460 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3461}
3462
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003463#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003464// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3465// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466static LPCSTR mshape_idcs[] =
3467{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003468 IDC_ARROW, // arrow
3469 MAKEINTRESOURCE(0), // blank
3470 IDC_IBEAM, // beam
3471 IDC_SIZENS, // updown
3472 IDC_SIZENS, // udsizing
3473 IDC_SIZEWE, // leftright
3474 IDC_SIZEWE, // lrsizing
3475 IDC_WAIT, // busy
3476 IDC_NO, // no
3477 IDC_ARROW, // crosshair
3478 IDC_ARROW, // hand1
3479 IDC_ARROW, // hand2
3480 IDC_ARROW, // pencil
3481 IDC_ARROW, // question
3482 IDC_ARROW, // right-arrow
3483 IDC_UPARROW, // up-arrow
3484 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485};
3486
3487 void
3488mch_set_mouse_shape(int shape)
3489{
3490 LPCSTR idc;
3491
3492 if (shape == MSHAPE_HIDE)
3493 ShowCursor(FALSE);
3494 else
3495 {
3496 if (shape >= MSHAPE_NUMBERED)
3497 idc = IDC_ARROW;
3498 else
3499 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003500 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 if (!p_mh)
3502 {
3503 POINT mp;
3504
Bram Moolenaar734a8672019-12-02 22:49:38 +01003505 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 (void)GetCursorPos((LPPOINT)&mp);
3507 (void)SetCursorPos(mp.x, mp.y);
3508 ShowCursor(TRUE);
3509 }
3510 }
3511}
3512#endif
3513
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003514#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516 * Wide version of convert_filter().
3517 */
3518 static WCHAR *
3519convert_filterW(char_u *s)
3520{
3521 char_u *tmp;
3522 int len;
3523 WCHAR *res;
3524
3525 tmp = convert_filter(s);
3526 if (tmp == NULL)
3527 return NULL;
3528 len = (int)STRLEN(s) + 3;
3529 res = enc_to_utf16(tmp, &len);
3530 vim_free(tmp);
3531 return res;
3532}
3533
3534/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003535 * Pop open a file browser and return the file selected, in allocated memory,
3536 * or NULL if Cancel is hit.
3537 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3538 * title - Title message for the file browser dialog.
3539 * dflt - Default name of file.
3540 * ext - Default extension to be added to files without extensions.
3541 * initdir - directory in which to open the browser (NULL = current dir)
3542 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003544 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003545gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546 int saving,
3547 char_u *title,
3548 char_u *dflt,
3549 char_u *ext,
3550 char_u *initdir,
3551 char_u *filter)
3552{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003553 // We always use the wide function. This means enc_to_utf16() must work,
3554 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555 OPENFILENAMEW fileStruct;
3556 WCHAR fileBuf[MAXPATHL];
3557 WCHAR *wp;
3558 int i;
3559 WCHAR *titlep = NULL;
3560 WCHAR *extp = NULL;
3561 WCHAR *initdirp = NULL;
3562 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003563 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564
3565 if (dflt == NULL)
3566 fileBuf[0] = NUL;
3567 else
3568 {
3569 wp = enc_to_utf16(dflt, NULL);
3570 if (wp == NULL)
3571 fileBuf[0] = NUL;
3572 else
3573 {
3574 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3575 fileBuf[i] = wp[i];
3576 fileBuf[i] = NUL;
3577 vim_free(wp);
3578 }
3579 }
3580
Bram Moolenaar734a8672019-12-02 22:49:38 +01003581 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003582 filterp = convert_filterW(filter);
3583
Bram Moolenaara80faa82020-04-12 19:37:17 +02003584 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003585# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003586 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003587 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003588# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003590# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591
3592 if (title != NULL)
3593 titlep = enc_to_utf16(title, NULL);
3594 fileStruct.lpstrTitle = titlep;
3595
3596 if (ext != NULL)
3597 extp = enc_to_utf16(ext, NULL);
3598 fileStruct.lpstrDefExt = extp;
3599
3600 fileStruct.lpstrFile = fileBuf;
3601 fileStruct.nMaxFile = MAXPATHL;
3602 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003603 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3604 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605 if (initdir != NULL && *initdir != NUL)
3606 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003607 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003608 initdirp = enc_to_utf16(initdir, NULL);
3609 if (initdirp != NULL)
3610 {
3611 for (wp = initdirp; *wp != NUL; ++wp)
3612 if (*wp == '/')
3613 *wp = '\\';
3614 }
3615 fileStruct.lpstrInitialDir = initdirp;
3616 }
3617
3618 /*
3619 * TODO: Allow selection of multiple files. Needs another arg to this
3620 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3621 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3622 * files that don't exist yet, so I haven't put it in. What about
3623 * OFN_PATHMUSTEXIST?
3624 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3625 */
3626 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003627# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 if (curbuf->b_p_bin)
3629 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003630# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631 if (saving)
3632 {
3633 if (!GetSaveFileNameW(&fileStruct))
3634 return NULL;
3635 }
3636 else
3637 {
3638 if (!GetOpenFileNameW(&fileStruct))
3639 return NULL;
3640 }
3641
3642 vim_free(filterp);
3643 vim_free(initdirp);
3644 vim_free(titlep);
3645 vim_free(extp);
3646
Bram Moolenaar734a8672019-12-02 22:49:38 +01003647 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003648 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003649 if (p == NULL)
3650 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003651
Bram Moolenaar734a8672019-12-02 22:49:38 +01003652 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 SetFocus(s_hwnd);
3654
Bram Moolenaar734a8672019-12-02 22:49:38 +01003655 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003656 q = vim_strsave(shorten_fname1(p));
3657 vim_free(p);
3658 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660
3661
3662/*
3663 * Convert the string s to the proper format for a filter string by replacing
3664 * the \t and \n delimiters with \0.
3665 * Returns the converted string in allocated memory.
3666 *
3667 * Keep in sync with convert_filterW() above!
3668 */
3669 static char_u *
3670convert_filter(char_u *s)
3671{
3672 char_u *res;
3673 unsigned s_len = (unsigned)STRLEN(s);
3674 unsigned i;
3675
3676 res = alloc(s_len + 3);
3677 if (res != NULL)
3678 {
3679 for (i = 0; i < s_len; ++i)
3680 if (s[i] == '\t' || s[i] == '\n')
3681 res[i] = '\0';
3682 else
3683 res[i] = s[i];
3684 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003685 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 res[s_len + 1] = NUL;
3687 res[s_len + 2] = NUL;
3688 }
3689 return res;
3690}
3691
3692/*
3693 * Select a directory.
3694 */
3695 char_u *
3696gui_mch_browsedir(char_u *title, char_u *initdir)
3697{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003698 // We fake this: Use a filter that doesn't select anything and a default
3699 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3701 initdir, (char_u *)_("Directory\t*.nothing\n"));
3702}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003703#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705 static void
3706_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003707 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003708 HDROP hDrop)
3709{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003710#define BUFPATHLEN _MAX_PATH
3711#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003713 char szFile[BUFPATHLEN];
3714 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3715 UINT i;
3716 char_u **fnames;
3717 POINT pt;
3718 int_u modifiers = 0;
3719
Bram Moolenaar734a8672019-12-02 22:49:38 +01003720 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003721
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723 DragQueryPoint(hDrop, &pt);
3724 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3725
3726 reset_VIsual();
3727
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003728 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729
3730 if (fnames != NULL)
3731 for (i = 0; i < cFiles; ++i)
3732 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3734 fnames[i] = utf16_to_enc(wszFile, NULL);
3735 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003736 {
3737 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3738 fnames[i] = vim_strsave((char_u *)szFile);
3739 }
3740 }
3741
3742 DragFinish(hDrop);
3743
3744 if (fnames != NULL)
3745 {
3746 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3747 modifiers |= MOUSE_SHIFT;
3748 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3749 modifiers |= MOUSE_CTRL;
3750 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3751 modifiers |= MOUSE_ALT;
3752
3753 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3754
3755 s_need_activate = TRUE;
3756 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757}
3758
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759 static int
3760_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003761 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762 HWND hwndCtl,
3763 UINT code,
3764 int pos)
3765{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003766 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 scrollbar_T *sb, *sb_info;
3768 long val;
3769 int dragging = FALSE;
3770 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 SCROLLINFO si;
3772
3773 si.cbSize = sizeof(si);
3774 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775
3776 sb = gui_mswin_find_scrollbar(hwndCtl);
3777 if (sb == NULL)
3778 return 0;
3779
Bram Moolenaar734a8672019-12-02 22:49:38 +01003780 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 {
3782 /*
3783 * Careful: need to get scrollbar info out of first (left) scrollbar
3784 * for window, but keep real scrollbar too because we must pass it to
3785 * gui_drag_scrollbar().
3786 */
3787 sb_info = &sb->wp->w_scrollbars[0];
3788 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003789 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 sb_info = sb;
3791 val = sb_info->value;
3792
3793 switch (code)
3794 {
3795 case SB_THUMBTRACK:
3796 val = pos;
3797 dragging = TRUE;
3798 if (sb->scroll_shift > 0)
3799 val <<= sb->scroll_shift;
3800 break;
3801 case SB_LINEDOWN:
3802 val++;
3803 break;
3804 case SB_LINEUP:
3805 val--;
3806 break;
3807 case SB_PAGEDOWN:
3808 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3809 break;
3810 case SB_PAGEUP:
3811 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3812 break;
3813 case SB_TOP:
3814 val = 0;
3815 break;
3816 case SB_BOTTOM:
3817 val = sb_info->max;
3818 break;
3819 case SB_ENDSCROLL:
3820 if (prev_code == SB_THUMBTRACK)
3821 {
3822 /*
3823 * "pos" only gives us 16-bit data. In case of large file,
3824 * use GetScrollPos() which returns 32-bit. Unfortunately it
3825 * is not valid while the scrollbar is being dragged.
3826 */
3827 val = GetScrollPos(hwndCtl, SB_CTL);
3828 if (sb->scroll_shift > 0)
3829 val <<= sb->scroll_shift;
3830 }
3831 break;
3832
3833 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003834 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 return 0;
3836 }
3837 prev_code = code;
3838
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3840 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841
3842 /*
3843 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3844 */
3845 if (sb->wp != NULL)
3846 {
3847 scrollbar_T *sba = sb->wp->w_scrollbars;
3848 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3849
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 }
3852
Bram Moolenaar734a8672019-12-02 22:49:38 +01003853 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003854 s_busy_processing = TRUE;
3855
Bram Moolenaar734a8672019-12-02 22:49:38 +01003856 // When "allow_scrollbar" is FALSE still need to remember the new
3857 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 dont_scroll = !allow_scrollbar;
3859
Bram Moolenaara338adc2018-01-31 20:51:47 +01003860 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003862 mch_enable_flush();
3863 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864
3865 s_busy_processing = FALSE;
3866 dont_scroll = dont_scroll_save;
3867
3868 return 0;
3869}
3870
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#ifdef FEAT_XPM_W32
3873# include "xpm_w32.h"
3874#endif
3875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876#ifdef __MINGW32__
3877/*
3878 * Add a lot of missing defines.
3879 * They are not always missing, we need the #ifndef's.
3880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881# ifndef IsMinimized
3882# define IsMinimized(hwnd) IsIconic(hwnd)
3883# endif
3884# ifndef IsMaximized
3885# define IsMaximized(hwnd) IsZoomed(hwnd)
3886# endif
3887# ifndef SelectFont
3888# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3889# endif
3890# ifndef GetStockBrush
3891# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3892# endif
3893# ifndef DeleteBrush
3894# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3895# endif
3896
3897# ifndef HANDLE_WM_RBUTTONDBLCLK
3898# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3899 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3900# endif
3901# ifndef HANDLE_WM_MBUTTONUP
3902# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3903 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3904# endif
3905# ifndef HANDLE_WM_MBUTTONDBLCLK
3906# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3907 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3908# endif
3909# ifndef HANDLE_WM_LBUTTONDBLCLK
3910# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3911 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3912# endif
3913# ifndef HANDLE_WM_RBUTTONDOWN
3914# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3915 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3916# endif
3917# ifndef HANDLE_WM_MOUSEMOVE
3918# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3919 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3920# endif
3921# ifndef HANDLE_WM_RBUTTONUP
3922# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3923 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3924# endif
3925# ifndef HANDLE_WM_MBUTTONDOWN
3926# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3927 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3928# endif
3929# ifndef HANDLE_WM_LBUTTONUP
3930# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3931 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3932# endif
3933# ifndef HANDLE_WM_LBUTTONDOWN
3934# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3935 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3936# endif
3937# ifndef HANDLE_WM_SYSCHAR
3938# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3939 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3940# endif
3941# ifndef HANDLE_WM_ACTIVATEAPP
3942# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3943 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3944# endif
3945# ifndef HANDLE_WM_WINDOWPOSCHANGING
3946# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3947 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3948# endif
3949# ifndef HANDLE_WM_VSCROLL
3950# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3951 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3952# endif
3953# ifndef HANDLE_WM_SETFOCUS
3954# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3955 ((fn)((hwnd), (HWND)(wParam)), 0L)
3956# endif
3957# ifndef HANDLE_WM_KILLFOCUS
3958# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3959 ((fn)((hwnd), (HWND)(wParam)), 0L)
3960# endif
3961# ifndef HANDLE_WM_HSCROLL
3962# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3963 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3964# endif
3965# ifndef HANDLE_WM_DROPFILES
3966# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3967 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3968# endif
3969# ifndef HANDLE_WM_CHAR
3970# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3971 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3972# endif
3973# ifndef HANDLE_WM_SYSDEADCHAR
3974# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3975 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3976# endif
3977# ifndef HANDLE_WM_DEADCHAR
3978# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3979 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3980# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003981#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982
3983
Bram Moolenaar734a8672019-12-02 22:49:38 +01003984// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#define TEAROFF_PADDING_X 2
3986#define TEAROFF_BUTTON_PAD_X 8
3987#define TEAROFF_MIN_WIDTH 200
3988#define TEAROFF_SUBMENU_LABEL ">>"
3989#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3990
3991
Bram Moolenaar734a8672019-12-02 22:49:38 +01003992// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003994# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#endif
3996
3997
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003998#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999# define ID_BEVAL_TOOLTIP 200
4000# define BEVAL_TEXT_LEN MAXPATHL
4001
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004002# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004003// Work around old versions of basetsd.h which wrongly declares
4004// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004005# undef UINT_PTR
4006# define UINT_PTR UINT
4007# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004010static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004012
Bram Moolenaar82881492012-11-20 16:53:39 +01004013
Bram Moolenaar734a8672019-12-02 22:49:38 +01004014// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004015# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004016
Bram Moolenaar45360022005-07-21 21:08:21 +00004017/*
4018 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004019 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004020 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004021# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004022
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004023# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004024
4025typedef struct _DllVersionInfo
4026{
4027 DWORD cbSize;
4028 DWORD dwMajorVersion;
4029 DWORD dwMinorVersion;
4030 DWORD dwBuildNumber;
4031 DWORD dwPlatformID;
4032} DLLVERSIONINFO;
4033
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004034# ifndef PROTO
4035# include <poppack.h>
4036# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004037
Bram Moolenaar45360022005-07-21 21:08:21 +00004038typedef struct tagTOOLINFOA_NEW
4039{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004040 UINT cbSize;
4041 UINT uFlags;
4042 HWND hwnd;
4043 UINT_PTR uId;
4044 RECT rect;
4045 HINSTANCE hinst;
4046 LPSTR lpszText;
4047 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004048} TOOLINFO_NEW;
4049
4050typedef struct tagNMTTDISPINFO_NEW
4051{
4052 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004053 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004054 char szText[80];
4055 HINSTANCE hinst;
4056 UINT uFlags;
4057 LPARAM lParam;
4058} NMTTDISPINFO_NEW;
4059
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004060typedef struct tagTOOLINFOW_NEW
4061{
4062 UINT cbSize;
4063 UINT uFlags;
4064 HWND hwnd;
4065 UINT_PTR uId;
4066 RECT rect;
4067 HINSTANCE hinst;
4068 LPWSTR lpszText;
4069 LPARAM lParam;
4070 void *lpReserved;
4071} TOOLINFOW_NEW;
4072
4073typedef struct tagNMTTDISPINFOW_NEW
4074{
4075 NMHDR hdr;
4076 LPWSTR lpszText;
4077 WCHAR szText[80];
4078 HINSTANCE hinst;
4079 UINT uFlags;
4080 LPARAM lParam;
4081} NMTTDISPINFOW_NEW;
4082
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004083
Bram Moolenaar45360022005-07-21 21:08:21 +00004084typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004085# ifndef TTM_SETMAXTIPWIDTH
4086# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004089# ifndef TTF_DI_SETITEM
4090# define TTF_DI_SETITEM 0x8000
4091# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004092
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004093# ifndef TTN_GETDISPINFO
4094# define TTN_GETDISPINFO (TTN_FIRST - 0)
4095# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004096
Bram Moolenaar734a8672019-12-02 22:49:38 +01004097#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004098
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004099#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004100// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4101// it here if LPNMTTDISPINFO isn't defined.
4102// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4103// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004104# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4105typedef struct tagNMTTDISPINFOA {
4106 NMHDR hdr;
4107 LPSTR lpszText;
4108 char szText[80];
4109 HINSTANCE hinst;
4110 UINT uFlags;
4111 LPARAM lParam;
4112} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4113# define LPNMTTDISPINFO LPNMTTDISPINFOA
4114
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004115typedef struct tagNMTTDISPINFOW {
4116 NMHDR hdr;
4117 LPWSTR lpszText;
4118 WCHAR szText[80];
4119 HINSTANCE hinst;
4120 UINT uFlags;
4121 LPARAM lParam;
4122} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004123# endif
4124#endif
4125
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004126#ifndef TTN_GETDISPINFOW
4127# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4128#endif
4129
Bram Moolenaar734a8672019-12-02 22:49:38 +01004130// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132#ifdef FEAT_MENU
4133static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136/*
4137 * Use the system font for dialogs and tear-off menus. Remove this line to
4138 * use DLG_FONT_NAME.
4139 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004140#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#define VIM_CLASSW L"Vim"
4144
Bram Moolenaar734a8672019-12-02 22:49:38 +01004145// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4146// thus there should be room for every dialog. For tearoffs it's made bigger
4147// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#define DLG_ALLOC_SIZE 16 * 1024
4149
4150/*
4151 * stuff for dialogs, menus, tearoffs etc.
4152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153static PWORD
4154add_dialog_element(
4155 PWORD p,
4156 DWORD lStyle,
4157 WORD x,
4158 WORD y,
4159 WORD w,
4160 WORD h,
4161 WORD Id,
4162 WORD clss,
4163 const char *caption);
4164static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004165static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004166#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169static void get_dialog_font_metrics(void);
4170
4171static int dialog_default_button = -1;
4172
Bram Moolenaar734a8672019-12-02 22:49:38 +01004173// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar734a8672019-12-02 22:49:38 +01004176static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#ifdef FEAT_TOOLBAR
4178static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004179static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180static int get_toolbar_bitmap(vimmenu_T *menu);
4181#endif
4182
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004183#ifdef FEAT_GUI_TABLINE
4184static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004185static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004186#endif
4187
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#ifdef FEAT_MBYTE_IME
4189static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4190static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4191#endif
4192#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4193# ifdef NOIME
4194typedef struct tagCOMPOSITIONFORM {
4195 DWORD dwStyle;
4196 POINT ptCurrentPos;
4197 RECT rcArea;
4198} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4199typedef HANDLE HIMC;
4200# endif
4201
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004202static HINSTANCE hLibImm = NULL;
4203static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4204static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4205static HIMC (WINAPI *pImmGetContext)(HWND);
4206static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4207static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4208static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4209static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004210static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4211static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004212static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4213static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004214static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215static void dyn_imm_load(void);
4216#else
4217# define pImmGetCompositionStringA ImmGetCompositionStringA
4218# define pImmGetCompositionStringW ImmGetCompositionStringW
4219# define pImmGetContext ImmGetContext
4220# define pImmAssociateContext ImmAssociateContext
4221# define pImmReleaseContext ImmReleaseContext
4222# define pImmGetOpenStatus ImmGetOpenStatus
4223# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004224# define pImmGetCompositionFontW ImmGetCompositionFontW
4225# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226# define pImmSetCompositionWindow ImmSetCompositionWindow
4227# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004228# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#endif
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#ifdef FEAT_MENU
4232/*
4233 * Figure out how high the menu bar is at the moment.
4234 */
4235 static int
4236gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004237 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 static int old_menu_height = -1;
4240
4241 RECT rc1, rc2;
4242 int num;
4243 int menu_height;
4244
4245 if (gui.menu_is_active)
4246 num = GetMenuItemCount(s_menuBar);
4247 else
4248 num = 0;
4249
4250 if (num == 0)
4251 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004252 else if (IsMinimized(s_hwnd))
4253 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004254 // The height of the menu cannot be determined while the window is
4255 // minimized. Take the previous height if the menu is changed in that
4256 // state, to avoid that Vim's vertical window size accidentally
4257 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004258 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 else
4261 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004262 /*
4263 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4264 * seem to have been set yet, so menu wraps in default window
4265 * width which is very narrow. Instead just return height of a
4266 * single menu item. Will still be wrong when the menu really
4267 * should wrap over more than one line.
4268 */
4269 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4270 if (gui.starting)
4271 menu_height = rc1.bottom - rc1.top + 1;
4272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004274 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4275 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 }
4278
4279 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004280 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004281 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 return menu_height;
4284}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004285#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287
4288/*
4289 * Setup for the Intellimouse
4290 */
4291 static void
4292init_mouse_wheel(void)
4293{
4294
4295#ifndef SPI_GETWHEELSCROLLLINES
4296# define SPI_GETWHEELSCROLLLINES 104
4297#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004298#ifndef SPI_SETWHEELSCROLLLINES
4299# define SPI_SETWHEELSCROLLLINES 105
4300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
Bram Moolenaar734a8672019-12-02 22:49:38 +01004302#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4303#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4305#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4306
Bram Moolenaar734a8672019-12-02 22:49:38 +01004307 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaar734a8672019-12-02 22:49:38 +01004309 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004310 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4311 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312}
4313
4314
Bram Moolenaarae20f342019-11-05 21:09:23 +01004315/*
4316 * Intellimouse wheel handler.
4317 * Treat a mouse wheel event as if it were a scroll request.
4318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 static void
4320_OnMouseWheel(
4321 HWND hwnd,
4322 short zDelta)
4323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int i;
4325 int size;
4326 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004327 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (mouse_scroll_lines == 0)
4330 init_mouse_wheel();
4331
Bram Moolenaarae20f342019-11-05 21:09:23 +01004332 wp = gui_mouse_window(FIND_POPUP);
4333
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004334#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004335 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004336 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004337 cmdarg_T cap;
4338 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004339
Bram Moolenaarae20f342019-11-05 21:09:23 +01004340 // Mouse hovers over popup window, scroll it if possible.
4341 mouse_row = wp->w_winrow;
4342 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004343 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004344 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4345 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4346 clear_oparg(&oa);
4347 cap.oap = &oa;
4348 nv_mousescroll(&cap);
4349 update_screen(0);
4350 setcursor();
4351 out_flush();
4352 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004353 }
4354#endif
4355
Bram Moolenaarae20f342019-11-05 21:09:23 +01004356 if (wp == NULL || !p_scf)
4357 wp = curwin;
4358
4359 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4360 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4361 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4362 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4363 else
4364 return;
4365 size = wp->w_height;
4366
Bram Moolenaara338adc2018-01-31 20:51:47 +01004367 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (mouse_scroll_lines > 0
4369 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4370 {
4371 for (i = mouse_scroll_lines; i > 0; --i)
4372 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4373 }
4374 else
4375 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004376 mch_enable_flush();
4377 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378}
4379
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004380#ifdef USE_SYSMENU_FONT
4381/*
4382 * Get Menu Font.
4383 * Return OK or FAIL.
4384 */
4385 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004386gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004387{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004388 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004389
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004390 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4391 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004392 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004393 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004394 &nm,
4395 0))
4396 return FAIL;
4397 *lf = nm.lfMenuFont;
4398 return OK;
4399}
4400#endif
4401
4402
4403#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4404/*
4405 * Set the GUI tabline font to the system menu font
4406 */
4407 static void
4408set_tabline_font(void)
4409{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004410 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004411 HFONT font;
4412 HWND hwnd;
4413 HDC hdc;
4414 HFONT hfntOld;
4415 TEXTMETRIC tm;
4416
4417 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4418 return;
4419
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004420 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004421
4422 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4423
4424 /*
4425 * Compute the height of the font used for the tab text
4426 */
4427 hwnd = GetDesktopWindow();
4428 hdc = GetWindowDC(hwnd);
4429 hfntOld = SelectFont(hdc, font);
4430
4431 GetTextMetrics(hdc, &tm);
4432
4433 SelectFont(hdc, hfntOld);
4434 ReleaseDC(hwnd, hdc);
4435
4436 /*
4437 * The space used by the tab border and the space between the tab label
4438 * and the tab border is included as 7.
4439 */
4440 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4441}
4442#endif
4443
Bram Moolenaar520470a2005-06-16 21:59:56 +00004444/*
4445 * Invoked when a setting was changed.
4446 */
4447 static LRESULT CALLBACK
4448_OnSettingChange(UINT n)
4449{
4450 if (n == SPI_SETWHEELSCROLLLINES)
4451 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4452 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004453#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4454 if (n == SPI_SETNONCLIENTMETRICS)
4455 set_tabline_font();
4456#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004457 return 0;
4458}
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460#ifdef FEAT_NETBEANS_INTG
4461 static void
4462_OnWindowPosChanged(
4463 HWND hwnd,
4464 const LPWINDOWPOS lpwpos)
4465{
4466 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004467 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4470 || lpwpos->cx != cx || lpwpos->cy != cy))
4471 {
4472 x = lpwpos->x;
4473 y = lpwpos->y;
4474 cx = lpwpos->cx;
4475 cy = lpwpos->cy;
4476 netbeans_frame_moved(x, y);
4477 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004478 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4480}
4481#endif
4482
4483 static int
4484_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 UINT fwSide,
4486 LPRECT lprc)
4487{
4488 int w, h;
4489 int valid_w, valid_h;
4490 int w_offset, h_offset;
4491
4492 w = lprc->right - lprc->left;
4493 h = lprc->bottom - lprc->top;
4494 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4495 w_offset = w - valid_w;
4496 h_offset = h - valid_h;
4497
4498 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4499 || fwSide == WMSZ_BOTTOMLEFT)
4500 lprc->left += w_offset;
4501 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4502 || fwSide == WMSZ_BOTTOMRIGHT)
4503 lprc->right -= w_offset;
4504
4505 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4506 || fwSide == WMSZ_TOPRIGHT)
4507 lprc->top += h_offset;
4508 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4509 || fwSide == WMSZ_BOTTOMRIGHT)
4510 lprc->bottom -= h_offset;
4511 return TRUE;
4512}
4513
4514
4515
4516 static LRESULT CALLBACK
4517_WndProc(
4518 HWND hwnd,
4519 UINT uMsg,
4520 WPARAM wParam,
4521 LPARAM lParam)
4522{
4523 /*
4524 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4525 hwnd, uMsg, wParam, lParam);
4526 */
4527
4528 HandleMouseHide(uMsg, lParam);
4529
4530 s_uMsg = uMsg;
4531 s_wParam = wParam;
4532 s_lParam = lParam;
4533
4534 switch (uMsg)
4535 {
4536 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4537 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004538 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004540 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4542 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4543 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4544 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4545#ifdef FEAT_MENU
4546 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4547#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004548 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4549 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4551 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004552 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4553 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4555 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4556 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4557#ifdef FEAT_NETBEANS_INTG
4558 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4559#endif
4560
Bram Moolenaarafa24992006-03-27 20:58:26 +00004561#ifdef FEAT_GUI_TABLINE
4562 case WM_RBUTTONUP:
4563 {
4564 if (gui_mch_showing_tabline())
4565 {
4566 POINT pt;
4567 RECT rect;
4568
4569 /*
4570 * If the cursor is on the tabline, display the tab menu
4571 */
4572 GetCursorPos((LPPOINT)&pt);
4573 GetWindowRect(s_textArea, &rect);
4574 if (pt.y < rect.top)
4575 {
4576 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004577 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004578 }
4579 }
4580 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4581 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004582 case WM_LBUTTONDBLCLK:
4583 {
4584 /*
4585 * If the user double clicked the tabline, create a new tab
4586 */
4587 if (gui_mch_showing_tabline())
4588 {
4589 POINT pt;
4590 RECT rect;
4591
4592 GetCursorPos((LPPOINT)&pt);
4593 GetWindowRect(s_textArea, &rect);
4594 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004595 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004596 }
4597 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4598 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004599#endif
4600
Bram Moolenaar734a8672019-12-02 22:49:38 +01004601 case WM_QUERYENDSESSION: // System wants to go down.
4602 gui_shell_closed(); // Will exit when no changed buffers.
4603 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004606 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004609 return 0L;
4610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004614 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4615 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004616 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return 0L;
4618
4619 case WM_SYSCHAR:
4620 /*
4621 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4622 * shortcut key, handle like a typed ALT key, otherwise call Windows
4623 * ALT key handling.
4624 */
4625#ifdef FEAT_MENU
4626 if ( !gui.menu_is_active
4627 || p_wak[0] == 'n'
4628 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4629 )
4630#endif
4631 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004632 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return 0L;
4634 }
4635#ifdef FEAT_MENU
4636 else
4637 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4638#endif
4639
4640 case WM_SYSKEYUP:
4641#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004642 // This used to be done only when menu is active: ALT key is used for
4643 // that. But that caused problems when menu is disabled and using
4644 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4645 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4647#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004648 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
4650
Bram Moolenaar734a8672019-12-02 22:49:38 +01004651 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 case WM_MOUSEWHEEL:
4655 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004656 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
Bram Moolenaar734a8672019-12-02 22:49:38 +01004658 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004659 case WM_SETTINGCHANGE:
4660 return _OnSettingChange((UINT)wParam);
4661
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004662#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 case WM_NOTIFY:
4664 switch (((LPNMHDR) lParam)->code)
4665 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004666 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004667 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004669 LPNMHDR hdr = (LPNMHDR)lParam;
4670 char_u *str = NULL;
4671 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004672
Bram Moolenaard23a8232018-02-10 18:45:26 +01004673 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004674
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004675# ifdef FEAT_GUI_TABLINE
4676 if (gui_mch_showing_tabline()
4677 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004678 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004679 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004680 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004681 * Mouse is over the GUI tabline. Display the
4682 * tooltip for the tab under the cursor
4683 *
4684 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004685 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004686 GetCursorPos(&pt);
4687 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004688 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004689 TCHITTESTINFO htinfo;
4690 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004691
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004692 /*
4693 * Get the tab under the cursor
4694 */
4695 htinfo.pt.x = pt.x;
4696 htinfo.pt.y = pt.y;
4697 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4698 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004699 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004700 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004701
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004702 tp = find_tabpage(idx + 1);
4703 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004704 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004705 get_tabline_label(tp, TRUE);
4706 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004707 }
4708 }
4709 }
4710 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004711# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004712# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004713# ifdef FEAT_GUI_TABLINE
4714 else
4715# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004717 UINT idButton;
4718 vimmenu_T *pMenu;
4719
4720 idButton = (UINT) hdr->idFrom;
4721 pMenu = gui_mswin_find_menu(root_menu, idButton);
4722 if (pMenu)
4723 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004725# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004726 if (str != NULL)
4727 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004728 if (hdr->code == TTN_GETDISPINFOW)
4729 {
4730 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4731
Bram Moolenaar734a8672019-12-02 22:49:38 +01004732 // Set the maximum width, this also enables using
4733 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004734 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4735 0, 500);
4736
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004737 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004738 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004739 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004740 }
4741 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004742 {
4743 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4744
Bram Moolenaar734a8672019-12-02 22:49:38 +01004745 // Set the maximum width, this also enables using
4746 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004747 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4748 0, 500);
4749
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004750 if (STRLEN(str) < sizeof(lpdi->szText)
4751 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004752 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004753 sizeof(lpdi->szText) - 1);
4754 else
4755 lpdi->lpszText = tt_text;
4756 }
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004760# ifdef FEAT_GUI_TABLINE
4761 case TCN_SELCHANGE:
4762 if (gui_mch_showing_tabline()
4763 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004764 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004765 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004766 return 0L;
4767 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004768 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004769
4770 case NM_RCLICK:
4771 if (gui_mch_showing_tabline()
4772 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004773 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004774 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004775 return 0L;
4776 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004777 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004780# ifdef FEAT_GUI_TABLINE
4781 if (gui_mch_showing_tabline()
4782 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4783 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 break;
4786 }
4787 break;
4788#endif
4789#if defined(MENUHINTS) && defined(FEAT_MENU)
4790 case WM_MENUSELECT:
4791 if (((UINT) HIWORD(wParam)
4792 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4793 == MF_HILITE
4794 && (State & CMDLINE) == 0)
4795 {
4796 UINT idButton;
4797 vimmenu_T *pMenu;
4798 static int did_menu_tip = FALSE;
4799
4800 if (did_menu_tip)
4801 {
4802 msg_clr_cmdline();
4803 setcursor();
4804 out_flush();
4805 did_menu_tip = FALSE;
4806 }
4807
4808 idButton = (UINT)LOWORD(wParam);
4809 pMenu = gui_mswin_find_menu(root_menu, idButton);
4810 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4811 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4812 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004813 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004814 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004815 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 setcursor();
4817 out_flush();
4818 did_menu_tip = TRUE;
4819 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004820 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822 break;
4823#endif
4824 case WM_NCHITTEST:
4825 {
4826 LRESULT result;
4827 int x, y;
4828 int xPos = GET_X_LPARAM(lParam);
4829
4830 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4831 if (result == HTCLIENT)
4832 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004833#ifdef FEAT_GUI_TABLINE
4834 if (gui_mch_showing_tabline())
4835 {
4836 int yPos = GET_Y_LPARAM(lParam);
4837 RECT rct;
4838
Bram Moolenaar734a8672019-12-02 22:49:38 +01004839 // If the cursor is on the GUI tabline, don't process this
4840 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004841 GetWindowRect(s_textArea, &rct);
4842 if (yPos < rct.top)
4843 return result;
4844 }
4845#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004846 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 xPos -= x;
4848
Bram Moolenaar734a8672019-12-02 22:49:38 +01004849 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 return HTBOTTOMLEFT;
4851 else
4852 return HTBOTTOMRIGHT;
4853 }
4854 else
4855 return result;
4856 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004857 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859#ifdef FEAT_MBYTE_IME
4860 case WM_IME_NOTIFY:
4861 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4862 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004863 return 1L;
4864
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 case WM_IME_COMPOSITION:
4866 if (!_OnImeComposition(hwnd, wParam, lParam))
4867 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004868 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#endif
4870
4871 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004873 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875#endif
4876 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4877 }
4878
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004879 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880}
4881
4882/*
4883 * End of call-back routines
4884 */
4885
Bram Moolenaar734a8672019-12-02 22:49:38 +01004886// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887HWND vim_parent_hwnd = NULL;
4888
4889 static BOOL CALLBACK
4890FindWindowTitle(HWND hwnd, LPARAM lParam)
4891{
4892 char buf[2048];
4893 char *title = (char *)lParam;
4894
4895 if (GetWindowText(hwnd, buf, sizeof(buf)))
4896 {
4897 if (strstr(buf, title) != NULL)
4898 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004899 // Found it. Store the window ref. and quit searching if MDI
4900 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004902 if (vim_parent_hwnd != NULL)
4903 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004906 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907}
4908
4909/*
4910 * Invoked for '-P "title"' argument: search for parent application to open
4911 * our window in.
4912 */
4913 void
4914gui_mch_set_parent(char *title)
4915{
4916 EnumWindows(FindWindowTitle, (LPARAM)title);
4917 if (vim_parent_hwnd == NULL)
4918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004919 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 mch_exit(2);
4921 }
4922}
4923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004924#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 static void
4926ole_error(char *arg)
4927{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004928 char buf[IOSIZE];
4929
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004930# ifdef VIMDLL
4931 gui.in_use = mch_is_gui_executable();
4932# endif
4933
Bram Moolenaar734a8672019-12-02 22:49:38 +01004934 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004935 vim_snprintf(buf, IOSIZE,
4936 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4937 arg);
4938 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004942#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4943 static char *
4944gvim_error(void)
4945{
4946 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4947
4948 if (starting)
4949 {
4950 mch_errmsg(msg);
4951 mch_errmsg("\n");
4952 mch_exit(2);
4953 }
4954 return msg;
4955}
4956
4957 char *
4958gui_mch_do_spawn(char_u *arg)
4959{
4960 int len;
4961# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4962 char_u *session = NULL;
4963 LPWSTR tofree1 = NULL;
4964# endif
4965 WCHAR name[MAX_PATH];
4966 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4967 STARTUPINFOW si = {sizeof(si)};
4968 PROCESS_INFORMATION pi;
4969
4970 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4971 goto error;
4972 p = wcsrchr(name, L'\\');
4973 if (p == NULL)
4974 goto error;
4975 // Replace the executable name from vim(d).exe to gvim(d).exe.
4976# ifdef DEBUG
4977 wcscpy(p + 1, L"gvimd.exe");
4978# else
4979 wcscpy(p + 1, L"gvim.exe");
4980# endif
4981
4982# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4983 if (starting)
4984# endif
4985 {
4986 // Pass the command line to the new process.
4987 p = GetCommandLineW();
4988 // Skip 1st argument.
4989 while (*p && *p != L' ' && *p != L'\t')
4990 {
4991 if (*p == L'"')
4992 {
4993 while (*p && *p != L'"')
4994 ++p;
4995 if (*p)
4996 ++p;
4997 }
4998 else
4999 ++p;
5000 }
5001 cmd = p;
5002 }
5003# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5004 else
5005 {
5006 // Create a session file and pass it to the new process.
5007 LPWSTR wsession;
5008 char_u *savebg;
5009 int ret;
5010
5011 session = vim_tempname('s', FALSE);
5012 if (session == NULL)
5013 goto error;
5014 savebg = p_bg;
5015 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5016 ret = write_session_file(session);
5017 vim_free(p_bg);
5018 p_bg = savebg;
5019 if (!ret)
5020 goto error;
5021 wsession = enc_to_utf16(session, NULL);
5022 if (wsession == NULL)
5023 goto error;
5024 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005025 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005026 if (cmd == NULL)
5027 {
5028 vim_free(wsession);
5029 goto error;
5030 }
5031 tofree1 = cmd;
5032 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5033 wsession, wsession);
5034 vim_free(wsession);
5035 }
5036# endif
5037
5038 // Check additional arguments to the `:gui` command.
5039 if (arg != NULL)
5040 {
5041 warg = enc_to_utf16(arg, NULL);
5042 if (warg == NULL)
5043 goto error;
5044 tofree2 = warg;
5045 }
5046 else
5047 warg = L"";
5048
5049 // Set up the new command line.
5050 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005051 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005052 if (newcmd == NULL)
5053 goto error;
5054 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5055
5056 // Spawn a new GUI process.
5057 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5058 NULL, NULL, &si, &pi))
5059 goto error;
5060 CloseHandle(pi.hProcess);
5061 CloseHandle(pi.hThread);
5062 mch_exit(0);
5063
5064error:
5065# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5066 if (session)
5067 mch_remove(session);
5068 vim_free(session);
5069 vim_free(tofree1);
5070# endif
5071 vim_free(newcmd);
5072 vim_free(tofree2);
5073 return gvim_error();
5074}
5075#endif
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077/*
5078 * Parse the GUI related command-line arguments. Any arguments used are
5079 * deleted from argv, and *argc is decremented accordingly. This is called
5080 * when vim is started, whether or not the GUI has been started.
5081 */
5082 void
5083gui_mch_prepare(int *argc, char **argv)
5084{
5085 int silent = FALSE;
5086 int idx;
5087
Bram Moolenaar734a8672019-12-02 22:49:38 +01005088 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5090 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005091 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5093 && (argv[2][0] == '-' || argv[2][0] == '/'))
5094 {
5095 silent = TRUE;
5096 idx = 2;
5097 }
5098 else
5099 idx = 1;
5100
Bram Moolenaar734a8672019-12-02 22:49:38 +01005101 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (STRICMP(argv[idx] + 1, "register") == 0)
5103 {
5104#ifdef FEAT_OLE
5105 RegisterMe(silent);
5106 mch_exit(0);
5107#else
5108 if (!silent)
5109 ole_error("register");
5110 mch_exit(2);
5111#endif
5112 }
5113
Bram Moolenaar734a8672019-12-02 22:49:38 +01005114 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5116 {
5117#ifdef FEAT_OLE
5118 UnregisterMe(!silent);
5119 mch_exit(0);
5120#else
5121 if (!silent)
5122 ole_error("unregister");
5123 mch_exit(2);
5124#endif
5125 }
5126
Bram Moolenaar734a8672019-12-02 22:49:38 +01005127 // Ignore an -embedding argument. It is only relevant if the
5128 // application wants to treat the case when it is started manually
5129 // differently from the case where it is started via automation (and
5130 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5132 {
5133#ifdef FEAT_OLE
5134 *argc = 1;
5135#else
5136 ole_error("embedding");
5137 mch_exit(2);
5138#endif
5139 }
5140 }
5141
5142#ifdef FEAT_OLE
5143 {
5144 int bDoRestart = FALSE;
5145
5146 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005147 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (bDoRestart)
5149 mch_exit(0);
5150 }
5151#endif
5152
5153#ifdef FEAT_NETBEANS_INTG
5154 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 int arg;
5157
5158 for (arg = 1; arg < *argc; arg++)
5159 if (strncmp("-nb", argv[arg], 3) == 0)
5160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 netbeansArg = argv[arg];
5162 mch_memmove(&argv[arg], &argv[arg + 1],
5163 (--*argc - arg) * sizeof(char *));
5164 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005165 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169}
5170
5171/*
5172 * Initialise the GUI. Create all the windows, set up all the call-backs
5173 * etc.
5174 */
5175 int
5176gui_mch_init(void)
5177{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005179 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef GLOBAL_IME
5182 ATOM atom;
5183#endif
5184
Bram Moolenaar734a8672019-12-02 22:49:38 +01005185 // Return here if the window was already opened (happens when
5186 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005188 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 /*
5191 * Load the tearoff bitmap
5192 */
5193#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005194 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#endif
5196
5197 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5198 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5199#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005200 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202 gui.border_width = 0;
5203
5204 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5205
Bram Moolenaar734a8672019-12-02 22:49:38 +01005206 // First try using the wide version, so that we can use any title.
5207 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005208 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005210 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 wndclassw.lpfnWndProc = _WndProc;
5212 wndclassw.cbClsExtra = 0;
5213 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005214 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5216 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5217 wndclassw.hbrBackground = s_brush;
5218 wndclassw.lpszMenuName = NULL;
5219 wndclassw.lpszClassName = szVimWndClassW;
5220
5221 if ((
5222#ifdef GLOBAL_IME
5223 atom =
5224#endif
5225 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005226 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (vim_parent_hwnd != NULL)
5230 {
5231#ifdef HAVE_TRY_EXCEPT
5232 __try
5233 {
5234#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005235 // Open inside the specified parent window.
5236 // TODO: last argument should point to a CLIENTCREATESTRUCT
5237 // structure.
5238 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005240 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005241 WS_OVERLAPPEDWINDOW | WS_CHILD
5242 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5244 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005245 100, // Any value will do
5246 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005248 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#ifdef HAVE_TRY_EXCEPT
5250 }
5251 __except(EXCEPTION_EXECUTE_HANDLER)
5252 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255#endif
5256 if (s_hwnd == NULL)
5257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005258 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 mch_exit(2);
5260 }
5261 }
5262 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005263 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005264 // If the provided windowid is not valid reset it to zero, so that it
5265 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005266 if (IsWindow((HWND)win_socket_id) <= 0)
5267 win_socket_id = 0;
5268
Bram Moolenaar734a8672019-12-02 22:49:38 +01005269 // Create a window. If win_socket_id is not zero without border and
5270 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005271 s_hwnd = CreateWindowW(
5272 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005273 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5274 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005275 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5276 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005277 100, // Any value will do
5278 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005279 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005280 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005281 if (s_hwnd != NULL && win_socket_id != 0)
5282 {
5283 SetParent(s_hwnd, (HWND)win_socket_id);
5284 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5285 }
5286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287
5288 if (s_hwnd == NULL)
5289 return FAIL;
5290
5291#ifdef GLOBAL_IME
5292 global_ime_init(atom, s_hwnd);
5293#endif
5294#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5295 dyn_imm_load();
5296#endif
5297
Bram Moolenaar734a8672019-12-02 22:49:38 +01005298 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005299 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005300 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005301 wndclassw.style = CS_OWNDC;
5302 wndclassw.lpfnWndProc = _TextAreaWndProc;
5303 wndclassw.cbClsExtra = 0;
5304 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005305 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005306 wndclassw.hIcon = NULL;
5307 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5308 wndclassw.hbrBackground = NULL;
5309 wndclassw.lpszMenuName = NULL;
5310 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005311
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005312 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 return FAIL;
5314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005316 s_textArea = CreateWindowExW(
5317 0,
5318 szTextAreaClassW, L"Vim text area",
5319 WS_CHILD | WS_VISIBLE, 0, 0,
5320 100, // Any value will do for now
5321 100, // Any value will do for now
5322 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005323 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 if (s_textArea == NULL)
5326 return FAIL;
5327
Bram Moolenaar20321902016-02-17 12:30:17 +01005328#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005329 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005330 {
5331 HANDLE hIcon = NULL;
5332
5333 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005334 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005335 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005336#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#ifdef FEAT_MENU
5339 s_menuBar = CreateMenu();
5340#endif
5341 s_hdc = GetDC(s_textArea);
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaar734a8672019-12-02 22:49:38 +01005345 // Do we need to bother with this?
5346 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
Bram Moolenaar734a8672019-12-02 22:49:38 +01005348 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 gui_mch_def_colors();
5350
Bram Moolenaar734a8672019-12-02 22:49:38 +01005351 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5352 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 set_normal_colors();
5354
5355 /*
5356 * Check that none of the colors are the same as the background color.
5357 * Then store the current values as the defaults.
5358 */
5359 gui_check_colors();
5360 gui.def_norm_pixel = gui.norm_pixel;
5361 gui.def_back_pixel = gui.back_pixel;
5362
Bram Moolenaar734a8672019-12-02 22:49:38 +01005363 // Get the colors for the highlight groups (gui_check_colors() might have
5364 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 highlight_gui_started();
5366
5367 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005368 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005370 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /*
5373 * Set up for Intellimouse processing
5374 */
5375 init_mouse_wheel();
5376
5377 /*
5378 * compute a couple of metrics used for the dialogs
5379 */
5380 get_dialog_font_metrics();
5381#ifdef FEAT_TOOLBAR
5382 /*
5383 * Create the toolbar
5384 */
5385 initialise_toolbar();
5386#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005387#ifdef FEAT_GUI_TABLINE
5388 /*
5389 * Create the tabline
5390 */
5391 initialise_tabline();
5392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef MSWIN_FIND_REPLACE
5394 /*
5395 * Initialise the dialog box stuff
5396 */
5397 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5398
Bram Moolenaar734a8672019-12-02 22:49:38 +01005399 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005401 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005403 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5405 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5406 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005409#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005410# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005411// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005412# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005413# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005414# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005415# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005416 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005417 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005418#endif
5419
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005420#ifdef FEAT_RENDER_OPTIONS
5421 if (p_rop)
5422 (void)gui_mch_set_rendering_options(p_rop);
5423#endif
5424
Bram Moolenaar748bf032005-02-02 23:04:36 +00005425theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005426 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005427 display_errors();
5428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 return OK;
5430}
5431
5432/*
5433 * Get the size of the screen, taking position on multiple monitors into
5434 * account (if supported).
5435 */
5436 static void
5437get_work_area(RECT *spi_rect)
5438{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005439 HMONITOR mon;
5440 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
Bram Moolenaar734a8672019-12-02 22:49:38 +01005442 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005443 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005444 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005446 moninfo.cbSize = sizeof(MONITORINFO);
5447 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005449 *spi_rect = moninfo.rcWork;
5450 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005453 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5455}
5456
5457/*
5458 * Set the size of the window to the given width and height in pixels.
5459 */
5460 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005461gui_mch_set_shellsize(
5462 int width,
5463 int height,
5464 int min_width UNUSED,
5465 int min_height UNUSED,
5466 int base_width UNUSED,
5467 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005468 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005471 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaar734a8672019-12-02 22:49:38 +01005474 // Try to keep window completely on screen.
5475 // Get position of the screen work area. This is the part that is not
5476 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 get_work_area(&workarea_rect);
5478
Bram Moolenaar734a8672019-12-02 22:49:38 +01005479 // Resizing a maximized window looks very strange, unzoom it first.
5480 // But don't do it when still starting up, it may have been requested in
5481 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005482 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005484
5485 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar734a8672019-12-02 22:49:38 +01005487 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005488 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005489 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005490 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005491 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 + GetSystemMetrics(SM_CYCAPTION)
5493#ifdef FEAT_MENU
5494 + gui_mswin_get_menu_height(FALSE)
5495#endif
5496 ;
5497
Bram Moolenaar734a8672019-12-02 22:49:38 +01005498 // The following should take care of keeping Vim on the same monitor, no
5499 // matter if the secondary monitor is left or right of the primary
5500 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005501 window_rect.right = window_rect.left + win_width;
5502 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005503
Bram Moolenaar734a8672019-12-02 22:49:38 +01005504 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005505 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5506 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005508 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5509 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005511 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5512 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005514 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5515 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005517 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5518 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 SetActiveWindow(s_hwnd);
5521 SetFocus(s_hwnd);
5522
5523#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005524 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 gui_mswin_get_menu_height(!gui.starting);
5526#endif
5527}
5528
5529
5530 void
5531gui_mch_set_scrollbar_thumb(
5532 scrollbar_T *sb,
5533 long val,
5534 long size,
5535 long max)
5536{
5537 SCROLLINFO info;
5538
5539 sb->scroll_shift = 0;
5540 while (max > 32767)
5541 {
5542 max = (max + 1) >> 1;
5543 val >>= 1;
5544 size >>= 1;
5545 ++sb->scroll_shift;
5546 }
5547
5548 if (sb->scroll_shift > 0)
5549 ++size;
5550
5551 info.cbSize = sizeof(info);
5552 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5553 info.nPos = val;
5554 info.nMin = 0;
5555 info.nMax = max;
5556 info.nPage = size;
5557 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5558}
5559
5560
5561/*
5562 * Set the current text font.
5563 */
5564 void
5565gui_mch_set_font(GuiFont font)
5566{
5567 gui.currFont = font;
5568}
5569
5570
5571/*
5572 * Set the current text foreground color.
5573 */
5574 void
5575gui_mch_set_fg_color(guicolor_T color)
5576{
5577 gui.currFgColor = color;
5578}
5579
5580/*
5581 * Set the current text background color.
5582 */
5583 void
5584gui_mch_set_bg_color(guicolor_T color)
5585{
5586 gui.currBgColor = color;
5587}
5588
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005589/*
5590 * Set the current text special color.
5591 */
5592 void
5593gui_mch_set_sp_color(guicolor_T color)
5594{
5595 gui.currSpColor = color;
5596}
5597
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005598#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599/*
5600 * Multi-byte handling, originally by Sung-Hoon Baek.
5601 * First static functions (no prototypes generated).
5602 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005603# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005604# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005605# endif
5606# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 * handle WM_IME_NOTIFY message
5610 */
5611 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005612_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
5614 LRESULT lResult = 0;
5615 HIMC hImc;
5616
5617 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5618 return lResult;
5619 switch (dwCommand)
5620 {
5621 case IMN_SETOPENSTATUS:
5622 if (pImmGetOpenStatus(hImc))
5623 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005624 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 im_set_position(gui.row, gui.col);
5626
Bram Moolenaar734a8672019-12-02 22:49:38 +01005627 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 State &= ~LANGMAP;
5629 if (State & INSERT)
5630 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005631# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005632 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5634 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005635 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 int old_row = gui.row;
5637 int old_col = gui.col;
5638
5639 // This must be called here before
5640 // status_redraw_curbuf(), otherwise the mode
5641 // message may appear in the wrong position.
5642 showmode();
5643 status_redraw_curbuf();
5644 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005645 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 gui.row = old_row;
5647 gui.col = old_col;
5648 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 }
5652 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005653 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 lResult = 0;
5655 break;
5656 }
5657 pImmReleaseContext(hWnd, hImc);
5658 return lResult;
5659}
5660
5661 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005662_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 char_u *ret;
5665 int len;
5666
Bram Moolenaar734a8672019-12-02 22:49:38 +01005667 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 return 0;
5669
5670 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5671 if (ret != NULL)
5672 {
5673 add_to_input_buf_csi(ret, len);
5674 vim_free(ret);
5675 return 1;
5676 }
5677 return 0;
5678}
5679
5680/*
5681 * get the current composition string, in UCS-2; *lenp is the number of
5682 * *lenp is the number of Unicode characters.
5683 */
5684 static short_u *
5685GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5686{
5687 LONG ret;
5688 LPWSTR wbuf = NULL;
5689 char_u *buf;
5690
5691 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005692 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
Bram Moolenaar734a8672019-12-02 22:49:38 +01005694 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5696 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005697 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 if (ret > 0)
5700 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005701 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005702 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 if (wbuf != NULL)
5704 {
5705 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5706 *lenp = ret / sizeof(WCHAR);
5707 }
5708 return (short_u *)wbuf;
5709 }
5710
Bram Moolenaar734a8672019-12-02 22:49:38 +01005711 // ret < 0; we got an error, so try the ANSI version. This'll work
5712 // on 9x/ME, but only if the codepage happens to be set to whatever
5713 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5715 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005716 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 buf = alloc(ret);
5719 if (buf == NULL)
5720 return NULL;
5721 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5722
Bram Moolenaar734a8672019-12-02 22:49:38 +01005723 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005724 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 vim_free(buf);
5726
5727 return (short_u *)wbuf;
5728}
5729
5730/*
5731 * void GetResultStr()
5732 *
5733 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5734 * get complete composition string
5735 */
5736 static char_u *
5737GetResultStr(HWND hwnd, int GCS, int *lenp)
5738{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005739 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 short_u *buf = NULL;
5741 char_u *convbuf = NULL;
5742
5743 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5744 return NULL;
5745
Bram Moolenaar734a8672019-12-02 22:49:38 +01005746 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5748 if (buf == NULL)
5749 return NULL;
5750
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005751 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 pImmReleaseContext(hwnd, hIMC);
5753 vim_free(buf);
5754 return convbuf;
5755}
5756#endif
5757
Bram Moolenaar734a8672019-12-02 22:49:38 +01005758// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005759#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761/*
5762 * set font to IM.
5763 */
5764 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005765im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 HIMC hImc;
5768
5769 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5770 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005771 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 pImmReleaseContext(s_hwnd, hImc);
5773 }
5774}
5775
5776/*
5777 * Notify cursor position to IM.
5778 */
5779 void
5780im_set_position(int row, int col)
5781{
5782 HIMC hImc;
5783
5784 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5785 {
5786 COMPOSITIONFORM cfs;
5787
5788 cfs.dwStyle = CFS_POINT;
5789 cfs.ptCurrentPos.x = FILL_X(col);
5790 cfs.ptCurrentPos.y = FILL_Y(row);
5791 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5792 pImmSetCompositionWindow(hImc, &cfs);
5793
5794 pImmReleaseContext(s_hwnd, hImc);
5795 }
5796}
5797
5798/*
5799 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5800 */
5801 void
5802im_set_active(int active)
5803{
5804 HIMC hImc;
5805 static HIMC hImcOld = (HIMC)0;
5806
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005807# ifdef VIMDLL
5808 if (!gui.in_use && !gui.starting)
5809 {
5810 mbyte_im_set_active(active);
5811 return;
5812 }
5813# endif
5814
Bram Moolenaar734a8672019-12-02 22:49:38 +01005815 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
5817 if (p_imdisable)
5818 {
5819 if (hImcOld == (HIMC)0)
5820 {
5821 hImcOld = pImmGetContext(s_hwnd);
5822 if (hImcOld)
5823 pImmAssociateContext(s_hwnd, (HIMC)0);
5824 }
5825 active = FALSE;
5826 }
5827 else if (hImcOld != (HIMC)0)
5828 {
5829 pImmAssociateContext(s_hwnd, hImcOld);
5830 hImcOld = (HIMC)0;
5831 }
5832
5833 hImc = pImmGetContext(s_hwnd);
5834 if (hImc)
5835 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005836 /*
5837 * for Korean ime
5838 */
5839 HKL hKL = GetKeyboardLayout(0);
5840
5841 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5842 {
5843 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5844 static BOOL bSaved = FALSE;
5845
5846 if (active)
5847 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005848 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005849 if (bSaved)
5850 pImmSetConversionStatus(hImc, dwConversionSaved,
5851 dwSentenceSaved);
5852 bSaved = FALSE;
5853 }
5854 else
5855 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005856 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005857 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5858 &dwSentenceSaved))
5859 {
5860 bSaved = TRUE;
5861 pImmSetConversionStatus(hImc,
5862 dwConversionSaved & ~(IME_CMODE_NATIVE
5863 | IME_CMODE_FULLSHAPE),
5864 dwSentenceSaved);
5865 }
5866 }
5867 }
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 pImmSetOpenStatus(hImc, active);
5870 pImmReleaseContext(s_hwnd, hImc);
5871 }
5872 }
5873}
5874
5875/*
5876 * Get IM status. When IM is on, return not 0. Else return 0.
5877 */
5878 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005879im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 int status = 0;
5882 HIMC hImc;
5883
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005884# ifdef VIMDLL
5885 if (!gui.in_use && !gui.starting)
5886 return mbyte_im_get_status();
5887# endif
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5890 {
5891 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5892 pImmReleaseContext(s_hwnd, hImc);
5893 }
5894 return status;
5895}
5896
Bram Moolenaar734a8672019-12-02 22:49:38 +01005897#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005899#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005900// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902/*
5903 * Notify cursor position to IM.
5904 */
5905 void
5906im_set_position(int row, int col)
5907{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005908 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 POINT p;
5910
5911 p.x = FILL_X(col);
5912 p.y = FILL_Y(row);
5913 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5914 global_ime_set_position(&p);
5915}
5916
5917/*
5918 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5919 */
5920 void
5921im_set_active(int active)
5922{
5923 global_ime_set_status(active);
5924}
5925
5926/*
5927 * Get IM status. When IM is on, return not 0. Else return 0.
5928 */
5929 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005930im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 return global_ime_get_status();
5933}
5934#endif
5935
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005936/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005937 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005938 */
5939 static void
5940latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5941{
5942 int c;
5943
Bram Moolenaarca003e12006-03-17 23:19:38 +00005944 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005945 {
5946 c = *text++;
5947 switch (c)
5948 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005949 case 0xa4: c = 0x20ac; break; // euro
5950 case 0xa6: c = 0x0160; break; // S hat
5951 case 0xa8: c = 0x0161; break; // S -hat
5952 case 0xb4: c = 0x017d; break; // Z hat
5953 case 0xb8: c = 0x017e; break; // Z -hat
5954 case 0xbc: c = 0x0152; break; // OE
5955 case 0xbd: c = 0x0153; break; // oe
5956 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005957 }
5958 *unicodebuf++ = c;
5959 }
5960}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
5962#ifdef FEAT_RIGHTLEFT
5963/*
5964 * What is this for? In the case where you are using Win98 or Win2K or later,
5965 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5966 * reverses the string sent to the TextOut... family. This sucks, because we
5967 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5968 * way to tell Windblows not to do this!
5969 *
5970 * The short of it is that this 'RevOut' only gets called if you are running
5971 * one of the new, "improved" MS OSes, and only if you are running in
5972 * 'rightleft' mode. It makes display take *slightly* longer, but not
5973 * noticeably so.
5974 */
5975 static void
5976RevOut( HDC s_hdc,
5977 int col,
5978 int row,
5979 UINT foptions,
5980 CONST RECT *pcliprect,
5981 LPCTSTR text,
5982 UINT len,
5983 CONST INT *padding)
5984{
5985 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005987 for (ix = 0; ix < (int)len; ++ix)
5988 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5989 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990}
5991#endif
5992
Bram Moolenaar92467d32017-12-05 13:22:16 +01005993 static void
5994draw_line(
5995 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005996 int y1,
5997 int x2,
5998 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005999 COLORREF color)
6000{
6001#if defined(FEAT_DIRECTX)
6002 if (IS_ENABLE_DIRECTX())
6003 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6004 else
6005#endif
6006 {
6007 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6008 HPEN old_pen = SelectObject(s_hdc, hpen);
6009 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006010 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006011 LineTo(s_hdc, x2, y2);
6012 DeleteObject(SelectObject(s_hdc, old_pen));
6013 }
6014}
6015
6016 static void
6017set_pixel(
6018 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006019 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006020 COLORREF color)
6021{
6022#if defined(FEAT_DIRECTX)
6023 if (IS_ENABLE_DIRECTX())
6024 DWriteContext_SetPixel(s_dwc, x, y, color);
6025 else
6026#endif
6027 SetPixel(s_hdc, x, y, color);
6028}
6029
6030 static void
6031fill_rect(
6032 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006033 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006034 COLORREF color)
6035{
6036#if defined(FEAT_DIRECTX)
6037 if (IS_ENABLE_DIRECTX())
6038 DWriteContext_FillRect(s_dwc, rcp, color);
6039 else
6040#endif
6041 {
6042 HBRUSH hbr2;
6043
6044 if (hbr == NULL)
6045 hbr2 = CreateSolidBrush(color);
6046 else
6047 hbr2 = hbr;
6048 FillRect(s_hdc, rcp, hbr2);
6049 if (hbr == NULL)
6050 DeleteBrush(hbr2);
6051 }
6052}
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 void
6055gui_mch_draw_string(
6056 int row,
6057 int col,
6058 char_u *text,
6059 int len,
6060 int flags)
6061{
6062 static int *padding = NULL;
6063 static int pad_size = 0;
6064 int i;
6065 const RECT *pcliprect = NULL;
6066 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 static WCHAR *unicodebuf = NULL;
6068 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006069 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int y;
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /*
6074 * Italic and bold text seems to have an extra row of pixels at the bottom
6075 * (below where the bottom of the character should be). If we draw the
6076 * characters with a solid background, the top row of pixels in the
6077 * character below will be overwritten. We can fix this by filling in the
6078 * background ourselves, to the correct character proportions, and then
6079 * writing the character in transparent mode. Still have a problem when
6080 * the character is "_", which gets written on to the character below.
6081 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6082 * pixel in their slots, which fixes the problem with the bottom row of
6083 * pixels. We still need this code because otherwise the top row of pixels
6084 * becomes a problem. - webb.
6085 */
6086 static HBRUSH hbr_cache[2] = {NULL, NULL};
6087 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6088 static int brush_lru = 0;
6089 HBRUSH hbr;
6090 RECT rc;
6091
6092 if (!(flags & DRAW_TRANSP))
6093 {
6094 /*
6095 * Clear background first.
6096 * Note: FillRect() excludes right and bottom of rectangle.
6097 */
6098 rc.left = FILL_X(col);
6099 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (has_mbyte)
6101 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006102 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006103 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 rc.right = FILL_X(col + len);
6107 rc.bottom = FILL_Y(row + 1);
6108
Bram Moolenaar734a8672019-12-02 22:49:38 +01006109 // Cache the created brush, that saves a lot of time. We need two:
6110 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (gui.currBgColor == brush_color[0])
6112 {
6113 hbr = hbr_cache[0];
6114 brush_lru = 1;
6115 }
6116 else if (gui.currBgColor == brush_color[1])
6117 {
6118 hbr = hbr_cache[1];
6119 brush_lru = 0;
6120 }
6121 else
6122 {
6123 if (hbr_cache[brush_lru] != NULL)
6124 DeleteBrush(hbr_cache[brush_lru]);
6125 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6126 brush_color[brush_lru] = gui.currBgColor;
6127 hbr = hbr_cache[brush_lru];
6128 brush_lru = !brush_lru;
6129 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006130
Bram Moolenaar92467d32017-12-05 13:22:16 +01006131 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 SetBkMode(s_hdc, TRANSPARENT);
6134
6135 /*
6136 * When drawing block cursor, prevent inverted character spilling
6137 * over character cell (can happen with bold/italic)
6138 */
6139 if (flags & DRAW_CURSOR)
6140 {
6141 pcliprect = &rc;
6142 foptions = ETO_CLIPPED;
6143 }
6144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 SetTextColor(s_hdc, gui.currFgColor);
6146 SelectFont(s_hdc, gui.currFont);
6147
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006148#ifdef FEAT_DIRECTX
6149 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006150 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006151#endif
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6154 {
6155 vim_free(padding);
6156 pad_size = Columns;
6157
Bram Moolenaar734a8672019-12-02 22:49:38 +01006158 // Don't give an out-of-memory message here, it would call us
6159 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006160 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (padding != NULL)
6162 for (i = 0; i < pad_size; i++)
6163 padding[i] = gui.char_width;
6164 }
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 /*
6167 * We have to provide the padding argument because italic and bold versions
6168 * of fixed-width fonts are often one pixel or so wider than their normal
6169 * versions.
6170 * No check for DRAW_BOLD, Windows will have done it already.
6171 */
6172
Bram Moolenaar734a8672019-12-02 22:49:38 +01006173 // Check if there are any UTF-8 characters. If not, use normal text
6174 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 if (enc_utf8)
6176 for (n = 0; n < len; ++n)
6177 if (text[n] >= 0x80)
6178 break;
6179
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006180#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006181 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6182 // required that unicode drawing routine, currently. So this forces it
6183 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006184 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006185 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006186#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006187
Bram Moolenaar734a8672019-12-02 22:49:38 +01006188 // Check if the Unicode buffer exists and is big enough. Create it
6189 // with the same length as the multi-byte string, the number of wide
6190 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006191 if ((enc_utf8
6192 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6193 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 && (unicodebuf == NULL || len > unibuflen))
6195 {
6196 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006197 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006200 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 unibuflen = len;
6203 }
6204
6205 if (enc_utf8 && n < len && unicodebuf != NULL)
6206 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006207 // Output UTF-8 characters. Composing characters should be
6208 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006209 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006210 int wlen; // string length in words
6211 int clen; // string length in characters
6212 int cells; // cell width of string up to composing char
6213 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006214 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006216 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006217 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006219 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006221 c = utf_ptr2char(text + i);
6222 if (c >= 0x10000)
6223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006225 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6226 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006227 }
6228 else
6229 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006230 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006231 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006232
6233 if (utf_iscomposing(c))
6234 cw = 0;
6235 else
6236 {
6237 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006238 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006239 cw = 1;
6240 }
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 if (unicodepdy != NULL)
6243 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006244 // Use unicodepdy to make characters fit as we expect, even
6245 // when the font uses different widths (e.g., bold character
6246 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006247 if (c >= 0x10000)
6248 {
6249 unicodepdy[wlen - 2] = cw * gui.char_width;
6250 unicodepdy[wlen - 1] = 0;
6251 }
6252 else
6253 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006256 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006257 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006259#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006260 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006261 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006262 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006263 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006264 TEXT_X(col), TEXT_Y(row),
6265 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006266 gui.char_width, gui.currFgColor,
6267 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006268 }
6269 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006270#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006271 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6272 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006273 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006275 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006277 // If we want to display codepage data, and the current CP is not the
6278 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (unicodebuf != NULL)
6280 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006281 if (enc_latin9)
6282 latin9_to_ucs(text, len, unicodebuf);
6283 else
6284 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 MB_PRECOMPOSED,
6286 (char *)text, len,
6287 (LPWSTR)unicodebuf, unibuflen);
6288 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006289 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006290 // Use unicodepdy to make characters fit as we expect, even
6291 // when the font uses different widths (e.g., bold character
6292 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006293 if (unicodepdy != NULL)
6294 {
6295 int i;
6296 int cw;
6297
6298 for (i = 0; i < len; ++i)
6299 {
6300 cw = utf_char2cells(unicodebuf[i]);
6301 if (cw > 2)
6302 cw = 1;
6303 unicodepdy[i] = cw * gui.char_width;
6304 }
6305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006307 foptions, pcliprect, unicodebuf, len, unicodepdy);
6308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
6310 }
6311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
6313#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006314 // Windows will mess up RL text, so we have to draw it character by
6315 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006316 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6318 foptions, pcliprect, (char *)text, len, padding);
6319 else
6320#endif
6321 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6322 foptions, pcliprect, (char *)text, len, padding);
6323 }
6324
Bram Moolenaar734a8672019-12-02 22:49:38 +01006325 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (flags & DRAW_UNDERL)
6327 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006328 // When p_linespace is 0, overwrite the bottom row of pixels.
6329 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 if (p_linespace > 1)
6332 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006333 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006335
Bram Moolenaar734a8672019-12-02 22:49:38 +01006336 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006337 if (flags & DRAW_STRIKE)
6338 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006339 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006340 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006341 }
6342
Bram Moolenaar734a8672019-12-02 22:49:38 +01006343 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006344 if (flags & DRAW_UNDERC)
6345 {
6346 int x;
6347 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006348 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006349
6350 y = FILL_Y(row + 1) - 1;
6351 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6352 {
6353 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006354 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006355 }
6356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
6359
6360/*
6361 * Output routines.
6362 */
6363
Bram Moolenaar734a8672019-12-02 22:49:38 +01006364/*
6365 * Flush any output to the screen
6366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 void
6368gui_mch_flush(void)
6369{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006370#if defined(FEAT_DIRECTX)
6371 if (IS_ENABLE_DIRECTX())
6372 DWriteContext_Flush(s_dwc);
6373#endif
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 GdiFlush();
6376}
6377
6378 static void
6379clear_rect(RECT *rcp)
6380{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006381 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382}
6383
6384
Bram Moolenaarc716c302006-01-21 22:12:51 +00006385 void
6386gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6387{
6388 RECT workarea_rect;
6389
6390 get_work_area(&workarea_rect);
6391
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006392 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006393 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006394 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006395
Bram Moolenaar734a8672019-12-02 22:49:38 +01006396 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6397 // the menubar for MSwin, we subtract it from the screen height, so that
6398 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006399 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006400 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006401 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006402 - GetSystemMetrics(SM_CYCAPTION)
6403#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006404 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006405#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006406 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006407}
6408
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410#if defined(FEAT_MENU) || defined(PROTO)
6411/*
6412 * Add a sub menu to the menu bar.
6413 */
6414 void
6415gui_mch_add_menu(
6416 vimmenu_T *menu,
6417 int pos)
6418{
6419 vimmenu_T *parent = menu->parent;
6420
6421 menu->submenu_id = CreatePopupMenu();
6422 menu->id = s_menu_id++;
6423
6424 if (menu_is_menubar(menu->name))
6425 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006426 WCHAR *wn;
6427 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006429 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006430 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006431 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006433 infow.cbSize = sizeof(infow);
6434 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6435 | MIIM_SUBMENU;
6436 infow.dwItemData = (long_u)menu;
6437 infow.wID = menu->id;
6438 infow.fType = MFT_STRING;
6439 infow.dwTypeData = wn;
6440 infow.cch = (UINT)wcslen(wn);
6441 infow.hSubMenu = menu->submenu_id;
6442 InsertMenuItemW((parent == NULL)
6443 ? s_menuBar : parent->submenu_id,
6444 (UINT)pos, TRUE, &infow);
6445 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 }
6447
Bram Moolenaar734a8672019-12-02 22:49:38 +01006448 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 if (parent == NULL)
6450 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006451# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 else if (IsWindow(parent->tearoff_handle))
6453 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455}
6456
6457 void
6458gui_mch_show_popupmenu(vimmenu_T *menu)
6459{
6460 POINT mp;
6461
6462 (void)GetCursorPos((LPPOINT)&mp);
6463 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6464}
6465
6466 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006467gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 vimmenu_T *menu = gui_find_menu(path_name);
6470
6471 if (menu != NULL)
6472 {
6473 POINT p;
6474
Bram Moolenaar734a8672019-12-02 22:49:38 +01006475 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006477 if (mouse_pos)
6478 {
6479 int mx, my;
6480
6481 gui_mch_getmouse(&mx, &my);
6482 p.x += mx;
6483 p.y += my;
6484 }
6485 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006487 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6489 }
6490 msg_scroll = FALSE;
6491 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6492 }
6493}
6494
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006495# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496/*
6497 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6498 * create it as a pseudo-"tearoff menu".
6499 */
6500 void
6501gui_make_tearoff(char_u *path_name)
6502{
6503 vimmenu_T *menu = gui_find_menu(path_name);
6504
Bram Moolenaar734a8672019-12-02 22:49:38 +01006505 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 if (menu != NULL)
6507 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6508}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511/*
6512 * Add a menu item to a menu
6513 */
6514 void
6515gui_mch_add_menu_item(
6516 vimmenu_T *menu,
6517 int idx)
6518{
6519 vimmenu_T *parent = menu->parent;
6520
6521 menu->id = s_menu_id++;
6522 menu->submenu_id = NULL;
6523
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006524# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6526 {
6527 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6528 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6529 }
6530 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006531# endif
6532# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (menu_is_toolbar(parent->name))
6534 {
6535 TBBUTTON newtb;
6536
Bram Moolenaara80faa82020-04-12 19:37:17 +02006537 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (menu_is_separator(menu->name))
6539 {
6540 newtb.iBitmap = 0;
6541 newtb.fsStyle = TBSTYLE_SEP;
6542 }
6543 else
6544 {
6545 newtb.iBitmap = get_toolbar_bitmap(menu);
6546 newtb.fsStyle = TBSTYLE_BUTTON;
6547 }
6548 newtb.idCommand = menu->id;
6549 newtb.fsState = TBSTATE_ENABLED;
6550 newtb.iString = 0;
6551 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6552 (LPARAM)&newtb);
6553 menu->submenu_id = (HMENU)-1;
6554 }
6555 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006558 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006560 wn = enc_to_utf16(menu->name, NULL);
6561 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006563 InsertMenuW(parent->submenu_id, (UINT)idx,
6564 (menu_is_separator(menu->name)
6565 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6566 (UINT)menu->id, wn);
6567 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006569# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 if (IsWindow(parent->tearoff_handle))
6571 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574}
6575
6576/*
6577 * Destroy the machine specific menu widget.
6578 */
6579 void
6580gui_mch_destroy_menu(vimmenu_T *menu)
6581{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006582# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 /*
6584 * is this a toolbar button?
6585 */
6586 if (menu->submenu_id == (HMENU)-1)
6587 {
6588 int iButton;
6589
6590 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6591 (WPARAM)menu->id, 0);
6592 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6593 }
6594 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 {
6597 if (menu->parent != NULL
6598 && menu_is_popup(menu->parent->dname)
6599 && menu->parent->submenu_id != NULL)
6600 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6601 else
6602 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6603 if (menu->submenu_id != NULL)
6604 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006605# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 if (IsWindow(menu->tearoff_handle))
6607 DestroyWindow(menu->tearoff_handle);
6608 if (menu->parent != NULL
6609 && menu->parent->children != NULL
6610 && IsWindow(menu->parent->tearoff_handle))
6611 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006612 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 menu->modes = 0;
6614 rebuild_tearoff(menu->parent);
6615 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
6618}
6619
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006620# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 static void
6622rebuild_tearoff(vimmenu_T *menu)
6623{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006624 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 char_u tbuf[128];
6626 RECT trect;
6627 RECT rct;
6628 RECT roct;
6629 int x, y;
6630
6631 HWND thwnd = menu->tearoff_handle;
6632
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006633 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (GetWindowRect(thwnd, &trect)
6635 && GetWindowRect(s_hwnd, &rct)
6636 && GetClientRect(s_hwnd, &roct))
6637 {
6638 x = trect.left - rct.left;
6639 y = (trect.top - rct.bottom + roct.bottom);
6640 }
6641 else
6642 {
6643 x = y = 0xffffL;
6644 }
6645 DestroyWindow(thwnd);
6646 if (menu->children != NULL)
6647 {
6648 gui_mch_tearoff(tbuf, menu, x, y);
6649 if (IsWindow(menu->tearoff_handle))
6650 (void) SetWindowPos(menu->tearoff_handle,
6651 NULL,
6652 (int)trect.left,
6653 (int)trect.top,
6654 0, 0,
6655 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6656 }
6657}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006658# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660/*
6661 * Make a menu either grey or not grey.
6662 */
6663 void
6664gui_mch_menu_grey(
6665 vimmenu_T *menu,
6666 int grey)
6667{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006668# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 /*
6670 * is this a toolbar button?
6671 */
6672 if (menu->submenu_id == (HMENU)-1)
6673 {
6674 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6675 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6676 }
6677 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006678# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006679 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6680 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006682# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6684 {
6685 WORD menuID;
6686 HWND menuHandle;
6687
6688 /*
6689 * A tearoff button has changed state.
6690 */
6691 if (menu->children == NULL)
6692 menuID = (WORD)(menu->id);
6693 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006694 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6696 if (menuHandle)
6697 EnableWindow(menuHandle, !grey);
6698
6699 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701}
6702
Bram Moolenaar734a8672019-12-02 22:49:38 +01006703#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705
Bram Moolenaar734a8672019-12-02 22:49:38 +01006706// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6709#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006710#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6713/*
6714 * stuff for dialogs
6715 */
6716
6717/*
6718 * The callback routine used by all the dialogs. Very simple. First,
6719 * acknowledges the INITDIALOG message so that Windows knows to do standard
6720 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6721 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6722 * number.
6723 */
6724 static LRESULT CALLBACK
6725dialog_callback(
6726 HWND hwnd,
6727 UINT message,
6728 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006729 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 if (message == WM_INITDIALOG)
6732 {
6733 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006734 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 (void)SetFocus(hwnd);
6736 if (dialog_default_button > IDCANCEL)
6737 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006738 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006739 // We don't have a default, set focus on another element of the
6740 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006741 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 return FALSE;
6743 }
6744
6745 if (message == WM_COMMAND)
6746 {
6747 int button = LOWORD(wParam);
6748
Bram Moolenaar734a8672019-12-02 22:49:38 +01006749 // Don't end the dialog if something was selected that was
6750 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (button >= DLG_NONBUTTON_CONTROL)
6752 return TRUE;
6753
Bram Moolenaar734a8672019-12-02 22:49:38 +01006754 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006756 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006757 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006758 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006759
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006760 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6761 p = utf16_to_enc(wp, NULL);
6762 vim_strncpy(s_textfield, p, IOSIZE);
6763 vim_free(p);
6764 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
6767 /*
6768 * Need to check for IDOK because if the user just hits Return to
6769 * accept the default value, some reason this is what we get.
6770 */
6771 if (button == IDOK)
6772 {
6773 if (dialog_default_button > IDCANCEL)
6774 EndDialog(hwnd, dialog_default_button);
6775 }
6776 else
6777 EndDialog(hwnd, button - IDCANCEL);
6778 return TRUE;
6779 }
6780
6781 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6782 {
6783 EndDialog(hwnd, 0);
6784 return TRUE;
6785 }
6786 return FALSE;
6787}
6788
6789/*
6790 * Create a dialog dynamically from the parameter strings.
6791 * type = type of dialog (question, alert, etc.)
6792 * title = dialog title. may be NULL for default title.
6793 * message = text to display. Dialog sizes to accommodate it.
6794 * buttons = '\n' separated list of button captions, default first.
6795 * dfltbutton = number of default button.
6796 *
6797 * This routine returns 1 if the first button is pressed,
6798 * 2 for the second, etc.
6799 *
6800 * 0 indicates Esc was pressed.
6801 * -1 for unexpected error
6802 *
6803 * If stubbing out this fn, return 1.
6804 */
6805
Bram Moolenaar734a8672019-12-02 22:49:38 +01006806static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
6808 "IDR_VIM",
6809 "IDR_VIM_ERROR",
6810 "IDR_VIM_ALERT",
6811 "IDR_VIM_INFO",
6812 "IDR_VIM_QUESTION"
6813};
6814
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 int
6816gui_mch_dialog(
6817 int type,
6818 char_u *title,
6819 char_u *message,
6820 char_u *buttons,
6821 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006822 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006823 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
6825 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006826 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 int numButtons;
6828 int *buttonWidths, *buttonPositions;
6829 int buttonYpos;
6830 int nchar, i;
6831 DWORD lStyle;
6832 int dlgwidth = 0;
6833 int dlgheight;
6834 int editboxheight;
6835 int horizWidth = 0;
6836 int msgheight;
6837 char_u *pstart;
6838 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006839 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 char_u *tbuffer;
6841 RECT rect;
6842 HWND hwnd;
6843 HDC hdc;
6844 HFONT font, oldFont;
6845 TEXTMETRIC fontInfo;
6846 int fontHeight;
6847 int textWidth, minButtonWidth, messageWidth;
6848 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006849 int maxDialogHeight;
6850 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 int vertical;
6852 int dlgPaddingX;
6853 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006854# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006855 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006857# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006858 garray_T ga;
6859 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006861# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006862 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006863# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006864 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006865# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006866 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006867 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
Bram Moolenaar748bf032005-02-02 23:04:36 +00006870 if (s_hwnd == NULL)
6871 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873 if ((type < 0) || (type > VIM_LAST_TYPE))
6874 type = 0;
6875
Bram Moolenaar734a8672019-12-02 22:49:38 +01006876 // allocate some memory for dialog template
6877 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006878 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006879 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
6881 if (p == NULL)
6882 return -1;
6883
6884 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006885 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6887 * const.
6888 */
6889 tbuffer = vim_strsave(buttons);
6890 if (tbuffer == NULL)
6891 return -1;
6892
Bram Moolenaar734a8672019-12-02 22:49:38 +01006893 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar734a8672019-12-02 22:49:38 +01006895 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 numButtons = 1;
6897 for (i = 0; tbuffer[i] != '\0'; i++)
6898 {
6899 if (tbuffer[i] == DLG_BUTTON_SEP)
6900 numButtons++;
6901 }
6902 if (dfltbutton >= numButtons)
6903 dfltbutton = -1;
6904
Bram Moolenaar734a8672019-12-02 22:49:38 +01006905 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006906 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (buttonWidths == NULL)
6908 return -1;
6909
Bram Moolenaar734a8672019-12-02 22:49:38 +01006910 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006911 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (buttonPositions == NULL)
6913 return -1;
6914
6915 /*
6916 * Calculate how big the dialog must be.
6917 */
6918 hwnd = GetDesktopWindow();
6919 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006920# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6922 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006923 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 use_lfSysmenu = TRUE;
6925 }
6926 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6929 VARIABLE_PITCH , DLG_FONT_NAME);
6930 if (s_usenewlook)
6931 {
6932 oldFont = SelectFont(hdc, font);
6933 dlgPaddingX = DLG_PADDING_X;
6934 dlgPaddingY = DLG_PADDING_Y;
6935 }
6936 else
6937 {
6938 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6939 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6940 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6941 }
6942 GetTextMetrics(hdc, &fontInfo);
6943 fontHeight = fontInfo.tmHeight;
6944
Bram Moolenaar734a8672019-12-02 22:49:38 +01006945 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006946 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaar734a8672019-12-02 22:49:38 +01006948 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006949 if (s_hwnd == NULL)
6950 {
6951 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaar734a8672019-12-02 22:49:38 +01006953 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006954 get_work_area(&workarea_rect);
6955 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6956 if (maxDialogWidth > 600)
6957 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006958 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006959 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006960 }
6961 else
6962 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006963 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006964 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006965 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006966 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006967 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006968 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6969 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006970
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006971 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006972 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006973 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006974 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006975 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6976 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6977 }
6978
Bram Moolenaar734a8672019-12-02 22:49:38 +01006979 // Set dlgwidth to width of message.
6980 // Copy the message into "ga", changing NL to CR-NL and inserting line
6981 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 pstart = message;
6983 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006984 msgheight = 0;
6985 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 do
6987 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006988 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006989
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 // Need to figure out where to break the string. The system does it
6991 // at a word boundary, which would mean we can't compute the number of
6992 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006993 textWidth = 0;
6994 last_white = NULL;
6995 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006996 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006997 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006998 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006999 && textWidth > maxDialogWidth * 3 / 4)
7000 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007001 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007002 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007003 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007004 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007005 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007006 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007007 textWidth = 0;
7008
7009 if (last_white != NULL)
7010 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007011 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007012 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007013 pend = last_white + 1;
7014 last_white = NULL;
7015 }
7016 ga_append(&ga, '\r');
7017 ga_append(&ga, '\n');
7018 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007019 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007020
7021 while (--l >= 0)
7022 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007023 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007024 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007026
7027 ga_append(&ga, '\r');
7028 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 pstart = pend + 1;
7030 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007031
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007032 if (ga.ga_data != NULL)
7033 message = ga.ga_data;
7034
Bram Moolenaar734a8672019-12-02 22:49:38 +01007035 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007036
Bram Moolenaar734a8672019-12-02 22:49:38 +01007037 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02007038 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7039 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
7041 if (msgheight < DLG_ICON_HEIGHT)
7042 msgheight = DLG_ICON_HEIGHT;
7043
7044 /*
7045 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007046 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007048 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 if (!vertical)
7050 {
7051 // Place buttons horizontally if they fit.
7052 horizWidth = dlgPaddingX;
7053 pstart = tbuffer;
7054 i = 0;
7055 do
7056 {
7057 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7058 if (pend == NULL)
7059 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007060 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 if (textWidth < minButtonWidth)
7062 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007063 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 buttonWidths[i] = textWidth;
7065 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007066 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 pstart = pend + 1;
7068 } while (*pend != NUL);
7069
7070 if (horizWidth > maxDialogWidth)
7071 vertical = TRUE; // Too wide to fit on the screen.
7072 else if (horizWidth > dlgwidth)
7073 dlgwidth = horizWidth;
7074 }
7075
7076 if (vertical)
7077 {
7078 // Stack buttons vertically.
7079 pstart = tbuffer;
7080 do
7081 {
7082 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7083 if (pend == NULL)
7084 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007085 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007086 textWidth += dlgPaddingX; // Padding within button
7087 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (textWidth > dlgwidth)
7089 dlgwidth = textWidth;
7090 pstart = pend + 1;
7091 } while (*pend != NUL);
7092 }
7093
7094 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007095 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaar734a8672019-12-02 22:49:38 +01007097 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 if (s_usenewlook)
7099 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7100 else
7101 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7102
7103 add_long(lStyle);
7104 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007105 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 add_word(0); // NumberOfItems(will change later)
7107 add_word(10); // x
7108 add_word(10); // y
7109 add_word(PixelToDialogX(dlgwidth)); // cx
7110
7111 // Dialog height.
7112 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007113 dlgheight = msgheight + 2 * dlgPaddingY
7114 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 else
7116 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7117
7118 // Dialog needs to be taller if contains an edit box.
7119 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7120 if (textfield != NULL)
7121 dlgheight += editboxheight;
7122
Bram Moolenaar734a8672019-12-02 22:49:38 +01007123 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007124 if (dlgheight > maxDialogHeight)
7125 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007126 msgheight = msgheight - (dlgheight - maxDialogHeight);
7127 dlgheight = maxDialogHeight;
7128 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007129 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007130 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007131 }
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 add_word(PixelToDialogY(dlgheight));
7134
7135 add_word(0); // Menu
7136 add_word(0); // Class
7137
Bram Moolenaar734a8672019-12-02 22:49:38 +01007138 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007139 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7140 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 p += nchar;
7142
7143 if (s_usenewlook)
7144 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007145 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007146# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (use_lfSysmenu)
7148 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007149 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7151 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007152 wcscpy(p, lfSysmenu.lfFaceName);
7153 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
7155 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
7158 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007159 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 }
7161 p += nchar;
7162 }
7163
7164 buttonYpos = msgheight + 2 * dlgPaddingY;
7165
7166 if (textfield != NULL)
7167 buttonYpos += editboxheight;
7168
7169 pstart = tbuffer;
7170 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007171 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 for (i = 0; i < numButtons; i++)
7173 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007174 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 for ( pend = pstart;
7176 *pend && (*pend != DLG_BUTTON_SEP);
7177 pend++)
7178 ;
7179
7180 if (*pend)
7181 *pend = '\0';
7182
7183 /*
7184 * old NOTE:
7185 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7186 * the focus to the first tab-able button and in so doing makes that
7187 * the default!! Grrr. Workaround: Make the default button the only
7188 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7189 * he/she can use arrow keys.
7190 *
7191 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007192 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 * dialog. Also needed for when the textfield is the default control.
7194 * It appears to work now (perhaps not on Win95?).
7195 */
7196 if (vertical)
7197 {
7198 p = add_dialog_element(p,
7199 (i == dfltbutton
7200 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7201 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007202 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 + 2 * fontHeight * i),
7204 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7205 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007206 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 else
7209 {
7210 p = add_dialog_element(p,
7211 (i == dfltbutton
7212 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7213 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007214 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 PixelToDialogX(buttonWidths[i]),
7216 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007217 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007219 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 }
7221 *pnumitems += numButtons;
7222
Bram Moolenaar734a8672019-12-02 22:49:38 +01007223 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 p = add_dialog_element(p, SS_ICON,
7225 PixelToDialogX(dlgPaddingX),
7226 PixelToDialogY(dlgPaddingY),
7227 PixelToDialogX(DLG_ICON_WIDTH),
7228 PixelToDialogY(DLG_ICON_HEIGHT),
7229 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7230 dlg_icons[type]);
7231
Bram Moolenaar734a8672019-12-02 22:49:38 +01007232 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007233 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7234 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7235 PixelToDialogY(dlgPaddingY),
7236 (WORD)(PixelToDialogX(messageWidth) + 1),
7237 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007238 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaar734a8672019-12-02 22:49:38 +01007240 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (textfield != NULL)
7242 {
7243 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7244 PixelToDialogX(2 * dlgPaddingX),
7245 PixelToDialogY(2 * dlgPaddingY + msgheight),
7246 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7247 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007248 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 *pnumitems += 1;
7250 }
7251
7252 *pnumitems += 2;
7253
7254 SelectFont(hdc, oldFont);
7255 DeleteObject(font);
7256 ReleaseDC(hwnd, hdc);
7257
Bram Moolenaar734a8672019-12-02 22:49:38 +01007258 // Let the dialog_callback() function know which button to make default
7259 // If we have an edit box, make that the default. We also need to tell
7260 // dialog_callback() if this dialog contains an edit box or not. We do
7261 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (textfield != NULL)
7263 {
7264 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7265 s_textfield = textfield;
7266 }
7267 else
7268 {
7269 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7270 s_textfield = NULL;
7271 }
7272
Bram Moolenaar734a8672019-12-02 22:49:38 +01007273 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007275 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 (LPDLGTEMPLATE)pdlgtemplate,
7277 s_hwnd,
7278 (DLGPROC)dialog_callback);
7279
7280 LocalFree(LocalHandle(pdlgtemplate));
7281 vim_free(tbuffer);
7282 vim_free(buttonWidths);
7283 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007284 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
Bram Moolenaar734a8672019-12-02 22:49:38 +01007286 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 (void)SetFocus(s_hwnd);
7288
7289 return nchar;
7290}
7291
Bram Moolenaar734a8672019-12-02 22:49:38 +01007292#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007293
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294/*
7295 * Put a simple element (basic class) onto a dialog template in memory.
7296 * return a pointer to where the next item should be added.
7297 *
7298 * parameters:
7299 * lStyle = additional style flags
7300 * (be careful, NT3.51 & Win32s will ignore the new ones)
7301 * x,y = x & y positions IN DIALOG UNITS
7302 * w,h = width and height IN DIALOG UNITS
7303 * Id = ID used in messages
7304 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7305 * caption = usually text or resource name
7306 *
7307 * TODO: use the length information noted here to enable the dialog creation
7308 * routines to work out more exactly how much memory they need to alloc.
7309 */
7310 static PWORD
7311add_dialog_element(
7312 PWORD p,
7313 DWORD lStyle,
7314 WORD x,
7315 WORD y,
7316 WORD w,
7317 WORD h,
7318 WORD Id,
7319 WORD clss,
7320 const char *caption)
7321{
7322 int nchar;
7323
Bram Moolenaar734a8672019-12-02 22:49:38 +01007324 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7326 *p++ = LOWORD(lStyle);
7327 *p++ = HIWORD(lStyle);
7328 *p++ = 0; // LOWORD (lExtendedStyle)
7329 *p++ = 0; // HIWORD (lExtendedStyle)
7330 *p++ = x;
7331 *p++ = y;
7332 *p++ = w;
7333 *p++ = h;
7334 *p++ = Id; //9 or 10 words in all
7335
7336 *p++ = (WORD)0xffff;
7337 *p++ = clss; //2 more here
7338
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007339 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 p += nchar;
7341
7342 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7343
7344 return p; //total = 15+ (strlen(caption)) words
7345 // = 30 + 2(strlen(caption) bytes reqd
7346}
7347
7348
7349/*
7350 * Helper routine. Take an input pointer, return closest pointer that is
7351 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7352 */
7353 static LPWORD
7354lpwAlign(
7355 LPWORD lpIn)
7356{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007357 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007359 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 ul += 3;
7361 ul >>= 2;
7362 ul <<= 2;
7363 return (LPWORD)ul;
7364}
7365
7366/*
7367 * Helper routine. Takes second parameter as Ansi string, copies it to first
7368 * parameter as wide character (16-bits / char) string, and returns integer
7369 * number of wide characters (words) in string (including the trailing wide
7370 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007371 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7372 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 static int
7374nCopyAnsiToWideChar(
7375 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007376 LPSTR lpAnsiIn,
7377 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007380 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 int i;
7382 WCHAR *wn;
7383
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007384 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007386 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007387 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 if (wn != NULL)
7389 {
7390 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007391 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 vim_free(wn);
7393 }
7394 }
7395 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007396 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 nChar = MultiByteToWideChar(
7398 enc_codepage > 0 ? enc_codepage : CP_ACP,
7399 MB_PRECOMPOSED,
7400 lpAnsiIn, len,
7401 lpWCStr, len);
7402 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007403 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406 return nChar;
7407}
7408
7409
7410#ifdef FEAT_TEAROFF
7411/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007412 * Lookup menu handle from "menu_id".
7413 */
7414 static HMENU
7415tearoff_lookup_menuhandle(
7416 vimmenu_T *menu,
7417 WORD menu_id)
7418{
7419 for ( ; menu != NULL; menu = menu->next)
7420 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007421 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007422 continue;
7423 if (menu_is_separator(menu->dname))
7424 continue;
7425 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7426 return menu->submenu_id;
7427 }
7428 return NULL;
7429}
7430
7431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 * The callback function for all the modeless dialogs that make up the
7433 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7434 * thinking its menus have been clicked), and go away when closed.
7435 */
7436 static LRESULT CALLBACK
7437tearoff_callback(
7438 HWND hwnd,
7439 UINT message,
7440 WPARAM wParam,
7441 LPARAM lParam)
7442{
7443 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007444 {
7445 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaar734a8672019-12-02 22:49:38 +01007449 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 HandleMouseHide(message, lParam);
7451
7452 if (message == WM_COMMAND)
7453 {
7454 if ((WORD)(LOWORD(wParam)) & 0x8000)
7455 {
7456 POINT mp;
7457 RECT rect;
7458
7459 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7460 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007461 vimmenu_T *menu;
7462
7463 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007465 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7467 (int)rect.right - 8,
7468 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007469 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 s_hwnd,
7471 NULL);
7472 /*
7473 * NOTE: The pop-up menu can eat the mouse up event.
7474 * We deal with this in normal.c.
7475 */
7476 }
7477 }
7478 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007479 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7481 /*
7482 * Give main window the focus back: this is so after
7483 * choosing a tearoff button you can start typing again
7484 * straight away.
7485 */
7486 (void)SetFocus(s_hwnd);
7487 return TRUE;
7488 }
7489 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7490 {
7491 DestroyWindow(hwnd);
7492 return TRUE;
7493 }
7494
Bram Moolenaar734a8672019-12-02 22:49:38 +01007495 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (message == WM_EXITSIZEMOVE)
7497 (void)SetActiveWindow(s_hwnd);
7498
7499 return FALSE;
7500}
7501#endif
7502
7503
7504/*
7505 * Decide whether to use the "new look" (small, non-bold font) or the "old
7506 * look" (big, clanky font) for dialogs, and work out a few values for use
7507 * later accordingly.
7508 */
7509 static void
7510get_dialog_font_metrics(void)
7511{
7512 HDC hdc;
7513 HFONT hfontTools = 0;
7514 DWORD dlgFontSize;
7515 SIZE size;
7516#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007517 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518#endif
7519
7520 s_usenewlook = FALSE;
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007523 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007524 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526#endif
7527 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7528 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7529
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007530 if (hfontTools)
7531 {
7532 hdc = GetDC(s_hwnd);
7533 SelectObject(hdc, hfontTools);
7534 /*
7535 * GetTextMetrics() doesn't return the right value in
7536 * tmAveCharWidth, so we have to figure out the dialog base units
7537 * ourselves.
7538 */
7539 GetTextExtentPoint(hdc,
7540 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7541 52, &size);
7542 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007544 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7545 s_dlgfntheight = (WORD)size.cy;
7546 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
7548
7549 if (!s_usenewlook)
7550 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007551 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 s_dlgfntwidth = LOWORD(dlgFontSize);
7553 s_dlgfntheight = HIWORD(dlgFontSize);
7554 }
7555}
7556
7557#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7558/*
7559 * Create a pseudo-"tearoff menu" based on the child
7560 * items of a given menu pointer.
7561 */
7562 static void
7563gui_mch_tearoff(
7564 char_u *title,
7565 vimmenu_T *menu,
7566 int initX,
7567 int initY)
7568{
7569 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7570 int template_len;
7571 int nchar, textWidth, submenuWidth;
7572 DWORD lStyle;
7573 DWORD lExtendedStyle;
7574 WORD dlgwidth;
7575 WORD menuID;
7576 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007577 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 vimmenu_T *the_menu = menu;
7579 HWND hwnd;
7580 HDC hdc;
7581 HFONT font, oldFont;
7582 int col, spaceWidth, len;
7583 int columnWidths[2];
7584 char_u *label, *text;
7585 int acLen = 0;
7586 int nameLen;
7587 int padding0, padding1, padding2 = 0;
7588 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007589 int x;
7590 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007591# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007592 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
7596 /*
7597 * If this menu is already torn off, move it to the mouse position.
7598 */
7599 if (IsWindow(menu->tearoff_handle))
7600 {
7601 POINT mp;
7602 if (GetCursorPos((LPPOINT)&mp))
7603 {
7604 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7605 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7606 }
7607 return;
7608 }
7609
7610 /*
7611 * Create a new tearoff.
7612 */
7613 if (*title == MNU_HIDDEN_CHAR)
7614 title++;
7615
Bram Moolenaar734a8672019-12-02 22:49:38 +01007616 // Allocate memory to store the dialog template. It's made bigger when
7617 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 template_len = DLG_ALLOC_SIZE;
7619 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7620 if (p == NULL)
7621 return;
7622
7623 hwnd = GetDesktopWindow();
7624 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007625# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7627 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007628 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 use_lfSysmenu = TRUE;
7630 }
7631 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7634 VARIABLE_PITCH , DLG_FONT_NAME);
7635 if (s_usenewlook)
7636 oldFont = SelectFont(hdc, font);
7637 else
7638 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7639
Bram Moolenaar734a8672019-12-02 22:49:38 +01007640 // Calculate width of a single space. Used for padding columns to the
7641 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007642 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643
Bram Moolenaar734a8672019-12-02 22:49:38 +01007644 // Figure out max width of the text column, the accelerator column and the
7645 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 submenuWidth = 0;
7647 for (col = 0; col < 2; col++)
7648 {
7649 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007650 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007652 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 text = (col == 0) ? pmenu->dname : pmenu->actext;
7654 if (text != NULL && *text != NUL)
7655 {
7656 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7657 if (textWidth > columnWidths[col])
7658 columnWidths[col] = textWidth;
7659 }
7660 if (pmenu->children != NULL)
7661 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7662 }
7663 }
7664 if (columnWidths[1] == 0)
7665 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007666 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (submenuWidth != 0)
7668 columnWidths[0] += submenuWidth;
7669 else
7670 columnWidths[0] += spaceWidth;
7671 }
7672 else
7673 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007674 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7676 columnWidths[1] += submenuWidth;
7677 }
7678
7679 /*
7680 * Now find the total width of our 'menu'.
7681 */
7682 textWidth = columnWidths[0] + columnWidths[1];
7683 if (submenuWidth != 0)
7684 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007685 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7687 textWidth += submenuWidth;
7688 }
7689 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7690 if (textWidth > dlgwidth)
7691 dlgwidth = textWidth;
7692 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7693
Bram Moolenaar734a8672019-12-02 22:49:38 +01007694 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 if (s_usenewlook)
7696 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7697 else
7698 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7699
7700 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7701 *p++ = LOWORD(lStyle);
7702 *p++ = HIWORD(lStyle);
7703 *p++ = LOWORD(lExtendedStyle);
7704 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007705 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007707 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007709 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 else
7711 *p++ = PixelToDialogX(initX); // x
7712 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007713 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 else
7715 *p++ = PixelToDialogY(initY); // y
7716 *p++ = PixelToDialogX(dlgwidth); // cx
7717 ptrueheight = p;
7718 *p++ = 0; // dialog height: changed later anyway
7719 *p++ = 0; // Menu
7720 *p++ = 0; // Class
7721
Bram Moolenaar734a8672019-12-02 22:49:38 +01007722 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007724 ? (LPSTR)title
7725 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 p += nchar;
7727
7728 if (s_usenewlook)
7729 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007730 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007731# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (use_lfSysmenu)
7733 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7736 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007737 wcscpy(p, lfSysmenu.lfFaceName);
7738 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
7743 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007744 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746 p += nchar;
7747 }
7748
7749 /*
7750 * Loop over all the items in the menu.
7751 * But skip over the tearbar.
7752 */
7753 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7754 menu = menu->children->next;
7755 else
7756 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007757 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 for ( ; menu != NULL; menu = menu->next)
7759 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007760 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 continue;
7762 if (menu_is_separator(menu->dname))
7763 {
7764 sepPadding += 3;
7765 continue;
7766 }
7767
Bram Moolenaar734a8672019-12-02 22:49:38 +01007768 // Check if there still is plenty of room in the template. Make it
7769 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7771 {
7772 WORD *newp;
7773
7774 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7775 if (newp != NULL)
7776 {
7777 template_len += 4096;
7778 mch_memmove(newp, pdlgtemplate,
7779 (char *)p - (char *)pdlgtemplate);
7780 p = newp + (p - pdlgtemplate);
7781 pnumitems = newp + (pnumitems - pdlgtemplate);
7782 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7783 LocalFree(LocalHandle(pdlgtemplate));
7784 pdlgtemplate = newp;
7785 }
7786 }
7787
Bram Moolenaar734a8672019-12-02 22:49:38 +01007788 // Figure out minimal length of this menu label. Use "name" for the
7789 // actual text, "dname" for estimating the displayed size. "name"
7790 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 len = nameLen = (int)STRLEN(menu->name);
7792 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7793 (int)STRLEN(menu->dname))) / spaceWidth;
7794 len += padding0;
7795
7796 if (menu->actext != NULL)
7797 {
7798 acLen = (int)STRLEN(menu->actext);
7799 len += acLen;
7800 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7801 }
7802 else
7803 textWidth = 0;
7804 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7805 len += padding1;
7806
7807 if (menu->children == NULL)
7808 {
7809 padding2 = submenuWidth / spaceWidth;
7810 len += padding2;
7811 menuID = (WORD)(menu->id);
7812 }
7813 else
7814 {
7815 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007816 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818
Bram Moolenaar734a8672019-12-02 22:49:38 +01007819 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007820 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 if (label == NULL)
7822 break;
7823
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007824 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007825 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007827 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 while (padding0-- > 0)
7829 *text++ = ' ';
7830 if (menu->actext != NULL)
7831 {
7832 STRNCPY(text, menu->actext, acLen);
7833 text += acLen;
7834 }
7835 while (padding1-- > 0)
7836 *text++ = ' ';
7837 if (menu->children != NULL)
7838 {
7839 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7840 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7841 }
7842 else
7843 {
7844 while (padding2-- > 0)
7845 *text++ = ' ';
7846 }
7847 *text = NUL;
7848
7849 /*
7850 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7851 * W95/NT4 it makes the tear-off look more like a menu.
7852 */
7853 p = add_dialog_element(p,
7854 BS_PUSHBUTTON|BS_LEFT,
7855 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7856 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7857 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7858 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007859 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 vim_free(label);
7861 (*pnumitems)++;
7862 }
7863
7864 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7865
7866
Bram Moolenaar734a8672019-12-02 22:49:38 +01007867 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007868 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007869 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 (LPDLGTEMPLATE)pdlgtemplate,
7871 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007872 (DLGPROC)tearoff_callback,
7873 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
7875 LocalFree(LocalHandle(pdlgtemplate));
7876 SelectFont(hdc, oldFont);
7877 DeleteObject(font);
7878 ReleaseDC(hwnd, hdc);
7879
7880 /*
7881 * Reassert ourselves as the active window. This is so that after creating
7882 * a tearoff, the user doesn't have to click with the mouse just to start
7883 * typing again!
7884 */
7885 (void)SetActiveWindow(s_hwnd);
7886
Bram Moolenaar734a8672019-12-02 22:49:38 +01007887 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 force_menu_update = TRUE;
7889}
7890#endif
7891
7892#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007893# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894
Bram Moolenaar734a8672019-12-02 22:49:38 +01007895// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896# ifndef TBSTYLE_FLAT
7897# define TBSTYLE_FLAT 0x0800
7898# endif
7899
7900/*
7901 * Create the toolbar, initially unpopulated.
7902 * (just like the menu, there are no defaults, it's all
7903 * set up through menu.vim)
7904 */
7905 static void
7906initialise_toolbar(void)
7907{
7908 InitCommonControls();
7909 s_toolbarhwnd = CreateToolbarEx(
7910 s_hwnd,
7911 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7912 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007913 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007914 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 IDR_TOOLBAR1, // id of initial bitmap
7916 NULL,
7917 0, // initial number of buttons
7918 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7919 TOOLBAR_BUTTON_HEIGHT,
7920 TOOLBAR_BUTTON_WIDTH,
7921 TOOLBAR_BUTTON_HEIGHT,
7922 sizeof(TBBUTTON)
7923 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007924
7925 // Remove transparency from the toolbar to prevent the main window
7926 // background colour showing through
7927 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7928 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7929
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007930 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7933}
7934
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007935 static LRESULT CALLBACK
7936toolbar_wndproc(
7937 HWND hwnd,
7938 UINT uMsg,
7939 WPARAM wParam,
7940 LPARAM lParam)
7941{
7942 HandleMouseHide(uMsg, lParam);
7943 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7944}
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 static int
7947get_toolbar_bitmap(vimmenu_T *menu)
7948{
7949 int i = -1;
7950
7951 /*
7952 * Check user bitmaps first, unless builtin is specified.
7953 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007954 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {
7956 char_u fname[MAXPATHL];
7957 HANDLE hbitmap = NULL;
7958
7959 if (menu->iconfile != NULL)
7960 {
7961 gui_find_iconfile(menu->iconfile, fname, "bmp");
7962 hbitmap = LoadImage(
7963 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007964 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 IMAGE_BITMAP,
7966 TOOLBAR_BUTTON_WIDTH,
7967 TOOLBAR_BUTTON_HEIGHT,
7968 LR_LOADFROMFILE |
7969 LR_LOADMAP3DCOLORS
7970 );
7971 }
7972
7973 /*
7974 * If the LoadImage call failed, or the "icon=" file
7975 * didn't exist or wasn't specified, try the menu name
7976 */
7977 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007978 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007979# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007980 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007981# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007982 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 hbitmap = LoadImage(
7984 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007985 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 IMAGE_BITMAP,
7987 TOOLBAR_BUTTON_WIDTH,
7988 TOOLBAR_BUTTON_HEIGHT,
7989 LR_LOADFROMFILE |
7990 LR_LOADMAP3DCOLORS
7991 );
7992
7993 if (hbitmap != NULL)
7994 {
7995 TBADDBITMAP tbAddBitmap;
7996
7997 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007998 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
8000 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8001 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008002 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 }
8004 }
8005 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8006 i = menu->iconidx;
8007
8008 return i;
8009}
8010#endif
8011
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008012#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8013 static void
8014initialise_tabline(void)
8015{
8016 InitCommonControls();
8017
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008018 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008019 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008020 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008021 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008022 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008023
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008024 gui.tabline_height = TABLINE_HEIGHT;
8025
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008026# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008027 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008028# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008029}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008030
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008031/*
8032 * Get tabpage_T from POINT.
8033 */
8034 static tabpage_T *
8035GetTabFromPoint(
8036 HWND hWnd,
8037 POINT pt)
8038{
8039 tabpage_T *ptp = NULL;
8040
8041 if (gui_mch_showing_tabline())
8042 {
8043 TCHITTESTINFO htinfo;
8044 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008045 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008046 if (s_tabhwnd == hWnd)
8047 {
8048 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8049 if (idx != -1)
8050 ptp = find_tabpage(idx + 1);
8051 }
8052 }
8053 return ptp;
8054}
8055
8056static POINT s_pt = {0, 0};
8057static HCURSOR s_hCursor = NULL;
8058
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008059 static LRESULT CALLBACK
8060tabline_wndproc(
8061 HWND hwnd,
8062 UINT uMsg,
8063 WPARAM wParam,
8064 LPARAM lParam)
8065{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008066 POINT pt;
8067 tabpage_T *tp;
8068 RECT rect;
8069 int nCenter;
8070 int idx0;
8071 int idx1;
8072
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008073 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008074
8075 switch (uMsg)
8076 {
8077 case WM_LBUTTONDOWN:
8078 {
8079 s_pt.x = GET_X_LPARAM(lParam);
8080 s_pt.y = GET_Y_LPARAM(lParam);
8081 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008082 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008083 break;
8084 }
8085 case WM_MOUSEMOVE:
8086 if (GetCapture() == hwnd
8087 && ((wParam & MK_LBUTTON)) != 0)
8088 {
8089 pt.x = GET_X_LPARAM(lParam);
8090 pt.y = s_pt.y;
8091 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8092 {
8093 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8094
8095 tp = GetTabFromPoint(hwnd, pt);
8096 if (tp != NULL)
8097 {
8098 idx0 = tabpage_index(curtab) - 1;
8099 idx1 = tabpage_index(tp) - 1;
8100
8101 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8102 nCenter = rect.left + (rect.right - rect.left) / 2;
8103
Bram Moolenaar734a8672019-12-02 22:49:38 +01008104 // Check if the mouse cursor goes over the center of
8105 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008106 if ((idx0 < idx1) && (nCenter < pt.x))
8107 {
8108 tabpage_move(idx1 + 1);
8109 update_screen(0);
8110 }
8111 else if ((idx1 < idx0) && (pt.x < nCenter))
8112 {
8113 tabpage_move(idx1);
8114 update_screen(0);
8115 }
8116 }
8117 }
8118 }
8119 break;
8120 case WM_LBUTTONUP:
8121 {
8122 if (GetCapture() == hwnd)
8123 {
8124 SetCursor(s_hCursor);
8125 ReleaseCapture();
8126 }
8127 break;
8128 }
8129 default:
8130 break;
8131 }
8132
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008133 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8134}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008135#endif
8136
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8138/*
8139 * Make the GUI window come to the foreground.
8140 */
8141 void
8142gui_mch_set_foreground(void)
8143{
8144 if (IsIconic(s_hwnd))
8145 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8146 SetForegroundWindow(s_hwnd);
8147}
8148#endif
8149
8150#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8151 static void
8152dyn_imm_load(void)
8153{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008154 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (hLibImm == NULL)
8156 return;
8157
8158 pImmGetCompositionStringA
8159 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8160 pImmGetCompositionStringW
8161 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8162 pImmGetContext
8163 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8164 pImmAssociateContext
8165 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8166 pImmReleaseContext
8167 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8168 pImmGetOpenStatus
8169 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8170 pImmSetOpenStatus
8171 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008172 pImmGetCompositionFontW
8173 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8174 pImmSetCompositionFontW
8175 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 pImmSetCompositionWindow
8177 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8178 pImmGetConversionStatus
8179 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008180 pImmSetConversionStatus
8181 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182
8183 if ( pImmGetCompositionStringA == NULL
8184 || pImmGetCompositionStringW == NULL
8185 || pImmGetContext == NULL
8186 || pImmAssociateContext == NULL
8187 || pImmReleaseContext == NULL
8188 || pImmGetOpenStatus == NULL
8189 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008190 || pImmGetCompositionFontW == NULL
8191 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008193 || pImmGetConversionStatus == NULL
8194 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
8196 FreeLibrary(hLibImm);
8197 hLibImm = NULL;
8198 pImmGetContext = NULL;
8199 return;
8200 }
8201
8202 return;
8203}
8204
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205#endif
8206
8207#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8208
8209# ifdef FEAT_XPM_W32
8210# define IMAGE_XPM 100
8211# endif
8212
8213typedef struct _signicon_t
8214{
8215 HANDLE hImage;
8216 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008217# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008218 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220} signicon_t;
8221
8222 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008223gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 signicon_t *sign;
8226 int x, y, w, h;
8227
8228 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8229 return;
8230
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008231# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008232 if (IS_ENABLE_DIRECTX())
8233 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008234# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008235
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 x = TEXT_X(col);
8237 y = TEXT_Y(row);
8238 w = gui.char_width * 2;
8239 h = gui.char_height;
8240 switch (sign->uType)
8241 {
8242 case IMAGE_BITMAP:
8243 {
8244 HDC hdcMem;
8245 HBITMAP hbmpOld;
8246
8247 hdcMem = CreateCompatibleDC(s_hdc);
8248 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8249 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8250 SelectObject(hdcMem, hbmpOld);
8251 DeleteDC(hdcMem);
8252 }
8253 break;
8254 case IMAGE_ICON:
8255 case IMAGE_CURSOR:
8256 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8257 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008258# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 case IMAGE_XPM:
8260 {
8261 HDC hdcMem;
8262 HBITMAP hbmpOld;
8263
8264 hdcMem = CreateCompatibleDC(s_hdc);
8265 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008266 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8268
8269 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008270 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8272 SelectObject(hdcMem, hbmpOld);
8273 DeleteDC(hdcMem);
8274 }
8275 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 }
8278}
8279
8280 static void
8281close_signicon_image(signicon_t *sign)
8282{
8283 if (sign)
8284 switch (sign->uType)
8285 {
8286 case IMAGE_BITMAP:
8287 DeleteObject((HGDIOBJ)sign->hImage);
8288 break;
8289 case IMAGE_CURSOR:
8290 DestroyCursor((HCURSOR)sign->hImage);
8291 break;
8292 case IMAGE_ICON:
8293 DestroyIcon((HICON)sign->hImage);
8294 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008295# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 case IMAGE_XPM:
8297 DeleteObject((HBITMAP)sign->hImage);
8298 DeleteObject((HBITMAP)sign->hShape);
8299 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 }
8302}
8303
8304 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008305gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 signicon_t sign, *psign;
8308 char_u *ext;
8309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008311 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 if (ext > signfile)
8313 {
8314 int do_load = 1;
8315
8316 if (!STRICMP(ext, ".bmp"))
8317 sign.uType = IMAGE_BITMAP;
8318 else if (!STRICMP(ext, ".ico"))
8319 sign.uType = IMAGE_ICON;
8320 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8321 sign.uType = IMAGE_CURSOR;
8322 else
8323 do_load = 0;
8324
8325 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008326 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 gui.char_width * 2, gui.char_height,
8328 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008329# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (!STRICMP(ext, ".xpm"))
8331 {
8332 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008333 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8334 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
8338
8339 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008340 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 *psign = sign;
8342
8343 if (!psign)
8344 {
8345 if (sign.hImage)
8346 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008347 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 }
8349 return (void *)psign;
8350
8351}
8352
8353 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008354gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356 if (sign)
8357 {
8358 close_signicon_image((signicon_t *)sign);
8359 vim_free(sign);
8360 }
8361}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008364#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365
Bram Moolenaar734a8672019-12-02 22:49:38 +01008366/*
8367 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008368 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008370 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8372 * to get current mouse position).
8373 *
8374 * Trying to use as more Windows services as possible, and as less
8375 * IE version as possible :)).
8376 *
8377 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8378 * BalloonEval struct.
8379 * 2) Enable/Disable simply create/kill BalloonEval Timer
8380 * 3) When there was enough inactivity, timer procedure posts
8381 * async request to debugger
8382 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8383 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008384 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 */
8386
Bram Moolenaar45360022005-07-21 21:08:21 +00008387/*
8388 * determine whether installed Common Controls support multiline tooltips
8389 * (i.e. their version is >= 4.70
8390 */
8391 int
8392multiline_balloon_available(void)
8393{
8394 HINSTANCE hDll;
8395 static char comctl_dll[] = "comctl32.dll";
8396 static int multiline_tip = MAYBE;
8397
8398 if (multiline_tip != MAYBE)
8399 return multiline_tip;
8400
8401 hDll = GetModuleHandle(comctl_dll);
8402 if (hDll != NULL)
8403 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008404 DLLGETVERSIONPROC pGetVer;
8405 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008406
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008407 if (pGetVer != NULL)
8408 {
8409 DLLVERSIONINFO dvi;
8410 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008411
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008412 ZeroMemory(&dvi, sizeof(dvi));
8413 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008414
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008415 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008416
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008417 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008418 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008419 || (dvi.dwMajorVersion == 4
8420 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008421 {
8422 multiline_tip = TRUE;
8423 return multiline_tip;
8424 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008425 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008426 else
8427 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008428 // there is chance we have ancient CommCtl 4.70
8429 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008430 DWORD dwHandle = 0;
8431 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8432 if (len > 0)
8433 {
8434 VS_FIXEDFILEINFO *ver;
8435 UINT vlen = 0;
8436 void *data = alloc(len);
8437
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008438 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008439 && GetFileVersionInfo(comctl_dll, 0, len, data)
8440 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8441 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008442 && HIWORD(ver->dwFileVersionMS) > 4)
8443 || ((HIWORD(ver->dwFileVersionMS) == 4
8444 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008445 {
8446 vim_free(data);
8447 multiline_tip = TRUE;
8448 return multiline_tip;
8449 }
8450 vim_free(data);
8451 }
8452 }
8453 }
8454 multiline_tip = FALSE;
8455 return multiline_tip;
8456}
8457
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008458 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008459make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008460{
8461 TOOLINFOW *pti;
8462 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008463
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008464 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008465 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8466 else
8467 ToolInfoSize = sizeof(TOOLINFOW);
8468
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008469 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008470 if (pti == NULL)
8471 return;
8472
8473 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8474 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8475 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008476 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008477
8478 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8479 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8480
8481 pti->cbSize = ToolInfoSize;
8482 pti->uFlags = TTF_SUBCLASS;
8483 pti->hwnd = beval->target;
8484 pti->hinst = 0; // Don't use string resources
8485 pti->uId = ID_BEVAL_TOOLTIP;
8486
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008487 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008488 {
8489 RECT rect;
8490 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8491 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008492 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8493 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008494 // switch multiline tooltips on
8495 if (GetClientRect(s_textArea, &rect))
8496 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8497 (LPARAM)rect.right);
8498 }
8499 else
8500 {
8501 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008502 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8503 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008504 }
8505
8506 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8507 pti->rect.left = pt.x - 3;
8508 pti->rect.top = pt.y - 3;
8509 pti->rect.right = pt.x + 3;
8510 pti->rect.bottom = pt.y + 3;
8511
8512 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8513 // Make tooltip appear sooner.
8514 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8515 // I've performed some tests and it seems the longest possible life time
8516 // of tooltip is 30 seconds.
8517 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8518 /*
8519 * HACK: force tooltip to appear, because it'll not appear until
8520 * first mouse move. D*mn M$
8521 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8522 */
8523 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8524 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8525 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008526}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008529delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008531 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532}
8533
8534 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008535BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008536 HWND hwnd UNUSED,
8537 UINT uMsg UNUSED,
8538 UINT_PTR idEvent UNUSED,
8539 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 POINT pt;
8542 RECT rect;
8543
8544 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8545 return;
8546
8547 GetCursorPos(&pt);
8548 if (WindowFromPoint(pt) != s_textArea)
8549 return;
8550
8551 ScreenToClient(s_textArea, &pt);
8552 GetClientRect(s_textArea, &rect);
8553 if (!PtInRect(&rect, pt))
8554 return;
8555
8556 if (LastActivity > 0
8557 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8558 && (cur_beval->showState != ShS_PENDING
8559 || abs(cur_beval->x - pt.x) > 3
8560 || abs(cur_beval->y - pt.y) > 3))
8561 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008562 // Pointer resting in one place long enough, it's time to show
8563 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 cur_beval->showState = ShS_PENDING;
8565 cur_beval->x = pt.x;
8566 cur_beval->y = pt.y;
8567
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008568 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 if (cur_beval->msgCB != NULL)
8571 (*cur_beval->msgCB)(cur_beval, 0);
8572 }
8573}
8574
8575 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008576gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008578 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008580 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008584gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008586 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 if (beval == NULL)
8588 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008589 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008590 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008591 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592}
8593
8594 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008595gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008598
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008599 vim_free(beval->msg);
8600 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8601 if (beval->msg == NULL)
8602 {
8603 delete_tooltip(beval);
8604 beval->showState = ShS_NEUTRAL;
8605 return;
8606 }
8607
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008608 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 if (beval->showState == ShS_SHOWING)
8610 return;
8611 GetCursorPos(&pt);
8612 ScreenToClient(s_textArea, &pt);
8613
8614 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008616 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 gui_mch_disable_beval_area(cur_beval);
8618 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008619 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008621 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622}
8623
8624 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008625gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008626 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008627 char_u *mesg,
8628 void (*mesgCB)(BalloonEval *, int),
8629 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008631 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 BalloonEval *beval;
8633
8634 if (mesg != NULL && mesgCB != NULL)
8635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008636 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 return NULL;
8638 }
8639
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008640 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (beval != NULL)
8642 {
8643 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
8645 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 beval->msg = mesg;
8647 beval->msgCB = mesgCB;
8648 beval->clientData = clientData;
8649
8650 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 cur_beval = beval;
8652
8653 if (p_beval)
8654 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 }
8656 return beval;
8657}
8658
8659 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008660Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008662 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 return;
8664
8665 if (cur_beval != NULL)
8666 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008667 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008669 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008670 // TRACE0("TTN_SHOW {{{");
8671 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008672 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008673 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008674 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 delete_tooltip(cur_beval);
8676 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008677 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008680 break;
8681 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008682 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008683 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008684 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8685 info->lpszText = (LPSTR)info->lParam;
8686 info->uFlags |= TTF_DI_SETITEM;
8687 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008688 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008689 case TTN_GETDISPINFOW:
8690 {
8691 // if we get here then we have new common controls
8692 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8693 info->lpszText = (LPWSTR)info->lParam;
8694 info->uFlags |= TTF_DI_SETITEM;
8695 }
8696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 }
8698 }
8699}
8700
8701 static void
8702TrackUserActivity(UINT uMsg)
8703{
8704 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8705 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8706 LastActivity = GetTickCount();
8707}
8708
8709 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008710gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008712# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008713 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008714# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008715 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 vim_free(beval);
8717}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008718#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8721/*
8722 * We have multiple signs to draw at the same location. Draw the
8723 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8724 */
8725 void
8726netbeans_draw_multisign_indicator(int row)
8727{
8728 int i;
8729 int y;
8730 int x;
8731
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008732 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008733 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008734
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 x = 0;
8736 y = TEXT_Y(row);
8737
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008738# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008739 if (IS_ENABLE_DIRECTX())
8740 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008741# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 for (i = 0; i < gui.char_height - 3; i++)
8744 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8745
8746 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8747 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8748 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8749 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8750 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8751 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8752 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8753}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008754#endif