blob: 5bc5a3903d8e3bfbe5488196a04862b5144a5f5f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010049# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
Bram Moolenaar734a8672019-12-02 22:49:38 +010061 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100120 else if (STRCMP(name, "scrlines") == 0)
121 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100122 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 else
125 return FAIL;
126 }
127
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100128 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100129 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 if (dx_enable)
133 {
134 if (!directx_enabled())
135 return FAIL;
136 DWriteContext_SetRenderingParams(s_dwc, NULL);
137 if (dx_flags)
138 {
139 DWriteRenderingParams param;
140 DWriteContext_GetRenderingParams(s_dwc, &param);
141 if (dx_flags & (1 << 0))
142 param.gamma = dx_gamma;
143 if (dx_flags & (1 << 1))
144 param.enhancedContrast = dx_contrast;
145 if (dx_flags & (1 << 2))
146 param.clearTypeLevel = dx_level;
147 if (dx_flags & (1 << 3))
148 param.pixelGeometry = dx_geom;
149 if (dx_flags & (1 << 4))
150 param.renderingMode = dx_renmode;
151 if (dx_flags & (1 << 5))
152 param.textAntialiasMode = dx_taamode;
153 DWriteContext_SetRenderingParams(s_dwc, &param);
154 }
155 }
156 s_directx_enabled = dx_enable;
157
158 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100159# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162}
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * These are new in Windows ME/XP, only defined in recent compilers.
167 */
168#ifndef HANDLE_WM_XBUTTONUP
169# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
170 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
171#endif
172#ifndef HANDLE_WM_XBUTTONDOWN
173# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDBLCLK
177# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100181
Bram Moolenaar734a8672019-12-02 22:49:38 +0100182#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
Bram Moolenaar734a8672019-12-02 22:49:38 +0100187// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188#ifndef PROTO
189
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100190# ifndef __MINGW32__
191# include <shellapi.h>
192# endif
193# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
194# include <commctrl.h>
195# endif
196# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifdef GLOBAL_IME
199# include "glbl_ime.h"
200# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
Bram Moolenaar734a8672019-12-02 22:49:38 +0100202#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
204#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100205# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100206#endif
207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200243# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200244# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define _cdecl
246typedef int BOOL;
247typedef int BYTE;
248typedef int DWORD;
249typedef int WCHAR;
250typedef int ENUMLOGFONT;
251typedef int FINDREPLACE;
252typedef int HANDLE;
253typedef int HBITMAP;
254typedef int HBRUSH;
255typedef int HDROP;
256typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100257typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100258typedef int LPARAM;
259typedef int LPCREATESTRUCT;
260typedef int LPCSTR;
261typedef int LPCTSTR;
262typedef int LPRECT;
263typedef int LPSTR;
264typedef int LPWINDOWPOS;
265typedef int LPWORD;
266typedef int LRESULT;
267typedef int HRESULT;
268# undef MSG
269typedef int MSG;
270typedef int NEWTEXTMETRIC;
271typedef int OSVERSIONINFO;
272typedef int PWORD;
273typedef int RECT;
274typedef int UINT;
275typedef int WORD;
276typedef int WPARAM;
277typedef int POINT;
278typedef void *HINSTANCE;
279typedef void *HMENU;
280typedef void *HWND;
281typedef void *HDC;
282typedef void VOID;
283typedef int LPNMHDR;
284typedef int LONG;
285typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200286typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200287typedef int COLORREF;
288typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
295static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200317static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
355#ifdef GLOBAL_IME
356# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
357#else
358# define MyTranslateMessage(x) TranslateMessage(x)
359#endif
360
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100361#if defined(FEAT_DIRECTX)
362 static int
363directx_enabled(void)
364{
365 if (s_dwc != NULL)
366 return 1;
367 else if (s_directx_load_attempted)
368 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100369 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100370 DWrite_Init();
371 s_directx_load_attempted = 1;
372 s_dwc = DWriteContext_Open();
373 directx_binddc();
374 return s_dwc != NULL ? 1 : 0;
375}
376
377 static void
378directx_binddc(void)
379{
380 if (s_textArea != NULL)
381 {
382 RECT rect;
383 GetClientRect(s_textArea, &rect);
384 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
385 }
386}
387#endif
388
Bram Moolenaar734a8672019-12-02 22:49:38 +0100389// use of WindowProc depends on Global IME
Bram Moolenaar945c8572020-07-17 22:17:03 +0200390static LRESULT WINAPI MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100391
Bram Moolenaar734a8672019-12-02 22:49:38 +0100392extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100393
394static struct
395{
396 UINT key_sym;
397 char_u vim_code0;
398 char_u vim_code1;
399} special_keys[] =
400{
401 {VK_UP, 'k', 'u'},
402 {VK_DOWN, 'k', 'd'},
403 {VK_LEFT, 'k', 'l'},
404 {VK_RIGHT, 'k', 'r'},
405
406 {VK_F1, 'k', '1'},
407 {VK_F2, 'k', '2'},
408 {VK_F3, 'k', '3'},
409 {VK_F4, 'k', '4'},
410 {VK_F5, 'k', '5'},
411 {VK_F6, 'k', '6'},
412 {VK_F7, 'k', '7'},
413 {VK_F8, 'k', '8'},
414 {VK_F9, 'k', '9'},
415 {VK_F10, 'k', ';'},
416
417 {VK_F11, 'F', '1'},
418 {VK_F12, 'F', '2'},
419 {VK_F13, 'F', '3'},
420 {VK_F14, 'F', '4'},
421 {VK_F15, 'F', '5'},
422 {VK_F16, 'F', '6'},
423 {VK_F17, 'F', '7'},
424 {VK_F18, 'F', '8'},
425 {VK_F19, 'F', '9'},
426 {VK_F20, 'F', 'A'},
427
428 {VK_F21, 'F', 'B'},
429#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100430 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100431#endif
432 {VK_F22, 'F', 'C'},
433 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100434 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100435
436 {VK_HELP, '%', '1'},
437 {VK_BACK, 'k', 'b'},
438 {VK_INSERT, 'k', 'I'},
439 {VK_DELETE, 'k', 'D'},
440 {VK_HOME, 'k', 'h'},
441 {VK_END, '@', '7'},
442 {VK_PRIOR, 'k', 'P'},
443 {VK_NEXT, 'k', 'N'},
444 {VK_PRINT, '%', '9'},
445 {VK_ADD, 'K', '6'},
446 {VK_SUBTRACT, 'K', '7'},
447 {VK_DIVIDE, 'K', '8'},
448 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100449 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100450 {VK_DECIMAL, 'K', 'B'},
451
452 {VK_NUMPAD0, 'K', 'C'},
453 {VK_NUMPAD1, 'K', 'D'},
454 {VK_NUMPAD2, 'K', 'E'},
455 {VK_NUMPAD3, 'K', 'F'},
456 {VK_NUMPAD4, 'K', 'G'},
457 {VK_NUMPAD5, 'K', 'H'},
458 {VK_NUMPAD6, 'K', 'I'},
459 {VK_NUMPAD7, 'K', 'J'},
460 {VK_NUMPAD8, 'K', 'K'},
461 {VK_NUMPAD9, 'K', 'L'},
462
Bram Moolenaar734a8672019-12-02 22:49:38 +0100463 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100464 {VK_SPACE, ' ', NUL},
465 {VK_TAB, TAB, NUL},
466 {VK_ESCAPE, ESC, NUL},
467 {NL, NL, NUL},
468 {CAR, CAR, NUL},
469
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471 {0, 0, 0}
472};
473
Bram Moolenaar734a8672019-12-02 22:49:38 +0100474// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100475static int s_button_pending = -1;
476
Bram Moolenaar734a8672019-12-02 22:49:38 +0100477// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
478// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479static int s_getting_focus = FALSE;
480
481static int s_x_pending;
482static int s_y_pending;
483static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200484static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200486static int dead_key = 0; // 0: no dead key, 1: dead key pressed
487static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
488 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100490#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100491// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
493static void TrackUserActivity(UINT uMsg);
494#endif
495
496/*
497 * For control IME.
498 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100499 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100501#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100502// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100503static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#endif
505#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100507static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100508#endif
509
510#ifdef FEAT_MBYTE_IME
511static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
512#endif
513
514#if defined(FEAT_BROWSE)
515static char_u *convert_filter(char_u *s);
516#endif
517
518#ifdef DEBUG_PRINT_ERROR
519/*
520 * Print out the last Windows error message
521 */
522 static void
523print_windows_error(void)
524{
525 LPVOID lpMsgBuf;
526
527 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
528 NULL, GetLastError(),
529 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
530 (LPTSTR) &lpMsgBuf, 0, NULL);
531 TRACE1("Error: %s\n", lpMsgBuf);
532 LocalFree(lpMsgBuf);
533}
534#endif
535
536/*
537 * Cursor blink functions.
538 *
539 * This is a simple state machine:
540 * BLINK_NONE not blinking at all
541 * BLINK_OFF blinking, cursor is not shown
542 * BLINK_ON blinking, cursor is shown
543 */
544
545#define BLINK_NONE 0
546#define BLINK_OFF 1
547#define BLINK_ON 2
548
549static int blink_state = BLINK_NONE;
550static long_u blink_waittime = 700;
551static long_u blink_ontime = 400;
552static long_u blink_offtime = 250;
553static UINT blink_timer = 0;
554
Bram Moolenaar703a8042016-06-04 16:24:32 +0200555 int
556gui_mch_is_blinking(void)
557{
558 return blink_state != BLINK_NONE;
559}
560
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200561 int
562gui_mch_is_blink_off(void)
563{
564 return blink_state == BLINK_OFF;
565}
566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100567 void
568gui_mch_set_blinking(long wait, long on, long off)
569{
570 blink_waittime = wait;
571 blink_ontime = on;
572 blink_offtime = off;
573}
574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 static VOID CALLBACK
576_OnBlinkTimer(
577 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100578 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100580 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100581{
582 MSG msg;
583
584 /*
585 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
586 */
587
588 KillTimer(NULL, idEvent);
589
Bram Moolenaar734a8672019-12-02 22:49:38 +0100590 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100591 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
592 ;
593
594 if (blink_state == BLINK_ON)
595 {
596 gui_undraw_cursor();
597 blink_state = BLINK_OFF;
598 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
599 (TIMERPROC)_OnBlinkTimer);
600 }
601 else
602 {
603 gui_update_cursor(TRUE, FALSE);
604 blink_state = BLINK_ON;
605 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100606 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100607 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100608 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609}
610
611 static void
612gui_mswin_rm_blink_timer(void)
613{
614 MSG msg;
615
616 if (blink_timer != 0)
617 {
618 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100619 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100620 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
621 ;
622 blink_timer = 0;
623 }
624}
625
626/*
627 * Stop the cursor blinking. Show the cursor if it wasn't shown.
628 */
629 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100630gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100631{
632 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100634 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100636 gui_mch_flush();
637 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 blink_state = BLINK_NONE;
639}
640
641/*
642 * Start the cursor blinking. If it was already blinking, this restarts the
643 * waiting time and shows the cursor.
644 */
645 void
646gui_mch_start_blink(void)
647{
648 gui_mswin_rm_blink_timer();
649
Bram Moolenaar734a8672019-12-02 22:49:38 +0100650 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
652 {
653 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
654 (TIMERPROC)_OnBlinkTimer);
655 blink_state = BLINK_ON;
656 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100657 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100658 }
659}
660
661/*
662 * Call-back routines.
663 */
664
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665 static VOID CALLBACK
666_OnTimer(
667 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100668 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100670 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671{
672 MSG msg;
673
674 /*
675 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
676 */
677 KillTimer(NULL, idEvent);
678 s_timed_out = TRUE;
679
Bram Moolenaar734a8672019-12-02 22:49:38 +0100680 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
682 ;
683 if (idEvent == s_wait_timer)
684 s_wait_timer = 0;
685}
686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100687 static void
688_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100689 HWND hwnd UNUSED,
690 UINT ch UNUSED,
691 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100692{
693 dead_key = 1;
694}
695
696/*
697 * Convert Unicode character "ch" to bytes in "string[slen]".
698 * When "had_alt" is TRUE the ALT key was included in "ch".
699 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200700 * Because the Windows API uses UTF-16, we have to deal with surrogate
701 * pairs; this is where we choose to deal with them: if "ch" is a high
702 * surrogate, it will be stored, and the length returned will be zero; the next
703 * char_to_string call will then include the high surrogate, decoding the pair
704 * of UTF-16 code units to a single Unicode code point, presuming it is the
705 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 */
707 static int
708char_to_string(int ch, char_u *string, int slen, int had_alt)
709{
710 int len;
711 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200713 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200715 if (surrogate_pending_ch != 0)
716 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100717 // We don't guarantee ch is a low surrogate to match the high surrogate
718 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200719 wstring[0] = surrogate_pending_ch;
720 wstring[1] = ch;
721 surrogate_pending_ch = 0;
722 len = 2;
723 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100724 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200725 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100726 // We don't have the entire code point yet, only the first UTF-16 code
727 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200728 surrogate_pending_ch = ch;
729 return 0;
730 }
731 else
732 {
733 wstring[0] = ch;
734 len = 1;
735 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200736
Bram Moolenaar734a8672019-12-02 22:49:38 +0100737 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
738 // "enc_codepage" is non-zero use the standard Win32 function,
739 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200740 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200742 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
743 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100744 // If we had included the ALT key into the character but now the
745 // upper bit is no longer set, that probably means the conversion
746 // failed. Convert the original character and set the upper bit
747 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200748 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200750 wstring[0] = ch & 0x7f;
751 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
752 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100753 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200754 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 }
756 }
757 else
758 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200759 ws = utf16_to_enc(wstring, &len);
760 if (ws == NULL)
761 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762 else
763 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100764 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200765 len = slen;
766 mch_memmove(string, ws, len);
767 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100768 }
769 }
770
771 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
773 string[0] = ch;
774 len = 1;
775 }
776
777 for (i = 0; i < len; ++i)
778 if (string[i] == CSI && len <= slen - 2)
779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100781 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
782 string[++i] = KS_EXTRA;
783 string[++i] = (int)KE_CSI;
784 len += 2;
785 }
786
787 return len;
788}
789
790/*
791 * Key hit, add it to the input buffer.
792 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 static void
794_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100795 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100797 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798{
799 char_u string[40];
800 int len = 0;
801
802 dead_key = 0;
803
804 len = char_to_string(ch, string, 40, FALSE);
805 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
806 {
807 trash_input_buf();
808 got_int = TRUE;
809 }
810
811 add_to_input_buf(string, len);
812}
813
814/*
815 * Alt-Key hit, add it to the input buffer.
816 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 static void
818_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100819 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100821 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100823 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 int len;
825 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100826 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827
828 dead_key = 0;
829
Bram Moolenaar734a8672019-12-02 22:49:38 +0100830 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100831
Bram Moolenaar734a8672019-12-02 22:49:38 +0100832 // OK, we have a character key (given by ch) which was entered with the
833 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
834 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
835 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100836 modifiers = MOD_MASK_ALT;
837 if (GetKeyState(VK_SHIFT) & 0x8000)
838 modifiers |= MOD_MASK_SHIFT;
839 if (GetKeyState(VK_CONTROL) & 0x8000)
840 modifiers |= MOD_MASK_CTRL;
841
842 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100843 // remove the SHIFT modifier for keys where it's already included, e.g.,
844 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200845 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100846
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200847 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
848 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100849 if (ch == CSI)
850 ch = K_CSI;
851
852 len = 0;
853 if (modifiers)
854 {
855 string[len++] = CSI;
856 string[len++] = KS_MODIFIER;
857 string[len++] = modifiers;
858 }
859
860 if (IS_SPECIAL((int)ch))
861 {
862 string[len++] = CSI;
863 string[len++] = K_SECOND((int)ch);
864 string[len++] = K_THIRD((int)ch);
865 }
866 else
867 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100868 // Although the documentation isn't clear about it, we assume "ch" is
869 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870 len += char_to_string(ch, string + len, 40 - len, TRUE);
871 }
872
873 add_to_input_buf(string, len);
874}
875
876 static void
877_OnMouseEvent(
878 int button,
879 int x,
880 int y,
881 int repeated_click,
882 UINT keyFlags)
883{
884 int vim_modifiers = 0x0;
885
886 s_getting_focus = FALSE;
887
888 if (keyFlags & MK_SHIFT)
889 vim_modifiers |= MOUSE_SHIFT;
890 if (keyFlags & MK_CONTROL)
891 vim_modifiers |= MOUSE_CTRL;
892 if (GetKeyState(VK_MENU) & 0x8000)
893 vim_modifiers |= MOUSE_ALT;
894
895 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
896}
897
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100898 static void
899_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100900 HWND hwnd UNUSED,
901 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100902 int x,
903 int y,
904 UINT keyFlags)
905{
906 static LONG s_prevTime = 0;
907
908 LONG currentTime = GetMessageTime();
909 int button = -1;
910 int repeated_click;
911
Bram Moolenaar734a8672019-12-02 22:49:38 +0100912 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100913 (void)SetFocus(s_hwnd);
914
915 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
916 button = MOUSE_LEFT;
917 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
918 button = MOUSE_MIDDLE;
919 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
920 button = MOUSE_RIGHT;
921 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
922 {
923#ifndef GET_XBUTTON_WPARAM
924# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
925#endif
926 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
927 }
928 else if (s_uMsg == WM_CAPTURECHANGED)
929 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100930 // on W95/NT4, somehow you get in here with an odd Msg
931 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 if (s_button_pending == MOUSE_LEFT)
933 button = MOUSE_RIGHT;
934 else
935 button = MOUSE_LEFT;
936 }
937 if (button >= 0)
938 {
939 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
940
941 /*
942 * Holding down the left and right buttons simulates pushing the middle
943 * button.
944 */
945 if (repeated_click
946 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
947 || (button == MOUSE_RIGHT
948 && s_button_pending == MOUSE_LEFT)))
949 {
950 /*
951 * Hmm, gui.c will ignore more than one button down at a time, so
952 * pretend we let go of it first.
953 */
954 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
955 button = MOUSE_MIDDLE;
956 repeated_click = FALSE;
957 s_button_pending = -1;
958 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
959 }
960 else if ((repeated_click)
961 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
962 {
963 if (s_button_pending > -1)
964 {
965 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
966 s_button_pending = -1;
967 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100968 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100969 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
970 }
971 else
972 {
973 /*
974 * If this is the first press (i.e. not a multiple click) don't
975 * action immediately, but store and wait for:
976 * i) button-up
977 * ii) mouse move
978 * iii) another button press
979 * before using it.
980 * This enables us to make left+right simulate middle button,
981 * without left or right being actioned first. The side-effect is
982 * that if you click and hold the mouse without dragging, the
983 * cursor doesn't move until you release the button. In practice
984 * this is hardly a problem.
985 */
986 s_button_pending = button;
987 s_x_pending = x;
988 s_y_pending = y;
989 s_kFlags_pending = keyFlags;
990 }
991
992 s_prevTime = currentTime;
993 }
994}
995
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100996 static void
997_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100998 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100999 int x,
1000 int y,
1001 UINT keyFlags)
1002{
1003 int button;
1004
1005 s_getting_focus = FALSE;
1006 if (s_button_pending > -1)
1007 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001008 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001009 _OnMouseEvent(s_button_pending, s_x_pending,
1010 s_y_pending, FALSE, s_kFlags_pending);
1011 s_button_pending = -1;
1012 }
1013 if (s_uMsg == WM_MOUSEMOVE)
1014 {
1015 /*
1016 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1017 * down.
1018 */
1019 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1020 | MK_XBUTTON1 | MK_XBUTTON2)))
1021 {
1022 gui_mouse_moved(x, y);
1023 return;
1024 }
1025
1026 /*
1027 * While button is down, keep grabbing mouse move events when
1028 * the mouse goes outside the window
1029 */
1030 SetCapture(s_textArea);
1031 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001032 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001033 }
1034 else
1035 {
1036 ReleaseCapture();
1037 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001038 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001039 }
1040
1041 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1042}
1043
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001044 static void
1045_OnSizeTextArea(
1046 HWND hwnd UNUSED,
1047 UINT state UNUSED,
1048 int cx UNUSED,
1049 int cy UNUSED)
1050{
1051#if defined(FEAT_DIRECTX)
1052 if (IS_ENABLE_DIRECTX())
1053 directx_binddc();
1054#endif
1055}
1056
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057#ifdef FEAT_MENU
1058/*
1059 * Find the vimmenu_T with the given id
1060 */
1061 static vimmenu_T *
1062gui_mswin_find_menu(
1063 vimmenu_T *pMenu,
1064 int id)
1065{
1066 vimmenu_T *pChildMenu;
1067
1068 while (pMenu)
1069 {
1070 if (pMenu->id == (UINT)id)
1071 break;
1072 if (pMenu->children != NULL)
1073 {
1074 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1075 if (pChildMenu)
1076 {
1077 pMenu = pChildMenu;
1078 break;
1079 }
1080 }
1081 pMenu = pMenu->next;
1082 }
1083 return pMenu;
1084}
1085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001086 static void
1087_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001088 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001089 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001090 HWND hwndCtl UNUSED,
1091 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092{
1093 vimmenu_T *pMenu;
1094
1095 pMenu = gui_mswin_find_menu(root_menu, id);
1096 if (pMenu)
1097 gui_menu_cb(pMenu);
1098}
1099#endif
1100
1101#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103 * Handle a Find/Replace window message.
1104 */
1105 static void
1106_OnFindRepl(void)
1107{
1108 int flags = 0;
1109 int down;
1110
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001111 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001112 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001113 (void)SetFocus(s_hwnd);
1114
1115 if (s_findrep_struct.Flags & FR_FINDNEXT)
1116 {
1117 flags = FRD_FINDNEXT;
1118
Bram Moolenaar734a8672019-12-02 22:49:38 +01001119 // Give main window the focus back: this is so the cursor isn't
1120 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001121 (void)SetFocus(s_hwnd);
1122 }
1123 else if (s_findrep_struct.Flags & FR_REPLACE)
1124 {
1125 flags = FRD_REPLACE;
1126
Bram Moolenaar734a8672019-12-02 22:49:38 +01001127 // Give main window the focus back: this is so the cursor isn't
1128 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001129 (void)SetFocus(s_hwnd);
1130 }
1131 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1132 {
1133 flags = FRD_REPLACEALL;
1134 }
1135
1136 if (flags != 0)
1137 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001138 char_u *p, *q;
1139
Bram Moolenaar734a8672019-12-02 22:49:38 +01001140 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001141 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1142 flags |= FRD_WHOLE_WORD;
1143 if (s_findrep_struct.Flags & FR_MATCHCASE)
1144 flags |= FRD_MATCH_CASE;
1145 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001146 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1147 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1148 if (p != NULL && q != NULL)
1149 gui_do_findrepl(flags, p, q, down);
1150 vim_free(p);
1151 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001152 }
1153}
1154#endif
1155
1156 static void
1157HandleMouseHide(UINT uMsg, LPARAM lParam)
1158{
1159 static LPARAM last_lParam = 0L;
1160
Bram Moolenaar734a8672019-12-02 22:49:38 +01001161 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001162 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1163 {
1164 if (lParam == last_lParam)
1165 return;
1166 last_lParam = lParam;
1167 }
1168
Bram Moolenaar734a8672019-12-02 22:49:38 +01001169 // Handle specially, to centralise coding. We need to be sure we catch all
1170 // possible events which should cause us to restore the cursor (as it is a
1171 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001172 switch (uMsg)
1173 {
1174 case WM_KEYUP:
1175 case WM_CHAR:
1176 /*
1177 * blank out the pointer if necessary
1178 */
1179 if (p_mh)
1180 gui_mch_mousehide(TRUE);
1181 break;
1182
Bram Moolenaar734a8672019-12-02 22:49:38 +01001183 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001184 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001185 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 case WM_LBUTTONDOWN:
1187 case WM_LBUTTONUP:
1188 case WM_MBUTTONDOWN:
1189 case WM_MBUTTONUP:
1190 case WM_RBUTTONDOWN:
1191 case WM_RBUTTONUP:
1192 case WM_XBUTTONDOWN:
1193 case WM_XBUTTONUP:
1194 case WM_NCMOUSEMOVE:
1195 case WM_NCLBUTTONDOWN:
1196 case WM_NCLBUTTONUP:
1197 case WM_NCMBUTTONDOWN:
1198 case WM_NCMBUTTONUP:
1199 case WM_NCRBUTTONDOWN:
1200 case WM_NCRBUTTONUP:
1201 case WM_KILLFOCUS:
1202 /*
1203 * if the pointer is currently hidden, then we should show it.
1204 */
1205 gui_mch_mousehide(FALSE);
1206 break;
1207 }
1208}
1209
1210 static LRESULT CALLBACK
1211_TextAreaWndProc(
1212 HWND hwnd,
1213 UINT uMsg,
1214 WPARAM wParam,
1215 LPARAM lParam)
1216{
1217 /*
1218 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1219 hwnd, uMsg, wParam, lParam);
1220 */
1221
1222 HandleMouseHide(uMsg, lParam);
1223
1224 s_uMsg = uMsg;
1225 s_wParam = wParam;
1226 s_lParam = lParam;
1227
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001228#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001229 TrackUserActivity(uMsg);
1230#endif
1231
1232 switch (uMsg)
1233 {
1234 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1235 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1236 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1237 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1238 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1239 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1240 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1241 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1242 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1243 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1245 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1247 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001248 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001250#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001251 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1252 return TRUE;
1253#endif
1254 default:
1255 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1256 }
1257}
1258
Bram Moolenaar945c8572020-07-17 22:17:03 +02001259 static LRESULT WINAPI
1260MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001261{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001262#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001264#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001265 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266#endif
1267}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269/*
1270 * Called when the foreground or background color has been changed.
1271 */
1272 void
1273gui_mch_new_colors(void)
1274{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001275 HBRUSH prevBrush;
1276
1277 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001278 prevBrush = (HBRUSH)SetClassLongPtr(
1279 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001280 InvalidateRect(s_hwnd, NULL, TRUE);
1281 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282}
1283
1284/*
1285 * Set the colors to their default values.
1286 */
1287 void
1288gui_mch_def_colors(void)
1289{
1290 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1291 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1292 gui.def_norm_pixel = gui.norm_pixel;
1293 gui.def_back_pixel = gui.back_pixel;
1294}
1295
1296/*
1297 * Open the GUI window which was created by a call to gui_mch_init().
1298 */
1299 int
1300gui_mch_open(void)
1301{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001302 // Actually open the window, if not already visible
1303 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304 if (!IsWindowVisible(s_hwnd))
1305 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1306
1307#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001308 // Init replace string here, so that we keep it when re-opening the
1309 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001310 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1311#endif
1312
1313 return OK;
1314}
1315
1316/*
1317 * Get the position of the top left corner of the window.
1318 */
1319 int
1320gui_mch_get_winpos(int *x, int *y)
1321{
1322 RECT rect;
1323
1324 GetWindowRect(s_hwnd, &rect);
1325 *x = rect.left;
1326 *y = rect.top;
1327 return OK;
1328}
1329
1330/*
1331 * Set the position of the top left corner of the window to the given
1332 * coordinates.
1333 */
1334 void
1335gui_mch_set_winpos(int x, int y)
1336{
1337 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1338 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1339}
1340 void
1341gui_mch_set_text_area_pos(int x, int y, int w, int h)
1342{
1343 static int oldx = 0;
1344 static int oldy = 0;
1345
1346 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1347
1348#ifdef FEAT_TOOLBAR
1349 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1350 SendMessage(s_toolbarhwnd, WM_SIZE,
1351 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1352#endif
1353#if defined(FEAT_GUI_TABLINE)
1354 if (showing_tabline)
1355 {
1356 int top = 0;
1357 RECT rect;
1358
1359# ifdef FEAT_TOOLBAR
1360 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1361 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1362# endif
1363 GetClientRect(s_hwnd, &rect);
1364 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1365 }
1366#endif
1367
Bram Moolenaar734a8672019-12-02 22:49:38 +01001368 // When side scroll bar is unshown, the size of window will change.
1369 // then, the text area move left or right. thus client rect should be
1370 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001371 if (oldx != x || oldy != y)
1372 {
1373 InvalidateRect(s_hwnd, NULL, FALSE);
1374 oldx = x;
1375 oldy = y;
1376 }
1377}
1378
1379
1380/*
1381 * Scrollbar stuff:
1382 */
1383
1384 void
1385gui_mch_enable_scrollbar(
1386 scrollbar_T *sb,
1387 int flag)
1388{
1389 ShowScrollBar(sb->id, SB_CTL, flag);
1390
Bram Moolenaar734a8672019-12-02 22:49:38 +01001391 // TODO: When the window is maximized, the size of the window stays the
1392 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1393 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001394}
1395
1396 void
1397gui_mch_set_scrollbar_pos(
1398 scrollbar_T *sb,
1399 int x,
1400 int y,
1401 int w,
1402 int h)
1403{
1404 SetWindowPos(sb->id, NULL, x, y, w, h,
1405 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1406}
1407
Bram Moolenaar203ec772020-07-17 20:43:43 +02001408 int
1409gui_mch_get_scrollbar_xpadding(void)
1410{
1411 RECT rcTxt, rcWnd;
1412 int xpad;
1413
1414 GetWindowRect(s_textArea, &rcTxt);
1415 GetWindowRect(s_hwnd, &rcWnd);
1416 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1417 - GetSystemMetrics(SM_CXFRAME)
1418 - GetSystemMetrics(SM_CXPADDEDBORDER);
1419 return (xpad < 0) ? 0 : xpad;
1420}
1421
1422 int
1423gui_mch_get_scrollbar_ypadding(void)
1424{
1425 RECT rcTxt, rcWnd;
1426 int ypad;
1427
1428 GetWindowRect(s_textArea, &rcTxt);
1429 GetWindowRect(s_hwnd, &rcWnd);
1430 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1431 - GetSystemMetrics(SM_CYFRAME)
1432 - GetSystemMetrics(SM_CXPADDEDBORDER);
1433 return (ypad < 0) ? 0 : ypad;
1434}
1435
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001436 void
1437gui_mch_create_scrollbar(
1438 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001439 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001440{
1441 sb->id = CreateWindow(
1442 "SCROLLBAR", "Scrollbar",
1443 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001444 10, // Any value will do for now
1445 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001446 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001447 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448}
1449
1450/*
1451 * Find the scrollbar with the given hwnd.
1452 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001453 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001454gui_mswin_find_scrollbar(HWND hwnd)
1455{
1456 win_T *wp;
1457
1458 if (gui.bottom_sbar.id == hwnd)
1459 return &gui.bottom_sbar;
1460 FOR_ALL_WINDOWS(wp)
1461 {
1462 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1463 return &wp->w_scrollbars[SBAR_LEFT];
1464 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1465 return &wp->w_scrollbars[SBAR_RIGHT];
1466 }
1467 return NULL;
1468}
1469
1470/*
1471 * Get the character size of a font.
1472 */
1473 static void
1474GetFontSize(GuiFont font)
1475{
1476 HWND hwnd = GetDesktopWindow();
1477 HDC hdc = GetWindowDC(hwnd);
1478 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001479 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001480 TEXTMETRIC tm;
1481
1482 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001483 // GetTextMetrics() may not return the right value in tmAveCharWidth
1484 // for some fonts. Do our own average computation.
1485 GetTextExtentPoint(hdc,
1486 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1487 52, &size);
1488 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001489
1490 gui.char_height = tm.tmHeight + p_linespace;
1491
1492 SelectFont(hdc, hfntOld);
1493
1494 ReleaseDC(hwnd, hdc);
1495}
1496
1497/*
1498 * Adjust gui.char_height (after 'linespace' was changed).
1499 */
1500 int
1501gui_mch_adjust_charheight(void)
1502{
1503 GetFontSize(gui.norm_font);
1504 return OK;
1505}
1506
1507 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001508get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001509{
1510 HFONT font = NULL;
1511
Bram Moolenaar734a8672019-12-02 22:49:38 +01001512 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001513 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001514
1515 if (font == NULL)
1516 return NOFONT;
1517
1518 return (GuiFont)font;
1519}
1520
1521 static int
1522pixels_to_points(int pixels, int vertical)
1523{
1524 int points;
1525 HWND hwnd;
1526 HDC hdc;
1527
1528 hwnd = GetDesktopWindow();
1529 hdc = GetWindowDC(hwnd);
1530
1531 points = MulDiv(pixels, 72,
1532 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1533
1534 ReleaseDC(hwnd, hdc);
1535
1536 return points;
1537}
1538
1539 GuiFont
1540gui_mch_get_font(
1541 char_u *name,
1542 int giveErrorIfMissing)
1543{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001544 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001545 GuiFont font = NOFONT;
1546
1547 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1548 font = get_font_handle(&lf);
1549 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001550 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001551 return font;
1552}
1553
1554#if defined(FEAT_EVAL) || defined(PROTO)
1555/*
1556 * Return the name of font "font" in allocated memory.
1557 * Don't know how to get the actual name, thus use the provided name.
1558 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001560gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001561{
1562 if (name == NULL)
1563 return NULL;
1564 return vim_strsave(name);
1565}
1566#endif
1567
1568 void
1569gui_mch_free_font(GuiFont font)
1570{
1571 if (font)
1572 DeleteObject((HFONT)font);
1573}
1574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001575/*
1576 * Return the Pixel value (color) for the given color name.
1577 * Return INVALCOLOR for error.
1578 */
1579 guicolor_T
1580gui_mch_get_color(char_u *name)
1581{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001582 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001583
1584 typedef struct SysColorTable
1585 {
1586 char *name;
1587 int color;
1588 } SysColorTable;
1589
1590 static SysColorTable sys_table[] =
1591 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1593 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001594#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1596#endif
1597 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1598 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1599 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1600 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1601 {"SYS_DESKTOP", COLOR_DESKTOP},
1602 {"SYS_INFOBK", COLOR_INFOBK},
1603 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1604 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001605 {"SYS_BTNFACE", COLOR_BTNFACE},
1606 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1607 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1608 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1609 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1610 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1611 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1612 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1613 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1614 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1615 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1616 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1617 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1618 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1619 {"SYS_MENU", COLOR_MENU},
1620 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1621 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1622 {"SYS_WINDOW", COLOR_WINDOW},
1623 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1624 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1625 };
1626
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 /*
1628 * Try to look up a system colour.
1629 */
1630 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1631 if (STRICMP(name, sys_table[i].name) == 0)
1632 return GetSysColor(sys_table[i].color);
1633
Bram Moolenaarab302212016-04-26 20:59:29 +02001634 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001635}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001636
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001637 guicolor_T
1638gui_mch_get_rgb_color(int r, int g, int b)
1639{
1640 return gui_get_rgb_color_cmn(r, g, b);
1641}
1642
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001643/*
1644 * Return OK if the key with the termcap name "name" is supported.
1645 */
1646 int
1647gui_mch_haskey(char_u *name)
1648{
1649 int i;
1650
1651 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1652 if (name[0] == special_keys[i].vim_code0 &&
1653 name[1] == special_keys[i].vim_code1)
1654 return OK;
1655 return FAIL;
1656}
1657
1658 void
1659gui_mch_beep(void)
1660{
1661 MessageBeep(MB_OK);
1662}
1663/*
1664 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1665 */
1666 void
1667gui_mch_invert_rectangle(
1668 int r,
1669 int c,
1670 int nr,
1671 int nc)
1672{
1673 RECT rc;
1674
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001675#if defined(FEAT_DIRECTX)
1676 if (IS_ENABLE_DIRECTX())
1677 DWriteContext_Flush(s_dwc);
1678#endif
1679
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680 /*
1681 * Note: InvertRect() excludes right and bottom of rectangle.
1682 */
1683 rc.left = FILL_X(c);
1684 rc.top = FILL_Y(r);
1685 rc.right = rc.left + nc * gui.char_width;
1686 rc.bottom = rc.top + nr * gui.char_height;
1687 InvertRect(s_hdc, &rc);
1688}
1689
1690/*
1691 * Iconify the GUI window.
1692 */
1693 void
1694gui_mch_iconify(void)
1695{
1696 ShowWindow(s_hwnd, SW_MINIMIZE);
1697}
1698
1699/*
1700 * Draw a cursor without focus.
1701 */
1702 void
1703gui_mch_draw_hollow_cursor(guicolor_T color)
1704{
1705 HBRUSH hbr;
1706 RECT rc;
1707
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001708#if defined(FEAT_DIRECTX)
1709 if (IS_ENABLE_DIRECTX())
1710 DWriteContext_Flush(s_dwc);
1711#endif
1712
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001713 /*
1714 * Note: FrameRect() excludes right and bottom of rectangle.
1715 */
1716 rc.left = FILL_X(gui.col);
1717 rc.top = FILL_Y(gui.row);
1718 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001719 if (mb_lefthalve(gui.row, gui.col))
1720 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001721 rc.bottom = rc.top + gui.char_height;
1722 hbr = CreateSolidBrush(color);
1723 FrameRect(s_hdc, &rc, hbr);
1724 DeleteBrush(hbr);
1725}
1726/*
1727 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1728 * color "color".
1729 */
1730 void
1731gui_mch_draw_part_cursor(
1732 int w,
1733 int h,
1734 guicolor_T color)
1735{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001736 RECT rc;
1737
1738 /*
1739 * Note: FillRect() excludes right and bottom of rectangle.
1740 */
1741 rc.left =
1742#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001743 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001744 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1745#endif
1746 FILL_X(gui.col);
1747 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1748 rc.right = rc.left + w;
1749 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001750
Bram Moolenaar92467d32017-12-05 13:22:16 +01001751 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001752}
1753
1754
1755/*
1756 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1757 * dead key's nominal character and re-post the original message.
1758 */
1759 static void
1760outputDeadKey_rePost(MSG originalMsg)
1761{
1762 static MSG deadCharExpel;
1763
1764 if (!dead_key)
1765 return;
1766
1767 dead_key = 0;
1768
Bram Moolenaar734a8672019-12-02 22:49:38 +01001769 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001770 deadCharExpel.message = originalMsg.message;
1771 deadCharExpel.hwnd = originalMsg.hwnd;
1772 deadCharExpel.wParam = VK_SPACE;
1773
1774 MyTranslateMessage(&deadCharExpel);
1775
Bram Moolenaar734a8672019-12-02 22:49:38 +01001776 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1778 originalMsg.lParam);
1779}
1780
1781
1782/*
1783 * Process a single Windows message.
1784 * If one is not available we hang until one is.
1785 */
1786 static void
1787process_message(void)
1788{
1789 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001790 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001791 char_u string[40];
1792 int i;
1793 int modifiers = 0;
1794 int key;
1795#ifdef FEAT_MENU
1796 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1797#endif
1798
1799 pGetMessage(&msg, NULL, 0, 0);
1800
1801#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001802 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001803 if (msg.message == WM_OLE)
1804 {
1805 char_u *str = (char_u *)msg.lParam;
1806 if (str == NULL || *str == NUL)
1807 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001808 // Message can't be ours, forward it. Fixes problem with Ultramon
1809 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001810 pDispatchMessage(&msg);
1811 }
1812 else
1813 {
1814 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001815 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001816 }
1817 return;
1818 }
1819#endif
1820
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001822 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1824 {
1825 HandleMouseHide(msg.message, msg.lParam);
1826 return;
1827 }
1828#endif
1829
1830 /*
1831 * Check if it's a special key that we recognise. If not, call
1832 * TranslateMessage().
1833 */
1834 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1835 {
1836 vk = (int) msg.wParam;
1837
1838 /*
1839 * Handle dead keys in special conditions in other cases we let Windows
1840 * handle them and do not interfere.
1841 *
1842 * The dead_key flag must be reset on several occasions:
1843 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1844 * consumed at that point (This is when we let Windows combine the
1845 * dead character on its own)
1846 *
1847 * - Before doing something special such as regenerating keypresses to
1848 * expel the dead character as this could trigger an infinite loop if
1849 * for some reason MyTranslateMessage() do not trigger a call
1850 * immediately to _OnChar() (or _OnSysChar()).
1851 */
1852 if (dead_key)
1853 {
1854 /*
1855 * If a dead key was pressed and the user presses VK_SPACE,
1856 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1857 * with the dead char now, so do nothing special and let Windows
1858 * handle it.
1859 *
1860 * Note that VK_SPACE combines with the dead_key's character and
1861 * only one WM_CHAR will be generated by TranslateMessage(), in
1862 * the two other cases two WM_CHAR will be generated: the dead
1863 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1864 * user expects.
1865 */
1866 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1867 {
1868 dead_key = 0;
1869 MyTranslateMessage(&msg);
1870 return;
1871 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001872 // In modes where we are not typing, dead keys should behave
1873 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1875 {
1876 outputDeadKey_rePost(msg);
1877 return;
1878 }
1879 }
1880
Bram Moolenaar734a8672019-12-02 22:49:38 +01001881 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001882 if (vk == VK_CANCEL)
1883 {
1884 trash_input_buf();
1885 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001886 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001887 string[0] = Ctrl_C;
1888 add_to_input_buf(string, 1);
1889 }
1890
1891 for (i = 0; special_keys[i].key_sym != 0; i++)
1892 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001893 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894 if (special_keys[i].key_sym == vk
1895 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1896 {
1897 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001898 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001899 * is a key that would normally trigger the dead key nominal
1900 * character output (such as a NUMPAD printable character or
1901 * the TAB key, etc...).
1902 */
1903 if (dead_key && (special_keys[i].vim_code0 == 'K'
1904 || vk == VK_TAB || vk == CAR))
1905 {
1906 outputDeadKey_rePost(msg);
1907 return;
1908 }
1909
1910#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001911 // Check for <F10>: Windows selects the menu. When <F10> is
1912 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001913 if (vk == VK_F10
1914 && gui.menu_is_active
1915 && check_map(k10, State, FALSE, TRUE, FALSE,
1916 NULL, NULL) == NULL)
1917 break;
1918#endif
1919 if (GetKeyState(VK_SHIFT) & 0x8000)
1920 modifiers |= MOD_MASK_SHIFT;
1921 /*
1922 * Don't use caps-lock as shift, because these are special keys
1923 * being considered here, and we only want letters to get
1924 * shifted -- webb
1925 */
1926 /*
1927 if (GetKeyState(VK_CAPITAL) & 0x0001)
1928 modifiers ^= MOD_MASK_SHIFT;
1929 */
1930 if (GetKeyState(VK_CONTROL) & 0x8000)
1931 modifiers |= MOD_MASK_CTRL;
1932 if (GetKeyState(VK_MENU) & 0x8000)
1933 modifiers |= MOD_MASK_ALT;
1934
1935 if (special_keys[i].vim_code1 == NUL)
1936 key = special_keys[i].vim_code0;
1937 else
1938 key = TO_SPECIAL(special_keys[i].vim_code0,
1939 special_keys[i].vim_code1);
1940 key = simplify_key(key, &modifiers);
1941 if (key == CSI)
1942 key = K_CSI;
1943
1944 if (modifiers)
1945 {
1946 string[0] = CSI;
1947 string[1] = KS_MODIFIER;
1948 string[2] = modifiers;
1949 add_to_input_buf(string, 3);
1950 }
1951
1952 if (IS_SPECIAL(key))
1953 {
1954 string[0] = CSI;
1955 string[1] = K_SECOND(key);
1956 string[2] = K_THIRD(key);
1957 add_to_input_buf(string, 3);
1958 }
1959 else
1960 {
1961 int len;
1962
Bram Moolenaar734a8672019-12-02 22:49:38 +01001963 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001964 len = char_to_string(key, string, 40, FALSE);
1965 add_to_input_buf(string, len);
1966 }
1967 break;
1968 }
1969 }
1970 if (special_keys[i].key_sym == 0)
1971 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001972 // Some keys need C-S- where they should only need C-.
1973 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1974 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001975 if (vk != 0xff
1976 && (GetKeyState(VK_CONTROL) & 0x8000)
1977 && !(GetKeyState(VK_SHIFT) & 0x8000)
1978 && !(GetKeyState(VK_MENU) & 0x8000))
1979 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001980 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001981 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1982 {
1983 string[0] = Ctrl_HAT;
1984 add_to_input_buf(string, 1);
1985 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001986 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
1987 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001988 {
1989 string[0] = Ctrl__;
1990 add_to_input_buf(string, 1);
1991 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001992 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001993 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1994 {
1995 string[0] = Ctrl_AT;
1996 add_to_input_buf(string, 1);
1997 }
1998 else
1999 MyTranslateMessage(&msg);
2000 }
2001 else
2002 MyTranslateMessage(&msg);
2003 }
2004 }
2005#ifdef FEAT_MBYTE_IME
2006 else if (msg.message == WM_IME_NOTIFY)
2007 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2008 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002009 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002010 MyTranslateMessage(&msg);
2011#endif
2012#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002013// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002014 else if (msg.message == WM_IME_STARTCOMPOSITION)
2015 {
2016 POINT point;
2017
2018 global_ime_set_font(&norm_logfont);
2019 point.x = FILL_X(gui.col);
2020 point.y = FILL_Y(gui.row);
2021 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2022 global_ime_set_position(&point);
2023 }
2024#endif
2025
2026#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002027 // Check for <F10>: Default effect is to select the menu. When <F10> is
2028 // mapped we need to stop it here to avoid strange effects (e.g., for the
2029 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002030 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2031 NULL, NULL) == NULL)
2032#endif
2033 pDispatchMessage(&msg);
2034}
2035
2036/*
2037 * Catch up with any queued events. This may put keyboard input into the
2038 * input buffer, call resize call-backs, trigger timers etc. If there is
2039 * nothing in the event queue (& no timers pending), then we return
2040 * immediately.
2041 */
2042 void
2043gui_mch_update(void)
2044{
2045 MSG msg;
2046
2047 if (!s_busy_processing)
2048 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2049 && !vim_is_input_buf_full())
2050 process_message();
2051}
2052
Bram Moolenaar4231da42016-06-02 14:30:04 +02002053 static void
2054remove_any_timer(void)
2055{
2056 MSG msg;
2057
2058 if (s_wait_timer != 0 && !s_timed_out)
2059 {
2060 KillTimer(NULL, s_wait_timer);
2061
Bram Moolenaar734a8672019-12-02 22:49:38 +01002062 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002063 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2064 ;
2065 s_wait_timer = 0;
2066 }
2067}
2068
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069/*
2070 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2071 * from the keyboard.
2072 * wtime == -1 Wait forever.
2073 * wtime == 0 This should never happen.
2074 * wtime > 0 Wait wtime milliseconds for a character.
2075 * Returns OK if a character was found to be available within the given time,
2076 * or FAIL otherwise.
2077 */
2078 int
2079gui_mch_wait_for_chars(int wtime)
2080{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081 int focus;
2082
2083 s_timed_out = FALSE;
2084
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002085 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002086 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002087 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088 if (s_busy_processing)
2089 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002090
2091 // When called with "wtime" zero, just want one msec.
2092 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 (TIMERPROC)_OnTimer);
2094 }
2095
2096 allow_scrollbar = TRUE;
2097
2098 focus = gui.in_focus;
2099 while (!s_timed_out)
2100 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002101 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002102 if (gui.in_focus != focus)
2103 {
2104 if (gui.in_focus)
2105 gui_mch_start_blink();
2106 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002107 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108 focus = gui.in_focus;
2109 }
2110
2111 if (s_need_activate)
2112 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002113 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 s_need_activate = FALSE;
2115 }
2116
Bram Moolenaar4231da42016-06-02 14:30:04 +02002117#ifdef FEAT_TIMERS
2118 did_add_timer = FALSE;
2119#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002121 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002122 for (;;)
2123 {
2124 MSG msg;
2125
2126 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002127# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002128 if (did_add_timer)
2129 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002130# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002131 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2132 {
2133 process_message();
2134 break;
2135 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 else if (input_available()
2137 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2138 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002139 break;
2140 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002141#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002142 // Don't use gui_mch_update() because then we will spin-lock until a
2143 // char arrives, instead we use GetMessage() to hang until an
2144 // event arrives. No need to check for input_buf_full because we are
2145 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002146 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002147#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148
2149 if (input_available())
2150 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002151 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002152 allow_scrollbar = FALSE;
2153
Bram Moolenaar89c00032019-09-04 13:53:21 +02002154 // Clear pending mouse button, the release event may have been
2155 // taken by the dialog window. But don't do this when getting
2156 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002157 if (!s_getting_focus)
2158 s_button_pending = -1;
2159
2160 return OK;
2161 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002162
2163#ifdef FEAT_TIMERS
2164 if (did_add_timer)
2165 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002166 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002167 remove_any_timer();
2168 break;
2169 }
2170#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002171 }
2172 allow_scrollbar = FALSE;
2173 return FAIL;
2174}
2175
2176/*
2177 * Clear a rectangular region of the screen from text pos (row1, col1) to
2178 * (row2, col2) inclusive.
2179 */
2180 void
2181gui_mch_clear_block(
2182 int row1,
2183 int col1,
2184 int row2,
2185 int col2)
2186{
2187 RECT rc;
2188
2189 /*
2190 * Clear one extra pixel at the far right, for when bold characters have
2191 * spilled over to the window border.
2192 * Note: FillRect() excludes right and bottom of rectangle.
2193 */
2194 rc.left = FILL_X(col1);
2195 rc.top = FILL_Y(row1);
2196 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2197 rc.bottom = FILL_Y(row2 + 1);
2198 clear_rect(&rc);
2199}
2200
2201/*
2202 * Clear the whole text window.
2203 */
2204 void
2205gui_mch_clear_all(void)
2206{
2207 RECT rc;
2208
2209 rc.left = 0;
2210 rc.top = 0;
2211 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2212 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2213 clear_rect(&rc);
2214}
2215/*
2216 * Menu stuff.
2217 */
2218
2219 void
2220gui_mch_enable_menu(int flag)
2221{
2222#ifdef FEAT_MENU
2223 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2224#endif
2225}
2226
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002227 void
2228gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002229 int x UNUSED,
2230 int y UNUSED,
2231 int w UNUSED,
2232 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002233{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002234 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002235}
2236
2237#if defined(FEAT_MENU) || defined(PROTO)
2238/*
2239 * Make menu item hidden or not hidden
2240 */
2241 void
2242gui_mch_menu_hidden(
2243 vimmenu_T *menu,
2244 int hidden)
2245{
2246 /*
2247 * This doesn't do what we want. Hmm, just grey the menu items for now.
2248 */
2249 /*
2250 if (hidden)
2251 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2252 else
2253 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2254 */
2255 gui_mch_menu_grey(menu, hidden);
2256}
2257
2258/*
2259 * This is called after setting all the menus to grey/hidden or not.
2260 */
2261 void
2262gui_mch_draw_menubar(void)
2263{
2264 DrawMenuBar(s_hwnd);
2265}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002266#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002267
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002268/*
2269 * Return the RGB value of a pixel as a long.
2270 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002271 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002272gui_mch_get_rgb(guicolor_T pixel)
2273{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002274 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2275 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002276}
2277
2278#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002279/*
2280 * Convert pixels in X to dialog units
2281 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002282 static WORD
2283PixelToDialogX(int numPixels)
2284{
2285 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2286}
2287
Bram Moolenaar734a8672019-12-02 22:49:38 +01002288/*
2289 * Convert pixels in Y to dialog units
2290 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002291 static WORD
2292PixelToDialogY(int numPixels)
2293{
2294 return (WORD)((numPixels * 8) / s_dlgfntheight);
2295}
2296
Bram Moolenaar734a8672019-12-02 22:49:38 +01002297/*
2298 * Return the width in pixels of the given text in the given DC.
2299 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002300 static int
2301GetTextWidth(HDC hdc, char_u *str, int len)
2302{
2303 SIZE size;
2304
2305 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2306 return size.cx;
2307}
2308
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309/*
2310 * Return the width in pixels of the given text in the given DC, taking care
2311 * of 'encoding' to active codepage conversion.
2312 */
2313 static int
2314GetTextWidthEnc(HDC hdc, char_u *str, int len)
2315{
2316 SIZE size;
2317 WCHAR *wstr;
2318 int n;
2319 int wlen = len;
2320
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002321 wstr = enc_to_utf16(str, &wlen);
2322 if (wstr == NULL)
2323 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002324
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002325 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2326 vim_free(wstr);
2327 if (n)
2328 return size.cx;
2329 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002330}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002332static void get_work_area(RECT *spi_rect);
2333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002334/*
2335 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002336 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2337 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 */
2339 static BOOL
2340CenterWindow(
2341 HWND hwndChild,
2342 HWND hwndParent)
2343{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002344 HMONITOR mon;
2345 MONITORINFO moninfo;
2346 RECT rChild, rParent, rScreen;
2347 int wChild, hChild, wParent, hParent;
2348 int xNew, yNew;
2349 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002350
2351 GetWindowRect(hwndChild, &rChild);
2352 wChild = rChild.right - rChild.left;
2353 hChild = rChild.bottom - rChild.top;
2354
Bram Moolenaar734a8672019-12-02 22:49:38 +01002355 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002356 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002357 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 else
2359 GetWindowRect(hwndParent, &rParent);
2360 wParent = rParent.right - rParent.left;
2361 hParent = rParent.bottom - rParent.top;
2362
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002363 moninfo.cbSize = sizeof(MONITORINFO);
2364 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2365 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002367 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002368 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002369 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002370 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002371 hdc = GetDC(hwndChild);
2372 rScreen.left = 0;
2373 rScreen.top = 0;
2374 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2375 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2376 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377 }
2378
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002379 xNew = rParent.left + ((wParent - wChild) / 2);
2380 if (xNew < rScreen.left)
2381 xNew = rScreen.left;
2382 else if ((xNew + wChild) > rScreen.right)
2383 xNew = rScreen.right - wChild;
2384
2385 yNew = rParent.top + ((hParent - hChild) / 2);
2386 if (yNew < rScreen.top)
2387 yNew = rScreen.top;
2388 else if ((yNew + hChild) > rScreen.bottom)
2389 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002390
2391 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2392 SWP_NOSIZE | SWP_NOZORDER);
2393}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002394#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002395
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396#if defined(FEAT_TOOLBAR) || defined(PROTO)
2397 void
2398gui_mch_show_toolbar(int showit)
2399{
2400 if (s_toolbarhwnd == NULL)
2401 return;
2402
2403 if (showit)
2404 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002405# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002406 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002407# define TB_SETUNICODEFORMAT 0x2005
2408# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002409 // Enable unicode support
2410 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2411 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002412 ShowWindow(s_toolbarhwnd, SW_SHOW);
2413 }
2414 else
2415 ShowWindow(s_toolbarhwnd, SW_HIDE);
2416}
2417
Bram Moolenaar734a8672019-12-02 22:49:38 +01002418// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002419# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002420
2421#endif
2422
2423#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2424 static void
2425add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2426{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002427 WCHAR *wn;
2428 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002429
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002430 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002431 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002432 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002434 infow.cbSize = sizeof(infow);
2435 infow.fMask = MIIM_TYPE | MIIM_ID;
2436 infow.wID = item_id;
2437 infow.fType = MFT_STRING;
2438 infow.dwTypeData = wn;
2439 infow.cch = (UINT)wcslen(wn);
2440 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2441 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002442}
2443
2444 static void
2445show_tabline_popup_menu(void)
2446{
2447 HMENU tab_pmenu;
2448 long rval;
2449 POINT pt;
2450
Bram Moolenaar734a8672019-12-02 22:49:38 +01002451 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002452 if (hold_gui_events
2453# ifdef FEAT_CMDWIN
2454 || cmdwin_type != 0
2455# endif
2456 )
2457 return;
2458
2459 tab_pmenu = CreatePopupMenu();
2460 if (tab_pmenu == NULL)
2461 return;
2462
2463 if (first_tabpage->tp_next != NULL)
2464 add_tabline_popup_menu_entry(tab_pmenu,
2465 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2466 add_tabline_popup_menu_entry(tab_pmenu,
2467 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2468 add_tabline_popup_menu_entry(tab_pmenu,
2469 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2470
2471 GetCursorPos(&pt);
2472 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2473 NULL);
2474
2475 DestroyMenu(tab_pmenu);
2476
Bram Moolenaar734a8672019-12-02 22:49:38 +01002477 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002478 if (rval > 0)
2479 {
2480 TCHITTESTINFO htinfo;
2481 int idx;
2482
2483 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2484 return;
2485
2486 htinfo.pt.x = pt.x;
2487 htinfo.pt.y = pt.y;
2488 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2489 if (idx == -1)
2490 idx = 0;
2491 else
2492 idx += 1;
2493
2494 send_tabline_menu_event(idx, (int)rval);
2495 }
2496}
2497
2498/*
2499 * Show or hide the tabline.
2500 */
2501 void
2502gui_mch_show_tabline(int showit)
2503{
2504 if (s_tabhwnd == NULL)
2505 return;
2506
2507 if (!showit != !showing_tabline)
2508 {
2509 if (showit)
2510 ShowWindow(s_tabhwnd, SW_SHOW);
2511 else
2512 ShowWindow(s_tabhwnd, SW_HIDE);
2513 showing_tabline = showit;
2514 }
2515}
2516
2517/*
2518 * Return TRUE when tabline is displayed.
2519 */
2520 int
2521gui_mch_showing_tabline(void)
2522{
2523 return s_tabhwnd != NULL && showing_tabline;
2524}
2525
2526/*
2527 * Update the labels of the tabline.
2528 */
2529 void
2530gui_mch_update_tabline(void)
2531{
2532 tabpage_T *tp;
2533 TCITEM tie;
2534 int nr = 0;
2535 int curtabidx = 0;
2536 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002537 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002538
2539 if (s_tabhwnd == NULL)
2540 return;
2541
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002542# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002543 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002544# define CCM_SETUNICODEFORMAT 0x2005
2545# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002546 // Enable unicode support
2547 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002548
2549 tie.mask = TCIF_TEXT;
2550 tie.iImage = -1;
2551
Bram Moolenaar734a8672019-12-02 22:49:38 +01002552 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2554
Bram Moolenaar734a8672019-12-02 22:49:38 +01002555 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002556 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2557 {
2558 if (tp == curtab)
2559 curtabidx = nr;
2560
2561 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2562 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002563 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002564 tie.pszText = "-Empty-";
2565 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2566 tabadded = 1;
2567 }
2568
2569 get_tabline_label(tp, FALSE);
2570 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002571
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002572 wstr = enc_to_utf16(NameBuff, NULL);
2573 if (wstr != NULL)
2574 {
2575 TCITEMW tiw;
2576
2577 tiw.mask = TCIF_TEXT;
2578 tiw.iImage = -1;
2579 tiw.pszText = wstr;
2580 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2581 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583 }
2584
Bram Moolenaar734a8672019-12-02 22:49:38 +01002585 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2587 TabCtrl_DeleteItem(s_tabhwnd, nr);
2588
2589 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2590 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2591
Bram Moolenaar734a8672019-12-02 22:49:38 +01002592 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2594 RedrawWindow(s_tabhwnd, NULL, NULL,
2595 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2596
2597 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2598 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2599}
2600
2601/*
2602 * Set the current tab to "nr". First tab is 1.
2603 */
2604 void
2605gui_mch_set_curtab(int nr)
2606{
2607 if (s_tabhwnd == NULL)
2608 return;
2609
2610 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2611 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2612}
2613
2614#endif
2615
2616/*
2617 * ":simalt" command.
2618 */
2619 void
2620ex_simalt(exarg_T *eap)
2621{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002622 char_u *keys = eap->arg;
2623 int fill_typebuf = FALSE;
2624 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625
2626 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2627 while (*keys)
2628 {
2629 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002630 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002631 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2632 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002633 fill_typebuf = TRUE;
2634 }
2635 if (fill_typebuf)
2636 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002637 // Put a NOP in the typeahead buffer so that the message will get
2638 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002639 key_name[0] = K_SPECIAL;
2640 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002641 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002642 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002643#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002644 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002645#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002646 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647 }
2648}
2649
2650/*
2651 * Create the find & replace dialogs.
2652 * You can't have both at once: ":find" when replace is showing, destroys
2653 * the replace dialog first, and the other way around.
2654 */
2655#ifdef MSWIN_FIND_REPLACE
2656 static void
2657initialise_findrep(char_u *initial_string)
2658{
2659 int wword = FALSE;
2660 int mcase = !p_ic;
2661 char_u *entry_text;
2662
Bram Moolenaar734a8672019-12-02 22:49:38 +01002663 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002664 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2665
2666 s_findrep_struct.hwndOwner = s_hwnd;
2667 s_findrep_struct.Flags = FR_DOWN;
2668 if (mcase)
2669 s_findrep_struct.Flags |= FR_MATCHCASE;
2670 if (wword)
2671 s_findrep_struct.Flags |= FR_WHOLEWORD;
2672 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002673 {
2674 WCHAR *p = enc_to_utf16(entry_text, NULL);
2675 if (p != NULL)
2676 {
2677 int len = s_findrep_struct.wFindWhatLen - 1;
2678
2679 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2680 s_findrep_struct.lpstrFindWhat[len] = NUL;
2681 vim_free(p);
2682 }
2683 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002684 vim_free(entry_text);
2685}
2686#endif
2687
2688 static void
2689set_window_title(HWND hwnd, char *title)
2690{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002691 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002692 {
2693 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002694
Bram Moolenaar734a8672019-12-02 22:49:38 +01002695 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002696 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2697 if (wbuf != NULL)
2698 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002699 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002700 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701 }
2702 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002703 else
2704 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002705}
2706
2707 void
2708gui_mch_find_dialog(exarg_T *eap)
2709{
2710#ifdef MSWIN_FIND_REPLACE
2711 if (s_findrep_msg != 0)
2712 {
2713 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2714 DestroyWindow(s_findrep_hwnd);
2715
2716 if (!IsWindow(s_findrep_hwnd))
2717 {
2718 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002719 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 }
2721
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002722 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 (void)SetFocus(s_findrep_hwnd);
2724
2725 s_findrep_is_find = TRUE;
2726 }
2727#endif
2728}
2729
2730
2731 void
2732gui_mch_replace_dialog(exarg_T *eap)
2733{
2734#ifdef MSWIN_FIND_REPLACE
2735 if (s_findrep_msg != 0)
2736 {
2737 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2738 DestroyWindow(s_findrep_hwnd);
2739
2740 if (!IsWindow(s_findrep_hwnd))
2741 {
2742 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002743 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 }
2745
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002746 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 (void)SetFocus(s_findrep_hwnd);
2748
2749 s_findrep_is_find = FALSE;
2750 }
2751#endif
2752}
2753
2754
2755/*
2756 * Set visibility of the pointer.
2757 */
2758 void
2759gui_mch_mousehide(int hide)
2760{
2761 if (hide != gui.pointer_hidden)
2762 {
2763 ShowCursor(!hide);
2764 gui.pointer_hidden = hide;
2765 }
2766}
2767
2768#ifdef FEAT_MENU
2769 static void
2770gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2771{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002772 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 gui_mch_mousehide(FALSE);
2774
2775 (void)TrackPopupMenu(
2776 (HMENU)menu->submenu_id,
2777 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2778 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002779 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002780 s_hwnd,
2781 NULL);
2782 /*
2783 * NOTE: The pop-up menu can eat the mouse up event.
2784 * We deal with this in normal.c.
2785 */
2786}
2787#endif
2788
2789/*
2790 * Got a message when the system will go down.
2791 */
2792 static void
2793_OnEndSession(void)
2794{
2795 getout_preserve_modified(1);
2796}
2797
2798/*
2799 * Get this message when the user clicks on the cross in the top right corner
2800 * of a Windows95 window.
2801 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002802 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002803_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002804{
2805 gui_shell_closed();
2806}
2807
2808/*
2809 * Get a message when the window is being destroyed.
2810 */
2811 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002812_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002813{
2814 if (!destroying)
2815 _OnClose(hwnd);
2816}
2817
2818 static void
2819_OnPaint(
2820 HWND hwnd)
2821{
2822 if (!IsMinimized(hwnd))
2823 {
2824 PAINTSTRUCT ps;
2825
Bram Moolenaar734a8672019-12-02 22:49:38 +01002826 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002827 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828
Bram Moolenaar734a8672019-12-02 22:49:38 +01002829 // prevent multi-byte characters from misprinting on an invalid
2830 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831 if (has_mbyte)
2832 {
2833 RECT rect;
2834
2835 GetClientRect(hwnd, &rect);
2836 ps.rcPaint.left = rect.left;
2837 ps.rcPaint.right = rect.right;
2838 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002839
2840 if (!IsRectEmpty(&ps.rcPaint))
2841 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002842 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2843 ps.rcPaint.right - ps.rcPaint.left + 1,
2844 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2845 }
2846
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002847 EndPaint(hwnd, &ps);
2848 }
2849}
2850
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851 static void
2852_OnSize(
2853 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002854 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002855 int cx,
2856 int cy)
2857{
2858 if (!IsMinimized(hwnd))
2859 {
2860 gui_resize_shell(cx, cy);
2861
2862#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002863 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002864 gui_mswin_get_menu_height(TRUE);
2865#endif
2866 }
2867}
2868
2869 static void
2870_OnSetFocus(
2871 HWND hwnd,
2872 HWND hwndOldFocus)
2873{
2874 gui_focus_change(TRUE);
2875 s_getting_focus = TRUE;
2876 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2877}
2878
2879 static void
2880_OnKillFocus(
2881 HWND hwnd,
2882 HWND hwndNewFocus)
2883{
2884 gui_focus_change(FALSE);
2885 s_getting_focus = FALSE;
2886 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2887}
2888
2889/*
2890 * Get a message when the user switches back to vim
2891 */
2892 static LRESULT
2893_OnActivateApp(
2894 HWND hwnd,
2895 BOOL fActivate,
2896 DWORD dwThreadId)
2897{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002898 // we call gui_focus_change() in _OnSetFocus()
2899 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2901}
2902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 void
2904gui_mch_destroy_scrollbar(scrollbar_T *sb)
2905{
2906 DestroyWindow(sb->id);
2907}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002908
2909/*
2910 * Get current mouse coordinates in text window.
2911 */
2912 void
2913gui_mch_getmouse(int *x, int *y)
2914{
2915 RECT rct;
2916 POINT mp;
2917
2918 (void)GetWindowRect(s_textArea, &rct);
2919 (void)GetCursorPos((LPPOINT)&mp);
2920 *x = (int)(mp.x - rct.left);
2921 *y = (int)(mp.y - rct.top);
2922}
2923
2924/*
2925 * Move mouse pointer to character at (x, y).
2926 */
2927 void
2928gui_mch_setmouse(int x, int y)
2929{
2930 RECT rct;
2931
2932 (void)GetWindowRect(s_textArea, &rct);
2933 (void)SetCursorPos(x + gui.border_offset + rct.left,
2934 y + gui.border_offset + rct.top);
2935}
2936
2937 static void
2938gui_mswin_get_valid_dimensions(
2939 int w,
2940 int h,
2941 int *valid_w,
2942 int *valid_h)
2943{
2944 int base_width, base_height;
2945
2946 base_width = gui_get_base_width()
2947 + (GetSystemMetrics(SM_CXFRAME) +
2948 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2949 base_height = gui_get_base_height()
2950 + (GetSystemMetrics(SM_CYFRAME) +
2951 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2952 + GetSystemMetrics(SM_CYCAPTION)
2953#ifdef FEAT_MENU
2954 + gui_mswin_get_menu_height(FALSE)
2955#endif
2956 ;
2957 *valid_w = base_width +
2958 ((w - base_width) / gui.char_width) * gui.char_width;
2959 *valid_h = base_height +
2960 ((h - base_height) / gui.char_height) * gui.char_height;
2961}
2962
2963 void
2964gui_mch_flash(int msec)
2965{
2966 RECT rc;
2967
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002968#if defined(FEAT_DIRECTX)
2969 if (IS_ENABLE_DIRECTX())
2970 DWriteContext_Flush(s_dwc);
2971#endif
2972
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002973 /*
2974 * Note: InvertRect() excludes right and bottom of rectangle.
2975 */
2976 rc.left = 0;
2977 rc.top = 0;
2978 rc.right = gui.num_cols * gui.char_width;
2979 rc.bottom = gui.num_rows * gui.char_height;
2980 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002981 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002982
Bram Moolenaar734a8672019-12-02 22:49:38 +01002983 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984
2985 InvertRect(s_hdc, &rc);
2986}
2987
2988/*
2989 * Return flags used for scrolling.
2990 * The SW_INVALIDATE is required when part of the window is covered or
2991 * off-screen. Refer to MS KB Q75236.
2992 */
2993 static int
2994get_scroll_flags(void)
2995{
2996 HWND hwnd;
2997 RECT rcVim, rcOther, rcDest;
2998
2999 GetWindowRect(s_hwnd, &rcVim);
3000
Bram Moolenaar734a8672019-12-02 22:49:38 +01003001 // Check if the window is partly above or below the screen. We don't care
3002 // about partly left or right of the screen, it is not relevant when
3003 // scrolling up or down.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003004 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3005 return SW_INVALIDATE;
3006
Bram Moolenaar734a8672019-12-02 22:49:38 +01003007 // Check if there is an window (partly) on top of us.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003008 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3009 if (IsWindowVisible(hwnd))
3010 {
3011 GetWindowRect(hwnd, &rcOther);
3012 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3013 return SW_INVALIDATE;
3014 }
3015 return 0;
3016}
3017
3018/*
3019 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3020 * may not be scrolled out properly.
3021 * For gVim, when _OnScroll() is repeated, the character at the
3022 * previous cursor position may be left drawn after scroll.
3023 * The problem can be avoided by calling GetPixel() to get a pixel in
3024 * the region before ScrollWindowEx().
3025 */
3026 static void
3027intel_gpu_workaround(void)
3028{
3029 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3030}
3031
3032/*
3033 * Delete the given number of lines from the given row, scrolling up any
3034 * text further down within the scroll region.
3035 */
3036 void
3037gui_mch_delete_lines(
3038 int row,
3039 int num_lines)
3040{
3041 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003042
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043 rc.left = FILL_X(gui.scroll_region_left);
3044 rc.right = FILL_X(gui.scroll_region_right + 1);
3045 rc.top = FILL_Y(row);
3046 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3047
Bram Moolenaar92467d32017-12-05 13:22:16 +01003048#if defined(FEAT_DIRECTX)
3049 if (IS_ENABLE_DIRECTX())
3050 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003051 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3052 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003053 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003054 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003055#endif
3056 {
3057 intel_gpu_workaround();
3058 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003060 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003061 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003062
Bram Moolenaar734a8672019-12-02 22:49:38 +01003063 // This seems to be required to avoid the cursor disappearing when
3064 // scrolling such that the cursor ends up in the top-left character on
3065 // the screen... But why? (Webb)
3066 // It's probably fixed by disabling drawing the cursor while scrolling.
3067 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003068
3069 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3070 gui.scroll_region_left,
3071 gui.scroll_region_bot, gui.scroll_region_right);
3072}
3073
3074/*
3075 * Insert the given number of lines before the given row, scrolling down any
3076 * following text within the scroll region.
3077 */
3078 void
3079gui_mch_insert_lines(
3080 int row,
3081 int num_lines)
3082{
3083 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003084
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003085 rc.left = FILL_X(gui.scroll_region_left);
3086 rc.right = FILL_X(gui.scroll_region_right + 1);
3087 rc.top = FILL_Y(row);
3088 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003089
3090#if defined(FEAT_DIRECTX)
3091 if (IS_ENABLE_DIRECTX())
3092 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003093 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3094 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003095 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003096 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003097#endif
3098 {
3099 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003100 // The SW_INVALIDATE is required when part of the window is covered or
3101 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003102 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003103 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003104 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003105 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003106
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107 gui_clear_block(row, gui.scroll_region_left,
3108 row + num_lines - 1, gui.scroll_region_right);
3109}
3110
3111
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003112 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003113gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003114{
3115#if defined(FEAT_DIRECTX)
3116 DWriteContext_Close(s_dwc);
3117 DWrite_Final();
3118 s_dwc = NULL;
3119#endif
3120
3121 ReleaseDC(s_textArea, s_hdc);
3122 DeleteObject(s_brush);
3123
3124#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003125 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3127#endif
3128
Bram Moolenaar734a8672019-12-02 22:49:38 +01003129 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 if (s_hwnd != NULL)
3131 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003132 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133 DestroyWindow(s_hwnd);
3134 }
3135
3136#ifdef GLOBAL_IME
3137 global_ime_end();
3138#endif
3139}
3140
3141 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003142logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003143{
3144 char *p;
3145 char *res;
3146 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003147 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003148 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003149 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003150
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003151 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3152 if (font_name == NULL)
3153 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003154 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003155 quality_name = quality_id2name((int)lf.lfQuality);
3156
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003157 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003158 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003159 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003160 if (res != NULL)
3161 {
3162 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003163 // make a normal font string out of the lf thing:
3164 points = pixels_to_points(
3165 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3166 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3167 sprintf((char *)p, "%s:h%d", font_name, points);
3168 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003169 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 while (*p)
3171 {
3172 if (*p == ' ')
3173 *p = '_';
3174 ++p;
3175 }
3176 if (lf.lfItalic)
3177 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003178 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003179 STRCAT(p, ":b");
3180 if (lf.lfUnderline)
3181 STRCAT(p, ":u");
3182 if (lf.lfStrikeOut)
3183 STRCAT(p, ":s");
3184 if (charset_name != NULL)
3185 {
3186 STRCAT(p, ":c");
3187 STRCAT(p, charset_name);
3188 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003189 if (quality_name != NULL)
3190 {
3191 STRCAT(p, ":q");
3192 STRCAT(p, quality_name);
3193 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 }
3195
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003196 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003197 return (char_u *)res;
3198}
3199
3200
3201#ifdef FEAT_MBYTE_IME
3202/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 * 'guifont'
3205 */
3206 static void
3207update_im_font(void)
3208{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003209 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003210
3211 if (p_guifontwide != NULL && *p_guifontwide != NUL
3212 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003213 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 norm_logfont = lf_wide;
3215 else
3216 norm_logfont = sub_logfont;
3217 im_set_font(&norm_logfont);
3218}
3219#endif
3220
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221/*
3222 * Handler of gui.wide_font (p_guifontwide) changed notification.
3223 */
3224 void
3225gui_mch_wide_font_changed(void)
3226{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003227 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003229#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003230 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003231#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003232
3233 gui_mch_free_font(gui.wide_ital_font);
3234 gui.wide_ital_font = NOFONT;
3235 gui_mch_free_font(gui.wide_bold_font);
3236 gui.wide_bold_font = NOFONT;
3237 gui_mch_free_font(gui.wide_boldital_font);
3238 gui.wide_boldital_font = NOFONT;
3239
3240 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003241 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242 {
3243 if (!lf.lfItalic)
3244 {
3245 lf.lfItalic = TRUE;
3246 gui.wide_ital_font = get_font_handle(&lf);
3247 lf.lfItalic = FALSE;
3248 }
3249 if (lf.lfWeight < FW_BOLD)
3250 {
3251 lf.lfWeight = FW_BOLD;
3252 gui.wide_bold_font = get_font_handle(&lf);
3253 if (!lf.lfItalic)
3254 {
3255 lf.lfItalic = TRUE;
3256 gui.wide_boldital_font = get_font_handle(&lf);
3257 }
3258 }
3259 }
3260}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261
3262/*
3263 * Initialise vim to use the font with the given name.
3264 * Return FAIL if the font could not be loaded, OK otherwise.
3265 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003267gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003269 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003270 GuiFont font = NOFONT;
3271 char_u *p;
3272
Bram Moolenaar734a8672019-12-02 22:49:38 +01003273 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003274 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3275 font = get_font_handle(&lf);
3276 if (font == NOFONT)
3277 return FAIL;
3278
3279 if (font_name == NULL)
3280 font_name = (char_u *)lf.lfFaceName;
3281#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3282 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003283#endif
3284#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003285 sub_logfont = lf;
3286#endif
3287#ifdef FEAT_MBYTE_IME
3288 update_im_font();
3289#endif
3290 gui_mch_free_font(gui.norm_font);
3291 gui.norm_font = font;
3292 current_font_height = lf.lfHeight;
3293 GetFontSize(font);
3294
3295 p = logfont2name(lf);
3296 if (p != NULL)
3297 {
3298 hl_set_font_name(p);
3299
Bram Moolenaar734a8672019-12-02 22:49:38 +01003300 // When setting 'guifont' to "*" replace it with the actual font name.
3301 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003302 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3303 {
3304 vim_free(p_guifont);
3305 p_guifont = p;
3306 }
3307 else
3308 vim_free(p);
3309 }
3310
3311 gui_mch_free_font(gui.ital_font);
3312 gui.ital_font = NOFONT;
3313 gui_mch_free_font(gui.bold_font);
3314 gui.bold_font = NOFONT;
3315 gui_mch_free_font(gui.boldital_font);
3316 gui.boldital_font = NOFONT;
3317
3318 if (!lf.lfItalic)
3319 {
3320 lf.lfItalic = TRUE;
3321 gui.ital_font = get_font_handle(&lf);
3322 lf.lfItalic = FALSE;
3323 }
3324 if (lf.lfWeight < FW_BOLD)
3325 {
3326 lf.lfWeight = FW_BOLD;
3327 gui.bold_font = get_font_handle(&lf);
3328 if (!lf.lfItalic)
3329 {
3330 lf.lfItalic = TRUE;
3331 gui.boldital_font = get_font_handle(&lf);
3332 }
3333 }
3334
3335 return OK;
3336}
3337
3338#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003339# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340#endif
3341
3342/*
3343 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003344 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003345 */
3346 int
3347gui_mch_maximized(void)
3348{
3349 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003350 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351
3352 wp.length = sizeof(WINDOWPLACEMENT);
3353 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003354 {
3355 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003356 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003357 && wp.flags == WPF_RESTORETOMAXIMIZED))
3358 return TRUE;
3359 if (wp.showCmd == SW_SHOWMINIMIZED)
3360 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003362 // Assume the window is snapped when the sizes from two APIs differ.
3363 GetWindowRect(s_hwnd, &rc);
3364 if ((rc.right - rc.left !=
3365 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3366 || (rc.bottom - rc.top !=
3367 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3368 return TRUE;
3369 }
3370 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003371}
3372
3373/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003374 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3375 * is set. Compute the new Rows and Columns. This is like resizing the
3376 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 */
3378 void
3379gui_mch_newfont(void)
3380{
3381 RECT rect;
3382
3383 GetWindowRect(s_hwnd, &rect);
3384 if (win_socket_id == 0)
3385 {
3386 gui_resize_shell(rect.right - rect.left
3387 - (GetSystemMetrics(SM_CXFRAME) +
3388 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3389 rect.bottom - rect.top
3390 - (GetSystemMetrics(SM_CYFRAME) +
3391 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3392 - GetSystemMetrics(SM_CYCAPTION)
3393#ifdef FEAT_MENU
3394 - gui_mswin_get_menu_height(FALSE)
3395#endif
3396 );
3397 }
3398 else
3399 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003400 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 gui_resize_shell(rect.right - rect.left,
3402 rect.bottom - rect.top
3403#ifdef FEAT_MENU
3404 - gui_mswin_get_menu_height(FALSE)
3405#endif
3406 );
3407 }
3408}
3409
3410/*
3411 * Set the window title
3412 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413 void
3414gui_mch_settitle(
3415 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003416 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417{
3418 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3419}
3420
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003421#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003422// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3423// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003424static LPCSTR mshape_idcs[] =
3425{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003426 IDC_ARROW, // arrow
3427 MAKEINTRESOURCE(0), // blank
3428 IDC_IBEAM, // beam
3429 IDC_SIZENS, // updown
3430 IDC_SIZENS, // udsizing
3431 IDC_SIZEWE, // leftright
3432 IDC_SIZEWE, // lrsizing
3433 IDC_WAIT, // busy
3434 IDC_NO, // no
3435 IDC_ARROW, // crosshair
3436 IDC_ARROW, // hand1
3437 IDC_ARROW, // hand2
3438 IDC_ARROW, // pencil
3439 IDC_ARROW, // question
3440 IDC_ARROW, // right-arrow
3441 IDC_UPARROW, // up-arrow
3442 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003443};
3444
3445 void
3446mch_set_mouse_shape(int shape)
3447{
3448 LPCSTR idc;
3449
3450 if (shape == MSHAPE_HIDE)
3451 ShowCursor(FALSE);
3452 else
3453 {
3454 if (shape >= MSHAPE_NUMBERED)
3455 idc = IDC_ARROW;
3456 else
3457 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003458 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459 if (!p_mh)
3460 {
3461 POINT mp;
3462
Bram Moolenaar734a8672019-12-02 22:49:38 +01003463 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 (void)GetCursorPos((LPPOINT)&mp);
3465 (void)SetCursorPos(mp.x, mp.y);
3466 ShowCursor(TRUE);
3467 }
3468 }
3469}
3470#endif
3471
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003472#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474 * Wide version of convert_filter().
3475 */
3476 static WCHAR *
3477convert_filterW(char_u *s)
3478{
3479 char_u *tmp;
3480 int len;
3481 WCHAR *res;
3482
3483 tmp = convert_filter(s);
3484 if (tmp == NULL)
3485 return NULL;
3486 len = (int)STRLEN(s) + 3;
3487 res = enc_to_utf16(tmp, &len);
3488 vim_free(tmp);
3489 return res;
3490}
3491
3492/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003493 * Pop open a file browser and return the file selected, in allocated memory,
3494 * or NULL if Cancel is hit.
3495 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3496 * title - Title message for the file browser dialog.
3497 * dflt - Default name of file.
3498 * ext - Default extension to be added to files without extensions.
3499 * initdir - directory in which to open the browser (NULL = current dir)
3500 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003502 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003503gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 int saving,
3505 char_u *title,
3506 char_u *dflt,
3507 char_u *ext,
3508 char_u *initdir,
3509 char_u *filter)
3510{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003511 // We always use the wide function. This means enc_to_utf16() must work,
3512 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 OPENFILENAMEW fileStruct;
3514 WCHAR fileBuf[MAXPATHL];
3515 WCHAR *wp;
3516 int i;
3517 WCHAR *titlep = NULL;
3518 WCHAR *extp = NULL;
3519 WCHAR *initdirp = NULL;
3520 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003521 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522
3523 if (dflt == NULL)
3524 fileBuf[0] = NUL;
3525 else
3526 {
3527 wp = enc_to_utf16(dflt, NULL);
3528 if (wp == NULL)
3529 fileBuf[0] = NUL;
3530 else
3531 {
3532 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3533 fileBuf[i] = wp[i];
3534 fileBuf[i] = NUL;
3535 vim_free(wp);
3536 }
3537 }
3538
Bram Moolenaar734a8672019-12-02 22:49:38 +01003539 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 filterp = convert_filterW(filter);
3541
Bram Moolenaara80faa82020-04-12 19:37:17 +02003542 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003543# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003544 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003545 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003546# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003547 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003548# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003549
3550 if (title != NULL)
3551 titlep = enc_to_utf16(title, NULL);
3552 fileStruct.lpstrTitle = titlep;
3553
3554 if (ext != NULL)
3555 extp = enc_to_utf16(ext, NULL);
3556 fileStruct.lpstrDefExt = extp;
3557
3558 fileStruct.lpstrFile = fileBuf;
3559 fileStruct.nMaxFile = MAXPATHL;
3560 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003561 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3562 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003563 if (initdir != NULL && *initdir != NUL)
3564 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003565 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566 initdirp = enc_to_utf16(initdir, NULL);
3567 if (initdirp != NULL)
3568 {
3569 for (wp = initdirp; *wp != NUL; ++wp)
3570 if (*wp == '/')
3571 *wp = '\\';
3572 }
3573 fileStruct.lpstrInitialDir = initdirp;
3574 }
3575
3576 /*
3577 * TODO: Allow selection of multiple files. Needs another arg to this
3578 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3579 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3580 * files that don't exist yet, so I haven't put it in. What about
3581 * OFN_PATHMUSTEXIST?
3582 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3583 */
3584 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003585# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003586 if (curbuf->b_p_bin)
3587 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003588# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 if (saving)
3590 {
3591 if (!GetSaveFileNameW(&fileStruct))
3592 return NULL;
3593 }
3594 else
3595 {
3596 if (!GetOpenFileNameW(&fileStruct))
3597 return NULL;
3598 }
3599
3600 vim_free(filterp);
3601 vim_free(initdirp);
3602 vim_free(titlep);
3603 vim_free(extp);
3604
Bram Moolenaar734a8672019-12-02 22:49:38 +01003605 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003606 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003607 if (p == NULL)
3608 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003609
Bram Moolenaar734a8672019-12-02 22:49:38 +01003610 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611 SetFocus(s_hwnd);
3612
Bram Moolenaar734a8672019-12-02 22:49:38 +01003613 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003614 q = vim_strsave(shorten_fname1(p));
3615 vim_free(p);
3616 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003617}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618
3619
3620/*
3621 * Convert the string s to the proper format for a filter string by replacing
3622 * the \t and \n delimiters with \0.
3623 * Returns the converted string in allocated memory.
3624 *
3625 * Keep in sync with convert_filterW() above!
3626 */
3627 static char_u *
3628convert_filter(char_u *s)
3629{
3630 char_u *res;
3631 unsigned s_len = (unsigned)STRLEN(s);
3632 unsigned i;
3633
3634 res = alloc(s_len + 3);
3635 if (res != NULL)
3636 {
3637 for (i = 0; i < s_len; ++i)
3638 if (s[i] == '\t' || s[i] == '\n')
3639 res[i] = '\0';
3640 else
3641 res[i] = s[i];
3642 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003643 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003644 res[s_len + 1] = NUL;
3645 res[s_len + 2] = NUL;
3646 }
3647 return res;
3648}
3649
3650/*
3651 * Select a directory.
3652 */
3653 char_u *
3654gui_mch_browsedir(char_u *title, char_u *initdir)
3655{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003656 // We fake this: Use a filter that doesn't select anything and a default
3657 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3659 initdir, (char_u *)_("Directory\t*.nothing\n"));
3660}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003661#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663 static void
3664_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003665 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666 HDROP hDrop)
3667{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003668#define BUFPATHLEN _MAX_PATH
3669#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671 char szFile[BUFPATHLEN];
3672 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3673 UINT i;
3674 char_u **fnames;
3675 POINT pt;
3676 int_u modifiers = 0;
3677
Bram Moolenaar734a8672019-12-02 22:49:38 +01003678 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679
Bram Moolenaar734a8672019-12-02 22:49:38 +01003680 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003681 DragQueryPoint(hDrop, &pt);
3682 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3683
3684 reset_VIsual();
3685
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003686 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003687
3688 if (fnames != NULL)
3689 for (i = 0; i < cFiles; ++i)
3690 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3692 fnames[i] = utf16_to_enc(wszFile, NULL);
3693 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003694 {
3695 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3696 fnames[i] = vim_strsave((char_u *)szFile);
3697 }
3698 }
3699
3700 DragFinish(hDrop);
3701
3702 if (fnames != NULL)
3703 {
3704 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3705 modifiers |= MOUSE_SHIFT;
3706 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3707 modifiers |= MOUSE_CTRL;
3708 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3709 modifiers |= MOUSE_ALT;
3710
3711 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3712
3713 s_need_activate = TRUE;
3714 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715}
3716
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003717 static int
3718_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003719 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720 HWND hwndCtl,
3721 UINT code,
3722 int pos)
3723{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003724 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003725 scrollbar_T *sb, *sb_info;
3726 long val;
3727 int dragging = FALSE;
3728 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729 SCROLLINFO si;
3730
3731 si.cbSize = sizeof(si);
3732 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733
3734 sb = gui_mswin_find_scrollbar(hwndCtl);
3735 if (sb == NULL)
3736 return 0;
3737
Bram Moolenaar734a8672019-12-02 22:49:38 +01003738 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003739 {
3740 /*
3741 * Careful: need to get scrollbar info out of first (left) scrollbar
3742 * for window, but keep real scrollbar too because we must pass it to
3743 * gui_drag_scrollbar().
3744 */
3745 sb_info = &sb->wp->w_scrollbars[0];
3746 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003747 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003748 sb_info = sb;
3749 val = sb_info->value;
3750
3751 switch (code)
3752 {
3753 case SB_THUMBTRACK:
3754 val = pos;
3755 dragging = TRUE;
3756 if (sb->scroll_shift > 0)
3757 val <<= sb->scroll_shift;
3758 break;
3759 case SB_LINEDOWN:
3760 val++;
3761 break;
3762 case SB_LINEUP:
3763 val--;
3764 break;
3765 case SB_PAGEDOWN:
3766 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3767 break;
3768 case SB_PAGEUP:
3769 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3770 break;
3771 case SB_TOP:
3772 val = 0;
3773 break;
3774 case SB_BOTTOM:
3775 val = sb_info->max;
3776 break;
3777 case SB_ENDSCROLL:
3778 if (prev_code == SB_THUMBTRACK)
3779 {
3780 /*
3781 * "pos" only gives us 16-bit data. In case of large file,
3782 * use GetScrollPos() which returns 32-bit. Unfortunately it
3783 * is not valid while the scrollbar is being dragged.
3784 */
3785 val = GetScrollPos(hwndCtl, SB_CTL);
3786 if (sb->scroll_shift > 0)
3787 val <<= sb->scroll_shift;
3788 }
3789 break;
3790
3791 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003792 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793 return 0;
3794 }
3795 prev_code = code;
3796
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3798 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799
3800 /*
3801 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3802 */
3803 if (sb->wp != NULL)
3804 {
3805 scrollbar_T *sba = sb->wp->w_scrollbars;
3806 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3807
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 }
3810
Bram Moolenaar734a8672019-12-02 22:49:38 +01003811 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 s_busy_processing = TRUE;
3813
Bram Moolenaar734a8672019-12-02 22:49:38 +01003814 // When "allow_scrollbar" is FALSE still need to remember the new
3815 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003816 dont_scroll = !allow_scrollbar;
3817
Bram Moolenaara338adc2018-01-31 20:51:47 +01003818 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003820 mch_enable_flush();
3821 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003822
3823 s_busy_processing = FALSE;
3824 dont_scroll = dont_scroll_save;
3825
3826 return 0;
3827}
3828
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#ifdef FEAT_XPM_W32
3831# include "xpm_w32.h"
3832#endif
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834#ifdef __MINGW32__
3835/*
3836 * Add a lot of missing defines.
3837 * They are not always missing, we need the #ifndef's.
3838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839# ifndef IsMinimized
3840# define IsMinimized(hwnd) IsIconic(hwnd)
3841# endif
3842# ifndef IsMaximized
3843# define IsMaximized(hwnd) IsZoomed(hwnd)
3844# endif
3845# ifndef SelectFont
3846# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3847# endif
3848# ifndef GetStockBrush
3849# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3850# endif
3851# ifndef DeleteBrush
3852# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3853# endif
3854
3855# ifndef HANDLE_WM_RBUTTONDBLCLK
3856# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3857 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3858# endif
3859# ifndef HANDLE_WM_MBUTTONUP
3860# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3861 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3862# endif
3863# ifndef HANDLE_WM_MBUTTONDBLCLK
3864# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3865 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3866# endif
3867# ifndef HANDLE_WM_LBUTTONDBLCLK
3868# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3869 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3870# endif
3871# ifndef HANDLE_WM_RBUTTONDOWN
3872# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3873 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3874# endif
3875# ifndef HANDLE_WM_MOUSEMOVE
3876# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3877 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3878# endif
3879# ifndef HANDLE_WM_RBUTTONUP
3880# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3881 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3882# endif
3883# ifndef HANDLE_WM_MBUTTONDOWN
3884# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3885 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3886# endif
3887# ifndef HANDLE_WM_LBUTTONUP
3888# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3889 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3890# endif
3891# ifndef HANDLE_WM_LBUTTONDOWN
3892# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3893 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3894# endif
3895# ifndef HANDLE_WM_SYSCHAR
3896# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3897 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3898# endif
3899# ifndef HANDLE_WM_ACTIVATEAPP
3900# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3901 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3902# endif
3903# ifndef HANDLE_WM_WINDOWPOSCHANGING
3904# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3905 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3906# endif
3907# ifndef HANDLE_WM_VSCROLL
3908# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3909 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3910# endif
3911# ifndef HANDLE_WM_SETFOCUS
3912# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3913 ((fn)((hwnd), (HWND)(wParam)), 0L)
3914# endif
3915# ifndef HANDLE_WM_KILLFOCUS
3916# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3917 ((fn)((hwnd), (HWND)(wParam)), 0L)
3918# endif
3919# ifndef HANDLE_WM_HSCROLL
3920# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3921 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3922# endif
3923# ifndef HANDLE_WM_DROPFILES
3924# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3925 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3926# endif
3927# ifndef HANDLE_WM_CHAR
3928# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3929 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3930# endif
3931# ifndef HANDLE_WM_SYSDEADCHAR
3932# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3933 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3934# endif
3935# ifndef HANDLE_WM_DEADCHAR
3936# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3937 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3938# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003939#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941
Bram Moolenaar734a8672019-12-02 22:49:38 +01003942// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943#define TEAROFF_PADDING_X 2
3944#define TEAROFF_BUTTON_PAD_X 8
3945#define TEAROFF_MIN_WIDTH 200
3946#define TEAROFF_SUBMENU_LABEL ">>"
3947#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3948
3949
Bram Moolenaar734a8672019-12-02 22:49:38 +01003950// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003952# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953#endif
3954
3955
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003956#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957# define ID_BEVAL_TOOLTIP 200
3958# define BEVAL_TEXT_LEN MAXPATHL
3959
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003960# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003961// Work around old versions of basetsd.h which wrongly declares
3962// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003963# undef UINT_PTR
3964# define UINT_PTR UINT
3965# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003966
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003968static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003970
Bram Moolenaar82881492012-11-20 16:53:39 +01003971
Bram Moolenaar734a8672019-12-02 22:49:38 +01003972// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003973# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01003974
Bram Moolenaar45360022005-07-21 21:08:21 +00003975/*
3976 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003977 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003978 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003979# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01003980
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003981# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003982
3983typedef struct _DllVersionInfo
3984{
3985 DWORD cbSize;
3986 DWORD dwMajorVersion;
3987 DWORD dwMinorVersion;
3988 DWORD dwBuildNumber;
3989 DWORD dwPlatformID;
3990} DLLVERSIONINFO;
3991
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003992# ifndef PROTO
3993# include <poppack.h>
3994# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003995
Bram Moolenaar45360022005-07-21 21:08:21 +00003996typedef struct tagTOOLINFOA_NEW
3997{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003998 UINT cbSize;
3999 UINT uFlags;
4000 HWND hwnd;
4001 UINT_PTR uId;
4002 RECT rect;
4003 HINSTANCE hinst;
4004 LPSTR lpszText;
4005 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004006} TOOLINFO_NEW;
4007
4008typedef struct tagNMTTDISPINFO_NEW
4009{
4010 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004011 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004012 char szText[80];
4013 HINSTANCE hinst;
4014 UINT uFlags;
4015 LPARAM lParam;
4016} NMTTDISPINFO_NEW;
4017
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004018typedef struct tagTOOLINFOW_NEW
4019{
4020 UINT cbSize;
4021 UINT uFlags;
4022 HWND hwnd;
4023 UINT_PTR uId;
4024 RECT rect;
4025 HINSTANCE hinst;
4026 LPWSTR lpszText;
4027 LPARAM lParam;
4028 void *lpReserved;
4029} TOOLINFOW_NEW;
4030
4031typedef struct tagNMTTDISPINFOW_NEW
4032{
4033 NMHDR hdr;
4034 LPWSTR lpszText;
4035 WCHAR szText[80];
4036 HINSTANCE hinst;
4037 UINT uFlags;
4038 LPARAM lParam;
4039} NMTTDISPINFOW_NEW;
4040
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004041
Bram Moolenaar45360022005-07-21 21:08:21 +00004042typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004043# ifndef TTM_SETMAXTIPWIDTH
4044# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004047# ifndef TTF_DI_SETITEM
4048# define TTF_DI_SETITEM 0x8000
4049# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004050
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004051# ifndef TTN_GETDISPINFO
4052# define TTN_GETDISPINFO (TTN_FIRST - 0)
4053# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004054
Bram Moolenaar734a8672019-12-02 22:49:38 +01004055#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004056
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004057#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004058// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4059// it here if LPNMTTDISPINFO isn't defined.
4060// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4061// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004062# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4063typedef struct tagNMTTDISPINFOA {
4064 NMHDR hdr;
4065 LPSTR lpszText;
4066 char szText[80];
4067 HINSTANCE hinst;
4068 UINT uFlags;
4069 LPARAM lParam;
4070} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4071# define LPNMTTDISPINFO LPNMTTDISPINFOA
4072
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004073typedef struct tagNMTTDISPINFOW {
4074 NMHDR hdr;
4075 LPWSTR lpszText;
4076 WCHAR szText[80];
4077 HINSTANCE hinst;
4078 UINT uFlags;
4079 LPARAM lParam;
4080} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004081# endif
4082#endif
4083
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004084#ifndef TTN_GETDISPINFOW
4085# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4086#endif
4087
Bram Moolenaar734a8672019-12-02 22:49:38 +01004088// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090#ifdef FEAT_MENU
4091static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094/*
4095 * Use the system font for dialogs and tear-off menus. Remove this line to
4096 * use DLG_FONT_NAME.
4097 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004098#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101#define VIM_CLASSW L"Vim"
4102
Bram Moolenaar734a8672019-12-02 22:49:38 +01004103// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4104// thus there should be room for every dialog. For tearoffs it's made bigger
4105// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#define DLG_ALLOC_SIZE 16 * 1024
4107
4108/*
4109 * stuff for dialogs, menus, tearoffs etc.
4110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111static PWORD
4112add_dialog_element(
4113 PWORD p,
4114 DWORD lStyle,
4115 WORD x,
4116 WORD y,
4117 WORD w,
4118 WORD h,
4119 WORD Id,
4120 WORD clss,
4121 const char *caption);
4122static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004123static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004124#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127static void get_dialog_font_metrics(void);
4128
4129static int dialog_default_button = -1;
4130
Bram Moolenaar734a8672019-12-02 22:49:38 +01004131// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar734a8672019-12-02 22:49:38 +01004134static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135#ifdef FEAT_TOOLBAR
4136static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004137static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138static int get_toolbar_bitmap(vimmenu_T *menu);
4139#endif
4140
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004141#ifdef FEAT_GUI_TABLINE
4142static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004143static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004144#endif
4145
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146#ifdef FEAT_MBYTE_IME
4147static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4148static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4149#endif
4150#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4151# ifdef NOIME
4152typedef struct tagCOMPOSITIONFORM {
4153 DWORD dwStyle;
4154 POINT ptCurrentPos;
4155 RECT rcArea;
4156} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4157typedef HANDLE HIMC;
4158# endif
4159
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004160static HINSTANCE hLibImm = NULL;
4161static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4162static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4163static HIMC (WINAPI *pImmGetContext)(HWND);
4164static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4165static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4166static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4167static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004168static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4169static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004170static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4171static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004172static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173static void dyn_imm_load(void);
4174#else
4175# define pImmGetCompositionStringA ImmGetCompositionStringA
4176# define pImmGetCompositionStringW ImmGetCompositionStringW
4177# define pImmGetContext ImmGetContext
4178# define pImmAssociateContext ImmAssociateContext
4179# define pImmReleaseContext ImmReleaseContext
4180# define pImmGetOpenStatus ImmGetOpenStatus
4181# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004182# define pImmGetCompositionFontW ImmGetCompositionFontW
4183# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184# define pImmSetCompositionWindow ImmSetCompositionWindow
4185# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004186# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#endif
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#ifdef FEAT_MENU
4190/*
4191 * Figure out how high the menu bar is at the moment.
4192 */
4193 static int
4194gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004195 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 static int old_menu_height = -1;
4198
4199 RECT rc1, rc2;
4200 int num;
4201 int menu_height;
4202
4203 if (gui.menu_is_active)
4204 num = GetMenuItemCount(s_menuBar);
4205 else
4206 num = 0;
4207
4208 if (num == 0)
4209 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004210 else if (IsMinimized(s_hwnd))
4211 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004212 // The height of the menu cannot be determined while the window is
4213 // minimized. Take the previous height if the menu is changed in that
4214 // state, to avoid that Vim's vertical window size accidentally
4215 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004216 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 else
4219 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004220 /*
4221 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4222 * seem to have been set yet, so menu wraps in default window
4223 * width which is very narrow. Instead just return height of a
4224 * single menu item. Will still be wrong when the menu really
4225 * should wrap over more than one line.
4226 */
4227 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4228 if (gui.starting)
4229 menu_height = rc1.bottom - rc1.top + 1;
4230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004232 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4233 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 }
4236
4237 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004238 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004239 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 return menu_height;
4242}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004243#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245
4246/*
4247 * Setup for the Intellimouse
4248 */
4249 static void
4250init_mouse_wheel(void)
4251{
4252
4253#ifndef SPI_GETWHEELSCROLLLINES
4254# define SPI_GETWHEELSCROLLLINES 104
4255#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004256#ifndef SPI_SETWHEELSCROLLLINES
4257# define SPI_SETWHEELSCROLLLINES 105
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaar734a8672019-12-02 22:49:38 +01004260#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4261#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4263#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4264
Bram Moolenaar734a8672019-12-02 22:49:38 +01004265 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaar734a8672019-12-02 22:49:38 +01004267 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004268 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4269 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270}
4271
4272
Bram Moolenaarae20f342019-11-05 21:09:23 +01004273/*
4274 * Intellimouse wheel handler.
4275 * Treat a mouse wheel event as if it were a scroll request.
4276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 static void
4278_OnMouseWheel(
4279 HWND hwnd,
4280 short zDelta)
4281{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 int i;
4283 int size;
4284 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004285 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (mouse_scroll_lines == 0)
4288 init_mouse_wheel();
4289
Bram Moolenaarae20f342019-11-05 21:09:23 +01004290 wp = gui_mouse_window(FIND_POPUP);
4291
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004292#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004293 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004294 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004295 cmdarg_T cap;
4296 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004297
Bram Moolenaarae20f342019-11-05 21:09:23 +01004298 // Mouse hovers over popup window, scroll it if possible.
4299 mouse_row = wp->w_winrow;
4300 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004301 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004302 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4303 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4304 clear_oparg(&oa);
4305 cap.oap = &oa;
4306 nv_mousescroll(&cap);
4307 update_screen(0);
4308 setcursor();
4309 out_flush();
4310 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004311 }
4312#endif
4313
Bram Moolenaarae20f342019-11-05 21:09:23 +01004314 if (wp == NULL || !p_scf)
4315 wp = curwin;
4316
4317 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4318 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4319 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4320 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4321 else
4322 return;
4323 size = wp->w_height;
4324
Bram Moolenaara338adc2018-01-31 20:51:47 +01004325 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (mouse_scroll_lines > 0
4327 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4328 {
4329 for (i = mouse_scroll_lines; i > 0; --i)
4330 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4331 }
4332 else
4333 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004334 mch_enable_flush();
4335 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336}
4337
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004338#ifdef USE_SYSMENU_FONT
4339/*
4340 * Get Menu Font.
4341 * Return OK or FAIL.
4342 */
4343 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004344gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004345{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004346 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004347
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004348 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4349 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004350 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004351 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004352 &nm,
4353 0))
4354 return FAIL;
4355 *lf = nm.lfMenuFont;
4356 return OK;
4357}
4358#endif
4359
4360
4361#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4362/*
4363 * Set the GUI tabline font to the system menu font
4364 */
4365 static void
4366set_tabline_font(void)
4367{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004368 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004369 HFONT font;
4370 HWND hwnd;
4371 HDC hdc;
4372 HFONT hfntOld;
4373 TEXTMETRIC tm;
4374
4375 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4376 return;
4377
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004378 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004379
4380 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4381
4382 /*
4383 * Compute the height of the font used for the tab text
4384 */
4385 hwnd = GetDesktopWindow();
4386 hdc = GetWindowDC(hwnd);
4387 hfntOld = SelectFont(hdc, font);
4388
4389 GetTextMetrics(hdc, &tm);
4390
4391 SelectFont(hdc, hfntOld);
4392 ReleaseDC(hwnd, hdc);
4393
4394 /*
4395 * The space used by the tab border and the space between the tab label
4396 * and the tab border is included as 7.
4397 */
4398 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4399}
4400#endif
4401
Bram Moolenaar520470a2005-06-16 21:59:56 +00004402/*
4403 * Invoked when a setting was changed.
4404 */
4405 static LRESULT CALLBACK
4406_OnSettingChange(UINT n)
4407{
4408 if (n == SPI_SETWHEELSCROLLLINES)
4409 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4410 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004411#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4412 if (n == SPI_SETNONCLIENTMETRICS)
4413 set_tabline_font();
4414#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004415 return 0;
4416}
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418#ifdef FEAT_NETBEANS_INTG
4419 static void
4420_OnWindowPosChanged(
4421 HWND hwnd,
4422 const LPWINDOWPOS lpwpos)
4423{
4424 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004425 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4428 || lpwpos->cx != cx || lpwpos->cy != cy))
4429 {
4430 x = lpwpos->x;
4431 y = lpwpos->y;
4432 cx = lpwpos->cx;
4433 cy = lpwpos->cy;
4434 netbeans_frame_moved(x, y);
4435 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004436 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4438}
4439#endif
4440
4441 static int
4442_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 UINT fwSide,
4444 LPRECT lprc)
4445{
4446 int w, h;
4447 int valid_w, valid_h;
4448 int w_offset, h_offset;
4449
4450 w = lprc->right - lprc->left;
4451 h = lprc->bottom - lprc->top;
4452 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4453 w_offset = w - valid_w;
4454 h_offset = h - valid_h;
4455
4456 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4457 || fwSide == WMSZ_BOTTOMLEFT)
4458 lprc->left += w_offset;
4459 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4460 || fwSide == WMSZ_BOTTOMRIGHT)
4461 lprc->right -= w_offset;
4462
4463 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4464 || fwSide == WMSZ_TOPRIGHT)
4465 lprc->top += h_offset;
4466 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4467 || fwSide == WMSZ_BOTTOMRIGHT)
4468 lprc->bottom -= h_offset;
4469 return TRUE;
4470}
4471
4472
4473
4474 static LRESULT CALLBACK
4475_WndProc(
4476 HWND hwnd,
4477 UINT uMsg,
4478 WPARAM wParam,
4479 LPARAM lParam)
4480{
4481 /*
4482 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4483 hwnd, uMsg, wParam, lParam);
4484 */
4485
4486 HandleMouseHide(uMsg, lParam);
4487
4488 s_uMsg = uMsg;
4489 s_wParam = wParam;
4490 s_lParam = lParam;
4491
4492 switch (uMsg)
4493 {
4494 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4495 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004496 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004498 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4500 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4501 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4502 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4503#ifdef FEAT_MENU
4504 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4505#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004506 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4507 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4509 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004510 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4511 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4513 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4514 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4515#ifdef FEAT_NETBEANS_INTG
4516 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4517#endif
4518
Bram Moolenaarafa24992006-03-27 20:58:26 +00004519#ifdef FEAT_GUI_TABLINE
4520 case WM_RBUTTONUP:
4521 {
4522 if (gui_mch_showing_tabline())
4523 {
4524 POINT pt;
4525 RECT rect;
4526
4527 /*
4528 * If the cursor is on the tabline, display the tab menu
4529 */
4530 GetCursorPos((LPPOINT)&pt);
4531 GetWindowRect(s_textArea, &rect);
4532 if (pt.y < rect.top)
4533 {
4534 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004535 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004536 }
4537 }
4538 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4539 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004540 case WM_LBUTTONDBLCLK:
4541 {
4542 /*
4543 * If the user double clicked the tabline, create a new tab
4544 */
4545 if (gui_mch_showing_tabline())
4546 {
4547 POINT pt;
4548 RECT rect;
4549
4550 GetCursorPos((LPPOINT)&pt);
4551 GetWindowRect(s_textArea, &rect);
4552 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004553 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004554 }
4555 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4556 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004557#endif
4558
Bram Moolenaar734a8672019-12-02 22:49:38 +01004559 case WM_QUERYENDSESSION: // System wants to go down.
4560 gui_shell_closed(); // Will exit when no changed buffers.
4561 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004564 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004567 return 0L;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 break;
4570
4571 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004572 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4573 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004574 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 return 0L;
4576
4577 case WM_SYSCHAR:
4578 /*
4579 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4580 * shortcut key, handle like a typed ALT key, otherwise call Windows
4581 * ALT key handling.
4582 */
4583#ifdef FEAT_MENU
4584 if ( !gui.menu_is_active
4585 || p_wak[0] == 'n'
4586 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4587 )
4588#endif
4589 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004590 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 return 0L;
4592 }
4593#ifdef FEAT_MENU
4594 else
4595 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4596#endif
4597
4598 case WM_SYSKEYUP:
4599#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004600 // This used to be done only when menu is active: ALT key is used for
4601 // that. But that caused problems when menu is disabled and using
4602 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4603 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4605#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004606 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#endif
4608
Bram Moolenaar734a8672019-12-02 22:49:38 +01004609 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004610 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 case WM_MOUSEWHEEL:
4613 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004614 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
Bram Moolenaar734a8672019-12-02 22:49:38 +01004616 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004617 case WM_SETTINGCHANGE:
4618 return _OnSettingChange((UINT)wParam);
4619
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004620#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 case WM_NOTIFY:
4622 switch (((LPNMHDR) lParam)->code)
4623 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004624 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004625 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004627 LPNMHDR hdr = (LPNMHDR)lParam;
4628 char_u *str = NULL;
4629 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004630
Bram Moolenaard23a8232018-02-10 18:45:26 +01004631 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004632
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004633# ifdef FEAT_GUI_TABLINE
4634 if (gui_mch_showing_tabline()
4635 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004636 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004637 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004638 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004639 * Mouse is over the GUI tabline. Display the
4640 * tooltip for the tab under the cursor
4641 *
4642 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004643 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004644 GetCursorPos(&pt);
4645 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004646 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004647 TCHITTESTINFO htinfo;
4648 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004649
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004650 /*
4651 * Get the tab under the cursor
4652 */
4653 htinfo.pt.x = pt.x;
4654 htinfo.pt.y = pt.y;
4655 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4656 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004657 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004658 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004659
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004660 tp = find_tabpage(idx + 1);
4661 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004662 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004663 get_tabline_label(tp, TRUE);
4664 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004665 }
4666 }
4667 }
4668 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004669# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004670# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004671# ifdef FEAT_GUI_TABLINE
4672 else
4673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004675 UINT idButton;
4676 vimmenu_T *pMenu;
4677
4678 idButton = (UINT) hdr->idFrom;
4679 pMenu = gui_mswin_find_menu(root_menu, idButton);
4680 if (pMenu)
4681 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004683# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004684 if (str != NULL)
4685 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004686 if (hdr->code == TTN_GETDISPINFOW)
4687 {
4688 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4689
Bram Moolenaar734a8672019-12-02 22:49:38 +01004690 // Set the maximum width, this also enables using
4691 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004692 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4693 0, 500);
4694
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004695 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004696 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004697 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004698 }
4699 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004700 {
4701 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4702
Bram Moolenaar734a8672019-12-02 22:49:38 +01004703 // Set the maximum width, this also enables using
4704 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004705 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4706 0, 500);
4707
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004708 if (STRLEN(str) < sizeof(lpdi->szText)
4709 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004710 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004711 sizeof(lpdi->szText) - 1);
4712 else
4713 lpdi->lpszText = tt_text;
4714 }
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004718# ifdef FEAT_GUI_TABLINE
4719 case TCN_SELCHANGE:
4720 if (gui_mch_showing_tabline()
4721 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004722 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004723 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004724 return 0L;
4725 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004726 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004727
4728 case NM_RCLICK:
4729 if (gui_mch_showing_tabline()
4730 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004731 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004732 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004733 return 0L;
4734 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004735 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004736# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004738# ifdef FEAT_GUI_TABLINE
4739 if (gui_mch_showing_tabline()
4740 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4741 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 break;
4744 }
4745 break;
4746#endif
4747#if defined(MENUHINTS) && defined(FEAT_MENU)
4748 case WM_MENUSELECT:
4749 if (((UINT) HIWORD(wParam)
4750 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4751 == MF_HILITE
4752 && (State & CMDLINE) == 0)
4753 {
4754 UINT idButton;
4755 vimmenu_T *pMenu;
4756 static int did_menu_tip = FALSE;
4757
4758 if (did_menu_tip)
4759 {
4760 msg_clr_cmdline();
4761 setcursor();
4762 out_flush();
4763 did_menu_tip = FALSE;
4764 }
4765
4766 idButton = (UINT)LOWORD(wParam);
4767 pMenu = gui_mswin_find_menu(root_menu, idButton);
4768 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4769 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4770 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004771 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004772 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004773 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 setcursor();
4775 out_flush();
4776 did_menu_tip = TRUE;
4777 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004778 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 break;
4781#endif
4782 case WM_NCHITTEST:
4783 {
4784 LRESULT result;
4785 int x, y;
4786 int xPos = GET_X_LPARAM(lParam);
4787
4788 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4789 if (result == HTCLIENT)
4790 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004791#ifdef FEAT_GUI_TABLINE
4792 if (gui_mch_showing_tabline())
4793 {
4794 int yPos = GET_Y_LPARAM(lParam);
4795 RECT rct;
4796
Bram Moolenaar734a8672019-12-02 22:49:38 +01004797 // If the cursor is on the GUI tabline, don't process this
4798 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004799 GetWindowRect(s_textArea, &rct);
4800 if (yPos < rct.top)
4801 return result;
4802 }
4803#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004804 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 xPos -= x;
4806
Bram Moolenaar734a8672019-12-02 22:49:38 +01004807 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 return HTBOTTOMLEFT;
4809 else
4810 return HTBOTTOMRIGHT;
4811 }
4812 else
4813 return result;
4814 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004815 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
4817#ifdef FEAT_MBYTE_IME
4818 case WM_IME_NOTIFY:
4819 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4820 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004821 return 1L;
4822
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 case WM_IME_COMPOSITION:
4824 if (!_OnImeComposition(hwnd, wParam, lParam))
4825 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004826 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#endif
4828
4829 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004831 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833#endif
4834 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4835 }
4836
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004837 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838}
4839
4840/*
4841 * End of call-back routines
4842 */
4843
Bram Moolenaar734a8672019-12-02 22:49:38 +01004844// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845HWND vim_parent_hwnd = NULL;
4846
4847 static BOOL CALLBACK
4848FindWindowTitle(HWND hwnd, LPARAM lParam)
4849{
4850 char buf[2048];
4851 char *title = (char *)lParam;
4852
4853 if (GetWindowText(hwnd, buf, sizeof(buf)))
4854 {
4855 if (strstr(buf, title) != NULL)
4856 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004857 // Found it. Store the window ref. and quit searching if MDI
4858 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004860 if (vim_parent_hwnd != NULL)
4861 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004864 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865}
4866
4867/*
4868 * Invoked for '-P "title"' argument: search for parent application to open
4869 * our window in.
4870 */
4871 void
4872gui_mch_set_parent(char *title)
4873{
4874 EnumWindows(FindWindowTitle, (LPARAM)title);
4875 if (vim_parent_hwnd == NULL)
4876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004877 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 mch_exit(2);
4879 }
4880}
4881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 static void
4884ole_error(char *arg)
4885{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004886 char buf[IOSIZE];
4887
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004888# ifdef VIMDLL
4889 gui.in_use = mch_is_gui_executable();
4890# endif
4891
Bram Moolenaar734a8672019-12-02 22:49:38 +01004892 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004893 vim_snprintf(buf, IOSIZE,
4894 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4895 arg);
4896 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004900#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4901 static char *
4902gvim_error(void)
4903{
4904 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4905
4906 if (starting)
4907 {
4908 mch_errmsg(msg);
4909 mch_errmsg("\n");
4910 mch_exit(2);
4911 }
4912 return msg;
4913}
4914
4915 char *
4916gui_mch_do_spawn(char_u *arg)
4917{
4918 int len;
4919# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4920 char_u *session = NULL;
4921 LPWSTR tofree1 = NULL;
4922# endif
4923 WCHAR name[MAX_PATH];
4924 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4925 STARTUPINFOW si = {sizeof(si)};
4926 PROCESS_INFORMATION pi;
4927
4928 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4929 goto error;
4930 p = wcsrchr(name, L'\\');
4931 if (p == NULL)
4932 goto error;
4933 // Replace the executable name from vim(d).exe to gvim(d).exe.
4934# ifdef DEBUG
4935 wcscpy(p + 1, L"gvimd.exe");
4936# else
4937 wcscpy(p + 1, L"gvim.exe");
4938# endif
4939
4940# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4941 if (starting)
4942# endif
4943 {
4944 // Pass the command line to the new process.
4945 p = GetCommandLineW();
4946 // Skip 1st argument.
4947 while (*p && *p != L' ' && *p != L'\t')
4948 {
4949 if (*p == L'"')
4950 {
4951 while (*p && *p != L'"')
4952 ++p;
4953 if (*p)
4954 ++p;
4955 }
4956 else
4957 ++p;
4958 }
4959 cmd = p;
4960 }
4961# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4962 else
4963 {
4964 // Create a session file and pass it to the new process.
4965 LPWSTR wsession;
4966 char_u *savebg;
4967 int ret;
4968
4969 session = vim_tempname('s', FALSE);
4970 if (session == NULL)
4971 goto error;
4972 savebg = p_bg;
4973 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4974 ret = write_session_file(session);
4975 vim_free(p_bg);
4976 p_bg = savebg;
4977 if (!ret)
4978 goto error;
4979 wsession = enc_to_utf16(session, NULL);
4980 if (wsession == NULL)
4981 goto error;
4982 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004983 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004984 if (cmd == NULL)
4985 {
4986 vim_free(wsession);
4987 goto error;
4988 }
4989 tofree1 = cmd;
4990 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4991 wsession, wsession);
4992 vim_free(wsession);
4993 }
4994# endif
4995
4996 // Check additional arguments to the `:gui` command.
4997 if (arg != NULL)
4998 {
4999 warg = enc_to_utf16(arg, NULL);
5000 if (warg == NULL)
5001 goto error;
5002 tofree2 = warg;
5003 }
5004 else
5005 warg = L"";
5006
5007 // Set up the new command line.
5008 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005009 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005010 if (newcmd == NULL)
5011 goto error;
5012 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5013
5014 // Spawn a new GUI process.
5015 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5016 NULL, NULL, &si, &pi))
5017 goto error;
5018 CloseHandle(pi.hProcess);
5019 CloseHandle(pi.hThread);
5020 mch_exit(0);
5021
5022error:
5023# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5024 if (session)
5025 mch_remove(session);
5026 vim_free(session);
5027 vim_free(tofree1);
5028# endif
5029 vim_free(newcmd);
5030 vim_free(tofree2);
5031 return gvim_error();
5032}
5033#endif
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035/*
5036 * Parse the GUI related command-line arguments. Any arguments used are
5037 * deleted from argv, and *argc is decremented accordingly. This is called
5038 * when vim is started, whether or not the GUI has been started.
5039 */
5040 void
5041gui_mch_prepare(int *argc, char **argv)
5042{
5043 int silent = FALSE;
5044 int idx;
5045
Bram Moolenaar734a8672019-12-02 22:49:38 +01005046 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5048 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005049 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5051 && (argv[2][0] == '-' || argv[2][0] == '/'))
5052 {
5053 silent = TRUE;
5054 idx = 2;
5055 }
5056 else
5057 idx = 1;
5058
Bram Moolenaar734a8672019-12-02 22:49:38 +01005059 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (STRICMP(argv[idx] + 1, "register") == 0)
5061 {
5062#ifdef FEAT_OLE
5063 RegisterMe(silent);
5064 mch_exit(0);
5065#else
5066 if (!silent)
5067 ole_error("register");
5068 mch_exit(2);
5069#endif
5070 }
5071
Bram Moolenaar734a8672019-12-02 22:49:38 +01005072 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5074 {
5075#ifdef FEAT_OLE
5076 UnregisterMe(!silent);
5077 mch_exit(0);
5078#else
5079 if (!silent)
5080 ole_error("unregister");
5081 mch_exit(2);
5082#endif
5083 }
5084
Bram Moolenaar734a8672019-12-02 22:49:38 +01005085 // Ignore an -embedding argument. It is only relevant if the
5086 // application wants to treat the case when it is started manually
5087 // differently from the case where it is started via automation (and
5088 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5090 {
5091#ifdef FEAT_OLE
5092 *argc = 1;
5093#else
5094 ole_error("embedding");
5095 mch_exit(2);
5096#endif
5097 }
5098 }
5099
5100#ifdef FEAT_OLE
5101 {
5102 int bDoRestart = FALSE;
5103
5104 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005105 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (bDoRestart)
5107 mch_exit(0);
5108 }
5109#endif
5110
5111#ifdef FEAT_NETBEANS_INTG
5112 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005113 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 int arg;
5115
5116 for (arg = 1; arg < *argc; arg++)
5117 if (strncmp("-nb", argv[arg], 3) == 0)
5118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 netbeansArg = argv[arg];
5120 mch_memmove(&argv[arg], &argv[arg + 1],
5121 (--*argc - arg) * sizeof(char *));
5122 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005123 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127}
5128
5129/*
5130 * Initialise the GUI. Create all the windows, set up all the call-backs
5131 * etc.
5132 */
5133 int
5134gui_mch_init(void)
5135{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005137 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#ifdef GLOBAL_IME
5140 ATOM atom;
5141#endif
5142
Bram Moolenaar734a8672019-12-02 22:49:38 +01005143 // Return here if the window was already opened (happens when
5144 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005146 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * Load the tearoff bitmap
5150 */
5151#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005152 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153#endif
5154
5155 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5156 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5157#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005158 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159#endif
5160 gui.border_width = 0;
5161
5162 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5163
Bram Moolenaar734a8672019-12-02 22:49:38 +01005164 // First try using the wide version, so that we can use any title.
5165 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005166 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005168 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 wndclassw.lpfnWndProc = _WndProc;
5170 wndclassw.cbClsExtra = 0;
5171 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005172 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5174 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5175 wndclassw.hbrBackground = s_brush;
5176 wndclassw.lpszMenuName = NULL;
5177 wndclassw.lpszClassName = szVimWndClassW;
5178
5179 if ((
5180#ifdef GLOBAL_IME
5181 atom =
5182#endif
5183 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (vim_parent_hwnd != NULL)
5188 {
5189#ifdef HAVE_TRY_EXCEPT
5190 __try
5191 {
5192#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005193 // Open inside the specified parent window.
5194 // TODO: last argument should point to a CLIENTCREATESTRUCT
5195 // structure.
5196 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005198 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005199 WS_OVERLAPPEDWINDOW | WS_CHILD
5200 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5202 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005203 100, // Any value will do
5204 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005206 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#ifdef HAVE_TRY_EXCEPT
5208 }
5209 __except(EXCEPTION_EXECUTE_HANDLER)
5210 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005211 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213#endif
5214 if (s_hwnd == NULL)
5215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005216 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 mch_exit(2);
5218 }
5219 }
5220 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005221 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005222 // If the provided windowid is not valid reset it to zero, so that it
5223 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005224 if (IsWindow((HWND)win_socket_id) <= 0)
5225 win_socket_id = 0;
5226
Bram Moolenaar734a8672019-12-02 22:49:38 +01005227 // Create a window. If win_socket_id is not zero without border and
5228 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005229 s_hwnd = CreateWindowW(
5230 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005231 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5232 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005233 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5234 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 100, // Any value will do
5236 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005237 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005238 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005239 if (s_hwnd != NULL && win_socket_id != 0)
5240 {
5241 SetParent(s_hwnd, (HWND)win_socket_id);
5242 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5243 }
5244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 if (s_hwnd == NULL)
5247 return FAIL;
5248
5249#ifdef GLOBAL_IME
5250 global_ime_init(atom, s_hwnd);
5251#endif
5252#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5253 dyn_imm_load();
5254#endif
5255
Bram Moolenaar734a8672019-12-02 22:49:38 +01005256 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005257 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005258 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005259 wndclassw.style = CS_OWNDC;
5260 wndclassw.lpfnWndProc = _TextAreaWndProc;
5261 wndclassw.cbClsExtra = 0;
5262 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005263 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005264 wndclassw.hIcon = NULL;
5265 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5266 wndclassw.hbrBackground = NULL;
5267 wndclassw.lpszMenuName = NULL;
5268 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005269
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005270 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 return FAIL;
5272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005274 s_textArea = CreateWindowExW(
5275 0,
5276 szTextAreaClassW, L"Vim text area",
5277 WS_CHILD | WS_VISIBLE, 0, 0,
5278 100, // Any value will do for now
5279 100, // Any value will do for now
5280 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005281 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (s_textArea == NULL)
5284 return FAIL;
5285
Bram Moolenaar20321902016-02-17 12:30:17 +01005286#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005287 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005288 {
5289 HANDLE hIcon = NULL;
5290
5291 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005292 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005293 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005294#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#ifdef FEAT_MENU
5297 s_menuBar = CreateMenu();
5298#endif
5299 s_hdc = GetDC(s_textArea);
5300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
Bram Moolenaar734a8672019-12-02 22:49:38 +01005303 // Do we need to bother with this?
5304 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaar734a8672019-12-02 22:49:38 +01005306 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 gui_mch_def_colors();
5308
Bram Moolenaar734a8672019-12-02 22:49:38 +01005309 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5310 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 set_normal_colors();
5312
5313 /*
5314 * Check that none of the colors are the same as the background color.
5315 * Then store the current values as the defaults.
5316 */
5317 gui_check_colors();
5318 gui.def_norm_pixel = gui.norm_pixel;
5319 gui.def_back_pixel = gui.back_pixel;
5320
Bram Moolenaar734a8672019-12-02 22:49:38 +01005321 // Get the colors for the highlight groups (gui_check_colors() might have
5322 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 highlight_gui_started();
5324
5325 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005326 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005328 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 /*
5331 * Set up for Intellimouse processing
5332 */
5333 init_mouse_wheel();
5334
5335 /*
5336 * compute a couple of metrics used for the dialogs
5337 */
5338 get_dialog_font_metrics();
5339#ifdef FEAT_TOOLBAR
5340 /*
5341 * Create the toolbar
5342 */
5343 initialise_toolbar();
5344#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005345#ifdef FEAT_GUI_TABLINE
5346 /*
5347 * Create the tabline
5348 */
5349 initialise_tabline();
5350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#ifdef MSWIN_FIND_REPLACE
5352 /*
5353 * Initialise the dialog box stuff
5354 */
5355 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5356
Bram Moolenaar734a8672019-12-02 22:49:38 +01005357 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005359 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005361 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5363 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5364 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005367#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005368# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005369// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005370# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005371# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005372# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005373# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005374 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005375 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005376#endif
5377
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005378#ifdef FEAT_RENDER_OPTIONS
5379 if (p_rop)
5380 (void)gui_mch_set_rendering_options(p_rop);
5381#endif
5382
Bram Moolenaar748bf032005-02-02 23:04:36 +00005383theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005384 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005385 display_errors();
5386
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 return OK;
5388}
5389
5390/*
5391 * Get the size of the screen, taking position on multiple monitors into
5392 * account (if supported).
5393 */
5394 static void
5395get_work_area(RECT *spi_rect)
5396{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005397 HMONITOR mon;
5398 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005401 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005402 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005404 moninfo.cbSize = sizeof(MONITORINFO);
5405 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005407 *spi_rect = moninfo.rcWork;
5408 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005411 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5413}
5414
5415/*
5416 * Set the size of the window to the given width and height in pixels.
5417 */
5418 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005419gui_mch_set_shellsize(
5420 int width,
5421 int height,
5422 int min_width UNUSED,
5423 int min_height UNUSED,
5424 int base_width UNUSED,
5425 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005426 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
5428 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005429 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaar734a8672019-12-02 22:49:38 +01005432 // Try to keep window completely on screen.
5433 // Get position of the screen work area. This is the part that is not
5434 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 get_work_area(&workarea_rect);
5436
Bram Moolenaar734a8672019-12-02 22:49:38 +01005437 // Resizing a maximized window looks very strange, unzoom it first.
5438 // But don't do it when still starting up, it may have been requested in
5439 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005440 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005442
5443 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar734a8672019-12-02 22:49:38 +01005445 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005446 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005447 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005448 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005449 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 + GetSystemMetrics(SM_CYCAPTION)
5451#ifdef FEAT_MENU
5452 + gui_mswin_get_menu_height(FALSE)
5453#endif
5454 ;
5455
Bram Moolenaar734a8672019-12-02 22:49:38 +01005456 // The following should take care of keeping Vim on the same monitor, no
5457 // matter if the secondary monitor is left or right of the primary
5458 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005459 window_rect.right = window_rect.left + win_width;
5460 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005461
Bram Moolenaar734a8672019-12-02 22:49:38 +01005462 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005463 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5464 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005466 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5467 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005469 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5470 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005472 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5473 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005475 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5476 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478 SetActiveWindow(s_hwnd);
5479 SetFocus(s_hwnd);
5480
5481#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005482 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 gui_mswin_get_menu_height(!gui.starting);
5484#endif
5485}
5486
5487
5488 void
5489gui_mch_set_scrollbar_thumb(
5490 scrollbar_T *sb,
5491 long val,
5492 long size,
5493 long max)
5494{
5495 SCROLLINFO info;
5496
5497 sb->scroll_shift = 0;
5498 while (max > 32767)
5499 {
5500 max = (max + 1) >> 1;
5501 val >>= 1;
5502 size >>= 1;
5503 ++sb->scroll_shift;
5504 }
5505
5506 if (sb->scroll_shift > 0)
5507 ++size;
5508
5509 info.cbSize = sizeof(info);
5510 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5511 info.nPos = val;
5512 info.nMin = 0;
5513 info.nMax = max;
5514 info.nPage = size;
5515 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5516}
5517
5518
5519/*
5520 * Set the current text font.
5521 */
5522 void
5523gui_mch_set_font(GuiFont font)
5524{
5525 gui.currFont = font;
5526}
5527
5528
5529/*
5530 * Set the current text foreground color.
5531 */
5532 void
5533gui_mch_set_fg_color(guicolor_T color)
5534{
5535 gui.currFgColor = color;
5536}
5537
5538/*
5539 * Set the current text background color.
5540 */
5541 void
5542gui_mch_set_bg_color(guicolor_T color)
5543{
5544 gui.currBgColor = color;
5545}
5546
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005547/*
5548 * Set the current text special color.
5549 */
5550 void
5551gui_mch_set_sp_color(guicolor_T color)
5552{
5553 gui.currSpColor = color;
5554}
5555
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005556#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557/*
5558 * Multi-byte handling, originally by Sung-Hoon Baek.
5559 * First static functions (no prototypes generated).
5560 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005561# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005562# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005563# endif
5564# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 * handle WM_IME_NOTIFY message
5568 */
5569 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005570_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 LRESULT lResult = 0;
5573 HIMC hImc;
5574
5575 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5576 return lResult;
5577 switch (dwCommand)
5578 {
5579 case IMN_SETOPENSTATUS:
5580 if (pImmGetOpenStatus(hImc))
5581 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005582 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 im_set_position(gui.row, gui.col);
5584
Bram Moolenaar734a8672019-12-02 22:49:38 +01005585 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 State &= ~LANGMAP;
5587 if (State & INSERT)
5588 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005589# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005590 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5592 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005593 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int old_row = gui.row;
5595 int old_col = gui.col;
5596
5597 // This must be called here before
5598 // status_redraw_curbuf(), otherwise the mode
5599 // message may appear in the wrong position.
5600 showmode();
5601 status_redraw_curbuf();
5602 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005603 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 gui.row = old_row;
5605 gui.col = old_col;
5606 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609 }
5610 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005611 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 lResult = 0;
5613 break;
5614 }
5615 pImmReleaseContext(hWnd, hImc);
5616 return lResult;
5617}
5618
5619 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005620_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 char_u *ret;
5623 int len;
5624
Bram Moolenaar734a8672019-12-02 22:49:38 +01005625 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 return 0;
5627
5628 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5629 if (ret != NULL)
5630 {
5631 add_to_input_buf_csi(ret, len);
5632 vim_free(ret);
5633 return 1;
5634 }
5635 return 0;
5636}
5637
5638/*
5639 * get the current composition string, in UCS-2; *lenp is the number of
5640 * *lenp is the number of Unicode characters.
5641 */
5642 static short_u *
5643GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5644{
5645 LONG ret;
5646 LPWSTR wbuf = NULL;
5647 char_u *buf;
5648
5649 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005650 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
Bram Moolenaar734a8672019-12-02 22:49:38 +01005652 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5654 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005655 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 if (ret > 0)
5658 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005659 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005660 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 if (wbuf != NULL)
5662 {
5663 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5664 *lenp = ret / sizeof(WCHAR);
5665 }
5666 return (short_u *)wbuf;
5667 }
5668
Bram Moolenaar734a8672019-12-02 22:49:38 +01005669 // ret < 0; we got an error, so try the ANSI version. This'll work
5670 // on 9x/ME, but only if the codepage happens to be set to whatever
5671 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5673 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005674 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
5676 buf = alloc(ret);
5677 if (buf == NULL)
5678 return NULL;
5679 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5680
Bram Moolenaar734a8672019-12-02 22:49:38 +01005681 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005682 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 vim_free(buf);
5684
5685 return (short_u *)wbuf;
5686}
5687
5688/*
5689 * void GetResultStr()
5690 *
5691 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5692 * get complete composition string
5693 */
5694 static char_u *
5695GetResultStr(HWND hwnd, int GCS, int *lenp)
5696{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005697 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 short_u *buf = NULL;
5699 char_u *convbuf = NULL;
5700
5701 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5702 return NULL;
5703
Bram Moolenaar734a8672019-12-02 22:49:38 +01005704 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5706 if (buf == NULL)
5707 return NULL;
5708
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005709 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 pImmReleaseContext(hwnd, hIMC);
5711 vim_free(buf);
5712 return convbuf;
5713}
5714#endif
5715
Bram Moolenaar734a8672019-12-02 22:49:38 +01005716// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005717#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719/*
5720 * set font to IM.
5721 */
5722 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005723im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 HIMC hImc;
5726
5727 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5728 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005729 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 pImmReleaseContext(s_hwnd, hImc);
5731 }
5732}
5733
5734/*
5735 * Notify cursor position to IM.
5736 */
5737 void
5738im_set_position(int row, int col)
5739{
5740 HIMC hImc;
5741
5742 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5743 {
5744 COMPOSITIONFORM cfs;
5745
5746 cfs.dwStyle = CFS_POINT;
5747 cfs.ptCurrentPos.x = FILL_X(col);
5748 cfs.ptCurrentPos.y = FILL_Y(row);
5749 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5750 pImmSetCompositionWindow(hImc, &cfs);
5751
5752 pImmReleaseContext(s_hwnd, hImc);
5753 }
5754}
5755
5756/*
5757 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5758 */
5759 void
5760im_set_active(int active)
5761{
5762 HIMC hImc;
5763 static HIMC hImcOld = (HIMC)0;
5764
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005765# ifdef VIMDLL
5766 if (!gui.in_use && !gui.starting)
5767 {
5768 mbyte_im_set_active(active);
5769 return;
5770 }
5771# endif
5772
Bram Moolenaar734a8672019-12-02 22:49:38 +01005773 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (p_imdisable)
5776 {
5777 if (hImcOld == (HIMC)0)
5778 {
5779 hImcOld = pImmGetContext(s_hwnd);
5780 if (hImcOld)
5781 pImmAssociateContext(s_hwnd, (HIMC)0);
5782 }
5783 active = FALSE;
5784 }
5785 else if (hImcOld != (HIMC)0)
5786 {
5787 pImmAssociateContext(s_hwnd, hImcOld);
5788 hImcOld = (HIMC)0;
5789 }
5790
5791 hImc = pImmGetContext(s_hwnd);
5792 if (hImc)
5793 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005794 /*
5795 * for Korean ime
5796 */
5797 HKL hKL = GetKeyboardLayout(0);
5798
5799 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5800 {
5801 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5802 static BOOL bSaved = FALSE;
5803
5804 if (active)
5805 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005806 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005807 if (bSaved)
5808 pImmSetConversionStatus(hImc, dwConversionSaved,
5809 dwSentenceSaved);
5810 bSaved = FALSE;
5811 }
5812 else
5813 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005814 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005815 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5816 &dwSentenceSaved))
5817 {
5818 bSaved = TRUE;
5819 pImmSetConversionStatus(hImc,
5820 dwConversionSaved & ~(IME_CMODE_NATIVE
5821 | IME_CMODE_FULLSHAPE),
5822 dwSentenceSaved);
5823 }
5824 }
5825 }
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 pImmSetOpenStatus(hImc, active);
5828 pImmReleaseContext(s_hwnd, hImc);
5829 }
5830 }
5831}
5832
5833/*
5834 * Get IM status. When IM is on, return not 0. Else return 0.
5835 */
5836 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005837im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 int status = 0;
5840 HIMC hImc;
5841
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005842# ifdef VIMDLL
5843 if (!gui.in_use && !gui.starting)
5844 return mbyte_im_get_status();
5845# endif
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5848 {
5849 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5850 pImmReleaseContext(s_hwnd, hImc);
5851 }
5852 return status;
5853}
5854
Bram Moolenaar734a8672019-12-02 22:49:38 +01005855#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005857#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005858// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
5860/*
5861 * Notify cursor position to IM.
5862 */
5863 void
5864im_set_position(int row, int col)
5865{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005866 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 POINT p;
5868
5869 p.x = FILL_X(col);
5870 p.y = FILL_Y(row);
5871 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5872 global_ime_set_position(&p);
5873}
5874
5875/*
5876 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5877 */
5878 void
5879im_set_active(int active)
5880{
5881 global_ime_set_status(active);
5882}
5883
5884/*
5885 * Get IM status. When IM is on, return not 0. Else return 0.
5886 */
5887 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005888im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 return global_ime_get_status();
5891}
5892#endif
5893
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005894/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005895 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005896 */
5897 static void
5898latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5899{
5900 int c;
5901
Bram Moolenaarca003e12006-03-17 23:19:38 +00005902 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005903 {
5904 c = *text++;
5905 switch (c)
5906 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005907 case 0xa4: c = 0x20ac; break; // euro
5908 case 0xa6: c = 0x0160; break; // S hat
5909 case 0xa8: c = 0x0161; break; // S -hat
5910 case 0xb4: c = 0x017d; break; // Z hat
5911 case 0xb8: c = 0x017e; break; // Z -hat
5912 case 0xbc: c = 0x0152; break; // OE
5913 case 0xbd: c = 0x0153; break; // oe
5914 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005915 }
5916 *unicodebuf++ = c;
5917 }
5918}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920#ifdef FEAT_RIGHTLEFT
5921/*
5922 * What is this for? In the case where you are using Win98 or Win2K or later,
5923 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5924 * reverses the string sent to the TextOut... family. This sucks, because we
5925 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5926 * way to tell Windblows not to do this!
5927 *
5928 * The short of it is that this 'RevOut' only gets called if you are running
5929 * one of the new, "improved" MS OSes, and only if you are running in
5930 * 'rightleft' mode. It makes display take *slightly* longer, but not
5931 * noticeably so.
5932 */
5933 static void
5934RevOut( HDC s_hdc,
5935 int col,
5936 int row,
5937 UINT foptions,
5938 CONST RECT *pcliprect,
5939 LPCTSTR text,
5940 UINT len,
5941 CONST INT *padding)
5942{
5943 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005945 for (ix = 0; ix < (int)len; ++ix)
5946 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5947 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948}
5949#endif
5950
Bram Moolenaar92467d32017-12-05 13:22:16 +01005951 static void
5952draw_line(
5953 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005954 int y1,
5955 int x2,
5956 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005957 COLORREF color)
5958{
5959#if defined(FEAT_DIRECTX)
5960 if (IS_ENABLE_DIRECTX())
5961 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5962 else
5963#endif
5964 {
5965 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5966 HPEN old_pen = SelectObject(s_hdc, hpen);
5967 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005968 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005969 LineTo(s_hdc, x2, y2);
5970 DeleteObject(SelectObject(s_hdc, old_pen));
5971 }
5972}
5973
5974 static void
5975set_pixel(
5976 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005977 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005978 COLORREF color)
5979{
5980#if defined(FEAT_DIRECTX)
5981 if (IS_ENABLE_DIRECTX())
5982 DWriteContext_SetPixel(s_dwc, x, y, color);
5983 else
5984#endif
5985 SetPixel(s_hdc, x, y, color);
5986}
5987
5988 static void
5989fill_rect(
5990 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005991 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005992 COLORREF color)
5993{
5994#if defined(FEAT_DIRECTX)
5995 if (IS_ENABLE_DIRECTX())
5996 DWriteContext_FillRect(s_dwc, rcp, color);
5997 else
5998#endif
5999 {
6000 HBRUSH hbr2;
6001
6002 if (hbr == NULL)
6003 hbr2 = CreateSolidBrush(color);
6004 else
6005 hbr2 = hbr;
6006 FillRect(s_hdc, rcp, hbr2);
6007 if (hbr == NULL)
6008 DeleteBrush(hbr2);
6009 }
6010}
6011
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 void
6013gui_mch_draw_string(
6014 int row,
6015 int col,
6016 char_u *text,
6017 int len,
6018 int flags)
6019{
6020 static int *padding = NULL;
6021 static int pad_size = 0;
6022 int i;
6023 const RECT *pcliprect = NULL;
6024 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 static WCHAR *unicodebuf = NULL;
6026 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006027 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 int y;
6030
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 /*
6032 * Italic and bold text seems to have an extra row of pixels at the bottom
6033 * (below where the bottom of the character should be). If we draw the
6034 * characters with a solid background, the top row of pixels in the
6035 * character below will be overwritten. We can fix this by filling in the
6036 * background ourselves, to the correct character proportions, and then
6037 * writing the character in transparent mode. Still have a problem when
6038 * the character is "_", which gets written on to the character below.
6039 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6040 * pixel in their slots, which fixes the problem with the bottom row of
6041 * pixels. We still need this code because otherwise the top row of pixels
6042 * becomes a problem. - webb.
6043 */
6044 static HBRUSH hbr_cache[2] = {NULL, NULL};
6045 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6046 static int brush_lru = 0;
6047 HBRUSH hbr;
6048 RECT rc;
6049
6050 if (!(flags & DRAW_TRANSP))
6051 {
6052 /*
6053 * Clear background first.
6054 * Note: FillRect() excludes right and bottom of rectangle.
6055 */
6056 rc.left = FILL_X(col);
6057 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (has_mbyte)
6059 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006060 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006061 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 rc.right = FILL_X(col + len);
6065 rc.bottom = FILL_Y(row + 1);
6066
Bram Moolenaar734a8672019-12-02 22:49:38 +01006067 // Cache the created brush, that saves a lot of time. We need two:
6068 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if (gui.currBgColor == brush_color[0])
6070 {
6071 hbr = hbr_cache[0];
6072 brush_lru = 1;
6073 }
6074 else if (gui.currBgColor == brush_color[1])
6075 {
6076 hbr = hbr_cache[1];
6077 brush_lru = 0;
6078 }
6079 else
6080 {
6081 if (hbr_cache[brush_lru] != NULL)
6082 DeleteBrush(hbr_cache[brush_lru]);
6083 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6084 brush_color[brush_lru] = gui.currBgColor;
6085 hbr = hbr_cache[brush_lru];
6086 brush_lru = !brush_lru;
6087 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006088
Bram Moolenaar92467d32017-12-05 13:22:16 +01006089 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
6091 SetBkMode(s_hdc, TRANSPARENT);
6092
6093 /*
6094 * When drawing block cursor, prevent inverted character spilling
6095 * over character cell (can happen with bold/italic)
6096 */
6097 if (flags & DRAW_CURSOR)
6098 {
6099 pcliprect = &rc;
6100 foptions = ETO_CLIPPED;
6101 }
6102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 SetTextColor(s_hdc, gui.currFgColor);
6104 SelectFont(s_hdc, gui.currFont);
6105
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006106#ifdef FEAT_DIRECTX
6107 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006108 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006109#endif
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6112 {
6113 vim_free(padding);
6114 pad_size = Columns;
6115
Bram Moolenaar734a8672019-12-02 22:49:38 +01006116 // Don't give an out-of-memory message here, it would call us
6117 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006118 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (padding != NULL)
6120 for (i = 0; i < pad_size; i++)
6121 padding[i] = gui.char_width;
6122 }
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 /*
6125 * We have to provide the padding argument because italic and bold versions
6126 * of fixed-width fonts are often one pixel or so wider than their normal
6127 * versions.
6128 * No check for DRAW_BOLD, Windows will have done it already.
6129 */
6130
Bram Moolenaar734a8672019-12-02 22:49:38 +01006131 // Check if there are any UTF-8 characters. If not, use normal text
6132 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (enc_utf8)
6134 for (n = 0; n < len; ++n)
6135 if (text[n] >= 0x80)
6136 break;
6137
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006138#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006139 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6140 // required that unicode drawing routine, currently. So this forces it
6141 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006142 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006143 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006144#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006145
Bram Moolenaar734a8672019-12-02 22:49:38 +01006146 // Check if the Unicode buffer exists and is big enough. Create it
6147 // with the same length as the multi-byte string, the number of wide
6148 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006149 if ((enc_utf8
6150 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6151 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 && (unicodebuf == NULL || len > unibuflen))
6153 {
6154 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006155 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006158 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 unibuflen = len;
6161 }
6162
6163 if (enc_utf8 && n < len && unicodebuf != NULL)
6164 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006165 // Output UTF-8 characters. Composing characters should be
6166 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006167 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006168 int wlen; // string length in words
6169 int clen; // string length in characters
6170 int cells; // cell width of string up to composing char
6171 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006172 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006174 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006175 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006177 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006179 c = utf_ptr2char(text + i);
6180 if (c >= 0x10000)
6181 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006182 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006183 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6184 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006185 }
6186 else
6187 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006188 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006189 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006190
6191 if (utf_iscomposing(c))
6192 cw = 0;
6193 else
6194 {
6195 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006196 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006197 cw = 1;
6198 }
6199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 if (unicodepdy != NULL)
6201 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006202 // Use unicodepdy to make characters fit as we expect, even
6203 // when the font uses different widths (e.g., bold character
6204 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006205 if (c >= 0x10000)
6206 {
6207 unicodepdy[wlen - 2] = cw * gui.char_width;
6208 unicodepdy[wlen - 1] = 0;
6209 }
6210 else
6211 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
6213 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006214 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006215 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006217#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006218 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006219 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006220 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006221 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006222 TEXT_X(col), TEXT_Y(row),
6223 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006224 gui.char_width, gui.currFgColor,
6225 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006226 }
6227 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006228#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006229 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6230 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006231 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006233 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006235 // If we want to display codepage data, and the current CP is not the
6236 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (unicodebuf != NULL)
6238 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006239 if (enc_latin9)
6240 latin9_to_ucs(text, len, unicodebuf);
6241 else
6242 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 MB_PRECOMPOSED,
6244 (char *)text, len,
6245 (LPWSTR)unicodebuf, unibuflen);
6246 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006247 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006248 // Use unicodepdy to make characters fit as we expect, even
6249 // when the font uses different widths (e.g., bold character
6250 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006251 if (unicodepdy != NULL)
6252 {
6253 int i;
6254 int cw;
6255
6256 for (i = 0; i < len; ++i)
6257 {
6258 cw = utf_char2cells(unicodebuf[i]);
6259 if (cw > 2)
6260 cw = 1;
6261 unicodepdy[i] = cw * gui.char_width;
6262 }
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006265 foptions, pcliprect, unicodebuf, len, unicodepdy);
6266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
6268 }
6269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006272 // Windows will mess up RL text, so we have to draw it character by
6273 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006274 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6276 foptions, pcliprect, (char *)text, len, padding);
6277 else
6278#endif
6279 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6280 foptions, pcliprect, (char *)text, len, padding);
6281 }
6282
Bram Moolenaar734a8672019-12-02 22:49:38 +01006283 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (flags & DRAW_UNDERL)
6285 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006286 // When p_linespace is 0, overwrite the bottom row of pixels.
6287 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (p_linespace > 1)
6290 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006291 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006293
Bram Moolenaar734a8672019-12-02 22:49:38 +01006294 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006295 if (flags & DRAW_STRIKE)
6296 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006297 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006298 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006299 }
6300
Bram Moolenaar734a8672019-12-02 22:49:38 +01006301 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006302 if (flags & DRAW_UNDERC)
6303 {
6304 int x;
6305 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006306 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006307
6308 y = FILL_Y(row + 1) - 1;
6309 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6310 {
6311 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006312 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006313 }
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315}
6316
6317
6318/*
6319 * Output routines.
6320 */
6321
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322/*
6323 * Flush any output to the screen
6324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 void
6326gui_mch_flush(void)
6327{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006328#if defined(FEAT_DIRECTX)
6329 if (IS_ENABLE_DIRECTX())
6330 DWriteContext_Flush(s_dwc);
6331#endif
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 GdiFlush();
6334}
6335
6336 static void
6337clear_rect(RECT *rcp)
6338{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006339 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340}
6341
6342
Bram Moolenaarc716c302006-01-21 22:12:51 +00006343 void
6344gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6345{
6346 RECT workarea_rect;
6347
6348 get_work_area(&workarea_rect);
6349
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006350 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006351 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006352 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006353
Bram Moolenaar734a8672019-12-02 22:49:38 +01006354 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6355 // the menubar for MSwin, we subtract it from the screen height, so that
6356 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006357 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006358 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006359 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006360 - GetSystemMetrics(SM_CYCAPTION)
6361#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006362 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006363#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006364 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006365}
6366
6367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368#if defined(FEAT_MENU) || defined(PROTO)
6369/*
6370 * Add a sub menu to the menu bar.
6371 */
6372 void
6373gui_mch_add_menu(
6374 vimmenu_T *menu,
6375 int pos)
6376{
6377 vimmenu_T *parent = menu->parent;
6378
6379 menu->submenu_id = CreatePopupMenu();
6380 menu->id = s_menu_id++;
6381
6382 if (menu_is_menubar(menu->name))
6383 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006384 WCHAR *wn;
6385 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006387 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006388 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006389 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006391 infow.cbSize = sizeof(infow);
6392 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6393 | MIIM_SUBMENU;
6394 infow.dwItemData = (long_u)menu;
6395 infow.wID = menu->id;
6396 infow.fType = MFT_STRING;
6397 infow.dwTypeData = wn;
6398 infow.cch = (UINT)wcslen(wn);
6399 infow.hSubMenu = menu->submenu_id;
6400 InsertMenuItemW((parent == NULL)
6401 ? s_menuBar : parent->submenu_id,
6402 (UINT)pos, TRUE, &infow);
6403 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405
Bram Moolenaar734a8672019-12-02 22:49:38 +01006406 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (parent == NULL)
6408 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006409# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 else if (IsWindow(parent->tearoff_handle))
6411 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413}
6414
6415 void
6416gui_mch_show_popupmenu(vimmenu_T *menu)
6417{
6418 POINT mp;
6419
6420 (void)GetCursorPos((LPPOINT)&mp);
6421 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6422}
6423
6424 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006425gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 vimmenu_T *menu = gui_find_menu(path_name);
6428
6429 if (menu != NULL)
6430 {
6431 POINT p;
6432
Bram Moolenaar734a8672019-12-02 22:49:38 +01006433 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006435 if (mouse_pos)
6436 {
6437 int mx, my;
6438
6439 gui_mch_getmouse(&mx, &my);
6440 p.x += mx;
6441 p.y += my;
6442 }
6443 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006445 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6447 }
6448 msg_scroll = FALSE;
6449 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6450 }
6451}
6452
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006453# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454/*
6455 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6456 * create it as a pseudo-"tearoff menu".
6457 */
6458 void
6459gui_make_tearoff(char_u *path_name)
6460{
6461 vimmenu_T *menu = gui_find_menu(path_name);
6462
Bram Moolenaar734a8672019-12-02 22:49:38 +01006463 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 if (menu != NULL)
6465 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6466}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
6469/*
6470 * Add a menu item to a menu
6471 */
6472 void
6473gui_mch_add_menu_item(
6474 vimmenu_T *menu,
6475 int idx)
6476{
6477 vimmenu_T *parent = menu->parent;
6478
6479 menu->id = s_menu_id++;
6480 menu->submenu_id = NULL;
6481
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006482# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6484 {
6485 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6486 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6487 }
6488 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006489# endif
6490# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (menu_is_toolbar(parent->name))
6492 {
6493 TBBUTTON newtb;
6494
Bram Moolenaara80faa82020-04-12 19:37:17 +02006495 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (menu_is_separator(menu->name))
6497 {
6498 newtb.iBitmap = 0;
6499 newtb.fsStyle = TBSTYLE_SEP;
6500 }
6501 else
6502 {
6503 newtb.iBitmap = get_toolbar_bitmap(menu);
6504 newtb.fsStyle = TBSTYLE_BUTTON;
6505 }
6506 newtb.idCommand = menu->id;
6507 newtb.fsState = TBSTATE_ENABLED;
6508 newtb.iString = 0;
6509 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6510 (LPARAM)&newtb);
6511 menu->submenu_id = (HMENU)-1;
6512 }
6513 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006516 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006518 wn = enc_to_utf16(menu->name, NULL);
6519 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006521 InsertMenuW(parent->submenu_id, (UINT)idx,
6522 (menu_is_separator(menu->name)
6523 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6524 (UINT)menu->id, wn);
6525 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006527# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (IsWindow(parent->tearoff_handle))
6529 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532}
6533
6534/*
6535 * Destroy the machine specific menu widget.
6536 */
6537 void
6538gui_mch_destroy_menu(vimmenu_T *menu)
6539{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006540# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /*
6542 * is this a toolbar button?
6543 */
6544 if (menu->submenu_id == (HMENU)-1)
6545 {
6546 int iButton;
6547
6548 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6549 (WPARAM)menu->id, 0);
6550 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6551 }
6552 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
6555 if (menu->parent != NULL
6556 && menu_is_popup(menu->parent->dname)
6557 && menu->parent->submenu_id != NULL)
6558 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6559 else
6560 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6561 if (menu->submenu_id != NULL)
6562 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006563# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 if (IsWindow(menu->tearoff_handle))
6565 DestroyWindow(menu->tearoff_handle);
6566 if (menu->parent != NULL
6567 && menu->parent->children != NULL
6568 && IsWindow(menu->parent->tearoff_handle))
6569 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006570 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 menu->modes = 0;
6572 rebuild_tearoff(menu->parent);
6573 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576}
6577
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006578# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 static void
6580rebuild_tearoff(vimmenu_T *menu)
6581{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006582 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 char_u tbuf[128];
6584 RECT trect;
6585 RECT rct;
6586 RECT roct;
6587 int x, y;
6588
6589 HWND thwnd = menu->tearoff_handle;
6590
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006591 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (GetWindowRect(thwnd, &trect)
6593 && GetWindowRect(s_hwnd, &rct)
6594 && GetClientRect(s_hwnd, &roct))
6595 {
6596 x = trect.left - rct.left;
6597 y = (trect.top - rct.bottom + roct.bottom);
6598 }
6599 else
6600 {
6601 x = y = 0xffffL;
6602 }
6603 DestroyWindow(thwnd);
6604 if (menu->children != NULL)
6605 {
6606 gui_mch_tearoff(tbuf, menu, x, y);
6607 if (IsWindow(menu->tearoff_handle))
6608 (void) SetWindowPos(menu->tearoff_handle,
6609 NULL,
6610 (int)trect.left,
6611 (int)trect.top,
6612 0, 0,
6613 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6614 }
6615}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006616# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618/*
6619 * Make a menu either grey or not grey.
6620 */
6621 void
6622gui_mch_menu_grey(
6623 vimmenu_T *menu,
6624 int grey)
6625{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006626# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 /*
6628 * is this a toolbar button?
6629 */
6630 if (menu->submenu_id == (HMENU)-1)
6631 {
6632 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6633 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6634 }
6635 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006636# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006637 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6638 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006640# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6642 {
6643 WORD menuID;
6644 HWND menuHandle;
6645
6646 /*
6647 * A tearoff button has changed state.
6648 */
6649 if (menu->children == NULL)
6650 menuID = (WORD)(menu->id);
6651 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006652 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6654 if (menuHandle)
6655 EnableWindow(menuHandle, !grey);
6656
6657 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659}
6660
Bram Moolenaar734a8672019-12-02 22:49:38 +01006661#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663
Bram Moolenaar734a8672019-12-02 22:49:38 +01006664// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6667#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006668#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669
6670#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6671/*
6672 * stuff for dialogs
6673 */
6674
6675/*
6676 * The callback routine used by all the dialogs. Very simple. First,
6677 * acknowledges the INITDIALOG message so that Windows knows to do standard
6678 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6679 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6680 * number.
6681 */
6682 static LRESULT CALLBACK
6683dialog_callback(
6684 HWND hwnd,
6685 UINT message,
6686 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006687 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 if (message == WM_INITDIALOG)
6690 {
6691 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006692 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 (void)SetFocus(hwnd);
6694 if (dialog_default_button > IDCANCEL)
6695 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006696 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006697 // We don't have a default, set focus on another element of the
6698 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006699 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 return FALSE;
6701 }
6702
6703 if (message == WM_COMMAND)
6704 {
6705 int button = LOWORD(wParam);
6706
Bram Moolenaar734a8672019-12-02 22:49:38 +01006707 // Don't end the dialog if something was selected that was
6708 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (button >= DLG_NONBUTTON_CONTROL)
6710 return TRUE;
6711
Bram Moolenaar734a8672019-12-02 22:49:38 +01006712 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006714 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006715 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006716 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006717
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006718 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6719 p = utf16_to_enc(wp, NULL);
6720 vim_strncpy(s_textfield, p, IOSIZE);
6721 vim_free(p);
6722 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 /*
6726 * Need to check for IDOK because if the user just hits Return to
6727 * accept the default value, some reason this is what we get.
6728 */
6729 if (button == IDOK)
6730 {
6731 if (dialog_default_button > IDCANCEL)
6732 EndDialog(hwnd, dialog_default_button);
6733 }
6734 else
6735 EndDialog(hwnd, button - IDCANCEL);
6736 return TRUE;
6737 }
6738
6739 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6740 {
6741 EndDialog(hwnd, 0);
6742 return TRUE;
6743 }
6744 return FALSE;
6745}
6746
6747/*
6748 * Create a dialog dynamically from the parameter strings.
6749 * type = type of dialog (question, alert, etc.)
6750 * title = dialog title. may be NULL for default title.
6751 * message = text to display. Dialog sizes to accommodate it.
6752 * buttons = '\n' separated list of button captions, default first.
6753 * dfltbutton = number of default button.
6754 *
6755 * This routine returns 1 if the first button is pressed,
6756 * 2 for the second, etc.
6757 *
6758 * 0 indicates Esc was pressed.
6759 * -1 for unexpected error
6760 *
6761 * If stubbing out this fn, return 1.
6762 */
6763
Bram Moolenaar734a8672019-12-02 22:49:38 +01006764static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 "IDR_VIM",
6767 "IDR_VIM_ERROR",
6768 "IDR_VIM_ALERT",
6769 "IDR_VIM_INFO",
6770 "IDR_VIM_QUESTION"
6771};
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 int
6774gui_mch_dialog(
6775 int type,
6776 char_u *title,
6777 char_u *message,
6778 char_u *buttons,
6779 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006780 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006781 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006784 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 int numButtons;
6786 int *buttonWidths, *buttonPositions;
6787 int buttonYpos;
6788 int nchar, i;
6789 DWORD lStyle;
6790 int dlgwidth = 0;
6791 int dlgheight;
6792 int editboxheight;
6793 int horizWidth = 0;
6794 int msgheight;
6795 char_u *pstart;
6796 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006797 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 char_u *tbuffer;
6799 RECT rect;
6800 HWND hwnd;
6801 HDC hdc;
6802 HFONT font, oldFont;
6803 TEXTMETRIC fontInfo;
6804 int fontHeight;
6805 int textWidth, minButtonWidth, messageWidth;
6806 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006807 int maxDialogHeight;
6808 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 int vertical;
6810 int dlgPaddingX;
6811 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006812# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006813 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006815# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006816 garray_T ga;
6817 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006819# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006820 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006821# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006822 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006823# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006824 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006825 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
Bram Moolenaar748bf032005-02-02 23:04:36 +00006828 if (s_hwnd == NULL)
6829 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
6831 if ((type < 0) || (type > VIM_LAST_TYPE))
6832 type = 0;
6833
Bram Moolenaar734a8672019-12-02 22:49:38 +01006834 // allocate some memory for dialog template
6835 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006836 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006837 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 if (p == NULL)
6840 return -1;
6841
6842 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006843 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6845 * const.
6846 */
6847 tbuffer = vim_strsave(buttons);
6848 if (tbuffer == NULL)
6849 return -1;
6850
Bram Moolenaar734a8672019-12-02 22:49:38 +01006851 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
Bram Moolenaar734a8672019-12-02 22:49:38 +01006853 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 numButtons = 1;
6855 for (i = 0; tbuffer[i] != '\0'; i++)
6856 {
6857 if (tbuffer[i] == DLG_BUTTON_SEP)
6858 numButtons++;
6859 }
6860 if (dfltbutton >= numButtons)
6861 dfltbutton = -1;
6862
Bram Moolenaar734a8672019-12-02 22:49:38 +01006863 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006864 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 if (buttonWidths == NULL)
6866 return -1;
6867
Bram Moolenaar734a8672019-12-02 22:49:38 +01006868 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006869 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (buttonPositions == NULL)
6871 return -1;
6872
6873 /*
6874 * Calculate how big the dialog must be.
6875 */
6876 hwnd = GetDesktopWindow();
6877 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006878# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6880 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006881 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 use_lfSysmenu = TRUE;
6883 }
6884 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6887 VARIABLE_PITCH , DLG_FONT_NAME);
6888 if (s_usenewlook)
6889 {
6890 oldFont = SelectFont(hdc, font);
6891 dlgPaddingX = DLG_PADDING_X;
6892 dlgPaddingY = DLG_PADDING_Y;
6893 }
6894 else
6895 {
6896 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6897 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6898 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6899 }
6900 GetTextMetrics(hdc, &fontInfo);
6901 fontHeight = fontInfo.tmHeight;
6902
Bram Moolenaar734a8672019-12-02 22:49:38 +01006903 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006904 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar734a8672019-12-02 22:49:38 +01006906 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006907 if (s_hwnd == NULL)
6908 {
6909 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
Bram Moolenaar734a8672019-12-02 22:49:38 +01006911 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006912 get_work_area(&workarea_rect);
6913 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6914 if (maxDialogWidth > 600)
6915 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006916 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006917 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006918 }
6919 else
6920 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006921 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006922 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006923 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006924 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006925 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006926 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6927 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006928
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006930 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006931 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006932 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006933 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6934 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6935 }
6936
Bram Moolenaar734a8672019-12-02 22:49:38 +01006937 // Set dlgwidth to width of message.
6938 // Copy the message into "ga", changing NL to CR-NL and inserting line
6939 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 pstart = message;
6941 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006942 msgheight = 0;
6943 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 do
6945 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006946 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006947
Bram Moolenaar734a8672019-12-02 22:49:38 +01006948 // Need to figure out where to break the string. The system does it
6949 // at a word boundary, which would mean we can't compute the number of
6950 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006951 textWidth = 0;
6952 last_white = NULL;
6953 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006955 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006956 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957 && textWidth > maxDialogWidth * 3 / 4)
6958 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006959 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006960 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006961 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006962 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006963 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006964 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006965 textWidth = 0;
6966
6967 if (last_white != NULL)
6968 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006969 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006970 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006971 pend = last_white + 1;
6972 last_white = NULL;
6973 }
6974 ga_append(&ga, '\r');
6975 ga_append(&ga, '\n');
6976 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006977 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006978
6979 while (--l >= 0)
6980 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006981 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006982 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006984
6985 ga_append(&ga, '\r');
6986 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 pstart = pend + 1;
6988 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006989
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006990 if (ga.ga_data != NULL)
6991 message = ga.ga_data;
6992
Bram Moolenaar734a8672019-12-02 22:49:38 +01006993 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006994
Bram Moolenaar734a8672019-12-02 22:49:38 +01006995 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02006996 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6997 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
6999 if (msgheight < DLG_ICON_HEIGHT)
7000 msgheight = DLG_ICON_HEIGHT;
7001
7002 /*
7003 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007004 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007006 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (!vertical)
7008 {
7009 // Place buttons horizontally if they fit.
7010 horizWidth = dlgPaddingX;
7011 pstart = tbuffer;
7012 i = 0;
7013 do
7014 {
7015 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7016 if (pend == NULL)
7017 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007018 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 if (textWidth < minButtonWidth)
7020 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007021 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 buttonWidths[i] = textWidth;
7023 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007024 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 pstart = pend + 1;
7026 } while (*pend != NUL);
7027
7028 if (horizWidth > maxDialogWidth)
7029 vertical = TRUE; // Too wide to fit on the screen.
7030 else if (horizWidth > dlgwidth)
7031 dlgwidth = horizWidth;
7032 }
7033
7034 if (vertical)
7035 {
7036 // Stack buttons vertically.
7037 pstart = tbuffer;
7038 do
7039 {
7040 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7041 if (pend == NULL)
7042 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007043 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007044 textWidth += dlgPaddingX; // Padding within button
7045 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (textWidth > dlgwidth)
7047 dlgwidth = textWidth;
7048 pstart = pend + 1;
7049 } while (*pend != NUL);
7050 }
7051
7052 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007053 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
Bram Moolenaar734a8672019-12-02 22:49:38 +01007055 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 if (s_usenewlook)
7057 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7058 else
7059 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7060
7061 add_long(lStyle);
7062 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007063 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 add_word(0); // NumberOfItems(will change later)
7065 add_word(10); // x
7066 add_word(10); // y
7067 add_word(PixelToDialogX(dlgwidth)); // cx
7068
7069 // Dialog height.
7070 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007071 dlgheight = msgheight + 2 * dlgPaddingY
7072 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 else
7074 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7075
7076 // Dialog needs to be taller if contains an edit box.
7077 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7078 if (textfield != NULL)
7079 dlgheight += editboxheight;
7080
Bram Moolenaar734a8672019-12-02 22:49:38 +01007081 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007082 if (dlgheight > maxDialogHeight)
7083 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007084 msgheight = msgheight - (dlgheight - maxDialogHeight);
7085 dlgheight = maxDialogHeight;
7086 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007087 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007088 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007089 }
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 add_word(PixelToDialogY(dlgheight));
7092
7093 add_word(0); // Menu
7094 add_word(0); // Class
7095
Bram Moolenaar734a8672019-12-02 22:49:38 +01007096 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007097 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7098 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 p += nchar;
7100
7101 if (s_usenewlook)
7102 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007103 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007104# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (use_lfSysmenu)
7106 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007107 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7109 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007110 wcscpy(p, lfSysmenu.lfFaceName);
7111 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 {
7116 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007117 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119 p += nchar;
7120 }
7121
7122 buttonYpos = msgheight + 2 * dlgPaddingY;
7123
7124 if (textfield != NULL)
7125 buttonYpos += editboxheight;
7126
7127 pstart = tbuffer;
7128 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007129 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 for (i = 0; i < numButtons; i++)
7131 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007132 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 for ( pend = pstart;
7134 *pend && (*pend != DLG_BUTTON_SEP);
7135 pend++)
7136 ;
7137
7138 if (*pend)
7139 *pend = '\0';
7140
7141 /*
7142 * old NOTE:
7143 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7144 * the focus to the first tab-able button and in so doing makes that
7145 * the default!! Grrr. Workaround: Make the default button the only
7146 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7147 * he/she can use arrow keys.
7148 *
7149 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007150 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 * dialog. Also needed for when the textfield is the default control.
7152 * It appears to work now (perhaps not on Win95?).
7153 */
7154 if (vertical)
7155 {
7156 p = add_dialog_element(p,
7157 (i == dfltbutton
7158 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7159 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007160 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 + 2 * fontHeight * i),
7162 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7163 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007164 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166 else
7167 {
7168 p = add_dialog_element(p,
7169 (i == dfltbutton
7170 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7171 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007172 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 PixelToDialogX(buttonWidths[i]),
7174 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007175 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179 *pnumitems += numButtons;
7180
Bram Moolenaar734a8672019-12-02 22:49:38 +01007181 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 p = add_dialog_element(p, SS_ICON,
7183 PixelToDialogX(dlgPaddingX),
7184 PixelToDialogY(dlgPaddingY),
7185 PixelToDialogX(DLG_ICON_WIDTH),
7186 PixelToDialogY(DLG_ICON_HEIGHT),
7187 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7188 dlg_icons[type]);
7189
Bram Moolenaar734a8672019-12-02 22:49:38 +01007190 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007191 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7192 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7193 PixelToDialogY(dlgPaddingY),
7194 (WORD)(PixelToDialogX(messageWidth) + 1),
7195 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007196 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (textfield != NULL)
7200 {
7201 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7202 PixelToDialogX(2 * dlgPaddingX),
7203 PixelToDialogY(2 * dlgPaddingY + msgheight),
7204 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7205 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007206 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 *pnumitems += 1;
7208 }
7209
7210 *pnumitems += 2;
7211
7212 SelectFont(hdc, oldFont);
7213 DeleteObject(font);
7214 ReleaseDC(hwnd, hdc);
7215
Bram Moolenaar734a8672019-12-02 22:49:38 +01007216 // Let the dialog_callback() function know which button to make default
7217 // If we have an edit box, make that the default. We also need to tell
7218 // dialog_callback() if this dialog contains an edit box or not. We do
7219 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (textfield != NULL)
7221 {
7222 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7223 s_textfield = textfield;
7224 }
7225 else
7226 {
7227 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7228 s_textfield = NULL;
7229 }
7230
Bram Moolenaar734a8672019-12-02 22:49:38 +01007231 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007233 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 (LPDLGTEMPLATE)pdlgtemplate,
7235 s_hwnd,
7236 (DLGPROC)dialog_callback);
7237
7238 LocalFree(LocalHandle(pdlgtemplate));
7239 vim_free(tbuffer);
7240 vim_free(buttonWidths);
7241 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007242 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaar734a8672019-12-02 22:49:38 +01007244 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 (void)SetFocus(s_hwnd);
7246
7247 return nchar;
7248}
7249
Bram Moolenaar734a8672019-12-02 22:49:38 +01007250#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252/*
7253 * Put a simple element (basic class) onto a dialog template in memory.
7254 * return a pointer to where the next item should be added.
7255 *
7256 * parameters:
7257 * lStyle = additional style flags
7258 * (be careful, NT3.51 & Win32s will ignore the new ones)
7259 * x,y = x & y positions IN DIALOG UNITS
7260 * w,h = width and height IN DIALOG UNITS
7261 * Id = ID used in messages
7262 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7263 * caption = usually text or resource name
7264 *
7265 * TODO: use the length information noted here to enable the dialog creation
7266 * routines to work out more exactly how much memory they need to alloc.
7267 */
7268 static PWORD
7269add_dialog_element(
7270 PWORD p,
7271 DWORD lStyle,
7272 WORD x,
7273 WORD y,
7274 WORD w,
7275 WORD h,
7276 WORD Id,
7277 WORD clss,
7278 const char *caption)
7279{
7280 int nchar;
7281
Bram Moolenaar734a8672019-12-02 22:49:38 +01007282 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7284 *p++ = LOWORD(lStyle);
7285 *p++ = HIWORD(lStyle);
7286 *p++ = 0; // LOWORD (lExtendedStyle)
7287 *p++ = 0; // HIWORD (lExtendedStyle)
7288 *p++ = x;
7289 *p++ = y;
7290 *p++ = w;
7291 *p++ = h;
7292 *p++ = Id; //9 or 10 words in all
7293
7294 *p++ = (WORD)0xffff;
7295 *p++ = clss; //2 more here
7296
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007297 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 p += nchar;
7299
7300 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7301
7302 return p; //total = 15+ (strlen(caption)) words
7303 // = 30 + 2(strlen(caption) bytes reqd
7304}
7305
7306
7307/*
7308 * Helper routine. Take an input pointer, return closest pointer that is
7309 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7310 */
7311 static LPWORD
7312lpwAlign(
7313 LPWORD lpIn)
7314{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007315 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007317 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 ul += 3;
7319 ul >>= 2;
7320 ul <<= 2;
7321 return (LPWORD)ul;
7322}
7323
7324/*
7325 * Helper routine. Takes second parameter as Ansi string, copies it to first
7326 * parameter as wide character (16-bits / char) string, and returns integer
7327 * number of wide characters (words) in string (including the trailing wide
7328 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007329 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7330 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 static int
7332nCopyAnsiToWideChar(
7333 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007334 LPSTR lpAnsiIn,
7335 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007338 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 int i;
7340 WCHAR *wn;
7341
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007342 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007344 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007345 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (wn != NULL)
7347 {
7348 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007349 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 vim_free(wn);
7351 }
7352 }
7353 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007354 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 nChar = MultiByteToWideChar(
7356 enc_codepage > 0 ? enc_codepage : CP_ACP,
7357 MB_PRECOMPOSED,
7358 lpAnsiIn, len,
7359 lpWCStr, len);
7360 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 return nChar;
7365}
7366
7367
7368#ifdef FEAT_TEAROFF
7369/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007370 * Lookup menu handle from "menu_id".
7371 */
7372 static HMENU
7373tearoff_lookup_menuhandle(
7374 vimmenu_T *menu,
7375 WORD menu_id)
7376{
7377 for ( ; menu != NULL; menu = menu->next)
7378 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007379 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007380 continue;
7381 if (menu_is_separator(menu->dname))
7382 continue;
7383 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7384 return menu->submenu_id;
7385 }
7386 return NULL;
7387}
7388
7389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 * The callback function for all the modeless dialogs that make up the
7391 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7392 * thinking its menus have been clicked), and go away when closed.
7393 */
7394 static LRESULT CALLBACK
7395tearoff_callback(
7396 HWND hwnd,
7397 UINT message,
7398 WPARAM wParam,
7399 LPARAM lParam)
7400{
7401 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007402 {
7403 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
Bram Moolenaar734a8672019-12-02 22:49:38 +01007407 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 HandleMouseHide(message, lParam);
7409
7410 if (message == WM_COMMAND)
7411 {
7412 if ((WORD)(LOWORD(wParam)) & 0x8000)
7413 {
7414 POINT mp;
7415 RECT rect;
7416
7417 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7418 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007419 vimmenu_T *menu;
7420
7421 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007423 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7425 (int)rect.right - 8,
7426 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007427 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 s_hwnd,
7429 NULL);
7430 /*
7431 * NOTE: The pop-up menu can eat the mouse up event.
7432 * We deal with this in normal.c.
7433 */
7434 }
7435 }
7436 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007437 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7439 /*
7440 * Give main window the focus back: this is so after
7441 * choosing a tearoff button you can start typing again
7442 * straight away.
7443 */
7444 (void)SetFocus(s_hwnd);
7445 return TRUE;
7446 }
7447 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7448 {
7449 DestroyWindow(hwnd);
7450 return TRUE;
7451 }
7452
Bram Moolenaar734a8672019-12-02 22:49:38 +01007453 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (message == WM_EXITSIZEMOVE)
7455 (void)SetActiveWindow(s_hwnd);
7456
7457 return FALSE;
7458}
7459#endif
7460
7461
7462/*
7463 * Decide whether to use the "new look" (small, non-bold font) or the "old
7464 * look" (big, clanky font) for dialogs, and work out a few values for use
7465 * later accordingly.
7466 */
7467 static void
7468get_dialog_font_metrics(void)
7469{
7470 HDC hdc;
7471 HFONT hfontTools = 0;
7472 DWORD dlgFontSize;
7473 SIZE size;
7474#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007475 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476#endif
7477
7478 s_usenewlook = FALSE;
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007481 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007482 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007483 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#endif
7485 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7486 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7487
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007488 if (hfontTools)
7489 {
7490 hdc = GetDC(s_hwnd);
7491 SelectObject(hdc, hfontTools);
7492 /*
7493 * GetTextMetrics() doesn't return the right value in
7494 * tmAveCharWidth, so we have to figure out the dialog base units
7495 * ourselves.
7496 */
7497 GetTextExtentPoint(hdc,
7498 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7499 52, &size);
7500 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007502 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7503 s_dlgfntheight = (WORD)size.cy;
7504 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506
7507 if (!s_usenewlook)
7508 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007509 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 s_dlgfntwidth = LOWORD(dlgFontSize);
7511 s_dlgfntheight = HIWORD(dlgFontSize);
7512 }
7513}
7514
7515#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7516/*
7517 * Create a pseudo-"tearoff menu" based on the child
7518 * items of a given menu pointer.
7519 */
7520 static void
7521gui_mch_tearoff(
7522 char_u *title,
7523 vimmenu_T *menu,
7524 int initX,
7525 int initY)
7526{
7527 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7528 int template_len;
7529 int nchar, textWidth, submenuWidth;
7530 DWORD lStyle;
7531 DWORD lExtendedStyle;
7532 WORD dlgwidth;
7533 WORD menuID;
7534 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007535 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 vimmenu_T *the_menu = menu;
7537 HWND hwnd;
7538 HDC hdc;
7539 HFONT font, oldFont;
7540 int col, spaceWidth, len;
7541 int columnWidths[2];
7542 char_u *label, *text;
7543 int acLen = 0;
7544 int nameLen;
7545 int padding0, padding1, padding2 = 0;
7546 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007547 int x;
7548 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007549# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007550 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 /*
7555 * If this menu is already torn off, move it to the mouse position.
7556 */
7557 if (IsWindow(menu->tearoff_handle))
7558 {
7559 POINT mp;
7560 if (GetCursorPos((LPPOINT)&mp))
7561 {
7562 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7563 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7564 }
7565 return;
7566 }
7567
7568 /*
7569 * Create a new tearoff.
7570 */
7571 if (*title == MNU_HIDDEN_CHAR)
7572 title++;
7573
Bram Moolenaar734a8672019-12-02 22:49:38 +01007574 // Allocate memory to store the dialog template. It's made bigger when
7575 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 template_len = DLG_ALLOC_SIZE;
7577 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7578 if (p == NULL)
7579 return;
7580
7581 hwnd = GetDesktopWindow();
7582 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007583# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7585 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007586 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 use_lfSysmenu = TRUE;
7588 }
7589 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7592 VARIABLE_PITCH , DLG_FONT_NAME);
7593 if (s_usenewlook)
7594 oldFont = SelectFont(hdc, font);
7595 else
7596 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7597
Bram Moolenaar734a8672019-12-02 22:49:38 +01007598 // Calculate width of a single space. Used for padding columns to the
7599 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007600 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601
Bram Moolenaar734a8672019-12-02 22:49:38 +01007602 // Figure out max width of the text column, the accelerator column and the
7603 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 submenuWidth = 0;
7605 for (col = 0; col < 2; col++)
7606 {
7607 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007608 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007610 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 text = (col == 0) ? pmenu->dname : pmenu->actext;
7612 if (text != NULL && *text != NUL)
7613 {
7614 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7615 if (textWidth > columnWidths[col])
7616 columnWidths[col] = textWidth;
7617 }
7618 if (pmenu->children != NULL)
7619 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7620 }
7621 }
7622 if (columnWidths[1] == 0)
7623 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007624 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 if (submenuWidth != 0)
7626 columnWidths[0] += submenuWidth;
7627 else
7628 columnWidths[0] += spaceWidth;
7629 }
7630 else
7631 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007632 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7634 columnWidths[1] += submenuWidth;
7635 }
7636
7637 /*
7638 * Now find the total width of our 'menu'.
7639 */
7640 textWidth = columnWidths[0] + columnWidths[1];
7641 if (submenuWidth != 0)
7642 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007643 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7645 textWidth += submenuWidth;
7646 }
7647 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7648 if (textWidth > dlgwidth)
7649 dlgwidth = textWidth;
7650 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7651
Bram Moolenaar734a8672019-12-02 22:49:38 +01007652 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (s_usenewlook)
7654 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7655 else
7656 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7657
7658 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7659 *p++ = LOWORD(lStyle);
7660 *p++ = HIWORD(lStyle);
7661 *p++ = LOWORD(lExtendedStyle);
7662 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007663 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007665 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007667 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 else
7669 *p++ = PixelToDialogX(initX); // x
7670 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007671 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 else
7673 *p++ = PixelToDialogY(initY); // y
7674 *p++ = PixelToDialogX(dlgwidth); // cx
7675 ptrueheight = p;
7676 *p++ = 0; // dialog height: changed later anyway
7677 *p++ = 0; // Menu
7678 *p++ = 0; // Class
7679
Bram Moolenaar734a8672019-12-02 22:49:38 +01007680 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007682 ? (LPSTR)title
7683 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 p += nchar;
7685
7686 if (s_usenewlook)
7687 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007688 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007689# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (use_lfSysmenu)
7691 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007692 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7694 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007695 wcscpy(p, lfSysmenu.lfFaceName);
7696 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 }
7698 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
7701 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007702 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 p += nchar;
7705 }
7706
7707 /*
7708 * Loop over all the items in the menu.
7709 * But skip over the tearbar.
7710 */
7711 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7712 menu = menu->children->next;
7713 else
7714 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007715 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 for ( ; menu != NULL; menu = menu->next)
7717 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007718 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 continue;
7720 if (menu_is_separator(menu->dname))
7721 {
7722 sepPadding += 3;
7723 continue;
7724 }
7725
Bram Moolenaar734a8672019-12-02 22:49:38 +01007726 // Check if there still is plenty of room in the template. Make it
7727 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7729 {
7730 WORD *newp;
7731
7732 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7733 if (newp != NULL)
7734 {
7735 template_len += 4096;
7736 mch_memmove(newp, pdlgtemplate,
7737 (char *)p - (char *)pdlgtemplate);
7738 p = newp + (p - pdlgtemplate);
7739 pnumitems = newp + (pnumitems - pdlgtemplate);
7740 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7741 LocalFree(LocalHandle(pdlgtemplate));
7742 pdlgtemplate = newp;
7743 }
7744 }
7745
Bram Moolenaar734a8672019-12-02 22:49:38 +01007746 // Figure out minimal length of this menu label. Use "name" for the
7747 // actual text, "dname" for estimating the displayed size. "name"
7748 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 len = nameLen = (int)STRLEN(menu->name);
7750 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7751 (int)STRLEN(menu->dname))) / spaceWidth;
7752 len += padding0;
7753
7754 if (menu->actext != NULL)
7755 {
7756 acLen = (int)STRLEN(menu->actext);
7757 len += acLen;
7758 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7759 }
7760 else
7761 textWidth = 0;
7762 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7763 len += padding1;
7764
7765 if (menu->children == NULL)
7766 {
7767 padding2 = submenuWidth / spaceWidth;
7768 len += padding2;
7769 menuID = (WORD)(menu->id);
7770 }
7771 else
7772 {
7773 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007774 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776
Bram Moolenaar734a8672019-12-02 22:49:38 +01007777 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007778 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 if (label == NULL)
7780 break;
7781
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007782 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007783 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007785 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 while (padding0-- > 0)
7787 *text++ = ' ';
7788 if (menu->actext != NULL)
7789 {
7790 STRNCPY(text, menu->actext, acLen);
7791 text += acLen;
7792 }
7793 while (padding1-- > 0)
7794 *text++ = ' ';
7795 if (menu->children != NULL)
7796 {
7797 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7798 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7799 }
7800 else
7801 {
7802 while (padding2-- > 0)
7803 *text++ = ' ';
7804 }
7805 *text = NUL;
7806
7807 /*
7808 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7809 * W95/NT4 it makes the tear-off look more like a menu.
7810 */
7811 p = add_dialog_element(p,
7812 BS_PUSHBUTTON|BS_LEFT,
7813 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7814 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7815 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7816 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007817 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 vim_free(label);
7819 (*pnumitems)++;
7820 }
7821
7822 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7823
7824
Bram Moolenaar734a8672019-12-02 22:49:38 +01007825 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007826 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007827 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 (LPDLGTEMPLATE)pdlgtemplate,
7829 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007830 (DLGPROC)tearoff_callback,
7831 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
7833 LocalFree(LocalHandle(pdlgtemplate));
7834 SelectFont(hdc, oldFont);
7835 DeleteObject(font);
7836 ReleaseDC(hwnd, hdc);
7837
7838 /*
7839 * Reassert ourselves as the active window. This is so that after creating
7840 * a tearoff, the user doesn't have to click with the mouse just to start
7841 * typing again!
7842 */
7843 (void)SetActiveWindow(s_hwnd);
7844
Bram Moolenaar734a8672019-12-02 22:49:38 +01007845 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 force_menu_update = TRUE;
7847}
7848#endif
7849
7850#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007851# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
Bram Moolenaar734a8672019-12-02 22:49:38 +01007853// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854# ifndef TBSTYLE_FLAT
7855# define TBSTYLE_FLAT 0x0800
7856# endif
7857
7858/*
7859 * Create the toolbar, initially unpopulated.
7860 * (just like the menu, there are no defaults, it's all
7861 * set up through menu.vim)
7862 */
7863 static void
7864initialise_toolbar(void)
7865{
7866 InitCommonControls();
7867 s_toolbarhwnd = CreateToolbarEx(
7868 s_hwnd,
7869 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7870 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007871 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007872 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 IDR_TOOLBAR1, // id of initial bitmap
7874 NULL,
7875 0, // initial number of buttons
7876 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7877 TOOLBAR_BUTTON_HEIGHT,
7878 TOOLBAR_BUTTON_WIDTH,
7879 TOOLBAR_BUTTON_HEIGHT,
7880 sizeof(TBBUTTON)
7881 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007882
7883 // Remove transparency from the toolbar to prevent the main window
7884 // background colour showing through
7885 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7886 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7887
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007888 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
7890 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7891}
7892
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007893 static LRESULT CALLBACK
7894toolbar_wndproc(
7895 HWND hwnd,
7896 UINT uMsg,
7897 WPARAM wParam,
7898 LPARAM lParam)
7899{
7900 HandleMouseHide(uMsg, lParam);
7901 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7902}
7903
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 static int
7905get_toolbar_bitmap(vimmenu_T *menu)
7906{
7907 int i = -1;
7908
7909 /*
7910 * Check user bitmaps first, unless builtin is specified.
7911 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007912 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
7914 char_u fname[MAXPATHL];
7915 HANDLE hbitmap = NULL;
7916
7917 if (menu->iconfile != NULL)
7918 {
7919 gui_find_iconfile(menu->iconfile, fname, "bmp");
7920 hbitmap = LoadImage(
7921 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007922 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 IMAGE_BITMAP,
7924 TOOLBAR_BUTTON_WIDTH,
7925 TOOLBAR_BUTTON_HEIGHT,
7926 LR_LOADFROMFILE |
7927 LR_LOADMAP3DCOLORS
7928 );
7929 }
7930
7931 /*
7932 * If the LoadImage call failed, or the "icon=" file
7933 * didn't exist or wasn't specified, try the menu name
7934 */
7935 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007936 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007937# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007938 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007939# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007940 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 hbitmap = LoadImage(
7942 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007943 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 IMAGE_BITMAP,
7945 TOOLBAR_BUTTON_WIDTH,
7946 TOOLBAR_BUTTON_HEIGHT,
7947 LR_LOADFROMFILE |
7948 LR_LOADMAP3DCOLORS
7949 );
7950
7951 if (hbitmap != NULL)
7952 {
7953 TBADDBITMAP tbAddBitmap;
7954
7955 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007956 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957
7958 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7959 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007960 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962 }
7963 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7964 i = menu->iconidx;
7965
7966 return i;
7967}
7968#endif
7969
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007970#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7971 static void
7972initialise_tabline(void)
7973{
7974 InitCommonControls();
7975
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007976 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007977 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007978 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007979 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007980 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007981
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007982 gui.tabline_height = TABLINE_HEIGHT;
7983
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007984# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007985 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007986# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007987}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007988
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007989/*
7990 * Get tabpage_T from POINT.
7991 */
7992 static tabpage_T *
7993GetTabFromPoint(
7994 HWND hWnd,
7995 POINT pt)
7996{
7997 tabpage_T *ptp = NULL;
7998
7999 if (gui_mch_showing_tabline())
8000 {
8001 TCHITTESTINFO htinfo;
8002 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008003 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008004 if (s_tabhwnd == hWnd)
8005 {
8006 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8007 if (idx != -1)
8008 ptp = find_tabpage(idx + 1);
8009 }
8010 }
8011 return ptp;
8012}
8013
8014static POINT s_pt = {0, 0};
8015static HCURSOR s_hCursor = NULL;
8016
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008017 static LRESULT CALLBACK
8018tabline_wndproc(
8019 HWND hwnd,
8020 UINT uMsg,
8021 WPARAM wParam,
8022 LPARAM lParam)
8023{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008024 POINT pt;
8025 tabpage_T *tp;
8026 RECT rect;
8027 int nCenter;
8028 int idx0;
8029 int idx1;
8030
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008031 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008032
8033 switch (uMsg)
8034 {
8035 case WM_LBUTTONDOWN:
8036 {
8037 s_pt.x = GET_X_LPARAM(lParam);
8038 s_pt.y = GET_Y_LPARAM(lParam);
8039 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008040 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008041 break;
8042 }
8043 case WM_MOUSEMOVE:
8044 if (GetCapture() == hwnd
8045 && ((wParam & MK_LBUTTON)) != 0)
8046 {
8047 pt.x = GET_X_LPARAM(lParam);
8048 pt.y = s_pt.y;
8049 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8050 {
8051 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8052
8053 tp = GetTabFromPoint(hwnd, pt);
8054 if (tp != NULL)
8055 {
8056 idx0 = tabpage_index(curtab) - 1;
8057 idx1 = tabpage_index(tp) - 1;
8058
8059 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8060 nCenter = rect.left + (rect.right - rect.left) / 2;
8061
Bram Moolenaar734a8672019-12-02 22:49:38 +01008062 // Check if the mouse cursor goes over the center of
8063 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008064 if ((idx0 < idx1) && (nCenter < pt.x))
8065 {
8066 tabpage_move(idx1 + 1);
8067 update_screen(0);
8068 }
8069 else if ((idx1 < idx0) && (pt.x < nCenter))
8070 {
8071 tabpage_move(idx1);
8072 update_screen(0);
8073 }
8074 }
8075 }
8076 }
8077 break;
8078 case WM_LBUTTONUP:
8079 {
8080 if (GetCapture() == hwnd)
8081 {
8082 SetCursor(s_hCursor);
8083 ReleaseCapture();
8084 }
8085 break;
8086 }
8087 default:
8088 break;
8089 }
8090
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008091 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8092}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008093#endif
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8096/*
8097 * Make the GUI window come to the foreground.
8098 */
8099 void
8100gui_mch_set_foreground(void)
8101{
8102 if (IsIconic(s_hwnd))
8103 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8104 SetForegroundWindow(s_hwnd);
8105}
8106#endif
8107
8108#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8109 static void
8110dyn_imm_load(void)
8111{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008112 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (hLibImm == NULL)
8114 return;
8115
8116 pImmGetCompositionStringA
8117 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8118 pImmGetCompositionStringW
8119 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8120 pImmGetContext
8121 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8122 pImmAssociateContext
8123 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8124 pImmReleaseContext
8125 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8126 pImmGetOpenStatus
8127 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8128 pImmSetOpenStatus
8129 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008130 pImmGetCompositionFontW
8131 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8132 pImmSetCompositionFontW
8133 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 pImmSetCompositionWindow
8135 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8136 pImmGetConversionStatus
8137 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008138 pImmSetConversionStatus
8139 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 if ( pImmGetCompositionStringA == NULL
8142 || pImmGetCompositionStringW == NULL
8143 || pImmGetContext == NULL
8144 || pImmAssociateContext == NULL
8145 || pImmReleaseContext == NULL
8146 || pImmGetOpenStatus == NULL
8147 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008148 || pImmGetCompositionFontW == NULL
8149 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008151 || pImmGetConversionStatus == NULL
8152 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
8154 FreeLibrary(hLibImm);
8155 hLibImm = NULL;
8156 pImmGetContext = NULL;
8157 return;
8158 }
8159
8160 return;
8161}
8162
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163#endif
8164
8165#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8166
8167# ifdef FEAT_XPM_W32
8168# define IMAGE_XPM 100
8169# endif
8170
8171typedef struct _signicon_t
8172{
8173 HANDLE hImage;
8174 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008175# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008176 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178} signicon_t;
8179
8180 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008181gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 signicon_t *sign;
8184 int x, y, w, h;
8185
8186 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8187 return;
8188
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008189# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008190 if (IS_ENABLE_DIRECTX())
8191 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008192# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008193
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 x = TEXT_X(col);
8195 y = TEXT_Y(row);
8196 w = gui.char_width * 2;
8197 h = gui.char_height;
8198 switch (sign->uType)
8199 {
8200 case IMAGE_BITMAP:
8201 {
8202 HDC hdcMem;
8203 HBITMAP hbmpOld;
8204
8205 hdcMem = CreateCompatibleDC(s_hdc);
8206 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8207 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8208 SelectObject(hdcMem, hbmpOld);
8209 DeleteDC(hdcMem);
8210 }
8211 break;
8212 case IMAGE_ICON:
8213 case IMAGE_CURSOR:
8214 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8215 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008216# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 case IMAGE_XPM:
8218 {
8219 HDC hdcMem;
8220 HBITMAP hbmpOld;
8221
8222 hdcMem = CreateCompatibleDC(s_hdc);
8223 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008224 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8226
8227 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008228 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8230 SelectObject(hdcMem, hbmpOld);
8231 DeleteDC(hdcMem);
8232 }
8233 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
8236}
8237
8238 static void
8239close_signicon_image(signicon_t *sign)
8240{
8241 if (sign)
8242 switch (sign->uType)
8243 {
8244 case IMAGE_BITMAP:
8245 DeleteObject((HGDIOBJ)sign->hImage);
8246 break;
8247 case IMAGE_CURSOR:
8248 DestroyCursor((HCURSOR)sign->hImage);
8249 break;
8250 case IMAGE_ICON:
8251 DestroyIcon((HICON)sign->hImage);
8252 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008253# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 case IMAGE_XPM:
8255 DeleteObject((HBITMAP)sign->hImage);
8256 DeleteObject((HBITMAP)sign->hShape);
8257 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260}
8261
8262 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008263gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264{
8265 signicon_t sign, *psign;
8266 char_u *ext;
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008269 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 if (ext > signfile)
8271 {
8272 int do_load = 1;
8273
8274 if (!STRICMP(ext, ".bmp"))
8275 sign.uType = IMAGE_BITMAP;
8276 else if (!STRICMP(ext, ".ico"))
8277 sign.uType = IMAGE_ICON;
8278 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8279 sign.uType = IMAGE_CURSOR;
8280 else
8281 do_load = 0;
8282
8283 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008284 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 gui.char_width * 2, gui.char_height,
8286 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008287# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (!STRICMP(ext, ".xpm"))
8289 {
8290 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008291 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8292 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296
8297 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008298 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 *psign = sign;
8300
8301 if (!psign)
8302 {
8303 if (sign.hImage)
8304 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008305 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307 return (void *)psign;
8308
8309}
8310
8311 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008312gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 if (sign)
8315 {
8316 close_signicon_image((signicon_t *)sign);
8317 vim_free(sign);
8318 }
8319}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008322#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
Bram Moolenaar734a8672019-12-02 22:49:38 +01008324/*
8325 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008326 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008328 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8330 * to get current mouse position).
8331 *
8332 * Trying to use as more Windows services as possible, and as less
8333 * IE version as possible :)).
8334 *
8335 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8336 * BalloonEval struct.
8337 * 2) Enable/Disable simply create/kill BalloonEval Timer
8338 * 3) When there was enough inactivity, timer procedure posts
8339 * async request to debugger
8340 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8341 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008342 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 */
8344
Bram Moolenaar45360022005-07-21 21:08:21 +00008345/*
8346 * determine whether installed Common Controls support multiline tooltips
8347 * (i.e. their version is >= 4.70
8348 */
8349 int
8350multiline_balloon_available(void)
8351{
8352 HINSTANCE hDll;
8353 static char comctl_dll[] = "comctl32.dll";
8354 static int multiline_tip = MAYBE;
8355
8356 if (multiline_tip != MAYBE)
8357 return multiline_tip;
8358
8359 hDll = GetModuleHandle(comctl_dll);
8360 if (hDll != NULL)
8361 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008362 DLLGETVERSIONPROC pGetVer;
8363 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008364
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008365 if (pGetVer != NULL)
8366 {
8367 DLLVERSIONINFO dvi;
8368 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008369
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008370 ZeroMemory(&dvi, sizeof(dvi));
8371 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008372
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008373 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008374
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008375 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008376 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008377 || (dvi.dwMajorVersion == 4
8378 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008379 {
8380 multiline_tip = TRUE;
8381 return multiline_tip;
8382 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008383 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008384 else
8385 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008386 // there is chance we have ancient CommCtl 4.70
8387 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008388 DWORD dwHandle = 0;
8389 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8390 if (len > 0)
8391 {
8392 VS_FIXEDFILEINFO *ver;
8393 UINT vlen = 0;
8394 void *data = alloc(len);
8395
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008396 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008397 && GetFileVersionInfo(comctl_dll, 0, len, data)
8398 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8399 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008400 && HIWORD(ver->dwFileVersionMS) > 4)
8401 || ((HIWORD(ver->dwFileVersionMS) == 4
8402 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008403 {
8404 vim_free(data);
8405 multiline_tip = TRUE;
8406 return multiline_tip;
8407 }
8408 vim_free(data);
8409 }
8410 }
8411 }
8412 multiline_tip = FALSE;
8413 return multiline_tip;
8414}
8415
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008416 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008417make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008418{
8419 TOOLINFOW *pti;
8420 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008421
8422 if (multiline_balloon_available() == TRUE)
8423 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8424 else
8425 ToolInfoSize = sizeof(TOOLINFOW);
8426
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008427 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008428 if (pti == NULL)
8429 return;
8430
8431 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8432 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8433 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008434 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008435
8436 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8437 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8438
8439 pti->cbSize = ToolInfoSize;
8440 pti->uFlags = TTF_SUBCLASS;
8441 pti->hwnd = beval->target;
8442 pti->hinst = 0; // Don't use string resources
8443 pti->uId = ID_BEVAL_TOOLTIP;
8444
8445 if (multiline_balloon_available() == TRUE)
8446 {
8447 RECT rect;
8448 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8449 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008450 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8451 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008452 // switch multiline tooltips on
8453 if (GetClientRect(s_textArea, &rect))
8454 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8455 (LPARAM)rect.right);
8456 }
8457 else
8458 {
8459 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008460 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8461 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008462 }
8463
8464 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8465 pti->rect.left = pt.x - 3;
8466 pti->rect.top = pt.y - 3;
8467 pti->rect.right = pt.x + 3;
8468 pti->rect.bottom = pt.y + 3;
8469
8470 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8471 // Make tooltip appear sooner.
8472 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8473 // I've performed some tests and it seems the longest possible life time
8474 // of tooltip is 30 seconds.
8475 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8476 /*
8477 * HACK: force tooltip to appear, because it'll not appear until
8478 * first mouse move. D*mn M$
8479 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8480 */
8481 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8482 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8483 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008484}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008487delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008489 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490}
8491
8492 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008493BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008494 HWND hwnd UNUSED,
8495 UINT uMsg UNUSED,
8496 UINT_PTR idEvent UNUSED,
8497 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 POINT pt;
8500 RECT rect;
8501
8502 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8503 return;
8504
8505 GetCursorPos(&pt);
8506 if (WindowFromPoint(pt) != s_textArea)
8507 return;
8508
8509 ScreenToClient(s_textArea, &pt);
8510 GetClientRect(s_textArea, &rect);
8511 if (!PtInRect(&rect, pt))
8512 return;
8513
8514 if (LastActivity > 0
8515 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8516 && (cur_beval->showState != ShS_PENDING
8517 || abs(cur_beval->x - pt.x) > 3
8518 || abs(cur_beval->y - pt.y) > 3))
8519 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008520 // Pointer resting in one place long enough, it's time to show
8521 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 cur_beval->showState = ShS_PENDING;
8523 cur_beval->x = pt.x;
8524 cur_beval->y = pt.y;
8525
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008526 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527
8528 if (cur_beval->msgCB != NULL)
8529 (*cur_beval->msgCB)(cur_beval, 0);
8530 }
8531}
8532
8533 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008534gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008536 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008538 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539}
8540
8541 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008542gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008544 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (beval == NULL)
8546 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008547 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008548 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008549 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550}
8551
8552 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008553gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
8555 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008556
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008557 vim_free(beval->msg);
8558 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8559 if (beval->msg == NULL)
8560 {
8561 delete_tooltip(beval);
8562 beval->showState = ShS_NEUTRAL;
8563 return;
8564 }
8565
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008566 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 if (beval->showState == ShS_SHOWING)
8568 return;
8569 GetCursorPos(&pt);
8570 ScreenToClient(s_textArea, &pt);
8571
8572 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008574 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 gui_mch_disable_beval_area(cur_beval);
8576 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008577 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008579 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580}
8581
8582 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008583gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008584 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008585 char_u *mesg,
8586 void (*mesgCB)(BalloonEval *, int),
8587 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008589 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 BalloonEval *beval;
8591
8592 if (mesg != NULL && mesgCB != NULL)
8593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008594 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 return NULL;
8596 }
8597
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008598 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 if (beval != NULL)
8600 {
8601 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 beval->msg = mesg;
8605 beval->msgCB = mesgCB;
8606 beval->clientData = clientData;
8607
8608 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 cur_beval = beval;
8610
8611 if (p_beval)
8612 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614 return beval;
8615}
8616
8617 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008618Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008620 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 return;
8622
8623 if (cur_beval != NULL)
8624 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008625 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008627 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008628 // TRACE0("TTN_SHOW {{{");
8629 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008630 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008631 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008632 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 delete_tooltip(cur_beval);
8634 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008635 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
8637 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008638 break;
8639 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008640 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008641 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008642 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8643 info->lpszText = (LPSTR)info->lParam;
8644 info->uFlags |= TTF_DI_SETITEM;
8645 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008646 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008647 case TTN_GETDISPINFOW:
8648 {
8649 // if we get here then we have new common controls
8650 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8651 info->lpszText = (LPWSTR)info->lParam;
8652 info->uFlags |= TTF_DI_SETITEM;
8653 }
8654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 }
8656 }
8657}
8658
8659 static void
8660TrackUserActivity(UINT uMsg)
8661{
8662 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8663 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8664 LastActivity = GetTickCount();
8665}
8666
8667 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008668gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008670# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008671 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008672# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008673 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 vim_free(beval);
8675}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008676#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
8678#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8679/*
8680 * We have multiple signs to draw at the same location. Draw the
8681 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8682 */
8683 void
8684netbeans_draw_multisign_indicator(int row)
8685{
8686 int i;
8687 int y;
8688 int x;
8689
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008690 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008691 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008692
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 x = 0;
8694 y = TEXT_Y(row);
8695
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008696# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008697 if (IS_ENABLE_DIRECTX())
8698 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008699# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008700
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 for (i = 0; i < gui.char_height - 3; i++)
8702 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8703
8704 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8705 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8706 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8707 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8708 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8709 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8710 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8711}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008712#endif