blob: b879aa3c88b36ebca729437356ef07b58ccbafa1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010049# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
Bram Moolenaar734a8672019-12-02 22:49:38 +010061 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100120 else if (STRCMP(name, "scrlines") == 0)
121 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100122 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 else
125 return FAIL;
126 }
127
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100128 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100129 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 if (dx_enable)
133 {
134 if (!directx_enabled())
135 return FAIL;
136 DWriteContext_SetRenderingParams(s_dwc, NULL);
137 if (dx_flags)
138 {
139 DWriteRenderingParams param;
140 DWriteContext_GetRenderingParams(s_dwc, &param);
141 if (dx_flags & (1 << 0))
142 param.gamma = dx_gamma;
143 if (dx_flags & (1 << 1))
144 param.enhancedContrast = dx_contrast;
145 if (dx_flags & (1 << 2))
146 param.clearTypeLevel = dx_level;
147 if (dx_flags & (1 << 3))
148 param.pixelGeometry = dx_geom;
149 if (dx_flags & (1 << 4))
150 param.renderingMode = dx_renmode;
151 if (dx_flags & (1 << 5))
152 param.textAntialiasMode = dx_taamode;
153 DWriteContext_SetRenderingParams(s_dwc, &param);
154 }
155 }
156 s_directx_enabled = dx_enable;
157
158 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100159# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162}
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * These are new in Windows ME/XP, only defined in recent compilers.
167 */
168#ifndef HANDLE_WM_XBUTTONUP
169# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
170 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
171#endif
172#ifndef HANDLE_WM_XBUTTONDOWN
173# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDBLCLK
177# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100181
Bram Moolenaar734a8672019-12-02 22:49:38 +0100182#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
Bram Moolenaar734a8672019-12-02 22:49:38 +0100187// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188#ifndef PROTO
189
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100190# ifndef __MINGW32__
191# include <shellapi.h>
192# endif
193# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
194# include <commctrl.h>
195# endif
196# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifdef GLOBAL_IME
199# include "glbl_ime.h"
200# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
Bram Moolenaar734a8672019-12-02 22:49:38 +0100202#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
204#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100205# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100206#endif
207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200243# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200244# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define _cdecl
246typedef int BOOL;
247typedef int BYTE;
248typedef int DWORD;
249typedef int WCHAR;
250typedef int ENUMLOGFONT;
251typedef int FINDREPLACE;
252typedef int HANDLE;
253typedef int HBITMAP;
254typedef int HBRUSH;
255typedef int HDROP;
256typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100257typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100258typedef int LPARAM;
259typedef int LPCREATESTRUCT;
260typedef int LPCSTR;
261typedef int LPCTSTR;
262typedef int LPRECT;
263typedef int LPSTR;
264typedef int LPWINDOWPOS;
265typedef int LPWORD;
266typedef int LRESULT;
267typedef int HRESULT;
268# undef MSG
269typedef int MSG;
270typedef int NEWTEXTMETRIC;
271typedef int OSVERSIONINFO;
272typedef int PWORD;
273typedef int RECT;
274typedef int UINT;
275typedef int WORD;
276typedef int WPARAM;
277typedef int POINT;
278typedef void *HINSTANCE;
279typedef void *HMENU;
280typedef void *HWND;
281typedef void *HDC;
282typedef void VOID;
283typedef int LPNMHDR;
284typedef int LONG;
285typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200286typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200287typedef int COLORREF;
288typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
295static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200317static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
355#ifdef GLOBAL_IME
356# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
357#else
358# define MyTranslateMessage(x) TranslateMessage(x)
359#endif
360
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100361#if defined(FEAT_DIRECTX)
362 static int
363directx_enabled(void)
364{
365 if (s_dwc != NULL)
366 return 1;
367 else if (s_directx_load_attempted)
368 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100369 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100370 DWrite_Init();
371 s_directx_load_attempted = 1;
372 s_dwc = DWriteContext_Open();
373 directx_binddc();
374 return s_dwc != NULL ? 1 : 0;
375}
376
377 static void
378directx_binddc(void)
379{
380 if (s_textArea != NULL)
381 {
382 RECT rect;
383 GetClientRect(s_textArea, &rect);
384 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
385 }
386}
387#endif
388
Bram Moolenaar734a8672019-12-02 22:49:38 +0100389// use of WindowProc depends on Global IME
Bram Moolenaar945c8572020-07-17 22:17:03 +0200390static LRESULT WINAPI MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100391
Bram Moolenaar734a8672019-12-02 22:49:38 +0100392extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100393
394static struct
395{
396 UINT key_sym;
397 char_u vim_code0;
398 char_u vim_code1;
399} special_keys[] =
400{
401 {VK_UP, 'k', 'u'},
402 {VK_DOWN, 'k', 'd'},
403 {VK_LEFT, 'k', 'l'},
404 {VK_RIGHT, 'k', 'r'},
405
406 {VK_F1, 'k', '1'},
407 {VK_F2, 'k', '2'},
408 {VK_F3, 'k', '3'},
409 {VK_F4, 'k', '4'},
410 {VK_F5, 'k', '5'},
411 {VK_F6, 'k', '6'},
412 {VK_F7, 'k', '7'},
413 {VK_F8, 'k', '8'},
414 {VK_F9, 'k', '9'},
415 {VK_F10, 'k', ';'},
416
417 {VK_F11, 'F', '1'},
418 {VK_F12, 'F', '2'},
419 {VK_F13, 'F', '3'},
420 {VK_F14, 'F', '4'},
421 {VK_F15, 'F', '5'},
422 {VK_F16, 'F', '6'},
423 {VK_F17, 'F', '7'},
424 {VK_F18, 'F', '8'},
425 {VK_F19, 'F', '9'},
426 {VK_F20, 'F', 'A'},
427
428 {VK_F21, 'F', 'B'},
429#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100430 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100431#endif
432 {VK_F22, 'F', 'C'},
433 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100434 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100435
436 {VK_HELP, '%', '1'},
437 {VK_BACK, 'k', 'b'},
438 {VK_INSERT, 'k', 'I'},
439 {VK_DELETE, 'k', 'D'},
440 {VK_HOME, 'k', 'h'},
441 {VK_END, '@', '7'},
442 {VK_PRIOR, 'k', 'P'},
443 {VK_NEXT, 'k', 'N'},
444 {VK_PRINT, '%', '9'},
445 {VK_ADD, 'K', '6'},
446 {VK_SUBTRACT, 'K', '7'},
447 {VK_DIVIDE, 'K', '8'},
448 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100449 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100450 {VK_DECIMAL, 'K', 'B'},
451
452 {VK_NUMPAD0, 'K', 'C'},
453 {VK_NUMPAD1, 'K', 'D'},
454 {VK_NUMPAD2, 'K', 'E'},
455 {VK_NUMPAD3, 'K', 'F'},
456 {VK_NUMPAD4, 'K', 'G'},
457 {VK_NUMPAD5, 'K', 'H'},
458 {VK_NUMPAD6, 'K', 'I'},
459 {VK_NUMPAD7, 'K', 'J'},
460 {VK_NUMPAD8, 'K', 'K'},
461 {VK_NUMPAD9, 'K', 'L'},
462
Bram Moolenaar734a8672019-12-02 22:49:38 +0100463 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100464 {VK_SPACE, ' ', NUL},
465 {VK_TAB, TAB, NUL},
466 {VK_ESCAPE, ESC, NUL},
467 {NL, NL, NUL},
468 {CAR, CAR, NUL},
469
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471 {0, 0, 0}
472};
473
Bram Moolenaar734a8672019-12-02 22:49:38 +0100474// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100475static int s_button_pending = -1;
476
Bram Moolenaar734a8672019-12-02 22:49:38 +0100477// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
478// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479static int s_getting_focus = FALSE;
480
481static int s_x_pending;
482static int s_y_pending;
483static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200484static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200486static int dead_key = 0; // 0: no dead key, 1: dead key pressed
487static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
488 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100490#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100491// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
493static void TrackUserActivity(UINT uMsg);
494#endif
495
496/*
497 * For control IME.
498 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100499 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100501#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100502// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100503static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#endif
505#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100507static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100508#endif
509
510#ifdef FEAT_MBYTE_IME
511static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
512#endif
513
514#if defined(FEAT_BROWSE)
515static char_u *convert_filter(char_u *s);
516#endif
517
518#ifdef DEBUG_PRINT_ERROR
519/*
520 * Print out the last Windows error message
521 */
522 static void
523print_windows_error(void)
524{
525 LPVOID lpMsgBuf;
526
527 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
528 NULL, GetLastError(),
529 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
530 (LPTSTR) &lpMsgBuf, 0, NULL);
531 TRACE1("Error: %s\n", lpMsgBuf);
532 LocalFree(lpMsgBuf);
533}
534#endif
535
536/*
537 * Cursor blink functions.
538 *
539 * This is a simple state machine:
540 * BLINK_NONE not blinking at all
541 * BLINK_OFF blinking, cursor is not shown
542 * BLINK_ON blinking, cursor is shown
543 */
544
545#define BLINK_NONE 0
546#define BLINK_OFF 1
547#define BLINK_ON 2
548
549static int blink_state = BLINK_NONE;
550static long_u blink_waittime = 700;
551static long_u blink_ontime = 400;
552static long_u blink_offtime = 250;
553static UINT blink_timer = 0;
554
Bram Moolenaar703a8042016-06-04 16:24:32 +0200555 int
556gui_mch_is_blinking(void)
557{
558 return blink_state != BLINK_NONE;
559}
560
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200561 int
562gui_mch_is_blink_off(void)
563{
564 return blink_state == BLINK_OFF;
565}
566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100567 void
568gui_mch_set_blinking(long wait, long on, long off)
569{
570 blink_waittime = wait;
571 blink_ontime = on;
572 blink_offtime = off;
573}
574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 static VOID CALLBACK
576_OnBlinkTimer(
577 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100578 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100580 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100581{
582 MSG msg;
583
584 /*
585 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
586 */
587
588 KillTimer(NULL, idEvent);
589
Bram Moolenaar734a8672019-12-02 22:49:38 +0100590 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100591 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
592 ;
593
594 if (blink_state == BLINK_ON)
595 {
596 gui_undraw_cursor();
597 blink_state = BLINK_OFF;
598 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
599 (TIMERPROC)_OnBlinkTimer);
600 }
601 else
602 {
603 gui_update_cursor(TRUE, FALSE);
604 blink_state = BLINK_ON;
605 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100606 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100607 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100608 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609}
610
611 static void
612gui_mswin_rm_blink_timer(void)
613{
614 MSG msg;
615
616 if (blink_timer != 0)
617 {
618 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100619 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100620 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
621 ;
622 blink_timer = 0;
623 }
624}
625
626/*
627 * Stop the cursor blinking. Show the cursor if it wasn't shown.
628 */
629 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100630gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100631{
632 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100634 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100636 gui_mch_flush();
637 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 blink_state = BLINK_NONE;
639}
640
641/*
642 * Start the cursor blinking. If it was already blinking, this restarts the
643 * waiting time and shows the cursor.
644 */
645 void
646gui_mch_start_blink(void)
647{
648 gui_mswin_rm_blink_timer();
649
Bram Moolenaar734a8672019-12-02 22:49:38 +0100650 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
652 {
653 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
654 (TIMERPROC)_OnBlinkTimer);
655 blink_state = BLINK_ON;
656 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100657 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100658 }
659}
660
661/*
662 * Call-back routines.
663 */
664
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665 static VOID CALLBACK
666_OnTimer(
667 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100668 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100670 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671{
672 MSG msg;
673
674 /*
675 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
676 */
677 KillTimer(NULL, idEvent);
678 s_timed_out = TRUE;
679
Bram Moolenaar734a8672019-12-02 22:49:38 +0100680 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
682 ;
683 if (idEvent == s_wait_timer)
684 s_wait_timer = 0;
685}
686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100687 static void
688_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100689 HWND hwnd UNUSED,
690 UINT ch UNUSED,
691 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100692{
693 dead_key = 1;
694}
695
696/*
697 * Convert Unicode character "ch" to bytes in "string[slen]".
698 * When "had_alt" is TRUE the ALT key was included in "ch".
699 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200700 * Because the Windows API uses UTF-16, we have to deal with surrogate
701 * pairs; this is where we choose to deal with them: if "ch" is a high
702 * surrogate, it will be stored, and the length returned will be zero; the next
703 * char_to_string call will then include the high surrogate, decoding the pair
704 * of UTF-16 code units to a single Unicode code point, presuming it is the
705 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 */
707 static int
708char_to_string(int ch, char_u *string, int slen, int had_alt)
709{
710 int len;
711 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200713 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200715 if (surrogate_pending_ch != 0)
716 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100717 // We don't guarantee ch is a low surrogate to match the high surrogate
718 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200719 wstring[0] = surrogate_pending_ch;
720 wstring[1] = ch;
721 surrogate_pending_ch = 0;
722 len = 2;
723 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100724 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200725 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100726 // We don't have the entire code point yet, only the first UTF-16 code
727 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200728 surrogate_pending_ch = ch;
729 return 0;
730 }
731 else
732 {
733 wstring[0] = ch;
734 len = 1;
735 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200736
Bram Moolenaar734a8672019-12-02 22:49:38 +0100737 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
738 // "enc_codepage" is non-zero use the standard Win32 function,
739 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200740 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200742 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
743 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100744 // If we had included the ALT key into the character but now the
745 // upper bit is no longer set, that probably means the conversion
746 // failed. Convert the original character and set the upper bit
747 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200748 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200750 wstring[0] = ch & 0x7f;
751 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
752 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100753 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200754 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 }
756 }
757 else
758 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200759 ws = utf16_to_enc(wstring, &len);
760 if (ws == NULL)
761 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762 else
763 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100764 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200765 len = slen;
766 mch_memmove(string, ws, len);
767 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100768 }
769 }
770
771 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
773 string[0] = ch;
774 len = 1;
775 }
776
777 for (i = 0; i < len; ++i)
778 if (string[i] == CSI && len <= slen - 2)
779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100781 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
782 string[++i] = KS_EXTRA;
783 string[++i] = (int)KE_CSI;
784 len += 2;
785 }
786
787 return len;
788}
789
790/*
791 * Key hit, add it to the input buffer.
792 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 static void
794_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100795 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100797 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798{
799 char_u string[40];
800 int len = 0;
801
802 dead_key = 0;
803
804 len = char_to_string(ch, string, 40, FALSE);
805 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
806 {
807 trash_input_buf();
808 got_int = TRUE;
809 }
810
811 add_to_input_buf(string, len);
812}
813
814/*
815 * Alt-Key hit, add it to the input buffer.
816 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 static void
818_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100819 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100821 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100823 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 int len;
825 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100826 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827
828 dead_key = 0;
829
Bram Moolenaar734a8672019-12-02 22:49:38 +0100830 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100831
Bram Moolenaar734a8672019-12-02 22:49:38 +0100832 // OK, we have a character key (given by ch) which was entered with the
833 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
834 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
835 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100836 modifiers = MOD_MASK_ALT;
837 if (GetKeyState(VK_SHIFT) & 0x8000)
838 modifiers |= MOD_MASK_SHIFT;
839 if (GetKeyState(VK_CONTROL) & 0x8000)
840 modifiers |= MOD_MASK_CTRL;
841
842 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100843 // remove the SHIFT modifier for keys where it's already included, e.g.,
844 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200845 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100846
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200847 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
848 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100849 if (ch == CSI)
850 ch = K_CSI;
851
852 len = 0;
853 if (modifiers)
854 {
855 string[len++] = CSI;
856 string[len++] = KS_MODIFIER;
857 string[len++] = modifiers;
858 }
859
860 if (IS_SPECIAL((int)ch))
861 {
862 string[len++] = CSI;
863 string[len++] = K_SECOND((int)ch);
864 string[len++] = K_THIRD((int)ch);
865 }
866 else
867 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100868 // Although the documentation isn't clear about it, we assume "ch" is
869 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870 len += char_to_string(ch, string + len, 40 - len, TRUE);
871 }
872
873 add_to_input_buf(string, len);
874}
875
876 static void
877_OnMouseEvent(
878 int button,
879 int x,
880 int y,
881 int repeated_click,
882 UINT keyFlags)
883{
884 int vim_modifiers = 0x0;
885
886 s_getting_focus = FALSE;
887
888 if (keyFlags & MK_SHIFT)
889 vim_modifiers |= MOUSE_SHIFT;
890 if (keyFlags & MK_CONTROL)
891 vim_modifiers |= MOUSE_CTRL;
892 if (GetKeyState(VK_MENU) & 0x8000)
893 vim_modifiers |= MOUSE_ALT;
894
895 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
896}
897
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100898 static void
899_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100900 HWND hwnd UNUSED,
901 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100902 int x,
903 int y,
904 UINT keyFlags)
905{
906 static LONG s_prevTime = 0;
907
908 LONG currentTime = GetMessageTime();
909 int button = -1;
910 int repeated_click;
911
Bram Moolenaar734a8672019-12-02 22:49:38 +0100912 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100913 (void)SetFocus(s_hwnd);
914
915 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
916 button = MOUSE_LEFT;
917 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
918 button = MOUSE_MIDDLE;
919 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
920 button = MOUSE_RIGHT;
921 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
922 {
923#ifndef GET_XBUTTON_WPARAM
924# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
925#endif
926 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
927 }
928 else if (s_uMsg == WM_CAPTURECHANGED)
929 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100930 // on W95/NT4, somehow you get in here with an odd Msg
931 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 if (s_button_pending == MOUSE_LEFT)
933 button = MOUSE_RIGHT;
934 else
935 button = MOUSE_LEFT;
936 }
937 if (button >= 0)
938 {
939 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
940
941 /*
942 * Holding down the left and right buttons simulates pushing the middle
943 * button.
944 */
945 if (repeated_click
946 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
947 || (button == MOUSE_RIGHT
948 && s_button_pending == MOUSE_LEFT)))
949 {
950 /*
951 * Hmm, gui.c will ignore more than one button down at a time, so
952 * pretend we let go of it first.
953 */
954 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
955 button = MOUSE_MIDDLE;
956 repeated_click = FALSE;
957 s_button_pending = -1;
958 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
959 }
960 else if ((repeated_click)
961 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
962 {
963 if (s_button_pending > -1)
964 {
965 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
966 s_button_pending = -1;
967 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100968 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100969 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
970 }
971 else
972 {
973 /*
974 * If this is the first press (i.e. not a multiple click) don't
975 * action immediately, but store and wait for:
976 * i) button-up
977 * ii) mouse move
978 * iii) another button press
979 * before using it.
980 * This enables us to make left+right simulate middle button,
981 * without left or right being actioned first. The side-effect is
982 * that if you click and hold the mouse without dragging, the
983 * cursor doesn't move until you release the button. In practice
984 * this is hardly a problem.
985 */
986 s_button_pending = button;
987 s_x_pending = x;
988 s_y_pending = y;
989 s_kFlags_pending = keyFlags;
990 }
991
992 s_prevTime = currentTime;
993 }
994}
995
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100996 static void
997_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100998 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100999 int x,
1000 int y,
1001 UINT keyFlags)
1002{
1003 int button;
1004
1005 s_getting_focus = FALSE;
1006 if (s_button_pending > -1)
1007 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001008 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001009 _OnMouseEvent(s_button_pending, s_x_pending,
1010 s_y_pending, FALSE, s_kFlags_pending);
1011 s_button_pending = -1;
1012 }
1013 if (s_uMsg == WM_MOUSEMOVE)
1014 {
1015 /*
1016 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1017 * down.
1018 */
1019 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1020 | MK_XBUTTON1 | MK_XBUTTON2)))
1021 {
1022 gui_mouse_moved(x, y);
1023 return;
1024 }
1025
1026 /*
1027 * While button is down, keep grabbing mouse move events when
1028 * the mouse goes outside the window
1029 */
1030 SetCapture(s_textArea);
1031 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001032 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001033 }
1034 else
1035 {
1036 ReleaseCapture();
1037 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001038 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001039 }
1040
1041 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1042}
1043
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001044 static void
1045_OnSizeTextArea(
1046 HWND hwnd UNUSED,
1047 UINT state UNUSED,
1048 int cx UNUSED,
1049 int cy UNUSED)
1050{
1051#if defined(FEAT_DIRECTX)
1052 if (IS_ENABLE_DIRECTX())
1053 directx_binddc();
1054#endif
1055}
1056
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057#ifdef FEAT_MENU
1058/*
1059 * Find the vimmenu_T with the given id
1060 */
1061 static vimmenu_T *
1062gui_mswin_find_menu(
1063 vimmenu_T *pMenu,
1064 int id)
1065{
1066 vimmenu_T *pChildMenu;
1067
1068 while (pMenu)
1069 {
1070 if (pMenu->id == (UINT)id)
1071 break;
1072 if (pMenu->children != NULL)
1073 {
1074 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1075 if (pChildMenu)
1076 {
1077 pMenu = pChildMenu;
1078 break;
1079 }
1080 }
1081 pMenu = pMenu->next;
1082 }
1083 return pMenu;
1084}
1085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001086 static void
1087_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001088 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001089 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001090 HWND hwndCtl UNUSED,
1091 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092{
1093 vimmenu_T *pMenu;
1094
1095 pMenu = gui_mswin_find_menu(root_menu, id);
1096 if (pMenu)
1097 gui_menu_cb(pMenu);
1098}
1099#endif
1100
1101#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103 * Handle a Find/Replace window message.
1104 */
1105 static void
1106_OnFindRepl(void)
1107{
1108 int flags = 0;
1109 int down;
1110
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001111 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001112 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001113 (void)SetFocus(s_hwnd);
1114
1115 if (s_findrep_struct.Flags & FR_FINDNEXT)
1116 {
1117 flags = FRD_FINDNEXT;
1118
Bram Moolenaar734a8672019-12-02 22:49:38 +01001119 // Give main window the focus back: this is so the cursor isn't
1120 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001121 (void)SetFocus(s_hwnd);
1122 }
1123 else if (s_findrep_struct.Flags & FR_REPLACE)
1124 {
1125 flags = FRD_REPLACE;
1126
Bram Moolenaar734a8672019-12-02 22:49:38 +01001127 // Give main window the focus back: this is so the cursor isn't
1128 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001129 (void)SetFocus(s_hwnd);
1130 }
1131 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1132 {
1133 flags = FRD_REPLACEALL;
1134 }
1135
1136 if (flags != 0)
1137 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001138 char_u *p, *q;
1139
Bram Moolenaar734a8672019-12-02 22:49:38 +01001140 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001141 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1142 flags |= FRD_WHOLE_WORD;
1143 if (s_findrep_struct.Flags & FR_MATCHCASE)
1144 flags |= FRD_MATCH_CASE;
1145 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001146 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1147 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1148 if (p != NULL && q != NULL)
1149 gui_do_findrepl(flags, p, q, down);
1150 vim_free(p);
1151 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 }
1153}
1154#endif
1155
1156 static void
1157HandleMouseHide(UINT uMsg, LPARAM lParam)
1158{
1159 static LPARAM last_lParam = 0L;
1160
Bram Moolenaar734a8672019-12-02 22:49:38 +01001161 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001162 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1163 {
1164 if (lParam == last_lParam)
1165 return;
1166 last_lParam = lParam;
1167 }
1168
Bram Moolenaar734a8672019-12-02 22:49:38 +01001169 // Handle specially, to centralise coding. We need to be sure we catch all
1170 // possible events which should cause us to restore the cursor (as it is a
1171 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001172 switch (uMsg)
1173 {
1174 case WM_KEYUP:
1175 case WM_CHAR:
1176 /*
1177 * blank out the pointer if necessary
1178 */
1179 if (p_mh)
1180 gui_mch_mousehide(TRUE);
1181 break;
1182
Bram Moolenaar734a8672019-12-02 22:49:38 +01001183 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001185 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 case WM_LBUTTONDOWN:
1187 case WM_LBUTTONUP:
1188 case WM_MBUTTONDOWN:
1189 case WM_MBUTTONUP:
1190 case WM_RBUTTONDOWN:
1191 case WM_RBUTTONUP:
1192 case WM_XBUTTONDOWN:
1193 case WM_XBUTTONUP:
1194 case WM_NCMOUSEMOVE:
1195 case WM_NCLBUTTONDOWN:
1196 case WM_NCLBUTTONUP:
1197 case WM_NCMBUTTONDOWN:
1198 case WM_NCMBUTTONUP:
1199 case WM_NCRBUTTONDOWN:
1200 case WM_NCRBUTTONUP:
1201 case WM_KILLFOCUS:
1202 /*
1203 * if the pointer is currently hidden, then we should show it.
1204 */
1205 gui_mch_mousehide(FALSE);
1206 break;
1207 }
1208}
1209
1210 static LRESULT CALLBACK
1211_TextAreaWndProc(
1212 HWND hwnd,
1213 UINT uMsg,
1214 WPARAM wParam,
1215 LPARAM lParam)
1216{
1217 /*
1218 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1219 hwnd, uMsg, wParam, lParam);
1220 */
1221
1222 HandleMouseHide(uMsg, lParam);
1223
1224 s_uMsg = uMsg;
1225 s_wParam = wParam;
1226 s_lParam = lParam;
1227
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001228#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001229 TrackUserActivity(uMsg);
1230#endif
1231
1232 switch (uMsg)
1233 {
1234 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1235 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1236 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1237 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1238 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1239 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1240 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1241 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1242 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1243 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1245 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1247 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001248 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001250#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001251 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1252 return TRUE;
1253#endif
1254 default:
1255 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1256 }
1257}
1258
Bram Moolenaar945c8572020-07-17 22:17:03 +02001259 static LRESULT WINAPI
1260MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001262#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001264#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001265 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266#endif
1267}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269/*
1270 * Called when the foreground or background color has been changed.
1271 */
1272 void
1273gui_mch_new_colors(void)
1274{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001275 HBRUSH prevBrush;
1276
1277 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001278 prevBrush = (HBRUSH)SetClassLongPtr(
1279 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001280 InvalidateRect(s_hwnd, NULL, TRUE);
1281 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282}
1283
1284/*
1285 * Set the colors to their default values.
1286 */
1287 void
1288gui_mch_def_colors(void)
1289{
1290 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1291 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1292 gui.def_norm_pixel = gui.norm_pixel;
1293 gui.def_back_pixel = gui.back_pixel;
1294}
1295
1296/*
1297 * Open the GUI window which was created by a call to gui_mch_init().
1298 */
1299 int
1300gui_mch_open(void)
1301{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001302 // Actually open the window, if not already visible
1303 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304 if (!IsWindowVisible(s_hwnd))
1305 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1306
1307#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001308 // Init replace string here, so that we keep it when re-opening the
1309 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001310 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1311#endif
1312
1313 return OK;
1314}
1315
1316/*
1317 * Get the position of the top left corner of the window.
1318 */
1319 int
1320gui_mch_get_winpos(int *x, int *y)
1321{
1322 RECT rect;
1323
1324 GetWindowRect(s_hwnd, &rect);
1325 *x = rect.left;
1326 *y = rect.top;
1327 return OK;
1328}
1329
1330/*
1331 * Set the position of the top left corner of the window to the given
1332 * coordinates.
1333 */
1334 void
1335gui_mch_set_winpos(int x, int y)
1336{
1337 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1338 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1339}
1340 void
1341gui_mch_set_text_area_pos(int x, int y, int w, int h)
1342{
1343 static int oldx = 0;
1344 static int oldy = 0;
1345
1346 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1347
1348#ifdef FEAT_TOOLBAR
1349 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1350 SendMessage(s_toolbarhwnd, WM_SIZE,
1351 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1352#endif
1353#if defined(FEAT_GUI_TABLINE)
1354 if (showing_tabline)
1355 {
1356 int top = 0;
1357 RECT rect;
1358
1359# ifdef FEAT_TOOLBAR
1360 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1361 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1362# endif
1363 GetClientRect(s_hwnd, &rect);
1364 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1365 }
1366#endif
1367
Bram Moolenaar734a8672019-12-02 22:49:38 +01001368 // When side scroll bar is unshown, the size of window will change.
1369 // then, the text area move left or right. thus client rect should be
1370 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001371 if (oldx != x || oldy != y)
1372 {
1373 InvalidateRect(s_hwnd, NULL, FALSE);
1374 oldx = x;
1375 oldy = y;
1376 }
1377}
1378
1379
1380/*
1381 * Scrollbar stuff:
1382 */
1383
1384 void
1385gui_mch_enable_scrollbar(
1386 scrollbar_T *sb,
1387 int flag)
1388{
1389 ShowScrollBar(sb->id, SB_CTL, flag);
1390
Bram Moolenaar734a8672019-12-02 22:49:38 +01001391 // TODO: When the window is maximized, the size of the window stays the
1392 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1393 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001394}
1395
1396 void
1397gui_mch_set_scrollbar_pos(
1398 scrollbar_T *sb,
1399 int x,
1400 int y,
1401 int w,
1402 int h)
1403{
1404 SetWindowPos(sb->id, NULL, x, y, w, h,
1405 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1406}
1407
Bram Moolenaar203ec772020-07-17 20:43:43 +02001408 int
1409gui_mch_get_scrollbar_xpadding(void)
1410{
1411 RECT rcTxt, rcWnd;
1412 int xpad;
1413
1414 GetWindowRect(s_textArea, &rcTxt);
1415 GetWindowRect(s_hwnd, &rcWnd);
1416 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1417 - GetSystemMetrics(SM_CXFRAME)
1418 - GetSystemMetrics(SM_CXPADDEDBORDER);
1419 return (xpad < 0) ? 0 : xpad;
1420}
1421
1422 int
1423gui_mch_get_scrollbar_ypadding(void)
1424{
1425 RECT rcTxt, rcWnd;
1426 int ypad;
1427
1428 GetWindowRect(s_textArea, &rcTxt);
1429 GetWindowRect(s_hwnd, &rcWnd);
1430 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1431 - GetSystemMetrics(SM_CYFRAME)
1432 - GetSystemMetrics(SM_CXPADDEDBORDER);
1433 return (ypad < 0) ? 0 : ypad;
1434}
1435
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001436 void
1437gui_mch_create_scrollbar(
1438 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001439 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001440{
1441 sb->id = CreateWindow(
1442 "SCROLLBAR", "Scrollbar",
1443 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001444 10, // Any value will do for now
1445 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001446 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001447 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448}
1449
1450/*
1451 * Find the scrollbar with the given hwnd.
1452 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001453 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001454gui_mswin_find_scrollbar(HWND hwnd)
1455{
1456 win_T *wp;
1457
1458 if (gui.bottom_sbar.id == hwnd)
1459 return &gui.bottom_sbar;
1460 FOR_ALL_WINDOWS(wp)
1461 {
1462 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1463 return &wp->w_scrollbars[SBAR_LEFT];
1464 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1465 return &wp->w_scrollbars[SBAR_RIGHT];
1466 }
1467 return NULL;
1468}
1469
1470/*
1471 * Get the character size of a font.
1472 */
1473 static void
1474GetFontSize(GuiFont font)
1475{
1476 HWND hwnd = GetDesktopWindow();
1477 HDC hdc = GetWindowDC(hwnd);
1478 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001479 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001480 TEXTMETRIC tm;
1481
1482 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001483 // GetTextMetrics() may not return the right value in tmAveCharWidth
1484 // for some fonts. Do our own average computation.
1485 GetTextExtentPoint(hdc,
1486 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1487 52, &size);
1488 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001489
1490 gui.char_height = tm.tmHeight + p_linespace;
1491
1492 SelectFont(hdc, hfntOld);
1493
1494 ReleaseDC(hwnd, hdc);
1495}
1496
1497/*
1498 * Adjust gui.char_height (after 'linespace' was changed).
1499 */
1500 int
1501gui_mch_adjust_charheight(void)
1502{
1503 GetFontSize(gui.norm_font);
1504 return OK;
1505}
1506
1507 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001508get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001509{
1510 HFONT font = NULL;
1511
Bram Moolenaar734a8672019-12-02 22:49:38 +01001512 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001513 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001514
1515 if (font == NULL)
1516 return NOFONT;
1517
1518 return (GuiFont)font;
1519}
1520
1521 static int
1522pixels_to_points(int pixels, int vertical)
1523{
1524 int points;
1525 HWND hwnd;
1526 HDC hdc;
1527
1528 hwnd = GetDesktopWindow();
1529 hdc = GetWindowDC(hwnd);
1530
1531 points = MulDiv(pixels, 72,
1532 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1533
1534 ReleaseDC(hwnd, hdc);
1535
1536 return points;
1537}
1538
1539 GuiFont
1540gui_mch_get_font(
1541 char_u *name,
1542 int giveErrorIfMissing)
1543{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001544 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001545 GuiFont font = NOFONT;
1546
1547 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1548 font = get_font_handle(&lf);
1549 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001550 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001551 return font;
1552}
1553
1554#if defined(FEAT_EVAL) || defined(PROTO)
1555/*
1556 * Return the name of font "font" in allocated memory.
1557 * Don't know how to get the actual name, thus use the provided name.
1558 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001560gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001561{
1562 if (name == NULL)
1563 return NULL;
1564 return vim_strsave(name);
1565}
1566#endif
1567
1568 void
1569gui_mch_free_font(GuiFont font)
1570{
1571 if (font)
1572 DeleteObject((HFONT)font);
1573}
1574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001575/*
1576 * Return the Pixel value (color) for the given color name.
1577 * Return INVALCOLOR for error.
1578 */
1579 guicolor_T
1580gui_mch_get_color(char_u *name)
1581{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001582 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001583
1584 typedef struct SysColorTable
1585 {
1586 char *name;
1587 int color;
1588 } SysColorTable;
1589
1590 static SysColorTable sys_table[] =
1591 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1593 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001594#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1596#endif
1597 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1598 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1599 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1600 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1601 {"SYS_DESKTOP", COLOR_DESKTOP},
1602 {"SYS_INFOBK", COLOR_INFOBK},
1603 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1604 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001605 {"SYS_BTNFACE", COLOR_BTNFACE},
1606 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1607 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1608 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1609 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1610 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1611 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1612 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1613 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1614 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1615 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1616 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1617 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1618 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1619 {"SYS_MENU", COLOR_MENU},
1620 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1621 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1622 {"SYS_WINDOW", COLOR_WINDOW},
1623 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1624 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1625 };
1626
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 /*
1628 * Try to look up a system colour.
1629 */
K.Takataeeec2542021-06-02 13:28:16 +02001630 for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001631 if (STRICMP(name, sys_table[i].name) == 0)
1632 return GetSysColor(sys_table[i].color);
1633
Bram Moolenaarab302212016-04-26 20:59:29 +02001634 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001635}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001636
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001637 guicolor_T
1638gui_mch_get_rgb_color(int r, int g, int b)
1639{
1640 return gui_get_rgb_color_cmn(r, g, b);
1641}
1642
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643/*
1644 * Return OK if the key with the termcap name "name" is supported.
1645 */
1646 int
1647gui_mch_haskey(char_u *name)
1648{
1649 int i;
1650
1651 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1652 if (name[0] == special_keys[i].vim_code0 &&
1653 name[1] == special_keys[i].vim_code1)
1654 return OK;
1655 return FAIL;
1656}
1657
1658 void
1659gui_mch_beep(void)
1660{
1661 MessageBeep(MB_OK);
1662}
1663/*
1664 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1665 */
1666 void
1667gui_mch_invert_rectangle(
1668 int r,
1669 int c,
1670 int nr,
1671 int nc)
1672{
1673 RECT rc;
1674
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001675#if defined(FEAT_DIRECTX)
1676 if (IS_ENABLE_DIRECTX())
1677 DWriteContext_Flush(s_dwc);
1678#endif
1679
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680 /*
1681 * Note: InvertRect() excludes right and bottom of rectangle.
1682 */
1683 rc.left = FILL_X(c);
1684 rc.top = FILL_Y(r);
1685 rc.right = rc.left + nc * gui.char_width;
1686 rc.bottom = rc.top + nr * gui.char_height;
1687 InvertRect(s_hdc, &rc);
1688}
1689
1690/*
1691 * Iconify the GUI window.
1692 */
1693 void
1694gui_mch_iconify(void)
1695{
1696 ShowWindow(s_hwnd, SW_MINIMIZE);
1697}
1698
1699/*
1700 * Draw a cursor without focus.
1701 */
1702 void
1703gui_mch_draw_hollow_cursor(guicolor_T color)
1704{
1705 HBRUSH hbr;
1706 RECT rc;
1707
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001708#if defined(FEAT_DIRECTX)
1709 if (IS_ENABLE_DIRECTX())
1710 DWriteContext_Flush(s_dwc);
1711#endif
1712
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001713 /*
1714 * Note: FrameRect() excludes right and bottom of rectangle.
1715 */
1716 rc.left = FILL_X(gui.col);
1717 rc.top = FILL_Y(gui.row);
1718 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001719 if (mb_lefthalve(gui.row, gui.col))
1720 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001721 rc.bottom = rc.top + gui.char_height;
1722 hbr = CreateSolidBrush(color);
1723 FrameRect(s_hdc, &rc, hbr);
1724 DeleteBrush(hbr);
1725}
1726/*
1727 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1728 * color "color".
1729 */
1730 void
1731gui_mch_draw_part_cursor(
1732 int w,
1733 int h,
1734 guicolor_T color)
1735{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001736 RECT rc;
1737
1738 /*
1739 * Note: FillRect() excludes right and bottom of rectangle.
1740 */
1741 rc.left =
1742#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001743 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001744 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1745#endif
1746 FILL_X(gui.col);
1747 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1748 rc.right = rc.left + w;
1749 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001750
Bram Moolenaar92467d32017-12-05 13:22:16 +01001751 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001752}
1753
1754
1755/*
1756 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1757 * dead key's nominal character and re-post the original message.
1758 */
1759 static void
1760outputDeadKey_rePost(MSG originalMsg)
1761{
1762 static MSG deadCharExpel;
1763
1764 if (!dead_key)
1765 return;
1766
1767 dead_key = 0;
1768
Bram Moolenaar734a8672019-12-02 22:49:38 +01001769 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001770 deadCharExpel.message = originalMsg.message;
1771 deadCharExpel.hwnd = originalMsg.hwnd;
1772 deadCharExpel.wParam = VK_SPACE;
1773
1774 MyTranslateMessage(&deadCharExpel);
1775
Bram Moolenaar734a8672019-12-02 22:49:38 +01001776 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1778 originalMsg.lParam);
1779}
1780
1781
1782/*
1783 * Process a single Windows message.
1784 * If one is not available we hang until one is.
1785 */
1786 static void
1787process_message(void)
1788{
1789 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001790 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001791 char_u string[40];
1792 int i;
1793 int modifiers = 0;
1794 int key;
1795#ifdef FEAT_MENU
1796 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1797#endif
1798
1799 pGetMessage(&msg, NULL, 0, 0);
1800
1801#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001802 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001803 if (msg.message == WM_OLE)
1804 {
1805 char_u *str = (char_u *)msg.lParam;
1806 if (str == NULL || *str == NUL)
1807 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001808 // Message can't be ours, forward it. Fixes problem with Ultramon
1809 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001810 pDispatchMessage(&msg);
1811 }
1812 else
1813 {
1814 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001815 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001816 }
1817 return;
1818 }
1819#endif
1820
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001822 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1824 {
1825 HandleMouseHide(msg.message, msg.lParam);
1826 return;
1827 }
1828#endif
1829
1830 /*
1831 * Check if it's a special key that we recognise. If not, call
1832 * TranslateMessage().
1833 */
1834 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1835 {
1836 vk = (int) msg.wParam;
1837
1838 /*
1839 * Handle dead keys in special conditions in other cases we let Windows
1840 * handle them and do not interfere.
1841 *
1842 * The dead_key flag must be reset on several occasions:
1843 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1844 * consumed at that point (This is when we let Windows combine the
1845 * dead character on its own)
1846 *
1847 * - Before doing something special such as regenerating keypresses to
1848 * expel the dead character as this could trigger an infinite loop if
1849 * for some reason MyTranslateMessage() do not trigger a call
1850 * immediately to _OnChar() (or _OnSysChar()).
1851 */
1852 if (dead_key)
1853 {
1854 /*
1855 * If a dead key was pressed and the user presses VK_SPACE,
1856 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1857 * with the dead char now, so do nothing special and let Windows
1858 * handle it.
1859 *
1860 * Note that VK_SPACE combines with the dead_key's character and
1861 * only one WM_CHAR will be generated by TranslateMessage(), in
1862 * the two other cases two WM_CHAR will be generated: the dead
1863 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1864 * user expects.
1865 */
1866 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1867 {
1868 dead_key = 0;
1869 MyTranslateMessage(&msg);
1870 return;
1871 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001872 // In modes where we are not typing, dead keys should behave
1873 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1875 {
1876 outputDeadKey_rePost(msg);
1877 return;
1878 }
1879 }
1880
Bram Moolenaar734a8672019-12-02 22:49:38 +01001881 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001882 if (vk == VK_CANCEL)
1883 {
1884 trash_input_buf();
1885 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001886 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001887 string[0] = Ctrl_C;
1888 add_to_input_buf(string, 1);
1889 }
1890
1891 for (i = 0; special_keys[i].key_sym != 0; i++)
1892 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894 if (special_keys[i].key_sym == vk
1895 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1896 {
1897 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001898 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001899 * is a key that would normally trigger the dead key nominal
1900 * character output (such as a NUMPAD printable character or
1901 * the TAB key, etc...).
1902 */
1903 if (dead_key && (special_keys[i].vim_code0 == 'K'
1904 || vk == VK_TAB || vk == CAR))
1905 {
1906 outputDeadKey_rePost(msg);
1907 return;
1908 }
1909
1910#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001911 // Check for <F10>: Windows selects the menu. When <F10> is
1912 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001913 if (vk == VK_F10
1914 && gui.menu_is_active
1915 && check_map(k10, State, FALSE, TRUE, FALSE,
1916 NULL, NULL) == NULL)
1917 break;
1918#endif
1919 if (GetKeyState(VK_SHIFT) & 0x8000)
1920 modifiers |= MOD_MASK_SHIFT;
1921 /*
1922 * Don't use caps-lock as shift, because these are special keys
1923 * being considered here, and we only want letters to get
1924 * shifted -- webb
1925 */
1926 /*
1927 if (GetKeyState(VK_CAPITAL) & 0x0001)
1928 modifiers ^= MOD_MASK_SHIFT;
1929 */
1930 if (GetKeyState(VK_CONTROL) & 0x8000)
1931 modifiers |= MOD_MASK_CTRL;
1932 if (GetKeyState(VK_MENU) & 0x8000)
1933 modifiers |= MOD_MASK_ALT;
1934
1935 if (special_keys[i].vim_code1 == NUL)
1936 key = special_keys[i].vim_code0;
1937 else
1938 key = TO_SPECIAL(special_keys[i].vim_code0,
1939 special_keys[i].vim_code1);
1940 key = simplify_key(key, &modifiers);
1941 if (key == CSI)
1942 key = K_CSI;
1943
1944 if (modifiers)
1945 {
1946 string[0] = CSI;
1947 string[1] = KS_MODIFIER;
1948 string[2] = modifiers;
1949 add_to_input_buf(string, 3);
1950 }
1951
1952 if (IS_SPECIAL(key))
1953 {
1954 string[0] = CSI;
1955 string[1] = K_SECOND(key);
1956 string[2] = K_THIRD(key);
1957 add_to_input_buf(string, 3);
1958 }
1959 else
1960 {
1961 int len;
1962
Bram Moolenaar734a8672019-12-02 22:49:38 +01001963 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001964 len = char_to_string(key, string, 40, FALSE);
1965 add_to_input_buf(string, len);
1966 }
1967 break;
1968 }
1969 }
1970 if (special_keys[i].key_sym == 0)
1971 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001972 // Some keys need C-S- where they should only need C-.
1973 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1974 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001975 if (vk != 0xff
1976 && (GetKeyState(VK_CONTROL) & 0x8000)
1977 && !(GetKeyState(VK_SHIFT) & 0x8000)
1978 && !(GetKeyState(VK_MENU) & 0x8000))
1979 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001980 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001981 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1982 {
1983 string[0] = Ctrl_HAT;
1984 add_to_input_buf(string, 1);
1985 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001986 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
1987 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001988 {
1989 string[0] = Ctrl__;
1990 add_to_input_buf(string, 1);
1991 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001992 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001993 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1994 {
1995 string[0] = Ctrl_AT;
1996 add_to_input_buf(string, 1);
1997 }
1998 else
1999 MyTranslateMessage(&msg);
2000 }
2001 else
2002 MyTranslateMessage(&msg);
2003 }
2004 }
2005#ifdef FEAT_MBYTE_IME
2006 else if (msg.message == WM_IME_NOTIFY)
2007 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2008 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002009 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002010 MyTranslateMessage(&msg);
2011#endif
2012#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002013// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002014 else if (msg.message == WM_IME_STARTCOMPOSITION)
2015 {
2016 POINT point;
2017
2018 global_ime_set_font(&norm_logfont);
2019 point.x = FILL_X(gui.col);
2020 point.y = FILL_Y(gui.row);
2021 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2022 global_ime_set_position(&point);
2023 }
2024#endif
2025
2026#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002027 // Check for <F10>: Default effect is to select the menu. When <F10> is
2028 // mapped we need to stop it here to avoid strange effects (e.g., for the
2029 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2031 NULL, NULL) == NULL)
2032#endif
2033 pDispatchMessage(&msg);
2034}
2035
2036/*
2037 * Catch up with any queued events. This may put keyboard input into the
2038 * input buffer, call resize call-backs, trigger timers etc. If there is
2039 * nothing in the event queue (& no timers pending), then we return
2040 * immediately.
2041 */
2042 void
2043gui_mch_update(void)
2044{
2045 MSG msg;
2046
2047 if (!s_busy_processing)
2048 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2049 && !vim_is_input_buf_full())
2050 process_message();
2051}
2052
Bram Moolenaar4231da42016-06-02 14:30:04 +02002053 static void
2054remove_any_timer(void)
2055{
2056 MSG msg;
2057
2058 if (s_wait_timer != 0 && !s_timed_out)
2059 {
2060 KillTimer(NULL, s_wait_timer);
2061
Bram Moolenaar734a8672019-12-02 22:49:38 +01002062 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002063 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2064 ;
2065 s_wait_timer = 0;
2066 }
2067}
2068
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069/*
2070 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2071 * from the keyboard.
2072 * wtime == -1 Wait forever.
2073 * wtime == 0 This should never happen.
2074 * wtime > 0 Wait wtime milliseconds for a character.
2075 * Returns OK if a character was found to be available within the given time,
2076 * or FAIL otherwise.
2077 */
2078 int
2079gui_mch_wait_for_chars(int wtime)
2080{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081 int focus;
2082
2083 s_timed_out = FALSE;
2084
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002085 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002086 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002087 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088 if (s_busy_processing)
2089 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002090
2091 // When called with "wtime" zero, just want one msec.
2092 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 (TIMERPROC)_OnTimer);
2094 }
2095
2096 allow_scrollbar = TRUE;
2097
2098 focus = gui.in_focus;
2099 while (!s_timed_out)
2100 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002101 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002102 if (gui.in_focus != focus)
2103 {
2104 if (gui.in_focus)
2105 gui_mch_start_blink();
2106 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002107 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108 focus = gui.in_focus;
2109 }
2110
2111 if (s_need_activate)
2112 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 s_need_activate = FALSE;
2115 }
2116
Bram Moolenaar4231da42016-06-02 14:30:04 +02002117#ifdef FEAT_TIMERS
2118 did_add_timer = FALSE;
2119#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002121 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002122 for (;;)
2123 {
2124 MSG msg;
2125
2126 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002127# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002128 if (did_add_timer)
2129 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002130# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002131 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2132 {
2133 process_message();
2134 break;
2135 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002137 // TODO: The 10 msec is a compromise between laggy response
2138 // and consuming more CPU time. Better would be to handle
2139 // channel messages when they arrive.
2140 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002141 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002142 break;
2143 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002144#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002145 // Don't use gui_mch_update() because then we will spin-lock until a
2146 // char arrives, instead we use GetMessage() to hang until an
2147 // event arrives. No need to check for input_buf_full because we are
2148 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002150#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002151
2152 if (input_available())
2153 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002154 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155 allow_scrollbar = FALSE;
2156
Bram Moolenaar89c00032019-09-04 13:53:21 +02002157 // Clear pending mouse button, the release event may have been
2158 // taken by the dialog window. But don't do this when getting
2159 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002160 if (!s_getting_focus)
2161 s_button_pending = -1;
2162
2163 return OK;
2164 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002165
2166#ifdef FEAT_TIMERS
2167 if (did_add_timer)
2168 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002169 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002170 remove_any_timer();
2171 break;
2172 }
2173#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002174 }
2175 allow_scrollbar = FALSE;
2176 return FAIL;
2177}
2178
2179/*
2180 * Clear a rectangular region of the screen from text pos (row1, col1) to
2181 * (row2, col2) inclusive.
2182 */
2183 void
2184gui_mch_clear_block(
2185 int row1,
2186 int col1,
2187 int row2,
2188 int col2)
2189{
2190 RECT rc;
2191
2192 /*
2193 * Clear one extra pixel at the far right, for when bold characters have
2194 * spilled over to the window border.
2195 * Note: FillRect() excludes right and bottom of rectangle.
2196 */
2197 rc.left = FILL_X(col1);
2198 rc.top = FILL_Y(row1);
2199 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2200 rc.bottom = FILL_Y(row2 + 1);
2201 clear_rect(&rc);
2202}
2203
2204/*
2205 * Clear the whole text window.
2206 */
2207 void
2208gui_mch_clear_all(void)
2209{
2210 RECT rc;
2211
2212 rc.left = 0;
2213 rc.top = 0;
2214 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2215 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2216 clear_rect(&rc);
2217}
2218/*
2219 * Menu stuff.
2220 */
2221
2222 void
2223gui_mch_enable_menu(int flag)
2224{
2225#ifdef FEAT_MENU
2226 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2227#endif
2228}
2229
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230 void
2231gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002232 int x UNUSED,
2233 int y UNUSED,
2234 int w UNUSED,
2235 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002236{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002237 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002238}
2239
2240#if defined(FEAT_MENU) || defined(PROTO)
2241/*
2242 * Make menu item hidden or not hidden
2243 */
2244 void
2245gui_mch_menu_hidden(
2246 vimmenu_T *menu,
2247 int hidden)
2248{
2249 /*
2250 * This doesn't do what we want. Hmm, just grey the menu items for now.
2251 */
2252 /*
2253 if (hidden)
2254 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2255 else
2256 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2257 */
2258 gui_mch_menu_grey(menu, hidden);
2259}
2260
2261/*
2262 * This is called after setting all the menus to grey/hidden or not.
2263 */
2264 void
2265gui_mch_draw_menubar(void)
2266{
2267 DrawMenuBar(s_hwnd);
2268}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002269#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002270
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271/*
2272 * Return the RGB value of a pixel as a long.
2273 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002274 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275gui_mch_get_rgb(guicolor_T pixel)
2276{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002277 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2278 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002279}
2280
2281#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002282/*
2283 * Convert pixels in X to dialog units
2284 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002285 static WORD
2286PixelToDialogX(int numPixels)
2287{
2288 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2289}
2290
Bram Moolenaar734a8672019-12-02 22:49:38 +01002291/*
2292 * Convert pixels in Y to dialog units
2293 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002294 static WORD
2295PixelToDialogY(int numPixels)
2296{
2297 return (WORD)((numPixels * 8) / s_dlgfntheight);
2298}
2299
Bram Moolenaar734a8672019-12-02 22:49:38 +01002300/*
2301 * Return the width in pixels of the given text in the given DC.
2302 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 static int
2304GetTextWidth(HDC hdc, char_u *str, int len)
2305{
2306 SIZE size;
2307
2308 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2309 return size.cx;
2310}
2311
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312/*
2313 * Return the width in pixels of the given text in the given DC, taking care
2314 * of 'encoding' to active codepage conversion.
2315 */
2316 static int
2317GetTextWidthEnc(HDC hdc, char_u *str, int len)
2318{
2319 SIZE size;
2320 WCHAR *wstr;
2321 int n;
2322 int wlen = len;
2323
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002324 wstr = enc_to_utf16(str, &wlen);
2325 if (wstr == NULL)
2326 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002327
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002328 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2329 vim_free(wstr);
2330 if (n)
2331 return size.cx;
2332 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002333}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002334
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002335static void get_work_area(RECT *spi_rect);
2336
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337/*
2338 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002339 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2340 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341 */
2342 static BOOL
2343CenterWindow(
2344 HWND hwndChild,
2345 HWND hwndParent)
2346{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002347 HMONITOR mon;
2348 MONITORINFO moninfo;
2349 RECT rChild, rParent, rScreen;
2350 int wChild, hChild, wParent, hParent;
2351 int xNew, yNew;
2352 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002353
2354 GetWindowRect(hwndChild, &rChild);
2355 wChild = rChild.right - rChild.left;
2356 hChild = rChild.bottom - rChild.top;
2357
Bram Moolenaar734a8672019-12-02 22:49:38 +01002358 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002360 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361 else
2362 GetWindowRect(hwndParent, &rParent);
2363 wParent = rParent.right - rParent.left;
2364 hParent = rParent.bottom - rParent.top;
2365
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002366 moninfo.cbSize = sizeof(MONITORINFO);
2367 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2368 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002372 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002373 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 hdc = GetDC(hwndChild);
2375 rScreen.left = 0;
2376 rScreen.top = 0;
2377 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2378 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2379 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002380 }
2381
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002382 xNew = rParent.left + ((wParent - wChild) / 2);
2383 if (xNew < rScreen.left)
2384 xNew = rScreen.left;
2385 else if ((xNew + wChild) > rScreen.right)
2386 xNew = rScreen.right - wChild;
2387
2388 yNew = rParent.top + ((hParent - hChild) / 2);
2389 if (yNew < rScreen.top)
2390 yNew = rScreen.top;
2391 else if ((yNew + hChild) > rScreen.bottom)
2392 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393
2394 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2395 SWP_NOSIZE | SWP_NOZORDER);
2396}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002397#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399#if defined(FEAT_TOOLBAR) || defined(PROTO)
2400 void
2401gui_mch_show_toolbar(int showit)
2402{
2403 if (s_toolbarhwnd == NULL)
2404 return;
2405
2406 if (showit)
2407 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002408# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002409 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002410# define TB_SETUNICODEFORMAT 0x2005
2411# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002412 // Enable unicode support
2413 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2414 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 ShowWindow(s_toolbarhwnd, SW_SHOW);
2416 }
2417 else
2418 ShowWindow(s_toolbarhwnd, SW_HIDE);
2419}
2420
Bram Moolenaar734a8672019-12-02 22:49:38 +01002421// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002422# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423
2424#endif
2425
2426#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2427 static void
2428add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2429{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002430 WCHAR *wn;
2431 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002433 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002435 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002437 infow.cbSize = sizeof(infow);
2438 infow.fMask = MIIM_TYPE | MIIM_ID;
2439 infow.wID = item_id;
2440 infow.fType = MFT_STRING;
2441 infow.dwTypeData = wn;
2442 infow.cch = (UINT)wcslen(wn);
2443 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2444 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445}
2446
2447 static void
2448show_tabline_popup_menu(void)
2449{
2450 HMENU tab_pmenu;
2451 long rval;
2452 POINT pt;
2453
Bram Moolenaar734a8672019-12-02 22:49:38 +01002454 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 if (hold_gui_events
2456# ifdef FEAT_CMDWIN
2457 || cmdwin_type != 0
2458# endif
2459 )
2460 return;
2461
2462 tab_pmenu = CreatePopupMenu();
2463 if (tab_pmenu == NULL)
2464 return;
2465
2466 if (first_tabpage->tp_next != NULL)
2467 add_tabline_popup_menu_entry(tab_pmenu,
2468 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2469 add_tabline_popup_menu_entry(tab_pmenu,
2470 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2471 add_tabline_popup_menu_entry(tab_pmenu,
2472 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2473
2474 GetCursorPos(&pt);
2475 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2476 NULL);
2477
2478 DestroyMenu(tab_pmenu);
2479
Bram Moolenaar734a8672019-12-02 22:49:38 +01002480 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002481 if (rval > 0)
2482 {
2483 TCHITTESTINFO htinfo;
2484 int idx;
2485
2486 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2487 return;
2488
2489 htinfo.pt.x = pt.x;
2490 htinfo.pt.y = pt.y;
2491 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2492 if (idx == -1)
2493 idx = 0;
2494 else
2495 idx += 1;
2496
2497 send_tabline_menu_event(idx, (int)rval);
2498 }
2499}
2500
2501/*
2502 * Show or hide the tabline.
2503 */
2504 void
2505gui_mch_show_tabline(int showit)
2506{
2507 if (s_tabhwnd == NULL)
2508 return;
2509
2510 if (!showit != !showing_tabline)
2511 {
2512 if (showit)
2513 ShowWindow(s_tabhwnd, SW_SHOW);
2514 else
2515 ShowWindow(s_tabhwnd, SW_HIDE);
2516 showing_tabline = showit;
2517 }
2518}
2519
2520/*
2521 * Return TRUE when tabline is displayed.
2522 */
2523 int
2524gui_mch_showing_tabline(void)
2525{
2526 return s_tabhwnd != NULL && showing_tabline;
2527}
2528
2529/*
2530 * Update the labels of the tabline.
2531 */
2532 void
2533gui_mch_update_tabline(void)
2534{
2535 tabpage_T *tp;
2536 TCITEM tie;
2537 int nr = 0;
2538 int curtabidx = 0;
2539 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002540 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541
2542 if (s_tabhwnd == NULL)
2543 return;
2544
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002545# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002546 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002547# define CCM_SETUNICODEFORMAT 0x2005
2548# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002549 // Enable unicode support
2550 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002551
2552 tie.mask = TCIF_TEXT;
2553 tie.iImage = -1;
2554
Bram Moolenaar734a8672019-12-02 22:49:38 +01002555 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002556 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2557
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2560 {
2561 if (tp == curtab)
2562 curtabidx = nr;
2563
2564 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2565 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002566 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002567 tie.pszText = "-Empty-";
2568 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2569 tabadded = 1;
2570 }
2571
2572 get_tabline_label(tp, FALSE);
2573 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002574
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002575 wstr = enc_to_utf16(NameBuff, NULL);
2576 if (wstr != NULL)
2577 {
2578 TCITEMW tiw;
2579
2580 tiw.mask = TCIF_TEXT;
2581 tiw.iImage = -1;
2582 tiw.pszText = wstr;
2583 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2584 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002585 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586 }
2587
Bram Moolenaar734a8672019-12-02 22:49:38 +01002588 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002589 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2590 TabCtrl_DeleteItem(s_tabhwnd, nr);
2591
2592 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2593 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2594
Bram Moolenaar734a8672019-12-02 22:49:38 +01002595 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002596 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2597 RedrawWindow(s_tabhwnd, NULL, NULL,
2598 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2599
2600 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2601 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2602}
2603
2604/*
2605 * Set the current tab to "nr". First tab is 1.
2606 */
2607 void
2608gui_mch_set_curtab(int nr)
2609{
2610 if (s_tabhwnd == NULL)
2611 return;
2612
2613 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2614 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2615}
2616
2617#endif
2618
2619/*
2620 * ":simalt" command.
2621 */
2622 void
2623ex_simalt(exarg_T *eap)
2624{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002625 char_u *keys = eap->arg;
2626 int fill_typebuf = FALSE;
2627 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628
2629 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2630 while (*keys)
2631 {
2632 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002633 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002634 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2635 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002636 fill_typebuf = TRUE;
2637 }
2638 if (fill_typebuf)
2639 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002640 // Put a NOP in the typeahead buffer so that the message will get
2641 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002642 key_name[0] = K_SPECIAL;
2643 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002644 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002645 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002646#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002647 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002648#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002649 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002650 }
2651}
2652
2653/*
2654 * Create the find & replace dialogs.
2655 * You can't have both at once: ":find" when replace is showing, destroys
2656 * the replace dialog first, and the other way around.
2657 */
2658#ifdef MSWIN_FIND_REPLACE
2659 static void
2660initialise_findrep(char_u *initial_string)
2661{
2662 int wword = FALSE;
2663 int mcase = !p_ic;
2664 char_u *entry_text;
2665
Bram Moolenaar734a8672019-12-02 22:49:38 +01002666 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002667 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2668
2669 s_findrep_struct.hwndOwner = s_hwnd;
2670 s_findrep_struct.Flags = FR_DOWN;
2671 if (mcase)
2672 s_findrep_struct.Flags |= FR_MATCHCASE;
2673 if (wword)
2674 s_findrep_struct.Flags |= FR_WHOLEWORD;
2675 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002676 {
2677 WCHAR *p = enc_to_utf16(entry_text, NULL);
2678 if (p != NULL)
2679 {
2680 int len = s_findrep_struct.wFindWhatLen - 1;
2681
2682 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2683 s_findrep_struct.lpstrFindWhat[len] = NUL;
2684 vim_free(p);
2685 }
2686 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002687 vim_free(entry_text);
2688}
2689#endif
2690
2691 static void
2692set_window_title(HWND hwnd, char *title)
2693{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002694 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002695 {
2696 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697
Bram Moolenaar734a8672019-12-02 22:49:38 +01002698 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2700 if (wbuf != NULL)
2701 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002702 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002703 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704 }
2705 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002706 else
2707 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708}
2709
2710 void
2711gui_mch_find_dialog(exarg_T *eap)
2712{
2713#ifdef MSWIN_FIND_REPLACE
2714 if (s_findrep_msg != 0)
2715 {
2716 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2717 DestroyWindow(s_findrep_hwnd);
2718
2719 if (!IsWindow(s_findrep_hwnd))
2720 {
2721 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002722 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 }
2724
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002725 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002726 (void)SetFocus(s_findrep_hwnd);
2727
2728 s_findrep_is_find = TRUE;
2729 }
2730#endif
2731}
2732
2733
2734 void
2735gui_mch_replace_dialog(exarg_T *eap)
2736{
2737#ifdef MSWIN_FIND_REPLACE
2738 if (s_findrep_msg != 0)
2739 {
2740 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2741 DestroyWindow(s_findrep_hwnd);
2742
2743 if (!IsWindow(s_findrep_hwnd))
2744 {
2745 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002746 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 }
2748
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002749 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002750 (void)SetFocus(s_findrep_hwnd);
2751
2752 s_findrep_is_find = FALSE;
2753 }
2754#endif
2755}
2756
2757
2758/*
2759 * Set visibility of the pointer.
2760 */
2761 void
2762gui_mch_mousehide(int hide)
2763{
2764 if (hide != gui.pointer_hidden)
2765 {
2766 ShowCursor(!hide);
2767 gui.pointer_hidden = hide;
2768 }
2769}
2770
2771#ifdef FEAT_MENU
2772 static void
2773gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2774{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002775 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002776 gui_mch_mousehide(FALSE);
2777
2778 (void)TrackPopupMenu(
2779 (HMENU)menu->submenu_id,
2780 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2781 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002782 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783 s_hwnd,
2784 NULL);
2785 /*
2786 * NOTE: The pop-up menu can eat the mouse up event.
2787 * We deal with this in normal.c.
2788 */
2789}
2790#endif
2791
2792/*
2793 * Got a message when the system will go down.
2794 */
2795 static void
2796_OnEndSession(void)
2797{
2798 getout_preserve_modified(1);
2799}
2800
2801/*
2802 * Get this message when the user clicks on the cross in the top right corner
2803 * of a Windows95 window.
2804 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002806_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002807{
2808 gui_shell_closed();
2809}
2810
2811/*
2812 * Get a message when the window is being destroyed.
2813 */
2814 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002815_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816{
2817 if (!destroying)
2818 _OnClose(hwnd);
2819}
2820
2821 static void
2822_OnPaint(
2823 HWND hwnd)
2824{
2825 if (!IsMinimized(hwnd))
2826 {
2827 PAINTSTRUCT ps;
2828
Bram Moolenaar734a8672019-12-02 22:49:38 +01002829 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002830 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831
Bram Moolenaar734a8672019-12-02 22:49:38 +01002832 // prevent multi-byte characters from misprinting on an invalid
2833 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002834 if (has_mbyte)
2835 {
2836 RECT rect;
2837
2838 GetClientRect(hwnd, &rect);
2839 ps.rcPaint.left = rect.left;
2840 ps.rcPaint.right = rect.right;
2841 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842
2843 if (!IsRectEmpty(&ps.rcPaint))
2844 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002845 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2846 ps.rcPaint.right - ps.rcPaint.left + 1,
2847 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2848 }
2849
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850 EndPaint(hwnd, &ps);
2851 }
2852}
2853
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002854 static void
2855_OnSize(
2856 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002857 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 int cx,
2859 int cy)
2860{
2861 if (!IsMinimized(hwnd))
2862 {
2863 gui_resize_shell(cx, cy);
2864
2865#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002866 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867 gui_mswin_get_menu_height(TRUE);
2868#endif
2869 }
2870}
2871
2872 static void
2873_OnSetFocus(
2874 HWND hwnd,
2875 HWND hwndOldFocus)
2876{
2877 gui_focus_change(TRUE);
2878 s_getting_focus = TRUE;
2879 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2880}
2881
2882 static void
2883_OnKillFocus(
2884 HWND hwnd,
2885 HWND hwndNewFocus)
2886{
2887 gui_focus_change(FALSE);
2888 s_getting_focus = FALSE;
2889 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2890}
2891
2892/*
2893 * Get a message when the user switches back to vim
2894 */
2895 static LRESULT
2896_OnActivateApp(
2897 HWND hwnd,
2898 BOOL fActivate,
2899 DWORD dwThreadId)
2900{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002901 // we call gui_focus_change() in _OnSetFocus()
2902 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2904}
2905
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002906 void
2907gui_mch_destroy_scrollbar(scrollbar_T *sb)
2908{
2909 DestroyWindow(sb->id);
2910}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002911
2912/*
2913 * Get current mouse coordinates in text window.
2914 */
2915 void
2916gui_mch_getmouse(int *x, int *y)
2917{
2918 RECT rct;
2919 POINT mp;
2920
2921 (void)GetWindowRect(s_textArea, &rct);
2922 (void)GetCursorPos((LPPOINT)&mp);
2923 *x = (int)(mp.x - rct.left);
2924 *y = (int)(mp.y - rct.top);
2925}
2926
2927/*
2928 * Move mouse pointer to character at (x, y).
2929 */
2930 void
2931gui_mch_setmouse(int x, int y)
2932{
2933 RECT rct;
2934
2935 (void)GetWindowRect(s_textArea, &rct);
2936 (void)SetCursorPos(x + gui.border_offset + rct.left,
2937 y + gui.border_offset + rct.top);
2938}
2939
2940 static void
2941gui_mswin_get_valid_dimensions(
2942 int w,
2943 int h,
2944 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02002945 int *valid_h,
2946 int *cols,
2947 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002948{
2949 int base_width, base_height;
2950
2951 base_width = gui_get_base_width()
2952 + (GetSystemMetrics(SM_CXFRAME) +
2953 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2954 base_height = gui_get_base_height()
2955 + (GetSystemMetrics(SM_CYFRAME) +
2956 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2957 + GetSystemMetrics(SM_CYCAPTION)
2958#ifdef FEAT_MENU
2959 + gui_mswin_get_menu_height(FALSE)
2960#endif
2961 ;
K.Takata4f2417f2021-06-05 16:25:32 +02002962 *cols = (w - base_width) / gui.char_width;
2963 *rows = (h - base_height) / gui.char_height;
2964 *valid_w = base_width + *cols * gui.char_width;
2965 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002966}
2967
2968 void
2969gui_mch_flash(int msec)
2970{
2971 RECT rc;
2972
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002973#if defined(FEAT_DIRECTX)
2974 if (IS_ENABLE_DIRECTX())
2975 DWriteContext_Flush(s_dwc);
2976#endif
2977
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002978 /*
2979 * Note: InvertRect() excludes right and bottom of rectangle.
2980 */
2981 rc.left = 0;
2982 rc.top = 0;
2983 rc.right = gui.num_cols * gui.char_width;
2984 rc.bottom = gui.num_rows * gui.char_height;
2985 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002986 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987
Bram Moolenaar734a8672019-12-02 22:49:38 +01002988 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002989
2990 InvertRect(s_hdc, &rc);
2991}
2992
2993/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01002994 * Check if the specified point is on-screen. (multi-monitor aware)
2995 */
2996 static BOOL
2997is_point_onscreen(int x, int y)
2998{
2999 POINT pt = {x, y};
3000
3001 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3002}
3003
3004/*
3005 * Check if the whole area of the specified window is on-screen.
3006 *
3007 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3008 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3009 * only works when the whole window is on-screen.
3010 */
3011 static BOOL
3012is_window_onscreen(HWND hwnd)
3013{
3014 RECT rc;
3015
3016 GetWindowRect(hwnd, &rc);
3017
3018 if (!is_point_onscreen(rc.left, rc.top))
3019 return FALSE;
3020 if (!is_point_onscreen(rc.left, rc.bottom))
3021 return FALSE;
3022 if (!is_point_onscreen(rc.right, rc.top))
3023 return FALSE;
3024 if (!is_point_onscreen(rc.right, rc.bottom))
3025 return FALSE;
3026 return TRUE;
3027}
3028
3029/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003030 * Return flags used for scrolling.
3031 * The SW_INVALIDATE is required when part of the window is covered or
3032 * off-screen. Refer to MS KB Q75236.
3033 */
3034 static int
3035get_scroll_flags(void)
3036{
3037 HWND hwnd;
3038 RECT rcVim, rcOther, rcDest;
3039
Bram Moolenaar185577e2020-10-29 20:08:21 +01003040 // Check if the window is (partly) off-screen.
3041 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003042 return SW_INVALIDATE;
3043
Bram Moolenaar734a8672019-12-02 22:49:38 +01003044 // Check if there is an window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003045 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003046 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3047 if (IsWindowVisible(hwnd))
3048 {
3049 GetWindowRect(hwnd, &rcOther);
3050 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3051 return SW_INVALIDATE;
3052 }
3053 return 0;
3054}
3055
3056/*
3057 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3058 * may not be scrolled out properly.
3059 * For gVim, when _OnScroll() is repeated, the character at the
3060 * previous cursor position may be left drawn after scroll.
3061 * The problem can be avoided by calling GetPixel() to get a pixel in
3062 * the region before ScrollWindowEx().
3063 */
3064 static void
3065intel_gpu_workaround(void)
3066{
3067 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3068}
3069
3070/*
3071 * Delete the given number of lines from the given row, scrolling up any
3072 * text further down within the scroll region.
3073 */
3074 void
3075gui_mch_delete_lines(
3076 int row,
3077 int num_lines)
3078{
3079 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003080
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003081 rc.left = FILL_X(gui.scroll_region_left);
3082 rc.right = FILL_X(gui.scroll_region_right + 1);
3083 rc.top = FILL_Y(row);
3084 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3085
Bram Moolenaar92467d32017-12-05 13:22:16 +01003086#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003087 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003088 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003089 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003090 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003091 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003092#endif
3093 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003094#if defined(FEAT_DIRECTX)
3095 if (IS_ENABLE_DIRECTX())
3096 DWriteContext_Flush(s_dwc);
3097#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003098 intel_gpu_workaround();
3099 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003100 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003101 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003102 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003103
Bram Moolenaar734a8672019-12-02 22:49:38 +01003104 // This seems to be required to avoid the cursor disappearing when
3105 // scrolling such that the cursor ends up in the top-left character on
3106 // the screen... But why? (Webb)
3107 // It's probably fixed by disabling drawing the cursor while scrolling.
3108 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003109
3110 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3111 gui.scroll_region_left,
3112 gui.scroll_region_bot, gui.scroll_region_right);
3113}
3114
3115/*
3116 * Insert the given number of lines before the given row, scrolling down any
3117 * following text within the scroll region.
3118 */
3119 void
3120gui_mch_insert_lines(
3121 int row,
3122 int num_lines)
3123{
3124 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003125
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 rc.left = FILL_X(gui.scroll_region_left);
3127 rc.right = FILL_X(gui.scroll_region_right + 1);
3128 rc.top = FILL_Y(row);
3129 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003130
3131#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003132 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003133 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003134 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003135 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003136 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003137#endif
3138 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003139#if defined(FEAT_DIRECTX)
3140 if (IS_ENABLE_DIRECTX())
3141 DWriteContext_Flush(s_dwc);
3142#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003143 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003144 // The SW_INVALIDATE is required when part of the window is covered or
3145 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003146 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003147 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003148 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003149 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003150
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151 gui_clear_block(row, gui.scroll_region_left,
3152 row + num_lines - 1, gui.scroll_region_right);
3153}
3154
3155
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003157gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003158{
3159#if defined(FEAT_DIRECTX)
3160 DWriteContext_Close(s_dwc);
3161 DWrite_Final();
3162 s_dwc = NULL;
3163#endif
3164
3165 ReleaseDC(s_textArea, s_hdc);
3166 DeleteObject(s_brush);
3167
3168#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003169 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3171#endif
3172
Bram Moolenaar734a8672019-12-02 22:49:38 +01003173 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003174 if (s_hwnd != NULL)
3175 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003176 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 DestroyWindow(s_hwnd);
3178 }
3179
3180#ifdef GLOBAL_IME
3181 global_ime_end();
3182#endif
3183}
3184
3185 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003186logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187{
3188 char *p;
3189 char *res;
3190 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003191 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003192 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003193 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003195 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3196 if (font_name == NULL)
3197 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003199 quality_name = quality_id2name((int)lf.lfQuality);
3200
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003201 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003202 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003203 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 if (res != NULL)
3205 {
3206 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003207 // make a normal font string out of the lf thing:
3208 points = pixels_to_points(
3209 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3210 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3211 sprintf((char *)p, "%s:h%d", font_name, points);
3212 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003213 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 while (*p)
3215 {
3216 if (*p == ' ')
3217 *p = '_';
3218 ++p;
3219 }
3220 if (lf.lfItalic)
3221 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003222 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003223 STRCAT(p, ":b");
3224 if (lf.lfUnderline)
3225 STRCAT(p, ":u");
3226 if (lf.lfStrikeOut)
3227 STRCAT(p, ":s");
3228 if (charset_name != NULL)
3229 {
3230 STRCAT(p, ":c");
3231 STRCAT(p, charset_name);
3232 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003233 if (quality_name != NULL)
3234 {
3235 STRCAT(p, ":q");
3236 STRCAT(p, quality_name);
3237 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 }
3239
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003240 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003241 return (char_u *)res;
3242}
3243
3244
3245#ifdef FEAT_MBYTE_IME
3246/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003247 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248 * 'guifont'
3249 */
3250 static void
3251update_im_font(void)
3252{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003253 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003254
3255 if (p_guifontwide != NULL && *p_guifontwide != NUL
3256 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003257 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003258 norm_logfont = lf_wide;
3259 else
3260 norm_logfont = sub_logfont;
3261 im_set_font(&norm_logfont);
3262}
3263#endif
3264
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003265/*
3266 * Handler of gui.wide_font (p_guifontwide) changed notification.
3267 */
3268 void
3269gui_mch_wide_font_changed(void)
3270{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003271 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003273#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003274 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003275#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003276
3277 gui_mch_free_font(gui.wide_ital_font);
3278 gui.wide_ital_font = NOFONT;
3279 gui_mch_free_font(gui.wide_bold_font);
3280 gui.wide_bold_font = NOFONT;
3281 gui_mch_free_font(gui.wide_boldital_font);
3282 gui.wide_boldital_font = NOFONT;
3283
3284 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003285 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003286 {
3287 if (!lf.lfItalic)
3288 {
3289 lf.lfItalic = TRUE;
3290 gui.wide_ital_font = get_font_handle(&lf);
3291 lf.lfItalic = FALSE;
3292 }
3293 if (lf.lfWeight < FW_BOLD)
3294 {
3295 lf.lfWeight = FW_BOLD;
3296 gui.wide_bold_font = get_font_handle(&lf);
3297 if (!lf.lfItalic)
3298 {
3299 lf.lfItalic = TRUE;
3300 gui.wide_boldital_font = get_font_handle(&lf);
3301 }
3302 }
3303 }
3304}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003305
3306/*
3307 * Initialise vim to use the font with the given name.
3308 * Return FAIL if the font could not be loaded, OK otherwise.
3309 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003311gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003312{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003313 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003314 GuiFont font = NOFONT;
3315 char_u *p;
3316
Bram Moolenaar734a8672019-12-02 22:49:38 +01003317 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003318 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3319 font = get_font_handle(&lf);
3320 if (font == NOFONT)
3321 return FAIL;
3322
3323 if (font_name == NULL)
3324 font_name = (char_u *)lf.lfFaceName;
3325#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3326 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003327#endif
3328#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003329 sub_logfont = lf;
3330#endif
3331#ifdef FEAT_MBYTE_IME
3332 update_im_font();
3333#endif
3334 gui_mch_free_font(gui.norm_font);
3335 gui.norm_font = font;
3336 current_font_height = lf.lfHeight;
3337 GetFontSize(font);
3338
3339 p = logfont2name(lf);
3340 if (p != NULL)
3341 {
3342 hl_set_font_name(p);
3343
Bram Moolenaar734a8672019-12-02 22:49:38 +01003344 // When setting 'guifont' to "*" replace it with the actual font name.
3345 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3347 {
3348 vim_free(p_guifont);
3349 p_guifont = p;
3350 }
3351 else
3352 vim_free(p);
3353 }
3354
3355 gui_mch_free_font(gui.ital_font);
3356 gui.ital_font = NOFONT;
3357 gui_mch_free_font(gui.bold_font);
3358 gui.bold_font = NOFONT;
3359 gui_mch_free_font(gui.boldital_font);
3360 gui.boldital_font = NOFONT;
3361
3362 if (!lf.lfItalic)
3363 {
3364 lf.lfItalic = TRUE;
3365 gui.ital_font = get_font_handle(&lf);
3366 lf.lfItalic = FALSE;
3367 }
3368 if (lf.lfWeight < FW_BOLD)
3369 {
3370 lf.lfWeight = FW_BOLD;
3371 gui.bold_font = get_font_handle(&lf);
3372 if (!lf.lfItalic)
3373 {
3374 lf.lfItalic = TRUE;
3375 gui.boldital_font = get_font_handle(&lf);
3376 }
3377 }
3378
3379 return OK;
3380}
3381
3382#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003383# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384#endif
3385
3386/*
3387 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003388 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389 */
3390 int
3391gui_mch_maximized(void)
3392{
3393 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003394 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395
3396 wp.length = sizeof(WINDOWPLACEMENT);
3397 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003398 {
3399 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003401 && wp.flags == WPF_RESTORETOMAXIMIZED))
3402 return TRUE;
3403 if (wp.showCmd == SW_SHOWMINIMIZED)
3404 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003405
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003406 // Assume the window is snapped when the sizes from two APIs differ.
3407 GetWindowRect(s_hwnd, &rc);
3408 if ((rc.right - rc.left !=
3409 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3410 || (rc.bottom - rc.top !=
3411 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3412 return TRUE;
3413 }
3414 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415}
3416
3417/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003418 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3419 * is set. Compute the new Rows and Columns. This is like resizing the
3420 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003421 */
3422 void
3423gui_mch_newfont(void)
3424{
3425 RECT rect;
3426
3427 GetWindowRect(s_hwnd, &rect);
3428 if (win_socket_id == 0)
3429 {
3430 gui_resize_shell(rect.right - rect.left
3431 - (GetSystemMetrics(SM_CXFRAME) +
3432 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3433 rect.bottom - rect.top
3434 - (GetSystemMetrics(SM_CYFRAME) +
3435 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3436 - GetSystemMetrics(SM_CYCAPTION)
3437#ifdef FEAT_MENU
3438 - gui_mswin_get_menu_height(FALSE)
3439#endif
3440 );
3441 }
3442 else
3443 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003444 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003445 gui_resize_shell(rect.right - rect.left,
3446 rect.bottom - rect.top
3447#ifdef FEAT_MENU
3448 - gui_mswin_get_menu_height(FALSE)
3449#endif
3450 );
3451 }
3452}
3453
3454/*
3455 * Set the window title
3456 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003457 void
3458gui_mch_settitle(
3459 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003460 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461{
3462 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3463}
3464
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003465#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003466// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3467// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003468static LPCSTR mshape_idcs[] =
3469{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003470 IDC_ARROW, // arrow
3471 MAKEINTRESOURCE(0), // blank
3472 IDC_IBEAM, // beam
3473 IDC_SIZENS, // updown
3474 IDC_SIZENS, // udsizing
3475 IDC_SIZEWE, // leftright
3476 IDC_SIZEWE, // lrsizing
3477 IDC_WAIT, // busy
3478 IDC_NO, // no
3479 IDC_ARROW, // crosshair
3480 IDC_ARROW, // hand1
3481 IDC_ARROW, // hand2
3482 IDC_ARROW, // pencil
3483 IDC_ARROW, // question
3484 IDC_ARROW, // right-arrow
3485 IDC_UPARROW, // up-arrow
3486 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003487};
3488
3489 void
3490mch_set_mouse_shape(int shape)
3491{
3492 LPCSTR idc;
3493
3494 if (shape == MSHAPE_HIDE)
3495 ShowCursor(FALSE);
3496 else
3497 {
3498 if (shape >= MSHAPE_NUMBERED)
3499 idc = IDC_ARROW;
3500 else
3501 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003502 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 if (!p_mh)
3504 {
3505 POINT mp;
3506
Bram Moolenaar734a8672019-12-02 22:49:38 +01003507 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508 (void)GetCursorPos((LPPOINT)&mp);
3509 (void)SetCursorPos(mp.x, mp.y);
3510 ShowCursor(TRUE);
3511 }
3512 }
3513}
3514#endif
3515
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003516#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003517/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 * Wide version of convert_filter().
3519 */
3520 static WCHAR *
3521convert_filterW(char_u *s)
3522{
3523 char_u *tmp;
3524 int len;
3525 WCHAR *res;
3526
3527 tmp = convert_filter(s);
3528 if (tmp == NULL)
3529 return NULL;
3530 len = (int)STRLEN(s) + 3;
3531 res = enc_to_utf16(tmp, &len);
3532 vim_free(tmp);
3533 return res;
3534}
3535
3536/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003537 * Pop open a file browser and return the file selected, in allocated memory,
3538 * or NULL if Cancel is hit.
3539 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3540 * title - Title message for the file browser dialog.
3541 * dflt - Default name of file.
3542 * ext - Default extension to be added to files without extensions.
3543 * initdir - directory in which to open the browser (NULL = current dir)
3544 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003545 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003546 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003547gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003548 int saving,
3549 char_u *title,
3550 char_u *dflt,
3551 char_u *ext,
3552 char_u *initdir,
3553 char_u *filter)
3554{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003555 // We always use the wide function. This means enc_to_utf16() must work,
3556 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557 OPENFILENAMEW fileStruct;
3558 WCHAR fileBuf[MAXPATHL];
3559 WCHAR *wp;
3560 int i;
3561 WCHAR *titlep = NULL;
3562 WCHAR *extp = NULL;
3563 WCHAR *initdirp = NULL;
3564 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003565 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566
3567 if (dflt == NULL)
3568 fileBuf[0] = NUL;
3569 else
3570 {
3571 wp = enc_to_utf16(dflt, NULL);
3572 if (wp == NULL)
3573 fileBuf[0] = NUL;
3574 else
3575 {
3576 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3577 fileBuf[i] = wp[i];
3578 fileBuf[i] = NUL;
3579 vim_free(wp);
3580 }
3581 }
3582
Bram Moolenaar734a8672019-12-02 22:49:38 +01003583 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003584 filterp = convert_filterW(filter);
3585
Bram Moolenaara80faa82020-04-12 19:37:17 +02003586 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003587# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003588 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003589 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003590# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003592# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003593
3594 if (title != NULL)
3595 titlep = enc_to_utf16(title, NULL);
3596 fileStruct.lpstrTitle = titlep;
3597
3598 if (ext != NULL)
3599 extp = enc_to_utf16(ext, NULL);
3600 fileStruct.lpstrDefExt = extp;
3601
3602 fileStruct.lpstrFile = fileBuf;
3603 fileStruct.nMaxFile = MAXPATHL;
3604 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003605 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3606 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003607 if (initdir != NULL && *initdir != NUL)
3608 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003609 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610 initdirp = enc_to_utf16(initdir, NULL);
3611 if (initdirp != NULL)
3612 {
3613 for (wp = initdirp; *wp != NUL; ++wp)
3614 if (*wp == '/')
3615 *wp = '\\';
3616 }
3617 fileStruct.lpstrInitialDir = initdirp;
3618 }
3619
3620 /*
3621 * TODO: Allow selection of multiple files. Needs another arg to this
3622 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3623 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3624 * files that don't exist yet, so I haven't put it in. What about
3625 * OFN_PATHMUSTEXIST?
3626 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3627 */
3628 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003629# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630 if (curbuf->b_p_bin)
3631 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003632# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633 if (saving)
3634 {
3635 if (!GetSaveFileNameW(&fileStruct))
3636 return NULL;
3637 }
3638 else
3639 {
3640 if (!GetOpenFileNameW(&fileStruct))
3641 return NULL;
3642 }
3643
3644 vim_free(filterp);
3645 vim_free(initdirp);
3646 vim_free(titlep);
3647 vim_free(extp);
3648
Bram Moolenaar734a8672019-12-02 22:49:38 +01003649 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003651 if (p == NULL)
3652 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653
Bram Moolenaar734a8672019-12-02 22:49:38 +01003654 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 SetFocus(s_hwnd);
3656
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003658 q = vim_strsave(shorten_fname1(p));
3659 vim_free(p);
3660 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003661}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663
3664/*
3665 * Convert the string s to the proper format for a filter string by replacing
3666 * the \t and \n delimiters with \0.
3667 * Returns the converted string in allocated memory.
3668 *
3669 * Keep in sync with convert_filterW() above!
3670 */
3671 static char_u *
3672convert_filter(char_u *s)
3673{
3674 char_u *res;
3675 unsigned s_len = (unsigned)STRLEN(s);
3676 unsigned i;
3677
3678 res = alloc(s_len + 3);
3679 if (res != NULL)
3680 {
3681 for (i = 0; i < s_len; ++i)
3682 if (s[i] == '\t' || s[i] == '\n')
3683 res[i] = '\0';
3684 else
3685 res[i] = s[i];
3686 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003687 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688 res[s_len + 1] = NUL;
3689 res[s_len + 2] = NUL;
3690 }
3691 return res;
3692}
3693
3694/*
3695 * Select a directory.
3696 */
3697 char_u *
3698gui_mch_browsedir(char_u *title, char_u *initdir)
3699{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003700 // We fake this: Use a filter that doesn't select anything and a default
3701 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3703 initdir, (char_u *)_("Directory\t*.nothing\n"));
3704}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003705#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003707 static void
3708_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003709 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 HDROP hDrop)
3711{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003712#define BUFPATHLEN _MAX_PATH
3713#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715 char szFile[BUFPATHLEN];
3716 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3717 UINT i;
3718 char_u **fnames;
3719 POINT pt;
3720 int_u modifiers = 0;
3721
Bram Moolenaar734a8672019-12-02 22:49:38 +01003722 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725 DragQueryPoint(hDrop, &pt);
3726 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3727
3728 reset_VIsual();
3729
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003730 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003731
3732 if (fnames != NULL)
3733 for (i = 0; i < cFiles; ++i)
3734 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3736 fnames[i] = utf16_to_enc(wszFile, NULL);
3737 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738 {
3739 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3740 fnames[i] = vim_strsave((char_u *)szFile);
3741 }
3742 }
3743
3744 DragFinish(hDrop);
3745
3746 if (fnames != NULL)
3747 {
3748 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3749 modifiers |= MOUSE_SHIFT;
3750 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3751 modifiers |= MOUSE_CTRL;
3752 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3753 modifiers |= MOUSE_ALT;
3754
3755 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3756
3757 s_need_activate = TRUE;
3758 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759}
3760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761 static int
3762_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003763 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764 HWND hwndCtl,
3765 UINT code,
3766 int pos)
3767{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003768 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769 scrollbar_T *sb, *sb_info;
3770 long val;
3771 int dragging = FALSE;
3772 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 SCROLLINFO si;
3774
3775 si.cbSize = sizeof(si);
3776 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777
3778 sb = gui_mswin_find_scrollbar(hwndCtl);
3779 if (sb == NULL)
3780 return 0;
3781
Bram Moolenaar734a8672019-12-02 22:49:38 +01003782 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 {
3784 /*
3785 * Careful: need to get scrollbar info out of first (left) scrollbar
3786 * for window, but keep real scrollbar too because we must pass it to
3787 * gui_drag_scrollbar().
3788 */
3789 sb_info = &sb->wp->w_scrollbars[0];
3790 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003791 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003792 sb_info = sb;
3793 val = sb_info->value;
3794
3795 switch (code)
3796 {
3797 case SB_THUMBTRACK:
3798 val = pos;
3799 dragging = TRUE;
3800 if (sb->scroll_shift > 0)
3801 val <<= sb->scroll_shift;
3802 break;
3803 case SB_LINEDOWN:
3804 val++;
3805 break;
3806 case SB_LINEUP:
3807 val--;
3808 break;
3809 case SB_PAGEDOWN:
3810 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3811 break;
3812 case SB_PAGEUP:
3813 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3814 break;
3815 case SB_TOP:
3816 val = 0;
3817 break;
3818 case SB_BOTTOM:
3819 val = sb_info->max;
3820 break;
3821 case SB_ENDSCROLL:
3822 if (prev_code == SB_THUMBTRACK)
3823 {
3824 /*
3825 * "pos" only gives us 16-bit data. In case of large file,
3826 * use GetScrollPos() which returns 32-bit. Unfortunately it
3827 * is not valid while the scrollbar is being dragged.
3828 */
3829 val = GetScrollPos(hwndCtl, SB_CTL);
3830 if (sb->scroll_shift > 0)
3831 val <<= sb->scroll_shift;
3832 }
3833 break;
3834
3835 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003836 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003837 return 0;
3838 }
3839 prev_code = code;
3840
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3842 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843
3844 /*
3845 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3846 */
3847 if (sb->wp != NULL)
3848 {
3849 scrollbar_T *sba = sb->wp->w_scrollbars;
3850 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3851
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003853 }
3854
Bram Moolenaar734a8672019-12-02 22:49:38 +01003855 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003856 s_busy_processing = TRUE;
3857
Bram Moolenaar734a8672019-12-02 22:49:38 +01003858 // When "allow_scrollbar" is FALSE still need to remember the new
3859 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003860 dont_scroll = !allow_scrollbar;
3861
Bram Moolenaara338adc2018-01-31 20:51:47 +01003862 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003864 mch_enable_flush();
3865 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003866
3867 s_busy_processing = FALSE;
3868 dont_scroll = dont_scroll_save;
3869
3870 return 0;
3871}
3872
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#ifdef FEAT_XPM_W32
3875# include "xpm_w32.h"
3876#endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878#ifdef __MINGW32__
3879/*
3880 * Add a lot of missing defines.
3881 * They are not always missing, we need the #ifndef's.
3882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883# ifndef IsMinimized
3884# define IsMinimized(hwnd) IsIconic(hwnd)
3885# endif
3886# ifndef IsMaximized
3887# define IsMaximized(hwnd) IsZoomed(hwnd)
3888# endif
3889# ifndef SelectFont
3890# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3891# endif
3892# ifndef GetStockBrush
3893# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3894# endif
3895# ifndef DeleteBrush
3896# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3897# endif
3898
3899# ifndef HANDLE_WM_RBUTTONDBLCLK
3900# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3901 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3902# endif
3903# ifndef HANDLE_WM_MBUTTONUP
3904# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3905 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3906# endif
3907# ifndef HANDLE_WM_MBUTTONDBLCLK
3908# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3909 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3910# endif
3911# ifndef HANDLE_WM_LBUTTONDBLCLK
3912# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3913 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3914# endif
3915# ifndef HANDLE_WM_RBUTTONDOWN
3916# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3917 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3918# endif
3919# ifndef HANDLE_WM_MOUSEMOVE
3920# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3921 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3922# endif
3923# ifndef HANDLE_WM_RBUTTONUP
3924# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3925 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3926# endif
3927# ifndef HANDLE_WM_MBUTTONDOWN
3928# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3929 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3930# endif
3931# ifndef HANDLE_WM_LBUTTONUP
3932# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3933 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3934# endif
3935# ifndef HANDLE_WM_LBUTTONDOWN
3936# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3937 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3938# endif
3939# ifndef HANDLE_WM_SYSCHAR
3940# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3941 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3942# endif
3943# ifndef HANDLE_WM_ACTIVATEAPP
3944# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3945 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3946# endif
3947# ifndef HANDLE_WM_WINDOWPOSCHANGING
3948# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3949 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3950# endif
3951# ifndef HANDLE_WM_VSCROLL
3952# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3953 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3954# endif
3955# ifndef HANDLE_WM_SETFOCUS
3956# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3957 ((fn)((hwnd), (HWND)(wParam)), 0L)
3958# endif
3959# ifndef HANDLE_WM_KILLFOCUS
3960# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3961 ((fn)((hwnd), (HWND)(wParam)), 0L)
3962# endif
3963# ifndef HANDLE_WM_HSCROLL
3964# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3965 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3966# endif
3967# ifndef HANDLE_WM_DROPFILES
3968# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3969 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3970# endif
3971# ifndef HANDLE_WM_CHAR
3972# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3973 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3974# endif
3975# ifndef HANDLE_WM_SYSDEADCHAR
3976# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3977 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3978# endif
3979# ifndef HANDLE_WM_DEADCHAR
3980# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3981 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3982# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003983#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985
Bram Moolenaar734a8672019-12-02 22:49:38 +01003986// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987#define TEAROFF_PADDING_X 2
3988#define TEAROFF_BUTTON_PAD_X 8
3989#define TEAROFF_MIN_WIDTH 200
3990#define TEAROFF_SUBMENU_LABEL ">>"
3991#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3992
3993
Bram Moolenaar734a8672019-12-02 22:49:38 +01003994// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003996# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#endif
3998
3999
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004000#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001# define ID_BEVAL_TOOLTIP 200
4002# define BEVAL_TEXT_LEN MAXPATHL
4003
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004004# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004005// Work around old versions of basetsd.h which wrongly declares
4006// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004007# undef UINT_PTR
4008# define UINT_PTR UINT
4009# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004012static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004014
Bram Moolenaar82881492012-11-20 16:53:39 +01004015
Bram Moolenaar734a8672019-12-02 22:49:38 +01004016// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004017# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01004018
Bram Moolenaar45360022005-07-21 21:08:21 +00004019/*
4020 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004021 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004022 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004023# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01004024
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004025# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004026
4027typedef struct _DllVersionInfo
4028{
4029 DWORD cbSize;
4030 DWORD dwMajorVersion;
4031 DWORD dwMinorVersion;
4032 DWORD dwBuildNumber;
4033 DWORD dwPlatformID;
4034} DLLVERSIONINFO;
4035
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004036# ifndef PROTO
4037# include <poppack.h>
4038# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004039
Bram Moolenaar45360022005-07-21 21:08:21 +00004040typedef struct tagTOOLINFOA_NEW
4041{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004042 UINT cbSize;
4043 UINT uFlags;
4044 HWND hwnd;
4045 UINT_PTR uId;
4046 RECT rect;
4047 HINSTANCE hinst;
4048 LPSTR lpszText;
4049 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004050} TOOLINFO_NEW;
4051
4052typedef struct tagNMTTDISPINFO_NEW
4053{
4054 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004055 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004056 char szText[80];
4057 HINSTANCE hinst;
4058 UINT uFlags;
4059 LPARAM lParam;
4060} NMTTDISPINFO_NEW;
4061
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004062typedef struct tagTOOLINFOW_NEW
4063{
4064 UINT cbSize;
4065 UINT uFlags;
4066 HWND hwnd;
4067 UINT_PTR uId;
4068 RECT rect;
4069 HINSTANCE hinst;
4070 LPWSTR lpszText;
4071 LPARAM lParam;
4072 void *lpReserved;
4073} TOOLINFOW_NEW;
4074
4075typedef struct tagNMTTDISPINFOW_NEW
4076{
4077 NMHDR hdr;
4078 LPWSTR lpszText;
4079 WCHAR szText[80];
4080 HINSTANCE hinst;
4081 UINT uFlags;
4082 LPARAM lParam;
4083} NMTTDISPINFOW_NEW;
4084
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004085
Bram Moolenaar45360022005-07-21 21:08:21 +00004086typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004087# ifndef TTM_SETMAXTIPWIDTH
4088# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004091# ifndef TTF_DI_SETITEM
4092# define TTF_DI_SETITEM 0x8000
4093# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004094
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004095# ifndef TTN_GETDISPINFO
4096# define TTN_GETDISPINFO (TTN_FIRST - 0)
4097# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004098
Bram Moolenaar734a8672019-12-02 22:49:38 +01004099#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004100
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004101#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004102// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4103// it here if LPNMTTDISPINFO isn't defined.
4104// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4105// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004106# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4107typedef struct tagNMTTDISPINFOA {
4108 NMHDR hdr;
4109 LPSTR lpszText;
4110 char szText[80];
4111 HINSTANCE hinst;
4112 UINT uFlags;
4113 LPARAM lParam;
4114} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4115# define LPNMTTDISPINFO LPNMTTDISPINFOA
4116
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004117typedef struct tagNMTTDISPINFOW {
4118 NMHDR hdr;
4119 LPWSTR lpszText;
4120 WCHAR szText[80];
4121 HINSTANCE hinst;
4122 UINT uFlags;
4123 LPARAM lParam;
4124} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004125# endif
4126#endif
4127
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004128#ifndef TTN_GETDISPINFOW
4129# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4130#endif
4131
Bram Moolenaar734a8672019-12-02 22:49:38 +01004132// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134#ifdef FEAT_MENU
4135static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138/*
4139 * Use the system font for dialogs and tear-off menus. Remove this line to
4140 * use DLG_FONT_NAME.
4141 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004142#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145#define VIM_CLASSW L"Vim"
4146
Bram Moolenaar734a8672019-12-02 22:49:38 +01004147// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4148// thus there should be room for every dialog. For tearoffs it's made bigger
4149// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#define DLG_ALLOC_SIZE 16 * 1024
4151
4152/*
4153 * stuff for dialogs, menus, tearoffs etc.
4154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155static PWORD
4156add_dialog_element(
4157 PWORD p,
4158 DWORD lStyle,
4159 WORD x,
4160 WORD y,
4161 WORD w,
4162 WORD h,
4163 WORD Id,
4164 WORD clss,
4165 const char *caption);
4166static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004167static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004168#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171static void get_dialog_font_metrics(void);
4172
4173static int dialog_default_button = -1;
4174
Bram Moolenaar734a8672019-12-02 22:49:38 +01004175// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Bram Moolenaar734a8672019-12-02 22:49:38 +01004178static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#ifdef FEAT_TOOLBAR
4180static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004181static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182static int get_toolbar_bitmap(vimmenu_T *menu);
4183#endif
4184
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004185#ifdef FEAT_GUI_TABLINE
4186static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004187static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004188#endif
4189
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifdef FEAT_MBYTE_IME
4191static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4192static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4193#endif
4194#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4195# ifdef NOIME
4196typedef struct tagCOMPOSITIONFORM {
4197 DWORD dwStyle;
4198 POINT ptCurrentPos;
4199 RECT rcArea;
4200} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4201typedef HANDLE HIMC;
4202# endif
4203
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004204static HINSTANCE hLibImm = NULL;
4205static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4206static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4207static HIMC (WINAPI *pImmGetContext)(HWND);
4208static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4209static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4210static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4211static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004212static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4213static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004214static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4215static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004216static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217static void dyn_imm_load(void);
4218#else
4219# define pImmGetCompositionStringA ImmGetCompositionStringA
4220# define pImmGetCompositionStringW ImmGetCompositionStringW
4221# define pImmGetContext ImmGetContext
4222# define pImmAssociateContext ImmAssociateContext
4223# define pImmReleaseContext ImmReleaseContext
4224# define pImmGetOpenStatus ImmGetOpenStatus
4225# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004226# define pImmGetCompositionFontW ImmGetCompositionFontW
4227# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228# define pImmSetCompositionWindow ImmSetCompositionWindow
4229# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004230# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#endif
4232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#ifdef FEAT_MENU
4234/*
4235 * Figure out how high the menu bar is at the moment.
4236 */
4237 static int
4238gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004239 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
4241 static int old_menu_height = -1;
4242
4243 RECT rc1, rc2;
4244 int num;
4245 int menu_height;
4246
4247 if (gui.menu_is_active)
4248 num = GetMenuItemCount(s_menuBar);
4249 else
4250 num = 0;
4251
4252 if (num == 0)
4253 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004254 else if (IsMinimized(s_hwnd))
4255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004256 // The height of the menu cannot be determined while the window is
4257 // minimized. Take the previous height if the menu is changed in that
4258 // state, to avoid that Vim's vertical window size accidentally
4259 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004260 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 else
4263 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004264 /*
4265 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4266 * seem to have been set yet, so menu wraps in default window
4267 * width which is very narrow. Instead just return height of a
4268 * single menu item. Will still be wrong when the menu really
4269 * should wrap over more than one line.
4270 */
4271 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4272 if (gui.starting)
4273 menu_height = rc1.bottom - rc1.top + 1;
4274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004276 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4277 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280
4281 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004282 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004283 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
4285 return menu_height;
4286}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004287#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289
4290/*
4291 * Setup for the Intellimouse
4292 */
4293 static void
4294init_mouse_wheel(void)
4295{
4296
4297#ifndef SPI_GETWHEELSCROLLLINES
4298# define SPI_GETWHEELSCROLLLINES 104
4299#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004300#ifndef SPI_SETWHEELSCROLLLINES
4301# define SPI_SETWHEELSCROLLLINES 105
4302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
Bram Moolenaar734a8672019-12-02 22:49:38 +01004304#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4305#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4307#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4308
Bram Moolenaar734a8672019-12-02 22:49:38 +01004309 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
Bram Moolenaar734a8672019-12-02 22:49:38 +01004311 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004312 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4313 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314}
4315
4316
Bram Moolenaarae20f342019-11-05 21:09:23 +01004317/*
4318 * Intellimouse wheel handler.
4319 * Treat a mouse wheel event as if it were a scroll request.
4320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 static void
4322_OnMouseWheel(
4323 HWND hwnd,
4324 short zDelta)
4325{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int i;
4327 int size;
4328 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004329 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (mouse_scroll_lines == 0)
4332 init_mouse_wheel();
4333
Bram Moolenaarae20f342019-11-05 21:09:23 +01004334 wp = gui_mouse_window(FIND_POPUP);
4335
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004336#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004337 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004338 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004339 cmdarg_T cap;
4340 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004341
Bram Moolenaarae20f342019-11-05 21:09:23 +01004342 // Mouse hovers over popup window, scroll it if possible.
4343 mouse_row = wp->w_winrow;
4344 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004345 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004346 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4347 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4348 clear_oparg(&oa);
4349 cap.oap = &oa;
4350 nv_mousescroll(&cap);
4351 update_screen(0);
4352 setcursor();
4353 out_flush();
4354 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004355 }
4356#endif
4357
Bram Moolenaarae20f342019-11-05 21:09:23 +01004358 if (wp == NULL || !p_scf)
4359 wp = curwin;
4360
4361 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4362 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4363 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4364 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4365 else
4366 return;
4367 size = wp->w_height;
4368
Bram Moolenaara338adc2018-01-31 20:51:47 +01004369 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (mouse_scroll_lines > 0
4371 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4372 {
4373 for (i = mouse_scroll_lines; i > 0; --i)
4374 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4375 }
4376 else
4377 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004378 mch_enable_flush();
4379 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380}
4381
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004382#ifdef USE_SYSMENU_FONT
4383/*
4384 * Get Menu Font.
4385 * Return OK or FAIL.
4386 */
4387 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004388gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004389{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004390 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004391
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004392 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4393 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004394 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004395 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004396 &nm,
4397 0))
4398 return FAIL;
4399 *lf = nm.lfMenuFont;
4400 return OK;
4401}
4402#endif
4403
4404
4405#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4406/*
4407 * Set the GUI tabline font to the system menu font
4408 */
4409 static void
4410set_tabline_font(void)
4411{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004412 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004413 HFONT font;
4414 HWND hwnd;
4415 HDC hdc;
4416 HFONT hfntOld;
4417 TEXTMETRIC tm;
4418
4419 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4420 return;
4421
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004422 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004423
4424 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4425
4426 /*
4427 * Compute the height of the font used for the tab text
4428 */
4429 hwnd = GetDesktopWindow();
4430 hdc = GetWindowDC(hwnd);
4431 hfntOld = SelectFont(hdc, font);
4432
4433 GetTextMetrics(hdc, &tm);
4434
4435 SelectFont(hdc, hfntOld);
4436 ReleaseDC(hwnd, hdc);
4437
4438 /*
4439 * The space used by the tab border and the space between the tab label
4440 * and the tab border is included as 7.
4441 */
4442 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4443}
4444#endif
4445
Bram Moolenaar520470a2005-06-16 21:59:56 +00004446/*
4447 * Invoked when a setting was changed.
4448 */
4449 static LRESULT CALLBACK
4450_OnSettingChange(UINT n)
4451{
4452 if (n == SPI_SETWHEELSCROLLLINES)
4453 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4454 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004455#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4456 if (n == SPI_SETNONCLIENTMETRICS)
4457 set_tabline_font();
4458#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004459 return 0;
4460}
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#ifdef FEAT_NETBEANS_INTG
4463 static void
4464_OnWindowPosChanged(
4465 HWND hwnd,
4466 const LPWINDOWPOS lpwpos)
4467{
4468 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004469 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4472 || lpwpos->cx != cx || lpwpos->cy != cy))
4473 {
4474 x = lpwpos->x;
4475 y = lpwpos->y;
4476 cx = lpwpos->cx;
4477 cy = lpwpos->cy;
4478 netbeans_frame_moved(x, y);
4479 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004480 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4482}
4483#endif
4484
K.Takata4f2417f2021-06-05 16:25:32 +02004485
4486static HWND hwndTip = NULL;
4487
4488 static void
4489show_sizing_tip(int cols, int rows)
4490{
4491 TOOLINFOA ti = {sizeof(ti)};
4492 char buf[32];
4493
4494 ti.hwnd = s_hwnd;
4495 ti.uId = (UINT_PTR)s_hwnd;
4496 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4497 ti.lpszText = buf;
4498 sprintf(buf, "%dx%d", cols, rows);
4499 if (hwndTip == NULL)
4500 {
4501 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4502 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4503 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4504 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4505 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4506 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4507 }
4508 else
4509 {
4510 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4511 }
4512 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4513}
4514
4515 static void
4516destroy_sizing_tip(void)
4517{
4518 if (hwndTip != NULL)
4519 {
4520 DestroyWindow(hwndTip);
4521 hwndTip = NULL;
4522 }
4523}
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 static int
4526_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 UINT fwSide,
4528 LPRECT lprc)
4529{
4530 int w, h;
4531 int valid_w, valid_h;
4532 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004533 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535 w = lprc->right - lprc->left;
4536 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004537 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 w_offset = w - valid_w;
4539 h_offset = h - valid_h;
4540
4541 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4542 || fwSide == WMSZ_BOTTOMLEFT)
4543 lprc->left += w_offset;
4544 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4545 || fwSide == WMSZ_BOTTOMRIGHT)
4546 lprc->right -= w_offset;
4547
4548 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4549 || fwSide == WMSZ_TOPRIGHT)
4550 lprc->top += h_offset;
4551 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4552 || fwSide == WMSZ_BOTTOMRIGHT)
4553 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004554
4555 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 return TRUE;
4557}
4558
4559
4560
4561 static LRESULT CALLBACK
4562_WndProc(
4563 HWND hwnd,
4564 UINT uMsg,
4565 WPARAM wParam,
4566 LPARAM lParam)
4567{
4568 /*
4569 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4570 hwnd, uMsg, wParam, lParam);
4571 */
4572
4573 HandleMouseHide(uMsg, lParam);
4574
4575 s_uMsg = uMsg;
4576 s_wParam = wParam;
4577 s_lParam = lParam;
4578
4579 switch (uMsg)
4580 {
4581 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4582 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004583 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004585 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4587 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4588 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4589 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4590#ifdef FEAT_MENU
4591 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4592#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004593 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4594 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4596 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004597 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4598 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4600 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4601 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4602#ifdef FEAT_NETBEANS_INTG
4603 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4604#endif
4605
Bram Moolenaarafa24992006-03-27 20:58:26 +00004606#ifdef FEAT_GUI_TABLINE
4607 case WM_RBUTTONUP:
4608 {
4609 if (gui_mch_showing_tabline())
4610 {
4611 POINT pt;
4612 RECT rect;
4613
4614 /*
4615 * If the cursor is on the tabline, display the tab menu
4616 */
4617 GetCursorPos((LPPOINT)&pt);
4618 GetWindowRect(s_textArea, &rect);
4619 if (pt.y < rect.top)
4620 {
4621 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004622 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004623 }
4624 }
4625 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4626 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004627 case WM_LBUTTONDBLCLK:
4628 {
4629 /*
4630 * If the user double clicked the tabline, create a new tab
4631 */
4632 if (gui_mch_showing_tabline())
4633 {
4634 POINT pt;
4635 RECT rect;
4636
4637 GetCursorPos((LPPOINT)&pt);
4638 GetWindowRect(s_textArea, &rect);
4639 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004640 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004641 }
4642 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4643 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004644#endif
4645
Bram Moolenaar734a8672019-12-02 22:49:38 +01004646 case WM_QUERYENDSESSION: // System wants to go down.
4647 gui_shell_closed(); // Will exit when no changed buffers.
4648 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004651 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004654 return 0L;
4655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 break;
4657
4658 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004659 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4660 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004661 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 return 0L;
4663
4664 case WM_SYSCHAR:
4665 /*
4666 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4667 * shortcut key, handle like a typed ALT key, otherwise call Windows
4668 * ALT key handling.
4669 */
4670#ifdef FEAT_MENU
4671 if ( !gui.menu_is_active
4672 || p_wak[0] == 'n'
4673 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4674 )
4675#endif
4676 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004677 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return 0L;
4679 }
4680#ifdef FEAT_MENU
4681 else
4682 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4683#endif
4684
4685 case WM_SYSKEYUP:
4686#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004687 // This used to be done only when menu is active: ALT key is used for
4688 // that. But that caused problems when menu is disabled and using
4689 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4690 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4692#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004693 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#endif
4695
K.Takata4f2417f2021-06-05 16:25:32 +02004696 case WM_EXITSIZEMOVE:
4697 destroy_sizing_tip();
4698 break;
4699
Bram Moolenaar734a8672019-12-02 22:49:38 +01004700 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004701 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 case WM_MOUSEWHEEL:
4704 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004705 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
Bram Moolenaar734a8672019-12-02 22:49:38 +01004707 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004708 case WM_SETTINGCHANGE:
4709 return _OnSettingChange((UINT)wParam);
4710
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004711#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 case WM_NOTIFY:
4713 switch (((LPNMHDR) lParam)->code)
4714 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004715 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004716 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004718 LPNMHDR hdr = (LPNMHDR)lParam;
4719 char_u *str = NULL;
4720 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004721
Bram Moolenaard23a8232018-02-10 18:45:26 +01004722 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004723
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004724# ifdef FEAT_GUI_TABLINE
4725 if (gui_mch_showing_tabline()
4726 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004727 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004728 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004729 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004730 * Mouse is over the GUI tabline. Display the
4731 * tooltip for the tab under the cursor
4732 *
4733 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004734 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004735 GetCursorPos(&pt);
4736 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004737 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004738 TCHITTESTINFO htinfo;
4739 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004740
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004741 /*
4742 * Get the tab under the cursor
4743 */
4744 htinfo.pt.x = pt.x;
4745 htinfo.pt.y = pt.y;
4746 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4747 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004748 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004749 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004750
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004751 tp = find_tabpage(idx + 1);
4752 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004753 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004754 get_tabline_label(tp, TRUE);
4755 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004756 }
4757 }
4758 }
4759 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004760# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004761# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004762# ifdef FEAT_GUI_TABLINE
4763 else
4764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004766 UINT idButton;
4767 vimmenu_T *pMenu;
4768
4769 idButton = (UINT) hdr->idFrom;
4770 pMenu = gui_mswin_find_menu(root_menu, idButton);
4771 if (pMenu)
4772 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004774# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004775 if (str != NULL)
4776 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004777 if (hdr->code == TTN_GETDISPINFOW)
4778 {
4779 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4780
Bram Moolenaar734a8672019-12-02 22:49:38 +01004781 // Set the maximum width, this also enables using
4782 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004783 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4784 0, 500);
4785
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004786 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004787 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004788 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004789 }
4790 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004791 {
4792 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4793
Bram Moolenaar734a8672019-12-02 22:49:38 +01004794 // Set the maximum width, this also enables using
4795 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004796 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4797 0, 500);
4798
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004799 if (STRLEN(str) < sizeof(lpdi->szText)
4800 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004801 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004802 sizeof(lpdi->szText) - 1);
4803 else
4804 lpdi->lpszText = tt_text;
4805 }
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004809# ifdef FEAT_GUI_TABLINE
4810 case TCN_SELCHANGE:
4811 if (gui_mch_showing_tabline()
4812 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004813 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004814 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004815 return 0L;
4816 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004817 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004818
4819 case NM_RCLICK:
4820 if (gui_mch_showing_tabline()
4821 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004822 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004823 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004824 return 0L;
4825 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004826 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004829# ifdef FEAT_GUI_TABLINE
4830 if (gui_mch_showing_tabline()
4831 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4832 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 break;
4835 }
4836 break;
4837#endif
4838#if defined(MENUHINTS) && defined(FEAT_MENU)
4839 case WM_MENUSELECT:
4840 if (((UINT) HIWORD(wParam)
4841 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4842 == MF_HILITE
4843 && (State & CMDLINE) == 0)
4844 {
4845 UINT idButton;
4846 vimmenu_T *pMenu;
4847 static int did_menu_tip = FALSE;
4848
4849 if (did_menu_tip)
4850 {
4851 msg_clr_cmdline();
4852 setcursor();
4853 out_flush();
4854 did_menu_tip = FALSE;
4855 }
4856
4857 idButton = (UINT)LOWORD(wParam);
4858 pMenu = gui_mswin_find_menu(root_menu, idButton);
4859 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4860 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4861 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004862 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004863 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004864 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 setcursor();
4866 out_flush();
4867 did_menu_tip = TRUE;
4868 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004869 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 break;
4872#endif
4873 case WM_NCHITTEST:
4874 {
4875 LRESULT result;
4876 int x, y;
4877 int xPos = GET_X_LPARAM(lParam);
4878
4879 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4880 if (result == HTCLIENT)
4881 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004882#ifdef FEAT_GUI_TABLINE
4883 if (gui_mch_showing_tabline())
4884 {
4885 int yPos = GET_Y_LPARAM(lParam);
4886 RECT rct;
4887
Bram Moolenaar734a8672019-12-02 22:49:38 +01004888 // If the cursor is on the GUI tabline, don't process this
4889 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004890 GetWindowRect(s_textArea, &rct);
4891 if (yPos < rct.top)
4892 return result;
4893 }
4894#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004895 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 xPos -= x;
4897
Bram Moolenaar734a8672019-12-02 22:49:38 +01004898 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 return HTBOTTOMLEFT;
4900 else
4901 return HTBOTTOMRIGHT;
4902 }
4903 else
4904 return result;
4905 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004906 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908#ifdef FEAT_MBYTE_IME
4909 case WM_IME_NOTIFY:
4910 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4911 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004912 return 1L;
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 case WM_IME_COMPOSITION:
4915 if (!_OnImeComposition(hwnd, wParam, lParam))
4916 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004917 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
4919
4920 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004922 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#endif
4925 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4926 }
4927
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004928 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929}
4930
4931/*
4932 * End of call-back routines
4933 */
4934
Bram Moolenaar734a8672019-12-02 22:49:38 +01004935// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936HWND vim_parent_hwnd = NULL;
4937
4938 static BOOL CALLBACK
4939FindWindowTitle(HWND hwnd, LPARAM lParam)
4940{
4941 char buf[2048];
4942 char *title = (char *)lParam;
4943
4944 if (GetWindowText(hwnd, buf, sizeof(buf)))
4945 {
4946 if (strstr(buf, title) != NULL)
4947 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004948 // Found it. Store the window ref. and quit searching if MDI
4949 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004951 if (vim_parent_hwnd != NULL)
4952 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004955 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956}
4957
4958/*
4959 * Invoked for '-P "title"' argument: search for parent application to open
4960 * our window in.
4961 */
4962 void
4963gui_mch_set_parent(char *title)
4964{
4965 EnumWindows(FindWindowTitle, (LPARAM)title);
4966 if (vim_parent_hwnd == NULL)
4967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004968 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 mch_exit(2);
4970 }
4971}
4972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004973#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 static void
4975ole_error(char *arg)
4976{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004977 char buf[IOSIZE];
4978
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004979# ifdef VIMDLL
4980 gui.in_use = mch_is_gui_executable();
4981# endif
4982
Bram Moolenaar734a8672019-12-02 22:49:38 +01004983 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004984 vim_snprintf(buf, IOSIZE,
4985 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4986 arg);
4987 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004991#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4992 static char *
4993gvim_error(void)
4994{
4995 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4996
4997 if (starting)
4998 {
4999 mch_errmsg(msg);
5000 mch_errmsg("\n");
5001 mch_exit(2);
5002 }
5003 return msg;
5004}
5005
5006 char *
5007gui_mch_do_spawn(char_u *arg)
5008{
5009 int len;
5010# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5011 char_u *session = NULL;
5012 LPWSTR tofree1 = NULL;
5013# endif
5014 WCHAR name[MAX_PATH];
5015 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5016 STARTUPINFOW si = {sizeof(si)};
5017 PROCESS_INFORMATION pi;
5018
5019 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5020 goto error;
5021 p = wcsrchr(name, L'\\');
5022 if (p == NULL)
5023 goto error;
5024 // Replace the executable name from vim(d).exe to gvim(d).exe.
5025# ifdef DEBUG
5026 wcscpy(p + 1, L"gvimd.exe");
5027# else
5028 wcscpy(p + 1, L"gvim.exe");
5029# endif
5030
5031# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5032 if (starting)
5033# endif
5034 {
5035 // Pass the command line to the new process.
5036 p = GetCommandLineW();
5037 // Skip 1st argument.
5038 while (*p && *p != L' ' && *p != L'\t')
5039 {
5040 if (*p == L'"')
5041 {
5042 while (*p && *p != L'"')
5043 ++p;
5044 if (*p)
5045 ++p;
5046 }
5047 else
5048 ++p;
5049 }
5050 cmd = p;
5051 }
5052# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5053 else
5054 {
5055 // Create a session file and pass it to the new process.
5056 LPWSTR wsession;
5057 char_u *savebg;
5058 int ret;
5059
5060 session = vim_tempname('s', FALSE);
5061 if (session == NULL)
5062 goto error;
5063 savebg = p_bg;
5064 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5065 ret = write_session_file(session);
5066 vim_free(p_bg);
5067 p_bg = savebg;
5068 if (!ret)
5069 goto error;
5070 wsession = enc_to_utf16(session, NULL);
5071 if (wsession == NULL)
5072 goto error;
5073 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005074 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005075 if (cmd == NULL)
5076 {
5077 vim_free(wsession);
5078 goto error;
5079 }
5080 tofree1 = cmd;
5081 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5082 wsession, wsession);
5083 vim_free(wsession);
5084 }
5085# endif
5086
5087 // Check additional arguments to the `:gui` command.
5088 if (arg != NULL)
5089 {
5090 warg = enc_to_utf16(arg, NULL);
5091 if (warg == NULL)
5092 goto error;
5093 tofree2 = warg;
5094 }
5095 else
5096 warg = L"";
5097
5098 // Set up the new command line.
5099 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005100 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005101 if (newcmd == NULL)
5102 goto error;
5103 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5104
5105 // Spawn a new GUI process.
5106 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5107 NULL, NULL, &si, &pi))
5108 goto error;
5109 CloseHandle(pi.hProcess);
5110 CloseHandle(pi.hThread);
5111 mch_exit(0);
5112
5113error:
5114# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5115 if (session)
5116 mch_remove(session);
5117 vim_free(session);
5118 vim_free(tofree1);
5119# endif
5120 vim_free(newcmd);
5121 vim_free(tofree2);
5122 return gvim_error();
5123}
5124#endif
5125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126/*
5127 * Parse the GUI related command-line arguments. Any arguments used are
5128 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005129 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 */
5131 void
5132gui_mch_prepare(int *argc, char **argv)
5133{
5134 int silent = FALSE;
5135 int idx;
5136
Bram Moolenaar734a8672019-12-02 22:49:38 +01005137 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5139 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005140 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5142 && (argv[2][0] == '-' || argv[2][0] == '/'))
5143 {
5144 silent = TRUE;
5145 idx = 2;
5146 }
5147 else
5148 idx = 1;
5149
Bram Moolenaar734a8672019-12-02 22:49:38 +01005150 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (STRICMP(argv[idx] + 1, "register") == 0)
5152 {
5153#ifdef FEAT_OLE
5154 RegisterMe(silent);
5155 mch_exit(0);
5156#else
5157 if (!silent)
5158 ole_error("register");
5159 mch_exit(2);
5160#endif
5161 }
5162
Bram Moolenaar734a8672019-12-02 22:49:38 +01005163 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5165 {
5166#ifdef FEAT_OLE
5167 UnregisterMe(!silent);
5168 mch_exit(0);
5169#else
5170 if (!silent)
5171 ole_error("unregister");
5172 mch_exit(2);
5173#endif
5174 }
5175
Bram Moolenaar734a8672019-12-02 22:49:38 +01005176 // Ignore an -embedding argument. It is only relevant if the
5177 // application wants to treat the case when it is started manually
5178 // differently from the case where it is started via automation (and
5179 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5181 {
5182#ifdef FEAT_OLE
5183 *argc = 1;
5184#else
5185 ole_error("embedding");
5186 mch_exit(2);
5187#endif
5188 }
5189 }
5190
5191#ifdef FEAT_OLE
5192 {
5193 int bDoRestart = FALSE;
5194
5195 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005196 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (bDoRestart)
5198 mch_exit(0);
5199 }
5200#endif
5201
5202#ifdef FEAT_NETBEANS_INTG
5203 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005204 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 int arg;
5206
5207 for (arg = 1; arg < *argc; arg++)
5208 if (strncmp("-nb", argv[arg], 3) == 0)
5209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 netbeansArg = argv[arg];
5211 mch_memmove(&argv[arg], &argv[arg + 1],
5212 (--*argc - arg) * sizeof(char *));
5213 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005214 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218}
5219
5220/*
5221 * Initialise the GUI. Create all the windows, set up all the call-backs
5222 * etc.
5223 */
5224 int
5225gui_mch_init(void)
5226{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005228 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#ifdef GLOBAL_IME
5231 ATOM atom;
5232#endif
5233
Bram Moolenaar734a8672019-12-02 22:49:38 +01005234 // Return here if the window was already opened (happens when
5235 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005237 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 /*
5240 * Load the tearoff bitmap
5241 */
5242#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005243 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245
5246 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5247 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5248#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005249 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#endif
5251 gui.border_width = 0;
5252
5253 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5254
Bram Moolenaar734a8672019-12-02 22:49:38 +01005255 // First try using the wide version, so that we can use any title.
5256 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005257 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005259 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 wndclassw.lpfnWndProc = _WndProc;
5261 wndclassw.cbClsExtra = 0;
5262 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005263 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5265 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5266 wndclassw.hbrBackground = s_brush;
5267 wndclassw.lpszMenuName = NULL;
5268 wndclassw.lpszClassName = szVimWndClassW;
5269
5270 if ((
5271#ifdef GLOBAL_IME
5272 atom =
5273#endif
5274 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 }
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (vim_parent_hwnd != NULL)
5279 {
5280#ifdef HAVE_TRY_EXCEPT
5281 __try
5282 {
5283#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005284 // Open inside the specified parent window.
5285 // TODO: last argument should point to a CLIENTCREATESTRUCT
5286 // structure.
5287 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005289 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005290 WS_OVERLAPPEDWINDOW | WS_CHILD
5291 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5293 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005294 100, // Any value will do
5295 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005297 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#ifdef HAVE_TRY_EXCEPT
5299 }
5300 __except(EXCEPTION_EXECUTE_HANDLER)
5301 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005302 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304#endif
5305 if (s_hwnd == NULL)
5306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005307 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 mch_exit(2);
5309 }
5310 }
5311 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005312 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005313 // If the provided windowid is not valid reset it to zero, so that it
5314 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005315 if (IsWindow((HWND)win_socket_id) <= 0)
5316 win_socket_id = 0;
5317
Bram Moolenaar734a8672019-12-02 22:49:38 +01005318 // Create a window. If win_socket_id is not zero without border and
5319 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005320 s_hwnd = CreateWindowW(
5321 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005322 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5323 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005324 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5325 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005326 100, // Any value will do
5327 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005328 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005329 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005330 if (s_hwnd != NULL && win_socket_id != 0)
5331 {
5332 SetParent(s_hwnd, (HWND)win_socket_id);
5333 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5334 }
5335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 if (s_hwnd == NULL)
5338 return FAIL;
5339
5340#ifdef GLOBAL_IME
5341 global_ime_init(atom, s_hwnd);
5342#endif
5343#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5344 dyn_imm_load();
5345#endif
5346
Bram Moolenaar734a8672019-12-02 22:49:38 +01005347 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005348 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005349 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005350 wndclassw.style = CS_OWNDC;
5351 wndclassw.lpfnWndProc = _TextAreaWndProc;
5352 wndclassw.cbClsExtra = 0;
5353 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005354 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005355 wndclassw.hIcon = NULL;
5356 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5357 wndclassw.hbrBackground = NULL;
5358 wndclassw.lpszMenuName = NULL;
5359 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005360
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005361 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 return FAIL;
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005365 s_textArea = CreateWindowExW(
5366 0,
5367 szTextAreaClassW, L"Vim text area",
5368 WS_CHILD | WS_VISIBLE, 0, 0,
5369 100, // Any value will do for now
5370 100, // Any value will do for now
5371 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005372 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (s_textArea == NULL)
5375 return FAIL;
5376
Bram Moolenaar20321902016-02-17 12:30:17 +01005377#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005378 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005379 {
5380 HANDLE hIcon = NULL;
5381
5382 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005383 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005384 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005385#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005386
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#ifdef FEAT_MENU
5388 s_menuBar = CreateMenu();
5389#endif
5390 s_hdc = GetDC(s_textArea);
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaar734a8672019-12-02 22:49:38 +01005394 // Do we need to bother with this?
5395 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
Bram Moolenaar734a8672019-12-02 22:49:38 +01005397 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 gui_mch_def_colors();
5399
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5401 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 set_normal_colors();
5403
5404 /*
5405 * Check that none of the colors are the same as the background color.
5406 * Then store the current values as the defaults.
5407 */
5408 gui_check_colors();
5409 gui.def_norm_pixel = gui.norm_pixel;
5410 gui.def_back_pixel = gui.back_pixel;
5411
Bram Moolenaar734a8672019-12-02 22:49:38 +01005412 // Get the colors for the highlight groups (gui_check_colors() might have
5413 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 highlight_gui_started();
5415
5416 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005417 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005419 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
5421 /*
5422 * Set up for Intellimouse processing
5423 */
5424 init_mouse_wheel();
5425
5426 /*
5427 * compute a couple of metrics used for the dialogs
5428 */
5429 get_dialog_font_metrics();
5430#ifdef FEAT_TOOLBAR
5431 /*
5432 * Create the toolbar
5433 */
5434 initialise_toolbar();
5435#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005436#ifdef FEAT_GUI_TABLINE
5437 /*
5438 * Create the tabline
5439 */
5440 initialise_tabline();
5441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#ifdef MSWIN_FIND_REPLACE
5443 /*
5444 * Initialise the dialog box stuff
5445 */
5446 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5447
Bram Moolenaar734a8672019-12-02 22:49:38 +01005448 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005450 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005452 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5454 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5455 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005458#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005459# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005460// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005461# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005462# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005463# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005464# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005465 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005466 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005467#endif
5468
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005469#ifdef FEAT_RENDER_OPTIONS
5470 if (p_rop)
5471 (void)gui_mch_set_rendering_options(p_rop);
5472#endif
5473
Bram Moolenaar748bf032005-02-02 23:04:36 +00005474theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005475 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005476 display_errors();
5477
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return OK;
5479}
5480
5481/*
5482 * Get the size of the screen, taking position on multiple monitors into
5483 * account (if supported).
5484 */
5485 static void
5486get_work_area(RECT *spi_rect)
5487{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005488 HMONITOR mon;
5489 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar734a8672019-12-02 22:49:38 +01005491 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005492 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005493 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005495 moninfo.cbSize = sizeof(MONITORINFO);
5496 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005498 *spi_rect = moninfo.rcWork;
5499 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005502 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5504}
5505
5506/*
5507 * Set the size of the window to the given width and height in pixels.
5508 */
5509 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005510gui_mch_set_shellsize(
5511 int width,
5512 int height,
5513 int min_width UNUSED,
5514 int min_height UNUSED,
5515 int base_width UNUSED,
5516 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005517 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005520 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar734a8672019-12-02 22:49:38 +01005523 // Try to keep window completely on screen.
5524 // Get position of the screen work area. This is the part that is not
5525 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 get_work_area(&workarea_rect);
5527
Bram Moolenaar734a8672019-12-02 22:49:38 +01005528 // Resizing a maximized window looks very strange, unzoom it first.
5529 // But don't do it when still starting up, it may have been requested in
5530 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005531 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005533
5534 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar734a8672019-12-02 22:49:38 +01005536 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005537 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005538 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005539 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005540 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 + GetSystemMetrics(SM_CYCAPTION)
5542#ifdef FEAT_MENU
5543 + gui_mswin_get_menu_height(FALSE)
5544#endif
5545 ;
5546
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 // The following should take care of keeping Vim on the same monitor, no
5548 // matter if the secondary monitor is left or right of the primary
5549 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005550 window_rect.right = window_rect.left + win_width;
5551 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005552
Bram Moolenaar734a8672019-12-02 22:49:38 +01005553 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005554 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5555 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005557 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5558 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005560 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5561 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005563 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5564 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005566 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5567 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 SetActiveWindow(s_hwnd);
5570 SetFocus(s_hwnd);
5571
5572#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005573 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 gui_mswin_get_menu_height(!gui.starting);
5575#endif
5576}
5577
5578
5579 void
5580gui_mch_set_scrollbar_thumb(
5581 scrollbar_T *sb,
5582 long val,
5583 long size,
5584 long max)
5585{
5586 SCROLLINFO info;
5587
5588 sb->scroll_shift = 0;
5589 while (max > 32767)
5590 {
5591 max = (max + 1) >> 1;
5592 val >>= 1;
5593 size >>= 1;
5594 ++sb->scroll_shift;
5595 }
5596
5597 if (sb->scroll_shift > 0)
5598 ++size;
5599
5600 info.cbSize = sizeof(info);
5601 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5602 info.nPos = val;
5603 info.nMin = 0;
5604 info.nMax = max;
5605 info.nPage = size;
5606 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5607}
5608
5609
5610/*
5611 * Set the current text font.
5612 */
5613 void
5614gui_mch_set_font(GuiFont font)
5615{
5616 gui.currFont = font;
5617}
5618
5619
5620/*
5621 * Set the current text foreground color.
5622 */
5623 void
5624gui_mch_set_fg_color(guicolor_T color)
5625{
5626 gui.currFgColor = color;
5627}
5628
5629/*
5630 * Set the current text background color.
5631 */
5632 void
5633gui_mch_set_bg_color(guicolor_T color)
5634{
5635 gui.currBgColor = color;
5636}
5637
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005638/*
5639 * Set the current text special color.
5640 */
5641 void
5642gui_mch_set_sp_color(guicolor_T color)
5643{
5644 gui.currSpColor = color;
5645}
5646
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005647#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648/*
5649 * Multi-byte handling, originally by Sung-Hoon Baek.
5650 * First static functions (no prototypes generated).
5651 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005652# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005653# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005654# endif
5655# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 * handle WM_IME_NOTIFY message
5659 */
5660 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005661_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 LRESULT lResult = 0;
5664 HIMC hImc;
5665
5666 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5667 return lResult;
5668 switch (dwCommand)
5669 {
5670 case IMN_SETOPENSTATUS:
5671 if (pImmGetOpenStatus(hImc))
5672 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005673 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 im_set_position(gui.row, gui.col);
5675
Bram Moolenaar734a8672019-12-02 22:49:38 +01005676 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 State &= ~LANGMAP;
5678 if (State & INSERT)
5679 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005680# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005681 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5683 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005684 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 int old_row = gui.row;
5686 int old_col = gui.col;
5687
5688 // This must be called here before
5689 // status_redraw_curbuf(), otherwise the mode
5690 // message may appear in the wrong position.
5691 showmode();
5692 status_redraw_curbuf();
5693 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005694 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 gui.row = old_row;
5696 gui.col = old_col;
5697 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 }
5701 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005702 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 lResult = 0;
5704 break;
5705 }
5706 pImmReleaseContext(hWnd, hImc);
5707 return lResult;
5708}
5709
5710 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005711_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *ret;
5714 int len;
5715
Bram Moolenaar734a8672019-12-02 22:49:38 +01005716 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 return 0;
5718
5719 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5720 if (ret != NULL)
5721 {
5722 add_to_input_buf_csi(ret, len);
5723 vim_free(ret);
5724 return 1;
5725 }
5726 return 0;
5727}
5728
5729/*
5730 * get the current composition string, in UCS-2; *lenp is the number of
5731 * *lenp is the number of Unicode characters.
5732 */
5733 static short_u *
5734GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5735{
5736 LONG ret;
5737 LPWSTR wbuf = NULL;
5738 char_u *buf;
5739
5740 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005741 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
Bram Moolenaar734a8672019-12-02 22:49:38 +01005743 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5745 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005746 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 if (ret > 0)
5749 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005750 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005751 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (wbuf != NULL)
5753 {
5754 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5755 *lenp = ret / sizeof(WCHAR);
5756 }
5757 return (short_u *)wbuf;
5758 }
5759
Bram Moolenaar734a8672019-12-02 22:49:38 +01005760 // ret < 0; we got an error, so try the ANSI version. This'll work
5761 // on 9x/ME, but only if the codepage happens to be set to whatever
5762 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5764 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005765 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 buf = alloc(ret);
5768 if (buf == NULL)
5769 return NULL;
5770 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5771
Bram Moolenaar734a8672019-12-02 22:49:38 +01005772 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005773 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 vim_free(buf);
5775
5776 return (short_u *)wbuf;
5777}
5778
5779/*
5780 * void GetResultStr()
5781 *
5782 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5783 * get complete composition string
5784 */
5785 static char_u *
5786GetResultStr(HWND hwnd, int GCS, int *lenp)
5787{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005788 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 short_u *buf = NULL;
5790 char_u *convbuf = NULL;
5791
5792 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5793 return NULL;
5794
Bram Moolenaar734a8672019-12-02 22:49:38 +01005795 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5797 if (buf == NULL)
5798 return NULL;
5799
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005800 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 pImmReleaseContext(hwnd, hIMC);
5802 vim_free(buf);
5803 return convbuf;
5804}
5805#endif
5806
Bram Moolenaar734a8672019-12-02 22:49:38 +01005807// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005808#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810/*
5811 * set font to IM.
5812 */
5813 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005814im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 HIMC hImc;
5817
5818 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5819 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005820 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 pImmReleaseContext(s_hwnd, hImc);
5822 }
5823}
5824
5825/*
5826 * Notify cursor position to IM.
5827 */
5828 void
5829im_set_position(int row, int col)
5830{
5831 HIMC hImc;
5832
5833 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5834 {
5835 COMPOSITIONFORM cfs;
5836
5837 cfs.dwStyle = CFS_POINT;
5838 cfs.ptCurrentPos.x = FILL_X(col);
5839 cfs.ptCurrentPos.y = FILL_Y(row);
5840 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5841 pImmSetCompositionWindow(hImc, &cfs);
5842
5843 pImmReleaseContext(s_hwnd, hImc);
5844 }
5845}
5846
5847/*
5848 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5849 */
5850 void
5851im_set_active(int active)
5852{
5853 HIMC hImc;
5854 static HIMC hImcOld = (HIMC)0;
5855
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005856# ifdef VIMDLL
5857 if (!gui.in_use && !gui.starting)
5858 {
5859 mbyte_im_set_active(active);
5860 return;
5861 }
5862# endif
5863
Bram Moolenaar734a8672019-12-02 22:49:38 +01005864 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 {
5866 if (p_imdisable)
5867 {
5868 if (hImcOld == (HIMC)0)
5869 {
5870 hImcOld = pImmGetContext(s_hwnd);
5871 if (hImcOld)
5872 pImmAssociateContext(s_hwnd, (HIMC)0);
5873 }
5874 active = FALSE;
5875 }
5876 else if (hImcOld != (HIMC)0)
5877 {
5878 pImmAssociateContext(s_hwnd, hImcOld);
5879 hImcOld = (HIMC)0;
5880 }
5881
5882 hImc = pImmGetContext(s_hwnd);
5883 if (hImc)
5884 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005885 /*
5886 * for Korean ime
5887 */
5888 HKL hKL = GetKeyboardLayout(0);
5889
5890 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5891 {
5892 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5893 static BOOL bSaved = FALSE;
5894
5895 if (active)
5896 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005897 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005898 if (bSaved)
5899 pImmSetConversionStatus(hImc, dwConversionSaved,
5900 dwSentenceSaved);
5901 bSaved = FALSE;
5902 }
5903 else
5904 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005905 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005906 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5907 &dwSentenceSaved))
5908 {
5909 bSaved = TRUE;
5910 pImmSetConversionStatus(hImc,
5911 dwConversionSaved & ~(IME_CMODE_NATIVE
5912 | IME_CMODE_FULLSHAPE),
5913 dwSentenceSaved);
5914 }
5915 }
5916 }
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 pImmSetOpenStatus(hImc, active);
5919 pImmReleaseContext(s_hwnd, hImc);
5920 }
5921 }
5922}
5923
5924/*
5925 * Get IM status. When IM is on, return not 0. Else return 0.
5926 */
5927 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005928im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 int status = 0;
5931 HIMC hImc;
5932
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005933# ifdef VIMDLL
5934 if (!gui.in_use && !gui.starting)
5935 return mbyte_im_get_status();
5936# endif
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5939 {
5940 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5941 pImmReleaseContext(s_hwnd, hImc);
5942 }
5943 return status;
5944}
5945
Bram Moolenaar734a8672019-12-02 22:49:38 +01005946#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005948#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005949// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
5951/*
5952 * Notify cursor position to IM.
5953 */
5954 void
5955im_set_position(int row, int col)
5956{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005957 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 POINT p;
5959
5960 p.x = FILL_X(col);
5961 p.y = FILL_Y(row);
5962 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5963 global_ime_set_position(&p);
5964}
5965
5966/*
5967 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5968 */
5969 void
5970im_set_active(int active)
5971{
5972 global_ime_set_status(active);
5973}
5974
5975/*
5976 * Get IM status. When IM is on, return not 0. Else return 0.
5977 */
5978 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005979im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 return global_ime_get_status();
5982}
5983#endif
5984
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005985/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005986 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005987 */
5988 static void
5989latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5990{
5991 int c;
5992
Bram Moolenaarca003e12006-03-17 23:19:38 +00005993 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005994 {
5995 c = *text++;
5996 switch (c)
5997 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005998 case 0xa4: c = 0x20ac; break; // euro
5999 case 0xa6: c = 0x0160; break; // S hat
6000 case 0xa8: c = 0x0161; break; // S -hat
6001 case 0xb4: c = 0x017d; break; // Z hat
6002 case 0xb8: c = 0x017e; break; // Z -hat
6003 case 0xbc: c = 0x0152; break; // OE
6004 case 0xbd: c = 0x0153; break; // oe
6005 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006006 }
6007 *unicodebuf++ = c;
6008 }
6009}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
6011#ifdef FEAT_RIGHTLEFT
6012/*
6013 * What is this for? In the case where you are using Win98 or Win2K or later,
6014 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6015 * reverses the string sent to the TextOut... family. This sucks, because we
6016 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6017 * way to tell Windblows not to do this!
6018 *
6019 * The short of it is that this 'RevOut' only gets called if you are running
6020 * one of the new, "improved" MS OSes, and only if you are running in
6021 * 'rightleft' mode. It makes display take *slightly* longer, but not
6022 * noticeably so.
6023 */
6024 static void
6025RevOut( HDC s_hdc,
6026 int col,
6027 int row,
6028 UINT foptions,
6029 CONST RECT *pcliprect,
6030 LPCTSTR text,
6031 UINT len,
6032 CONST INT *padding)
6033{
6034 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006036 for (ix = 0; ix < (int)len; ++ix)
6037 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6038 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039}
6040#endif
6041
Bram Moolenaar92467d32017-12-05 13:22:16 +01006042 static void
6043draw_line(
6044 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006045 int y1,
6046 int x2,
6047 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006048 COLORREF color)
6049{
6050#if defined(FEAT_DIRECTX)
6051 if (IS_ENABLE_DIRECTX())
6052 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6053 else
6054#endif
6055 {
6056 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6057 HPEN old_pen = SelectObject(s_hdc, hpen);
6058 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006059 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006060 LineTo(s_hdc, x2, y2);
6061 DeleteObject(SelectObject(s_hdc, old_pen));
6062 }
6063}
6064
6065 static void
6066set_pixel(
6067 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006068 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006069 COLORREF color)
6070{
6071#if defined(FEAT_DIRECTX)
6072 if (IS_ENABLE_DIRECTX())
6073 DWriteContext_SetPixel(s_dwc, x, y, color);
6074 else
6075#endif
6076 SetPixel(s_hdc, x, y, color);
6077}
6078
6079 static void
6080fill_rect(
6081 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006082 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006083 COLORREF color)
6084{
6085#if defined(FEAT_DIRECTX)
6086 if (IS_ENABLE_DIRECTX())
6087 DWriteContext_FillRect(s_dwc, rcp, color);
6088 else
6089#endif
6090 {
6091 HBRUSH hbr2;
6092
6093 if (hbr == NULL)
6094 hbr2 = CreateSolidBrush(color);
6095 else
6096 hbr2 = hbr;
6097 FillRect(s_hdc, rcp, hbr2);
6098 if (hbr == NULL)
6099 DeleteBrush(hbr2);
6100 }
6101}
6102
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 void
6104gui_mch_draw_string(
6105 int row,
6106 int col,
6107 char_u *text,
6108 int len,
6109 int flags)
6110{
6111 static int *padding = NULL;
6112 static int pad_size = 0;
6113 int i;
6114 const RECT *pcliprect = NULL;
6115 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 static WCHAR *unicodebuf = NULL;
6117 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006118 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 int y;
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 /*
6123 * Italic and bold text seems to have an extra row of pixels at the bottom
6124 * (below where the bottom of the character should be). If we draw the
6125 * characters with a solid background, the top row of pixels in the
6126 * character below will be overwritten. We can fix this by filling in the
6127 * background ourselves, to the correct character proportions, and then
6128 * writing the character in transparent mode. Still have a problem when
6129 * the character is "_", which gets written on to the character below.
6130 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6131 * pixel in their slots, which fixes the problem with the bottom row of
6132 * pixels. We still need this code because otherwise the top row of pixels
6133 * becomes a problem. - webb.
6134 */
6135 static HBRUSH hbr_cache[2] = {NULL, NULL};
6136 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6137 static int brush_lru = 0;
6138 HBRUSH hbr;
6139 RECT rc;
6140
6141 if (!(flags & DRAW_TRANSP))
6142 {
6143 /*
6144 * Clear background first.
6145 * Note: FillRect() excludes right and bottom of rectangle.
6146 */
6147 rc.left = FILL_X(col);
6148 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (has_mbyte)
6150 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006151 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006152 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 }
6154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 rc.right = FILL_X(col + len);
6156 rc.bottom = FILL_Y(row + 1);
6157
Bram Moolenaar734a8672019-12-02 22:49:38 +01006158 // Cache the created brush, that saves a lot of time. We need two:
6159 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (gui.currBgColor == brush_color[0])
6161 {
6162 hbr = hbr_cache[0];
6163 brush_lru = 1;
6164 }
6165 else if (gui.currBgColor == brush_color[1])
6166 {
6167 hbr = hbr_cache[1];
6168 brush_lru = 0;
6169 }
6170 else
6171 {
6172 if (hbr_cache[brush_lru] != NULL)
6173 DeleteBrush(hbr_cache[brush_lru]);
6174 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6175 brush_color[brush_lru] = gui.currBgColor;
6176 hbr = hbr_cache[brush_lru];
6177 brush_lru = !brush_lru;
6178 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006179
Bram Moolenaar92467d32017-12-05 13:22:16 +01006180 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182 SetBkMode(s_hdc, TRANSPARENT);
6183
6184 /*
6185 * When drawing block cursor, prevent inverted character spilling
6186 * over character cell (can happen with bold/italic)
6187 */
6188 if (flags & DRAW_CURSOR)
6189 {
6190 pcliprect = &rc;
6191 foptions = ETO_CLIPPED;
6192 }
6193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 SetTextColor(s_hdc, gui.currFgColor);
6195 SelectFont(s_hdc, gui.currFont);
6196
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006197#ifdef FEAT_DIRECTX
6198 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006199 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006200#endif
6201
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6203 {
6204 vim_free(padding);
6205 pad_size = Columns;
6206
Bram Moolenaar734a8672019-12-02 22:49:38 +01006207 // Don't give an out-of-memory message here, it would call us
6208 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006209 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (padding != NULL)
6211 for (i = 0; i < pad_size; i++)
6212 padding[i] = gui.char_width;
6213 }
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 /*
6216 * We have to provide the padding argument because italic and bold versions
6217 * of fixed-width fonts are often one pixel or so wider than their normal
6218 * versions.
6219 * No check for DRAW_BOLD, Windows will have done it already.
6220 */
6221
Bram Moolenaar734a8672019-12-02 22:49:38 +01006222 // Check if there are any UTF-8 characters. If not, use normal text
6223 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (enc_utf8)
6225 for (n = 0; n < len; ++n)
6226 if (text[n] >= 0x80)
6227 break;
6228
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006229#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006230 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6231 // required that unicode drawing routine, currently. So this forces it
6232 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006233 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006234 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006235#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006236
Bram Moolenaar734a8672019-12-02 22:49:38 +01006237 // Check if the Unicode buffer exists and is big enough. Create it
6238 // with the same length as the multi-byte string, the number of wide
6239 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006240 if ((enc_utf8
6241 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6242 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 && (unicodebuf == NULL || len > unibuflen))
6244 {
6245 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006246 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
6248 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006249 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251 unibuflen = len;
6252 }
6253
6254 if (enc_utf8 && n < len && unicodebuf != NULL)
6255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006256 // Output UTF-8 characters. Composing characters should be
6257 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006258 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006259 int wlen; // string length in words
6260 int clen; // string length in characters
6261 int cells; // cell width of string up to composing char
6262 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006263 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006265 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006266 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006268 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006270 c = utf_ptr2char(text + i);
6271 if (c >= 0x10000)
6272 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006273 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006274 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6275 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006276 }
6277 else
6278 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006279 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006280 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006281
6282 if (utf_iscomposing(c))
6283 cw = 0;
6284 else
6285 {
6286 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006287 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006288 cw = 1;
6289 }
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (unicodepdy != NULL)
6292 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006293 // Use unicodepdy to make characters fit as we expect, even
6294 // when the font uses different widths (e.g., bold character
6295 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006296 if (c >= 0x10000)
6297 {
6298 unicodepdy[wlen - 2] = cw * gui.char_width;
6299 unicodepdy[wlen - 1] = 0;
6300 }
6301 else
6302 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006305 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006306 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006308#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006309 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006310 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006311 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006312 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006313 TEXT_X(col), TEXT_Y(row),
6314 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006315 gui.char_width, gui.currFgColor,
6316 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006317 }
6318 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006319#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006320 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6321 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006324 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006326 // If we want to display codepage data, and the current CP is not the
6327 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (unicodebuf != NULL)
6329 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006330 if (enc_latin9)
6331 latin9_to_ucs(text, len, unicodebuf);
6332 else
6333 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 MB_PRECOMPOSED,
6335 (char *)text, len,
6336 (LPWSTR)unicodebuf, unibuflen);
6337 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006338 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006339 // Use unicodepdy to make characters fit as we expect, even
6340 // when the font uses different widths (e.g., bold character
6341 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006342 if (unicodepdy != NULL)
6343 {
6344 int i;
6345 int cw;
6346
6347 for (i = 0; i < len; ++i)
6348 {
6349 cw = utf_char2cells(unicodebuf[i]);
6350 if (cw > 2)
6351 cw = 1;
6352 unicodepdy[i] = cw * gui.char_width;
6353 }
6354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006356 foptions, pcliprect, unicodebuf, len, unicodepdy);
6357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 }
6359 }
6360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 {
6362#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006363 // Windows will mess up RL text, so we have to draw it character by
6364 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006365 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6367 foptions, pcliprect, (char *)text, len, padding);
6368 else
6369#endif
6370 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6371 foptions, pcliprect, (char *)text, len, padding);
6372 }
6373
Bram Moolenaar734a8672019-12-02 22:49:38 +01006374 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 if (flags & DRAW_UNDERL)
6376 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006377 // When p_linespace is 0, overwrite the bottom row of pixels.
6378 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (p_linespace > 1)
6381 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006382 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006384
Bram Moolenaar734a8672019-12-02 22:49:38 +01006385 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006386 if (flags & DRAW_STRIKE)
6387 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006388 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006389 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006390 }
6391
Bram Moolenaar734a8672019-12-02 22:49:38 +01006392 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006393 if (flags & DRAW_UNDERC)
6394 {
6395 int x;
6396 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006397 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006398
6399 y = FILL_Y(row + 1) - 1;
6400 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6401 {
6402 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006403 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006404 }
6405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406}
6407
6408
6409/*
6410 * Output routines.
6411 */
6412
Bram Moolenaar734a8672019-12-02 22:49:38 +01006413/*
6414 * Flush any output to the screen
6415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 void
6417gui_mch_flush(void)
6418{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006419#if defined(FEAT_DIRECTX)
6420 if (IS_ENABLE_DIRECTX())
6421 DWriteContext_Flush(s_dwc);
6422#endif
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 GdiFlush();
6425}
6426
6427 static void
6428clear_rect(RECT *rcp)
6429{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006430 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
6433
Bram Moolenaarc716c302006-01-21 22:12:51 +00006434 void
6435gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6436{
6437 RECT workarea_rect;
6438
6439 get_work_area(&workarea_rect);
6440
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006441 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006442 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006443 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006444
Bram Moolenaar734a8672019-12-02 22:49:38 +01006445 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6446 // the menubar for MSwin, we subtract it from the screen height, so that
6447 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006448 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006449 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006450 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006451 - GetSystemMetrics(SM_CYCAPTION)
6452#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006453 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006454#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006455 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006456}
6457
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459#if defined(FEAT_MENU) || defined(PROTO)
6460/*
6461 * Add a sub menu to the menu bar.
6462 */
6463 void
6464gui_mch_add_menu(
6465 vimmenu_T *menu,
6466 int pos)
6467{
6468 vimmenu_T *parent = menu->parent;
6469
6470 menu->submenu_id = CreatePopupMenu();
6471 menu->id = s_menu_id++;
6472
6473 if (menu_is_menubar(menu->name))
6474 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006475 WCHAR *wn;
6476 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006478 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006479 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006480 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006482 infow.cbSize = sizeof(infow);
6483 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6484 | MIIM_SUBMENU;
6485 infow.dwItemData = (long_u)menu;
6486 infow.wID = menu->id;
6487 infow.fType = MFT_STRING;
6488 infow.dwTypeData = wn;
6489 infow.cch = (UINT)wcslen(wn);
6490 infow.hSubMenu = menu->submenu_id;
6491 InsertMenuItemW((parent == NULL)
6492 ? s_menuBar : parent->submenu_id,
6493 (UINT)pos, TRUE, &infow);
6494 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 }
6496
Bram Moolenaar734a8672019-12-02 22:49:38 +01006497 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (parent == NULL)
6499 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006500# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 else if (IsWindow(parent->tearoff_handle))
6502 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504}
6505
6506 void
6507gui_mch_show_popupmenu(vimmenu_T *menu)
6508{
6509 POINT mp;
6510
6511 (void)GetCursorPos((LPPOINT)&mp);
6512 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6513}
6514
6515 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006516gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 vimmenu_T *menu = gui_find_menu(path_name);
6519
6520 if (menu != NULL)
6521 {
6522 POINT p;
6523
Bram Moolenaar734a8672019-12-02 22:49:38 +01006524 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006526 if (mouse_pos)
6527 {
6528 int mx, my;
6529
6530 gui_mch_getmouse(&mx, &my);
6531 p.x += mx;
6532 p.y += my;
6533 }
6534 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006536 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6538 }
6539 msg_scroll = FALSE;
6540 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6541 }
6542}
6543
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006544# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545/*
6546 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6547 * create it as a pseudo-"tearoff menu".
6548 */
6549 void
6550gui_make_tearoff(char_u *path_name)
6551{
6552 vimmenu_T *menu = gui_find_menu(path_name);
6553
Bram Moolenaar734a8672019-12-02 22:49:38 +01006554 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (menu != NULL)
6556 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6557}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
6560/*
6561 * Add a menu item to a menu
6562 */
6563 void
6564gui_mch_add_menu_item(
6565 vimmenu_T *menu,
6566 int idx)
6567{
6568 vimmenu_T *parent = menu->parent;
6569
6570 menu->id = s_menu_id++;
6571 menu->submenu_id = NULL;
6572
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006573# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6575 {
6576 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6577 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6578 }
6579 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006580# endif
6581# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 if (menu_is_toolbar(parent->name))
6583 {
6584 TBBUTTON newtb;
6585
Bram Moolenaara80faa82020-04-12 19:37:17 +02006586 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 if (menu_is_separator(menu->name))
6588 {
6589 newtb.iBitmap = 0;
6590 newtb.fsStyle = TBSTYLE_SEP;
6591 }
6592 else
6593 {
6594 newtb.iBitmap = get_toolbar_bitmap(menu);
6595 newtb.fsStyle = TBSTYLE_BUTTON;
6596 }
6597 newtb.idCommand = menu->id;
6598 newtb.fsState = TBSTATE_ENABLED;
6599 newtb.iString = 0;
6600 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6601 (LPARAM)&newtb);
6602 menu->submenu_id = (HMENU)-1;
6603 }
6604 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006607 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006609 wn = enc_to_utf16(menu->name, NULL);
6610 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006612 InsertMenuW(parent->submenu_id, (UINT)idx,
6613 (menu_is_separator(menu->name)
6614 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6615 (UINT)menu->id, wn);
6616 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006618# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (IsWindow(parent->tearoff_handle))
6620 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623}
6624
6625/*
6626 * Destroy the machine specific menu widget.
6627 */
6628 void
6629gui_mch_destroy_menu(vimmenu_T *menu)
6630{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006631# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 /*
6633 * is this a toolbar button?
6634 */
6635 if (menu->submenu_id == (HMENU)-1)
6636 {
6637 int iButton;
6638
6639 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6640 (WPARAM)menu->id, 0);
6641 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6642 }
6643 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
6646 if (menu->parent != NULL
6647 && menu_is_popup(menu->parent->dname)
6648 && menu->parent->submenu_id != NULL)
6649 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6650 else
6651 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6652 if (menu->submenu_id != NULL)
6653 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006654# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (IsWindow(menu->tearoff_handle))
6656 DestroyWindow(menu->tearoff_handle);
6657 if (menu->parent != NULL
6658 && menu->parent->children != NULL
6659 && IsWindow(menu->parent->tearoff_handle))
6660 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006661 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 menu->modes = 0;
6663 rebuild_tearoff(menu->parent);
6664 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667}
6668
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006669# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 static void
6671rebuild_tearoff(vimmenu_T *menu)
6672{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006673 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 char_u tbuf[128];
6675 RECT trect;
6676 RECT rct;
6677 RECT roct;
6678 int x, y;
6679
6680 HWND thwnd = menu->tearoff_handle;
6681
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006682 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (GetWindowRect(thwnd, &trect)
6684 && GetWindowRect(s_hwnd, &rct)
6685 && GetClientRect(s_hwnd, &roct))
6686 {
6687 x = trect.left - rct.left;
6688 y = (trect.top - rct.bottom + roct.bottom);
6689 }
6690 else
6691 {
6692 x = y = 0xffffL;
6693 }
6694 DestroyWindow(thwnd);
6695 if (menu->children != NULL)
6696 {
6697 gui_mch_tearoff(tbuf, menu, x, y);
6698 if (IsWindow(menu->tearoff_handle))
6699 (void) SetWindowPos(menu->tearoff_handle,
6700 NULL,
6701 (int)trect.left,
6702 (int)trect.top,
6703 0, 0,
6704 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6705 }
6706}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006707# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709/*
6710 * Make a menu either grey or not grey.
6711 */
6712 void
6713gui_mch_menu_grey(
6714 vimmenu_T *menu,
6715 int grey)
6716{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006717# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 /*
6719 * is this a toolbar button?
6720 */
6721 if (menu->submenu_id == (HMENU)-1)
6722 {
6723 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6724 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6725 }
6726 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006727# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006728 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6729 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006731# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6733 {
6734 WORD menuID;
6735 HWND menuHandle;
6736
6737 /*
6738 * A tearoff button has changed state.
6739 */
6740 if (menu->children == NULL)
6741 menuID = (WORD)(menu->id);
6742 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006743 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6745 if (menuHandle)
6746 EnableWindow(menuHandle, !grey);
6747
6748 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750}
6751
Bram Moolenaar734a8672019-12-02 22:49:38 +01006752#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
6754
Bram Moolenaar734a8672019-12-02 22:49:38 +01006755// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6758#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006759#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
6761#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6762/*
6763 * stuff for dialogs
6764 */
6765
6766/*
6767 * The callback routine used by all the dialogs. Very simple. First,
6768 * acknowledges the INITDIALOG message so that Windows knows to do standard
6769 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6770 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6771 * number.
6772 */
6773 static LRESULT CALLBACK
6774dialog_callback(
6775 HWND hwnd,
6776 UINT message,
6777 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006778 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 if (message == WM_INITDIALOG)
6781 {
6782 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006783 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 (void)SetFocus(hwnd);
6785 if (dialog_default_button > IDCANCEL)
6786 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006787 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006788 // We don't have a default, set focus on another element of the
6789 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006790 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 return FALSE;
6792 }
6793
6794 if (message == WM_COMMAND)
6795 {
6796 int button = LOWORD(wParam);
6797
Bram Moolenaar734a8672019-12-02 22:49:38 +01006798 // Don't end the dialog if something was selected that was
6799 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (button >= DLG_NONBUTTON_CONTROL)
6801 return TRUE;
6802
Bram Moolenaar734a8672019-12-02 22:49:38 +01006803 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006805 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006806 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006807 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006808
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006809 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6810 p = utf16_to_enc(wp, NULL);
6811 vim_strncpy(s_textfield, p, IOSIZE);
6812 vim_free(p);
6813 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
6816 /*
6817 * Need to check for IDOK because if the user just hits Return to
6818 * accept the default value, some reason this is what we get.
6819 */
6820 if (button == IDOK)
6821 {
6822 if (dialog_default_button > IDCANCEL)
6823 EndDialog(hwnd, dialog_default_button);
6824 }
6825 else
6826 EndDialog(hwnd, button - IDCANCEL);
6827 return TRUE;
6828 }
6829
6830 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6831 {
6832 EndDialog(hwnd, 0);
6833 return TRUE;
6834 }
6835 return FALSE;
6836}
6837
6838/*
6839 * Create a dialog dynamically from the parameter strings.
6840 * type = type of dialog (question, alert, etc.)
6841 * title = dialog title. may be NULL for default title.
6842 * message = text to display. Dialog sizes to accommodate it.
6843 * buttons = '\n' separated list of button captions, default first.
6844 * dfltbutton = number of default button.
6845 *
6846 * This routine returns 1 if the first button is pressed,
6847 * 2 for the second, etc.
6848 *
6849 * 0 indicates Esc was pressed.
6850 * -1 for unexpected error
6851 *
6852 * If stubbing out this fn, return 1.
6853 */
6854
Bram Moolenaar734a8672019-12-02 22:49:38 +01006855static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 "IDR_VIM",
6858 "IDR_VIM_ERROR",
6859 "IDR_VIM_ALERT",
6860 "IDR_VIM_INFO",
6861 "IDR_VIM_QUESTION"
6862};
6863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 int
6865gui_mch_dialog(
6866 int type,
6867 char_u *title,
6868 char_u *message,
6869 char_u *buttons,
6870 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006871 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006872 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006875 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 int numButtons;
6877 int *buttonWidths, *buttonPositions;
6878 int buttonYpos;
6879 int nchar, i;
6880 DWORD lStyle;
6881 int dlgwidth = 0;
6882 int dlgheight;
6883 int editboxheight;
6884 int horizWidth = 0;
6885 int msgheight;
6886 char_u *pstart;
6887 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006888 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 char_u *tbuffer;
6890 RECT rect;
6891 HWND hwnd;
6892 HDC hdc;
6893 HFONT font, oldFont;
6894 TEXTMETRIC fontInfo;
6895 int fontHeight;
6896 int textWidth, minButtonWidth, messageWidth;
6897 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006898 int maxDialogHeight;
6899 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 int vertical;
6901 int dlgPaddingX;
6902 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006903# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006904 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006906# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006907 garray_T ga;
6908 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006910# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006912# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006913 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006914# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006915 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006916 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar748bf032005-02-02 23:04:36 +00006919 if (s_hwnd == NULL)
6920 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 if ((type < 0) || (type > VIM_LAST_TYPE))
6923 type = 0;
6924
Bram Moolenaar734a8672019-12-02 22:49:38 +01006925 // allocate some memory for dialog template
6926 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006928 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 if (p == NULL)
6931 return -1;
6932
6933 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006934 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6936 * const.
6937 */
6938 tbuffer = vim_strsave(buttons);
6939 if (tbuffer == NULL)
6940 return -1;
6941
Bram Moolenaar734a8672019-12-02 22:49:38 +01006942 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 numButtons = 1;
6946 for (i = 0; tbuffer[i] != '\0'; i++)
6947 {
6948 if (tbuffer[i] == DLG_BUTTON_SEP)
6949 numButtons++;
6950 }
6951 if (dfltbutton >= numButtons)
6952 dfltbutton = -1;
6953
Bram Moolenaar734a8672019-12-02 22:49:38 +01006954 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006955 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (buttonWidths == NULL)
6957 return -1;
6958
Bram Moolenaar734a8672019-12-02 22:49:38 +01006959 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006960 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (buttonPositions == NULL)
6962 return -1;
6963
6964 /*
6965 * Calculate how big the dialog must be.
6966 */
6967 hwnd = GetDesktopWindow();
6968 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006969# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6971 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006972 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 use_lfSysmenu = TRUE;
6974 }
6975 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6978 VARIABLE_PITCH , DLG_FONT_NAME);
6979 if (s_usenewlook)
6980 {
6981 oldFont = SelectFont(hdc, font);
6982 dlgPaddingX = DLG_PADDING_X;
6983 dlgPaddingY = DLG_PADDING_Y;
6984 }
6985 else
6986 {
6987 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6988 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6989 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6990 }
6991 GetTextMetrics(hdc, &fontInfo);
6992 fontHeight = fontInfo.tmHeight;
6993
Bram Moolenaar734a8672019-12-02 22:49:38 +01006994 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006995 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998 if (s_hwnd == NULL)
6999 {
7000 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
Bram Moolenaar734a8672019-12-02 22:49:38 +01007002 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007003 get_work_area(&workarea_rect);
7004 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7005 if (maxDialogWidth > 600)
7006 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007007 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007008 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007009 }
7010 else
7011 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007012 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007013 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007014 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007015 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007016 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007017 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7018 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007019
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007020 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007021 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007022 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007023 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007024 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7025 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7026 }
7027
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028 // Set dlgwidth to width of message.
7029 // Copy the message into "ga", changing NL to CR-NL and inserting line
7030 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 pstart = message;
7032 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007033 msgheight = 0;
7034 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 do
7036 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007037 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007038
Bram Moolenaar734a8672019-12-02 22:49:38 +01007039 // Need to figure out where to break the string. The system does it
7040 // at a word boundary, which would mean we can't compute the number of
7041 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007042 textWidth = 0;
7043 last_white = NULL;
7044 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007045 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007046 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007047 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007048 && textWidth > maxDialogWidth * 3 / 4)
7049 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007050 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007051 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007052 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007053 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007054 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007055 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007056 textWidth = 0;
7057
7058 if (last_white != NULL)
7059 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007060 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007061 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007062 pend = last_white + 1;
7063 last_white = NULL;
7064 }
7065 ga_append(&ga, '\r');
7066 ga_append(&ga, '\n');
7067 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007068 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007069
7070 while (--l >= 0)
7071 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007072 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007073 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007075
7076 ga_append(&ga, '\r');
7077 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 pstart = pend + 1;
7079 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007080
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007081 if (ga.ga_data != NULL)
7082 message = ga.ga_data;
7083
Bram Moolenaar734a8672019-12-02 22:49:38 +01007084 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007085
Bram Moolenaar734a8672019-12-02 22:49:38 +01007086 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02007087 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7088 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
7090 if (msgheight < DLG_ICON_HEIGHT)
7091 msgheight = DLG_ICON_HEIGHT;
7092
7093 /*
7094 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007095 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007097 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 if (!vertical)
7099 {
7100 // Place buttons horizontally if they fit.
7101 horizWidth = dlgPaddingX;
7102 pstart = tbuffer;
7103 i = 0;
7104 do
7105 {
7106 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7107 if (pend == NULL)
7108 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007109 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (textWidth < minButtonWidth)
7111 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007112 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 buttonWidths[i] = textWidth;
7114 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007115 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 pstart = pend + 1;
7117 } while (*pend != NUL);
7118
7119 if (horizWidth > maxDialogWidth)
7120 vertical = TRUE; // Too wide to fit on the screen.
7121 else if (horizWidth > dlgwidth)
7122 dlgwidth = horizWidth;
7123 }
7124
7125 if (vertical)
7126 {
7127 // Stack buttons vertically.
7128 pstart = tbuffer;
7129 do
7130 {
7131 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7132 if (pend == NULL)
7133 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007134 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007135 textWidth += dlgPaddingX; // Padding within button
7136 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (textWidth > dlgwidth)
7138 dlgwidth = textWidth;
7139 pstart = pend + 1;
7140 } while (*pend != NUL);
7141 }
7142
7143 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007144 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaar734a8672019-12-02 22:49:38 +01007146 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (s_usenewlook)
7148 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7149 else
7150 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7151
7152 add_long(lStyle);
7153 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007154 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 add_word(0); // NumberOfItems(will change later)
7156 add_word(10); // x
7157 add_word(10); // y
7158 add_word(PixelToDialogX(dlgwidth)); // cx
7159
7160 // Dialog height.
7161 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007162 dlgheight = msgheight + 2 * dlgPaddingY
7163 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 else
7165 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7166
7167 // Dialog needs to be taller if contains an edit box.
7168 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7169 if (textfield != NULL)
7170 dlgheight += editboxheight;
7171
Bram Moolenaar734a8672019-12-02 22:49:38 +01007172 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007173 if (dlgheight > maxDialogHeight)
7174 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007175 msgheight = msgheight - (dlgheight - maxDialogHeight);
7176 dlgheight = maxDialogHeight;
7177 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007178 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007179 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007180 }
7181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 add_word(PixelToDialogY(dlgheight));
7183
7184 add_word(0); // Menu
7185 add_word(0); // Class
7186
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007188 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7189 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 p += nchar;
7191
7192 if (s_usenewlook)
7193 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007194 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007195# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (use_lfSysmenu)
7197 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7200 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007201 wcscpy(p, lfSysmenu.lfFaceName);
7202 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
7204 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
7207 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007208 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
7210 p += nchar;
7211 }
7212
7213 buttonYpos = msgheight + 2 * dlgPaddingY;
7214
7215 if (textfield != NULL)
7216 buttonYpos += editboxheight;
7217
7218 pstart = tbuffer;
7219 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007220 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 for (i = 0; i < numButtons; i++)
7222 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007223 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 for ( pend = pstart;
7225 *pend && (*pend != DLG_BUTTON_SEP);
7226 pend++)
7227 ;
7228
7229 if (*pend)
7230 *pend = '\0';
7231
7232 /*
7233 * old NOTE:
7234 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7235 * the focus to the first tab-able button and in so doing makes that
7236 * the default!! Grrr. Workaround: Make the default button the only
7237 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7238 * he/she can use arrow keys.
7239 *
7240 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007241 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 * dialog. Also needed for when the textfield is the default control.
7243 * It appears to work now (perhaps not on Win95?).
7244 */
7245 if (vertical)
7246 {
7247 p = add_dialog_element(p,
7248 (i == dfltbutton
7249 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7250 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007251 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 + 2 * fontHeight * i),
7253 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7254 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007255 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 else
7258 {
7259 p = add_dialog_element(p,
7260 (i == dfltbutton
7261 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7262 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007263 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 PixelToDialogX(buttonWidths[i]),
7265 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007266 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007268 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 *pnumitems += numButtons;
7271
Bram Moolenaar734a8672019-12-02 22:49:38 +01007272 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 p = add_dialog_element(p, SS_ICON,
7274 PixelToDialogX(dlgPaddingX),
7275 PixelToDialogY(dlgPaddingY),
7276 PixelToDialogX(DLG_ICON_WIDTH),
7277 PixelToDialogY(DLG_ICON_HEIGHT),
7278 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7279 dlg_icons[type]);
7280
Bram Moolenaar734a8672019-12-02 22:49:38 +01007281 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007282 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7283 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7284 PixelToDialogY(dlgPaddingY),
7285 (WORD)(PixelToDialogX(messageWidth) + 1),
7286 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007287 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288
Bram Moolenaar734a8672019-12-02 22:49:38 +01007289 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 if (textfield != NULL)
7291 {
7292 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7293 PixelToDialogX(2 * dlgPaddingX),
7294 PixelToDialogY(2 * dlgPaddingY + msgheight),
7295 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7296 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007297 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 *pnumitems += 1;
7299 }
7300
7301 *pnumitems += 2;
7302
7303 SelectFont(hdc, oldFont);
7304 DeleteObject(font);
7305 ReleaseDC(hwnd, hdc);
7306
Bram Moolenaar734a8672019-12-02 22:49:38 +01007307 // Let the dialog_callback() function know which button to make default
7308 // If we have an edit box, make that the default. We also need to tell
7309 // dialog_callback() if this dialog contains an edit box or not. We do
7310 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 if (textfield != NULL)
7312 {
7313 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7314 s_textfield = textfield;
7315 }
7316 else
7317 {
7318 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7319 s_textfield = NULL;
7320 }
7321
Bram Moolenaar734a8672019-12-02 22:49:38 +01007322 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007324 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 (LPDLGTEMPLATE)pdlgtemplate,
7326 s_hwnd,
7327 (DLGPROC)dialog_callback);
7328
7329 LocalFree(LocalHandle(pdlgtemplate));
7330 vim_free(tbuffer);
7331 vim_free(buttonWidths);
7332 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007333 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
Bram Moolenaar734a8672019-12-02 22:49:38 +01007335 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 (void)SetFocus(s_hwnd);
7337
7338 return nchar;
7339}
7340
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343/*
7344 * Put a simple element (basic class) onto a dialog template in memory.
7345 * return a pointer to where the next item should be added.
7346 *
7347 * parameters:
7348 * lStyle = additional style flags
7349 * (be careful, NT3.51 & Win32s will ignore the new ones)
7350 * x,y = x & y positions IN DIALOG UNITS
7351 * w,h = width and height IN DIALOG UNITS
7352 * Id = ID used in messages
7353 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7354 * caption = usually text or resource name
7355 *
7356 * TODO: use the length information noted here to enable the dialog creation
7357 * routines to work out more exactly how much memory they need to alloc.
7358 */
7359 static PWORD
7360add_dialog_element(
7361 PWORD p,
7362 DWORD lStyle,
7363 WORD x,
7364 WORD y,
7365 WORD w,
7366 WORD h,
7367 WORD Id,
7368 WORD clss,
7369 const char *caption)
7370{
7371 int nchar;
7372
Bram Moolenaar734a8672019-12-02 22:49:38 +01007373 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7375 *p++ = LOWORD(lStyle);
7376 *p++ = HIWORD(lStyle);
7377 *p++ = 0; // LOWORD (lExtendedStyle)
7378 *p++ = 0; // HIWORD (lExtendedStyle)
7379 *p++ = x;
7380 *p++ = y;
7381 *p++ = w;
7382 *p++ = h;
7383 *p++ = Id; //9 or 10 words in all
7384
7385 *p++ = (WORD)0xffff;
7386 *p++ = clss; //2 more here
7387
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007388 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 p += nchar;
7390
7391 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7392
7393 return p; //total = 15+ (strlen(caption)) words
7394 // = 30 + 2(strlen(caption) bytes reqd
7395}
7396
7397
7398/*
7399 * Helper routine. Take an input pointer, return closest pointer that is
7400 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7401 */
7402 static LPWORD
7403lpwAlign(
7404 LPWORD lpIn)
7405{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007406 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007408 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 ul += 3;
7410 ul >>= 2;
7411 ul <<= 2;
7412 return (LPWORD)ul;
7413}
7414
7415/*
7416 * Helper routine. Takes second parameter as Ansi string, copies it to first
7417 * parameter as wide character (16-bits / char) string, and returns integer
7418 * number of wide characters (words) in string (including the trailing wide
7419 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007420 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7421 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 static int
7423nCopyAnsiToWideChar(
7424 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007425 LPSTR lpAnsiIn,
7426 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007429 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 int i;
7431 WCHAR *wn;
7432
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007433 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007435 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007436 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (wn != NULL)
7438 {
7439 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007440 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 vim_free(wn);
7442 }
7443 }
7444 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007445 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 nChar = MultiByteToWideChar(
7447 enc_codepage > 0 ? enc_codepage : CP_ACP,
7448 MB_PRECOMPOSED,
7449 lpAnsiIn, len,
7450 lpWCStr, len);
7451 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007452 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454
7455 return nChar;
7456}
7457
7458
7459#ifdef FEAT_TEAROFF
7460/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007461 * Lookup menu handle from "menu_id".
7462 */
7463 static HMENU
7464tearoff_lookup_menuhandle(
7465 vimmenu_T *menu,
7466 WORD menu_id)
7467{
7468 for ( ; menu != NULL; menu = menu->next)
7469 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007471 continue;
7472 if (menu_is_separator(menu->dname))
7473 continue;
7474 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7475 return menu->submenu_id;
7476 }
7477 return NULL;
7478}
7479
7480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 * The callback function for all the modeless dialogs that make up the
7482 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7483 * thinking its menus have been clicked), and go away when closed.
7484 */
7485 static LRESULT CALLBACK
7486tearoff_callback(
7487 HWND hwnd,
7488 UINT message,
7489 WPARAM wParam,
7490 LPARAM lParam)
7491{
7492 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007493 {
7494 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497
Bram Moolenaar734a8672019-12-02 22:49:38 +01007498 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 HandleMouseHide(message, lParam);
7500
7501 if (message == WM_COMMAND)
7502 {
7503 if ((WORD)(LOWORD(wParam)) & 0x8000)
7504 {
7505 POINT mp;
7506 RECT rect;
7507
7508 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7509 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007510 vimmenu_T *menu;
7511
7512 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007514 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7516 (int)rect.right - 8,
7517 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007518 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 s_hwnd,
7520 NULL);
7521 /*
7522 * NOTE: The pop-up menu can eat the mouse up event.
7523 * We deal with this in normal.c.
7524 */
7525 }
7526 }
7527 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007528 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7530 /*
7531 * Give main window the focus back: this is so after
7532 * choosing a tearoff button you can start typing again
7533 * straight away.
7534 */
7535 (void)SetFocus(s_hwnd);
7536 return TRUE;
7537 }
7538 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7539 {
7540 DestroyWindow(hwnd);
7541 return TRUE;
7542 }
7543
Bram Moolenaar734a8672019-12-02 22:49:38 +01007544 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 if (message == WM_EXITSIZEMOVE)
7546 (void)SetActiveWindow(s_hwnd);
7547
7548 return FALSE;
7549}
7550#endif
7551
7552
7553/*
7554 * Decide whether to use the "new look" (small, non-bold font) or the "old
7555 * look" (big, clanky font) for dialogs, and work out a few values for use
7556 * later accordingly.
7557 */
7558 static void
7559get_dialog_font_metrics(void)
7560{
7561 HDC hdc;
7562 HFONT hfontTools = 0;
7563 DWORD dlgFontSize;
7564 SIZE size;
7565#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007566 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#endif
7568
7569 s_usenewlook = FALSE;
7570
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007572 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007573 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575#endif
7576 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7577 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7578
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007579 if (hfontTools)
7580 {
7581 hdc = GetDC(s_hwnd);
7582 SelectObject(hdc, hfontTools);
7583 /*
7584 * GetTextMetrics() doesn't return the right value in
7585 * tmAveCharWidth, so we have to figure out the dialog base units
7586 * ourselves.
7587 */
7588 GetTextExtentPoint(hdc,
7589 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7590 52, &size);
7591 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007593 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7594 s_dlgfntheight = (WORD)size.cy;
7595 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597
7598 if (!s_usenewlook)
7599 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007600 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 s_dlgfntwidth = LOWORD(dlgFontSize);
7602 s_dlgfntheight = HIWORD(dlgFontSize);
7603 }
7604}
7605
7606#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7607/*
7608 * Create a pseudo-"tearoff menu" based on the child
7609 * items of a given menu pointer.
7610 */
7611 static void
7612gui_mch_tearoff(
7613 char_u *title,
7614 vimmenu_T *menu,
7615 int initX,
7616 int initY)
7617{
7618 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7619 int template_len;
7620 int nchar, textWidth, submenuWidth;
7621 DWORD lStyle;
7622 DWORD lExtendedStyle;
7623 WORD dlgwidth;
7624 WORD menuID;
7625 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007626 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 vimmenu_T *the_menu = menu;
7628 HWND hwnd;
7629 HDC hdc;
7630 HFONT font, oldFont;
7631 int col, spaceWidth, len;
7632 int columnWidths[2];
7633 char_u *label, *text;
7634 int acLen = 0;
7635 int nameLen;
7636 int padding0, padding1, padding2 = 0;
7637 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007638 int x;
7639 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007640# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007641 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
7645 /*
7646 * If this menu is already torn off, move it to the mouse position.
7647 */
7648 if (IsWindow(menu->tearoff_handle))
7649 {
7650 POINT mp;
7651 if (GetCursorPos((LPPOINT)&mp))
7652 {
7653 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7654 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7655 }
7656 return;
7657 }
7658
7659 /*
7660 * Create a new tearoff.
7661 */
7662 if (*title == MNU_HIDDEN_CHAR)
7663 title++;
7664
Bram Moolenaar734a8672019-12-02 22:49:38 +01007665 // Allocate memory to store the dialog template. It's made bigger when
7666 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 template_len = DLG_ALLOC_SIZE;
7668 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7669 if (p == NULL)
7670 return;
7671
7672 hwnd = GetDesktopWindow();
7673 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007674# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7676 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007677 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 use_lfSysmenu = TRUE;
7679 }
7680 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7683 VARIABLE_PITCH , DLG_FONT_NAME);
7684 if (s_usenewlook)
7685 oldFont = SelectFont(hdc, font);
7686 else
7687 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7688
Bram Moolenaar734a8672019-12-02 22:49:38 +01007689 // Calculate width of a single space. Used for padding columns to the
7690 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007691 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692
Bram Moolenaar734a8672019-12-02 22:49:38 +01007693 // Figure out max width of the text column, the accelerator column and the
7694 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 submenuWidth = 0;
7696 for (col = 0; col < 2; col++)
7697 {
7698 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007699 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007701 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 text = (col == 0) ? pmenu->dname : pmenu->actext;
7703 if (text != NULL && *text != NUL)
7704 {
7705 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7706 if (textWidth > columnWidths[col])
7707 columnWidths[col] = textWidth;
7708 }
7709 if (pmenu->children != NULL)
7710 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7711 }
7712 }
7713 if (columnWidths[1] == 0)
7714 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007715 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 if (submenuWidth != 0)
7717 columnWidths[0] += submenuWidth;
7718 else
7719 columnWidths[0] += spaceWidth;
7720 }
7721 else
7722 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007723 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7725 columnWidths[1] += submenuWidth;
7726 }
7727
7728 /*
7729 * Now find the total width of our 'menu'.
7730 */
7731 textWidth = columnWidths[0] + columnWidths[1];
7732 if (submenuWidth != 0)
7733 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007734 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7736 textWidth += submenuWidth;
7737 }
7738 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7739 if (textWidth > dlgwidth)
7740 dlgwidth = textWidth;
7741 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7742
Bram Moolenaar734a8672019-12-02 22:49:38 +01007743 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 if (s_usenewlook)
7745 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7746 else
7747 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7748
7749 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7750 *p++ = LOWORD(lStyle);
7751 *p++ = HIWORD(lStyle);
7752 *p++ = LOWORD(lExtendedStyle);
7753 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007754 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007756 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007758 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 else
7760 *p++ = PixelToDialogX(initX); // x
7761 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007762 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 else
7764 *p++ = PixelToDialogY(initY); // y
7765 *p++ = PixelToDialogX(dlgwidth); // cx
7766 ptrueheight = p;
7767 *p++ = 0; // dialog height: changed later anyway
7768 *p++ = 0; // Menu
7769 *p++ = 0; // Class
7770
Bram Moolenaar734a8672019-12-02 22:49:38 +01007771 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007773 ? (LPSTR)title
7774 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 p += nchar;
7776
7777 if (s_usenewlook)
7778 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007779 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007780# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (use_lfSysmenu)
7782 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007783 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7785 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007786 wcscpy(p, lfSysmenu.lfFaceName);
7787 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {
7792 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007793 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 }
7795 p += nchar;
7796 }
7797
7798 /*
7799 * Loop over all the items in the menu.
7800 * But skip over the tearbar.
7801 */
7802 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7803 menu = menu->children->next;
7804 else
7805 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007806 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 for ( ; menu != NULL; menu = menu->next)
7808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007809 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 continue;
7811 if (menu_is_separator(menu->dname))
7812 {
7813 sepPadding += 3;
7814 continue;
7815 }
7816
Bram Moolenaar734a8672019-12-02 22:49:38 +01007817 // Check if there still is plenty of room in the template. Make it
7818 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7820 {
7821 WORD *newp;
7822
7823 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7824 if (newp != NULL)
7825 {
7826 template_len += 4096;
7827 mch_memmove(newp, pdlgtemplate,
7828 (char *)p - (char *)pdlgtemplate);
7829 p = newp + (p - pdlgtemplate);
7830 pnumitems = newp + (pnumitems - pdlgtemplate);
7831 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7832 LocalFree(LocalHandle(pdlgtemplate));
7833 pdlgtemplate = newp;
7834 }
7835 }
7836
Bram Moolenaar734a8672019-12-02 22:49:38 +01007837 // Figure out minimal length of this menu label. Use "name" for the
7838 // actual text, "dname" for estimating the displayed size. "name"
7839 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 len = nameLen = (int)STRLEN(menu->name);
7841 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7842 (int)STRLEN(menu->dname))) / spaceWidth;
7843 len += padding0;
7844
7845 if (menu->actext != NULL)
7846 {
7847 acLen = (int)STRLEN(menu->actext);
7848 len += acLen;
7849 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7850 }
7851 else
7852 textWidth = 0;
7853 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7854 len += padding1;
7855
7856 if (menu->children == NULL)
7857 {
7858 padding2 = submenuWidth / spaceWidth;
7859 len += padding2;
7860 menuID = (WORD)(menu->id);
7861 }
7862 else
7863 {
7864 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007865 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 }
7867
Bram Moolenaar734a8672019-12-02 22:49:38 +01007868 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007869 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (label == NULL)
7871 break;
7872
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007873 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007874 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007876 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 while (padding0-- > 0)
7878 *text++ = ' ';
7879 if (menu->actext != NULL)
7880 {
7881 STRNCPY(text, menu->actext, acLen);
7882 text += acLen;
7883 }
7884 while (padding1-- > 0)
7885 *text++ = ' ';
7886 if (menu->children != NULL)
7887 {
7888 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7889 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7890 }
7891 else
7892 {
7893 while (padding2-- > 0)
7894 *text++ = ' ';
7895 }
7896 *text = NUL;
7897
7898 /*
7899 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7900 * W95/NT4 it makes the tear-off look more like a menu.
7901 */
7902 p = add_dialog_element(p,
7903 BS_PUSHBUTTON|BS_LEFT,
7904 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7905 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7906 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7907 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007908 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 vim_free(label);
7910 (*pnumitems)++;
7911 }
7912
7913 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7914
7915
Bram Moolenaar734a8672019-12-02 22:49:38 +01007916 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007917 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007918 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 (LPDLGTEMPLATE)pdlgtemplate,
7920 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007921 (DLGPROC)tearoff_callback,
7922 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924 LocalFree(LocalHandle(pdlgtemplate));
7925 SelectFont(hdc, oldFont);
7926 DeleteObject(font);
7927 ReleaseDC(hwnd, hdc);
7928
7929 /*
7930 * Reassert ourselves as the active window. This is so that after creating
7931 * a tearoff, the user doesn't have to click with the mouse just to start
7932 * typing again!
7933 */
7934 (void)SetActiveWindow(s_hwnd);
7935
Bram Moolenaar734a8672019-12-02 22:49:38 +01007936 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 force_menu_update = TRUE;
7938}
7939#endif
7940
7941#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007942# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
Bram Moolenaar734a8672019-12-02 22:49:38 +01007944// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945# ifndef TBSTYLE_FLAT
7946# define TBSTYLE_FLAT 0x0800
7947# endif
7948
7949/*
7950 * Create the toolbar, initially unpopulated.
7951 * (just like the menu, there are no defaults, it's all
7952 * set up through menu.vim)
7953 */
7954 static void
7955initialise_toolbar(void)
7956{
7957 InitCommonControls();
7958 s_toolbarhwnd = CreateToolbarEx(
7959 s_hwnd,
7960 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7961 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007962 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007963 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 IDR_TOOLBAR1, // id of initial bitmap
7965 NULL,
7966 0, // initial number of buttons
7967 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7968 TOOLBAR_BUTTON_HEIGHT,
7969 TOOLBAR_BUTTON_WIDTH,
7970 TOOLBAR_BUTTON_HEIGHT,
7971 sizeof(TBBUTTON)
7972 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007973
7974 // Remove transparency from the toolbar to prevent the main window
7975 // background colour showing through
7976 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7977 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7978
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007979 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7982}
7983
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007984 static LRESULT CALLBACK
7985toolbar_wndproc(
7986 HWND hwnd,
7987 UINT uMsg,
7988 WPARAM wParam,
7989 LPARAM lParam)
7990{
7991 HandleMouseHide(uMsg, lParam);
7992 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7993}
7994
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 static int
7996get_toolbar_bitmap(vimmenu_T *menu)
7997{
7998 int i = -1;
7999
8000 /*
8001 * Check user bitmaps first, unless builtin is specified.
8002 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008003 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
8005 char_u fname[MAXPATHL];
8006 HANDLE hbitmap = NULL;
8007
8008 if (menu->iconfile != NULL)
8009 {
8010 gui_find_iconfile(menu->iconfile, fname, "bmp");
8011 hbitmap = LoadImage(
8012 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008013 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 IMAGE_BITMAP,
8015 TOOLBAR_BUTTON_WIDTH,
8016 TOOLBAR_BUTTON_HEIGHT,
8017 LR_LOADFROMFILE |
8018 LR_LOADMAP3DCOLORS
8019 );
8020 }
8021
8022 /*
8023 * If the LoadImage call failed, or the "icon=" file
8024 * didn't exist or wasn't specified, try the menu name
8025 */
8026 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008027 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008028# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008029 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008030# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008031 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 hbitmap = LoadImage(
8033 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008034 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 IMAGE_BITMAP,
8036 TOOLBAR_BUTTON_WIDTH,
8037 TOOLBAR_BUTTON_HEIGHT,
8038 LR_LOADFROMFILE |
8039 LR_LOADMAP3DCOLORS
8040 );
8041
8042 if (hbitmap != NULL)
8043 {
8044 TBADDBITMAP tbAddBitmap;
8045
8046 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008047 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
8049 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8050 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008051 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 }
8054 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8055 i = menu->iconidx;
8056
8057 return i;
8058}
8059#endif
8060
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008061#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8062 static void
8063initialise_tabline(void)
8064{
8065 InitCommonControls();
8066
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008067 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008068 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008069 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008070 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008071 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008072
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008073 gui.tabline_height = TABLINE_HEIGHT;
8074
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008075# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008076 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008077# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008078}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008079
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008080/*
8081 * Get tabpage_T from POINT.
8082 */
8083 static tabpage_T *
8084GetTabFromPoint(
8085 HWND hWnd,
8086 POINT pt)
8087{
8088 tabpage_T *ptp = NULL;
8089
8090 if (gui_mch_showing_tabline())
8091 {
8092 TCHITTESTINFO htinfo;
8093 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008094 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008095 if (s_tabhwnd == hWnd)
8096 {
8097 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8098 if (idx != -1)
8099 ptp = find_tabpage(idx + 1);
8100 }
8101 }
8102 return ptp;
8103}
8104
8105static POINT s_pt = {0, 0};
8106static HCURSOR s_hCursor = NULL;
8107
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008108 static LRESULT CALLBACK
8109tabline_wndproc(
8110 HWND hwnd,
8111 UINT uMsg,
8112 WPARAM wParam,
8113 LPARAM lParam)
8114{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008115 POINT pt;
8116 tabpage_T *tp;
8117 RECT rect;
8118 int nCenter;
8119 int idx0;
8120 int idx1;
8121
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008122 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008123
8124 switch (uMsg)
8125 {
8126 case WM_LBUTTONDOWN:
8127 {
8128 s_pt.x = GET_X_LPARAM(lParam);
8129 s_pt.y = GET_Y_LPARAM(lParam);
8130 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008131 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008132 break;
8133 }
8134 case WM_MOUSEMOVE:
8135 if (GetCapture() == hwnd
8136 && ((wParam & MK_LBUTTON)) != 0)
8137 {
8138 pt.x = GET_X_LPARAM(lParam);
8139 pt.y = s_pt.y;
8140 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8141 {
8142 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8143
8144 tp = GetTabFromPoint(hwnd, pt);
8145 if (tp != NULL)
8146 {
8147 idx0 = tabpage_index(curtab) - 1;
8148 idx1 = tabpage_index(tp) - 1;
8149
8150 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8151 nCenter = rect.left + (rect.right - rect.left) / 2;
8152
Bram Moolenaar734a8672019-12-02 22:49:38 +01008153 // Check if the mouse cursor goes over the center of
8154 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008155 if ((idx0 < idx1) && (nCenter < pt.x))
8156 {
8157 tabpage_move(idx1 + 1);
8158 update_screen(0);
8159 }
8160 else if ((idx1 < idx0) && (pt.x < nCenter))
8161 {
8162 tabpage_move(idx1);
8163 update_screen(0);
8164 }
8165 }
8166 }
8167 }
8168 break;
8169 case WM_LBUTTONUP:
8170 {
8171 if (GetCapture() == hwnd)
8172 {
8173 SetCursor(s_hCursor);
8174 ReleaseCapture();
8175 }
8176 break;
8177 }
8178 default:
8179 break;
8180 }
8181
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008182 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8183}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008184#endif
8185
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8187/*
8188 * Make the GUI window come to the foreground.
8189 */
8190 void
8191gui_mch_set_foreground(void)
8192{
8193 if (IsIconic(s_hwnd))
8194 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8195 SetForegroundWindow(s_hwnd);
8196}
8197#endif
8198
8199#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8200 static void
8201dyn_imm_load(void)
8202{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008203 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 if (hLibImm == NULL)
8205 return;
8206
8207 pImmGetCompositionStringA
8208 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8209 pImmGetCompositionStringW
8210 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8211 pImmGetContext
8212 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8213 pImmAssociateContext
8214 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8215 pImmReleaseContext
8216 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8217 pImmGetOpenStatus
8218 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8219 pImmSetOpenStatus
8220 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008221 pImmGetCompositionFontW
8222 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8223 pImmSetCompositionFontW
8224 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 pImmSetCompositionWindow
8226 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8227 pImmGetConversionStatus
8228 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008229 pImmSetConversionStatus
8230 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 if ( pImmGetCompositionStringA == NULL
8233 || pImmGetCompositionStringW == NULL
8234 || pImmGetContext == NULL
8235 || pImmAssociateContext == NULL
8236 || pImmReleaseContext == NULL
8237 || pImmGetOpenStatus == NULL
8238 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008239 || pImmGetCompositionFontW == NULL
8240 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008242 || pImmGetConversionStatus == NULL
8243 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
8245 FreeLibrary(hLibImm);
8246 hLibImm = NULL;
8247 pImmGetContext = NULL;
8248 return;
8249 }
8250
8251 return;
8252}
8253
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254#endif
8255
8256#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8257
8258# ifdef FEAT_XPM_W32
8259# define IMAGE_XPM 100
8260# endif
8261
8262typedef struct _signicon_t
8263{
8264 HANDLE hImage;
8265 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008266# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008267 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269} signicon_t;
8270
8271 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008272gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273{
8274 signicon_t *sign;
8275 int x, y, w, h;
8276
8277 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8278 return;
8279
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008280# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008281 if (IS_ENABLE_DIRECTX())
8282 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008283# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008284
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 x = TEXT_X(col);
8286 y = TEXT_Y(row);
8287 w = gui.char_width * 2;
8288 h = gui.char_height;
8289 switch (sign->uType)
8290 {
8291 case IMAGE_BITMAP:
8292 {
8293 HDC hdcMem;
8294 HBITMAP hbmpOld;
8295
8296 hdcMem = CreateCompatibleDC(s_hdc);
8297 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8298 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8299 SelectObject(hdcMem, hbmpOld);
8300 DeleteDC(hdcMem);
8301 }
8302 break;
8303 case IMAGE_ICON:
8304 case IMAGE_CURSOR:
8305 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8306 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008307# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 case IMAGE_XPM:
8309 {
8310 HDC hdcMem;
8311 HBITMAP hbmpOld;
8312
8313 hdcMem = CreateCompatibleDC(s_hdc);
8314 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008315 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8317
8318 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008319 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8321 SelectObject(hdcMem, hbmpOld);
8322 DeleteDC(hdcMem);
8323 }
8324 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327}
8328
8329 static void
8330close_signicon_image(signicon_t *sign)
8331{
8332 if (sign)
8333 switch (sign->uType)
8334 {
8335 case IMAGE_BITMAP:
8336 DeleteObject((HGDIOBJ)sign->hImage);
8337 break;
8338 case IMAGE_CURSOR:
8339 DestroyCursor((HCURSOR)sign->hImage);
8340 break;
8341 case IMAGE_ICON:
8342 DestroyIcon((HICON)sign->hImage);
8343 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008344# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 case IMAGE_XPM:
8346 DeleteObject((HBITMAP)sign->hImage);
8347 DeleteObject((HBITMAP)sign->hShape);
8348 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 }
8351}
8352
8353 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008354gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356 signicon_t sign, *psign;
8357 char_u *ext;
8358
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008360 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (ext > signfile)
8362 {
8363 int do_load = 1;
8364
8365 if (!STRICMP(ext, ".bmp"))
8366 sign.uType = IMAGE_BITMAP;
8367 else if (!STRICMP(ext, ".ico"))
8368 sign.uType = IMAGE_ICON;
8369 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8370 sign.uType = IMAGE_CURSOR;
8371 else
8372 do_load = 0;
8373
8374 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008375 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 gui.char_width * 2, gui.char_height,
8377 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008378# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (!STRICMP(ext, ".xpm"))
8380 {
8381 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008382 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8383 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387
8388 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008389 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 *psign = sign;
8391
8392 if (!psign)
8393 {
8394 if (sign.hImage)
8395 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008396 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 }
8398 return (void *)psign;
8399
8400}
8401
8402 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008403gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 if (sign)
8406 {
8407 close_signicon_image((signicon_t *)sign);
8408 vim_free(sign);
8409 }
8410}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008413#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
Bram Moolenaar734a8672019-12-02 22:49:38 +01008415/*
8416 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008417 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008419 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8421 * to get current mouse position).
8422 *
8423 * Trying to use as more Windows services as possible, and as less
8424 * IE version as possible :)).
8425 *
8426 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8427 * BalloonEval struct.
8428 * 2) Enable/Disable simply create/kill BalloonEval Timer
8429 * 3) When there was enough inactivity, timer procedure posts
8430 * async request to debugger
8431 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8432 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008433 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 */
8435
Bram Moolenaar45360022005-07-21 21:08:21 +00008436/*
8437 * determine whether installed Common Controls support multiline tooltips
8438 * (i.e. their version is >= 4.70
8439 */
8440 int
8441multiline_balloon_available(void)
8442{
8443 HINSTANCE hDll;
8444 static char comctl_dll[] = "comctl32.dll";
8445 static int multiline_tip = MAYBE;
8446
8447 if (multiline_tip != MAYBE)
8448 return multiline_tip;
8449
8450 hDll = GetModuleHandle(comctl_dll);
8451 if (hDll != NULL)
8452 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008453 DLLGETVERSIONPROC pGetVer;
8454 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008455
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008456 if (pGetVer != NULL)
8457 {
8458 DLLVERSIONINFO dvi;
8459 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008460
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008461 ZeroMemory(&dvi, sizeof(dvi));
8462 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008463
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008464 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008465
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008466 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008467 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008468 || (dvi.dwMajorVersion == 4
8469 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008470 {
8471 multiline_tip = TRUE;
8472 return multiline_tip;
8473 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008474 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008475 else
8476 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008477 // there is chance we have ancient CommCtl 4.70
8478 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008479 DWORD dwHandle = 0;
8480 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8481 if (len > 0)
8482 {
8483 VS_FIXEDFILEINFO *ver;
8484 UINT vlen = 0;
8485 void *data = alloc(len);
8486
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008487 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008488 && GetFileVersionInfo(comctl_dll, 0, len, data)
8489 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8490 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008491 && HIWORD(ver->dwFileVersionMS) > 4)
8492 || ((HIWORD(ver->dwFileVersionMS) == 4
8493 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008494 {
8495 vim_free(data);
8496 multiline_tip = TRUE;
8497 return multiline_tip;
8498 }
8499 vim_free(data);
8500 }
8501 }
8502 }
8503 multiline_tip = FALSE;
8504 return multiline_tip;
8505}
8506
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008507 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008508make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008509{
8510 TOOLINFOW *pti;
8511 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008512
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008513 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008514 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8515 else
8516 ToolInfoSize = sizeof(TOOLINFOW);
8517
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008518 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008519 if (pti == NULL)
8520 return;
8521
8522 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8523 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8524 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008525 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008526
8527 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8528 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8529
8530 pti->cbSize = ToolInfoSize;
8531 pti->uFlags = TTF_SUBCLASS;
8532 pti->hwnd = beval->target;
8533 pti->hinst = 0; // Don't use string resources
8534 pti->uId = ID_BEVAL_TOOLTIP;
8535
Bram Moolenaar032f40a2020-11-18 15:21:50 +01008536 if (multiline_balloon_available())
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008537 {
8538 RECT rect;
8539 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8540 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008541 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8542 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008543 // switch multiline tooltips on
8544 if (GetClientRect(s_textArea, &rect))
8545 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8546 (LPARAM)rect.right);
8547 }
8548 else
8549 {
8550 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008551 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8552 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008553 }
8554
8555 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8556 pti->rect.left = pt.x - 3;
8557 pti->rect.top = pt.y - 3;
8558 pti->rect.right = pt.x + 3;
8559 pti->rect.bottom = pt.y + 3;
8560
8561 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8562 // Make tooltip appear sooner.
8563 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8564 // I've performed some tests and it seems the longest possible life time
8565 // of tooltip is 30 seconds.
8566 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8567 /*
8568 * HACK: force tooltip to appear, because it'll not appear until
8569 * first mouse move. D*mn M$
8570 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8571 */
8572 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8573 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8574 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008575}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008578delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008580 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008584BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008585 HWND hwnd UNUSED,
8586 UINT uMsg UNUSED,
8587 UINT_PTR idEvent UNUSED,
8588 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 POINT pt;
8591 RECT rect;
8592
8593 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8594 return;
8595
8596 GetCursorPos(&pt);
8597 if (WindowFromPoint(pt) != s_textArea)
8598 return;
8599
8600 ScreenToClient(s_textArea, &pt);
8601 GetClientRect(s_textArea, &rect);
8602 if (!PtInRect(&rect, pt))
8603 return;
8604
8605 if (LastActivity > 0
8606 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8607 && (cur_beval->showState != ShS_PENDING
8608 || abs(cur_beval->x - pt.x) > 3
8609 || abs(cur_beval->y - pt.y) > 3))
8610 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008611 // Pointer resting in one place long enough, it's time to show
8612 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 cur_beval->showState = ShS_PENDING;
8614 cur_beval->x = pt.x;
8615 cur_beval->y = pt.y;
8616
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008617 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 if (cur_beval->msgCB != NULL)
8620 (*cur_beval->msgCB)(cur_beval, 0);
8621 }
8622}
8623
8624 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008625gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008627 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008629 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630}
8631
8632 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008633gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008635 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (beval == NULL)
8637 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008638 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008639 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008640 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641}
8642
8643 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008644gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008647
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008648 vim_free(beval->msg);
8649 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8650 if (beval->msg == NULL)
8651 {
8652 delete_tooltip(beval);
8653 beval->showState = ShS_NEUTRAL;
8654 return;
8655 }
8656
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008657 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 if (beval->showState == ShS_SHOWING)
8659 return;
8660 GetCursorPos(&pt);
8661 ScreenToClient(s_textArea, &pt);
8662
8663 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008665 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 gui_mch_disable_beval_area(cur_beval);
8667 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008668 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008670 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671}
8672
8673 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008674gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008675 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008676 char_u *mesg,
8677 void (*mesgCB)(BalloonEval *, int),
8678 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008680 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 BalloonEval *beval;
8682
8683 if (mesg != NULL && mesgCB != NULL)
8684 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008685 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 return NULL;
8687 }
8688
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008689 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 if (beval != NULL)
8691 {
8692 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693
8694 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 beval->msg = mesg;
8696 beval->msgCB = mesgCB;
8697 beval->clientData = clientData;
8698
8699 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 cur_beval = beval;
8701
8702 if (p_beval)
8703 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705 return beval;
8706}
8707
8708 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008709Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008711 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 return;
8713
8714 if (cur_beval != NULL)
8715 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008716 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008718 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008719 // TRACE0("TTN_SHOW {{{");
8720 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008721 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008722 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008723 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 delete_tooltip(cur_beval);
8725 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008726 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008729 break;
8730 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008731 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008732 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008733 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8734 info->lpszText = (LPSTR)info->lParam;
8735 info->uFlags |= TTF_DI_SETITEM;
8736 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008737 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008738 case TTN_GETDISPINFOW:
8739 {
8740 // if we get here then we have new common controls
8741 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8742 info->lpszText = (LPWSTR)info->lParam;
8743 info->uFlags |= TTF_DI_SETITEM;
8744 }
8745 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 }
8747 }
8748}
8749
8750 static void
8751TrackUserActivity(UINT uMsg)
8752{
8753 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8754 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8755 LastActivity = GetTickCount();
8756}
8757
8758 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008759gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008761# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008762 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008763# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008764 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 vim_free(beval);
8766}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008767#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8770/*
8771 * We have multiple signs to draw at the same location. Draw the
8772 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8773 */
8774 void
8775netbeans_draw_multisign_indicator(int row)
8776{
8777 int i;
8778 int y;
8779 int x;
8780
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008781 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008782 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008783
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 x = 0;
8785 y = TEXT_Y(row);
8786
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008787# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008788 if (IS_ENABLE_DIRECTX())
8789 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008790# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 for (i = 0; i < gui.char_height - 3; i++)
8793 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8794
8795 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8796 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8797 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8798 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8799 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8800 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8801 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8802}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008803#endif