blob: c3178ba5d38d5d2360f1a210c46c04fd22a8ba30 [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 Moolenaar92467d32017-12-05 13:22:16 +010036static int s_directx_scrlines = 0;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020037# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010038static int directx_enabled(void);
39static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010040#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020041
Bram Moolenaar065bbac2016-02-20 13:08:46 +010042#ifdef FEAT_MENU
43static int gui_mswin_get_menu_height(int fix_window);
44#endif
45
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020046#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
47 int
48gui_mch_set_rendering_options(char_u *s)
49{
50#ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020051 char_u *p, *q;
52
53 int dx_enable = 0;
54 int dx_flags = 0;
55 float dx_gamma = 0.0f;
56 float dx_contrast = 0.0f;
57 float dx_level = 0.0f;
58 int dx_geom = 0;
59 int dx_renmode = 0;
60 int dx_taamode = 0;
Bram Moolenaar92467d32017-12-05 13:22:16 +010061 int dx_scrlines = 0;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062
63 /* parse string as rendering options. */
64 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
124 dx_scrlines = atoi((char *)value);
125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
130 /* Enable DirectX/DirectWrite */
131 if (dx_enable)
132 {
133 if (!directx_enabled())
134 return FAIL;
135 DWriteContext_SetRenderingParams(s_dwc, NULL);
136 if (dx_flags)
137 {
138 DWriteRenderingParams param;
139 DWriteContext_GetRenderingParams(s_dwc, &param);
140 if (dx_flags & (1 << 0))
141 param.gamma = dx_gamma;
142 if (dx_flags & (1 << 1))
143 param.enhancedContrast = dx_contrast;
144 if (dx_flags & (1 << 2))
145 param.clearTypeLevel = dx_level;
146 if (dx_flags & (1 << 3))
147 param.pixelGeometry = dx_geom;
148 if (dx_flags & (1 << 4))
149 param.renderingMode = dx_renmode;
150 if (dx_flags & (1 << 5))
151 param.textAntialiasMode = dx_taamode;
152 DWriteContext_SetRenderingParams(s_dwc, &param);
153 }
154 }
155 s_directx_enabled = dx_enable;
Bram Moolenaar92467d32017-12-05 13:22:16 +0100156 s_directx_scrlines = dx_scrlines;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200157
158 return OK;
159#else
160 return FAIL;
161#endif
162}
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
182#include "version.h" /* used by dialog box routine for default title */
183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
187/* cproto fails on missing include files */
188#ifndef PROTO
189
190#ifndef __MINGW32__
191# include <shellapi.h>
192#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100193#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100194# include <commctrl.h>
195#endif
196#include <windowsx.h>
197
198#ifdef GLOBAL_IME
199# include "glbl_ime.h"
200#endif
201
202#endif /* PROTO */
203
204#ifdef FEAT_MENU
205# define MENUHINTS /* show menu hints in command line */
206#endif
207
208/* Some parameters for dialog boxes. All in pixels. */
209#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
213#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
214#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
223#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
224
225#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
226# 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 Moolenaara6b7a082016-08-10 20:53:05 +0200243# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100244# define _cdecl
245typedef int BOOL;
246typedef int BYTE;
247typedef int DWORD;
248typedef int WCHAR;
249typedef int ENUMLOGFONT;
250typedef int FINDREPLACE;
251typedef int HANDLE;
252typedef int HBITMAP;
253typedef int HBRUSH;
254typedef int HDROP;
255typedef int INT;
256typedef int LOGFONT[];
257typedef int LPARAM;
258typedef int LPCREATESTRUCT;
259typedef int LPCSTR;
260typedef int LPCTSTR;
261typedef int LPRECT;
262typedef int LPSTR;
263typedef int LPWINDOWPOS;
264typedef int LPWORD;
265typedef int LRESULT;
266typedef int HRESULT;
267# undef MSG
268typedef int MSG;
269typedef int NEWTEXTMETRIC;
270typedef int OSVERSIONINFO;
271typedef int PWORD;
272typedef int RECT;
273typedef int UINT;
274typedef int WORD;
275typedef int WPARAM;
276typedef int POINT;
277typedef void *HINSTANCE;
278typedef void *HMENU;
279typedef void *HWND;
280typedef void *HDC;
281typedef void VOID;
282typedef int LPNMHDR;
283typedef int LONG;
284typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200285typedef int UINT_PTR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100286#endif
287
288#ifndef GET_X_LPARAM
289# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
290#endif
291
292static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100293static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100294static void clear_rect(RECT *rcp);
295
296static WORD s_dlgfntheight; /* height of the dialog font */
297static WORD s_dlgfntwidth; /* width of the dialog font */
298
299#ifdef FEAT_MENU
300static HMENU s_menuBar = NULL;
301#endif
302#ifdef FEAT_TEAROFF
303static void rebuild_tearoff(vimmenu_T *menu);
304static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
305#endif
306
307/* Flag that is set while processing a message that must not be interrupted by
308 * processing another message. */
309static int s_busy_processing = FALSE;
310
311static int destroying = FALSE; /* call DestroyWindow() ourselves */
312
313#ifdef MSWIN_FIND_REPLACE
314static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
315static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200316# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100317static FINDREPLACEW s_findrep_struct_w;
318# endif
319static HWND s_findrep_hwnd = NULL;
320static int s_findrep_is_find; /* TRUE for find dialog, FALSE
321 for find/replace dialog */
322#endif
323
324static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100325#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326static
327#endif
328HWND s_hwnd = NULL;
329static HDC s_hdc = NULL;
330static HBRUSH s_brush = NULL;
331
332#ifdef FEAT_TOOLBAR
333static HWND s_toolbarhwnd = NULL;
334static WNDPROC s_toolbar_wndproc = NULL;
335#endif
336
337#ifdef FEAT_GUI_TABLINE
338static HWND s_tabhwnd = NULL;
339static WNDPROC s_tabline_wndproc = NULL;
340static int showing_tabline = 0;
341#endif
342
343static WPARAM s_wParam = 0;
344static LPARAM s_lParam = 0;
345
346static HWND s_textArea = NULL;
347static UINT s_uMsg = 0;
348
349static char_u *s_textfield; /* Used by dialogs to pass back strings */
350
351static int s_need_activate = FALSE;
352
353/* This variable is set when waiting for an event, which is the only moment
354 * scrollbar dragging can be done directly. It's not allowed while commands
355 * are executed, because it may move the cursor and that may cause unexpected
356 * problems (e.g., while ":s" is working).
357 */
358static int allow_scrollbar = FALSE;
359
360#ifdef GLOBAL_IME
361# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
362#else
363# define MyTranslateMessage(x) TranslateMessage(x)
364#endif
365
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100366#if defined(FEAT_DIRECTX)
367 static int
368directx_enabled(void)
369{
370 if (s_dwc != NULL)
371 return 1;
372 else if (s_directx_load_attempted)
373 return 0;
374 /* load DirectX */
375 DWrite_Init();
376 s_directx_load_attempted = 1;
377 s_dwc = DWriteContext_Open();
378 directx_binddc();
379 return s_dwc != NULL ? 1 : 0;
380}
381
382 static void
383directx_binddc(void)
384{
385 if (s_textArea != NULL)
386 {
387 RECT rect;
388 GetClientRect(s_textArea, &rect);
389 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
390 }
391}
392#endif
393
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200394#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100395 /* use of WindowProc depends on wide_WindowProc */
396# define MyWindowProc vim_WindowProc
397#else
398 /* use ordinary WindowProc */
399# define MyWindowProc DefWindowProc
400#endif
401
402extern int current_font_height; /* this is in os_mswin.c */
403
404static struct
405{
406 UINT key_sym;
407 char_u vim_code0;
408 char_u vim_code1;
409} special_keys[] =
410{
411 {VK_UP, 'k', 'u'},
412 {VK_DOWN, 'k', 'd'},
413 {VK_LEFT, 'k', 'l'},
414 {VK_RIGHT, 'k', 'r'},
415
416 {VK_F1, 'k', '1'},
417 {VK_F2, 'k', '2'},
418 {VK_F3, 'k', '3'},
419 {VK_F4, 'k', '4'},
420 {VK_F5, 'k', '5'},
421 {VK_F6, 'k', '6'},
422 {VK_F7, 'k', '7'},
423 {VK_F8, 'k', '8'},
424 {VK_F9, 'k', '9'},
425 {VK_F10, 'k', ';'},
426
427 {VK_F11, 'F', '1'},
428 {VK_F12, 'F', '2'},
429 {VK_F13, 'F', '3'},
430 {VK_F14, 'F', '4'},
431 {VK_F15, 'F', '5'},
432 {VK_F16, 'F', '6'},
433 {VK_F17, 'F', '7'},
434 {VK_F18, 'F', '8'},
435 {VK_F19, 'F', '9'},
436 {VK_F20, 'F', 'A'},
437
438 {VK_F21, 'F', 'B'},
439#ifdef FEAT_NETBEANS_INTG
440 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
441#endif
442 {VK_F22, 'F', 'C'},
443 {VK_F23, 'F', 'D'},
444 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
445
446 {VK_HELP, '%', '1'},
447 {VK_BACK, 'k', 'b'},
448 {VK_INSERT, 'k', 'I'},
449 {VK_DELETE, 'k', 'D'},
450 {VK_HOME, 'k', 'h'},
451 {VK_END, '@', '7'},
452 {VK_PRIOR, 'k', 'P'},
453 {VK_NEXT, 'k', 'N'},
454 {VK_PRINT, '%', '9'},
455 {VK_ADD, 'K', '6'},
456 {VK_SUBTRACT, 'K', '7'},
457 {VK_DIVIDE, 'K', '8'},
458 {VK_MULTIPLY, 'K', '9'},
459 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
460 {VK_DECIMAL, 'K', 'B'},
461
462 {VK_NUMPAD0, 'K', 'C'},
463 {VK_NUMPAD1, 'K', 'D'},
464 {VK_NUMPAD2, 'K', 'E'},
465 {VK_NUMPAD3, 'K', 'F'},
466 {VK_NUMPAD4, 'K', 'G'},
467 {VK_NUMPAD5, 'K', 'H'},
468 {VK_NUMPAD6, 'K', 'I'},
469 {VK_NUMPAD7, 'K', 'J'},
470 {VK_NUMPAD8, 'K', 'K'},
471 {VK_NUMPAD9, 'K', 'L'},
472
473 /* Keys that we want to be able to use any modifier with: */
474 {VK_SPACE, ' ', NUL},
475 {VK_TAB, TAB, NUL},
476 {VK_ESCAPE, ESC, NUL},
477 {NL, NL, NUL},
478 {CAR, CAR, NUL},
479
480 /* End of list marker: */
481 {0, 0, 0}
482};
483
484/* Local variables */
485static int s_button_pending = -1;
486
487/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
488 * so don't reset s_button_pending. */
489static int s_getting_focus = FALSE;
490
491static int s_x_pending;
492static int s_y_pending;
493static UINT s_kFlags_pending;
494static UINT s_wait_timer = 0; /* Timer for get char from user */
495static int s_timed_out = FALSE;
496static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
497
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100498#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100499/* balloon-eval WM_NOTIFY_HANDLER */
500static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
501static void TrackUserActivity(UINT uMsg);
502#endif
503
504/*
505 * For control IME.
506 *
507 * These LOGFONT used for IME.
508 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100509#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100510/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
511static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100512#endif
513#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100514/* holds LOGFONT for 'guifont' always. */
515static LOGFONT sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100516#endif
517
518#ifdef FEAT_MBYTE_IME
519static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
520#endif
521
522#if defined(FEAT_BROWSE)
523static char_u *convert_filter(char_u *s);
524#endif
525
526#ifdef DEBUG_PRINT_ERROR
527/*
528 * Print out the last Windows error message
529 */
530 static void
531print_windows_error(void)
532{
533 LPVOID lpMsgBuf;
534
535 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
536 NULL, GetLastError(),
537 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
538 (LPTSTR) &lpMsgBuf, 0, NULL);
539 TRACE1("Error: %s\n", lpMsgBuf);
540 LocalFree(lpMsgBuf);
541}
542#endif
543
544/*
545 * Cursor blink functions.
546 *
547 * This is a simple state machine:
548 * BLINK_NONE not blinking at all
549 * BLINK_OFF blinking, cursor is not shown
550 * BLINK_ON blinking, cursor is shown
551 */
552
553#define BLINK_NONE 0
554#define BLINK_OFF 1
555#define BLINK_ON 2
556
557static int blink_state = BLINK_NONE;
558static long_u blink_waittime = 700;
559static long_u blink_ontime = 400;
560static long_u blink_offtime = 250;
561static UINT blink_timer = 0;
562
Bram Moolenaar703a8042016-06-04 16:24:32 +0200563 int
564gui_mch_is_blinking(void)
565{
566 return blink_state != BLINK_NONE;
567}
568
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200569 int
570gui_mch_is_blink_off(void)
571{
572 return blink_state == BLINK_OFF;
573}
574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 void
576gui_mch_set_blinking(long wait, long on, long off)
577{
578 blink_waittime = wait;
579 blink_ontime = on;
580 blink_offtime = off;
581}
582
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100583 static VOID CALLBACK
584_OnBlinkTimer(
585 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100586 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100587 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100588 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100589{
590 MSG msg;
591
592 /*
593 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
594 */
595
596 KillTimer(NULL, idEvent);
597
598 /* Eat spurious WM_TIMER messages */
599 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
600 ;
601
602 if (blink_state == BLINK_ON)
603 {
604 gui_undraw_cursor();
605 blink_state = BLINK_OFF;
606 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
607 (TIMERPROC)_OnBlinkTimer);
608 }
609 else
610 {
611 gui_update_cursor(TRUE, FALSE);
612 blink_state = BLINK_ON;
613 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100614 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100615 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100616 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100617}
618
619 static void
620gui_mswin_rm_blink_timer(void)
621{
622 MSG msg;
623
624 if (blink_timer != 0)
625 {
626 KillTimer(NULL, blink_timer);
627 /* Eat spurious WM_TIMER messages */
628 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
629 ;
630 blink_timer = 0;
631 }
632}
633
634/*
635 * Stop the cursor blinking. Show the cursor if it wasn't shown.
636 */
637 void
638gui_mch_stop_blink(void)
639{
640 gui_mswin_rm_blink_timer();
641 if (blink_state == BLINK_OFF)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100642 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100643 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100644 gui_mch_flush();
645 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646 blink_state = BLINK_NONE;
647}
648
649/*
650 * Start the cursor blinking. If it was already blinking, this restarts the
651 * waiting time and shows the cursor.
652 */
653 void
654gui_mch_start_blink(void)
655{
656 gui_mswin_rm_blink_timer();
657
658 /* Only switch blinking on if none of the times is zero */
659 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
660 {
661 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
662 (TIMERPROC)_OnBlinkTimer);
663 blink_state = BLINK_ON;
664 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100665 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100666 }
667}
668
669/*
670 * Call-back routines.
671 */
672
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673 static VOID CALLBACK
674_OnTimer(
675 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100676 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100677 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100678 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679{
680 MSG msg;
681
682 /*
683 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
684 */
685 KillTimer(NULL, idEvent);
686 s_timed_out = TRUE;
687
688 /* Eat spurious WM_TIMER messages */
689 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
690 ;
691 if (idEvent == s_wait_timer)
692 s_wait_timer = 0;
693}
694
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695 static void
696_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100697 HWND hwnd UNUSED,
698 UINT ch UNUSED,
699 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100700{
701 dead_key = 1;
702}
703
704/*
705 * Convert Unicode character "ch" to bytes in "string[slen]".
706 * When "had_alt" is TRUE the ALT key was included in "ch".
707 * Return the length.
708 */
709 static int
710char_to_string(int ch, char_u *string, int slen, int had_alt)
711{
712 int len;
713 int i;
714#ifdef FEAT_MBYTE
715 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200716 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200718 wstring[0] = ch;
719 len = 1;
720
721 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
722 * "enc_codepage" is non-zero use the standard Win32 function,
723 * otherwise use our own conversion function (e.g., for UTF-8). */
724 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100725 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200726 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
727 (LPSTR)string, slen, 0, NULL);
728 /* If we had included the ALT key into the character but now the
729 * upper bit is no longer set, that probably means the conversion
730 * failed. Convert the original character and set the upper bit
731 * afterwards. */
732 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100733 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200734 wstring[0] = ch & 0x7f;
735 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
736 (LPSTR)string, slen, 0, NULL);
737 if (len == 1) /* safety check */
738 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100739 }
740 }
741 else
742 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100743 len = 1;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200744 ws = utf16_to_enc(wstring, &len);
745 if (ws == NULL)
746 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100747 else
748 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200749 if (len > slen) /* just in case */
750 len = slen;
751 mch_memmove(string, ws, len);
752 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100753 }
754 }
755
756 if (len == 0)
757#endif
758 {
759 string[0] = ch;
760 len = 1;
761 }
762
763 for (i = 0; i < len; ++i)
764 if (string[i] == CSI && len <= slen - 2)
765 {
766 /* Insert CSI as K_CSI. */
767 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
768 string[++i] = KS_EXTRA;
769 string[++i] = (int)KE_CSI;
770 len += 2;
771 }
772
773 return len;
774}
775
776/*
777 * Key hit, add it to the input buffer.
778 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100779 static void
780_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100781 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100782 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100783 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100784{
785 char_u string[40];
786 int len = 0;
787
788 dead_key = 0;
789
790 len = char_to_string(ch, string, 40, FALSE);
791 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
792 {
793 trash_input_buf();
794 got_int = TRUE;
795 }
796
797 add_to_input_buf(string, len);
798}
799
800/*
801 * Alt-Key hit, add it to the input buffer.
802 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 static void
804_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100805 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100806 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100807 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100808{
809 char_u string[40]; /* Enough for multibyte character */
810 int len;
811 int modifiers;
812 int ch = cch; /* special keys are negative */
813
814 dead_key = 0;
815
816 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
817
818 /* OK, we have a character key (given by ch) which was entered with the
819 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
820 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
821 * CAPSLOCK is pressed) at this point.
822 */
823 modifiers = MOD_MASK_ALT;
824 if (GetKeyState(VK_SHIFT) & 0x8000)
825 modifiers |= MOD_MASK_SHIFT;
826 if (GetKeyState(VK_CONTROL) & 0x8000)
827 modifiers |= MOD_MASK_CTRL;
828
829 ch = simplify_key(ch, &modifiers);
830 /* remove the SHIFT modifier for keys where it's already included, e.g.,
831 * '(' and '*' */
832 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
833 modifiers &= ~MOD_MASK_SHIFT;
834
835 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
836 ch = extract_modifiers(ch, &modifiers);
837 if (ch == CSI)
838 ch = K_CSI;
839
840 len = 0;
841 if (modifiers)
842 {
843 string[len++] = CSI;
844 string[len++] = KS_MODIFIER;
845 string[len++] = modifiers;
846 }
847
848 if (IS_SPECIAL((int)ch))
849 {
850 string[len++] = CSI;
851 string[len++] = K_SECOND((int)ch);
852 string[len++] = K_THIRD((int)ch);
853 }
854 else
855 {
856 /* Although the documentation isn't clear about it, we assume "ch" is
857 * a Unicode character. */
858 len += char_to_string(ch, string + len, 40 - len, TRUE);
859 }
860
861 add_to_input_buf(string, len);
862}
863
864 static void
865_OnMouseEvent(
866 int button,
867 int x,
868 int y,
869 int repeated_click,
870 UINT keyFlags)
871{
872 int vim_modifiers = 0x0;
873
874 s_getting_focus = FALSE;
875
876 if (keyFlags & MK_SHIFT)
877 vim_modifiers |= MOUSE_SHIFT;
878 if (keyFlags & MK_CONTROL)
879 vim_modifiers |= MOUSE_CTRL;
880 if (GetKeyState(VK_MENU) & 0x8000)
881 vim_modifiers |= MOUSE_ALT;
882
883 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
884}
885
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100886 static void
887_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100888 HWND hwnd UNUSED,
889 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100890 int x,
891 int y,
892 UINT keyFlags)
893{
894 static LONG s_prevTime = 0;
895
896 LONG currentTime = GetMessageTime();
897 int button = -1;
898 int repeated_click;
899
900 /* Give main window the focus: this is so the cursor isn't hollow. */
901 (void)SetFocus(s_hwnd);
902
903 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
904 button = MOUSE_LEFT;
905 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
906 button = MOUSE_MIDDLE;
907 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
908 button = MOUSE_RIGHT;
909 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
910 {
911#ifndef GET_XBUTTON_WPARAM
912# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
913#endif
914 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
915 }
916 else if (s_uMsg == WM_CAPTURECHANGED)
917 {
918 /* on W95/NT4, somehow you get in here with an odd Msg
919 * if you press one button while holding down the other..*/
920 if (s_button_pending == MOUSE_LEFT)
921 button = MOUSE_RIGHT;
922 else
923 button = MOUSE_LEFT;
924 }
925 if (button >= 0)
926 {
927 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
928
929 /*
930 * Holding down the left and right buttons simulates pushing the middle
931 * button.
932 */
933 if (repeated_click
934 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
935 || (button == MOUSE_RIGHT
936 && s_button_pending == MOUSE_LEFT)))
937 {
938 /*
939 * Hmm, gui.c will ignore more than one button down at a time, so
940 * pretend we let go of it first.
941 */
942 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
943 button = MOUSE_MIDDLE;
944 repeated_click = FALSE;
945 s_button_pending = -1;
946 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
947 }
948 else if ((repeated_click)
949 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
950 {
951 if (s_button_pending > -1)
952 {
953 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
954 s_button_pending = -1;
955 }
956 /* TRACE("Button down at x %d, y %d\n", x, y); */
957 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
958 }
959 else
960 {
961 /*
962 * If this is the first press (i.e. not a multiple click) don't
963 * action immediately, but store and wait for:
964 * i) button-up
965 * ii) mouse move
966 * iii) another button press
967 * before using it.
968 * This enables us to make left+right simulate middle button,
969 * without left or right being actioned first. The side-effect is
970 * that if you click and hold the mouse without dragging, the
971 * cursor doesn't move until you release the button. In practice
972 * this is hardly a problem.
973 */
974 s_button_pending = button;
975 s_x_pending = x;
976 s_y_pending = y;
977 s_kFlags_pending = keyFlags;
978 }
979
980 s_prevTime = currentTime;
981 }
982}
983
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100984 static void
985_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100986 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100987 int x,
988 int y,
989 UINT keyFlags)
990{
991 int button;
992
993 s_getting_focus = FALSE;
994 if (s_button_pending > -1)
995 {
996 /* Delayed action for mouse down event */
997 _OnMouseEvent(s_button_pending, s_x_pending,
998 s_y_pending, FALSE, s_kFlags_pending);
999 s_button_pending = -1;
1000 }
1001 if (s_uMsg == WM_MOUSEMOVE)
1002 {
1003 /*
1004 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1005 * down.
1006 */
1007 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1008 | MK_XBUTTON1 | MK_XBUTTON2)))
1009 {
1010 gui_mouse_moved(x, y);
1011 return;
1012 }
1013
1014 /*
1015 * While button is down, keep grabbing mouse move events when
1016 * the mouse goes outside the window
1017 */
1018 SetCapture(s_textArea);
1019 button = MOUSE_DRAG;
1020 /* TRACE(" move at x %d, y %d\n", x, y); */
1021 }
1022 else
1023 {
1024 ReleaseCapture();
1025 button = MOUSE_RELEASE;
1026 /* TRACE(" up at x %d, y %d\n", x, y); */
1027 }
1028
1029 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1030}
1031
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001032 static void
1033_OnSizeTextArea(
1034 HWND hwnd UNUSED,
1035 UINT state UNUSED,
1036 int cx UNUSED,
1037 int cy UNUSED)
1038{
1039#if defined(FEAT_DIRECTX)
1040 if (IS_ENABLE_DIRECTX())
1041 directx_binddc();
1042#endif
1043}
1044
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001045#ifdef FEAT_MENU
1046/*
1047 * Find the vimmenu_T with the given id
1048 */
1049 static vimmenu_T *
1050gui_mswin_find_menu(
1051 vimmenu_T *pMenu,
1052 int id)
1053{
1054 vimmenu_T *pChildMenu;
1055
1056 while (pMenu)
1057 {
1058 if (pMenu->id == (UINT)id)
1059 break;
1060 if (pMenu->children != NULL)
1061 {
1062 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1063 if (pChildMenu)
1064 {
1065 pMenu = pChildMenu;
1066 break;
1067 }
1068 }
1069 pMenu = pMenu->next;
1070 }
1071 return pMenu;
1072}
1073
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001074 static void
1075_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001076 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001077 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001078 HWND hwndCtl UNUSED,
1079 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001080{
1081 vimmenu_T *pMenu;
1082
1083 pMenu = gui_mswin_find_menu(root_menu, id);
1084 if (pMenu)
1085 gui_menu_cb(pMenu);
1086}
1087#endif
1088
1089#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001090# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001091/*
1092 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1093 */
1094 static void
1095findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1096{
1097 WCHAR *wp;
1098
1099 lpfrw->hwndOwner = lpfr->hwndOwner;
1100 lpfrw->Flags = lpfr->Flags;
1101
1102 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1103 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1104 vim_free(wp);
1105
1106 /* the field "lpstrReplaceWith" doesn't need to be copied */
1107}
1108
1109/*
1110 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1111 */
1112 static void
1113findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1114{
1115 char_u *p;
1116
1117 lpfr->Flags = lpfrw->Flags;
1118
1119 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1120 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1121 vim_free(p);
1122
1123 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1124 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1125 vim_free(p);
1126}
1127# endif
1128
1129/*
1130 * Handle a Find/Replace window message.
1131 */
1132 static void
1133_OnFindRepl(void)
1134{
1135 int flags = 0;
1136 int down;
1137
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001138# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001139 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1140 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001141 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001142 {
1143 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1144 }
1145# endif
1146
1147 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1148 /* Give main window the focus back. */
1149 (void)SetFocus(s_hwnd);
1150
1151 if (s_findrep_struct.Flags & FR_FINDNEXT)
1152 {
1153 flags = FRD_FINDNEXT;
1154
1155 /* Give main window the focus back: this is so the cursor isn't
1156 * hollow. */
1157 (void)SetFocus(s_hwnd);
1158 }
1159 else if (s_findrep_struct.Flags & FR_REPLACE)
1160 {
1161 flags = FRD_REPLACE;
1162
1163 /* Give main window the focus back: this is so the cursor isn't
1164 * hollow. */
1165 (void)SetFocus(s_hwnd);
1166 }
1167 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1168 {
1169 flags = FRD_REPLACEALL;
1170 }
1171
1172 if (flags != 0)
1173 {
1174 /* Call the generic GUI function to do the actual work. */
1175 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1176 flags |= FRD_WHOLE_WORD;
1177 if (s_findrep_struct.Flags & FR_MATCHCASE)
1178 flags |= FRD_MATCH_CASE;
1179 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1180 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1181 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1182 }
1183}
1184#endif
1185
1186 static void
1187HandleMouseHide(UINT uMsg, LPARAM lParam)
1188{
1189 static LPARAM last_lParam = 0L;
1190
1191 /* We sometimes get a mousemove when the mouse didn't move... */
1192 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1193 {
1194 if (lParam == last_lParam)
1195 return;
1196 last_lParam = lParam;
1197 }
1198
1199 /* Handle specially, to centralise coding. We need to be sure we catch all
1200 * possible events which should cause us to restore the cursor (as it is a
1201 * shared resource, we take full responsibility for it).
1202 */
1203 switch (uMsg)
1204 {
1205 case WM_KEYUP:
1206 case WM_CHAR:
1207 /*
1208 * blank out the pointer if necessary
1209 */
1210 if (p_mh)
1211 gui_mch_mousehide(TRUE);
1212 break;
1213
1214 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1215 case WM_SYSCHAR:
1216 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1217 case WM_LBUTTONDOWN:
1218 case WM_LBUTTONUP:
1219 case WM_MBUTTONDOWN:
1220 case WM_MBUTTONUP:
1221 case WM_RBUTTONDOWN:
1222 case WM_RBUTTONUP:
1223 case WM_XBUTTONDOWN:
1224 case WM_XBUTTONUP:
1225 case WM_NCMOUSEMOVE:
1226 case WM_NCLBUTTONDOWN:
1227 case WM_NCLBUTTONUP:
1228 case WM_NCMBUTTONDOWN:
1229 case WM_NCMBUTTONUP:
1230 case WM_NCRBUTTONDOWN:
1231 case WM_NCRBUTTONUP:
1232 case WM_KILLFOCUS:
1233 /*
1234 * if the pointer is currently hidden, then we should show it.
1235 */
1236 gui_mch_mousehide(FALSE);
1237 break;
1238 }
1239}
1240
1241 static LRESULT CALLBACK
1242_TextAreaWndProc(
1243 HWND hwnd,
1244 UINT uMsg,
1245 WPARAM wParam,
1246 LPARAM lParam)
1247{
1248 /*
1249 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1250 hwnd, uMsg, wParam, lParam);
1251 */
1252
1253 HandleMouseHide(uMsg, lParam);
1254
1255 s_uMsg = uMsg;
1256 s_wParam = wParam;
1257 s_lParam = lParam;
1258
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001259#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001260 TrackUserActivity(uMsg);
1261#endif
1262
1263 switch (uMsg)
1264 {
1265 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1266 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1267 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1268 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1269 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1270 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1271 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1272 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1273 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1274 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1275 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1276 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1277 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1278 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001279 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001280
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001281#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001282 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1283 return TRUE;
1284#endif
1285 default:
1286 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1287 }
1288}
1289
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001290#if defined(FEAT_MBYTE) \
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001291 || defined(GLOBAL_IME) \
1292 || defined(PROTO)
1293# ifdef PROTO
1294typedef int WINAPI;
1295# endif
1296
1297 LRESULT WINAPI
1298vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1299{
1300# ifdef GLOBAL_IME
1301 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1302# else
1303 if (wide_WindowProc)
1304 return DefWindowProcW(hwnd, message, wParam, lParam);
1305 return DefWindowProc(hwnd, message, wParam, lParam);
1306#endif
1307}
1308#endif
1309
1310/*
1311 * Called when the foreground or background color has been changed.
1312 */
1313 void
1314gui_mch_new_colors(void)
1315{
1316 /* nothing to do? */
1317}
1318
1319/*
1320 * Set the colors to their default values.
1321 */
1322 void
1323gui_mch_def_colors(void)
1324{
1325 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1326 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1327 gui.def_norm_pixel = gui.norm_pixel;
1328 gui.def_back_pixel = gui.back_pixel;
1329}
1330
1331/*
1332 * Open the GUI window which was created by a call to gui_mch_init().
1333 */
1334 int
1335gui_mch_open(void)
1336{
1337#ifndef SW_SHOWDEFAULT
1338# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1339#endif
1340 /* Actually open the window, if not already visible
1341 * (may be done already in gui_mch_set_shellsize) */
1342 if (!IsWindowVisible(s_hwnd))
1343 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1344
1345#ifdef MSWIN_FIND_REPLACE
1346 /* Init replace string here, so that we keep it when re-opening the
1347 * dialog. */
1348 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1349#endif
1350
1351 return OK;
1352}
1353
1354/*
1355 * Get the position of the top left corner of the window.
1356 */
1357 int
1358gui_mch_get_winpos(int *x, int *y)
1359{
1360 RECT rect;
1361
1362 GetWindowRect(s_hwnd, &rect);
1363 *x = rect.left;
1364 *y = rect.top;
1365 return OK;
1366}
1367
1368/*
1369 * Set the position of the top left corner of the window to the given
1370 * coordinates.
1371 */
1372 void
1373gui_mch_set_winpos(int x, int y)
1374{
1375 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1376 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1377}
1378 void
1379gui_mch_set_text_area_pos(int x, int y, int w, int h)
1380{
1381 static int oldx = 0;
1382 static int oldy = 0;
1383
1384 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1385
1386#ifdef FEAT_TOOLBAR
1387 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1388 SendMessage(s_toolbarhwnd, WM_SIZE,
1389 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1390#endif
1391#if defined(FEAT_GUI_TABLINE)
1392 if (showing_tabline)
1393 {
1394 int top = 0;
1395 RECT rect;
1396
1397# ifdef FEAT_TOOLBAR
1398 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1399 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1400# endif
1401 GetClientRect(s_hwnd, &rect);
1402 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1403 }
1404#endif
1405
1406 /* When side scroll bar is unshown, the size of window will change.
1407 * then, the text area move left or right. thus client rect should be
1408 * forcedly redrawn. (Yasuhiro Matsumoto) */
1409 if (oldx != x || oldy != y)
1410 {
1411 InvalidateRect(s_hwnd, NULL, FALSE);
1412 oldx = x;
1413 oldy = y;
1414 }
1415}
1416
1417
1418/*
1419 * Scrollbar stuff:
1420 */
1421
1422 void
1423gui_mch_enable_scrollbar(
1424 scrollbar_T *sb,
1425 int flag)
1426{
1427 ShowScrollBar(sb->id, SB_CTL, flag);
1428
1429 /* TODO: When the window is maximized, the size of the window stays the
1430 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1431 * NT 4.0 it's not... */
1432}
1433
1434 void
1435gui_mch_set_scrollbar_pos(
1436 scrollbar_T *sb,
1437 int x,
1438 int y,
1439 int w,
1440 int h)
1441{
1442 SetWindowPos(sb->id, NULL, x, y, w, h,
1443 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1444}
1445
1446 void
1447gui_mch_create_scrollbar(
1448 scrollbar_T *sb,
1449 int orient) /* SBAR_VERT or SBAR_HORIZ */
1450{
1451 sb->id = CreateWindow(
1452 "SCROLLBAR", "Scrollbar",
1453 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1454 10, /* Any value will do for now */
1455 10, /* Any value will do for now */
1456 s_hwnd, NULL,
1457 s_hinst, NULL);
1458}
1459
1460/*
1461 * Find the scrollbar with the given hwnd.
1462 */
1463 static scrollbar_T *
1464gui_mswin_find_scrollbar(HWND hwnd)
1465{
1466 win_T *wp;
1467
1468 if (gui.bottom_sbar.id == hwnd)
1469 return &gui.bottom_sbar;
1470 FOR_ALL_WINDOWS(wp)
1471 {
1472 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1473 return &wp->w_scrollbars[SBAR_LEFT];
1474 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1475 return &wp->w_scrollbars[SBAR_RIGHT];
1476 }
1477 return NULL;
1478}
1479
1480/*
1481 * Get the character size of a font.
1482 */
1483 static void
1484GetFontSize(GuiFont font)
1485{
1486 HWND hwnd = GetDesktopWindow();
1487 HDC hdc = GetWindowDC(hwnd);
1488 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1489 TEXTMETRIC tm;
1490
1491 GetTextMetrics(hdc, &tm);
1492 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1493
1494 gui.char_height = tm.tmHeight + p_linespace;
1495
1496 SelectFont(hdc, hfntOld);
1497
1498 ReleaseDC(hwnd, hdc);
1499}
1500
1501/*
1502 * Adjust gui.char_height (after 'linespace' was changed).
1503 */
1504 int
1505gui_mch_adjust_charheight(void)
1506{
1507 GetFontSize(gui.norm_font);
1508 return OK;
1509}
1510
1511 static GuiFont
1512get_font_handle(LOGFONT *lf)
1513{
1514 HFONT font = NULL;
1515
1516 /* Load the font */
1517 font = CreateFontIndirect(lf);
1518
1519 if (font == NULL)
1520 return NOFONT;
1521
1522 return (GuiFont)font;
1523}
1524
1525 static int
1526pixels_to_points(int pixels, int vertical)
1527{
1528 int points;
1529 HWND hwnd;
1530 HDC hdc;
1531
1532 hwnd = GetDesktopWindow();
1533 hdc = GetWindowDC(hwnd);
1534
1535 points = MulDiv(pixels, 72,
1536 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1537
1538 ReleaseDC(hwnd, hdc);
1539
1540 return points;
1541}
1542
1543 GuiFont
1544gui_mch_get_font(
1545 char_u *name,
1546 int giveErrorIfMissing)
1547{
1548 LOGFONT lf;
1549 GuiFont font = NOFONT;
1550
1551 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1552 font = get_font_handle(&lf);
1553 if (font == NOFONT && giveErrorIfMissing)
1554 EMSG2(_(e_font), name);
1555 return font;
1556}
1557
1558#if defined(FEAT_EVAL) || defined(PROTO)
1559/*
1560 * Return the name of font "font" in allocated memory.
1561 * Don't know how to get the actual name, thus use the provided name.
1562 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001563 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001564gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001565{
1566 if (name == NULL)
1567 return NULL;
1568 return vim_strsave(name);
1569}
1570#endif
1571
1572 void
1573gui_mch_free_font(GuiFont font)
1574{
1575 if (font)
1576 DeleteObject((HFONT)font);
1577}
1578
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001579/*
1580 * Return the Pixel value (color) for the given color name.
1581 * Return INVALCOLOR for error.
1582 */
1583 guicolor_T
1584gui_mch_get_color(char_u *name)
1585{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001586 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587
1588 typedef struct SysColorTable
1589 {
1590 char *name;
1591 int color;
1592 } SysColorTable;
1593
1594 static SysColorTable sys_table[] =
1595 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1597 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001598#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1600#endif
1601 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1602 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1603 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1604 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1605 {"SYS_DESKTOP", COLOR_DESKTOP},
1606 {"SYS_INFOBK", COLOR_INFOBK},
1607 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1608 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001609 {"SYS_BTNFACE", COLOR_BTNFACE},
1610 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1611 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1612 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1613 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1614 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1615 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1616 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1617 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1618 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1619 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1620 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1621 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1622 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1623 {"SYS_MENU", COLOR_MENU},
1624 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1625 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1626 {"SYS_WINDOW", COLOR_WINDOW},
1627 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1628 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1629 };
1630
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001631 /*
1632 * Try to look up a system colour.
1633 */
1634 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1635 if (STRICMP(name, sys_table[i].name) == 0)
1636 return GetSysColor(sys_table[i].color);
1637
Bram Moolenaarab302212016-04-26 20:59:29 +02001638 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001639}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001640
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001641 guicolor_T
1642gui_mch_get_rgb_color(int r, int g, int b)
1643{
1644 return gui_get_rgb_color_cmn(r, g, b);
1645}
1646
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001647/*
1648 * Return OK if the key with the termcap name "name" is supported.
1649 */
1650 int
1651gui_mch_haskey(char_u *name)
1652{
1653 int i;
1654
1655 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1656 if (name[0] == special_keys[i].vim_code0 &&
1657 name[1] == special_keys[i].vim_code1)
1658 return OK;
1659 return FAIL;
1660}
1661
1662 void
1663gui_mch_beep(void)
1664{
1665 MessageBeep(MB_OK);
1666}
1667/*
1668 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1669 */
1670 void
1671gui_mch_invert_rectangle(
1672 int r,
1673 int c,
1674 int nr,
1675 int nc)
1676{
1677 RECT rc;
1678
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001679#if defined(FEAT_DIRECTX)
1680 if (IS_ENABLE_DIRECTX())
1681 DWriteContext_Flush(s_dwc);
1682#endif
1683
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001684 /*
1685 * Note: InvertRect() excludes right and bottom of rectangle.
1686 */
1687 rc.left = FILL_X(c);
1688 rc.top = FILL_Y(r);
1689 rc.right = rc.left + nc * gui.char_width;
1690 rc.bottom = rc.top + nr * gui.char_height;
1691 InvertRect(s_hdc, &rc);
1692}
1693
1694/*
1695 * Iconify the GUI window.
1696 */
1697 void
1698gui_mch_iconify(void)
1699{
1700 ShowWindow(s_hwnd, SW_MINIMIZE);
1701}
1702
1703/*
1704 * Draw a cursor without focus.
1705 */
1706 void
1707gui_mch_draw_hollow_cursor(guicolor_T color)
1708{
1709 HBRUSH hbr;
1710 RECT rc;
1711
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001712#if defined(FEAT_DIRECTX)
1713 if (IS_ENABLE_DIRECTX())
1714 DWriteContext_Flush(s_dwc);
1715#endif
1716
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001717 /*
1718 * Note: FrameRect() excludes right and bottom of rectangle.
1719 */
1720 rc.left = FILL_X(gui.col);
1721 rc.top = FILL_Y(gui.row);
1722 rc.right = rc.left + gui.char_width;
1723#ifdef FEAT_MBYTE
1724 if (mb_lefthalve(gui.row, gui.col))
1725 rc.right += gui.char_width;
1726#endif
1727 rc.bottom = rc.top + gui.char_height;
1728 hbr = CreateSolidBrush(color);
1729 FrameRect(s_hdc, &rc, hbr);
1730 DeleteBrush(hbr);
1731}
1732/*
1733 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1734 * color "color".
1735 */
1736 void
1737gui_mch_draw_part_cursor(
1738 int w,
1739 int h,
1740 guicolor_T color)
1741{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001742 RECT rc;
1743
1744 /*
1745 * Note: FillRect() excludes right and bottom of rectangle.
1746 */
1747 rc.left =
1748#ifdef FEAT_RIGHTLEFT
1749 /* vertical line should be on the right of current point */
1750 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1751#endif
1752 FILL_X(gui.col);
1753 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1754 rc.right = rc.left + w;
1755 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001756
Bram Moolenaar92467d32017-12-05 13:22:16 +01001757 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001758}
1759
1760
1761/*
1762 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1763 * dead key's nominal character and re-post the original message.
1764 */
1765 static void
1766outputDeadKey_rePost(MSG originalMsg)
1767{
1768 static MSG deadCharExpel;
1769
1770 if (!dead_key)
1771 return;
1772
1773 dead_key = 0;
1774
1775 /* Make Windows generate the dead key's character */
1776 deadCharExpel.message = originalMsg.message;
1777 deadCharExpel.hwnd = originalMsg.hwnd;
1778 deadCharExpel.wParam = VK_SPACE;
1779
1780 MyTranslateMessage(&deadCharExpel);
1781
1782 /* re-generate the current character free of the dead char influence */
1783 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1784 originalMsg.lParam);
1785}
1786
1787
1788/*
1789 * Process a single Windows message.
1790 * If one is not available we hang until one is.
1791 */
1792 static void
1793process_message(void)
1794{
1795 MSG msg;
1796 UINT vk = 0; /* Virtual key */
1797 char_u string[40];
1798 int i;
1799 int modifiers = 0;
1800 int key;
1801#ifdef FEAT_MENU
1802 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1803#endif
1804
1805 pGetMessage(&msg, NULL, 0, 0);
1806
1807#ifdef FEAT_OLE
1808 /* Look after OLE Automation commands */
1809 if (msg.message == WM_OLE)
1810 {
1811 char_u *str = (char_u *)msg.lParam;
1812 if (str == NULL || *str == NUL)
1813 {
1814 /* Message can't be ours, forward it. Fixes problem with Ultramon
1815 * 3.0.4 */
1816 pDispatchMessage(&msg);
1817 }
1818 else
1819 {
1820 add_to_input_buf(str, (int)STRLEN(str));
1821 vim_free(str); /* was allocated in CVim::SendKeys() */
1822 }
1823 return;
1824 }
1825#endif
1826
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001827#ifdef MSWIN_FIND_REPLACE
1828 /* Don't process messages used by the dialog */
1829 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1830 {
1831 HandleMouseHide(msg.message, msg.lParam);
1832 return;
1833 }
1834#endif
1835
1836 /*
1837 * Check if it's a special key that we recognise. If not, call
1838 * TranslateMessage().
1839 */
1840 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1841 {
1842 vk = (int) msg.wParam;
1843
1844 /*
1845 * Handle dead keys in special conditions in other cases we let Windows
1846 * handle them and do not interfere.
1847 *
1848 * The dead_key flag must be reset on several occasions:
1849 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1850 * consumed at that point (This is when we let Windows combine the
1851 * dead character on its own)
1852 *
1853 * - Before doing something special such as regenerating keypresses to
1854 * expel the dead character as this could trigger an infinite loop if
1855 * for some reason MyTranslateMessage() do not trigger a call
1856 * immediately to _OnChar() (or _OnSysChar()).
1857 */
1858 if (dead_key)
1859 {
1860 /*
1861 * If a dead key was pressed and the user presses VK_SPACE,
1862 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1863 * with the dead char now, so do nothing special and let Windows
1864 * handle it.
1865 *
1866 * Note that VK_SPACE combines with the dead_key's character and
1867 * only one WM_CHAR will be generated by TranslateMessage(), in
1868 * the two other cases two WM_CHAR will be generated: the dead
1869 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1870 * user expects.
1871 */
1872 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1873 {
1874 dead_key = 0;
1875 MyTranslateMessage(&msg);
1876 return;
1877 }
1878 /* In modes where we are not typing, dead keys should behave
1879 * normally */
1880 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1881 {
1882 outputDeadKey_rePost(msg);
1883 return;
1884 }
1885 }
1886
1887 /* Check for CTRL-BREAK */
1888 if (vk == VK_CANCEL)
1889 {
1890 trash_input_buf();
1891 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001892 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001893 string[0] = Ctrl_C;
1894 add_to_input_buf(string, 1);
1895 }
1896
1897 for (i = 0; special_keys[i].key_sym != 0; i++)
1898 {
1899 /* ignore VK_SPACE when ALT key pressed: system menu */
1900 if (special_keys[i].key_sym == vk
1901 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1902 {
1903 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001904 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001905 * is a key that would normally trigger the dead key nominal
1906 * character output (such as a NUMPAD printable character or
1907 * the TAB key, etc...).
1908 */
1909 if (dead_key && (special_keys[i].vim_code0 == 'K'
1910 || vk == VK_TAB || vk == CAR))
1911 {
1912 outputDeadKey_rePost(msg);
1913 return;
1914 }
1915
1916#ifdef FEAT_MENU
1917 /* Check for <F10>: Windows selects the menu. When <F10> is
1918 * mapped we want to use the mapping instead. */
1919 if (vk == VK_F10
1920 && gui.menu_is_active
1921 && check_map(k10, State, FALSE, TRUE, FALSE,
1922 NULL, NULL) == NULL)
1923 break;
1924#endif
1925 if (GetKeyState(VK_SHIFT) & 0x8000)
1926 modifiers |= MOD_MASK_SHIFT;
1927 /*
1928 * Don't use caps-lock as shift, because these are special keys
1929 * being considered here, and we only want letters to get
1930 * shifted -- webb
1931 */
1932 /*
1933 if (GetKeyState(VK_CAPITAL) & 0x0001)
1934 modifiers ^= MOD_MASK_SHIFT;
1935 */
1936 if (GetKeyState(VK_CONTROL) & 0x8000)
1937 modifiers |= MOD_MASK_CTRL;
1938 if (GetKeyState(VK_MENU) & 0x8000)
1939 modifiers |= MOD_MASK_ALT;
1940
1941 if (special_keys[i].vim_code1 == NUL)
1942 key = special_keys[i].vim_code0;
1943 else
1944 key = TO_SPECIAL(special_keys[i].vim_code0,
1945 special_keys[i].vim_code1);
1946 key = simplify_key(key, &modifiers);
1947 if (key == CSI)
1948 key = K_CSI;
1949
1950 if (modifiers)
1951 {
1952 string[0] = CSI;
1953 string[1] = KS_MODIFIER;
1954 string[2] = modifiers;
1955 add_to_input_buf(string, 3);
1956 }
1957
1958 if (IS_SPECIAL(key))
1959 {
1960 string[0] = CSI;
1961 string[1] = K_SECOND(key);
1962 string[2] = K_THIRD(key);
1963 add_to_input_buf(string, 3);
1964 }
1965 else
1966 {
1967 int len;
1968
1969 /* Handle "key" as a Unicode character. */
1970 len = char_to_string(key, string, 40, FALSE);
1971 add_to_input_buf(string, len);
1972 }
1973 break;
1974 }
1975 }
1976 if (special_keys[i].key_sym == 0)
1977 {
1978 /* Some keys need C-S- where they should only need C-.
1979 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1980 * system startup (Helmut Stiegler, 2003 Oct 3). */
1981 if (vk != 0xff
1982 && (GetKeyState(VK_CONTROL) & 0x8000)
1983 && !(GetKeyState(VK_SHIFT) & 0x8000)
1984 && !(GetKeyState(VK_MENU) & 0x8000))
1985 {
1986 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1987 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1988 {
1989 string[0] = Ctrl_HAT;
1990 add_to_input_buf(string, 1);
1991 }
1992 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1993 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1994 {
1995 string[0] = Ctrl__;
1996 add_to_input_buf(string, 1);
1997 }
1998 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1999 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2000 {
2001 string[0] = Ctrl_AT;
2002 add_to_input_buf(string, 1);
2003 }
2004 else
2005 MyTranslateMessage(&msg);
2006 }
2007 else
2008 MyTranslateMessage(&msg);
2009 }
2010 }
2011#ifdef FEAT_MBYTE_IME
2012 else if (msg.message == WM_IME_NOTIFY)
2013 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2014 else if (msg.message == WM_KEYUP && im_get_status())
2015 /* added for non-MS IME (Yasuhiro Matsumoto) */
2016 MyTranslateMessage(&msg);
2017#endif
2018#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2019/* GIME_TEST */
2020 else if (msg.message == WM_IME_STARTCOMPOSITION)
2021 {
2022 POINT point;
2023
2024 global_ime_set_font(&norm_logfont);
2025 point.x = FILL_X(gui.col);
2026 point.y = FILL_Y(gui.row);
2027 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2028 global_ime_set_position(&point);
2029 }
2030#endif
2031
2032#ifdef FEAT_MENU
2033 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2034 * mapped we need to stop it here to avoid strange effects (e.g., for the
2035 * key-up event) */
2036 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2037 NULL, NULL) == NULL)
2038#endif
2039 pDispatchMessage(&msg);
2040}
2041
2042/*
2043 * Catch up with any queued events. This may put keyboard input into the
2044 * input buffer, call resize call-backs, trigger timers etc. If there is
2045 * nothing in the event queue (& no timers pending), then we return
2046 * immediately.
2047 */
2048 void
2049gui_mch_update(void)
2050{
2051 MSG msg;
2052
2053 if (!s_busy_processing)
2054 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2055 && !vim_is_input_buf_full())
2056 process_message();
2057}
2058
Bram Moolenaar4231da42016-06-02 14:30:04 +02002059 static void
2060remove_any_timer(void)
2061{
2062 MSG msg;
2063
2064 if (s_wait_timer != 0 && !s_timed_out)
2065 {
2066 KillTimer(NULL, s_wait_timer);
2067
2068 /* Eat spurious WM_TIMER messages */
2069 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2070 ;
2071 s_wait_timer = 0;
2072 }
2073}
2074
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002075/*
2076 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2077 * from the keyboard.
2078 * wtime == -1 Wait forever.
2079 * wtime == 0 This should never happen.
2080 * wtime > 0 Wait wtime milliseconds for a character.
2081 * Returns OK if a character was found to be available within the given time,
2082 * or FAIL otherwise.
2083 */
2084 int
2085gui_mch_wait_for_chars(int wtime)
2086{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 int focus;
2088
2089 s_timed_out = FALSE;
2090
2091 if (wtime > 0)
2092 {
2093 /* Don't do anything while processing a (scroll) message. */
2094 if (s_busy_processing)
2095 return FAIL;
2096 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2097 (TIMERPROC)_OnTimer);
2098 }
2099
2100 allow_scrollbar = TRUE;
2101
2102 focus = gui.in_focus;
2103 while (!s_timed_out)
2104 {
2105 /* Stop or start blinking when focus changes */
2106 if (gui.in_focus != focus)
2107 {
2108 if (gui.in_focus)
2109 gui_mch_start_blink();
2110 else
2111 gui_mch_stop_blink();
2112 focus = gui.in_focus;
2113 }
2114
2115 if (s_need_activate)
2116 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002117 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002118 s_need_activate = FALSE;
2119 }
2120
Bram Moolenaar4231da42016-06-02 14:30:04 +02002121#ifdef FEAT_TIMERS
2122 did_add_timer = FALSE;
2123#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002125 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002126 for (;;)
2127 {
2128 MSG msg;
2129
2130 parse_queued_messages();
2131
Bram Moolenaar62426e12017-08-13 15:37:58 +02002132 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2133 {
2134 process_message();
2135 break;
2136 }
2137 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2138 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002139 break;
2140 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002141#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002142 /*
2143 * Don't use gui_mch_update() because then we will spin-lock until a
2144 * char arrives, instead we use GetMessage() to hang until an
2145 * event arrives. No need to check for input_buf_full because we are
2146 * returning as soon as it contains a single char -- webb
2147 */
2148 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002149#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002150
2151 if (input_available())
2152 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002153 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002154 allow_scrollbar = FALSE;
2155
2156 /* Clear pending mouse button, the release event may have been
2157 * taken by the dialog window. But don't do this when getting
2158 * focus, we need the mouse-up event then. */
2159 if (!s_getting_focus)
2160 s_button_pending = -1;
2161
2162 return OK;
2163 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002164
2165#ifdef FEAT_TIMERS
2166 if (did_add_timer)
2167 {
2168 /* Need to recompute the waiting time. */
2169 remove_any_timer();
2170 break;
2171 }
2172#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002173 }
2174 allow_scrollbar = FALSE;
2175 return FAIL;
2176}
2177
2178/*
2179 * Clear a rectangular region of the screen from text pos (row1, col1) to
2180 * (row2, col2) inclusive.
2181 */
2182 void
2183gui_mch_clear_block(
2184 int row1,
2185 int col1,
2186 int row2,
2187 int col2)
2188{
2189 RECT rc;
2190
2191 /*
2192 * Clear one extra pixel at the far right, for when bold characters have
2193 * spilled over to the window border.
2194 * Note: FillRect() excludes right and bottom of rectangle.
2195 */
2196 rc.left = FILL_X(col1);
2197 rc.top = FILL_Y(row1);
2198 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2199 rc.bottom = FILL_Y(row2 + 1);
2200 clear_rect(&rc);
2201}
2202
2203/*
2204 * Clear the whole text window.
2205 */
2206 void
2207gui_mch_clear_all(void)
2208{
2209 RECT rc;
2210
2211 rc.left = 0;
2212 rc.top = 0;
2213 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2214 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2215 clear_rect(&rc);
2216}
2217/*
2218 * Menu stuff.
2219 */
2220
2221 void
2222gui_mch_enable_menu(int flag)
2223{
2224#ifdef FEAT_MENU
2225 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2226#endif
2227}
2228
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002229 void
2230gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002231 int x UNUSED,
2232 int y UNUSED,
2233 int w UNUSED,
2234 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002235{
2236 /* It will be in the right place anyway */
2237}
2238
2239#if defined(FEAT_MENU) || defined(PROTO)
2240/*
2241 * Make menu item hidden or not hidden
2242 */
2243 void
2244gui_mch_menu_hidden(
2245 vimmenu_T *menu,
2246 int hidden)
2247{
2248 /*
2249 * This doesn't do what we want. Hmm, just grey the menu items for now.
2250 */
2251 /*
2252 if (hidden)
2253 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2254 else
2255 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2256 */
2257 gui_mch_menu_grey(menu, hidden);
2258}
2259
2260/*
2261 * This is called after setting all the menus to grey/hidden or not.
2262 */
2263 void
2264gui_mch_draw_menubar(void)
2265{
2266 DrawMenuBar(s_hwnd);
2267}
2268#endif /*FEAT_MENU*/
2269
2270#ifndef PROTO
2271void
2272#ifdef VIMDLL
2273_export
2274#endif
2275_cdecl
2276SaveInst(HINSTANCE hInst)
2277{
2278 s_hinst = hInst;
2279}
2280#endif
2281
2282/*
2283 * Return the RGB value of a pixel as a long.
2284 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002285 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002286gui_mch_get_rgb(guicolor_T pixel)
2287{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002288 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2289 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002290}
2291
2292#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2293/* Convert pixels in X to dialog units */
2294 static WORD
2295PixelToDialogX(int numPixels)
2296{
2297 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2298}
2299
2300/* Convert pixels in Y to dialog units */
2301 static WORD
2302PixelToDialogY(int numPixels)
2303{
2304 return (WORD)((numPixels * 8) / s_dlgfntheight);
2305}
2306
2307/* Return the width in pixels of the given text in the given DC. */
2308 static int
2309GetTextWidth(HDC hdc, char_u *str, int len)
2310{
2311 SIZE size;
2312
2313 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2314 return size.cx;
2315}
2316
2317#ifdef FEAT_MBYTE
2318/*
2319 * Return the width in pixels of the given text in the given DC, taking care
2320 * of 'encoding' to active codepage conversion.
2321 */
2322 static int
2323GetTextWidthEnc(HDC hdc, char_u *str, int len)
2324{
2325 SIZE size;
2326 WCHAR *wstr;
2327 int n;
2328 int wlen = len;
2329
2330 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2331 {
2332 /* 'encoding' differs from active codepage: convert text and use wide
2333 * function */
2334 wstr = enc_to_utf16(str, &wlen);
2335 if (wstr != NULL)
2336 {
2337 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2338 vim_free(wstr);
2339 if (n)
2340 return size.cx;
2341 }
2342 }
2343
2344 return GetTextWidth(hdc, str, len);
2345}
2346#else
2347# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2348#endif
2349
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002350static void get_work_area(RECT *spi_rect);
2351
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352/*
2353 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002354 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2355 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002356 */
2357 static BOOL
2358CenterWindow(
2359 HWND hwndChild,
2360 HWND hwndParent)
2361{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002362 HMONITOR mon;
2363 MONITORINFO moninfo;
2364 RECT rChild, rParent, rScreen;
2365 int wChild, hChild, wParent, hParent;
2366 int xNew, yNew;
2367 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002368
2369 GetWindowRect(hwndChild, &rChild);
2370 wChild = rChild.right - rChild.left;
2371 hChild = rChild.bottom - rChild.top;
2372
2373 /* If Vim is minimized put the window in the middle of the screen. */
2374 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002375 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002376 else
2377 GetWindowRect(hwndParent, &rParent);
2378 wParent = rParent.right - rParent.left;
2379 hParent = rParent.bottom - rParent.top;
2380
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002381 moninfo.cbSize = sizeof(MONITORINFO);
2382 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2383 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002385 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002386 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002387 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002388 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002389 hdc = GetDC(hwndChild);
2390 rScreen.left = 0;
2391 rScreen.top = 0;
2392 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2393 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2394 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002395 }
2396
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002397 xNew = rParent.left + ((wParent - wChild) / 2);
2398 if (xNew < rScreen.left)
2399 xNew = rScreen.left;
2400 else if ((xNew + wChild) > rScreen.right)
2401 xNew = rScreen.right - wChild;
2402
2403 yNew = rParent.top + ((hParent - hChild) / 2);
2404 if (yNew < rScreen.top)
2405 yNew = rScreen.top;
2406 else if ((yNew + hChild) > rScreen.bottom)
2407 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408
2409 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2410 SWP_NOSIZE | SWP_NOZORDER);
2411}
2412#endif /* FEAT_GUI_DIALOG */
2413
2414void
2415gui_mch_activate_window(void)
2416{
2417 (void)SetActiveWindow(s_hwnd);
2418}
2419
2420#if defined(FEAT_TOOLBAR) || defined(PROTO)
2421 void
2422gui_mch_show_toolbar(int showit)
2423{
2424 if (s_toolbarhwnd == NULL)
2425 return;
2426
2427 if (showit)
2428 {
2429# ifdef FEAT_MBYTE
2430# ifndef TB_SETUNICODEFORMAT
2431 /* For older compilers. We assume this never changes. */
2432# define TB_SETUNICODEFORMAT 0x2005
2433# endif
2434 /* Enable/disable unicode support */
2435 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2436 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2437# endif
2438 ShowWindow(s_toolbarhwnd, SW_SHOW);
2439 }
2440 else
2441 ShowWindow(s_toolbarhwnd, SW_HIDE);
2442}
2443
2444/* Then number of bitmaps is fixed. Exit is missing! */
2445#define TOOLBAR_BITMAP_COUNT 31
2446
2447#endif
2448
2449#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2450 static void
2451add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2452{
2453#ifdef FEAT_MBYTE
2454 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455
2456 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2457 {
2458 /* 'encoding' differs from active codepage: convert menu name
2459 * and use wide function */
2460 wn = enc_to_utf16(item_text, NULL);
2461 if (wn != NULL)
2462 {
2463 MENUITEMINFOW infow;
2464
2465 infow.cbSize = sizeof(infow);
2466 infow.fMask = MIIM_TYPE | MIIM_ID;
2467 infow.wID = item_id;
2468 infow.fType = MFT_STRING;
2469 infow.dwTypeData = wn;
2470 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002471 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002472 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002473 }
2474 }
2475
2476 if (wn == NULL)
2477#endif
2478 {
2479 MENUITEMINFO info;
2480
2481 info.cbSize = sizeof(info);
2482 info.fMask = MIIM_TYPE | MIIM_ID;
2483 info.wID = item_id;
2484 info.fType = MFT_STRING;
2485 info.dwTypeData = (LPTSTR)item_text;
2486 info.cch = (UINT)STRLEN(item_text);
2487 InsertMenuItem(pmenu, item_id, FALSE, &info);
2488 }
2489}
2490
2491 static void
2492show_tabline_popup_menu(void)
2493{
2494 HMENU tab_pmenu;
2495 long rval;
2496 POINT pt;
2497
2498 /* When ignoring events don't show the menu. */
2499 if (hold_gui_events
2500# ifdef FEAT_CMDWIN
2501 || cmdwin_type != 0
2502# endif
2503 )
2504 return;
2505
2506 tab_pmenu = CreatePopupMenu();
2507 if (tab_pmenu == NULL)
2508 return;
2509
2510 if (first_tabpage->tp_next != NULL)
2511 add_tabline_popup_menu_entry(tab_pmenu,
2512 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2513 add_tabline_popup_menu_entry(tab_pmenu,
2514 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2515 add_tabline_popup_menu_entry(tab_pmenu,
2516 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2517
2518 GetCursorPos(&pt);
2519 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2520 NULL);
2521
2522 DestroyMenu(tab_pmenu);
2523
2524 /* Add the string cmd into input buffer */
2525 if (rval > 0)
2526 {
2527 TCHITTESTINFO htinfo;
2528 int idx;
2529
2530 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2531 return;
2532
2533 htinfo.pt.x = pt.x;
2534 htinfo.pt.y = pt.y;
2535 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2536 if (idx == -1)
2537 idx = 0;
2538 else
2539 idx += 1;
2540
2541 send_tabline_menu_event(idx, (int)rval);
2542 }
2543}
2544
2545/*
2546 * Show or hide the tabline.
2547 */
2548 void
2549gui_mch_show_tabline(int showit)
2550{
2551 if (s_tabhwnd == NULL)
2552 return;
2553
2554 if (!showit != !showing_tabline)
2555 {
2556 if (showit)
2557 ShowWindow(s_tabhwnd, SW_SHOW);
2558 else
2559 ShowWindow(s_tabhwnd, SW_HIDE);
2560 showing_tabline = showit;
2561 }
2562}
2563
2564/*
2565 * Return TRUE when tabline is displayed.
2566 */
2567 int
2568gui_mch_showing_tabline(void)
2569{
2570 return s_tabhwnd != NULL && showing_tabline;
2571}
2572
2573/*
2574 * Update the labels of the tabline.
2575 */
2576 void
2577gui_mch_update_tabline(void)
2578{
2579 tabpage_T *tp;
2580 TCITEM tie;
2581 int nr = 0;
2582 int curtabidx = 0;
2583 int tabadded = 0;
2584#ifdef FEAT_MBYTE
2585 static int use_unicode = FALSE;
2586 int uu;
2587 WCHAR *wstr = NULL;
2588#endif
2589
2590 if (s_tabhwnd == NULL)
2591 return;
2592
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002593#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594# ifndef CCM_SETUNICODEFORMAT
2595 /* For older compilers. We assume this never changes. */
2596# define CCM_SETUNICODEFORMAT 0x2005
2597# endif
2598 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2599 if (uu != use_unicode)
2600 {
2601 /* Enable/disable unicode support */
2602 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2603 use_unicode = uu;
2604 }
2605#endif
2606
2607 tie.mask = TCIF_TEXT;
2608 tie.iImage = -1;
2609
2610 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2611 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2612
2613 /* Add a label for each tab page. They all contain the same text area. */
2614 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2615 {
2616 if (tp == curtab)
2617 curtabidx = nr;
2618
2619 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2620 {
2621 /* Add the tab */
2622 tie.pszText = "-Empty-";
2623 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2624 tabadded = 1;
2625 }
2626
2627 get_tabline_label(tp, FALSE);
2628 tie.pszText = (LPSTR)NameBuff;
2629#ifdef FEAT_MBYTE
2630 wstr = NULL;
2631 if (use_unicode)
2632 {
2633 /* Need to go through Unicode. */
2634 wstr = enc_to_utf16(NameBuff, NULL);
2635 if (wstr != NULL)
2636 {
2637 TCITEMW tiw;
2638
2639 tiw.mask = TCIF_TEXT;
2640 tiw.iImage = -1;
2641 tiw.pszText = wstr;
2642 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2643 vim_free(wstr);
2644 }
2645 }
2646 if (wstr == NULL)
2647#endif
2648 {
2649 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2650 }
2651 }
2652
2653 /* Remove any old labels. */
2654 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2655 TabCtrl_DeleteItem(s_tabhwnd, nr);
2656
2657 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2658 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2659
2660 /* Re-enable redraw and redraw. */
2661 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2662 RedrawWindow(s_tabhwnd, NULL, NULL,
2663 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2664
2665 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2666 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2667}
2668
2669/*
2670 * Set the current tab to "nr". First tab is 1.
2671 */
2672 void
2673gui_mch_set_curtab(int nr)
2674{
2675 if (s_tabhwnd == NULL)
2676 return;
2677
2678 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2679 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2680}
2681
2682#endif
2683
2684/*
2685 * ":simalt" command.
2686 */
2687 void
2688ex_simalt(exarg_T *eap)
2689{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002690 char_u *keys = eap->arg;
2691 int fill_typebuf = FALSE;
2692 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693
2694 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2695 while (*keys)
2696 {
2697 if (*keys == '~')
2698 *keys = ' '; /* for showing system menu */
2699 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2700 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002701 fill_typebuf = TRUE;
2702 }
2703 if (fill_typebuf)
2704 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002705 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002706 * processed. */
2707 key_name[0] = K_SPECIAL;
2708 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002709 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002710 key_name[3] = NUL;
2711 typebuf_was_filled = TRUE;
2712 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002713 }
2714}
2715
2716/*
2717 * Create the find & replace dialogs.
2718 * You can't have both at once: ":find" when replace is showing, destroys
2719 * the replace dialog first, and the other way around.
2720 */
2721#ifdef MSWIN_FIND_REPLACE
2722 static void
2723initialise_findrep(char_u *initial_string)
2724{
2725 int wword = FALSE;
2726 int mcase = !p_ic;
2727 char_u *entry_text;
2728
2729 /* Get the search string to use. */
2730 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2731
2732 s_findrep_struct.hwndOwner = s_hwnd;
2733 s_findrep_struct.Flags = FR_DOWN;
2734 if (mcase)
2735 s_findrep_struct.Flags |= FR_MATCHCASE;
2736 if (wword)
2737 s_findrep_struct.Flags |= FR_WHOLEWORD;
2738 if (entry_text != NULL && *entry_text != NUL)
2739 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2740 s_findrep_struct.wFindWhatLen - 1);
2741 vim_free(entry_text);
2742}
2743#endif
2744
2745 static void
2746set_window_title(HWND hwnd, char *title)
2747{
2748#ifdef FEAT_MBYTE
2749 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2750 {
2751 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002752
2753 /* Convert the title from 'encoding' to UTF-16. */
2754 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2755 if (wbuf != NULL)
2756 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002757 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002758 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002759 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002760 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 }
2762#endif
2763 (void)SetWindowText(hwnd, (LPCSTR)title);
2764}
2765
2766 void
2767gui_mch_find_dialog(exarg_T *eap)
2768{
2769#ifdef MSWIN_FIND_REPLACE
2770 if (s_findrep_msg != 0)
2771 {
2772 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2773 DestroyWindow(s_findrep_hwnd);
2774
2775 if (!IsWindow(s_findrep_hwnd))
2776 {
2777 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002778# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002779 /* If the OS is Windows NT, and 'encoding' differs from active
2780 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002781 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782 {
2783 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2784 s_findrep_hwnd = FindTextW(
2785 (LPFINDREPLACEW) &s_findrep_struct_w);
2786 }
2787 else
2788# endif
2789 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2790 }
2791
2792 set_window_title(s_findrep_hwnd,
2793 _("Find string (use '\\\\' to find a '\\')"));
2794 (void)SetFocus(s_findrep_hwnd);
2795
2796 s_findrep_is_find = TRUE;
2797 }
2798#endif
2799}
2800
2801
2802 void
2803gui_mch_replace_dialog(exarg_T *eap)
2804{
2805#ifdef MSWIN_FIND_REPLACE
2806 if (s_findrep_msg != 0)
2807 {
2808 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2809 DestroyWindow(s_findrep_hwnd);
2810
2811 if (!IsWindow(s_findrep_hwnd))
2812 {
2813 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002814# ifdef FEAT_MBYTE
2815 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816 {
2817 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2818 s_findrep_hwnd = ReplaceTextW(
2819 (LPFINDREPLACEW) &s_findrep_struct_w);
2820 }
2821 else
2822# endif
2823 s_findrep_hwnd = ReplaceText(
2824 (LPFINDREPLACE) &s_findrep_struct);
2825 }
2826
2827 set_window_title(s_findrep_hwnd,
2828 _("Find & Replace (use '\\\\' to find a '\\')"));
2829 (void)SetFocus(s_findrep_hwnd);
2830
2831 s_findrep_is_find = FALSE;
2832 }
2833#endif
2834}
2835
2836
2837/*
2838 * Set visibility of the pointer.
2839 */
2840 void
2841gui_mch_mousehide(int hide)
2842{
2843 if (hide != gui.pointer_hidden)
2844 {
2845 ShowCursor(!hide);
2846 gui.pointer_hidden = hide;
2847 }
2848}
2849
2850#ifdef FEAT_MENU
2851 static void
2852gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2853{
2854 /* Unhide the mouse, we don't get move events here. */
2855 gui_mch_mousehide(FALSE);
2856
2857 (void)TrackPopupMenu(
2858 (HMENU)menu->submenu_id,
2859 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2860 x, y,
2861 (int)0, /*reserved param*/
2862 s_hwnd,
2863 NULL);
2864 /*
2865 * NOTE: The pop-up menu can eat the mouse up event.
2866 * We deal with this in normal.c.
2867 */
2868}
2869#endif
2870
2871/*
2872 * Got a message when the system will go down.
2873 */
2874 static void
2875_OnEndSession(void)
2876{
2877 getout_preserve_modified(1);
2878}
2879
2880/*
2881 * Get this message when the user clicks on the cross in the top right corner
2882 * of a Windows95 window.
2883 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002884 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002885_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002886{
2887 gui_shell_closed();
2888}
2889
2890/*
2891 * Get a message when the window is being destroyed.
2892 */
2893 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002894_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002895{
2896 if (!destroying)
2897 _OnClose(hwnd);
2898}
2899
2900 static void
2901_OnPaint(
2902 HWND hwnd)
2903{
2904 if (!IsMinimized(hwnd))
2905 {
2906 PAINTSTRUCT ps;
2907
2908 out_flush(); /* make sure all output has been processed */
2909 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002910
2911#ifdef FEAT_MBYTE
2912 /* prevent multi-byte characters from misprinting on an invalid
2913 * rectangle */
2914 if (has_mbyte)
2915 {
2916 RECT rect;
2917
2918 GetClientRect(hwnd, &rect);
2919 ps.rcPaint.left = rect.left;
2920 ps.rcPaint.right = rect.right;
2921 }
2922#endif
2923
2924 if (!IsRectEmpty(&ps.rcPaint))
2925 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002926 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2927 ps.rcPaint.right - ps.rcPaint.left + 1,
2928 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2929 }
2930
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002931 EndPaint(hwnd, &ps);
2932 }
2933}
2934
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002935 static void
2936_OnSize(
2937 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002938 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 int cx,
2940 int cy)
2941{
2942 if (!IsMinimized(hwnd))
2943 {
2944 gui_resize_shell(cx, cy);
2945
2946#ifdef FEAT_MENU
2947 /* Menu bar may wrap differently now */
2948 gui_mswin_get_menu_height(TRUE);
2949#endif
2950 }
2951}
2952
2953 static void
2954_OnSetFocus(
2955 HWND hwnd,
2956 HWND hwndOldFocus)
2957{
2958 gui_focus_change(TRUE);
2959 s_getting_focus = TRUE;
2960 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2961}
2962
2963 static void
2964_OnKillFocus(
2965 HWND hwnd,
2966 HWND hwndNewFocus)
2967{
2968 gui_focus_change(FALSE);
2969 s_getting_focus = FALSE;
2970 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2971}
2972
2973/*
2974 * Get a message when the user switches back to vim
2975 */
2976 static LRESULT
2977_OnActivateApp(
2978 HWND hwnd,
2979 BOOL fActivate,
2980 DWORD dwThreadId)
2981{
2982 /* we call gui_focus_change() in _OnSetFocus() */
2983 /* gui_focus_change((int)fActivate); */
2984 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2985}
2986
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987 void
2988gui_mch_destroy_scrollbar(scrollbar_T *sb)
2989{
2990 DestroyWindow(sb->id);
2991}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002992
2993/*
2994 * Get current mouse coordinates in text window.
2995 */
2996 void
2997gui_mch_getmouse(int *x, int *y)
2998{
2999 RECT rct;
3000 POINT mp;
3001
3002 (void)GetWindowRect(s_textArea, &rct);
3003 (void)GetCursorPos((LPPOINT)&mp);
3004 *x = (int)(mp.x - rct.left);
3005 *y = (int)(mp.y - rct.top);
3006}
3007
3008/*
3009 * Move mouse pointer to character at (x, y).
3010 */
3011 void
3012gui_mch_setmouse(int x, int y)
3013{
3014 RECT rct;
3015
3016 (void)GetWindowRect(s_textArea, &rct);
3017 (void)SetCursorPos(x + gui.border_offset + rct.left,
3018 y + gui.border_offset + rct.top);
3019}
3020
3021 static void
3022gui_mswin_get_valid_dimensions(
3023 int w,
3024 int h,
3025 int *valid_w,
3026 int *valid_h)
3027{
3028 int base_width, base_height;
3029
3030 base_width = gui_get_base_width()
3031 + (GetSystemMetrics(SM_CXFRAME) +
3032 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3033 base_height = gui_get_base_height()
3034 + (GetSystemMetrics(SM_CYFRAME) +
3035 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3036 + GetSystemMetrics(SM_CYCAPTION)
3037#ifdef FEAT_MENU
3038 + gui_mswin_get_menu_height(FALSE)
3039#endif
3040 ;
3041 *valid_w = base_width +
3042 ((w - base_width) / gui.char_width) * gui.char_width;
3043 *valid_h = base_height +
3044 ((h - base_height) / gui.char_height) * gui.char_height;
3045}
3046
3047 void
3048gui_mch_flash(int msec)
3049{
3050 RECT rc;
3051
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003052#if defined(FEAT_DIRECTX)
3053 if (IS_ENABLE_DIRECTX())
3054 DWriteContext_Flush(s_dwc);
3055#endif
3056
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057 /*
3058 * Note: InvertRect() excludes right and bottom of rectangle.
3059 */
3060 rc.left = 0;
3061 rc.top = 0;
3062 rc.right = gui.num_cols * gui.char_width;
3063 rc.bottom = gui.num_rows * gui.char_height;
3064 InvertRect(s_hdc, &rc);
3065 gui_mch_flush(); /* make sure it's displayed */
3066
3067 ui_delay((long)msec, TRUE); /* wait for a few msec */
3068
3069 InvertRect(s_hdc, &rc);
3070}
3071
3072/*
3073 * Return flags used for scrolling.
3074 * The SW_INVALIDATE is required when part of the window is covered or
3075 * off-screen. Refer to MS KB Q75236.
3076 */
3077 static int
3078get_scroll_flags(void)
3079{
3080 HWND hwnd;
3081 RECT rcVim, rcOther, rcDest;
3082
3083 GetWindowRect(s_hwnd, &rcVim);
3084
3085 /* Check if the window is partly above or below the screen. We don't care
3086 * about partly left or right of the screen, it is not relevant when
3087 * scrolling up or down. */
3088 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3089 return SW_INVALIDATE;
3090
3091 /* Check if there is an window (partly) on top of us. */
3092 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3093 if (IsWindowVisible(hwnd))
3094 {
3095 GetWindowRect(hwnd, &rcOther);
3096 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3097 return SW_INVALIDATE;
3098 }
3099 return 0;
3100}
3101
3102/*
3103 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3104 * may not be scrolled out properly.
3105 * For gVim, when _OnScroll() is repeated, the character at the
3106 * previous cursor position may be left drawn after scroll.
3107 * The problem can be avoided by calling GetPixel() to get a pixel in
3108 * the region before ScrollWindowEx().
3109 */
3110 static void
3111intel_gpu_workaround(void)
3112{
3113 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3114}
3115
3116/*
3117 * Delete the given number of lines from the given row, scrolling up any
3118 * text further down within the scroll region.
3119 */
3120 void
3121gui_mch_delete_lines(
3122 int row,
3123 int num_lines)
3124{
3125 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003126#if defined(FEAT_DIRECTX)
Bram Moolenaar92467d32017-12-05 13:22:16 +01003127 int use_redraw = 0;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003128#endif
3129
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 rc.left = FILL_X(gui.scroll_region_left);
3131 rc.right = FILL_X(gui.scroll_region_right + 1);
3132 rc.top = FILL_Y(row);
3133 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3134
Bram Moolenaar92467d32017-12-05 13:22:16 +01003135#if defined(FEAT_DIRECTX)
3136 if (IS_ENABLE_DIRECTX())
3137 {
3138 if (s_directx_scrlines > 0 && s_directx_scrlines <= num_lines)
3139 {
3140 RedrawWindow(s_textArea, &rc, NULL, RDW_INVALIDATE);
3141 use_redraw = 1;
3142 }
3143 else
3144 DWriteContext_Flush(s_dwc);
3145 }
3146 if (!use_redraw)
3147#endif
3148 {
3149 intel_gpu_workaround();
3150 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar92467d32017-12-05 13:22:16 +01003152 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003153
3154 UpdateWindow(s_textArea);
3155 /* This seems to be required to avoid the cursor disappearing when
3156 * scrolling such that the cursor ends up in the top-left character on
3157 * the screen... But why? (Webb) */
3158 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3159 /* gui.cursor_is_valid = FALSE; */
3160
3161 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3162 gui.scroll_region_left,
3163 gui.scroll_region_bot, gui.scroll_region_right);
3164}
3165
3166/*
3167 * Insert the given number of lines before the given row, scrolling down any
3168 * following text within the scroll region.
3169 */
3170 void
3171gui_mch_insert_lines(
3172 int row,
3173 int num_lines)
3174{
3175 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003176#if defined(FEAT_DIRECTX)
Bram Moolenaar92467d32017-12-05 13:22:16 +01003177 int use_redraw = 0;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003178#endif
3179
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180 rc.left = FILL_X(gui.scroll_region_left);
3181 rc.right = FILL_X(gui.scroll_region_right + 1);
3182 rc.top = FILL_Y(row);
3183 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003184
3185#if defined(FEAT_DIRECTX)
3186 if (IS_ENABLE_DIRECTX())
3187 {
3188 if (s_directx_scrlines > 0 && s_directx_scrlines <= num_lines)
3189 {
3190 RedrawWindow(s_textArea, &rc, NULL, RDW_INVALIDATE);
3191 use_redraw = 1;
3192 }
3193 else
3194 DWriteContext_Flush(s_dwc);
3195 }
3196 if (!use_redraw)
3197#endif
3198 {
3199 intel_gpu_workaround();
3200 /* The SW_INVALIDATE is required when part of the window is covered or
3201 * off-screen. How do we avoid it when it's not needed? */
3202 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003203 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar92467d32017-12-05 13:22:16 +01003204 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205
3206 UpdateWindow(s_textArea);
3207
3208 gui_clear_block(row, gui.scroll_region_left,
3209 row + num_lines - 1, gui.scroll_region_right);
3210}
3211
3212
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003214gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215{
3216#if defined(FEAT_DIRECTX)
3217 DWriteContext_Close(s_dwc);
3218 DWrite_Final();
3219 s_dwc = NULL;
3220#endif
3221
3222 ReleaseDC(s_textArea, s_hdc);
3223 DeleteObject(s_brush);
3224
3225#ifdef FEAT_TEAROFF
3226 /* Unload the tearoff bitmap */
3227 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3228#endif
3229
3230 /* Destroy our window (if we have one). */
3231 if (s_hwnd != NULL)
3232 {
3233 destroying = TRUE; /* ignore WM_DESTROY message now */
3234 DestroyWindow(s_hwnd);
3235 }
3236
3237#ifdef GLOBAL_IME
3238 global_ime_end();
3239#endif
3240}
3241
3242 static char_u *
3243logfont2name(LOGFONT lf)
3244{
3245 char *p;
3246 char *res;
3247 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003248 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 char *font_name = lf.lfFaceName;
3250
3251 charset_name = charset_id2name((int)lf.lfCharSet);
3252#ifdef FEAT_MBYTE
3253 /* Convert a font name from the current codepage to 'encoding'.
3254 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3255 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3256 {
3257 int len;
3258 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3259 (char_u **)&font_name, &len);
3260 }
3261#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003262 quality_name = quality_id2name((int)lf.lfQuality);
3263
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003264 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3265 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3266 if (res != NULL)
3267 {
3268 p = res;
3269 /* make a normal font string out of the lf thing:*/
3270 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3271 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3272 while (*p)
3273 {
3274 if (*p == ' ')
3275 *p = '_';
3276 ++p;
3277 }
3278 if (lf.lfItalic)
3279 STRCAT(p, ":i");
3280 if (lf.lfWeight >= FW_BOLD)
3281 STRCAT(p, ":b");
3282 if (lf.lfUnderline)
3283 STRCAT(p, ":u");
3284 if (lf.lfStrikeOut)
3285 STRCAT(p, ":s");
3286 if (charset_name != NULL)
3287 {
3288 STRCAT(p, ":c");
3289 STRCAT(p, charset_name);
3290 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003291 if (quality_name != NULL)
3292 {
3293 STRCAT(p, ":q");
3294 STRCAT(p, quality_name);
3295 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003296 }
3297
3298#ifdef FEAT_MBYTE
3299 if (font_name != lf.lfFaceName)
3300 vim_free(font_name);
3301#endif
3302 return (char_u *)res;
3303}
3304
3305
3306#ifdef FEAT_MBYTE_IME
3307/*
3308 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3309 * 'guifont'
3310 */
3311 static void
3312update_im_font(void)
3313{
3314 LOGFONT lf_wide;
3315
3316 if (p_guifontwide != NULL && *p_guifontwide != NUL
3317 && gui.wide_font != NOFONT
3318 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3319 norm_logfont = lf_wide;
3320 else
3321 norm_logfont = sub_logfont;
3322 im_set_font(&norm_logfont);
3323}
3324#endif
3325
3326#ifdef FEAT_MBYTE
3327/*
3328 * Handler of gui.wide_font (p_guifontwide) changed notification.
3329 */
3330 void
3331gui_mch_wide_font_changed(void)
3332{
3333 LOGFONT lf;
3334
3335# ifdef FEAT_MBYTE_IME
3336 update_im_font();
3337# endif
3338
3339 gui_mch_free_font(gui.wide_ital_font);
3340 gui.wide_ital_font = NOFONT;
3341 gui_mch_free_font(gui.wide_bold_font);
3342 gui.wide_bold_font = NOFONT;
3343 gui_mch_free_font(gui.wide_boldital_font);
3344 gui.wide_boldital_font = NOFONT;
3345
3346 if (gui.wide_font
3347 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3348 {
3349 if (!lf.lfItalic)
3350 {
3351 lf.lfItalic = TRUE;
3352 gui.wide_ital_font = get_font_handle(&lf);
3353 lf.lfItalic = FALSE;
3354 }
3355 if (lf.lfWeight < FW_BOLD)
3356 {
3357 lf.lfWeight = FW_BOLD;
3358 gui.wide_bold_font = get_font_handle(&lf);
3359 if (!lf.lfItalic)
3360 {
3361 lf.lfItalic = TRUE;
3362 gui.wide_boldital_font = get_font_handle(&lf);
3363 }
3364 }
3365 }
3366}
3367#endif
3368
3369/*
3370 * Initialise vim to use the font with the given name.
3371 * Return FAIL if the font could not be loaded, OK otherwise.
3372 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003373 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003374gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375{
3376 LOGFONT lf;
3377 GuiFont font = NOFONT;
3378 char_u *p;
3379
3380 /* Load the font */
3381 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3382 font = get_font_handle(&lf);
3383 if (font == NOFONT)
3384 return FAIL;
3385
3386 if (font_name == NULL)
3387 font_name = (char_u *)lf.lfFaceName;
3388#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3389 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003390#endif
3391#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392 sub_logfont = lf;
3393#endif
3394#ifdef FEAT_MBYTE_IME
3395 update_im_font();
3396#endif
3397 gui_mch_free_font(gui.norm_font);
3398 gui.norm_font = font;
3399 current_font_height = lf.lfHeight;
3400 GetFontSize(font);
3401
3402 p = logfont2name(lf);
3403 if (p != NULL)
3404 {
3405 hl_set_font_name(p);
3406
3407 /* When setting 'guifont' to "*" replace it with the actual font name.
3408 * */
3409 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3410 {
3411 vim_free(p_guifont);
3412 p_guifont = p;
3413 }
3414 else
3415 vim_free(p);
3416 }
3417
3418 gui_mch_free_font(gui.ital_font);
3419 gui.ital_font = NOFONT;
3420 gui_mch_free_font(gui.bold_font);
3421 gui.bold_font = NOFONT;
3422 gui_mch_free_font(gui.boldital_font);
3423 gui.boldital_font = NOFONT;
3424
3425 if (!lf.lfItalic)
3426 {
3427 lf.lfItalic = TRUE;
3428 gui.ital_font = get_font_handle(&lf);
3429 lf.lfItalic = FALSE;
3430 }
3431 if (lf.lfWeight < FW_BOLD)
3432 {
3433 lf.lfWeight = FW_BOLD;
3434 gui.bold_font = get_font_handle(&lf);
3435 if (!lf.lfItalic)
3436 {
3437 lf.lfItalic = TRUE;
3438 gui.boldital_font = get_font_handle(&lf);
3439 }
3440 }
3441
3442 return OK;
3443}
3444
3445#ifndef WPF_RESTORETOMAXIMIZED
3446# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3447#endif
3448
3449/*
3450 * Return TRUE if the GUI window is maximized, filling the whole screen.
3451 */
3452 int
3453gui_mch_maximized(void)
3454{
3455 WINDOWPLACEMENT wp;
3456
3457 wp.length = sizeof(WINDOWPLACEMENT);
3458 if (GetWindowPlacement(s_hwnd, &wp))
3459 return wp.showCmd == SW_SHOWMAXIMIZED
3460 || (wp.showCmd == SW_SHOWMINIMIZED
3461 && wp.flags == WPF_RESTORETOMAXIMIZED);
3462
3463 return 0;
3464}
3465
3466/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003467 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3468 * is set. Compute the new Rows and Columns. This is like resizing the
3469 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470 */
3471 void
3472gui_mch_newfont(void)
3473{
3474 RECT rect;
3475
3476 GetWindowRect(s_hwnd, &rect);
3477 if (win_socket_id == 0)
3478 {
3479 gui_resize_shell(rect.right - rect.left
3480 - (GetSystemMetrics(SM_CXFRAME) +
3481 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3482 rect.bottom - rect.top
3483 - (GetSystemMetrics(SM_CYFRAME) +
3484 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3485 - GetSystemMetrics(SM_CYCAPTION)
3486#ifdef FEAT_MENU
3487 - gui_mswin_get_menu_height(FALSE)
3488#endif
3489 );
3490 }
3491 else
3492 {
3493 /* Inside another window, don't use the frame and border. */
3494 gui_resize_shell(rect.right - rect.left,
3495 rect.bottom - rect.top
3496#ifdef FEAT_MENU
3497 - gui_mswin_get_menu_height(FALSE)
3498#endif
3499 );
3500 }
3501}
3502
3503/*
3504 * Set the window title
3505 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 void
3507gui_mch_settitle(
3508 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003509 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510{
3511 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3512}
3513
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003514#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3516 * misc2.c! */
3517static LPCSTR mshape_idcs[] =
3518{
3519 IDC_ARROW, /* arrow */
3520 MAKEINTRESOURCE(0), /* blank */
3521 IDC_IBEAM, /* beam */
3522 IDC_SIZENS, /* updown */
3523 IDC_SIZENS, /* udsizing */
3524 IDC_SIZEWE, /* leftright */
3525 IDC_SIZEWE, /* lrsizing */
3526 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003528 IDC_ARROW, /* crosshair */
3529 IDC_ARROW, /* hand1 */
3530 IDC_ARROW, /* hand2 */
3531 IDC_ARROW, /* pencil */
3532 IDC_ARROW, /* question */
3533 IDC_ARROW, /* right-arrow */
3534 IDC_UPARROW, /* up-arrow */
3535 IDC_ARROW /* last one */
3536};
3537
3538 void
3539mch_set_mouse_shape(int shape)
3540{
3541 LPCSTR idc;
3542
3543 if (shape == MSHAPE_HIDE)
3544 ShowCursor(FALSE);
3545 else
3546 {
3547 if (shape >= MSHAPE_NUMBERED)
3548 idc = IDC_ARROW;
3549 else
3550 idc = mshape_idcs[shape];
3551#ifdef SetClassLongPtr
3552 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3553#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003554 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555#endif
3556 if (!p_mh)
3557 {
3558 POINT mp;
3559
3560 /* Set the position to make it redrawn with the new shape. */
3561 (void)GetCursorPos((LPPOINT)&mp);
3562 (void)SetCursorPos(mp.x, mp.y);
3563 ShowCursor(TRUE);
3564 }
3565 }
3566}
3567#endif
3568
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003569#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003570/*
3571 * The file browser exists in two versions: with "W" uses wide characters,
3572 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3573 * Windows NT/2000/XP the "W" functions are used.
3574 */
3575
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003576# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577/*
3578 * Wide version of convert_filter().
3579 */
3580 static WCHAR *
3581convert_filterW(char_u *s)
3582{
3583 char_u *tmp;
3584 int len;
3585 WCHAR *res;
3586
3587 tmp = convert_filter(s);
3588 if (tmp == NULL)
3589 return NULL;
3590 len = (int)STRLEN(s) + 3;
3591 res = enc_to_utf16(tmp, &len);
3592 vim_free(tmp);
3593 return res;
3594}
3595
3596/*
3597 * Wide version of gui_mch_browse(). Keep in sync!
3598 */
3599 static char_u *
3600gui_mch_browseW(
3601 int saving,
3602 char_u *title,
3603 char_u *dflt,
3604 char_u *ext,
3605 char_u *initdir,
3606 char_u *filter)
3607{
3608 /* We always use the wide function. This means enc_to_utf16() must work,
3609 * otherwise it fails miserably! */
3610 OPENFILENAMEW fileStruct;
3611 WCHAR fileBuf[MAXPATHL];
3612 WCHAR *wp;
3613 int i;
3614 WCHAR *titlep = NULL;
3615 WCHAR *extp = NULL;
3616 WCHAR *initdirp = NULL;
3617 WCHAR *filterp;
3618 char_u *p;
3619
3620 if (dflt == NULL)
3621 fileBuf[0] = NUL;
3622 else
3623 {
3624 wp = enc_to_utf16(dflt, NULL);
3625 if (wp == NULL)
3626 fileBuf[0] = NUL;
3627 else
3628 {
3629 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3630 fileBuf[i] = wp[i];
3631 fileBuf[i] = NUL;
3632 vim_free(wp);
3633 }
3634 }
3635
3636 /* Convert the filter to Windows format. */
3637 filterp = convert_filterW(filter);
3638
3639 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003640# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003641 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003642 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003643# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003644 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003645# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003646
3647 if (title != NULL)
3648 titlep = enc_to_utf16(title, NULL);
3649 fileStruct.lpstrTitle = titlep;
3650
3651 if (ext != NULL)
3652 extp = enc_to_utf16(ext, NULL);
3653 fileStruct.lpstrDefExt = extp;
3654
3655 fileStruct.lpstrFile = fileBuf;
3656 fileStruct.nMaxFile = MAXPATHL;
3657 fileStruct.lpstrFilter = filterp;
3658 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3659 /* has an initial dir been specified? */
3660 if (initdir != NULL && *initdir != NUL)
3661 {
3662 /* Must have backslashes here, no matter what 'shellslash' says */
3663 initdirp = enc_to_utf16(initdir, NULL);
3664 if (initdirp != NULL)
3665 {
3666 for (wp = initdirp; *wp != NUL; ++wp)
3667 if (*wp == '/')
3668 *wp = '\\';
3669 }
3670 fileStruct.lpstrInitialDir = initdirp;
3671 }
3672
3673 /*
3674 * TODO: Allow selection of multiple files. Needs another arg to this
3675 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3676 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3677 * files that don't exist yet, so I haven't put it in. What about
3678 * OFN_PATHMUSTEXIST?
3679 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3680 */
3681 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003682# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683 if (curbuf->b_p_bin)
3684 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003685# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 if (saving)
3687 {
3688 if (!GetSaveFileNameW(&fileStruct))
3689 return NULL;
3690 }
3691 else
3692 {
3693 if (!GetOpenFileNameW(&fileStruct))
3694 return NULL;
3695 }
3696
3697 vim_free(filterp);
3698 vim_free(initdirp);
3699 vim_free(titlep);
3700 vim_free(extp);
3701
3702 /* Convert from UCS2 to 'encoding'. */
3703 p = utf16_to_enc(fileBuf, NULL);
3704 if (p != NULL)
3705 /* when out of memory we get garbage for non-ASCII chars */
3706 STRCPY(fileBuf, p);
3707 vim_free(p);
3708
3709 /* Give focus back to main window (when using MDI). */
3710 SetFocus(s_hwnd);
3711
3712 /* Shorten the file name if possible */
3713 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3714}
3715# endif /* FEAT_MBYTE */
3716
3717
3718/*
3719 * Convert the string s to the proper format for a filter string by replacing
3720 * the \t and \n delimiters with \0.
3721 * Returns the converted string in allocated memory.
3722 *
3723 * Keep in sync with convert_filterW() above!
3724 */
3725 static char_u *
3726convert_filter(char_u *s)
3727{
3728 char_u *res;
3729 unsigned s_len = (unsigned)STRLEN(s);
3730 unsigned i;
3731
3732 res = alloc(s_len + 3);
3733 if (res != NULL)
3734 {
3735 for (i = 0; i < s_len; ++i)
3736 if (s[i] == '\t' || s[i] == '\n')
3737 res[i] = '\0';
3738 else
3739 res[i] = s[i];
3740 res[s_len] = NUL;
3741 /* Add two extra NULs to make sure it's properly terminated. */
3742 res[s_len + 1] = NUL;
3743 res[s_len + 2] = NUL;
3744 }
3745 return res;
3746}
3747
3748/*
3749 * Select a directory.
3750 */
3751 char_u *
3752gui_mch_browsedir(char_u *title, char_u *initdir)
3753{
3754 /* We fake this: Use a filter that doesn't select anything and a default
3755 * file name that won't be used. */
3756 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3757 initdir, (char_u *)_("Directory\t*.nothing\n"));
3758}
3759
3760/*
3761 * Pop open a file browser and return the file selected, in allocated memory,
3762 * or NULL if Cancel is hit.
3763 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3764 * title - Title message for the file browser dialog.
3765 * dflt - Default name of file.
3766 * ext - Default extension to be added to files without extensions.
3767 * initdir - directory in which to open the browser (NULL = current dir)
3768 * filter - Filter for matched files to choose from.
3769 *
3770 * Keep in sync with gui_mch_browseW() above!
3771 */
3772 char_u *
3773gui_mch_browse(
3774 int saving,
3775 char_u *title,
3776 char_u *dflt,
3777 char_u *ext,
3778 char_u *initdir,
3779 char_u *filter)
3780{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003781# ifdef FEAT_MBYTE
3782 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3783# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 OPENFILENAME fileStruct;
3785 char_u fileBuf[MAXPATHL];
3786 char_u *initdirp = NULL;
3787 char_u *filterp;
3788 char_u *p;
3789
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 if (dflt == NULL)
3791 fileBuf[0] = NUL;
3792 else
3793 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3794
3795 /* Convert the filter to Windows format. */
3796 filterp = convert_filter(filter);
3797
3798 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003799# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003800 /* be compatible with Windows NT 4.0 */
3801 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003802# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003804# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805
3806 fileStruct.lpstrTitle = (LPSTR)title;
3807 fileStruct.lpstrDefExt = (LPSTR)ext;
3808
3809 fileStruct.lpstrFile = (LPSTR)fileBuf;
3810 fileStruct.nMaxFile = MAXPATHL;
3811 fileStruct.lpstrFilter = (LPSTR)filterp;
3812 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3813 /* has an initial dir been specified? */
3814 if (initdir != NULL && *initdir != NUL)
3815 {
3816 /* Must have backslashes here, no matter what 'shellslash' says */
3817 initdirp = vim_strsave(initdir);
3818 if (initdirp != NULL)
3819 for (p = initdirp; *p != NUL; ++p)
3820 if (*p == '/')
3821 *p = '\\';
3822 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3823 }
3824
3825 /*
3826 * TODO: Allow selection of multiple files. Needs another arg to this
3827 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3828 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3829 * files that don't exist yet, so I haven't put it in. What about
3830 * OFN_PATHMUSTEXIST?
3831 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3832 */
3833 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003834# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003835 if (curbuf->b_p_bin)
3836 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003837# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838 if (saving)
3839 {
3840 if (!GetSaveFileName(&fileStruct))
3841 return NULL;
3842 }
3843 else
3844 {
3845 if (!GetOpenFileName(&fileStruct))
3846 return NULL;
3847 }
3848
3849 vim_free(filterp);
3850 vim_free(initdirp);
3851
3852 /* Give focus back to main window (when using MDI). */
3853 SetFocus(s_hwnd);
3854
3855 /* Shorten the file name if possible */
3856 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003857# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858}
3859#endif /* FEAT_BROWSE */
3860
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 static void
3862_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003863 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864 HDROP hDrop)
3865{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003866#define BUFPATHLEN _MAX_PATH
3867#define DRAGQVAL 0xFFFFFFFF
3868#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003869 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003870#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003871 char szFile[BUFPATHLEN];
3872 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3873 UINT i;
3874 char_u **fnames;
3875 POINT pt;
3876 int_u modifiers = 0;
3877
3878 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3879
3880 /* Obtain dropped position */
3881 DragQueryPoint(hDrop, &pt);
3882 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3883
3884 reset_VIsual();
3885
3886 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3887
3888 if (fnames != NULL)
3889 for (i = 0; i < cFiles; ++i)
3890 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003891#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003892 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3893 fnames[i] = utf16_to_enc(wszFile, NULL);
3894 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003895#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003896 {
3897 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3898 fnames[i] = vim_strsave((char_u *)szFile);
3899 }
3900 }
3901
3902 DragFinish(hDrop);
3903
3904 if (fnames != NULL)
3905 {
3906 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3907 modifiers |= MOUSE_SHIFT;
3908 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3909 modifiers |= MOUSE_CTRL;
3910 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3911 modifiers |= MOUSE_ALT;
3912
3913 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3914
3915 s_need_activate = TRUE;
3916 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917}
3918
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003919 static int
3920_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003921 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 HWND hwndCtl,
3923 UINT code,
3924 int pos)
3925{
3926 static UINT prev_code = 0; /* code of previous call */
3927 scrollbar_T *sb, *sb_info;
3928 long val;
3929 int dragging = FALSE;
3930 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 SCROLLINFO si;
3932
3933 si.cbSize = sizeof(si);
3934 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003935
3936 sb = gui_mswin_find_scrollbar(hwndCtl);
3937 if (sb == NULL)
3938 return 0;
3939
3940 if (sb->wp != NULL) /* Left or right scrollbar */
3941 {
3942 /*
3943 * Careful: need to get scrollbar info out of first (left) scrollbar
3944 * for window, but keep real scrollbar too because we must pass it to
3945 * gui_drag_scrollbar().
3946 */
3947 sb_info = &sb->wp->w_scrollbars[0];
3948 }
3949 else /* Bottom scrollbar */
3950 sb_info = sb;
3951 val = sb_info->value;
3952
3953 switch (code)
3954 {
3955 case SB_THUMBTRACK:
3956 val = pos;
3957 dragging = TRUE;
3958 if (sb->scroll_shift > 0)
3959 val <<= sb->scroll_shift;
3960 break;
3961 case SB_LINEDOWN:
3962 val++;
3963 break;
3964 case SB_LINEUP:
3965 val--;
3966 break;
3967 case SB_PAGEDOWN:
3968 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3969 break;
3970 case SB_PAGEUP:
3971 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3972 break;
3973 case SB_TOP:
3974 val = 0;
3975 break;
3976 case SB_BOTTOM:
3977 val = sb_info->max;
3978 break;
3979 case SB_ENDSCROLL:
3980 if (prev_code == SB_THUMBTRACK)
3981 {
3982 /*
3983 * "pos" only gives us 16-bit data. In case of large file,
3984 * use GetScrollPos() which returns 32-bit. Unfortunately it
3985 * is not valid while the scrollbar is being dragged.
3986 */
3987 val = GetScrollPos(hwndCtl, SB_CTL);
3988 if (sb->scroll_shift > 0)
3989 val <<= sb->scroll_shift;
3990 }
3991 break;
3992
3993 default:
3994 /* TRACE("Unknown scrollbar event %d\n", code); */
3995 return 0;
3996 }
3997 prev_code = code;
3998
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003999 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4000 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004001
4002 /*
4003 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4004 */
4005 if (sb->wp != NULL)
4006 {
4007 scrollbar_T *sba = sb->wp->w_scrollbars;
4008 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4009
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004010 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004011 }
4012
4013 /* Don't let us be interrupted here by another message. */
4014 s_busy_processing = TRUE;
4015
4016 /* When "allow_scrollbar" is FALSE still need to remember the new
4017 * position, but don't actually scroll by setting "dont_scroll". */
4018 dont_scroll = !allow_scrollbar;
4019
4020 gui_drag_scrollbar(sb, val, dragging);
4021
4022 s_busy_processing = FALSE;
4023 dont_scroll = dont_scroll_save;
4024
4025 return 0;
4026}
4027
4028
4029/*
4030 * Get command line arguments.
4031 * Use "prog" as the name of the program and "cmdline" as the arguments.
4032 * Copy the arguments to allocated memory.
4033 * Return the number of arguments (including program name).
4034 * Return pointers to the arguments in "argvp". Memory is allocated with
4035 * malloc(), use free() instead of vim_free().
4036 * Return pointer to buffer in "tofree".
4037 * Returns zero when out of memory.
4038 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004039 int
4040get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4041{
4042 int i;
4043 char *p;
4044 char *progp;
4045 char *pnew = NULL;
4046 char *newcmdline;
4047 int inquote;
4048 int argc;
4049 char **argv = NULL;
4050 int round;
4051
4052 *tofree = NULL;
4053
4054#ifdef FEAT_MBYTE
4055 /* Try using the Unicode version first, it takes care of conversion when
4056 * 'encoding' is changed. */
4057 argc = get_cmd_argsW(&argv);
4058 if (argc != 0)
4059 goto done;
4060#endif
4061
4062 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4063 * non-space. */
4064 p = strrchr(prog, '.');
4065 if (p != NULL)
4066 *p = NUL;
4067 for (progp = prog; *progp == ' '; ++progp)
4068 ;
4069
4070 /* The command line is copied to allocated memory, so that we can change
4071 * it. Add the size of the string, the separating NUL and a terminating
4072 * NUL. */
4073 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4074 if (newcmdline == NULL)
4075 return 0;
4076
4077 /*
4078 * First round: count the number of arguments ("pnew" == NULL).
4079 * Second round: produce the arguments.
4080 */
4081 for (round = 1; round <= 2; ++round)
4082 {
4083 /* First argument is the program name. */
4084 if (pnew != NULL)
4085 {
4086 argv[0] = pnew;
4087 strcpy(pnew, progp);
4088 pnew += strlen(pnew);
4089 *pnew++ = NUL;
4090 }
4091
4092 /*
4093 * Isolate each argument and put it in argv[].
4094 */
4095 p = cmdline;
4096 argc = 1;
4097 while (*p != NUL)
4098 {
4099 inquote = FALSE;
4100 if (pnew != NULL)
4101 argv[argc] = pnew;
4102 ++argc;
4103 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4104 {
4105 /* Backslashes are only special when followed by a double
4106 * quote. */
4107 i = (int)strspn(p, "\\");
4108 if (p[i] == '"')
4109 {
4110 /* Halve the number of backslashes. */
4111 if (i > 1 && pnew != NULL)
4112 {
4113 vim_memset(pnew, '\\', i / 2);
4114 pnew += i / 2;
4115 }
4116
4117 /* Even nr of backslashes toggles quoting, uneven copies
4118 * the double quote. */
4119 if ((i & 1) == 0)
4120 inquote = !inquote;
4121 else if (pnew != NULL)
4122 *pnew++ = '"';
4123 p += i + 1;
4124 }
4125 else if (i > 0)
4126 {
4127 /* Copy span of backslashes unmodified. */
4128 if (pnew != NULL)
4129 {
4130 vim_memset(pnew, '\\', i);
4131 pnew += i;
4132 }
4133 p += i;
4134 }
4135 else
4136 {
4137 if (pnew != NULL)
4138 *pnew++ = *p;
4139#ifdef FEAT_MBYTE
4140 /* Can't use mb_* functions, because 'encoding' is not
4141 * initialized yet here. */
4142 if (IsDBCSLeadByte(*p))
4143 {
4144 ++p;
4145 if (pnew != NULL)
4146 *pnew++ = *p;
4147 }
4148#endif
4149 ++p;
4150 }
4151 }
4152
4153 if (pnew != NULL)
4154 *pnew++ = NUL;
4155 while (*p == ' ' || *p == '\t')
4156 ++p; /* advance until a non-space */
4157 }
4158
4159 if (round == 1)
4160 {
4161 argv = (char **)malloc((argc + 1) * sizeof(char *));
4162 if (argv == NULL )
4163 {
4164 free(newcmdline);
4165 return 0; /* malloc error */
4166 }
4167 pnew = newcmdline;
4168 *tofree = newcmdline;
4169 }
4170 }
4171
4172#ifdef FEAT_MBYTE
4173done:
4174#endif
4175 argv[argc] = NULL; /* NULL-terminated list */
4176 *argvp = argv;
4177 return argc;
4178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180#ifdef FEAT_XPM_W32
4181# include "xpm_w32.h"
4182#endif
4183
4184#ifdef PROTO
4185# define WINAPI
4186#endif
4187
4188#ifdef __MINGW32__
4189/*
4190 * Add a lot of missing defines.
4191 * They are not always missing, we need the #ifndef's.
4192 */
4193# ifndef _cdecl
4194# define _cdecl
4195# endif
4196# ifndef IsMinimized
4197# define IsMinimized(hwnd) IsIconic(hwnd)
4198# endif
4199# ifndef IsMaximized
4200# define IsMaximized(hwnd) IsZoomed(hwnd)
4201# endif
4202# ifndef SelectFont
4203# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4204# endif
4205# ifndef GetStockBrush
4206# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4207# endif
4208# ifndef DeleteBrush
4209# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4210# endif
4211
4212# ifndef HANDLE_WM_RBUTTONDBLCLK
4213# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4214 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4215# endif
4216# ifndef HANDLE_WM_MBUTTONUP
4217# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4218 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4219# endif
4220# ifndef HANDLE_WM_MBUTTONDBLCLK
4221# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4222 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4223# endif
4224# ifndef HANDLE_WM_LBUTTONDBLCLK
4225# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4226 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4227# endif
4228# ifndef HANDLE_WM_RBUTTONDOWN
4229# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4230 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4231# endif
4232# ifndef HANDLE_WM_MOUSEMOVE
4233# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4234 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4235# endif
4236# ifndef HANDLE_WM_RBUTTONUP
4237# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4238 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4239# endif
4240# ifndef HANDLE_WM_MBUTTONDOWN
4241# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4242 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4243# endif
4244# ifndef HANDLE_WM_LBUTTONUP
4245# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4246 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4247# endif
4248# ifndef HANDLE_WM_LBUTTONDOWN
4249# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4250 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4251# endif
4252# ifndef HANDLE_WM_SYSCHAR
4253# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4254 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4255# endif
4256# ifndef HANDLE_WM_ACTIVATEAPP
4257# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4258 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4259# endif
4260# ifndef HANDLE_WM_WINDOWPOSCHANGING
4261# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4262 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4263# endif
4264# ifndef HANDLE_WM_VSCROLL
4265# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4266 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4267# endif
4268# ifndef HANDLE_WM_SETFOCUS
4269# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4270 ((fn)((hwnd), (HWND)(wParam)), 0L)
4271# endif
4272# ifndef HANDLE_WM_KILLFOCUS
4273# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4274 ((fn)((hwnd), (HWND)(wParam)), 0L)
4275# endif
4276# ifndef HANDLE_WM_HSCROLL
4277# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4278 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4279# endif
4280# ifndef HANDLE_WM_DROPFILES
4281# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4282 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4283# endif
4284# ifndef HANDLE_WM_CHAR
4285# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4286 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4287# endif
4288# ifndef HANDLE_WM_SYSDEADCHAR
4289# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4290 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4291# endif
4292# ifndef HANDLE_WM_DEADCHAR
4293# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4294 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4295# endif
4296#endif /* __MINGW32__ */
4297
4298
4299/* Some parameters for tearoff menus. All in pixels. */
4300#define TEAROFF_PADDING_X 2
4301#define TEAROFF_BUTTON_PAD_X 8
4302#define TEAROFF_MIN_WIDTH 200
4303#define TEAROFF_SUBMENU_LABEL ">>"
4304#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4305
4306
4307/* For the Intellimouse: */
4308#ifndef WM_MOUSEWHEEL
4309#define WM_MOUSEWHEEL 0x20a
4310#endif
4311
4312
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004313#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314# define ID_BEVAL_TOOLTIP 200
4315# define BEVAL_TEXT_LEN MAXPATHL
4316
Bram Moolenaar167632f2010-05-26 21:42:54 +02004317#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004318/* Work around old versions of basetsd.h which wrongly declares
4319 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004320# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004321# define UINT_PTR UINT
4322#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004323
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004324static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4325static void delete_tooltip(BalloonEval *beval);
4326static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004327
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004329static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004331
Bram Moolenaar82881492012-11-20 16:53:39 +01004332
4333/* cproto fails on missing include files */
4334#ifndef PROTO
4335
Bram Moolenaar45360022005-07-21 21:08:21 +00004336/*
4337 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004338 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004339 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004340# include <pshpack1.h>
4341
4342#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004343
4344typedef struct _DllVersionInfo
4345{
4346 DWORD cbSize;
4347 DWORD dwMajorVersion;
4348 DWORD dwMinorVersion;
4349 DWORD dwBuildNumber;
4350 DWORD dwPlatformID;
4351} DLLVERSIONINFO;
4352
Bram Moolenaar82881492012-11-20 16:53:39 +01004353#ifndef PROTO
4354# include <poppack.h>
4355#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004356
Bram Moolenaar45360022005-07-21 21:08:21 +00004357typedef struct tagTOOLINFOA_NEW
4358{
4359 UINT cbSize;
4360 UINT uFlags;
4361 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004362 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004363 RECT rect;
4364 HINSTANCE hinst;
4365 LPSTR lpszText;
4366 LPARAM lParam;
4367} TOOLINFO_NEW;
4368
4369typedef struct tagNMTTDISPINFO_NEW
4370{
4371 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004372 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004373 char szText[80];
4374 HINSTANCE hinst;
4375 UINT uFlags;
4376 LPARAM lParam;
4377} NMTTDISPINFO_NEW;
4378
Bram Moolenaar45360022005-07-21 21:08:21 +00004379typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4380#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004381# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382#endif
4383
Bram Moolenaar45360022005-07-21 21:08:21 +00004384#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004385# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004386#endif
4387
4388#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004389# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004390#endif
4391
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004392#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004393
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004394#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4395/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4396 * it here if LPNMTTDISPINFO isn't defined.
4397 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4398 * _MSC_VER. */
4399# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4400typedef struct tagNMTTDISPINFOA {
4401 NMHDR hdr;
4402 LPSTR lpszText;
4403 char szText[80];
4404 HINSTANCE hinst;
4405 UINT uFlags;
4406 LPARAM lParam;
4407} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4408# define LPNMTTDISPINFO LPNMTTDISPINFOA
4409
4410# ifdef FEAT_MBYTE
4411typedef struct tagNMTTDISPINFOW {
4412 NMHDR hdr;
4413 LPWSTR lpszText;
4414 WCHAR szText[80];
4415 HINSTANCE hinst;
4416 UINT uFlags;
4417 LPARAM lParam;
4418} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4419# endif
4420# endif
4421#endif
4422
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004423#ifndef TTN_GETDISPINFOW
4424# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4425#endif
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427/* Local variables: */
4428
4429#ifdef FEAT_MENU
4430static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433/*
4434 * Use the system font for dialogs and tear-off menus. Remove this line to
4435 * use DLG_FONT_NAME.
4436 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004437#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439#define VIM_NAME "vim"
4440#define VIM_CLASS "Vim"
4441#define VIM_CLASSW L"Vim"
4442
4443/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4444 * thus there should be room for every dialog. For tearoffs it's made bigger
4445 * when needed. */
4446#define DLG_ALLOC_SIZE 16 * 1024
4447
4448/*
4449 * stuff for dialogs, menus, tearoffs etc.
4450 */
4451static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004452#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455static PWORD
4456add_dialog_element(
4457 PWORD p,
4458 DWORD lStyle,
4459 WORD x,
4460 WORD y,
4461 WORD w,
4462 WORD h,
4463 WORD Id,
4464 WORD clss,
4465 const char *caption);
4466static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004467static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004468#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471static void get_dialog_font_metrics(void);
4472
4473static int dialog_default_button = -1;
4474
4475/* Intellimouse support */
4476static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
4478static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4479#ifdef FEAT_TOOLBAR
4480static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004481static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482static int get_toolbar_bitmap(vimmenu_T *menu);
4483#endif
4484
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004485#ifdef FEAT_GUI_TABLINE
4486static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004487static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004488#endif
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490#ifdef FEAT_MBYTE_IME
4491static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4492static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4493#endif
4494#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4495# ifdef NOIME
4496typedef struct tagCOMPOSITIONFORM {
4497 DWORD dwStyle;
4498 POINT ptCurrentPos;
4499 RECT rcArea;
4500} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4501typedef HANDLE HIMC;
4502# endif
4503
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004504static HINSTANCE hLibImm = NULL;
4505static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4506static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4507static HIMC (WINAPI *pImmGetContext)(HWND);
4508static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4509static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4510static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4511static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4512static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4513static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4514static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4515static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004516static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517static void dyn_imm_load(void);
4518#else
4519# define pImmGetCompositionStringA ImmGetCompositionStringA
4520# define pImmGetCompositionStringW ImmGetCompositionStringW
4521# define pImmGetContext ImmGetContext
4522# define pImmAssociateContext ImmAssociateContext
4523# define pImmReleaseContext ImmReleaseContext
4524# define pImmGetOpenStatus ImmGetOpenStatus
4525# define pImmSetOpenStatus ImmSetOpenStatus
4526# define pImmGetCompositionFont ImmGetCompositionFontA
4527# define pImmSetCompositionFont ImmSetCompositionFontA
4528# define pImmSetCompositionWindow ImmSetCompositionWindow
4529# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004530# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#endif
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533#ifdef FEAT_MENU
4534/*
4535 * Figure out how high the menu bar is at the moment.
4536 */
4537 static int
4538gui_mswin_get_menu_height(
4539 int fix_window) /* If TRUE, resize window if menu height changed */
4540{
4541 static int old_menu_height = -1;
4542
4543 RECT rc1, rc2;
4544 int num;
4545 int menu_height;
4546
4547 if (gui.menu_is_active)
4548 num = GetMenuItemCount(s_menuBar);
4549 else
4550 num = 0;
4551
4552 if (num == 0)
4553 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004554 else if (IsMinimized(s_hwnd))
4555 {
4556 /* The height of the menu cannot be determined while the window is
4557 * minimized. Take the previous height if the menu is changed in that
4558 * state, to avoid that Vim's vertical window size accidentally
4559 * increases due to the unaccounted-for menu height. */
4560 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
4563 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004564 /*
4565 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4566 * seem to have been set yet, so menu wraps in default window
4567 * width which is very narrow. Instead just return height of a
4568 * single menu item. Will still be wrong when the menu really
4569 * should wrap over more than one line.
4570 */
4571 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4572 if (gui.starting)
4573 menu_height = rc1.bottom - rc1.top + 1;
4574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004576 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4577 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579 }
4580
4581 if (fix_window && menu_height != old_menu_height)
4582 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004583 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004585 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 return menu_height;
4588}
4589#endif /*FEAT_MENU*/
4590
4591
4592/*
4593 * Setup for the Intellimouse
4594 */
4595 static void
4596init_mouse_wheel(void)
4597{
4598
4599#ifndef SPI_GETWHEELSCROLLLINES
4600# define SPI_GETWHEELSCROLLLINES 104
4601#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004602#ifndef SPI_SETWHEELSCROLLLINES
4603# define SPI_SETWHEELSCROLLLINES 105
4604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4607#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4608#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4609#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4610
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 mouse_scroll_lines = 3; /* reasonable default */
4612
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004613 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4614 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4615 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616}
4617
4618
4619/* Intellimouse wheel handler */
4620 static void
4621_OnMouseWheel(
4622 HWND hwnd,
4623 short zDelta)
4624{
4625/* Treat a mouse wheel event as if it were a scroll request */
4626 int i;
4627 int size;
4628 HWND hwndCtl;
4629
4630 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4631 {
4632 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4633 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4634 }
4635 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4636 {
4637 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4638 size = curwin->w_scrollbars[SBAR_LEFT].size;
4639 }
4640 else
4641 return;
4642
4643 size = curwin->w_height;
4644 if (mouse_scroll_lines == 0)
4645 init_mouse_wheel();
4646
4647 if (mouse_scroll_lines > 0
4648 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4649 {
4650 for (i = mouse_scroll_lines; i > 0; --i)
4651 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4652 }
4653 else
4654 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4655}
4656
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004657#ifdef USE_SYSMENU_FONT
4658/*
4659 * Get Menu Font.
4660 * Return OK or FAIL.
4661 */
4662 static int
4663gui_w32_get_menu_font(LOGFONT *lf)
4664{
4665 NONCLIENTMETRICS nm;
4666
4667 nm.cbSize = sizeof(NONCLIENTMETRICS);
4668 if (!SystemParametersInfo(
4669 SPI_GETNONCLIENTMETRICS,
4670 sizeof(NONCLIENTMETRICS),
4671 &nm,
4672 0))
4673 return FAIL;
4674 *lf = nm.lfMenuFont;
4675 return OK;
4676}
4677#endif
4678
4679
4680#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4681/*
4682 * Set the GUI tabline font to the system menu font
4683 */
4684 static void
4685set_tabline_font(void)
4686{
4687 LOGFONT lfSysmenu;
4688 HFONT font;
4689 HWND hwnd;
4690 HDC hdc;
4691 HFONT hfntOld;
4692 TEXTMETRIC tm;
4693
4694 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4695 return;
4696
4697 font = CreateFontIndirect(&lfSysmenu);
4698
4699 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4700
4701 /*
4702 * Compute the height of the font used for the tab text
4703 */
4704 hwnd = GetDesktopWindow();
4705 hdc = GetWindowDC(hwnd);
4706 hfntOld = SelectFont(hdc, font);
4707
4708 GetTextMetrics(hdc, &tm);
4709
4710 SelectFont(hdc, hfntOld);
4711 ReleaseDC(hwnd, hdc);
4712
4713 /*
4714 * The space used by the tab border and the space between the tab label
4715 * and the tab border is included as 7.
4716 */
4717 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4718}
4719#endif
4720
Bram Moolenaar520470a2005-06-16 21:59:56 +00004721/*
4722 * Invoked when a setting was changed.
4723 */
4724 static LRESULT CALLBACK
4725_OnSettingChange(UINT n)
4726{
4727 if (n == SPI_SETWHEELSCROLLLINES)
4728 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4729 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004730#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4731 if (n == SPI_SETNONCLIENTMETRICS)
4732 set_tabline_font();
4733#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004734 return 0;
4735}
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737#ifdef FEAT_NETBEANS_INTG
4738 static void
4739_OnWindowPosChanged(
4740 HWND hwnd,
4741 const LPWINDOWPOS lpwpos)
4742{
4743 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004744 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4747 || lpwpos->cx != cx || lpwpos->cy != cy))
4748 {
4749 x = lpwpos->x;
4750 y = lpwpos->y;
4751 cx = lpwpos->cx;
4752 cy = lpwpos->cy;
4753 netbeans_frame_moved(x, y);
4754 }
4755 /* Allow to send WM_SIZE and WM_MOVE */
4756 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4757}
4758#endif
4759
4760 static int
4761_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 UINT fwSide,
4763 LPRECT lprc)
4764{
4765 int w, h;
4766 int valid_w, valid_h;
4767 int w_offset, h_offset;
4768
4769 w = lprc->right - lprc->left;
4770 h = lprc->bottom - lprc->top;
4771 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4772 w_offset = w - valid_w;
4773 h_offset = h - valid_h;
4774
4775 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4776 || fwSide == WMSZ_BOTTOMLEFT)
4777 lprc->left += w_offset;
4778 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4779 || fwSide == WMSZ_BOTTOMRIGHT)
4780 lprc->right -= w_offset;
4781
4782 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4783 || fwSide == WMSZ_TOPRIGHT)
4784 lprc->top += h_offset;
4785 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4786 || fwSide == WMSZ_BOTTOMRIGHT)
4787 lprc->bottom -= h_offset;
4788 return TRUE;
4789}
4790
4791
4792
4793 static LRESULT CALLBACK
4794_WndProc(
4795 HWND hwnd,
4796 UINT uMsg,
4797 WPARAM wParam,
4798 LPARAM lParam)
4799{
4800 /*
4801 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4802 hwnd, uMsg, wParam, lParam);
4803 */
4804
4805 HandleMouseHide(uMsg, lParam);
4806
4807 s_uMsg = uMsg;
4808 s_wParam = wParam;
4809 s_lParam = lParam;
4810
4811 switch (uMsg)
4812 {
4813 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4814 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4815 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4816 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4817 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4818 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4819 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4820 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4821 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4822#ifdef FEAT_MENU
4823 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4824#endif
4825 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4826 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4827 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4828 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4829 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4830 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4831 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4832 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4833 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4834#ifdef FEAT_NETBEANS_INTG
4835 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4836#endif
4837
Bram Moolenaarafa24992006-03-27 20:58:26 +00004838#ifdef FEAT_GUI_TABLINE
4839 case WM_RBUTTONUP:
4840 {
4841 if (gui_mch_showing_tabline())
4842 {
4843 POINT pt;
4844 RECT rect;
4845
4846 /*
4847 * If the cursor is on the tabline, display the tab menu
4848 */
4849 GetCursorPos((LPPOINT)&pt);
4850 GetWindowRect(s_textArea, &rect);
4851 if (pt.y < rect.top)
4852 {
4853 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004854 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004855 }
4856 }
4857 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4858 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004859 case WM_LBUTTONDBLCLK:
4860 {
4861 /*
4862 * If the user double clicked the tabline, create a new tab
4863 */
4864 if (gui_mch_showing_tabline())
4865 {
4866 POINT pt;
4867 RECT rect;
4868
4869 GetCursorPos((LPPOINT)&pt);
4870 GetWindowRect(s_textArea, &rect);
4871 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004872 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004873 }
4874 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4875 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004876#endif
4877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 case WM_QUERYENDSESSION: /* System wants to go down. */
4879 gui_shell_closed(); /* Will exit when no changed buffers. */
4880 return FALSE; /* Do NOT allow system to go down. */
4881
4882 case WM_ENDSESSION:
4883 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004886 return 0L;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 break;
4889
4890 case WM_CHAR:
4891 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4892 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004893 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 return 0L;
4895
4896 case WM_SYSCHAR:
4897 /*
4898 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4899 * shortcut key, handle like a typed ALT key, otherwise call Windows
4900 * ALT key handling.
4901 */
4902#ifdef FEAT_MENU
4903 if ( !gui.menu_is_active
4904 || p_wak[0] == 'n'
4905 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4906 )
4907#endif
4908 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004909 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return 0L;
4911 }
4912#ifdef FEAT_MENU
4913 else
4914 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4915#endif
4916
4917 case WM_SYSKEYUP:
4918#ifdef FEAT_MENU
4919 /* This used to be done only when menu is active: ALT key is used for
4920 * that. But that caused problems when menu is disabled and using
4921 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4922 * are received, mouse pointer remains hidden. */
4923 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4924#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004925 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#endif
4927
4928 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004929 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 case WM_MOUSEWHEEL:
4932 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004933 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Bram Moolenaar520470a2005-06-16 21:59:56 +00004935 /* Notification for change in SystemParametersInfo() */
4936 case WM_SETTINGCHANGE:
4937 return _OnSettingChange((UINT)wParam);
4938
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004939#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 case WM_NOTIFY:
4941 switch (((LPNMHDR) lParam)->code)
4942 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004943# ifdef FEAT_MBYTE
4944 case TTN_GETDISPINFOW:
4945# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004946 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004948 LPNMHDR hdr = (LPNMHDR)lParam;
4949 char_u *str = NULL;
4950 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004951
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004952 vim_free(tt_text);
4953 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004954
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004955# ifdef FEAT_GUI_TABLINE
4956 if (gui_mch_showing_tabline()
4957 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004958 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004959 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004960 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004961 * Mouse is over the GUI tabline. Display the
4962 * tooltip for the tab under the cursor
4963 *
4964 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004965 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004966 GetCursorPos(&pt);
4967 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004968 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004969 TCHITTESTINFO htinfo;
4970 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004971
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004972 /*
4973 * Get the tab under the cursor
4974 */
4975 htinfo.pt.x = pt.x;
4976 htinfo.pt.y = pt.y;
4977 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4978 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004979 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004980 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004981
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004982 tp = find_tabpage(idx + 1);
4983 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004984 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004985 get_tabline_label(tp, TRUE);
4986 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004987 }
4988 }
4989 }
4990 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004991# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004992# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004993# ifdef FEAT_GUI_TABLINE
4994 else
4995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004997 UINT idButton;
4998 vimmenu_T *pMenu;
4999
5000 idButton = (UINT) hdr->idFrom;
5001 pMenu = gui_mswin_find_menu(root_menu, idButton);
5002 if (pMenu)
5003 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005005# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005006 if (str != NULL)
5007 {
5008# ifdef FEAT_MBYTE
5009 if (hdr->code == TTN_GETDISPINFOW)
5010 {
5011 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5012
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005013 /* Set the maximum width, this also enables using
5014 * \n for line break. */
5015 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5016 0, 500);
5017
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005018 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005019 lpdi->lpszText = tt_text;
5020 /* can't show tooltip if failed */
5021 }
5022 else
5023# endif
5024 {
5025 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5026
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005027 /* Set the maximum width, this also enables using
5028 * \n for line break. */
5029 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5030 0, 500);
5031
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005032 if (STRLEN(str) < sizeof(lpdi->szText)
5033 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005034 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005035 sizeof(lpdi->szText) - 1);
5036 else
5037 lpdi->lpszText = tt_text;
5038 }
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005042# ifdef FEAT_GUI_TABLINE
5043 case TCN_SELCHANGE:
5044 if (gui_mch_showing_tabline()
5045 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005046 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005047 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005048 return 0L;
5049 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005050 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005051
5052 case NM_RCLICK:
5053 if (gui_mch_showing_tabline()
5054 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005055 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005056 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005057 return 0L;
5058 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005059 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005062# ifdef FEAT_GUI_TABLINE
5063 if (gui_mch_showing_tabline()
5064 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5065 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068 }
5069 break;
5070#endif
5071#if defined(MENUHINTS) && defined(FEAT_MENU)
5072 case WM_MENUSELECT:
5073 if (((UINT) HIWORD(wParam)
5074 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5075 == MF_HILITE
5076 && (State & CMDLINE) == 0)
5077 {
5078 UINT idButton;
5079 vimmenu_T *pMenu;
5080 static int did_menu_tip = FALSE;
5081
5082 if (did_menu_tip)
5083 {
5084 msg_clr_cmdline();
5085 setcursor();
5086 out_flush();
5087 did_menu_tip = FALSE;
5088 }
5089
5090 idButton = (UINT)LOWORD(wParam);
5091 pMenu = gui_mswin_find_menu(root_menu, idButton);
5092 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5093 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5094 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005095 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005097 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 setcursor();
5099 out_flush();
5100 did_menu_tip = TRUE;
5101 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005102 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 break;
5105#endif
5106 case WM_NCHITTEST:
5107 {
5108 LRESULT result;
5109 int x, y;
5110 int xPos = GET_X_LPARAM(lParam);
5111
5112 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5113 if (result == HTCLIENT)
5114 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005115#ifdef FEAT_GUI_TABLINE
5116 if (gui_mch_showing_tabline())
5117 {
5118 int yPos = GET_Y_LPARAM(lParam);
5119 RECT rct;
5120
5121 /* If the cursor is on the GUI tabline, don't process this
5122 * event */
5123 GetWindowRect(s_textArea, &rct);
5124 if (yPos < rct.top)
5125 return result;
5126 }
5127#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005128 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 xPos -= x;
5130
5131 if (xPos < 48) /* <VN> TODO should use system metric? */
5132 return HTBOTTOMLEFT;
5133 else
5134 return HTBOTTOMRIGHT;
5135 }
5136 else
5137 return result;
5138 }
5139 /* break; notreached */
5140
5141#ifdef FEAT_MBYTE_IME
5142 case WM_IME_NOTIFY:
5143 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5144 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005145 return 1L;
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 case WM_IME_COMPOSITION:
5148 if (!_OnImeComposition(hwnd, wParam, lParam))
5149 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005150 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151#endif
5152
5153 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005155 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
5157 _OnFindRepl();
5158 }
5159#endif
5160 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5161 }
5162
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005163 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164}
5165
5166/*
5167 * End of call-back routines
5168 */
5169
5170/* parent window, if specified with -P */
5171HWND vim_parent_hwnd = NULL;
5172
5173 static BOOL CALLBACK
5174FindWindowTitle(HWND hwnd, LPARAM lParam)
5175{
5176 char buf[2048];
5177 char *title = (char *)lParam;
5178
5179 if (GetWindowText(hwnd, buf, sizeof(buf)))
5180 {
5181 if (strstr(buf, title) != NULL)
5182 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005183 /* Found it. Store the window ref. and quit searching if MDI
5184 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005186 if (vim_parent_hwnd != NULL)
5187 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 }
5190 return TRUE; /* continue searching */
5191}
5192
5193/*
5194 * Invoked for '-P "title"' argument: search for parent application to open
5195 * our window in.
5196 */
5197 void
5198gui_mch_set_parent(char *title)
5199{
5200 EnumWindows(FindWindowTitle, (LPARAM)title);
5201 if (vim_parent_hwnd == NULL)
5202 {
5203 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5204 mch_exit(2);
5205 }
5206}
5207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 static void
5210ole_error(char *arg)
5211{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005212 char buf[IOSIZE];
5213
5214 /* Can't use EMSG() here, we have not finished initialisation yet. */
5215 vim_snprintf(buf, IOSIZE,
5216 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5217 arg);
5218 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222/*
5223 * Parse the GUI related command-line arguments. Any arguments used are
5224 * deleted from argv, and *argc is decremented accordingly. This is called
5225 * when vim is started, whether or not the GUI has been started.
5226 */
5227 void
5228gui_mch_prepare(int *argc, char **argv)
5229{
5230 int silent = FALSE;
5231 int idx;
5232
5233 /* Check for special OLE command line parameters */
5234 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5235 {
5236 /* Check for a "-silent" argument first. */
5237 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5238 && (argv[2][0] == '-' || argv[2][0] == '/'))
5239 {
5240 silent = TRUE;
5241 idx = 2;
5242 }
5243 else
5244 idx = 1;
5245
5246 /* Register Vim as an OLE Automation server */
5247 if (STRICMP(argv[idx] + 1, "register") == 0)
5248 {
5249#ifdef FEAT_OLE
5250 RegisterMe(silent);
5251 mch_exit(0);
5252#else
5253 if (!silent)
5254 ole_error("register");
5255 mch_exit(2);
5256#endif
5257 }
5258
5259 /* Unregister Vim as an OLE Automation server */
5260 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5261 {
5262#ifdef FEAT_OLE
5263 UnregisterMe(!silent);
5264 mch_exit(0);
5265#else
5266 if (!silent)
5267 ole_error("unregister");
5268 mch_exit(2);
5269#endif
5270 }
5271
5272 /* Ignore an -embedding argument. It is only relevant if the
5273 * application wants to treat the case when it is started manually
5274 * differently from the case where it is started via automation (and
5275 * we don't).
5276 */
5277 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5278 {
5279#ifdef FEAT_OLE
5280 *argc = 1;
5281#else
5282 ole_error("embedding");
5283 mch_exit(2);
5284#endif
5285 }
5286 }
5287
5288#ifdef FEAT_OLE
5289 {
5290 int bDoRestart = FALSE;
5291
5292 InitOLE(&bDoRestart);
5293 /* automatically exit after registering */
5294 if (bDoRestart)
5295 mch_exit(0);
5296 }
5297#endif
5298
5299#ifdef FEAT_NETBEANS_INTG
5300 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005301 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 int arg;
5303
5304 for (arg = 1; arg < *argc; arg++)
5305 if (strncmp("-nb", argv[arg], 3) == 0)
5306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 netbeansArg = argv[arg];
5308 mch_memmove(&argv[arg], &argv[arg + 1],
5309 (--*argc - arg) * sizeof(char *));
5310 argv[*argc] = NULL;
5311 break; /* enough? */
5312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
5314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315}
5316
5317/*
5318 * Initialise the GUI. Create all the windows, set up all the call-backs
5319 * etc.
5320 */
5321 int
5322gui_mch_init(void)
5323{
5324 const char szVimWndClass[] = VIM_CLASS;
5325 const char szTextAreaClass[] = "VimTextArea";
5326 WNDCLASS wndclass;
5327#ifdef FEAT_MBYTE
5328 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005329 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 WNDCLASSW wndclassw;
5331#endif
5332#ifdef GLOBAL_IME
5333 ATOM atom;
5334#endif
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 /* Return here if the window was already opened (happens when
5337 * gui_mch_dialog() is called early). */
5338 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005339 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 /*
5342 * Load the tearoff bitmap
5343 */
5344#ifdef FEAT_TEAROFF
5345 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5346#endif
5347
5348 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5349 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5350#ifdef FEAT_MENU
5351 gui.menu_height = 0; /* Windows takes care of this */
5352#endif
5353 gui.border_width = 0;
5354
5355 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5356
5357#ifdef FEAT_MBYTE
5358 /* First try using the wide version, so that we can use any title.
5359 * Otherwise only characters in the active codepage will work. */
5360 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5361 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005362 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 wndclassw.lpfnWndProc = _WndProc;
5364 wndclassw.cbClsExtra = 0;
5365 wndclassw.cbWndExtra = 0;
5366 wndclassw.hInstance = s_hinst;
5367 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5368 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5369 wndclassw.hbrBackground = s_brush;
5370 wndclassw.lpszMenuName = NULL;
5371 wndclassw.lpszClassName = szVimWndClassW;
5372
5373 if ((
5374#ifdef GLOBAL_IME
5375 atom =
5376#endif
5377 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 else
5380 wide_WindowProc = TRUE;
5381 }
5382
5383 if (!wide_WindowProc)
5384#endif
5385
5386 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5387 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005388 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 wndclass.lpfnWndProc = _WndProc;
5390 wndclass.cbClsExtra = 0;
5391 wndclass.cbWndExtra = 0;
5392 wndclass.hInstance = s_hinst;
5393 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5394 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5395 wndclass.hbrBackground = s_brush;
5396 wndclass.lpszMenuName = NULL;
5397 wndclass.lpszClassName = szVimWndClass;
5398
5399 if ((
5400#ifdef GLOBAL_IME
5401 atom =
5402#endif
5403 RegisterClass(&wndclass)) == 0)
5404 return FAIL;
5405 }
5406
5407 if (vim_parent_hwnd != NULL)
5408 {
5409#ifdef HAVE_TRY_EXCEPT
5410 __try
5411 {
5412#endif
5413 /* Open inside the specified parent window.
5414 * TODO: last argument should point to a CLIENTCREATESTRUCT
5415 * structure. */
5416 s_hwnd = CreateWindowEx(
5417 WS_EX_MDICHILD,
5418 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005419 WS_OVERLAPPEDWINDOW | WS_CHILD
5420 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5422 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5423 100, /* Any value will do */
5424 100, /* Any value will do */
5425 vim_parent_hwnd, NULL,
5426 s_hinst, NULL);
5427#ifdef HAVE_TRY_EXCEPT
5428 }
5429 __except(EXCEPTION_EXECUTE_HANDLER)
5430 {
5431 /* NOP */
5432 }
5433#endif
5434 if (s_hwnd == NULL)
5435 {
5436 EMSG(_("E672: Unable to open window inside MDI application"));
5437 mch_exit(2);
5438 }
5439 }
5440 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005441 {
5442 /* If the provided windowid is not valid reset it to zero, so that it
5443 * is ignored and we open our own window. */
5444 if (IsWindow((HWND)win_socket_id) <= 0)
5445 win_socket_id = 0;
5446
5447 /* Create a window. If win_socket_id is not zero without border and
5448 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005450 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005451 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5452 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005453 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5454 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5455 100, /* Any value will do */
5456 100, /* Any value will do */
5457 NULL, NULL,
5458 s_hinst, NULL);
5459 if (s_hwnd != NULL && win_socket_id != 0)
5460 {
5461 SetParent(s_hwnd, (HWND)win_socket_id);
5462 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5463 }
5464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466 if (s_hwnd == NULL)
5467 return FAIL;
5468
5469#ifdef GLOBAL_IME
5470 global_ime_init(atom, s_hwnd);
5471#endif
5472#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5473 dyn_imm_load();
5474#endif
5475
5476 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005477#ifdef FEAT_MBYTE
5478 if (wide_WindowProc)
5479 {
5480 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5481 {
5482 wndclassw.style = CS_OWNDC;
5483 wndclassw.lpfnWndProc = _TextAreaWndProc;
5484 wndclassw.cbClsExtra = 0;
5485 wndclassw.cbWndExtra = 0;
5486 wndclassw.hInstance = s_hinst;
5487 wndclassw.hIcon = NULL;
5488 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5489 wndclassw.hbrBackground = NULL;
5490 wndclassw.lpszMenuName = NULL;
5491 wndclassw.lpszClassName = szTextAreaClassW;
5492
5493 if (RegisterClassW(&wndclassw) == 0)
5494 return FAIL;
5495 }
5496 }
5497 else
5498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5500 {
5501 wndclass.style = CS_OWNDC;
5502 wndclass.lpfnWndProc = _TextAreaWndProc;
5503 wndclass.cbClsExtra = 0;
5504 wndclass.cbWndExtra = 0;
5505 wndclass.hInstance = s_hinst;
5506 wndclass.hIcon = NULL;
5507 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5508 wndclass.hbrBackground = NULL;
5509 wndclass.lpszMenuName = NULL;
5510 wndclass.lpszClassName = szTextAreaClass;
5511
5512 if (RegisterClass(&wndclass) == 0)
5513 return FAIL;
5514 }
5515 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005516 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 szTextAreaClass, "Vim text area",
5518 WS_CHILD | WS_VISIBLE, 0, 0,
5519 100, /* Any value will do for now */
5520 100, /* Any value will do for now */
5521 s_hwnd, NULL,
5522 s_hinst, NULL);
5523
5524 if (s_textArea == NULL)
5525 return FAIL;
5526
Bram Moolenaar20321902016-02-17 12:30:17 +01005527#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005528 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5529 {
5530 HANDLE hIcon = NULL;
5531
5532 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005533 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005534 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005535#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537#ifdef FEAT_MENU
5538 s_menuBar = CreateMenu();
5539#endif
5540 s_hdc = GetDC(s_textArea);
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544 /* Do we need to bother with this? */
5545 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5546
5547 /* Get background/foreground colors from the system */
5548 gui_mch_def_colors();
5549
5550 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5551 * file) */
5552 set_normal_colors();
5553
5554 /*
5555 * Check that none of the colors are the same as the background color.
5556 * Then store the current values as the defaults.
5557 */
5558 gui_check_colors();
5559 gui.def_norm_pixel = gui.norm_pixel;
5560 gui.def_back_pixel = gui.back_pixel;
5561
5562 /* Get the colors for the highlight groups (gui_check_colors() might have
5563 * changed them) */
5564 highlight_gui_started();
5565
5566 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005567 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005569 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 /*
5572 * Set up for Intellimouse processing
5573 */
5574 init_mouse_wheel();
5575
5576 /*
5577 * compute a couple of metrics used for the dialogs
5578 */
5579 get_dialog_font_metrics();
5580#ifdef FEAT_TOOLBAR
5581 /*
5582 * Create the toolbar
5583 */
5584 initialise_toolbar();
5585#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005586#ifdef FEAT_GUI_TABLINE
5587 /*
5588 * Create the tabline
5589 */
5590 initialise_tabline();
5591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592#ifdef MSWIN_FIND_REPLACE
5593 /*
5594 * Initialise the dialog box stuff
5595 */
5596 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5597
5598 /* Initialise the struct */
5599 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005600 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005602 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5604 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5605 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005606# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005607 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5608 s_findrep_struct_w.lpstrFindWhat =
5609 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5610 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5611 s_findrep_struct_w.lpstrReplaceWith =
5612 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5613 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5614 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5615 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005619#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005620# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5621/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5622# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005623# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005624# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005625# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005626 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005627 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005628#endif
5629
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005630#ifdef FEAT_RENDER_OPTIONS
5631 if (p_rop)
5632 (void)gui_mch_set_rendering_options(p_rop);
5633#endif
5634
Bram Moolenaar748bf032005-02-02 23:04:36 +00005635theend:
5636 /* Display any pending error messages */
5637 display_errors();
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 return OK;
5640}
5641
5642/*
5643 * Get the size of the screen, taking position on multiple monitors into
5644 * account (if supported).
5645 */
5646 static void
5647get_work_area(RECT *spi_rect)
5648{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005649 HMONITOR mon;
5650 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005652 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005653 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005654 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005656 moninfo.cbSize = sizeof(MONITORINFO);
5657 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005659 *spi_rect = moninfo.rcWork;
5660 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
5662 }
5663 /* this is the old method... */
5664 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5665}
5666
5667/*
5668 * Set the size of the window to the given width and height in pixels.
5669 */
5670 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005671gui_mch_set_shellsize(
5672 int width,
5673 int height,
5674 int min_width UNUSED,
5675 int min_height UNUSED,
5676 int base_width UNUSED,
5677 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005678 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 RECT workarea_rect;
5681 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 WINDOWPLACEMENT wndpl;
5683
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005684 /* Try to keep window completely on screen. */
5685 /* Get position of the screen work area. This is the part that is not
5686 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 get_work_area(&workarea_rect);
5688
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005689 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005690 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 wndpl.length = sizeof(WINDOWPLACEMENT);
5692 GetWindowPlacement(s_hwnd, &wndpl);
5693
5694 /* Resizing a maximized window looks very strange, unzoom it first.
5695 * But don't do it when still starting up, it may have been requested in
5696 * the shortcut. */
5697 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5698 {
5699 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5700 /* Need to get the settings of the normal window. */
5701 GetWindowPlacement(s_hwnd, &wndpl);
5702 }
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005705 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005706 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005707 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005708 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 + GetSystemMetrics(SM_CYCAPTION)
5710#ifdef FEAT_MENU
5711 + gui_mswin_get_menu_height(FALSE)
5712#endif
5713 ;
5714
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005715 /* The following should take care of keeping Vim on the same monitor, no
5716 * matter if the secondary monitor is left or right of the primary
5717 * monitor. */
5718 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5719 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005720
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005721 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005722 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005723 && wndpl.rcNormalPosition.right > workarea_rect.right)
5724 OffsetRect(&wndpl.rcNormalPosition,
5725 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005727 if ((direction & RESIZE_HOR)
5728 && wndpl.rcNormalPosition.left < workarea_rect.left)
5729 OffsetRect(&wndpl.rcNormalPosition,
5730 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaarafa24992006-03-27 20:58:26 +00005732 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005733 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5734 OffsetRect(&wndpl.rcNormalPosition,
5735 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005737 if ((direction & RESIZE_VERT)
5738 && wndpl.rcNormalPosition.top < workarea_rect.top)
5739 OffsetRect(&wndpl.rcNormalPosition,
5740 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 /* set window position - we should use SetWindowPlacement rather than
5743 * SetWindowPos as the MSDN docs say the coord systems returned by
5744 * these two are not compatible. */
5745 SetWindowPlacement(s_hwnd, &wndpl);
5746
5747 SetActiveWindow(s_hwnd);
5748 SetFocus(s_hwnd);
5749
5750#ifdef FEAT_MENU
5751 /* Menu may wrap differently now */
5752 gui_mswin_get_menu_height(!gui.starting);
5753#endif
5754}
5755
5756
5757 void
5758gui_mch_set_scrollbar_thumb(
5759 scrollbar_T *sb,
5760 long val,
5761 long size,
5762 long max)
5763{
5764 SCROLLINFO info;
5765
5766 sb->scroll_shift = 0;
5767 while (max > 32767)
5768 {
5769 max = (max + 1) >> 1;
5770 val >>= 1;
5771 size >>= 1;
5772 ++sb->scroll_shift;
5773 }
5774
5775 if (sb->scroll_shift > 0)
5776 ++size;
5777
5778 info.cbSize = sizeof(info);
5779 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5780 info.nPos = val;
5781 info.nMin = 0;
5782 info.nMax = max;
5783 info.nPage = size;
5784 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5785}
5786
5787
5788/*
5789 * Set the current text font.
5790 */
5791 void
5792gui_mch_set_font(GuiFont font)
5793{
5794 gui.currFont = font;
5795}
5796
5797
5798/*
5799 * Set the current text foreground color.
5800 */
5801 void
5802gui_mch_set_fg_color(guicolor_T color)
5803{
5804 gui.currFgColor = color;
5805}
5806
5807/*
5808 * Set the current text background color.
5809 */
5810 void
5811gui_mch_set_bg_color(guicolor_T color)
5812{
5813 gui.currBgColor = color;
5814}
5815
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005816/*
5817 * Set the current text special color.
5818 */
5819 void
5820gui_mch_set_sp_color(guicolor_T color)
5821{
5822 gui.currSpColor = color;
5823}
5824
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005825#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826/*
5827 * Multi-byte handling, originally by Sung-Hoon Baek.
5828 * First static functions (no prototypes generated).
5829 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005830# ifdef _MSC_VER
5831# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5832# endif
5833# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 * handle WM_IME_NOTIFY message
5837 */
5838 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005839_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 LRESULT lResult = 0;
5842 HIMC hImc;
5843
5844 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5845 return lResult;
5846 switch (dwCommand)
5847 {
5848 case IMN_SETOPENSTATUS:
5849 if (pImmGetOpenStatus(hImc))
5850 {
5851 pImmSetCompositionFont(hImc, &norm_logfont);
5852 im_set_position(gui.row, gui.col);
5853
5854 /* Disable langmap */
5855 State &= ~LANGMAP;
5856 if (State & INSERT)
5857 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005858#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 /* Unshown 'keymap' in status lines */
5860 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5861 {
5862 /* Save cursor position */
5863 int old_row = gui.row;
5864 int old_col = gui.col;
5865
5866 // This must be called here before
5867 // status_redraw_curbuf(), otherwise the mode
5868 // message may appear in the wrong position.
5869 showmode();
5870 status_redraw_curbuf();
5871 update_screen(0);
5872 /* Restore cursor position */
5873 gui.row = old_row;
5874 gui.col = old_col;
5875 }
5876#endif
5877 }
5878 }
5879 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005880 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 lResult = 0;
5882 break;
5883 }
5884 pImmReleaseContext(hWnd, hImc);
5885 return lResult;
5886}
5887
5888 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005889_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 char_u *ret;
5892 int len;
5893
5894 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5895 return 0;
5896
5897 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5898 if (ret != NULL)
5899 {
5900 add_to_input_buf_csi(ret, len);
5901 vim_free(ret);
5902 return 1;
5903 }
5904 return 0;
5905}
5906
5907/*
5908 * get the current composition string, in UCS-2; *lenp is the number of
5909 * *lenp is the number of Unicode characters.
5910 */
5911 static short_u *
5912GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5913{
5914 LONG ret;
5915 LPWSTR wbuf = NULL;
5916 char_u *buf;
5917
5918 if (!pImmGetContext)
5919 return NULL; /* no imm32.dll */
5920
5921 /* Try Unicode; this'll always work on NT regardless of codepage. */
5922 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5923 if (ret == 0)
5924 return NULL; /* empty */
5925
5926 if (ret > 0)
5927 {
5928 /* Allocate the requested buffer plus space for the NUL character. */
5929 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5930 if (wbuf != NULL)
5931 {
5932 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5933 *lenp = ret / sizeof(WCHAR);
5934 }
5935 return (short_u *)wbuf;
5936 }
5937
5938 /* ret < 0; we got an error, so try the ANSI version. This'll work
5939 * on 9x/ME, but only if the codepage happens to be set to whatever
5940 * we're inputting. */
5941 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5942 if (ret <= 0)
5943 return NULL; /* empty or error */
5944
5945 buf = alloc(ret);
5946 if (buf == NULL)
5947 return NULL;
5948 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5949
5950 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005951 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 vim_free(buf);
5953
5954 return (short_u *)wbuf;
5955}
5956
5957/*
5958 * void GetResultStr()
5959 *
5960 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5961 * get complete composition string
5962 */
5963 static char_u *
5964GetResultStr(HWND hwnd, int GCS, int *lenp)
5965{
5966 HIMC hIMC; /* Input context handle. */
5967 short_u *buf = NULL;
5968 char_u *convbuf = NULL;
5969
5970 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5971 return NULL;
5972
5973 /* Reads in the composition string. */
5974 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5975 if (buf == NULL)
5976 return NULL;
5977
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005978 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 pImmReleaseContext(hwnd, hIMC);
5980 vim_free(buf);
5981 return convbuf;
5982}
5983#endif
5984
5985/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005986#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
5988/*
5989 * set font to IM.
5990 */
5991 void
5992im_set_font(LOGFONT *lf)
5993{
5994 HIMC hImc;
5995
5996 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5997 {
5998 pImmSetCompositionFont(hImc, lf);
5999 pImmReleaseContext(s_hwnd, hImc);
6000 }
6001}
6002
6003/*
6004 * Notify cursor position to IM.
6005 */
6006 void
6007im_set_position(int row, int col)
6008{
6009 HIMC hImc;
6010
6011 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6012 {
6013 COMPOSITIONFORM cfs;
6014
6015 cfs.dwStyle = CFS_POINT;
6016 cfs.ptCurrentPos.x = FILL_X(col);
6017 cfs.ptCurrentPos.y = FILL_Y(row);
6018 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6019 pImmSetCompositionWindow(hImc, &cfs);
6020
6021 pImmReleaseContext(s_hwnd, hImc);
6022 }
6023}
6024
6025/*
6026 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6027 */
6028 void
6029im_set_active(int active)
6030{
6031 HIMC hImc;
6032 static HIMC hImcOld = (HIMC)0;
6033
6034 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6035 {
6036 if (p_imdisable)
6037 {
6038 if (hImcOld == (HIMC)0)
6039 {
6040 hImcOld = pImmGetContext(s_hwnd);
6041 if (hImcOld)
6042 pImmAssociateContext(s_hwnd, (HIMC)0);
6043 }
6044 active = FALSE;
6045 }
6046 else if (hImcOld != (HIMC)0)
6047 {
6048 pImmAssociateContext(s_hwnd, hImcOld);
6049 hImcOld = (HIMC)0;
6050 }
6051
6052 hImc = pImmGetContext(s_hwnd);
6053 if (hImc)
6054 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006055 /*
6056 * for Korean ime
6057 */
6058 HKL hKL = GetKeyboardLayout(0);
6059
6060 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6061 {
6062 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6063 static BOOL bSaved = FALSE;
6064
6065 if (active)
6066 {
6067 /* if we have a saved conversion status, restore it */
6068 if (bSaved)
6069 pImmSetConversionStatus(hImc, dwConversionSaved,
6070 dwSentenceSaved);
6071 bSaved = FALSE;
6072 }
6073 else
6074 {
6075 /* save conversion status and disable korean */
6076 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6077 &dwSentenceSaved))
6078 {
6079 bSaved = TRUE;
6080 pImmSetConversionStatus(hImc,
6081 dwConversionSaved & ~(IME_CMODE_NATIVE
6082 | IME_CMODE_FULLSHAPE),
6083 dwSentenceSaved);
6084 }
6085 }
6086 }
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 pImmSetOpenStatus(hImc, active);
6089 pImmReleaseContext(s_hwnd, hImc);
6090 }
6091 }
6092}
6093
6094/*
6095 * Get IM status. When IM is on, return not 0. Else return 0.
6096 */
6097 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006098im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 int status = 0;
6101 HIMC hImc;
6102
6103 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6104 {
6105 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6106 pImmReleaseContext(s_hwnd, hImc);
6107 }
6108 return status;
6109}
6110
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006111#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6114/* Win32 with GLOBAL IME */
6115
6116/*
6117 * Notify cursor position to IM.
6118 */
6119 void
6120im_set_position(int row, int col)
6121{
6122 /* Win32 with GLOBAL IME */
6123 POINT p;
6124
6125 p.x = FILL_X(col);
6126 p.y = FILL_Y(row);
6127 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6128 global_ime_set_position(&p);
6129}
6130
6131/*
6132 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6133 */
6134 void
6135im_set_active(int active)
6136{
6137 global_ime_set_status(active);
6138}
6139
6140/*
6141 * Get IM status. When IM is on, return not 0. Else return 0.
6142 */
6143 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006144im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 return global_ime_get_status();
6147}
6148#endif
6149
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006150#ifdef FEAT_MBYTE
6151/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006152 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006153 */
6154 static void
6155latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6156{
6157 int c;
6158
Bram Moolenaarca003e12006-03-17 23:19:38 +00006159 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006160 {
6161 c = *text++;
6162 switch (c)
6163 {
6164 case 0xa4: c = 0x20ac; break; /* euro */
6165 case 0xa6: c = 0x0160; break; /* S hat */
6166 case 0xa8: c = 0x0161; break; /* S -hat */
6167 case 0xb4: c = 0x017d; break; /* Z hat */
6168 case 0xb8: c = 0x017e; break; /* Z -hat */
6169 case 0xbc: c = 0x0152; break; /* OE */
6170 case 0xbd: c = 0x0153; break; /* oe */
6171 case 0xbe: c = 0x0178; break; /* Y */
6172 }
6173 *unicodebuf++ = c;
6174 }
6175}
6176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178#ifdef FEAT_RIGHTLEFT
6179/*
6180 * What is this for? In the case where you are using Win98 or Win2K or later,
6181 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6182 * reverses the string sent to the TextOut... family. This sucks, because we
6183 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6184 * way to tell Windblows not to do this!
6185 *
6186 * The short of it is that this 'RevOut' only gets called if you are running
6187 * one of the new, "improved" MS OSes, and only if you are running in
6188 * 'rightleft' mode. It makes display take *slightly* longer, but not
6189 * noticeably so.
6190 */
6191 static void
6192RevOut( HDC s_hdc,
6193 int col,
6194 int row,
6195 UINT foptions,
6196 CONST RECT *pcliprect,
6197 LPCTSTR text,
6198 UINT len,
6199 CONST INT *padding)
6200{
6201 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006203 for (ix = 0; ix < (int)len; ++ix)
6204 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6205 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206}
6207#endif
6208
Bram Moolenaar92467d32017-12-05 13:22:16 +01006209 static void
6210draw_line(
6211 int x1,
6212 int y1,
6213 int x2,
6214 int y2,
6215 COLORREF color)
6216{
6217#if defined(FEAT_DIRECTX)
6218 if (IS_ENABLE_DIRECTX())
6219 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6220 else
6221#endif
6222 {
6223 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6224 HPEN old_pen = SelectObject(s_hdc, hpen);
6225 MoveToEx(s_hdc, x1, y1, NULL);
6226 /* Note: LineTo() excludes the last pixel in the line. */
6227 LineTo(s_hdc, x2, y2);
6228 DeleteObject(SelectObject(s_hdc, old_pen));
6229 }
6230}
6231
6232 static void
6233set_pixel(
6234 int x,
6235 int y,
6236 COLORREF color)
6237{
6238#if defined(FEAT_DIRECTX)
6239 if (IS_ENABLE_DIRECTX())
6240 DWriteContext_SetPixel(s_dwc, x, y, color);
6241 else
6242#endif
6243 SetPixel(s_hdc, x, y, color);
6244}
6245
6246 static void
6247fill_rect(
6248 const RECT *rcp,
6249 HBRUSH hbr,
6250 COLORREF color)
6251{
6252#if defined(FEAT_DIRECTX)
6253 if (IS_ENABLE_DIRECTX())
6254 DWriteContext_FillRect(s_dwc, rcp, color);
6255 else
6256#endif
6257 {
6258 HBRUSH hbr2;
6259
6260 if (hbr == NULL)
6261 hbr2 = CreateSolidBrush(color);
6262 else
6263 hbr2 = hbr;
6264 FillRect(s_hdc, rcp, hbr2);
6265 if (hbr == NULL)
6266 DeleteBrush(hbr2);
6267 }
6268}
6269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 void
6271gui_mch_draw_string(
6272 int row,
6273 int col,
6274 char_u *text,
6275 int len,
6276 int flags)
6277{
6278 static int *padding = NULL;
6279 static int pad_size = 0;
6280 int i;
6281 const RECT *pcliprect = NULL;
6282 UINT foptions = 0;
6283#ifdef FEAT_MBYTE
6284 static WCHAR *unicodebuf = NULL;
6285 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006286 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 int n = 0;
6288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 int y;
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 /*
6292 * Italic and bold text seems to have an extra row of pixels at the bottom
6293 * (below where the bottom of the character should be). If we draw the
6294 * characters with a solid background, the top row of pixels in the
6295 * character below will be overwritten. We can fix this by filling in the
6296 * background ourselves, to the correct character proportions, and then
6297 * writing the character in transparent mode. Still have a problem when
6298 * the character is "_", which gets written on to the character below.
6299 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6300 * pixel in their slots, which fixes the problem with the bottom row of
6301 * pixels. We still need this code because otherwise the top row of pixels
6302 * becomes a problem. - webb.
6303 */
6304 static HBRUSH hbr_cache[2] = {NULL, NULL};
6305 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6306 static int brush_lru = 0;
6307 HBRUSH hbr;
6308 RECT rc;
6309
6310 if (!(flags & DRAW_TRANSP))
6311 {
6312 /*
6313 * Clear background first.
6314 * Note: FillRect() excludes right and bottom of rectangle.
6315 */
6316 rc.left = FILL_X(col);
6317 rc.top = FILL_Y(row);
6318#ifdef FEAT_MBYTE
6319 if (has_mbyte)
6320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006322 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
6324 else
6325#endif
6326 rc.right = FILL_X(col + len);
6327 rc.bottom = FILL_Y(row + 1);
6328
6329 /* Cache the created brush, that saves a lot of time. We need two:
6330 * one for cursor background and one for the normal background. */
6331 if (gui.currBgColor == brush_color[0])
6332 {
6333 hbr = hbr_cache[0];
6334 brush_lru = 1;
6335 }
6336 else if (gui.currBgColor == brush_color[1])
6337 {
6338 hbr = hbr_cache[1];
6339 brush_lru = 0;
6340 }
6341 else
6342 {
6343 if (hbr_cache[brush_lru] != NULL)
6344 DeleteBrush(hbr_cache[brush_lru]);
6345 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6346 brush_color[brush_lru] = gui.currBgColor;
6347 hbr = hbr_cache[brush_lru];
6348 brush_lru = !brush_lru;
6349 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006350
Bram Moolenaar92467d32017-12-05 13:22:16 +01006351 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
6353 SetBkMode(s_hdc, TRANSPARENT);
6354
6355 /*
6356 * When drawing block cursor, prevent inverted character spilling
6357 * over character cell (can happen with bold/italic)
6358 */
6359 if (flags & DRAW_CURSOR)
6360 {
6361 pcliprect = &rc;
6362 foptions = ETO_CLIPPED;
6363 }
6364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 SetTextColor(s_hdc, gui.currFgColor);
6366 SelectFont(s_hdc, gui.currFont);
6367
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006368#ifdef FEAT_DIRECTX
6369 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006370 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006371#endif
6372
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6374 {
6375 vim_free(padding);
6376 pad_size = Columns;
6377
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006378 /* Don't give an out-of-memory message here, it would call us
6379 * recursively. */
6380 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 if (padding != NULL)
6382 for (i = 0; i < pad_size; i++)
6383 padding[i] = gui.char_width;
6384 }
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 /*
6387 * We have to provide the padding argument because italic and bold versions
6388 * of fixed-width fonts are often one pixel or so wider than their normal
6389 * versions.
6390 * No check for DRAW_BOLD, Windows will have done it already.
6391 */
6392
6393#ifdef FEAT_MBYTE
6394 /* Check if there are any UTF-8 characters. If not, use normal text
6395 * output to speed up output. */
6396 if (enc_utf8)
6397 for (n = 0; n < len; ++n)
6398 if (text[n] >= 0x80)
6399 break;
6400
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006401#if defined(FEAT_DIRECTX)
6402 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6403 * required that unicode drawing routine, currently. So this forces it
6404 * enabled. */
6405 if (enc_utf8 && IS_ENABLE_DIRECTX())
6406 n = 0; /* Keep n < len, to enter block for unicode. */
6407#endif
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006410 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006412 if ((enc_utf8
6413 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6414 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 && (unicodebuf == NULL || len > unibuflen))
6416 {
6417 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006418 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006421 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
6423 unibuflen = len;
6424 }
6425
6426 if (enc_utf8 && n < len && unicodebuf != NULL)
6427 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006428 /* Output UTF-8 characters. Composing characters should be
6429 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006430 int i;
6431 int wlen; /* string length in words */
6432 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 int cells; /* cell width of string up to composing char */
6434 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006435 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006437 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006438 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006440 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006442 c = utf_ptr2char(text + i);
6443 if (c >= 0x10000)
6444 {
6445 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006446 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6447 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006448 }
6449 else
6450 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006451 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006452 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006453
6454 if (utf_iscomposing(c))
6455 cw = 0;
6456 else
6457 {
6458 cw = utf_char2cells(c);
6459 if (cw > 2) /* don't use 4 for unprintable char */
6460 cw = 1;
6461 }
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (unicodepdy != NULL)
6464 {
6465 /* Use unicodepdy to make characters fit as we expect, even
6466 * when the font uses different widths (e.g., bold character
6467 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006468 if (c >= 0x10000)
6469 {
6470 unicodepdy[wlen - 2] = cw * gui.char_width;
6471 unicodepdy[wlen - 1] = 0;
6472 }
6473 else
6474 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006477 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006478 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006480#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006481 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006482 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006483 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006484 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006485 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006486 gui.char_width, gui.currFgColor,
6487 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006488 }
6489 else
6490#endif
6491 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6492 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 len = cells; /* used for underlining */
6494 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006495 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 {
6497 /* If we want to display codepage data, and the current CP is not the
6498 * ANSI one, we need to go via Unicode. */
6499 if (unicodebuf != NULL)
6500 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006501 if (enc_latin9)
6502 latin9_to_ucs(text, len, unicodebuf);
6503 else
6504 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 MB_PRECOMPOSED,
6506 (char *)text, len,
6507 (LPWSTR)unicodebuf, unibuflen);
6508 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006509 {
6510 /* Use unicodepdy to make characters fit as we expect, even
6511 * when the font uses different widths (e.g., bold character
6512 * is wider). */
6513 if (unicodepdy != NULL)
6514 {
6515 int i;
6516 int cw;
6517
6518 for (i = 0; i < len; ++i)
6519 {
6520 cw = utf_char2cells(unicodebuf[i]);
6521 if (cw > 2)
6522 cw = 1;
6523 unicodepdy[i] = cw * gui.char_width;
6524 }
6525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006527 foptions, pcliprect, unicodebuf, len, unicodepdy);
6528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
6530 }
6531 else
6532#endif
6533 {
6534#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006535 /* Windows will mess up RL text, so we have to draw it character by
6536 * character. Only do this if RL is on, since it's slow. */
6537 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6539 foptions, pcliprect, (char *)text, len, padding);
6540 else
6541#endif
6542 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6543 foptions, pcliprect, (char *)text, len, padding);
6544 }
6545
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006546 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (flags & DRAW_UNDERL)
6548 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 /* When p_linespace is 0, overwrite the bottom row of pixels.
6550 * Otherwise put the line just below the character. */
6551 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (p_linespace > 1)
6553 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006554 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006556
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006557 /* Strikethrough */
6558 if (flags & DRAW_STRIKE)
6559 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006560 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006561 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006562 }
6563
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006564 /* Undercurl */
6565 if (flags & DRAW_UNDERC)
6566 {
6567 int x;
6568 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006569 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006570
6571 y = FILL_Y(row + 1) - 1;
6572 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6573 {
6574 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006575 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006576 }
6577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578}
6579
6580
6581/*
6582 * Output routines.
6583 */
6584
6585/* Flush any output to the screen */
6586 void
6587gui_mch_flush(void)
6588{
6589# if defined(__BORLANDC__)
6590 /*
6591 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6592 * prototype declaration.
6593 * The compiler complains if __stdcall is not used in both declarations.
6594 */
6595 BOOL __stdcall GdiFlush(void);
6596# endif
6597
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006598#if defined(FEAT_DIRECTX)
6599 if (IS_ENABLE_DIRECTX())
6600 DWriteContext_Flush(s_dwc);
6601#endif
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 GdiFlush();
6604}
6605
6606 static void
6607clear_rect(RECT *rcp)
6608{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006609 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610}
6611
6612
Bram Moolenaarc716c302006-01-21 22:12:51 +00006613 void
6614gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6615{
6616 RECT workarea_rect;
6617
6618 get_work_area(&workarea_rect);
6619
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006620 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006621 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006622 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006623
6624 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6625 * the menubar for MSwin, we subtract it from the screen height, so that
6626 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006627 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006628 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006629 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006630 - GetSystemMetrics(SM_CYCAPTION)
6631#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006632 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006633#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006634 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006635}
6636
6637
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638#if defined(FEAT_MENU) || defined(PROTO)
6639/*
6640 * Add a sub menu to the menu bar.
6641 */
6642 void
6643gui_mch_add_menu(
6644 vimmenu_T *menu,
6645 int pos)
6646{
6647 vimmenu_T *parent = menu->parent;
6648
6649 menu->submenu_id = CreatePopupMenu();
6650 menu->id = s_menu_id++;
6651
6652 if (menu_is_menubar(menu->name))
6653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006655 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006657 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6658 {
6659 /* 'encoding' differs from active codepage: convert menu name
6660 * and use wide function */
6661 wn = enc_to_utf16(menu->name, NULL);
6662 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006664 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006666 infow.cbSize = sizeof(infow);
6667 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6668 | MIIM_SUBMENU;
6669 infow.dwItemData = (long_u)menu;
6670 infow.wID = menu->id;
6671 infow.fType = MFT_STRING;
6672 infow.dwTypeData = wn;
6673 infow.cch = (UINT)wcslen(wn);
6674 infow.hSubMenu = menu->submenu_id;
6675 InsertMenuItemW((parent == NULL)
6676 ? s_menuBar : parent->submenu_id,
6677 (UINT)pos, TRUE, &infow);
6678 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006682 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006684 {
6685 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006687 info.cbSize = sizeof(info);
6688 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6689 info.dwItemData = (long_u)menu;
6690 info.wID = menu->id;
6691 info.fType = MFT_STRING;
6692 info.dwTypeData = (LPTSTR)menu->name;
6693 info.cch = (UINT)STRLEN(menu->name);
6694 info.hSubMenu = menu->submenu_id;
6695 InsertMenuItem((parent == NULL)
6696 ? s_menuBar : parent->submenu_id,
6697 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699 }
6700
6701 /* Fix window size if menu may have wrapped */
6702 if (parent == NULL)
6703 gui_mswin_get_menu_height(!gui.starting);
6704#ifdef FEAT_TEAROFF
6705 else if (IsWindow(parent->tearoff_handle))
6706 rebuild_tearoff(parent);
6707#endif
6708}
6709
6710 void
6711gui_mch_show_popupmenu(vimmenu_T *menu)
6712{
6713 POINT mp;
6714
6715 (void)GetCursorPos((LPPOINT)&mp);
6716 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6717}
6718
6719 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006720gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 vimmenu_T *menu = gui_find_menu(path_name);
6723
6724 if (menu != NULL)
6725 {
6726 POINT p;
6727
6728 /* Find the position of the current cursor */
6729 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006730 if (mouse_pos)
6731 {
6732 int mx, my;
6733
6734 gui_mch_getmouse(&mx, &my);
6735 p.x += mx;
6736 p.y += my;
6737 }
6738 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006740 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6742 }
6743 msg_scroll = FALSE;
6744 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6745 }
6746}
6747
6748#if defined(FEAT_TEAROFF) || defined(PROTO)
6749/*
6750 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6751 * create it as a pseudo-"tearoff menu".
6752 */
6753 void
6754gui_make_tearoff(char_u *path_name)
6755{
6756 vimmenu_T *menu = gui_find_menu(path_name);
6757
6758 /* Found the menu, so tear it off. */
6759 if (menu != NULL)
6760 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6761}
6762#endif
6763
6764/*
6765 * Add a menu item to a menu
6766 */
6767 void
6768gui_mch_add_menu_item(
6769 vimmenu_T *menu,
6770 int idx)
6771{
6772 vimmenu_T *parent = menu->parent;
6773
6774 menu->id = s_menu_id++;
6775 menu->submenu_id = NULL;
6776
6777#ifdef FEAT_TEAROFF
6778 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6779 {
6780 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6781 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6782 }
6783 else
6784#endif
6785#ifdef FEAT_TOOLBAR
6786 if (menu_is_toolbar(parent->name))
6787 {
6788 TBBUTTON newtb;
6789
6790 vim_memset(&newtb, 0, sizeof(newtb));
6791 if (menu_is_separator(menu->name))
6792 {
6793 newtb.iBitmap = 0;
6794 newtb.fsStyle = TBSTYLE_SEP;
6795 }
6796 else
6797 {
6798 newtb.iBitmap = get_toolbar_bitmap(menu);
6799 newtb.fsStyle = TBSTYLE_BUTTON;
6800 }
6801 newtb.idCommand = menu->id;
6802 newtb.fsState = TBSTATE_ENABLED;
6803 newtb.iString = 0;
6804 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6805 (LPARAM)&newtb);
6806 menu->submenu_id = (HMENU)-1;
6807 }
6808 else
6809#endif
6810 {
6811#ifdef FEAT_MBYTE
6812 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6815 {
6816 /* 'encoding' differs from active codepage: convert menu item name
6817 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006818 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (wn != NULL)
6820 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006821 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 (menu_is_separator(menu->name)
6823 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6824 (UINT)menu->id, wn);
6825 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827 }
6828 if (wn == NULL)
6829#endif
6830 InsertMenu(parent->submenu_id, (UINT)idx,
6831 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6832 | MF_BYPOSITION,
6833 (UINT)menu->id, (LPCTSTR)menu->name);
6834#ifdef FEAT_TEAROFF
6835 if (IsWindow(parent->tearoff_handle))
6836 rebuild_tearoff(parent);
6837#endif
6838 }
6839}
6840
6841/*
6842 * Destroy the machine specific menu widget.
6843 */
6844 void
6845gui_mch_destroy_menu(vimmenu_T *menu)
6846{
6847#ifdef FEAT_TOOLBAR
6848 /*
6849 * is this a toolbar button?
6850 */
6851 if (menu->submenu_id == (HMENU)-1)
6852 {
6853 int iButton;
6854
6855 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6856 (WPARAM)menu->id, 0);
6857 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6858 }
6859 else
6860#endif
6861 {
6862 if (menu->parent != NULL
6863 && menu_is_popup(menu->parent->dname)
6864 && menu->parent->submenu_id != NULL)
6865 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6866 else
6867 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6868 if (menu->submenu_id != NULL)
6869 DestroyMenu(menu->submenu_id);
6870#ifdef FEAT_TEAROFF
6871 if (IsWindow(menu->tearoff_handle))
6872 DestroyWindow(menu->tearoff_handle);
6873 if (menu->parent != NULL
6874 && menu->parent->children != NULL
6875 && IsWindow(menu->parent->tearoff_handle))
6876 {
6877 /* This menu must not show up when rebuilding the tearoff window. */
6878 menu->modes = 0;
6879 rebuild_tearoff(menu->parent);
6880 }
6881#endif
6882 }
6883}
6884
6885#ifdef FEAT_TEAROFF
6886 static void
6887rebuild_tearoff(vimmenu_T *menu)
6888{
6889 /*hackish*/
6890 char_u tbuf[128];
6891 RECT trect;
6892 RECT rct;
6893 RECT roct;
6894 int x, y;
6895
6896 HWND thwnd = menu->tearoff_handle;
6897
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006898 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (GetWindowRect(thwnd, &trect)
6900 && GetWindowRect(s_hwnd, &rct)
6901 && GetClientRect(s_hwnd, &roct))
6902 {
6903 x = trect.left - rct.left;
6904 y = (trect.top - rct.bottom + roct.bottom);
6905 }
6906 else
6907 {
6908 x = y = 0xffffL;
6909 }
6910 DestroyWindow(thwnd);
6911 if (menu->children != NULL)
6912 {
6913 gui_mch_tearoff(tbuf, menu, x, y);
6914 if (IsWindow(menu->tearoff_handle))
6915 (void) SetWindowPos(menu->tearoff_handle,
6916 NULL,
6917 (int)trect.left,
6918 (int)trect.top,
6919 0, 0,
6920 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6921 }
6922}
6923#endif /* FEAT_TEAROFF */
6924
6925/*
6926 * Make a menu either grey or not grey.
6927 */
6928 void
6929gui_mch_menu_grey(
6930 vimmenu_T *menu,
6931 int grey)
6932{
6933#ifdef FEAT_TOOLBAR
6934 /*
6935 * is this a toolbar button?
6936 */
6937 if (menu->submenu_id == (HMENU)-1)
6938 {
6939 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6940 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6941 }
6942 else
6943#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006944 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6945 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947#ifdef FEAT_TEAROFF
6948 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6949 {
6950 WORD menuID;
6951 HWND menuHandle;
6952
6953 /*
6954 * A tearoff button has changed state.
6955 */
6956 if (menu->children == NULL)
6957 menuID = (WORD)(menu->id);
6958 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006959 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6961 if (menuHandle)
6962 EnableWindow(menuHandle, !grey);
6963
6964 }
6965#endif
6966}
6967
6968#endif /* FEAT_MENU */
6969
6970
6971/* define some macros used to make the dialogue creation more readable */
6972
6973#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6974#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006975#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6978/*
6979 * stuff for dialogs
6980 */
6981
6982/*
6983 * The callback routine used by all the dialogs. Very simple. First,
6984 * acknowledges the INITDIALOG message so that Windows knows to do standard
6985 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6986 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6987 * number.
6988 */
6989 static LRESULT CALLBACK
6990dialog_callback(
6991 HWND hwnd,
6992 UINT message,
6993 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006994 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
6996 if (message == WM_INITDIALOG)
6997 {
6998 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6999 /* Set focus to the dialog. Set the default button, if specified. */
7000 (void)SetFocus(hwnd);
7001 if (dialog_default_button > IDCANCEL)
7002 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007003 else
7004 /* We don't have a default, set focus on another element of the
7005 * dialog window, probably the icon */
7006 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 return FALSE;
7008 }
7009
7010 if (message == WM_COMMAND)
7011 {
7012 int button = LOWORD(wParam);
7013
7014 /* Don't end the dialog if something was selected that was
7015 * not a button.
7016 */
7017 if (button >= DLG_NONBUTTON_CONTROL)
7018 return TRUE;
7019
7020 /* If the edit box exists, copy the string. */
7021 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007022 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007023# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007024 /* If the OS is Windows NT, and 'encoding' differs from active
7025 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007026 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007027 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007028 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7029 char_u *p;
7030
7031 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7032 p = utf16_to_enc(wp, NULL);
7033 vim_strncpy(s_textfield, p, IOSIZE);
7034 vim_free(p);
7035 vim_free(wp);
7036 }
7037 else
7038# endif
7039 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007040 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
7043 /*
7044 * Need to check for IDOK because if the user just hits Return to
7045 * accept the default value, some reason this is what we get.
7046 */
7047 if (button == IDOK)
7048 {
7049 if (dialog_default_button > IDCANCEL)
7050 EndDialog(hwnd, dialog_default_button);
7051 }
7052 else
7053 EndDialog(hwnd, button - IDCANCEL);
7054 return TRUE;
7055 }
7056
7057 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7058 {
7059 EndDialog(hwnd, 0);
7060 return TRUE;
7061 }
7062 return FALSE;
7063}
7064
7065/*
7066 * Create a dialog dynamically from the parameter strings.
7067 * type = type of dialog (question, alert, etc.)
7068 * title = dialog title. may be NULL for default title.
7069 * message = text to display. Dialog sizes to accommodate it.
7070 * buttons = '\n' separated list of button captions, default first.
7071 * dfltbutton = number of default button.
7072 *
7073 * This routine returns 1 if the first button is pressed,
7074 * 2 for the second, etc.
7075 *
7076 * 0 indicates Esc was pressed.
7077 * -1 for unexpected error
7078 *
7079 * If stubbing out this fn, return 1.
7080 */
7081
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007082static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
7084 "IDR_VIM",
7085 "IDR_VIM_ERROR",
7086 "IDR_VIM_ALERT",
7087 "IDR_VIM_INFO",
7088 "IDR_VIM_QUESTION"
7089};
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 int
7092gui_mch_dialog(
7093 int type,
7094 char_u *title,
7095 char_u *message,
7096 char_u *buttons,
7097 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007098 char_u *textfield,
7099 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007102 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 int numButtons;
7104 int *buttonWidths, *buttonPositions;
7105 int buttonYpos;
7106 int nchar, i;
7107 DWORD lStyle;
7108 int dlgwidth = 0;
7109 int dlgheight;
7110 int editboxheight;
7111 int horizWidth = 0;
7112 int msgheight;
7113 char_u *pstart;
7114 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007115 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 char_u *tbuffer;
7117 RECT rect;
7118 HWND hwnd;
7119 HDC hdc;
7120 HFONT font, oldFont;
7121 TEXTMETRIC fontInfo;
7122 int fontHeight;
7123 int textWidth, minButtonWidth, messageWidth;
7124 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007125 int maxDialogHeight;
7126 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 int vertical;
7128 int dlgPaddingX;
7129 int dlgPaddingY;
7130#ifdef USE_SYSMENU_FONT
7131 LOGFONT lfSysmenu;
7132 int use_lfSysmenu = FALSE;
7133#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007134 garray_T ga;
7135 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136
7137#ifndef NO_CONSOLE
7138 /* Don't output anything in silent mode ("ex -s") */
7139 if (silent_mode)
7140 return dfltbutton; /* return default option */
7141#endif
7142
Bram Moolenaar748bf032005-02-02 23:04:36 +00007143 if (s_hwnd == NULL)
7144 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 if ((type < 0) || (type > VIM_LAST_TYPE))
7147 type = 0;
7148
7149 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007150 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007151 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007152 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 if (p == NULL)
7155 return -1;
7156
7157 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007158 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7160 * const.
7161 */
7162 tbuffer = vim_strsave(buttons);
7163 if (tbuffer == NULL)
7164 return -1;
7165
7166 --dfltbutton; /* Change from one-based to zero-based */
7167
7168 /* Count buttons */
7169 numButtons = 1;
7170 for (i = 0; tbuffer[i] != '\0'; i++)
7171 {
7172 if (tbuffer[i] == DLG_BUTTON_SEP)
7173 numButtons++;
7174 }
7175 if (dfltbutton >= numButtons)
7176 dfltbutton = -1;
7177
7178 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007179 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (buttonWidths == NULL)
7181 return -1;
7182
7183 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007184 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (buttonPositions == NULL)
7186 return -1;
7187
7188 /*
7189 * Calculate how big the dialog must be.
7190 */
7191 hwnd = GetDesktopWindow();
7192 hdc = GetWindowDC(hwnd);
7193#ifdef USE_SYSMENU_FONT
7194 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7195 {
7196 font = CreateFontIndirect(&lfSysmenu);
7197 use_lfSysmenu = TRUE;
7198 }
7199 else
7200#endif
7201 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7202 VARIABLE_PITCH , DLG_FONT_NAME);
7203 if (s_usenewlook)
7204 {
7205 oldFont = SelectFont(hdc, font);
7206 dlgPaddingX = DLG_PADDING_X;
7207 dlgPaddingY = DLG_PADDING_Y;
7208 }
7209 else
7210 {
7211 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7212 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7213 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7214 }
7215 GetTextMetrics(hdc, &fontInfo);
7216 fontHeight = fontInfo.tmHeight;
7217
7218 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007219 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007222 if (s_hwnd == NULL)
7223 {
7224 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaarc716c302006-01-21 22:12:51 +00007226 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007227 get_work_area(&workarea_rect);
7228 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7229 if (maxDialogWidth > 600)
7230 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007231 /* Leave some room for the taskbar. */
7232 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007233 }
7234 else
7235 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007236 /* Use our own window for the size, unless it's very small. */
7237 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007238 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007239 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007240 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007241 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7242 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007243
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007244 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007245 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007246 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007247 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007248 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7249 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7250 }
7251
7252 /* Set dlgwidth to width of message.
7253 * Copy the message into "ga", changing NL to CR-NL and inserting line
7254 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 pstart = message;
7256 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007257 msgheight = 0;
7258 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 do
7260 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007261 msgheight += fontHeight; /* at least one line */
7262
7263 /* Need to figure out where to break the string. The system does it
7264 * at a word boundary, which would mean we can't compute the number of
7265 * wrapped lines. */
7266 textWidth = 0;
7267 last_white = NULL;
7268 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007269 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007270#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007271 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007272#else
7273 l = 1;
7274#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007275 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007276 && textWidth > maxDialogWidth * 3 / 4)
7277 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007278 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007279 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007280 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007281 /* Line will wrap. */
7282 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007283 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007284 textWidth = 0;
7285
7286 if (last_white != NULL)
7287 {
7288 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007290 pend = last_white + 1;
7291 last_white = NULL;
7292 }
7293 ga_append(&ga, '\r');
7294 ga_append(&ga, '\n');
7295 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007296 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007297
7298 while (--l >= 0)
7299 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007300 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007301 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007303
7304 ga_append(&ga, '\r');
7305 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 pstart = pend + 1;
7307 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007308
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007309 if (ga.ga_data != NULL)
7310 message = ga.ga_data;
7311
Bram Moolenaar748bf032005-02-02 23:04:36 +00007312 messageWidth += 10; /* roundoff space */
7313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007315 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7316 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318 if (msgheight < DLG_ICON_HEIGHT)
7319 msgheight = DLG_ICON_HEIGHT;
7320
7321 /*
7322 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007323 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007325 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (!vertical)
7327 {
7328 // Place buttons horizontally if they fit.
7329 horizWidth = dlgPaddingX;
7330 pstart = tbuffer;
7331 i = 0;
7332 do
7333 {
7334 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7335 if (pend == NULL)
7336 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007337 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (textWidth < minButtonWidth)
7339 textWidth = minButtonWidth;
7340 textWidth += dlgPaddingX; /* Padding within button */
7341 buttonWidths[i] = textWidth;
7342 buttonPositions[i++] = horizWidth;
7343 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7344 pstart = pend + 1;
7345 } while (*pend != NUL);
7346
7347 if (horizWidth > maxDialogWidth)
7348 vertical = TRUE; // Too wide to fit on the screen.
7349 else if (horizWidth > dlgwidth)
7350 dlgwidth = horizWidth;
7351 }
7352
7353 if (vertical)
7354 {
7355 // Stack buttons vertically.
7356 pstart = tbuffer;
7357 do
7358 {
7359 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7360 if (pend == NULL)
7361 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007362 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 textWidth += dlgPaddingX; /* Padding within button */
7364 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7365 if (textWidth > dlgwidth)
7366 dlgwidth = textWidth;
7367 pstart = pend + 1;
7368 } while (*pend != NUL);
7369 }
7370
7371 if (dlgwidth < DLG_MIN_WIDTH)
7372 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7373
7374 /* start to fill in the dlgtemplate information. addressing by WORDs */
7375 if (s_usenewlook)
7376 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7377 else
7378 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7379
7380 add_long(lStyle);
7381 add_long(0); // (lExtendedStyle)
7382 pnumitems = p; /*save where the number of items must be stored*/
7383 add_word(0); // NumberOfItems(will change later)
7384 add_word(10); // x
7385 add_word(10); // y
7386 add_word(PixelToDialogX(dlgwidth)); // cx
7387
7388 // Dialog height.
7389 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007390 dlgheight = msgheight + 2 * dlgPaddingY
7391 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 else
7393 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7394
7395 // Dialog needs to be taller if contains an edit box.
7396 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7397 if (textfield != NULL)
7398 dlgheight += editboxheight;
7399
Bram Moolenaara95d8232013-08-07 15:27:11 +02007400 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7401 if (dlgheight > maxDialogHeight)
7402 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007403 msgheight = msgheight - (dlgheight - maxDialogHeight);
7404 dlgheight = maxDialogHeight;
7405 scroll_flag = WS_VSCROLL;
7406 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7407 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007408 }
7409
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 add_word(PixelToDialogY(dlgheight));
7411
7412 add_word(0); // Menu
7413 add_word(0); // Class
7414
7415 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007416 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7417 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 p += nchar;
7419
7420 if (s_usenewlook)
7421 {
7422 /* do the font, since DS_3DLOOK doesn't work properly */
7423#ifdef USE_SYSMENU_FONT
7424 if (use_lfSysmenu)
7425 {
7426 /* point size */
7427 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7428 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007429 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431 else
7432#endif
7433 {
7434 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007435 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
7437 p += nchar;
7438 }
7439
7440 buttonYpos = msgheight + 2 * dlgPaddingY;
7441
7442 if (textfield != NULL)
7443 buttonYpos += editboxheight;
7444
7445 pstart = tbuffer;
7446 if (!vertical)
7447 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7448 for (i = 0; i < numButtons; i++)
7449 {
7450 /* get end of this button. */
7451 for ( pend = pstart;
7452 *pend && (*pend != DLG_BUTTON_SEP);
7453 pend++)
7454 ;
7455
7456 if (*pend)
7457 *pend = '\0';
7458
7459 /*
7460 * old NOTE:
7461 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7462 * the focus to the first tab-able button and in so doing makes that
7463 * the default!! Grrr. Workaround: Make the default button the only
7464 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7465 * he/she can use arrow keys.
7466 *
7467 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007468 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 * dialog. Also needed for when the textfield is the default control.
7470 * It appears to work now (perhaps not on Win95?).
7471 */
7472 if (vertical)
7473 {
7474 p = add_dialog_element(p,
7475 (i == dfltbutton
7476 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7477 PixelToDialogX(DLG_VERT_PADDING_X),
7478 PixelToDialogY(buttonYpos /* TBK */
7479 + 2 * fontHeight * i),
7480 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7481 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007482 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484 else
7485 {
7486 p = add_dialog_element(p,
7487 (i == dfltbutton
7488 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7489 PixelToDialogX(horizWidth + buttonPositions[i]),
7490 PixelToDialogY(buttonYpos), /* TBK */
7491 PixelToDialogX(buttonWidths[i]),
7492 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007493 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
7495 pstart = pend + 1; /*next button*/
7496 }
7497 *pnumitems += numButtons;
7498
7499 /* Vim icon */
7500 p = add_dialog_element(p, SS_ICON,
7501 PixelToDialogX(dlgPaddingX),
7502 PixelToDialogY(dlgPaddingY),
7503 PixelToDialogX(DLG_ICON_WIDTH),
7504 PixelToDialogY(DLG_ICON_HEIGHT),
7505 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7506 dlg_icons[type]);
7507
Bram Moolenaar748bf032005-02-02 23:04:36 +00007508 /* Dialog message */
7509 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7510 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7511 PixelToDialogY(dlgPaddingY),
7512 (WORD)(PixelToDialogX(messageWidth) + 1),
7513 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007514 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
7516 /* Edit box */
7517 if (textfield != NULL)
7518 {
7519 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7520 PixelToDialogX(2 * dlgPaddingX),
7521 PixelToDialogY(2 * dlgPaddingY + msgheight),
7522 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7523 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007524 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 *pnumitems += 1;
7526 }
7527
7528 *pnumitems += 2;
7529
7530 SelectFont(hdc, oldFont);
7531 DeleteObject(font);
7532 ReleaseDC(hwnd, hdc);
7533
7534 /* Let the dialog_callback() function know which button to make default
7535 * If we have an edit box, make that the default. We also need to tell
7536 * dialog_callback() if this dialog contains an edit box or not. We do
7537 * this by setting s_textfield if it does.
7538 */
7539 if (textfield != NULL)
7540 {
7541 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7542 s_textfield = textfield;
7543 }
7544 else
7545 {
7546 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7547 s_textfield = NULL;
7548 }
7549
7550 /* show the dialog box modally and get a return value */
7551 nchar = (int)DialogBoxIndirect(
7552 s_hinst,
7553 (LPDLGTEMPLATE)pdlgtemplate,
7554 s_hwnd,
7555 (DLGPROC)dialog_callback);
7556
7557 LocalFree(LocalHandle(pdlgtemplate));
7558 vim_free(tbuffer);
7559 vim_free(buttonWidths);
7560 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007561 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
7563 /* Focus back to our window (for when MDI is used). */
7564 (void)SetFocus(s_hwnd);
7565
7566 return nchar;
7567}
7568
7569#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007570
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571/*
7572 * Put a simple element (basic class) onto a dialog template in memory.
7573 * return a pointer to where the next item should be added.
7574 *
7575 * parameters:
7576 * lStyle = additional style flags
7577 * (be careful, NT3.51 & Win32s will ignore the new ones)
7578 * x,y = x & y positions IN DIALOG UNITS
7579 * w,h = width and height IN DIALOG UNITS
7580 * Id = ID used in messages
7581 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7582 * caption = usually text or resource name
7583 *
7584 * TODO: use the length information noted here to enable the dialog creation
7585 * routines to work out more exactly how much memory they need to alloc.
7586 */
7587 static PWORD
7588add_dialog_element(
7589 PWORD p,
7590 DWORD lStyle,
7591 WORD x,
7592 WORD y,
7593 WORD w,
7594 WORD h,
7595 WORD Id,
7596 WORD clss,
7597 const char *caption)
7598{
7599 int nchar;
7600
7601 p = lpwAlign(p); /* Align to dword boundary*/
7602 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7603 *p++ = LOWORD(lStyle);
7604 *p++ = HIWORD(lStyle);
7605 *p++ = 0; // LOWORD (lExtendedStyle)
7606 *p++ = 0; // HIWORD (lExtendedStyle)
7607 *p++ = x;
7608 *p++ = y;
7609 *p++ = w;
7610 *p++ = h;
7611 *p++ = Id; //9 or 10 words in all
7612
7613 *p++ = (WORD)0xffff;
7614 *p++ = clss; //2 more here
7615
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007616 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 p += nchar;
7618
7619 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7620
7621 return p; //total = 15+ (strlen(caption)) words
7622 // = 30 + 2(strlen(caption) bytes reqd
7623}
7624
7625
7626/*
7627 * Helper routine. Take an input pointer, return closest pointer that is
7628 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7629 */
7630 static LPWORD
7631lpwAlign(
7632 LPWORD lpIn)
7633{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007634 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007636 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 ul += 3;
7638 ul >>= 2;
7639 ul <<= 2;
7640 return (LPWORD)ul;
7641}
7642
7643/*
7644 * Helper routine. Takes second parameter as Ansi string, copies it to first
7645 * parameter as wide character (16-bits / char) string, and returns integer
7646 * number of wide characters (words) in string (including the trailing wide
7647 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007648 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7649 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 static int
7651nCopyAnsiToWideChar(
7652 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007653 LPSTR lpAnsiIn,
7654 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656 int nChar = 0;
7657#ifdef FEAT_MBYTE
7658 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7659 int i;
7660 WCHAR *wn;
7661
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007662 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {
7664 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007665 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (wn != NULL)
7667 {
7668 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007669 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 vim_free(wn);
7671 }
7672 }
7673 if (nChar == 0)
7674 /* Use Win32 conversion function. */
7675 nChar = MultiByteToWideChar(
7676 enc_codepage > 0 ? enc_codepage : CP_ACP,
7677 MB_PRECOMPOSED,
7678 lpAnsiIn, len,
7679 lpWCStr, len);
7680 for (i = 0; i < nChar; ++i)
7681 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7682 lpWCStr[i] = (WORD)' ';
7683#else
7684 do
7685 {
7686 if (*lpAnsiIn == '\t')
7687 *lpWCStr++ = (WORD)' ';
7688 else
7689 *lpWCStr++ = (WORD)*lpAnsiIn;
7690 nChar++;
7691 } while (*lpAnsiIn++);
7692#endif
7693
7694 return nChar;
7695}
7696
7697
7698#ifdef FEAT_TEAROFF
7699/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007700 * Lookup menu handle from "menu_id".
7701 */
7702 static HMENU
7703tearoff_lookup_menuhandle(
7704 vimmenu_T *menu,
7705 WORD menu_id)
7706{
7707 for ( ; menu != NULL; menu = menu->next)
7708 {
7709 if (menu->modes == 0) /* this menu has just been deleted */
7710 continue;
7711 if (menu_is_separator(menu->dname))
7712 continue;
7713 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7714 return menu->submenu_id;
7715 }
7716 return NULL;
7717}
7718
7719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 * The callback function for all the modeless dialogs that make up the
7721 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7722 * thinking its menus have been clicked), and go away when closed.
7723 */
7724 static LRESULT CALLBACK
7725tearoff_callback(
7726 HWND hwnd,
7727 UINT message,
7728 WPARAM wParam,
7729 LPARAM lParam)
7730{
7731 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007732 {
7733 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
7737 /* May show the mouse pointer again. */
7738 HandleMouseHide(message, lParam);
7739
7740 if (message == WM_COMMAND)
7741 {
7742 if ((WORD)(LOWORD(wParam)) & 0x8000)
7743 {
7744 POINT mp;
7745 RECT rect;
7746
7747 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7748 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007749 vimmenu_T *menu;
7750
7751 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007753 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7755 (int)rect.right - 8,
7756 (int)mp.y,
7757 (int)0, /*reserved param*/
7758 s_hwnd,
7759 NULL);
7760 /*
7761 * NOTE: The pop-up menu can eat the mouse up event.
7762 * We deal with this in normal.c.
7763 */
7764 }
7765 }
7766 else
7767 /* Pass on messages to the main Vim window */
7768 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7769 /*
7770 * Give main window the focus back: this is so after
7771 * choosing a tearoff button you can start typing again
7772 * straight away.
7773 */
7774 (void)SetFocus(s_hwnd);
7775 return TRUE;
7776 }
7777 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7778 {
7779 DestroyWindow(hwnd);
7780 return TRUE;
7781 }
7782
7783 /* When moved around, give main window the focus back. */
7784 if (message == WM_EXITSIZEMOVE)
7785 (void)SetActiveWindow(s_hwnd);
7786
7787 return FALSE;
7788}
7789#endif
7790
7791
7792/*
7793 * Decide whether to use the "new look" (small, non-bold font) or the "old
7794 * look" (big, clanky font) for dialogs, and work out a few values for use
7795 * later accordingly.
7796 */
7797 static void
7798get_dialog_font_metrics(void)
7799{
7800 HDC hdc;
7801 HFONT hfontTools = 0;
7802 DWORD dlgFontSize;
7803 SIZE size;
7804#ifdef USE_SYSMENU_FONT
7805 LOGFONT lfSysmenu;
7806#endif
7807
7808 s_usenewlook = FALSE;
7809
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007811 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7812 hfontTools = CreateFontIndirect(&lfSysmenu);
7813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814#endif
7815 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7816 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7817
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007818 if (hfontTools)
7819 {
7820 hdc = GetDC(s_hwnd);
7821 SelectObject(hdc, hfontTools);
7822 /*
7823 * GetTextMetrics() doesn't return the right value in
7824 * tmAveCharWidth, so we have to figure out the dialog base units
7825 * ourselves.
7826 */
7827 GetTextExtentPoint(hdc,
7828 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7829 52, &size);
7830 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007832 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7833 s_dlgfntheight = (WORD)size.cy;
7834 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 }
7836
7837 if (!s_usenewlook)
7838 {
7839 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7840 s_dlgfntwidth = LOWORD(dlgFontSize);
7841 s_dlgfntheight = HIWORD(dlgFontSize);
7842 }
7843}
7844
7845#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7846/*
7847 * Create a pseudo-"tearoff menu" based on the child
7848 * items of a given menu pointer.
7849 */
7850 static void
7851gui_mch_tearoff(
7852 char_u *title,
7853 vimmenu_T *menu,
7854 int initX,
7855 int initY)
7856{
7857 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7858 int template_len;
7859 int nchar, textWidth, submenuWidth;
7860 DWORD lStyle;
7861 DWORD lExtendedStyle;
7862 WORD dlgwidth;
7863 WORD menuID;
7864 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007865 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 vimmenu_T *the_menu = menu;
7867 HWND hwnd;
7868 HDC hdc;
7869 HFONT font, oldFont;
7870 int col, spaceWidth, len;
7871 int columnWidths[2];
7872 char_u *label, *text;
7873 int acLen = 0;
7874 int nameLen;
7875 int padding0, padding1, padding2 = 0;
7876 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007877 int x;
7878 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#ifdef USE_SYSMENU_FONT
7880 LOGFONT lfSysmenu;
7881 int use_lfSysmenu = FALSE;
7882#endif
7883
7884 /*
7885 * If this menu is already torn off, move it to the mouse position.
7886 */
7887 if (IsWindow(menu->tearoff_handle))
7888 {
7889 POINT mp;
7890 if (GetCursorPos((LPPOINT)&mp))
7891 {
7892 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7893 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7894 }
7895 return;
7896 }
7897
7898 /*
7899 * Create a new tearoff.
7900 */
7901 if (*title == MNU_HIDDEN_CHAR)
7902 title++;
7903
7904 /* Allocate memory to store the dialog template. It's made bigger when
7905 * needed. */
7906 template_len = DLG_ALLOC_SIZE;
7907 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7908 if (p == NULL)
7909 return;
7910
7911 hwnd = GetDesktopWindow();
7912 hdc = GetWindowDC(hwnd);
7913#ifdef USE_SYSMENU_FONT
7914 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7915 {
7916 font = CreateFontIndirect(&lfSysmenu);
7917 use_lfSysmenu = TRUE;
7918 }
7919 else
7920#endif
7921 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7922 VARIABLE_PITCH , DLG_FONT_NAME);
7923 if (s_usenewlook)
7924 oldFont = SelectFont(hdc, font);
7925 else
7926 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7927
7928 /* Calculate width of a single space. Used for padding columns to the
7929 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007930 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932 /* Figure out max width of the text column, the accelerator column and the
7933 * optional submenu column. */
7934 submenuWidth = 0;
7935 for (col = 0; col < 2; col++)
7936 {
7937 columnWidths[col] = 0;
7938 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7939 {
7940 /* Use "dname" here to compute the width of the visible text. */
7941 text = (col == 0) ? pmenu->dname : pmenu->actext;
7942 if (text != NULL && *text != NUL)
7943 {
7944 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7945 if (textWidth > columnWidths[col])
7946 columnWidths[col] = textWidth;
7947 }
7948 if (pmenu->children != NULL)
7949 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7950 }
7951 }
7952 if (columnWidths[1] == 0)
7953 {
7954 /* no accelerators */
7955 if (submenuWidth != 0)
7956 columnWidths[0] += submenuWidth;
7957 else
7958 columnWidths[0] += spaceWidth;
7959 }
7960 else
7961 {
7962 /* there is an accelerator column */
7963 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7964 columnWidths[1] += submenuWidth;
7965 }
7966
7967 /*
7968 * Now find the total width of our 'menu'.
7969 */
7970 textWidth = columnWidths[0] + columnWidths[1];
7971 if (submenuWidth != 0)
7972 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007973 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7975 textWidth += submenuWidth;
7976 }
7977 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7978 if (textWidth > dlgwidth)
7979 dlgwidth = textWidth;
7980 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7981
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 /* start to fill in the dlgtemplate information. addressing by WORDs */
7983 if (s_usenewlook)
7984 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7985 else
7986 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7987
7988 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7989 *p++ = LOWORD(lStyle);
7990 *p++ = HIWORD(lStyle);
7991 *p++ = LOWORD(lExtendedStyle);
7992 *p++ = HIWORD(lExtendedStyle);
7993 pnumitems = p; /* save where the number of items must be stored */
7994 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007995 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007997 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 else
7999 *p++ = PixelToDialogX(initX); // x
8000 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008001 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 else
8003 *p++ = PixelToDialogY(initY); // y
8004 *p++ = PixelToDialogX(dlgwidth); // cx
8005 ptrueheight = p;
8006 *p++ = 0; // dialog height: changed later anyway
8007 *p++ = 0; // Menu
8008 *p++ = 0; // Class
8009
8010 /* copy the title of the dialog */
8011 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008012 ? (LPSTR)title
8013 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 p += nchar;
8015
8016 if (s_usenewlook)
8017 {
8018 /* do the font, since DS_3DLOOK doesn't work properly */
8019#ifdef USE_SYSMENU_FONT
8020 if (use_lfSysmenu)
8021 {
8022 /* point size */
8023 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8024 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008025 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027 else
8028#endif
8029 {
8030 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008031 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033 p += nchar;
8034 }
8035
8036 /*
8037 * Loop over all the items in the menu.
8038 * But skip over the tearbar.
8039 */
8040 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8041 menu = menu->children->next;
8042 else
8043 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008044 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 for ( ; menu != NULL; menu = menu->next)
8046 {
8047 if (menu->modes == 0) /* this menu has just been deleted */
8048 continue;
8049 if (menu_is_separator(menu->dname))
8050 {
8051 sepPadding += 3;
8052 continue;
8053 }
8054
8055 /* Check if there still is plenty of room in the template. Make it
8056 * larger when needed. */
8057 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8058 {
8059 WORD *newp;
8060
8061 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8062 if (newp != NULL)
8063 {
8064 template_len += 4096;
8065 mch_memmove(newp, pdlgtemplate,
8066 (char *)p - (char *)pdlgtemplate);
8067 p = newp + (p - pdlgtemplate);
8068 pnumitems = newp + (pnumitems - pdlgtemplate);
8069 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8070 LocalFree(LocalHandle(pdlgtemplate));
8071 pdlgtemplate = newp;
8072 }
8073 }
8074
8075 /* Figure out minimal length of this menu label. Use "name" for the
8076 * actual text, "dname" for estimating the displayed size. "name"
8077 * has "&a" for mnemonic and includes the accelerator. */
8078 len = nameLen = (int)STRLEN(menu->name);
8079 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8080 (int)STRLEN(menu->dname))) / spaceWidth;
8081 len += padding0;
8082
8083 if (menu->actext != NULL)
8084 {
8085 acLen = (int)STRLEN(menu->actext);
8086 len += acLen;
8087 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8088 }
8089 else
8090 textWidth = 0;
8091 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8092 len += padding1;
8093
8094 if (menu->children == NULL)
8095 {
8096 padding2 = submenuWidth / spaceWidth;
8097 len += padding2;
8098 menuID = (WORD)(menu->id);
8099 }
8100 else
8101 {
8102 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008103 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 }
8105
8106 /* Allocate menu label and fill it in */
8107 text = label = alloc((unsigned)len + 1);
8108 if (label == NULL)
8109 break;
8110
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008111 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8113 if (text == NULL)
8114 text = label + nameLen; /* no actext, use whole name */
8115 while (padding0-- > 0)
8116 *text++ = ' ';
8117 if (menu->actext != NULL)
8118 {
8119 STRNCPY(text, menu->actext, acLen);
8120 text += acLen;
8121 }
8122 while (padding1-- > 0)
8123 *text++ = ' ';
8124 if (menu->children != NULL)
8125 {
8126 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8127 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8128 }
8129 else
8130 {
8131 while (padding2-- > 0)
8132 *text++ = ' ';
8133 }
8134 *text = NUL;
8135
8136 /*
8137 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8138 * W95/NT4 it makes the tear-off look more like a menu.
8139 */
8140 p = add_dialog_element(p,
8141 BS_PUSHBUTTON|BS_LEFT,
8142 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8143 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8144 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8145 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008146 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 vim_free(label);
8148 (*pnumitems)++;
8149 }
8150
8151 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8152
8153
8154 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008155 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 s_hinst,
8157 (LPDLGTEMPLATE)pdlgtemplate,
8158 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008159 (DLGPROC)tearoff_callback,
8160 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
8162 LocalFree(LocalHandle(pdlgtemplate));
8163 SelectFont(hdc, oldFont);
8164 DeleteObject(font);
8165 ReleaseDC(hwnd, hdc);
8166
8167 /*
8168 * Reassert ourselves as the active window. This is so that after creating
8169 * a tearoff, the user doesn't have to click with the mouse just to start
8170 * typing again!
8171 */
8172 (void)SetActiveWindow(s_hwnd);
8173
8174 /* make sure the right buttons are enabled */
8175 force_menu_update = TRUE;
8176}
8177#endif
8178
8179#if defined(FEAT_TOOLBAR) || defined(PROTO)
8180#include "gui_w32_rc.h"
8181
8182/* This not defined in older SDKs */
8183# ifndef TBSTYLE_FLAT
8184# define TBSTYLE_FLAT 0x0800
8185# endif
8186
8187/*
8188 * Create the toolbar, initially unpopulated.
8189 * (just like the menu, there are no defaults, it's all
8190 * set up through menu.vim)
8191 */
8192 static void
8193initialise_toolbar(void)
8194{
8195 InitCommonControls();
8196 s_toolbarhwnd = CreateToolbarEx(
8197 s_hwnd,
8198 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8199 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008200 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 s_hinst,
8202 IDR_TOOLBAR1, // id of initial bitmap
8203 NULL,
8204 0, // initial number of buttons
8205 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8206 TOOLBAR_BUTTON_HEIGHT,
8207 TOOLBAR_BUTTON_WIDTH,
8208 TOOLBAR_BUTTON_HEIGHT,
8209 sizeof(TBBUTTON)
8210 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008211 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
8213 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8214}
8215
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008216 static LRESULT CALLBACK
8217toolbar_wndproc(
8218 HWND hwnd,
8219 UINT uMsg,
8220 WPARAM wParam,
8221 LPARAM lParam)
8222{
8223 HandleMouseHide(uMsg, lParam);
8224 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8225}
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 static int
8228get_toolbar_bitmap(vimmenu_T *menu)
8229{
8230 int i = -1;
8231
8232 /*
8233 * Check user bitmaps first, unless builtin is specified.
8234 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008235 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
8237 char_u fname[MAXPATHL];
8238 HANDLE hbitmap = NULL;
8239
8240 if (menu->iconfile != NULL)
8241 {
8242 gui_find_iconfile(menu->iconfile, fname, "bmp");
8243 hbitmap = LoadImage(
8244 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008245 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 IMAGE_BITMAP,
8247 TOOLBAR_BUTTON_WIDTH,
8248 TOOLBAR_BUTTON_HEIGHT,
8249 LR_LOADFROMFILE |
8250 LR_LOADMAP3DCOLORS
8251 );
8252 }
8253
8254 /*
8255 * If the LoadImage call failed, or the "icon=" file
8256 * didn't exist or wasn't specified, try the menu name
8257 */
8258 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008259 && (gui_find_bitmap(
8260#ifdef FEAT_MULTI_LANG
8261 menu->en_dname != NULL ? menu->en_dname :
8262#endif
8263 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 hbitmap = LoadImage(
8265 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008266 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 IMAGE_BITMAP,
8268 TOOLBAR_BUTTON_WIDTH,
8269 TOOLBAR_BUTTON_HEIGHT,
8270 LR_LOADFROMFILE |
8271 LR_LOADMAP3DCOLORS
8272 );
8273
8274 if (hbitmap != NULL)
8275 {
8276 TBADDBITMAP tbAddBitmap;
8277
8278 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008279 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
8281 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8282 (WPARAM)1, (LPARAM)&tbAddBitmap);
8283 /* i will be set to -1 if it fails */
8284 }
8285 }
8286 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8287 i = menu->iconidx;
8288
8289 return i;
8290}
8291#endif
8292
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008293#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8294 static void
8295initialise_tabline(void)
8296{
8297 InitCommonControls();
8298
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008299 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008300 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008301 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8302 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008303 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008304
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008305 gui.tabline_height = TABLINE_HEIGHT;
8306
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008307# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008308 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008309# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008310}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008311
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008312/*
8313 * Get tabpage_T from POINT.
8314 */
8315 static tabpage_T *
8316GetTabFromPoint(
8317 HWND hWnd,
8318 POINT pt)
8319{
8320 tabpage_T *ptp = NULL;
8321
8322 if (gui_mch_showing_tabline())
8323 {
8324 TCHITTESTINFO htinfo;
8325 htinfo.pt = pt;
8326 /* ignore if a window under cusor is not tabcontrol. */
8327 if (s_tabhwnd == hWnd)
8328 {
8329 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8330 if (idx != -1)
8331 ptp = find_tabpage(idx + 1);
8332 }
8333 }
8334 return ptp;
8335}
8336
8337static POINT s_pt = {0, 0};
8338static HCURSOR s_hCursor = NULL;
8339
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008340 static LRESULT CALLBACK
8341tabline_wndproc(
8342 HWND hwnd,
8343 UINT uMsg,
8344 WPARAM wParam,
8345 LPARAM lParam)
8346{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008347 POINT pt;
8348 tabpage_T *tp;
8349 RECT rect;
8350 int nCenter;
8351 int idx0;
8352 int idx1;
8353
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008354 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008355
8356 switch (uMsg)
8357 {
8358 case WM_LBUTTONDOWN:
8359 {
8360 s_pt.x = GET_X_LPARAM(lParam);
8361 s_pt.y = GET_Y_LPARAM(lParam);
8362 SetCapture(hwnd);
8363 s_hCursor = GetCursor(); /* backup default cursor */
8364 break;
8365 }
8366 case WM_MOUSEMOVE:
8367 if (GetCapture() == hwnd
8368 && ((wParam & MK_LBUTTON)) != 0)
8369 {
8370 pt.x = GET_X_LPARAM(lParam);
8371 pt.y = s_pt.y;
8372 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8373 {
8374 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8375
8376 tp = GetTabFromPoint(hwnd, pt);
8377 if (tp != NULL)
8378 {
8379 idx0 = tabpage_index(curtab) - 1;
8380 idx1 = tabpage_index(tp) - 1;
8381
8382 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8383 nCenter = rect.left + (rect.right - rect.left) / 2;
8384
8385 /* Check if the mouse cursor goes over the center of
8386 * the next tab to prevent "flickering". */
8387 if ((idx0 < idx1) && (nCenter < pt.x))
8388 {
8389 tabpage_move(idx1 + 1);
8390 update_screen(0);
8391 }
8392 else if ((idx1 < idx0) && (pt.x < nCenter))
8393 {
8394 tabpage_move(idx1);
8395 update_screen(0);
8396 }
8397 }
8398 }
8399 }
8400 break;
8401 case WM_LBUTTONUP:
8402 {
8403 if (GetCapture() == hwnd)
8404 {
8405 SetCursor(s_hCursor);
8406 ReleaseCapture();
8407 }
8408 break;
8409 }
8410 default:
8411 break;
8412 }
8413
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008414 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8415}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008416#endif
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8419/*
8420 * Make the GUI window come to the foreground.
8421 */
8422 void
8423gui_mch_set_foreground(void)
8424{
8425 if (IsIconic(s_hwnd))
8426 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8427 SetForegroundWindow(s_hwnd);
8428}
8429#endif
8430
8431#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8432 static void
8433dyn_imm_load(void)
8434{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008435 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (hLibImm == NULL)
8437 return;
8438
8439 pImmGetCompositionStringA
8440 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8441 pImmGetCompositionStringW
8442 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8443 pImmGetContext
8444 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8445 pImmAssociateContext
8446 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8447 pImmReleaseContext
8448 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8449 pImmGetOpenStatus
8450 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8451 pImmSetOpenStatus
8452 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8453 pImmGetCompositionFont
8454 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8455 pImmSetCompositionFont
8456 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8457 pImmSetCompositionWindow
8458 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8459 pImmGetConversionStatus
8460 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008461 pImmSetConversionStatus
8462 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
8464 if ( pImmGetCompositionStringA == NULL
8465 || pImmGetCompositionStringW == NULL
8466 || pImmGetContext == NULL
8467 || pImmAssociateContext == NULL
8468 || pImmReleaseContext == NULL
8469 || pImmGetOpenStatus == NULL
8470 || pImmSetOpenStatus == NULL
8471 || pImmGetCompositionFont == NULL
8472 || pImmSetCompositionFont == NULL
8473 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008474 || pImmGetConversionStatus == NULL
8475 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
8477 FreeLibrary(hLibImm);
8478 hLibImm = NULL;
8479 pImmGetContext = NULL;
8480 return;
8481 }
8482
8483 return;
8484}
8485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#endif
8487
8488#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8489
8490# ifdef FEAT_XPM_W32
8491# define IMAGE_XPM 100
8492# endif
8493
8494typedef struct _signicon_t
8495{
8496 HANDLE hImage;
8497 UINT uType;
8498#ifdef FEAT_XPM_W32
8499 HANDLE hShape; /* Mask bitmap handle */
8500#endif
8501} signicon_t;
8502
8503 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008504gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
8506 signicon_t *sign;
8507 int x, y, w, h;
8508
8509 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8510 return;
8511
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008512#if defined(FEAT_DIRECTX)
8513 if (IS_ENABLE_DIRECTX())
8514 DWriteContext_Flush(s_dwc);
8515#endif
8516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 x = TEXT_X(col);
8518 y = TEXT_Y(row);
8519 w = gui.char_width * 2;
8520 h = gui.char_height;
8521 switch (sign->uType)
8522 {
8523 case IMAGE_BITMAP:
8524 {
8525 HDC hdcMem;
8526 HBITMAP hbmpOld;
8527
8528 hdcMem = CreateCompatibleDC(s_hdc);
8529 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8530 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8531 SelectObject(hdcMem, hbmpOld);
8532 DeleteDC(hdcMem);
8533 }
8534 break;
8535 case IMAGE_ICON:
8536 case IMAGE_CURSOR:
8537 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8538 break;
8539#ifdef FEAT_XPM_W32
8540 case IMAGE_XPM:
8541 {
8542 HDC hdcMem;
8543 HBITMAP hbmpOld;
8544
8545 hdcMem = CreateCompatibleDC(s_hdc);
8546 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8547 /* Make hole */
8548 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8549
8550 SelectObject(hdcMem, sign->hImage);
8551 /* Paint sign */
8552 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8553 SelectObject(hdcMem, hbmpOld);
8554 DeleteDC(hdcMem);
8555 }
8556 break;
8557#endif
8558 }
8559}
8560
8561 static void
8562close_signicon_image(signicon_t *sign)
8563{
8564 if (sign)
8565 switch (sign->uType)
8566 {
8567 case IMAGE_BITMAP:
8568 DeleteObject((HGDIOBJ)sign->hImage);
8569 break;
8570 case IMAGE_CURSOR:
8571 DestroyCursor((HCURSOR)sign->hImage);
8572 break;
8573 case IMAGE_ICON:
8574 DestroyIcon((HICON)sign->hImage);
8575 break;
8576#ifdef FEAT_XPM_W32
8577 case IMAGE_XPM:
8578 DeleteObject((HBITMAP)sign->hImage);
8579 DeleteObject((HBITMAP)sign->hShape);
8580 break;
8581#endif
8582 }
8583}
8584
8585 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008586gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
8588 signicon_t sign, *psign;
8589 char_u *ext;
8590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008592 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (ext > signfile)
8594 {
8595 int do_load = 1;
8596
8597 if (!STRICMP(ext, ".bmp"))
8598 sign.uType = IMAGE_BITMAP;
8599 else if (!STRICMP(ext, ".ico"))
8600 sign.uType = IMAGE_ICON;
8601 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8602 sign.uType = IMAGE_CURSOR;
8603 else
8604 do_load = 0;
8605
8606 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008607 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 gui.char_width * 2, gui.char_height,
8609 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8610#ifdef FEAT_XPM_W32
8611 if (!STRICMP(ext, ".xpm"))
8612 {
8613 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008614 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8615 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 }
8617#endif
8618 }
8619
8620 psign = NULL;
8621 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8622 != NULL)
8623 *psign = sign;
8624
8625 if (!psign)
8626 {
8627 if (sign.hImage)
8628 close_signicon_image(&sign);
8629 EMSG(_(e_signdata));
8630 }
8631 return (void *)psign;
8632
8633}
8634
8635 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008636gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
8638 if (sign)
8639 {
8640 close_signicon_image((signicon_t *)sign);
8641 vim_free(sign);
8642 }
8643}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008646#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008649 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008651 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8653 * to get current mouse position).
8654 *
8655 * Trying to use as more Windows services as possible, and as less
8656 * IE version as possible :)).
8657 *
8658 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8659 * BalloonEval struct.
8660 * 2) Enable/Disable simply create/kill BalloonEval Timer
8661 * 3) When there was enough inactivity, timer procedure posts
8662 * async request to debugger
8663 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8664 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008665 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 */
8667
Bram Moolenaar45360022005-07-21 21:08:21 +00008668/*
8669 * determine whether installed Common Controls support multiline tooltips
8670 * (i.e. their version is >= 4.70
8671 */
8672 int
8673multiline_balloon_available(void)
8674{
8675 HINSTANCE hDll;
8676 static char comctl_dll[] = "comctl32.dll";
8677 static int multiline_tip = MAYBE;
8678
8679 if (multiline_tip != MAYBE)
8680 return multiline_tip;
8681
8682 hDll = GetModuleHandle(comctl_dll);
8683 if (hDll != NULL)
8684 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008685 DLLGETVERSIONPROC pGetVer;
8686 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008687
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008688 if (pGetVer != NULL)
8689 {
8690 DLLVERSIONINFO dvi;
8691 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008692
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008693 ZeroMemory(&dvi, sizeof(dvi));
8694 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008695
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008696 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008697
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008698 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008699 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008700 || (dvi.dwMajorVersion == 4
8701 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008702 {
8703 multiline_tip = TRUE;
8704 return multiline_tip;
8705 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008706 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008707 else
8708 {
8709 /* there is chance we have ancient CommCtl 4.70
8710 which doesn't export DllGetVersion */
8711 DWORD dwHandle = 0;
8712 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8713 if (len > 0)
8714 {
8715 VS_FIXEDFILEINFO *ver;
8716 UINT vlen = 0;
8717 void *data = alloc(len);
8718
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008719 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008720 && GetFileVersionInfo(comctl_dll, 0, len, data)
8721 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8722 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008723 && HIWORD(ver->dwFileVersionMS) > 4)
8724 || ((HIWORD(ver->dwFileVersionMS) == 4
8725 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008726 {
8727 vim_free(data);
8728 multiline_tip = TRUE;
8729 return multiline_tip;
8730 }
8731 vim_free(data);
8732 }
8733 }
8734 }
8735 multiline_tip = FALSE;
8736 return multiline_tip;
8737}
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008740make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
Bram Moolenaar45360022005-07-21 21:08:21 +00008742 TOOLINFO *pti;
8743 int ToolInfoSize;
8744
8745 if (multiline_balloon_available() == TRUE)
8746 ToolInfoSize = sizeof(TOOLINFO_NEW);
8747 else
8748 ToolInfoSize = sizeof(TOOLINFO);
8749
8750 pti = (TOOLINFO *)alloc(ToolInfoSize);
8751 if (pti == NULL)
8752 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753
8754 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8755 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8756 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8757 beval->target, NULL, s_hinst, NULL);
8758
8759 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8760 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8761
Bram Moolenaar45360022005-07-21 21:08:21 +00008762 pti->cbSize = ToolInfoSize;
8763 pti->uFlags = TTF_SUBCLASS;
8764 pti->hwnd = beval->target;
8765 pti->hinst = 0; /* Don't use string resources */
8766 pti->uId = ID_BEVAL_TOOLTIP;
8767
8768 if (multiline_balloon_available() == TRUE)
8769 {
8770 RECT rect;
8771 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8772 pti->lpszText = LPSTR_TEXTCALLBACK;
8773 ptin->lParam = (LPARAM)text;
8774 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8775 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8776 (LPARAM)rect.right);
8777 }
8778 else
8779 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
8781 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008782 pti->rect.left = pt.x - 3;
8783 pti->rect.top = pt.y - 3;
8784 pti->rect.right = pt.x + 3;
8785 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786
Bram Moolenaar45360022005-07-21 21:08:21 +00008787 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 /* Make tooltip appear sooner */
8789 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008790 /* I've performed some tests and it seems the longest possible life time
8791 * of tooltip is 30 seconds */
8792 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 /*
8794 * HACK: force tooltip to appear, because it'll not appear until
8795 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008796 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008798 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008800 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801}
8802
8803 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008804delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008806 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008810BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008811 HWND hwnd UNUSED,
8812 UINT uMsg UNUSED,
8813 UINT_PTR idEvent UNUSED,
8814 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 POINT pt;
8817 RECT rect;
8818
8819 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8820 return;
8821
8822 GetCursorPos(&pt);
8823 if (WindowFromPoint(pt) != s_textArea)
8824 return;
8825
8826 ScreenToClient(s_textArea, &pt);
8827 GetClientRect(s_textArea, &rect);
8828 if (!PtInRect(&rect, pt))
8829 return;
8830
8831 if (LastActivity > 0
8832 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8833 && (cur_beval->showState != ShS_PENDING
8834 || abs(cur_beval->x - pt.x) > 3
8835 || abs(cur_beval->y - pt.y) > 3))
8836 {
8837 /* Pointer resting in one place long enough, it's time to show
8838 * the tooltip. */
8839 cur_beval->showState = ShS_PENDING;
8840 cur_beval->x = pt.x;
8841 cur_beval->y = pt.y;
8842
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008843 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
8845 if (cur_beval->msgCB != NULL)
8846 (*cur_beval->msgCB)(cur_beval, 0);
8847 }
8848}
8849
8850 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008851gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008853 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008855 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856}
8857
8858 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008859gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008861 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (beval == NULL)
8863 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008864 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008865 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008866 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867}
8868
8869 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008870gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008873
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008874 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 if (beval->showState == ShS_SHOWING)
8876 return;
8877 GetCursorPos(&pt);
8878 ScreenToClient(s_textArea, &pt);
8879
8880 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008882 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 gui_mch_disable_beval_area(cur_beval);
8884 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008885 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008887 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008891gui_mch_create_beval_area(
8892 void *target, /* ignored, always use s_textArea */
8893 char_u *mesg,
8894 void (*mesgCB)(BalloonEval *, int),
8895 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 /* partially stolen from gui_beval.c */
8898 BalloonEval *beval;
8899
8900 if (mesg != NULL && mesgCB != NULL)
8901 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008902 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 return NULL;
8904 }
8905
8906 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8907 if (beval != NULL)
8908 {
8909 beval->target = s_textArea;
8910 beval->balloon = NULL;
8911
8912 beval->showState = ShS_NEUTRAL;
8913 beval->x = 0;
8914 beval->y = 0;
8915 beval->msg = mesg;
8916 beval->msgCB = mesgCB;
8917 beval->clientData = clientData;
8918
8919 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 cur_beval = beval;
8921
8922 if (p_beval)
8923 gui_mch_enable_beval_area(beval);
8924
8925 }
8926 return beval;
8927}
8928
8929 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008930Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8933 return;
8934
8935 if (cur_beval != NULL)
8936 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008937 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008939 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008940 // TRACE0("TTN_SHOW {{{");
8941 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008942 break;
8943 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008944 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 delete_tooltip(cur_beval);
8946 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008947 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008950 break;
8951 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008952 {
8953 /* if you get there then we have new common controls */
8954 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8955 info->lpszText = (LPSTR)info->lParam;
8956 info->uFlags |= TTF_DI_SETITEM;
8957 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008958 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 }
8960 }
8961}
8962
8963 static void
8964TrackUserActivity(UINT uMsg)
8965{
8966 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8967 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8968 LastActivity = GetTickCount();
8969}
8970
8971 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008972gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
8974 vim_free(beval);
8975}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008976#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8979/*
8980 * We have multiple signs to draw at the same location. Draw the
8981 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8982 */
8983 void
8984netbeans_draw_multisign_indicator(int row)
8985{
8986 int i;
8987 int y;
8988 int x;
8989
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008990 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008991 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008992
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 x = 0;
8994 y = TEXT_Y(row);
8995
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008996#if defined(FEAT_DIRECTX)
8997 if (IS_ENABLE_DIRECTX())
8998 DWriteContext_Flush(s_dwc);
8999#endif
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 for (i = 0; i < gui.char_height - 3; i++)
9002 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9003
9004 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9005 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9006 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9007 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9008 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9009 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9010 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9011}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009012#endif