blob: 19db1a33ad3e73993b7177b56837091dab4115dd [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 Moolenaarcf7164a2016-02-20 13:55:06 +0100845 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
846 modifiers &= ~MOD_MASK_SHIFT;
847
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200848 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
849 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100850 if (ch == CSI)
851 ch = K_CSI;
852
853 len = 0;
854 if (modifiers)
855 {
856 string[len++] = CSI;
857 string[len++] = KS_MODIFIER;
858 string[len++] = modifiers;
859 }
860
861 if (IS_SPECIAL((int)ch))
862 {
863 string[len++] = CSI;
864 string[len++] = K_SECOND((int)ch);
865 string[len++] = K_THIRD((int)ch);
866 }
867 else
868 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100869 // Although the documentation isn't clear about it, we assume "ch" is
870 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100871 len += char_to_string(ch, string + len, 40 - len, TRUE);
872 }
873
874 add_to_input_buf(string, len);
875}
876
877 static void
878_OnMouseEvent(
879 int button,
880 int x,
881 int y,
882 int repeated_click,
883 UINT keyFlags)
884{
885 int vim_modifiers = 0x0;
886
887 s_getting_focus = FALSE;
888
889 if (keyFlags & MK_SHIFT)
890 vim_modifiers |= MOUSE_SHIFT;
891 if (keyFlags & MK_CONTROL)
892 vim_modifiers |= MOUSE_CTRL;
893 if (GetKeyState(VK_MENU) & 0x8000)
894 vim_modifiers |= MOUSE_ALT;
895
896 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
897}
898
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100899 static void
900_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100901 HWND hwnd UNUSED,
902 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 int x,
904 int y,
905 UINT keyFlags)
906{
907 static LONG s_prevTime = 0;
908
909 LONG currentTime = GetMessageTime();
910 int button = -1;
911 int repeated_click;
912
Bram Moolenaar734a8672019-12-02 22:49:38 +0100913 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100914 (void)SetFocus(s_hwnd);
915
916 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
917 button = MOUSE_LEFT;
918 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
919 button = MOUSE_MIDDLE;
920 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
921 button = MOUSE_RIGHT;
922 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
923 {
924#ifndef GET_XBUTTON_WPARAM
925# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
926#endif
927 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
928 }
929 else if (s_uMsg == WM_CAPTURECHANGED)
930 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100931 // on W95/NT4, somehow you get in here with an odd Msg
932 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933 if (s_button_pending == MOUSE_LEFT)
934 button = MOUSE_RIGHT;
935 else
936 button = MOUSE_LEFT;
937 }
938 if (button >= 0)
939 {
940 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
941
942 /*
943 * Holding down the left and right buttons simulates pushing the middle
944 * button.
945 */
946 if (repeated_click
947 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
948 || (button == MOUSE_RIGHT
949 && s_button_pending == MOUSE_LEFT)))
950 {
951 /*
952 * Hmm, gui.c will ignore more than one button down at a time, so
953 * pretend we let go of it first.
954 */
955 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
956 button = MOUSE_MIDDLE;
957 repeated_click = FALSE;
958 s_button_pending = -1;
959 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
960 }
961 else if ((repeated_click)
962 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
963 {
964 if (s_button_pending > -1)
965 {
966 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
967 s_button_pending = -1;
968 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100969 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100970 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
971 }
972 else
973 {
974 /*
975 * If this is the first press (i.e. not a multiple click) don't
976 * action immediately, but store and wait for:
977 * i) button-up
978 * ii) mouse move
979 * iii) another button press
980 * before using it.
981 * This enables us to make left+right simulate middle button,
982 * without left or right being actioned first. The side-effect is
983 * that if you click and hold the mouse without dragging, the
984 * cursor doesn't move until you release the button. In practice
985 * this is hardly a problem.
986 */
987 s_button_pending = button;
988 s_x_pending = x;
989 s_y_pending = y;
990 s_kFlags_pending = keyFlags;
991 }
992
993 s_prevTime = currentTime;
994 }
995}
996
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100997 static void
998_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100999 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001000 int x,
1001 int y,
1002 UINT keyFlags)
1003{
1004 int button;
1005
1006 s_getting_focus = FALSE;
1007 if (s_button_pending > -1)
1008 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001009 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001010 _OnMouseEvent(s_button_pending, s_x_pending,
1011 s_y_pending, FALSE, s_kFlags_pending);
1012 s_button_pending = -1;
1013 }
1014 if (s_uMsg == WM_MOUSEMOVE)
1015 {
1016 /*
1017 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1018 * down.
1019 */
1020 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1021 | MK_XBUTTON1 | MK_XBUTTON2)))
1022 {
1023 gui_mouse_moved(x, y);
1024 return;
1025 }
1026
1027 /*
1028 * While button is down, keep grabbing mouse move events when
1029 * the mouse goes outside the window
1030 */
1031 SetCapture(s_textArea);
1032 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001033 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001034 }
1035 else
1036 {
1037 ReleaseCapture();
1038 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001039 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001040 }
1041
1042 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1043}
1044
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001045 static void
1046_OnSizeTextArea(
1047 HWND hwnd UNUSED,
1048 UINT state UNUSED,
1049 int cx UNUSED,
1050 int cy UNUSED)
1051{
1052#if defined(FEAT_DIRECTX)
1053 if (IS_ENABLE_DIRECTX())
1054 directx_binddc();
1055#endif
1056}
1057
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001058#ifdef FEAT_MENU
1059/*
1060 * Find the vimmenu_T with the given id
1061 */
1062 static vimmenu_T *
1063gui_mswin_find_menu(
1064 vimmenu_T *pMenu,
1065 int id)
1066{
1067 vimmenu_T *pChildMenu;
1068
1069 while (pMenu)
1070 {
1071 if (pMenu->id == (UINT)id)
1072 break;
1073 if (pMenu->children != NULL)
1074 {
1075 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1076 if (pChildMenu)
1077 {
1078 pMenu = pChildMenu;
1079 break;
1080 }
1081 }
1082 pMenu = pMenu->next;
1083 }
1084 return pMenu;
1085}
1086
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001087 static void
1088_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001089 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001090 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001091 HWND hwndCtl UNUSED,
1092 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001093{
1094 vimmenu_T *pMenu;
1095
1096 pMenu = gui_mswin_find_menu(root_menu, id);
1097 if (pMenu)
1098 gui_menu_cb(pMenu);
1099}
1100#endif
1101
1102#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001104 * Handle a Find/Replace window message.
1105 */
1106 static void
1107_OnFindRepl(void)
1108{
1109 int flags = 0;
1110 int down;
1111
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001112 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001113 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 (void)SetFocus(s_hwnd);
1115
1116 if (s_findrep_struct.Flags & FR_FINDNEXT)
1117 {
1118 flags = FRD_FINDNEXT;
1119
Bram Moolenaar734a8672019-12-02 22:49:38 +01001120 // Give main window the focus back: this is so the cursor isn't
1121 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001122 (void)SetFocus(s_hwnd);
1123 }
1124 else if (s_findrep_struct.Flags & FR_REPLACE)
1125 {
1126 flags = FRD_REPLACE;
1127
Bram Moolenaar734a8672019-12-02 22:49:38 +01001128 // Give main window the focus back: this is so the cursor isn't
1129 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001130 (void)SetFocus(s_hwnd);
1131 }
1132 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1133 {
1134 flags = FRD_REPLACEALL;
1135 }
1136
1137 if (flags != 0)
1138 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001139 char_u *p, *q;
1140
Bram Moolenaar734a8672019-12-02 22:49:38 +01001141 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001142 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1143 flags |= FRD_WHOLE_WORD;
1144 if (s_findrep_struct.Flags & FR_MATCHCASE)
1145 flags |= FRD_MATCH_CASE;
1146 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001147 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1148 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1149 if (p != NULL && q != NULL)
1150 gui_do_findrepl(flags, p, q, down);
1151 vim_free(p);
1152 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001153 }
1154}
1155#endif
1156
1157 static void
1158HandleMouseHide(UINT uMsg, LPARAM lParam)
1159{
1160 static LPARAM last_lParam = 0L;
1161
Bram Moolenaar734a8672019-12-02 22:49:38 +01001162 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001163 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1164 {
1165 if (lParam == last_lParam)
1166 return;
1167 last_lParam = lParam;
1168 }
1169
Bram Moolenaar734a8672019-12-02 22:49:38 +01001170 // Handle specially, to centralise coding. We need to be sure we catch all
1171 // possible events which should cause us to restore the cursor (as it is a
1172 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173 switch (uMsg)
1174 {
1175 case WM_KEYUP:
1176 case WM_CHAR:
1177 /*
1178 * blank out the pointer if necessary
1179 */
1180 if (p_mh)
1181 gui_mch_mousehide(TRUE);
1182 break;
1183
Bram Moolenaar734a8672019-12-02 22:49:38 +01001184 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001185 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001186 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001187 case WM_LBUTTONDOWN:
1188 case WM_LBUTTONUP:
1189 case WM_MBUTTONDOWN:
1190 case WM_MBUTTONUP:
1191 case WM_RBUTTONDOWN:
1192 case WM_RBUTTONUP:
1193 case WM_XBUTTONDOWN:
1194 case WM_XBUTTONUP:
1195 case WM_NCMOUSEMOVE:
1196 case WM_NCLBUTTONDOWN:
1197 case WM_NCLBUTTONUP:
1198 case WM_NCMBUTTONDOWN:
1199 case WM_NCMBUTTONUP:
1200 case WM_NCRBUTTONDOWN:
1201 case WM_NCRBUTTONUP:
1202 case WM_KILLFOCUS:
1203 /*
1204 * if the pointer is currently hidden, then we should show it.
1205 */
1206 gui_mch_mousehide(FALSE);
1207 break;
1208 }
1209}
1210
1211 static LRESULT CALLBACK
1212_TextAreaWndProc(
1213 HWND hwnd,
1214 UINT uMsg,
1215 WPARAM wParam,
1216 LPARAM lParam)
1217{
1218 /*
1219 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1220 hwnd, uMsg, wParam, lParam);
1221 */
1222
1223 HandleMouseHide(uMsg, lParam);
1224
1225 s_uMsg = uMsg;
1226 s_wParam = wParam;
1227 s_lParam = lParam;
1228
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001229#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001230 TrackUserActivity(uMsg);
1231#endif
1232
1233 switch (uMsg)
1234 {
1235 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1236 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1237 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1238 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1239 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1240 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1241 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1242 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1243 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1247 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1248 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001249 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001250
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001251#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001252 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1253 return TRUE;
1254#endif
1255 default:
1256 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1257 }
1258}
1259
Bram Moolenaar945c8572020-07-17 22:17:03 +02001260 static LRESULT WINAPI
1261MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001262{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001263#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001264 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001266 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001267#endif
1268}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001269
1270/*
1271 * Called when the foreground or background color has been changed.
1272 */
1273 void
1274gui_mch_new_colors(void)
1275{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001276 HBRUSH prevBrush;
1277
1278 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001279 prevBrush = (HBRUSH)SetClassLongPtr(
1280 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001281 InvalidateRect(s_hwnd, NULL, TRUE);
1282 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001283}
1284
1285/*
1286 * Set the colors to their default values.
1287 */
1288 void
1289gui_mch_def_colors(void)
1290{
1291 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1292 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1293 gui.def_norm_pixel = gui.norm_pixel;
1294 gui.def_back_pixel = gui.back_pixel;
1295}
1296
1297/*
1298 * Open the GUI window which was created by a call to gui_mch_init().
1299 */
1300 int
1301gui_mch_open(void)
1302{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001303 // Actually open the window, if not already visible
1304 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001305 if (!IsWindowVisible(s_hwnd))
1306 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1307
1308#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001309 // Init replace string here, so that we keep it when re-opening the
1310 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001311 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1312#endif
1313
1314 return OK;
1315}
1316
1317/*
1318 * Get the position of the top left corner of the window.
1319 */
1320 int
1321gui_mch_get_winpos(int *x, int *y)
1322{
1323 RECT rect;
1324
1325 GetWindowRect(s_hwnd, &rect);
1326 *x = rect.left;
1327 *y = rect.top;
1328 return OK;
1329}
1330
1331/*
1332 * Set the position of the top left corner of the window to the given
1333 * coordinates.
1334 */
1335 void
1336gui_mch_set_winpos(int x, int y)
1337{
1338 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1339 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1340}
1341 void
1342gui_mch_set_text_area_pos(int x, int y, int w, int h)
1343{
1344 static int oldx = 0;
1345 static int oldy = 0;
1346
1347 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1348
1349#ifdef FEAT_TOOLBAR
1350 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1351 SendMessage(s_toolbarhwnd, WM_SIZE,
1352 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1353#endif
1354#if defined(FEAT_GUI_TABLINE)
1355 if (showing_tabline)
1356 {
1357 int top = 0;
1358 RECT rect;
1359
1360# ifdef FEAT_TOOLBAR
1361 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1362 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1363# endif
1364 GetClientRect(s_hwnd, &rect);
1365 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1366 }
1367#endif
1368
Bram Moolenaar734a8672019-12-02 22:49:38 +01001369 // When side scroll bar is unshown, the size of window will change.
1370 // then, the text area move left or right. thus client rect should be
1371 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001372 if (oldx != x || oldy != y)
1373 {
1374 InvalidateRect(s_hwnd, NULL, FALSE);
1375 oldx = x;
1376 oldy = y;
1377 }
1378}
1379
1380
1381/*
1382 * Scrollbar stuff:
1383 */
1384
1385 void
1386gui_mch_enable_scrollbar(
1387 scrollbar_T *sb,
1388 int flag)
1389{
1390 ShowScrollBar(sb->id, SB_CTL, flag);
1391
Bram Moolenaar734a8672019-12-02 22:49:38 +01001392 // TODO: When the window is maximized, the size of the window stays the
1393 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1394 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001395}
1396
1397 void
1398gui_mch_set_scrollbar_pos(
1399 scrollbar_T *sb,
1400 int x,
1401 int y,
1402 int w,
1403 int h)
1404{
1405 SetWindowPos(sb->id, NULL, x, y, w, h,
1406 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1407}
1408
Bram Moolenaar203ec772020-07-17 20:43:43 +02001409 int
1410gui_mch_get_scrollbar_xpadding(void)
1411{
1412 RECT rcTxt, rcWnd;
1413 int xpad;
1414
1415 GetWindowRect(s_textArea, &rcTxt);
1416 GetWindowRect(s_hwnd, &rcWnd);
1417 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1418 - GetSystemMetrics(SM_CXFRAME)
1419 - GetSystemMetrics(SM_CXPADDEDBORDER);
1420 return (xpad < 0) ? 0 : xpad;
1421}
1422
1423 int
1424gui_mch_get_scrollbar_ypadding(void)
1425{
1426 RECT rcTxt, rcWnd;
1427 int ypad;
1428
1429 GetWindowRect(s_textArea, &rcTxt);
1430 GetWindowRect(s_hwnd, &rcWnd);
1431 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1432 - GetSystemMetrics(SM_CYFRAME)
1433 - GetSystemMetrics(SM_CXPADDEDBORDER);
1434 return (ypad < 0) ? 0 : ypad;
1435}
1436
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001437 void
1438gui_mch_create_scrollbar(
1439 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001440 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001441{
1442 sb->id = CreateWindow(
1443 "SCROLLBAR", "Scrollbar",
1444 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001445 10, // Any value will do for now
1446 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001447 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001448 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001449}
1450
1451/*
1452 * Find the scrollbar with the given hwnd.
1453 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001454 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001455gui_mswin_find_scrollbar(HWND hwnd)
1456{
1457 win_T *wp;
1458
1459 if (gui.bottom_sbar.id == hwnd)
1460 return &gui.bottom_sbar;
1461 FOR_ALL_WINDOWS(wp)
1462 {
1463 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1464 return &wp->w_scrollbars[SBAR_LEFT];
1465 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1466 return &wp->w_scrollbars[SBAR_RIGHT];
1467 }
1468 return NULL;
1469}
1470
1471/*
1472 * Get the character size of a font.
1473 */
1474 static void
1475GetFontSize(GuiFont font)
1476{
1477 HWND hwnd = GetDesktopWindow();
1478 HDC hdc = GetWindowDC(hwnd);
1479 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001480 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001481 TEXTMETRIC tm;
1482
1483 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001484 // GetTextMetrics() may not return the right value in tmAveCharWidth
1485 // for some fonts. Do our own average computation.
1486 GetTextExtentPoint(hdc,
1487 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1488 52, &size);
1489 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001490
1491 gui.char_height = tm.tmHeight + p_linespace;
1492
1493 SelectFont(hdc, hfntOld);
1494
1495 ReleaseDC(hwnd, hdc);
1496}
1497
1498/*
1499 * Adjust gui.char_height (after 'linespace' was changed).
1500 */
1501 int
1502gui_mch_adjust_charheight(void)
1503{
1504 GetFontSize(gui.norm_font);
1505 return OK;
1506}
1507
1508 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001509get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001510{
1511 HFONT font = NULL;
1512
Bram Moolenaar734a8672019-12-02 22:49:38 +01001513 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001514 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515
1516 if (font == NULL)
1517 return NOFONT;
1518
1519 return (GuiFont)font;
1520}
1521
1522 static int
1523pixels_to_points(int pixels, int vertical)
1524{
1525 int points;
1526 HWND hwnd;
1527 HDC hdc;
1528
1529 hwnd = GetDesktopWindow();
1530 hdc = GetWindowDC(hwnd);
1531
1532 points = MulDiv(pixels, 72,
1533 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1534
1535 ReleaseDC(hwnd, hdc);
1536
1537 return points;
1538}
1539
1540 GuiFont
1541gui_mch_get_font(
1542 char_u *name,
1543 int giveErrorIfMissing)
1544{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001545 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001546 GuiFont font = NOFONT;
1547
1548 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1549 font = get_font_handle(&lf);
1550 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001552 return font;
1553}
1554
1555#if defined(FEAT_EVAL) || defined(PROTO)
1556/*
1557 * Return the name of font "font" in allocated memory.
1558 * Don't know how to get the actual name, thus use the provided name.
1559 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001560 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001561gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001562{
1563 if (name == NULL)
1564 return NULL;
1565 return vim_strsave(name);
1566}
1567#endif
1568
1569 void
1570gui_mch_free_font(GuiFont font)
1571{
1572 if (font)
1573 DeleteObject((HFONT)font);
1574}
1575
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001576/*
1577 * Return the Pixel value (color) for the given color name.
1578 * Return INVALCOLOR for error.
1579 */
1580 guicolor_T
1581gui_mch_get_color(char_u *name)
1582{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001583 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
1585 typedef struct SysColorTable
1586 {
1587 char *name;
1588 int color;
1589 } SysColorTable;
1590
1591 static SysColorTable sys_table[] =
1592 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001593 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1594 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001595#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1597#endif
1598 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1599 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1600 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1601 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1602 {"SYS_DESKTOP", COLOR_DESKTOP},
1603 {"SYS_INFOBK", COLOR_INFOBK},
1604 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1605 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001606 {"SYS_BTNFACE", COLOR_BTNFACE},
1607 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1608 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1609 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1610 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1611 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1612 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1613 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1614 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1615 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1616 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1617 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1618 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1619 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1620 {"SYS_MENU", COLOR_MENU},
1621 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1622 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1623 {"SYS_WINDOW", COLOR_WINDOW},
1624 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1625 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1626 };
1627
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001628 /*
1629 * Try to look up a system colour.
1630 */
1631 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1632 if (STRICMP(name, sys_table[i].name) == 0)
1633 return GetSysColor(sys_table[i].color);
1634
Bram Moolenaarab302212016-04-26 20:59:29 +02001635 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001637
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001638 guicolor_T
1639gui_mch_get_rgb_color(int r, int g, int b)
1640{
1641 return gui_get_rgb_color_cmn(r, g, b);
1642}
1643
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644/*
1645 * Return OK if the key with the termcap name "name" is supported.
1646 */
1647 int
1648gui_mch_haskey(char_u *name)
1649{
1650 int i;
1651
1652 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1653 if (name[0] == special_keys[i].vim_code0 &&
1654 name[1] == special_keys[i].vim_code1)
1655 return OK;
1656 return FAIL;
1657}
1658
1659 void
1660gui_mch_beep(void)
1661{
1662 MessageBeep(MB_OK);
1663}
1664/*
1665 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1666 */
1667 void
1668gui_mch_invert_rectangle(
1669 int r,
1670 int c,
1671 int nr,
1672 int nc)
1673{
1674 RECT rc;
1675
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001676#if defined(FEAT_DIRECTX)
1677 if (IS_ENABLE_DIRECTX())
1678 DWriteContext_Flush(s_dwc);
1679#endif
1680
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001681 /*
1682 * Note: InvertRect() excludes right and bottom of rectangle.
1683 */
1684 rc.left = FILL_X(c);
1685 rc.top = FILL_Y(r);
1686 rc.right = rc.left + nc * gui.char_width;
1687 rc.bottom = rc.top + nr * gui.char_height;
1688 InvertRect(s_hdc, &rc);
1689}
1690
1691/*
1692 * Iconify the GUI window.
1693 */
1694 void
1695gui_mch_iconify(void)
1696{
1697 ShowWindow(s_hwnd, SW_MINIMIZE);
1698}
1699
1700/*
1701 * Draw a cursor without focus.
1702 */
1703 void
1704gui_mch_draw_hollow_cursor(guicolor_T color)
1705{
1706 HBRUSH hbr;
1707 RECT rc;
1708
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001709#if defined(FEAT_DIRECTX)
1710 if (IS_ENABLE_DIRECTX())
1711 DWriteContext_Flush(s_dwc);
1712#endif
1713
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001714 /*
1715 * Note: FrameRect() excludes right and bottom of rectangle.
1716 */
1717 rc.left = FILL_X(gui.col);
1718 rc.top = FILL_Y(gui.row);
1719 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001720 if (mb_lefthalve(gui.row, gui.col))
1721 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001722 rc.bottom = rc.top + gui.char_height;
1723 hbr = CreateSolidBrush(color);
1724 FrameRect(s_hdc, &rc, hbr);
1725 DeleteBrush(hbr);
1726}
1727/*
1728 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1729 * color "color".
1730 */
1731 void
1732gui_mch_draw_part_cursor(
1733 int w,
1734 int h,
1735 guicolor_T color)
1736{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001737 RECT rc;
1738
1739 /*
1740 * Note: FillRect() excludes right and bottom of rectangle.
1741 */
1742 rc.left =
1743#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001744 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001745 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1746#endif
1747 FILL_X(gui.col);
1748 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1749 rc.right = rc.left + w;
1750 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001751
Bram Moolenaar92467d32017-12-05 13:22:16 +01001752 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753}
1754
1755
1756/*
1757 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1758 * dead key's nominal character and re-post the original message.
1759 */
1760 static void
1761outputDeadKey_rePost(MSG originalMsg)
1762{
1763 static MSG deadCharExpel;
1764
1765 if (!dead_key)
1766 return;
1767
1768 dead_key = 0;
1769
Bram Moolenaar734a8672019-12-02 22:49:38 +01001770 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001771 deadCharExpel.message = originalMsg.message;
1772 deadCharExpel.hwnd = originalMsg.hwnd;
1773 deadCharExpel.wParam = VK_SPACE;
1774
1775 MyTranslateMessage(&deadCharExpel);
1776
Bram Moolenaar734a8672019-12-02 22:49:38 +01001777 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001778 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1779 originalMsg.lParam);
1780}
1781
1782
1783/*
1784 * Process a single Windows message.
1785 * If one is not available we hang until one is.
1786 */
1787 static void
1788process_message(void)
1789{
1790 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001791 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 char_u string[40];
1793 int i;
1794 int modifiers = 0;
1795 int key;
1796#ifdef FEAT_MENU
1797 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1798#endif
1799
1800 pGetMessage(&msg, NULL, 0, 0);
1801
1802#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001803 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001804 if (msg.message == WM_OLE)
1805 {
1806 char_u *str = (char_u *)msg.lParam;
1807 if (str == NULL || *str == NUL)
1808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001809 // Message can't be ours, forward it. Fixes problem with Ultramon
1810 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001811 pDispatchMessage(&msg);
1812 }
1813 else
1814 {
1815 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001816 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 }
1818 return;
1819 }
1820#endif
1821
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001822#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001823 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001824 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1825 {
1826 HandleMouseHide(msg.message, msg.lParam);
1827 return;
1828 }
1829#endif
1830
1831 /*
1832 * Check if it's a special key that we recognise. If not, call
1833 * TranslateMessage().
1834 */
1835 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1836 {
1837 vk = (int) msg.wParam;
1838
1839 /*
1840 * Handle dead keys in special conditions in other cases we let Windows
1841 * handle them and do not interfere.
1842 *
1843 * The dead_key flag must be reset on several occasions:
1844 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1845 * consumed at that point (This is when we let Windows combine the
1846 * dead character on its own)
1847 *
1848 * - Before doing something special such as regenerating keypresses to
1849 * expel the dead character as this could trigger an infinite loop if
1850 * for some reason MyTranslateMessage() do not trigger a call
1851 * immediately to _OnChar() (or _OnSysChar()).
1852 */
1853 if (dead_key)
1854 {
1855 /*
1856 * If a dead key was pressed and the user presses VK_SPACE,
1857 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1858 * with the dead char now, so do nothing special and let Windows
1859 * handle it.
1860 *
1861 * Note that VK_SPACE combines with the dead_key's character and
1862 * only one WM_CHAR will be generated by TranslateMessage(), in
1863 * the two other cases two WM_CHAR will be generated: the dead
1864 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1865 * user expects.
1866 */
1867 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1868 {
1869 dead_key = 0;
1870 MyTranslateMessage(&msg);
1871 return;
1872 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001873 // In modes where we are not typing, dead keys should behave
1874 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001875 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1876 {
1877 outputDeadKey_rePost(msg);
1878 return;
1879 }
1880 }
1881
Bram Moolenaar734a8672019-12-02 22:49:38 +01001882 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001883 if (vk == VK_CANCEL)
1884 {
1885 trash_input_buf();
1886 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001887 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 string[0] = Ctrl_C;
1889 add_to_input_buf(string, 1);
1890 }
1891
1892 for (i = 0; special_keys[i].key_sym != 0; i++)
1893 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001894 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001895 if (special_keys[i].key_sym == vk
1896 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1897 {
1898 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001899 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001900 * is a key that would normally trigger the dead key nominal
1901 * character output (such as a NUMPAD printable character or
1902 * the TAB key, etc...).
1903 */
1904 if (dead_key && (special_keys[i].vim_code0 == 'K'
1905 || vk == VK_TAB || vk == CAR))
1906 {
1907 outputDeadKey_rePost(msg);
1908 return;
1909 }
1910
1911#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001912 // Check for <F10>: Windows selects the menu. When <F10> is
1913 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001914 if (vk == VK_F10
1915 && gui.menu_is_active
1916 && check_map(k10, State, FALSE, TRUE, FALSE,
1917 NULL, NULL) == NULL)
1918 break;
1919#endif
1920 if (GetKeyState(VK_SHIFT) & 0x8000)
1921 modifiers |= MOD_MASK_SHIFT;
1922 /*
1923 * Don't use caps-lock as shift, because these are special keys
1924 * being considered here, and we only want letters to get
1925 * shifted -- webb
1926 */
1927 /*
1928 if (GetKeyState(VK_CAPITAL) & 0x0001)
1929 modifiers ^= MOD_MASK_SHIFT;
1930 */
1931 if (GetKeyState(VK_CONTROL) & 0x8000)
1932 modifiers |= MOD_MASK_CTRL;
1933 if (GetKeyState(VK_MENU) & 0x8000)
1934 modifiers |= MOD_MASK_ALT;
1935
1936 if (special_keys[i].vim_code1 == NUL)
1937 key = special_keys[i].vim_code0;
1938 else
1939 key = TO_SPECIAL(special_keys[i].vim_code0,
1940 special_keys[i].vim_code1);
1941 key = simplify_key(key, &modifiers);
1942 if (key == CSI)
1943 key = K_CSI;
1944
1945 if (modifiers)
1946 {
1947 string[0] = CSI;
1948 string[1] = KS_MODIFIER;
1949 string[2] = modifiers;
1950 add_to_input_buf(string, 3);
1951 }
1952
1953 if (IS_SPECIAL(key))
1954 {
1955 string[0] = CSI;
1956 string[1] = K_SECOND(key);
1957 string[2] = K_THIRD(key);
1958 add_to_input_buf(string, 3);
1959 }
1960 else
1961 {
1962 int len;
1963
Bram Moolenaar734a8672019-12-02 22:49:38 +01001964 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001965 len = char_to_string(key, string, 40, FALSE);
1966 add_to_input_buf(string, len);
1967 }
1968 break;
1969 }
1970 }
1971 if (special_keys[i].key_sym == 0)
1972 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001973 // Some keys need C-S- where they should only need C-.
1974 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1975 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001976 if (vk != 0xff
1977 && (GetKeyState(VK_CONTROL) & 0x8000)
1978 && !(GetKeyState(VK_SHIFT) & 0x8000)
1979 && !(GetKeyState(VK_MENU) & 0x8000))
1980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001981 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001982 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1983 {
1984 string[0] = Ctrl_HAT;
1985 add_to_input_buf(string, 1);
1986 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001987 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
1988 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001989 {
1990 string[0] = Ctrl__;
1991 add_to_input_buf(string, 1);
1992 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001993 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001994 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1995 {
1996 string[0] = Ctrl_AT;
1997 add_to_input_buf(string, 1);
1998 }
1999 else
2000 MyTranslateMessage(&msg);
2001 }
2002 else
2003 MyTranslateMessage(&msg);
2004 }
2005 }
2006#ifdef FEAT_MBYTE_IME
2007 else if (msg.message == WM_IME_NOTIFY)
2008 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2009 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002010 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002011 MyTranslateMessage(&msg);
2012#endif
2013#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002014// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002015 else if (msg.message == WM_IME_STARTCOMPOSITION)
2016 {
2017 POINT point;
2018
2019 global_ime_set_font(&norm_logfont);
2020 point.x = FILL_X(gui.col);
2021 point.y = FILL_Y(gui.row);
2022 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2023 global_ime_set_position(&point);
2024 }
2025#endif
2026
2027#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002028 // Check for <F10>: Default effect is to select the menu. When <F10> is
2029 // mapped we need to stop it here to avoid strange effects (e.g., for the
2030 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002031 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2032 NULL, NULL) == NULL)
2033#endif
2034 pDispatchMessage(&msg);
2035}
2036
2037/*
2038 * Catch up with any queued events. This may put keyboard input into the
2039 * input buffer, call resize call-backs, trigger timers etc. If there is
2040 * nothing in the event queue (& no timers pending), then we return
2041 * immediately.
2042 */
2043 void
2044gui_mch_update(void)
2045{
2046 MSG msg;
2047
2048 if (!s_busy_processing)
2049 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2050 && !vim_is_input_buf_full())
2051 process_message();
2052}
2053
Bram Moolenaar4231da42016-06-02 14:30:04 +02002054 static void
2055remove_any_timer(void)
2056{
2057 MSG msg;
2058
2059 if (s_wait_timer != 0 && !s_timed_out)
2060 {
2061 KillTimer(NULL, s_wait_timer);
2062
Bram Moolenaar734a8672019-12-02 22:49:38 +01002063 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002064 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2065 ;
2066 s_wait_timer = 0;
2067 }
2068}
2069
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002070/*
2071 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2072 * from the keyboard.
2073 * wtime == -1 Wait forever.
2074 * wtime == 0 This should never happen.
2075 * wtime > 0 Wait wtime milliseconds for a character.
2076 * Returns OK if a character was found to be available within the given time,
2077 * or FAIL otherwise.
2078 */
2079 int
2080gui_mch_wait_for_chars(int wtime)
2081{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002082 int focus;
2083
2084 s_timed_out = FALSE;
2085
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002086 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002088 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002089 if (s_busy_processing)
2090 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002091
2092 // When called with "wtime" zero, just want one msec.
2093 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094 (TIMERPROC)_OnTimer);
2095 }
2096
2097 allow_scrollbar = TRUE;
2098
2099 focus = gui.in_focus;
2100 while (!s_timed_out)
2101 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002102 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002103 if (gui.in_focus != focus)
2104 {
2105 if (gui.in_focus)
2106 gui_mch_start_blink();
2107 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002108 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002109 focus = gui.in_focus;
2110 }
2111
2112 if (s_need_activate)
2113 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002115 s_need_activate = FALSE;
2116 }
2117
Bram Moolenaar4231da42016-06-02 14:30:04 +02002118#ifdef FEAT_TIMERS
2119 did_add_timer = FALSE;
2120#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002122 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002123 for (;;)
2124 {
2125 MSG msg;
2126
2127 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002128# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002129 if (did_add_timer)
2130 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002131# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002132 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2133 {
2134 process_message();
2135 break;
2136 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002137 else if (input_available()
2138 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2139 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002140 break;
2141 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002142#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002143 // Don't use gui_mch_update() because then we will spin-lock until a
2144 // char arrives, instead we use GetMessage() to hang until an
2145 // event arrives. No need to check for input_buf_full because we are
2146 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002147 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002148#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149
2150 if (input_available())
2151 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153 allow_scrollbar = FALSE;
2154
Bram Moolenaar89c00032019-09-04 13:53:21 +02002155 // Clear pending mouse button, the release event may have been
2156 // taken by the dialog window. But don't do this when getting
2157 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002158 if (!s_getting_focus)
2159 s_button_pending = -1;
2160
2161 return OK;
2162 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002163
2164#ifdef FEAT_TIMERS
2165 if (did_add_timer)
2166 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002167 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002168 remove_any_timer();
2169 break;
2170 }
2171#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002172 }
2173 allow_scrollbar = FALSE;
2174 return FAIL;
2175}
2176
2177/*
2178 * Clear a rectangular region of the screen from text pos (row1, col1) to
2179 * (row2, col2) inclusive.
2180 */
2181 void
2182gui_mch_clear_block(
2183 int row1,
2184 int col1,
2185 int row2,
2186 int col2)
2187{
2188 RECT rc;
2189
2190 /*
2191 * Clear one extra pixel at the far right, for when bold characters have
2192 * spilled over to the window border.
2193 * Note: FillRect() excludes right and bottom of rectangle.
2194 */
2195 rc.left = FILL_X(col1);
2196 rc.top = FILL_Y(row1);
2197 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2198 rc.bottom = FILL_Y(row2 + 1);
2199 clear_rect(&rc);
2200}
2201
2202/*
2203 * Clear the whole text window.
2204 */
2205 void
2206gui_mch_clear_all(void)
2207{
2208 RECT rc;
2209
2210 rc.left = 0;
2211 rc.top = 0;
2212 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2213 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2214 clear_rect(&rc);
2215}
2216/*
2217 * Menu stuff.
2218 */
2219
2220 void
2221gui_mch_enable_menu(int flag)
2222{
2223#ifdef FEAT_MENU
2224 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2225#endif
2226}
2227
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002228 void
2229gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002230 int x UNUSED,
2231 int y UNUSED,
2232 int w UNUSED,
2233 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002234{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002235 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002236}
2237
2238#if defined(FEAT_MENU) || defined(PROTO)
2239/*
2240 * Make menu item hidden or not hidden
2241 */
2242 void
2243gui_mch_menu_hidden(
2244 vimmenu_T *menu,
2245 int hidden)
2246{
2247 /*
2248 * This doesn't do what we want. Hmm, just grey the menu items for now.
2249 */
2250 /*
2251 if (hidden)
2252 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2253 else
2254 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2255 */
2256 gui_mch_menu_grey(menu, hidden);
2257}
2258
2259/*
2260 * This is called after setting all the menus to grey/hidden or not.
2261 */
2262 void
2263gui_mch_draw_menubar(void)
2264{
2265 DrawMenuBar(s_hwnd);
2266}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002267#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002268
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269/*
2270 * Return the RGB value of a pixel as a long.
2271 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002272 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273gui_mch_get_rgb(guicolor_T pixel)
2274{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002275 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2276 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002277}
2278
2279#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002280/*
2281 * Convert pixels in X to dialog units
2282 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002283 static WORD
2284PixelToDialogX(int numPixels)
2285{
2286 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2287}
2288
Bram Moolenaar734a8672019-12-02 22:49:38 +01002289/*
2290 * Convert pixels in Y to dialog units
2291 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292 static WORD
2293PixelToDialogY(int numPixels)
2294{
2295 return (WORD)((numPixels * 8) / s_dlgfntheight);
2296}
2297
Bram Moolenaar734a8672019-12-02 22:49:38 +01002298/*
2299 * Return the width in pixels of the given text in the given DC.
2300 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301 static int
2302GetTextWidth(HDC hdc, char_u *str, int len)
2303{
2304 SIZE size;
2305
2306 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2307 return size.cx;
2308}
2309
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310/*
2311 * Return the width in pixels of the given text in the given DC, taking care
2312 * of 'encoding' to active codepage conversion.
2313 */
2314 static int
2315GetTextWidthEnc(HDC hdc, char_u *str, int len)
2316{
2317 SIZE size;
2318 WCHAR *wstr;
2319 int n;
2320 int wlen = len;
2321
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002322 wstr = enc_to_utf16(str, &wlen);
2323 if (wstr == NULL)
2324 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002325
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002326 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2327 vim_free(wstr);
2328 if (n)
2329 return size.cx;
2330 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002332
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002333static void get_work_area(RECT *spi_rect);
2334
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335/*
2336 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002337 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2338 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 */
2340 static BOOL
2341CenterWindow(
2342 HWND hwndChild,
2343 HWND hwndParent)
2344{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002345 HMONITOR mon;
2346 MONITORINFO moninfo;
2347 RECT rChild, rParent, rScreen;
2348 int wChild, hChild, wParent, hParent;
2349 int xNew, yNew;
2350 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351
2352 GetWindowRect(hwndChild, &rChild);
2353 wChild = rChild.right - rChild.left;
2354 hChild = rChild.bottom - rChild.top;
2355
Bram Moolenaar734a8672019-12-02 22:49:38 +01002356 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002358 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359 else
2360 GetWindowRect(hwndParent, &rParent);
2361 wParent = rParent.right - rParent.left;
2362 hParent = rParent.bottom - rParent.top;
2363
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002364 moninfo.cbSize = sizeof(MONITORINFO);
2365 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2366 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002368 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002372 hdc = GetDC(hwndChild);
2373 rScreen.left = 0;
2374 rScreen.top = 0;
2375 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2376 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2377 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378 }
2379
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002380 xNew = rParent.left + ((wParent - wChild) / 2);
2381 if (xNew < rScreen.left)
2382 xNew = rScreen.left;
2383 else if ((xNew + wChild) > rScreen.right)
2384 xNew = rScreen.right - wChild;
2385
2386 yNew = rParent.top + ((hParent - hChild) / 2);
2387 if (yNew < rScreen.top)
2388 yNew = rScreen.top;
2389 else if ((yNew + hChild) > rScreen.bottom)
2390 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002391
2392 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2393 SWP_NOSIZE | SWP_NOZORDER);
2394}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002395#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397#if defined(FEAT_TOOLBAR) || defined(PROTO)
2398 void
2399gui_mch_show_toolbar(int showit)
2400{
2401 if (s_toolbarhwnd == NULL)
2402 return;
2403
2404 if (showit)
2405 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002406# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002407 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002408# define TB_SETUNICODEFORMAT 0x2005
2409# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002410 // Enable unicode support
2411 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2412 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413 ShowWindow(s_toolbarhwnd, SW_SHOW);
2414 }
2415 else
2416 ShowWindow(s_toolbarhwnd, SW_HIDE);
2417}
2418
Bram Moolenaar734a8672019-12-02 22:49:38 +01002419// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002420# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002421
2422#endif
2423
2424#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2425 static void
2426add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2427{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002428 WCHAR *wn;
2429 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002430
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002431 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002433 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002435 infow.cbSize = sizeof(infow);
2436 infow.fMask = MIIM_TYPE | MIIM_ID;
2437 infow.wID = item_id;
2438 infow.fType = MFT_STRING;
2439 infow.dwTypeData = wn;
2440 infow.cch = (UINT)wcslen(wn);
2441 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2442 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443}
2444
2445 static void
2446show_tabline_popup_menu(void)
2447{
2448 HMENU tab_pmenu;
2449 long rval;
2450 POINT pt;
2451
Bram Moolenaar734a8672019-12-02 22:49:38 +01002452 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 if (hold_gui_events
2454# ifdef FEAT_CMDWIN
2455 || cmdwin_type != 0
2456# endif
2457 )
2458 return;
2459
2460 tab_pmenu = CreatePopupMenu();
2461 if (tab_pmenu == NULL)
2462 return;
2463
2464 if (first_tabpage->tp_next != NULL)
2465 add_tabline_popup_menu_entry(tab_pmenu,
2466 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2467 add_tabline_popup_menu_entry(tab_pmenu,
2468 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2469 add_tabline_popup_menu_entry(tab_pmenu,
2470 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2471
2472 GetCursorPos(&pt);
2473 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2474 NULL);
2475
2476 DestroyMenu(tab_pmenu);
2477
Bram Moolenaar734a8672019-12-02 22:49:38 +01002478 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479 if (rval > 0)
2480 {
2481 TCHITTESTINFO htinfo;
2482 int idx;
2483
2484 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2485 return;
2486
2487 htinfo.pt.x = pt.x;
2488 htinfo.pt.y = pt.y;
2489 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2490 if (idx == -1)
2491 idx = 0;
2492 else
2493 idx += 1;
2494
2495 send_tabline_menu_event(idx, (int)rval);
2496 }
2497}
2498
2499/*
2500 * Show or hide the tabline.
2501 */
2502 void
2503gui_mch_show_tabline(int showit)
2504{
2505 if (s_tabhwnd == NULL)
2506 return;
2507
2508 if (!showit != !showing_tabline)
2509 {
2510 if (showit)
2511 ShowWindow(s_tabhwnd, SW_SHOW);
2512 else
2513 ShowWindow(s_tabhwnd, SW_HIDE);
2514 showing_tabline = showit;
2515 }
2516}
2517
2518/*
2519 * Return TRUE when tabline is displayed.
2520 */
2521 int
2522gui_mch_showing_tabline(void)
2523{
2524 return s_tabhwnd != NULL && showing_tabline;
2525}
2526
2527/*
2528 * Update the labels of the tabline.
2529 */
2530 void
2531gui_mch_update_tabline(void)
2532{
2533 tabpage_T *tp;
2534 TCITEM tie;
2535 int nr = 0;
2536 int curtabidx = 0;
2537 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002538 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002539
2540 if (s_tabhwnd == NULL)
2541 return;
2542
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002543# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002544 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002545# define CCM_SETUNICODEFORMAT 0x2005
2546# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002547 // Enable unicode support
2548 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002549
2550 tie.mask = TCIF_TEXT;
2551 tie.iImage = -1;
2552
Bram Moolenaar734a8672019-12-02 22:49:38 +01002553 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2555
Bram Moolenaar734a8672019-12-02 22:49:38 +01002556 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002557 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2558 {
2559 if (tp == curtab)
2560 curtabidx = nr;
2561
2562 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2563 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002564 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002565 tie.pszText = "-Empty-";
2566 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2567 tabadded = 1;
2568 }
2569
2570 get_tabline_label(tp, FALSE);
2571 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002572
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002573 wstr = enc_to_utf16(NameBuff, NULL);
2574 if (wstr != NULL)
2575 {
2576 TCITEMW tiw;
2577
2578 tiw.mask = TCIF_TEXT;
2579 tiw.iImage = -1;
2580 tiw.pszText = wstr;
2581 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2582 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002584 }
2585
Bram Moolenaar734a8672019-12-02 22:49:38 +01002586 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2588 TabCtrl_DeleteItem(s_tabhwnd, nr);
2589
2590 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2591 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2592
Bram Moolenaar734a8672019-12-02 22:49:38 +01002593 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2595 RedrawWindow(s_tabhwnd, NULL, NULL,
2596 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2597
2598 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2599 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2600}
2601
2602/*
2603 * Set the current tab to "nr". First tab is 1.
2604 */
2605 void
2606gui_mch_set_curtab(int nr)
2607{
2608 if (s_tabhwnd == NULL)
2609 return;
2610
2611 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2612 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2613}
2614
2615#endif
2616
2617/*
2618 * ":simalt" command.
2619 */
2620 void
2621ex_simalt(exarg_T *eap)
2622{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002623 char_u *keys = eap->arg;
2624 int fill_typebuf = FALSE;
2625 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002626
2627 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2628 while (*keys)
2629 {
2630 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2633 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002634 fill_typebuf = TRUE;
2635 }
2636 if (fill_typebuf)
2637 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002638 // Put a NOP in the typeahead buffer so that the message will get
2639 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002640 key_name[0] = K_SPECIAL;
2641 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002642 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002643 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002644#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002645 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002646#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002647 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002648 }
2649}
2650
2651/*
2652 * Create the find & replace dialogs.
2653 * You can't have both at once: ":find" when replace is showing, destroys
2654 * the replace dialog first, and the other way around.
2655 */
2656#ifdef MSWIN_FIND_REPLACE
2657 static void
2658initialise_findrep(char_u *initial_string)
2659{
2660 int wword = FALSE;
2661 int mcase = !p_ic;
2662 char_u *entry_text;
2663
Bram Moolenaar734a8672019-12-02 22:49:38 +01002664 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002665 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2666
2667 s_findrep_struct.hwndOwner = s_hwnd;
2668 s_findrep_struct.Flags = FR_DOWN;
2669 if (mcase)
2670 s_findrep_struct.Flags |= FR_MATCHCASE;
2671 if (wword)
2672 s_findrep_struct.Flags |= FR_WHOLEWORD;
2673 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002674 {
2675 WCHAR *p = enc_to_utf16(entry_text, NULL);
2676 if (p != NULL)
2677 {
2678 int len = s_findrep_struct.wFindWhatLen - 1;
2679
2680 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2681 s_findrep_struct.lpstrFindWhat[len] = NUL;
2682 vim_free(p);
2683 }
2684 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002685 vim_free(entry_text);
2686}
2687#endif
2688
2689 static void
2690set_window_title(HWND hwnd, char *title)
2691{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002692 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693 {
2694 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002695
Bram Moolenaar734a8672019-12-02 22:49:38 +01002696 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2698 if (wbuf != NULL)
2699 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002700 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702 }
2703 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002704 else
2705 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002706}
2707
2708 void
2709gui_mch_find_dialog(exarg_T *eap)
2710{
2711#ifdef MSWIN_FIND_REPLACE
2712 if (s_findrep_msg != 0)
2713 {
2714 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2715 DestroyWindow(s_findrep_hwnd);
2716
2717 if (!IsWindow(s_findrep_hwnd))
2718 {
2719 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002720 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 }
2722
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002723 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724 (void)SetFocus(s_findrep_hwnd);
2725
2726 s_findrep_is_find = TRUE;
2727 }
2728#endif
2729}
2730
2731
2732 void
2733gui_mch_replace_dialog(exarg_T *eap)
2734{
2735#ifdef MSWIN_FIND_REPLACE
2736 if (s_findrep_msg != 0)
2737 {
2738 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2739 DestroyWindow(s_findrep_hwnd);
2740
2741 if (!IsWindow(s_findrep_hwnd))
2742 {
2743 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002744 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 }
2746
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002747 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002748 (void)SetFocus(s_findrep_hwnd);
2749
2750 s_findrep_is_find = FALSE;
2751 }
2752#endif
2753}
2754
2755
2756/*
2757 * Set visibility of the pointer.
2758 */
2759 void
2760gui_mch_mousehide(int hide)
2761{
2762 if (hide != gui.pointer_hidden)
2763 {
2764 ShowCursor(!hide);
2765 gui.pointer_hidden = hide;
2766 }
2767}
2768
2769#ifdef FEAT_MENU
2770 static void
2771gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2772{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002773 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774 gui_mch_mousehide(FALSE);
2775
2776 (void)TrackPopupMenu(
2777 (HMENU)menu->submenu_id,
2778 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2779 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002780 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781 s_hwnd,
2782 NULL);
2783 /*
2784 * NOTE: The pop-up menu can eat the mouse up event.
2785 * We deal with this in normal.c.
2786 */
2787}
2788#endif
2789
2790/*
2791 * Got a message when the system will go down.
2792 */
2793 static void
2794_OnEndSession(void)
2795{
2796 getout_preserve_modified(1);
2797}
2798
2799/*
2800 * Get this message when the user clicks on the cross in the top right corner
2801 * of a Windows95 window.
2802 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002803 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002804_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805{
2806 gui_shell_closed();
2807}
2808
2809/*
2810 * Get a message when the window is being destroyed.
2811 */
2812 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002813_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002814{
2815 if (!destroying)
2816 _OnClose(hwnd);
2817}
2818
2819 static void
2820_OnPaint(
2821 HWND hwnd)
2822{
2823 if (!IsMinimized(hwnd))
2824 {
2825 PAINTSTRUCT ps;
2826
Bram Moolenaar734a8672019-12-02 22:49:38 +01002827 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002829
Bram Moolenaar734a8672019-12-02 22:49:38 +01002830 // prevent multi-byte characters from misprinting on an invalid
2831 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002832 if (has_mbyte)
2833 {
2834 RECT rect;
2835
2836 GetClientRect(hwnd, &rect);
2837 ps.rcPaint.left = rect.left;
2838 ps.rcPaint.right = rect.right;
2839 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840
2841 if (!IsRectEmpty(&ps.rcPaint))
2842 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002843 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2844 ps.rcPaint.right - ps.rcPaint.left + 1,
2845 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2846 }
2847
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848 EndPaint(hwnd, &ps);
2849 }
2850}
2851
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 static void
2853_OnSize(
2854 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002855 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002856 int cx,
2857 int cy)
2858{
2859 if (!IsMinimized(hwnd))
2860 {
2861 gui_resize_shell(cx, cy);
2862
2863#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002864 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 gui_mswin_get_menu_height(TRUE);
2866#endif
2867 }
2868}
2869
2870 static void
2871_OnSetFocus(
2872 HWND hwnd,
2873 HWND hwndOldFocus)
2874{
2875 gui_focus_change(TRUE);
2876 s_getting_focus = TRUE;
2877 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2878}
2879
2880 static void
2881_OnKillFocus(
2882 HWND hwnd,
2883 HWND hwndNewFocus)
2884{
2885 gui_focus_change(FALSE);
2886 s_getting_focus = FALSE;
2887 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2888}
2889
2890/*
2891 * Get a message when the user switches back to vim
2892 */
2893 static LRESULT
2894_OnActivateApp(
2895 HWND hwnd,
2896 BOOL fActivate,
2897 DWORD dwThreadId)
2898{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002899 // we call gui_focus_change() in _OnSetFocus()
2900 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2902}
2903
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 void
2905gui_mch_destroy_scrollbar(scrollbar_T *sb)
2906{
2907 DestroyWindow(sb->id);
2908}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002909
2910/*
2911 * Get current mouse coordinates in text window.
2912 */
2913 void
2914gui_mch_getmouse(int *x, int *y)
2915{
2916 RECT rct;
2917 POINT mp;
2918
2919 (void)GetWindowRect(s_textArea, &rct);
2920 (void)GetCursorPos((LPPOINT)&mp);
2921 *x = (int)(mp.x - rct.left);
2922 *y = (int)(mp.y - rct.top);
2923}
2924
2925/*
2926 * Move mouse pointer to character at (x, y).
2927 */
2928 void
2929gui_mch_setmouse(int x, int y)
2930{
2931 RECT rct;
2932
2933 (void)GetWindowRect(s_textArea, &rct);
2934 (void)SetCursorPos(x + gui.border_offset + rct.left,
2935 y + gui.border_offset + rct.top);
2936}
2937
2938 static void
2939gui_mswin_get_valid_dimensions(
2940 int w,
2941 int h,
2942 int *valid_w,
2943 int *valid_h)
2944{
2945 int base_width, base_height;
2946
2947 base_width = gui_get_base_width()
2948 + (GetSystemMetrics(SM_CXFRAME) +
2949 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2950 base_height = gui_get_base_height()
2951 + (GetSystemMetrics(SM_CYFRAME) +
2952 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2953 + GetSystemMetrics(SM_CYCAPTION)
2954#ifdef FEAT_MENU
2955 + gui_mswin_get_menu_height(FALSE)
2956#endif
2957 ;
2958 *valid_w = base_width +
2959 ((w - base_width) / gui.char_width) * gui.char_width;
2960 *valid_h = base_height +
2961 ((h - base_height) / gui.char_height) * gui.char_height;
2962}
2963
2964 void
2965gui_mch_flash(int msec)
2966{
2967 RECT rc;
2968
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002969#if defined(FEAT_DIRECTX)
2970 if (IS_ENABLE_DIRECTX())
2971 DWriteContext_Flush(s_dwc);
2972#endif
2973
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002974 /*
2975 * Note: InvertRect() excludes right and bottom of rectangle.
2976 */
2977 rc.left = 0;
2978 rc.top = 0;
2979 rc.right = gui.num_cols * gui.char_width;
2980 rc.bottom = gui.num_rows * gui.char_height;
2981 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002982 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002983
Bram Moolenaar734a8672019-12-02 22:49:38 +01002984 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985
2986 InvertRect(s_hdc, &rc);
2987}
2988
2989/*
2990 * Return flags used for scrolling.
2991 * The SW_INVALIDATE is required when part of the window is covered or
2992 * off-screen. Refer to MS KB Q75236.
2993 */
2994 static int
2995get_scroll_flags(void)
2996{
2997 HWND hwnd;
2998 RECT rcVim, rcOther, rcDest;
2999
3000 GetWindowRect(s_hwnd, &rcVim);
3001
Bram Moolenaar734a8672019-12-02 22:49:38 +01003002 // Check if the window is partly above or below the screen. We don't care
3003 // about partly left or right of the screen, it is not relevant when
3004 // scrolling up or down.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003005 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3006 return SW_INVALIDATE;
3007
Bram Moolenaar734a8672019-12-02 22:49:38 +01003008 // Check if there is an window (partly) on top of us.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003009 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3010 if (IsWindowVisible(hwnd))
3011 {
3012 GetWindowRect(hwnd, &rcOther);
3013 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3014 return SW_INVALIDATE;
3015 }
3016 return 0;
3017}
3018
3019/*
3020 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3021 * may not be scrolled out properly.
3022 * For gVim, when _OnScroll() is repeated, the character at the
3023 * previous cursor position may be left drawn after scroll.
3024 * The problem can be avoided by calling GetPixel() to get a pixel in
3025 * the region before ScrollWindowEx().
3026 */
3027 static void
3028intel_gpu_workaround(void)
3029{
3030 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3031}
3032
3033/*
3034 * Delete the given number of lines from the given row, scrolling up any
3035 * text further down within the scroll region.
3036 */
3037 void
3038gui_mch_delete_lines(
3039 int row,
3040 int num_lines)
3041{
3042 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 rc.left = FILL_X(gui.scroll_region_left);
3045 rc.right = FILL_X(gui.scroll_region_right + 1);
3046 rc.top = FILL_Y(row);
3047 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3048
Bram Moolenaar92467d32017-12-05 13:22:16 +01003049#if defined(FEAT_DIRECTX)
3050 if (IS_ENABLE_DIRECTX())
3051 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003052 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3053 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003054 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003055 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003056#endif
3057 {
3058 intel_gpu_workaround();
3059 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003061 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003062 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003063
Bram Moolenaar734a8672019-12-02 22:49:38 +01003064 // This seems to be required to avoid the cursor disappearing when
3065 // scrolling such that the cursor ends up in the top-left character on
3066 // the screen... But why? (Webb)
3067 // It's probably fixed by disabling drawing the cursor while scrolling.
3068 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003069
3070 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3071 gui.scroll_region_left,
3072 gui.scroll_region_bot, gui.scroll_region_right);
3073}
3074
3075/*
3076 * Insert the given number of lines before the given row, scrolling down any
3077 * following text within the scroll region.
3078 */
3079 void
3080gui_mch_insert_lines(
3081 int row,
3082 int num_lines)
3083{
3084 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003086 rc.left = FILL_X(gui.scroll_region_left);
3087 rc.right = FILL_X(gui.scroll_region_right + 1);
3088 rc.top = FILL_Y(row);
3089 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003090
3091#if defined(FEAT_DIRECTX)
3092 if (IS_ENABLE_DIRECTX())
3093 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003094 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3095 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003096 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003097 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003098#endif
3099 {
3100 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003101 // The SW_INVALIDATE is required when part of the window is covered or
3102 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003103 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003104 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003105 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003106 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003108 gui_clear_block(row, gui.scroll_region_left,
3109 row + num_lines - 1, gui.scroll_region_right);
3110}
3111
3112
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003114gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003115{
3116#if defined(FEAT_DIRECTX)
3117 DWriteContext_Close(s_dwc);
3118 DWrite_Final();
3119 s_dwc = NULL;
3120#endif
3121
3122 ReleaseDC(s_textArea, s_hdc);
3123 DeleteObject(s_brush);
3124
3125#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003126 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003127 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3128#endif
3129
Bram Moolenaar734a8672019-12-02 22:49:38 +01003130 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003131 if (s_hwnd != NULL)
3132 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003133 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003134 DestroyWindow(s_hwnd);
3135 }
3136
3137#ifdef GLOBAL_IME
3138 global_ime_end();
3139#endif
3140}
3141
3142 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003143logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003144{
3145 char *p;
3146 char *res;
3147 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003148 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003149 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003150 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003152 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3153 if (font_name == NULL)
3154 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003155 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003156 quality_name = quality_id2name((int)lf.lfQuality);
3157
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003158 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003159 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003160 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 if (res != NULL)
3162 {
3163 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003164 // make a normal font string out of the lf thing:
3165 points = pixels_to_points(
3166 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3167 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3168 sprintf((char *)p, "%s:h%d", font_name, points);
3169 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003170 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171 while (*p)
3172 {
3173 if (*p == ' ')
3174 *p = '_';
3175 ++p;
3176 }
3177 if (lf.lfItalic)
3178 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003179 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180 STRCAT(p, ":b");
3181 if (lf.lfUnderline)
3182 STRCAT(p, ":u");
3183 if (lf.lfStrikeOut)
3184 STRCAT(p, ":s");
3185 if (charset_name != NULL)
3186 {
3187 STRCAT(p, ":c");
3188 STRCAT(p, charset_name);
3189 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003190 if (quality_name != NULL)
3191 {
3192 STRCAT(p, ":q");
3193 STRCAT(p, quality_name);
3194 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195 }
3196
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003197 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 return (char_u *)res;
3199}
3200
3201
3202#ifdef FEAT_MBYTE_IME
3203/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003204 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205 * 'guifont'
3206 */
3207 static void
3208update_im_font(void)
3209{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003210 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211
3212 if (p_guifontwide != NULL && *p_guifontwide != NUL
3213 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003214 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 norm_logfont = lf_wide;
3216 else
3217 norm_logfont = sub_logfont;
3218 im_set_font(&norm_logfont);
3219}
3220#endif
3221
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003222/*
3223 * Handler of gui.wide_font (p_guifontwide) changed notification.
3224 */
3225 void
3226gui_mch_wide_font_changed(void)
3227{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003228 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003230#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003232#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233
3234 gui_mch_free_font(gui.wide_ital_font);
3235 gui.wide_ital_font = NOFONT;
3236 gui_mch_free_font(gui.wide_bold_font);
3237 gui.wide_bold_font = NOFONT;
3238 gui_mch_free_font(gui.wide_boldital_font);
3239 gui.wide_boldital_font = NOFONT;
3240
3241 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003242 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003243 {
3244 if (!lf.lfItalic)
3245 {
3246 lf.lfItalic = TRUE;
3247 gui.wide_ital_font = get_font_handle(&lf);
3248 lf.lfItalic = FALSE;
3249 }
3250 if (lf.lfWeight < FW_BOLD)
3251 {
3252 lf.lfWeight = FW_BOLD;
3253 gui.wide_bold_font = get_font_handle(&lf);
3254 if (!lf.lfItalic)
3255 {
3256 lf.lfItalic = TRUE;
3257 gui.wide_boldital_font = get_font_handle(&lf);
3258 }
3259 }
3260 }
3261}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
3263/*
3264 * Initialise vim to use the font with the given name.
3265 * Return FAIL if the font could not be loaded, OK otherwise.
3266 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003268gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003269{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003270 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271 GuiFont font = NOFONT;
3272 char_u *p;
3273
Bram Moolenaar734a8672019-12-02 22:49:38 +01003274 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3276 font = get_font_handle(&lf);
3277 if (font == NOFONT)
3278 return FAIL;
3279
3280 if (font_name == NULL)
3281 font_name = (char_u *)lf.lfFaceName;
3282#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3283 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003284#endif
3285#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003286 sub_logfont = lf;
3287#endif
3288#ifdef FEAT_MBYTE_IME
3289 update_im_font();
3290#endif
3291 gui_mch_free_font(gui.norm_font);
3292 gui.norm_font = font;
3293 current_font_height = lf.lfHeight;
3294 GetFontSize(font);
3295
3296 p = logfont2name(lf);
3297 if (p != NULL)
3298 {
3299 hl_set_font_name(p);
3300
Bram Moolenaar734a8672019-12-02 22:49:38 +01003301 // When setting 'guifont' to "*" replace it with the actual font name.
3302 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3304 {
3305 vim_free(p_guifont);
3306 p_guifont = p;
3307 }
3308 else
3309 vim_free(p);
3310 }
3311
3312 gui_mch_free_font(gui.ital_font);
3313 gui.ital_font = NOFONT;
3314 gui_mch_free_font(gui.bold_font);
3315 gui.bold_font = NOFONT;
3316 gui_mch_free_font(gui.boldital_font);
3317 gui.boldital_font = NOFONT;
3318
3319 if (!lf.lfItalic)
3320 {
3321 lf.lfItalic = TRUE;
3322 gui.ital_font = get_font_handle(&lf);
3323 lf.lfItalic = FALSE;
3324 }
3325 if (lf.lfWeight < FW_BOLD)
3326 {
3327 lf.lfWeight = FW_BOLD;
3328 gui.bold_font = get_font_handle(&lf);
3329 if (!lf.lfItalic)
3330 {
3331 lf.lfItalic = TRUE;
3332 gui.boldital_font = get_font_handle(&lf);
3333 }
3334 }
3335
3336 return OK;
3337}
3338
3339#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003340# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341#endif
3342
3343/*
3344 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003345 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 */
3347 int
3348gui_mch_maximized(void)
3349{
3350 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003351 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352
3353 wp.length = sizeof(WINDOWPLACEMENT);
3354 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003355 {
3356 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003357 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003358 && wp.flags == WPF_RESTORETOMAXIMIZED))
3359 return TRUE;
3360 if (wp.showCmd == SW_SHOWMINIMIZED)
3361 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003362
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003363 // Assume the window is snapped when the sizes from two APIs differ.
3364 GetWindowRect(s_hwnd, &rc);
3365 if ((rc.right - rc.left !=
3366 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3367 || (rc.bottom - rc.top !=
3368 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3369 return TRUE;
3370 }
3371 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372}
3373
3374/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003375 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3376 * is set. Compute the new Rows and Columns. This is like resizing the
3377 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378 */
3379 void
3380gui_mch_newfont(void)
3381{
3382 RECT rect;
3383
3384 GetWindowRect(s_hwnd, &rect);
3385 if (win_socket_id == 0)
3386 {
3387 gui_resize_shell(rect.right - rect.left
3388 - (GetSystemMetrics(SM_CXFRAME) +
3389 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3390 rect.bottom - rect.top
3391 - (GetSystemMetrics(SM_CYFRAME) +
3392 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3393 - GetSystemMetrics(SM_CYCAPTION)
3394#ifdef FEAT_MENU
3395 - gui_mswin_get_menu_height(FALSE)
3396#endif
3397 );
3398 }
3399 else
3400 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003401 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003402 gui_resize_shell(rect.right - rect.left,
3403 rect.bottom - rect.top
3404#ifdef FEAT_MENU
3405 - gui_mswin_get_menu_height(FALSE)
3406#endif
3407 );
3408 }
3409}
3410
3411/*
3412 * Set the window title
3413 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003414 void
3415gui_mch_settitle(
3416 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003417 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003418{
3419 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3420}
3421
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003422#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003423// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3424// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003425static LPCSTR mshape_idcs[] =
3426{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003427 IDC_ARROW, // arrow
3428 MAKEINTRESOURCE(0), // blank
3429 IDC_IBEAM, // beam
3430 IDC_SIZENS, // updown
3431 IDC_SIZENS, // udsizing
3432 IDC_SIZEWE, // leftright
3433 IDC_SIZEWE, // lrsizing
3434 IDC_WAIT, // busy
3435 IDC_NO, // no
3436 IDC_ARROW, // crosshair
3437 IDC_ARROW, // hand1
3438 IDC_ARROW, // hand2
3439 IDC_ARROW, // pencil
3440 IDC_ARROW, // question
3441 IDC_ARROW, // right-arrow
3442 IDC_UPARROW, // up-arrow
3443 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444};
3445
3446 void
3447mch_set_mouse_shape(int shape)
3448{
3449 LPCSTR idc;
3450
3451 if (shape == MSHAPE_HIDE)
3452 ShowCursor(FALSE);
3453 else
3454 {
3455 if (shape >= MSHAPE_NUMBERED)
3456 idc = IDC_ARROW;
3457 else
3458 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003459 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460 if (!p_mh)
3461 {
3462 POINT mp;
3463
Bram Moolenaar734a8672019-12-02 22:49:38 +01003464 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465 (void)GetCursorPos((LPPOINT)&mp);
3466 (void)SetCursorPos(mp.x, mp.y);
3467 ShowCursor(TRUE);
3468 }
3469 }
3470}
3471#endif
3472
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003473#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 * Wide version of convert_filter().
3476 */
3477 static WCHAR *
3478convert_filterW(char_u *s)
3479{
3480 char_u *tmp;
3481 int len;
3482 WCHAR *res;
3483
3484 tmp = convert_filter(s);
3485 if (tmp == NULL)
3486 return NULL;
3487 len = (int)STRLEN(s) + 3;
3488 res = enc_to_utf16(tmp, &len);
3489 vim_free(tmp);
3490 return res;
3491}
3492
3493/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003494 * Pop open a file browser and return the file selected, in allocated memory,
3495 * or NULL if Cancel is hit.
3496 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3497 * title - Title message for the file browser dialog.
3498 * dflt - Default name of file.
3499 * ext - Default extension to be added to files without extensions.
3500 * initdir - directory in which to open the browser (NULL = current dir)
3501 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003502 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003503 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003504gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003505 int saving,
3506 char_u *title,
3507 char_u *dflt,
3508 char_u *ext,
3509 char_u *initdir,
3510 char_u *filter)
3511{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003512 // We always use the wide function. This means enc_to_utf16() must work,
3513 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 OPENFILENAMEW fileStruct;
3515 WCHAR fileBuf[MAXPATHL];
3516 WCHAR *wp;
3517 int i;
3518 WCHAR *titlep = NULL;
3519 WCHAR *extp = NULL;
3520 WCHAR *initdirp = NULL;
3521 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003522 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523
3524 if (dflt == NULL)
3525 fileBuf[0] = NUL;
3526 else
3527 {
3528 wp = enc_to_utf16(dflt, NULL);
3529 if (wp == NULL)
3530 fileBuf[0] = NUL;
3531 else
3532 {
3533 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3534 fileBuf[i] = wp[i];
3535 fileBuf[i] = NUL;
3536 vim_free(wp);
3537 }
3538 }
3539
Bram Moolenaar734a8672019-12-02 22:49:38 +01003540 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541 filterp = convert_filterW(filter);
3542
Bram Moolenaara80faa82020-04-12 19:37:17 +02003543 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003544# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003545 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003546 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003547# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003548 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003549# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550
3551 if (title != NULL)
3552 titlep = enc_to_utf16(title, NULL);
3553 fileStruct.lpstrTitle = titlep;
3554
3555 if (ext != NULL)
3556 extp = enc_to_utf16(ext, NULL);
3557 fileStruct.lpstrDefExt = extp;
3558
3559 fileStruct.lpstrFile = fileBuf;
3560 fileStruct.nMaxFile = MAXPATHL;
3561 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003562 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3563 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564 if (initdir != NULL && *initdir != NUL)
3565 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003566 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003567 initdirp = enc_to_utf16(initdir, NULL);
3568 if (initdirp != NULL)
3569 {
3570 for (wp = initdirp; *wp != NUL; ++wp)
3571 if (*wp == '/')
3572 *wp = '\\';
3573 }
3574 fileStruct.lpstrInitialDir = initdirp;
3575 }
3576
3577 /*
3578 * TODO: Allow selection of multiple files. Needs another arg to this
3579 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3580 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3581 * files that don't exist yet, so I haven't put it in. What about
3582 * OFN_PATHMUSTEXIST?
3583 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3584 */
3585 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003586# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587 if (curbuf->b_p_bin)
3588 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003589# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003590 if (saving)
3591 {
3592 if (!GetSaveFileNameW(&fileStruct))
3593 return NULL;
3594 }
3595 else
3596 {
3597 if (!GetOpenFileNameW(&fileStruct))
3598 return NULL;
3599 }
3600
3601 vim_free(filterp);
3602 vim_free(initdirp);
3603 vim_free(titlep);
3604 vim_free(extp);
3605
Bram Moolenaar734a8672019-12-02 22:49:38 +01003606 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003607 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003608 if (p == NULL)
3609 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610
Bram Moolenaar734a8672019-12-02 22:49:38 +01003611 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003612 SetFocus(s_hwnd);
3613
Bram Moolenaar734a8672019-12-02 22:49:38 +01003614 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003615 q = vim_strsave(shorten_fname1(p));
3616 vim_free(p);
3617 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003619
3620
3621/*
3622 * Convert the string s to the proper format for a filter string by replacing
3623 * the \t and \n delimiters with \0.
3624 * Returns the converted string in allocated memory.
3625 *
3626 * Keep in sync with convert_filterW() above!
3627 */
3628 static char_u *
3629convert_filter(char_u *s)
3630{
3631 char_u *res;
3632 unsigned s_len = (unsigned)STRLEN(s);
3633 unsigned i;
3634
3635 res = alloc(s_len + 3);
3636 if (res != NULL)
3637 {
3638 for (i = 0; i < s_len; ++i)
3639 if (s[i] == '\t' || s[i] == '\n')
3640 res[i] = '\0';
3641 else
3642 res[i] = s[i];
3643 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003644 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003645 res[s_len + 1] = NUL;
3646 res[s_len + 2] = NUL;
3647 }
3648 return res;
3649}
3650
3651/*
3652 * Select a directory.
3653 */
3654 char_u *
3655gui_mch_browsedir(char_u *title, char_u *initdir)
3656{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // We fake this: Use a filter that doesn't select anything and a default
3658 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3660 initdir, (char_u *)_("Directory\t*.nothing\n"));
3661}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003662#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664 static void
3665_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003666 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003667 HDROP hDrop)
3668{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003669#define BUFPATHLEN _MAX_PATH
3670#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672 char szFile[BUFPATHLEN];
3673 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3674 UINT i;
3675 char_u **fnames;
3676 POINT pt;
3677 int_u modifiers = 0;
3678
Bram Moolenaar734a8672019-12-02 22:49:38 +01003679 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680
Bram Moolenaar734a8672019-12-02 22:49:38 +01003681 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003682 DragQueryPoint(hDrop, &pt);
3683 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3684
3685 reset_VIsual();
3686
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003687 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688
3689 if (fnames != NULL)
3690 for (i = 0; i < cFiles; ++i)
3691 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3693 fnames[i] = utf16_to_enc(wszFile, NULL);
3694 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695 {
3696 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3697 fnames[i] = vim_strsave((char_u *)szFile);
3698 }
3699 }
3700
3701 DragFinish(hDrop);
3702
3703 if (fnames != NULL)
3704 {
3705 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3706 modifiers |= MOUSE_SHIFT;
3707 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3708 modifiers |= MOUSE_CTRL;
3709 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3710 modifiers |= MOUSE_ALT;
3711
3712 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3713
3714 s_need_activate = TRUE;
3715 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716}
3717
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718 static int
3719_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003720 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003721 HWND hwndCtl,
3722 UINT code,
3723 int pos)
3724{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003725 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726 scrollbar_T *sb, *sb_info;
3727 long val;
3728 int dragging = FALSE;
3729 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003730 SCROLLINFO si;
3731
3732 si.cbSize = sizeof(si);
3733 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003734
3735 sb = gui_mswin_find_scrollbar(hwndCtl);
3736 if (sb == NULL)
3737 return 0;
3738
Bram Moolenaar734a8672019-12-02 22:49:38 +01003739 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003740 {
3741 /*
3742 * Careful: need to get scrollbar info out of first (left) scrollbar
3743 * for window, but keep real scrollbar too because we must pass it to
3744 * gui_drag_scrollbar().
3745 */
3746 sb_info = &sb->wp->w_scrollbars[0];
3747 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003748 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003749 sb_info = sb;
3750 val = sb_info->value;
3751
3752 switch (code)
3753 {
3754 case SB_THUMBTRACK:
3755 val = pos;
3756 dragging = TRUE;
3757 if (sb->scroll_shift > 0)
3758 val <<= sb->scroll_shift;
3759 break;
3760 case SB_LINEDOWN:
3761 val++;
3762 break;
3763 case SB_LINEUP:
3764 val--;
3765 break;
3766 case SB_PAGEDOWN:
3767 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3768 break;
3769 case SB_PAGEUP:
3770 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3771 break;
3772 case SB_TOP:
3773 val = 0;
3774 break;
3775 case SB_BOTTOM:
3776 val = sb_info->max;
3777 break;
3778 case SB_ENDSCROLL:
3779 if (prev_code == SB_THUMBTRACK)
3780 {
3781 /*
3782 * "pos" only gives us 16-bit data. In case of large file,
3783 * use GetScrollPos() which returns 32-bit. Unfortunately it
3784 * is not valid while the scrollbar is being dragged.
3785 */
3786 val = GetScrollPos(hwndCtl, SB_CTL);
3787 if (sb->scroll_shift > 0)
3788 val <<= sb->scroll_shift;
3789 }
3790 break;
3791
3792 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003793 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794 return 0;
3795 }
3796 prev_code = code;
3797
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003798 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3799 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003800
3801 /*
3802 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3803 */
3804 if (sb->wp != NULL)
3805 {
3806 scrollbar_T *sba = sb->wp->w_scrollbars;
3807 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3808
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003810 }
3811
Bram Moolenaar734a8672019-12-02 22:49:38 +01003812 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003813 s_busy_processing = TRUE;
3814
Bram Moolenaar734a8672019-12-02 22:49:38 +01003815 // When "allow_scrollbar" is FALSE still need to remember the new
3816 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 dont_scroll = !allow_scrollbar;
3818
Bram Moolenaara338adc2018-01-31 20:51:47 +01003819 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003820 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003821 mch_enable_flush();
3822 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003823
3824 s_busy_processing = FALSE;
3825 dont_scroll = dont_scroll_save;
3826
3827 return 0;
3828}
3829
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#ifdef FEAT_XPM_W32
3832# include "xpm_w32.h"
3833#endif
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#ifdef __MINGW32__
3836/*
3837 * Add a lot of missing defines.
3838 * They are not always missing, we need the #ifndef's.
3839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840# ifndef IsMinimized
3841# define IsMinimized(hwnd) IsIconic(hwnd)
3842# endif
3843# ifndef IsMaximized
3844# define IsMaximized(hwnd) IsZoomed(hwnd)
3845# endif
3846# ifndef SelectFont
3847# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3848# endif
3849# ifndef GetStockBrush
3850# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3851# endif
3852# ifndef DeleteBrush
3853# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3854# endif
3855
3856# ifndef HANDLE_WM_RBUTTONDBLCLK
3857# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3858 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3859# endif
3860# ifndef HANDLE_WM_MBUTTONUP
3861# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3862 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3863# endif
3864# ifndef HANDLE_WM_MBUTTONDBLCLK
3865# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3866 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3867# endif
3868# ifndef HANDLE_WM_LBUTTONDBLCLK
3869# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3870 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3871# endif
3872# ifndef HANDLE_WM_RBUTTONDOWN
3873# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3874 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3875# endif
3876# ifndef HANDLE_WM_MOUSEMOVE
3877# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3878 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3879# endif
3880# ifndef HANDLE_WM_RBUTTONUP
3881# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3882 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3883# endif
3884# ifndef HANDLE_WM_MBUTTONDOWN
3885# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3886 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3887# endif
3888# ifndef HANDLE_WM_LBUTTONUP
3889# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3890 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3891# endif
3892# ifndef HANDLE_WM_LBUTTONDOWN
3893# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3894 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3895# endif
3896# ifndef HANDLE_WM_SYSCHAR
3897# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3898 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3899# endif
3900# ifndef HANDLE_WM_ACTIVATEAPP
3901# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3902 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3903# endif
3904# ifndef HANDLE_WM_WINDOWPOSCHANGING
3905# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3906 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3907# endif
3908# ifndef HANDLE_WM_VSCROLL
3909# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3910 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3911# endif
3912# ifndef HANDLE_WM_SETFOCUS
3913# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3914 ((fn)((hwnd), (HWND)(wParam)), 0L)
3915# endif
3916# ifndef HANDLE_WM_KILLFOCUS
3917# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3918 ((fn)((hwnd), (HWND)(wParam)), 0L)
3919# endif
3920# ifndef HANDLE_WM_HSCROLL
3921# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3922 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3923# endif
3924# ifndef HANDLE_WM_DROPFILES
3925# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3926 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3927# endif
3928# ifndef HANDLE_WM_CHAR
3929# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3930 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3931# endif
3932# ifndef HANDLE_WM_SYSDEADCHAR
3933# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3934 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3935# endif
3936# ifndef HANDLE_WM_DEADCHAR
3937# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3938 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3939# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003940#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942
Bram Moolenaar734a8672019-12-02 22:49:38 +01003943// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#define TEAROFF_PADDING_X 2
3945#define TEAROFF_BUTTON_PAD_X 8
3946#define TEAROFF_MIN_WIDTH 200
3947#define TEAROFF_SUBMENU_LABEL ">>"
3948#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3949
3950
Bram Moolenaar734a8672019-12-02 22:49:38 +01003951// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003953# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954#endif
3955
3956
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003957#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958# define ID_BEVAL_TOOLTIP 200
3959# define BEVAL_TEXT_LEN MAXPATHL
3960
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003961# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003962// Work around old versions of basetsd.h which wrongly declares
3963// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003964# undef UINT_PTR
3965# define UINT_PTR UINT
3966# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003969static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003971
Bram Moolenaar82881492012-11-20 16:53:39 +01003972
Bram Moolenaar734a8672019-12-02 22:49:38 +01003973// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003974# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01003975
Bram Moolenaar45360022005-07-21 21:08:21 +00003976/*
3977 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003978 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003979 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003980# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01003981
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003982# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003983
3984typedef struct _DllVersionInfo
3985{
3986 DWORD cbSize;
3987 DWORD dwMajorVersion;
3988 DWORD dwMinorVersion;
3989 DWORD dwBuildNumber;
3990 DWORD dwPlatformID;
3991} DLLVERSIONINFO;
3992
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003993# ifndef PROTO
3994# include <poppack.h>
3995# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003996
Bram Moolenaar45360022005-07-21 21:08:21 +00003997typedef struct tagTOOLINFOA_NEW
3998{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003999 UINT cbSize;
4000 UINT uFlags;
4001 HWND hwnd;
4002 UINT_PTR uId;
4003 RECT rect;
4004 HINSTANCE hinst;
4005 LPSTR lpszText;
4006 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004007} TOOLINFO_NEW;
4008
4009typedef struct tagNMTTDISPINFO_NEW
4010{
4011 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004012 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004013 char szText[80];
4014 HINSTANCE hinst;
4015 UINT uFlags;
4016 LPARAM lParam;
4017} NMTTDISPINFO_NEW;
4018
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004019typedef struct tagTOOLINFOW_NEW
4020{
4021 UINT cbSize;
4022 UINT uFlags;
4023 HWND hwnd;
4024 UINT_PTR uId;
4025 RECT rect;
4026 HINSTANCE hinst;
4027 LPWSTR lpszText;
4028 LPARAM lParam;
4029 void *lpReserved;
4030} TOOLINFOW_NEW;
4031
4032typedef struct tagNMTTDISPINFOW_NEW
4033{
4034 NMHDR hdr;
4035 LPWSTR lpszText;
4036 WCHAR szText[80];
4037 HINSTANCE hinst;
4038 UINT uFlags;
4039 LPARAM lParam;
4040} NMTTDISPINFOW_NEW;
4041
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004042
Bram Moolenaar45360022005-07-21 21:08:21 +00004043typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004044# ifndef TTM_SETMAXTIPWIDTH
4045# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004048# ifndef TTF_DI_SETITEM
4049# define TTF_DI_SETITEM 0x8000
4050# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004051
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004052# ifndef TTN_GETDISPINFO
4053# define TTN_GETDISPINFO (TTN_FIRST - 0)
4054# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004055
Bram Moolenaar734a8672019-12-02 22:49:38 +01004056#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004057
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004058#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004059// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4060// it here if LPNMTTDISPINFO isn't defined.
4061// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4062// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004063# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4064typedef struct tagNMTTDISPINFOA {
4065 NMHDR hdr;
4066 LPSTR lpszText;
4067 char szText[80];
4068 HINSTANCE hinst;
4069 UINT uFlags;
4070 LPARAM lParam;
4071} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4072# define LPNMTTDISPINFO LPNMTTDISPINFOA
4073
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004074typedef struct tagNMTTDISPINFOW {
4075 NMHDR hdr;
4076 LPWSTR lpszText;
4077 WCHAR szText[80];
4078 HINSTANCE hinst;
4079 UINT uFlags;
4080 LPARAM lParam;
4081} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004082# endif
4083#endif
4084
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004085#ifndef TTN_GETDISPINFOW
4086# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4087#endif
4088
Bram Moolenaar734a8672019-12-02 22:49:38 +01004089// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091#ifdef FEAT_MENU
4092static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095/*
4096 * Use the system font for dialogs and tear-off menus. Remove this line to
4097 * use DLG_FONT_NAME.
4098 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004099#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#define VIM_CLASSW L"Vim"
4103
Bram Moolenaar734a8672019-12-02 22:49:38 +01004104// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4105// thus there should be room for every dialog. For tearoffs it's made bigger
4106// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107#define DLG_ALLOC_SIZE 16 * 1024
4108
4109/*
4110 * stuff for dialogs, menus, tearoffs etc.
4111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112static PWORD
4113add_dialog_element(
4114 PWORD p,
4115 DWORD lStyle,
4116 WORD x,
4117 WORD y,
4118 WORD w,
4119 WORD h,
4120 WORD Id,
4121 WORD clss,
4122 const char *caption);
4123static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004124static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004125#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128static void get_dialog_font_metrics(void);
4129
4130static int dialog_default_button = -1;
4131
Bram Moolenaar734a8672019-12-02 22:49:38 +01004132// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
Bram Moolenaar734a8672019-12-02 22:49:38 +01004135static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136#ifdef FEAT_TOOLBAR
4137static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004138static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139static int get_toolbar_bitmap(vimmenu_T *menu);
4140#endif
4141
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004142#ifdef FEAT_GUI_TABLINE
4143static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004144static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004145#endif
4146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#ifdef FEAT_MBYTE_IME
4148static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4149static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4150#endif
4151#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4152# ifdef NOIME
4153typedef struct tagCOMPOSITIONFORM {
4154 DWORD dwStyle;
4155 POINT ptCurrentPos;
4156 RECT rcArea;
4157} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4158typedef HANDLE HIMC;
4159# endif
4160
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004161static HINSTANCE hLibImm = NULL;
4162static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4163static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4164static HIMC (WINAPI *pImmGetContext)(HWND);
4165static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4166static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4167static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4168static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004169static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4170static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004171static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4172static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004173static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174static void dyn_imm_load(void);
4175#else
4176# define pImmGetCompositionStringA ImmGetCompositionStringA
4177# define pImmGetCompositionStringW ImmGetCompositionStringW
4178# define pImmGetContext ImmGetContext
4179# define pImmAssociateContext ImmAssociateContext
4180# define pImmReleaseContext ImmReleaseContext
4181# define pImmGetOpenStatus ImmGetOpenStatus
4182# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004183# define pImmGetCompositionFontW ImmGetCompositionFontW
4184# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185# define pImmSetCompositionWindow ImmSetCompositionWindow
4186# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004187# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#endif
4189
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifdef FEAT_MENU
4191/*
4192 * Figure out how high the menu bar is at the moment.
4193 */
4194 static int
4195gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004196 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 static int old_menu_height = -1;
4199
4200 RECT rc1, rc2;
4201 int num;
4202 int menu_height;
4203
4204 if (gui.menu_is_active)
4205 num = GetMenuItemCount(s_menuBar);
4206 else
4207 num = 0;
4208
4209 if (num == 0)
4210 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004211 else if (IsMinimized(s_hwnd))
4212 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004213 // The height of the menu cannot be determined while the window is
4214 // minimized. Take the previous height if the menu is changed in that
4215 // state, to avoid that Vim's vertical window size accidentally
4216 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004217 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 else
4220 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004221 /*
4222 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4223 * seem to have been set yet, so menu wraps in default window
4224 * width which is very narrow. Instead just return height of a
4225 * single menu item. Will still be wrong when the menu really
4226 * should wrap over more than one line.
4227 */
4228 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4229 if (gui.starting)
4230 menu_height = rc1.bottom - rc1.top + 1;
4231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004233 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4234 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236 }
4237
4238 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004239 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004240 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 return menu_height;
4243}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004244#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246
4247/*
4248 * Setup for the Intellimouse
4249 */
4250 static void
4251init_mouse_wheel(void)
4252{
4253
4254#ifndef SPI_GETWHEELSCROLLLINES
4255# define SPI_GETWHEELSCROLLLINES 104
4256#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004257#ifndef SPI_SETWHEELSCROLLLINES
4258# define SPI_SETWHEELSCROLLLINES 105
4259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaar734a8672019-12-02 22:49:38 +01004261#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4262#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4264#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4265
Bram Moolenaar734a8672019-12-02 22:49:38 +01004266 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar734a8672019-12-02 22:49:38 +01004268 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004269 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4270 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271}
4272
4273
Bram Moolenaarae20f342019-11-05 21:09:23 +01004274/*
4275 * Intellimouse wheel handler.
4276 * Treat a mouse wheel event as if it were a scroll request.
4277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 static void
4279_OnMouseWheel(
4280 HWND hwnd,
4281 short zDelta)
4282{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int i;
4284 int size;
4285 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004286 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (mouse_scroll_lines == 0)
4289 init_mouse_wheel();
4290
Bram Moolenaarae20f342019-11-05 21:09:23 +01004291 wp = gui_mouse_window(FIND_POPUP);
4292
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004293#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004294 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004295 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004296 cmdarg_T cap;
4297 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004298
Bram Moolenaarae20f342019-11-05 21:09:23 +01004299 // Mouse hovers over popup window, scroll it if possible.
4300 mouse_row = wp->w_winrow;
4301 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004302 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004303 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4304 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4305 clear_oparg(&oa);
4306 cap.oap = &oa;
4307 nv_mousescroll(&cap);
4308 update_screen(0);
4309 setcursor();
4310 out_flush();
4311 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004312 }
4313#endif
4314
Bram Moolenaarae20f342019-11-05 21:09:23 +01004315 if (wp == NULL || !p_scf)
4316 wp = curwin;
4317
4318 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4319 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4320 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4321 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4322 else
4323 return;
4324 size = wp->w_height;
4325
Bram Moolenaara338adc2018-01-31 20:51:47 +01004326 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (mouse_scroll_lines > 0
4328 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4329 {
4330 for (i = mouse_scroll_lines; i > 0; --i)
4331 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4332 }
4333 else
4334 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004335 mch_enable_flush();
4336 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337}
4338
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004339#ifdef USE_SYSMENU_FONT
4340/*
4341 * Get Menu Font.
4342 * Return OK or FAIL.
4343 */
4344 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004345gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004346{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004347 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004348
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004349 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4350 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004351 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004352 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004353 &nm,
4354 0))
4355 return FAIL;
4356 *lf = nm.lfMenuFont;
4357 return OK;
4358}
4359#endif
4360
4361
4362#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4363/*
4364 * Set the GUI tabline font to the system menu font
4365 */
4366 static void
4367set_tabline_font(void)
4368{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004369 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004370 HFONT font;
4371 HWND hwnd;
4372 HDC hdc;
4373 HFONT hfntOld;
4374 TEXTMETRIC tm;
4375
4376 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4377 return;
4378
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004379 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004380
4381 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4382
4383 /*
4384 * Compute the height of the font used for the tab text
4385 */
4386 hwnd = GetDesktopWindow();
4387 hdc = GetWindowDC(hwnd);
4388 hfntOld = SelectFont(hdc, font);
4389
4390 GetTextMetrics(hdc, &tm);
4391
4392 SelectFont(hdc, hfntOld);
4393 ReleaseDC(hwnd, hdc);
4394
4395 /*
4396 * The space used by the tab border and the space between the tab label
4397 * and the tab border is included as 7.
4398 */
4399 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4400}
4401#endif
4402
Bram Moolenaar520470a2005-06-16 21:59:56 +00004403/*
4404 * Invoked when a setting was changed.
4405 */
4406 static LRESULT CALLBACK
4407_OnSettingChange(UINT n)
4408{
4409 if (n == SPI_SETWHEELSCROLLLINES)
4410 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4411 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004412#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4413 if (n == SPI_SETNONCLIENTMETRICS)
4414 set_tabline_font();
4415#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004416 return 0;
4417}
4418
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#ifdef FEAT_NETBEANS_INTG
4420 static void
4421_OnWindowPosChanged(
4422 HWND hwnd,
4423 const LPWINDOWPOS lpwpos)
4424{
4425 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004426 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4429 || lpwpos->cx != cx || lpwpos->cy != cy))
4430 {
4431 x = lpwpos->x;
4432 y = lpwpos->y;
4433 cx = lpwpos->cx;
4434 cy = lpwpos->cy;
4435 netbeans_frame_moved(x, y);
4436 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004437 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4439}
4440#endif
4441
4442 static int
4443_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 UINT fwSide,
4445 LPRECT lprc)
4446{
4447 int w, h;
4448 int valid_w, valid_h;
4449 int w_offset, h_offset;
4450
4451 w = lprc->right - lprc->left;
4452 h = lprc->bottom - lprc->top;
4453 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4454 w_offset = w - valid_w;
4455 h_offset = h - valid_h;
4456
4457 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4458 || fwSide == WMSZ_BOTTOMLEFT)
4459 lprc->left += w_offset;
4460 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4461 || fwSide == WMSZ_BOTTOMRIGHT)
4462 lprc->right -= w_offset;
4463
4464 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4465 || fwSide == WMSZ_TOPRIGHT)
4466 lprc->top += h_offset;
4467 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4468 || fwSide == WMSZ_BOTTOMRIGHT)
4469 lprc->bottom -= h_offset;
4470 return TRUE;
4471}
4472
4473
4474
4475 static LRESULT CALLBACK
4476_WndProc(
4477 HWND hwnd,
4478 UINT uMsg,
4479 WPARAM wParam,
4480 LPARAM lParam)
4481{
4482 /*
4483 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4484 hwnd, uMsg, wParam, lParam);
4485 */
4486
4487 HandleMouseHide(uMsg, lParam);
4488
4489 s_uMsg = uMsg;
4490 s_wParam = wParam;
4491 s_lParam = lParam;
4492
4493 switch (uMsg)
4494 {
4495 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4496 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004497 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004499 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4501 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4502 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4503 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4504#ifdef FEAT_MENU
4505 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4506#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004507 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4508 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4510 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004511 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4512 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4514 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4515 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4516#ifdef FEAT_NETBEANS_INTG
4517 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4518#endif
4519
Bram Moolenaarafa24992006-03-27 20:58:26 +00004520#ifdef FEAT_GUI_TABLINE
4521 case WM_RBUTTONUP:
4522 {
4523 if (gui_mch_showing_tabline())
4524 {
4525 POINT pt;
4526 RECT rect;
4527
4528 /*
4529 * If the cursor is on the tabline, display the tab menu
4530 */
4531 GetCursorPos((LPPOINT)&pt);
4532 GetWindowRect(s_textArea, &rect);
4533 if (pt.y < rect.top)
4534 {
4535 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004536 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004537 }
4538 }
4539 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4540 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004541 case WM_LBUTTONDBLCLK:
4542 {
4543 /*
4544 * If the user double clicked the tabline, create a new tab
4545 */
4546 if (gui_mch_showing_tabline())
4547 {
4548 POINT pt;
4549 RECT rect;
4550
4551 GetCursorPos((LPPOINT)&pt);
4552 GetWindowRect(s_textArea, &rect);
4553 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004554 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004555 }
4556 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4557 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004558#endif
4559
Bram Moolenaar734a8672019-12-02 22:49:38 +01004560 case WM_QUERYENDSESSION: // System wants to go down.
4561 gui_shell_closed(); // Will exit when no changed buffers.
4562 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004565 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004568 return 0L;
4569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571
4572 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004573 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4574 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004575 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return 0L;
4577
4578 case WM_SYSCHAR:
4579 /*
4580 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4581 * shortcut key, handle like a typed ALT key, otherwise call Windows
4582 * ALT key handling.
4583 */
4584#ifdef FEAT_MENU
4585 if ( !gui.menu_is_active
4586 || p_wak[0] == 'n'
4587 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4588 )
4589#endif
4590 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004591 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 return 0L;
4593 }
4594#ifdef FEAT_MENU
4595 else
4596 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4597#endif
4598
4599 case WM_SYSKEYUP:
4600#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004601 // This used to be done only when menu is active: ALT key is used for
4602 // that. But that caused problems when menu is disabled and using
4603 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4604 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4606#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004607 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608#endif
4609
Bram Moolenaar734a8672019-12-02 22:49:38 +01004610 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004611 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 case WM_MOUSEWHEEL:
4614 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004615 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
Bram Moolenaar734a8672019-12-02 22:49:38 +01004617 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004618 case WM_SETTINGCHANGE:
4619 return _OnSettingChange((UINT)wParam);
4620
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004621#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 case WM_NOTIFY:
4623 switch (((LPNMHDR) lParam)->code)
4624 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004625 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004626 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004628 LPNMHDR hdr = (LPNMHDR)lParam;
4629 char_u *str = NULL;
4630 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004631
Bram Moolenaard23a8232018-02-10 18:45:26 +01004632 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004633
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004634# ifdef FEAT_GUI_TABLINE
4635 if (gui_mch_showing_tabline()
4636 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004637 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004638 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004639 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004640 * Mouse is over the GUI tabline. Display the
4641 * tooltip for the tab under the cursor
4642 *
4643 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004644 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004645 GetCursorPos(&pt);
4646 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004647 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004648 TCHITTESTINFO htinfo;
4649 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004650
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004651 /*
4652 * Get the tab under the cursor
4653 */
4654 htinfo.pt.x = pt.x;
4655 htinfo.pt.y = pt.y;
4656 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4657 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004658 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004659 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004660
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004661 tp = find_tabpage(idx + 1);
4662 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004663 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004664 get_tabline_label(tp, TRUE);
4665 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004666 }
4667 }
4668 }
4669 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004670# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004671# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004672# ifdef FEAT_GUI_TABLINE
4673 else
4674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004676 UINT idButton;
4677 vimmenu_T *pMenu;
4678
4679 idButton = (UINT) hdr->idFrom;
4680 pMenu = gui_mswin_find_menu(root_menu, idButton);
4681 if (pMenu)
4682 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004684# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004685 if (str != NULL)
4686 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004687 if (hdr->code == TTN_GETDISPINFOW)
4688 {
4689 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4690
Bram Moolenaar734a8672019-12-02 22:49:38 +01004691 // Set the maximum width, this also enables using
4692 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004693 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4694 0, 500);
4695
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004696 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004697 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004698 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004699 }
4700 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004701 {
4702 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4703
Bram Moolenaar734a8672019-12-02 22:49:38 +01004704 // Set the maximum width, this also enables using
4705 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004706 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4707 0, 500);
4708
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004709 if (STRLEN(str) < sizeof(lpdi->szText)
4710 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004711 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004712 sizeof(lpdi->szText) - 1);
4713 else
4714 lpdi->lpszText = tt_text;
4715 }
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 }
4718 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004719# ifdef FEAT_GUI_TABLINE
4720 case TCN_SELCHANGE:
4721 if (gui_mch_showing_tabline()
4722 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004723 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004724 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004725 return 0L;
4726 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004727 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004728
4729 case NM_RCLICK:
4730 if (gui_mch_showing_tabline()
4731 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004732 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004733 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004734 return 0L;
4735 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004736 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004739# ifdef FEAT_GUI_TABLINE
4740 if (gui_mch_showing_tabline()
4741 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4742 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 break;
4745 }
4746 break;
4747#endif
4748#if defined(MENUHINTS) && defined(FEAT_MENU)
4749 case WM_MENUSELECT:
4750 if (((UINT) HIWORD(wParam)
4751 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4752 == MF_HILITE
4753 && (State & CMDLINE) == 0)
4754 {
4755 UINT idButton;
4756 vimmenu_T *pMenu;
4757 static int did_menu_tip = FALSE;
4758
4759 if (did_menu_tip)
4760 {
4761 msg_clr_cmdline();
4762 setcursor();
4763 out_flush();
4764 did_menu_tip = FALSE;
4765 }
4766
4767 idButton = (UINT)LOWORD(wParam);
4768 pMenu = gui_mswin_find_menu(root_menu, idButton);
4769 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4770 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4771 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004772 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004773 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004774 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 setcursor();
4776 out_flush();
4777 did_menu_tip = TRUE;
4778 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004779 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 break;
4782#endif
4783 case WM_NCHITTEST:
4784 {
4785 LRESULT result;
4786 int x, y;
4787 int xPos = GET_X_LPARAM(lParam);
4788
4789 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4790 if (result == HTCLIENT)
4791 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004792#ifdef FEAT_GUI_TABLINE
4793 if (gui_mch_showing_tabline())
4794 {
4795 int yPos = GET_Y_LPARAM(lParam);
4796 RECT rct;
4797
Bram Moolenaar734a8672019-12-02 22:49:38 +01004798 // If the cursor is on the GUI tabline, don't process this
4799 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004800 GetWindowRect(s_textArea, &rct);
4801 if (yPos < rct.top)
4802 return result;
4803 }
4804#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004805 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 xPos -= x;
4807
Bram Moolenaar734a8672019-12-02 22:49:38 +01004808 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return HTBOTTOMLEFT;
4810 else
4811 return HTBOTTOMRIGHT;
4812 }
4813 else
4814 return result;
4815 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004816 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
4818#ifdef FEAT_MBYTE_IME
4819 case WM_IME_NOTIFY:
4820 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4821 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004822 return 1L;
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 case WM_IME_COMPOSITION:
4825 if (!_OnImeComposition(hwnd, wParam, lParam))
4826 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004827 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#endif
4829
4830 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004832 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#endif
4835 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4836 }
4837
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004838 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
4840
4841/*
4842 * End of call-back routines
4843 */
4844
Bram Moolenaar734a8672019-12-02 22:49:38 +01004845// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846HWND vim_parent_hwnd = NULL;
4847
4848 static BOOL CALLBACK
4849FindWindowTitle(HWND hwnd, LPARAM lParam)
4850{
4851 char buf[2048];
4852 char *title = (char *)lParam;
4853
4854 if (GetWindowText(hwnd, buf, sizeof(buf)))
4855 {
4856 if (strstr(buf, title) != NULL)
4857 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004858 // Found it. Store the window ref. and quit searching if MDI
4859 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004861 if (vim_parent_hwnd != NULL)
4862 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004865 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866}
4867
4868/*
4869 * Invoked for '-P "title"' argument: search for parent application to open
4870 * our window in.
4871 */
4872 void
4873gui_mch_set_parent(char *title)
4874{
4875 EnumWindows(FindWindowTitle, (LPARAM)title);
4876 if (vim_parent_hwnd == NULL)
4877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004878 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 mch_exit(2);
4880 }
4881}
4882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004883#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 static void
4885ole_error(char *arg)
4886{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004887 char buf[IOSIZE];
4888
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004889# ifdef VIMDLL
4890 gui.in_use = mch_is_gui_executable();
4891# endif
4892
Bram Moolenaar734a8672019-12-02 22:49:38 +01004893 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004894 vim_snprintf(buf, IOSIZE,
4895 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4896 arg);
4897 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004901#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4902 static char *
4903gvim_error(void)
4904{
4905 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4906
4907 if (starting)
4908 {
4909 mch_errmsg(msg);
4910 mch_errmsg("\n");
4911 mch_exit(2);
4912 }
4913 return msg;
4914}
4915
4916 char *
4917gui_mch_do_spawn(char_u *arg)
4918{
4919 int len;
4920# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4921 char_u *session = NULL;
4922 LPWSTR tofree1 = NULL;
4923# endif
4924 WCHAR name[MAX_PATH];
4925 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4926 STARTUPINFOW si = {sizeof(si)};
4927 PROCESS_INFORMATION pi;
4928
4929 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4930 goto error;
4931 p = wcsrchr(name, L'\\');
4932 if (p == NULL)
4933 goto error;
4934 // Replace the executable name from vim(d).exe to gvim(d).exe.
4935# ifdef DEBUG
4936 wcscpy(p + 1, L"gvimd.exe");
4937# else
4938 wcscpy(p + 1, L"gvim.exe");
4939# endif
4940
4941# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4942 if (starting)
4943# endif
4944 {
4945 // Pass the command line to the new process.
4946 p = GetCommandLineW();
4947 // Skip 1st argument.
4948 while (*p && *p != L' ' && *p != L'\t')
4949 {
4950 if (*p == L'"')
4951 {
4952 while (*p && *p != L'"')
4953 ++p;
4954 if (*p)
4955 ++p;
4956 }
4957 else
4958 ++p;
4959 }
4960 cmd = p;
4961 }
4962# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4963 else
4964 {
4965 // Create a session file and pass it to the new process.
4966 LPWSTR wsession;
4967 char_u *savebg;
4968 int ret;
4969
4970 session = vim_tempname('s', FALSE);
4971 if (session == NULL)
4972 goto error;
4973 savebg = p_bg;
4974 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4975 ret = write_session_file(session);
4976 vim_free(p_bg);
4977 p_bg = savebg;
4978 if (!ret)
4979 goto error;
4980 wsession = enc_to_utf16(session, NULL);
4981 if (wsession == NULL)
4982 goto error;
4983 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004984 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004985 if (cmd == NULL)
4986 {
4987 vim_free(wsession);
4988 goto error;
4989 }
4990 tofree1 = cmd;
4991 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4992 wsession, wsession);
4993 vim_free(wsession);
4994 }
4995# endif
4996
4997 // Check additional arguments to the `:gui` command.
4998 if (arg != NULL)
4999 {
5000 warg = enc_to_utf16(arg, NULL);
5001 if (warg == NULL)
5002 goto error;
5003 tofree2 = warg;
5004 }
5005 else
5006 warg = L"";
5007
5008 // Set up the new command line.
5009 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005010 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005011 if (newcmd == NULL)
5012 goto error;
5013 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5014
5015 // Spawn a new GUI process.
5016 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5017 NULL, NULL, &si, &pi))
5018 goto error;
5019 CloseHandle(pi.hProcess);
5020 CloseHandle(pi.hThread);
5021 mch_exit(0);
5022
5023error:
5024# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5025 if (session)
5026 mch_remove(session);
5027 vim_free(session);
5028 vim_free(tofree1);
5029# endif
5030 vim_free(newcmd);
5031 vim_free(tofree2);
5032 return gvim_error();
5033}
5034#endif
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036/*
5037 * Parse the GUI related command-line arguments. Any arguments used are
5038 * deleted from argv, and *argc is decremented accordingly. This is called
5039 * when vim is started, whether or not the GUI has been started.
5040 */
5041 void
5042gui_mch_prepare(int *argc, char **argv)
5043{
5044 int silent = FALSE;
5045 int idx;
5046
Bram Moolenaar734a8672019-12-02 22:49:38 +01005047 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5049 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005050 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5052 && (argv[2][0] == '-' || argv[2][0] == '/'))
5053 {
5054 silent = TRUE;
5055 idx = 2;
5056 }
5057 else
5058 idx = 1;
5059
Bram Moolenaar734a8672019-12-02 22:49:38 +01005060 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (STRICMP(argv[idx] + 1, "register") == 0)
5062 {
5063#ifdef FEAT_OLE
5064 RegisterMe(silent);
5065 mch_exit(0);
5066#else
5067 if (!silent)
5068 ole_error("register");
5069 mch_exit(2);
5070#endif
5071 }
5072
Bram Moolenaar734a8672019-12-02 22:49:38 +01005073 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5075 {
5076#ifdef FEAT_OLE
5077 UnregisterMe(!silent);
5078 mch_exit(0);
5079#else
5080 if (!silent)
5081 ole_error("unregister");
5082 mch_exit(2);
5083#endif
5084 }
5085
Bram Moolenaar734a8672019-12-02 22:49:38 +01005086 // Ignore an -embedding argument. It is only relevant if the
5087 // application wants to treat the case when it is started manually
5088 // differently from the case where it is started via automation (and
5089 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5091 {
5092#ifdef FEAT_OLE
5093 *argc = 1;
5094#else
5095 ole_error("embedding");
5096 mch_exit(2);
5097#endif
5098 }
5099 }
5100
5101#ifdef FEAT_OLE
5102 {
5103 int bDoRestart = FALSE;
5104
5105 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005106 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (bDoRestart)
5108 mch_exit(0);
5109 }
5110#endif
5111
5112#ifdef FEAT_NETBEANS_INTG
5113 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005114 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 int arg;
5116
5117 for (arg = 1; arg < *argc; arg++)
5118 if (strncmp("-nb", argv[arg], 3) == 0)
5119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 netbeansArg = argv[arg];
5121 mch_memmove(&argv[arg], &argv[arg + 1],
5122 (--*argc - arg) * sizeof(char *));
5123 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005124 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128}
5129
5130/*
5131 * Initialise the GUI. Create all the windows, set up all the call-backs
5132 * etc.
5133 */
5134 int
5135gui_mch_init(void)
5136{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005138 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140#ifdef GLOBAL_IME
5141 ATOM atom;
5142#endif
5143
Bram Moolenaar734a8672019-12-02 22:49:38 +01005144 // Return here if the window was already opened (happens when
5145 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005147 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 /*
5150 * Load the tearoff bitmap
5151 */
5152#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005153 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#endif
5155
5156 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5157 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5158#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005159 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160#endif
5161 gui.border_width = 0;
5162
5163 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5164
Bram Moolenaar734a8672019-12-02 22:49:38 +01005165 // First try using the wide version, so that we can use any title.
5166 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005167 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005169 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 wndclassw.lpfnWndProc = _WndProc;
5171 wndclassw.cbClsExtra = 0;
5172 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005173 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5175 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5176 wndclassw.hbrBackground = s_brush;
5177 wndclassw.lpszMenuName = NULL;
5178 wndclassw.lpszClassName = szVimWndClassW;
5179
5180 if ((
5181#ifdef GLOBAL_IME
5182 atom =
5183#endif
5184 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 if (vim_parent_hwnd != NULL)
5189 {
5190#ifdef HAVE_TRY_EXCEPT
5191 __try
5192 {
5193#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005194 // Open inside the specified parent window.
5195 // TODO: last argument should point to a CLIENTCREATESTRUCT
5196 // structure.
5197 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005199 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005200 WS_OVERLAPPEDWINDOW | WS_CHILD
5201 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5203 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005204 100, // Any value will do
5205 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005207 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#ifdef HAVE_TRY_EXCEPT
5209 }
5210 __except(EXCEPTION_EXECUTE_HANDLER)
5211 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005212 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214#endif
5215 if (s_hwnd == NULL)
5216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005217 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 mch_exit(2);
5219 }
5220 }
5221 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005222 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005223 // If the provided windowid is not valid reset it to zero, so that it
5224 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005225 if (IsWindow((HWND)win_socket_id) <= 0)
5226 win_socket_id = 0;
5227
Bram Moolenaar734a8672019-12-02 22:49:38 +01005228 // Create a window. If win_socket_id is not zero without border and
5229 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005230 s_hwnd = CreateWindowW(
5231 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005232 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5233 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005234 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5235 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005236 100, // Any value will do
5237 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005238 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005239 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005240 if (s_hwnd != NULL && win_socket_id != 0)
5241 {
5242 SetParent(s_hwnd, (HWND)win_socket_id);
5243 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5244 }
5245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 if (s_hwnd == NULL)
5248 return FAIL;
5249
5250#ifdef GLOBAL_IME
5251 global_ime_init(atom, s_hwnd);
5252#endif
5253#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5254 dyn_imm_load();
5255#endif
5256
Bram Moolenaar734a8672019-12-02 22:49:38 +01005257 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005258 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005259 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005260 wndclassw.style = CS_OWNDC;
5261 wndclassw.lpfnWndProc = _TextAreaWndProc;
5262 wndclassw.cbClsExtra = 0;
5263 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005264 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005265 wndclassw.hIcon = NULL;
5266 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5267 wndclassw.hbrBackground = NULL;
5268 wndclassw.lpszMenuName = NULL;
5269 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005270
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005271 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 return FAIL;
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005275 s_textArea = CreateWindowExW(
5276 0,
5277 szTextAreaClassW, L"Vim text area",
5278 WS_CHILD | WS_VISIBLE, 0, 0,
5279 100, // Any value will do for now
5280 100, // Any value will do for now
5281 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005282 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (s_textArea == NULL)
5285 return FAIL;
5286
Bram Moolenaar20321902016-02-17 12:30:17 +01005287#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005288 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005289 {
5290 HANDLE hIcon = NULL;
5291
5292 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005293 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005294 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005295#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#ifdef FEAT_MENU
5298 s_menuBar = CreateMenu();
5299#endif
5300 s_hdc = GetDC(s_textArea);
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
Bram Moolenaar734a8672019-12-02 22:49:38 +01005304 // Do we need to bother with this?
5305 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar734a8672019-12-02 22:49:38 +01005307 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 gui_mch_def_colors();
5309
Bram Moolenaar734a8672019-12-02 22:49:38 +01005310 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5311 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 set_normal_colors();
5313
5314 /*
5315 * Check that none of the colors are the same as the background color.
5316 * Then store the current values as the defaults.
5317 */
5318 gui_check_colors();
5319 gui.def_norm_pixel = gui.norm_pixel;
5320 gui.def_back_pixel = gui.back_pixel;
5321
Bram Moolenaar734a8672019-12-02 22:49:38 +01005322 // Get the colors for the highlight groups (gui_check_colors() might have
5323 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 highlight_gui_started();
5325
5326 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005327 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005329 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 /*
5332 * Set up for Intellimouse processing
5333 */
5334 init_mouse_wheel();
5335
5336 /*
5337 * compute a couple of metrics used for the dialogs
5338 */
5339 get_dialog_font_metrics();
5340#ifdef FEAT_TOOLBAR
5341 /*
5342 * Create the toolbar
5343 */
5344 initialise_toolbar();
5345#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005346#ifdef FEAT_GUI_TABLINE
5347 /*
5348 * Create the tabline
5349 */
5350 initialise_tabline();
5351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352#ifdef MSWIN_FIND_REPLACE
5353 /*
5354 * Initialise the dialog box stuff
5355 */
5356 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5357
Bram Moolenaar734a8672019-12-02 22:49:38 +01005358 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005360 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005362 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5364 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5365 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005368#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005369# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005370// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005371# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005372# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005373# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005374# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005375 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005376 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005377#endif
5378
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005379#ifdef FEAT_RENDER_OPTIONS
5380 if (p_rop)
5381 (void)gui_mch_set_rendering_options(p_rop);
5382#endif
5383
Bram Moolenaar748bf032005-02-02 23:04:36 +00005384theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005385 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005386 display_errors();
5387
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 return OK;
5389}
5390
5391/*
5392 * Get the size of the screen, taking position on multiple monitors into
5393 * account (if supported).
5394 */
5395 static void
5396get_work_area(RECT *spi_rect)
5397{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005398 HMONITOR mon;
5399 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar734a8672019-12-02 22:49:38 +01005401 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005402 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005403 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005405 moninfo.cbSize = sizeof(MONITORINFO);
5406 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005408 *spi_rect = moninfo.rcWork;
5409 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
5411 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005412 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5414}
5415
5416/*
5417 * Set the size of the window to the given width and height in pixels.
5418 */
5419 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005420gui_mch_set_shellsize(
5421 int width,
5422 int height,
5423 int min_width UNUSED,
5424 int min_height UNUSED,
5425 int base_width UNUSED,
5426 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005427 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005430 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
Bram Moolenaar734a8672019-12-02 22:49:38 +01005433 // Try to keep window completely on screen.
5434 // Get position of the screen work area. This is the part that is not
5435 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 get_work_area(&workarea_rect);
5437
Bram Moolenaar734a8672019-12-02 22:49:38 +01005438 // Resizing a maximized window looks very strange, unzoom it first.
5439 // But don't do it when still starting up, it may have been requested in
5440 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005441 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005443
5444 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar734a8672019-12-02 22:49:38 +01005446 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005447 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005448 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005449 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005450 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 + GetSystemMetrics(SM_CYCAPTION)
5452#ifdef FEAT_MENU
5453 + gui_mswin_get_menu_height(FALSE)
5454#endif
5455 ;
5456
Bram Moolenaar734a8672019-12-02 22:49:38 +01005457 // The following should take care of keeping Vim on the same monitor, no
5458 // matter if the secondary monitor is left or right of the primary
5459 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005460 window_rect.right = window_rect.left + win_width;
5461 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005462
Bram Moolenaar734a8672019-12-02 22:49:38 +01005463 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005464 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5465 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005467 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5468 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005470 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5471 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005473 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5474 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005476 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5477 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479 SetActiveWindow(s_hwnd);
5480 SetFocus(s_hwnd);
5481
5482#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005483 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 gui_mswin_get_menu_height(!gui.starting);
5485#endif
5486}
5487
5488
5489 void
5490gui_mch_set_scrollbar_thumb(
5491 scrollbar_T *sb,
5492 long val,
5493 long size,
5494 long max)
5495{
5496 SCROLLINFO info;
5497
5498 sb->scroll_shift = 0;
5499 while (max > 32767)
5500 {
5501 max = (max + 1) >> 1;
5502 val >>= 1;
5503 size >>= 1;
5504 ++sb->scroll_shift;
5505 }
5506
5507 if (sb->scroll_shift > 0)
5508 ++size;
5509
5510 info.cbSize = sizeof(info);
5511 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5512 info.nPos = val;
5513 info.nMin = 0;
5514 info.nMax = max;
5515 info.nPage = size;
5516 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5517}
5518
5519
5520/*
5521 * Set the current text font.
5522 */
5523 void
5524gui_mch_set_font(GuiFont font)
5525{
5526 gui.currFont = font;
5527}
5528
5529
5530/*
5531 * Set the current text foreground color.
5532 */
5533 void
5534gui_mch_set_fg_color(guicolor_T color)
5535{
5536 gui.currFgColor = color;
5537}
5538
5539/*
5540 * Set the current text background color.
5541 */
5542 void
5543gui_mch_set_bg_color(guicolor_T color)
5544{
5545 gui.currBgColor = color;
5546}
5547
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005548/*
5549 * Set the current text special color.
5550 */
5551 void
5552gui_mch_set_sp_color(guicolor_T color)
5553{
5554 gui.currSpColor = color;
5555}
5556
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005557#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558/*
5559 * Multi-byte handling, originally by Sung-Hoon Baek.
5560 * First static functions (no prototypes generated).
5561 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005562# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005563# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005564# endif
5565# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 * handle WM_IME_NOTIFY message
5569 */
5570 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005571_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
5573 LRESULT lResult = 0;
5574 HIMC hImc;
5575
5576 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5577 return lResult;
5578 switch (dwCommand)
5579 {
5580 case IMN_SETOPENSTATUS:
5581 if (pImmGetOpenStatus(hImc))
5582 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005583 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 im_set_position(gui.row, gui.col);
5585
Bram Moolenaar734a8672019-12-02 22:49:38 +01005586 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 State &= ~LANGMAP;
5588 if (State & INSERT)
5589 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005590# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005591 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5593 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005594 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 int old_row = gui.row;
5596 int old_col = gui.col;
5597
5598 // This must be called here before
5599 // status_redraw_curbuf(), otherwise the mode
5600 // message may appear in the wrong position.
5601 showmode();
5602 status_redraw_curbuf();
5603 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005604 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 gui.row = old_row;
5606 gui.col = old_col;
5607 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
5610 }
5611 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005612 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 lResult = 0;
5614 break;
5615 }
5616 pImmReleaseContext(hWnd, hImc);
5617 return lResult;
5618}
5619
5620 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005621_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622{
5623 char_u *ret;
5624 int len;
5625
Bram Moolenaar734a8672019-12-02 22:49:38 +01005626 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 return 0;
5628
5629 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5630 if (ret != NULL)
5631 {
5632 add_to_input_buf_csi(ret, len);
5633 vim_free(ret);
5634 return 1;
5635 }
5636 return 0;
5637}
5638
5639/*
5640 * get the current composition string, in UCS-2; *lenp is the number of
5641 * *lenp is the number of Unicode characters.
5642 */
5643 static short_u *
5644GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5645{
5646 LONG ret;
5647 LPWSTR wbuf = NULL;
5648 char_u *buf;
5649
5650 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005651 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar734a8672019-12-02 22:49:38 +01005653 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5655 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005656 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 if (ret > 0)
5659 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005660 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005661 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (wbuf != NULL)
5663 {
5664 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5665 *lenp = ret / sizeof(WCHAR);
5666 }
5667 return (short_u *)wbuf;
5668 }
5669
Bram Moolenaar734a8672019-12-02 22:49:38 +01005670 // ret < 0; we got an error, so try the ANSI version. This'll work
5671 // on 9x/ME, but only if the codepage happens to be set to whatever
5672 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5674 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005675 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677 buf = alloc(ret);
5678 if (buf == NULL)
5679 return NULL;
5680 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5681
Bram Moolenaar734a8672019-12-02 22:49:38 +01005682 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005683 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 vim_free(buf);
5685
5686 return (short_u *)wbuf;
5687}
5688
5689/*
5690 * void GetResultStr()
5691 *
5692 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5693 * get complete composition string
5694 */
5695 static char_u *
5696GetResultStr(HWND hwnd, int GCS, int *lenp)
5697{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005698 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 short_u *buf = NULL;
5700 char_u *convbuf = NULL;
5701
5702 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5703 return NULL;
5704
Bram Moolenaar734a8672019-12-02 22:49:38 +01005705 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5707 if (buf == NULL)
5708 return NULL;
5709
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005710 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 pImmReleaseContext(hwnd, hIMC);
5712 vim_free(buf);
5713 return convbuf;
5714}
5715#endif
5716
Bram Moolenaar734a8672019-12-02 22:49:38 +01005717// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005718#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720/*
5721 * set font to IM.
5722 */
5723 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005724im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 HIMC hImc;
5727
5728 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5729 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005730 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 pImmReleaseContext(s_hwnd, hImc);
5732 }
5733}
5734
5735/*
5736 * Notify cursor position to IM.
5737 */
5738 void
5739im_set_position(int row, int col)
5740{
5741 HIMC hImc;
5742
5743 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5744 {
5745 COMPOSITIONFORM cfs;
5746
5747 cfs.dwStyle = CFS_POINT;
5748 cfs.ptCurrentPos.x = FILL_X(col);
5749 cfs.ptCurrentPos.y = FILL_Y(row);
5750 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5751 pImmSetCompositionWindow(hImc, &cfs);
5752
5753 pImmReleaseContext(s_hwnd, hImc);
5754 }
5755}
5756
5757/*
5758 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5759 */
5760 void
5761im_set_active(int active)
5762{
5763 HIMC hImc;
5764 static HIMC hImcOld = (HIMC)0;
5765
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005766# ifdef VIMDLL
5767 if (!gui.in_use && !gui.starting)
5768 {
5769 mbyte_im_set_active(active);
5770 return;
5771 }
5772# endif
5773
Bram Moolenaar734a8672019-12-02 22:49:38 +01005774 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
5776 if (p_imdisable)
5777 {
5778 if (hImcOld == (HIMC)0)
5779 {
5780 hImcOld = pImmGetContext(s_hwnd);
5781 if (hImcOld)
5782 pImmAssociateContext(s_hwnd, (HIMC)0);
5783 }
5784 active = FALSE;
5785 }
5786 else if (hImcOld != (HIMC)0)
5787 {
5788 pImmAssociateContext(s_hwnd, hImcOld);
5789 hImcOld = (HIMC)0;
5790 }
5791
5792 hImc = pImmGetContext(s_hwnd);
5793 if (hImc)
5794 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005795 /*
5796 * for Korean ime
5797 */
5798 HKL hKL = GetKeyboardLayout(0);
5799
5800 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5801 {
5802 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5803 static BOOL bSaved = FALSE;
5804
5805 if (active)
5806 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005807 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005808 if (bSaved)
5809 pImmSetConversionStatus(hImc, dwConversionSaved,
5810 dwSentenceSaved);
5811 bSaved = FALSE;
5812 }
5813 else
5814 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005815 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005816 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5817 &dwSentenceSaved))
5818 {
5819 bSaved = TRUE;
5820 pImmSetConversionStatus(hImc,
5821 dwConversionSaved & ~(IME_CMODE_NATIVE
5822 | IME_CMODE_FULLSHAPE),
5823 dwSentenceSaved);
5824 }
5825 }
5826 }
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 pImmSetOpenStatus(hImc, active);
5829 pImmReleaseContext(s_hwnd, hImc);
5830 }
5831 }
5832}
5833
5834/*
5835 * Get IM status. When IM is on, return not 0. Else return 0.
5836 */
5837 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005838im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 int status = 0;
5841 HIMC hImc;
5842
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005843# ifdef VIMDLL
5844 if (!gui.in_use && !gui.starting)
5845 return mbyte_im_get_status();
5846# endif
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5849 {
5850 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5851 pImmReleaseContext(s_hwnd, hImc);
5852 }
5853 return status;
5854}
5855
Bram Moolenaar734a8672019-12-02 22:49:38 +01005856#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005858#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005859// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861/*
5862 * Notify cursor position to IM.
5863 */
5864 void
5865im_set_position(int row, int col)
5866{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005867 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 POINT p;
5869
5870 p.x = FILL_X(col);
5871 p.y = FILL_Y(row);
5872 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5873 global_ime_set_position(&p);
5874}
5875
5876/*
5877 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5878 */
5879 void
5880im_set_active(int active)
5881{
5882 global_ime_set_status(active);
5883}
5884
5885/*
5886 * Get IM status. When IM is on, return not 0. Else return 0.
5887 */
5888 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005889im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 return global_ime_get_status();
5892}
5893#endif
5894
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005895/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005896 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005897 */
5898 static void
5899latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5900{
5901 int c;
5902
Bram Moolenaarca003e12006-03-17 23:19:38 +00005903 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005904 {
5905 c = *text++;
5906 switch (c)
5907 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005908 case 0xa4: c = 0x20ac; break; // euro
5909 case 0xa6: c = 0x0160; break; // S hat
5910 case 0xa8: c = 0x0161; break; // S -hat
5911 case 0xb4: c = 0x017d; break; // Z hat
5912 case 0xb8: c = 0x017e; break; // Z -hat
5913 case 0xbc: c = 0x0152; break; // OE
5914 case 0xbd: c = 0x0153; break; // oe
5915 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005916 }
5917 *unicodebuf++ = c;
5918 }
5919}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921#ifdef FEAT_RIGHTLEFT
5922/*
5923 * What is this for? In the case where you are using Win98 or Win2K or later,
5924 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5925 * reverses the string sent to the TextOut... family. This sucks, because we
5926 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5927 * way to tell Windblows not to do this!
5928 *
5929 * The short of it is that this 'RevOut' only gets called if you are running
5930 * one of the new, "improved" MS OSes, and only if you are running in
5931 * 'rightleft' mode. It makes display take *slightly* longer, but not
5932 * noticeably so.
5933 */
5934 static void
5935RevOut( HDC s_hdc,
5936 int col,
5937 int row,
5938 UINT foptions,
5939 CONST RECT *pcliprect,
5940 LPCTSTR text,
5941 UINT len,
5942 CONST INT *padding)
5943{
5944 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005946 for (ix = 0; ix < (int)len; ++ix)
5947 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5948 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949}
5950#endif
5951
Bram Moolenaar92467d32017-12-05 13:22:16 +01005952 static void
5953draw_line(
5954 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005955 int y1,
5956 int x2,
5957 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005958 COLORREF color)
5959{
5960#if defined(FEAT_DIRECTX)
5961 if (IS_ENABLE_DIRECTX())
5962 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5963 else
5964#endif
5965 {
5966 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5967 HPEN old_pen = SelectObject(s_hdc, hpen);
5968 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005969 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005970 LineTo(s_hdc, x2, y2);
5971 DeleteObject(SelectObject(s_hdc, old_pen));
5972 }
5973}
5974
5975 static void
5976set_pixel(
5977 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005978 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005979 COLORREF color)
5980{
5981#if defined(FEAT_DIRECTX)
5982 if (IS_ENABLE_DIRECTX())
5983 DWriteContext_SetPixel(s_dwc, x, y, color);
5984 else
5985#endif
5986 SetPixel(s_hdc, x, y, color);
5987}
5988
5989 static void
5990fill_rect(
5991 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005992 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005993 COLORREF color)
5994{
5995#if defined(FEAT_DIRECTX)
5996 if (IS_ENABLE_DIRECTX())
5997 DWriteContext_FillRect(s_dwc, rcp, color);
5998 else
5999#endif
6000 {
6001 HBRUSH hbr2;
6002
6003 if (hbr == NULL)
6004 hbr2 = CreateSolidBrush(color);
6005 else
6006 hbr2 = hbr;
6007 FillRect(s_hdc, rcp, hbr2);
6008 if (hbr == NULL)
6009 DeleteBrush(hbr2);
6010 }
6011}
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 void
6014gui_mch_draw_string(
6015 int row,
6016 int col,
6017 char_u *text,
6018 int len,
6019 int flags)
6020{
6021 static int *padding = NULL;
6022 static int pad_size = 0;
6023 int i;
6024 const RECT *pcliprect = NULL;
6025 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 static WCHAR *unicodebuf = NULL;
6027 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006028 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 int y;
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 /*
6033 * Italic and bold text seems to have an extra row of pixels at the bottom
6034 * (below where the bottom of the character should be). If we draw the
6035 * characters with a solid background, the top row of pixels in the
6036 * character below will be overwritten. We can fix this by filling in the
6037 * background ourselves, to the correct character proportions, and then
6038 * writing the character in transparent mode. Still have a problem when
6039 * the character is "_", which gets written on to the character below.
6040 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6041 * pixel in their slots, which fixes the problem with the bottom row of
6042 * pixels. We still need this code because otherwise the top row of pixels
6043 * becomes a problem. - webb.
6044 */
6045 static HBRUSH hbr_cache[2] = {NULL, NULL};
6046 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6047 static int brush_lru = 0;
6048 HBRUSH hbr;
6049 RECT rc;
6050
6051 if (!(flags & DRAW_TRANSP))
6052 {
6053 /*
6054 * Clear background first.
6055 * Note: FillRect() excludes right and bottom of rectangle.
6056 */
6057 rc.left = FILL_X(col);
6058 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (has_mbyte)
6060 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006061 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006062 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 }
6064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 rc.right = FILL_X(col + len);
6066 rc.bottom = FILL_Y(row + 1);
6067
Bram Moolenaar734a8672019-12-02 22:49:38 +01006068 // Cache the created brush, that saves a lot of time. We need two:
6069 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (gui.currBgColor == brush_color[0])
6071 {
6072 hbr = hbr_cache[0];
6073 brush_lru = 1;
6074 }
6075 else if (gui.currBgColor == brush_color[1])
6076 {
6077 hbr = hbr_cache[1];
6078 brush_lru = 0;
6079 }
6080 else
6081 {
6082 if (hbr_cache[brush_lru] != NULL)
6083 DeleteBrush(hbr_cache[brush_lru]);
6084 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6085 brush_color[brush_lru] = gui.currBgColor;
6086 hbr = hbr_cache[brush_lru];
6087 brush_lru = !brush_lru;
6088 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006089
Bram Moolenaar92467d32017-12-05 13:22:16 +01006090 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 SetBkMode(s_hdc, TRANSPARENT);
6093
6094 /*
6095 * When drawing block cursor, prevent inverted character spilling
6096 * over character cell (can happen with bold/italic)
6097 */
6098 if (flags & DRAW_CURSOR)
6099 {
6100 pcliprect = &rc;
6101 foptions = ETO_CLIPPED;
6102 }
6103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 SetTextColor(s_hdc, gui.currFgColor);
6105 SelectFont(s_hdc, gui.currFont);
6106
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006107#ifdef FEAT_DIRECTX
6108 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006109 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006110#endif
6111
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6113 {
6114 vim_free(padding);
6115 pad_size = Columns;
6116
Bram Moolenaar734a8672019-12-02 22:49:38 +01006117 // Don't give an out-of-memory message here, it would call us
6118 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006119 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (padding != NULL)
6121 for (i = 0; i < pad_size; i++)
6122 padding[i] = gui.char_width;
6123 }
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 /*
6126 * We have to provide the padding argument because italic and bold versions
6127 * of fixed-width fonts are often one pixel or so wider than their normal
6128 * versions.
6129 * No check for DRAW_BOLD, Windows will have done it already.
6130 */
6131
Bram Moolenaar734a8672019-12-02 22:49:38 +01006132 // Check if there are any UTF-8 characters. If not, use normal text
6133 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (enc_utf8)
6135 for (n = 0; n < len; ++n)
6136 if (text[n] >= 0x80)
6137 break;
6138
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006139#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006140 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6141 // required that unicode drawing routine, currently. So this forces it
6142 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006143 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006144 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006145#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006146
Bram Moolenaar734a8672019-12-02 22:49:38 +01006147 // Check if the Unicode buffer exists and is big enough. Create it
6148 // with the same length as the multi-byte string, the number of wide
6149 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006150 if ((enc_utf8
6151 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6152 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 && (unicodebuf == NULL || len > unibuflen))
6154 {
6155 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006156 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006159 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 unibuflen = len;
6162 }
6163
6164 if (enc_utf8 && n < len && unicodebuf != NULL)
6165 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006166 // Output UTF-8 characters. Composing characters should be
6167 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006168 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006169 int wlen; // string length in words
6170 int clen; // string length in characters
6171 int cells; // cell width of string up to composing char
6172 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006173 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006175 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006176 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006178 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006180 c = utf_ptr2char(text + i);
6181 if (c >= 0x10000)
6182 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006184 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6185 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006186 }
6187 else
6188 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006189 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006190 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006191
6192 if (utf_iscomposing(c))
6193 cw = 0;
6194 else
6195 {
6196 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006197 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006198 cw = 1;
6199 }
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (unicodepdy != NULL)
6202 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006203 // Use unicodepdy to make characters fit as we expect, even
6204 // when the font uses different widths (e.g., bold character
6205 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006206 if (c >= 0x10000)
6207 {
6208 unicodepdy[wlen - 2] = cw * gui.char_width;
6209 unicodepdy[wlen - 1] = 0;
6210 }
6211 else
6212 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
6214 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006215 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006216 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006218#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006219 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006220 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006221 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006222 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006223 TEXT_X(col), TEXT_Y(row),
6224 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006225 gui.char_width, gui.currFgColor,
6226 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006227 }
6228 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006229#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006230 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6231 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006234 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006236 // If we want to display codepage data, and the current CP is not the
6237 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (unicodebuf != NULL)
6239 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006240 if (enc_latin9)
6241 latin9_to_ucs(text, len, unicodebuf);
6242 else
6243 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 MB_PRECOMPOSED,
6245 (char *)text, len,
6246 (LPWSTR)unicodebuf, unibuflen);
6247 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006248 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006249 // Use unicodepdy to make characters fit as we expect, even
6250 // when the font uses different widths (e.g., bold character
6251 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006252 if (unicodepdy != NULL)
6253 {
6254 int i;
6255 int cw;
6256
6257 for (i = 0; i < len; ++i)
6258 {
6259 cw = utf_char2cells(unicodebuf[i]);
6260 if (cw > 2)
6261 cw = 1;
6262 unicodepdy[i] = cw * gui.char_width;
6263 }
6264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006266 foptions, pcliprect, unicodebuf, len, unicodepdy);
6267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
6269 }
6270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006273 // Windows will mess up RL text, so we have to draw it character by
6274 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006275 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6277 foptions, pcliprect, (char *)text, len, padding);
6278 else
6279#endif
6280 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6281 foptions, pcliprect, (char *)text, len, padding);
6282 }
6283
Bram Moolenaar734a8672019-12-02 22:49:38 +01006284 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (flags & DRAW_UNDERL)
6286 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006287 // When p_linespace is 0, overwrite the bottom row of pixels.
6288 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (p_linespace > 1)
6291 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006292 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006294
Bram Moolenaar734a8672019-12-02 22:49:38 +01006295 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006296 if (flags & DRAW_STRIKE)
6297 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006298 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006299 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006300 }
6301
Bram Moolenaar734a8672019-12-02 22:49:38 +01006302 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006303 if (flags & DRAW_UNDERC)
6304 {
6305 int x;
6306 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006307 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006308
6309 y = FILL_Y(row + 1) - 1;
6310 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6311 {
6312 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006313 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006314 }
6315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316}
6317
6318
6319/*
6320 * Output routines.
6321 */
6322
Bram Moolenaar734a8672019-12-02 22:49:38 +01006323/*
6324 * Flush any output to the screen
6325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 void
6327gui_mch_flush(void)
6328{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006329#if defined(FEAT_DIRECTX)
6330 if (IS_ENABLE_DIRECTX())
6331 DWriteContext_Flush(s_dwc);
6332#endif
6333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 GdiFlush();
6335}
6336
6337 static void
6338clear_rect(RECT *rcp)
6339{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006340 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342
6343
Bram Moolenaarc716c302006-01-21 22:12:51 +00006344 void
6345gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6346{
6347 RECT workarea_rect;
6348
6349 get_work_area(&workarea_rect);
6350
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006351 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006352 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006353 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006354
Bram Moolenaar734a8672019-12-02 22:49:38 +01006355 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6356 // the menubar for MSwin, we subtract it from the screen height, so that
6357 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006358 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006359 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006360 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006361 - GetSystemMetrics(SM_CYCAPTION)
6362#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006363 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006364#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006365 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006366}
6367
6368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#if defined(FEAT_MENU) || defined(PROTO)
6370/*
6371 * Add a sub menu to the menu bar.
6372 */
6373 void
6374gui_mch_add_menu(
6375 vimmenu_T *menu,
6376 int pos)
6377{
6378 vimmenu_T *parent = menu->parent;
6379
6380 menu->submenu_id = CreatePopupMenu();
6381 menu->id = s_menu_id++;
6382
6383 if (menu_is_menubar(menu->name))
6384 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006385 WCHAR *wn;
6386 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006388 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006389 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006390 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006392 infow.cbSize = sizeof(infow);
6393 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6394 | MIIM_SUBMENU;
6395 infow.dwItemData = (long_u)menu;
6396 infow.wID = menu->id;
6397 infow.fType = MFT_STRING;
6398 infow.dwTypeData = wn;
6399 infow.cch = (UINT)wcslen(wn);
6400 infow.hSubMenu = menu->submenu_id;
6401 InsertMenuItemW((parent == NULL)
6402 ? s_menuBar : parent->submenu_id,
6403 (UINT)pos, TRUE, &infow);
6404 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406
Bram Moolenaar734a8672019-12-02 22:49:38 +01006407 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (parent == NULL)
6409 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006410# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 else if (IsWindow(parent->tearoff_handle))
6412 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
6416 void
6417gui_mch_show_popupmenu(vimmenu_T *menu)
6418{
6419 POINT mp;
6420
6421 (void)GetCursorPos((LPPOINT)&mp);
6422 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6423}
6424
6425 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006426gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 vimmenu_T *menu = gui_find_menu(path_name);
6429
6430 if (menu != NULL)
6431 {
6432 POINT p;
6433
Bram Moolenaar734a8672019-12-02 22:49:38 +01006434 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006436 if (mouse_pos)
6437 {
6438 int mx, my;
6439
6440 gui_mch_getmouse(&mx, &my);
6441 p.x += mx;
6442 p.y += my;
6443 }
6444 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006446 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6448 }
6449 msg_scroll = FALSE;
6450 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6451 }
6452}
6453
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006454# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455/*
6456 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6457 * create it as a pseudo-"tearoff menu".
6458 */
6459 void
6460gui_make_tearoff(char_u *path_name)
6461{
6462 vimmenu_T *menu = gui_find_menu(path_name);
6463
Bram Moolenaar734a8672019-12-02 22:49:38 +01006464 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (menu != NULL)
6466 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6467}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469
6470/*
6471 * Add a menu item to a menu
6472 */
6473 void
6474gui_mch_add_menu_item(
6475 vimmenu_T *menu,
6476 int idx)
6477{
6478 vimmenu_T *parent = menu->parent;
6479
6480 menu->id = s_menu_id++;
6481 menu->submenu_id = NULL;
6482
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006483# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6485 {
6486 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6487 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6488 }
6489 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006490# endif
6491# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 if (menu_is_toolbar(parent->name))
6493 {
6494 TBBUTTON newtb;
6495
Bram Moolenaara80faa82020-04-12 19:37:17 +02006496 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (menu_is_separator(menu->name))
6498 {
6499 newtb.iBitmap = 0;
6500 newtb.fsStyle = TBSTYLE_SEP;
6501 }
6502 else
6503 {
6504 newtb.iBitmap = get_toolbar_bitmap(menu);
6505 newtb.fsStyle = TBSTYLE_BUTTON;
6506 }
6507 newtb.idCommand = menu->id;
6508 newtb.fsState = TBSTATE_ENABLED;
6509 newtb.iString = 0;
6510 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6511 (LPARAM)&newtb);
6512 menu->submenu_id = (HMENU)-1;
6513 }
6514 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006517 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006519 wn = enc_to_utf16(menu->name, NULL);
6520 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006522 InsertMenuW(parent->submenu_id, (UINT)idx,
6523 (menu_is_separator(menu->name)
6524 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6525 (UINT)menu->id, wn);
6526 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006528# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (IsWindow(parent->tearoff_handle))
6530 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
6533}
6534
6535/*
6536 * Destroy the machine specific menu widget.
6537 */
6538 void
6539gui_mch_destroy_menu(vimmenu_T *menu)
6540{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006541# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 /*
6543 * is this a toolbar button?
6544 */
6545 if (menu->submenu_id == (HMENU)-1)
6546 {
6547 int iButton;
6548
6549 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6550 (WPARAM)menu->id, 0);
6551 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6552 }
6553 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 if (menu->parent != NULL
6557 && menu_is_popup(menu->parent->dname)
6558 && menu->parent->submenu_id != NULL)
6559 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6560 else
6561 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6562 if (menu->submenu_id != NULL)
6563 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006564# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (IsWindow(menu->tearoff_handle))
6566 DestroyWindow(menu->tearoff_handle);
6567 if (menu->parent != NULL
6568 && menu->parent->children != NULL
6569 && IsWindow(menu->parent->tearoff_handle))
6570 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006571 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 menu->modes = 0;
6573 rebuild_tearoff(menu->parent);
6574 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 }
6577}
6578
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006579# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 static void
6581rebuild_tearoff(vimmenu_T *menu)
6582{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006583 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 char_u tbuf[128];
6585 RECT trect;
6586 RECT rct;
6587 RECT roct;
6588 int x, y;
6589
6590 HWND thwnd = menu->tearoff_handle;
6591
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006592 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 if (GetWindowRect(thwnd, &trect)
6594 && GetWindowRect(s_hwnd, &rct)
6595 && GetClientRect(s_hwnd, &roct))
6596 {
6597 x = trect.left - rct.left;
6598 y = (trect.top - rct.bottom + roct.bottom);
6599 }
6600 else
6601 {
6602 x = y = 0xffffL;
6603 }
6604 DestroyWindow(thwnd);
6605 if (menu->children != NULL)
6606 {
6607 gui_mch_tearoff(tbuf, menu, x, y);
6608 if (IsWindow(menu->tearoff_handle))
6609 (void) SetWindowPos(menu->tearoff_handle,
6610 NULL,
6611 (int)trect.left,
6612 (int)trect.top,
6613 0, 0,
6614 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6615 }
6616}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006617# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619/*
6620 * Make a menu either grey or not grey.
6621 */
6622 void
6623gui_mch_menu_grey(
6624 vimmenu_T *menu,
6625 int grey)
6626{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006627# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 /*
6629 * is this a toolbar button?
6630 */
6631 if (menu->submenu_id == (HMENU)-1)
6632 {
6633 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6634 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6635 }
6636 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006637# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006638 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6639 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006641# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6643 {
6644 WORD menuID;
6645 HWND menuHandle;
6646
6647 /*
6648 * A tearoff button has changed state.
6649 */
6650 if (menu->children == NULL)
6651 menuID = (WORD)(menu->id);
6652 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006653 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6655 if (menuHandle)
6656 EnableWindow(menuHandle, !grey);
6657
6658 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
Bram Moolenaar734a8672019-12-02 22:49:38 +01006662#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
6664
Bram Moolenaar734a8672019-12-02 22:49:38 +01006665// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6668#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006669#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6672/*
6673 * stuff for dialogs
6674 */
6675
6676/*
6677 * The callback routine used by all the dialogs. Very simple. First,
6678 * acknowledges the INITDIALOG message so that Windows knows to do standard
6679 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6680 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6681 * number.
6682 */
6683 static LRESULT CALLBACK
6684dialog_callback(
6685 HWND hwnd,
6686 UINT message,
6687 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006688 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 if (message == WM_INITDIALOG)
6691 {
6692 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006693 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 (void)SetFocus(hwnd);
6695 if (dialog_default_button > IDCANCEL)
6696 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006697 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006698 // We don't have a default, set focus on another element of the
6699 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006700 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 return FALSE;
6702 }
6703
6704 if (message == WM_COMMAND)
6705 {
6706 int button = LOWORD(wParam);
6707
Bram Moolenaar734a8672019-12-02 22:49:38 +01006708 // Don't end the dialog if something was selected that was
6709 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (button >= DLG_NONBUTTON_CONTROL)
6711 return TRUE;
6712
Bram Moolenaar734a8672019-12-02 22:49:38 +01006713 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006715 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006716 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006717 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006718
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006719 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6720 p = utf16_to_enc(wp, NULL);
6721 vim_strncpy(s_textfield, p, IOSIZE);
6722 vim_free(p);
6723 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 /*
6727 * Need to check for IDOK because if the user just hits Return to
6728 * accept the default value, some reason this is what we get.
6729 */
6730 if (button == IDOK)
6731 {
6732 if (dialog_default_button > IDCANCEL)
6733 EndDialog(hwnd, dialog_default_button);
6734 }
6735 else
6736 EndDialog(hwnd, button - IDCANCEL);
6737 return TRUE;
6738 }
6739
6740 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6741 {
6742 EndDialog(hwnd, 0);
6743 return TRUE;
6744 }
6745 return FALSE;
6746}
6747
6748/*
6749 * Create a dialog dynamically from the parameter strings.
6750 * type = type of dialog (question, alert, etc.)
6751 * title = dialog title. may be NULL for default title.
6752 * message = text to display. Dialog sizes to accommodate it.
6753 * buttons = '\n' separated list of button captions, default first.
6754 * dfltbutton = number of default button.
6755 *
6756 * This routine returns 1 if the first button is pressed,
6757 * 2 for the second, etc.
6758 *
6759 * 0 indicates Esc was pressed.
6760 * -1 for unexpected error
6761 *
6762 * If stubbing out this fn, return 1.
6763 */
6764
Bram Moolenaar734a8672019-12-02 22:49:38 +01006765static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 "IDR_VIM",
6768 "IDR_VIM_ERROR",
6769 "IDR_VIM_ALERT",
6770 "IDR_VIM_INFO",
6771 "IDR_VIM_QUESTION"
6772};
6773
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 int
6775gui_mch_dialog(
6776 int type,
6777 char_u *title,
6778 char_u *message,
6779 char_u *buttons,
6780 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006781 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006782 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006785 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 int numButtons;
6787 int *buttonWidths, *buttonPositions;
6788 int buttonYpos;
6789 int nchar, i;
6790 DWORD lStyle;
6791 int dlgwidth = 0;
6792 int dlgheight;
6793 int editboxheight;
6794 int horizWidth = 0;
6795 int msgheight;
6796 char_u *pstart;
6797 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006798 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 char_u *tbuffer;
6800 RECT rect;
6801 HWND hwnd;
6802 HDC hdc;
6803 HFONT font, oldFont;
6804 TEXTMETRIC fontInfo;
6805 int fontHeight;
6806 int textWidth, minButtonWidth, messageWidth;
6807 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006808 int maxDialogHeight;
6809 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 int vertical;
6811 int dlgPaddingX;
6812 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006813# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006814 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006816# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006817 garray_T ga;
6818 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006820# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006821 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006822# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006823 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006824# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006825 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006826 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828
Bram Moolenaar748bf032005-02-02 23:04:36 +00006829 if (s_hwnd == NULL)
6830 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831
6832 if ((type < 0) || (type > VIM_LAST_TYPE))
6833 type = 0;
6834
Bram Moolenaar734a8672019-12-02 22:49:38 +01006835 // allocate some memory for dialog template
6836 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006837 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006838 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
6840 if (p == NULL)
6841 return -1;
6842
6843 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006844 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6846 * const.
6847 */
6848 tbuffer = vim_strsave(buttons);
6849 if (tbuffer == NULL)
6850 return -1;
6851
Bram Moolenaar734a8672019-12-02 22:49:38 +01006852 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
Bram Moolenaar734a8672019-12-02 22:49:38 +01006854 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 numButtons = 1;
6856 for (i = 0; tbuffer[i] != '\0'; i++)
6857 {
6858 if (tbuffer[i] == DLG_BUTTON_SEP)
6859 numButtons++;
6860 }
6861 if (dfltbutton >= numButtons)
6862 dfltbutton = -1;
6863
Bram Moolenaar734a8672019-12-02 22:49:38 +01006864 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006865 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (buttonWidths == NULL)
6867 return -1;
6868
Bram Moolenaar734a8672019-12-02 22:49:38 +01006869 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006870 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 if (buttonPositions == NULL)
6872 return -1;
6873
6874 /*
6875 * Calculate how big the dialog must be.
6876 */
6877 hwnd = GetDesktopWindow();
6878 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006879# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6881 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006882 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 use_lfSysmenu = TRUE;
6884 }
6885 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6888 VARIABLE_PITCH , DLG_FONT_NAME);
6889 if (s_usenewlook)
6890 {
6891 oldFont = SelectFont(hdc, font);
6892 dlgPaddingX = DLG_PADDING_X;
6893 dlgPaddingY = DLG_PADDING_Y;
6894 }
6895 else
6896 {
6897 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6898 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6899 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6900 }
6901 GetTextMetrics(hdc, &fontInfo);
6902 fontHeight = fontInfo.tmHeight;
6903
Bram Moolenaar734a8672019-12-02 22:49:38 +01006904 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006905 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
Bram Moolenaar734a8672019-12-02 22:49:38 +01006907 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006908 if (s_hwnd == NULL)
6909 {
6910 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
Bram Moolenaar734a8672019-12-02 22:49:38 +01006912 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006913 get_work_area(&workarea_rect);
6914 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6915 if (maxDialogWidth > 600)
6916 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006917 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006918 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006919 }
6920 else
6921 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006922 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006923 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006924 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006925 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006926 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6928 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006929
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006930 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006931 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006932 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006933 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006934 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6935 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6936 }
6937
Bram Moolenaar734a8672019-12-02 22:49:38 +01006938 // Set dlgwidth to width of message.
6939 // Copy the message into "ga", changing NL to CR-NL and inserting line
6940 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 pstart = message;
6942 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006943 msgheight = 0;
6944 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 do
6946 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006947 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006948
Bram Moolenaar734a8672019-12-02 22:49:38 +01006949 // Need to figure out where to break the string. The system does it
6950 // at a word boundary, which would mean we can't compute the number of
6951 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006952 textWidth = 0;
6953 last_white = NULL;
6954 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006955 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006956 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006957 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006958 && textWidth > maxDialogWidth * 3 / 4)
6959 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006960 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006961 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006962 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006963 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006964 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006965 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006966 textWidth = 0;
6967
6968 if (last_white != NULL)
6969 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006970 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006971 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006972 pend = last_white + 1;
6973 last_white = NULL;
6974 }
6975 ga_append(&ga, '\r');
6976 ga_append(&ga, '\n');
6977 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006978 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006979
6980 while (--l >= 0)
6981 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006982 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006983 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006985
6986 ga_append(&ga, '\r');
6987 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 pstart = pend + 1;
6989 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006990
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006991 if (ga.ga_data != NULL)
6992 message = ga.ga_data;
6993
Bram Moolenaar734a8672019-12-02 22:49:38 +01006994 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006995
Bram Moolenaar734a8672019-12-02 22:49:38 +01006996 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02006997 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6998 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 if (msgheight < DLG_ICON_HEIGHT)
7001 msgheight = DLG_ICON_HEIGHT;
7002
7003 /*
7004 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007005 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007007 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 if (!vertical)
7009 {
7010 // Place buttons horizontally if they fit.
7011 horizWidth = dlgPaddingX;
7012 pstart = tbuffer;
7013 i = 0;
7014 do
7015 {
7016 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7017 if (pend == NULL)
7018 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007019 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (textWidth < minButtonWidth)
7021 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007022 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 buttonWidths[i] = textWidth;
7024 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007025 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 pstart = pend + 1;
7027 } while (*pend != NUL);
7028
7029 if (horizWidth > maxDialogWidth)
7030 vertical = TRUE; // Too wide to fit on the screen.
7031 else if (horizWidth > dlgwidth)
7032 dlgwidth = horizWidth;
7033 }
7034
7035 if (vertical)
7036 {
7037 // Stack buttons vertically.
7038 pstart = tbuffer;
7039 do
7040 {
7041 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7042 if (pend == NULL)
7043 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007044 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007045 textWidth += dlgPaddingX; // Padding within button
7046 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (textWidth > dlgwidth)
7048 dlgwidth = textWidth;
7049 pstart = pend + 1;
7050 } while (*pend != NUL);
7051 }
7052
7053 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007054 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar734a8672019-12-02 22:49:38 +01007056 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (s_usenewlook)
7058 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7059 else
7060 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7061
7062 add_long(lStyle);
7063 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007064 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 add_word(0); // NumberOfItems(will change later)
7066 add_word(10); // x
7067 add_word(10); // y
7068 add_word(PixelToDialogX(dlgwidth)); // cx
7069
7070 // Dialog height.
7071 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007072 dlgheight = msgheight + 2 * dlgPaddingY
7073 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else
7075 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7076
7077 // Dialog needs to be taller if contains an edit box.
7078 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7079 if (textfield != NULL)
7080 dlgheight += editboxheight;
7081
Bram Moolenaar734a8672019-12-02 22:49:38 +01007082 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007083 if (dlgheight > maxDialogHeight)
7084 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007085 msgheight = msgheight - (dlgheight - maxDialogHeight);
7086 dlgheight = maxDialogHeight;
7087 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007088 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007089 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007090 }
7091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 add_word(PixelToDialogY(dlgheight));
7093
7094 add_word(0); // Menu
7095 add_word(0); // Class
7096
Bram Moolenaar734a8672019-12-02 22:49:38 +01007097 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007098 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7099 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 p += nchar;
7101
7102 if (s_usenewlook)
7103 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007104 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007105# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (use_lfSysmenu)
7107 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007108 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7110 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007111 wcscpy(p, lfSysmenu.lfFaceName);
7112 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 }
7114 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
7117 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007118 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120 p += nchar;
7121 }
7122
7123 buttonYpos = msgheight + 2 * dlgPaddingY;
7124
7125 if (textfield != NULL)
7126 buttonYpos += editboxheight;
7127
7128 pstart = tbuffer;
7129 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007130 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 for (i = 0; i < numButtons; i++)
7132 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007133 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 for ( pend = pstart;
7135 *pend && (*pend != DLG_BUTTON_SEP);
7136 pend++)
7137 ;
7138
7139 if (*pend)
7140 *pend = '\0';
7141
7142 /*
7143 * old NOTE:
7144 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7145 * the focus to the first tab-able button and in so doing makes that
7146 * the default!! Grrr. Workaround: Make the default button the only
7147 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7148 * he/she can use arrow keys.
7149 *
7150 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007151 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 * dialog. Also needed for when the textfield is the default control.
7153 * It appears to work now (perhaps not on Win95?).
7154 */
7155 if (vertical)
7156 {
7157 p = add_dialog_element(p,
7158 (i == dfltbutton
7159 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7160 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007161 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 + 2 * fontHeight * i),
7163 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7164 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007165 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 }
7167 else
7168 {
7169 p = add_dialog_element(p,
7170 (i == dfltbutton
7171 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7172 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007173 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 PixelToDialogX(buttonWidths[i]),
7175 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007176 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007178 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180 *pnumitems += numButtons;
7181
Bram Moolenaar734a8672019-12-02 22:49:38 +01007182 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 p = add_dialog_element(p, SS_ICON,
7184 PixelToDialogX(dlgPaddingX),
7185 PixelToDialogY(dlgPaddingY),
7186 PixelToDialogX(DLG_ICON_WIDTH),
7187 PixelToDialogY(DLG_ICON_HEIGHT),
7188 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7189 dlg_icons[type]);
7190
Bram Moolenaar734a8672019-12-02 22:49:38 +01007191 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007192 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7193 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7194 PixelToDialogY(dlgPaddingY),
7195 (WORD)(PixelToDialogX(messageWidth) + 1),
7196 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007197 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
Bram Moolenaar734a8672019-12-02 22:49:38 +01007199 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (textfield != NULL)
7201 {
7202 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7203 PixelToDialogX(2 * dlgPaddingX),
7204 PixelToDialogY(2 * dlgPaddingY + msgheight),
7205 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7206 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007207 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 *pnumitems += 1;
7209 }
7210
7211 *pnumitems += 2;
7212
7213 SelectFont(hdc, oldFont);
7214 DeleteObject(font);
7215 ReleaseDC(hwnd, hdc);
7216
Bram Moolenaar734a8672019-12-02 22:49:38 +01007217 // Let the dialog_callback() function know which button to make default
7218 // If we have an edit box, make that the default. We also need to tell
7219 // dialog_callback() if this dialog contains an edit box or not. We do
7220 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 if (textfield != NULL)
7222 {
7223 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7224 s_textfield = textfield;
7225 }
7226 else
7227 {
7228 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7229 s_textfield = NULL;
7230 }
7231
Bram Moolenaar734a8672019-12-02 22:49:38 +01007232 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007234 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 (LPDLGTEMPLATE)pdlgtemplate,
7236 s_hwnd,
7237 (DLGPROC)dialog_callback);
7238
7239 LocalFree(LocalHandle(pdlgtemplate));
7240 vim_free(tbuffer);
7241 vim_free(buttonWidths);
7242 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007243 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaar734a8672019-12-02 22:49:38 +01007245 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 (void)SetFocus(s_hwnd);
7247
7248 return nchar;
7249}
7250
Bram Moolenaar734a8672019-12-02 22:49:38 +01007251#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253/*
7254 * Put a simple element (basic class) onto a dialog template in memory.
7255 * return a pointer to where the next item should be added.
7256 *
7257 * parameters:
7258 * lStyle = additional style flags
7259 * (be careful, NT3.51 & Win32s will ignore the new ones)
7260 * x,y = x & y positions IN DIALOG UNITS
7261 * w,h = width and height IN DIALOG UNITS
7262 * Id = ID used in messages
7263 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7264 * caption = usually text or resource name
7265 *
7266 * TODO: use the length information noted here to enable the dialog creation
7267 * routines to work out more exactly how much memory they need to alloc.
7268 */
7269 static PWORD
7270add_dialog_element(
7271 PWORD p,
7272 DWORD lStyle,
7273 WORD x,
7274 WORD y,
7275 WORD w,
7276 WORD h,
7277 WORD Id,
7278 WORD clss,
7279 const char *caption)
7280{
7281 int nchar;
7282
Bram Moolenaar734a8672019-12-02 22:49:38 +01007283 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7285 *p++ = LOWORD(lStyle);
7286 *p++ = HIWORD(lStyle);
7287 *p++ = 0; // LOWORD (lExtendedStyle)
7288 *p++ = 0; // HIWORD (lExtendedStyle)
7289 *p++ = x;
7290 *p++ = y;
7291 *p++ = w;
7292 *p++ = h;
7293 *p++ = Id; //9 or 10 words in all
7294
7295 *p++ = (WORD)0xffff;
7296 *p++ = clss; //2 more here
7297
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007298 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 p += nchar;
7300
7301 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7302
7303 return p; //total = 15+ (strlen(caption)) words
7304 // = 30 + 2(strlen(caption) bytes reqd
7305}
7306
7307
7308/*
7309 * Helper routine. Take an input pointer, return closest pointer that is
7310 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7311 */
7312 static LPWORD
7313lpwAlign(
7314 LPWORD lpIn)
7315{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007316 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007318 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 ul += 3;
7320 ul >>= 2;
7321 ul <<= 2;
7322 return (LPWORD)ul;
7323}
7324
7325/*
7326 * Helper routine. Takes second parameter as Ansi string, copies it to first
7327 * parameter as wide character (16-bits / char) string, and returns integer
7328 * number of wide characters (words) in string (including the trailing wide
7329 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007330 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7331 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 static int
7333nCopyAnsiToWideChar(
7334 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007335 LPSTR lpAnsiIn,
7336 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007339 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 int i;
7341 WCHAR *wn;
7342
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007343 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007345 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007346 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (wn != NULL)
7348 {
7349 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007350 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 vim_free(wn);
7352 }
7353 }
7354 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007355 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 nChar = MultiByteToWideChar(
7357 enc_codepage > 0 ? enc_codepage : CP_ACP,
7358 MB_PRECOMPOSED,
7359 lpAnsiIn, len,
7360 lpWCStr, len);
7361 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007362 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 return nChar;
7366}
7367
7368
7369#ifdef FEAT_TEAROFF
7370/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007371 * Lookup menu handle from "menu_id".
7372 */
7373 static HMENU
7374tearoff_lookup_menuhandle(
7375 vimmenu_T *menu,
7376 WORD menu_id)
7377{
7378 for ( ; menu != NULL; menu = menu->next)
7379 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007380 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007381 continue;
7382 if (menu_is_separator(menu->dname))
7383 continue;
7384 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7385 return menu->submenu_id;
7386 }
7387 return NULL;
7388}
7389
7390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 * The callback function for all the modeless dialogs that make up the
7392 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7393 * thinking its menus have been clicked), and go away when closed.
7394 */
7395 static LRESULT CALLBACK
7396tearoff_callback(
7397 HWND hwnd,
7398 UINT message,
7399 WPARAM wParam,
7400 LPARAM lParam)
7401{
7402 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007403 {
7404 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
Bram Moolenaar734a8672019-12-02 22:49:38 +01007408 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 HandleMouseHide(message, lParam);
7410
7411 if (message == WM_COMMAND)
7412 {
7413 if ((WORD)(LOWORD(wParam)) & 0x8000)
7414 {
7415 POINT mp;
7416 RECT rect;
7417
7418 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7419 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007420 vimmenu_T *menu;
7421
7422 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007424 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7426 (int)rect.right - 8,
7427 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007428 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 s_hwnd,
7430 NULL);
7431 /*
7432 * NOTE: The pop-up menu can eat the mouse up event.
7433 * We deal with this in normal.c.
7434 */
7435 }
7436 }
7437 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007438 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7440 /*
7441 * Give main window the focus back: this is so after
7442 * choosing a tearoff button you can start typing again
7443 * straight away.
7444 */
7445 (void)SetFocus(s_hwnd);
7446 return TRUE;
7447 }
7448 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7449 {
7450 DestroyWindow(hwnd);
7451 return TRUE;
7452 }
7453
Bram Moolenaar734a8672019-12-02 22:49:38 +01007454 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (message == WM_EXITSIZEMOVE)
7456 (void)SetActiveWindow(s_hwnd);
7457
7458 return FALSE;
7459}
7460#endif
7461
7462
7463/*
7464 * Decide whether to use the "new look" (small, non-bold font) or the "old
7465 * look" (big, clanky font) for dialogs, and work out a few values for use
7466 * later accordingly.
7467 */
7468 static void
7469get_dialog_font_metrics(void)
7470{
7471 HDC hdc;
7472 HFONT hfontTools = 0;
7473 DWORD dlgFontSize;
7474 SIZE size;
7475#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007476 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#endif
7478
7479 s_usenewlook = FALSE;
7480
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007482 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007483 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485#endif
7486 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7487 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7488
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007489 if (hfontTools)
7490 {
7491 hdc = GetDC(s_hwnd);
7492 SelectObject(hdc, hfontTools);
7493 /*
7494 * GetTextMetrics() doesn't return the right value in
7495 * tmAveCharWidth, so we have to figure out the dialog base units
7496 * ourselves.
7497 */
7498 GetTextExtentPoint(hdc,
7499 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7500 52, &size);
7501 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007503 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7504 s_dlgfntheight = (WORD)size.cy;
7505 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507
7508 if (!s_usenewlook)
7509 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007510 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 s_dlgfntwidth = LOWORD(dlgFontSize);
7512 s_dlgfntheight = HIWORD(dlgFontSize);
7513 }
7514}
7515
7516#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7517/*
7518 * Create a pseudo-"tearoff menu" based on the child
7519 * items of a given menu pointer.
7520 */
7521 static void
7522gui_mch_tearoff(
7523 char_u *title,
7524 vimmenu_T *menu,
7525 int initX,
7526 int initY)
7527{
7528 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7529 int template_len;
7530 int nchar, textWidth, submenuWidth;
7531 DWORD lStyle;
7532 DWORD lExtendedStyle;
7533 WORD dlgwidth;
7534 WORD menuID;
7535 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007536 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 vimmenu_T *the_menu = menu;
7538 HWND hwnd;
7539 HDC hdc;
7540 HFONT font, oldFont;
7541 int col, spaceWidth, len;
7542 int columnWidths[2];
7543 char_u *label, *text;
7544 int acLen = 0;
7545 int nameLen;
7546 int padding0, padding1, padding2 = 0;
7547 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007548 int x;
7549 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007550# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007551 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
7555 /*
7556 * If this menu is already torn off, move it to the mouse position.
7557 */
7558 if (IsWindow(menu->tearoff_handle))
7559 {
7560 POINT mp;
7561 if (GetCursorPos((LPPOINT)&mp))
7562 {
7563 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7564 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7565 }
7566 return;
7567 }
7568
7569 /*
7570 * Create a new tearoff.
7571 */
7572 if (*title == MNU_HIDDEN_CHAR)
7573 title++;
7574
Bram Moolenaar734a8672019-12-02 22:49:38 +01007575 // Allocate memory to store the dialog template. It's made bigger when
7576 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 template_len = DLG_ALLOC_SIZE;
7578 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7579 if (p == NULL)
7580 return;
7581
7582 hwnd = GetDesktopWindow();
7583 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007584# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7586 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007587 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 use_lfSysmenu = TRUE;
7589 }
7590 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7593 VARIABLE_PITCH , DLG_FONT_NAME);
7594 if (s_usenewlook)
7595 oldFont = SelectFont(hdc, font);
7596 else
7597 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7598
Bram Moolenaar734a8672019-12-02 22:49:38 +01007599 // Calculate width of a single space. Used for padding columns to the
7600 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007601 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
Bram Moolenaar734a8672019-12-02 22:49:38 +01007603 // Figure out max width of the text column, the accelerator column and the
7604 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 submenuWidth = 0;
7606 for (col = 0; col < 2; col++)
7607 {
7608 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007609 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007611 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 text = (col == 0) ? pmenu->dname : pmenu->actext;
7613 if (text != NULL && *text != NUL)
7614 {
7615 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7616 if (textWidth > columnWidths[col])
7617 columnWidths[col] = textWidth;
7618 }
7619 if (pmenu->children != NULL)
7620 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7621 }
7622 }
7623 if (columnWidths[1] == 0)
7624 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007625 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (submenuWidth != 0)
7627 columnWidths[0] += submenuWidth;
7628 else
7629 columnWidths[0] += spaceWidth;
7630 }
7631 else
7632 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007633 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7635 columnWidths[1] += submenuWidth;
7636 }
7637
7638 /*
7639 * Now find the total width of our 'menu'.
7640 */
7641 textWidth = columnWidths[0] + columnWidths[1];
7642 if (submenuWidth != 0)
7643 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007644 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7646 textWidth += submenuWidth;
7647 }
7648 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7649 if (textWidth > dlgwidth)
7650 dlgwidth = textWidth;
7651 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7652
Bram Moolenaar734a8672019-12-02 22:49:38 +01007653 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 if (s_usenewlook)
7655 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7656 else
7657 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7658
7659 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7660 *p++ = LOWORD(lStyle);
7661 *p++ = HIWORD(lStyle);
7662 *p++ = LOWORD(lExtendedStyle);
7663 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007664 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007666 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007668 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 else
7670 *p++ = PixelToDialogX(initX); // x
7671 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007672 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 else
7674 *p++ = PixelToDialogY(initY); // y
7675 *p++ = PixelToDialogX(dlgwidth); // cx
7676 ptrueheight = p;
7677 *p++ = 0; // dialog height: changed later anyway
7678 *p++ = 0; // Menu
7679 *p++ = 0; // Class
7680
Bram Moolenaar734a8672019-12-02 22:49:38 +01007681 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007683 ? (LPSTR)title
7684 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 p += nchar;
7686
7687 if (s_usenewlook)
7688 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007689 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007690# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 if (use_lfSysmenu)
7692 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007693 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7695 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007696 wcscpy(p, lfSysmenu.lfFaceName);
7697 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {
7702 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007703 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
7705 p += nchar;
7706 }
7707
7708 /*
7709 * Loop over all the items in the menu.
7710 * But skip over the tearbar.
7711 */
7712 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7713 menu = menu->children->next;
7714 else
7715 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007716 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 for ( ; menu != NULL; menu = menu->next)
7718 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007719 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 continue;
7721 if (menu_is_separator(menu->dname))
7722 {
7723 sepPadding += 3;
7724 continue;
7725 }
7726
Bram Moolenaar734a8672019-12-02 22:49:38 +01007727 // Check if there still is plenty of room in the template. Make it
7728 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7730 {
7731 WORD *newp;
7732
7733 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7734 if (newp != NULL)
7735 {
7736 template_len += 4096;
7737 mch_memmove(newp, pdlgtemplate,
7738 (char *)p - (char *)pdlgtemplate);
7739 p = newp + (p - pdlgtemplate);
7740 pnumitems = newp + (pnumitems - pdlgtemplate);
7741 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7742 LocalFree(LocalHandle(pdlgtemplate));
7743 pdlgtemplate = newp;
7744 }
7745 }
7746
Bram Moolenaar734a8672019-12-02 22:49:38 +01007747 // Figure out minimal length of this menu label. Use "name" for the
7748 // actual text, "dname" for estimating the displayed size. "name"
7749 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 len = nameLen = (int)STRLEN(menu->name);
7751 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7752 (int)STRLEN(menu->dname))) / spaceWidth;
7753 len += padding0;
7754
7755 if (menu->actext != NULL)
7756 {
7757 acLen = (int)STRLEN(menu->actext);
7758 len += acLen;
7759 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7760 }
7761 else
7762 textWidth = 0;
7763 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7764 len += padding1;
7765
7766 if (menu->children == NULL)
7767 {
7768 padding2 = submenuWidth / spaceWidth;
7769 len += padding2;
7770 menuID = (WORD)(menu->id);
7771 }
7772 else
7773 {
7774 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007775 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777
Bram Moolenaar734a8672019-12-02 22:49:38 +01007778 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007779 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 if (label == NULL)
7781 break;
7782
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007783 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007784 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007786 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 while (padding0-- > 0)
7788 *text++ = ' ';
7789 if (menu->actext != NULL)
7790 {
7791 STRNCPY(text, menu->actext, acLen);
7792 text += acLen;
7793 }
7794 while (padding1-- > 0)
7795 *text++ = ' ';
7796 if (menu->children != NULL)
7797 {
7798 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7799 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7800 }
7801 else
7802 {
7803 while (padding2-- > 0)
7804 *text++ = ' ';
7805 }
7806 *text = NUL;
7807
7808 /*
7809 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7810 * W95/NT4 it makes the tear-off look more like a menu.
7811 */
7812 p = add_dialog_element(p,
7813 BS_PUSHBUTTON|BS_LEFT,
7814 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7815 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7816 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7817 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007818 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 vim_free(label);
7820 (*pnumitems)++;
7821 }
7822
7823 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7824
7825
Bram Moolenaar734a8672019-12-02 22:49:38 +01007826 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007827 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007828 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 (LPDLGTEMPLATE)pdlgtemplate,
7830 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007831 (DLGPROC)tearoff_callback,
7832 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833
7834 LocalFree(LocalHandle(pdlgtemplate));
7835 SelectFont(hdc, oldFont);
7836 DeleteObject(font);
7837 ReleaseDC(hwnd, hdc);
7838
7839 /*
7840 * Reassert ourselves as the active window. This is so that after creating
7841 * a tearoff, the user doesn't have to click with the mouse just to start
7842 * typing again!
7843 */
7844 (void)SetActiveWindow(s_hwnd);
7845
Bram Moolenaar734a8672019-12-02 22:49:38 +01007846 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 force_menu_update = TRUE;
7848}
7849#endif
7850
7851#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007852# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
Bram Moolenaar734a8672019-12-02 22:49:38 +01007854// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855# ifndef TBSTYLE_FLAT
7856# define TBSTYLE_FLAT 0x0800
7857# endif
7858
7859/*
7860 * Create the toolbar, initially unpopulated.
7861 * (just like the menu, there are no defaults, it's all
7862 * set up through menu.vim)
7863 */
7864 static void
7865initialise_toolbar(void)
7866{
7867 InitCommonControls();
7868 s_toolbarhwnd = CreateToolbarEx(
7869 s_hwnd,
7870 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7871 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007872 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007873 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 IDR_TOOLBAR1, // id of initial bitmap
7875 NULL,
7876 0, // initial number of buttons
7877 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7878 TOOLBAR_BUTTON_HEIGHT,
7879 TOOLBAR_BUTTON_WIDTH,
7880 TOOLBAR_BUTTON_HEIGHT,
7881 sizeof(TBBUTTON)
7882 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007883
7884 // Remove transparency from the toolbar to prevent the main window
7885 // background colour showing through
7886 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7887 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7888
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007889 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890
7891 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7892}
7893
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007894 static LRESULT CALLBACK
7895toolbar_wndproc(
7896 HWND hwnd,
7897 UINT uMsg,
7898 WPARAM wParam,
7899 LPARAM lParam)
7900{
7901 HandleMouseHide(uMsg, lParam);
7902 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7903}
7904
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 static int
7906get_toolbar_bitmap(vimmenu_T *menu)
7907{
7908 int i = -1;
7909
7910 /*
7911 * Check user bitmaps first, unless builtin is specified.
7912 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007913 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {
7915 char_u fname[MAXPATHL];
7916 HANDLE hbitmap = NULL;
7917
7918 if (menu->iconfile != NULL)
7919 {
7920 gui_find_iconfile(menu->iconfile, fname, "bmp");
7921 hbitmap = LoadImage(
7922 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007923 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 IMAGE_BITMAP,
7925 TOOLBAR_BUTTON_WIDTH,
7926 TOOLBAR_BUTTON_HEIGHT,
7927 LR_LOADFROMFILE |
7928 LR_LOADMAP3DCOLORS
7929 );
7930 }
7931
7932 /*
7933 * If the LoadImage call failed, or the "icon=" file
7934 * didn't exist or wasn't specified, try the menu name
7935 */
7936 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007937 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007938# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007939 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007940# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007941 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 hbitmap = LoadImage(
7943 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007944 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 IMAGE_BITMAP,
7946 TOOLBAR_BUTTON_WIDTH,
7947 TOOLBAR_BUTTON_HEIGHT,
7948 LR_LOADFROMFILE |
7949 LR_LOADMAP3DCOLORS
7950 );
7951
7952 if (hbitmap != NULL)
7953 {
7954 TBADDBITMAP tbAddBitmap;
7955
7956 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007957 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
7959 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7960 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007961 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963 }
7964 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7965 i = menu->iconidx;
7966
7967 return i;
7968}
7969#endif
7970
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007971#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7972 static void
7973initialise_tabline(void)
7974{
7975 InitCommonControls();
7976
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007977 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007978 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007979 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007980 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007981 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007982
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007983 gui.tabline_height = TABLINE_HEIGHT;
7984
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007985# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007986 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007987# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007988}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007989
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007990/*
7991 * Get tabpage_T from POINT.
7992 */
7993 static tabpage_T *
7994GetTabFromPoint(
7995 HWND hWnd,
7996 POINT pt)
7997{
7998 tabpage_T *ptp = NULL;
7999
8000 if (gui_mch_showing_tabline())
8001 {
8002 TCHITTESTINFO htinfo;
8003 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008004 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008005 if (s_tabhwnd == hWnd)
8006 {
8007 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8008 if (idx != -1)
8009 ptp = find_tabpage(idx + 1);
8010 }
8011 }
8012 return ptp;
8013}
8014
8015static POINT s_pt = {0, 0};
8016static HCURSOR s_hCursor = NULL;
8017
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008018 static LRESULT CALLBACK
8019tabline_wndproc(
8020 HWND hwnd,
8021 UINT uMsg,
8022 WPARAM wParam,
8023 LPARAM lParam)
8024{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008025 POINT pt;
8026 tabpage_T *tp;
8027 RECT rect;
8028 int nCenter;
8029 int idx0;
8030 int idx1;
8031
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008032 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008033
8034 switch (uMsg)
8035 {
8036 case WM_LBUTTONDOWN:
8037 {
8038 s_pt.x = GET_X_LPARAM(lParam);
8039 s_pt.y = GET_Y_LPARAM(lParam);
8040 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008041 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008042 break;
8043 }
8044 case WM_MOUSEMOVE:
8045 if (GetCapture() == hwnd
8046 && ((wParam & MK_LBUTTON)) != 0)
8047 {
8048 pt.x = GET_X_LPARAM(lParam);
8049 pt.y = s_pt.y;
8050 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8051 {
8052 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8053
8054 tp = GetTabFromPoint(hwnd, pt);
8055 if (tp != NULL)
8056 {
8057 idx0 = tabpage_index(curtab) - 1;
8058 idx1 = tabpage_index(tp) - 1;
8059
8060 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8061 nCenter = rect.left + (rect.right - rect.left) / 2;
8062
Bram Moolenaar734a8672019-12-02 22:49:38 +01008063 // Check if the mouse cursor goes over the center of
8064 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008065 if ((idx0 < idx1) && (nCenter < pt.x))
8066 {
8067 tabpage_move(idx1 + 1);
8068 update_screen(0);
8069 }
8070 else if ((idx1 < idx0) && (pt.x < nCenter))
8071 {
8072 tabpage_move(idx1);
8073 update_screen(0);
8074 }
8075 }
8076 }
8077 }
8078 break;
8079 case WM_LBUTTONUP:
8080 {
8081 if (GetCapture() == hwnd)
8082 {
8083 SetCursor(s_hCursor);
8084 ReleaseCapture();
8085 }
8086 break;
8087 }
8088 default:
8089 break;
8090 }
8091
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008092 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8093}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008094#endif
8095
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8097/*
8098 * Make the GUI window come to the foreground.
8099 */
8100 void
8101gui_mch_set_foreground(void)
8102{
8103 if (IsIconic(s_hwnd))
8104 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8105 SetForegroundWindow(s_hwnd);
8106}
8107#endif
8108
8109#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8110 static void
8111dyn_imm_load(void)
8112{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008113 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 if (hLibImm == NULL)
8115 return;
8116
8117 pImmGetCompositionStringA
8118 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8119 pImmGetCompositionStringW
8120 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8121 pImmGetContext
8122 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8123 pImmAssociateContext
8124 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8125 pImmReleaseContext
8126 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8127 pImmGetOpenStatus
8128 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8129 pImmSetOpenStatus
8130 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008131 pImmGetCompositionFontW
8132 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8133 pImmSetCompositionFontW
8134 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 pImmSetCompositionWindow
8136 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8137 pImmGetConversionStatus
8138 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008139 pImmSetConversionStatus
8140 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141
8142 if ( pImmGetCompositionStringA == NULL
8143 || pImmGetCompositionStringW == NULL
8144 || pImmGetContext == NULL
8145 || pImmAssociateContext == NULL
8146 || pImmReleaseContext == NULL
8147 || pImmGetOpenStatus == NULL
8148 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008149 || pImmGetCompositionFontW == NULL
8150 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008152 || pImmGetConversionStatus == NULL
8153 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
8155 FreeLibrary(hLibImm);
8156 hLibImm = NULL;
8157 pImmGetContext = NULL;
8158 return;
8159 }
8160
8161 return;
8162}
8163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164#endif
8165
8166#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8167
8168# ifdef FEAT_XPM_W32
8169# define IMAGE_XPM 100
8170# endif
8171
8172typedef struct _signicon_t
8173{
8174 HANDLE hImage;
8175 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008176# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008177 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179} signicon_t;
8180
8181 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008182gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183{
8184 signicon_t *sign;
8185 int x, y, w, h;
8186
8187 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8188 return;
8189
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008190# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008191 if (IS_ENABLE_DIRECTX())
8192 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008193# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008194
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 x = TEXT_X(col);
8196 y = TEXT_Y(row);
8197 w = gui.char_width * 2;
8198 h = gui.char_height;
8199 switch (sign->uType)
8200 {
8201 case IMAGE_BITMAP:
8202 {
8203 HDC hdcMem;
8204 HBITMAP hbmpOld;
8205
8206 hdcMem = CreateCompatibleDC(s_hdc);
8207 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8208 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8209 SelectObject(hdcMem, hbmpOld);
8210 DeleteDC(hdcMem);
8211 }
8212 break;
8213 case IMAGE_ICON:
8214 case IMAGE_CURSOR:
8215 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8216 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008217# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 case IMAGE_XPM:
8219 {
8220 HDC hdcMem;
8221 HBITMAP hbmpOld;
8222
8223 hdcMem = CreateCompatibleDC(s_hdc);
8224 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008225 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8227
8228 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008229 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8231 SelectObject(hdcMem, hbmpOld);
8232 DeleteDC(hdcMem);
8233 }
8234 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237}
8238
8239 static void
8240close_signicon_image(signicon_t *sign)
8241{
8242 if (sign)
8243 switch (sign->uType)
8244 {
8245 case IMAGE_BITMAP:
8246 DeleteObject((HGDIOBJ)sign->hImage);
8247 break;
8248 case IMAGE_CURSOR:
8249 DestroyCursor((HCURSOR)sign->hImage);
8250 break;
8251 case IMAGE_ICON:
8252 DestroyIcon((HICON)sign->hImage);
8253 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008254# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 case IMAGE_XPM:
8256 DeleteObject((HBITMAP)sign->hImage);
8257 DeleteObject((HBITMAP)sign->hShape);
8258 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261}
8262
8263 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008264gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 signicon_t sign, *psign;
8267 char_u *ext;
8268
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008270 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (ext > signfile)
8272 {
8273 int do_load = 1;
8274
8275 if (!STRICMP(ext, ".bmp"))
8276 sign.uType = IMAGE_BITMAP;
8277 else if (!STRICMP(ext, ".ico"))
8278 sign.uType = IMAGE_ICON;
8279 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8280 sign.uType = IMAGE_CURSOR;
8281 else
8282 do_load = 0;
8283
8284 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008285 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 gui.char_width * 2, gui.char_height,
8287 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008288# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 if (!STRICMP(ext, ".xpm"))
8290 {
8291 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008292 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8293 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 }
8297
8298 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008299 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 *psign = sign;
8301
8302 if (!psign)
8303 {
8304 if (sign.hImage)
8305 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008306 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 return (void *)psign;
8309
8310}
8311
8312 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008313gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 if (sign)
8316 {
8317 close_signicon_image((signicon_t *)sign);
8318 vim_free(sign);
8319 }
8320}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008323#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324
Bram Moolenaar734a8672019-12-02 22:49:38 +01008325/*
8326 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008327 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008329 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8331 * to get current mouse position).
8332 *
8333 * Trying to use as more Windows services as possible, and as less
8334 * IE version as possible :)).
8335 *
8336 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8337 * BalloonEval struct.
8338 * 2) Enable/Disable simply create/kill BalloonEval Timer
8339 * 3) When there was enough inactivity, timer procedure posts
8340 * async request to debugger
8341 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8342 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008343 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 */
8345
Bram Moolenaar45360022005-07-21 21:08:21 +00008346/*
8347 * determine whether installed Common Controls support multiline tooltips
8348 * (i.e. their version is >= 4.70
8349 */
8350 int
8351multiline_balloon_available(void)
8352{
8353 HINSTANCE hDll;
8354 static char comctl_dll[] = "comctl32.dll";
8355 static int multiline_tip = MAYBE;
8356
8357 if (multiline_tip != MAYBE)
8358 return multiline_tip;
8359
8360 hDll = GetModuleHandle(comctl_dll);
8361 if (hDll != NULL)
8362 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008363 DLLGETVERSIONPROC pGetVer;
8364 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008365
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008366 if (pGetVer != NULL)
8367 {
8368 DLLVERSIONINFO dvi;
8369 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008370
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008371 ZeroMemory(&dvi, sizeof(dvi));
8372 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008373
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008374 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008375
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008376 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008377 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008378 || (dvi.dwMajorVersion == 4
8379 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008380 {
8381 multiline_tip = TRUE;
8382 return multiline_tip;
8383 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008384 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008385 else
8386 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008387 // there is chance we have ancient CommCtl 4.70
8388 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008389 DWORD dwHandle = 0;
8390 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8391 if (len > 0)
8392 {
8393 VS_FIXEDFILEINFO *ver;
8394 UINT vlen = 0;
8395 void *data = alloc(len);
8396
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008397 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008398 && GetFileVersionInfo(comctl_dll, 0, len, data)
8399 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8400 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008401 && HIWORD(ver->dwFileVersionMS) > 4)
8402 || ((HIWORD(ver->dwFileVersionMS) == 4
8403 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008404 {
8405 vim_free(data);
8406 multiline_tip = TRUE;
8407 return multiline_tip;
8408 }
8409 vim_free(data);
8410 }
8411 }
8412 }
8413 multiline_tip = FALSE;
8414 return multiline_tip;
8415}
8416
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008417 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008418make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008419{
8420 TOOLINFOW *pti;
8421 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008422
8423 if (multiline_balloon_available() == TRUE)
8424 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8425 else
8426 ToolInfoSize = sizeof(TOOLINFOW);
8427
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008428 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008429 if (pti == NULL)
8430 return;
8431
8432 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8433 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8434 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008435 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008436
8437 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8438 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8439
8440 pti->cbSize = ToolInfoSize;
8441 pti->uFlags = TTF_SUBCLASS;
8442 pti->hwnd = beval->target;
8443 pti->hinst = 0; // Don't use string resources
8444 pti->uId = ID_BEVAL_TOOLTIP;
8445
8446 if (multiline_balloon_available() == TRUE)
8447 {
8448 RECT rect;
8449 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8450 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008451 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8452 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008453 // switch multiline tooltips on
8454 if (GetClientRect(s_textArea, &rect))
8455 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8456 (LPARAM)rect.right);
8457 }
8458 else
8459 {
8460 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008461 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8462 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008463 }
8464
8465 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8466 pti->rect.left = pt.x - 3;
8467 pti->rect.top = pt.y - 3;
8468 pti->rect.right = pt.x + 3;
8469 pti->rect.bottom = pt.y + 3;
8470
8471 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8472 // Make tooltip appear sooner.
8473 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8474 // I've performed some tests and it seems the longest possible life time
8475 // of tooltip is 30 seconds.
8476 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8477 /*
8478 * HACK: force tooltip to appear, because it'll not appear until
8479 * first mouse move. D*mn M$
8480 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8481 */
8482 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8483 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8484 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008485}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008488delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008490 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491}
8492
8493 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008494BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008495 HWND hwnd UNUSED,
8496 UINT uMsg UNUSED,
8497 UINT_PTR idEvent UNUSED,
8498 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 POINT pt;
8501 RECT rect;
8502
8503 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8504 return;
8505
8506 GetCursorPos(&pt);
8507 if (WindowFromPoint(pt) != s_textArea)
8508 return;
8509
8510 ScreenToClient(s_textArea, &pt);
8511 GetClientRect(s_textArea, &rect);
8512 if (!PtInRect(&rect, pt))
8513 return;
8514
8515 if (LastActivity > 0
8516 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8517 && (cur_beval->showState != ShS_PENDING
8518 || abs(cur_beval->x - pt.x) > 3
8519 || abs(cur_beval->y - pt.y) > 3))
8520 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008521 // Pointer resting in one place long enough, it's time to show
8522 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 cur_beval->showState = ShS_PENDING;
8524 cur_beval->x = pt.x;
8525 cur_beval->y = pt.y;
8526
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008527 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 if (cur_beval->msgCB != NULL)
8530 (*cur_beval->msgCB)(cur_beval, 0);
8531 }
8532}
8533
8534 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008535gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008537 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008539 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540}
8541
8542 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008543gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008545 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 if (beval == NULL)
8547 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008548 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008549 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008550 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008554gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008557
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008558 vim_free(beval->msg);
8559 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8560 if (beval->msg == NULL)
8561 {
8562 delete_tooltip(beval);
8563 beval->showState = ShS_NEUTRAL;
8564 return;
8565 }
8566
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008567 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 if (beval->showState == ShS_SHOWING)
8569 return;
8570 GetCursorPos(&pt);
8571 ScreenToClient(s_textArea, &pt);
8572
8573 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008575 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 gui_mch_disable_beval_area(cur_beval);
8577 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008578 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008580 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008584gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008585 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008586 char_u *mesg,
8587 void (*mesgCB)(BalloonEval *, int),
8588 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008590 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 BalloonEval *beval;
8592
8593 if (mesg != NULL && mesgCB != NULL)
8594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008595 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 return NULL;
8597 }
8598
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008599 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (beval != NULL)
8601 {
8602 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603
8604 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 beval->msg = mesg;
8606 beval->msgCB = mesgCB;
8607 beval->clientData = clientData;
8608
8609 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 cur_beval = beval;
8611
8612 if (p_beval)
8613 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
8615 return beval;
8616}
8617
8618 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008619Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008621 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 return;
8623
8624 if (cur_beval != NULL)
8625 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008626 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008628 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008629 // TRACE0("TTN_SHOW {{{");
8630 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008631 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008632 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008633 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 delete_tooltip(cur_beval);
8635 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008636 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008639 break;
8640 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008641 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008642 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008643 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8644 info->lpszText = (LPSTR)info->lParam;
8645 info->uFlags |= TTF_DI_SETITEM;
8646 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008647 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008648 case TTN_GETDISPINFOW:
8649 {
8650 // if we get here then we have new common controls
8651 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8652 info->lpszText = (LPWSTR)info->lParam;
8653 info->uFlags |= TTF_DI_SETITEM;
8654 }
8655 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657 }
8658}
8659
8660 static void
8661TrackUserActivity(UINT uMsg)
8662{
8663 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8664 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8665 LastActivity = GetTickCount();
8666}
8667
8668 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008669gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008671# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008672 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008673# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008674 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 vim_free(beval);
8676}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008677#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8680/*
8681 * We have multiple signs to draw at the same location. Draw the
8682 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8683 */
8684 void
8685netbeans_draw_multisign_indicator(int row)
8686{
8687 int i;
8688 int y;
8689 int x;
8690
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008691 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008692 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 x = 0;
8695 y = TEXT_Y(row);
8696
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008697# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008698 if (IS_ENABLE_DIRECTX())
8699 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008700# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008701
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 for (i = 0; i < gui.char_height - 3; i++)
8703 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8704
8705 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8706 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8707 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8708 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8709 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8710 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8711 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8712}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008713#endif